github.com/MetalBlockchain/subnet-evm@v0.6.3/trie/stacktrie_test.go (about) 1 // (c) 2020-2021, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2020 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package trie 28 29 import ( 30 "bytes" 31 "math/big" 32 "testing" 33 34 "github.com/MetalBlockchain/subnet-evm/core/rawdb" 35 "github.com/ethereum/go-ethereum/common" 36 "github.com/ethereum/go-ethereum/crypto" 37 ) 38 39 func TestStackTrieInsertAndHash(t *testing.T) { 40 type KeyValueHash struct { 41 K string // Hex string for key. 42 V string // Value, directly converted to bytes. 43 H string // Expected root hash after insert of (K, V) to an existing trie. 44 } 45 tests := [][]KeyValueHash{ 46 { // {0:0, 7:0, f:0} 47 {"00", "v_______________________0___0", "5cb26357b95bb9af08475be00243ceb68ade0b66b5cd816b0c18a18c612d2d21"}, 48 {"70", "v_______________________0___1", "8ff64309574f7a437a7ad1628e690eb7663cfde10676f8a904a8c8291dbc1603"}, 49 {"f0", "v_______________________0___2", "9e3a01bd8d43efb8e9d4b5506648150b8e3ed1caea596f84ee28e01a72635470"}, 50 }, 51 { // {1:0cc, e:{1:fc, e:fc}} 52 {"10cc", "v_______________________1___0", "233e9b257843f3dfdb1cce6676cdaf9e595ac96ee1b55031434d852bc7ac9185"}, 53 {"e1fc", "v_______________________1___1", "39c5e908ae83d0c78520c7c7bda0b3782daf594700e44546e93def8f049cca95"}, 54 {"eefc", "v_______________________1___2", "d789567559fd76fe5b7d9cc42f3750f942502ac1c7f2a466e2f690ec4b6c2a7c"}, 55 }, 56 { // {b:{a:ac, b:ac}, d:acc} 57 {"baac", "v_______________________2___0", "8be1c86ba7ec4c61e14c1a9b75055e0464c2633ae66a055a24e75450156a5d42"}, 58 {"bbac", "v_______________________2___1", "8495159b9895a7d88d973171d737c0aace6fe6ac02a4769fff1bc43bcccce4cc"}, 59 {"dacc", "v_______________________2___2", "9bcfc5b220a27328deb9dc6ee2e3d46c9ebc9c69e78acda1fa2c7040602c63ca"}, 60 }, 61 { // {0:0cccc, 2:456{0:0, 2:2} 62 {"00cccc", "v_______________________3___0", "e57dc2785b99ce9205080cb41b32ebea7ac3e158952b44c87d186e6d190a6530"}, 63 {"245600", "v_______________________3___1", "0335354adbd360a45c1871a842452287721b64b4234dfe08760b243523c998db"}, 64 {"245622", "v_______________________3___2", "9e6832db0dca2b5cf81c0e0727bfde6afc39d5de33e5720bccacc183c162104e"}, 65 }, 66 { // {1:4567{1:1c, 3:3c}, 3:0cccccc} 67 {"1456711c", "v_______________________4___0", "f2389e78d98fed99f3e63d6d1623c1d4d9e8c91cb1d585de81fbc7c0e60d3529"}, 68 {"1456733c", "v_______________________4___1", "101189b3fab852be97a0120c03d95eefcf984d3ed639f2328527de6def55a9c0"}, 69 {"30cccccc", "v_______________________4___2", "3780ce111f98d15751dfde1eb21080efc7d3914b429e5c84c64db637c55405b3"}, 70 }, 71 { // 8800{1:f, 2:e, 3:d} 72 {"88001f", "v_______________________5___0", "e817db50d84f341d443c6f6593cafda093fc85e773a762421d47daa6ac993bd5"}, 73 {"88002e", "v_______________________5___1", "d6e3e6047bdc110edd296a4d63c030aec451bee9d8075bc5a198eee8cda34f68"}, 74 {"88003d", "v_______________________5___2", "b6bdf8298c703342188e5f7f84921a402042d0e5fb059969dd53a6b6b1fb989e"}, 75 }, 76 { // 0{1:fc, 2:ec, 4:dc} 77 {"01fc", "v_______________________6___0", "693268f2ca80d32b015f61cd2c4dba5a47a6b52a14c34f8e6945fad684e7a0d5"}, 78 {"02ec", "v_______________________6___1", "e24ddd44469310c2b785a2044618874bf486d2f7822603a9b8dce58d6524d5de"}, 79 {"04dc", "v_______________________6___2", "33fc259629187bbe54b92f82f0cd8083b91a12e41a9456b84fc155321e334db7"}, 80 }, 81 { // f{0:fccc, f:ff{0:f, f:f}} 82 {"f0fccc", "v_______________________7___0", "b0966b5aa469a3e292bc5fcfa6c396ae7a657255eef552ea7e12f996de795b90"}, 83 {"ffff0f", "v_______________________7___1", "3b1ca154ec2a3d96d8d77bddef0abfe40a53a64eb03cecf78da9ec43799fa3d0"}, 84 {"ffffff", "v_______________________7___2", "e75463041f1be8252781be0ace579a44ea4387bf5b2739f4607af676f7719678"}, 85 }, 86 { // ff{0:f{0:f, f:f}, f:fcc} 87 {"ff0f0f", "v_______________________8___0", "0928af9b14718ec8262ab89df430f1e5fbf66fac0fed037aff2b6767ae8c8684"}, 88 {"ff0fff", "v_______________________8___1", "d870f4d3ce26b0bf86912810a1960693630c20a48ba56be0ad04bc3e9ddb01e6"}, 89 {"ffffcc", "v_______________________8___2", "4239f10dd9d9915ecf2e047d6a576bdc1733ed77a30830f1bf29deaf7d8e966f"}, 90 }, 91 { 92 {"123d", "x___________________________0", "fc453d88b6f128a77c448669710497380fa4588abbea9f78f4c20c80daa797d0"}, 93 {"123e", "x___________________________1", "5af48f2d8a9a015c1ff7fa8b8c7f6b676233bd320e8fb57fd7933622badd2cec"}, 94 {"123f", "x___________________________2", "1164d7299964e74ac40d761f9189b2a3987fae959800d0f7e29d3aaf3eae9e15"}, 95 }, 96 { 97 {"123d", "x___________________________0", "fc453d88b6f128a77c448669710497380fa4588abbea9f78f4c20c80daa797d0"}, 98 {"123e", "x___________________________1", "5af48f2d8a9a015c1ff7fa8b8c7f6b676233bd320e8fb57fd7933622badd2cec"}, 99 {"124a", "x___________________________2", "661a96a669869d76b7231380da0649d013301425fbea9d5c5fae6405aa31cfce"}, 100 }, 101 { 102 {"123d", "x___________________________0", "fc453d88b6f128a77c448669710497380fa4588abbea9f78f4c20c80daa797d0"}, 103 {"123e", "x___________________________1", "5af48f2d8a9a015c1ff7fa8b8c7f6b676233bd320e8fb57fd7933622badd2cec"}, 104 {"13aa", "x___________________________2", "6590120e1fd3ffd1a90e8de5bb10750b61079bb0776cca4414dd79a24e4d4356"}, 105 }, 106 { 107 {"123d", "x___________________________0", "fc453d88b6f128a77c448669710497380fa4588abbea9f78f4c20c80daa797d0"}, 108 {"123e", "x___________________________1", "5af48f2d8a9a015c1ff7fa8b8c7f6b676233bd320e8fb57fd7933622badd2cec"}, 109 {"2aaa", "x___________________________2", "f869b40e0c55eace1918332ef91563616fbf0755e2b946119679f7ef8e44b514"}, 110 }, 111 { 112 {"1234da", "x___________________________0", "1c4b4462e9f56a80ca0f5d77c0d632c41b0102290930343cf1791e971a045a79"}, 113 {"1234ea", "x___________________________1", "2f502917f3ba7d328c21c8b45ee0f160652e68450332c166d4ad02d1afe31862"}, 114 {"1234fa", "x___________________________2", "4f4e368ab367090d5bc3dbf25f7729f8bd60df84de309b4633a6b69ab66142c0"}, 115 }, 116 { 117 {"1234da", "x___________________________0", "1c4b4462e9f56a80ca0f5d77c0d632c41b0102290930343cf1791e971a045a79"}, 118 {"1234ea", "x___________________________1", "2f502917f3ba7d328c21c8b45ee0f160652e68450332c166d4ad02d1afe31862"}, 119 {"1235aa", "x___________________________2", "21840121d11a91ac8bbad9a5d06af902a5c8d56a47b85600ba813814b7bfcb9b"}, 120 }, 121 { 122 {"1234da", "x___________________________0", "1c4b4462e9f56a80ca0f5d77c0d632c41b0102290930343cf1791e971a045a79"}, 123 {"1234ea", "x___________________________1", "2f502917f3ba7d328c21c8b45ee0f160652e68450332c166d4ad02d1afe31862"}, 124 {"124aaa", "x___________________________2", "ea4040ddf6ae3fbd1524bdec19c0ab1581015996262006632027fa5cf21e441e"}, 125 }, 126 { 127 {"1234da", "x___________________________0", "1c4b4462e9f56a80ca0f5d77c0d632c41b0102290930343cf1791e971a045a79"}, 128 {"1234ea", "x___________________________1", "2f502917f3ba7d328c21c8b45ee0f160652e68450332c166d4ad02d1afe31862"}, 129 {"13aaaa", "x___________________________2", "e4beb66c67e44f2dd8ba36036e45a44ff68f8d52942472b1911a45f886a34507"}, 130 }, 131 { 132 {"1234da", "x___________________________0", "1c4b4462e9f56a80ca0f5d77c0d632c41b0102290930343cf1791e971a045a79"}, 133 {"1234ea", "x___________________________1", "2f502917f3ba7d328c21c8b45ee0f160652e68450332c166d4ad02d1afe31862"}, 134 {"2aaaaa", "x___________________________2", "5f5989b820ff5d76b7d49e77bb64f26602294f6c42a1a3becc669cd9e0dc8ec9"}, 135 }, 136 { 137 {"000000", "x___________________________0", "3b32b7af0bddc7940e7364ee18b5a59702c1825e469452c8483b9c4e0218b55a"}, 138 {"1234da", "x___________________________1", "3ab152a1285dca31945566f872c1cc2f17a770440eda32aeee46a5e91033dde2"}, 139 {"1234ea", "x___________________________2", "0cccc87f96ddef55563c1b3be3c64fff6a644333c3d9cd99852cb53b6412b9b8"}, 140 {"1234fa", "x___________________________3", "65bb3aafea8121111d693ffe34881c14d27b128fd113fa120961f251fe28428d"}, 141 }, 142 { 143 {"000000", "x___________________________0", "3b32b7af0bddc7940e7364ee18b5a59702c1825e469452c8483b9c4e0218b55a"}, 144 {"1234da", "x___________________________1", "3ab152a1285dca31945566f872c1cc2f17a770440eda32aeee46a5e91033dde2"}, 145 {"1234ea", "x___________________________2", "0cccc87f96ddef55563c1b3be3c64fff6a644333c3d9cd99852cb53b6412b9b8"}, 146 {"1235aa", "x___________________________3", "f670e4d2547c533c5f21e0045442e2ecb733f347ad6d29ef36e0f5ba31bb11a8"}, 147 }, 148 { 149 {"000000", "x___________________________0", "3b32b7af0bddc7940e7364ee18b5a59702c1825e469452c8483b9c4e0218b55a"}, 150 {"1234da", "x___________________________1", "3ab152a1285dca31945566f872c1cc2f17a770440eda32aeee46a5e91033dde2"}, 151 {"1234ea", "x___________________________2", "0cccc87f96ddef55563c1b3be3c64fff6a644333c3d9cd99852cb53b6412b9b8"}, 152 {"124aaa", "x___________________________3", "c17464123050a9a6f29b5574bb2f92f6d305c1794976b475b7fb0316b6335598"}, 153 }, 154 { 155 {"000000", "x___________________________0", "3b32b7af0bddc7940e7364ee18b5a59702c1825e469452c8483b9c4e0218b55a"}, 156 {"1234da", "x___________________________1", "3ab152a1285dca31945566f872c1cc2f17a770440eda32aeee46a5e91033dde2"}, 157 {"1234ea", "x___________________________2", "0cccc87f96ddef55563c1b3be3c64fff6a644333c3d9cd99852cb53b6412b9b8"}, 158 {"13aaaa", "x___________________________3", "aa8301be8cb52ea5cd249f5feb79fb4315ee8de2140c604033f4b3fff78f0105"}, 159 }, 160 { 161 {"0000", "x___________________________0", "cb8c09ad07ae882136f602b3f21f8733a9f5a78f1d2525a8d24d1c13258000b2"}, 162 {"123d", "x___________________________1", "8f09663deb02f08958136410dc48565e077f76bb6c9d8c84d35fc8913a657d31"}, 163 {"123e", "x___________________________2", "0d230561e398c579e09a9f7b69ceaf7d3970f5a436fdb28b68b7a37c5bdd6b80"}, 164 {"123f", "x___________________________3", "80f7bad1893ca57e3443bb3305a517723a74d3ba831bcaca22a170645eb7aafb"}, 165 }, 166 { 167 {"0000", "x___________________________0", "cb8c09ad07ae882136f602b3f21f8733a9f5a78f1d2525a8d24d1c13258000b2"}, 168 {"123d", "x___________________________1", "8f09663deb02f08958136410dc48565e077f76bb6c9d8c84d35fc8913a657d31"}, 169 {"123e", "x___________________________2", "0d230561e398c579e09a9f7b69ceaf7d3970f5a436fdb28b68b7a37c5bdd6b80"}, 170 {"124a", "x___________________________3", "383bc1bb4f019e6bc4da3751509ea709b58dd1ac46081670834bae072f3e9557"}, 171 }, 172 { 173 {"0000", "x___________________________0", "cb8c09ad07ae882136f602b3f21f8733a9f5a78f1d2525a8d24d1c13258000b2"}, 174 {"123d", "x___________________________1", "8f09663deb02f08958136410dc48565e077f76bb6c9d8c84d35fc8913a657d31"}, 175 {"123e", "x___________________________2", "0d230561e398c579e09a9f7b69ceaf7d3970f5a436fdb28b68b7a37c5bdd6b80"}, 176 {"13aa", "x___________________________3", "ff0dc70ce2e5db90ee42a4c2ad12139596b890e90eb4e16526ab38fa465b35cf"}, 177 }, 178 } 179 st := NewStackTrie(nil) 180 for i, test := range tests { 181 // The StackTrie does not allow Insert(), Hash(), Insert(), ... 182 // so we will create new trie for every sequence length of inserts. 183 for l := 1; l <= len(test); l++ { 184 st.Reset() 185 for j := 0; j < l; j++ { 186 kv := &test[j] 187 if err := st.Update(common.FromHex(kv.K), []byte(kv.V)); err != nil { 188 t.Fatal(err) 189 } 190 } 191 expected := common.HexToHash(test[l-1].H) 192 if h := st.Hash(); h != expected { 193 t.Errorf("%d(%d): root hash mismatch: %x, expected %x", i, l, h, expected) 194 } 195 } 196 } 197 } 198 199 func TestSizeBug(t *testing.T) { 200 st := NewStackTrie(nil) 201 nt := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) 202 203 leaf := common.FromHex("290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563") 204 value := common.FromHex("94cf40d0d2b44f2b66e07cace1372ca42b73cf21a3") 205 206 nt.Update(leaf, value) 207 st.Update(leaf, value) 208 209 if nt.Hash() != st.Hash() { 210 t.Fatalf("error %x != %x", st.Hash(), nt.Hash()) 211 } 212 } 213 214 func TestEmptyBug(t *testing.T) { 215 st := NewStackTrie(nil) 216 nt := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) 217 218 //leaf := common.FromHex("290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563") 219 //value := common.FromHex("94cf40d0d2b44f2b66e07cace1372ca42b73cf21a3") 220 kvs := []struct { 221 K string 222 V string 223 }{ 224 {K: "405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace", V: "9496f4ec2bf9dab484cac6be589e8417d84781be08"}, 225 {K: "40edb63a35fcf86c08022722aa3287cdd36440d671b4918131b2514795fefa9c", V: "01"}, 226 {K: "b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6", V: "947a30f7736e48d6599356464ba4c150d8da0302ff"}, 227 {K: "c2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b", V: "02"}, 228 } 229 230 for _, kv := range kvs { 231 nt.Update(common.FromHex(kv.K), common.FromHex(kv.V)) 232 st.Update(common.FromHex(kv.K), common.FromHex(kv.V)) 233 } 234 235 if nt.Hash() != st.Hash() { 236 t.Fatalf("error %x != %x", st.Hash(), nt.Hash()) 237 } 238 } 239 240 func TestValLength56(t *testing.T) { 241 st := NewStackTrie(nil) 242 nt := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) 243 244 //leaf := common.FromHex("290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563") 245 //value := common.FromHex("94cf40d0d2b44f2b66e07cace1372ca42b73cf21a3") 246 kvs := []struct { 247 K string 248 V string 249 }{ 250 {K: "405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace", V: "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"}, 251 } 252 253 for _, kv := range kvs { 254 nt.Update(common.FromHex(kv.K), common.FromHex(kv.V)) 255 st.Update(common.FromHex(kv.K), common.FromHex(kv.V)) 256 } 257 258 if nt.Hash() != st.Hash() { 259 t.Fatalf("error %x != %x", st.Hash(), nt.Hash()) 260 } 261 } 262 263 // TestUpdateSmallNodes tests a case where the leaves are small (both key and value), 264 // which causes a lot of node-within-node. This case was found via fuzzing. 265 func TestUpdateSmallNodes(t *testing.T) { 266 st := NewStackTrie(nil) 267 nt := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) 268 kvs := []struct { 269 K string 270 V string 271 }{ 272 {"63303030", "3041"}, // stacktrie.Update 273 {"65", "3000"}, // stacktrie.Update 274 } 275 for _, kv := range kvs { 276 nt.Update(common.FromHex(kv.K), common.FromHex(kv.V)) 277 st.Update(common.FromHex(kv.K), common.FromHex(kv.V)) 278 } 279 if nt.Hash() != st.Hash() { 280 t.Fatalf("error %x != %x", st.Hash(), nt.Hash()) 281 } 282 } 283 284 // TestUpdateVariableKeys contains a case which stacktrie fails: when keys of different 285 // sizes are used, and the second one has the same prefix as the first, then the 286 // stacktrie fails, since it's unable to 'expand' on an already added leaf. 287 // For all practical purposes, this is fine, since keys are fixed-size length 288 // in account and storage tries. 289 // 290 // The test is marked as 'skipped', and exists just to have the behaviour documented. 291 // This case was found via fuzzing. 292 func TestUpdateVariableKeys(t *testing.T) { 293 t.SkipNow() 294 st := NewStackTrie(nil) 295 nt := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) 296 kvs := []struct { 297 K string 298 V string 299 }{ 300 {"0x33303534636532393561313031676174", "303030"}, 301 {"0x3330353463653239356131303167617430", "313131"}, 302 } 303 for _, kv := range kvs { 304 nt.Update(common.FromHex(kv.K), common.FromHex(kv.V)) 305 st.Update(common.FromHex(kv.K), common.FromHex(kv.V)) 306 } 307 if nt.Hash() != st.Hash() { 308 t.Fatalf("error %x != %x", st.Hash(), nt.Hash()) 309 } 310 } 311 312 // TestStacktrieNotModifyValues checks that inserting blobs of data into the 313 // stacktrie does not mutate the blobs 314 func TestStacktrieNotModifyValues(t *testing.T) { 315 st := NewStackTrie(nil) 316 { // Test a very small trie 317 // Give it the value as a slice with large backing alloc, 318 // so if the stacktrie tries to append, it won't have to realloc 319 value := make([]byte, 1, 100) 320 value[0] = 0x2 321 want := common.CopyBytes(value) 322 st.Update([]byte{0x01}, value) 323 st.Hash() 324 if have := value; !bytes.Equal(have, want) { 325 t.Fatalf("tiny trie: have %#x want %#x", have, want) 326 } 327 st = NewStackTrie(nil) 328 } 329 // Test with a larger trie 330 keyB := big.NewInt(1) 331 keyDelta := big.NewInt(1) 332 var vals [][]byte 333 getValue := func(i int) []byte { 334 if i%2 == 0 { // large 335 return crypto.Keccak256(big.NewInt(int64(i)).Bytes()) 336 } else { //small 337 return big.NewInt(int64(i)).Bytes() 338 } 339 } 340 for i := 0; i < 1000; i++ { 341 key := common.BigToHash(keyB) 342 value := getValue(i) 343 st.Update(key.Bytes(), value) 344 vals = append(vals, value) 345 keyB = keyB.Add(keyB, keyDelta) 346 keyDelta.Add(keyDelta, common.Big1) 347 } 348 st.Hash() 349 for i := 0; i < 1000; i++ { 350 want := getValue(i) 351 352 have := vals[i] 353 if !bytes.Equal(have, want) { 354 t.Fatalf("item %d, have %#x want %#x", i, have, want) 355 } 356 } 357 } 358 359 // TestStacktrieSerialization tests that the stacktrie works well if we 360 // serialize/unserialize it a lot 361 func TestStacktrieSerialization(t *testing.T) { 362 var ( 363 st = NewStackTrie(nil) 364 nt = NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) 365 keyB = big.NewInt(1) 366 keyDelta = big.NewInt(1) 367 vals [][]byte 368 keys [][]byte 369 ) 370 getValue := func(i int) []byte { 371 if i%2 == 0 { // large 372 return crypto.Keccak256(big.NewInt(int64(i)).Bytes()) 373 } else { //small 374 return big.NewInt(int64(i)).Bytes() 375 } 376 } 377 for i := 0; i < 10; i++ { 378 vals = append(vals, getValue(i)) 379 keys = append(keys, common.BigToHash(keyB).Bytes()) 380 keyB = keyB.Add(keyB, keyDelta) 381 keyDelta.Add(keyDelta, common.Big1) 382 } 383 for i, k := range keys { 384 nt.Update(k, common.CopyBytes(vals[i])) 385 } 386 387 for i, k := range keys { 388 blob, err := st.MarshalBinary() 389 if err != nil { 390 t.Fatal(err) 391 } 392 newSt, err := NewFromBinary(blob, nil) 393 if err != nil { 394 t.Fatal(err) 395 } 396 st = newSt 397 st.Update(k, common.CopyBytes(vals[i])) 398 } 399 if have, want := st.Hash(), nt.Hash(); have != want { 400 t.Fatalf("have %#x want %#x", have, want) 401 } 402 }