-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add kvstore * use a fork of substrate rpc client * fix lint * use client decode function * move decodeSecondKey to utils.go
- Loading branch information
1 parent
ea6fdf3
commit 10a6bc1
Showing
7 changed files
with
249 additions
and
586 deletions.
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,133 @@ | ||
package substrate | ||
|
||
import ( | ||
"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 | ||
} |
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