Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relayer: CLI tool #85

Merged
merged 28 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b05dacd
draft implementation of the CMD tool for the native relayer
sczembor Jan 9, 2025
db95ebf
add yaml config file and logic to parse it in the cmd tool
sczembor Jan 9, 2025
71521f1
error handling + use config file to init the relayer
sczembor Jan 10, 2025
4633988
white spaces
sczembor Jan 10, 2025
bb10bbb
lint + move config to a separate file
sczembor Jan 10, 2025
ac7df82
Apply suggestions from code review
sczembor Jan 10, 2025
f5f3d66
apply code review suggestions
sczembor Jan 10, 2025
f20c994
apply code review suggestions
sczembor Jan 10, 2025
02bb366
Merge branch 'master' into stan/78-cmd
sczembor Jan 13, 2025
1a6376b
merge fix
sczembor Jan 13, 2025
2d6931a
apply code review suggesitons
sczembor Jan 13, 2025
b86d3f4
config structure
sczembor Jan 13, 2025
53ffbb1
remove unused config
sczembor Jan 13, 2025
342cfcb
update the function to accept a lvl argument
sczembor Jan 13, 2025
c16b0da
Merge branch 'master' into stan/78-cmd
sczembor Jan 13, 2025
7f05352
remove mock btc client
sczembor Jan 13, 2025
83dfbe0
add network type
sczembor Jan 13, 2025
45356b0
update tags + add unit test
sczembor Jan 14, 2025
43e67d5
Apply suggestions from code review
sczembor Jan 14, 2025
e37f79c
create a helper function to prepare env
sczembor Jan 14, 2025
27be8c5
Update cmd/native-relayer/cli/start.go
sczembor Jan 14, 2025
09ddd9c
divide the start into smaller chunks, remove unecesary json tags
sczembor Jan 14, 2025
53a336d
fix linter
sczembor Jan 14, 2025
b8173b3
mispelling
sczembor Jan 14, 2025
142bc6f
apply code review suggestions
sczembor Jan 14, 2025
bec097c
Merge branch 'master' into stan/78-cmd
sczembor Jan 14, 2025
0f917f0
linter
sczembor Jan 14, 2025
3d0ec15
Merge branch 'stan/78-cmd' of https://github.com/gonative-cc/relayer …
sczembor Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/native-relayer/cli/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cli

import "time"

// Configuration struct
type Config struct {
NativeRPC string `mapstructure:"native_rpc"`
NativeGRPC string `mapstructure:"native_grpc"`
StartBlockHeight int `mapstructure:"start_block_height"`
IkaRPC string `mapstructure:"ika_rpc"`
IkaSignerMnemonic string `mapstructure:"ika_signer_mnemonic"`
IkaNativeLcPackage string `mapstructure:"ika_native_lc_package"`
IkaNativeLcModule string `mapstructure:"ika_native_lc_module"`
IkaNativeLcFunction string `mapstructure:"ika_native_lc_function"`
IkaGasAcc string `mapstructure:"ika_gas_acc"`
IkaGasBudget string `mapstructure:"ika_gas_budget"`
BtcRPCHost string `mapstructure:"btc_rpc_host"`
BtcRPCUser string `mapstructure:"btc_rpc_user"`
BtcRPCPass string `mapstructure:"btc_rpc_pass"`
BtcConfirmationThreshold uint8 `mapstructure:"btc_confirmation_threshold"`
HTTPPostMode bool `mapstructure:"http_post_mode"`
DisableTLS bool `mapstructure:"disable_tls"`
ProcessTxsInterval time.Duration `mapstructure:"process_txs_interval"`
ConfirmTxsInterval time.Duration `mapstructure:"confirm_txs_interval"`
DBFile string `mapstructure:"db_file"`
}
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions cmd/native-relayer/cli/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cli

import (
"os"
"sync"

"github.com/gonative-cc/relayer/nbtc"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var (
verbose bool // Flag for verbose mode

rootCmd = &cobra.Command{
Use: "relayer-cli",
Short: "CLI tool for managing the relayer",
}

relayerInstance *nbtc.Relayer
relayerWg sync.WaitGroup
)

func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Error().Err(err).Msg("CLI execution failed")
os.Exit(1)
}
}

func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
sczembor marked this conversation as resolved.
Show resolved Hide resolved
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(stopCmd)
}
129 changes: 129 additions & 0 deletions cmd/native-relayer/cli/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package cli

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/block-vision/sui-go-sdk/signer"
"github.com/block-vision/sui-go-sdk/sui"
"github.com/btcsuite/btcd/rpcclient"
"github.com/gonative-cc/relayer/bitcoin"
"github.com/gonative-cc/relayer/dal"
"github.com/gonative-cc/relayer/ika"
"github.com/gonative-cc/relayer/ika2btc"
"github.com/gonative-cc/relayer/native2ika"
"github.com/gonative-cc/relayer/nbtc"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var startCmd = &cobra.Command{
Use: "start",
Short: "Starts the relayer",
Run: func(cmd *cobra.Command, args []string) {
config, err := loadConfig()
if err != nil {
log.Error().Err(err).Msg("Error loading config")
os.Exit(1)
}
sczembor marked this conversation as resolved.
Show resolved Hide resolved

// TODO:Set up logging based on verbose flag
sczembor marked this conversation as resolved.
Show resolved Hide resolved
sczembor marked this conversation as resolved.
Show resolved Hide resolved

db, err := dal.NewDB(config.DBFile)
if err != nil {
log.Error().Err(err).Msg("Error creating database")
os.Exit(1)
}
if err := db.InitDB(); err != nil {
log.Error().Err(err).Msg("Error initializing database")
os.Exit(1)
}

suiClient := sui.NewSuiClient(config.IkaRPC).(*sui.Client)
if suiClient == nil {
log.Error().Err(err).Msg("Error creating Sui client")
os.Exit(1)
}
ikaSigner, err := signer.NewSignertWithMnemonic(config.IkaSignerMnemonic)
sczembor marked this conversation as resolved.
Show resolved Hide resolved
sczembor marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Error().Err(err).Msg("Error creating signer with mnemonic")
os.Exit(1)
}

ikaClient, err := ika.NewClient(
suiClient,
ikaSigner,
ika.SuiCtrCall{
Package: config.IkaNativeLcPackage,
Module: config.IkaNativeLcModule,
Function: config.IkaNativeLcFunction,
},
ika.SuiCtrCall{
Package: config.IkaNativeLcPackage,
Module: config.IkaNativeLcModule,
Function: config.IkaNativeLcFunction,
},
config.IkaGasAcc,
config.IkaGasBudget,
)
if err != nil {
log.Error().Err(err).Msg("Error creating IKA client")
os.Exit(1)
}

btcProcessor, err := ika2btc.NewProcessor(
rpcclient.ConnConfig{
sczembor marked this conversation as resolved.
Show resolved Hide resolved
Host: config.BtcRPCHost,
User: config.BtcRPCUser,
Pass: config.BtcRPCPass,
HTTPPostMode: config.HTTPPostMode,
DisableTLS: config.DisableTLS,
},
config.BtcConfirmationThreshold,
db,
)
sczembor marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Error().Err(err).Msg("Error creating Bitcoin processor")
os.Exit(1)
}
btcProcessor.BtcClient = &bitcoin.MockClient{}

nativeProcessor := native2ika.NewProcessor(ikaClient, db)

relayer, err := nbtc.NewRelayer(
sczembor marked this conversation as resolved.
Show resolved Hide resolved
nbtc.RelayerConfig{
ProcessTxsInterval: config.ProcessTxsInterval,
ConfirmTxsInterval: config.ConfirmTxsInterval,
ConfirmationThreshold: config.BtcConfirmationThreshold,
},
db,
nativeProcessor,
btcProcessor,

Check failure on line 103 in cmd/native-relayer/cli/start.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

not enough arguments in call to nbtc.NewRelayer

Check failure on line 103 in cmd/native-relayer/cli/start.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

not enough arguments in call to nbtc.NewRelayer

Check failure on line 103 in cmd/native-relayer/cli/start.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

not enough arguments in call to nbtc.NewRelayer
)

Check failure on line 104 in cmd/native-relayer/cli/start.go

View workflow job for this annotation

GitHub Actions / Run govulncheck

not enough arguments in call to nbtc.NewRelayer
if err != nil {
log.Error().Err(err).Msg("Error creating relayer")
os.Exit(1)
}

relayerWg.Add(1)
go func() {
defer relayerWg.Done()
if err := relayer.Start(context.Background()); err != nil {
sczembor marked this conversation as resolved.
Show resolved Hide resolved
log.Error().Err(err).Msg("Relayer encountered an error")
}
}()

relayerInstance = relayer

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

<-c
log.Info().Msg("Stopping the relayer...")
relayerInstance.Stop()
relayerWg.Wait()
log.Info().Msg("Relayer stopped.")
},
}
23 changes: 23 additions & 0 deletions cmd/native-relayer/cli/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cli

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stops the relayer",
Run: func(cmd *cobra.Command, args []string) {
if relayerInstance == nil {
log.Info().Msg("Relayer is not running.")
return
}
log.Info().Msg("Stopping the relayer...")
relayerInstance.Stop()
relayerWg.Wait()
relayerInstance = nil
log.Info().Msg("Relayer stopped.")

},
}
sczembor marked this conversation as resolved.
Show resolved Hide resolved
40 changes: 40 additions & 0 deletions cmd/native-relayer/cli/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cli

import (
"flag"
"fmt"

"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

func loadConfig() (*Config, error) {
var configFile string
flag.StringVar(&configFile, "config", "", "Path to the config file")
flag.Parse()

viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.native-relayer")
sczembor marked this conversation as resolved.
Show resolved Hide resolved

if configFile != "" {
viper.SetConfigFile(configFile)
}
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return nil, fmt.Errorf("error reading config file: %w", err)
}
}

var config Config
if err := viper.Unmarshal(&config); err != nil {
return nil, fmt.Errorf("error unmarshalling config: %w", err)
}

log.Info().Msg("Loaded Configuration:")
log.Info().Interface("config", config).Msg("")

return &config, nil
}
29 changes: 29 additions & 0 deletions cmd/native-relayer/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Relayer Configuration

# Native
native_rpc: "127.0.0.1:26657"
native_grpc: "127.0.0.1:9090"
sczembor marked this conversation as resolved.
Show resolved Hide resolved

# IKA
ika_rpc: "127.0.0.1:0001"
ika_signer_mnemonic: ""
ika_native_lc_package: ""
ika_native_lc_module: ""
ika_native_lc_function: ""
ika_gas_acc: ""
ika_gas_budget: ""
sczembor marked this conversation as resolved.
Show resolved Hide resolved

# Bitcoin
btc_rpc_host: "your-bitcoin-rpc.com"
btc_rpc_user: "your-bitcoin-rpc-user"
btc_rpc_pass: "your-bitcoin-rpc-password"
btc_confirmation_threshold: 6
http_post_mode: true
disable_tls: false
sczembor marked this conversation as resolved.
Show resolved Hide resolved

# Relayer intervals in seconds
process_txs_interval: 5s
confirm_txs_interval: 7s

# Database
db_file: "relayer.db"
sczembor marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions cmd/native-relayer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/gonative-cc/relayer/cmd/native-relayer/cli"

Check failure on line 3 in cmd/native-relayer/main.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

could not import github.com/gonative-cc/relayer/cmd/native-relayer/cli (-: # github.com/gonative-cc/relayer/cmd/native-relayer/cli

func main() {
cli.Execute()
}
Loading