Skip to content

Commit

Permalink
Two step withdrawal indexer updates
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Dec 7, 2022
1 parent c129ec6 commit 9e1b6fe
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 5 deletions.
36 changes: 33 additions & 3 deletions indexer/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ func (d *Database) AddIndexedL1Block(block *IndexedL1Block) error {
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
`

const updateWithdrawalStatement = `
const updateProvenWithdrawalStatement = `
UPDATE withdrawals SET (br_withdrawal_proven_tx_hash, br_withdrawal_proven_log_index) = ($1, $2)
WHERE br_withdrawal_hash = $3
`

const updateFinalizedWithdrawalStatement = `
UPDATE withdrawals SET (br_withdrawal_finalized_tx_hash, br_withdrawal_finalized_log_index, br_withdrawal_finalized_success) = ($1, $2, $3)
WHERE br_withdrawal_hash = $4
`
Expand Down Expand Up @@ -236,10 +241,24 @@ func (d *Database) AddIndexedL1Block(block *IndexedL1Block) error {
}
}

if len(block.ProvenWithdrawals) > 0 {
for _, wd := range block.ProvenWithdrawals {
_, err = tx.Exec(
updateProvenWithdrawalStatement,
wd.TxHash.String(),
wd.LogIndex,
wd.WithdrawalHash.String(),
)
if err != nil {
return err
}
}
}

if len(block.FinalizedWithdrawals) > 0 {
for _, wd := range block.FinalizedWithdrawals {
_, err = tx.Exec(
updateWithdrawalStatement,
updateFinalizedWithdrawalStatement,
wd.TxHash.String(),
wd.LogIndex,
wd.Success,
Expand Down Expand Up @@ -486,6 +505,7 @@ func (d *Database) GetWithdrawalsByAddress(address common.Address, page Paginati
withdrawals.l1_token, withdrawals.l2_token,
l2_tokens.name, l2_tokens.symbol, l2_tokens.decimals,
l2_blocks.number, l2_blocks.timestamp, withdrawals.br_withdrawal_hash,
withdrawals.br_withdrawal_proven_tx_hash, withdrawals.br_withdrawal_proven_log_index,
withdrawals.br_withdrawal_finalized_tx_hash, withdrawals.br_withdrawal_finalized_log_index,
withdrawals.br_withdrawal_finalized_success
FROM withdrawals
Expand All @@ -506,6 +526,8 @@ func (d *Database) GetWithdrawalsByAddress(address common.Address, page Paginati
var withdrawal WithdrawalJSON
var l2Token Token
var wdHash sql.NullString
var proveTxHash sql.NullString
var proveLogIndex sql.NullInt32
var finTxHash sql.NullString
var finLogIndex sql.NullInt32
var finSuccess sql.NullBool
Expand All @@ -515,14 +537,22 @@ func (d *Database) GetWithdrawalsByAddress(address common.Address, page Paginati
&withdrawal.L1Token, &l2Token.Address,
&l2Token.Name, &l2Token.Symbol, &l2Token.Decimals,
&withdrawal.BlockNumber, &withdrawal.BlockTimestamp,
&wdHash, &finTxHash, &finLogIndex, &finSuccess,
&wdHash, &proveTxHash, &proveLogIndex,
&finTxHash, &finLogIndex, &finSuccess,
); err != nil {
return err
}
withdrawal.L2Token = &l2Token
if wdHash.Valid {
withdrawal.BedrockWithdrawalHash = &wdHash.String
}
if proveTxHash.Valid {
withdrawal.BedrockProvenTxHash = &proveTxHash.String
}
if proveLogIndex.Valid {
idx := int(proveLogIndex.Int32)
withdrawal.BedrockProvenLogIndex = &idx
}
if finTxHash.Valid {
withdrawal.BedrockFinalizedTxHash = &finTxHash.String
}
Expand Down
1 change: 1 addition & 0 deletions indexer/db/l1block.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type IndexedL1Block struct {
Number uint64
Timestamp uint64
Deposits []Deposit
ProvenWithdrawals []ProvenWithdrawal
FinalizedWithdrawals []FinalizedWithdrawal
}

Expand Down
2 changes: 2 additions & 0 deletions indexer/db/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ CREATE TABLE IF NOT EXISTS airdrops (

const updateWithdrawalsTable = `
ALTER TABLE withdrawals ADD COLUMN IF NOT EXISTS br_withdrawal_hash VARCHAR NULL;
ALTER TABLE withdrawals ADD COLUMN IF NOT EXISTS br_withdrawal_proven_tx_hash VARCHAR NULL;
ALTER TABLE withdrawals ADD COLUMN IF NOT EXISTS br_withdrawal_proven_log_index INTEGER NULL;
ALTER TABLE withdrawals ADD COLUMN IF NOT EXISTS br_withdrawal_finalized_tx_hash VARCHAR NULL;
ALTER TABLE withdrawals ADD COLUMN IF NOT EXISTS br_withdrawal_finalized_log_index INTEGER NULL;
ALTER TABLE withdrawals ADD COLUMN IF NOT EXISTS br_withdrawal_finalized_success BOOLEAN NULL;
Expand Down
10 changes: 10 additions & 0 deletions indexer/db/withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type WithdrawalJSON struct {
TxHash string `json:"transactionHash"`
Batch *StateBatchJSON `json:"batch"`
BedrockWithdrawalHash *string `json:"bedrockWithdrawalHash"`
BedrockProvenTxHash *string `json:"bedrockProvenTxHash"`
BedrockProvenLogIndex *int `json:"bedrockProvenLogIndex"`
BedrockFinalizedTxHash *string `json:"bedrockFinalizedTxHash"`
BedrockFinalizedLogIndex *int `json:"bedrockFinalizedLogIndex"`
BedrockFinalizedSuccess *bool `json:"bedrockFinalizedSuccess"`
Expand Down Expand Up @@ -75,6 +77,14 @@ func (f FinalizationState) SQL() string {
return ""
}

type ProvenWithdrawal struct {
From common.Address
To common.Address
WithdrawalHash common.Hash
TxHash common.Hash
LogIndex uint
}

type FinalizedWithdrawal struct {
WithdrawalHash common.Hash
TxHash common.Hash
Expand Down
6 changes: 5 additions & 1 deletion indexer/services/l1/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ type DepositsMap map[common.Hash][]db.Deposit
// on block hashes.
type InitiatedWithdrawalMap map[common.Hash][]db.Withdrawal

// ProvenWithdrawalsMap is a collection of proven withdrawal
// objects keyed on block hashses
type ProvenWithdrawalsMap map[common.Hash][]db.ProvenWithdrawal

// FinalizedWithdrawalsMap is a collection of finalized withdrawal
// objected keyed on block hashes.
// objects keyed on block hashes.
type FinalizedWithdrawalsMap map[common.Hash][]db.FinalizedWithdrawal

type Bridge interface {
Expand Down
34 changes: 34 additions & 0 deletions indexer/services/l1/bridge/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,40 @@ func (p *Portal) Address() common.Address {
return p.address
}

func (p *Portal) GetProvenWithdrawalsByBlockRange(ctx context.Context, start, end uint64) (ProvenWithdrawalsMap, error) {
wdsByBlockHash := make(ProvenWithdrawalsMap)
opts := &bind.FilterOpts{
Context: ctx,
Start: start,
End: &end,
}

var iter *bindings.OptimismPortalWithdrawalProvenIterator
err := backoff.Do(3, backoff.Exponential(), func() error {
var err error
iter, err = p.contract.FilterWithdrawalProven(opts, nil, nil, nil)
return err
})
if err != nil {
return nil, err
}

defer iter.Close()
for iter.Next() {
wdsByBlockHash[iter.Event.Raw.BlockHash] = append(
wdsByBlockHash[iter.Event.Raw.BlockHash], db.ProvenWithdrawal{
WithdrawalHash: iter.Event.WithdrawalHash,
From: iter.Event.From,
To: iter.Event.To,
TxHash: iter.Event.Raw.TxHash,
LogIndex: iter.Event.Raw.Index,
},
)
}

return wdsByBlockHash, iter.Error()
}

func (p *Portal) GetFinalizedWithdrawalsByBlockRange(ctx context.Context, start, end uint64) (FinalizedWithdrawalsMap, error) {
wdsByBlockHash := make(FinalizedWithdrawalsMap)
opts := &bind.FilterOpts{
Expand Down
14 changes: 13 additions & 1 deletion indexer/services/l1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func (s *Service) Update(newHeader *types.Header) error {
}()

bridgeDepositsCh := make(chan bridge.DepositsMap, len(s.bridges))
provenWithdrawalsCh := make(chan bridge.ProvenWithdrawalsMap, 1)
finalizedWithdrawalsCh := make(chan bridge.FinalizedWithdrawalsMap, 1)
errCh := make(chan error, len(s.bridges)+1)

Expand All @@ -259,6 +260,14 @@ func (s *Service) Update(newHeader *types.Header) error {
bridgeDepositsCh <- deposits
}(bridgeImpl)
}
go func() {
provenWithdrawals, err := s.portal.GetProvenWithdrawalsByBlockRange(s.ctx, startHeight, endHeight)
if err != nil {
errCh <- err
return
}
provenWithdrawalsCh <- provenWithdrawals
}()
go func() {
finalizedWithdrawals, err := s.portal.GetFinalizedWithdrawalsByBlockRange(s.ctx, startHeight, endHeight)
if err != nil {
Expand Down Expand Up @@ -291,6 +300,7 @@ func (s *Service) Update(newHeader *types.Header) error {
}
}

provenWithdrawalsByBlockHash := <-provenWithdrawalsCh
finalizedWithdrawalsByBlockHash := <-finalizedWithdrawalsCh

var stateBatches map[common.Hash][]db.StateBatch
Expand All @@ -307,11 +317,12 @@ func (s *Service) Update(newHeader *types.Header) error {
number := header.Number.Uint64()
deposits := depositsByBlockHash[blockHash]
batches := stateBatches[blockHash]
provenWds := provenWithdrawalsByBlockHash[blockHash]
finalizedWds := finalizedWithdrawalsByBlockHash[blockHash]

// Always record block data in the last block
// in the list of headers
if len(deposits) == 0 && len(batches) == 0 && len(finalizedWds) == 0 && i != len(headers)-1 {
if len(deposits) == 0 && len(batches) == 0 && len(provenWds) == 0 && len(finalizedWds) == 0 && i != len(headers)-1 {
continue
}

Expand All @@ -321,6 +332,7 @@ func (s *Service) Update(newHeader *types.Header) error {
Number: number,
Timestamp: header.Time,
Deposits: deposits,
ProvenWithdrawals: provenWds,
FinalizedWithdrawals: finalizedWds,
}

Expand Down

0 comments on commit 9e1b6fe

Please sign in to comment.