diff --git a/bridge-history-api/conf/config.json b/bridge-history-api/conf/config.json index d8e0ad1f58..bc27421b96 100644 --- a/bridge-history-api/conf/config.json +++ b/bridge-history-api/conf/config.json @@ -1,6 +1,5 @@ { "L1": { - "chain_id": 1, "confirmation": 64, "endpoint": "https://rpc.ankr.com/eth", "startHeight": 18318215, @@ -21,7 +20,6 @@ "MessageQueueAddr": "0x0d7E906BD9cAFa154b048cFa766Cc1E54E39AF9B" }, "L2": { - "chain_id": 534352, "confirmation": 1, "endpoint": "http://mainnet-l2geth-internal-1.mainnet.scroll.tech:8545", "startHeight": 0, diff --git a/bridge-history-api/internal/config/config.go b/bridge-history-api/internal/config/config.go index b568d92999..bed5e5116b 100644 --- a/bridge-history-api/internal/config/config.go +++ b/bridge-history-api/internal/config/config.go @@ -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"` diff --git a/bridge-history-api/internal/controller/fetcher/l1_fetcher.go b/bridge-history-api/internal/controller/fetcher/l1_fetcher.go index c9bf2aa6c7..f5a45d7f7d 100644 --- a/bridge-history-api/internal/controller/fetcher/l1_fetcher.go +++ b/bridge-history-api/internal/controller/fetcher/l1_fetcher.go @@ -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), diff --git a/bridge-history-api/internal/controller/fetcher/l2_fetcher.go b/bridge-history-api/internal/controller/fetcher/l2_fetcher.go index d6af61bb45..6ef15268b0 100644 --- a/bridge-history-api/internal/controller/fetcher/l2_fetcher.go +++ b/bridge-history-api/internal/controller/fetcher/l2_fetcher.go @@ -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" @@ -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) @@ -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), @@ -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(), + }) + } + } } } @@ -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 diff --git a/bridge-history-api/internal/orm/batch_event.go b/bridge-history-api/internal/orm/batch_event.go index b559096032..47022ff344 100644 --- a/bridge-history-api/internal/orm/batch_event.go +++ b/bridge-history-api/internal/orm/batch_event.go @@ -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 { diff --git a/bridge-history-api/internal/orm/cross_message.go b/bridge-history-api/internal/orm/cross_message.go index c5dfd28627..aa19e8b8ac 100644 --- a/bridge-history-api/internal/orm/cross_message.go +++ b/bridge-history-api/internal/orm/cross_message.go @@ -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] @@ -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] @@ -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] @@ -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) } @@ -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{ @@ -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]