-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Split config package so it doesn't import core, core/vm (#734)
Co-authored-by: Quentin McGaw <[email protected]>
- Loading branch information
Showing
5 changed files
with
118 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// (c) 2025, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package evm | ||
|
||
import ( | ||
"github.com/ava-labs/coreth/core/txpool/legacypool" | ||
"github.com/ava-labs/coreth/plugin/evm/config" | ||
) | ||
|
||
// defaultTxPoolConfig uses [legacypool.DefaultConfig] to make a [config.TxPoolConfig] | ||
// that can be passed to [config.Config.SetDefaults]. | ||
var defaultTxPoolConfig = config.TxPoolConfig{ | ||
PriceLimit: legacypool.DefaultConfig.PriceLimit, | ||
PriceBump: legacypool.DefaultConfig.PriceBump, | ||
AccountSlots: legacypool.DefaultConfig.AccountSlots, | ||
GlobalSlots: legacypool.DefaultConfig.GlobalSlots, | ||
AccountQueue: legacypool.DefaultConfig.AccountQueue, | ||
GlobalQueue: legacypool.DefaultConfig.GlobalQueue, | ||
Lifetime: legacypool.DefaultConfig.Lifetime, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// (c) 2025, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package evm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/tools/go/packages" | ||
) | ||
|
||
// getDependencies takes a fully qualified package name and returns a map of all | ||
// its recursive package imports (including itself) in the same format. | ||
func getDependencies(packageName string) (map[string]struct{}, error) { | ||
// Configure the load mode to include dependencies | ||
cfg := &packages.Config{Mode: packages.NeedDeps | packages.NeedImports | packages.NeedName | packages.NeedModule} | ||
pkgs, err := packages.Load(cfg, packageName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load package: %v", err) | ||
} | ||
|
||
if len(pkgs) == 0 || pkgs[0].Errors != nil { | ||
return nil, fmt.Errorf("failed to load package %s", packageName) | ||
} | ||
|
||
deps := make(map[string]struct{}) | ||
var collectDeps func(pkg *packages.Package) | ||
collectDeps = func(pkg *packages.Package) { | ||
if _, ok := deps[pkg.PkgPath]; ok { | ||
return // Avoid re-processing the same dependency | ||
} | ||
deps[pkg.PkgPath] = struct{}{} | ||
for _, dep := range pkg.Imports { | ||
collectDeps(dep) | ||
} | ||
} | ||
|
||
// Start collecting dependencies | ||
collectDeps(pkgs[0]) | ||
return deps, nil | ||
} | ||
|
||
func TestMustNotImport(t *testing.T) { | ||
withRepo := func(pkg string) string { | ||
const repo = "github.com/ava-labs/coreth" | ||
return fmt.Sprintf("%s/%s", repo, pkg) | ||
} | ||
mustNotImport := map[string][]string{ | ||
// The following sub-packages of plugin/evm must not import core, core/vm | ||
// so clients (e.g., wallets, e2e tests) can import them without pulling in | ||
// the entire VM logic. | ||
// Importing these packages configures libevm globally and it is not | ||
// possible to do so for both coreth and subnet-evm, where the client may | ||
// wish to connect to multiple chains. | ||
"plugin/evm/atomic": {"core", "core/vm"}, | ||
"plugin/evm/client": {"core", "core/vm"}, | ||
"plugin/evm/config": {"core", "core/vm"}, | ||
} | ||
|
||
for packageName, forbiddenImports := range mustNotImport { | ||
imports, err := getDependencies(withRepo(packageName)) | ||
require.NoError(t, err) | ||
|
||
for _, forbiddenImport := range forbiddenImports { | ||
fullForbiddenImport := withRepo(forbiddenImport) | ||
_, found := imports[fullForbiddenImport] | ||
require.False(t, found, "package %s must not import %s, check output of go list -f '{{ .Deps }}' \"%s\" ", packageName, fullForbiddenImport, withRepo(packageName)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters