-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into udit/spv-e2e
- Loading branch information
Showing
9 changed files
with
390 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cli | ||
|
||
import "time" | ||
|
||
// Configuration struct | ||
type Config struct { | ||
Native NativeCfg `mapstructure:"native"` | ||
Ika IkaCfg `mapstructure:"ika"` | ||
Btc BitcoinCfg `mapstructure:"bitcoin"` | ||
Relayer RelayerCfg `mapstructure:"relayer"` | ||
DB DBCfg `mapstructure:"db"` | ||
} | ||
|
||
type NativeCfg struct { | ||
RPC string `mapstructure:"rpc"` | ||
GRPC string `mapstructure:"grpc"` | ||
} | ||
|
||
type IkaCfg struct { | ||
RPC string `mapstructure:"rpc"` | ||
SignerMnemonic string `mapstructure:"signer_mnemonic"` | ||
NativeLcPackage string `mapstructure:"native_lc_package"` | ||
NativeLcModule string `mapstructure:"native_lc_module"` | ||
NativeLcFunction string `mapstructure:"native_lc_function"` | ||
GasAcc string `mapstructure:"gas_acc"` | ||
GasBudget string `mapstructure:"gas_budget"` | ||
} | ||
|
||
type BitcoinCfg struct { | ||
RPCHost string `mapstructure:"rpc_host"` | ||
RPCUser string `mapstructure:"rpc_user"` | ||
RPCPass string `mapstructure:"rpc_pass"` | ||
ConfirmationThreshold uint8 `mapstructure:"confirmation_threshold"` | ||
HTTPPostMode bool `mapstructure:"http_post_mode"` | ||
DisableTLS bool `mapstructure:"disable_tls"` | ||
Network string `mapstructure:"network"` | ||
} | ||
|
||
type RelayerCfg struct { | ||
ProcessTxsInterval time.Duration `mapstructure:"process_txs_interval"` | ||
ConfirmTxsInterval time.Duration `mapstructure:"confirm_txs_interval"` | ||
SignReqFetchInterval time.Duration `mapstructure:"sign_req_fetch_interval"` | ||
SignReqFetchFrom int `mapstructure:"sign_req_fetch_from"` | ||
SignReqFetchLimit int `mapstructure:"sign_req_fetch_limit"` | ||
} | ||
|
||
type DBCfg struct { | ||
File string `mapstructure:"file"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cli | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
configFile string | ||
logLevel string | ||
|
||
rootCmd = &cobra.Command{ | ||
Use: "relayer-cli", | ||
Short: "CLI tool for managing the relayer", | ||
} | ||
) | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
log.Error().Err(err).Msg("CLI execution failed") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "Path to the config file") | ||
rootCmd.PersistentFlags().StringVar( | ||
&logLevel, "log-level", | ||
"", | ||
"Set the log level (trace, debug, info, warn, error, fatal, panic)", | ||
) | ||
rootCmd.AddCommand(startCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"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/dal" | ||
"github.com/gonative-cc/relayer/env" | ||
"github.com/gonative-cc/relayer/ika" | ||
"github.com/gonative-cc/relayer/ika2btc" | ||
"github.com/gonative-cc/relayer/native" | ||
"github.com/gonative-cc/relayer/native2ika" | ||
"github.com/gonative-cc/relayer/nbtc" | ||
"github.com/rs/zerolog" | ||
"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 := prepareEnv(cmd) | ||
if err != nil { | ||
log.Error().Err(err).Msg("Failed to prepare environment") | ||
os.Exit(1) | ||
} | ||
db, err := initDatabase(config.DB) | ||
if err != nil { | ||
log.Error().Err(err).Msg("Failed to initialize database") | ||
os.Exit(1) | ||
} | ||
nativeProcessor, err := createNativeProcessor(config.Ika, db) | ||
if err != nil { | ||
log.Error().Err(err).Msg("Failed to create Native Processor") | ||
os.Exit(1) | ||
} | ||
btcProcessor, err := createBTCProcessor(config.Btc, db) | ||
if err != nil { | ||
log.Error().Err(err).Msg("Failed to create Bitcoin processor") | ||
os.Exit(1) | ||
} | ||
fetcher, err := createSignReqFetcher() | ||
if err != nil { | ||
log.Error().Err(err).Msg("Failed to create SignReq fetcher ") | ||
os.Exit(1) | ||
} | ||
relayer, err := createRelayer( | ||
config.Relayer, | ||
db, | ||
nativeProcessor, | ||
btcProcessor, | ||
fetcher, | ||
) | ||
if err != nil { | ||
log.Error().Err(err).Msg("Failed to create relayer") | ||
os.Exit(1) | ||
} | ||
// We need it to ensure the relayer actually stops before displaying `realyer stopped` and exiting. | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
if err := relayer.Start(cmd.Context()); err != nil { | ||
log.Error().Err(err).Msg("Relayer encountered an error") | ||
} | ||
}() | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
|
||
<-c | ||
log.Info().Msg("Stopping the relayer...") | ||
relayer.Stop() | ||
wg.Wait() | ||
log.Info().Msg("Relayer stopped.") | ||
}, | ||
} | ||
|
||
func prepareEnv(cmd *cobra.Command) (*Config, error) { | ||
flags := cmd.Root().PersistentFlags() | ||
lvl, err := flags.GetString("log-level") | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting log level: %w", err) | ||
} | ||
logLvl, err := zerolog.ParseLevel(lvl) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing log level: %w", err) | ||
} | ||
env.InitLogger(logLvl) | ||
configFile, err := flags.GetString("config") | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting config file path: %w", err) | ||
} | ||
config, err := loadConfig(configFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("error loading config: %w", err) | ||
} | ||
return config, nil | ||
} | ||
|
||
func initDatabase(cfg DBCfg) (*dal.DB, error) { | ||
db, err := dal.NewDB(cfg.File) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating database: %w", err) | ||
} | ||
if err := db.InitDB(); err != nil { | ||
return nil, fmt.Errorf("error initializing database: %w", err) | ||
} | ||
return db, nil | ||
} | ||
|
||
func createNativeProcessor(cfg IkaCfg, db *dal.DB) (*native2ika.Processor, error) { | ||
suiClient := sui.NewSuiClient(cfg.RPC).(*sui.Client) | ||
if suiClient == nil { | ||
return nil, fmt.Errorf("error creating Sui client") | ||
} | ||
signer, err := signer.NewSignertWithMnemonic(cfg.SignerMnemonic) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating signer with mnemonic: %w", err) | ||
} | ||
client, err := ika.NewClient( | ||
suiClient, | ||
signer, | ||
ika.SuiCtrCall{ | ||
Package: cfg.NativeLcPackage, | ||
Module: cfg.NativeLcModule, | ||
Function: cfg.NativeLcFunction, | ||
}, | ||
ika.SuiCtrCall{ | ||
Package: cfg.NativeLcPackage, | ||
Module: cfg.NativeLcModule, | ||
Function: cfg.NativeLcFunction, | ||
}, | ||
cfg.GasAcc, | ||
cfg.GasBudget, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating Ika client: %w", err) | ||
} | ||
nativeProcessor := native2ika.NewProcessor(client, db) | ||
return nativeProcessor, nil | ||
} | ||
func createBTCProcessor(btcCfg BitcoinCfg, db *dal.DB) (*ika2btc.Processor, error) { | ||
btcProcessor, err := ika2btc.NewProcessor(rpcclient.ConnConfig{ | ||
Host: btcCfg.RPCHost, | ||
User: btcCfg.RPCUser, | ||
Pass: btcCfg.RPCPass, | ||
HTTPPostMode: btcCfg.HTTPPostMode, | ||
DisableTLS: btcCfg.DisableTLS, | ||
Params: btcCfg.Network, | ||
}, btcCfg.ConfirmationThreshold, db) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating Bitcoin processor: %w", err) | ||
} | ||
return btcProcessor, nil | ||
} | ||
|
||
func createSignReqFetcher() (*native.APISignRequestFetcher, error) { | ||
// TODO: replace with the the real endpoint once available | ||
return native.NewMockAPISignRequestFetcher() | ||
} | ||
|
||
func createRelayer( | ||
relayerCfg RelayerCfg, | ||
db *dal.DB, | ||
nativeProcessor *native2ika.Processor, | ||
btcProcessor *ika2btc.Processor, | ||
fetcher *native.APISignRequestFetcher, | ||
) (*nbtc.Relayer, error) { | ||
relayer, err := nbtc.NewRelayer( | ||
nbtc.RelayerConfig(relayerCfg), | ||
db, | ||
nativeProcessor, | ||
btcProcessor, | ||
fetcher, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating relayer: %w", err) | ||
} | ||
return relayer, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func loadConfig(configFile string) (*Config, error) { | ||
viper.SetConfigName("config") | ||
viper.SetConfigType("yaml") | ||
viper.AddConfigPath(".") | ||
if configFile != "" { | ||
viper.SetConfigFile(configFile) | ||
} | ||
if err := viper.ReadInConfig(); err != nil { | ||
if _, ok := err.(viper.ConfigFileNotFoundError); ok { | ||
return nil, fmt.Errorf("config file not found: %w", err) | ||
} | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cli | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
// temporary file with test config | ||
tempFile, err := os.CreateTemp("", "config_test.yaml") | ||
assert.NilError(t, err) | ||
defer os.Remove(tempFile.Name()) | ||
|
||
configYAML := ` | ||
bitcoin: | ||
rpc_host: "test-rpc-host" | ||
rpc_user: "test-rpc-user" | ||
rpc_pass: "test-rpc-pass" | ||
confirmation_threshold: 3 | ||
http_post_mode: true | ||
disable_tls: false | ||
network: "regtest" | ||
` | ||
_, err = tempFile.WriteString(configYAML) | ||
assert.NilError(t, err) | ||
|
||
config, err := loadConfig(tempFile.Name()) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "test-rpc-host", config.Btc.RPCHost) | ||
assert.Equal(t, "test-rpc-user", config.Btc.RPCUser) | ||
assert.Equal(t, "test-rpc-pass", config.Btc.RPCPass) | ||
assert.Equal(t, uint8(3), config.Btc.ConfirmationThreshold) | ||
assert.Equal(t, true, config.Btc.HTTPPostMode) | ||
assert.Equal(t, false, config.Btc.DisableTLS) | ||
assert.Equal(t, "regtest", config.Btc.Network) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
func main() { | ||
cli.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Relayer Configuration | ||
|
||
native: | ||
native_rpc: "127.0.0.1:26657" | ||
native_grpc: "127.0.0.1:9090" | ||
|
||
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: "" | ||
|
||
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 | ||
network: "testnet" | ||
|
||
relayer: | ||
process_txs_interval: 5s | ||
confirm_txs_interval: 7s | ||
sign_req_fetch_interval: 5s | ||
sign_req_fetch_from: 0 | ||
sign_req_fetch_limit: 5 | ||
|
||
db: | ||
db_file: "relayer.db" |
Oops, something went wrong.