Skip to content

Commit b4c779a

Browse files
Child storage (#27)
* Add Type type * Add metadata v7 * Add child storage methods
1 parent 5c358fd commit b4c779a

21 files changed

Lines changed: 1309 additions & 722 deletions

rpc/state/get_child_keys.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
2+
// Copyright (C) 2019 Centrifuge GmbH
3+
//
4+
// This file is part of Go Substrate RPC Client (GSRPC).
5+
//
6+
// GSRPC is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
//
11+
// GSRPC is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
package state
20+
21+
import (
22+
"github.com/centrifuge/go-substrate-rpc-client/client"
23+
"github.com/centrifuge/go-substrate-rpc-client/types"
24+
)
25+
26+
// GetChildKeys retreives the keys with the given prefix of a specific child storage
27+
func (s *State) GetChildKeys(childStorageKey, prefix types.StorageKey, blockHash types.Hash) (
28+
[]types.StorageKey, error) {
29+
return s.getChildKeys(childStorageKey, prefix, &blockHash)
30+
}
31+
32+
// GetChildKeysLatest retreives the keys with the given prefix of a specific child storage for the latest block height
33+
func (s *State) GetChildKeysLatest(childStorageKey, prefix types.StorageKey) ([]types.StorageKey, error) {
34+
return s.getChildKeys(childStorageKey, prefix, nil)
35+
}
36+
37+
func (s *State) getChildKeys(childStorageKey, prefix types.StorageKey, blockHash *types.Hash) (
38+
[]types.StorageKey, error) {
39+
var res []string
40+
err := client.CallWithBlockHash(*s.client, &res, "state_getChildKeys", blockHash, childStorageKey.Hex(), prefix.Hex())
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
keys := make([]types.StorageKey, len(res))
46+
for i, r := range res {
47+
err = types.DecodeFromHexString(r, &keys[i])
48+
if err != nil {
49+
return nil, err
50+
}
51+
}
52+
return keys, err
53+
}

rpc/state/get_child_storage.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
2+
// Copyright (C) 2019 Centrifuge GmbH
3+
//
4+
// This file is part of Go Substrate RPC Client (GSRPC).
5+
//
6+
// GSRPC is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
//
11+
// GSRPC is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
package state
20+
21+
import (
22+
"github.com/centrifuge/go-substrate-rpc-client/client"
23+
"github.com/centrifuge/go-substrate-rpc-client/types"
24+
)
25+
26+
// GetChildStorage retreives the child storage for a key and decodes them into the provided interface
27+
func (s *State) GetChildStorage(childStorageKey, key types.StorageKey, target interface{}, blockHash types.Hash) error {
28+
raw, err := s.getChildStorageRaw(childStorageKey, key, &blockHash)
29+
if err != nil {
30+
return err
31+
}
32+
return types.DecodeFromBytes(*raw, target)
33+
}
34+
35+
// GetChildStorageLatest retreives the child storage for a key for the latest block height and decodes them into the
36+
// provided interface
37+
func (s *State) GetChildStorageLatest(childStorageKey, key types.StorageKey, target interface{}) error {
38+
raw, err := s.getChildStorageRaw(childStorageKey, key, nil)
39+
if err != nil {
40+
return err
41+
}
42+
return types.DecodeFromBytes(*raw, target)
43+
}
44+
45+
// GetChildStorageRaw retreives the child storage for a key as raw bytes, without decoding them
46+
func (s *State) GetChildStorageRaw(childStorageKey, key types.StorageKey, blockHash types.Hash) (
47+
*types.StorageDataRaw, error) {
48+
return s.getChildStorageRaw(childStorageKey, key, &blockHash)
49+
}
50+
51+
// GetChildStorageRawLatest retreives the child storage for a key for the latest block height as raw bytes,
52+
// without decoding them
53+
func (s *State) GetChildStorageRawLatest(childStorageKey, key types.StorageKey) (*types.StorageDataRaw, error) {
54+
return s.getChildStorageRaw(childStorageKey, key, nil)
55+
}
56+
57+
func (s *State) getChildStorageRaw(childStorageKey, key types.StorageKey, blockHash *types.Hash) (
58+
*types.StorageDataRaw, error) {
59+
var res string
60+
err := client.CallWithBlockHash(*s.client, &res, "state_getChildStorage", blockHash, childStorageKey.Hex(),
61+
key.Hex())
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
bz, err := types.HexDecodeString(res)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
data := types.NewStorageDataRaw(bz)
72+
return &data, nil
73+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
2+
// Copyright (C) 2019 Centrifuge GmbH
3+
//
4+
// This file is part of Go Substrate RPC Client (GSRPC).
5+
//
6+
// GSRPC is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
//
11+
// GSRPC is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
package state
20+
21+
import (
22+
"github.com/centrifuge/go-substrate-rpc-client/client"
23+
"github.com/centrifuge/go-substrate-rpc-client/types"
24+
)
25+
26+
// GetChildStorageHash retreives the child storage hash for the given key
27+
func (s *State) GetChildStorageHash(childStorageKey, key types.StorageKey, blockHash types.Hash) (types.Hash, error) {
28+
return s.getChildStorageHash(childStorageKey, key, &blockHash)
29+
}
30+
31+
// GetChildStorageHashLatest retreives the child storage hash for the given key for the latest block height
32+
func (s *State) GetChildStorageHashLatest(childStorageKey, key types.StorageKey) (types.Hash, error) {
33+
return s.getChildStorageHash(childStorageKey, key, nil)
34+
}
35+
36+
func (s *State) getChildStorageHash(childStorageKey, key types.StorageKey, blockHash *types.Hash) (types.Hash, error) {
37+
var res string
38+
err := client.CallWithBlockHash(*s.client, &res, "state_getChildStorageHash", blockHash, childStorageKey.Hex(),
39+
key.Hex())
40+
if err != nil {
41+
return types.Hash{}, err
42+
}
43+
44+
return types.NewHashFromHexString(res)
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
2+
// Copyright (C) 2019 Centrifuge GmbH
3+
//
4+
// This file is part of Go Substrate RPC Client (GSRPC).
5+
//
6+
// GSRPC is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
//
11+
// GSRPC is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
package state
20+
21+
import (
22+
"github.com/centrifuge/go-substrate-rpc-client/client"
23+
"github.com/centrifuge/go-substrate-rpc-client/types"
24+
)
25+
26+
// GetChildStorageSize retreives the child storage size for the given key
27+
func (s *State) GetChildStorageSize(childStorageKey, key types.StorageKey, blockHash types.Hash) (types.U64, error) {
28+
return s.getChildStorageSize(childStorageKey, key, &blockHash)
29+
}
30+
31+
// GetChildStorageSizeLatest retreives the child storage size for the given key for the latest block height
32+
func (s *State) GetChildStorageSizeLatest(childStorageKey, key types.StorageKey) (types.U64, error) {
33+
return s.getChildStorageSize(childStorageKey, key, nil)
34+
}
35+
36+
func (s *State) getChildStorageSize(childStorageKey, key types.StorageKey, blockHash *types.Hash) (types.U64, error) {
37+
var res types.U64
38+
err := client.CallWithBlockHash(*s.client, &res, "state_getChildStorageSize", blockHash, childStorageKey.Hex(),
39+
key.Hex())
40+
if err != nil {
41+
return 0, err
42+
}
43+
return res, err
44+
}

rpc/state/get_metadata.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (s *State) getMetadata(blockHash *types.Hash) (*types.Metadata, error) {
3636
return nil, err
3737
}
3838

39-
metadata := types.NewMetadata()
40-
err = types.DecodeFromHexString(res, metadata)
41-
return metadata, err
39+
var metadata types.Metadata
40+
err = types.DecodeFromHexString(res, &metadata)
41+
return &metadata, err
4242
}

rpc/state/get_metadata_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ import (
2525
func TestState_GetMetadataLatest(t *testing.T) {
2626
md, err := state.GetMetadataLatest()
2727
assert.NoError(t, err)
28-
assert.Equal(t, &mockSrv.metadata, md)
28+
assert.Equal(t, *mockSrv.metadata, *md)
2929
}
3030

3131
func TestState_GetMetadata(t *testing.T) {
3232
md, err := state.GetMetadata(mockSrv.blockHashLatest)
3333
assert.NoError(t, err)
34-
assert.Equal(t, &mockSrv.metadata, md)
34+
assert.Equal(t, *mockSrv.metadata, *md)
3535
}

rpc/state/get_storage_hash.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ import (
2323
"github.com/centrifuge/go-substrate-rpc-client/types"
2424
)
2525

26-
// GetStorageHash retreives the keys with the given key
26+
// GetStorageHash retreives the storage hash for the given key
2727
func (s *State) GetStorageHash(key types.StorageKey, blockHash types.Hash) (types.Hash, error) {
2828
return s.getStorageHash(key, &blockHash)
2929
}
3030

31-
// GetStorageHashLatest retreives the keys with the given key for the latest block height
31+
// GetStorageHashLatest retreives the storage hash for the given key for the latest block height
3232
func (s *State) GetStorageHashLatest(key types.StorageKey) (types.Hash, error) {
3333
return s.getStorageHash(key, nil)
3434
}

rpc/state/get_storage_size.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ import (
2323
"github.com/centrifuge/go-substrate-rpc-client/types"
2424
)
2525

26-
// GetStorageSize retreives the keys with the given key
26+
// GetStorageSize retreives the storage size for the given key
2727
func (s *State) GetStorageSize(key types.StorageKey, blockHash types.Hash) (types.U64, error) {
2828
return s.getStorageSize(key, &blockHash)
2929
}
3030

31-
// GetStorageSizeLatest retreives the keys with the given key for the latest block height
31+
// GetStorageSizeLatest retreives the storage size for the given key for the latest block height
3232
func (s *State) GetStorageSizeLatest(key types.StorageKey) (types.U64, error) {
3333
return s.getStorageSize(key, nil)
3434
}

rpc/state/state_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func TestMain(m *testing.M) {
3737

3838
cl, err := client.Connect(s.URL)
3939
// cl, err := client.Connect(config.NewDefaultConfig().RPCURL)
40+
// cl, err := client.Connect("ws://35.246.140.178:9944")
4041
if err != nil {
4142
panic(err)
4243
}
@@ -49,7 +50,7 @@ func TestMain(m *testing.M) {
4950
type MockSrv struct {
5051
blockHashLatest types.Hash
5152
metadataString string
52-
metadata types.Metadata
53+
metadata *types.Metadata
5354
runtimeVersion types.RuntimeVersion
5455
storageKeyHex string
5556
storageDataHex string

types/extrinsic.go

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ type Call struct {
203203
Args Args
204204
}
205205

206-
func NewCall(m RuntimeMetadataV4, call string, args ...interface{}) (Call, error) {
206+
func NewCall(m *Metadata, call string, args ...interface{}) (Call, error) {
207207
c, err := FindCallIndex(m, call)
208208
if err != nil {
209209
return Call{}, err
@@ -221,23 +221,42 @@ func NewCall(m RuntimeMetadataV4, call string, args ...interface{}) (Call, error
221221
return Call{c, a}, nil
222222
}
223223

224-
func FindCallIndex(m RuntimeMetadataV4, call string) (CallIndex, error) {
224+
func FindCallIndex(m *Metadata, call string) (CallIndex, error) {
225225
s := strings.Split(call, ".")
226226
// section index, method index
227227
var sIndex uint8 = 0
228228

229-
for _, n := range m.Modules {
230-
if n.HasCalls {
231-
if n.Name == s[0] {
232-
for j, f := range n.Calls {
233-
if f.Name == s[1] {
234-
mIndex := uint8(j)
235-
return CallIndex{sIndex, mIndex}, nil
229+
switch {
230+
case m.IsMetadataV4:
231+
for _, n := range m.AsMetadataV4.Modules {
232+
if n.HasCalls {
233+
if string(n.Name) == s[0] {
234+
for j, f := range n.Calls {
235+
if string(f.Name) == s[1] {
236+
mIndex := uint8(j)
237+
return CallIndex{sIndex, mIndex}, nil
238+
}
236239
}
237240
}
241+
sIndex++
238242
}
239-
sIndex++
240243
}
244+
case m.IsMetadataV7:
245+
for _, n := range m.AsMetadataV7.Modules {
246+
if n.HasCalls {
247+
if string(n.Name) == s[0] {
248+
for j, f := range n.Calls {
249+
if string(f.Name) == s[1] {
250+
mIndex := uint8(j)
251+
return CallIndex{sIndex, mIndex}, nil
252+
}
253+
}
254+
}
255+
sIndex++
256+
}
257+
}
258+
default:
259+
return CallIndex{}, fmt.Errorf("valid metadata expected, got %#v", m)
241260
}
242261

243262
return CallIndex{}, fmt.Errorf("could not find CallIndex for call %v in metadata", call)
@@ -314,24 +333,3 @@ type SignaturePayload struct {
314333
Tip UCompact
315334
Version uint8
316335
}
317-
318-
func (e SignaturePayload) Encode(encoder scale.Encoder) error {
319-
// err := encoder.EncodeUintCompact(e.Nonce)
320-
// if err != nil {
321-
// return err
322-
// }
323-
// err = encoder.Encode(e.Method)
324-
// if err != nil {
325-
// return err
326-
// }
327-
// err = encoder.Encode(e.Era)
328-
// if err != nil {
329-
// return err
330-
// }
331-
// // encoder.Encode(e.ImmortalEra) // always immortal
332-
// err = encoder.Write(e.PriorBlock[:])
333-
// if err != nil {
334-
// return err
335-
// }
336-
return nil
337-
}

0 commit comments

Comments
 (0)