github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/state/state_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package state 19 20 import ( 21 "bytes" 22 "math/big" 23 "testing" 24 25 "github.com/AigarNetwork/aigar/common" 26 "github.com/AigarNetwork/aigar/core/rawdb" 27 "github.com/AigarNetwork/aigar/crypto" 28 "github.com/AigarNetwork/aigar/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) SetUpTest(c *checker.C) { 87 s.db = rawdb.NewMemoryDatabase() 88 s.state, _ = New(common.Hash{}, NewDatabase(s.db)) 89 } 90 91 func (s *StateSuite) TestNull(c *checker.C) { 92 address := common.HexToAddress("0x823140710bf13990e4500136726d8b55") 93 s.state.CreateAccount(address) 94 //value := common.FromHex("0x823140710bf13990e4500136726d8b55") 95 var value common.Hash 96 97 s.state.SetState(address, common.Hash{}, value) 98 s.state.Commit(false) 99 100 if value := s.state.GetState(address, common.Hash{}); value != (common.Hash{}) { 101 c.Errorf("expected empty current value, got %x", value) 102 } 103 if value := s.state.GetCommittedState(address, common.Hash{}); value != (common.Hash{}) { 104 c.Errorf("expected empty committed value, got %x", value) 105 } 106 } 107 108 func (s *StateSuite) TestSnapshot(c *checker.C) { 109 stateobjaddr := toAddr([]byte("aa")) 110 var storageaddr common.Hash 111 data1 := common.BytesToHash([]byte{42}) 112 data2 := common.BytesToHash([]byte{43}) 113 114 // snapshot the genesis state 115 genesis := s.state.Snapshot() 116 117 // set initial state object value 118 s.state.SetState(stateobjaddr, storageaddr, data1) 119 snapshot := s.state.Snapshot() 120 121 // set a new state object value, revert it and ensure correct content 122 s.state.SetState(stateobjaddr, storageaddr, data2) 123 s.state.RevertToSnapshot(snapshot) 124 125 c.Assert(s.state.GetState(stateobjaddr, storageaddr), checker.DeepEquals, data1) 126 c.Assert(s.state.GetCommittedState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{}) 127 128 // revert up to the genesis state and ensure correct content 129 s.state.RevertToSnapshot(genesis) 130 c.Assert(s.state.GetState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{}) 131 c.Assert(s.state.GetCommittedState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{}) 132 } 133 134 func (s *StateSuite) TestSnapshotEmpty(c *checker.C) { 135 s.state.RevertToSnapshot(s.state.Snapshot()) 136 } 137 138 // use testing instead of checker because checker does not support 139 // printing/logging in tests (-check.vv does not work) 140 func TestSnapshot2(t *testing.T) { 141 state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase())) 142 143 stateobjaddr0 := toAddr([]byte("so0")) 144 stateobjaddr1 := toAddr([]byte("so1")) 145 var storageaddr common.Hash 146 147 data0 := common.BytesToHash([]byte{17}) 148 data1 := common.BytesToHash([]byte{18}) 149 150 state.SetState(stateobjaddr0, storageaddr, data0) 151 state.SetState(stateobjaddr1, storageaddr, data1) 152 153 // db, trie are already non-empty values 154 so0 := state.getStateObject(stateobjaddr0) 155 so0.SetBalance(big.NewInt(42)) 156 so0.SetNonce(43) 157 so0.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e'}), []byte{'c', 'a', 'f', 'e'}) 158 so0.suicided = false 159 so0.deleted = false 160 state.setStateObject(so0) 161 162 root, _ := state.Commit(false) 163 state.Reset(root) 164 165 // and one with deleted == true 166 so1 := state.getStateObject(stateobjaddr1) 167 so1.SetBalance(big.NewInt(52)) 168 so1.SetNonce(53) 169 so1.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e', '2'}), []byte{'c', 'a', 'f', 'e', '2'}) 170 so1.suicided = true 171 so1.deleted = true 172 state.setStateObject(so1) 173 174 so1 = state.getStateObject(stateobjaddr1) 175 if so1 != nil { 176 t.Fatalf("deleted object not nil when getting") 177 } 178 179 snapshot := state.Snapshot() 180 state.RevertToSnapshot(snapshot) 181 182 so0Restored := state.getStateObject(stateobjaddr0) 183 // Update lazily-loaded values before comparing. 184 so0Restored.GetState(state.db, storageaddr) 185 so0Restored.Code(state.db) 186 // non-deleted is equal (restored) 187 compareStateObjects(so0Restored, so0, t) 188 189 // deleted should be nil, both before and after restore of state copy 190 so1Restored := state.getStateObject(stateobjaddr1) 191 if so1Restored != nil { 192 t.Fatalf("deleted object not nil after restoring snapshot: %+v", so1Restored) 193 } 194 } 195 196 func compareStateObjects(so0, so1 *stateObject, t *testing.T) { 197 if so0.Address() != so1.Address() { 198 t.Fatalf("Address mismatch: have %v, want %v", so0.address, so1.address) 199 } 200 if so0.Balance().Cmp(so1.Balance()) != 0 { 201 t.Fatalf("Balance mismatch: have %v, want %v", so0.Balance(), so1.Balance()) 202 } 203 if so0.Nonce() != so1.Nonce() { 204 t.Fatalf("Nonce mismatch: have %v, want %v", so0.Nonce(), so1.Nonce()) 205 } 206 if so0.data.Root != so1.data.Root { 207 t.Errorf("Root mismatch: have %x, want %x", so0.data.Root[:], so1.data.Root[:]) 208 } 209 if !bytes.Equal(so0.CodeHash(), so1.CodeHash()) { 210 t.Fatalf("CodeHash mismatch: have %v, want %v", so0.CodeHash(), so1.CodeHash()) 211 } 212 if !bytes.Equal(so0.code, so1.code) { 213 t.Fatalf("Code mismatch: have %v, want %v", so0.code, so1.code) 214 } 215 216 if len(so1.dirtyStorage) != len(so0.dirtyStorage) { 217 t.Errorf("Dirty storage size mismatch: have %d, want %d", len(so1.dirtyStorage), len(so0.dirtyStorage)) 218 } 219 for k, v := range so1.dirtyStorage { 220 if so0.dirtyStorage[k] != v { 221 t.Errorf("Dirty storage key %x mismatch: have %v, want %v", k, so0.dirtyStorage[k], v) 222 } 223 } 224 for k, v := range so0.dirtyStorage { 225 if so1.dirtyStorage[k] != v { 226 t.Errorf("Dirty storage key %x mismatch: have %v, want none.", k, v) 227 } 228 } 229 if len(so1.originStorage) != len(so0.originStorage) { 230 t.Errorf("Origin storage size mismatch: have %d, want %d", len(so1.originStorage), len(so0.originStorage)) 231 } 232 for k, v := range so1.originStorage { 233 if so0.originStorage[k] != v { 234 t.Errorf("Origin storage key %x mismatch: have %v, want %v", k, so0.originStorage[k], v) 235 } 236 } 237 for k, v := range so0.originStorage { 238 if so1.originStorage[k] != v { 239 t.Errorf("Origin storage key %x mismatch: have %v, want none.", k, v) 240 } 241 } 242 }