all: implement EIP-1559 (#22837)

This is the initial implementation of EIP-1559 in packages core/types and core.
Mining, RPC, etc. will be added in subsequent commits.

Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de>
Co-authored-by: lightclient@protonmail.com <lightclient@protonmail.com>
Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Martin Holst Swende
2021-05-17 15:13:22 +02:00
committed by GitHub
parent 14bc6e5130
commit 94451c2788
59 changed files with 1522 additions and 173 deletions

View File

@ -22,6 +22,7 @@ import (
"math/big"
"github.com/ethereum/go-ethereum/common"
cmath "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
@ -49,6 +50,8 @@ type StateTransition struct {
msg Message
gas uint64
gasPrice *big.Int
feeCap *big.Int
tip *big.Int
initialGas uint64
value *big.Int
data []byte
@ -62,6 +65,8 @@ type Message interface {
To() *common.Address
GasPrice() *big.Int
FeeCap() *big.Int
Tip() *big.Int
Gas() uint64
Value() *big.Int
@ -154,6 +159,8 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition
evm: evm,
msg: msg,
gasPrice: msg.GasPrice(),
feeCap: msg.FeeCap(),
tip: msg.Tip(),
value: msg.Value(),
data: msg.Data(),
state: evm.StateDB,
@ -206,6 +213,15 @@ func (st *StateTransition) preCheck() error {
st.msg.From().Hex(), msgNonce, stNonce)
}
}
// Make sure that transaction feeCap is greater than the baseFee (post london)
if st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) {
// This will panic if baseFee is nil, but basefee presence is verified
// as part of header validation.
if st.feeCap.Cmp(st.evm.Context.BaseFee) < 0 {
return fmt.Errorf("%w: address %v, feeCap: %s baseFee: %s", ErrFeeCapTooLow,
st.msg.From().Hex(), st.feeCap, st.evm.Context.BaseFee)
}
}
return st.buyGas()
}
@ -281,7 +297,11 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
// After EIP-3529: refunds are capped to gasUsed / 5
st.refundGas(params.RefundQuotientEIP3529)
}
st.state.AddBalance(st.evm.Context.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice))
effectiveTip := st.gasPrice
if st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) {
effectiveTip = cmath.BigMin(st.tip, new(big.Int).Sub(st.feeCap, st.evm.Context.BaseFee))
}
st.state.AddBalance(st.evm.Context.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), effectiveTip))
return &ExecutionResult{
UsedGas: st.gasUsed(),