ava-labs / subnet-evm

Launch your own EVM as an Avalanche Subnet
https://docs.avax.network/subnets/create-a-fuji-subnet
GNU Lesser General Public License v3.0
245 stars 224 forks source link

refactor: allow `types` to be imported by `precompile/contract` #1271

Open ARR4N opened 3 months ago

ARR4N commented 3 months ago

Why this should be merged

Importing core/types from precompile/{modules,contract} results in a circular dependency because types -> params -> precompile/modules -> precompile/contract -> types.

How this works

precompile/modules only depended on precompile/contract for interfaces; by moving the interfaces into the new precompile/interfaces package, there is no longer a transitive dependency params -> precompile/modules -> precompile/contract.

Note that this only allows importing core/types from precompile/contract but not from precompile/modules. This is OK as the latter is merely a registry that has implementations injected into it.

Visualising the dependency graph (raw Graphviz below) with:

$ go install github.com/loov/goda@latest
$ goda graph -cluster -short ./params ./core/types ./precompile/{contract,modules,precompileconfig} | dot -Tpng > depgraph.png

Before

image

After (as at 12febd1 PoC)

image

How this was tested

How is this documented

Raw Graphviz input

Dependency graph at caf34ea master:

digraph G {
    node [penwidth=2 fontsize=10 shape=rectangle target="_graphviz"];
    edge [tailport=e penwidth=2];
    compound=true;
    rankdir=LR;
    newrank=true;
    ranksep="1.5";
    quantum="0.5";
subgraph "cluster_./precompile/interfaces" {
    label="./precompile/interfaces"
    tooltip="./precompile/interfaces"
    href="https://pkg.go.dev/./precompile/interfaces"
    "./precompile/interfaces" [label="" tooltip="./precompile/interfaces" shape=point color="#07918cb2" rank=0];
}
subgraph "cluster_github.com/ava-labs/subnet-evm" {
    label="github.com/ava-labs/subnet-evm (local)"
    tooltip="github.com/ava-labs/subnet-evm (local)"
    href="https://pkg.go.dev/github.com/ava-labs/subnet-evm@"
    "github.com/ava-labs/subnet-evm/core/types" [label="core/types\l3822 / 130.6KB\l" tooltip="github.com/ava-labs/subnet-evm/core/types" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/core/types" color="#910750b2"];
    "github.com/ava-labs/subnet-evm/params" [label="params\l1669 / 78.0KB\l" tooltip="github.com/ava-labs/subnet-evm/params" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/params" color="#915107b2"];
    "github.com/ava-labs/subnet-evm/precompile/contract" [label="precompile/contract\l530 / 22.0KB\l" tooltip="github.com/ava-labs/subnet-evm/precompile/contract" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/precompile/contract" color="#910769b2"];
    "github.com/ava-labs/subnet-evm/precompile/modules" [label="precompile/modules\l113 / 3.5KB\l" tooltip="github.com/ava-labs/subnet-evm/precompile/modules" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/precompile/modules" color="#075191b2"];
    "github.com/ava-labs/subnet-evm/precompile/precompileconfig" [label="precompile/precompileconfig\l334 / 13.7KB\l" tooltip="github.com/ava-labs/subnet-evm/precompile/precompileconfig" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/precompile/precompileconfig" color="#07914cb2"];
}
    "github.com/ava-labs/subnet-evm/core/types" -> "github.com/ava-labs/subnet-evm/params" [tooltip="github.com/ava-labs/subnet-evm/core/types -> github.com/ava-labs/subnet-evm/params" color="#915107b2"];
    "github.com/ava-labs/subnet-evm/params" -> "github.com/ava-labs/subnet-evm/precompile/modules" [tooltip="github.com/ava-labs/subnet-evm/params -> github.com/ava-labs/subnet-evm/precompile/modules" color="#075191b2"];
    "github.com/ava-labs/subnet-evm/params" -> "github.com/ava-labs/subnet-evm/precompile/precompileconfig" [tooltip="github.com/ava-labs/subnet-evm/params -> github.com/ava-labs/subnet-evm/precompile/precompileconfig" color="#07914cb2"];
    "github.com/ava-labs/subnet-evm/precompile/contract" -> "github.com/ava-labs/subnet-evm/precompile/precompileconfig" [tooltip="github.com/ava-labs/subnet-evm/precompile/contract -> github.com/ava-labs/subnet-evm/precompile/precompileconfig" color="#07914cb2"];
    "github.com/ava-labs/subnet-evm/precompile/modules" -> "github.com/ava-labs/subnet-evm/precompile/contract" [tooltip="github.com/ava-labs/subnet-evm/precompile/modules -> github.com/ava-labs/subnet-evm/precompile/contract" color="#910769b2"];
}

Dependency graph at 12febd1 proof-of-concept:

digraph G {
    node [penwidth=2 fontsize=10 shape=rectangle target="_graphviz"];
    edge [tailport=e penwidth=2];
    compound=true;
    rankdir=LR;
    newrank=true;
    ranksep="1.5";
    quantum="0.5";
subgraph "cluster_github.com/ava-labs/subnet-evm" {
    label="github.com/ava-labs/subnet-evm (local)"
    tooltip="github.com/ava-labs/subnet-evm (local)"
    href="https://pkg.go.dev/github.com/ava-labs/subnet-evm@"
    "github.com/ava-labs/subnet-evm/core/types" [label="core/types\l3822 / 130.6KB\l" tooltip="github.com/ava-labs/subnet-evm/core/types" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/core/types" color="#910750b2"];
    "github.com/ava-labs/subnet-evm/params" [label="params\l1669 / 78.0KB\l" tooltip="github.com/ava-labs/subnet-evm/params" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/params" color="#915107b2"];
    "github.com/ava-labs/subnet-evm/precompile/contract" [label="precompile/contract\l486 / 20.5KB\l" tooltip="github.com/ava-labs/subnet-evm/precompile/contract" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/precompile/contract" color="#910769b2"];
    "github.com/ava-labs/subnet-evm/precompile/interfaces" [label="precompile/interfaces\l61 / 2.4KB\l" tooltip="github.com/ava-labs/subnet-evm/precompile/interfaces" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/precompile/interfaces" color="#075a91b2"];
    "github.com/ava-labs/subnet-evm/precompile/modules" [label="precompile/modules\l113 / 3.5KB\l" tooltip="github.com/ava-labs/subnet-evm/precompile/modules" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/precompile/modules" color="#075191b2"];
    "github.com/ava-labs/subnet-evm/precompile/precompileconfig" [label="precompile/precompileconfig\l334 / 13.7KB\l" tooltip="github.com/ava-labs/subnet-evm/precompile/precompileconfig" href="https://pkg.go.dev/github.com/ava-labs/subnet-evm/precompile/precompileconfig" color="#07914cb2"];
}
    "github.com/ava-labs/subnet-evm/core/types" -> "github.com/ava-labs/subnet-evm/params" [tooltip="github.com/ava-labs/subnet-evm/core/types -> github.com/ava-labs/subnet-evm/params" color="#915107b2"];
    "github.com/ava-labs/subnet-evm/params" -> "github.com/ava-labs/subnet-evm/precompile/modules" [tooltip="github.com/ava-labs/subnet-evm/params -> github.com/ava-labs/subnet-evm/precompile/modules" color="#075191b2"];
    "github.com/ava-labs/subnet-evm/params" -> "github.com/ava-labs/subnet-evm/precompile/precompileconfig" [tooltip="github.com/ava-labs/subnet-evm/params -> github.com/ava-labs/subnet-evm/precompile/precompileconfig" color="#07914cb2"];
    "github.com/ava-labs/subnet-evm/precompile/contract" -> "github.com/ava-labs/subnet-evm/core/types" [tooltip="github.com/ava-labs/subnet-evm/precompile/contract -> github.com/ava-labs/subnet-evm/core/types" color="#910750b2"];
    "github.com/ava-labs/subnet-evm/precompile/contract" -> "github.com/ava-labs/subnet-evm/precompile/interfaces" [tooltip="github.com/ava-labs/subnet-evm/precompile/contract -> github.com/ava-labs/subnet-evm/precompile/interfaces" color="#075a91b2"];
    "github.com/ava-labs/subnet-evm/precompile/contract" -> "github.com/ava-labs/subnet-evm/precompile/precompileconfig" [tooltip="github.com/ava-labs/subnet-evm/precompile/contract -> github.com/ava-labs/subnet-evm/precompile/precompileconfig" color="#07914cb2"];
    "github.com/ava-labs/subnet-evm/precompile/interfaces" -> "github.com/ava-labs/subnet-evm/precompile/precompileconfig" [tooltip="github.com/ava-labs/subnet-evm/precompile/interfaces -> github.com/ava-labs/subnet-evm/precompile/precompileconfig" color="#07914cb2"];
    "github.com/ava-labs/subnet-evm/precompile/modules" -> "github.com/ava-labs/subnet-evm/precompile/interfaces" [tooltip="github.com/ava-labs/subnet-evm/precompile/modules -> github.com/ava-labs/subnet-evm/precompile/interfaces" color="#075a91b2"];
}