Skip to content

Commit

Permalink
set BaseFee to nil (ethereum#222)
Browse files Browse the repository at this point in the history
* set BaseFee to nil

* fix lint

* comment fix

* update genesis.go
  • Loading branch information
NazariiDenha committed Feb 21, 2023
1 parent ec9254b commit 310c870
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 25 deletions.
6 changes: 5 additions & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,11 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
// Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
call.GasPrice = new(big.Int)
if call.GasFeeCap.BitLen() > 0 || call.GasTipCap.BitLen() > 0 {
call.GasPrice = math.BigMin(new(big.Int).Add(call.GasTipCap, head.BaseFee), call.GasFeeCap)
if head.BaseFee != nil {
call.GasPrice = math.BigMin(new(big.Int).Add(call.GasTipCap, head.BaseFee), call.GasFeeCap)
} else {
call.GasPrice = math.BigMin(call.GasTipCap, call.GasFeeCap)
}
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions accounts/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,14 @@ func (c *BoundContract) createDynamicTx(opts *TransactOpts, contract *common.Add
// Estimate FeeCap
gasFeeCap := opts.GasFeeCap
if gasFeeCap == nil {
gasFeeCap = new(big.Int).Add(
gasTipCap,
new(big.Int).Mul(head.BaseFee, big.NewInt(2)),
)
if head.BaseFee != nil {
gasFeeCap = new(big.Int).Add(
gasTipCap,
new(big.Int).Mul(head.BaseFee, big.NewInt(2)),
)
} else {
gasFeeCap = new(big.Int).Set(gasTipCap)
}
}
if gasFeeCap.Cmp(gasTipCap) < 0 {
return nil, fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", gasFeeCap, gasTipCap)
Expand Down
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func Transition(ctx *cli.Context) error {
}
// Sanity check, to not `panic` in state_transition
if chainConfig.IsLondon(big.NewInt(int64(prestate.Env.Number))) {
if prestate.Env.BaseFee == nil {
if prestate.Env.BaseFee == nil && chainConfig.EnableEIP2718 && chainConfig.EnableEIP1559 {
return NewError(ErrorConfig, errors.New("EIP-1559 config but missing 'currentBaseFee' in env section"))
}
}
Expand Down
9 changes: 8 additions & 1 deletion consensus/misc/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ func VerifyEip1559Header(config *params.ChainConfig, parent, header *types.Heade
return err
}
// Verify the header is not malformed
if header.BaseFee == nil {
if header.BaseFee == nil && (config.EnableEIP2718 && config.EnableEIP1559) {
return fmt.Errorf("header is missing baseFee")
}
// Now BaseFee can be nil, because !(config.EnableEIP2718 && config.EnableEIP1559)
if header.BaseFee == nil {
return nil
}
// Verify the baseFee is correct based on the parent header.

var expectedBaseFee *big.Int
Expand Down Expand Up @@ -72,6 +76,9 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
parentGasTargetBig = new(big.Int).SetUint64(parentGasTarget)
baseFeeChangeDenominator = new(big.Int).SetUint64(params.BaseFeeChangeDenominator)
)
if !config.EnableEIP2718 || !config.EnableEIP1559 {
return nil
}
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
if parent.GasUsed == parentGasTarget {
return new(big.Int).Set(parent.BaseFee)
Expand Down
6 changes: 5 additions & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ func (b *BlockGen) Number() *big.Int {

// BaseFee returns the EIP-1559 base fee of the block being generated.
func (b *BlockGen) BaseFee() *big.Int {
return new(big.Int).Set(b.header.BaseFee)
if b.header.BaseFee != nil {
return new(big.Int).Set(b.header.BaseFee)
} else {
return big.NewInt(0)
}
}

// AddUncheckedReceipt forcefully adds a receipts to the block without a
Expand Down
4 changes: 3 additions & 1 deletion core/gen_genesis.go

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

4 changes: 3 additions & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,10 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
if g.Config != nil && g.Config.IsLondon(common.Big0) {
if g.BaseFee != nil {
head.BaseFee = g.BaseFee
} else {
} else if g.Config.EnableEIP2718 && g.Config.EnableEIP1559 {
head.BaseFee = new(big.Int).SetUint64(params.InitialBaseFee)
} else {
head.BaseFee = nil
}
}
statedb.Commit(false)
Expand Down
12 changes: 6 additions & 6 deletions core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ func TestStateProcessorErrors(t *testing.T) {
},
want: "could not apply tx 0 [0xbd49d8dadfd47fb846986695f7d4da3f7b2c48c8da82dbc211a26eb124883de9]: gas limit reached",
},
{ // ErrFeeCapTooLow
txs: []*types.Transaction{
mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(0), big.NewInt(0)),
},
want: "could not apply tx 0 [0xc4ab868fef0c82ae0387b742aee87907f2d0fc528fc6ea0a021459fb0fc4a4a8]: max fee per gas less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxFeePerGas: 0 baseFee: 875000000",
},
// { // ErrFeeCapTooLow
// txs: []*types.Transaction{
// mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(0), big.NewInt(0)),
// },
// want: "could not apply tx 0 [0xc4ab868fef0c82ae0387b742aee87907f2d0fc528fc6ea0a021459fb0fc4a4a8]: max fee per gas less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxFeePerGas: 0 baseFee: 875000000",
// },
{ // ErrTipVeryHigh
txs: []*types.Transaction{
mkDynamicTx(0, common.Address{}, params.TxGas, tooBigNumber, big.NewInt(1)),
Expand Down
12 changes: 10 additions & 2 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ func (st *StateTransition) preCheck() error {
}
// This will panic if baseFee is nil, but basefee presence is verified
// as part of header validation.
if st.gasFeeCap.Cmp(st.evm.Context.BaseFee) < 0 {
if st.evm.Context.BaseFee != nil && st.gasFeeCap.Cmp(st.evm.Context.BaseFee) < 0 {
return fmt.Errorf("%w: address %v, maxFeePerGas: %s baseFee: %s", ErrFeeCapTooLow,
st.msg.From().Hex(), st.gasFeeCap, st.evm.Context.BaseFee)
}
if st.evm.Context.BaseFee == nil && st.gasFeeCap.Cmp(big.NewInt(0)) < 0 {
return fmt.Errorf("%w: address %v, maxFeePerGas: %s baseFee: %s", ErrFeeCapTooLow,
st.msg.From().Hex(), st.gasFeeCap, st.evm.Context.BaseFee)
}
Expand Down Expand Up @@ -352,7 +356,11 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
}
effectiveTip := st.gasPrice
if london {
effectiveTip = cmath.BigMin(st.gasTipCap, new(big.Int).Sub(st.gasFeeCap, st.evm.Context.BaseFee))
if st.evm.Context.BaseFee != nil {
effectiveTip = cmath.BigMin(st.gasTipCap, new(big.Int).Sub(st.gasFeeCap, st.evm.Context.BaseFee))
} else {
effectiveTip = cmath.BigMin(st.gasTipCap, st.gasFeeCap)
}
}

if st.evm.ChainConfig().UsingScroll {
Expand Down
18 changes: 13 additions & 5 deletions internal/ethapi/transaction_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
args.MaxPriorityFeePerGas = (*hexutil.Big)(tip)
}
if args.MaxFeePerGas == nil {
gasFeeCap := new(big.Int).Add(
(*big.Int)(args.MaxPriorityFeePerGas),
new(big.Int).Mul(head.BaseFee, big.NewInt(2)),
)
var gasFeeCap *big.Int
if head.BaseFee != nil {
gasFeeCap = new(big.Int).Add(
(*big.Int)(args.MaxPriorityFeePerGas),
new(big.Int).Mul(head.BaseFee, big.NewInt(2)),
)
} else {
gasFeeCap = new(big.Int).Set(
(*big.Int)(args.MaxPriorityFeePerGas))
}
args.MaxFeePerGas = (*hexutil.Big)(gasFeeCap)
}
if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 {
Expand All @@ -115,7 +121,9 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
// The legacy tx gas price suggestion should not add 2x base fee
// because all fees are consumed, so it would result in a spiral
// upwards.
price.Add(price, head.BaseFee)
if head.BaseFee != nil {
price.Add(price, head.BaseFee)
}
}
args.GasPrice = (*hexutil.Big)(price)
}
Expand Down
4 changes: 2 additions & 2 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,9 +933,9 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64)
header.BaseFee = misc.CalcBaseFee(w.chainConfig, parent.Header())
} else {
// When disabling EIP-2718 or EIP-1559, we do not set baseFeePerGas in RPC response.
// Setting BaseFee as 0 here can help outside SDK calculates l2geth's RLP encoding,
// Setting BaseFee as nil here can help outside SDK calculates l2geth's RLP encoding,
// otherwise the l2geth's BaseFee is not known from the outside.
header.BaseFee = big.NewInt(0)
header.BaseFee = nil
}
if !w.chainConfig.IsLondon(parent.Number()) {
parentGasLimit := parent.GasLimit() * params.ElasticityMultiplier
Expand Down

0 comments on commit 310c870

Please sign in to comment.