github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/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/tirogen/go-ethereum/common" 25 "github.com/tirogen/go-ethereum/core/rawdb" 26 "github.com/tirogen/go-ethereum/crypto" 27 "github.com/tirogen/go-ethereum/ethdb" 28 "github.com/tirogen/go-ethereum/trie" 29 ) 30 31 type stateTest struct { 32 db ethdb.Database 33 state *StateDB 34 } 35 36 func newStateTest() *stateTest { 37 db := rawdb.NewMemoryDatabase() 38 sdb, _ := New(common.Hash{}, NewDatabase(db), nil) 39 return &stateTest{db: db, state: sdb} 40 } 41 42 func TestDump(t *testing.T) { 43 db := rawdb.NewMemoryDatabase() 44 sdb, _ := New(common.Hash{}, NewDatabaseWithConfig(db, &trie.Config{Preimages: true}), nil) 45 s := &stateTest{db: db, state: sdb} 46 47 // generate a few entries 48 obj1 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01})) 49 obj1.AddBalance(big.NewInt(22)) 50 obj2 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01, 0x02})) 51 obj2.SetCode(crypto.Keccak256Hash([]byte{3, 3, 3, 3, 3, 3, 3}), []byte{3, 3, 3, 3, 3, 3, 3}) 52 obj3 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x02})) 53 obj3.SetBalance(big.NewInt(44)) 54 55 // write some of them to the trie 56 s.state.updateStateObject(obj1) 57 s.state.updateStateObject(obj2) 58 s.state.Commit(false) 59 60 // check that DumpToCollector contains the state objects that are in trie 61 got := string(s.state.Dump(nil)) 62 want := `{ 63 "root": "71edff0130dd2385947095001c73d9e28d862fc286fca2b922ca6f6f3cddfdd2", 64 "accounts": { 65 "0x0000000000000000000000000000000000000001": { 66 "balance": "22", 67 "nonce": 0, 68 "root": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", 69 "codeHash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", 70 "key": "0x1468288056310c82aa4c01a7e12a10f8111a0560e72b700555479031b86c357d" 71 }, 72 "0x0000000000000000000000000000000000000002": { 73 "balance": "44", 74 "nonce": 0, 75 "root": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", 76 "codeHash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", 77 "key": "0xd52688a8f926c816ca1e079067caba944f158e764817b83fc43594370ca9cf62" 78 }, 79 "0x0000000000000000000000000000000000000102": { 80 "balance": "0", 81 "nonce": 0, 82 "root": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", 83 "codeHash": "0x87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3", 84 "code": "0x03030303030303", 85 "key": "0xa17eacbc25cda025e81db9c5c62868822c73ce097cee2a63e33a2e41268358a1" 86 } 87 } 88 }` 89 if got != want { 90 t.Errorf("DumpToCollector mismatch:\ngot: %s\nwant: %s\n", got, want) 91 } 92 } 93 94 func TestNull(t *testing.T) { 95 s := newStateTest() 96 address := common.HexToAddress("0x823140710bf13990e4500136726d8b55") 97 s.state.CreateAccount(address) 98 //value := common.FromHex("0x823140710bf13990e4500136726d8b55") 99 var value common.Hash 100 101 s.state.SetState(address, common.Hash{}, value) 102 s.state.Commit(false) 103 104 if value := s.state.GetState(address, common.Hash{}); value != (common.Hash{}) { 105 t.Errorf("expected empty current value, got %x", value) 106 } 107 if value := s.state.GetCommittedState(address, common.Hash{}); value != (common.Hash{}) { 108 t.Errorf("expected empty committed value, got %x", value) 109 } 110 } 111 112 func TestSnapshot(t *testing.T) { 113 stateobjaddr := common.BytesToAddress([]byte("aa")) 114 var storageaddr common.Hash 115 data1 := common.BytesToHash([]byte{42}) 116 data2 := common.BytesToHash([]byte{43}) 117 s := newStateTest() 118 119 // snapshot the genesis state 120 genesis := s.state.Snapshot() 121 122 // set initial state object value 123 s.state.SetState(stateobjaddr, storageaddr, data1) 124 snapshot := s.state.Snapshot() 125 126 // set a new state object value, revert it and ensure correct content 127 s.state.SetState(stateobjaddr, storageaddr, data2) 128 s.state.RevertToSnapshot(snapshot) 129 130 if v := s.state.GetState(stateobjaddr, storageaddr); v != data1 { 131 t.Errorf("wrong storage value %v, want %v", v, data1) 132 } 133 if v := s.state.GetCommittedState(stateobjaddr, storageaddr); v != (common.Hash{}) { 134 t.Errorf("wrong committed storage value %v, want %v", v, common.Hash{}) 135 } 136 137 // revert up to the genesis state and ensure correct content 138 s.state.RevertToSnapshot(genesis) 139 if v := s.state.GetState(stateobjaddr, storageaddr); v != (common.Hash{}) { 140 t.Errorf("wrong storage value %v, want %v", v, common.Hash{}) 141 } 142 if v := s.state.GetCommittedState(stateobjaddr, storageaddr); v != (common.Hash{}) { 143 t.Errorf("wrong committed storage value %v, want %v", v, common.Hash{}) 144 } 145 } 146 147 func TestSnapshotEmpty(t *testing.T) { 148 s := newStateTest() 149 s.state.RevertToSnapshot(s.state.Snapshot()) 150 } 151 152 func TestSnapshot2(t *testing.T) { 153 state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil) 154 155 stateobjaddr0 := common.BytesToAddress([]byte("so0")) 156 stateobjaddr1 := common.BytesToAddress([]byte("so1")) 157 var storageaddr common.Hash 158 159 data0 := common.BytesToHash([]byte{17}) 160 data1 := common.BytesToHash([]byte{18}) 161 162 state.SetState(stateobjaddr0, storageaddr, data0) 163 state.SetState(stateobjaddr1, storageaddr, data1) 164 165 // db, trie are already non-empty values 166 so0 := state.getStateObject(stateobjaddr0) 167 so0.SetBalance(big.NewInt(42)) 168 so0.SetNonce(43) 169 so0.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e'}), []byte{'c', 'a', 'f', 'e'}) 170 so0.suicided = false 171 so0.deleted = false 172 state.setStateObject(so0) 173 174 root, _ := state.Commit(false) 175 state, _ = New(root, state.db, state.snaps) 176 177 // and one with deleted == true 178 so1 := state.getStateObject(stateobjaddr1) 179 so1.SetBalance(big.NewInt(52)) 180 so1.SetNonce(53) 181 so1.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e', '2'}), []byte{'c', 'a', 'f', 'e', '2'}) 182 so1.suicided = true 183 so1.deleted = true 184 state.setStateObject(so1) 185 186 so1 = state.getStateObject(stateobjaddr1) 187 if so1 != nil { 188 t.Fatalf("deleted object not nil when getting") 189 } 190 191 snapshot := state.Snapshot() 192 state.RevertToSnapshot(snapshot) 193 194 so0Restored := state.getStateObject(stateobjaddr0) 195 // Update lazily-loaded values before comparing. 196 so0Restored.GetState(state.db, storageaddr) 197 so0Restored.Code(state.db) 198 // non-deleted is equal (restored) 199 compareStateObjects(so0Restored, so0, t) 200 201 // deleted should be nil, both before and after restore of state copy 202 so1Restored := state.getStateObject(stateobjaddr1) 203 if so1Restored != nil { 204 t.Fatalf("deleted object not nil after restoring snapshot: %+v", so1Restored) 205 } 206 } 207 208 func compareStateObjects(so0, so1 *stateObject, t *testing.T) { 209 if so0.Address() != so1.Address() { 210 t.Fatalf("Address mismatch: have %v, want %v", so0.address, so1.address) 211 } 212 if so0.Balance().Cmp(so1.Balance()) != 0 { 213 t.Fatalf("Balance mismatch: have %v, want %v", so0.Balance(), so1.Balance()) 214 } 215 if so0.Nonce() != so1.Nonce() { 216 t.Fatalf("Nonce mismatch: have %v, want %v", so0.Nonce(), so1.Nonce()) 217 } 218 if so0.data.Root != so1.data.Root { 219 t.Errorf("Root mismatch: have %x, want %x", so0.data.Root[:], so1.data.Root[:]) 220 } 221 if !bytes.Equal(so0.CodeHash(), so1.CodeHash()) { 222 t.Fatalf("CodeHash mismatch: have %v, want %v", so0.CodeHash(), so1.CodeHash()) 223 } 224 if !bytes.Equal(so0.code, so1.code) { 225 t.Fatalf("Code mismatch: have %v, want %v", so0.code, so1.code) 226 } 227 228 if len(so1.dirtyStorage) != len(so0.dirtyStorage) { 229 t.Errorf("Dirty storage size mismatch: have %d, want %d", len(so1.dirtyStorage), len(so0.dirtyStorage)) 230 } 231 for k, v := range so1.dirtyStorage { 232 if so0.dirtyStorage[k] != v { 233 t.Errorf("Dirty storage key %x mismatch: have %v, want %v", k, so0.dirtyStorage[k], v) 234 } 235 } 236 for k, v := range so0.dirtyStorage { 237 if so1.dirtyStorage[k] != v { 238 t.Errorf("Dirty storage key %x mismatch: have %v, want none.", k, v) 239 } 240 } 241 if len(so1.originStorage) != len(so0.originStorage) { 242 t.Errorf("Origin storage size mismatch: have %d, want %d", len(so1.originStorage), len(so0.originStorage)) 243 } 244 for k, v := range so1.originStorage { 245 if so0.originStorage[k] != v { 246 t.Errorf("Origin storage key %x mismatch: have %v, want %v", k, so0.originStorage[k], v) 247 } 248 } 249 for k, v := range so0.originStorage { 250 if so1.originStorage[k] != v { 251 t.Errorf("Origin storage key %x mismatch: have %v, want none.", k, v) 252 } 253 } 254 }