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