Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch up master merge (takes 94dca86) + refactor customheaders #844

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 10 additions & 23 deletions consensus/dummy/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"math/big"
"time"

"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/coreth/consensus"
"github.com/ava-labs/coreth/consensus/misc/eip4844"
Expand All @@ -22,8 +21,6 @@ import (
"github.com/ava-labs/libevm/trie"

customheader "github.com/ava-labs/coreth/plugin/evm/header"
"github.com/ava-labs/coreth/plugin/evm/upgrade/ap1"
"github.com/ava-labs/coreth/plugin/evm/upgrade/cortina"
)

var (
Expand Down Expand Up @@ -115,31 +112,13 @@ func NewFullFaker() *DummyEngine {

func (eng *DummyEngine) verifyHeaderGasFields(config *params.ChainConfig, header *types.Header, parent *types.Header) error {
configExtra := params.GetExtra(config)
// Verify that the gas limit is <= 2^63-1
if header.GasLimit > params.MaxGasLimit {
return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, params.MaxGasLimit)
}
// Verify that the gasUsed is <= gasLimit
if header.GasUsed > header.GasLimit {
return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit)
}
if configExtra.IsCortina(header.Time) {
if header.GasLimit != cortina.GasLimit {
return fmt.Errorf("expected gas limit to be %d in Cortina, but found %d", cortina.GasLimit, header.GasLimit)
}
} else if configExtra.IsApricotPhase1(header.Time) {
if header.GasLimit != ap1.GasLimit {
return fmt.Errorf("expected gas limit to be %d in ApricotPhase1, but found %d", ap1.GasLimit, header.GasLimit)
}
} else {
// Verify that the gas limit remains within allowed bounds
diff := math.AbsDiff(parent.GasLimit, header.GasLimit)
limit := parent.GasLimit / params.GasLimitBoundDivisor
if diff >= limit || header.GasLimit < params.MinGasLimit {
return fmt.Errorf("invalid gas limit: have %d, want %d += %d", header.GasLimit, parent.GasLimit, limit)
}
if err := customheader.VerifyGasLimit(configExtra, parent, header); err != nil {
return err
}

// Verify header.Extra matches the expected value.
expectedExtraPrefix, err := customheader.ExtraPrefix(configExtra, parent, header.Time)
if err != nil {
Expand Down Expand Up @@ -446,6 +425,14 @@ func (eng *DummyEngine) FinalizeAndAssemble(chain consensus.ChainHeaderReader, h
return nil, err
}
}

// finalize the header.Extra
extraPrefix, err := customheader.ExtraPrefix(configExtra, parent, header.Time)
if err != nil {
return nil, fmt.Errorf("failed to calculate new header.Extra: %w", err)
}
header.Extra = append(extraPrefix, header.Extra...)

// commit the final state root
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))

Expand Down
9 changes: 5 additions & 4 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/plugin/evm/upgrade/ap0"
"github.com/ava-labs/libevm/trie"
)

Expand Down Expand Up @@ -147,10 +148,10 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
// the gas allowance.
func CalcGasLimit(parentGasUsed, parentGasLimit, gasFloor, gasCeil uint64) uint64 {
// contrib = (parentGasUsed * 3 / 2) / 1024
contrib := (parentGasUsed + parentGasUsed/2) / params.GasLimitBoundDivisor
contrib := (parentGasUsed + parentGasUsed/2) / ap0.GasLimitBoundDivisor

// decay = parentGasLimit / 1024 -1
decay := parentGasLimit/params.GasLimitBoundDivisor - 1
decay := parentGasLimit/ap0.GasLimitBoundDivisor - 1

/*
strategy: gasLimit of block-to-mine is set based on parent's
Expand All @@ -160,8 +161,8 @@ func CalcGasLimit(parentGasUsed, parentGasLimit, gasFloor, gasCeil uint64) uint6
from parentGasLimit * (2/3) parentGasUsed is.
*/
limit := parentGasLimit - decay + contrib
if limit < params.MinGasLimit {
limit = params.MinGasLimit
if limit < ap0.MinGasLimit {
limit = ap0.MinGasLimit
}
// If we're outside our allowed gas range, we try to hone towards them
if limit < gasFloor {
Expand Down
17 changes: 1 addition & 16 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ import (
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/plugin/evm/header"
"github.com/ava-labs/coreth/plugin/evm/upgrade/ap1"
"github.com/ava-labs/coreth/plugin/evm/upgrade/cortina"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/ethdb"
Expand Down Expand Up @@ -375,20 +373,8 @@ func GenerateChainWithGenesis(genesis *Genesis, engine consensus.Engine, n int,
func (cm *chainMaker) makeHeader(parent *types.Block, gap uint64, state *state.StateDB, engine consensus.Engine) *types.Header {
time := parent.Time() + gap // block time is fixed at [gap] seconds

var gasLimit uint64
configExtra := params.GetExtra(cm.config)
if configExtra.IsCortina(time) {
gasLimit = cortina.GasLimit
} else if configExtra.IsApricotPhase1(time) {
gasLimit = ap1.GasLimit
} else {
gasLimit = CalcGasLimit(parent.GasUsed(), parent.GasLimit(), parent.GasLimit(), parent.GasLimit())
}

extra, err := header.ExtraPrefix(configExtra, parent.Header(), time)
if err != nil {
panic(err)
}
gasLimit := header.GasLimit(configExtra, parent.Header(), time)
baseFee, err := header.BaseFee(configExtra, parent.Header(), time)
if err != nil {
panic(err)
Expand All @@ -402,7 +388,6 @@ func (cm *chainMaker) makeHeader(parent *types.Block, gap uint64, state *state.S
GasLimit: gasLimit,
Number: new(big.Int).Add(parent.Number(), common.Big1),
Time: time,
Extra: extra,
BaseFee: baseFee,
}

Expand Down
50 changes: 16 additions & 34 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ import (
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/predicate"
customheader "github.com/ava-labs/coreth/plugin/evm/header"
"github.com/ava-labs/libevm/common"
ethtypes "github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/log"
"github.com/holiman/uint256"
)

Expand Down Expand Up @@ -104,37 +103,6 @@ type ChainContext interface {

// NewEVMBlockContext creates a new context for use in the EVM.
func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common.Address) vm.BlockContext {
predicateBytes := predicate.GetPredicateResultBytes(header.Extra)
if len(predicateBytes) == 0 {
return newEVMBlockContext(header, chain, author, nil)
}
// Prior to Durango, the VM enforces the extra data is smaller than or
// equal to this size. After Durango, the VM pre-verifies the extra
// data past the dynamic fee rollup window is valid.
_, err := predicate.ParseResults(predicateBytes)
if err != nil {
log.Error("failed to parse predicate results creating new block context", "err", err, "extra", header.Extra)
// As mentioned above, we pre-verify the extra data to ensure this never happens.
// If we hit an error, construct a new block context rather than use a potentially half initialized value
// as defense in depth.
return newEVMBlockContext(header, chain, author, nil)
}
return newEVMBlockContext(header, chain, author, header.Extra)
}

// NewEVMBlockContextWithPredicateResults creates a new context for use in the EVM with an override for the predicate results that is not present
// in header.Extra.
// This function is used to create a BlockContext when the header Extra data is not fully formed yet and it's more efficient to pass in predicateResults
// directly rather than re-encode the latest results when executing each individaul transaction.
func NewEVMBlockContextWithPredicateResults(header *types.Header, chain ChainContext, author *common.Address, predicateBytes []byte) vm.BlockContext {
extra := bytes.Clone(header.Extra)
if len(predicateBytes) > 0 {
extra = predicate.SetPredicateResultBytes(extra, predicateBytes)
}
return newEVMBlockContext(header, chain, author, extra)
}

func newEVMBlockContext(header *types.Header, chain ChainContext, author *common.Address, extra []byte) vm.BlockContext {
var (
beneficiary common.Address
baseFee *big.Int
Expand Down Expand Up @@ -168,11 +136,25 @@ func newEVMBlockContext(header *types.Header, chain ChainContext, author *common
Header: &ethtypes.Header{
Number: new(big.Int).Set(header.Number),
Time: header.Time,
Extra: extra,
Extra: header.Extra,
},
}
}

// NewEVMBlockContextWithPredicateResults creates a new context for use in the
// EVM with an override for the predicate results. The miner uses this to pass
// predicate results to the EVM when header.Extra is not fully formed yet.
func NewEVMBlockContextWithPredicateResults(header *types.Header, chain ChainContext, author *common.Address, predicateResults []byte) vm.BlockContext {
blockCtx := NewEVMBlockContext(header, chain, author)
// Note this only sets the block context, which is the hand-off point for
// the EVM. The actual header is not modified.
blockCtx.Header.Extra = customheader.SetPredicateBytesInExtra(
bytes.Clone(header.Extra),
predicateResults,
)
return blockCtx
}

// NewEVMTxContext creates a new transaction context for a single transaction.
func NewEVMTxContext(msg *Message) vm.TxContext {
ctx := vm.TxContext{
Expand Down
9 changes: 4 additions & 5 deletions core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/params/extras"
"github.com/ava-labs/coreth/plugin/evm/header"
customheader "github.com/ava-labs/coreth/plugin/evm/header"
"github.com/ava-labs/coreth/plugin/evm/upgrade/ap1"
"github.com/ava-labs/coreth/plugin/evm/upgrade/ap3"
"github.com/ava-labs/coreth/plugin/evm/upgrade/cortina"
Expand Down Expand Up @@ -361,11 +361,10 @@ func TestStateProcessorErrors(t *testing.T) {
// valid to be considered for import:
// - valid pow (fake), ancestry, difficulty, gaslimit etc
func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Transactions, config *params.ChainConfig) *types.Block {
configExtra := params.GetExtra(config)
fakeChainReader := newChainMaker(nil, config, engine)
time := parent.Time() + 10
configExtra := params.GetExtra(config)
extra, _ := header.ExtraPrefix(configExtra, parent.Header(), time)
baseFee, _ := header.BaseFee(configExtra, parent.Header(), time)
baseFee, _ := customheader.BaseFee(configExtra, parent.Header(), time)
header := &types.Header{
ParentHash: parent.Hash(),
Coinbase: parent.Coinbase(),
Expand All @@ -378,7 +377,6 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
GasLimit: parent.GasLimit(),
Number: new(big.Int).Add(parent.Number(), common.Big1),
Time: time,
Extra: extra,
UncleHash: types.EmptyUncleHash,
BaseFee: baseFee,
}
Expand All @@ -403,6 +401,7 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
cumulativeGas += tx.Gas()
nBlobs += len(tx.BlobHashes())
}
header.Extra, _ = customheader.ExtraPrefix(configExtra, parent.Header(), time)
header.Root = common.BytesToHash(hasher.Sum(nil))
if config.IsCancun(header.Number, header.Time) {
var pExcess, pUsed = uint64(0), uint64(0)
Expand Down
21 changes: 1 addition & 20 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ import (
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/plugin/evm/header"
"github.com/ava-labs/coreth/plugin/evm/upgrade/ap1"
"github.com/ava-labs/coreth/plugin/evm/upgrade/cortina"
"github.com/ava-labs/coreth/precompile/precompileconfig"
"github.com/ava-labs/coreth/predicate"
"github.com/ava-labs/libevm/common"
Expand Down Expand Up @@ -148,24 +146,8 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte
timestamp = parent.Time
}

var gasLimit uint64
chainExtra := params.GetExtra(w.chainConfig)
if chainExtra.IsCortina(timestamp) {
gasLimit = cortina.GasLimit
} else if chainExtra.IsApricotPhase1(timestamp) {
gasLimit = ap1.GasLimit
} else {
// The gas limit is set in phase1 to [ap1.GasLimit] because the ceiling
// and floor were set to the same value such that the gas limit
// converged to it. Since this is hardcoded now, we remove the ability
// to configure it.
gasLimit = core.CalcGasLimit(parent.GasUsed, parent.GasLimit, ap1.GasLimit, ap1.GasLimit)
}

extra, err := header.ExtraPrefix(chainExtra, parent, timestamp)
if err != nil {
return nil, fmt.Errorf("failed to calculate new extra prefix: %w", err)
}
gasLimit := header.GasLimit(chainExtra, parent, timestamp)
baseFee, err := header.BaseFee(chainExtra, parent, timestamp)
if err != nil {
return nil, fmt.Errorf("failed to calculate new base fee: %w", err)
Expand All @@ -176,7 +158,6 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte
Number: new(big.Int).Add(parent.Number, common.Big1),
GasLimit: gasLimit,
Time: timestamp,
Extra: extra,
BaseFee: baseFee,
}

Expand Down
Loading
Loading