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