github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/rpc/state/state_test.go (about) 1 // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls 2 // 3 // Copyright 2020 Stafi Protocol 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 "os" 21 "strings" 22 "testing" 23 24 "github.com/stafiprotocol/go-substrate-rpc-client/pkg/client" 25 "github.com/stafiprotocol/go-substrate-rpc-client/pkg/rpcmocksrv" 26 "github.com/stafiprotocol/go-substrate-rpc-client/types" 27 ) 28 29 var state *State 30 31 func TestMain(m *testing.M) { 32 s := rpcmocksrv.New() 33 err := s.RegisterName("state", &mockSrv) 34 if err != nil { 35 panic(err) 36 } 37 38 cl, err := client.Connect(s.URL) 39 // cl, err := client.Connect(config.Default().RPCURL) 40 // cl, err := client.Connect("ws://127.0.0.1:9944") 41 if err != nil { 42 panic(err) 43 } 44 state = NewState(cl) 45 46 os.Exit(m.Run()) 47 } 48 49 // MockSrv holds data and methods exposed by the RPC Mock Server used in integration tests 50 type MockSrv struct { 51 blockHashLatest types.Hash 52 metadataString string 53 metadata *types.Metadata 54 runtimeVersion types.RuntimeVersion 55 storageKeyHex string 56 storageKeyHexEmpty string 57 storageChangeSets []types.StorageChangeSet 58 storageDataHex string 59 storageSize types.U64 60 storageHashHex string 61 childStorageKeyHex string // the key pointing to the child storage trie 62 childStorageTrieKeyHex string // a key within the child storage trie 63 childStorageTrieValueHex string // a value stored int he child storage trie 64 childStorageTrieValue ChildStorageTrieTestVal 65 childStorageTrieSize types.U64 66 childStorageTrieHashHex string 67 } 68 69 func (s *MockSrv) GetMetadata(hash *string) string { 70 return mockSrv.metadataString 71 } 72 73 func (s *MockSrv) GetRuntimeVersion(hash *string) types.RuntimeVersion { 74 return mockSrv.runtimeVersion 75 } 76 77 func (s *MockSrv) GetKeys(key string, hash *string) []string { 78 if !strings.HasPrefix(mockSrv.storageKeyHex, key) { 79 panic("key not found") 80 } 81 return []string{mockSrv.storageKeyHex} 82 } 83 84 func (s *MockSrv) GetStorage(key string, hash *string) string { 85 if key != s.storageKeyHex { 86 return "" 87 } 88 return mockSrv.storageDataHex 89 } 90 91 func (s *MockSrv) GetStorageSize(key string, hash *string) types.U64 { 92 return mockSrv.storageSize 93 } 94 95 func (s *MockSrv) GetStorageHash(key string, hash *string) string { 96 return mockSrv.storageHashHex 97 } 98 99 func (s *MockSrv) GetChildKeys(childStorageKey, prefix string, hash *string) []string { 100 if childStorageKey != mockSrv.childStorageKeyHex { 101 panic("childStorageKey not found") 102 } 103 if !strings.HasPrefix(mockSrv.childStorageTrieKeyHex, prefix) { 104 panic("no keys for prefix found") 105 } 106 return []string{mockSrv.childStorageTrieKeyHex} 107 } 108 109 func (s *MockSrv) GetChildStorage(childStorageKey, key string, hash *string) string { 110 if childStorageKey != mockSrv.childStorageKeyHex { 111 panic("childStorageKey not found") 112 } 113 if key != mockSrv.childStorageTrieKeyHex { 114 panic("key not found") 115 } 116 return mockSrv.childStorageTrieValueHex 117 } 118 119 func (s *MockSrv) GetChildStorageSize(childStorageKey, key string, hash *string) types.U64 { 120 if childStorageKey != mockSrv.childStorageKeyHex { 121 panic("childStorageKey not found") 122 } 123 if key != mockSrv.childStorageTrieKeyHex { 124 panic("key not found") 125 } 126 return mockSrv.childStorageTrieSize 127 } 128 129 func (s *MockSrv) GetChildStorageHash(childStorageKey, key string, hash *string) string { 130 if childStorageKey != mockSrv.childStorageKeyHex { 131 panic("childStorageKey not found") 132 } 133 if key != mockSrv.childStorageTrieKeyHex { 134 panic("key not found") 135 } 136 return mockSrv.childStorageTrieHashHex 137 } 138 139 func (s *MockSrv) QueryStorage(keys []string, startBlock string, block *string) []types.StorageChangeSet { 140 if len(keys) != 1 { 141 panic("keys need to have len of 1 in tests") 142 } 143 if keys[0] != mockSrv.storageKeyHex { 144 panic("key not found") 145 } 146 if startBlock != "0xdd1816b6f6889f46e23b0d6750bc441af9dad0fda8bae90677c1708d01035fbe" { 147 panic("startBlock must be 0xdd1816b6f6889f46e23b0d6750bc441af9dad0fda8bae90677c1708d01035fbe in tests") 148 } 149 150 return mockSrv.storageChangeSets 151 } 152 153 // func (s *MockSrv) SubscribeStorage(args []string) { 154 // fmt.Println("Hit") 155 // } 156 157 type ChildStorageTrieTestVal struct { 158 Key types.Hash 159 OtherID types.Hash 160 Value types.U32 161 } 162 163 // mockSrv sets default data used in tests. This data might become stale when substrate is updated – just run the tests 164 // against real servers and update the values stored here. To do that, replace s.URL with 165 // config.Default().RPCURL 166 var mockSrv = MockSrv{ 167 blockHashLatest: types.Hash{1, 2, 3}, 168 metadata: types.ExamplaryMetadataV4, 169 metadataString: types.ExamplaryMetadataV4String, 170 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 171 storageKeyHex: "0x0e4944cfd98d6f4cc374d16f5a4e3f9c", 172 storageKeyHexEmpty: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 173 storageChangeSets: []types.StorageChangeSet{{Block: types.Hash{0xdd, 0x18, 0x16, 0xb6, 0xf6, 0x88, 0x9f, 0x46, 0xe2, 0x3b, 0xd, 0x67, 0x50, 0xbc, 0x44, 0x1a, 0xf9, 0xda, 0xd0, 0xfd, 0xa8, 0xba, 0xe9, 0x6, 0x77, 0xc1, 0x70, 0x8d, 0x1, 0x3, 0x5f, 0xbe}, Changes: []types.KeyValueOption{{StorageKey: types.StorageKey{0xe, 0x49, 0x44, 0xcf, 0xd9, 0x8d, 0x6f, 0x4c, 0xc3, 0x74, 0xd1, 0x6f, 0x5a, 0x4e, 0x3f, 0x9c}, HasStorageData: true, StorageData: types.StorageDataRaw{0x88, 0x2, 0x66, 0x9f, 0x6e, 0x1, 0x0, 0x0}}}}, {Block: types.Hash{0x82, 0x14, 0xa1, 0x80, 0x8b, 0xd6, 0xb0, 0x46, 0xc8, 0x77, 0xa6, 0x4f, 0xce, 0xad, 0xb4, 0xa2, 0xa7, 0x3a, 0x65, 0x76, 0x9f, 0x61, 0x4, 0xc0, 0x20, 0xd7, 0x59, 0xad, 0x8f, 0x61, 0xc0, 0xd8}, Changes: []types.KeyValueOption{{StorageKey: types.StorageKey{0xe, 0x49, 0x44, 0xcf, 0xd9, 0x8d, 0x6f, 0x4c, 0xc3, 0x74, 0xd1, 0x6f, 0x5a, 0x4e, 0x3f, 0x9c}, HasStorageData: true, StorageData: types.StorageDataRaw{0x40, 0xe, 0x66, 0x9f, 0x6e, 0x1, 0x0, 0x0}}}}}, //nolint:lll 174 storageDataHex: "0xb82d895d00000000", 175 storageSize: 926778, 176 storageHashHex: "0xdf0e877ee1cb973b9a566f53707d365b269d7131b55e65b9790994e4e63b95e1", 177 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 178 childStorageTrieKeyHex: "0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d0", 179 childStorageTrieValueHex: "0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d09c1705d98d059a2d7f5faa89277ee5d0a38cc455f8b5fdf38fda471e988cb8a921000000", //nolint:lll 180 childStorageTrieValue: ChildStorageTrieTestVal{ 181 Key: types.NewHash(types.MustHexDecodeString("0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d0")), //nolint:lll 182 OtherID: types.NewHash(types.MustHexDecodeString("0x9c1705d98d059a2d7f5faa89277ee5d0a38cc455f8b5fdf38fda471e988cb8a9")), //nolint:lll 183 Value: types.NewU32(0x21), 184 }, 185 childStorageTrieSize: 68, 186 childStorageTrieHashHex: "0x20e3fc48a91087d091c17de08a5c470de53ccdaebd361025b0e5b7c65b9a0d30", //nolint:lll 187 }