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:
Felix Lange 2016-09-22 21:04:58 +02:00 committed by Péter Szilágyi
parent e859f36967
commit 4d989d0065
4 changed files with 25 additions and 36 deletions

View file

@ -33,14 +33,14 @@ type ManagedState struct {
mu sync.RWMutex 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 // ManagedState returns a new managed state with the statedb as it's backing layer
func ManageState(statedb *StateDB) *ManagedState { func ManageState(statedb *StateDB) *ManagedState {
return &ManagedState{ return &ManagedState{
StateDB: statedb.Copy(), 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 := ms.GetOrNewStateObject(addr)
so.SetNonce(nonce) so.SetNonce(nonce)
ms.accounts[addr.Str()] = newAccount(so) ms.accounts[addr] = newAccount(so)
} }
// HasAccount returns whether the given address is managed or not // 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 { func (ms *ManagedState) hasAccount(addr common.Address) bool {
_, ok := ms.accounts[addr.Str()] _, ok := ms.accounts[addr]
return ok return ok
} }
// populate the managed state // populate the managed state
func (ms *ManagedState) getAccount(addr common.Address) *account { func (ms *ManagedState) getAccount(addr common.Address) *account {
straddr := addr.Str() if account, ok := ms.accounts[addr]; !ok {
if account, ok := ms.accounts[straddr]; !ok {
so := ms.GetOrNewStateObject(addr) so := ms.GetOrNewStateObject(addr)
ms.accounts[straddr] = newAccount(so) ms.accounts[addr] = newAccount(so)
} else { } else {
// Always make sure the state account nonce isn't actually higher // Always make sure the state account nonce isn't actually higher
// than the tracked one. // than the tracked one.
so := ms.StateDB.GetStateObject(addr) so := ms.StateDB.GetStateObject(addr)
if so != nil && uint64(len(account.nonces))+account.nstart < so.nonce { 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 { func newAccount(so *StateObject) *account {

View file

@ -30,10 +30,10 @@ func create() (*ManagedState, *account) {
statedb, _ := New(common.Hash{}, db) statedb, _ := New(common.Hash{}, db)
ms := ManageState(statedb) ms := ManageState(statedb)
so := &StateObject{address: addr, nonce: 100} so := &StateObject{address: addr, nonce: 100}
ms.StateDB.stateObjects[addr.Str()] = so ms.StateDB.stateObjects[addr] = so
ms.accounts[addr.Str()] = newAccount(so) ms.accounts[addr] = newAccount(so)
return ms, ms.accounts[addr.Str()] return ms, ms.accounts[addr]
} }
func TestNewNonce(t *testing.T) { func TestNewNonce(t *testing.T) {
@ -92,7 +92,7 @@ func TestRemoteNonceChange(t *testing.T) {
account.nonces = append(account.nonces, nn...) account.nonces = append(account.nonces, nn...)
nonce := ms.NewNonce(addr) nonce := ms.NewNonce(addr)
ms.StateDB.stateObjects[addr.Str()].nonce = 200 ms.StateDB.stateObjects[addr].nonce = 200
nonce = ms.NewNonce(addr) nonce = ms.NewNonce(addr)
if nonce != 200 { if nonce != 200 {
t.Error("expected nonce after remote update to be", 201, "got", nonce) 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.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) nonce = ms.NewNonce(addr)
if nonce != 204 { if nonce != 204 {
t.Error("expected nonce after remote update to be", 201, "got", nonce) t.Error("expected nonce after remote update to be", 201, "got", nonce)

View file

@ -58,7 +58,6 @@ func (self Storage) Copy() Storage {
} }
type StateObject struct { type StateObject struct {
db trie.Database // State database for storing state changes
trie *trie.SecureTrie trie *trie.SecureTrie
// Address belonging to this account // Address belonging to this account
@ -84,7 +83,6 @@ type StateObject struct {
func NewStateObject(address common.Address, db trie.Database) *StateObject { func NewStateObject(address common.Address, db trie.Database) *StateObject {
object := &StateObject{ object := &StateObject{
db: db,
address: address, address: address,
balance: new(big.Int), balance: new(big.Int),
dirty: true, dirty: true,
@ -119,10 +117,6 @@ func (c *StateObject) setAddr(addr, value common.Hash) {
c.trie.Update(addr[:], v) c.trie.Update(addr[:], v)
} }
func (self *StateObject) Storage() Storage {
return self.storage
}
func (self *StateObject) GetState(key common.Hash) common.Hash { func (self *StateObject) GetState(key common.Hash) common.Hash {
value, exists := self.storage[key] value, exists := self.storage[key]
if !exists { if !exists {
@ -178,15 +172,11 @@ func (c *StateObject) SetBalance(amount *big.Int) {
c.dirty = true 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 // Return the gas back to the origin. Used by the Virtual machine or Closures
func (c *StateObject) ReturnGas(gas, price *big.Int) {} func (c *StateObject) ReturnGas(gas, price *big.Int) {}
func (self *StateObject) Copy() *StateObject { func (self *StateObject) Copy(db trie.Database) *StateObject {
stateObject := NewStateObject(self.Address(), self.db) stateObject := NewStateObject(self.address, db)
stateObject.balance.Set(self.balance) stateObject.balance.Set(self.balance)
stateObject.codeHash = common.CopyBytes(self.codeHash) stateObject.codeHash = common.CopyBytes(self.codeHash)
stateObject.nonce = self.nonce stateObject.nonce = self.nonce
@ -278,7 +268,7 @@ func (c *StateObject) EncodeRLP(w io.Writer) error {
// DecodeObject decodes an RLP-encoded state object. // DecodeObject decodes an RLP-encoded state object.
func DecodeObject(address common.Address, db trie.Database, data []byte) (*StateObject, error) { func DecodeObject(address common.Address, db trie.Database, data []byte) (*StateObject, error) {
var ( var (
obj = &StateObject{address: address, db: db, storage: make(Storage)} obj = &StateObject{address: address, storage: make(Storage)}
ext extStateObject ext extStateObject
err error err error
) )

View file

@ -43,7 +43,7 @@ type StateDB struct {
db ethdb.Database db ethdb.Database
trie *trie.SecureTrie trie *trie.SecureTrie
stateObjects map[string]*StateObject stateObjects map[common.Address]*StateObject
refund *big.Int refund *big.Int
@ -62,7 +62,7 @@ func New(root common.Hash, db ethdb.Database) (*StateDB, error) {
return &StateDB{ return &StateDB{
db: db, db: db,
trie: tr, trie: tr,
stateObjects: make(map[string]*StateObject), stateObjects: make(map[common.Address]*StateObject),
refund: new(big.Int), refund: new(big.Int),
logs: make(map[common.Hash]vm.Logs), logs: make(map[common.Hash]vm.Logs),
}, nil }, nil
@ -83,7 +83,7 @@ func (self *StateDB) Reset(root common.Hash) error {
*self = StateDB{ *self = StateDB{
db: self.db, db: self.db,
trie: tr, trie: tr,
stateObjects: make(map[string]*StateObject), stateObjects: make(map[common.Address]*StateObject),
refund: new(big.Int), refund: new(big.Int),
logs: make(map[common.Hash]vm.Logs), logs: make(map[common.Hash]vm.Logs),
} }
@ -242,12 +242,12 @@ func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
addr := stateObject.Address() addr := stateObject.Address()
self.trie.Delete(addr[:]) 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 // Retrieve a state object given my the address. Nil if not found
func (self *StateDB) GetStateObject(addr common.Address) (stateObject *StateObject) { func (self *StateDB) GetStateObject(addr common.Address) (stateObject *StateObject) {
stateObject = self.stateObjects[addr.Str()] stateObject = self.stateObjects[addr]
if stateObject != nil { if stateObject != nil {
if stateObject.deleted { if stateObject.deleted {
stateObject = nil stateObject = nil
@ -270,7 +270,7 @@ func (self *StateDB) GetStateObject(addr common.Address) (stateObject *StateObje
} }
func (self *StateDB) SetStateObject(object *StateObject) { 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 // 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 := NewStateObject(addr, self.db)
stateObject.SetNonce(StartingNonce) stateObject.SetNonce(StartingNonce)
self.stateObjects[addr.Str()] = stateObject self.stateObjects[addr] = stateObject
return stateObject return stateObject
} }
@ -323,9 +323,9 @@ func (self *StateDB) Copy() *StateDB {
// ignore error - we assume state-to-be-copied always exists // ignore error - we assume state-to-be-copied always exists
state, _ := New(common.Hash{}, self.db) state, _ := New(common.Hash{}, self.db)
state.trie = self.trie state.trie = self.trie
for k, stateObject := range self.stateObjects { for addr, stateObject := range self.stateObjects {
if stateObject.dirty { if stateObject.dirty {
state.stateObjects[k] = stateObject.Copy() state.stateObjects[addr] = stateObject.Copy(self.db)
} }
} }