managed_state_test.go 1.88 KB
Newer Older
obscuren's avatar
obscuren committed
1 2
package state

obscuren's avatar
obscuren committed
3 4
import (
	"testing"
obscuren's avatar
obscuren committed
5

obscuren's avatar
obscuren committed
6
	"github.com/ethereum/go-ethereum/common"
obscuren's avatar
obscuren committed
7 8
)

obscuren's avatar
obscuren committed
9
var addr = common.BytesToAddress([]byte("test"))
obscuren's avatar
obscuren committed
10 11

func create() (*ManagedState, *account) {
obscuren's avatar
obscuren committed
12 13
	ms := ManageState(&StateDB{stateObjects: make(map[string]*StateObject)})
	so := &StateObject{address: addr, nonce: 100}
obscuren's avatar
obscuren committed
14 15
	ms.StateDB.stateObjects[addr.Str()] = so
	ms.accounts[addr.Str()] = newAccount(so)
obscuren's avatar
obscuren committed
16

obscuren's avatar
obscuren committed
17
	return ms, ms.accounts[addr.Str()]
obscuren's avatar
obscuren committed
18 19 20 21 22 23 24
}

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

	nonce := ms.NewNonce(addr)
	if nonce != 100 {
obscuren's avatar
obscuren committed
25 26 27 28 29
		t.Error("expected nonce 100. got", nonce)
	}

	nonce = ms.NewNonce(addr)
	if nonce != 101 {
obscuren's avatar
obscuren committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		t.Error("expected nonce 101. got", nonce)
	}
}

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

	nn := make([]bool, 10)
	for i, _ := range nn {
		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)
	for i, _ := range nn {
		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
66 67 68 69 70 71 72 73 74 75

func TestRemoteNonceChange(t *testing.T) {
	ms, account := create()
	nn := make([]bool, 10)
	for i, _ := range nn {
		nn[i] = true
	}
	account.nonces = append(account.nonces, nn...)
	nonce := ms.NewNonce(addr)

obscuren's avatar
obscuren committed
76
	ms.StateDB.stateObjects[addr.Str()].nonce = 200
obscuren's avatar
obscuren committed
77 78 79 80
	nonce = ms.NewNonce(addr)
	if nonce != 200 {
		t.Error("expected nonce after remote update to be", 201, "got", nonce)
	}
obscuren's avatar
obscuren committed
81 82 83
	ms.NewNonce(addr)
	ms.NewNonce(addr)
	ms.NewNonce(addr)
obscuren's avatar
obscuren committed
84
	ms.StateDB.stateObjects[addr.Str()].nonce = 200
obscuren's avatar
obscuren committed
85 86 87 88
	nonce = ms.NewNonce(addr)
	if nonce != 204 {
		t.Error("expected nonce after remote update to be", 201, "got", nonce)
	}
obscuren's avatar
obscuren committed
89
}