From d47f1e3657ac8bc4adcad33e9c277574c1f898d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Garamv=C3=B6lgyi?= Date: Thu, 22 Oct 2026 12:48:55 +0200 Subject: [PATCH] fix: restore unpatched SDK, read hash from RPC response --- bridge-history-api/cmd/fetcher/app/app.go | 11 ++- bridge-history-api/go.mod | 5 -- bridge-history-api/go.sum | 4 +- .../internal/controller/fetcher/l1_fetcher.go | 20 +++-- .../internal/controller/fetcher/l2_fetcher.go | 14 +-- .../internal/logic/l1_fetcher.go | 33 +++---- .../internal/logic/l2_fetcher.go | 33 +++---- bridge-history-api/internal/utils/utils.go | 60 ++++++++++++- .../internal/utils/utils_test.go | 89 +++++++++++++++++++ build/.golangci.yml | 19 ++++ common/version/version.go | 2 +- .../internal/controller/watcher/l1_watcher.go | 28 +++--- .../controller/watcher/l1_watcher_test.go | 37 +++----- rollup/internal/utils/utils.go | 2 +- 14 files changed, 261 insertions(+), 96 deletions(-) diff --git a/bridge-history-api/cmd/fetcher/app/app.go b/bridge-history-api/cmd/fetcher/app/app.go index abb11002c8..6fecac8459 100644 --- a/bridge-history-api/cmd/fetcher/app/app.go +++ b/bridge-history-api/cmd/fetcher/app/app.go @@ -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" @@ -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 { @@ -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. diff --git a/bridge-history-api/go.mod b/bridge-history-api/go.mod index 4705c44625..19b94066fb 100644 --- a/bridge-history-api/go.mod +++ b/bridge-history-api/go.mod @@ -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 diff --git a/bridge-history-api/go.sum b/bridge-history-api/go.sum index 37c07cde11..ad2afe595e 100644 --- a/bridge-history-api/go.sum +++ b/bridge-history-api/go.sum @@ -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= diff --git a/bridge-history-api/internal/controller/fetcher/l1_fetcher.go b/bridge-history-api/internal/controller/fetcher/l1_fetcher.go index 35ada5d9f9..c8790d0e54 100644 --- a/bridge-history-api/internal/controller/fetcher/l1_fetcher.go +++ b/bridge-history-api/internal/controller/fetcher/l1_fetcher.go @@ -3,7 +3,6 @@ package fetcher import ( "context" "fmt" - "math/big" "time" "github.com/prometheus/client_golang/prometheus" @@ -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" @@ -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 @@ -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)) @@ -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 @@ -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, diff --git a/bridge-history-api/internal/controller/fetcher/l2_fetcher.go b/bridge-history-api/internal/controller/fetcher/l2_fetcher.go index 2eba55c0eb..83ccd9b2f6 100644 --- a/bridge-history-api/internal/controller/fetcher/l2_fetcher.go +++ b/bridge-history-api/internal/controller/fetcher/l2_fetcher.go @@ -2,7 +2,6 @@ package fetcher import ( "context" - "math/big" "time" "github.com/prometheus/client_golang/prometheus" @@ -10,6 +9,7 @@ import ( "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" @@ -23,6 +23,7 @@ type L2MessageFetcher struct { cfg *config.FetcherConfig db *gorm.DB client *ethclient.Client + rpcClient *rpc.Client l2SyncHeight uint64 l2LastSyncBlockHash common.Hash @@ -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 @@ -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) diff --git a/bridge-history-api/internal/logic/l1_fetcher.go b/bridge-history-api/internal/logic/l1_fetcher.go index 1312f4edf6..028cefc4ef 100644 --- a/bridge-history-api/internal/logic/l1_fetcher.go +++ b/bridge-history-api/internal/logic/l1_fetcher.go @@ -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" @@ -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 @@ -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), @@ -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), @@ -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) { @@ -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), }) } @@ -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) diff --git a/bridge-history-api/internal/logic/l2_fetcher.go b/bridge-history-api/internal/logic/l2_fetcher.go index 6f581fc931..0b86036ec6 100644 --- a/bridge-history-api/internal/logic/l2_fetcher.go +++ b/bridge-history-api/internal/logic/l2_fetcher.go @@ -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" @@ -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 @@ -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), @@ -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), @@ -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 { @@ -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), }) } diff --git a/bridge-history-api/internal/utils/utils.go b/bridge-history-api/internal/utils/utils.go index 2879ce7d28..b9d9ea3606 100644 --- a/bridge-history-api/internal/utils/utils.go +++ b/bridge-history-api/internal/utils/utils.go @@ -8,12 +8,15 @@ import ( "math/big" "strings" + "github.com/scroll-tech/go-ethereum" "github.com/scroll-tech/go-ethereum/accounts/abi" "github.com/scroll-tech/go-ethereum/common" + "github.com/scroll-tech/go-ethereum/common/hexutil" "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" "golang.org/x/sync/errgroup" backendabi "scroll-tech/bridge-history-api/abi" @@ -180,22 +183,71 @@ func GetParentBatchHashFromCalldata(txData []byte) (common.Hash, error) { return common.Hash{}, fmt.Errorf("method %s does not support parent batch header", method.Name) } +// Block contains the block fields that the fetchers use. +// Hash comes from the RPC node, not from types.Header.Hash(). The SDK hashes its own Header struct, +// which follows Scroll L2 rules: it lacks newer L1 fields (e.g. EIP-7685 requestsHash) and encodes +// Scroll-only fields after baseFee. For post-Shanghai L1 blocks the computed hash is therefore wrong. +type Block struct { + Number uint64 + Hash common.Hash + ParentHash common.Hash + Time uint64 + Transactions types.Transactions +} + +type rpcBlock struct { + Number *hexutil.Uint64 `json:"number"` + Hash *common.Hash `json:"hash"` + ParentHash *common.Hash `json:"parentHash"` + Time *hexutil.Uint64 `json:"timestamp"` + TxHash *common.Hash `json:"transactionsRoot"` + Transactions []*types.Transaction `json:"transactions"` +} + +// GetBlockByNumber gets a block with its transactions and its RPC-reported hash. +func GetBlockByNumber(ctx context.Context, cli *rpc.Client, number uint64) (*Block, error) { + var block *rpcBlock + if err := cli.CallContext(ctx, &block, "eth_getBlockByNumber", hexutil.EncodeUint64(number), true); err != nil { + return nil, err + } + if block == nil { + return nil, ethereum.NotFound + } + if block.Number == nil || block.Hash == nil || block.ParentHash == nil || block.Time == nil || block.TxHash == nil { + return nil, fmt.Errorf("block %d response is missing required fields", number) + } + if *block.Hash == (common.Hash{}) { + return nil, fmt.Errorf("block %d has a zero hash", number) + } + // Same sanity checks as ethclient: a missing transaction list would silently drop reverted txs. + if (*block.TxHash == types.EmptyRootHash) != (len(block.Transactions) == 0) { + return nil, fmt.Errorf("block %d transaction list does not match transactions root", number) + } + return &Block{ + Number: uint64(*block.Number), + Hash: *block.Hash, + ParentHash: *block.ParentHash, + Time: uint64(*block.Time), + Transactions: block.Transactions, + }, nil +} + // GetBlocksInRange gets a batch of blocks for a block range [start, end] inclusive. -func GetBlocksInRange(ctx context.Context, cli *ethclient.Client, start, end uint64) ([]*types.Block, error) { +func GetBlocksInRange(ctx context.Context, cli *rpc.Client, start, end uint64) ([]*Block, error) { var ( eg errgroup.Group - blocks = make([]*types.Block, end-start+1) + blocks = make([]*Block, end-start+1) concurrency = 32 sem = make(chan struct{}, concurrency) ) for i := start; i <= end; i++ { sem <- struct{}{} // Acquire a slot in the semaphore - blockNum := int64(i) + blockNum := i index := i - start eg.Go(func() error { defer func() { <-sem }() // Release the slot when done - block, err := cli.BlockByNumber(ctx, big.NewInt(blockNum)) + block, err := GetBlockByNumber(ctx, cli, blockNum) if err != nil { log.Error("Failed to fetch block number", "number", blockNum, "error", err) return err diff --git a/bridge-history-api/internal/utils/utils_test.go b/bridge-history-api/internal/utils/utils_test.go index 4c1a2b2b42..1c0a3abdc4 100644 --- a/bridge-history-api/internal/utils/utils_test.go +++ b/bridge-history-api/internal/utils/utils_test.go @@ -1,11 +1,20 @@ package utils import ( + "context" + "encoding/json" "math/big" + "net/http" + "net/http/httptest" "testing" + "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/rpc" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestKeccak2(t *testing.T) { @@ -125,3 +134,83 @@ func TestGetSkippedQueueIndices(t *testing.T) { assert.Equal(t, test.expected, got) } } + +func TestGetBlockByNumber(t *testing.T) { + key, err := crypto.GenerateKey() + require.NoError(t, err) + tx, err := types.SignTx(types.NewTransaction(0, common.Address{1}, big.NewInt(1), 21000, big.NewInt(1), nil), types.NewEIP155Signer(big.NewInt(1)), key) + require.NoError(t, err) + + newBlock := func() map[string]interface{} { + return map[string]interface{}{ + "number": "0xa", + "hash": common.Hash{2}, + "parentHash": common.Hash{3}, + "timestamp": "0x64", + "transactionsRoot": common.Hash{4}, + "transactions": []*types.Transaction{tx}, + } + } + + tests := []struct { + name string + mutate func(block map[string]interface{}) + null bool + wantErr string + }{ + {name: "valid block", mutate: func(map[string]interface{}) {}}, + {name: "empty block", mutate: func(b map[string]interface{}) { + b["transactionsRoot"] = types.EmptyRootHash + b["transactions"] = []*types.Transaction{} + }}, + {name: "null block", null: true, wantErr: ethereum.NotFound.Error()}, + {name: "missing hash", mutate: func(b map[string]interface{}) { delete(b, "hash") }, wantErr: "missing required fields"}, + {name: "zero hash", mutate: func(b map[string]interface{}) { b["hash"] = common.Hash{} }, wantErr: "zero hash"}, + {name: "zero parent hash", mutate: func(b map[string]interface{}) { b["parentHash"] = common.Hash{} }}, + {name: "missing parent hash", mutate: func(b map[string]interface{}) { delete(b, "parentHash") }, wantErr: "missing required fields"}, + {name: "missing number", mutate: func(b map[string]interface{}) { delete(b, "number") }, wantErr: "missing required fields"}, + {name: "missing timestamp", mutate: func(b map[string]interface{}) { delete(b, "timestamp") }, wantErr: "missing required fields"}, + {name: "missing transactions root", mutate: func(b map[string]interface{}) { delete(b, "transactionsRoot") }, wantErr: "missing required fields"}, + {name: "missing transactions", mutate: func(b map[string]interface{}) { delete(b, "transactions") }, wantErr: "does not match transactions root"}, + {name: "null transactions", mutate: func(b map[string]interface{}) { b["transactions"] = nil }, wantErr: "does not match transactions root"}, + {name: "empty transactions", mutate: func(b map[string]interface{}) { b["transactions"] = []*types.Transaction{} }, wantErr: "does not match transactions root"}, + {name: "unexpected transactions", mutate: func(b map[string]interface{}) { b["transactionsRoot"] = types.EmptyRootHash }, wantErr: "does not match transactions root"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var result interface{} + served := newBlock() + if !tt.null { + tt.mutate(served) + result = served + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var req struct { + ID json.RawMessage `json:"id"` + } + if decodeErr := json.NewDecoder(r.Body).Decode(&req); decodeErr != nil { + http.Error(w, decodeErr.Error(), http.StatusBadRequest) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]interface{}{"jsonrpc": "2.0", "id": req.ID, "result": result}) + })) + defer server.Close() + client, err := rpc.Dial(server.URL) + require.NoError(t, err) + defer client.Close() + + block, err := GetBlockByNumber(context.Background(), client, 10) + if tt.wantErr != "" { + assert.ErrorContains(t, err, tt.wantErr) + return + } + require.NoError(t, err) + assert.Equal(t, uint64(10), block.Number) + assert.Equal(t, common.Hash{2}, block.Hash) + assert.Equal(t, served["parentHash"], block.ParentHash) + assert.Equal(t, uint64(100), block.Time) + }) + } +} diff --git a/build/.golangci.yml b/build/.golangci.yml index 5e2eb31213..e0905e4663 100644 --- a/build/.golangci.yml +++ b/build/.golangci.yml @@ -110,6 +110,16 @@ linters-settings: - $all deny: - pkg: "github.com/davecgh/go-spew/spew" + forbidigo: + # The SDK hashes headers with Scroll L2 rules, so it computes wrong hashes for L1 blocks. + # For L1 blocks, read "hash" from the eth_getBlockByNumber response instead. + # For L2 blocks, the SDK hash is correct: add `//nolint:forbidigo // L2 block`, + # or list an L2-only file under issues.exclude-rules. + forbid: + - p: ^types\.(Header|Block)\.Hash$ + pkg: ^github\.com/scroll-tech/go-ethereum/core/types$ + msg: wrong for L1 blocks; use the RPC-reported hash (see build/.golangci.yml) + analyze-types: true misspell: # Correct spellings using locale preferences for US or UK. # Default is to use a neutral variety of English. @@ -183,6 +193,7 @@ linters: - goprintffuncname - golint - depguard + - forbidigo - gocyclo - unparam - exportloopref @@ -223,6 +234,14 @@ issues: - gosec - golint + # These files only hash L2 blocks, where the SDK hash is correct (see forbidigo above). + - path: (^|/)(orm/(chunk|l2_block)|l2_(watcher|relayer))\.go$ + linters: + - forbidigo + - path: ^tests/ + linters: + - forbidigo + # Exclude some staticcheck messages - linters: - staticcheck diff --git a/common/version/version.go b/common/version/version.go index 566d9ce4b2..4b30af77d8 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -5,7 +5,7 @@ import ( "runtime/debug" ) -var tag = "v4.8.3" +var tag = "v4.8.4" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok { diff --git a/rollup/internal/controller/watcher/l1_watcher.go b/rollup/internal/controller/watcher/l1_watcher.go index 7af4e2afa1..0ff18aaf63 100644 --- a/rollup/internal/controller/watcher/l1_watcher.go +++ b/rollup/internal/controller/watcher/l1_watcher.go @@ -4,12 +4,10 @@ import ( "context" "errors" "fmt" - "math/big" "github.com/prometheus/client_golang/prometheus" + "github.com/scroll-tech/go-ethereum/common" "github.com/scroll-tech/go-ethereum/common/hexutil" - gethTypes "github.com/scroll-tech/go-ethereum/core/types" - "github.com/scroll-tech/go-ethereum/ethclient" "github.com/scroll-tech/go-ethereum/log" "github.com/scroll-tech/go-ethereum/rpc" "gorm.io/gorm" @@ -22,8 +20,7 @@ import ( // L1WatcherClient will listen for smart contract events from Eth L1. type L1WatcherClient struct { ctx context.Context - rpcClient *rpc.Client // Raw RPC client - client *ethclient.Client // Go SDK RPC client + rpcClient *rpc.Client l1BlockOrm *orm.L1Block // The height of the block that the watcher has retrieved header rlp @@ -47,7 +44,6 @@ func NewL1WatcherClient(ctx context.Context, rpcClient *rpc.Client, startHeight w := &L1WatcherClient{ ctx: ctx, rpcClient: rpcClient, - client: ethclient.NewClient(rpcClient), l1BlockOrm: l1BlockOrm, processedBlockHeight: savedL1BlockHeight, @@ -70,8 +66,15 @@ func (w *L1WatcherClient) ProcessedBlockHeight() uint64 { func (w *L1WatcherClient) FetchBlockHeader(blockHeight uint64) error { w.metrics.l1WatcherFetchBlockHeaderTotal.Inc() - var block *gethTypes.Header - block, err := w.client.HeaderByNumber(w.ctx, big.NewInt(int64(blockHeight))) + // Use the RPC-reported hash, not types.Header.Hash(). The SDK header follows Scroll L2 rules: + // it lacks newer L1 fields (e.g. EIP-7685 requestsHash) and encodes Scroll-only fields after baseFee. + // For post-Shanghai L1 blocks the computed hash is therefore wrong. + var block *struct { + Hash common.Hash `json:"hash"` + BaseFee *hexutil.Big `json:"baseFeePerGas"` + ExcessBlobGas *hexutil.Uint64 `json:"excessBlobGas"` + } + err := w.rpcClient.CallContext(w.ctx, &block, "eth_getBlockByNumber", hexutil.EncodeUint64(blockHeight), false) if err != nil { log.Warn("Failed to get block", "height", blockHeight, "err", err) return err @@ -82,9 +85,14 @@ func (w *L1WatcherClient) FetchBlockHeader(blockHeight uint64) error { return errors.New("received nil block") } + if block.Hash == (common.Hash{}) { + log.Warn("Received block without hash", "height", blockHeight) + return errors.New("received block without hash") + } + var baseFee uint64 if block.BaseFee != nil { - baseFee = block.BaseFee.Uint64() + baseFee = block.BaseFee.ToInt().Uint64() } var blobBaseFee uint64 @@ -104,7 +112,7 @@ func (w *L1WatcherClient) FetchBlockHeader(blockHeight uint64) error { l1Block := orm.L1Block{ Number: blockHeight, - Hash: block.Hash().String(), + Hash: block.Hash.String(), BaseFee: baseFee, BlobBaseFee: blobBaseFee, GasOracleStatus: int16(types.GasOraclePending), diff --git a/rollup/internal/controller/watcher/l1_watcher_test.go b/rollup/internal/controller/watcher/l1_watcher_test.go index 7b5d929ebf..c5c7efd5da 100644 --- a/rollup/internal/controller/watcher/l1_watcher_test.go +++ b/rollup/internal/controller/watcher/l1_watcher_test.go @@ -2,14 +2,13 @@ package watcher import ( "context" + "encoding/json" "errors" - "math/big" "testing" "github.com/agiledragon/gomonkey/v2" "github.com/scroll-tech/go-ethereum" - "github.com/scroll-tech/go-ethereum/core/types" - "github.com/scroll-tech/go-ethereum/ethclient" + "github.com/scroll-tech/go-ethereum/rpc" "github.com/smartystreets/goconvey/convey" "github.com/stretchr/testify/assert" "gorm.io/gorm" @@ -43,9 +42,9 @@ func testL1WatcherClientFetchBlockHeader(t *testing.T) { }) convey.Convey("test get header from client error", t, func() { - var c *ethclient.Client - patchGuard := gomonkey.ApplyMethodFunc(c, "HeaderByNumber", func(ctx context.Context, height *big.Int) (*types.Header, error) { - return nil, ethereum.NotFound + var c *rpc.Client + patchGuard := gomonkey.ApplyMethodFunc(c, "CallContext", func(ctx context.Context, result interface{}, method string, args ...interface{}) error { + return ethereum.NotFound }) defer patchGuard.Reset() @@ -56,15 +55,10 @@ func testL1WatcherClientFetchBlockHeader(t *testing.T) { var l1BlockOrm *orm.L1Block convey.Convey("insert l1 block error", t, func() { - var c *ethclient.Client - patchGuard := gomonkey.ApplyMethodFunc(c, "HeaderByNumber", func(ctx context.Context, height *big.Int) (*types.Header, error) { - if height == nil { - height = big.NewInt(100) - } - t.Log(height) - return &types.Header{ - BaseFee: big.NewInt(100), - }, nil + var c *rpc.Client + patchGuard := gomonkey.ApplyMethodFunc(c, "CallContext", func(ctx context.Context, result interface{}, method string, args ...interface{}) error { + t.Log(method, args) + return json.Unmarshal([]byte(`{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","baseFeePerGas":"0x64"}`), result) }) defer patchGuard.Reset() @@ -78,15 +72,10 @@ func testL1WatcherClientFetchBlockHeader(t *testing.T) { }) convey.Convey("fetch block header success", t, func() { - var c *ethclient.Client - patchGuard := gomonkey.ApplyMethodFunc(c, "HeaderByNumber", func(ctx context.Context, height *big.Int) (*types.Header, error) { - if height == nil { - height = big.NewInt(100) - } - t.Log(height) - return &types.Header{ - BaseFee: big.NewInt(100), - }, nil + var c *rpc.Client + patchGuard := gomonkey.ApplyMethodFunc(c, "CallContext", func(ctx context.Context, result interface{}, method string, args ...interface{}) error { + t.Log(method, args) + return json.Unmarshal([]byte(`{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","baseFeePerGas":"0x64"}`), result) }) defer patchGuard.Reset() diff --git a/rollup/internal/utils/utils.go b/rollup/internal/utils/utils.go index df9cad18cb..03aa839eff 100644 --- a/rollup/internal/utils/utils.go +++ b/rollup/internal/utils/utils.go @@ -137,7 +137,7 @@ func encodeBatchHeaderValidium(b *encoding.Batch, codecVersion encoding.CodecVer // For validium mode, use the last block hash as commitment to the off-chain data // TODO: This is a temporary solution, we might use a larger commitment in the future lastBlock := b.Blocks[len(b.Blocks)-1] - commitment := lastBlock.Header.Hash() + commitment := lastBlock.Header.Hash() //nolint:forbidigo // L2 block stateRoot := b.StateRoot() // Batch header field sizes