state_test.go 7.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// 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.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package light

import (
	"bytes"
	"math/big"
	"testing"

	"github.com/ethereum/go-ethereum/common"
25
	"github.com/ethereum/go-ethereum/core"
26
	"github.com/ethereum/go-ethereum/core/state"
27
	"github.com/ethereum/go-ethereum/core/types"
28 29 30 31 32 33 34 35
	"github.com/ethereum/go-ethereum/ethdb"
	"golang.org/x/net/context"
)

func makeTestState() (common.Hash, ethdb.Database) {
	sdb, _ := ethdb.NewMemDatabase()
	st, _ := state.New(common.Hash{}, sdb)
	for i := byte(0); i < 100; i++ {
36
		addr := common.Address{i}
37
		for j := byte(0); j < 100; j++ {
38
			st.SetState(addr, common.Hash{j}, common.Hash{i, j})
39
		}
40 41 42
		st.SetNonce(addr, 100)
		st.AddBalance(addr, big.NewInt(int64(i)))
		st.SetCode(addr, []byte{i, i, i})
43
	}
44
	root, _ := st.Commit(false)
45 46 47 48 49
	return root, sdb
}

func TestLightStateOdr(t *testing.T) {
	root, sdb := makeTestState()
50 51
	header := &types.Header{Root: root, Number: big.NewInt(0)}
	core.WriteHeader(sdb, header)
52 53
	ldb, _ := ethdb.NewMemDatabase()
	odr := &testOdr{sdb: sdb, ldb: ldb}
54
	ls := NewLightState(StateTrieID(header), odr)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	ctx := context.Background()

	for i := byte(0); i < 100; i++ {
		addr := common.Address{i}
		err := ls.AddBalance(ctx, addr, big.NewInt(1000))
		if err != nil {
			t.Fatalf("Error adding balance to acc[%d]: %v", i, err)
		}
		err = ls.SetState(ctx, addr, common.Hash{100}, common.Hash{i, 100})
		if err != nil {
			t.Fatalf("Error setting storage of acc[%d]: %v", i, err)
		}
	}

	addr := common.Address{100}
	_, err := ls.CreateStateObject(ctx, addr)
	if err != nil {
		t.Fatalf("Error creating state object: %v", err)
	}
	err = ls.SetCode(ctx, addr, []byte{100, 100, 100})
	if err != nil {
		t.Fatalf("Error setting code: %v", err)
	}
	err = ls.AddBalance(ctx, addr, big.NewInt(1100))
	if err != nil {
		t.Fatalf("Error adding balance to acc[100]: %v", err)
	}
	for j := byte(0); j < 101; j++ {
		err = ls.SetState(ctx, addr, common.Hash{j}, common.Hash{100, j})
		if err != nil {
			t.Fatalf("Error setting storage of acc[100]: %v", err)
		}
	}
	err = ls.SetNonce(ctx, addr, 100)
	if err != nil {
		t.Fatalf("Error setting nonce for acc[100]: %v", err)
	}

	for i := byte(0); i < 101; i++ {
		addr := common.Address{i}

		bal, err := ls.GetBalance(ctx, addr)
		if err != nil {
			t.Fatalf("Error getting balance of acc[%d]: %v", i, err)
		}
		if bal.Int64() != int64(i)+1000 {
			t.Fatalf("Incorrect balance at acc[%d]: expected %v, got %v", i, int64(i)+1000, bal.Int64())
		}

		nonce, err := ls.GetNonce(ctx, addr)
		if err != nil {
			t.Fatalf("Error getting nonce of acc[%d]: %v", i, err)
		}
		if nonce != 100 {
			t.Fatalf("Incorrect nonce at acc[%d]: expected %v, got %v", i, 100, nonce)
		}

		code, err := ls.GetCode(ctx, addr)
		exp := []byte{i, i, i}
		if err != nil {
			t.Fatalf("Error getting code of acc[%d]: %v", i, err)
		}
		if !bytes.Equal(code, exp) {
			t.Fatalf("Incorrect code at acc[%d]: expected %v, got %v", i, exp, code)
		}

		for j := byte(0); j < 101; j++ {
			exp := common.Hash{i, j}
			val, err := ls.GetState(ctx, addr, common.Hash{j})
			if err != nil {
				t.Fatalf("Error retrieving acc[%d].storage[%d]: %v", i, j, err)
			}
			if val != exp {
				t.Fatalf("Retrieved wrong value from acc[%d].storage[%d]: expected %04x, got %04x", i, j, exp, val)
			}
		}
	}
}

func TestLightStateSetCopy(t *testing.T) {
	root, sdb := makeTestState()
136 137
	header := &types.Header{Root: root, Number: big.NewInt(0)}
	core.WriteHeader(sdb, header)
138 139
	ldb, _ := ethdb.NewMemDatabase()
	odr := &testOdr{sdb: sdb, ldb: ldb}
140
	ls := NewLightState(StateTrieID(header), odr)
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
	ctx := context.Background()

	for i := byte(0); i < 100; i++ {
		addr := common.Address{i}
		err := ls.AddBalance(ctx, addr, big.NewInt(1000))
		if err != nil {
			t.Fatalf("Error adding balance to acc[%d]: %v", i, err)
		}
		err = ls.SetState(ctx, addr, common.Hash{100}, common.Hash{i, 100})
		if err != nil {
			t.Fatalf("Error setting storage of acc[%d]: %v", i, err)
		}
	}

	ls2 := ls.Copy()

	for i := byte(0); i < 100; i++ {
		addr := common.Address{i}
		err := ls2.AddBalance(ctx, addr, big.NewInt(1000))
		if err != nil {
			t.Fatalf("Error adding balance to acc[%d]: %v", i, err)
		}
		err = ls2.SetState(ctx, addr, common.Hash{100}, common.Hash{i, 200})
		if err != nil {
			t.Fatalf("Error setting storage of acc[%d]: %v", i, err)
		}
	}

	lsx := ls.Copy()
	ls.Set(ls2)
	ls2.Set(lsx)

	for i := byte(0); i < 100; i++ {
		addr := common.Address{i}
		// check balance in ls
		bal, err := ls.GetBalance(ctx, addr)
		if err != nil {
			t.Fatalf("Error getting balance to acc[%d]: %v", i, err)
		}
		if bal.Int64() != int64(i)+2000 {
			t.Fatalf("Incorrect balance at ls.acc[%d]: expected %v, got %v", i, int64(i)+1000, bal.Int64())
		}
		// check balance in ls2
		bal, err = ls2.GetBalance(ctx, addr)
		if err != nil {
			t.Fatalf("Error getting balance to acc[%d]: %v", i, err)
		}
		if bal.Int64() != int64(i)+1000 {
			t.Fatalf("Incorrect balance at ls.acc[%d]: expected %v, got %v", i, int64(i)+1000, bal.Int64())
		}
		// check storage in ls
		exp := common.Hash{i, 200}
		val, err := ls.GetState(ctx, addr, common.Hash{100})
		if err != nil {
			t.Fatalf("Error retrieving acc[%d].storage[100]: %v", i, err)
		}
		if val != exp {
			t.Fatalf("Retrieved wrong value from acc[%d].storage[100]: expected %04x, got %04x", i, exp, val)
		}
		// check storage in ls2
		exp = common.Hash{i, 100}
		val, err = ls2.GetState(ctx, addr, common.Hash{100})
		if err != nil {
			t.Fatalf("Error retrieving acc[%d].storage[100]: %v", i, err)
		}
		if val != exp {
			t.Fatalf("Retrieved wrong value from acc[%d].storage[100]: expected %04x, got %04x", i, exp, val)
		}
	}
}

func TestLightStateDelete(t *testing.T) {
	root, sdb := makeTestState()
214 215
	header := &types.Header{Root: root, Number: big.NewInt(0)}
	core.WriteHeader(sdb, header)
216 217
	ldb, _ := ethdb.NewMemDatabase()
	odr := &testOdr{sdb: sdb, ldb: ldb}
218
	ls := NewLightState(StateTrieID(header), odr)
219 220 221 222 223 224 225 226 227 228 229 230
	ctx := context.Background()

	addr := common.Address{42}

	b, err := ls.HasAccount(ctx, addr)
	if err != nil {
		t.Fatalf("HasAccount error: %v", err)
	}
	if !b {
		t.Fatalf("HasAccount returned false, expected true")
	}

231
	b, err = ls.HasSuicided(ctx, addr)
232
	if err != nil {
233
		t.Fatalf("HasSuicided error: %v", err)
234 235
	}
	if b {
236
		t.Fatalf("HasSuicided returned true, expected false")
237 238
	}

239
	ls.Suicide(ctx, addr)
240

241
	b, err = ls.HasSuicided(ctx, addr)
242
	if err != nil {
243
		t.Fatalf("HasSuicided error: %v", err)
244 245
	}
	if !b {
246
		t.Fatalf("HasSuicided returned false, expected true")
247 248
	}
}