Skip to content

Commit 9c3db1b

Browse files
committed
Merge pull request bnb-chain#1369 from obscuren/statedb-update-cleanup
core, core/state: throw out intermediate state
2 parents 9bb575b + 47460b3 commit 9c3db1b

16 files changed

Lines changed: 106 additions & 49 deletions

‎core/block_processor.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated
7777
}
7878

7979
// Update the state with pending changes
80-
statedb.Update()
80+
statedb.SyncIntermediate()
8181

8282
usedGas.Add(usedGas, gas)
8383
receipt := types.NewReceipt(statedb.Root().Bytes(), usedGas)
@@ -243,7 +243,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
243243

244244
// Commit state objects/accounts to a temporary trie (does not save)
245245
// used to calculate the state root.
246-
state.Update()
246+
state.SyncObjects()
247247
if header.Root != state.Root() {
248248
err = fmt.Errorf("invalid merkle root. received=%x got=%x", header.Root, state.Root())
249249
return

‎core/chain_makers.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (b *BlockGen) AddTx(tx *types.Transaction) {
7777
if err != nil {
7878
panic(err)
7979
}
80-
b.statedb.Update()
80+
b.statedb.SyncIntermediate()
8181
b.header.GasUsed.Add(b.header.GasUsed, gas)
8282
receipt := types.NewReceipt(b.statedb.Root().Bytes(), b.header.GasUsed)
8383
logs := b.statedb.GetLogs(tx.Hash())
@@ -135,7 +135,7 @@ func GenerateChain(parent *types.Block, db common.Database, n int, gen func(int,
135135
gen(i, b)
136136
}
137137
AccumulateRewards(statedb, h, b.uncles)
138-
statedb.Update()
138+
statedb.SyncIntermediate()
139139
h.Root = statedb.Root()
140140
return types.NewBlock(h, b.txs, b.uncles, b.receipts)
141141
}

‎core/genesis.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func GenesisBlockForTesting(db common.Database, addr common.Address, balance *bi
6464
statedb := state.New(common.Hash{}, db)
6565
obj := statedb.GetOrNewStateObject(addr)
6666
obj.SetBalance(balance)
67-
statedb.Update()
67+
statedb.SyncObjects()
6868
statedb.Sync()
6969
block := types.NewBlock(&types.Header{
7070
Difficulty: params.GenesisDifficulty,

‎core/state/state_object.go‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ type StateObject struct {
5757
initCode Code
5858
// Cached storage (flushed when updated)
5959
storage Storage
60-
// Temporary prepaid gas, reward after transition
61-
prepaid *big.Int
6260

6361
// Total gas pool is the total amount of gas currently
6462
// left if this object is the coinbase. Gas is directly
@@ -77,14 +75,10 @@ func (self *StateObject) Reset() {
7775
}
7876

7977
func NewStateObject(address common.Address, db common.Database) *StateObject {
80-
// This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter.
81-
//address := common.ToAddress(addr)
82-
8378
object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true}
8479
object.trie = trie.NewSecure((common.Hash{}).Bytes(), db)
8580
object.storage = make(Storage)
8681
object.gasPool = new(big.Int)
87-
object.prepaid = new(big.Int)
8882

8983
return object
9084
}
@@ -110,7 +104,6 @@ func NewStateObjectFromBytes(address common.Address, data []byte, db common.Data
110104
object.trie = trie.NewSecure(extobject.Root[:], db)
111105
object.storage = make(map[string]common.Hash)
112106
object.gasPool = new(big.Int)
113-
object.prepaid = new(big.Int)
114107
object.code, _ = db.Get(extobject.CodeHash)
115108

116109
return object
@@ -172,7 +165,6 @@ func (self *StateObject) Update() {
172165

173166
self.setAddr([]byte(key), value)
174167
}
175-
self.storage = make(Storage)
176168
}
177169

178170
func (c *StateObject) GetInstr(pc *big.Int) *common.Value {

‎core/state/state_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestNull(t *testing.T) {
7272
//value := common.FromHex("0x823140710bf13990e4500136726d8b55")
7373
var value common.Hash
7474
state.SetState(address, common.Hash{}, value)
75-
state.Update()
75+
state.SyncIntermediate()
7676
state.Sync()
7777
value = state.GetState(address, common.Hash{})
7878
if !common.EmptyHash(value) {

‎core/state/statedb.go‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
type StateDB struct {
1919
db common.Database
2020
trie *trie.SecureTrie
21+
root common.Hash
2122

2223
stateObjects map[string]*StateObject
2324

@@ -31,7 +32,7 @@ type StateDB struct {
3132
// Create a new state from a given trie
3233
func New(root common.Hash, db common.Database) *StateDB {
3334
trie := trie.NewSecure(root[:], db)
34-
return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: new(big.Int), logs: make(map[common.Hash]Logs)}
35+
return &StateDB{root: root, db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: new(big.Int), logs: make(map[common.Hash]Logs)}
3536
}
3637

3738
func (self *StateDB) PrintRoot() {
@@ -185,7 +186,7 @@ func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
185186
addr := stateObject.Address()
186187
self.trie.Delete(addr[:])
187188

188-
delete(self.stateObjects, addr.Str())
189+
//delete(self.stateObjects, addr.Str())
189190
}
190191

191192
// Retrieve a state object given my the address. Nil if not found
@@ -323,7 +324,8 @@ func (self *StateDB) Refunds() *big.Int {
323324
return self.refund
324325
}
325326

326-
func (self *StateDB) Update() {
327+
// SyncIntermediate updates the intermediate state and all mid steps
328+
func (self *StateDB) SyncIntermediate() {
327329
self.refund = new(big.Int)
328330

329331
for _, stateObject := range self.stateObjects {
@@ -340,6 +342,24 @@ func (self *StateDB) Update() {
340342
}
341343
}
342344

345+
// SyncObjects syncs the changed objects to the trie
346+
func (self *StateDB) SyncObjects() {
347+
self.trie = trie.NewSecure(self.root[:], self.db)
348+
349+
self.refund = new(big.Int)
350+
351+
for _, stateObject := range self.stateObjects {
352+
if stateObject.remove {
353+
self.DeleteStateObject(stateObject)
354+
} else {
355+
stateObject.Update()
356+
357+
self.UpdateStateObject(stateObject)
358+
}
359+
stateObject.dirty = false
360+
}
361+
}
362+
343363
// Debug stuff
344364
func (self *StateDB) CreateOutputForDiff() {
345365
for _, stateObject := range self.stateObjects {

‎miner/worker.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func (self *worker) commitNewWork() {
453453
if atomic.LoadInt32(&self.mining) == 1 {
454454
// commit state root after all state transitions.
455455
core.AccumulateRewards(self.current.state, header, uncles)
456-
current.state.Update()
456+
current.state.SyncObjects()
457457
self.current.state.Sync()
458458
header.Root = current.state.Root()
459459
}

‎tests/block_test_util.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, erro
215215
}
216216
}
217217
// sync objects to trie
218-
statedb.Update()
218+
statedb.SyncObjects()
219219
// sync trie to disk
220220
statedb.Sync()
221221

‎tests/state_test_util.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.
175175
if core.IsNonceErr(err) || core.IsInvalidTxErr(err) || state.IsGasLimitErr(err) {
176176
statedb.Set(snapshot)
177177
}
178-
statedb.Update()
178+
statedb.SyncObjects()
179179

180180
return ret, vmenv.state.Logs(), vmenv.Gas, err
181181
}

‎trie/fullnode.go‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package trie
22

3-
import "fmt"
4-
53
type FullNode struct {
64
trie *Trie
75
nodes [17]Node
6+
dirty bool
87
}
98

109
func NewFullNode(t *Trie) *FullNode {
1110
return &FullNode{trie: t}
1211
}
1312

14-
func (self *FullNode) Dirty() bool { return true }
13+
func (self *FullNode) Dirty() bool { return self.dirty }
1514
func (self *FullNode) Value() Node {
1615
self.nodes[16] = self.trie.trans(self.nodes[16])
1716
return self.nodes[16]
@@ -24,9 +23,10 @@ func (self *FullNode) Copy(t *Trie) Node {
2423
nnode := NewFullNode(t)
2524
for i, node := range self.nodes {
2625
if node != nil {
27-
nnode.nodes[i] = node.Copy(t)
26+
nnode.nodes[i] = node
2827
}
2928
}
29+
nnode.dirty = true
3030

3131
return nnode
3232
}
@@ -60,11 +60,8 @@ func (self *FullNode) RlpData() interface{} {
6060
}
6161

6262
func (self *FullNode) set(k byte, value Node) {
63-
if _, ok := value.(*ValueNode); ok && k != 16 {
64-
fmt.Println(value, k)
65-
}
66-
6763
self.nodes[int(k)] = value
64+
self.dirty = true
6865
}
6966

7067
func (self *FullNode) branch(i byte) Node {
@@ -75,3 +72,7 @@ func (self *FullNode) branch(i byte) Node {
7572
}
7673
return nil
7774
}
75+
76+
func (self *FullNode) setDirty(dirty bool) {
77+
self.dirty = dirty
78+
}

0 commit comments

Comments
 (0)