Skip to content

Commit

Permalink
Merge pull request #126 from alephium/fix-explorer-backend-fetch-events
Browse files Browse the repository at this point in the history
Retrieve the latest event index from the database when restarting
  • Loading branch information
Lbqds authored Mar 15, 2024
2 parents 785fa0c + 015b0ac commit 459789d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
14 changes: 2 additions & 12 deletions explorer-backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,18 +346,13 @@ func run(cmd *cobra.Command, args []string) {
return err
}

ethEventIndex, err := watcher.GetLatestEventIndexEth(ctx)
if err != nil {
logger.Error("failed to get latest event index", zap.Uint16("chainId", uint16(vaa.ChainIDEthereum)), zap.Error(err))
return err
}
ethWatcher, err := transactions.NewEVMWatcher(
logger,
ctx,
*ethRpcUrl,
vaa.ChainIDEthereum,
bridgeConfig.Ethereum,
*ethEventIndex,
watcher.GetLatestEventIndexEth,
blockTxsC,
)
if err != nil {
Expand All @@ -369,18 +364,13 @@ func run(cmd *cobra.Command, args []string) {
}

if *bscRpcUrl != "" && *network == "devnet" {
bscEventIndex, err := watcher.GetLatestEventIndexBsc(ctx)
if err != nil {
logger.Error("failed to get latest event index", zap.Uint16("chainId", uint16(vaa.ChainIDBSC)), zap.Error(err))
return err
}
bscWatcher, err := transactions.NewEVMWatcher(
logger,
ctx,
*bscRpcUrl,
vaa.ChainIDBSC,
bridgeConfig.Bsc,
*bscEventIndex,
watcher.GetLatestEventIndexBsc,
blockTxsC,
)
if err != nil {
Expand Down
16 changes: 11 additions & 5 deletions explorer-backend/transactions/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type EVMWatcher struct {
chainConfig *common.ChainConfig
connector *ethereum.EthereumConnector
contractAddress *ethCommon.Address
fromHeight uint32
getEventIndex func(context.Context) (*uint32, error)
blockTxsC chan<- []*BlockTransactions
logger *zap.Logger
}
Expand All @@ -34,7 +34,7 @@ func NewEVMWatcher(
rpcUrl string,
chainId vaa.ChainID,
chainConfig *common.ChainConfig,
fromHeight uint32,
getEventIndex func(context.Context) (*uint32, error),
blockTxsC chan<- []*BlockTransactions,
) (*EVMWatcher, error) {
contractAddress := ethCommon.HexToAddress(chainConfig.Contracts.Governance)
Expand All @@ -48,7 +48,7 @@ func NewEVMWatcher(
chainConfig: chainConfig,
contractAddress: &contractAddress,
connector: connector,
fromHeight: fromHeight,
getEventIndex: getEventIndex,
blockTxsC: blockTxsC,
logger: namedLogger,
}, nil
Expand All @@ -69,7 +69,13 @@ func (w *EVMWatcher) Run() func(ctx context.Context) error {
}

func (w *EVMWatcher) fetchEvents(ctx context.Context, errC chan<- error) {
w.logger.Info("evm watcher started", zap.Uint32("fromHeight", w.fromHeight))
fromHeight, err := w.getEventIndex(ctx)
if err != nil {
w.logger.Error("failed to get latest event index", zap.Error(err), zap.Uint16("chainId", uint16(w.chainId)))
errC <- err
return
}
w.logger.Info("evm watcher started", zap.Uint32("fromHeight", *fromHeight))

blockC := make(chan *ethereum.NewBlock, 64)
subscription, err := w.connector.SubscribeForBlocks(ctx, blockC)
Expand All @@ -92,7 +98,7 @@ func (w *EVMWatcher) fetchEvents(ctx context.Context, errC chan<- error) {
w.logger.Info("received new block", zap.Uint64("height", block.Number.Uint64()), zap.String("hash", block.Hash.Hex()))
if isFirstBlock {
isFirstBlock = false
if err := w.fetchEventsFromBlockRange(ctx, w.fromHeight, uint32(block.Number.Uint64())); err != nil {
if err := w.fetchEventsFromBlockRange(ctx, *fromHeight, uint32(block.Number.Uint64())); err != nil {
w.logger.Error("failed to fetch events by block range", zap.Error(err))
errC <- err
return
Expand Down

0 comments on commit 459789d

Please sign in to comment.