Skip to content

Commit

Permalink
POS-15 Remove building Heimdall for each chain
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed Nov 3, 2021
1 parent 1cb8737 commit ba2bf08
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 78 deletions.
15 changes: 3 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,6 +51,7 @@ contracts:


init-heimdall:
cp mainnet.toml mumbai.toml build
./build/heimdalld init

show-account-heimdall:
Expand Down
2 changes: 1 addition & 1 deletion bor/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 8 additions & 0 deletions bridge/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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("")
Expand All @@ -68,6 +71,11 @@ func init() {
"",
"Heimdall config file path (default <home>/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,
Expand Down
7 changes: 6 additions & 1 deletion cmd/heimdallcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down Expand Up @@ -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"))
Expand Down
6 changes: 6 additions & 0 deletions cmd/heimdalld/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 12 additions & 2 deletions cmd/heimdalld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,20 @@ func main() {
"Heimdall config file path (default <home>/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())
Expand Down
38 changes: 38 additions & 0 deletions helper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
WithHeimdallConfigFlag = "with-heimdall-config"
HomeFlag = "home"
FlagClientHome = "home-client"
NetworkChainFlag = "chain"

// ---
// TODO Move these to common client flags
Expand Down Expand Up @@ -68,6 +69,8 @@ const (

DefaultBorChainID string = "15001"

DefaultNetworkChain string = "mainnet"

secretFilePerm = 0600
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -227,6 +244,8 @@ func GetDefaultHeimdallConfig() Configuration {
SpanPollInterval: DefaultSpanPollInterval,

NoACKWaitTime: NoACKWaitTime,

NetworkChain: DefaultNetworkChain,
}
}

Expand Down Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions helper/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
62 changes: 0 additions & 62 deletions helper/heimdall-params.template.go

This file was deleted.

42 changes: 42 additions & 0 deletions helper/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package helper

import (
"bytes"
"io/ioutil"
"log"
"path/filepath"
"strings"
"text/template"

"github.com/spf13/viper"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit ba2bf08

Please sign in to comment.