github.com/haliliceylan/bsc@v1.1.10-0.20220501224556-eb78d644ebcb/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/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/core/rawdb"
    26  	"github.com/ethereum/go-ethereum/crypto"
    27  	"github.com/ethereum/go-ethereum/ethdb"
    28  )
    29  
    30  type stateTest struct {
    31  	db    ethdb.Database
    32  	state *StateDB
    33  }
    34  
    35  func newStateTest() *stateTest {
    36  	db := rawdb.NewMemoryDatabase()
    37  	sdb, _ := New(common.Hash{}, NewDatabase(db), nil)
    38  	return &stateTest{db: db, state: sdb}
    39  }
    40  
    41  func TestDump(t *testing.T) {
    42  	db := rawdb.NewMemoryDatabase()
    43  	sdb, _ := New(common.Hash{}, NewDatabaseWithConfig(db, nil), nil)
    44  	s := &stateTest{db: db, state: sdb}
    45  
    46  	// generate a few entries
    47  	obj1 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01}))
    48  	obj1.AddBalance(big.NewInt(22))
    49  	obj2 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01, 0x02}))
    50  	obj2.SetCode(crypto.Keccak256Hash([]byte{3, 3, 3, 3, 3, 3, 3}), []byte{3, 3, 3, 3, 3, 3, 3})
    51  	obj3 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x02}))
    52  	obj3.SetBalance(big.NewInt(44))
    53  
    54  	// write some of them to the trie
    55  	s.state.updateStateObject(obj1)
    56  	s.state.updateStateObject(obj2)
    57  	s.state.Finalise(false)
    58  	s.state.AccountsIntermediateRoot()
    59  	s.state.Commit(nil)
    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.Finalise(false)
   101  	s.state.AccountsIntermediateRoot()
   102  	s.state.Commit(nil)
   103  
   104  	if value := s.state.GetState(address, common.Hash{}); value != (common.Hash{}) {
   105  		t.Errorf("expected empty current value, got %x", value)
   106  	}
   107  	if value := s.state.GetCommittedState(address, common.Hash{}); value != (common.Hash{}) {
   108  		t.Errorf("expected empty committed value, got %x", value)
   109  	}
   110  }
   111  
   112  func TestSnapshot(t *testing.T) {
   113  	stateobjaddr := common.BytesToAddress([]byte("aa"))
   114  	var storageaddr common.Hash
   115  	data1 := common.BytesToHash([]byte{42})
   116  	data2 := common.BytesToHash([]byte{43})
   117  	s := newStateTest()
   118  
   119  	// snapshot the genesis state
   120  	genesis := s.state.Snapshot()
   121  
   122  	// set initial state object value
   123  	s.state.SetState(stateobjaddr, storageaddr, data1)
   124  	snapshot := s.state.Snapshot()
   125  
   126  	// set a new state object value, revert it and ensure correct content
   127  	s.state.SetState(stateobjaddr, storageaddr, data2)
   128  	s.state.RevertToSnapshot(snapshot)
   129  
   130  	if v := s.state.GetState(stateobjaddr, storageaddr); v != data1 {
   131  		t.Errorf("wrong storage value %v, want %v", v, data1)
   132  	}
   133  	if v := s.state.GetCommittedState(stateobjaddr, storageaddr); v != (common.Hash{}) {
   134  		t.Errorf("wrong committed storage value %v, want %v", v, common.Hash{})
   135  	}
   136  
   137  	// revert up to the genesis state and ensure correct content
   138  	s.state.RevertToSnapshot(genesis)
   139  	if v := s.state.GetState(stateobjaddr, storageaddr); v != (common.Hash{}) {
   140  		t.Errorf("wrong storage value %v, want %v", v, common.Hash{})
   141  	}
   142  	if v := s.state.GetCommittedState(stateobjaddr, storageaddr); v != (common.Hash{}) {
   143  		t.Errorf("wrong committed storage value %v, want %v", v, common.Hash{})
   144  	}
   145  }
   146  
   147  func TestSnapshotEmpty(t *testing.T) {
   148  	s := newStateTest()
   149  	s.state.RevertToSnapshot(s.state.Snapshot())
   150  }
   151  
   152  func TestSnapshot2(t *testing.T) {
   153  	state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
   154  
   155  	stateobjaddr0 := common.BytesToAddress([]byte("so0"))
   156  	stateobjaddr1 := common.BytesToAddress([]byte("so1"))
   157  	var storageaddr common.Hash
   158  
   159  	data0 := common.BytesToHash([]byte{17})
   160  	data1 := common.BytesToHash([]byte{18})
   161  
   162  	state.SetState(stateobjaddr0, storageaddr, data0)
   163  	state.SetState(stateobjaddr1, storageaddr, data1)
   164  
   165  	// db, trie are already non-empty values
   166  	so0 := state.getStateObject(stateobjaddr0)
   167  	so0.SetBalance(big.NewInt(42))
   168  	so0.SetNonce(43)
   169  	so0.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e'}), []byte{'c', 'a', 'f', 'e'})
   170  	so0.suicided = false
   171  	so0.deleted = false
   172  	state.SetStateObject(so0)
   173  
   174  	state.Finalise(false)
   175  	state.AccountsIntermediateRoot()
   176  	root, _, _ := state.Commit(nil)
   177  	state, _ = New(root, state.db, state.snaps)
   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.Keccak256Hash([]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  }