Skip to content

Commit d211255

Browse files
author
Gustav Simonsson
committed
Merge pull request ethereum#1755 from fjl/coinbase
core: improve block gas tracking (cherry picked from commit e9b031b) Conflicts: core/block_processor.go
1 parent f0c7af0 commit d211255

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

core/block_processor.go

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

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

7384
func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block *types.Block, transientProcess bool) (receipts types.Receipts, err error) {
74-
coinbase := statedb.GetOrNewStateObject(block.Coinbase())
75-
coinbase.SetGasLimit(block.GasLimit())
85+
gp := statedb.GetOrNewStateObject(block.Coinbase())
86+
gp.SetGasLimit(block.GasLimit())
7687

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

8394
return receipts, nil
8495
}
8596

86-
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) {
87-
// If we are mining this block and validating we want to set the logs back to 0
88-
89-
cb := statedb.GetStateObject(coinbase.Address())
90-
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, cb)
97+
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) {
98+
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, gp)
9199
if err != nil {
92100
return nil, nil, err
93101
}
@@ -122,7 +130,7 @@ func (self *BlockProcessor) ChainManager() *ChainManager {
122130
return self.bc
123131
}
124132

125-
func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) {
133+
func (self *BlockProcessor) ApplyTransactions(gp GasPool, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) {
126134
var (
127135
receipts types.Receipts
128136
totalUsedGas = big.NewInt(0)
@@ -134,7 +142,7 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
134142
for i, tx := range txs {
135143
statedb.StartRecord(tx.Hash(), block.Hash(), i)
136144

137-
receipt, txGas, err := self.ApplyTransaction(coinbase, statedb, header, tx, totalUsedGas, transientProcess)
145+
receipt, txGas, err := self.ApplyTransaction(gp, statedb, header, tx, totalUsedGas, transientProcess)
138146
if err != nil {
139147
return nil, err
140148
}

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)