github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/core/state/state_test.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package state 18 19 import ( 20 "bytes" 21 "math/big" 22 "testing" 23 24 "github.com/scroll-tech/go-ethereum/common" 25 "github.com/scroll-tech/go-ethereum/core/rawdb" 26 "github.com/scroll-tech/go-ethereum/ethdb" 27 "github.com/scroll-tech/go-ethereum/trie" 28 ) 29 30 type stateTest struct { 31 db ethdb.Database 32 state *StateDB 33 } 34 35 func newStateTest() *stateTest { 36 db := rawdb.NewMemoryDatabase() 37 sdb, _ := New(common.Hash{}, NewDatabase(db), nil) 38 return &stateTest{db: db, state: sdb} 39 } 40 41 func TestDump(t *testing.T) { 42 db := rawdb.NewMemoryDatabase() 43 sdb, _ := New(common.Hash{}, NewDatabaseWithConfig(db, &trie.Config{Preimages: true}), nil) 44 s := &stateTest{db: db, state: sdb} 45 46 // generate a few entries 47 obj1 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01})) 48 obj1.AddBalance(big.NewInt(22)) 49 obj2 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01, 0x02})) 50 obj2.SetCode([]byte{3, 3, 3, 3, 3, 3, 3}) 51 obj3 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x02})) 52 obj3.SetBalance(big.NewInt(44)) 53 54 // write some of them to the trie 55 s.state.updateStateObject(obj1) 56 s.state.updateStateObject(obj2) 57 s.state.Commit(false) 58 59 // check that DumpToCollector contains the state objects that are in trie 60 got := string(s.state.Dump(nil)) 61 want := `{ 62 "root": "789955993afb9d2a04b957a91be5d7b139aabb60fb7af63df6405021211c13c4", 63 "accounts": { 64 "0x0000000000000000000000000000000000000001": { 65 "balance": "22", 66 "nonce": 0, 67 "root": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", 68 "keccakCodeHash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", 69 "poseidonCodeHash": "0x2098f5fb9e239eab3ceac3f27b81e481dc3124d55ffed523a839ee8446b64864", 70 "codeSize": 0, 71 "key": "0x1468288056310c82aa4c01a7e12a10f8111a0560e72b700555479031b86c357d" 72 }, 73 "0x0000000000000000000000000000000000000002": { 74 "balance": "44", 75 "nonce": 0, 76 "root": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", 77 "keccakCodeHash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", 78 "poseidonCodeHash": "0x2098f5fb9e239eab3ceac3f27b81e481dc3124d55ffed523a839ee8446b64864", 79 "codeSize": 0, 80 "key": "0xd52688a8f926c816ca1e079067caba944f158e764817b83fc43594370ca9cf62" 81 }, 82 "0x0000000000000000000000000000000000000102": { 83 "balance": "0", 84 "nonce": 0, 85 "root": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", 86 "keccakCodeHash": "0x87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3", 87 "poseidonCodeHash": "0x1f090de833dd6dee7af5ee49f94fd64d1079aee3df47795eaaf2775d6921458c", 88 "codeSize": 7, 89 "code": "0x03030303030303", 90 "key": "0xa17eacbc25cda025e81db9c5c62868822c73ce097cee2a63e33a2e41268358a1" 91 } 92 } 93 }` 94 if got != want { 95 t.Errorf("DumpToCollector mismatch:\ngot: %s\nwant: %s\n", got, want) 96 } 97 } 98 99 func TestNull(t *testing.T) { 100 s := newStateTest() 101 address := common.HexToAddress("0x823140710bf13990e4500136726d8b55") 102 s.state.CreateAccount(address) 103 //value := common.FromHex("0x823140710bf13990e4500136726d8b55") 104 var value common.Hash 105 106 s.state.SetState(address, common.Hash{}, value) 107 s.state.Commit(false) 108 109 if value := s.state.GetState(address, common.Hash{}); value != (common.Hash{}) { 110 t.Errorf("expected empty current value, got %x", value) 111 } 112 if value := s.state.GetCommittedState(address, common.Hash{}); value != (common.Hash{}) { 113 t.Errorf("expected empty committed value, got %x", value) 114 } 115 } 116 117 func TestSnapshot(t *testing.T) { 118 stateobjaddr := common.BytesToAddress([]byte("aa")) 119 var storageaddr common.Hash 120 data1 := common.BytesToHash([]byte{42}) 121 data2 := common.BytesToHash([]byte{43}) 122 s := newStateTest() 123 124 // snapshot the genesis state 125 genesis := s.state.Snapshot() 126 127 // set initial state object value 128 s.state.SetState(stateobjaddr, storageaddr, data1) 129 snapshot := s.state.Snapshot() 130 131 // set a new state object value, revert it and ensure correct content 132 s.state.SetState(stateobjaddr, storageaddr, data2) 133 s.state.RevertToSnapshot(snapshot) 134 135 if v := s.state.GetState(stateobjaddr, storageaddr); v != data1 { 136 t.Errorf("wrong storage value %v, want %v", v, data1) 137 } 138 if v := s.state.GetCommittedState(stateobjaddr, storageaddr); v != (common.Hash{}) { 139 t.Errorf("wrong committed storage value %v, want %v", v, common.Hash{}) 140 } 141 142 // revert up to the genesis state and ensure correct content 143 s.state.RevertToSnapshot(genesis) 144 if v := s.state.GetState(stateobjaddr, storageaddr); v != (common.Hash{}) { 145 t.Errorf("wrong storage value %v, want %v", v, common.Hash{}) 146 } 147 if v := s.state.GetCommittedState(stateobjaddr, storageaddr); v != (common.Hash{}) { 148 t.Errorf("wrong committed storage value %v, want %v", v, common.Hash{}) 149 } 150 } 151 152 func TestSnapshotEmpty(t *testing.T) { 153 s := newStateTest() 154 s.state.RevertToSnapshot(s.state.Snapshot()) 155 } 156 157 func TestSnapshot2(t *testing.T) { 158 state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil) 159 160 stateobjaddr0 := common.BytesToAddress([]byte("so0")) 161 stateobjaddr1 := common.BytesToAddress([]byte("so1")) 162 var storageaddr common.Hash 163 164 data0 := common.BytesToHash([]byte{17}) 165 data1 := common.BytesToHash([]byte{18}) 166 167 state.SetState(stateobjaddr0, storageaddr, data0) 168 state.SetState(stateobjaddr1, storageaddr, data1) 169 170 // db, trie are already non-empty values 171 so0 := state.getStateObject(stateobjaddr0) 172 so0.SetBalance(big.NewInt(42)) 173 so0.SetNonce(43) 174 so0.SetCode([]byte{'c', 'a', 'f', 'e'}) 175 so0.suicided = false 176 so0.deleted = false 177 state.setStateObject(so0) 178 179 root, _ := state.Commit(false) 180 state, _ = New(root, state.db, state.snaps) 181 182 // and one with deleted == true 183 so1 := state.getStateObject(stateobjaddr1) 184 so1.SetBalance(big.NewInt(52)) 185 so1.SetNonce(53) 186 so1.SetCode([]byte{'c', 'a', 'f', 'e', '2'}) 187 so1.suicided = true 188 so1.deleted = true 189 state.setStateObject(so1) 190 191 so1 = state.getStateObject(stateobjaddr1) 192 if so1 != nil { 193 t.Fatalf("deleted object not nil when getting") 194 } 195 196 snapshot := state.Snapshot() 197 state.RevertToSnapshot(snapshot) 198 199 so0Restored := state.getStateObject(stateobjaddr0) 200 // Update lazily-loaded values before comparing. 201 so0Restored.GetState(state.db, storageaddr) 202 so0Restored.Code(state.db) 203 // non-deleted is equal (restored) 204 compareStateObjects(so0Restored, so0, t) 205 206 // deleted should be nil, both before and after restore of state copy 207 so1Restored := state.getStateObject(stateobjaddr1) 208 if so1Restored != nil { 209 t.Fatalf("deleted object not nil after restoring snapshot: %+v", so1Restored) 210 } 211 } 212 213 func compareStateObjects(so0, so1 *stateObject, t *testing.T) { 214 if so0.Address() != so1.Address() { 215 t.Fatalf("Address mismatch: have %v, want %v", so0.address, so1.address) 216 } 217 if so0.Balance().Cmp(so1.Balance()) != 0 { 218 t.Fatalf("Balance mismatch: have %v, want %v", so0.Balance(), so1.Balance()) 219 } 220 if so0.Nonce() != so1.Nonce() { 221 t.Fatalf("Nonce mismatch: have %v, want %v", so0.Nonce(), so1.Nonce()) 222 } 223 if so0.data.Root != so1.data.Root { 224 t.Errorf("Root mismatch: have %x, want %x", so0.data.Root[:], so1.data.Root[:]) 225 } 226 if !bytes.Equal(so0.KeccakCodeHash(), so1.KeccakCodeHash()) { 227 t.Fatalf("KeccakCodeHash mismatch: have %v, want %v", so0.KeccakCodeHash(), so1.KeccakCodeHash()) 228 } 229 if !bytes.Equal(so0.PoseidonCodeHash(), so1.PoseidonCodeHash()) { 230 t.Fatalf("PoseidonCodeHash mismatch: have %v, want %v", so0.PoseidonCodeHash(), so1.PoseidonCodeHash()) 231 } 232 if so0.CodeSize() != so1.CodeSize() { 233 t.Fatalf("CodeSize mismatch: have %v, want %v", so0.CodeSize(), so1.CodeSize()) 234 } 235 if !bytes.Equal(so0.code, so1.code) { 236 t.Fatalf("Code mismatch: have %v, want %v", so0.code, so1.code) 237 } 238 239 if len(so1.dirtyStorage) != len(so0.dirtyStorage) { 240 t.Errorf("Dirty storage size mismatch: have %d, want %d", len(so1.dirtyStorage), len(so0.dirtyStorage)) 241 } 242 for k, v := range so1.dirtyStorage { 243 if so0.dirtyStorage[k] != v { 244 t.Errorf("Dirty storage key %x mismatch: have %v, want %v", k, so0.dirtyStorage[k], v) 245 } 246 } 247 for k, v := range so0.dirtyStorage { 248 if so1.dirtyStorage[k] != v { 249 t.Errorf("Dirty storage key %x mismatch: have %v, want none.", k, v) 250 } 251 } 252 if len(so1.originStorage) != len(so0.originStorage) { 253 t.Errorf("Origin storage size mismatch: have %d, want %d", len(so1.originStorage), len(so0.originStorage)) 254 } 255 for k, v := range so1.originStorage { 256 if so0.originStorage[k] != v { 257 t.Errorf("Origin storage key %x mismatch: have %v, want %v", k, so0.originStorage[k], v) 258 } 259 } 260 for k, v := range so0.originStorage { 261 if so1.originStorage[k] != v { 262 t.Errorf("Origin storage key %x mismatch: have %v, want none.", k, v) 263 } 264 } 265 }