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