From 8fab6a25f4c96d988a1cdff7dfdea8c7d419ce1a Mon Sep 17 00:00:00 2001 From: Ian Norden Date: Mon, 13 Jan 2020 14:15:07 -0600 Subject: [PATCH] fix VerifyEIP1559BaseFee so that ethash engine enforces exact BaseFee value consensus --- consensus/misc/forks.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/consensus/misc/forks.go b/consensus/misc/forks.go index ca2e59ffb..f1c4e51bd 100644 --- a/consensus/misc/forks.go +++ b/consensus/misc/forks.go @@ -64,15 +64,21 @@ func VerifyEIP1559BaseFee(config *params.ChainConfig, header, parent *types.Head } // Verify the BaseFee is valid if we are past the EIP1559 activation block if config.IsEIP1559(header.Number) { - // A valid BASEFEE is one such that abs(BASEFEE - PARENT_BASEFEE) <= max(1, PARENT_BASEFEE // BASEFEE_MAX_CHANGE_DENOMINATOR) if parent.BaseFee == nil { return errMissingParentBaseFee } if header.BaseFee == nil { return errMissingBaseFee } - diff := new(big.Int).Sub(header.BaseFee, parent.BaseFee) + delta := new(big.Int).Sub(new(big.Int).SetUint64(parent.GasUsed), new(big.Int).SetUint64(params.TargetGasUsed)) + mul := new(big.Int).Mul(parent.BaseFee, delta) + div := new(big.Int).Div(mul, new(big.Int).SetUint64(params.TargetGasUsed)) + div2 := new(big.Int).Div(div, new(big.Int).SetUint64(params.BaseFeeMaxChangeDenominator)) + expectedBaseFee := new(big.Int).Add(parent.BaseFee, div2) + diff := new(big.Int).Sub(expectedBaseFee, parent.BaseFee) + neg := false if diff.Sign() < 0 { + neg = true diff.Neg(diff) } max := new(big.Int).Div(parent.BaseFee, new(big.Int).SetUint64(params.BaseFeeMaxChangeDenominator)) @@ -80,6 +86,12 @@ func VerifyEIP1559BaseFee(config *params.ChainConfig, header, parent *types.Head max = common.Big1 } if diff.Cmp(max) > 0 { + if neg { + max.Neg(max) + } + expectedBaseFee.Set(new(big.Int).Add(parent.BaseFee, max)) + } + if expectedBaseFee.Cmp(header.BaseFee) > 0 { return errInvalidBaseFee } return nil