erigon-pulse/consensus/misc/eip1559.go
ledgerwatch 12cde41772
Aleut support (Eip1559) (#1704)
* Where I am at

* Refactoring of transaction types

* More refactoring

* Use Homested signer in rpc daemon

* Unified signer

* Continue unified signer

* A bit more

* Fixes and down the rabbit hole...

* More tx pool fixes

* More refactoring fixes

* More fixes'

* more fixes

* More fixes

* More compile fixes

* More RLP hand-writing

* Finish RLP encoding/decoding of transactions

* Fixes to header encoding, start on protocol packets

* Transaction decoding

* Use DecodeTransaction function

* Decoding BlockBodyPacket

* Encode and decode for pool txs

* Start fixing tests

* Introduce SigningHash

* Fixes to SignHash

* RLP encoding fixes

* Fixes for encoding/decoding

* More test fixes

* Fix more tests

* More test fixes

* More test fixes

* Fix core tests

* More fixes for signer

* Fix for tx

* Fixes to string encoding/size

* Fix eip2930 test

* Fix rest of ./tests

* More fixes

* Fix compilation

* More test fixes

* More test fixes

* Test fixes

* More fixes

* Reuse EncodingSize in EncodeRLP for accessList

* Rearrange things in dynamic fee tx

* Add MarshalBinary

* More fixes

* Make V,R,S non-pointers

* More NPE fixes

* More fixes

* Receipt fixes

* Fix core/types

* Fix ./eth

* More compile fixes for tests

* More test fixes

* More test fixes

* Try to see lint errors better

* Try to see lint errors better

* Fix lint

* Debugging eip1559 test

* Fix TestEIP1559Transition test

* Fix NewBlockPacket encoding/decoding

* Fix calculation of TxHash

* Fix perf problem with senders

* Update aleut config values

* Try adding static peers

* Add staticpeers to defaul flags

* Change aleut networkID

* Fix test

Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
2021-04-22 18:11:37 +01:00

75 lines
2.4 KiB
Go

package misc
import (
"fmt"
"math/big"
"github.com/ledgerwatch/turbo-geth/common/math"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/params"
)
func VerifyEip1559Header(parent, header *types.Header, notFirst bool) error {
// Verify that the gasUsed is <= gasTarget*elasticityMultiplier
if header.GasUsed > header.GasLimit*params.ElasticityMultiplier {
return fmt.Errorf("exceeded elasticity multiplier: gasUsed %d, gasTarget*elasticityMultiplier %d", header.GasUsed, header.GasLimit*params.ElasticityMultiplier)
}
// Verify the baseFee is correct based on the parent header.
expectedBaseFee := new(big.Int).SetUint64(params.InitialBaseFee)
if notFirst {
// Only calculate the correct baseFee if the parent header is
// also a EIP-1559 header.
expectedBaseFee = CalcBaseFee(parent)
}
if header.BaseFee.Cmp(expectedBaseFee) != 0 {
var parentBaseFee big.Int
if parent.BaseFee != nil {
parentBaseFee.Set(parent.BaseFee)
}
return fmt.Errorf("invalid baseFee: expected: %d, have %d", &parentBaseFee, header.BaseFee.Int64())
}
return nil
}
func CalcBaseFee(parent *types.Header) *big.Int {
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
if parent.GasUsed == parent.GasLimit {
return new(big.Int).Set(parent.BaseFee)
}
var (
gasLimit = new(big.Int).SetUint64(parent.GasLimit)
baseFeeChangeDenominator = new(big.Int).SetUint64(params.BaseFeeChangeDenominator)
)
var parentBaseFee big.Int
if parent.BaseFee != nil {
parentBaseFee.Set(parent.BaseFee)
}
if parent.GasUsed > parent.GasLimit {
// If the parent block used more gas than its target, the baseFee should increase.
gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - parent.GasLimit)
x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
y := new(big.Int).Div(x, gasLimit)
baseFeeDelta := math.BigMax(
new(big.Int).Div(y, baseFeeChangeDenominator),
big.NewInt(1),
)
return new(big.Int).Add(parent.BaseFee, baseFeeDelta)
} else {
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
gasUsedDelta := new(big.Int).SetUint64(parent.GasLimit - parent.GasUsed)
x := new(big.Int).Mul(&parentBaseFee, gasUsedDelta)
y := new(big.Int).Div(x, gasLimit)
baseFeeDelta := new(big.Int).Div(y, baseFeeChangeDenominator)
return math.BigMax(
new(big.Int).Sub(&parentBaseFee, baseFeeDelta),
big.NewInt(0),
)
}
}