github.com/immesys/bw2bc@v1.1.0/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  	"math/big"
    21  	"testing"
    22  
    23  	checker "gopkg.in/check.v1"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/ethdb"
    27  )
    28  
    29  type StateSuite struct {
    30  	state *StateDB
    31  }
    32  
    33  var _ = checker.Suite(&StateSuite{})
    34  
    35  var toAddr = common.BytesToAddress
    36  
    37  func (s *StateSuite) TestDump(c *checker.C) {
    38  	return
    39  	// generate a few entries
    40  	obj1 := s.state.GetOrNewStateObject(toAddr([]byte{0x01}))
    41  	obj1.AddBalance(big.NewInt(22))
    42  	obj2 := s.state.GetOrNewStateObject(toAddr([]byte{0x01, 0x02}))
    43  	obj2.SetCode([]byte{3, 3, 3, 3, 3, 3, 3})
    44  	obj3 := s.state.GetOrNewStateObject(toAddr([]byte{0x02}))
    45  	obj3.SetBalance(big.NewInt(44))
    46  
    47  	// write some of them to the trie
    48  	s.state.UpdateStateObject(obj1)
    49  	s.state.UpdateStateObject(obj2)
    50  
    51  	// check that dump contains the state objects that are in trie
    52  	got := string(s.state.Dump())
    53  	want := `{
    54      "root": "6e277ae8357d013e50f74eedb66a991f6922f93ae03714de58b3d0c5e9eee53f",
    55      "accounts": {
    56          "1468288056310c82aa4c01a7e12a10f8111a0560e72b700555479031b86c357d": {
    57              "balance": "22",
    58              "nonce": 0,
    59              "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
    60              "codeHash": "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
    61              "storage": {}
    62          },
    63          "a17eacbc25cda025e81db9c5c62868822c73ce097cee2a63e33a2e41268358a1": {
    64              "balance": "0",
    65              "nonce": 0,
    66              "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
    67              "codeHash": "87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3",
    68              "storage": {}
    69          }
    70      }
    71  }`
    72  	if got != want {
    73  		c.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want)
    74  	}
    75  }
    76  
    77  func (s *StateSuite) SetUpTest(c *checker.C) {
    78  	db, _ := ethdb.NewMemDatabase()
    79  	s.state = New(common.Hash{}, db)
    80  }
    81  
    82  func TestNull(t *testing.T) {
    83  	db, _ := ethdb.NewMemDatabase()
    84  	state := New(common.Hash{}, db)
    85  
    86  	address := common.HexToAddress("0x823140710bf13990e4500136726d8b55")
    87  	state.CreateAccount(address)
    88  	//value := common.FromHex("0x823140710bf13990e4500136726d8b55")
    89  	var value common.Hash
    90  	state.SetState(address, common.Hash{}, value)
    91  	state.SyncIntermediate()
    92  	state.Sync()
    93  	value = state.GetState(address, common.Hash{})
    94  	if !common.EmptyHash(value) {
    95  		t.Errorf("expected empty hash. got %x", value)
    96  	}
    97  }
    98  
    99  func (s *StateSuite) TestSnapshot(c *checker.C) {
   100  	stateobjaddr := toAddr([]byte("aa"))
   101  	var storageaddr common.Hash
   102  	data1 := common.BytesToHash([]byte{42})
   103  	data2 := common.BytesToHash([]byte{43})
   104  
   105  	// set inital state object value
   106  	s.state.SetState(stateobjaddr, storageaddr, data1)
   107  	// get snapshot of current state
   108  	snapshot := s.state.Copy()
   109  
   110  	// set new state object value
   111  	s.state.SetState(stateobjaddr, storageaddr, data2)
   112  	// restore snapshot
   113  	s.state.Set(snapshot)
   114  
   115  	// get state storage value
   116  	res := s.state.GetState(stateobjaddr, storageaddr)
   117  
   118  	c.Assert(data1, checker.DeepEquals, res)
   119  }