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

[WIP] chore(all): merge master into libevm #828

Draft
wants to merge 20 commits into
base: libevm
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2dd2fb5
Move `header.ExtraPrefix` calculation (#825)
StephenButtolph Feb 21, 2025
3d2037f
Move `GasLimit` handling into the `header` package (#822)
StephenButtolph Feb 24, 2025
2663434
Update ACP-176 math (#827)
StephenButtolph Feb 24, 2025
94dca86
Move predicate header parsing (#824)
StephenButtolph Feb 25, 2025
c273f33
chore(scripts): remove lint.sh (#834)
ceyonur Feb 25, 2025
6c6f02b
chore: bump ci versions to 1.23.6 (#835)
ceyonur Feb 25, 2025
0c24416
chore(ci): use go.mod Go version for actions/setup-go (#836)
ceyonur Feb 25, 2025
d96ce7e
Cleanup block fee tests (#837)
StephenButtolph Feb 26, 2025
8cc9ade
Cleanup `atomic.CalculateDynamicFee` (#838)
StephenButtolph Feb 26, 2025
7f12b46
Cleanup `SyntacticVerify` (#839)
StephenButtolph Feb 26, 2025
8068dec
Round up in `header.EstimateRequiredTip` (#840)
StephenButtolph Feb 26, 2025
05221e1
Export ACP-176 constants (#842)
StephenButtolph Feb 26, 2025
ebc14ef
Filter by total gas rather than GasUsed (#843)
StephenButtolph Feb 27, 2025
14969d9
refactor(plugin/evm/header): do not depend on params package
qdm12 Feb 28, 2025
31312e9
Merge commit '2dd2fb5ae4dc958cc60f4ec035fb6c7056fc0ab5' into qdm12/li…
qdm12 Feb 28, 2025
0097caa
Merge commit '3d2037f1ba843325a0722e247ade7a3cffa5ac26' into qdm12/li…
qdm12 Feb 28, 2025
210fbee
Merge branch 'master' into qdm12/libevm-master-merge
qdm12 Feb 28, 2025
7effabd
Add Rules to PredicateBytesFromExtra (#845)
StephenButtolph Feb 28, 2025
7a1f5df
Merge branch 'master' into qdm12/libevm-master-merge
qdm12 Feb 28, 2025
12c6dcf
Merge branch 'libevm' into qdm12/libevm-master-merge
qdm12 Feb 28, 2025
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
token: ${{ secrets.AVALANCHE_PAT }}
- uses: actions/setup-go@v5
with:
go-version: "~1.22.8"
go-version-file: "go.mod"
- name: change avalanchego dep
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
Expand Down Expand Up @@ -72,7 +72,7 @@ jobs:
token: ${{ secrets.AVALANCHE_PAT }}
- uses: actions/setup-go@v5
with:
go-version: "~1.22.8"
go-version-file: "go.mod"
- name: change avalanchego dep
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
Expand Down Expand Up @@ -121,7 +121,7 @@ jobs:
token: ${{ secrets.AVALANCHE_PAT }}
- uses: actions/setup-go@v5
with:
go-version: "~1.22.8"
go-version-file: "go.mod"
- name: Run e2e tests
run: E2E_SERIAL=1 ./scripts/tests.e2e.sh
shell: bash
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/sync-subnet-evm-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: "~1.22.8"
go-version-file: "go.mod"
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ============= Compilation Stage ================
FROM golang:1.22.8-bullseye AS builder
FROM golang:1.23.6-bullseye AS builder

ARG AVALANCHE_VERSION

Expand All @@ -17,7 +17,7 @@ WORKDIR $GOPATH/src/github.com/ava-labs/avalanchego
RUN go mod download
# Replace the coreth dependency
RUN go mod edit -replace github.com/ava-labs/coreth=../coreth
RUN go mod download && go mod tidy -compat=1.22
RUN go mod download && go mod tidy -compat=1.23

# Build the AvalancheGo binary with local version of coreth.
RUN ./scripts/build_avalanche.sh
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2179,7 +2179,7 @@ func golangBindings(t *testing.T, overload bool) {
if out, err := replacer.CombinedOutput(); err != nil {
t.Fatalf("failed to replace binding test dependency to current source tree: %v\n%s", err, out)
}
tidier := exec.Command(gocmd, "mod", "tidy", "-compat=1.22")
tidier := exec.Command(gocmd, "mod", "tidy", "-compat=1.23")
tidier.Dir = pkg
if out, err := tidier.CombinedOutput(); err != nil {
t.Fatalf("failed to tidy Go module file: %v\n%s", err, out)
Expand Down
50 changes: 20 additions & 30 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,13 +21,13 @@ 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 (
allowedFutureBlockTime = 10 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks

ErrInsufficientBlockGas = errors.New("insufficient gas to cover the block cost")

errInvalidBlockTime = errors.New("timestamp less than parent's")
errUnclesUnsupported = errors.New("uncles unsupported")
errExtDataGasUsedNil = errors.New("extDataGasUsed is nil")
Expand Down Expand Up @@ -115,31 +114,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 @@ -340,15 +321,16 @@ func (eng *DummyEngine) verifyBlockFee(
// set by the base fee of this block.
blockGas := new(big.Int).Div(totalBlockFee, baseFee)

// Require that the amount of gas purchased by the effective tips within the block, [blockGas],
// covers at least [requiredBlockGasCost].
// Require that the amount of gas purchased by the effective tips within the
// block covers at least `requiredBlockGasCost`.
//
// NOTE: To determine the [requiredBlockFee], multiply [requiredBlockGasCost]
// by [baseFee].
// NOTE: To determine the required block fee, multiply
// `requiredBlockGasCost` by `baseFee`.
if blockGas.Cmp(requiredBlockGasCost) < 0 {
return fmt.Errorf(
"insufficient gas (%d) to cover the block cost (%d) at base fee (%d) (total block fee: %d)",
blockGas, requiredBlockGasCost, baseFee, totalBlockFee,
return fmt.Errorf("%w: expected %d but got %d",
ErrInsufficientBlockGas,
requiredBlockGasCost,
blockGas,
)
}
return nil
Expand Down Expand Up @@ -448,6 +430,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
7 changes: 3 additions & 4 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 @@ -364,8 +364,7 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
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 @@ -404,6 +402,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
Loading
Loading