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