Skip to content

Commit 91c969b

Browse files
Force reindex (#35)
* IMPORTANT: reindex in L2ScannedBlockV2 * IMPORTANT: force reindex in WithdrawalInitiatedLogV2
1 parent 05d6fae commit 91c969b

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

cmd/bot/run.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ func RunCommand(ctx *cli.Context) error {
4949
return fmt.Errorf("failed to connect database: %w", err)
5050
}
5151

52-
err = db.AutoMigrate(&core.L2ScannedBlock{})
52+
err = db.AutoMigrate(&core.L2ScannedBlockV2{})
5353
if err != nil {
5454
return fmt.Errorf("failed to migrate l2_scanned_blocks: %w", err)
5555
}
56-
err = db.AutoMigrate(&core.WithdrawalInitiatedLog{})
56+
err = db.AutoMigrate(&core.WithdrawalInitiatedLogV2{})
5757
if err != nil {
5858
return fmt.Errorf("failed to migrate withdrawals: %w", err)
5959
}
@@ -130,7 +130,7 @@ func ProcessUnprovenBotDelegatedWithdrawals(ctx context.Context, log log.Logger,
130130
processor := core.NewProcessor(log, l1Client, l2Client, cfg)
131131
limit := 1000
132132

133-
unprovens := make([]core.WithdrawalInitiatedLog, 0)
133+
unprovens := make([]core.WithdrawalInitiatedLogV2, 0)
134134
result := db.Order("id asc").Where("proven_time IS NULL AND initiated_block_number <= ? AND failure_reason IS NULL", latestProposedNumber.Uint64()).Limit(limit).Find(&unprovens)
135135
if result.Error != nil {
136136
log.Error("failed to query withdrawals", "error", result.Error)
@@ -186,7 +186,7 @@ func ProcessUnfinalizedBotDelegatedWithdrawals(ctx context.Context, log log.Logg
186186
now := time.Now()
187187
maxProvenTime := now.Add(-time.Duration(cfg.ChallengeTimeWindow) * time.Second)
188188

189-
unfinalizeds := make([]core.WithdrawalInitiatedLog, 0)
189+
unfinalizeds := make([]core.WithdrawalInitiatedLogV2, 0)
190190
result := db.Order("id asc").Where("finalized_time IS NULL AND proven_time IS NOT NULL AND proven_time < ? AND failure_reason IS NULL", maxProvenTime).Limit(limit).Find(&unfinalizeds)
191191
if result.Error != nil {
192192
log.Error("failed to query withdrawals", "error", result.Error)
@@ -259,8 +259,8 @@ func connect(log log.Logger, dbConfig config.DBConfig) (*gorm.DB, error) {
259259
}
260260

261261
// queryL2ScannedBlock queries the l2_scanned_blocks table for the last scanned block
262-
func queryL2ScannedBlock(db *gorm.DB, l2StartingNumber int64) (*core.L2ScannedBlock, error) {
263-
l2ScannedBlock := core.L2ScannedBlock{}
262+
func queryL2ScannedBlock(db *gorm.DB, l2StartingNumber int64) (*core.L2ScannedBlockV2, error) {
263+
l2ScannedBlock := core.L2ScannedBlockV2{}
264264
if result := db.Order("number desc").Last(&l2ScannedBlock); result.Error != nil {
265265
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
266266
l2ScannedBlock.Number = l2StartingNumber

core/indexer.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func NewIndexer(log log.Logger, db *gorm.DB, l2Client *ClientExt, cfg Config) *I
9595
}
9696

9797
// Start watches for new bot-delegated withdrawals and stores them in the database.
98-
func (i *Indexer) Start(ctx context.Context, l2ScannedBlock *L2ScannedBlock) {
98+
func (i *Indexer) Start(ctx context.Context, l2ScannedBlock *L2ScannedBlockV2) {
9999
timer := time.NewTimer(0)
100100
fromBlockNumber := big.NewInt(l2ScannedBlock.Number)
101101
lastTimeL2ScannedBlock := time.Now()
@@ -196,7 +196,7 @@ func (i *Indexer) getWithdrawalInitiatedLogs(ctx context.Context, fromBlock *big
196196
if withdrawalInitiatedLog != nil && i.isL2StandardBridgeWithdrawalInitiatedLog(withdrawalInitiatedLog) {
197197
withdrawalInitiatedLogs = append(withdrawalInitiatedLogs, *withdrawalInitiatedLog)
198198
} else {
199-
i.log.Crit("eth_getLogs returned an unexpected event", "L2StandardBridgeBotWithdrawLog", vlog, "WithdrawalInitiatedLog", withdrawalInitiatedLog)
199+
i.log.Crit("eth_getLogs returned an unexpected event", "L2StandardBridgeBotWithdrawLog", vlog, "WithdrawalInitiatedLogV2", withdrawalInitiatedLog)
200200
}
201201
} else if i.isFeeVaultWithdrawEvent(&vlog) {
202202
// Events flow:
@@ -211,7 +211,7 @@ func (i *Indexer) getWithdrawalInitiatedLogs(ctx context.Context, fromBlock *big
211211
if withdrawalInitiatedLog != nil && i.isL2StandardBridgeWithdrawalInitiatedLog(withdrawalInitiatedLog) {
212212
withdrawalInitiatedLogs = append(withdrawalInitiatedLogs, *withdrawalInitiatedLog)
213213
} else {
214-
i.log.Crit("eth_getLogs returned an unexpected event", "FeeVaultWithdrawLog", vlog, "WithdrawalInitiatedLog", withdrawalInitiatedLog)
214+
i.log.Crit("eth_getLogs returned an unexpected event", "FeeVaultWithdrawLog", vlog, "WithdrawalInitiatedLogV2", withdrawalInitiatedLog)
215215
}
216216
} else {
217217
i.log.Crit("eth_getLogs returned an unexpected event", "log", vlog)
@@ -231,7 +231,7 @@ func (i *Indexer) storeLogs(logs []types.Log) error {
231231
}
232232

233233
deduped := i.db.Clauses(clause.OnConflict{DoNothing: true})
234-
result := deduped.Create(&WithdrawalInitiatedLog{
234+
result := deduped.Create(&WithdrawalInitiatedLogV2{
235235
TransactionHash: vLog.TxHash.Hex(),
236236
LogIndex: int(vLog.Index),
237237
InitiatedBlockNumber: int64(header.Number.Uint64()),

core/metrics.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -55,43 +55,43 @@ func StartMetrics(ctx context.Context, cfg *Config, l1Client *ethclient.Client,
5555
}
5656
TxSignerBalance.Set(float64(balance.Int64()))
5757

58-
var scannedBlock L2ScannedBlock
58+
var scannedBlock L2ScannedBlockV2
5959
result := db.Last(&scannedBlock)
6060
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
6161
logger.Error("failed to query scanned block", "error", result.Error)
6262
}
6363
ScannedBlockNumber.Set(float64(scannedBlock.Number))
6464

6565
var unprovenCnt int64
66-
result = db.Table("withdrawal_initiated_logs").Where("proven_time IS NULL AND failure_reason IS NULL").Count(&unprovenCnt)
66+
result = db.Table("withdrawal_initiated_log_v2").Where("proven_time IS NULL AND failure_reason IS NULL").Count(&unprovenCnt)
6767
if result.Error != nil {
6868
logger.Error("failed to count withdrawals", "error", result.Error)
6969
}
7070
UnprovenWithdrawals.Set(float64(unprovenCnt))
7171

7272
var unfinalizedCnt int64
73-
result = db.Table("withdrawal_initiated_logs").Where("finalized_time IS NULL AND proven_time IS NOT NULL AND failure_reason IS NULL").Count(&unfinalizedCnt)
73+
result = db.Table("withdrawal_initiated_log_v2").Where("finalized_time IS NULL AND proven_time IS NOT NULL AND failure_reason IS NULL").Count(&unfinalizedCnt)
7474
if result.Error != nil {
7575
logger.Error("failed to count withdrawals", "error", result.Error)
7676
}
7777
UnfinalizedWithdrawals.Set(float64(unfinalizedCnt))
7878

7979
var failedCnt int64
80-
result = db.Table("withdrawal_initiated_logs").Where("failure_reason IS NOT NULL").Count(&failedCnt)
80+
result = db.Table("withdrawal_initiated_log_v2").Where("failure_reason IS NOT NULL").Count(&failedCnt)
8181
if result.Error != nil {
8282
logger.Error("failed to count withdrawals", "error", result.Error)
8383
}
8484
FailedWithdrawals.Set(float64(failedCnt))
8585

86-
firstUnproven := WithdrawalInitiatedLog{}
87-
result = db.Table("withdrawal_initiated_logs").Order("id asc").Where("proven_time IS NULL AND failure_reason IS NULL").First(&firstUnproven)
86+
firstUnproven := WithdrawalInitiatedLogV2{}
87+
result = db.Table("withdrawal_initiated_log_v2").Order("id asc").Where("proven_time IS NULL AND failure_reason IS NULL").First(&firstUnproven)
8888
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
8989
logger.Error("failed to query withdrawals", "error", result.Error)
9090
}
9191
EarliestUnProvenWithdrawalBlockNumber.Set(float64(firstUnproven.InitiatedBlockNumber))
9292

93-
firstUnfinalized := WithdrawalInitiatedLog{}
94-
result = db.Table("withdrawal_initiated_logs").Order("id asc").Where("finalized_time IS NULL AND proven_time IS NOT NULL AND failure_reason IS NULL").First(&firstUnfinalized)
93+
firstUnfinalized := WithdrawalInitiatedLogV2{}
94+
result = db.Table("withdrawal_initiated_log_v2").Order("id asc").Where("finalized_time IS NULL AND proven_time IS NOT NULL AND failure_reason IS NULL").First(&firstUnfinalized)
9595
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
9696
logger.Error("failed to query withdrawals", "error", result.Error)
9797
}

core/processor.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewProcessor(
4848
return &Processor{log, l1Client, l2Client, cfg, l2Contracts, whitelistL2TokenMap}
4949
}
5050

51-
func (b *Processor) toWithdrawal(wi *WithdrawalInitiatedLog, receipt *types.Receipt) (*bindings.TypesWithdrawalTransaction, error) {
51+
func (b *Processor) toWithdrawal(wi *WithdrawalInitiatedLogV2, receipt *types.Receipt) (*bindings.TypesWithdrawalTransaction, error) {
5252
// Events flow:
5353
//
5454
// event[i]: WithdrawalInitiated
@@ -80,7 +80,7 @@ func (b *Processor) toWithdrawal(wi *WithdrawalInitiatedLog, receipt *types.Rece
8080
return withdrawalTx, nil
8181
}
8282

83-
func (b *Processor) ProveWithdrawalTransaction(ctx context.Context, wi *WithdrawalInitiatedLog, nonce uint64) error {
83+
func (b *Processor) ProveWithdrawalTransaction(ctx context.Context, wi *WithdrawalInitiatedLogV2, nonce uint64) error {
8484
receipt, err := b.L2Client.TransactionReceipt(ctx, common.HexToHash(wi.TransactionHash))
8585
if err != nil {
8686
return err
@@ -189,7 +189,7 @@ func (b *Processor) ProveWithdrawalTransaction(ctx context.Context, wi *Withdraw
189189
}
190190

191191
// FinalizeMessage https://github.com/ethereum-optimism/optimism/blob/d90e7818de894f0bc93ae7b449b9049416bda370/packages/sdk/src/cross-chain-messenger.ts#L1611
192-
func (b *Processor) FinalizeMessage(ctx context.Context, wi *WithdrawalInitiatedLog) error {
192+
func (b *Processor) FinalizeMessage(ctx context.Context, wi *WithdrawalInitiatedLogV2) error {
193193
receipt, err := b.L2Client.TransactionReceipt(ctx, common.HexToHash(wi.TransactionHash))
194194
if err != nil {
195195
return err
@@ -243,7 +243,7 @@ func (b *Processor) FinalizeMessage(ctx context.Context, wi *WithdrawalInitiated
243243
return nil
244244
}
245245

246-
func (b *Processor) GetProvenTime(wi *WithdrawalInitiatedLog) (*time.Time, error) {
246+
func (b *Processor) GetProvenTime(wi *WithdrawalInitiatedLogV2) (*time.Time, error) {
247247
optimismPortal, err := bindings.NewOptimismPortalCaller(b.cfg.L1Contracts.OptimismPortalProxy, b.L1Client)
248248
if err != nil {
249249
return nil, err

core/types.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@ package core
22

33
import "time"
44

5-
type L2ScannedBlock struct {
5+
type L2ScannedBlockV2 struct {
66
Number int64 `gorm:"type:integer;primarykey"`
77
}
88

9-
// WithdrawalInitiatedLog is parsed record that represent a withdrawal.
9+
// WithdrawalInitiatedLogV2 is parsed record that represent a withdrawal.
1010
//
1111
// See also [L2StandardBridge.WithdrawalInitiated](https://github.com/bnb-chain/opbnb/blob/develop/packages/contracts-bedrock/contracts/L2/L2StandardBridge.sol#L21-L39)
12-
type WithdrawalInitiatedLog struct {
12+
type WithdrawalInitiatedLogV2 struct {
1313
// ID is the incrementing primary key.
1414
ID uint `gorm:"primarykey"`
1515

1616
// TransactionHash and LogIndex are the L2 transaction hash and log index of the withdrawal event.
17-
TransactionHash string `gorm:"type:varchar(256);not null;uniqueIndex:idx_withdrawal_initiated_logs_transaction_hash_log_index_key,priority:1"`
18-
LogIndex int `gorm:"type:integer;not null;uniqueIndex:idx_withdrawal_initiated_logs_transaction_hash_log_index_key,priority:2"`
17+
TransactionHash string `gorm:"type:varchar(256);not null;uniqueIndex:idx_withdrawal_initiated_log_v2_transaction_hash_log_index_key,priority:1"`
18+
LogIndex int `gorm:"type:integer;not null;uniqueIndex:idx_withdrawal_initiated_log_v2_transaction_hash_log_index_key,priority:2"`
1919

2020
// InitiatedBlockNumber is the l2 block number at which the withdrawal was initiated on L2.
21-
InitiatedBlockNumber int64 `gorm:"type:integer;not null;index:idx_withdrawal_initiated_logs_initiated_block_number"`
21+
InitiatedBlockNumber int64 `gorm:"type:integer;not null;index:idx_withdrawal_initiated_log_v2_initiated_block_number"`
2222

2323
// ProvenTime is the local time at which the withdrawal was proven on L1. NULL if not yet proven.
24-
ProvenTime *time.Time `gorm:"type:datetime;index:idx_withdrawal_initiated_logs_proven_time"`
24+
ProvenTime *time.Time `gorm:"type:datetime;index:idx_withdrawal_initiated_log_v2_proven_time"`
2525

2626
// FinalizedTime is the local time at which the withdrawal was finalized on L1. NULL if not yet finalized.
27-
FinalizedTime *time.Time `gorm:"type:datetime;index:idx_withdrawal_initiated_logs_finalized_time"`
27+
FinalizedTime *time.Time `gorm:"type:datetime;index:idx_withdrawal_initiated_log_v2_finalized_time"`
2828

2929
// FailureReason is the reason for the withdrawal failure, including sending transaction error and off-chain configured filter error. NULL if not yet failed.
3030
FailureReason *string `gorm:"type:text"`

0 commit comments

Comments
 (0)