github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/config/flags_chains.go (about)

     1  /*
     2   * Copyright (C) 2021 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package config
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/mysteriumnetwork/node/metadata"
    24  	"github.com/urfave/cli/v2"
    25  )
    26  
    27  // TODO: open to suggestions how to do this better.
    28  var (
    29  	// FlagChain1RegistryAddress represents the registry address for chain1.
    30  	FlagChain1RegistryAddress = getRegistryFlag(1)
    31  	// FlagChain2RegistryAddress represents the registry address for chain2.
    32  	FlagChain2RegistryAddress = getRegistryFlag(2)
    33  	// FlagChain1HermesAddress represents the hermes address for chain1.
    34  	FlagChain1HermesAddress = getHermesIDFlag(1)
    35  	// FlagChain2HermesAddress represents the hermes address for chain2.
    36  	FlagChain2HermesAddress = getHermesIDFlag(2)
    37  	// FlagChain1ChannelImplementationAddress represents the channel implementation address for chain1.
    38  	FlagChain1ChannelImplementationAddress = getChannelImplementationFlag(1)
    39  	// FlagChain2ChannelImplementationAddress represents the channel implementation address for chain2.
    40  	FlagChain2ChannelImplementationAddress = getChannelImplementationFlag(2)
    41  	// FlagChain1MystAddress represents the myst address for chain1.
    42  	FlagChain1MystAddress = getMystAddressFlag(1)
    43  	// FlagChain2MystAddress represents the myst address for chain2.
    44  	FlagChain2MystAddress = getMystAddressFlag(2)
    45  	// FlagChain1ChainID represents the chainID for chain1.
    46  	FlagChain1ChainID = getChainIDFlag(1)
    47  	// FlagChain1ChainID represents the chainID for chain2.
    48  	FlagChain2ChainID = getChainIDFlag(2)
    49  	// FlagChain1KnownHermeses represents the known hermeses for chain1.
    50  	FlagChain1KnownHermeses = getKnownHermesesFlag(1)
    51  	// FlagChain2KnownHermeses represents the known hermeses for chain2.
    52  	FlagChain2KnownHermeses = getKnownHermesesFlag(2)
    53  )
    54  
    55  // RegisterFlagsChains function registers chain flags to flag list.
    56  func RegisterFlagsChains(flags *[]cli.Flag) {
    57  	*flags = append(*flags,
    58  		&FlagChain1RegistryAddress,
    59  		&FlagChain2RegistryAddress,
    60  		&FlagChain1HermesAddress,
    61  		&FlagChain2HermesAddress,
    62  		&FlagChain1ChannelImplementationAddress,
    63  		&FlagChain2ChannelImplementationAddress,
    64  		&FlagChain1MystAddress,
    65  		&FlagChain2MystAddress,
    66  		&FlagChain1ChainID,
    67  		&FlagChain2ChainID,
    68  		&FlagChain1KnownHermeses,
    69  		&FlagChain2KnownHermeses,
    70  	)
    71  }
    72  
    73  // ParseFlagsChains function fills in chain options from CLI context.
    74  func ParseFlagsChains(ctx *cli.Context) {
    75  	Current.ParseStringFlag(ctx, FlagChain1RegistryAddress)
    76  	Current.ParseStringFlag(ctx, FlagChain2RegistryAddress)
    77  	Current.ParseStringFlag(ctx, FlagChain1HermesAddress)
    78  	Current.ParseStringFlag(ctx, FlagChain2HermesAddress)
    79  	Current.ParseStringFlag(ctx, FlagChain1ChannelImplementationAddress)
    80  	Current.ParseStringFlag(ctx, FlagChain2ChannelImplementationAddress)
    81  	Current.ParseStringFlag(ctx, FlagChain1MystAddress)
    82  	Current.ParseStringFlag(ctx, FlagChain2MystAddress)
    83  	Current.ParseInt64Flag(ctx, FlagChain1ChainID)
    84  	Current.ParseInt64Flag(ctx, FlagChain2ChainID)
    85  	Current.ParseStringSliceFlag(ctx, FlagChain1KnownHermeses)
    86  	Current.ParseStringSliceFlag(ctx, FlagChain2KnownHermeses)
    87  }
    88  
    89  func getChainFlagData(chainIndex int64) (metadata.ChainDefinition, metadata.ChainDefinitionFlagNames) {
    90  	chainDefinition := metadata.DefaultNetwork.Chain1
    91  	if chainIndex == 2 {
    92  		chainDefinition = metadata.DefaultNetwork.Chain2
    93  	}
    94  
    95  	flagNames := metadata.FlagNames.Chain1Flag
    96  	if chainIndex == 2 {
    97  		flagNames = metadata.FlagNames.Chain2Flag
    98  	}
    99  
   100  	return chainDefinition, flagNames
   101  }
   102  
   103  func getRegistryFlag(chainIndex int64) cli.StringFlag {
   104  	definition, flagNames := getChainFlagData(chainIndex)
   105  
   106  	return cli.StringFlag{
   107  		Name:  flagNames.RegistryAddress,
   108  		Value: definition.RegistryAddress,
   109  		Usage: fmt.Sprintf("Sets the registry smart contract address for main chain %v", chainIndex),
   110  	}
   111  }
   112  
   113  func getHermesIDFlag(chainIndex int64) cli.StringFlag {
   114  	definition, flagNames := getChainFlagData(chainIndex)
   115  
   116  	return cli.StringFlag{
   117  		Name:  flagNames.HermesID,
   118  		Value: definition.HermesID,
   119  		Usage: fmt.Sprintf("Sets the hermes smart contract address for chain %v", chainIndex),
   120  	}
   121  }
   122  
   123  func getChannelImplementationFlag(chainIndex int64) cli.StringFlag {
   124  	definition, flagNames := getChainFlagData(chainIndex)
   125  
   126  	return cli.StringFlag{
   127  		Name:  flagNames.ChannelImplAddress,
   128  		Value: definition.ChannelImplAddress,
   129  		Usage: fmt.Sprintf("Sets the channel implementation smart contract address for chain %v", chainIndex),
   130  	}
   131  }
   132  
   133  func getMystAddressFlag(chainIndex int64) cli.StringFlag {
   134  	definition, flagNames := getChainFlagData(chainIndex)
   135  
   136  	return cli.StringFlag{
   137  		Name:  flagNames.MystAddress,
   138  		Value: definition.MystAddress,
   139  		Usage: fmt.Sprintf("Sets the myst smart contract address for chain %v", chainIndex),
   140  	}
   141  }
   142  
   143  func getChainIDFlag(chainIndex int64) cli.Int64Flag {
   144  	definition, flagNames := getChainFlagData(chainIndex)
   145  
   146  	return cli.Int64Flag{
   147  		Name:  flagNames.ChainIDFlag,
   148  		Value: definition.ChainID,
   149  		Usage: fmt.Sprintf("Sets the chainID for chain %v", chainIndex),
   150  	}
   151  }
   152  
   153  func getKnownHermesesFlag(chainIndex int64) cli.StringSliceFlag {
   154  	definition, flagNames := getChainFlagData(chainIndex)
   155  
   156  	return cli.StringSliceFlag{
   157  		Name:  flagNames.KnownHermesesFlag,
   158  		Value: cli.NewStringSlice(definition.KnownHermeses...),
   159  		Usage: fmt.Sprintf("Sets the known hermeses list for chain %v", chainIndex),
   160  	}
   161  }