aboutsummaryrefslogtreecommitdiffstats
path: root/core/state/state_object.go
diff options
context:
space:
mode:
authorMartin Holst Swende <martin@swende.se>2017-10-02 03:07:30 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-03-28 14:29:28 +0800
commit958ed4f3d977b08465915e475e11aaab3d2dc574 (patch)
tree177e7bf727a11cc18237c5299bfceffe56ccee88 /core/state/state_object.go
parent1a8894b3d59bdf592244b91b6a629f7cf25dcdde (diff)
downloadgo-tangerine-958ed4f3d977b08465915e475e11aaab3d2dc574.tar.gz
go-tangerine-958ed4f3d977b08465915e475e11aaab3d2dc574.tar.zst
go-tangerine-958ed4f3d977b08465915e475e11aaab3d2dc574.zip
core/state: rework dirty handling to avoid quadratic overhead
Diffstat (limited to 'core/state/state_object.go')
-rw-r--r--core/state/state_object.go51
1 files changed, 13 insertions, 38 deletions
diff --git a/core/state/state_object.go b/core/state/state_object.go
index b2112bfae..523bb7150 100644
--- a/core/state/state_object.go
+++ b/core/state/state_object.go
@@ -85,9 +85,7 @@ type stateObject struct {
// during the "update" phase of the state transition.
dirtyCode bool // true if the code was updated
suicided bool
- touched bool
deleted bool
- onDirty func(addr common.Address) // Callback method to mark a state object newly dirty
}
// empty returns whether the account is considered empty.
@@ -105,7 +103,7 @@ type Account struct {
}
// newObject creates a state object.
-func newObject(db *StateDB, address common.Address, data Account, onDirty func(addr common.Address)) *stateObject {
+func newObject(db *StateDB, address common.Address, data Account) *stateObject {
if data.Balance == nil {
data.Balance = new(big.Int)
}
@@ -119,7 +117,6 @@ func newObject(db *StateDB, address common.Address, data Account, onDirty func(a
data: data,
cachedStorage: make(Storage),
dirtyStorage: make(Storage),
- onDirty: onDirty,
}
}
@@ -137,23 +134,17 @@ func (self *stateObject) setError(err error) {
func (self *stateObject) markSuicided() {
self.suicided = true
- if self.onDirty != nil {
- self.onDirty(self.Address())
- self.onDirty = nil
- }
}
func (c *stateObject) touch() {
- c.db.journal = append(c.db.journal, touchChange{
- account: &c.address,
- prev: c.touched,
- prevDirty: c.onDirty == nil,
+ c.db.journal.append(touchChange{
+ account: &c.address,
})
- if c.onDirty != nil {
- c.onDirty(c.Address())
- c.onDirty = nil
+ if c.address == ripemd {
+ //Explicitly put it in the dirty-cache, which is otherwise
+ // generated from flattened journals
+ c.db.journal.dirtyOverride(c.address)
}
- c.touched = true
}
func (c *stateObject) getTrie(db Database) Trie {
@@ -195,7 +186,7 @@ func (self *stateObject) GetState(db Database, key common.Hash) common.Hash {
// SetState updates a value in account storage.
func (self *stateObject) SetState(db Database, key, value common.Hash) {
- self.db.journal = append(self.db.journal, storageChange{
+ self.db.journal.append(storageChange{
account: &self.address,
key: key,
prevalue: self.GetState(db, key),
@@ -207,10 +198,6 @@ func (self *stateObject) setState(key, value common.Hash) {
self.cachedStorage[key] = value
self.dirtyStorage[key] = value
- if self.onDirty != nil {
- self.onDirty(self.Address())
- self.onDirty = nil
- }
}
// updateTrie writes cached storage modifications into the object's storage trie.
@@ -274,7 +261,7 @@ func (c *stateObject) SubBalance(amount *big.Int) {
}
func (self *stateObject) SetBalance(amount *big.Int) {
- self.db.journal = append(self.db.journal, balanceChange{
+ self.db.journal.append(balanceChange{
account: &self.address,
prev: new(big.Int).Set(self.data.Balance),
})
@@ -283,17 +270,13 @@ func (self *stateObject) SetBalance(amount *big.Int) {
func (self *stateObject) setBalance(amount *big.Int) {
self.data.Balance = amount
- if self.onDirty != nil {
- self.onDirty(self.Address())
- self.onDirty = nil
- }
}
// Return the gas back to the origin. Used by the Virtual machine or Closures
func (c *stateObject) ReturnGas(gas *big.Int) {}
-func (self *stateObject) deepCopy(db *StateDB, onDirty func(addr common.Address)) *stateObject {
- stateObject := newObject(db, self.address, self.data, onDirty)
+func (self *stateObject) deepCopy(db *StateDB) *stateObject {
+ stateObject := newObject(db, self.address, self.data)
if self.trie != nil {
stateObject.trie = db.db.CopyTrie(self.trie)
}
@@ -333,7 +316,7 @@ func (self *stateObject) Code(db Database) []byte {
func (self *stateObject) SetCode(codeHash common.Hash, code []byte) {
prevcode := self.Code(self.db.db)
- self.db.journal = append(self.db.journal, codeChange{
+ self.db.journal.append(codeChange{
account: &self.address,
prevhash: self.CodeHash(),
prevcode: prevcode,
@@ -345,14 +328,10 @@ func (self *stateObject) setCode(codeHash common.Hash, code []byte) {
self.code = code
self.data.CodeHash = codeHash[:]
self.dirtyCode = true
- if self.onDirty != nil {
- self.onDirty(self.Address())
- self.onDirty = nil
- }
}
func (self *stateObject) SetNonce(nonce uint64) {
- self.db.journal = append(self.db.journal, nonceChange{
+ self.db.journal.append(nonceChange{
account: &self.address,
prev: self.data.Nonce,
})
@@ -361,10 +340,6 @@ func (self *stateObject) SetNonce(nonce uint64) {
func (self *stateObject) setNonce(nonce uint64) {
self.data.Nonce = nonce
- if self.onDirty != nil {
- self.onDirty(self.Address())
- self.onDirty = nil
- }
}
func (self *stateObject) CodeHash() []byte {