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