Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit 14e9d30

Browse files
Darioush Jalaliqdm12
andauthored
refactor: Split config package so it doesn't import core, core/vm (#734)
Co-authored-by: Quentin McGaw <[email protected]>
1 parent e19a2f7 commit 14e9d30

File tree

5 files changed

+118
-18
lines changed

5 files changed

+118
-18
lines changed

plugin/evm/config.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// (c) 2025, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package evm
5+
6+
import (
7+
"github.com/ava-labs/coreth/core/txpool/legacypool"
8+
"github.com/ava-labs/coreth/plugin/evm/config"
9+
)
10+
11+
// defaultTxPoolConfig uses [legacypool.DefaultConfig] to make a [config.TxPoolConfig]
12+
// that can be passed to [config.Config.SetDefaults].
13+
var defaultTxPoolConfig = config.TxPoolConfig{
14+
PriceLimit: legacypool.DefaultConfig.PriceLimit,
15+
PriceBump: legacypool.DefaultConfig.PriceBump,
16+
AccountSlots: legacypool.DefaultConfig.AccountSlots,
17+
GlobalSlots: legacypool.DefaultConfig.GlobalSlots,
18+
AccountQueue: legacypool.DefaultConfig.AccountQueue,
19+
GlobalQueue: legacypool.DefaultConfig.GlobalQueue,
20+
Lifetime: legacypool.DefaultConfig.Lifetime,
21+
}

plugin/evm/config/config.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"time"
1010

1111
"github.com/ava-labs/avalanchego/utils/constants"
12-
"github.com/ava-labs/coreth/core/txpool/legacypool"
13-
"github.com/ava-labs/coreth/eth"
1412
"github.com/ethereum/go-ethereum/common"
1513
"github.com/ethereum/go-ethereum/common/hexutil"
1614
"github.com/spf13/cast"
@@ -224,28 +222,37 @@ type Config struct {
224222
HttpBodyLimit uint64 `json:"http-body-limit"`
225223
}
226224

225+
// TxPoolConfig contains the transaction pool config to be passed
226+
// to [Config.SetDefaults].
227+
type TxPoolConfig struct {
228+
PriceLimit uint64
229+
PriceBump uint64
230+
AccountSlots uint64
231+
GlobalSlots uint64
232+
AccountQueue uint64
233+
GlobalQueue uint64
234+
Lifetime time.Duration
235+
}
236+
227237
// EthAPIs returns an array of strings representing the Eth APIs that should be enabled
228238
func (c Config) EthAPIs() []string {
229239
return c.EnabledEthAPIs
230240
}
231241

232-
func (c Config) EthBackendSettings() eth.Settings {
233-
return eth.Settings{MaxBlocksPerRequest: c.MaxBlocksPerRequest}
234-
}
235-
236-
func (c *Config) SetDefaults() {
242+
func (c *Config) SetDefaults(txPoolConfig TxPoolConfig) {
237243
c.EnabledEthAPIs = defaultEnabledAPIs
238244
c.RPCGasCap = defaultRpcGasCap
239245
c.RPCTxFeeCap = defaultRpcTxFeeCap
240246
c.MetricsExpensiveEnabled = defaultMetricsExpensiveEnabled
241247

242-
c.TxPoolPriceLimit = legacypool.DefaultConfig.PriceLimit
243-
c.TxPoolPriceBump = legacypool.DefaultConfig.PriceBump
244-
c.TxPoolAccountSlots = legacypool.DefaultConfig.AccountSlots
245-
c.TxPoolGlobalSlots = legacypool.DefaultConfig.GlobalSlots
246-
c.TxPoolAccountQueue = legacypool.DefaultConfig.AccountQueue
247-
c.TxPoolGlobalQueue = legacypool.DefaultConfig.GlobalQueue
248-
c.TxPoolLifetime.Duration = legacypool.DefaultConfig.Lifetime
248+
// TxPool settings
249+
c.TxPoolPriceLimit = txPoolConfig.PriceLimit
250+
c.TxPoolPriceBump = txPoolConfig.PriceBump
251+
c.TxPoolAccountSlots = txPoolConfig.AccountSlots
252+
c.TxPoolGlobalSlots = txPoolConfig.GlobalSlots
253+
c.TxPoolAccountQueue = txPoolConfig.AccountQueue
254+
c.TxPoolGlobalQueue = txPoolConfig.GlobalQueue
255+
c.TxPoolLifetime.Duration = txPoolConfig.Lifetime
249256

250257
c.APIMaxDuration.Duration = defaultApiMaxDuration
251258
c.WSCPURefillRate.Duration = defaultWsCpuRefillRate

plugin/evm/imports_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// (c) 2025, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package evm
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
"golang.org/x/tools/go/packages"
12+
)
13+
14+
// getDependencies takes a fully qualified package name and returns a map of all
15+
// its recursive package imports (including itself) in the same format.
16+
func getDependencies(packageName string) (map[string]struct{}, error) {
17+
// Configure the load mode to include dependencies
18+
cfg := &packages.Config{Mode: packages.NeedDeps | packages.NeedImports | packages.NeedName | packages.NeedModule}
19+
pkgs, err := packages.Load(cfg, packageName)
20+
if err != nil {
21+
return nil, fmt.Errorf("failed to load package: %v", err)
22+
}
23+
24+
if len(pkgs) == 0 || pkgs[0].Errors != nil {
25+
return nil, fmt.Errorf("failed to load package %s", packageName)
26+
}
27+
28+
deps := make(map[string]struct{})
29+
var collectDeps func(pkg *packages.Package)
30+
collectDeps = func(pkg *packages.Package) {
31+
if _, ok := deps[pkg.PkgPath]; ok {
32+
return // Avoid re-processing the same dependency
33+
}
34+
deps[pkg.PkgPath] = struct{}{}
35+
for _, dep := range pkg.Imports {
36+
collectDeps(dep)
37+
}
38+
}
39+
40+
// Start collecting dependencies
41+
collectDeps(pkgs[0])
42+
return deps, nil
43+
}
44+
45+
func TestMustNotImport(t *testing.T) {
46+
withRepo := func(pkg string) string {
47+
const repo = "github.com/ava-labs/coreth"
48+
return fmt.Sprintf("%s/%s", repo, pkg)
49+
}
50+
mustNotImport := map[string][]string{
51+
// The following sub-packages of plugin/evm must not import core, core/vm
52+
// so clients (e.g., wallets, e2e tests) can import them without pulling in
53+
// the entire VM logic.
54+
// Importing these packages configures libevm globally and it is not
55+
// possible to do so for both coreth and subnet-evm, where the client may
56+
// wish to connect to multiple chains.
57+
"plugin/evm/atomic": {"core", "core/vm"},
58+
"plugin/evm/client": {"core", "core/vm"},
59+
"plugin/evm/config": {"core", "core/vm"},
60+
}
61+
62+
for packageName, forbiddenImports := range mustNotImport {
63+
imports, err := getDependencies(withRepo(packageName))
64+
require.NoError(t, err)
65+
66+
for _, forbiddenImport := range forbiddenImports {
67+
fullForbiddenImport := withRepo(forbiddenImport)
68+
_, found := imports[fullForbiddenImport]
69+
require.False(t, found, "package %s must not import %s, check output of go list -f '{{ .Deps }}' \"%s\" ", packageName, fullForbiddenImport, withRepo(packageName))
70+
}
71+
}
72+
}

plugin/evm/vm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func (vm *VM) Initialize(
339339
fxs []*commonEng.Fx,
340340
appSender commonEng.AppSender,
341341
) error {
342-
vm.config.SetDefaults()
342+
vm.config.SetDefaults(defaultTxPoolConfig)
343343
if len(configBytes) > 0 {
344344
if err := json.Unmarshal(configBytes, &vm.config); err != nil {
345345
return fmt.Errorf("failed to unmarshal config %s: %w", string(configBytes), err)
@@ -660,7 +660,7 @@ func (vm *VM) initializeChain(lastAcceptedHash common.Hash) error {
660660
&vm.ethConfig,
661661
&EthPushGossiper{vm: vm},
662662
vm.chaindb,
663-
vm.config.EthBackendSettings(),
663+
eth.Settings{MaxBlocksPerRequest: vm.config.MaxBlocksPerRequest},
664664
lastAcceptedHash,
665665
dummy.NewFakerWithClock(callbacks, &vm.clock),
666666
&vm.clock,

plugin/evm/vm_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func TestVMConfigDefaults(t *testing.T) {
402402
_, vm, _, _, _ := GenesisVM(t, false, "", configJSON, "")
403403

404404
var vmConfig config.Config
405-
vmConfig.SetDefaults()
405+
vmConfig.SetDefaults(defaultTxPoolConfig)
406406
vmConfig.RPCTxFeeCap = txFeeCap
407407
vmConfig.EnabledEthAPIs = enabledEthAPIs
408408
require.Equal(t, vmConfig, vm.config, "VM Config should match default with overrides")
@@ -414,7 +414,7 @@ func TestVMNilConfig(t *testing.T) {
414414

415415
// VM Config should match defaults if no config is passed in
416416
var vmConfig config.Config
417-
vmConfig.SetDefaults()
417+
vmConfig.SetDefaults(defaultTxPoolConfig)
418418
require.Equal(t, vmConfig, vm.config, "VM Config should match default config")
419419
require.NoError(t, vm.Shutdown(context.Background()))
420420
}

0 commit comments

Comments
 (0)