Skip to content

Commit

Permalink
fix: revert starting block changes (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Apr 15, 2024
1 parent cdfa2be commit 3babab3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
45 changes: 38 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"syscall"
"time"

"github.com/ChainSafe/sygma-relayer/chains"
"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"
Expand Down Expand Up @@ -153,7 +154,7 @@ func Run() error {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
chains := make(map[uint8]relayer.RelayedChain)
domains := make(map[uint8]relayer.RelayedChain)
for _, chainConfig := range configuration.ChainConfigs {
switch chainConfig["type"] {
case "evm":
Expand Down Expand Up @@ -217,9 +218,24 @@ func Run() error {
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())

chain := coreEvm.NewEVMChain(evmListener, mh, executor, *config.GeneralChainConfig.Id, config.StartBlock)
startBlock, err := blockstore.GetStartBlock(*config.GeneralChainConfig.Id, config.StartBlock, config.GeneralChainConfig.LatestBlock, config.GeneralChainConfig.FreshStart)
if err != nil {
panic(err)
}
if startBlock == nil {
head, err := client.LatestBlock()
if err != nil {
panic(err)
}
startBlock = head
}
startBlock, err = chains.CalculateStartingBlock(startBlock, config.BlockInterval)
if err != nil {
panic(err)
}
chain := coreEvm.NewEVMChain(evmListener, mh, executor, *config.GeneralChainConfig.Id, startBlock)

chains[*config.GeneralChainConfig.Id] = chain
domains[*config.GeneralChainConfig.Id] = chain
}
case "substrate":
{
Expand Down Expand Up @@ -255,9 +271,25 @@ func Run() error {
mh.RegisterMessageHandler(transfer.TransferMessageType, &substrateExecutor.SubstrateMessageHandler{})

sExecutor := substrateExecutor.NewExecutor(host, communication, coordinator, bridgePallet, keyshareStore, conn, exitLock)
substrateChain := coreSubstrate.NewSubstrateChain(substrateListener, mh, sExecutor, *config.GeneralChainConfig.Id, config.StartBlock)

chains[*config.GeneralChainConfig.Id] = substrateChain
startBlock, err := blockstore.GetStartBlock(*config.GeneralChainConfig.Id, config.StartBlock, config.GeneralChainConfig.LatestBlock, config.GeneralChainConfig.FreshStart)
if err != nil {
panic(err)
}
if startBlock == nil {
head, err := substrateClient.LatestBlock()
if err != nil {
panic(err)
}
startBlock = head
}
startBlock, err = chains.CalculateStartingBlock(startBlock, config.BlockInterval)
if err != nil {
panic(err)
}
substrateChain := coreSubstrate.NewSubstrateChain(substrateListener, mh, sExecutor, *config.GeneralChainConfig.Id, startBlock)

domains[*config.GeneralChainConfig.Id] = substrateChain
}
default:
panic(fmt.Errorf("type '%s' not recognized", chainConfig["type"]))
Expand All @@ -266,8 +298,7 @@ func Run() error {

go jobs.StartCommunicationHealthCheckJob(host, configuration.RelayerConfig.MpcConfig.CommHealthCheckInterval, sygmaMetrics)

r := relayer.NewRelayer(chains)

r := relayer.NewRelayer(domains)
go r.Start(ctx, msgChan)

sysErr := make(chan os.Signal, 1)
Expand Down
19 changes: 19 additions & 0 deletions chains/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// The Licensed Work is (c) 2022 Sygma
// SPDX-License-Identifier: LGPL-3.0-only

package chains

import (
"fmt"
"math/big"
)

// CalculateStartingBlock returns first block number (smaller or equal) that is dividable with block confirmations
func CalculateStartingBlock(startBlock *big.Int, blockConfirmations *big.Int) (*big.Int, error) {
if startBlock == nil || blockConfirmations == nil {
return nil, fmt.Errorf("startBlock or blockConfirmations can not be nill when calculating CalculateStartingBlock")
}
mod := big.NewInt(0).Mod(startBlock, blockConfirmations)
startBlock.Sub(startBlock, mod)
return startBlock, nil
}
37 changes: 37 additions & 0 deletions chains/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// The Licensed Work is (c) 2022 Sygma
// SPDX-License-Identifier: LGPL-3.0-only

package chains

import (
"math/big"
"testing"

"github.com/stretchr/testify/suite"
)

type UtilTestSuite struct {
suite.Suite
}

func TestRunNewEVMConfigTestSuite(t *testing.T) {
suite.Run(t, new(UtilTestSuite))
}

func (s *UtilTestSuite) Test_CalculateStartingBlock_ProperAdjustment() {
res, err := CalculateStartingBlock(big.NewInt(104), big.NewInt(5))
s.Equal(big.NewInt(100), res)
s.Nil(err)
}

func (s *UtilTestSuite) Test_CalculateStartingBlock_NoAdjustment() {
res, err := CalculateStartingBlock(big.NewInt(200), big.NewInt(5))
s.Equal(big.NewInt(200), res)
s.Nil(err)
}

func (s *UtilTestSuite) Test_CalculateStartingBlock_Nil() {
res, err := CalculateStartingBlock(nil, nil)
s.Nil(res)
s.NotNil(err)
}

0 comments on commit 3babab3

Please sign in to comment.