diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-07-04 18:42:13 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-07-04 18:42:13 +0800 |
commit | 9c3db1be1dd24c366a58a7ced22adfa0b0839efe (patch) | |
tree | 0c102cd7f590a80bc291b82274e7ca16335a250f /core | |
parent | 9bb575be7db85f967771018a915fbc4e80345ee2 (diff) | |
parent | 47460b3b4af51e08518c781680897cf2986415cc (diff) | |
download | dexon-9c3db1be1dd24c366a58a7ced22adfa0b0839efe.tar dexon-9c3db1be1dd24c366a58a7ced22adfa0b0839efe.tar.gz dexon-9c3db1be1dd24c366a58a7ced22adfa0b0839efe.tar.bz2 dexon-9c3db1be1dd24c366a58a7ced22adfa0b0839efe.tar.lz dexon-9c3db1be1dd24c366a58a7ced22adfa0b0839efe.tar.xz dexon-9c3db1be1dd24c366a58a7ced22adfa0b0839efe.tar.zst dexon-9c3db1be1dd24c366a58a7ced22adfa0b0839efe.zip |
Merge pull request #1369 from obscuren/statedb-update-cleanup
core, core/state: throw out intermediate state
Diffstat (limited to 'core')
-rw-r--r-- | core/block_processor.go | 4 | ||||
-rw-r--r-- | core/chain_makers.go | 4 | ||||
-rw-r--r-- | core/genesis.go | 2 | ||||
-rw-r--r-- | core/state/state_object.go | 8 | ||||
-rw-r--r-- | core/state/state_test.go | 2 | ||||
-rw-r--r-- | core/state/statedb.go | 26 |
6 files changed, 29 insertions, 17 deletions
diff --git a/core/block_processor.go b/core/block_processor.go index e8014ec22..e7ad059c3 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -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) @@ -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 diff --git a/core/chain_makers.go b/core/chain_makers.go index 37475e0ae..c46f627f8 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -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()) @@ -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) } diff --git a/core/genesis.go b/core/genesis.go index df13466ec..d27e7097b 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -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, diff --git a/core/state/state_object.go b/core/state/state_object.go index a31c182d2..e40aeda82 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -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 @@ -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 } @@ -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 @@ -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 { diff --git a/core/state/state_test.go b/core/state/state_test.go index 00e133dab..b63b8ae9b 100644 --- a/core/state/state_test.go +++ b/core/state/state_test.go @@ -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) { diff --git a/core/state/statedb.go b/core/state/statedb.go index f6f63f329..4ccda1fc7 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -18,6 +18,7 @@ import ( type StateDB struct { db common.Database trie *trie.SecureTrie + root common.Hash stateObjects map[string]*StateObject @@ -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() { @@ -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 @@ -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 { @@ -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 { |