-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add kvstore #1004
Merged
sameh-farouk
merged 5 commits into
development
from
development-add-kvstore-to-go-client
Sep 16, 2024
Merged
add kvstore #1004
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package substrate | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/centrifuge/go-substrate-rpc-client/v4/types" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func (s *Substrate) KVStoreSet(identity Identity, key string, value string) error { | ||
cl, meta, err := s.GetClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c, err := types.NewCall(meta, "TFKVStore.set", | ||
key, value, | ||
) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create call") | ||
} | ||
|
||
res, err := s.Call(cl, meta, identity, c) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create contract") | ||
} | ||
|
||
if err := s.checkForError(res); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Substrate) KVStoreDelete(identity Identity, key string) error { | ||
cl, meta, err := s.GetClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c, err := types.NewCall(meta, "TFKVStore.delete", | ||
key, | ||
) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create call") | ||
} | ||
|
||
res, err := s.Call(cl, meta, identity, c) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create contract") | ||
} | ||
|
||
if err := s.checkForError(res); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (s *Substrate) KVStoreGet(identity Identity, key string) ([]byte, error) { | ||
cl, meta, err := s.GetClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bytes, err := Encode(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
storageKey, err := types.CreateStorageKey(meta, "TFKVStore", "TFKVStore", identity.PublicKey(), bytes) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create substrate query key") | ||
} | ||
|
||
var value []byte | ||
ok, err := cl.RPC.State.GetStorageLatest(storageKey, &value) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to lookup entity") | ||
} | ||
|
||
if !ok { | ||
return nil, errors.Wrap(ErrNotFound, "key not found") | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
type Val struct { | ||
Id string | ||
Key string | ||
} | ||
|
||
func (s *Substrate) KVStoreList(identity Identity) (map[string]string, error) { | ||
cl, meta, err := s.GetClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
storageKey, err := types.CreateStorageKey(meta, "TFKVStore", "TFKVStore", identity.PublicKey()) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create substrate query key") | ||
} | ||
|
||
keys, err := cl.RPC.State.GetKeysLatest(storageKey) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to lookup entity") | ||
} | ||
|
||
query, err := cl.RPC.State.QueryStorageAtLatest(keys) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pairs := make(map[string]string) | ||
for _, q := range query { | ||
for _, c := range q.Changes { | ||
var key, val []byte | ||
|
||
key, err = decodeSecondKey(c.StorageKey, identity) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = Decode(c.StorageData, &val) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to decode value %v", string(c.StorageData)) | ||
} | ||
|
||
pairs[string(key)] = string(val) | ||
} | ||
} | ||
|
||
return pairs, nil | ||
} | ||
|
||
// decodeSecondKey extracts and decodes the second key(Vec<u8>) from the storage key. | ||
func decodeSecondKey(storageKey types.StorageKey, identity Identity) (key []byte, err error) { | ||
// remove 16 bytes(32 in hex) pallet and map prefixes. | ||
// pallet prefix (8 bytes): twox64(pallet_name) | ||
// map prefix (8bytes) twox64(map_name) | ||
prefixLen := 32 | ||
|
||
// the storage key contains two keys (AccountID and Vec<u8>) | ||
// remove the length of the first key(AccountID) | ||
// the hasher `Blake2_128Concat` includes a 16-byte hash followed by the AccountID | ||
firstKeyLen := 32 + len(identity.PublicKey()) | ||
|
||
offset := prefixLen + firstKeyLen | ||
|
||
if len(storageKey) < offset { | ||
return nil, errors.New(fmt.Sprintf("failed to decode second key, storage key len should not be less than %d bytes", offset)) | ||
} | ||
|
||
err = Decode(storageKey[offset:], &key) | ||
return key, err | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we move this function to utils.go ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package substrate | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestKVStore(t *testing.T) { | ||
pairs := map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"} | ||
|
||
// Alice identity | ||
id, err := NewIdentityFromSr25519Phrase("//Alice") | ||
require.NoError(t, err) | ||
|
||
sub := startLocalConnection(t) | ||
defer sub.Close() | ||
|
||
t.Run("kvstore set values", func(t *testing.T) { | ||
for key, val := range pairs { | ||
err = sub.KVStoreSet(id, key, val) | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
|
||
t.Run("kvstore get values", func(t *testing.T) { | ||
for key, val := range pairs { | ||
value, err := sub.KVStoreGet(id, key) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte(val), value) | ||
} | ||
}) | ||
|
||
t.Run("kvstore list keys", func(t *testing.T) { | ||
values, err := sub.KVStoreList(id) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, values, pairs) | ||
}) | ||
|
||
t.Run("kvstore delete", func(t *testing.T) { | ||
for key := range pairs { | ||
err = sub.KVStoreDelete(id, key) | ||
assert.NoError(t, err) | ||
} | ||
|
||
values, err := sub.KVStoreList(id) | ||
assert.NoError(t, err) | ||
assert.Empty(t, values) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a particular reason to update the MSV?
Given that there haven't been any significant changes, I assume we don't need this.