Skip to content

Commit

Permalink
Refactor feature extensions out of VMManager (#2578)
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
  • Loading branch information
joshua-kim and StephenButtolph committed Jan 19, 2024
1 parent 3d4feed commit 7cea467
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 51 deletions.
9 changes: 9 additions & 0 deletions api/info/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/propertyfx"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

var errNoChainProvided = errors.New("argument 'chain' not given")
Expand Down Expand Up @@ -416,6 +419,7 @@ func (i *Info) GetTxFee(_ *http.Request, _ *struct{}, reply *GetTxFeeResponse) e
// GetVMsReply contains the response metadata for GetVMs
type GetVMsReply struct {
VMs map[ids.ID][]string `json:"vms"`
Fxs map[ids.ID]string `json:"fxs"`
}

// GetVMs lists the virtual machines installed on the node
Expand All @@ -432,5 +436,10 @@ func (i *Info) GetVMs(_ *http.Request, _ *struct{}, reply *GetVMsReply) error {
}

reply.VMs, err = ids.GetRelevantAliases(i.VMManager, vmIDs)
reply.Fxs = map[ids.ID]string{
secp256k1fx.ID: secp256k1fx.Name,
nftfx.ID: nftfx.Name,
propertyfx.ID: propertyfx.Name,
}
return err
}
33 changes: 18 additions & 15 deletions chains/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/fx"
"github.com/ava-labs/avalanchego/vms/metervm"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/avalanchego/vms/propertyfx"
"github.com/ava-labs/avalanchego/vms/proposervm"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/vms/tracedvm"

timetracker "github.com/ava-labs/avalanchego/snow/networking/tracker"
Expand Down Expand Up @@ -96,6 +100,12 @@ var (
errNoPrimaryNetworkConfig = errors.New("no subnet config for primary network found")
errPartialSyncAsAValidator = errors.New("partial sync should not be configured for a validator")

fxs = map[ids.ID]fx.Factory{
secp256k1fx.ID: &secp256k1fx.Factory{},
nftfx.ID: &nftfx.Factory{},
propertyfx.ID: &propertyfx.Factory{},
}

_ Manager = (*manager)(nil)
)

Expand Down Expand Up @@ -511,23 +521,16 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c
}
// TODO: Shutdown VM if an error occurs

fxs := make([]*common.Fx, len(chainParams.FxIDs))
chainFxs := make([]*common.Fx, len(chainParams.FxIDs))
for i, fxID := range chainParams.FxIDs {
// Get a factory for the fx we want to use on our chain
fxFactory, err := m.VMManager.GetFactory(fxID)
if err != nil {
return nil, fmt.Errorf("error while getting fxFactory: %w", err)
}

fx, err := fxFactory.New(chainLog)
if err != nil {
return nil, fmt.Errorf("error while creating fx: %w", err)
fxFactory, ok := fxs[fxID]
if !ok {
return nil, fmt.Errorf("fx %s not found", fxID)
}

// Create the fx
fxs[i] = &common.Fx{
chainFxs[i] = &common.Fx{
ID: fxID,
Fx: fx,
Fx: fxFactory.New(),
}
}

Expand All @@ -539,7 +542,7 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c
chainParams.GenesisData,
m.Validators,
vm,
fxs,
chainFxs,
sb,
)
if err != nil {
Expand All @@ -557,7 +560,7 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c
m.Validators,
beacons,
vm,
fxs,
chainFxs,
sb,
)
if err != nil {
Expand Down
6 changes: 0 additions & 6 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,12 @@ import (
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/avm"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/propertyfx"
"github.com/ava-labs/avalanchego/vms/registry"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/runtime"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

ipcsapi "github.com/ava-labs/avalanchego/api/ipcs"
avmconfig "github.com/ava-labs/avalanchego/vms/avm/config"
Expand Down Expand Up @@ -1226,9 +1223,6 @@ func (n *Node) initVMs() error {
},
}),
n.VMManager.RegisterFactory(context.TODO(), constants.EVMID, &coreth.Factory{}),
n.VMManager.RegisterFactory(context.TODO(), secp256k1fx.ID, &secp256k1fx.Factory{}),
n.VMManager.RegisterFactory(context.TODO(), nftfx.ID, &nftfx.Factory{}),
n.VMManager.RegisterFactory(context.TODO(), propertyfx.ID, &propertyfx.Factory{}),
)
if err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions vms/fx/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package fx

// Factory returns an instance of a feature extension
type Factory interface {
New() any
}
11 changes: 6 additions & 5 deletions vms/nftfx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ package nftfx

import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/fx"
)

const Name = "nftfx"

var (
_ vms.Factory = (*Factory)(nil)
_ fx.Factory = (*Factory)(nil)

// ID that this Fx uses when labeled
ID = ids.ID{'n', 'f', 't', 'f', 'x'}
)

type Factory struct{}

func (*Factory) New(logging.Logger) (interface{}, error) {
return &Fx{}, nil
func (*Factory) New() any {
return &Fx{}
}
6 changes: 1 addition & 5 deletions vms/nftfx/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/utils/logging"
)

func TestFactory(t *testing.T) {
require := require.New(t)

factory := Factory{}
fx, err := factory.New(logging.NoLog{})
require.NoError(err)
require.NotNil(fx)
require.Equal(&Fx{}, factory.New())
}
11 changes: 6 additions & 5 deletions vms/propertyfx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ package propertyfx

import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/fx"
)

const Name = "propertyfx"

var (
_ vms.Factory = (*Factory)(nil)
_ fx.Factory = (*Factory)(nil)

// ID that this Fx uses when labeled
ID = ids.ID{'p', 'r', 'o', 'p', 'e', 'r', 't', 'y', 'f', 'x'}
)

type Factory struct{}

func (*Factory) New(logging.Logger) (interface{}, error) {
return &Fx{}, nil
func (*Factory) New() any {
return &Fx{}
}
6 changes: 1 addition & 5 deletions vms/propertyfx/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/utils/logging"
)

func TestFactory(t *testing.T) {
require := require.New(t)

factory := Factory{}
fx, err := factory.New(logging.NoLog{})
require.NoError(err)
require.NotNil(fx)
require.Equal(&Fx{}, factory.New())
}
11 changes: 6 additions & 5 deletions vms/secp256k1fx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ package secp256k1fx

import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/fx"
)

const Name = "secp256k1fx"

var (
_ vms.Factory = (*Factory)(nil)
_ fx.Factory = (*Factory)(nil)

// ID that this Fx uses when labeled
ID = ids.ID{'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', 'f', 'x'}
)

type Factory struct{}

func (*Factory) New(logging.Logger) (interface{}, error) {
return &Fx{}, nil
func (*Factory) New() any {
return &Fx{}
}
6 changes: 1 addition & 5 deletions vms/secp256k1fx/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/utils/logging"
)

func TestFactory(t *testing.T) {
require := require.New(t)
factory := Factory{}
fx, err := factory.New(logging.NoLog{})
require.NoError(err)
require.NotNil(fx)
require.Equal(&Fx{}, factory.New())
}

0 comments on commit 7cea467

Please sign in to comment.