managed_state.go 3.8 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

17 18
package state

obscuren's avatar
obscuren committed
19 20 21 22 23
import (
	"sync"

	"github.com/ethereum/go-ethereum/common"
)
24

obscuren's avatar
obscuren committed
25
type account struct {
26
	stateObject *stateObject
obscuren's avatar
obscuren committed
27 28 29 30
	nstart      uint64
	nonces      []bool
}

31 32 33 34 35
type ManagedState struct {
	*StateDB

	mu sync.RWMutex

36
	accounts map[common.Address]*account
37 38
}

39
// ManagedState returns a new managed state with the statedb as it's backing layer
40 41
func ManageState(statedb *StateDB) *ManagedState {
	return &ManagedState{
42
		StateDB:  statedb.Copy(),
43
		accounts: make(map[common.Address]*account),
44 45 46
	}
}

47
// SetState sets the backing layer of the managed state
obscuren's avatar
obscuren committed
48 49 50 51 52 53
func (ms *ManagedState) SetState(statedb *StateDB) {
	ms.mu.Lock()
	defer ms.mu.Unlock()
	ms.StateDB = statedb
}

54
// RemoveNonce removed the nonce from the managed state and all future pending nonces
obscuren's avatar
obscuren committed
55
func (ms *ManagedState) RemoveNonce(addr common.Address, n uint64) {
obscuren's avatar
obscuren committed
56
	if ms.hasAccount(addr) {
57 58 59
		ms.mu.Lock()
		defer ms.mu.Unlock()

obscuren's avatar
obscuren committed
60
		account := ms.getAccount(addr)
obscuren's avatar
obscuren committed
61
		if n-account.nstart <= uint64(len(account.nonces)) {
obscuren's avatar
obscuren committed
62 63 64 65
			reslice := make([]bool, n-account.nstart)
			copy(reslice, account.nonces[:n-account.nstart])
			account.nonces = reslice
		}
66 67 68
	}
}

69
// NewNonce returns the new canonical nonce for the managed account
obscuren's avatar
obscuren committed
70
func (ms *ManagedState) NewNonce(addr common.Address) uint64 {
71 72
	ms.mu.Lock()
	defer ms.mu.Unlock()
obscuren's avatar
obscuren committed
73 74 75 76 77 78 79

	account := ms.getAccount(addr)
	for i, nonce := range account.nonces {
		if !nonce {
			return account.nstart + uint64(i)
		}
	}
obscuren's avatar
obscuren committed
80
	account.nonces = append(account.nonces, true)
81

82
	return uint64(len(account.nonces)-1) + account.nstart
83 84
}

85 86 87
// GetNonce returns the canonical nonce for the managed or unmanaged account.
//
// Because GetNonce mutates the DB, we must take a write lock.
88
func (ms *ManagedState) GetNonce(addr common.Address) uint64 {
89 90
	ms.mu.Lock()
	defer ms.mu.Unlock()
obscuren's avatar
obscuren committed
91 92

	if ms.hasAccount(addr) {
93 94 95 96 97 98 99 100 101
		account := ms.getAccount(addr)
		return uint64(len(account.nonces)) + account.nstart
	} else {
		return ms.StateDB.GetNonce(addr)
	}
}

// SetNonce sets the new canonical nonce for the managed state
func (ms *ManagedState) SetNonce(addr common.Address, nonce uint64) {
obscuren's avatar
obscuren committed
102 103 104
	ms.mu.Lock()
	defer ms.mu.Unlock()

105 106 107
	so := ms.GetOrNewStateObject(addr)
	so.SetNonce(nonce)

108
	ms.accounts[addr] = newAccount(so)
109 110 111 112
}

// HasAccount returns whether the given address is managed or not
func (ms *ManagedState) HasAccount(addr common.Address) bool {
obscuren's avatar
obscuren committed
113 114 115 116 117 118
	ms.mu.RLock()
	defer ms.mu.RUnlock()
	return ms.hasAccount(addr)
}

func (ms *ManagedState) hasAccount(addr common.Address) bool {
119
	_, ok := ms.accounts[addr]
120 121 122
	return ok
}

123
// populate the managed state
obscuren's avatar
obscuren committed
124
func (ms *ManagedState) getAccount(addr common.Address) *account {
125
	if account, ok := ms.accounts[addr]; !ok {
obscuren's avatar
obscuren committed
126
		so := ms.GetOrNewStateObject(addr)
127
		ms.accounts[addr] = newAccount(so)
obscuren's avatar
obscuren committed
128 129 130
	} else {
		// Always make sure the state account nonce isn't actually higher
		// than the tracked one.
131
		so := ms.StateDB.getStateObject(addr)
132 133
		if so != nil && uint64(len(account.nonces))+account.nstart < so.Nonce() {
			ms.accounts[addr] = newAccount(so)
obscuren's avatar
obscuren committed
134 135
		}

136 137
	}

138
	return ms.accounts[addr]
139
}
obscuren's avatar
obscuren committed
140

141
func newAccount(so *stateObject) *account {
142
	return &account{so, so.Nonce(), nil}
obscuren's avatar
obscuren committed
143
}