Skip to content

Commit

Permalink
add batch sync height in db
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Dec 12, 2023
1 parent f11ce33 commit cd883a3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 16 deletions.
4 changes: 2 additions & 2 deletions bridge-history-api/conf/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"L1": {
"confirmation": 64,
"confirmation": 32,
"endpoint": "https://rpc.ankr.com/eth",
"startHeight": 18318215,
"blockTime": 12,
Expand All @@ -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",
Expand Down
15 changes: 12 additions & 3 deletions bridge-history-api/internal/controller/fetcher/l1_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions bridge-history-api/internal/controller/fetcher/l2_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
15 changes: 9 additions & 6 deletions bridge-history-api/internal/logic/watcher_event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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{}
Expand All @@ -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,
})
}
}
Expand Down
18 changes: 17 additions & 1 deletion bridge-history-api/internal/orm/batch_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions bridge-history-api/internal/orm/cross_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit cd883a3

Please sign in to comment.