state_test.go 6.8 KB
Newer Older
F
Felix Lange 已提交
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
F
Felix Lange 已提交
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
F
Felix Lange 已提交
5 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
F
Felix Lange 已提交
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
F
Felix Lange 已提交
12 13 14
// GNU Lesser General Public License for more details.
//
// 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/>.
F
Felix Lange 已提交
16

O
obscuren 已提交
17
package state
O
obscuren 已提交
18 19

import (
20
	"bytes"
F
Felix Lange 已提交
21
	"math/big"
O
obscuren 已提交
22
	"testing"
F
Felix Lange 已提交
23

24
	checker "gopkg.in/check.v1"
O
obscuren 已提交
25

O
obscuren 已提交
26
	"github.com/ethereum/go-ethereum/common"
O
obscuren 已提交
27
	"github.com/ethereum/go-ethereum/ethdb"
O
obscuren 已提交
28 29
)

30
type StateSuite struct {
O
obscuren 已提交
31
	state *StateDB
32
}
O
obscuren 已提交
33

34
var _ = checker.Suite(&StateSuite{})
O
obscuren 已提交
35

O
obscuren 已提交
36
var toAddr = common.BytesToAddress
O
obscuren 已提交
37

38
func (s *StateSuite) TestDump(c *checker.C) {
O
obscuren 已提交
39
	return
F
Felix Lange 已提交
40
	// generate a few entries
O
obscuren 已提交
41
	obj1 := s.state.GetOrNewStateObject(toAddr([]byte{0x01}))
F
Felix Lange 已提交
42
	obj1.AddBalance(big.NewInt(22))
O
obscuren 已提交
43
	obj2 := s.state.GetOrNewStateObject(toAddr([]byte{0x01, 0x02}))
F
Felix Lange 已提交
44
	obj2.SetCode([]byte{3, 3, 3, 3, 3, 3, 3})
O
obscuren 已提交
45
	obj3 := s.state.GetOrNewStateObject(toAddr([]byte{0x02}))
F
Felix Lange 已提交
46 47 48 49 50 51 52 53 54
	obj3.SetBalance(big.NewInt(44))

	// write some of them to the trie
	s.state.UpdateStateObject(obj1)
	s.state.UpdateStateObject(obj2)

	// check that dump contains the state objects that are in trie
	got := string(s.state.Dump())
	want := `{
F
Felix Lange 已提交
55
    "root": "6e277ae8357d013e50f74eedb66a991f6922f93ae03714de58b3d0c5e9eee53f",
F
Felix Lange 已提交
56
    "accounts": {
F
Felix Lange 已提交
57
        "1468288056310c82aa4c01a7e12a10f8111a0560e72b700555479031b86c357d": {
F
Felix Lange 已提交
58 59 60 61 62 63
            "balance": "22",
            "nonce": 0,
            "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
            "codeHash": "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
            "storage": {}
        },
F
Felix Lange 已提交
64
        "a17eacbc25cda025e81db9c5c62868822c73ce097cee2a63e33a2e41268358a1": {
F
Felix Lange 已提交
65 66 67 68 69 70 71 72 73 74 75
            "balance": "0",
            "nonce": 0,
            "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
            "codeHash": "87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3",
            "storage": {}
        }
    }
}`
	if got != want {
		c.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want)
	}
76
}
O
obscuren 已提交
77

78
func (s *StateSuite) SetUpTest(c *checker.C) {
O
obscuren 已提交
79
	db, _ := ethdb.NewMemDatabase()
O
obscuren 已提交
80
	s.state = New(common.Hash{}, db)
81
}
O
obscuren 已提交
82

O
obscuren 已提交
83 84
func TestNull(t *testing.T) {
	db, _ := ethdb.NewMemDatabase()
O
obscuren 已提交
85
	state := New(common.Hash{}, db)
O
obscuren 已提交
86

O
obscuren 已提交
87
	address := common.HexToAddress("0x823140710bf13990e4500136726d8b55")
O
obscuren 已提交
88
	state.CreateAccount(address)
O
obscuren 已提交
89
	//value := common.FromHex("0x823140710bf13990e4500136726d8b55")
O
obscuren 已提交
90
	var value common.Hash
O
obscuren 已提交
91
	state.SetState(address, common.Hash{}, value)
92
	state.SyncIntermediate()
O
obscuren 已提交
93
	state.Sync()
O
obscuren 已提交
94
	value = state.GetState(address, common.Hash{})
O
obscuren 已提交
95 96 97
	if !common.EmptyHash(value) {
		t.Errorf("expected empty hash. got %x", value)
	}
O
obscuren 已提交
98 99
}

100
func (s *StateSuite) TestSnapshot(c *checker.C) {
O
obscuren 已提交
101
	stateobjaddr := toAddr([]byte("aa"))
O
obscuren 已提交
102 103 104
	var storageaddr common.Hash
	data1 := common.BytesToHash([]byte{42})
	data2 := common.BytesToHash([]byte{43})
105 106

	// set inital state object value
O
obscuren 已提交
107
	s.state.SetState(stateobjaddr, storageaddr, data1)
108 109 110 111
	// get snapshot of current state
	snapshot := s.state.Copy()

	// set new state object value
O
obscuren 已提交
112
	s.state.SetState(stateobjaddr, storageaddr, data2)
113 114 115 116
	// restore snapshot
	s.state.Set(snapshot)

	// get state storage value
O
obscuren 已提交
117
	res := s.state.GetState(stateobjaddr, storageaddr)
118 119

	c.Assert(data1, checker.DeepEquals, res)
O
obscuren 已提交
120
}
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

// use testing instead of checker because checker does not support
// printing/logging in tests (-check.vv does not work)
func TestSnapshot2(t *testing.T) {
	db, _ := ethdb.NewMemDatabase()
	state := New(common.Hash{}, db)

	stateobjaddr0 := toAddr([]byte("so0"))
	stateobjaddr1 := toAddr([]byte("so1"))
	var storageaddr common.Hash

	data0 := common.BytesToHash([]byte{17})
	data1 := common.BytesToHash([]byte{18})

	state.SetState(stateobjaddr0, storageaddr, data0)
	state.SetState(stateobjaddr1, storageaddr, data1)

	// db, trie are already non-empty values
	so0 := state.GetStateObject(stateobjaddr0)
	so0.balance = big.NewInt(42)
	so0.nonce = 43
	so0.gasPool = big.NewInt(44)
	so0.code = []byte{'c', 'a', 'f', 'e'}
	so0.codeHash = so0.CodeHash()
	so0.remove = true
	so0.deleted = false
	so0.dirty = false
	state.SetStateObject(so0)

	// and one with deleted == true
	so1 := state.GetStateObject(stateobjaddr1)
	so1.balance = big.NewInt(52)
	so1.nonce = 53
	so1.gasPool = big.NewInt(54)
	so1.code = []byte{'c', 'a', 'f', 'e', '2'}
	so1.codeHash = so1.CodeHash()
	so1.remove = true
	so1.deleted = true
	so1.dirty = true
	state.SetStateObject(so1)

	so1 = state.GetStateObject(stateobjaddr1)
	if so1 != nil {
		t.Fatalf("deleted object not nil when getting")
	}

	snapshot := state.Copy()
	state.Set(snapshot)

	so0Restored := state.GetStateObject(stateobjaddr0)
	so1Restored := state.GetStateObject(stateobjaddr1)
	// non-deleted is equal (restored)
	compareStateObjects(so0Restored, so0, t)
	// deleted should be nil, both before and after restore of state copy
	if so1Restored != nil {
		t.Fatalf("deleted object not nil after restoring snapshot")
	}
}

func compareStateObjects(so0, so1 *StateObject, t *testing.T) {
	if so0.address != so1.address {
		t.Fatalf("Address mismatch: have %v, want %v", so0.address, so1.address)
	}
	if so0.balance.Cmp(so1.balance) != 0 {
		t.Fatalf("Balance mismatch: have %v, want %v", so0.balance, so1.balance)
	}
	if so0.nonce != so1.nonce {
		t.Fatalf("Nonce mismatch: have %v, want %v", so0.nonce, so1.nonce)
	}
	if !bytes.Equal(so0.codeHash, so1.codeHash) {
		t.Fatalf("CodeHash mismatch: have %v, want %v", so0.codeHash, so1.codeHash)
	}
	if !bytes.Equal(so0.code, so1.code) {
		t.Fatalf("Code mismatch: have %v, want %v", so0.code, so1.code)
	}
	if !bytes.Equal(so0.initCode, so1.initCode) {
		t.Fatalf("InitCode mismatch: have %v, want %v", so0.initCode, so1.initCode)
	}

	for k, v := range so1.storage {
		if so0.storage[k] != v {
			t.Fatalf("Storage key %s mismatch: have %v, want %v", k, so0.storage[k], v)
		}
	}
	for k, v := range so0.storage {
		if so1.storage[k] != v {
			t.Fatalf("Storage key %s mismatch: have %v, want none.", k, v)
		}
	}

	if so0.gasPool.Cmp(so1.gasPool) != 0 {
		t.Fatalf("GasPool mismatch: have %v, want %v", so0.gasPool, so1.gasPool)
	}
	if so0.remove != so1.remove {
		t.Fatalf("Remove mismatch: have %v, want %v", so0.remove, so1.remove)
	}
	if so0.deleted != so1.deleted {
		t.Fatalf("Deleted mismatch: have %v, want %v", so0.deleted, so1.deleted)
	}
	if so0.dirty != so1.dirty {
		t.Fatalf("Dirty mismatch: have %v, want %v", so0.dirty, so1.dirty)
	}
}