mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 14:16:44 +00:00
core/state: remove StateObject.db, prepare for removal of address
db is essentially unused and removing it shrinks StateObject by 16 bytes. I'd love to remove address as well but that's a very invasive change.
This commit is contained in:
parent
3778f1bf77
commit
9df74cb584
4 changed files with 25 additions and 36 deletions
|
|
@ -33,14 +33,14 @@ type ManagedState struct {
|
|||
|
||||
mu sync.RWMutex
|
||||
|
||||
accounts map[string]*account
|
||||
accounts map[common.Address]*account
|
||||
}
|
||||
|
||||
// ManagedState returns a new managed state with the statedb as it's backing layer
|
||||
func ManageState(statedb *StateDB) *ManagedState {
|
||||
return &ManagedState{
|
||||
StateDB: statedb.Copy(),
|
||||
accounts: make(map[string]*account),
|
||||
accounts: make(map[common.Address]*account),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -103,7 +103,7 @@ func (ms *ManagedState) SetNonce(addr common.Address, nonce uint64) {
|
|||
so := ms.GetOrNewStateObject(addr)
|
||||
so.SetNonce(nonce)
|
||||
|
||||
ms.accounts[addr.Str()] = newAccount(so)
|
||||
ms.accounts[addr] = newAccount(so)
|
||||
}
|
||||
|
||||
// HasAccount returns whether the given address is managed or not
|
||||
|
|
@ -114,27 +114,26 @@ func (ms *ManagedState) HasAccount(addr common.Address) bool {
|
|||
}
|
||||
|
||||
func (ms *ManagedState) hasAccount(addr common.Address) bool {
|
||||
_, ok := ms.accounts[addr.Str()]
|
||||
_, ok := ms.accounts[addr]
|
||||
return ok
|
||||
}
|
||||
|
||||
// populate the managed state
|
||||
func (ms *ManagedState) getAccount(addr common.Address) *account {
|
||||
straddr := addr.Str()
|
||||
if account, ok := ms.accounts[straddr]; !ok {
|
||||
if account, ok := ms.accounts[addr]; !ok {
|
||||
so := ms.GetOrNewStateObject(addr)
|
||||
ms.accounts[straddr] = newAccount(so)
|
||||
ms.accounts[addr] = 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[straddr] = newAccount(so)
|
||||
ms.accounts[addr] = newAccount(so)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ms.accounts[straddr]
|
||||
return ms.accounts[addr]
|
||||
}
|
||||
|
||||
func newAccount(so *StateObject) *account {
|
||||
|
|
|
|||
|
|
@ -30,10 +30,10 @@ func create() (*ManagedState, *account) {
|
|||
statedb, _ := New(common.Hash{}, db)
|
||||
ms := ManageState(statedb)
|
||||
so := &StateObject{address: addr, nonce: 100}
|
||||
ms.StateDB.stateObjects[addr.Str()] = so
|
||||
ms.accounts[addr.Str()] = newAccount(so)
|
||||
ms.StateDB.stateObjects[addr] = so
|
||||
ms.accounts[addr] = newAccount(so)
|
||||
|
||||
return ms, ms.accounts[addr.Str()]
|
||||
return ms, ms.accounts[addr]
|
||||
}
|
||||
|
||||
func TestNewNonce(t *testing.T) {
|
||||
|
|
@ -92,7 +92,7 @@ func TestRemoteNonceChange(t *testing.T) {
|
|||
account.nonces = append(account.nonces, nn...)
|
||||
nonce := ms.NewNonce(addr)
|
||||
|
||||
ms.StateDB.stateObjects[addr.Str()].nonce = 200
|
||||
ms.StateDB.stateObjects[addr].nonce = 200
|
||||
nonce = ms.NewNonce(addr)
|
||||
if nonce != 200 {
|
||||
t.Error("expected nonce after remote update to be", 201, "got", nonce)
|
||||
|
|
@ -100,7 +100,7 @@ func TestRemoteNonceChange(t *testing.T) {
|
|||
ms.NewNonce(addr)
|
||||
ms.NewNonce(addr)
|
||||
ms.NewNonce(addr)
|
||||
ms.StateDB.stateObjects[addr.Str()].nonce = 200
|
||||
ms.StateDB.stateObjects[addr].nonce = 200
|
||||
nonce = ms.NewNonce(addr)
|
||||
if nonce != 204 {
|
||||
t.Error("expected nonce after remote update to be", 201, "got", nonce)
|
||||
|
|
|
|||
|
|
@ -58,7 +58,6 @@ func (self Storage) Copy() Storage {
|
|||
}
|
||||
|
||||
type StateObject struct {
|
||||
db trie.Database // State database for storing state changes
|
||||
trie *trie.SecureTrie
|
||||
|
||||
// Address belonging to this account
|
||||
|
|
@ -84,7 +83,6 @@ type StateObject struct {
|
|||
|
||||
func NewStateObject(address common.Address, db trie.Database) *StateObject {
|
||||
object := &StateObject{
|
||||
db: db,
|
||||
address: address,
|
||||
balance: new(big.Int),
|
||||
dirty: true,
|
||||
|
|
@ -119,10 +117,6 @@ func (c *StateObject) setAddr(addr, value common.Hash) {
|
|||
c.trie.Update(addr[:], v)
|
||||
}
|
||||
|
||||
func (self *StateObject) Storage() Storage {
|
||||
return self.storage
|
||||
}
|
||||
|
||||
func (self *StateObject) GetState(key common.Hash) common.Hash {
|
||||
value, exists := self.storage[key]
|
||||
if !exists {
|
||||
|
|
@ -172,15 +166,11 @@ func (c *StateObject) SetBalance(amount *big.Int) {
|
|||
c.dirty = true
|
||||
}
|
||||
|
||||
func (c *StateObject) St() Storage {
|
||||
return c.storage
|
||||
}
|
||||
|
||||
// Return the gas back to the origin. Used by the Virtual machine or Closures
|
||||
func (c *StateObject) ReturnGas(gas, price *big.Int) {}
|
||||
|
||||
func (self *StateObject) Copy() *StateObject {
|
||||
stateObject := NewStateObject(self.Address(), self.db)
|
||||
func (self *StateObject) Copy(db trie.Database) *StateObject {
|
||||
stateObject := NewStateObject(self.address, db)
|
||||
stateObject.balance.Set(self.balance)
|
||||
stateObject.codeHash = common.CopyBytes(self.codeHash)
|
||||
stateObject.nonce = self.nonce
|
||||
|
|
@ -272,7 +262,7 @@ func (c *StateObject) EncodeRLP(w io.Writer) error {
|
|||
// DecodeObject decodes an RLP-encoded state object.
|
||||
func DecodeObject(address common.Address, db trie.Database, data []byte) (*StateObject, error) {
|
||||
var (
|
||||
obj = &StateObject{address: address, db: db, storage: make(Storage)}
|
||||
obj = &StateObject{address: address, storage: make(Storage)}
|
||||
ext extStateObject
|
||||
err error
|
||||
)
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ type StateDB struct {
|
|||
db ethdb.Database
|
||||
trie *trie.SecureTrie
|
||||
|
||||
stateObjects map[string]*StateObject
|
||||
stateObjects map[common.Address]*StateObject
|
||||
|
||||
refund *big.Int
|
||||
|
||||
|
|
@ -62,7 +62,7 @@ func New(root common.Hash, db ethdb.Database) (*StateDB, error) {
|
|||
return &StateDB{
|
||||
db: db,
|
||||
trie: tr,
|
||||
stateObjects: make(map[string]*StateObject),
|
||||
stateObjects: make(map[common.Address]*StateObject),
|
||||
refund: new(big.Int),
|
||||
logs: make(map[common.Hash]vm.Logs),
|
||||
}, nil
|
||||
|
|
@ -83,7 +83,7 @@ func (self *StateDB) Reset(root common.Hash) error {
|
|||
*self = StateDB{
|
||||
db: self.db,
|
||||
trie: tr,
|
||||
stateObjects: make(map[string]*StateObject),
|
||||
stateObjects: make(map[common.Address]*StateObject),
|
||||
refund: new(big.Int),
|
||||
logs: make(map[common.Hash]vm.Logs),
|
||||
}
|
||||
|
|
@ -242,12 +242,12 @@ func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
|
|||
|
||||
addr := stateObject.Address()
|
||||
self.trie.Delete(addr[:])
|
||||
//delete(self.stateObjects, addr.Str())
|
||||
//delete(self.stateObjects, addr)
|
||||
}
|
||||
|
||||
// Retrieve a state object given my the address. Nil if not found
|
||||
func (self *StateDB) GetStateObject(addr common.Address) (stateObject *StateObject) {
|
||||
stateObject = self.stateObjects[addr.Str()]
|
||||
stateObject = self.stateObjects[addr]
|
||||
if stateObject != nil {
|
||||
if stateObject.deleted {
|
||||
stateObject = nil
|
||||
|
|
@ -270,7 +270,7 @@ func (self *StateDB) GetStateObject(addr common.Address) (stateObject *StateObje
|
|||
}
|
||||
|
||||
func (self *StateDB) SetStateObject(object *StateObject) {
|
||||
self.stateObjects[object.Address().Str()] = object
|
||||
self.stateObjects[object.Address()] = object
|
||||
}
|
||||
|
||||
// Retrieve a state object or create a new state object if nil
|
||||
|
|
@ -291,7 +291,7 @@ func (self *StateDB) newStateObject(addr common.Address) *StateObject {
|
|||
|
||||
stateObject := NewStateObject(addr, self.db)
|
||||
stateObject.SetNonce(StartingNonce)
|
||||
self.stateObjects[addr.Str()] = stateObject
|
||||
self.stateObjects[addr] = stateObject
|
||||
|
||||
return stateObject
|
||||
}
|
||||
|
|
@ -323,9 +323,9 @@ func (self *StateDB) Copy() *StateDB {
|
|||
// ignore error - we assume state-to-be-copied always exists
|
||||
state, _ := New(common.Hash{}, self.db)
|
||||
state.trie = self.trie
|
||||
for k, stateObject := range self.stateObjects {
|
||||
for addr, stateObject := range self.stateObjects {
|
||||
if stateObject.dirty {
|
||||
state.stateObjects[k] = stateObject.Copy()
|
||||
state.stateObjects[addr] = stateObject.Copy(self.db)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue