Skip to content

Commit

Permalink
Merge pull request ethereum#102 from marioevz/fix-b11r-excess-data-gas
Browse files Browse the repository at this point in the history
cmd/evm: fix `excessDataGas` marshalling in b11r
  • Loading branch information
Inphi authored Mar 24, 2023
2 parents 8569d91 + b51448c commit 4607abe
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 46 deletions.
15 changes: 8 additions & 7 deletions cmd/evm/internal/t8ntool/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ type header struct {
}

type headerMarshaling struct {
Difficulty *math.HexOrDecimal256
Number *math.HexOrDecimal256
GasLimit math.HexOrDecimal64
GasUsed math.HexOrDecimal64
Time math.HexOrDecimal64
Extra hexutil.Bytes
BaseFee *math.HexOrDecimal256
Difficulty *math.HexOrDecimal256
Number *math.HexOrDecimal256
GasLimit math.HexOrDecimal64
GasUsed math.HexOrDecimal64
Time math.HexOrDecimal64
Extra hexutil.Bytes
BaseFee *math.HexOrDecimal256
ExcessDataGas *math.HexOrDecimal256
}

type bbInput struct {
Expand Down
21 changes: 17 additions & 4 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type ExecutionResult struct {
GasUsed math.HexOrDecimal64 `json:"gasUsed"`
BaseFee *math.HexOrDecimal256 `json:"currentBaseFee,omitempty"`
WithdrawalsRoot *common.Hash `json:"withdrawalsRoot,omitempty"`
ExcessDataGas *math.HexOrDecimal256 `json:"currentExcessDataGas,omitempty"`
}

type ommer struct {
Expand All @@ -83,7 +84,6 @@ type stEnv struct {
Ommers []ommer `json:"ommers,omitempty"`
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
ExcessDataGas *big.Int `json:"currentExcessDataGas,omitempty"`
ParentUncleHash common.Hash `json:"parentUncleHash"`
}

Expand All @@ -101,7 +101,6 @@ type stEnvMarshaling struct {
Timestamp math.HexOrDecimal64
ParentTimestamp math.HexOrDecimal64
BaseFee *math.HexOrDecimal256
ExcessDataGas *math.HexOrDecimal256
}

type rejectedTx struct {
Expand Down Expand Up @@ -161,8 +160,12 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
vmContext.Random = &rnd
}
// If excess data gas is defined, add it to vmContext
if pre.Env.ExcessDataGas != nil {
vmContext.ExcessDataGas = pre.Env.ExcessDataGas
if chainConfig.IsSharding(pre.Env.Timestamp) {
if pre.Env.ParentExcessDataGas != nil {
vmContext.ExcessDataGas = pre.Env.ParentExcessDataGas
} else {
vmContext.ExcessDataGas = big.NewInt(0)
}
}
// If DAO is supported/enabled, we need to handle it here. In geth 'proper', it's
// done in StateProcessor.Process(block, ...), right before transactions are applied.
Expand Down Expand Up @@ -301,6 +304,16 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
h := types.DeriveSha(types.Withdrawals(pre.Env.Withdrawals), trie.NewStackTrie(nil))
execRs.WithdrawalsRoot = &h
}
if vmContext.ExcessDataGas != nil {
// calculate and set the excess data gas at the end of this block execution
newBlobs := 0
for _, tx := range txs {
if tx.Type() == types.BlobTxType {
newBlobs += len(tx.DataHashes())
}
}
execRs.ExcessDataGas = (*math.HexOrDecimal256)(misc.CalcExcessDataGas(vmContext.ExcessDataGas, newBlobs))
}
return statedb, execRs, nil
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/evm/internal/t8ntool/gen_header.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions cmd/evm/internal/t8ntool/gen_stenv.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,6 @@ func Transition(ctx *cli.Context) error {
if chainConfig.IsShanghai(prestate.Env.Number) && prestate.Env.Withdrawals == nil {
return NewError(ErrorConfig, errors.New("Shanghai config but missing 'withdrawals' in env section"))
}
if chainConfig.IsSharding(prestate.Env.Timestamp) {
if prestate.Env.ExcessDataGas != nil {
// Already set, excess data gas has precedent over parent excess data gas.
} else if prestate.Env.ParentExcessDataGas != nil {
newBlobs := 0
for _, tx := range txs {
if tx.Type() == types.BlobTxType {
newBlobs += len(tx.DataHashes())
}
}
prestate.Env.ExcessDataGas = misc.CalcExcessDataGas(prestate.Env.ParentExcessDataGas, newBlobs)
}
}
isMerged := chainConfig.TerminalTotalDifficulty != nil && chainConfig.TerminalTotalDifficulty.BitLen() == 0
env := prestate.Env
if isMerged {
Expand Down
30 changes: 18 additions & 12 deletions tests/block_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type btHeader struct {
Timestamp uint64
BaseFeePerGas *big.Int
WithdrawalsRoot *common.Hash
ExcessDataGas *big.Int
}

type btHeaderMarshaling struct {
Expand All @@ -99,6 +100,7 @@ type btHeaderMarshaling struct {
GasUsed math.HexOrDecimal64
Timestamp math.HexOrDecimal64
BaseFeePerGas *math.HexOrDecimal256
ExcessDataGas *math.HexOrDecimal256
}

func (t *BlockTest) Run(snapshotter bool) error {
Expand Down Expand Up @@ -163,18 +165,19 @@ func (t *BlockTest) Run(snapshotter bool) error {

func (t *BlockTest) genesis(config *params.ChainConfig) *core.Genesis {
return &core.Genesis{
Config: config,
Nonce: t.json.Genesis.Nonce.Uint64(),
Timestamp: t.json.Genesis.Timestamp,
ParentHash: t.json.Genesis.ParentHash,
ExtraData: t.json.Genesis.ExtraData,
GasLimit: t.json.Genesis.GasLimit,
GasUsed: t.json.Genesis.GasUsed,
Difficulty: t.json.Genesis.Difficulty,
Mixhash: t.json.Genesis.MixHash,
Coinbase: t.json.Genesis.Coinbase,
Alloc: t.json.Pre,
BaseFee: t.json.Genesis.BaseFeePerGas,
Config: config,
Nonce: t.json.Genesis.Nonce.Uint64(),
Timestamp: t.json.Genesis.Timestamp,
ParentHash: t.json.Genesis.ParentHash,
ExtraData: t.json.Genesis.ExtraData,
GasLimit: t.json.Genesis.GasLimit,
GasUsed: t.json.Genesis.GasUsed,
Difficulty: t.json.Genesis.Difficulty,
Mixhash: t.json.Genesis.MixHash,
Coinbase: t.json.Genesis.Coinbase,
Alloc: t.json.Pre,
BaseFee: t.json.Genesis.BaseFeePerGas,
ExcessDataGas: t.json.Genesis.ExcessDataGas,
}
}

Expand Down Expand Up @@ -283,6 +286,9 @@ func validateHeader(h *btHeader, h2 *types.Header) error {
if !reflect.DeepEqual(h.WithdrawalsRoot, h2.WithdrawalsHash) {
return fmt.Errorf("withdrawalsRoot: want: %v have: %v", h.WithdrawalsRoot, h2.WithdrawalsHash)
}
if !reflect.DeepEqual(h.ExcessDataGas, h2.ExcessDataGas) {
return fmt.Errorf("excessDataGas: want: %v have: %v", h.ExcessDataGas, h2.ExcessDataGas)
}
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions tests/gen_btheader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4607abe

Please sign in to comment.