Skip to content

Commit 40ac528

Browse files
QuentinIdailinsubjam
authored andcommitted
Respect espresso.fetch-api flag (#253)
(cherry picked from commit 6d00dcb)
1 parent 7a8503b commit 40ac528

File tree

7 files changed

+99
-95
lines changed

7 files changed

+99
-95
lines changed

espresso/docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ services:
301301
- --rpc.addr=0.0.0.0
302302
- --sequencer.enabled=false
303303
- --espresso.enabled=true
304+
- --espresso.fetch-api=true
304305
- --verifier.l1-confs=0
305306
- --rollup.load-protocol-versions=false
306307
- --rollup.halt=none
@@ -342,6 +343,7 @@ services:
342343
command:
343344
- op-batcher
344345
- --espresso.enabled=true
346+
- --espresso.fetch-api=true
345347
- --espresso.poll-interval=1s
346348
- --espresso.light-client-addr=0x703848f4c85f18e3acd8196c8ec91eb0b7bd0797
347349
- --espresso.testing-batcher-private-key=${OP_TESTING_BATCHER_PRIVATE_KEY:-$OPERATOR_PRIVATE_KEY}

espresso/docker/op-batcher-tee/run-enclave.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ BATCHER_ARGS="$BATCHER_ARGS,--hd-path=m/44'/60'/0'/0/0"
4646
BATCHER_ARGS="$BATCHER_ARGS,--throttle-threshold=0"
4747
BATCHER_ARGS="$BATCHER_ARGS,--max-channel-duration=1"
4848
BATCHER_ARGS="$BATCHER_ARGS,--target-num-frames=1"
49+
BATCHER_ARGS="$BATCHER_ARGS,--espresso.fetch-api=true"
4950
BATCHER_ARGS="$BATCHER_ARGS,--espresso.light-client-addr=0x703848f4c85f18e3acd8196c8ec91eb0b7bd0797"
5051

5152
# Add debug arguments if enabled

espresso/environment/3_2_espresso_deterministic_state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func TestValidEspressoTransactionCreation(t *testing.T) {
345345

346346
// Make sure the transaction will go through to op node by checking it will go through batch submitter's streamer
347347
batchSubmitter := system.BatchSubmitter
348-
_, err = batchSubmitter.EspressoStreamer().UnmarshalBatch(realEspressoTransaction.Payload)
348+
_, err = batchSubmitter.EspressoStreamer.UnmarshalBatch(realEspressoTransaction.Payload)
349349
if have, want := err, error(nil); have != want {
350350
t.Fatalf("Failed to unmarshal batch:\nhave:\n\t\"%v\"\nwant:\n\t\"%v\"\n", have, want)
351351
}

op-batcher/batcher/driver.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/ethereum/go-ethereum/rpc"
2525

2626
espressoClient "github.com/EspressoSystems/espresso-network/sdks/go/client"
27-
espressoLightClient "github.com/EspressoSystems/espresso-network/sdks/go/light-client"
2827
"github.com/ethereum-optimism/optimism/espresso"
2928
altda "github.com/ethereum-optimism/optimism/op-alt-da"
3029
"github.com/ethereum-optimism/optimism/op-batcher/metrics"
@@ -107,11 +106,10 @@ type DriverSetup struct {
107106
ChannelOutFactory ChannelOutFactory
108107
ActiveSeqChanged chan struct{} // optional
109108

110-
Espresso *espressoClient.MultipleNodesClient
111-
EspressoLightClient *espressoLightClient.LightclientCaller
112-
ChainSigner opcrypto.ChainSigner
113-
SequencerAddress common.Address
114-
Attestation *nitrite.Result
109+
EspressoStreamer espresso.EspressoStreamer[derive.EspressoBatch]
110+
EspressoClient espressoClient.EspressoClient
111+
ChainSigner opcrypto.ChainSigner
112+
Attestation *nitrite.Result
115113
}
116114

117115
// BatchSubmitter encapsulates a service responsible for submitting L2 tx
@@ -139,7 +137,6 @@ type BatchSubmitter struct {
139137
prevCurrentL1 eth.L1BlockRef // cached CurrentL1 from the last syncStatus
140138

141139
espressoSubmitter *espressoTransactionSubmitter
142-
espressoStreamer espresso.EspressoStreamer[derive.EspressoBatch]
143140
}
144141

145142
// NewBatchSubmitter initializes the BatchSubmitter driver from a preconfigured DriverSetup
@@ -154,22 +151,6 @@ func NewBatchSubmitter(setup DriverSetup) *BatchSubmitter {
154151
channelMgr: state,
155152
}
156153

157-
batchSubmitter.espressoStreamer = espresso.NewBufferedEspressoStreamer(
158-
espresso.NewEspressoStreamer(
159-
batchSubmitter.RollupConfig.L2ChainID.Uint64(),
160-
NewAdaptL1BlockRefClient(batchSubmitter.L1Client),
161-
batchSubmitter.Espresso,
162-
batchSubmitter.EspressoLightClient,
163-
batchSubmitter.Log,
164-
func(data []byte) (*derive.EspressoBatch, error) {
165-
return derive.UnmarshalEspressoTransaction(data, batchSubmitter.SequencerAddress)
166-
},
167-
2*time.Second,
168-
),
169-
)
170-
171-
log.Info("Streamer started", "streamer", batchSubmitter.espressoStreamer)
172-
173154
return batchSubmitter
174155
}
175156

@@ -226,7 +207,7 @@ func (l *BatchSubmitter) StartBatchSubmitting() error {
226207
l.espressoSubmitter = NewEspressoTransactionSubmitter(
227208
WithContext(l.shutdownCtx),
228209
WithWaitGroup(l.wg),
229-
WithEspressoClient(l.Espresso),
210+
WithEspressoClient(l.EspressoClient),
230211
)
231212
l.espressoSubmitter.SpawnWorkers(4, 4)
232213
l.espressoSubmitter.Start()
@@ -827,7 +808,7 @@ func (l *BatchSubmitter) clearState(ctx context.Context) {
827808
defer l.channelMgrMutex.Unlock()
828809
l.channelMgr.Clear(l1SafeOrigin)
829810
if l.Config.UseEspresso {
830-
l.espressoStreamer.Reset()
811+
l.EspressoStreamer.Reset()
831812
}
832813
return true
833814
}

op-batcher/batcher/espresso.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"github.com/ethereum/go-ethereum/crypto"
2020
"github.com/ethereum/go-ethereum/log"
2121

22-
"github.com/ethereum-optimism/optimism/espresso"
23-
espressoLocal "github.com/ethereum-optimism/optimism/espresso"
2422
"github.com/ethereum-optimism/optimism/op-batcher/bindings"
2523
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
2624
"github.com/ethereum-optimism/optimism/op-service/eth"
@@ -650,10 +648,6 @@ func (s *espressoTransactionSubmitter) Start() {
650648
go s.handleVerifyReceiptJobResponse()
651649
}
652650

653-
func (bs *BatcherService) EspressoStreamer() espressoLocal.EspressoStreamer[derive.EspressoBatch] {
654-
return bs.driver.espressoStreamer
655-
}
656-
657651
func (bs *BatcherService) initKeyPair() error {
658652
key, err := crypto.GenerateKey()
659653
if err != nil {
@@ -664,11 +658,6 @@ func (bs *BatcherService) initKeyPair() error {
664658
return nil
665659
}
666660

667-
// EspressoStreamer returns the batch submitter's Espresso streamer instance
668-
func (l *BatchSubmitter) EspressoStreamer() espresso.EspressoStreamer[derive.EspressoBatch] {
669-
return l.espressoStreamer
670-
}
671-
672661
// Converts a block to an EspressoBatch and starts a goroutine that publishes it to Espresso
673662
// Returns error only if batch conversion fails, otherwise it is infallible, as the goroutine
674663
// will retry publishing until successful.
@@ -691,7 +680,7 @@ func (l *BatchSubmitter) queueBlockToEspresso(ctx context.Context, block *types.
691680
}
692681

693682
func (l *BatchSubmitter) espressoSyncAndRefresh(ctx context.Context, newSyncStatus *eth.SyncStatus) {
694-
err := l.espressoStreamer.Refresh(ctx, newSyncStatus.FinalizedL1, newSyncStatus.FinalizedL2.Number, newSyncStatus.FinalizedL2.L1Origin)
683+
err := l.EspressoStreamer.Refresh(ctx, newSyncStatus.FinalizedL1, newSyncStatus.FinalizedL2.Number, newSyncStatus.FinalizedL2.L1Origin)
695684
if err != nil {
696685
l.Log.Warn("Failed to refresh Espresso streamer", "err", err)
697686
}
@@ -706,7 +695,7 @@ func (l *BatchSubmitter) espressoSyncAndRefresh(ctx context.Context, newSyncStat
706695
l.prevCurrentL1 = newSyncStatus.CurrentL1
707696
if syncActions.clearState != nil {
708697
l.channelMgr.Clear(*syncActions.clearState)
709-
l.espressoStreamer.Reset()
698+
l.EspressoStreamer.Reset()
710699
} else {
711700
l.channelMgr.PruneSafeBlocks(syncActions.blocksToPrune)
712701
l.channelMgr.PruneChannels(syncActions.channelsToPrune)
@@ -755,13 +744,13 @@ func (l *BatchSubmitter) espressoBatchLoadingLoop(ctx context.Context, wg *sync.
755744

756745
l.espressoSyncAndRefresh(ctx, newSyncStatus)
757746

758-
err = l.espressoStreamer.Update(ctx)
747+
err = l.EspressoStreamer.Update(ctx)
759748

760749
var batch *derive.EspressoBatch
761750

762751
for {
763752

764-
batch = l.espressoStreamer.Next(ctx)
753+
batch = l.EspressoStreamer.Next(ctx)
765754

766755
if batch == nil {
767756
break
@@ -789,7 +778,7 @@ func (l *BatchSubmitter) espressoBatchLoadingLoop(ctx context.Context, wg *sync.
789778
if err != nil {
790779
l.Log.Error("failed to add L2 block to channel manager", "err", err)
791780
l.clearState(ctx)
792-
l.espressoStreamer.Reset()
781+
l.EspressoStreamer.Reset()
793782
}
794783

795784
l.Log.Info("Added L2 block to channel manager")

op-batcher/batcher/service.go

Lines changed: 83 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
espressoClient "github.com/EspressoSystems/espresso-network/sdks/go/client"
1313
espressoLightClient "github.com/EspressoSystems/espresso-network/sdks/go/light-client"
14+
"github.com/ethereum-optimism/optimism/espresso"
1415
opcrypto "github.com/ethereum-optimism/optimism/op-service/crypto"
1516
"github.com/ethereum/go-ethereum/ethclient"
1617
"github.com/ethereum/go-ethereum/log"
@@ -24,6 +25,7 @@ import (
2425
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
2526
"github.com/ethereum-optimism/optimism/op-node/params"
2627
"github.com/ethereum-optimism/optimism/op-node/rollup"
28+
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
2729
"github.com/ethereum-optimism/optimism/op-service/cliapp"
2830
"github.com/ethereum-optimism/optimism/op-service/dial"
2931
"github.com/ethereum-optimism/optimism/op-service/eth"
@@ -91,7 +93,8 @@ type BatcherService struct {
9193
NotSubmittingOnStart bool
9294

9395
opcrypto.ChainSigner
94-
Espresso *espressoClient.MultipleNodesClient
96+
EspressoStreamer espresso.EspressoStreamer[derive.EspressoBatch]
97+
EspressoClient espressoClient.EspressoClient
9598
EspressoLightClient *espressoLightClient.LightclientCaller
9699
Attestation *nitrite.Result
97100
}
@@ -135,53 +138,6 @@ func (bs *BatcherService) initFromCLIConfig(ctx context.Context, version string,
135138
return err
136139
}
137140

138-
if cfg.Espresso.Enabled {
139-
bs.EspressoPollInterval = cfg.Espresso.PollInterval
140-
client, err := espressoClient.NewMultipleNodesClient(cfg.Espresso.QueryServiceURLs)
141-
if err != nil {
142-
return fmt.Errorf("failed to create Espresso client: %w", err)
143-
}
144-
bs.Espresso = client
145-
espressoLightClient, err := espressoLightClient.NewLightclientCaller(cfg.Espresso.LightClientAddr, bs.L1Client)
146-
if err != nil {
147-
return fmt.Errorf("failed to create Espresso light client")
148-
}
149-
bs.EspressoLightClient = espressoLightClient
150-
bs.UseEspresso = true
151-
if err := bs.initKeyPair(); err != nil {
152-
return fmt.Errorf("failed to create key pair for batcher: %w", err)
153-
}
154-
155-
// try to generate attestationBytes on public key when start batcher
156-
attestationBytes, err := enclave.AttestationWithPublicKey(bs.BatcherPublicKey)
157-
if err != nil {
158-
bs.Log.Info("Not running in enclave, skipping attestation", "info", err)
159-
160-
// Replace ephemeral keys with configured keys, as in devnet they'll be pre-approved for batching
161-
privateKey := cfg.Espresso.TestingBatcherPrivateKey
162-
if privateKey == nil {
163-
return fmt.Errorf("when not running in enclave, testing batcher private key should be set")
164-
}
165-
166-
publicKey := privateKey.Public()
167-
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
168-
if !ok {
169-
return fmt.Errorf("error casting public key to ECDSA")
170-
}
171-
172-
bs.BatcherPrivateKey = privateKey
173-
bs.BatcherPublicKey = publicKeyECDSA
174-
} else {
175-
// output length of attestation
176-
bs.Log.Info("Successfully got attestation. Attestation length", "length", len(attestationBytes))
177-
result, err := nitrite.Verify(attestationBytes, nitrite.VerifyOptions{})
178-
if err != nil {
179-
return fmt.Errorf("Couldn't verify attestation: %w", err)
180-
}
181-
bs.Attestation = result
182-
}
183-
}
184-
185141
if err := bs.initRollupConfig(ctx); err != nil {
186142
return fmt.Errorf("failed to load rollup config: %w", err)
187143
}
@@ -202,6 +158,9 @@ func (bs *BatcherService) initFromCLIConfig(ctx context.Context, version string,
202158
if err := bs.initPProf(cfg); err != nil {
203159
return fmt.Errorf("failed to init profiling: %w", err)
204160
}
161+
if err := bs.initEspresso(cfg); err != nil {
162+
return fmt.Errorf("failed to init Espresso: %w", err)
163+
}
205164
bs.initDriver(opts...)
206165
if err := bs.initRPCServer(cfg); err != nil {
207166
return fmt.Errorf("failed to start RPC server: %w", err)
@@ -427,11 +386,10 @@ func (bs *BatcherService) initDriver(opts ...DriverSetupOption) {
427386
ChannelConfig: bs.ChannelConfig,
428387
AltDA: bs.AltDA,
429388

430-
SequencerAddress: bs.TxManager.From(),
431-
ChainSigner: bs.ChainSigner,
432-
Espresso: bs.Espresso,
433-
EspressoLightClient: bs.EspressoLightClient,
434-
Attestation: bs.Attestation,
389+
EspressoStreamer: bs.EspressoStreamer,
390+
EspressoClient: bs.EspressoClient,
391+
ChainSigner: bs.ChainSigner,
392+
Attestation: bs.Attestation,
435393
}
436394
for _, opt := range opts {
437395
opt(&ds)
@@ -579,3 +537,75 @@ func (bs *BatcherService) HTTPEndpoint() string {
579537
}
580538
return "http://" + bs.rpcServer.Endpoint()
581539
}
540+
541+
func (bs *BatcherService) initEspresso(cfg *CLIConfig) error {
542+
if !cfg.Espresso.Enabled {
543+
return nil
544+
}
545+
546+
bs.UseEspresso = true
547+
bs.EspressoPollInterval = cfg.Espresso.PollInterval
548+
549+
client, err := espressoClient.NewMultipleNodesClient(cfg.Espresso.QueryServiceURLs)
550+
if err != nil {
551+
return fmt.Errorf("failed to create Espresso client: %w", err)
552+
}
553+
bs.EspressoClient = client
554+
555+
espressoLightClient, err := espressoLightClient.NewLightclientCaller(cfg.Espresso.LightClientAddr, bs.L1Client)
556+
if err != nil {
557+
return fmt.Errorf("failed to create Espresso light client")
558+
}
559+
bs.EspressoLightClient = espressoLightClient
560+
561+
if err := bs.initKeyPair(); err != nil {
562+
return fmt.Errorf("failed to create key pair for batcher: %w", err)
563+
}
564+
565+
unbufferedStreamer := espresso.NewEspressoStreamer(
566+
bs.RollupConfig.L2ChainID.Uint64(),
567+
NewAdaptL1BlockRefClient(bs.L1Client),
568+
client,
569+
bs.EspressoLightClient,
570+
bs.Log,
571+
func(data []byte) (*derive.EspressoBatch, error) {
572+
return derive.UnmarshalEspressoTransaction(data, bs.TxManager.From())
573+
},
574+
2*time.Second,
575+
)
576+
unbufferedStreamer.UseFetchApi = cfg.Espresso.UseFetchAPI
577+
578+
// We wrap the streamer in a BufferedStreamer to reduce impact of streamer resets
579+
bs.EspressoStreamer = espresso.NewBufferedEspressoStreamer(unbufferedStreamer)
580+
581+
// try to generate attestationBytes on public key when start batcher
582+
attestationBytes, err := enclave.AttestationWithPublicKey(bs.BatcherPublicKey)
583+
if err != nil {
584+
bs.Log.Info("Not running in enclave, skipping attestation", "info", err)
585+
586+
// Replace ephemeral keys with configured keys, as in devnet they'll be pre-approved for batching
587+
privateKey := cfg.Espresso.TestingBatcherPrivateKey
588+
if privateKey == nil {
589+
return fmt.Errorf("when not running in enclave, testing batcher private key should be set")
590+
}
591+
592+
publicKey := privateKey.Public()
593+
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
594+
if !ok {
595+
return fmt.Errorf("error casting public key to ECDSA")
596+
}
597+
598+
bs.BatcherPrivateKey = privateKey
599+
bs.BatcherPublicKey = publicKeyECDSA
600+
} else {
601+
// output length of attestation
602+
bs.Log.Info("Successfully got attestation. Attestation length", "length", len(attestationBytes))
603+
result, err := nitrite.Verify(attestationBytes, nitrite.VerifyOptions{})
604+
if err != nil {
605+
return fmt.Errorf("Couldn't verify attestation: %w", err)
606+
}
607+
bs.Attestation = result
608+
}
609+
610+
return nil
611+
}

op-node/rollup/derive/attributes_queue.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func initEspressoStreamer(log log.Logger, cfg *rollup.Config, l1Fetcher L1Fetche
112112
},
113113
cfg.CaffNodeConfig.PollInterval,
114114
)
115+
streamer.UseFetchApi = cfg.CaffNodeConfig.UseFetchAPI
115116

116117
log.Debug("Espresso Streamer namespace:", streamer.Namespace)
117118

0 commit comments

Comments
 (0)