Skip to content

Commit

Permalink
chore: Config files and other minor refactors (#252)
Browse files Browse the repository at this point in the history
Co-authored-by: mj52951 <mj52951@fer.hr>
  • Loading branch information
mj52951 and mj52951 authored Mar 18, 2024
1 parent f4ca83d commit f870fbe
Show file tree
Hide file tree
Showing 23 changed files with 230 additions and 797 deletions.
65 changes: 31 additions & 34 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@ import (
"syscall"
"time"

"github.com/ChainSafe/chainbridge-core/chains/evm/calls/evmgaspricer"
"github.com/ChainSafe/chainbridge-core/crypto/secp256k1"
"github.com/ChainSafe/chainbridge-core/flags"
"github.com/ChainSafe/chainbridge-core/logger"
"github.com/ChainSafe/chainbridge-core/lvldb"
"github.com/ChainSafe/chainbridge-core/opentelemetry"
"github.com/ChainSafe/chainbridge-core/store"
"github.com/ChainSafe/sygma-relayer/chains/evm"
"github.com/ChainSafe/sygma-relayer/chains/evm/calls/contracts/bridge"
"github.com/ChainSafe/sygma-relayer/chains/evm/calls/events"
"github.com/ChainSafe/sygma-relayer/chains/evm/executor"
"github.com/ChainSafe/sygma-relayer/chains/evm/listener/depositHandlers"
hubEventHandlers "github.com/ChainSafe/sygma-relayer/chains/evm/listener/eventHandlers"
"github.com/ChainSafe/sygma-relayer/chains/substrate"
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/gas"
coreSubstrate "github.com/sygmaprotocol/sygma-core/chains/substrate"
"github.com/sygmaprotocol/sygma-core/crypto/secp256k1"
"github.com/sygmaprotocol/sygma-core/observability"
"github.com/sygmaprotocol/sygma-core/relayer"
"github.com/sygmaprotocol/sygma-core/relayer/message"
"github.com/sygmaprotocol/sygma-core/store"
"github.com/sygmaprotocol/sygma-core/store/lvldb"

substrateExecutor "github.com/ChainSafe/sygma-relayer/chains/substrate/executor"
substrate_listener "github.com/ChainSafe/sygma-relayer/chains/substrate/listener"
substrate_pallet "github.com/ChainSafe/sygma-relayer/chains/substrate/pallet"
substrateListener "github.com/ChainSafe/sygma-relayer/chains/substrate/listener"
substratePallet "github.com/ChainSafe/sygma-relayer/chains/substrate/pallet"
"github.com/ChainSafe/sygma-relayer/metrics"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
coreEvm "github.com/sygmaprotocol/sygma-core/chains/evm"
coreClient "github.com/sygmaprotocol/sygma-core/chains/evm/client"
evmClient "github.com/sygmaprotocol/sygma-core/chains/evm/client"
"github.com/sygmaprotocol/sygma-core/chains/evm/listener"
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/monitored"
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/transaction"
"github.com/sygmaprotocol/sygma-core/chains/substrate/client"
substrateClient "github.com/sygmaprotocol/sygma-core/chains/substrate/client"
"github.com/sygmaprotocol/sygma-core/chains/substrate/connection"
core_substrate_listener "github.com/sygmaprotocol/sygma-core/chains/substrate/listener"
coreSubstrateListener "github.com/sygmaprotocol/sygma-core/chains/substrate/listener"

"github.com/ChainSafe/sygma-relayer/comm/elector"
"github.com/ChainSafe/sygma-relayer/comm/p2p"
Expand All @@ -56,17 +56,14 @@ import (
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"

"github.com/sygmaprotocol/sygma-core/relayer"
coreMessage "github.com/sygmaprotocol/sygma-core/relayer/message"
)

var Version string

func Run() error {
var err error

configFlag := viper.GetString(flags.ConfigFlagName)
configFlag := viper.GetString(config.ConfigFlagName)
configURL := viper.GetString("config-url")

configuration := &config.Config{}
Expand All @@ -83,7 +80,7 @@ func Run() error {
panicOnError(err)
}

logger.ConfigureLogger(configuration.RelayerConfig.LogLevel, os.Stdout)
observability.ConfigureLogger(configuration.RelayerConfig.LogLevel, os.Stdout)

log.Info().Msg("Successfully loaded configuration")

Expand Down Expand Up @@ -123,7 +120,7 @@ func Run() error {
// effectively it waits until old instance is killed
var db *lvldb.LVLDB
for {
db, err = lvldb.NewLvlDB(viper.GetString(flags.BlockstoreFlagName))
db, err = lvldb.NewLvlDB(viper.GetString(config.BlockstoreFlagName))
if err != nil {
log.Error().Err(err).Msg("Unable to connect to blockstore file, retry in 10 seconds")
time.Sleep(10 * time.Second)
Expand All @@ -138,7 +135,7 @@ func Run() error {
exitLock := &sync.RWMutex{}
defer exitLock.Lock()

mp, err := opentelemetry.InitMetricProvider(context.Background(), configuration.RelayerConfig.OpenTelemetryCollectorURL)
mp, err := observability.InitMetricProvider(context.Background(), configuration.RelayerConfig.OpenTelemetryCollectorURL)
if err != nil {
panic(err)
}
Expand All @@ -164,13 +161,13 @@ func Run() error {
kp, err := secp256k1.NewKeypairFromString(config.GeneralChainConfig.Key)
panicOnError(err)

client, err := coreClient.NewEVMClient(config.GeneralChainConfig.Endpoint, kp)
client, err := evmClient.NewEVMClient(config.GeneralChainConfig.Endpoint, kp)
panicOnError(err)

log.Info().Str("domain", config.String()).Msgf("Registering EVM domain")

bridgeAddress := common.HexToAddress(config.Bridge)
gasPricer := evmgaspricer.NewLondonGasPriceClient(client, &evmgaspricer.GasPricerOpts{
gasPricer := gas.NewLondonGasPriceClient(client, &gas.GasPricerOpts{
UpperLimitFeePerGas: config.MaxGasPrice,
GasPriceFactor: config.GasMultiplier,
})
Expand All @@ -179,7 +176,7 @@ func Run() error {
bridgeContract := bridge.NewBridgeContract(client, bridgeAddress, t)

depositHandler := depositHandlers.NewETHDepositHandler(bridgeContract)
mh := coreMessage.NewMessageHandler()
mh := message.NewMessageHandler()
for _, handler := range config.Handlers {

mh.RegisterMessageHandler("Transfer", &executor.TransferMessageHandler{})
Expand Down Expand Up @@ -207,10 +204,10 @@ func Run() error {
tssListener := events.NewListener(client)
eventHandlers := make([]listener.EventHandler, 0)
l := log.With().Str("chain", fmt.Sprintf("%v", config.GeneralChainConfig.Name)).Uint8("domainID", *config.GeneralChainConfig.Id)
eventHandlers = append(eventHandlers, hubEventHandlers.NewDepositEventHandler(depositListener, depositHandler, bridgeAddress, *config.GeneralChainConfig.Id, make(chan []*coreMessage.Message, 1)))
eventHandlers = append(eventHandlers, hubEventHandlers.NewDepositEventHandler(depositListener, depositHandler, bridgeAddress, *config.GeneralChainConfig.Id, make(chan []*message.Message, 1)))
eventHandlers = append(eventHandlers, hubEventHandlers.NewKeygenEventHandler(l, tssListener, coordinator, host, communication, keyshareStore, bridgeAddress, networkTopology.Threshold))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRefreshEventHandler(l, topologyProvider, topologyStore, tssListener, coordinator, host, communication, connectionGate, keyshareStore, bridgeAddress))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRetryEventHandler(l, tssListener, depositHandler, bridgeAddress, *config.GeneralChainConfig.Id, config.BlockConfirmations, make(chan []*coreMessage.Message, 1)))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRetryEventHandler(l, tssListener, depositHandler, bridgeAddress, *config.GeneralChainConfig.Id, config.BlockConfirmations, make(chan []*message.Message, 1)))
evmListener := listener.NewEVMListener(client, eventHandlers, blockstore, sygmaMetrics, *config.GeneralChainConfig.Id, config.BlockRetryInterval, config.BlockConfirmations, config.BlockInterval)
executor := executor.NewExecutor(host, communication, coordinator, bridgeContract, keyshareStore, exitLock, config.GasLimit.Uint64())

Expand All @@ -234,21 +231,21 @@ func Run() error {
panic(err)
}

substrateClient := client.NewSubstrateClient(conn, &keyPair, config.ChainID, config.Tip)
bridgePallet := substrate_pallet.NewPallet(substrateClient)
substrateClient := substrateClient.NewSubstrateClient(conn, &keyPair, config.ChainID, config.Tip)
bridgePallet := substratePallet.NewPallet(substrateClient)

log.Info().Str("domain", config.String()).Msgf("Registering substrate domain")

l := log.With().Str("chain", fmt.Sprintf("%v", config.GeneralChainConfig.Name)).Uint8("domainID", *config.GeneralChainConfig.Id)
depositHandler := substrate_listener.NewSubstrateDepositHandler()
depositHandler.RegisterDepositHandler(substrate.FungibleTransfer, substrate_listener.FungibleTransferHandler)
eventHandlers := make([]core_substrate_listener.EventHandler, 0)
eventHandlers = append(eventHandlers, substrate_listener.NewFungibleTransferEventHandler(l, *config.GeneralChainConfig.Id, depositHandler, make(chan []*coreMessage.Message, 1), conn))
eventHandlers = append(eventHandlers, substrate_listener.NewRetryEventHandler(l, conn, depositHandler, *config.GeneralChainConfig.Id, make(chan []*coreMessage.Message, 1)))
depositHandler := substrateListener.NewSubstrateDepositHandler()
depositHandler.RegisterDepositHandler(substrate.FungibleTransfer, substrateListener.FungibleTransferHandler)
eventHandlers := make([]coreSubstrateListener.EventHandler, 0)
eventHandlers = append(eventHandlers, substrateListener.NewFungibleTransferEventHandler(l, *config.GeneralChainConfig.Id, depositHandler, make(chan []*message.Message, 1), conn))
eventHandlers = append(eventHandlers, substrateListener.NewRetryEventHandler(l, conn, depositHandler, *config.GeneralChainConfig.Id, make(chan []*message.Message, 1)))

substrateListener := core_substrate_listener.NewSubstrateListener(conn, eventHandlers, blockstore, sygmaMetrics, *config.GeneralChainConfig.Id, config.BlockRetryInterval, config.BlockInterval)
substrateListener := coreSubstrateListener.NewSubstrateListener(conn, eventHandlers, blockstore, sygmaMetrics, *config.GeneralChainConfig.Id, config.BlockRetryInterval, config.BlockInterval)

mh := coreMessage.NewMessageHandler()
mh := message.NewMessageHandler()
mh.RegisterMessageHandler(substrate.FungibleTransfer, &substrateExecutor.SubstrateMessageHandler{})

sExecutor := substrateExecutor.NewExecutor(host, communication, coordinator, bridgePallet, keyshareStore, conn, exitLock)
Expand All @@ -265,7 +262,7 @@ func Run() error {

r := relayer.NewRelayer(chains)

msgChan := make(chan []*coreMessage.Message)
msgChan := make(chan []*message.Message)
go r.Start(ctx, msgChan)

sysErr := make(chan os.Signal, 1)
Expand Down
Loading

0 comments on commit f870fbe

Please sign in to comment.