new type + additional methods
This commit is contained in:
@ -1,6 +1,10 @@
|
||||
package state
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
type account struct {
|
||||
stateObject *StateObject
|
||||
@ -29,7 +33,7 @@ func (ms *ManagedState) SetState(statedb *StateDB) {
|
||||
ms.StateDB = statedb
|
||||
}
|
||||
|
||||
func (ms *ManagedState) RemoveNonce(addr []byte, n uint64) {
|
||||
func (ms *ManagedState) RemoveNonce(addr common.Address, n uint64) {
|
||||
if ms.hasAccount(addr) {
|
||||
ms.mu.Lock()
|
||||
defer ms.mu.Unlock()
|
||||
@ -43,7 +47,7 @@ func (ms *ManagedState) RemoveNonce(addr []byte, n uint64) {
|
||||
}
|
||||
}
|
||||
|
||||
func (ms *ManagedState) NewNonce(addr []byte) uint64 {
|
||||
func (ms *ManagedState) NewNonce(addr common.Address) uint64 {
|
||||
ms.mu.RLock()
|
||||
defer ms.mu.RUnlock()
|
||||
|
||||
@ -57,26 +61,27 @@ func (ms *ManagedState) NewNonce(addr []byte) uint64 {
|
||||
return uint64(len(account.nonces)) + account.nstart
|
||||
}
|
||||
|
||||
func (ms *ManagedState) hasAccount(addr []byte) bool {
|
||||
_, ok := ms.accounts[string(addr)]
|
||||
func (ms *ManagedState) hasAccount(addr common.Address) bool {
|
||||
_, ok := ms.accounts[addr.Str()]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (ms *ManagedState) getAccount(addr []byte) *account {
|
||||
if account, ok := ms.accounts[string(addr)]; !ok {
|
||||
func (ms *ManagedState) getAccount(addr common.Address) *account {
|
||||
straddr := addr.Str()
|
||||
if account, ok := ms.accounts[straddr]; !ok {
|
||||
so := ms.GetOrNewStateObject(addr)
|
||||
ms.accounts[string(addr)] = newAccount(so)
|
||||
ms.accounts[straddr] = newAccount(so)
|
||||
} else {
|
||||
// Always make sure the state account nonce isn't actually higher
|
||||
// than the tracked one.
|
||||
so := ms.StateDB.GetStateObject(addr)
|
||||
if so != nil && uint64(len(account.nonces))+account.nstart < so.nonce {
|
||||
ms.accounts[string(addr)] = newAccount(so)
|
||||
ms.accounts[straddr] = newAccount(so)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ms.accounts[string(addr)]
|
||||
return ms.accounts[straddr]
|
||||
}
|
||||
|
||||
func newAccount(so *StateObject) *account {
|
||||
|
@ -5,8 +5,8 @@ import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
)
|
||||
@ -44,7 +44,7 @@ type StateObject struct {
|
||||
State *StateDB
|
||||
|
||||
// Address belonging to this account
|
||||
address []byte
|
||||
address common.Address
|
||||
// The balance of the account
|
||||
balance *big.Int
|
||||
// The nonce of the account
|
||||
@ -77,9 +77,9 @@ func (self *StateObject) Reset() {
|
||||
self.State.Reset()
|
||||
}
|
||||
|
||||
func NewStateObject(addr []byte, db common.Database) *StateObject {
|
||||
func NewStateObject(address common.Address, db common.Database) *StateObject {
|
||||
// This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter.
|
||||
address := common.Address(addr)
|
||||
//address := common.ToAddress(addr)
|
||||
|
||||
object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true}
|
||||
object.State = New(nil, db) //New(trie.New(common.Config.Db, ""))
|
||||
@ -90,12 +90,12 @@ func NewStateObject(addr []byte, db common.Database) *StateObject {
|
||||
return object
|
||||
}
|
||||
|
||||
func NewStateObjectFromBytes(address, data []byte, db common.Database) *StateObject {
|
||||
func NewStateObjectFromBytes(address common.Address, data []byte, db common.Database) *StateObject {
|
||||
// TODO clean me up
|
||||
var extobject struct {
|
||||
Nonce uint64
|
||||
Balance *big.Int
|
||||
Root []byte
|
||||
Root common.Hash
|
||||
CodeHash []byte
|
||||
}
|
||||
err := rlp.Decode(bytes.NewReader(data), &extobject)
|
||||
@ -284,7 +284,7 @@ func (c *StateObject) N() *big.Int {
|
||||
}
|
||||
|
||||
// Returns the address of the contract/account
|
||||
func (c *StateObject) Address() []byte {
|
||||
func (c *StateObject) Address() common.Address {
|
||||
return c.address
|
||||
}
|
||||
|
||||
|
@ -45,15 +45,16 @@ func (self *StateDB) Logs() Logs {
|
||||
return self.logs
|
||||
}
|
||||
|
||||
func (self *StateDB) Refund(addr []byte, gas *big.Int) {
|
||||
if self.refund[string(addr)] == nil {
|
||||
self.refund[string(addr)] = new(big.Int)
|
||||
func (self *StateDB) Refund(address common.Address, gas *big.Int) {
|
||||
addr := address.Str()
|
||||
if self.refund[addr] == nil {
|
||||
self.refund[addr] = new(big.Int)
|
||||
}
|
||||
self.refund[string(addr)].Add(self.refund[string(addr)], gas)
|
||||
self.refund[addr].Add(self.refund[addr], gas)
|
||||
}
|
||||
|
||||
// Retrieve the balance from the given address or 0 if object not found
|
||||
func (self *StateDB) GetBalance(addr []byte) *big.Int {
|
||||
func (self *StateDB) GetBalance(addr common.Address) *big.Int {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject != nil {
|
||||
return stateObject.balance
|
||||
@ -62,14 +63,14 @@ func (self *StateDB) GetBalance(addr []byte) *big.Int {
|
||||
return common.Big0
|
||||
}
|
||||
|
||||
func (self *StateDB) AddBalance(addr []byte, amount *big.Int) {
|
||||
func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.AddBalance(amount)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateDB) GetNonce(addr []byte) uint64 {
|
||||
func (self *StateDB) GetNonce(addr common.Address) uint64 {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject != nil {
|
||||
return stateObject.nonce
|
||||
@ -78,7 +79,7 @@ func (self *StateDB) GetNonce(addr []byte) uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (self *StateDB) GetCode(addr []byte) []byte {
|
||||
func (self *StateDB) GetCode(addr common.Address) []byte {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject != nil {
|
||||
return stateObject.code
|
||||
@ -87,7 +88,7 @@ func (self *StateDB) GetCode(addr []byte) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *StateDB) GetState(a, b []byte) []byte {
|
||||
func (self *StateDB) GetState(a common.Adress, b common.Hash) []byte {
|
||||
stateObject := self.GetStateObject(a)
|
||||
if stateObject != nil {
|
||||
return stateObject.GetState(b).Bytes()
|
||||
@ -96,28 +97,28 @@ func (self *StateDB) GetState(a, b []byte) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *StateDB) SetNonce(addr []byte, nonce uint64) {
|
||||
func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.SetNonce(nonce)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateDB) SetCode(addr, code []byte) {
|
||||
func (self *StateDB) SetCode(addr common.Address, code []byte) {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.SetCode(code)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateDB) SetState(addr, key []byte, value interface{}) {
|
||||
func (self *StateDB) SetState(addr common.Address, key common.Hash, value interface{}) {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.SetState(key, common.NewValue(value))
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateDB) Delete(addr []byte) bool {
|
||||
func (self *StateDB) Delete(addr common.Address) bool {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.MarkForDeletion()
|
||||
@ -129,7 +130,7 @@ func (self *StateDB) Delete(addr []byte) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *StateDB) IsDeleted(addr []byte) bool {
|
||||
func (self *StateDB) IsDeleted(addr common.Address) bool {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject != nil {
|
||||
return stateObject.remove
|
||||
@ -143,27 +144,27 @@ func (self *StateDB) IsDeleted(addr []byte) bool {
|
||||
|
||||
// Update the given state object and apply it to state trie
|
||||
func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
|
||||
addr := stateObject.Address()
|
||||
//addr := stateObject.Address()
|
||||
|
||||
if len(stateObject.CodeHash()) > 0 {
|
||||
self.db.Put(stateObject.CodeHash(), stateObject.code)
|
||||
}
|
||||
|
||||
self.trie.Update(addr, stateObject.RlpEncode())
|
||||
self.trie.Update(stateObject.Address(), stateObject.RlpEncode())
|
||||
}
|
||||
|
||||
// Delete the given state object and delete it from the state trie
|
||||
func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
|
||||
self.trie.Delete(stateObject.Address())
|
||||
|
||||
delete(self.stateObjects, string(stateObject.Address()))
|
||||
delete(self.stateObjects, stateObject.Address().Str())
|
||||
}
|
||||
|
||||
// Retrieve a state object given my the address. Nil if not found
|
||||
func (self *StateDB) GetStateObject(addr []byte) *StateObject {
|
||||
addr = common.Address(addr)
|
||||
func (self *StateDB) GetStateObject(addr common.Address) *StateObject {
|
||||
//addr = common.Address(addr)
|
||||
|
||||
stateObject := self.stateObjects[string(addr)]
|
||||
stateObject := self.stateObjects[addr.Str()]
|
||||
if stateObject != nil {
|
||||
return stateObject
|
||||
}
|
||||
@ -180,11 +181,11 @@ func (self *StateDB) GetStateObject(addr []byte) *StateObject {
|
||||
}
|
||||
|
||||
func (self *StateDB) SetStateObject(object *StateObject) {
|
||||
self.stateObjects[string(object.address)] = object
|
||||
self.stateObjects[object.Address().Str()] = object
|
||||
}
|
||||
|
||||
// Retrieve a state object or create a new state object if nil
|
||||
func (self *StateDB) GetOrNewStateObject(addr []byte) *StateObject {
|
||||
func (self *StateDB) GetOrNewStateObject(addr common.Address) *StateObject {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject == nil {
|
||||
stateObject = self.NewStateObject(addr)
|
||||
@ -194,8 +195,8 @@ func (self *StateDB) GetOrNewStateObject(addr []byte) *StateObject {
|
||||
}
|
||||
|
||||
// Create a state object whether it exist in the trie or not
|
||||
func (self *StateDB) NewStateObject(addr []byte) *StateObject {
|
||||
addr = common.Address(addr)
|
||||
func (self *StateDB) NewStateObject(addr common.Address) *StateObject {
|
||||
//addr = common.Address(addr)
|
||||
|
||||
statelogger.Debugf("(+) %x\n", addr)
|
||||
|
||||
|
Reference in New Issue
Block a user