Files
evm/core/blockchain_log_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

108 lines
3.8 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package core
import (
"math/big"
"strings"
"testing"
"github.com/luxfi/crypto"
"github.com/luxfi/evm/accounts/abi"
"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/types"
"github.com/luxfi/geth/core/vm"
"github.com/stretchr/testify/require"
)
func TestAcceptedLogsSubscription(t *testing.T) {
/*
Example contract to test event emission:
pragma solidity >=0.7.0 <0.9.0;
contract Callable {
event Called();
function Call() public { emit Called(); }
}
*/
const (
callableABI = "[{\"anonymous\":false,\"inputs\":[],\"name\":\"Called\",\"type\":\"event\"},{\"inputs\":[],\"name\":\"Call\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"
callableBin = "6080604052348015600f57600080fd5b5060998061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806334e2292114602d575b600080fd5b60336035565b005b7f81fab7a4a0aa961db47eefc81f143a5220e8c8495260dd65b1356f1d19d3c7b860405160405180910390a156fea2646970667358221220029436d24f3ac598ceca41d4d712e13ced6d70727f4cdc580667de66d2f51d8b64736f6c63430008010033"
)
var (
require = require.New(t)
engine = dummy.NewCoinbaseFaker()
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
// Get crypto.Address and convert to common.Address via bytes
cryptoAddr1 = crypto.PubkeyToAddress(key1.PublicKey)
addr1 = common.BytesToAddress(cryptoAddr1[:])
funds = new(big.Int).Mul(big.NewInt(100), big.NewInt(params.Ether))
gspec = &Genesis{
Config: params.TestChainConfig,
Alloc: GenesisAlloc{addr1: {Balance: funds}},
BaseFee: big.NewInt(legacy.BaseFee),
}
cryptoContractAddr = crypto.CreateAddress(cryptoAddr1, 0)
contractAddress = common.BytesToAddress(cryptoContractAddr[:])
signer = types.LatestSigner(gspec.Config)
)
parsed, err := abi.JSON(strings.NewReader(callableABI))
require.NoError(err)
packedFunction, err := parsed.Pack("Call")
require.NoError(err)
db, blocks, _, err := GenerateChainWithGenesis(gspec, engine, 2, 10, func(i int, b *BlockGen) {
switch i {
case 0:
// First, we deploy the contract
contractTx := types.NewContractCreation(0, common.Big0, 200000, big.NewInt(legacy.BaseFee), common.FromHex(callableBin))
contractSignedTx, err := types.SignTx(contractTx, signer, key1)
require.NoError(err)
b.AddTx(contractSignedTx)
case 1:
// In the next block, we call the contract function
tx := types.NewTransaction(1, contractAddress, common.Big0, 23000, big.NewInt(legacy.BaseFee), packedFunction)
tx, err := types.SignTx(tx, signer, key1)
require.NoError(err)
b.AddTx(tx)
}
})
require.NoError(err)
// Create blockchain using the database from GenerateChainWithGenesis to ensure
// genesis hash and state are consistent with the generated blocks.
// Disable snapshots to avoid snapshot flattening issues during Accept.
cacheConfig := *DefaultCacheConfig
cacheConfig.SnapshotLimit = 0
chain, err := NewBlockChain(db, &cacheConfig, gspec, engine, vm.Config{}, common.Hash{}, false, nil)
require.NoError(err)
defer chain.Stop()
// Create Log Subscriber
logsCh := make(chan []*types.Log, 10)
defer close(logsCh)
sub := chain.SubscribeAcceptedLogsEvent(logsCh)
defer sub.Unsubscribe()
_, err = chain.InsertChain(blocks)
require.NoError(err)
for _, block := range blocks {
require.NoError(chain.Accept(block))
}
chain.DrainAcceptorQueue()
logs := <-logsCh
require.Len(logs, 1)
require.Equal(blocks[1].Hash(), logs[0].BlockHash)
require.Equal(blocks[1].Number().Uint64(), logs[0].BlockNumber)
}