Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force reindex #35

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 8 additions & 8 deletions core/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,43 @@ 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)
}
ScannedBlockNumber.Set(float64(scannedBlock.Number))

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

var unfinalizedCnt int64
result = db.Table("withdrawal_initiated_logs").Where("finalized_time IS NULL AND proven_time IS NOT NULL AND failure_reason IS NULL").Count(&unfinalizedCnt)
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)
if result.Error != nil {
logger.Error("failed to count withdrawals", "error", result.Error)
}
UnfinalizedWithdrawals.Set(float64(unfinalizedCnt))

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

firstUnproven := WithdrawalInitiatedLog{}
result = db.Table("withdrawal_initiated_logs").Order("id asc").Where("proven_time IS NULL AND failure_reason IS NULL").First(&firstUnproven)
firstUnproven := WithdrawalInitiatedLogV2{}
result = db.Table("withdrawal_initiated_log_v2").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{}
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)
firstUnfinalized := WithdrawalInitiatedLogV2{}
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)
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
16 changes: 8 additions & 8 deletions core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ 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"`

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

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

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

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

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