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