Skip to content

Commit e9b031b

Browse files
author
Gustav Simonsson
committed
Merge pull request ethereum#1755 from fjl/coinbase
core: improve block gas tracking
2 parents 1ffc5b0 + 00b45ac commit e9b031b

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

core/block_processor.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ type BlockProcessor struct {
5656
eventMux *event.TypeMux
5757
}
5858

59+
// TODO: type GasPool big.Int
60+
//
61+
// GasPool is implemented by state.StateObject. This is a historical
62+
// coincidence. Gas tracking should move out of StateObject.
63+
64+
// GasPool tracks the amount of gas available during
65+
// execution of the transactions in a block.
66+
type GasPool interface {
67+
AddGas(gas, price *big.Int)
68+
SubGas(gas, price *big.Int) error
69+
}
70+
5971
func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
6072
sm := &BlockProcessor{
6173
chainDb: db,
@@ -64,26 +76,24 @@ func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManag
6476
bc: chainManager,
6577
eventMux: eventMux,
6678
}
67-
6879
return sm
6980
}
7081

7182
func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block *types.Block, transientProcess bool) (receipts types.Receipts, err error) {
72-
coinbase := statedb.GetOrNewStateObject(block.Coinbase())
73-
coinbase.SetGasLimit(block.GasLimit())
83+
gp := statedb.GetOrNewStateObject(block.Coinbase())
84+
gp.SetGasLimit(block.GasLimit())
7485

7586
// Process the transactions on to parent state
76-
receipts, err = sm.ApplyTransactions(coinbase, statedb, block, block.Transactions(), transientProcess)
87+
receipts, err = sm.ApplyTransactions(gp, statedb, block, block.Transactions(), transientProcess)
7788
if err != nil {
7889
return nil, err
7990
}
8091

8192
return receipts, nil
8293
}
8394

84-
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) {
85-
cb := statedb.GetStateObject(coinbase.Address())
86-
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, cb)
95+
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) {
96+
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, gp)
8797
if err != nil {
8898
return nil, nil, err
8999
}
@@ -118,7 +128,7 @@ func (self *BlockProcessor) ChainManager() *ChainManager {
118128
return self.bc
119129
}
120130

121-
func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) {
131+
func (self *BlockProcessor) ApplyTransactions(gp GasPool, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) {
122132
var (
123133
receipts types.Receipts
124134
totalUsedGas = big.NewInt(0)
@@ -130,7 +140,7 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
130140
for i, tx := range txs {
131141
statedb.StartRecord(tx.Hash(), block.Hash(), i)
132142

133-
receipt, txGas, err := self.ApplyTransaction(coinbase, statedb, header, tx, totalUsedGas, transientProcess)
143+
receipt, txGas, err := self.ApplyTransaction(gp, statedb, header, tx, totalUsedGas, transientProcess)
134144
if err != nil {
135145
return nil, err
136146
}

core/state_transition.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ import (
4545
* 6) Derive new state root
4646
*/
4747
type StateTransition struct {
48-
coinbase common.Address
48+
gp GasPool
4949
msg Message
5050
gas, gasPrice *big.Int
5151
initialGas *big.Int
5252
value *big.Int
5353
data []byte
5454
state *state.StateDB
5555

56-
cb, rec, sen *state.StateObject
57-
5856
env vm.Environment
5957
}
6058

@@ -96,13 +94,13 @@ func IntrinsicGas(data []byte) *big.Int {
9694
return igas
9795
}
9896

99-
func ApplyMessage(env vm.Environment, msg Message, coinbase *state.StateObject) ([]byte, *big.Int, error) {
100-
return NewStateTransition(env, msg, coinbase).transitionState()
97+
func ApplyMessage(env vm.Environment, msg Message, gp GasPool) ([]byte, *big.Int, error) {
98+
return NewStateTransition(env, msg, gp).transitionState()
10199
}
102100

103-
func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateObject) *StateTransition {
101+
func NewStateTransition(env vm.Environment, msg Message, gp GasPool) *StateTransition {
104102
return &StateTransition{
105-
coinbase: coinbase.Address(),
103+
gp: gp,
106104
env: env,
107105
msg: msg,
108106
gas: new(big.Int),
@@ -111,13 +109,9 @@ func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateOb
111109
value: msg.Value(),
112110
data: msg.Data(),
113111
state: env.State(),
114-
cb: coinbase,
115112
}
116113
}
117114

118-
func (self *StateTransition) Coinbase() *state.StateObject {
119-
return self.state.GetOrNewStateObject(self.coinbase)
120-
}
121115
func (self *StateTransition) From() (*state.StateObject, error) {
122116
f, err := self.msg.From()
123117
if err != nil {
@@ -160,7 +154,7 @@ func (self *StateTransition) BuyGas() error {
160154
if sender.Balance().Cmp(mgval) < 0 {
161155
return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address().Bytes()[:4], mgval, sender.Balance())
162156
}
163-
if err = self.Coinbase().SubGas(mgas, self.gasPrice); err != nil {
157+
if err = self.gp.SubGas(mgas, self.gasPrice); err != nil {
164158
return err
165159
}
166160
self.AddGas(mgas)
@@ -241,13 +235,12 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er
241235
}
242236

243237
self.refundGas()
244-
self.state.AddBalance(self.coinbase, new(big.Int).Mul(self.gasUsed(), self.gasPrice))
238+
self.state.AddBalance(self.env.Coinbase(), new(big.Int).Mul(self.gasUsed(), self.gasPrice))
245239

246240
return ret, self.gasUsed(), err
247241
}
248242

249243
func (self *StateTransition) refundGas() {
250-
coinbase := self.Coinbase()
251244
sender, _ := self.From() // err already checked
252245
// Return remaining gas
253246
remaining := new(big.Int).Mul(self.gas, self.gasPrice)
@@ -258,7 +251,7 @@ func (self *StateTransition) refundGas() {
258251
self.gas.Add(self.gas, refund)
259252
self.state.AddBalance(sender.Address(), refund.Mul(refund, self.gasPrice))
260253

261-
coinbase.AddGas(self.gas, self.gasPrice)
254+
self.gp.AddGas(self.gas, self.gasPrice)
262255
}
263256

264257
func (self *StateTransition) gasUsed() *big.Int {

0 commit comments

Comments
 (0)