Files
evm/core/headerchain_test.go
zeekay 7341b4a885 fix(evm): bind chain identity before NewBlockChain reprocess (no new type)
The 0x9999 DEX value path is identity-gated: the asset resolver refuses to
admit a market unless the running chain's identity matches. core.NewBlockChain
re-executes accepted blocks during construction (loadLastState recovery /
populateMissingTries backfill), but the identity was only bound onto the chain
AFTER eth.New returned (vm.go SetConsensusContext) — so an unclean restart after
a 0x9999 swap re-executed that block with networkID 0 / C-Chain Empty, reverted
the committed swap, failed ValidateState, and the node could not boot.

Fix is purely timing: hand NewBlockChain the *runtime.Runtime the VM already
holds (eth.Settings.ChainRuntime -> core.NewBlockChain) and bind the consensus
context BEFORE reprocess. No new identity type — networkID/chainID already live
on runtime.Runtime; the context is just the last-mile transport to geth's
ConsensusContext API. stdlib runtime aliased stdruntime in blockchain.go.
2026-06-23 08:53:26 -07:00

127 lines
4.1 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
//
// This file is a derived work, based on the go-ethereum library whose original
// notices appear below.
//
// It is distributed under a license compatible with the licensing terms of the
// original code from which it is derived.
//
// Much love to the original authors for their work.
// **********
// Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"errors"
"fmt"
"math/big"
"testing"
"github.com/luxfi/evm/consensus"
"github.com/luxfi/evm/consensus/dummy"
"github.com/luxfi/evm/params"
"github.com/luxfi/evm/plugin/evm/upgrade/legacy"
"github.com/luxfi/geth/common"
"github.com/luxfi/geth/core/rawdb"
"github.com/luxfi/geth/core/types"
"github.com/luxfi/geth/core/vm"
)
func verifyUnbrokenCanonchain(bc *BlockChain) error {
h := bc.hc.CurrentHeader()
for {
canonHash := rawdb.ReadCanonicalHash(bc.hc.chainDb, h.Number.Uint64())
if exp := h.Hash(); canonHash != exp {
return fmt.Errorf("Canon hash chain broken, block %d got %x, expected %x",
h.Number, canonHash[:8], exp[:8])
}
if h.Number.Uint64() == 0 {
break
}
h = bc.hc.GetHeader(h.ParentHash, h.Number.Uint64()-1)
}
return nil
}
func testInsert(t *testing.T, bc *BlockChain, chain []*types.Block, wantErr error) {
t.Helper()
_, err := bc.InsertChain(chain)
// Always verify that the header chain is unbroken
if err := verifyUnbrokenCanonchain(bc); err != nil {
t.Fatal(err)
}
if !errors.Is(err, wantErr) {
t.Fatalf("unexpected error from InsertHeaderChain: %v", err)
}
}
// This test checks status reporting of InsertHeaderChain.
func TestHeaderInsertion(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
gspec = &Genesis{
BaseFee: big.NewInt(legacy.BaseFee),
Config: params.TestChainConfig,
}
)
genesis := gspec.ToBlock()
chain, err := NewBlockChain(db, DefaultCacheConfig, gspec, dummy.NewCoinbaseFaker(), vm.Config{}, common.Hash{}, false, nil)
if err != nil {
t.Fatal(err)
}
defer chain.Stop()
// chain A: G->A1->A2...A128
chainA, _, _ := GenerateChain(params.TestChainConfig, types.NewBlockWithHeader(genesis.Header()), dummy.NewCoinbaseFaker(), db, 128, 10, func(i int, b *BlockGen) {
b.SetCoinbase(common.Address{0: byte(10), 19: byte(i)})
})
// chain B: G->A1->B2...B128
chainB, _, _ := GenerateChain(params.TestChainConfig, types.NewBlockWithHeader(chainA[0].Header()), dummy.NewCoinbaseFaker(), db, 128, 10, func(i int, b *BlockGen) {
b.SetCoinbase(common.Address{0: byte(10), 19: byte(i)})
})
// Inserting 64 headers on an empty chain
testInsert(t, chain, chainA[:64], nil)
// Inserting 64 identical headers
testInsert(t, chain, chainA[:64], nil)
// Inserting the same some old, some new headers
testInsert(t, chain, chainA[32:96], nil)
// Inserting side blocks, but not overtaking the canon chain
testInsert(t, chain, chainB[0:32], nil)
// Inserting more side blocks, but we don't have the parent
testInsert(t, chain, chainB[34:36], consensus.ErrUnknownAncestor)
// Inserting more sideblocks, overtaking the canon chain
testInsert(t, chain, chainB[32:97], nil)
// Inserting more A-headers, taking back the canonicality
testInsert(t, chain, chainA[90:100], nil)
// And B becomes canon again
testInsert(t, chain, chainB[97:107], nil)
// And B becomes even longer
testInsert(t, chain, chainB[107:128], nil)
}