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