github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/storage_key_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 types_test 18 19 import ( 20 "testing" 21 22 . "github.com/stafiprotocol/go-substrate-rpc-client/types" 23 "github.com/stretchr/testify/assert" 24 ) 25 26 const ( 27 AlicePubKey = "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" 28 ) 29 30 func TestCreateStorageKeyPlainV13(t *testing.T) { 31 m := ExamplaryMetadataV13 32 33 key, err := CreateStorageKey(m, "Timestamp", "Now", nil) 34 assert.NoError(t, err) 35 hex, err := Hex(key) 36 assert.NoError(t, err) 37 assert.Equal(t, "0xf0c365c3cf59d671eb72da0e7a4113c49f1f0515f462cdcf84e0f1d6045dfcbb", hex) 38 } 39 40 func TestCreateStorageKeyPlainV10(t *testing.T) { 41 m := ExamplaryMetadataV10 42 43 key, err := CreateStorageKey(m, "Timestamp", "Now", nil, nil) 44 assert.NoError(t, err) 45 hex, err := Hex(key) 46 assert.NoError(t, err) 47 assert.Equal(t, "0xf0c365c3cf59d671eb72da0e7a4113c49f1f0515f462cdcf84e0f1d6045dfcbb", hex) 48 } 49 50 func TestCreateStorageKeyPlainV9(t *testing.T) { 51 m := ExamplaryMetadataV9 52 53 key, err := CreateStorageKey(m, "Timestamp", "Now", nil, nil) 54 assert.NoError(t, err) 55 hex, err := Hex(key) 56 assert.NoError(t, err) 57 assert.Equal(t, "0xf0c365c3cf59d671eb72da0e7a4113c49f1f0515f462cdcf84e0f1d6045dfcbb", hex) 58 } 59 60 func TestCreateStorageKeyPlainV4(t *testing.T) { 61 m := ExamplaryMetadataV4 62 63 key, err := CreateStorageKey(m, "Timestamp", "Now", nil, nil) 64 assert.NoError(t, err) 65 hex, err := Hex(key) 66 assert.NoError(t, err) 67 assert.Equal(t, "0x0e4944cfd98d6f4cc374d16f5a4e3f9c", hex) 68 } 69 70 func TestCreateStorageKeyMapV10(t *testing.T) { 71 b := MustHexDecodeString(AlicePubKey) 72 m := ExamplaryMetadataV10 73 key, err := CreateStorageKey(m, "System", "AccountNonce", b, nil) 74 assert.NoError(t, err) 75 hex, err := Hex(key) 76 assert.NoError(t, err) 77 assert.Equal(t, "0x26aa394eea5630e07c48ae0c9558cef79c2f82b23e5fd031fb54c292794b4cc42e3fb4c297a84c5cebc0e78257d213d0927ccc7596044c6ba013dd05522aacba", hex) //nolint:lll 78 } 79 80 func TestCreateStorageKeyMapV9(t *testing.T) { 81 b := MustHexDecodeString(AlicePubKey) 82 m := ExamplaryMetadataV9 83 key, err := CreateStorageKey(m, "System", "AccountNonce", b, nil) 84 assert.NoError(t, err) 85 hex, err := Hex(key) 86 assert.NoError(t, err) 87 assert.Equal(t, "0x26aa394eea5630e07c48ae0c9558cef79c2f82b23e5fd031fb54c292794b4cc42e3fb4c297a84c5cebc0e78257d213d0927ccc7596044c6ba013dd05522aacba", hex) //nolint:lll 88 } 89 90 func TestCreateStorageKeyMapV4(t *testing.T) { 91 b := MustHexDecodeString(AlicePubKey) 92 m := ExamplaryMetadataV4 93 key, err := CreateStorageKey(m, "System", "AccountNonce", b, nil) 94 assert.NoError(t, err) 95 hex, err := Hex(key) 96 assert.NoError(t, err) 97 assert.Equal(t, "0x5c54163a1c72509b5250f0a30b9001fdee9d9b48388b06921f1b210e81e3a1f0", hex) 98 } 99 100 func TestCreateStorageKeyMapV13(t *testing.T) { 101 m := ExamplaryMetadataV13 102 b := MustHexDecodeString(AlicePubKey) 103 key, err := CreateStorageKey(m, "System", "Account", b) 104 assert.NoError(t, err) 105 hex, err := Hex(key) 106 assert.NoError(t, err) 107 assert.Equal(t, "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9de1e86a9a8c739864cf3cc5ec2bea59fd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d", hex) 108 } 109 110 func TestCreateStorageKeyDoubleMapV10(t *testing.T) { 111 m := ExamplaryMetadataV10 112 key, err := CreateStorageKey(m, "Session", "NextKeys", 113 []byte{0x34, 0x3a, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x6b, 0x65, 0x79, 0x73}, 114 []byte{0xbe, 0x5d, 0xdb, 0x15, 0x79, 0xb7, 0x2e, 0x84, 0x52, 0x4f, 0xc2, 0x9e, 0x78, 0x60, 0x9e, 0x3c, 115 0xaf, 0x42, 0xe8, 0x5a, 0xa1, 0x18, 0xeb, 0xfe, 0x0b, 0x0a, 0xd4, 0x04, 0xb5, 0xbd, 0xd2, 0x5f}, 116 ) 117 assert.NoError(t, err) 118 hex, err := Hex(key) 119 assert.NoError(t, err) 120 assert.Equal(t, "0x"+ 121 "cec5070d609dd3497f72bde07fc96ba0"+ // twox 128 122 "4c014e6bf8b8c2c011e7290b85696bb3"+ // twox 128 123 "9fe6329cc0b39e09"+ // twox 64 124 "343a73657373696f6e3a6b657973"+ // twox 64 (concat, with length) 125 "4724e5390fcf0d08afc9608ff4c45df257266ae599ac7a32baba26155dcf4402", // blake2 126 hex) //nolint:lll 127 } 128 129 func TestCreateStorageKeyDoubleMapV9(t *testing.T) { 130 m := ExamplaryMetadataV9 131 key, err := CreateStorageKey(m, "Session", "NextKeys", 132 []byte{0x34, 0x3a, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x6b, 0x65, 0x79, 0x73}, 133 []byte{0xbe, 0x5d, 0xdb, 0x15, 0x79, 0xb7, 0x2e, 0x84, 0x52, 0x4f, 0xc2, 0x9e, 0x78, 0x60, 0x9e, 0x3c, 134 0xaf, 0x42, 0xe8, 0x5a, 0xa1, 0x18, 0xeb, 0xfe, 0x0b, 0x0a, 0xd4, 0x04, 0xb5, 0xbd, 0xd2, 0x5f}, 135 ) 136 assert.NoError(t, err) 137 hex, err := Hex(key) 138 assert.NoError(t, err) 139 assert.Equal(t, "0x"+ 140 "cec5070d609dd3497f72bde07fc96ba0"+ // twox 128 141 "4c014e6bf8b8c2c011e7290b85696bb3"+ // twox 128 142 "9fe6329cc0b39e09"+ // twox 64 143 "343a73657373696f6e3a6b657973"+ // twox 64 (concat, with length) 144 "4724e5390fcf0d08afc9608ff4c45df257266ae599ac7a32baba26155dcf4402", // blake2 145 hex) //nolint:lll 146 } 147 148 func TestStorageKey_EncodedLength(t *testing.T) { 149 assertEncodedLength(t, []encodedLengthAssert{ 150 {NewStorageKey(MustHexDecodeString("0x00")), 1}, 151 {NewStorageKey(MustHexDecodeString("0xab1234")), 3}, 152 {NewStorageKey(MustHexDecodeString("0x0001")), 2}, 153 }) 154 } 155 156 func TestStorageKey_Encode(t *testing.T) { 157 assertEncode(t, []encodingAssert{ 158 {NewStorageKey([]byte{171, 18, 52}), MustHexDecodeString("0xab1234")}, 159 {NewStorageKey([]byte{}), MustHexDecodeString("0x")}, 160 }) 161 } 162 163 func TestStorageKey_Decode(t *testing.T) { 164 bz := []byte{12, 251, 42} 165 decoded := make(StorageKey, len(bz)) 166 err := DecodeFromBytes(bz, &decoded) 167 assert.NoError(t, err) 168 assert.Equal(t, StorageKey(bz), decoded) 169 } 170 171 func TestStorageKey_Hash(t *testing.T) { 172 assertHash(t, []hashAssert{ 173 {NewStorageKey([]byte{0, 42, 254}), MustHexDecodeString( 174 "0x537db36f5b5970b679a28a3df8d219317d658014fb9c3d409c0c799d8ecf149d")}, 175 {NewStorageKey([]byte{0, 0}), MustHexDecodeString( 176 "0x9ee6dfb61a2fb903df487c401663825643bb825d41695e63df8af6162ab145a6")}, 177 }) 178 } 179 180 func TestStorageKey_Hex(t *testing.T) { 181 assertEncodeToHex(t, []encodeToHexAssert{ 182 {NewStorageKey([]byte{0, 0, 0}), "0x000000"}, 183 {NewStorageKey([]byte{171, 18, 52}), "0xab1234"}, 184 {NewStorageKey([]byte{0, 1}), "0x0001"}, 185 {NewStorageKey([]byte{18, 52, 86}), "0x123456"}, 186 }) 187 } 188 189 func TestStorageKey_String(t *testing.T) { 190 assertString(t, []stringAssert{ 191 {NewStorageKey([]byte{0, 0, 0}), "[0 0 0]"}, 192 {NewStorageKey([]byte{171, 18, 52}), "[171 18 52]"}, 193 {NewStorageKey([]byte{0, 1}), "[0 1]"}, 194 {NewStorageKey([]byte{18, 52, 86}), "[18 52 86]"}, 195 }) 196 } 197 198 func TestStorageKey_Eq(t *testing.T) { 199 assertEq(t, []eqAssert{ 200 {NewStorageKey([]byte{1, 0, 0}), NewStorageKey([]byte{1, 0}), false}, 201 {NewStorageKey([]byte{0, 0, 1}), NewStorageKey([]byte{0, 1}), false}, 202 {NewStorageKey([]byte{0, 0, 0}), NewStorageKey([]byte{0, 0}), false}, 203 {NewStorageKey([]byte{12, 48, 255}), NewStorageKey([]byte{12, 48, 255}), true}, 204 {NewStorageKey([]byte{0}), NewStorageKey([]byte{0}), true}, 205 {NewStorageKey([]byte{1}), NewBool(true), false}, 206 {NewStorageKey([]byte{0}), NewBool(false), false}, 207 }) 208 }