Skip to content
Merged
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
103 changes: 1 addition & 102 deletions tests/reexecute/c/vm_reexecute.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,15 @@ import (
"go.uber.org/zap"

"github.com/ava-labs/avalanchego/api/metrics"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/leveldb"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/graft/coreth/plugin/evm"
"github.com/ava-labs/avalanchego/graft/coreth/plugin/factory"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/engine/enginetest"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/snow/validators/validatorstest"
"github.com/ava-labs/avalanchego/tests"
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
"github.com/ava-labs/avalanchego/tests/reexecute"
"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls/signer/localsigner"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/perms"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/vms/metervm"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
)

var (
mainnetXChainID = ids.FromStringOrPanic("2oYMBNV4eNHyqk2fjjV5nVQLDbtmNJzq5s3qs3Lo6ftnC6FByM")
mainnetCChainID = ids.FromStringOrPanic("2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5")
mainnetAvaxAssetID = ids.FromStringOrPanic("FvwEAhmxKfeiG8SnEvq42hc6whRyY3EFYAvebMqDNDGCgxN5Z")
)

var (
Expand Down Expand Up @@ -252,7 +232,7 @@ func benchmarkReexecuteRange(
r.NoError(db.Close())
}()

vm, err := newMainnetCChainVM(
vm, err := reexecute.NewMainnetCChainVM(
ctx,
db,
chainDataDir,
Expand Down Expand Up @@ -289,87 +269,6 @@ func benchmarkReexecuteRange(
}
}

func newMainnetCChainVM(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have to move to be exported?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function lives in an executable package (main) - functions in executable packages can't be exported for reuse:

// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package main

ctx context.Context,
vmAndSharedMemoryDB database.Database,
chainDataDir string,
configBytes []byte,
vmMultiGatherer metrics.MultiGatherer,
meterVMRegistry prometheus.Registerer,
) (block.ChainVM, error) {
factory := factory.Factory{}
vmIntf, err := factory.New(logging.NoLog{})
if err != nil {
return nil, fmt.Errorf("failed to create VM from factory: %w", err)
}
vm := vmIntf.(block.ChainVM)

blsKey, err := localsigner.New()
if err != nil {
return nil, fmt.Errorf("failed to create BLS key: %w", err)
}

blsPublicKey := blsKey.PublicKey()
warpSigner := warp.NewSigner(blsKey, constants.MainnetID, mainnetCChainID)

genesisConfig := genesis.GetConfig(constants.MainnetID)

sharedMemoryDB := prefixdb.New([]byte("sharedmemory"), vmAndSharedMemoryDB)
atomicMemory := atomic.NewMemory(sharedMemoryDB)

chainIDToSubnetID := map[ids.ID]ids.ID{
mainnetXChainID: constants.PrimaryNetworkID,
mainnetCChainID: constants.PrimaryNetworkID,
ids.Empty: constants.PrimaryNetworkID,
}

vm = metervm.NewBlockVM(vm, meterVMRegistry)

if err := vm.Initialize(
ctx,
&snow.Context{
NetworkID: constants.MainnetID,
SubnetID: constants.PrimaryNetworkID,
ChainID: mainnetCChainID,
NodeID: ids.GenerateTestNodeID(),
PublicKey: blsPublicKey,
NetworkUpgrades: upgrade.Mainnet,

XChainID: mainnetXChainID,
CChainID: mainnetCChainID,
AVAXAssetID: mainnetAvaxAssetID,

Log: tests.NewDefaultLogger("mainnet-vm-reexecution"),
SharedMemory: atomicMemory.NewSharedMemory(mainnetCChainID),
BCLookup: ids.NewAliaser(),
Metrics: vmMultiGatherer,

WarpSigner: warpSigner,

ValidatorState: &validatorstest.State{
GetSubnetIDF: func(_ context.Context, chainID ids.ID) (ids.ID, error) {
subnetID, ok := chainIDToSubnetID[chainID]
if ok {
return subnetID, nil
}
return ids.Empty, fmt.Errorf("unknown chainID: %s", chainID)
},
},
ChainDataDir: chainDataDir,
},
prefixdb.New([]byte("vm"), vmAndSharedMemoryDB),
[]byte(genesisConfig.CChainGenesis),
nil,
configBytes,
nil,
&enginetest.Sender{},
); err != nil {
return nil, fmt.Errorf("failed to initialize VM: %w", err)
}

return vm, nil
}

type vmExecutorConfig struct {
Log logging.Logger
// Registry is the registry to register the metrics with.
Expand Down
117 changes: 117 additions & 0 deletions tests/reexecute/vm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package reexecute

import (
"context"
"fmt"

"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/avalanchego/api/metrics"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/graft/coreth/plugin/factory"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/engine/enginetest"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/snow/validators/validatorstest"
"github.com/ava-labs/avalanchego/tests"
"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls/signer/localsigner"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/metervm"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
)

var (
mainnetXChainID = ids.FromStringOrPanic("2oYMBNV4eNHyqk2fjjV5nVQLDbtmNJzq5s3qs3Lo6ftnC6FByM")
mainnetCChainID = ids.FromStringOrPanic("2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5")
mainnetAvaxAssetID = ids.FromStringOrPanic("FvwEAhmxKfeiG8SnEvq42hc6whRyY3EFYAvebMqDNDGCgxN5Z")
)

func NewMainnetCChainVM(
ctx context.Context,
vmAndSharedMemoryDB database.Database,
chainDataDir string,
configBytes []byte,
vmMultiGatherer metrics.MultiGatherer,
meterVMRegistry prometheus.Registerer,
) (block.ChainVM, error) {
factory := factory.Factory{}
vmIntf, err := factory.New(logging.NoLog{})
if err != nil {
return nil, fmt.Errorf("failed to create VM from factory: %w", err)
}
vm := vmIntf.(block.ChainVM)

blsKey, err := localsigner.New()
if err != nil {
return nil, fmt.Errorf("failed to create BLS key: %w", err)
}

blsPublicKey := blsKey.PublicKey()
warpSigner := warp.NewSigner(blsKey, constants.MainnetID, mainnetCChainID)

genesisConfig := genesis.GetConfig(constants.MainnetID)

sharedMemoryDB := prefixdb.New([]byte("sharedmemory"), vmAndSharedMemoryDB)
atomicMemory := atomic.NewMemory(sharedMemoryDB)

chainIDToSubnetID := map[ids.ID]ids.ID{
mainnetXChainID: constants.PrimaryNetworkID,
mainnetCChainID: constants.PrimaryNetworkID,
ids.Empty: constants.PrimaryNetworkID,
}

vm = metervm.NewBlockVM(vm, meterVMRegistry)

if err := vm.Initialize(
ctx,
&snow.Context{
NetworkID: constants.MainnetID,
SubnetID: constants.PrimaryNetworkID,
ChainID: mainnetCChainID,
NodeID: ids.GenerateTestNodeID(),
PublicKey: blsPublicKey,
NetworkUpgrades: upgrade.Mainnet,

XChainID: mainnetXChainID,
CChainID: mainnetCChainID,
AVAXAssetID: mainnetAvaxAssetID,

Log: tests.NewDefaultLogger("mainnet-vm-reexecution"),
SharedMemory: atomicMemory.NewSharedMemory(mainnetCChainID),
BCLookup: ids.NewAliaser(),
Metrics: vmMultiGatherer,

WarpSigner: warpSigner,

ValidatorState: &validatorstest.State{
GetSubnetIDF: func(_ context.Context, chainID ids.ID) (ids.ID, error) {
subnetID, ok := chainIDToSubnetID[chainID]
if ok {
return subnetID, nil
}
return ids.Empty, fmt.Errorf("unknown chainID: %s", chainID)
},
},
ChainDataDir: chainDataDir,
},
prefixdb.New([]byte("vm"), vmAndSharedMemoryDB),
[]byte(genesisConfig.CChainGenesis),
nil,
configBytes,
nil,
&enginetest.Sender{},
); err != nil {
return nil, fmt.Errorf("failed to initialize VM: %w", err)
}

return vm, nil
}