Skip to content

Commit

Permalink
IMPORTANT: reindex in L2ScannedBlockV2
Browse files Browse the repository at this point in the history
  • Loading branch information
bendanzhentan committed Mar 14, 2024
1 parent 05d6fae commit dcb58d8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
12 changes: 6 additions & 6 deletions cmd/bot/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func RunCommand(ctx *cli.Context) error {
return fmt.Errorf("failed to connect database: %w", err)
}

err = db.AutoMigrate(&core.L2ScannedBlock{})
err = db.AutoMigrate(&core.L2ScannedBlockV2{})
if err != nil {
return fmt.Errorf("failed to migrate l2_scanned_blocks: %w", err)
}
err = db.AutoMigrate(&core.WithdrawalInitiatedLog{})
err = db.AutoMigrate(&core.WithdrawalInitiatedLogV2{})
if err != nil {
return fmt.Errorf("failed to migrate withdrawals: %w", err)
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func ProcessUnprovenBotDelegatedWithdrawals(ctx context.Context, log log.Logger,
processor := core.NewProcessor(log, l1Client, l2Client, cfg)
limit := 1000

unprovens := make([]core.WithdrawalInitiatedLog, 0)
unprovens := make([]core.WithdrawalInitiatedLogV2, 0)
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)
if result.Error != nil {
log.Error("failed to query withdrawals", "error", result.Error)
Expand Down Expand Up @@ -186,7 +186,7 @@ func ProcessUnfinalizedBotDelegatedWithdrawals(ctx context.Context, log log.Logg
now := time.Now()
maxProvenTime := now.Add(-time.Duration(cfg.ChallengeTimeWindow) * time.Second)

unfinalizeds := make([]core.WithdrawalInitiatedLog, 0)
unfinalizeds := make([]core.WithdrawalInitiatedLogV2, 0)
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)
if result.Error != nil {
log.Error("failed to query withdrawals", "error", result.Error)
Expand Down Expand Up @@ -259,8 +259,8 @@ func connect(log log.Logger, dbConfig config.DBConfig) (*gorm.DB, error) {
}

// queryL2ScannedBlock queries the l2_scanned_blocks table for the last scanned block
func queryL2ScannedBlock(db *gorm.DB, l2StartingNumber int64) (*core.L2ScannedBlock, error) {
l2ScannedBlock := core.L2ScannedBlock{}
func queryL2ScannedBlock(db *gorm.DB, l2StartingNumber int64) (*core.L2ScannedBlockV2, error) {
l2ScannedBlock := core.L2ScannedBlockV2{}
if result := db.Order("number desc").Last(&l2ScannedBlock); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
l2ScannedBlock.Number = l2StartingNumber
Expand Down
8 changes: 4 additions & 4 deletions core/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NewIndexer(log log.Logger, db *gorm.DB, l2Client *ClientExt, cfg Config) *I
}

// Start watches for new bot-delegated withdrawals and stores them in the database.
func (i *Indexer) Start(ctx context.Context, l2ScannedBlock *L2ScannedBlock) {
func (i *Indexer) Start(ctx context.Context, l2ScannedBlock *L2ScannedBlockV2) {
timer := time.NewTimer(0)
fromBlockNumber := big.NewInt(l2ScannedBlock.Number)
lastTimeL2ScannedBlock := time.Now()
Expand Down Expand Up @@ -196,7 +196,7 @@ func (i *Indexer) getWithdrawalInitiatedLogs(ctx context.Context, fromBlock *big
if withdrawalInitiatedLog != nil && i.isL2StandardBridgeWithdrawalInitiatedLog(withdrawalInitiatedLog) {
withdrawalInitiatedLogs = append(withdrawalInitiatedLogs, *withdrawalInitiatedLog)
} else {
i.log.Crit("eth_getLogs returned an unexpected event", "L2StandardBridgeBotWithdrawLog", vlog, "WithdrawalInitiatedLog", withdrawalInitiatedLog)
i.log.Crit("eth_getLogs returned an unexpected event", "L2StandardBridgeBotWithdrawLog", vlog, "WithdrawalInitiatedLogV2", withdrawalInitiatedLog)
}
} else if i.isFeeVaultWithdrawEvent(&vlog) {
// Events flow:
Expand All @@ -211,7 +211,7 @@ func (i *Indexer) getWithdrawalInitiatedLogs(ctx context.Context, fromBlock *big
if withdrawalInitiatedLog != nil && i.isL2StandardBridgeWithdrawalInitiatedLog(withdrawalInitiatedLog) {
withdrawalInitiatedLogs = append(withdrawalInitiatedLogs, *withdrawalInitiatedLog)
} else {
i.log.Crit("eth_getLogs returned an unexpected event", "FeeVaultWithdrawLog", vlog, "WithdrawalInitiatedLog", withdrawalInitiatedLog)
i.log.Crit("eth_getLogs returned an unexpected event", "FeeVaultWithdrawLog", vlog, "WithdrawalInitiatedLogV2", withdrawalInitiatedLog)
}
} else {
i.log.Crit("eth_getLogs returned an unexpected event", "log", vlog)
Expand All @@ -231,7 +231,7 @@ func (i *Indexer) storeLogs(logs []types.Log) error {
}

deduped := i.db.Clauses(clause.OnConflict{DoNothing: true})
result := deduped.Create(&WithdrawalInitiatedLog{
result := deduped.Create(&WithdrawalInitiatedLogV2{
TransactionHash: vLog.TxHash.Hex(),
LogIndex: int(vLog.Index),
InitiatedBlockNumber: int64(header.Number.Uint64()),
Expand Down
6 changes: 3 additions & 3 deletions core/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func StartMetrics(ctx context.Context, cfg *Config, l1Client *ethclient.Client,
}
TxSignerBalance.Set(float64(balance.Int64()))

var scannedBlock L2ScannedBlock
var scannedBlock L2ScannedBlockV2
result := db.Last(&scannedBlock)
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
logger.Error("failed to query scanned block", "error", result.Error)
Expand Down Expand Up @@ -83,14 +83,14 @@ func StartMetrics(ctx context.Context, cfg *Config, l1Client *ethclient.Client,
}
FailedWithdrawals.Set(float64(failedCnt))

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

firstUnfinalized := WithdrawalInitiatedLog{}
firstUnfinalized := WithdrawalInitiatedLogV2{}
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)
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
logger.Error("failed to query withdrawals", "error", result.Error)
Expand Down
8 changes: 4 additions & 4 deletions core/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewProcessor(
return &Processor{log, l1Client, l2Client, cfg, l2Contracts, whitelistL2TokenMap}
}

func (b *Processor) toWithdrawal(wi *WithdrawalInitiatedLog, receipt *types.Receipt) (*bindings.TypesWithdrawalTransaction, error) {
func (b *Processor) toWithdrawal(wi *WithdrawalInitiatedLogV2, receipt *types.Receipt) (*bindings.TypesWithdrawalTransaction, error) {
// Events flow:
//
// event[i]: WithdrawalInitiated
Expand Down Expand Up @@ -80,7 +80,7 @@ func (b *Processor) toWithdrawal(wi *WithdrawalInitiatedLog, receipt *types.Rece
return withdrawalTx, nil
}

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

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

func (b *Processor) GetProvenTime(wi *WithdrawalInitiatedLog) (*time.Time, error) {
func (b *Processor) GetProvenTime(wi *WithdrawalInitiatedLogV2) (*time.Time, error) {
optimismPortal, err := bindings.NewOptimismPortalCaller(b.cfg.L1Contracts.OptimismPortalProxy, b.L1Client)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package core

import "time"

type L2ScannedBlock struct {
type L2ScannedBlockV2 struct {
Number int64 `gorm:"type:integer;primarykey"`
}

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

Expand Down

0 comments on commit dcb58d8

Please sign in to comment.