Skip to content

Commit

Permalink
Merge pull request #8297 from ethereum-optimism/indexer.db.ordering
Browse files Browse the repository at this point in the history
fix(indexer): versioned hash database ordering
  • Loading branch information
hamdiallam authored Nov 27, 2023
2 parents ec6a2bd + 17ba90a commit 4911356
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
14 changes: 5 additions & 9 deletions indexer/database/bridge_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type BridgeMessagesDB interface {
StoreL2BridgeMessages([]L2BridgeMessage) error
MarkRelayedL2BridgeMessage(common.Hash, uuid.UUID) error

StoreL2BridgeMessageV1MessageHash(common.Hash, common.Hash) error
StoreL2BridgeMessageV1MessageHashes([]L2BridgeMessageVersionedMessageHash) error
}

/**
Expand Down Expand Up @@ -141,15 +141,11 @@ func (db bridgeMessagesDB) StoreL2BridgeMessages(messages []L2BridgeMessage) err
return result.Error
}

func (db bridgeMessagesDB) StoreL2BridgeMessageV1MessageHash(msgHash, v1MsgHash common.Hash) error {
if msgHash == v1MsgHash {
return fmt.Errorf("message hash is equal to the v1 message: %s", msgHash)
}

func (db bridgeMessagesDB) StoreL2BridgeMessageV1MessageHashes(versionedHashes []L2BridgeMessageVersionedMessageHash) error {
deduped := db.gorm.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "message_hash"}}, DoNothing: true})
result := deduped.Create(&L2BridgeMessageVersionedMessageHash{MessageHash: msgHash, V1MessageHash: v1MsgHash})
if result.Error == nil && int(result.RowsAffected) < 1 {
db.log.Warn("ignored L2 bridge v1 message hash duplicates")
result := deduped.Create(&versionedHashes)
if result.Error == nil && int(result.RowsAffected) < len(versionedHashes) {
db.log.Warn("ignored L2 bridge v1 message hash duplicates", "duplicates", len(versionedHashes)-int(result.RowsAffected))
}

return result.Error
Expand Down
17 changes: 9 additions & 8 deletions indexer/processors/bridge/legacy_bridge_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,22 @@ func LegacyL2ProcessInitiatedBridgeEvents(log log.Logger, db *database.DB, metri
withdrawnWEI := bigint.Zero
sentMessages := make(map[logKey]sentMessageEvent, len(crossDomainSentMessages))
bridgeMessages := make([]database.L2BridgeMessage, len(crossDomainSentMessages))
versionedMessageHashes := make([]database.L2BridgeMessageVersionedMessageHash, len(sentMessages))
transactionWithdrawals := make([]database.L2TransactionWithdrawal, len(crossDomainSentMessages))
for i := range crossDomainSentMessages {
sentMessage := crossDomainSentMessages[i]
withdrawnWEI = new(big.Int).Add(withdrawnWEI, sentMessage.BridgeMessage.Tx.Amount)

// Since these message can be relayed in bedrock, we utilize the migrated withdrawal hash
// and also store the v1 version of the message hash such that the bedrock l1 finalization
// processor works as expected
// processor works as expected. There will be some entries relayed pre-bedrock that will not
// require the entry but we'll store it anyways as a large % of these withdrawals are not relayed
// pre-bedrock.

v1MessageHash, err := legacyBridgeMessageV1MessageHash(&sentMessage.BridgeMessage)
if err != nil {
return fmt.Errorf("failed to compute versioned message hash: %w", err)
}
if err := db.BridgeMessages.StoreL2BridgeMessageV1MessageHash(sentMessage.BridgeMessage.MessageHash, v1MessageHash); err != nil {
return err
}

withdrawalHash, err := legacyBridgeMessageWithdrawalHash(preset, &sentMessage.BridgeMessage)
if err != nil {
Expand All @@ -203,10 +203,8 @@ func LegacyL2ProcessInitiatedBridgeEvents(log log.Logger, db *database.DB, metri
}

sentMessages[logKey{sentMessage.Event.BlockHash, sentMessage.Event.LogIndex}] = sentMessageEvent{&sentMessage, withdrawalHash}
bridgeMessages[i] = database.L2BridgeMessage{
TransactionWithdrawalHash: sentMessage.BridgeMessage.MessageHash,
BridgeMessage: sentMessage.BridgeMessage,
}
bridgeMessages[i] = database.L2BridgeMessage{TransactionWithdrawalHash: withdrawalHash, BridgeMessage: sentMessage.BridgeMessage}
versionedMessageHashes[i] = database.L2BridgeMessageVersionedMessageHash{MessageHash: sentMessage.BridgeMessage.MessageHash, V1MessageHash: v1MessageHash}
}
if len(bridgeMessages) > 0 {
if err := db.BridgeTransactions.StoreL2TransactionWithdrawals(transactionWithdrawals); err != nil {
Expand All @@ -215,6 +213,9 @@ func LegacyL2ProcessInitiatedBridgeEvents(log log.Logger, db *database.DB, metri
if err := db.BridgeMessages.StoreL2BridgeMessages(bridgeMessages); err != nil {
return err
}
if err := db.BridgeMessages.StoreL2BridgeMessageV1MessageHashes(versionedMessageHashes); err != nil {
return err
}

withdrawnETH, _ := bigint.WeiToETH(withdrawnWEI).Float64()
metrics.RecordL2TransactionWithdrawals(len(transactionWithdrawals), withdrawnETH)
Expand Down

0 comments on commit 4911356

Please sign in to comment.