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