-
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.
- Loading branch information
1 parent
ea6fdf3
commit 03daef8
Showing
2 changed files
with
193 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
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 | ||
} | ||
|
||
bytes, err := Encode(types.NewOptionBytes1024Empty()) | ||
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") | ||
} | ||
|
||
keys, err := cl.RPC.State.GetKeysLatest(storageKey) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to lookup entity") | ||
} | ||
|
||
fmt.Println(len(keys)) | ||
|
||
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 { | ||
pairs[string(c.StorageKey)] = string(c.StorageData) | ||
} | ||
} | ||
|
||
return pairs, nil | ||
} | ||
|
||
// id := types.NewAccountID([]byte(identity.Address())) | ||
|
||
// v, err := hex.DecodeString("bd05d43493846b6a9b68833881cd4092bd05d43493846b6a9b68833881cd409222838b57e06152572297591bf067d778a6ce805400dfd431f12e495d6e14e56388bb1da5144096387d83e11eb8afbd0d") | ||
|
||
// sk := types.NewStorageKey(v) | ||
// log.Printf("sk: %+v", hex.EncodeToString(storageKey)) | ||
// log.Printf("sk: %+v", hex.EncodeToString(sk)) | ||
// log.Printf("storageKey: %+v", storageKey) | ||
// var value []byte |
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,54 @@ | ||
package substrate | ||
|
||
import ( | ||
"fmt" | ||
"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"} | ||
mnem := "route visual hundred rabbit wet crunch ice castle milk model inherit outside" | ||
|
||
id, err := NewIdentityFromSr25519Phrase(mnem) | ||
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) | ||
|
||
fmt.Println(len(values)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, pairs, values) | ||
}) | ||
|
||
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.Equal(t, 0, len(values)) | ||
}) | ||
} |