Skip to content

Commit 6fb88e3

Browse files
Add tests for child storage functionality (#30)
* Add tests for child storage functionality
1 parent e44fc74 commit 6fb88e3

5 files changed

Lines changed: 240 additions & 17 deletions

File tree

rpc/state/get_child_keys_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
2+
//
3+
// Copyright 2019 Centrifuge GmbH
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package state
18+
19+
import (
20+
"testing"
21+
22+
"github.com/centrifuge/go-substrate-rpc-client/types"
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
var prefix = types.NewStorageKey(types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex))[:8]
27+
28+
func TestState_GetChildKeysLatest(t *testing.T) {
29+
keys, err := state.GetChildKeysLatest(childStorageKey, prefix)
30+
assert.NoError(t, err)
31+
assert.Equal(t, []types.StorageKey{types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex)}, keys)
32+
}
33+
34+
func TestState_GetChildKeys(t *testing.T) {
35+
keys, err := state.GetChildKeys(childStorageKey, prefix, mockSrv.blockHashLatest)
36+
assert.NoError(t, err)
37+
assert.Equal(t, []types.StorageKey{types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex)}, keys)
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
2+
//
3+
// Copyright 2019 Centrifuge GmbH
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package state
18+
19+
import (
20+
"testing"
21+
22+
"github.com/centrifuge/go-substrate-rpc-client/types"
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestState_GetChildStorageHashLatest(t *testing.T) {
27+
hash, err := state.GetChildStorageHashLatest(childStorageKey, key)
28+
assert.NoError(t, err)
29+
assert.Equal(t, types.NewHash(types.MustHexDecodeString(mockSrv.childStorageTrieHashHex)), hash)
30+
}
31+
32+
func TestState_GetChildStorageHash(t *testing.T) {
33+
hash, err := state.GetChildStorageHash(childStorageKey, key, mockSrv.blockHashLatest)
34+
assert.NoError(t, err)
35+
assert.Equal(t, types.NewHash(types.MustHexDecodeString(mockSrv.childStorageTrieHashHex)), hash)
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
2+
//
3+
// Copyright 2019 Centrifuge GmbH
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package state
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestState_GetChildStorageSizeLatest(t *testing.T) {
26+
size, err := state.GetChildStorageSizeLatest(childStorageKey, key)
27+
assert.NoError(t, err)
28+
assert.Equal(t, mockSrv.childStorageTrieSize, size)
29+
}
30+
31+
func TestState_GetChildStorageSize(t *testing.T) {
32+
size, err := state.GetChildStorageSize(childStorageKey, key, mockSrv.blockHashLatest)
33+
assert.NoError(t, err)
34+
assert.Equal(t, mockSrv.childStorageTrieSize, size)
35+
}
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+
//
3+
// Copyright 2019 Centrifuge GmbH
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package state
18+
19+
import (
20+
"testing"
21+
22+
"github.com/centrifuge/go-substrate-rpc-client/types"
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
var childStorageKey = types.NewStorageKey(types.MustHexDecodeString(mockSrv.childStorageKeyHex))
27+
var key = types.NewStorageKey(types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex))
28+
29+
func TestState_GetChildStorageLatest(t *testing.T) {
30+
var decoded ChildStorageTrieTestVal
31+
err := state.GetChildStorageLatest(childStorageKey, key, &decoded)
32+
assert.NoError(t, err)
33+
assert.Equal(t, mockSrv.childStorageTrieValue, decoded)
34+
}
35+
36+
func TestState_GetChildStorage(t *testing.T) {
37+
var decoded ChildStorageTrieTestVal
38+
err := state.GetChildStorageLatest(childStorageKey, key, &decoded)
39+
assert.NoError(t, err)
40+
assert.Equal(t, mockSrv.childStorageTrieValue, decoded, mockSrv.blockHashLatest)
41+
}
42+
43+
func TestState_GetChildStorageRawLatest(t *testing.T) {
44+
data, err := state.GetChildStorageRawLatest(childStorageKey, key)
45+
assert.NoError(t, err)
46+
assert.Equal(t, mockSrv.childStorageTrieValueHex, data.Hex())
47+
}
48+
49+
func TestState_GetChildStorageRaw(t *testing.T) {
50+
data, err := state.GetChildStorageRaw(childStorageKey, key, mockSrv.blockHashLatest)
51+
assert.NoError(t, err)
52+
assert.Equal(t, mockSrv.childStorageTrieValueHex, data.Hex())
53+
}

rpc/state/state_test.go

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ 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")
4140
if err != nil {
4241
panic(err)
4342
}
@@ -48,14 +47,20 @@ func TestMain(m *testing.M) {
4847

4948
// MockSrv holds data and methods exposed by the RPC Mock Server used in integration tests
5049
type MockSrv struct {
51-
blockHashLatest types.Hash
52-
metadataString string
53-
metadata *types.Metadata
54-
runtimeVersion types.RuntimeVersion
55-
storageKeyHex string
56-
storageDataHex string
57-
storageSize types.U64
58-
storageHashHex string
50+
blockHashLatest types.Hash
51+
metadataString string
52+
metadata *types.Metadata
53+
runtimeVersion types.RuntimeVersion
54+
storageKeyHex string
55+
storageDataHex string
56+
storageSize types.U64
57+
storageHashHex string
58+
childStorageKeyHex string // the key pointing to the child storage trie
59+
childStorageTrieKeyHex string // a key within the child storage trie
60+
childStorageTrieValueHex string // a value stored int he child storage trie
61+
childStorageTrieValue ChildStorageTrieTestVal
62+
childStorageTrieSize types.U64
63+
childStorageTrieHashHex string
5964
}
6065

6166
func (s *MockSrv) GetMetadata(hash *string) string {
@@ -88,16 +93,72 @@ func (s *MockSrv) GetStorageHash(key string, hash *string) string {
8893
return mockSrv.storageHashHex
8994
}
9095

96+
func (s *MockSrv) GetChildKeys(childStorageKey, prefix string, hash *string) []string {
97+
if childStorageKey != mockSrv.childStorageKeyHex {
98+
panic("childStorageKey not found")
99+
}
100+
if !strings.HasPrefix(mockSrv.childStorageTrieKeyHex, prefix) {
101+
panic("no keys for prefix found")
102+
}
103+
return []string{mockSrv.childStorageTrieKeyHex}
104+
}
105+
106+
func (s *MockSrv) GetChildStorage(childStorageKey, key string, hash *string) string {
107+
if childStorageKey != mockSrv.childStorageKeyHex {
108+
panic("childStorageKey not found")
109+
}
110+
if key != mockSrv.childStorageTrieKeyHex {
111+
panic("key not found")
112+
}
113+
return mockSrv.childStorageTrieValueHex
114+
}
115+
116+
func (s *MockSrv) GetChildStorageSize(childStorageKey, key string, hash *string) types.U64 {
117+
if childStorageKey != mockSrv.childStorageKeyHex {
118+
panic("childStorageKey not found")
119+
}
120+
if key != mockSrv.childStorageTrieKeyHex {
121+
panic("key not found")
122+
}
123+
return mockSrv.childStorageTrieSize
124+
}
125+
126+
func (s *MockSrv) GetChildStorageHash(childStorageKey, key string, hash *string) string {
127+
if childStorageKey != mockSrv.childStorageKeyHex {
128+
panic("childStorageKey not found")
129+
}
130+
if key != mockSrv.childStorageTrieKeyHex {
131+
panic("key not found")
132+
}
133+
return mockSrv.childStorageTrieHashHex
134+
}
135+
136+
type ChildStorageTrieTestVal struct {
137+
Key types.Hash
138+
OtherID types.Hash
139+
Value types.U32
140+
}
141+
91142
// mockSrv sets default data used in tests. This data might become stale when substrate is updated – just run the tests
92143
// against real servers and update the values stored here. To do that, replace s.URL with
93144
// config.NewDefaultConfig().RPCURL
94145
var mockSrv = MockSrv{
95-
blockHashLatest: types.Hash{1, 2, 3},
96-
metadata: types.ExamplaryMetadataV4,
97-
metadataString: types.ExamplaryMetadataV4String,
98-
runtimeVersion: types.RuntimeVersion{APIs: []types.RuntimeVersionAPI{{APIID: "0xdf6acb689907609b", Version: 0x2}, {APIID: "0x37e397fc7c91f5e4", Version: 0x1}, {APIID: "0x40fe3ad401f8959a", Version: 0x3}, {APIID: "0xd2bc9897eed08f15", Version: 0x1}, {APIID: "0xf78b278be53f454c", Version: 0x1}, {APIID: "0xed99c5acb25eedf5", Version: 0x2}, {APIID: "0xdd718d5cc53262d4", Version: 0x1}, {APIID: "0x7801759919ee83e5", Version: 0x1}}, AuthoringVersion: 0xa, ImplName: "substrate-node", ImplVersion: 0x3e, SpecName: "node", SpecVersion: 0x3c}, //nolint:lll
99-
storageKeyHex: "0x0e4944cfd98d6f4cc374d16f5a4e3f9c",
100-
storageDataHex: "0xb82d895d00000000",
101-
storageSize: 926778,
102-
storageHashHex: "0xdf0e877ee1cb973b9a566f53707d365b269d7131b55e65b9790994e4e63b95e1",
146+
blockHashLatest: types.Hash{1, 2, 3},
147+
metadata: types.ExamplaryMetadataV4,
148+
metadataString: types.ExamplaryMetadataV4String,
149+
runtimeVersion: types.RuntimeVersion{APIs: []types.RuntimeVersionAPI{{APIID: "0xdf6acb689907609b", Version: 0x2}, {APIID: "0x37e397fc7c91f5e4", Version: 0x1}, {APIID: "0x40fe3ad401f8959a", Version: 0x3}, {APIID: "0xd2bc9897eed08f15", Version: 0x1}, {APIID: "0xf78b278be53f454c", Version: 0x1}, {APIID: "0xed99c5acb25eedf5", Version: 0x2}, {APIID: "0xdd718d5cc53262d4", Version: 0x1}, {APIID: "0x7801759919ee83e5", Version: 0x1}}, AuthoringVersion: 0xa, ImplName: "substrate-node", ImplVersion: 0x3e, SpecName: "node", SpecVersion: 0x3c}, //nolint:lll
150+
storageKeyHex: "0x0e4944cfd98d6f4cc374d16f5a4e3f9c",
151+
storageDataHex: "0xb82d895d00000000",
152+
storageSize: 926778,
153+
storageHashHex: "0xdf0e877ee1cb973b9a566f53707d365b269d7131b55e65b9790994e4e63b95e1",
154+
childStorageKeyHex: "0x3a6368696c645f73746f726167653a64656661756c743a05470000", //nolint:lll beginning with hex encoded `:child_storage:` as per https://github.com/paritytech/substrate/blob/master/core/primitives/storage/src/lib.rs#L71
155+
childStorageTrieKeyHex: "0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d0",
156+
childStorageTrieValueHex: "0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d09c1705d98d059a2d7f5faa89277ee5d0a38cc455f8b5fdf38fda471e988cb8a921000000", //nolint:lll
157+
childStorageTrieValue: ChildStorageTrieTestVal{
158+
Key: types.NewHash(types.MustHexDecodeString("0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d0")), //nolint:lll
159+
OtherID: types.NewHash(types.MustHexDecodeString("0x9c1705d98d059a2d7f5faa89277ee5d0a38cc455f8b5fdf38fda471e988cb8a9")), //nolint:lll
160+
Value: types.NewU32(0x21),
161+
},
162+
childStorageTrieSize: 68,
163+
childStorageTrieHashHex: "0x20e3fc48a91087d091c17de08a5c470de53ccdaebd361025b0e5b7c65b9a0d30", //nolint:lll
103164
}

0 commit comments

Comments
 (0)