-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathkvstore.go
133 lines (106 loc) · 2.71 KB
/
kvstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
}