diff --git a/bridge-history-api/conf/config.json b/bridge-history-api/conf/config.json index 1a77ba210d..150d0b1097 100644 --- a/bridge-history-api/conf/config.json +++ b/bridge-history-api/conf/config.json @@ -1,6 +1,6 @@ { "L1": { - "confirmation": 64, + "confirmation": 32, "endpoint": "https://rpc.ankr.com/eth", "startHeight": 18318215, "blockTime": 12, @@ -23,7 +23,7 @@ "confirmation": 1, "endpoint": "http://mainnet-l2geth-internal-1.mainnet.scroll.tech:8545", "blockTime": 3, - "fetchLimit": 400, + "fetchLimit": 128, "MessengerAddr": "0x781e90f1c8Fc4611c9b7497C3B47F99Ef6969CbC", "ETHGatewayAddr": "0x6EA73e05AdC79974B931123675ea8F78FfdacDF0", "WETHGatewayAddr": "0x7003E7B7186f0E6601203b99F7B8DECBfA391cf9", diff --git a/bridge-history-api/internal/controller/fetcher/l1_fetcher.go b/bridge-history-api/internal/controller/fetcher/l1_fetcher.go index 4d9d12da77..0868f6599d 100644 --- a/bridge-history-api/internal/controller/fetcher/l1_fetcher.go +++ b/bridge-history-api/internal/controller/fetcher/l1_fetcher.go @@ -75,15 +75,24 @@ func NewL1MessageFetcher(ctx context.Context, cfg *config.LayerConfig, db *gorm. // Start starts the L1 message fetching process. func (c *L1MessageFetcher) Start() { - var err error - c.l1ScanHeight, err = c.crossMessageOrm.GetMessageProcessedHeightInDB(c.ctx, orm.MessageTypeL1SentMessage) + messageSyncedHeight, err := c.crossMessageOrm.GetMessageSyncedHeightInDB(c.ctx, orm.MessageTypeL1SentMessage) if err != nil { - log.Error("failed to get L1 cross message processed height", "err", err) + log.Error("failed to get L1 cross message synced height", "error", err) return } + batchSyncedHeight, err := c.batchEventOrm.GetBatchEventSyncedHeightInDB(c.ctx) + if err != nil { + log.Error("failed to get L1 batch event synced height", "error", err) + return + } + c.l1ScanHeight = messageSyncedHeight + if batchSyncedHeight > c.l1ScanHeight { + c.l1ScanHeight = batchSyncedHeight + } if c.cfg.StartHeight > c.l1ScanHeight { c.l1ScanHeight = c.cfg.StartHeight - 1 } + log.Info("Start L1 message fetcher", "message synced height", messageSyncedHeight, "batch synced height", batchSyncedHeight, "config start height", c.cfg.StartHeight) tick := time.NewTicker(time.Duration(c.cfg.BlockTime) * time.Second) go func() { diff --git a/bridge-history-api/internal/controller/fetcher/l2_fetcher.go b/bridge-history-api/internal/controller/fetcher/l2_fetcher.go index 47c0c7af5b..1fdcbc8d73 100644 --- a/bridge-history-api/internal/controller/fetcher/l2_fetcher.go +++ b/bridge-history-api/internal/controller/fetcher/l2_fetcher.go @@ -70,12 +70,12 @@ func NewL2MessageFetcher(ctx context.Context, cfg *config.LayerConfig, db *gorm. // Start starts the L2 message fetching process. func (c *L2MessageFetcher) Start() { - l2SentMessageProcessedHeight, err := c.crossMessageOrm.GetMessageProcessedHeightInDB(c.ctx, orm.MessageTypeL2SentMessage) + l2SentMessageSyncedHeight, err := c.crossMessageOrm.GetMessageSyncedHeightInDB(c.ctx, orm.MessageTypeL2SentMessage) if err != nil { log.Error("failed to get L2 cross message processed height", "err", err) return } - c.syncInfo.SetL2ScanHeight(l2SentMessageProcessedHeight) + c.syncInfo.SetL2ScanHeight(l2SentMessageSyncedHeight) tick := time.NewTicker(time.Duration(c.cfg.BlockTime) * time.Second) go func() { diff --git a/bridge-history-api/internal/logic/watcher_event_parser.go b/bridge-history-api/internal/logic/watcher_event_parser.go index e986eadb95..d0c553cd29 100644 --- a/bridge-history-api/internal/logic/watcher_event_parser.go +++ b/bridge-history-api/internal/logic/watcher_event_parser.go @@ -177,6 +177,7 @@ func ParseL1BatchEventLogs(ctx context.Context, logs []types.Log, blockTimestamp BatchHash: event.BatchHash.String(), StartBlockNumber: startBlock, EndBlockNumber: endBlock, + L1BlockNumber: vlog.BlockNumber, }) case backendabi.L1RevertBatchEventSig: event := backendabi.L1RevertBatchEvent{} @@ -185,9 +186,10 @@ func ParseL1BatchEventLogs(ctx context.Context, logs []types.Log, blockTimestamp return nil, err } l1BatchEvents = append(l1BatchEvents, &orm.BatchEvent{ - BatchStatus: int(orm.BatchStatusTypeReverted), - BatchIndex: event.BatchIndex.Uint64(), - BatchHash: event.BatchHash.String(), + BatchStatus: int(orm.BatchStatusTypeReverted), + BatchIndex: event.BatchIndex.Uint64(), + BatchHash: event.BatchHash.String(), + L1BlockNumber: vlog.BlockNumber, }) case backendabi.L1FinalizeBatchEventSig: event := backendabi.L1FinalizeBatchEvent{} @@ -196,9 +198,10 @@ func ParseL1BatchEventLogs(ctx context.Context, logs []types.Log, blockTimestamp return nil, err } l1BatchEvents = append(l1BatchEvents, &orm.BatchEvent{ - BatchStatus: int(orm.BatchStatusTypeFinalized), - BatchIndex: event.BatchIndex.Uint64(), - BatchHash: event.BatchHash.String(), + BatchStatus: int(orm.BatchStatusTypeFinalized), + BatchIndex: event.BatchIndex.Uint64(), + BatchHash: event.BatchHash.String(), + L1BlockNumber: vlog.BlockNumber, }) } } diff --git a/bridge-history-api/internal/orm/batch_event.go b/bridge-history-api/internal/orm/batch_event.go index 8db63be702..eaa2697b92 100644 --- a/bridge-history-api/internal/orm/batch_event.go +++ b/bridge-history-api/internal/orm/batch_event.go @@ -33,6 +33,7 @@ type BatchEvent struct { db *gorm.DB `gorm:"column:-"` ID uint64 `json:"id" gorm:"column:id;primary_key"` + L1BlockNumber uint64 `json:"l1_block_number" gorm:"column:l1_block_number"` BatchStatus int `json:"batch_status" gorm:"column:batch_status"` BatchIndex uint64 `json:"batch_index" gorm:"column:batch_index"` BatchHash string `json:"batch_hash" gorm:"column:batch_hash"` @@ -54,6 +55,21 @@ func NewBatchEvent(db *gorm.DB) *BatchEvent { return &BatchEvent{db: db} } +// GetBatchEventSyncedHeightInDB returns the maximum l1_block_number from the batch_event table. +func (c *BatchEvent) GetBatchEventSyncedHeightInDB(ctx context.Context) (uint64, error) { + var batch BatchEvent + db := c.db.WithContext(ctx) + db = db.Model(&BatchEvent{}) + db = db.Order("l1_block_number desc") + if err := db.First(&batch).Error; err != nil { + if err == gorm.ErrRecordNotFound { + return 0, nil + } + return 0, fmt.Errorf("failed to get batch synced height in db, error: %w", err) + } + return batch.L1BlockNumber, nil +} + // GetBatchesLEBlockHeight returns the batches with end block <= given block height in db. func (c *BatchEvent) GetBatchesLEBlockHeight(ctx context.Context, blockHeight uint64) ([]*BatchEvent, error) { var batches []*BatchEvent @@ -100,7 +116,7 @@ func (c *BatchEvent) InsertOrUpdateBatchEvents(ctx context.Context, l1BatchEvent if err := db.Updates(updateFields).Error; err != nil { return fmt.Errorf("failed to update batch event, batch: %+v, error: %w", l1BatchEvent, err) } - // Soft delete the batch event + // Soft delete the batch event. if err := db.Delete(l1BatchEvent).Error; err != nil { return fmt.Errorf("failed to soft delete batch event, batch: %+v, error: %w", l1BatchEvent, err) } diff --git a/bridge-history-api/internal/orm/cross_message.go b/bridge-history-api/internal/orm/cross_message.go index 1c1e70c72c..e3c56499eb 100644 --- a/bridge-history-api/internal/orm/cross_message.go +++ b/bridge-history-api/internal/orm/cross_message.go @@ -117,8 +117,8 @@ func NewCrossMessage(db *gorm.DB) *CrossMessage { return &CrossMessage{db: db} } -// GetMessageProcessedHeightInDB returns the latest processed cross message height from the database for a given message type. -func (c *CrossMessage) GetMessageProcessedHeightInDB(ctx context.Context, messageType MessageType) (uint64, error) { +// GetMessageSyncedHeightInDB returns the latest synced cross message height from the database for a given message type. +func (c *CrossMessage) GetMessageSyncedHeightInDB(ctx context.Context, messageType MessageType) (uint64, error) { var message CrossMessage db := c.db.WithContext(ctx) db = db.Model(&CrossMessage{}) diff --git a/bridge-history-api/internal/orm/migrate/migrations/00002_batch_event.sql b/bridge-history-api/internal/orm/migrate/migrations/00002_batch_event.sql index c90a20d04d..797b7ed59a 100644 --- a/bridge-history-api/internal/orm/migrate/migrations/00002_batch_event.sql +++ b/bridge-history-api/internal/orm/migrate/migrations/00002_batch_event.sql @@ -3,6 +3,7 @@ CREATE TABLE batch_event ( id BIGSERIAL PRIMARY KEY, + l1_block_number BIGINT NOT NULL, batch_status SMALLINT NOT NULL, batch_index BIGINT NOT NULL, batch_hash VARCHAR NOT NULL, @@ -14,6 +15,7 @@ CREATE TABLE batch_event deleted_at TIMESTAMP(0) DEFAULT NULL ); +CREATE INDEX IF NOT EXISTS idx_be_l1_block_number ON batch_event (l1_block_number); CREATE INDEX IF NOT EXISTS idx_be_batch_index ON batch_event (batch_index); CREATE UNIQUE INDEX IF NOT EXISTS unique_idx_be_batch_index_batch_hash ON batch_event (batch_index, batch_hash); CREATE INDEX IF NOT EXISTS idx_be_end_block_number_update_status_batch_index ON batch_event (end_block_number, update_status, batch_index);