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