Skip to content

Commit

Permalink
Add vm-factory logger (ava-labs#2701)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Mar 4, 2023
1 parent c1dfcbc commit 98ebdaf
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 35 deletions.
11 changes: 5 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/storage"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/proposervm"
)
Expand Down Expand Up @@ -925,21 +924,21 @@ func getChainAliases(v *viper.Viper) (map[ids.ID][]string, error) {
return getAliases(v, "chain aliases", ChainAliasesContentKey, ChainAliasesFileKey)
}

func getVMManager(v *viper.Viper) (vms.Manager, error) {
func getVMAliaser(v *viper.Viper) (ids.Aliaser, error) {
vmAliases, err := getVMAliases(v)
if err != nil {
return nil, err
}

manager := vms.NewManager()
aliser := ids.NewAliaser()
for vmID, aliases := range vmAliases {
for _, alias := range aliases {
if err := manager.Alias(vmID, alias); err != nil {
if err := aliser.Alias(vmID, alias); err != nil {
return nil, err
}
}
}
return manager, nil
return aliser, nil
}

// getPathFromDirKey reads flag value from viper instance and then checks the folder existence
Expand Down Expand Up @@ -1401,7 +1400,7 @@ func GetNodeConfig(v *viper.Viper) (node.Config, error) {
}

// VM Aliases
nodeConfig.VMManager, err = getVMManager(v)
nodeConfig.VMAliaser, err = getVMAliaser(v)
if err != nil {
return node.Config{}, err
}
Expand Down
3 changes: 1 addition & 2 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/ava-labs/avalanchego/utils/profiler"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/vms"
)

type IPCConfig struct {
Expand Down Expand Up @@ -186,7 +185,7 @@ type Config struct {
ChainConfigs map[string]chains.ChainConfig `json:"-"`
ChainAliases map[ids.ID][]string `json:"chainAliases"`

VMManager vms.Manager `json:"-"`
VMAliaser ids.Aliaser `json:"-"`

// Halflife to use for the processing requests tracker.
// Larger halflife --> usage metrics change more slowly.
Expand Down
42 changes: 27 additions & 15 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import (
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/utils/wrappers"
"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"
Expand All @@ -94,8 +95,9 @@ var (

// Node is an instance of an Avalanche node.
type Node struct {
Log logging.Logger
LogFactory logging.Factory
Log logging.Logger
VMFactoryLog logging.Logger
LogFactory logging.Factory

// This node's unique ID used when communicating with other nodes
// (in consensus, for example)
Expand Down Expand Up @@ -176,6 +178,8 @@ type Node struct {
MetricsRegisterer *prometheus.Registry
MetricsGatherer metrics.MultiGatherer

VMManager vms.Manager

// VM endpoint registry
VMRegistry registry.VMRegistry

Expand Down Expand Up @@ -627,7 +631,7 @@ func (n *Node) addDefaultVMAliases() error {

for vmID, aliases := range vmAliases {
for _, alias := range aliases {
if err := n.Config.VMManager.Alias(vmID, alias); err != nil {
if err := n.Config.VMAliaser.Alias(vmID, alias); err != nil {
return err
}
}
Expand Down Expand Up @@ -695,7 +699,7 @@ func (n *Node) initChainManager(avaxAssetID ids.ID) error {
StakingBLSKey: n.Config.StakingSigningKey,
Log: n.Log,
LogFactory: n.LogFactory,
VMManager: n.Config.VMManager,
VMManager: n.VMManager,
DecisionAcceptorGroup: n.DecisionAcceptorGroup,
ConsensusAcceptorGroup: n.ConsensusAcceptorGroup,
DBManager: n.DBManager,
Expand Down Expand Up @@ -755,9 +759,10 @@ func (n *Node) initVMs() error {
}

vmRegisterer := registry.NewVMRegisterer(registry.VMRegistererConfig{
APIServer: n.APIServer,
Log: n.Log,
VMManager: n.Config.VMManager,
APIServer: n.APIServer,
Log: n.Log,
VMFactoryLog: n.VMFactoryLog,
VMManager: n.VMManager,
})

// Register the VMs that Avalanche supports
Expand Down Expand Up @@ -801,9 +806,9 @@ func (n *Node) initVMs() error {
},
}),
vmRegisterer.Register(context.TODO(), constants.EVMID, &coreth.Factory{}),
n.Config.VMManager.RegisterFactory(context.TODO(), secp256k1fx.ID, &secp256k1fx.Factory{}),
n.Config.VMManager.RegisterFactory(context.TODO(), nftfx.ID, &nftfx.Factory{}),
n.Config.VMManager.RegisterFactory(context.TODO(), propertyfx.ID, &propertyfx.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 errs.Errored() {
return errs.Err
Expand All @@ -816,7 +821,7 @@ func (n *Node) initVMs() error {
n.VMRegistry = registry.NewVMRegistry(registry.VMRegistryConfig{
VMGetter: registry.NewVMGetter(registry.VMGetterConfig{
FileReader: filesystem.NewReader(),
Manager: n.Config.VMManager,
Manager: n.VMManager,
PluginDirectory: n.Config.PluginDir,
CPUTracker: n.resourceManager,
RuntimeTracker: n.runtimeManager,
Expand Down Expand Up @@ -920,7 +925,7 @@ func (n *Node) initAdminAPI() error {
ProfileDir: n.Config.ProfilerConfig.Dir,
LogFactory: n.LogFactory,
NodeConfig: n.Config,
VMManager: n.Config.VMManager,
VMManager: n.VMManager,
VMRegistry: n.VMRegistry,
},
)
Expand Down Expand Up @@ -978,11 +983,11 @@ func (n *Node) initInfoAPI() error {
AddPrimaryNetworkDelegatorFee: n.Config.AddPrimaryNetworkDelegatorFee,
AddSubnetValidatorFee: n.Config.AddSubnetValidatorFee,
AddSubnetDelegatorFee: n.Config.AddSubnetDelegatorFee,
VMManager: n.Config.VMManager,
VMManager: n.VMManager,
},
n.Log,
n.chainManager,
n.Config.VMManager,
n.VMManager,
n.Config.NetworkConfig.MyIPPort,
n.Net,
primaryValidators,
Expand Down Expand Up @@ -1235,12 +1240,19 @@ func (n *Node) Initialize(
zap.Reflect("config", n.Config),
)

var err error
n.VMFactoryLog, err = logFactory.Make("vm-factory")
if err != nil {
return fmt.Errorf("problem creating vm logger: %w", err)
}

n.VMManager = vms.NewManager(n.VMFactoryLog, config.VMAliaser)

if err := n.initBeacons(); err != nil { // Configure the beacons
return fmt.Errorf("problem initializing node beacons: %w", err)
}

// Set up tracer
var err error
n.tracer, err = trace.New(n.Config.TraceConfig)
if err != nil {
return fmt.Errorf("couldn't initialize tracer: %w", err)
Expand Down
10 changes: 6 additions & 4 deletions vms/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type manager struct {
// alias of the VM. That is, [vmID].String() is an alias for [vmID].
ids.Aliaser

log logging.Logger

lock sync.RWMutex

// Key: A VM's ID
Expand All @@ -74,9 +76,10 @@ type manager struct {
}

// NewManager returns an instance of a VM manager
func NewManager() Manager {
func NewManager(log logging.Logger, aliaser ids.Aliaser) Manager {
return &manager{
Aliaser: ids.NewAliaser(),
Aliaser: aliaser,
log: log,
factories: make(map[ids.ID]Factory),
versions: make(map[ids.ID]string),
}
Expand Down Expand Up @@ -105,8 +108,7 @@ func (m *manager) RegisterFactory(ctx context.Context, vmID ids.ID, factory Fact

m.factories[vmID] = factory

// TODO: Pass in a VM specific logger
vm, err := factory.New(logging.NoLog{})
vm, err := factory.New(m.log)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions vms/registry/vm_registerer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ type registerer interface {

// VMRegistererConfig configures settings for VMRegisterer.
type VMRegistererConfig struct {
APIServer server.Server
Log logging.Logger
VMManager vms.Manager
APIServer server.Server
Log logging.Logger
VMFactoryLog logging.Logger
VMManager vms.Manager
}

type vmRegisterer struct {
Expand Down Expand Up @@ -89,8 +90,7 @@ func (r *vmRegisterer) createStaticHandlers(
vmID ids.ID,
factory vms.Factory,
) (map[string]*common.HTTPHandler, error) {
// TODO: Pass in a VM specific logger
vm, err := factory.New(logging.NoLog{})
vm, err := factory.New(r.config.VMFactoryLog)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions vms/registry/vm_registerer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,10 @@ func initRegistererTest(t *testing.T) *vmRegistererTestResources {
mockLog := logging.NewMockLogger(ctrl)

registerer := NewVMRegisterer(VMRegistererConfig{
APIServer: mockServer,
Log: mockLog,
VMManager: mockManager,
APIServer: mockServer,
Log: mockLog,
VMFactoryLog: logging.NoLog{},
VMManager: mockManager,
})

mockLog.EXPECT().Error(gomock.Any(), gomock.Any()).AnyTimes()
Expand Down

0 comments on commit 98ebdaf

Please sign in to comment.