good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
e9b031b8
Commit
e9b031b8
authored
Sep 1, 2015
by
Gustav Simonsson
Browse files
Options
Downloads
Plain Diff
Merge pull request #1755 from fjl/coinbase
core: improve block gas tracking
parents
1ffc5b0c
00b45acb
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/block_processor.go
+19
-9
19 additions, 9 deletions
core/block_processor.go
core/state_transition.go
+8
-15
8 additions, 15 deletions
core/state_transition.go
with
27 additions
and
24 deletions
core/block_processor.go
+
19
−
9
View file @
e9b031b8
...
@@ -56,6 +56,18 @@ type BlockProcessor struct {
...
@@ -56,6 +56,18 @@ type BlockProcessor struct {
eventMux
*
event
.
TypeMux
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
{
func
NewBlockProcessor
(
db
common
.
Database
,
pow
pow
.
PoW
,
chainManager
*
ChainManager
,
eventMux
*
event
.
TypeMux
)
*
BlockProcessor
{
sm
:=
&
BlockProcessor
{
sm
:=
&
BlockProcessor
{
chainDb
:
db
,
chainDb
:
db
,
...
@@ -64,16 +76,15 @@ func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManag
...
@@ -64,16 +76,15 @@ func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManag
bc
:
chainManager
,
bc
:
chainManager
,
eventMux
:
eventMux
,
eventMux
:
eventMux
,
}
}
return
sm
return
sm
}
}
func
(
sm
*
BlockProcessor
)
TransitionState
(
statedb
*
state
.
StateDB
,
parent
,
block
*
types
.
Block
,
transientProcess
bool
)
(
receipts
types
.
Receipts
,
err
error
)
{
func
(
sm
*
BlockProcessor
)
TransitionState
(
statedb
*
state
.
StateDB
,
parent
,
block
*
types
.
Block
,
transientProcess
bool
)
(
receipts
types
.
Receipts
,
err
error
)
{
coinbase
:=
statedb
.
GetOrNewStateObject
(
block
.
Coinbase
())
gp
:=
statedb
.
GetOrNewStateObject
(
block
.
Coinbase
())
coinbase
.
SetGasLimit
(
block
.
GasLimit
())
gp
.
SetGasLimit
(
block
.
GasLimit
())
// Process the transactions on to parent state
// 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
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -81,9 +92,8 @@ func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block
...
@@ -81,9 +92,8 @@ func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block
return
receipts
,
nil
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
)
{
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
)
{
cb
:=
statedb
.
GetStateObject
(
coinbase
.
Address
())
_
,
gas
,
err
:=
ApplyMessage
(
NewEnv
(
statedb
,
self
.
bc
,
tx
,
header
),
tx
,
gp
)
_
,
gas
,
err
:=
ApplyMessage
(
NewEnv
(
statedb
,
self
.
bc
,
tx
,
header
),
tx
,
cb
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
nil
,
err
return
nil
,
nil
,
err
}
}
...
@@ -118,7 +128,7 @@ func (self *BlockProcessor) ChainManager() *ChainManager {
...
@@ -118,7 +128,7 @@ func (self *BlockProcessor) ChainManager() *ChainManager {
return
self
.
bc
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
(
var
(
receipts
types
.
Receipts
receipts
types
.
Receipts
totalUsedGas
=
big
.
NewInt
(
0
)
totalUsedGas
=
big
.
NewInt
(
0
)
...
@@ -130,7 +140,7 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
...
@@ -130,7 +140,7 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
for
i
,
tx
:=
range
txs
{
for
i
,
tx
:=
range
txs
{
statedb
.
StartRecord
(
tx
.
Hash
(),
block
.
Hash
(),
i
)
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
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
...
This diff is collapsed.
Click to expand it.
core/state_transition.go
+
8
−
15
View file @
e9b031b8
...
@@ -45,7 +45,7 @@ import (
...
@@ -45,7 +45,7 @@ import (
* 6) Derive new state root
* 6) Derive new state root
*/
*/
type
StateTransition
struct
{
type
StateTransition
struct
{
coinbase
common
.
Address
gp
GasPool
msg
Message
msg
Message
gas
,
gasPrice
*
big
.
Int
gas
,
gasPrice
*
big
.
Int
initialGas
*
big
.
Int
initialGas
*
big
.
Int
...
@@ -53,8 +53,6 @@ type StateTransition struct {
...
@@ -53,8 +53,6 @@ type StateTransition struct {
data
[]
byte
data
[]
byte
state
*
state
.
StateDB
state
*
state
.
StateDB
cb
,
rec
,
sen
*
state
.
StateObject
env
vm
.
Environment
env
vm
.
Environment
}
}
...
@@ -96,13 +94,13 @@ func IntrinsicGas(data []byte) *big.Int {
...
@@ -96,13 +94,13 @@ func IntrinsicGas(data []byte) *big.Int {
return
igas
return
igas
}
}
func
ApplyMessage
(
env
vm
.
Environment
,
msg
Message
,
coinbase
*
state
.
StateObject
)
([]
byte
,
*
big
.
Int
,
error
)
{
func
ApplyMessage
(
env
vm
.
Environment
,
msg
Message
,
gp
GasPool
)
([]
byte
,
*
big
.
Int
,
error
)
{
return
NewStateTransition
(
env
,
msg
,
coinbase
)
.
transitionState
()
return
NewStateTransition
(
env
,
msg
,
gp
)
.
transitionState
()
}
}
func
NewStateTransition
(
env
vm
.
Environment
,
msg
Message
,
coinbase
*
state
.
StateObject
)
*
StateTransition
{
func
NewStateTransition
(
env
vm
.
Environment
,
msg
Message
,
gp
GasPool
)
*
StateTransition
{
return
&
StateTransition
{
return
&
StateTransition
{
coinbase
:
coinbase
.
Address
()
,
gp
:
gp
,
env
:
env
,
env
:
env
,
msg
:
msg
,
msg
:
msg
,
gas
:
new
(
big
.
Int
),
gas
:
new
(
big
.
Int
),
...
@@ -111,13 +109,9 @@ func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateOb
...
@@ -111,13 +109,9 @@ func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateOb
value
:
msg
.
Value
(),
value
:
msg
.
Value
(),
data
:
msg
.
Data
(),
data
:
msg
.
Data
(),
state
:
env
.
State
(),
state
:
env
.
State
(),
cb
:
coinbase
,
}
}
}
}
func
(
self
*
StateTransition
)
Coinbase
()
*
state
.
StateObject
{
return
self
.
state
.
GetOrNewStateObject
(
self
.
coinbase
)
}
func
(
self
*
StateTransition
)
From
()
(
*
state
.
StateObject
,
error
)
{
func
(
self
*
StateTransition
)
From
()
(
*
state
.
StateObject
,
error
)
{
f
,
err
:=
self
.
msg
.
From
()
f
,
err
:=
self
.
msg
.
From
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -160,7 +154,7 @@ func (self *StateTransition) BuyGas() error {
...
@@ -160,7 +154,7 @@ func (self *StateTransition) BuyGas() error {
if
sender
.
Balance
()
.
Cmp
(
mgval
)
<
0
{
if
sender
.
Balance
()
.
Cmp
(
mgval
)
<
0
{
return
fmt
.
Errorf
(
"insufficient ETH for gas (%x). Req %v, has %v"
,
sender
.
Address
()
.
Bytes
()[
:
4
],
mgval
,
sender
.
Balance
())
return
fmt
.
Errorf
(
"insufficient ETH for gas (%x). Req %v, has %v"
,
sender
.
Address
()
.
Bytes
()[
:
4
],
mgval
,
sender
.
Balance
())
}
}
if
err
=
self
.
Coinbase
()
.
SubGas
(
mgas
,
self
.
gasPrice
);
err
!=
nil
{
if
err
=
self
.
gp
.
SubGas
(
mgas
,
self
.
gasPrice
);
err
!=
nil
{
return
err
return
err
}
}
self
.
AddGas
(
mgas
)
self
.
AddGas
(
mgas
)
...
@@ -241,13 +235,12 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er
...
@@ -241,13 +235,12 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er
}
}
self
.
refundGas
()
self
.
refundGas
()
self
.
state
.
AddBalance
(
self
.
c
oinbase
,
new
(
big
.
Int
)
.
Mul
(
self
.
gasUsed
(),
self
.
gasPrice
))
self
.
state
.
AddBalance
(
self
.
env
.
C
oinbase
()
,
new
(
big
.
Int
)
.
Mul
(
self
.
gasUsed
(),
self
.
gasPrice
))
return
ret
,
self
.
gasUsed
(),
err
return
ret
,
self
.
gasUsed
(),
err
}
}
func
(
self
*
StateTransition
)
refundGas
()
{
func
(
self
*
StateTransition
)
refundGas
()
{
coinbase
:=
self
.
Coinbase
()
sender
,
_
:=
self
.
From
()
// err already checked
sender
,
_
:=
self
.
From
()
// err already checked
// Return remaining gas
// Return remaining gas
remaining
:=
new
(
big
.
Int
)
.
Mul
(
self
.
gas
,
self
.
gasPrice
)
remaining
:=
new
(
big
.
Int
)
.
Mul
(
self
.
gas
,
self
.
gasPrice
)
...
@@ -258,7 +251,7 @@ func (self *StateTransition) refundGas() {
...
@@ -258,7 +251,7 @@ func (self *StateTransition) refundGas() {
self
.
gas
.
Add
(
self
.
gas
,
refund
)
self
.
gas
.
Add
(
self
.
gas
,
refund
)
self
.
state
.
AddBalance
(
sender
.
Address
(),
refund
.
Mul
(
refund
,
self
.
gasPrice
))
self
.
state
.
AddBalance
(
sender
.
Address
(),
refund
.
Mul
(
refund
,
self
.
gasPrice
))
coinbase
.
AddGas
(
self
.
gas
,
self
.
gasPrice
)
self
.
gp
.
AddGas
(
self
.
gas
,
self
.
gasPrice
)
}
}
func
(
self
*
StateTransition
)
gasUsed
()
*
big
.
Int
{
func
(
self
*
StateTransition
)
gasUsed
()
*
big
.
Int
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment