Skip to content

Commit

Permalink
Merge pull request #73 from bttcprotocol/develop
Browse files Browse the repository at this point in the history
delivery v1.0.7
  • Loading branch information
yuekun0707 authored Nov 9, 2023
2 parents ffbf042 + dad2493 commit 06f9224
Show file tree
Hide file tree
Showing 25 changed files with 734 additions and 309 deletions.
95 changes: 59 additions & 36 deletions bridge/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cmd

import (
"fmt"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/maticnetwork/heimdall/helper"
tendermintLogger "github.com/tendermint/tendermint/libs/log"
)

const (
Expand All @@ -16,6 +18,8 @@ const (
startListenBlockFlag = "start-listen-block"
)

var logger = helper.Logger.With("module", "bridge/cmd/")

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "bridge",
Expand All @@ -26,19 +30,69 @@ var rootCmd = &cobra.Command{
},
}

func BridgeCommands() *cobra.Command {
func BridgeCommands(viperInstance *viper.Viper, loggerInstance tendermintLogger.Logger, caller string) *cobra.Command {
DecorateWithBridgeRootFlags(rootCmd, viperInstance, loggerInstance, caller)
return rootCmd
}

func DecorateWithBridgeRootFlags(
cmd *cobra.Command, viperInstance *viper.Viper, loggerInstance tendermintLogger.Logger, caller string,
) {
cmd.PersistentFlags().StringP(helper.TendermintNodeFlag, "n", helper.DefaultTendermintNode, "Node to connect to")

if err := viperInstance.BindPFlag(helper.TendermintNodeFlag,
cmd.PersistentFlags().Lookup(helper.TendermintNodeFlag)); err != nil {
loggerInstance.Error(fmt.Sprintf("%v | BindPFlag | %v", caller, helper.TendermintNodeFlag), "Error", err)
}

cmd.PersistentFlags().String(helper.HomeFlag, helper.DefaultNodeHome, "directory for config and data")

if err := viperInstance.BindPFlag(helper.HomeFlag, cmd.PersistentFlags().Lookup(helper.HomeFlag)); err != nil {
loggerInstance.Error(fmt.Sprintf("%v | BindPFlag | %v", caller, helper.HomeFlag), "Error", err)
}

// bridge storage db
cmd.PersistentFlags().String(
bridgeDBFlag,
"",
"Bridge db path (default <home>/bridge/storage)",
)

if err := viperInstance.BindPFlag(bridgeDBFlag, cmd.PersistentFlags().Lookup(bridgeDBFlag)); err != nil {
loggerInstance.Error(fmt.Sprintf("%v | BindPFlag | %v", caller, bridgeDBFlag), "Error", err)
}

// bridge chain id
cmd.PersistentFlags().String(
bttcChainIDFlag,
helper.DefaultBttcChainID,
"Bttc chain id",
)

if err := viperInstance.BindPFlag(bttcChainIDFlag, cmd.PersistentFlags().Lookup(bttcChainIDFlag)); err != nil {
loggerInstance.Error(fmt.Sprintf("%v | BindPFlag | %v", caller, bttcChainIDFlag), "Error", err)
}
}

// InitTendermintViperConfig sets global viper configuration needed to heimdall
func InitTendermintViperConfig(cmd *cobra.Command) {
// set appropriate bridge db.
AdjustBridgeDBValue(cmd, viper.GetViper())

// start heimdall config.
helper.InitDeliveryConfig("")
}

// function is called to set appropriate bridge db path.
func AdjustBridgeDBValue(cmd *cobra.Command, v *viper.Viper) {
tendermintNode, _ := cmd.Flags().GetString(helper.TendermintNodeFlag)
homeValue, _ := cmd.Flags().GetString(helper.HomeFlag)
withDeliveryConfigValue, _ := cmd.Flags().GetString(helper.WithDeliveryConfigFlag)
withDeliveryConfigFlag, _ := cmd.Flags().GetString(helper.WithDeliveryConfigFlag)
bridgeDBValue, _ := cmd.Flags().GetString(bridgeDBFlag)
borChainIDValue, _ := cmd.Flags().GetString(bttcChainIDFlag)
bttcChainIDValue, _ := cmd.Flags().GetString(bttcChainIDFlag)
rootChainTypeValue, _ := cmd.Flags().GetString(rootChainTypeFlag)
startListenBlockValue, _ := cmd.Flags().GetInt64(startListenBlockFlag)

// bridge-db directory (default storage)
if bridgeDBValue == "" {
bridgeDBValue = filepath.Join(homeValue, "bridge", "storage")
Expand All @@ -47,40 +101,9 @@ func InitTendermintViperConfig(cmd *cobra.Command) {
// set to viper
viper.Set(helper.TendermintNodeFlag, tendermintNode)
viper.Set(helper.HomeFlag, homeValue)
viper.Set(helper.WithDeliveryConfigFlag, withDeliveryConfigValue)
viper.Set(helper.WithDeliveryConfigFlag, withDeliveryConfigFlag)
viper.Set(bridgeDBFlag, bridgeDBValue)
viper.Set(bttcChainIDFlag, borChainIDValue)
viper.Set(bttcChainIDFlag, bttcChainIDValue)
viper.Set(rootChainTypeFlag, rootChainTypeValue)
viper.Set(startListenBlockFlag, startListenBlockValue)
// start heimdall config
helper.InitDeliveryConfig("")
}

func init() {
var logger = helper.Logger.With("module", "bridge/cmd/")

rootCmd.PersistentFlags().StringP(helper.TendermintNodeFlag, "n", helper.DefaultTendermintNode, "Node to connect to")
rootCmd.PersistentFlags().String(helper.HomeFlag, helper.DefaultNodeHome, "directory for config and data")
rootCmd.PersistentFlags().String(
helper.WithDeliveryConfigFlag,
"",
"Delivery config file path (default <home>/config/delivery-config.toml)",
)
// bridge storage db
rootCmd.PersistentFlags().String(
bridgeDBFlag,
"",
"Bridge db path (default <home>/bridge/storage)",
)
// bridge chain id
rootCmd.PersistentFlags().String(
bttcChainIDFlag,
helper.DefaultBttcChainID,
"Bor chain id",
)

// bind all flags with viper
if err := viper.BindPFlags(rootCmd.Flags()); err != nil {
logger.Error("init | BindPFlag | rootCmd.Flags", "Error", err)
}
}
Loading

0 comments on commit 06f9224

Please sign in to comment.