Skip to content

Commit 94dbf57

Browse files
authored
[Fix] Call GetL1Message in l2watcher (#1749)
1 parent ad2c24c commit 94dbf57

File tree

8 files changed

+61
-21
lines changed

8 files changed

+61
-21
lines changed

common/testcontainers/testcontainers.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/scroll-tech/go-ethereum/ethclient"
13+
"github.com/scroll-tech/go-ethereum/rpc"
1314
"github.com/testcontainers/testcontainers-go"
1415
"github.com/testcontainers/testcontainers-go/modules/compose"
1516
"github.com/testcontainers/testcontainers-go/modules/postgres"
@@ -220,11 +221,21 @@ func (t *TestcontainerApps) GetGormDBClient() (*gorm.DB, error) {
220221

221222
// GetL2GethClient returns a ethclient by dialing running L2Geth
222223
func (t *TestcontainerApps) GetL2GethClient() (*ethclient.Client, error) {
224+
225+
rpcCli, err := t.GetL2Client()
226+
if err != nil {
227+
return nil, err
228+
}
229+
return ethclient.NewClient(rpcCli), nil
230+
}
231+
232+
// GetL2GethClient returns a rpc client by dialing running L2Geth
233+
func (t *TestcontainerApps) GetL2Client() (*rpc.Client, error) {
223234
endpoint, err := t.GetL2GethEndPoint()
224235
if err != nil {
225236
return nil, err
226237
}
227-
client, err := ethclient.Dial(endpoint)
238+
client, err := rpc.Dial(endpoint)
228239
if err != nil {
229240
return nil, err
230241
}

rollup/cmd/permissionless_batches/app/app.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/scroll-tech/da-codec/encoding"
1111
"github.com/urfave/cli/v2"
1212

13-
"github.com/scroll-tech/go-ethereum/ethclient"
1413
"github.com/scroll-tech/go-ethereum/log"
14+
"github.com/scroll-tech/go-ethereum/rpc"
1515

1616
"scroll-tech/common/database"
1717
"scroll-tech/common/observability"
@@ -91,12 +91,13 @@ func action(ctx *cli.Context) error {
9191
bundleProposer := watcher.NewBundleProposer(subCtx, cfg.L2Config.BundleProposerConfig, minCodecVersion, genesis.Config, db, registry)
9292

9393
// Init l2geth connection
94-
l2client, err := ethclient.Dial(cfg.L2Config.Endpoint)
94+
l2client, err := rpc.Dial(cfg.L2Config.Endpoint)
9595
if err != nil {
9696
return fmt.Errorf("failed to connect to L2geth at RPC=%s: %w", cfg.L2Config.Endpoint, err)
9797
}
9898

99-
l2Watcher := watcher.NewL2WatcherClient(subCtx, l2client, cfg.L2Config.Confirmations, cfg.L2Config.L2MessageQueueAddress, cfg.L2Config.WithdrawTrieRootSlot, genesis.Config, db, registry)
99+
l2Watcher := watcher.NewL2WatcherClient(subCtx, l2client, cfg.L2Config.Confirmations, cfg.L2Config.L2MessageQueueAddress,
100+
cfg.L2Config.WithdrawTrieRootSlot, genesis.Config, db, cfg.L2Config.RelayerConfig.ValidiumMode, registry)
100101

101102
recovery := permissionless_batches.NewRecovery(subCtx, cfg, genesis, db, chunkProposer, batchProposer, bundleProposer, l2Watcher)
102103

rollup/cmd/rollup_relayer/app/app.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/scroll-tech/go-ethereum/ethclient"
1313
"github.com/scroll-tech/go-ethereum/log"
1414
"github.com/scroll-tech/go-ethereum/rollup/l1"
15+
"github.com/scroll-tech/go-ethereum/rpc"
1516
"github.com/urfave/cli/v2"
1617

1718
"scroll-tech/common/database"
@@ -69,10 +70,11 @@ func action(ctx *cli.Context) error {
6970
observability.Server(ctx, db)
7071

7172
// Init l2geth connection
72-
l2client, err := ethclient.Dial(cfg.L2Config.Endpoint)
73+
l2client, err := rpc.Dial(cfg.L2Config.Endpoint)
7374
if err != nil {
7475
log.Crit("failed to connect l2 geth", "config file", cfgFile, "error", err)
7576
}
77+
l2ethClient := ethclient.NewClient(l2client)
7678

7779
genesisPath := ctx.String(utils.Genesis.Name)
7880
genesis, err := utils.ReadGenesis(genesisPath)
@@ -100,7 +102,7 @@ func action(ctx *cli.Context) error {
100102
log.Crit("cfg.L2Config.RelayerConfig.SenderConfig.FusakaTimestamp must be set")
101103
}
102104

103-
l2relayer, err := relayer.NewLayer2Relayer(ctx.Context, l2client, db, cfg.L2Config.RelayerConfig, genesis.Config, relayer.ServiceTypeL2RollupRelayer, registry)
105+
l2relayer, err := relayer.NewLayer2Relayer(ctx.Context, l2ethClient, db, cfg.L2Config.RelayerConfig, genesis.Config, relayer.ServiceTypeL2RollupRelayer, registry)
104106
if err != nil {
105107
log.Crit("failed to create l2 relayer", "config file", cfgFile, "error", err)
106108
}
@@ -144,7 +146,7 @@ func action(ctx *cli.Context) error {
144146

145147
// Watcher loop to fetch missing blocks
146148
go utils.LoopWithContext(subCtx, 2*time.Second, func(ctx context.Context) {
147-
number, loopErr := rutils.GetLatestConfirmedBlockNumber(ctx, l2client, cfg.L2Config.Confirmations)
149+
number, loopErr := rutils.GetLatestConfirmedBlockNumber(ctx, l2ethClient, cfg.L2Config.Confirmations)
148150
if loopErr != nil {
149151
log.Error("failed to get block number", "err", loopErr)
150152
return

rollup/go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ require (
5252
github.com/crate-crypto/go-eth-kzg v1.4.0 // indirect
5353
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
5454
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea // indirect
55-
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
5655
github.com/edsrzf/mmap-go v1.0.0 // indirect
5756
github.com/ethereum/c-kzg-4844/v2 v2.1.5 // indirect
5857
github.com/fjl/memsize v0.0.2 // indirect
@@ -111,7 +110,6 @@ require (
111110
github.com/russross/blackfriday/v2 v2.1.0 // indirect
112111
github.com/sagikazarmark/locafero v0.4.0 // indirect
113112
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
114-
github.com/scroll-tech/ecies-go/v2 v2.0.10-beta.1 // indirect
115113
github.com/scroll-tech/zktrie v0.8.4 // indirect
116114
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
117115
github.com/smartystreets/assertions v1.13.1 // indirect

rollup/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
9090
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9191
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea h1:j4317fAZh7X6GqbFowYdYdI0L9bwxL07jyPZIdepyZ0=
9292
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ=
93-
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg=
94-
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
9593
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
9694
github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw=
9795
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=

rollup/internal/controller/watcher/l2_watcher.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/scroll-tech/da-codec/encoding"
1010
"github.com/scroll-tech/go-ethereum/common"
1111
"github.com/scroll-tech/go-ethereum/core/types"
12-
"github.com/scroll-tech/go-ethereum/eth"
1312
"github.com/scroll-tech/go-ethereum/ethclient"
1413
"github.com/scroll-tech/go-ethereum/event"
1514
"github.com/scroll-tech/go-ethereum/log"
@@ -26,6 +25,7 @@ type L2WatcherClient struct {
2625
event.Feed
2726

2827
*ethclient.Client
28+
rpcCli *rpc.Client
2929

3030
l2BlockOrm *orm.L2Block
3131

@@ -42,10 +42,11 @@ type L2WatcherClient struct {
4242
}
4343

4444
// NewL2WatcherClient take a l2geth instance to generate a l2watcherclient instance
45-
func NewL2WatcherClient(ctx context.Context, client *ethclient.Client, confirmations rpc.BlockNumber, messageQueueAddress common.Address, withdrawTrieRootSlot common.Hash, chainCfg *params.ChainConfig, db *gorm.DB, validiumMode bool, reg prometheus.Registerer) *L2WatcherClient {
45+
func NewL2WatcherClient(ctx context.Context, client *rpc.Client, confirmations rpc.BlockNumber, messageQueueAddress common.Address, withdrawTrieRootSlot common.Hash, chainCfg *params.ChainConfig, db *gorm.DB, validiumMode bool, reg prometheus.Registerer) *L2WatcherClient {
4646
return &L2WatcherClient{
4747
ctx: ctx,
48-
Client: client,
48+
Client: ethclient.NewClient(client),
49+
rpcCli: client,
4950

5051
l2BlockOrm: orm.NewL2Block(db),
5152

@@ -117,7 +118,7 @@ func (w *L2WatcherClient) GetAndStoreBlocks(ctx context.Context, from, to uint64
117118

118119
if count > 0 {
119120
log.Info("Fetching encrypted messages in validium mode")
120-
txs, err = w.GetL1MessagesInBlock(context.Background(), block.Hash(), eth.QueryModeSynced)
121+
err = w.rpcCli.CallContext(ctx, &txs, "scroll_getL1MessagesInBlock", block.Hash(), "synced")
121122
if err != nil {
122123
return fmt.Errorf("failed to get L1 messages: %v, block hash: %v", err, block.Hash().Hex())
123124
}

rollup/internal/controller/watcher/l2_watcher_test.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package watcher
22

33
import (
44
"context"
5+
"os"
56
"testing"
67

78
"gorm.io/gorm"
89

910
"github.com/scroll-tech/go-ethereum/common"
10-
"github.com/scroll-tech/go-ethereum/ethclient"
11+
"github.com/scroll-tech/go-ethereum/core/types"
1112
"github.com/scroll-tech/go-ethereum/rpc"
1213
"github.com/stretchr/testify/assert"
1314

@@ -20,7 +21,7 @@ import (
2021
func setupL2Watcher(t *testing.T) (*L2WatcherClient, *gorm.DB) {
2122
db := setupDB(t)
2223
l2cfg := cfg.L2Config
23-
watcher := NewL2WatcherClient(context.Background(), l2Cli, l2cfg.Confirmations, l2cfg.L2MessageQueueAddress, l2cfg.WithdrawTrieRootSlot, nil, db, nil)
24+
watcher := NewL2WatcherClient(context.Background(), l2Rpc, l2cfg.Confirmations, l2cfg.L2MessageQueueAddress, l2cfg.WithdrawTrieRootSlot, nil, db, false, nil)
2425
return watcher, db
2526
}
2627

@@ -34,15 +35,40 @@ func testFetchRunningMissingBlocks(t *testing.T) {
3435
if err != nil {
3536
return false
3637
}
37-
wc := prepareWatcherClient(l2Cli, db)
38+
wc := prepareWatcherClient(l2Rpc, db)
3839
wc.TryFetchRunningMissingBlocks(latestHeight)
3940
fetchedHeight, err := l2BlockOrm.GetL2BlocksLatestHeight(context.Background())
4041
return err == nil && fetchedHeight == latestHeight
4142
})
4243
assert.True(t, ok)
4344
}
4445

45-
func prepareWatcherClient(l2Cli *ethclient.Client, db *gorm.DB) *L2WatcherClient {
46+
func prepareWatcherClient(l2Cli *rpc.Client, db *gorm.DB) *L2WatcherClient {
4647
confirmations := rpc.LatestBlockNumber
47-
return NewL2WatcherClient(context.Background(), l2Cli, confirmations, common.Address{}, common.Hash{}, nil, db, nil)
48+
return NewL2WatcherClient(context.Background(), l2Cli, confirmations, common.Address{}, common.Hash{}, nil, db, false, nil)
49+
}
50+
51+
// New test for raw RPC GetBlockByHash from an endpoint URL in env.
52+
func TestRawRPCGetBlockByHash(t *testing.T) {
53+
url := os.Getenv("RPC_ENDPOINT_URL")
54+
if url == "" {
55+
t.Log("warn: RPC_ENDPOINT_URL not set, skipping raw RPC test")
56+
t.Skip("missing RPC_ENDPOINT_URL")
57+
}
58+
59+
ctx := context.Background()
60+
cli, err := rpc.DialContext(ctx, url)
61+
if err != nil {
62+
t.Fatalf("failed to dial RPC endpoint %s: %v", url, err)
63+
}
64+
defer cli.Close()
65+
66+
var txs []*types.Transaction
67+
blkHash := common.HexToHash("0xc80cf12883341827d71c08f734ba9a9d6da7e59eb16921d26e6706887e552c74")
68+
err = cli.CallContext(ctx, &txs, "scroll_getL1MessagesInBlock", blkHash, "synced")
69+
if err != nil {
70+
t.Logf("scroll_getL1MessagesInBlock failed: err=%v", err)
71+
t.Fail()
72+
}
73+
t.Log(txs, txs[0].Hash())
4874
}

rollup/internal/controller/watcher/watcher_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/scroll-tech/da-codec/encoding"
99
"github.com/scroll-tech/go-ethereum/ethclient"
1010
"github.com/scroll-tech/go-ethereum/log"
11+
"github.com/scroll-tech/go-ethereum/rpc"
1112
"github.com/stretchr/testify/assert"
1213
"gorm.io/gorm"
1314

@@ -27,6 +28,7 @@ var (
2728

2829
// l2geth client
2930
l2Cli *ethclient.Client
31+
l2Rpc *rpc.Client
3032

3133
// block trace
3234
block1 *encoding.Block
@@ -62,8 +64,9 @@ func setupEnv(t *testing.T) (err error) {
6264
}
6365

6466
// Create l2geth client.
65-
l2Cli, err = testApps.GetL2GethClient()
67+
l2Rpc, err = testApps.GetL2Client()
6668
assert.NoError(t, err)
69+
l2Cli = ethclient.NewClient(l2Rpc)
6770

6871
block1 = readBlockFromJSON(t, "../../../testdata/blockTrace_02.json")
6972
block2 = readBlockFromJSON(t, "../../../testdata/blockTrace_03.json")

0 commit comments

Comments
 (0)