diff options
author | Felix Lange <fjl@twurst.com> | 2015-09-02 03:35:30 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-09-02 05:11:03 +0800 |
commit | 00b45acb9e104f3229a7f2f3be88686d4bcb5706 (patch) | |
tree | 162a82ce1434c9fc8584284d6028dc423db80d2e /core/block_processor.go | |
parent | 1ffc5b0cfdf09b7faf07d2f1bc4d76eab7785a92 (diff) | |
download | go-tangerine-00b45acb9e104f3229a7f2f3be88686d4bcb5706.tar go-tangerine-00b45acb9e104f3229a7f2f3be88686d4bcb5706.tar.gz go-tangerine-00b45acb9e104f3229a7f2f3be88686d4bcb5706.tar.bz2 go-tangerine-00b45acb9e104f3229a7f2f3be88686d4bcb5706.tar.lz go-tangerine-00b45acb9e104f3229a7f2f3be88686d4bcb5706.tar.xz go-tangerine-00b45acb9e104f3229a7f2f3be88686d4bcb5706.tar.zst go-tangerine-00b45acb9e104f3229a7f2f3be88686d4bcb5706.zip |
core: improve block gas tracking
Diffstat (limited to 'core/block_processor.go')
-rw-r--r-- | core/block_processor.go | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/core/block_processor.go b/core/block_processor.go index 99d27fa71..1238fda7b 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -56,6 +56,18 @@ type BlockProcessor struct { eventMux *event.TypeMux } +// TODO: type GasPool big.Int +// +// GasPool is implemented by state.StateObject. This is a historical +// coincidence. Gas tracking should move out of StateObject. + +// GasPool tracks the amount of gas available during +// execution of the transactions in a block. +type GasPool interface { + AddGas(gas, price *big.Int) + SubGas(gas, price *big.Int) error +} + func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor { sm := &BlockProcessor{ chainDb: db, @@ -64,16 +76,15 @@ func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManag bc: chainManager, eventMux: eventMux, } - return sm } func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block *types.Block, transientProcess bool) (receipts types.Receipts, err error) { - coinbase := statedb.GetOrNewStateObject(block.Coinbase()) - coinbase.SetGasLimit(block.GasLimit()) + gp := statedb.GetOrNewStateObject(block.Coinbase()) + gp.SetGasLimit(block.GasLimit()) // Process the transactions on to parent state - receipts, err = sm.ApplyTransactions(coinbase, statedb, block, block.Transactions(), transientProcess) + receipts, err = sm.ApplyTransactions(gp, statedb, block, block.Transactions(), transientProcess) if err != nil { return nil, err } @@ -81,9 +92,8 @@ func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block return receipts, nil } -func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) { - cb := statedb.GetStateObject(coinbase.Address()) - _, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, cb) +func (self *BlockProcessor) ApplyTransaction(gp GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) { + _, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, gp) if err != nil { return nil, nil, err } @@ -118,7 +128,7 @@ func (self *BlockProcessor) ChainManager() *ChainManager { return self.bc } -func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) { +func (self *BlockProcessor) ApplyTransactions(gp GasPool, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) { var ( receipts types.Receipts totalUsedGas = big.NewInt(0) @@ -130,7 +140,7 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state for i, tx := range txs { statedb.StartRecord(tx.Hash(), block.Hash(), i) - receipt, txGas, err := self.ApplyTransaction(coinbase, statedb, header, tx, totalUsedGas, transientProcess) + receipt, txGas, err := self.ApplyTransaction(gp, statedb, header, tx, totalUsedGas, transientProcess) if err != nil { return nil, err } |