From ba2bf08df53a47fe99caf0a6032cf099b721beb5 Mon Sep 17 00:00:00 2001 From: Igor Crevar Date: Tue, 2 Nov 2021 18:22:22 +0100 Subject: [PATCH] POS-15 Remove building Heimdall for each chain --- Makefile | 15 ++------ bor/keeper.go | 2 +- bridge/cmd/root.go | 8 ++++ cmd/heimdallcli/main.go | 7 +++- cmd/heimdalld/init.go | 6 +++ cmd/heimdalld/main.go | 14 ++++++- helper/config.go | 38 ++++++++++++++++++ helper/config_test.go | 13 +++++++ helper/heimdall-params.template.go | 62 ------------------------------ helper/toml.go | 42 ++++++++++++++++++++ 10 files changed, 129 insertions(+), 78 deletions(-) delete mode 100644 helper/heimdall-params.template.go diff --git a/Makefile b/Makefile index ccf7c3a04..80c9d875f 100644 --- a/Makefile +++ b/Makefile @@ -25,26 +25,16 @@ tests: go test -v ./app/ ./auth/ ./clerk/ ./sidechannel/ ./bank/ ./chainmanager/ ./topup/ ./checkpoint/ ./staking/ -cover -coverprofile=cover.out -# make build Will generate for mainnet by default -# make build network=mainnet Will generate for mainnet -# make build network=mumbai Will generate for mumbai -# make build network=local Will generate for local with NewSelectionAlgoHeight = 0 -# make build network=anythingElse Will generate for mainnet by default +# make build build: clean - go run helper/heimdall-params.template.go $(network) mkdir -p build go build -o build/heimdalld ./cmd/heimdalld go build -o build/heimdallcli ./cmd/heimdallcli go build -o build/bridge bridge/bridge.go @echo "====================================================\n==================Build Successful==================\n====================================================" -# make install Will generate for mainnet by default -# make install network=mainnet Will generate for mainnet -# make install network=mumbai Will generate for mumbai -# make install network=local Will generate for local with NewSelectionAlgoHeight = 0 -# make install network=anythingElse Will generate for mainnet by default +# make install install: - go run helper/heimdall-params.template.go $(network) go install $(BUILD_FLAGS) ./cmd/heimdalld go install $(BUILD_FLAGS) ./cmd/heimdallcli go install $(BUILD_FLAGS) bridge/bridge.go @@ -61,6 +51,7 @@ contracts: init-heimdall: + cp mainnet.toml mumbai.toml build ./build/heimdalld init show-account-heimdall: diff --git a/bor/keeper.go b/bor/keeper.go index a7fb8b9fa..5b902dff2 100644 --- a/bor/keeper.go +++ b/bor/keeper.go @@ -232,7 +232,7 @@ func (k *Keeper) SelectNextProducers(ctx sdk.Context, seed common.Hash) (vals [] // TODO remove old selection algorigthm // select next producers using seed as blockheader hash fn := SelectNextProducers - if ctx.BlockHeight() < helper.NewSelectionAlgoHeight { + if ctx.BlockHeight() < helper.GetNewSelectionAlgoHeight() { fn = XXXSelectNextProducers } diff --git a/bridge/cmd/root.go b/bridge/cmd/root.go index d32997bf0..a8a8ef3fe 100644 --- a/bridge/cmd/root.go +++ b/bridge/cmd/root.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -33,6 +34,7 @@ func InitTendermintViperConfig(cmd *cobra.Command) { withHeimdallConfigValue, _ := cmd.Flags().GetString(helper.WithHeimdallConfigFlag) bridgeDBValue, _ := cmd.Flags().GetString(bridgeDBFlag) borChainIDValue, _ := cmd.Flags().GetString(borChainIDFlag) + networkChain, _ := cmd.Flags().GetString(helper.NetworkChainFlag) // bridge-db directory (default storage) if bridgeDBValue == "" { @@ -45,6 +47,7 @@ func InitTendermintViperConfig(cmd *cobra.Command) { viper.Set(helper.WithHeimdallConfigFlag, withHeimdallConfigValue) viper.Set(bridgeDBFlag, bridgeDBValue) viper.Set(borChainIDFlag, borChainIDValue) + viper.Set(helper.NetworkChainFlag, networkChain) // start heimdall config helper.InitHeimdallConfig("") @@ -68,6 +71,11 @@ func init() { "", "Heimdall config file path (default /config/heimdall-config.json)", ) + rootCmd.PersistentFlags().String( + helper.NetworkChainFlag, + "", + fmt.Sprintf("Set one of the chains: [%s]", strings.Join(helper.GetValidNetworkChains(), ",")), + ) // bridge storage db rootCmd.PersistentFlags().String( bridgeDBFlag, diff --git a/cmd/heimdallcli/main.go b/cmd/heimdallcli/main.go index 17ce1823d..c73cb0336 100644 --- a/cmd/heimdallcli/main.go +++ b/cmd/heimdallcli/main.go @@ -60,11 +60,13 @@ func initTendermintViperConfig(cmd *cobra.Command) { tendermintNode, _ := cmd.Flags().GetString(helper.NodeFlag) homeValue, _ := cmd.Flags().GetString(helper.HomeFlag) withHeimdallConfigValue, _ := cmd.Flags().GetString(helper.WithHeimdallConfigFlag) + networkChain, _ := cmd.Flags().GetString(helper.NetworkChainFlag) // set to viper viper.Set(helper.NodeFlag, tendermintNode) viper.Set(helper.HomeFlag, homeValue) viper.Set(helper.WithHeimdallConfigFlag, withHeimdallConfigValue) + viper.Set(helper.NetworkChainFlag, networkChain) // start heimdall config helper.InitHeimdallConfig("") @@ -111,10 +113,13 @@ func main() { ApproveCmd(cliCtx), ) - // bind with-heimdall-config config with root cmd + // bind with-heimdall-config config and chain flag with root cmd if err := viper.BindPFlag(helper.WithHeimdallConfigFlag, rootCmd.Flags().Lookup(helper.WithHeimdallConfigFlag)); err != nil { logger.Error("main | BindPFlag | helper.WithHeimdallConfigFlag", "Error", err) } + if err := viper.BindPFlag(helper.NetworkChainFlag, rootCmd.Flags().Lookup(helper.NetworkChainFlag)); err != nil { + logger.Error("main | BindPFlag | helper.NetworkChainFlag", "Error", err) + } // prepare and add flags executor := cli.PrepareMainCmd(rootCmd, "HD", os.ExpandEnv("$HOME/.heimdalld")) diff --git a/cmd/heimdalld/init.go b/cmd/heimdalld/init.go index 39136484f..76cb5c0d7 100644 --- a/cmd/heimdalld/init.go +++ b/cmd/heimdalld/init.go @@ -50,6 +50,12 @@ func initCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { WriteDefaultHeimdallConfig(filepath.Join(config.RootDir, "config/heimdall-config.toml"), helper.GetDefaultHeimdallConfig()) + // copy toml files for each network chain into config folder + err = helper.CopyNetworkChainTomlsToConfigDir(filepath.Join(config.RootDir, "config"), helper.GetValidNetworkChains()) + if err != nil { + return err + } + // get pubkey newPubkey := CryptoKeyToPubkey(valPubKey) diff --git a/cmd/heimdalld/main.go b/cmd/heimdalld/main.go index 6579cb1b6..3619a645a 100644 --- a/cmd/heimdalld/main.go +++ b/cmd/heimdalld/main.go @@ -93,10 +93,20 @@ func main() { "Heimdall config file path (default /config/heimdall-config.json)", ) - // bind with-heimdall-config config with root cmd - if err := viper.BindPFlag(helper.WithHeimdallConfigFlag, rootCmd.Flags().Lookup(helper.WithHeimdallConfigFlag)); err != nil { + rootCmd.PersistentFlags().String( + helper.NetworkChainFlag, + "", + fmt.Sprintf("Set one of the chains: [%s]", strings.Join(helper.GetValidNetworkChains(), ",")), + ) + + // bind with-heimdall-config config and chain flag with root cmd + if err := viper.BindPFlag(helper.WithHeimdallConfigFlag, rootCmd.PersistentFlags().Lookup(helper.WithHeimdallConfigFlag)); err != nil { logger.Error("main | BindPFlag | helper.WithHeimdallConfigFlag", "Error", err) } + if err := viper.BindPFlag(helper.NetworkChainFlag, rootCmd.PersistentFlags().Lookup(helper.NetworkChainFlag)); err != nil { + logger.Error("main | BindPFlag | helper.NetworkChainFlag", "Error", err) + } + server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators) rootCmd.AddCommand(showAccountCmd()) rootCmd.AddCommand(showPrivateKeyCmd()) diff --git a/helper/config.go b/helper/config.go index c10705c8b..c10be25f9 100644 --- a/helper/config.go +++ b/helper/config.go @@ -28,6 +28,7 @@ const ( WithHeimdallConfigFlag = "with-heimdall-config" HomeFlag = "home" FlagClientHome = "home-client" + NetworkChainFlag = "chain" // --- // TODO Move these to common client flags @@ -68,6 +69,8 @@ const ( DefaultBorChainID string = "15001" + DefaultNetworkChain string = "mainnet" + secretFilePerm = 0600 ) @@ -107,6 +110,9 @@ type Configuration struct { // wait time related options NoACKWaitTime time.Duration `mapstructure:"no_ack_wait_time"` // Time ack service waits to clear buffer and elect new proposer + + // current network chain + NetworkChain string `mapstructure:"network_chain"` } var conf Configuration @@ -132,6 +138,9 @@ var Logger logger.Logger // GenesisDoc contains the genesis file var GenesisDoc tmTypes.GenesisDoc +// keeps newSelectionAlgoHeight from network chain toml file +var newSelectionAlgoHeight int64 = 0 + // Contracts // var RootChain types.Contract // var DepositManager types.Contract @@ -204,6 +213,14 @@ func InitHeimdallConfigWith(homeDir string, heimdallConfigFilePath string) { privVal := privval.LoadFilePV(filepath.Join(configDir, "priv_validator_key.json"), filepath.Join(configDir, "priv_validator_key.json")) cdc.MustUnmarshalBinaryBare(privVal.Key.PrivKey.Bytes(), &privObject) cdc.MustUnmarshalBinaryBare(privObject.PubKey().Bytes(), &pubObject) + + // get network chain form viper/cobra flag and set newSelectionAlgoHeight + networkChain := viper.GetString(NetworkChainFlag) + if networkChain != "" && isValidNetworkChain(networkChain) { + newSelectionAlgoHeight = readNetworkChainToml(configDir, networkChain) + } else { + newSelectionAlgoHeight = readNetworkChainToml(configDir, GetConfig().NetworkChain) + } } // GetDefaultHeimdallConfig returns configration with default params @@ -227,6 +244,8 @@ func GetDefaultHeimdallConfig() Configuration { SpanPollInterval: DefaultSpanPollInterval, NoACKWaitTime: NoACKWaitTime, + + NetworkChain: DefaultNetworkChain, } } @@ -298,3 +317,22 @@ func GetPubKey() secp256k1.PubKeySecp256k1 { func GetAddress() []byte { return GetPubKey().Address().Bytes() } + +// get all valid networks +func GetValidNetworkChains() []string { + return []string{"mainnet", "mumbai", "local"} +} + +// Gets NewSelectionAlgoHeight +func GetNewSelectionAlgoHeight() int64 { + return newSelectionAlgoHeight +} + +func isValidNetworkChain(network string) bool { + for _, validNetwork := range GetValidNetworkChains() { + if strings.Compare(validNetwork, network) == 0 { + return true + } + } + return false +} diff --git a/helper/config_test.go b/helper/config_test.go index 3c8bb1ca4..85269452e 100644 --- a/helper/config_test.go +++ b/helper/config_test.go @@ -25,3 +25,16 @@ func TestHeimdallConfig(t *testing.T) { fmt.Println("PublicKey", pubKey.String()) // fmt.Println("CryptoPublicKey", pubKey.CryptoPubKey().String()) } + +func TestHeimdallConfigNewSelectionAlgoHeight(t *testing.T) { + var data map[string]bool = map[string]bool{"mumbai": false, "mainnet": false, "local": true} + for chain, shouldBeZero := range data { + conf.BorRPCUrl = "" // allow config to be loaded again + viper.Set("chain", chain) + InitHeimdallConfig(os.ExpandEnv("$HOME/.heimdalld")) + nsah := GetNewSelectionAlgoHeight() + if nsah == 0 && !shouldBeZero || nsah != 0 && shouldBeZero { + t.Errorf("Invalid GetNewSelectionAlgoHeight = %d for chain %s", nsah, chain) + } + } +} diff --git a/helper/heimdall-params.template.go b/helper/heimdall-params.template.go deleted file mode 100644 index 00ad74b47..000000000 --- a/helper/heimdall-params.template.go +++ /dev/null @@ -1,62 +0,0 @@ -// The following directive is necessary to make the package coherent: - -// +build ignore - -// This program generate heimdall-params.go, It must be invoked from make file that purpose only. - -package main - -import ( - "fmt" - "log" - "os" - "text/template" - - "github.com/BurntSushi/toml" -) - -var packageTemplate = template.Must(template.New("").Parse(`package helper - -const NewSelectionAlgoHeight = {{ .BlockHeight }} -`)) - -var tomlConfig struct { - NewSelectionAlgoHeight int `toml:"new_selection_algo_height"` -} - -var networks = []string{ - "mainnet", - "mumbai", - "local", -} - -func main() { - var network = networks[0] - if len(os.Args) > 1 { - networkFile := os.Args[1] - for _, n := range networks { - if n == networkFile { - network = networkFile - } - } - } - - filePath := fmt.Sprintf("%s.toml", network) - toml.DecodeFile(filePath, &tomlConfig) - - f, err := os.Create("helper/heimdall-params.go") - chekcError(err) - defer f.Close() - - packageTemplate.Execute(f, struct { - BlockHeight int - }{ - BlockHeight: tomlConfig.NewSelectionAlgoHeight, - }) -} - -func chekcError(err error) { - if err != nil { - log.Fatal(err) - } -} diff --git a/helper/toml.go b/helper/toml.go index 26d8a9751..9db2f6447 100644 --- a/helper/toml.go +++ b/helper/toml.go @@ -2,6 +2,10 @@ package helper import ( "bytes" + "io/ioutil" + "log" + "path/filepath" + "strings" "text/template" "github.com/spf13/viper" @@ -48,6 +52,8 @@ main_chain_max_gas_price = "{{ .MainchainMaxGasPrice }}" ##### Timeout Config ##### no_ack_wait_time = "{{ .NoACKWaitTime }}" +##### current network chain - newSelectionAlgoHeight depends on this ##### +network_chain = "{{ .NetworkChain }}" ` var configTemplate *template.Template @@ -79,3 +85,39 @@ func WriteConfigFile(configFilePath string, config *Configuration) { cmn.MustWriteFile(configFilePath, buffer.Bytes(), 0644) } + +func readNetworkChainToml(configDir string, network string) int64 { + if strings.Compare(network, "local") == 0 { + return 0 + } + v := viper.New() + v.SetConfigType("toml") + v.SetConfigName(network) + v.AddConfigPath(configDir) + if err := v.ReadInConfig(); err != nil { + log.Fatal(err) + } + + result := v.GetInt64("new_selection_algo_height") + return result +} + +func CopyNetworkChainTomlsToConfigDir(configDir string, networkChains []string) (err error) { + var input []byte + for _, chain := range networkChains { + if strings.Compare(chain, "local") == 0 { + continue + } + + fileName := chain + ".toml" + sourceDir := filepath.Join(".", fileName) + destDir := filepath.Join(configDir, fileName) + if input, err = ioutil.ReadFile(sourceDir); err != nil { + return + } + if err = ioutil.WriteFile(destDir, input, 0644); err != nil { + return + } + } + return +}