managed_state_test.go 2.98 KB
Newer Older
1
// Copyright 2015 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 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,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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/>.
16

obscuren's avatar
obscuren committed
17 18
package state

obscuren's avatar
obscuren committed
19 20
import (
	"testing"
obscuren's avatar
obscuren committed
21

obscuren's avatar
obscuren committed
22
	"github.com/ethereum/go-ethereum/common"
23
	"github.com/ethereum/go-ethereum/ethdb"
obscuren's avatar
obscuren committed
24 25
)

obscuren's avatar
obscuren committed
26
var addr = common.BytesToAddress([]byte("test"))
obscuren's avatar
obscuren committed
27 28

func create() (*ManagedState, *account) {
29
	statedb, _ := New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase()))
30
	ms := ManageState(statedb)
31
	ms.StateDB.SetNonce(addr, 100)
32
	ms.accounts[addr] = newAccount(ms.StateDB.getStateObject(addr))
33
	return ms, ms.accounts[addr]
obscuren's avatar
obscuren committed
34 35 36 37 38 39 40
}

func TestNewNonce(t *testing.T) {
	ms, _ := create()

	nonce := ms.NewNonce(addr)
	if nonce != 100 {
obscuren's avatar
obscuren committed
41 42 43 44 45
		t.Error("expected nonce 100. got", nonce)
	}

	nonce = ms.NewNonce(addr)
	if nonce != 101 {
obscuren's avatar
obscuren committed
46 47 48 49 50 51 52 53
		t.Error("expected nonce 101. got", nonce)
	}
}

func TestRemove(t *testing.T) {
	ms, account := create()

	nn := make([]bool, 10)
Felix Lange's avatar
Felix Lange committed
54
	for i := range nn {
obscuren's avatar
obscuren committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
		nn[i] = true
	}
	account.nonces = append(account.nonces, nn...)

	i := uint64(5)
	ms.RemoveNonce(addr, account.nstart+i)
	if len(account.nonces) != 5 {
		t.Error("expected", i, "'th index to be false")
	}
}

func TestReuse(t *testing.T) {
	ms, account := create()

	nn := make([]bool, 10)
Felix Lange's avatar
Felix Lange committed
70
	for i := range nn {
obscuren's avatar
obscuren committed
71 72 73 74 75 76 77 78 79 80 81
		nn[i] = true
	}
	account.nonces = append(account.nonces, nn...)

	i := uint64(5)
	ms.RemoveNonce(addr, account.nstart+i)
	nonce := ms.NewNonce(addr)
	if nonce != 105 {
		t.Error("expected nonce to be 105. got", nonce)
	}
}
obscuren's avatar
obscuren committed
82 83 84 85

func TestRemoteNonceChange(t *testing.T) {
	ms, account := create()
	nn := make([]bool, 10)
Felix Lange's avatar
Felix Lange committed
86
	for i := range nn {
obscuren's avatar
obscuren committed
87 88 89
		nn[i] = true
	}
	account.nonces = append(account.nonces, nn...)
90
	ms.NewNonce(addr)
obscuren's avatar
obscuren committed
91

92
	ms.StateDB.stateObjects[addr].data.Nonce = 200
93
	nonce := ms.NewNonce(addr)
obscuren's avatar
obscuren committed
94
	if nonce != 200 {
95
		t.Error("expected nonce after remote update to be", 200, "got", nonce)
obscuren's avatar
obscuren committed
96
	}
obscuren's avatar
obscuren committed
97 98 99
	ms.NewNonce(addr)
	ms.NewNonce(addr)
	ms.NewNonce(addr)
100
	ms.StateDB.stateObjects[addr].data.Nonce = 200
obscuren's avatar
obscuren committed
101 102
	nonce = ms.NewNonce(addr)
	if nonce != 204 {
103
		t.Error("expected nonce after remote update to be", 204, "got", nonce)
obscuren's avatar
obscuren committed
104
	}
obscuren's avatar
obscuren committed
105
}
106 107 108 109 110 111 112 113

func TestSetNonce(t *testing.T) {
	ms, _ := create()

	var addr common.Address
	ms.SetNonce(addr, 10)

	if ms.GetNonce(addr) != 10 {
114
		t.Error("Expected nonce of 10, got", ms.GetNonce(addr))
115 116 117 118 119 120
	}

	addr[0] = 1
	ms.StateDB.SetNonce(addr, 1)

	if ms.GetNonce(addr) != 1 {
121
		t.Error("Expected nonce of 1, got", ms.GetNonce(addr))
122 123
	}
}