Skip to content

Commit

Permalink
Merge pull request ethereum#1369 from obscuren/statedb-update-cleanup
Browse files Browse the repository at this point in the history
core, core/state: throw out intermediate state
  • Loading branch information
obscuren committed Jul 4, 2015
2 parents 9bb575b + 47460b3 commit 9c3db1b
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 49 deletions.
4 changes: 2 additions & 2 deletions core/block_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated
}

// Update the state with pending changes
statedb.Update()
statedb.SyncIntermediate()

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

// Commit state objects/accounts to a temporary trie (does not save)
// used to calculate the state root.
state.Update()
state.SyncObjects()
if header.Root != state.Root() {
err = fmt.Errorf("invalid merkle root. received=%x got=%x", header.Root, state.Root())
return
Expand Down
4 changes: 2 additions & 2 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (b *BlockGen) AddTx(tx *types.Transaction) {
if err != nil {
panic(err)
}
b.statedb.Update()
b.statedb.SyncIntermediate()
b.header.GasUsed.Add(b.header.GasUsed, gas)
receipt := types.NewReceipt(b.statedb.Root().Bytes(), b.header.GasUsed)
logs := b.statedb.GetLogs(tx.Hash())
Expand Down Expand Up @@ -135,7 +135,7 @@ func GenerateChain(parent *types.Block, db common.Database, n int, gen func(int,
gen(i, b)
}
AccumulateRewards(statedb, h, b.uncles)
statedb.Update()
statedb.SyncIntermediate()
h.Root = statedb.Root()
return types.NewBlock(h, b.txs, b.uncles, b.receipts)
}
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func GenesisBlockForTesting(db common.Database, addr common.Address, balance *bi
statedb := state.New(common.Hash{}, db)
obj := statedb.GetOrNewStateObject(addr)
obj.SetBalance(balance)
statedb.Update()
statedb.SyncObjects()
statedb.Sync()
block := types.NewBlock(&types.Header{
Difficulty: params.GenesisDifficulty,
Expand Down
8 changes: 0 additions & 8 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ type StateObject struct {
initCode Code
// Cached storage (flushed when updated)
storage Storage
// Temporary prepaid gas, reward after transition
prepaid *big.Int

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

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.ToAddress(addr)

object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true}
object.trie = trie.NewSecure((common.Hash{}).Bytes(), db)
object.storage = make(Storage)
object.gasPool = new(big.Int)
object.prepaid = new(big.Int)

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

return object
Expand Down Expand Up @@ -172,7 +165,6 @@ func (self *StateObject) Update() {

self.setAddr([]byte(key), value)
}
self.storage = make(Storage)
}

func (c *StateObject) GetInstr(pc *big.Int) *common.Value {
Expand Down
2 changes: 1 addition & 1 deletion core/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestNull(t *testing.T) {
//value := common.FromHex("0x823140710bf13990e4500136726d8b55")
var value common.Hash
state.SetState(address, common.Hash{}, value)
state.Update()
state.SyncIntermediate()
state.Sync()
value = state.GetState(address, common.Hash{})
if !common.EmptyHash(value) {
Expand Down
26 changes: 23 additions & 3 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
type StateDB struct {
db common.Database
trie *trie.SecureTrie
root common.Hash

stateObjects map[string]*StateObject

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

func (self *StateDB) PrintRoot() {
Expand Down Expand Up @@ -185,7 +186,7 @@ func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
addr := stateObject.Address()
self.trie.Delete(addr[:])

delete(self.stateObjects, addr.Str())
//delete(self.stateObjects, addr.Str())
}

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

func (self *StateDB) Update() {
// SyncIntermediate updates the intermediate state and all mid steps
func (self *StateDB) SyncIntermediate() {
self.refund = new(big.Int)

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

// SyncObjects syncs the changed objects to the trie
func (self *StateDB) SyncObjects() {
self.trie = trie.NewSecure(self.root[:], self.db)

self.refund = new(big.Int)

for _, stateObject := range self.stateObjects {
if stateObject.remove {
self.DeleteStateObject(stateObject)
} else {
stateObject.Update()

self.UpdateStateObject(stateObject)
}
stateObject.dirty = false
}
}

// Debug stuff
func (self *StateDB) CreateOutputForDiff() {
for _, stateObject := range self.stateObjects {
Expand Down
2 changes: 1 addition & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func (self *worker) commitNewWork() {
if atomic.LoadInt32(&self.mining) == 1 {
// commit state root after all state transitions.
core.AccumulateRewards(self.current.state, header, uncles)
current.state.Update()
current.state.SyncObjects()
self.current.state.Sync()
header.Root = current.state.Root()
}
Expand Down
2 changes: 1 addition & 1 deletion tests/block_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, erro
}
}
// sync objects to trie
statedb.Update()
statedb.SyncObjects()
// sync trie to disk
statedb.Sync()

Expand Down
2 changes: 1 addition & 1 deletion tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.
if core.IsNonceErr(err) || core.IsInvalidTxErr(err) || state.IsGasLimitErr(err) {
statedb.Set(snapshot)
}
statedb.Update()
statedb.SyncObjects()

return ret, vmenv.state.Logs(), vmenv.Gas, err
}
17 changes: 9 additions & 8 deletions trie/fullnode.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package trie

import "fmt"

type FullNode struct {
trie *Trie
nodes [17]Node
dirty bool
}

func NewFullNode(t *Trie) *FullNode {
return &FullNode{trie: t}
}

func (self *FullNode) Dirty() bool { return true }
func (self *FullNode) Dirty() bool { return self.dirty }
func (self *FullNode) Value() Node {
self.nodes[16] = self.trie.trans(self.nodes[16])
return self.nodes[16]
Expand All @@ -24,9 +23,10 @@ func (self *FullNode) Copy(t *Trie) Node {
nnode := NewFullNode(t)
for i, node := range self.nodes {
if node != nil {
nnode.nodes[i] = node.Copy(t)
nnode.nodes[i] = node
}
}
nnode.dirty = true

return nnode
}
Expand Down Expand Up @@ -60,11 +60,8 @@ func (self *FullNode) RlpData() interface{} {
}

func (self *FullNode) set(k byte, value Node) {
if _, ok := value.(*ValueNode); ok && k != 16 {
fmt.Println(value, k)
}

self.nodes[int(k)] = value
self.dirty = true
}

func (self *FullNode) branch(i byte) Node {
Expand All @@ -75,3 +72,7 @@ func (self *FullNode) branch(i byte) Node {
}
return nil
}

func (self *FullNode) setDirty(dirty bool) {
self.dirty = dirty
}
11 changes: 8 additions & 3 deletions trie/hashnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package trie
import "github.com/ethereum/go-ethereum/common"

type HashNode struct {
key []byte
trie *Trie
key []byte
trie *Trie
dirty bool
}

func NewHash(key []byte, trie *Trie) *HashNode {
return &HashNode{key, trie}
return &HashNode{key, trie, false}
}

func (self *HashNode) RlpData() interface{} {
Expand All @@ -19,6 +20,10 @@ func (self *HashNode) Hash() interface{} {
return self.key
}

func (self *HashNode) setDirty(dirty bool) {
self.dirty = dirty
}

// These methods will never be called but we have to satisfy Node interface
func (self *HashNode) Value() Node { return nil }
func (self *HashNode) Dirty() bool { return true }
Expand Down
1 change: 1 addition & 0 deletions trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Node interface {
fstring(string) string
Hash() interface{}
RlpData() interface{}
setDirty(dirty bool)
}

// Value node
Expand Down
12 changes: 9 additions & 3 deletions trie/shortnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ type ShortNode struct {
trie *Trie
key []byte
value Node
dirty bool
}

func NewShortNode(t *Trie, key []byte, value Node) *ShortNode {
return &ShortNode{t, []byte(CompactEncode(key)), value}
return &ShortNode{t, []byte(CompactEncode(key)), value, false}
}
func (self *ShortNode) Value() Node {
self.value = self.trie.trans(self.value)

return self.value
}
func (self *ShortNode) Dirty() bool { return true }
func (self *ShortNode) Dirty() bool { return self.dirty }
func (self *ShortNode) Copy(t *Trie) Node {
node := &ShortNode{t, nil, self.value.Copy(t)}
node := &ShortNode{t, nil, self.value.Copy(t), self.dirty}
node.key = common.CopyBytes(self.key)
node.dirty = true
return node
}

Expand All @@ -33,3 +35,7 @@ func (self *ShortNode) Hash() interface{} {
func (self *ShortNode) Key() []byte {
return CompactDecode(string(self.key))
}

func (self *ShortNode) setDirty(dirty bool) {
self.dirty = dirty
}
Loading

0 comments on commit 9c3db1b

Please sign in to comment.