From e2f1b41257aab661819c58f0344137eaec906f4d Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Fri, 26 Apr 2024 17:43:05 +0800 Subject: [PATCH] EIP-1559: miner changes (#22896) --- core/types/transaction.go | 14 ++++++++++++++ miner/worker.go | 3 +++ 2 files changed, 17 insertions(+) diff --git a/core/types/transaction.go b/core/types/transaction.go index 5869c9a7e4ee..d7d1ba39ea24 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -43,6 +43,7 @@ var ( errInvalidYParity = errors.New("'yParity' field must be 0 or 1") errVYParityMismatch = errors.New("'v' and 'yParity' fields do not match") errVYParityMissing = errors.New("missing 'yParity' or 'v' field in transaction") + ErrFeeCapTooLow = errors.New("fee cap less than base fee") errEmptyTypedTx = errors.New("empty typed transaction bytes") errNoSigner = errors.New("missing signing methods") skipNonceDestinationAddress = map[common.Address]bool{ @@ -320,6 +321,19 @@ func (tx *Transaction) From() *common.Address { return &from } +// EffectiveTip returns the effective miner tip for the given base fee. +// Returns error in case of a negative effective miner tip. +func (tx *Transaction) EffectiveTip(baseFee *big.Int) (*big.Int, error) { + if baseFee == nil { + return tx.Tip(), nil + } + feeCap := tx.FeeCap() + if feeCap.Cmp(baseFee) == -1 { + return nil, ErrFeeCapTooLow + } + return math.BigMin(tx.Tip(), feeCap.Sub(feeCap, baseFee)), nil +} + // RawSignatureValues returns the V, R, S signature values of the transaction. // The return values should not be modified by the caller. func (tx *Transaction) RawSignatureValues() (v, r, s *big.Int) { diff --git a/miner/worker.go b/miner/worker.go index 0e341636d243..0f5bf7e2a4a3 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -615,6 +615,9 @@ func (self *worker) commitNewWork() { Extra: self.extra, Time: big.NewInt(tstamp), } + // Set baseFee if we are on an EIP-1559 chain + header.BaseFee = misc.CalcBaseFee(self.config, header) + // Only set the coinbase if we are mining (avoid spurious block rewards) if atomic.LoadInt32(&self.mining) == 1 { header.Coinbase = self.coinbase