Skip to content

Commit

Permalink
Move VMAliaser into node from config (#2689)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Feb 1, 2024
1 parent 271c32e commit 813bd48
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 45 deletions.
19 changes: 1 addition & 18 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,23 +985,6 @@ func getChainAliases(v *viper.Viper) (map[ids.ID][]string, error) {
return getAliases(v, "chain aliases", ChainAliasesContentKey, ChainAliasesFileKey)
}

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

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

// getPathFromDirKey reads flag value from viper instance and then checks the folder existence
func getPathFromDirKey(v *viper.Viper, configKey string) (string, error) {
configDir := GetExpandedArg(v, configKey)
Expand Down Expand Up @@ -1475,7 +1458,7 @@ func GetNodeConfig(v *viper.Viper) (node.Config, error) {
}

// VM Aliases
nodeConfig.VMAliaser, err = getVMAliaser(v)
nodeConfig.VMAliases, err = getVMAliases(v)
if err != nil {
return node.Config{}, err
}
Expand Down
39 changes: 17 additions & 22 deletions genesis/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import (
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

var (
PChainAliases = []string{"P", "platform"}
XChainAliases = []string{"X", "avm"}
CChainAliases = []string{"C", "evm"}
VMAliases = map[ids.ID][]string{
constants.PlatformVMID: {"platform"},
constants.AVMID: {"avm"},
constants.EVMID: {"evm"},
secp256k1fx.ID: {"secp256k1fx"},
nftfx.ID: {"nftfx"},
propertyfx.ID: {"propertyfx"},
}
)

// Aliases returns the default aliases based on the network ID
func Aliases(genesisBytes []byte) (map[string][]string, map[ids.ID][]string, error) {
apiAliases := map[string][]string{
Expand All @@ -26,7 +40,7 @@ func Aliases(genesisBytes []byte) (map[string][]string, map[ids.ID][]string, err
},
}
chainAliases := map[ids.ID][]string{
constants.PlatformChainID: {"P", "platform"},
constants.PlatformChainID: PChainAliases,
}

genesis, err := genesis.Parse(genesisBytes) // TODO let's not re-create genesis to do aliasing
Expand All @@ -45,35 +59,16 @@ func Aliases(genesisBytes []byte) (map[string][]string, map[ids.ID][]string, err
path.Join(constants.ChainAliasPrefix, "X"),
path.Join(constants.ChainAliasPrefix, "avm"),
}
chainAliases[chainID] = GetXChainAliases()
chainAliases[chainID] = XChainAliases
case constants.EVMID:
apiAliases[endpoint] = []string{
"C",
"evm",
path.Join(constants.ChainAliasPrefix, "C"),
path.Join(constants.ChainAliasPrefix, "evm"),
}
chainAliases[chainID] = GetCChainAliases()
chainAliases[chainID] = CChainAliases
}
}
return apiAliases, chainAliases, nil
}

func GetCChainAliases() []string {
return []string{"C", "evm"}
}

func GetXChainAliases() []string {
return []string{"X", "avm"}
}

func GetVMAliases() map[ids.ID][]string {
return map[ids.ID][]string{
constants.PlatformVMID: {"platform"},
constants.AVMID: {"avm"},
constants.EVMID: {"evm"},
secp256k1fx.ID: {"secp256k1fx"},
nftfx.ID: {"nftfx"},
propertyfx.ID: {"propertyfx"},
}
}
2 changes: 1 addition & 1 deletion node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ type Config struct {
ChainConfigs map[string]chains.ChainConfig `json:"-"`
ChainAliases map[ids.ID][]string `json:"chainAliases"`

VMAliaser ids.Aliaser `json:"-"`
VMAliases map[ids.ID][]string `json:"vmAliases"`

// Halflife to use for the processing requests tracker.
// Larger halflife --> usage metrics change more slowly.
Expand Down
16 changes: 12 additions & 4 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,15 @@ func New(
return nil, fmt.Errorf("problem creating vm logger: %w", err)
}

n.VMManager = vms.NewManager(n.VMFactoryLog, config.VMAliaser)
n.VMAliaser = ids.NewAliaser()
for vmID, aliases := range config.VMAliases {
for _, alias := range aliases {
if err := n.VMAliaser.Alias(vmID, alias); err != nil {
return nil, err
}
}
}
n.VMManager = vms.NewManager(n.VMFactoryLog, n.VMAliaser)

if err := n.initBootstrappers(); err != nil { // Configure the bootstrappers
return nil, fmt.Errorf("problem initializing node beacons: %w", err)
Expand Down Expand Up @@ -357,6 +365,7 @@ type Node struct {
MetricsRegisterer *prometheus.Registry
MetricsGatherer metrics.MultiGatherer

VMAliaser ids.Aliaser
VMManager vms.Manager

// VM endpoint registry
Expand Down Expand Up @@ -1045,11 +1054,10 @@ func (n *Node) initAPIServer() error {
// Add the default VM aliases
func (n *Node) addDefaultVMAliases() error {
n.Log.Info("adding the default VM aliases")
vmAliases := genesis.GetVMAliases()

for vmID, aliases := range vmAliases {
for vmID, aliases := range genesis.VMAliases {
for _, alias := range aliases {
if err := n.Config.VMAliaser.Alias(vmID, alias); err != nil {
if err := n.VMAliaser.Alias(vmID, alias); err != nil {
return err
}
}
Expand Down

0 comments on commit 813bd48

Please sign in to comment.