Skip to content
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
11 changes: 7 additions & 4 deletions bridge-history-api/cmd/fetcher/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/urfave/cli/v2"

"scroll-tech/common/database"
Expand Down Expand Up @@ -43,15 +44,17 @@ func action(ctx *cli.Context) error {
subCtx, cancel := context.WithCancel(ctx.Context)
defer cancel()

l1Client, err := ethclient.Dial(cfg.L1.Endpoint)
l1RPCClient, err := rpc.Dial(cfg.L1.Endpoint)
if err != nil {
log.Crit("failed to connect to L1 geth", "endpoint", cfg.L1.Endpoint, "err", err)
}
l1Client := ethclient.NewClient(l1RPCClient)

l2Client, err := ethclient.Dial(cfg.L2.Endpoint)
l2RPCClient, err := rpc.Dial(cfg.L2.Endpoint)
if err != nil {
log.Crit("failed to connect to L2 geth", "endpoint", cfg.L2.Endpoint, "err", err)
}
l2Client := ethclient.NewClient(l2RPCClient)

db, err := database.InitDB(cfg.DB)
if err != nil {
Expand All @@ -68,13 +71,13 @@ func action(ctx *cli.Context) error {

observability.Server(ctx, db)

l1MessageFetcher, err := fetcher.NewL1MessageFetcher(subCtx, cfg.L1, db, l1Client)
l1MessageFetcher, err := fetcher.NewL1MessageFetcher(subCtx, cfg.L1, db, l1Client, l1RPCClient)
if err != nil {
log.Crit("failed to create L1MessageFetcher", "err", err)
}
go l1MessageFetcher.Start()

l2MessageFetcher := fetcher.NewL2MessageFetcher(subCtx, cfg.L2, db, l2Client)
l2MessageFetcher := fetcher.NewL2MessageFetcher(subCtx, cfg.L2, db, l2Client, l2RPCClient)
go l2MessageFetcher.Start()

// Catch CTRL-C to ensure a graceful shutdown.
Expand Down
5 changes: 0 additions & 5 deletions bridge-history-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ require (
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde
)

// Hotfix for header hash incompatibility issue.
// PR: https://github.com/scroll-tech/go-ethereum/pull/1133/
// CAUTION: Requires careful handling. When upgrading go-ethereum, ensure this fix remains up-to-date in this branch.
replace github.com/scroll-tech/go-ethereum => github.com/scroll-tech/go-ethereum v1.10.14-0.20251128092359-25d5bf6b817b

require (
dario.cat/mergo v1.0.0 // indirect
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions bridge-history-api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/scroll-tech/da-codec v0.10.1 h1:PhKP6ZhYRTZ6q8EgzCBy/a4yB8PGIDewBgXhRkTbFUw=
github.com/scroll-tech/da-codec v0.10.1/go.mod h1:MBlIP4wCXPcUDZ/Ci2B7n/2IbVU1WBo9OTFTZ5ffE0U=
github.com/scroll-tech/go-ethereum v1.10.14-0.20251128092359-25d5bf6b817b h1:pMQKnroJoS/FeL1aOWkz7/u1iBHUP8PWjZstNuzoUGE=
github.com/scroll-tech/go-ethereum v1.10.14-0.20251128092359-25d5bf6b817b/go.mod h1:Aa/kD1XB+OV/7rRxMQrjcPCB4b0pKyLH0gsTrtuHi38=
github.com/scroll-tech/go-ethereum v1.10.14-0.20251128092113-8629f088d78f h1:j6SjP98MoWFFX9TwB1/nFYEkayqHQsrtE66Ll2C+oT0=
github.com/scroll-tech/go-ethereum v1.10.14-0.20251128092113-8629f088d78f/go.mod h1:Aa/kD1XB+OV/7rRxMQrjcPCB4b0pKyLH0gsTrtuHi38=
github.com/scroll-tech/zktrie v0.8.4 h1:UagmnZ4Z3ITCk+aUq9NQZJNAwnWl4gSxsLb2Nl7IgRE=
github.com/scroll-tech/zktrie v0.8.4/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
Expand Down
20 changes: 11 additions & 9 deletions bridge-history-api/internal/controller/fetcher/l1_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fetcher
import (
"context"
"fmt"
"math/big"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rollup/da_syncer/blob_client"
"github.com/scroll-tech/go-ethereum/rpc"
"gorm.io/gorm"

"scroll-tech/bridge-history-api/internal/config"
Expand All @@ -21,9 +21,10 @@ import (

// L1MessageFetcher fetches cross message events from L1 and saves them to database.
type L1MessageFetcher struct {
ctx context.Context
cfg *config.FetcherConfig
client *ethclient.Client
ctx context.Context
cfg *config.FetcherConfig
client *ethclient.Client
rpcClient *rpc.Client

l1SyncHeight uint64
l1LastSyncBlockHash common.Hash
Expand All @@ -37,7 +38,7 @@ type L1MessageFetcher struct {
}

// NewL1MessageFetcher creates a new L1MessageFetcher instance.
func NewL1MessageFetcher(ctx context.Context, cfg *config.FetcherConfig, db *gorm.DB, client *ethclient.Client) (*L1MessageFetcher, error) {
func NewL1MessageFetcher(ctx context.Context, cfg *config.FetcherConfig, db *gorm.DB, client *ethclient.Client, rpcClient *rpc.Client) (*L1MessageFetcher, error) {
blobClient := blob_client.NewBlobClients()
if cfg.AwsS3Endpoint != "" {
blobClient.AddBlobClient(blob_client.NewAwsS3Client(cfg.AwsS3Endpoint))
Expand All @@ -64,8 +65,9 @@ func NewL1MessageFetcher(ctx context.Context, cfg *config.FetcherConfig, db *gor
ctx: ctx,
cfg: cfg,
client: client,
rpcClient: rpcClient,
eventUpdateLogic: logic.NewEventUpdateLogic(db, true),
l1FetcherLogic: logic.NewL1FetcherLogic(cfg, db, client, blobClient),
l1FetcherLogic: logic.NewL1FetcherLogic(cfg, db, client, rpcClient, blobClient),
}

reg := prometheus.DefaultRegisterer
Expand Down Expand Up @@ -112,13 +114,13 @@ func (c *L1MessageFetcher) Start() {
l1SyncHeight -= logic.L1ReorgSafeDepth
}

header, err := c.client.HeaderByNumber(c.ctx, new(big.Int).SetUint64(l1SyncHeight))
block, err := utils.GetBlockByNumber(c.ctx, c.rpcClient, l1SyncHeight)
if err != nil {
log.Crit("failed to get L1 header by number", "block number", l1SyncHeight, "err", err)
log.Crit("failed to get L1 block by number", "block number", l1SyncHeight, "err", err)
return
}

c.updateL1SyncHeight(l1SyncHeight, header.Hash())
c.updateL1SyncHeight(l1SyncHeight, block.Hash)

log.Info("Start L1 message fetcher",
"message synced height", messageSyncedHeight,
Expand Down
14 changes: 8 additions & 6 deletions bridge-history-api/internal/controller/fetcher/l2_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package fetcher

import (
"context"
"math/big"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rpc"
"gorm.io/gorm"

"scroll-tech/bridge-history-api/internal/config"
Expand All @@ -23,6 +23,7 @@ type L2MessageFetcher struct {
cfg *config.FetcherConfig
db *gorm.DB
client *ethclient.Client
rpcClient *rpc.Client
l2SyncHeight uint64
l2LastSyncBlockHash common.Hash

Expand All @@ -35,14 +36,15 @@ type L2MessageFetcher struct {
}

// NewL2MessageFetcher creates a new L2MessageFetcher instance.
func NewL2MessageFetcher(ctx context.Context, cfg *config.FetcherConfig, db *gorm.DB, client *ethclient.Client) *L2MessageFetcher {
func NewL2MessageFetcher(ctx context.Context, cfg *config.FetcherConfig, db *gorm.DB, client *ethclient.Client, rpcClient *rpc.Client) *L2MessageFetcher {
c := &L2MessageFetcher{
ctx: ctx,
cfg: cfg,
db: db,
client: client,
rpcClient: rpcClient,
eventUpdateLogic: logic.NewEventUpdateLogic(db, false),
l2FetcherLogic: logic.NewL2FetcherLogic(cfg, db, client),
l2FetcherLogic: logic.NewL2FetcherLogic(cfg, db, client, rpcClient),
}

reg := prometheus.DefaultRegisterer
Expand Down Expand Up @@ -82,13 +84,13 @@ func (c *L2MessageFetcher) Start() {
l2SyncHeight -= logic.L2ReorgSafeDepth
}

header, err := c.client.HeaderByNumber(c.ctx, new(big.Int).SetUint64(l2SyncHeight))
block, err := utils.GetBlockByNumber(c.ctx, c.rpcClient, l2SyncHeight)
if err != nil {
log.Crit("failed to get L2 header by number", "block number", l2SyncHeight, "err", err)
log.Crit("failed to get L2 block by number", "block number", l2SyncHeight, "err", err)
return
}

c.updateL2SyncHeight(l2SyncHeight, header.Hash())
c.updateL2SyncHeight(l2SyncHeight, block.Hash)

log.Info("Start L2 message fetcher", "l2 sent message synced height", l2SentMessageSyncedHeight,
"bridge batch deposit synced height", l2BridgeBatchDepositSyncedHeight, "sync start height", l2SyncHeight+1)
Expand Down
33 changes: 18 additions & 15 deletions bridge-history-api/internal/logic/l1_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rollup/da_syncer/blob_client"
"github.com/scroll-tech/go-ethereum/rpc"
"gorm.io/gorm"

backendabi "scroll-tech/bridge-history-api/abi"
Expand Down Expand Up @@ -39,6 +40,7 @@ type L1FilterResult struct {
type L1FetcherLogic struct {
cfg *config.FetcherConfig
client *ethclient.Client
rpcClient *rpc.Client
addressList []common.Address
gatewayList []common.Address
parser *L1EventParser
Expand All @@ -50,7 +52,7 @@ type L1FetcherLogic struct {
}

// NewL1FetcherLogic creates L1 fetcher logic
func NewL1FetcherLogic(cfg *config.FetcherConfig, db *gorm.DB, client *ethclient.Client, blobClient blob_client.BlobClient) *L1FetcherLogic {
func NewL1FetcherLogic(cfg *config.FetcherConfig, db *gorm.DB, client *ethclient.Client, rpcClient *rpc.Client, blobClient blob_client.BlobClient) *L1FetcherLogic {
addressList := []common.Address{
common.HexToAddress(cfg.StandardERC20GatewayAddr),
common.HexToAddress(cfg.CustomERC20GatewayAddr),
Expand Down Expand Up @@ -132,6 +134,7 @@ func NewL1FetcherLogic(cfg *config.FetcherConfig, db *gorm.DB, client *ethclient
batchEventOrm: orm.NewBatchEvent(db),
cfg: cfg,
client: client,
rpcClient: rpcClient,
addressList: addressList,
gatewayList: gatewayList,
parser: NewL1EventParser(cfg, client, blobClient),
Expand All @@ -146,38 +149,38 @@ func NewL1FetcherLogic(cfg *config.FetcherConfig, db *gorm.DB, client *ethclient
return f
}

func (f *L1FetcherLogic) getBlocksAndDetectReorg(ctx context.Context, from, to uint64, lastBlockHash common.Hash) (bool, uint64, common.Hash, []*types.Block, error) {
blocks, err := utils.GetBlocksInRange(ctx, f.client, from, to)
func (f *L1FetcherLogic) getBlocksAndDetectReorg(ctx context.Context, from, to uint64, lastBlockHash common.Hash) (bool, uint64, common.Hash, []*utils.Block, error) {
blocks, err := utils.GetBlocksInRange(ctx, f.rpcClient, from, to)
if err != nil {
log.Error("failed to get L1 blocks in range", "from", from, "to", to, "err", err)
return false, 0, common.Hash{}, nil, err
}

for _, block := range blocks {
if block.ParentHash() != lastBlockHash {
log.Warn("L1 reorg detected", "reorg height", block.NumberU64()-1, "expected hash", block.ParentHash().String(), "local hash", lastBlockHash.String())
if block.ParentHash != lastBlockHash {
log.Warn("L1 reorg detected", "reorg height", block.Number-1, "expected hash", block.ParentHash.String(), "local hash", lastBlockHash.String())
var resyncHeight uint64
if block.NumberU64() > L1ReorgSafeDepth+1 {
resyncHeight = block.NumberU64() - L1ReorgSafeDepth - 1
if block.Number > L1ReorgSafeDepth+1 {
resyncHeight = block.Number - L1ReorgSafeDepth - 1
}
header, err := f.client.HeaderByNumber(ctx, new(big.Int).SetUint64(resyncHeight))
resyncBlock, err := utils.GetBlockByNumber(ctx, f.rpcClient, resyncHeight)
if err != nil {
log.Error("failed to get L1 header by number", "block number", resyncHeight, "err", err)
log.Error("failed to get L1 block by number", "block number", resyncHeight, "err", err)
return false, 0, common.Hash{}, nil, err
}
return true, resyncHeight, header.Hash(), nil, nil
return true, resyncHeight, resyncBlock.Hash, nil, nil
}
lastBlockHash = block.Hash()
lastBlockHash = block.Hash
}

return false, 0, lastBlockHash, blocks, nil
}

func (f *L1FetcherLogic) getRevertedTxs(ctx context.Context, from, to uint64, blocks []*types.Block) ([]*orm.CrossMessage, error) {
func (f *L1FetcherLogic) getRevertedTxs(ctx context.Context, from, to uint64, blocks []*utils.Block) ([]*orm.CrossMessage, error) {
var l1RevertedTxs []*orm.CrossMessage
for i := from; i <= to; i++ {
block := blocks[i-from]
for _, tx := range block.Transactions() {
for _, tx := range block.Transactions {
// Gateways: L1 deposit.
// Messenger: L1 deposit retry (replayMessage), L1 deposit refund (dropMessage), L2 withdrawal's claim (relayMessageWithProof).
if !isTransactionToGateway(tx, f.gatewayList) {
Expand Down Expand Up @@ -209,7 +212,7 @@ func (f *L1FetcherLogic) getRevertedTxs(ctx context.Context, from, to uint64, bl
Sender: sender.String(),
Receiver: (*tx.To()).String(),
L1BlockNumber: receipt.BlockNumber.Uint64(),
BlockTimestamp: block.Time(),
BlockTimestamp: block.Time,
TxStatus: int(btypes.TxStatusTypeSentTxReverted),
})
}
Expand Down Expand Up @@ -275,7 +278,7 @@ func (f *L1FetcherLogic) L1Fetcher(ctx context.Context, from, to uint64, lastBlo
// Map block number to block timestamp to avoid fetching block header multiple times to get block timestamp.
blockTimestampsMap := make(map[uint64]uint64)
for _, block := range blocks {
blockTimestampsMap[block.NumberU64()] = block.Time()
blockTimestampsMap[block.Number] = block.Time
}

eventLogs, err := f.l1FetcherLogs(ctx, from, to)
Expand Down
33 changes: 18 additions & 15 deletions bridge-history-api/internal/logic/l2_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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"
"gorm.io/gorm"

backendabi "scroll-tech/bridge-history-api/abi"
Expand All @@ -37,6 +38,7 @@ type L2FilterResult struct {
type L2FetcherLogic struct {
cfg *config.FetcherConfig
client *ethclient.Client
rpcClient *rpc.Client
addressList []common.Address
gatewayList []common.Address
parser *L2EventParser
Expand All @@ -48,7 +50,7 @@ type L2FetcherLogic struct {
}

// NewL2FetcherLogic create L2 fetcher logic
func NewL2FetcherLogic(cfg *config.FetcherConfig, db *gorm.DB, client *ethclient.Client) *L2FetcherLogic {
func NewL2FetcherLogic(cfg *config.FetcherConfig, db *gorm.DB, client *ethclient.Client, rpcClient *rpc.Client) *L2FetcherLogic {
addressList := []common.Address{
common.HexToAddress(cfg.ETHGatewayAddr),

Expand Down Expand Up @@ -111,6 +113,7 @@ func NewL2FetcherLogic(cfg *config.FetcherConfig, db *gorm.DB, client *ethclient
batchEventOrm: orm.NewBatchEvent(db),
cfg: cfg,
client: client,
rpcClient: rpcClient,
addressList: addressList,
gatewayList: gatewayList,
parser: NewL2EventParser(cfg, client),
Expand All @@ -125,43 +128,43 @@ func NewL2FetcherLogic(cfg *config.FetcherConfig, db *gorm.DB, client *ethclient
return f
}

func (f *L2FetcherLogic) getBlocksAndDetectReorg(ctx context.Context, from, to uint64, lastBlockHash common.Hash) (bool, uint64, common.Hash, []*types.Block, error) {
blocks, err := utils.GetBlocksInRange(ctx, f.client, from, to)
func (f *L2FetcherLogic) getBlocksAndDetectReorg(ctx context.Context, from, to uint64, lastBlockHash common.Hash) (bool, uint64, common.Hash, []*utils.Block, error) {
blocks, err := utils.GetBlocksInRange(ctx, f.rpcClient, from, to)
if err != nil {
log.Error("failed to get L2 blocks in range", "from", from, "to", to, "err", err)
return false, 0, common.Hash{}, nil, err
}

for _, block := range blocks {
if block.ParentHash() != lastBlockHash {
log.Warn("L2 reorg detected", "reorg height", block.NumberU64()-1, "expected hash", block.ParentHash().String(), "local hash", lastBlockHash.String())
if block.ParentHash != lastBlockHash {
log.Warn("L2 reorg detected", "reorg height", block.Number-1, "expected hash", block.ParentHash.String(), "local hash", lastBlockHash.String())
var resyncHeight uint64
if block.NumberU64() > L2ReorgSafeDepth+1 {
resyncHeight = block.NumberU64() - L2ReorgSafeDepth - 1
if block.Number > L2ReorgSafeDepth+1 {
resyncHeight = block.Number - L2ReorgSafeDepth - 1
}
header, err := f.client.HeaderByNumber(ctx, new(big.Int).SetUint64(resyncHeight))
resyncBlock, err := utils.GetBlockByNumber(ctx, f.rpcClient, resyncHeight)
if err != nil {
log.Error("failed to get L2 header by number", "block number", resyncHeight, "err", err)
log.Error("failed to get L2 block by number", "block number", resyncHeight, "err", err)
return false, 0, common.Hash{}, nil, err
}
return true, resyncHeight, header.Hash(), nil, nil
return true, resyncHeight, resyncBlock.Hash, nil, nil
}
lastBlockHash = block.Hash()
lastBlockHash = block.Hash
}

return false, 0, lastBlockHash, blocks, nil
}

func (f *L2FetcherLogic) getRevertedTxs(ctx context.Context, from, to uint64, blocks []*types.Block) (map[uint64]uint64, []*orm.CrossMessage, []*orm.CrossMessage, error) {
func (f *L2FetcherLogic) getRevertedTxs(ctx context.Context, from, to uint64, blocks []*utils.Block) (map[uint64]uint64, []*orm.CrossMessage, []*orm.CrossMessage, error) {
var l2RevertedUserTxs []*orm.CrossMessage
var l2RevertedRelayedMessageTxs []*orm.CrossMessage
blockTimestampsMap := make(map[uint64]uint64)

for i := from; i <= to; i++ {
block := blocks[i-from]
blockTimestampsMap[block.NumberU64()] = block.Time()
blockTimestampsMap[block.Number] = block.Time

for _, tx := range block.Transactions() {
for _, tx := range block.Transactions {
if tx.IsL1MessageTx() {
receipt, receiptErr := f.client.TransactionReceipt(ctx, tx.Hash())
if receiptErr != nil {
Expand Down Expand Up @@ -208,7 +211,7 @@ func (f *L2FetcherLogic) getRevertedTxs(ctx context.Context, from, to uint64, bl
Sender: sender.String(),
Receiver: (*tx.To()).String(),
L2BlockNumber: receipt.BlockNumber.Uint64(),
BlockTimestamp: block.Time(),
BlockTimestamp: block.Time,
TxStatus: int(btypes.TxStatusTypeSentTxReverted),
})
}
Expand Down
Loading
Loading