Skip to content

Commit

Permalink
add fetching reverted relayed messages and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Dec 7, 2023
1 parent 59c8470 commit 01d35d7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 33 deletions.
2 changes: 0 additions & 2 deletions bridge-history-api/conf/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"L1": {
"chain_id": 1,
"confirmation": 64,
"endpoint": "https://rpc.ankr.com/eth",
"startHeight": 18318215,
Expand All @@ -21,7 +20,6 @@
"MessageQueueAddr": "0x0d7E906BD9cAFa154b048cFa766Cc1E54E39AF9B"
},
"L2": {
"chain_id": 534352,
"confirmation": 1,
"endpoint": "http://mainnet-l2geth-internal-1.mainnet.scroll.tech:8545",
"startHeight": 0,
Expand Down
1 change: 0 additions & 1 deletion bridge-history-api/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

// LayerConfig is the configuration of Layer1/Layer2
type LayerConfig struct {
ChainID uint64 `json:"chain_id"`
Confirmation uint64 `json:"confirmation"`
Endpoint string `json:"endpoint"`
StartHeight uint64 `json:"startHeight"`
Expand Down
15 changes: 7 additions & 8 deletions bridge-history-api/internal/controller/fetcher/l1_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,18 @@ func (c *L1MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64
if toAddress == c.cfg.GatewayRouterAddr {
receipt, err := c.client.TransactionReceipt(ctx, tx.Hash())
if err != nil {
log.Error("Failed to get transaction receipt", "txHash", tx.Hash(), "err", err)
return err
}

signer := types.NewLondonSigner(new(big.Int).SetUint64(c.cfg.ChainID))
sender, err := signer.Sender(tx)
if err != nil {
log.Error("get sender failed", "chain id", c.cfg.ChainID, "tx hash", tx.Hash().String(), "err", err)
log.Error("Failed to get transaction receipt", "txHash", tx.Hash().String(), "err", err)
return err
}

// Check if the transaction failed
if receipt.Status == types.ReceiptStatusFailed {
signer := types.NewLondonSigner(new(big.Int).SetUint64(tx.ChainId().Uint64()))
sender, err := signer.Sender(tx)
if err != nil {
log.Error("get sender failed", "chain id", tx.ChainId().Uint64(), "tx hash", tx.Hash().String(), "err", err)
return err
}
l1FailedGatewayRouterTxs = append(l1FailedGatewayRouterTxs, &orm.CrossMessage{
L1TxHash: tx.Hash().String(),
MessageType: int(orm.MessageTypeL1SentMessage),
Expand Down
37 changes: 29 additions & 8 deletions bridge-history-api/internal/controller/fetcher/l2_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/scroll-tech/go-ethereum"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/crypto"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rpc"
Expand Down Expand Up @@ -116,6 +117,7 @@ func (c *L2MessageFetcher) fetchAndSaveEvents(confirmation uint64) {
func (c *L2MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64, to uint64, addrList []common.Address) error {
log.Info("fetch and save L2 events", "from", from, "to", to)
var l2FailedGatewayRouterTxs []*orm.CrossMessage
var l2RevertedRelayedMessages []*orm.CrossMessage
blockTimestampsMap := make(map[uint64]uint64)
for number := from; number <= to; number++ {
blockNumber := new(big.Int).SetUint64(number)
Expand All @@ -135,19 +137,18 @@ func (c *L2MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64
if toAddress == c.cfg.GatewayRouterAddr {
receipt, err := c.client.TransactionReceipt(ctx, tx.Hash())
if err != nil {
log.Error("Failed to get transaction receipt", "txHash", tx.Hash(), "err", err)
return err
}

signer := types.NewLondonSigner(new(big.Int).SetUint64(c.cfg.ChainID))
sender, err := signer.Sender(tx)
if err != nil {
log.Error("get sender failed", "chain id", c.cfg.ChainID, "tx hash", tx.Hash().String(), "err", err)
log.Error("Failed to get transaction receipt", "txHash", tx.Hash().String(), "err", err)
return err
}

// Check if the transaction failed
if receipt.Status == types.ReceiptStatusFailed {
signer := types.NewLondonSigner(new(big.Int).SetUint64(tx.ChainId().Uint64()))
sender, err := signer.Sender(tx)
if err != nil {
log.Error("get sender failed", "chain id", tx.ChainId().Uint64(), "tx hash", tx.Hash().String(), "err", err)
return err
}
l2FailedGatewayRouterTxs = append(l2FailedGatewayRouterTxs, &orm.CrossMessage{
L2TxHash: tx.Hash().String(),
MessageType: int(orm.MessageTypeL2SentMessage),
Expand All @@ -158,6 +159,22 @@ func (c *L2MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64
})
}
}
if tx.Type() == types.L1MessageTxType {
receipt, err := c.client.TransactionReceipt(ctx, tx.Hash())
if err != nil {
log.Error("Failed to get transaction receipt", "txHash", tx.Hash().String(), "err", err)
return err
}
// Check if the transaction failed
if receipt.Status == types.ReceiptStatusFailed {
l2RevertedRelayedMessages = append(l2RevertedRelayedMessages, &orm.CrossMessage{
MessageHash: common.Bytes2Hex(crypto.Keccak256(tx.AsL1MessageTx().Data)),
L2TxHash: tx.Hash().String(),
TxStatus: int(orm.TxStatusTypeRelayedFailed),
L2BlockNumber: receipt.BlockNumber.Uint64(),
})
}
}
}
}

Expand Down Expand Up @@ -200,6 +217,10 @@ func (c *L2MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64
log.Error("failed to update L2 relayed messages of L1 deposits", "from", from, "to", to, "err", txErr)
return txErr
}
if txErr := c.crossMessageOrm.InsertOrUpdateL2RelayedMessagesOfL1Deposits(ctx, l2RevertedRelayedMessages, tx); txErr != nil {
log.Error("failed to insert L2 reverted relayed messages of L1 deposits", "from", from, "to", to, "err", txErr)
return txErr
}
if txErr := c.crossMessageOrm.InsertFailedGatewayRouterTxs(ctx, l2FailedGatewayRouterTxs, tx); txErr != nil {
log.Error("failed to insert L2 failed gateway router transactions", "from", from, "to", to, "err", txErr)
return txErr
Expand Down
12 changes: 0 additions & 12 deletions bridge-history-api/internal/orm/batch_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ func (c *BatchEvent) GetBatchesGEBlockHeight(ctx context.Context, blockHeight ui
return batches, nil
}

// GetBatchByIndex returns the batch by index.
func (c *BatchEvent) GetBatchByIndex(ctx context.Context, index uint64) (*BatchEvent, error) {
var result BatchEvent
db := c.db.WithContext(ctx)
db = db.Model(&BatchEvent{})
db = db.Where("batch_index = ?", index)
if err := db.First(&result).Error; err != nil {
return nil, fmt.Errorf("failed to get batch by index, error: %w", err)
}
return &result, nil
}

// InsertOrUpdateBatchEvents inserts a new batch event or updates an existing one based on the BatchStatusType.
func (c *BatchEvent) InsertOrUpdateBatchEvents(ctx context.Context, l1BatchEvents []*BatchEvent, dbTX ...*gorm.DB) error {
for _, l1BatchEvent := range l1BatchEvents {
Expand Down
18 changes: 16 additions & 2 deletions bridge-history-api/internal/orm/cross_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ func (c *CrossMessage) UpdateBatchStatusOfL2Withdrawals(ctx context.Context, sta

// InsertOrUpdateL1Messages inserts or updates a list of L1 cross messages into the database.
func (c *CrossMessage) InsertOrUpdateL1Messages(ctx context.Context, messages []*CrossMessage, dbTX ...*gorm.DB) error {
if len(messages) == 0 {
return nil
}
db := c.db
if len(dbTX) > 0 && dbTX[0] != nil {
db = dbTX[0]
Expand All @@ -317,6 +320,9 @@ func (c *CrossMessage) InsertOrUpdateL1Messages(ctx context.Context, messages []

// InsertOrUpdateL2Messages inserts or updates a list of L2 cross messages into the database.
func (c *CrossMessage) InsertOrUpdateL2Messages(ctx context.Context, messages []*CrossMessage, dbTX ...*gorm.DB) error {
if len(messages) == 0 {
return nil
}
db := c.db
if len(dbTX) > 0 && dbTX[0] != nil {
db = dbTX[0]
Expand All @@ -339,6 +345,9 @@ func (c *CrossMessage) InsertOrUpdateL2Messages(ctx context.Context, messages []
// These failed transactions are only fetched once, so they are inserted without checking for duplicates.
// To resolve unique index confliction, a random UUID will be generated and used as the MessageHash.
func (c *CrossMessage) InsertFailedGatewayRouterTxs(ctx context.Context, messages []*CrossMessage, dbTX ...*gorm.DB) error {
if len(messages) == 0 {
return nil
}
db := c.db
if len(dbTX) > 0 && dbTX[0] != nil {
db = dbTX[0]
Expand All @@ -347,8 +356,7 @@ func (c *CrossMessage) InsertFailedGatewayRouterTxs(ctx context.Context, message
db = db.WithContext(ctx)
db = db.Model(&CrossMessage{})
for _, message := range messages {
u := uuid.New()
message.MessageHash = u.String()
message.MessageHash = uuid.New().String()
if err := db.Create(message).Error; err != nil {
return fmt.Errorf("failed to insert failed gateway router txs, message: %+v, error: %w", message, err)
}
Expand All @@ -358,6 +366,9 @@ func (c *CrossMessage) InsertFailedGatewayRouterTxs(ctx context.Context, message

// InsertOrUpdateL2RelayedMessagesOfL1Deposits inserts or updates the database with a list of L2 relayed messages related to L1 deposits.
func (c *CrossMessage) InsertOrUpdateL2RelayedMessagesOfL1Deposits(ctx context.Context, l2RelayedMessages []*CrossMessage, dbTX ...*gorm.DB) error {
if len(l2RelayedMessages) == 0 {
return nil
}
db := c.db.WithContext(ctx)
db = db.Model(&CrossMessage{})
db = db.Clauses(clause.OnConflict{
Expand All @@ -375,6 +386,9 @@ func (c *CrossMessage) InsertOrUpdateL2RelayedMessagesOfL1Deposits(ctx context.C

// InsertOrUpdateL1RelayedMessagesOfL2Withdrawals inserts or updates the database with a list of L1 relayed messages related to L2 withdrawals.
func (c *CrossMessage) InsertOrUpdateL1RelayedMessagesOfL2Withdrawals(ctx context.Context, l1RelayedMessages []*CrossMessage, dbTX ...*gorm.DB) error {
if len(l1RelayedMessages) == 0 {
return nil
}
db := c.db
if len(dbTX) > 0 && dbTX[0] != nil {
db = dbTX[0]
Expand Down

0 comments on commit 01d35d7

Please sign in to comment.