Skip to content

Commit

Permalink
feat(relayer): document indexer with comments, fix broken crawler log…
Browse files Browse the repository at this point in the history
…ic, add new metrics (#16330)

Co-authored-by: Roger <50648015+RogerLamTd@users.noreply.github.com>
  • Loading branch information
cyberhorsey and RogerLamTd committed Mar 6, 2024
1 parent 71553ac commit 4d8d9d7
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 120 deletions.
2 changes: 2 additions & 0 deletions packages/relayer/indexer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"gorm.io/gorm/logger"
)

// Config is a struct which should be created from the cli or environment variables, populated,
// and used to create a new Indexer.
type Config struct {
// address configs
SrcBridgeAddress common.Address
Expand Down
2 changes: 2 additions & 0 deletions packages/relayer/indexer/detect_and_handle_reorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log/slog"
)

// detectAndHandleReorg will look up an event in the database to see if we have seen this event
// before. If we have , we need to delete it before re-indexing.
func (i *Indexer) detectAndHandleReorg(ctx context.Context, eventType string, msgHash string) error {
// dont check on crawling past blocks, it will be a secondary indexer.
// we expect to see duplicates in this mode.
Expand Down
2 changes: 2 additions & 0 deletions packages/relayer/indexer/handle_chain_data_synced_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,7 @@ func (i *Indexer) handleChainDataSyncedEvent(

slog.Info("chainDataSynced event saved")

relayer.ChainDataSyncedEventsIndexed.Inc()

return nil
}
2 changes: 2 additions & 0 deletions packages/relayer/indexer/handle_message_sent_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func (i *Indexer) handleMessageSentEvent(
return errors.Wrap(err, "i.queue.Publish")
}

relayer.MessageSentEventsIndexed.Inc()

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,13 @@ import (
"encoding/json"
"math/big"

"log/slog"

"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/taikoxyz/taiko-mono/packages/relayer"
"github.com/taikoxyz/taiko-mono/packages/relayer/bindings/bridge"
)

func (i *Indexer) saveMessageStatusChangedEvents(
ctx context.Context,
chainID *big.Int,
events *bridge.BridgeMessageStatusChangedIterator,
) error {
if !events.Next() || events.Event == nil {
slog.Info("no messageStatusChanged events")
return nil
}

for {
event := events.Event

slog.Info("messageStatusChanged", "msgHash", common.Hash(event.MsgHash).Hex())

if err := i.detectAndHandleReorg(
ctx,
relayer.EventNameMessageStatusChanged,
common.Hash(event.MsgHash).Hex(),
); err != nil {
return errors.Wrap(err, "i.detectAndHandleReorg")
}

if err := i.saveMessageStatusChangedEvent(ctx, chainID, event); err != nil {
return errors.Wrap(err, "i.saveMessageStatusChangedEvent")
}

if !events.Next() {
return nil
}
}
}

func (i *Indexer) saveMessageStatusChangedEvent(
func (i *Indexer) handleMessageStatusChangedEvent(
ctx context.Context,
chainID *big.Int,
event *bridge.BridgeMessageStatusChanged,
Expand Down Expand Up @@ -81,5 +46,7 @@ func (i *Indexer) saveMessageStatusChangedEvent(
return errors.Wrap(err, "i.eventRepo.Save")
}

relayer.MessageStatusChangedEventsIndexed.Inc()

return nil
}
4 changes: 2 additions & 2 deletions packages/relayer/indexer/handle_no_events_in_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/taikoxyz/taiko-mono/packages/relayer"
)

// handleNoEventsInBatch is used when an entire batch call has no events in the entire response,
// and we need to update the latest block processed
// handleNoEventsInBatch is used when there are no events remaining in a batch, or
// the batch itself contained no events.
func (i *Indexer) handleNoEventsInBatch(
ctx context.Context,
chainID *big.Int,
Expand Down
Loading

0 comments on commit 4d8d9d7

Please sign in to comment.