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