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