Skip to content

Commit

Permalink
feat: metrics (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Oct 29, 2024
1 parent f997da2 commit 03ec27c
Show file tree
Hide file tree
Showing 40 changed files with 368 additions and 363 deletions.
16 changes: 7 additions & 9 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,16 @@ func Run() error {
log.Error().Msgf("Error shutting down meter provider: %v", err)
}
}()
sygmaMetrics, err := metrics.NewSygmaMetrics(mp.Meter("relayer-metric-provider"), configuration.RelayerConfig.Env, configuration.RelayerConfig.Id)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

sygmaMetrics, err := metrics.NewSygmaMetrics(ctx, mp.Meter("relayer-metric-provider"), configuration.RelayerConfig.Env, configuration.RelayerConfig.Id, Version)
if err != nil {
panic(err)
}
msgChan := make(chan []*message.Message)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
domains := make(map[uint8]relayer.RelayedChain)
for _, chainConfig := range configuration.ChainConfigs {
switch chainConfig["type"] {
Expand All @@ -185,7 +187,7 @@ func Run() error {
UpperLimitFeePerGas: config.MaxGasPrice,
GasPriceFactor: config.GasMultiplier,
})
t := monitored.NewMonitoredTransactor(transaction.NewTransaction, gasPricer, client, config.MaxGasPrice, config.GasIncreasePercentage)
t := monitored.NewMonitoredTransactor(*config.GeneralChainConfig.Id, transaction.NewTransaction, gasPricer, sygmaMetrics, client, config.MaxGasPrice, config.GasIncreasePercentage)
go t.Monitor(ctx, time.Minute*3, time.Minute*10, time.Minute)
bridgeContract := bridge.NewBridgeContract(client, bridgeAddress, t)

Expand All @@ -200,10 +202,6 @@ func Run() error {
{
depositHandler.RegisterDepositHandler(handler.Address, &depositHandlers.Erc20DepositHandler{})
}
case "permissionedGeneric":
{
depositHandler.RegisterDepositHandler(handler.Address, &depositHandlers.GenericDepositHandler{})
}
case "permissionlessGeneric":
{
depositHandler.RegisterDepositHandler(handler.Address, &depositHandlers.PermissionlessGenericDepositHandler{})
Expand Down Expand Up @@ -358,7 +356,7 @@ func Run() error {

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

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

sysErr := make(chan os.Signal, 1)
Expand Down
7 changes: 6 additions & 1 deletion chains/btc/listener/deposit-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/big"
"strconv"
"strings"
"time"

"github.com/ChainSafe/sygma-relayer/relayer/transfer"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -29,6 +30,7 @@ func (e *BtcDepositHandler) HandleDeposit(
amount *big.Int,
data string,
blockNumber *big.Int,
timestamp time.Time,
) (*message.Message, error) {
// data is composed of recieverEVMAddress_destinationDomainID
parsedData := strings.Split(data, "_")
Expand All @@ -54,5 +56,8 @@ func (e *BtcDepositHandler) HandleDeposit(
Metadata: nil,
Payload: payload,
Type: transfer.FungibleTransfer,
}, messageID, transfer.TransferMessageType), nil
},
messageID,
transfer.TransferMessageType,
timestamp), nil
}
11 changes: 7 additions & 4 deletions chains/btc/listener/deposit-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package listener_test
import (
"fmt"
"strings"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/sygmaprotocol/sygma-core/relayer/message"
Expand Down Expand Up @@ -43,6 +44,7 @@ func (s *Erc20HandlerTestSuite) Test_Erc20HandleEvent() {
evmAdd := common.HexToAddress(dat[0]).Bytes()
messageID := fmt.Sprintf("%d-%d-%d", sourceID, 1, blockNumber)

timestamp := time.Now()
expected := &message.Message{
Source: sourceID,
Destination: uint8(1),
Expand All @@ -55,12 +57,13 @@ func (s *Erc20HandlerTestSuite) Test_Erc20HandleEvent() {
},
Type: transfer.FungibleTransfer,
},
Type: transfer.TransferMessageType,
ID: messageID,
Type: transfer.TransferMessageType,
ID: messageID,
Timestamp: timestamp,
}

btcDepositHandler := listener.NewBtcDepositHandler()
message, err := btcDepositHandler.HandleDeposit(sourceID, depositNonce, deposit.ResourceID, deposit.Amount, deposit.Data, blockNumber)
message, err := btcDepositHandler.HandleDeposit(sourceID, depositNonce, deposit.ResourceID, deposit.Amount, deposit.Data, blockNumber, timestamp)

s.Nil(err)
s.NotNil(message)
Expand All @@ -82,7 +85,7 @@ func (s *Erc20HandlerTestSuite) Test_Erc20HandleEvent_InvalidDestinationDomainID
depositNonce := uint64(1)

btcDepositHandler := listener.NewBtcDepositHandler()
message, err := btcDepositHandler.HandleDeposit(sourceID, depositNonce, deposit.ResourceID, deposit.Amount, deposit.Data, blockNumber)
message, err := btcDepositHandler.HandleDeposit(sourceID, depositNonce, deposit.ResourceID, deposit.Amount, deposit.Data, blockNumber, time.Now())

s.Nil(message)
s.NotNil(err)
Expand Down
4 changes: 3 additions & 1 deletion chains/btc/listener/event-handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/sha256"
"encoding/binary"
"math/big"
"time"

"github.com/ChainSafe/sygma-relayer/chains/btc/config"
"github.com/btcsuite/btcd/btcjson"
Expand Down Expand Up @@ -34,6 +35,7 @@ type DepositHandler interface {
amount *big.Int,
data string,
blockNumber *big.Int,
timestamp time.Time,
) (*message.Message, error)
}

Expand Down Expand Up @@ -87,7 +89,7 @@ func (eh *FungibleTransferEventHandler) HandleEvents(blockNumber *big.Int) error
return err
}

m, err := eh.depositHandler.HandleDeposit(eh.domainID, nonce, d.ResourceID, d.Amount, d.Data, blockNumber)
m, err := eh.depositHandler.HandleDeposit(eh.domainID, nonce, d.ResourceID, d.Amount, d.Data, blockNumber, time.Unix(evt.Blocktime, 0))
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions chains/btc/listener/event-handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (s *DepositHandlerTestSuite) Test_HandleDepositFails_ExecutionContinue() {
data2["amount"],
data2["deposit_data"],
blockNumber,
gomock.Any(),
).Return(&message.Message{
Source: s.domainID,
Destination: uint8(1),
Expand Down
9 changes: 5 additions & 4 deletions chains/btc/listener/mock/handlers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions chains/evm/calls/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package events

import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
Expand Down Expand Up @@ -50,4 +52,6 @@ type Deposit struct {
// ERC721Handler: responds with deposited token metadata acquired by calling a tokenURI method in the token contract
// GenericHandler: responds with the raw bytes returned from the call to the target contract
HandlerResponse []byte
// Timestamp is the timestamp of the block that the deposit event is in
Timestamp time.Time
}
33 changes: 21 additions & 12 deletions chains/evm/calls/events/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"math/big"
"strings"
"time"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -21,6 +22,7 @@ type ChainClient interface {
FetchEventLogs(ctx context.Context, contractAddress common.Address, event string, startBlock *big.Int, endBlock *big.Int) ([]ethTypes.Log, error)
WaitAndReturnTxReceipt(h common.Hash) (*ethTypes.Receipt, error)
LatestBlock() (*big.Int, error)
BlockByNumber(ctx context.Context, number *big.Int) (*ethTypes.Block, error)
}

type Listener struct {
Expand All @@ -44,30 +46,36 @@ func (l *Listener) FetchDeposits(ctx context.Context, contractAddress common.Add
deposits := make([]*Deposit, 0)

for _, dl := range logs {
d, err := l.unpackDeposit(l.abi, dl.Data)
d, err := l.parseDeposit(ctx, dl)
if err != nil {
log.Error().Msgf("failed unpacking deposit event log: %v", err)
continue
}

d.SenderAddress = common.BytesToAddress(dl.Topics[1].Bytes())
log.Debug().Msgf("Found deposit log in block: %d, TxHash: %s, contractAddress: %s, sender: %s", dl.BlockNumber, dl.TxHash, dl.Address, d.SenderAddress)

deposits = append(deposits, d)
}

return deposits, nil
}

func (l *Listener) unpackDeposit(abi abi.ABI, data []byte) (*Deposit, error) {
var dl Deposit

err := abi.UnpackIntoInterface(&dl, "Deposit", data)
func (l *Listener) parseDeposit(ctx context.Context, dl ethTypes.Log) (*Deposit, error) {
var d Deposit
err := l.abi.UnpackIntoInterface(&d, "Deposit", dl.Data)
if err != nil {
return &Deposit{}, err
}

return &dl, nil
d.SenderAddress = common.BytesToAddress(dl.Topics[1].Bytes())
block, err := l.client.BlockByNumber(ctx, new(big.Int).SetUint64(dl.BlockNumber))
if err == nil {
log.Warn().Msgf("Failed fetching block with number %d because of: %+v", dl.BlockNumber, err)
d.Timestamp = time.Unix(int64(block.Time()), 0)
} else {
d.Timestamp = time.Now()
}

return &d, nil
}

func (l *Listener) FetchRetryDepositEvents(event RetryEvent, bridgeAddress common.Address, blockConfirmations *big.Int) ([]Deposit, error) {
Expand Down Expand Up @@ -96,11 +104,12 @@ func (l *Listener) FetchRetryDepositEvents(event RetryEvent, bridgeAddress commo
continue
}

var depositEvent Deposit
err := l.abi.UnpackIntoInterface(&depositEvent, "Deposit", lg.Data)
if err == nil {
depositEvents = append(depositEvents, depositEvent)
d, err := l.parseDeposit(context.Background(), *lg)
if err != nil {
log.Error().Msgf("failed unpacking deposit event log: %v", err)
continue
}
depositEvents = append(depositEvents, *d)
}

return depositEvents, nil
Expand Down
11 changes: 11 additions & 0 deletions chains/evm/calls/events/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,25 @@ func (s *ListenerTestSuite) Test_FetchRetryDepositEvents_ValidEvent() {
{
Address: common.HexToAddress("0x1ec6b294902d42fee964d29fa962e5976e71e67d"),
Data: depositEvent,
Topics: []common.Hash{
{},
{},
},
BlockNumber: 14,
},
{
Address: common.HexToAddress("0x5798e01f4b1d8f6a5d91167414f3a915d021bc4a"),
Data: depositEvent,
Topics: []common.Hash{
{},
{},
},
BlockNumber: 14,
},
},
}, nil)
s.mockClient.EXPECT().LatestBlock().Return(big.NewInt(20), nil)
s.mockClient.EXPECT().BlockByNumber(gomock.Any(), big.NewInt(14)).Return(nil, fmt.Errorf("error"))

deposits, err := s.listener.FetchRetryDepositEvents(
events.RetryEvent{TxHash: "0xf25ed4a14bf7ad20354b46fe38d7d4525f2ea3042db9a9954ef8d73c558b500c"},
Expand Down
15 changes: 15 additions & 0 deletions chains/evm/calls/events/mock/listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions chains/evm/listener/depositHandlers/deposit-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package depositHandlers

import (
"errors"
"time"

"github.com/ChainSafe/sygma-relayer/chains/evm/listener/eventHandlers"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -38,7 +39,8 @@ func (e *ETHDepositHandler) HandleDeposit(
resourceID [32]byte,
calldata,
handlerResponse []byte,
messageID string) (*message.Message, error) {
messageID string,
timestamp time.Time) (*message.Message, error) {
handlerAddr, err := e.handlerMatcher.GetHandlerAddressForResourceID(resourceID)
if err != nil {
return nil, err
Expand All @@ -49,7 +51,7 @@ func (e *ETHDepositHandler) HandleDeposit(
return nil, err
}

return depositHandler.HandleDeposit(sourceID, destID, depositNonce, resourceID, calldata, handlerResponse, messageID)
return depositHandler.HandleDeposit(sourceID, destID, depositNonce, resourceID, calldata, handlerResponse, messageID, timestamp)
}

// matchAddressWithHandlerFunc matches a handler address with an associated handler function
Expand Down
9 changes: 7 additions & 2 deletions chains/evm/listener/depositHandlers/erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package depositHandlers

import (
"time"

"github.com/ChainSafe/sygma-relayer/relayer/transfer"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/sygmaprotocol/sygma-core/relayer/message"
Expand All @@ -17,7 +19,8 @@ func (dh *Erc1155DepositHandler) HandleDeposit(
nonce uint64,
resourceID [32]byte,
calldata, handlerResponse []byte,
messageID string) (*message.Message, error) {
messageID string,
timestamp time.Time) (*message.Message, error) {
erc1155Type, err := GetErc1155Type()
if err != nil {
return nil, err
Expand Down Expand Up @@ -46,7 +49,9 @@ func (dh *Erc1155DepositHandler) HandleDeposit(
Type: transfer.SemiFungibleTransfer,
},
messageID,
transfer.TransferMessageType), nil
transfer.TransferMessageType,
timestamp,
), nil
}

func GetErc1155Type() (abi.Arguments, error) {
Expand Down
Loading

0 comments on commit 03ec27c

Please sign in to comment.