@@ -2,6 +2,7 @@ package aggregator
2
2
3
3
import (
4
4
"context"
5
+ "crypto/ecdsa"
5
6
"encoding/json"
6
7
"errors"
7
8
"fmt"
@@ -13,6 +14,9 @@ import (
13
14
"time"
14
15
"unicode"
15
16
17
+ "github.com/0xPolygon/agglayer/client"
18
+ agglayerTypes "github.com/0xPolygon/agglayer/rpc/types"
19
+ "github.com/0xPolygon/agglayer/tx"
16
20
"github.com/0xPolygonHermez/zkevm-node/aggregator/metrics"
17
21
"github.com/0xPolygonHermez/zkevm-node/aggregator/prover"
18
22
"github.com/0xPolygonHermez/zkevm-node/config/types"
@@ -65,6 +69,9 @@ type Aggregator struct {
65
69
srv * grpc.Server
66
70
ctx context.Context
67
71
exit context.CancelFunc
72
+
73
+ AggLayerClient client.ClientInterface
74
+ sequencerPrivateKey * ecdsa.PrivateKey
68
75
}
69
76
70
77
// New creates a new aggregator.
@@ -73,6 +80,8 @@ func New(
73
80
stateInterface stateInterface ,
74
81
ethTxManager ethTxManager ,
75
82
etherman etherman ,
83
+ agglayerClient client.ClientInterface ,
84
+ sequencerPrivateKey * ecdsa.PrivateKey ,
76
85
) (Aggregator , error ) {
77
86
var profitabilityChecker aggregatorTxProfitabilityChecker
78
87
switch cfg .TxProfitabilityCheckerType {
@@ -94,6 +103,9 @@ func New(
94
103
TimeCleanupLockedProofs : cfg .CleanupLockedProofsInterval ,
95
104
96
105
finalProof : make (chan finalProofMsg ),
106
+
107
+ AggLayerClient : agglayerClient ,
108
+ sequencerPrivateKey : sequencerPrivateKey ,
97
109
}
98
110
99
111
return a , nil
@@ -267,34 +279,139 @@ func (a *Aggregator) sendFinalProof() {
267
279
268
280
log .Infof ("Final proof inputs: NewLocalExitRoot [%#x], NewStateRoot [%#x]" , inputs .NewLocalExitRoot , inputs .NewStateRoot )
269
281
270
- // add batch verification to be monitored
271
- sender := common .HexToAddress (a .cfg .SenderAddress )
272
- to , data , err := a .Ethman .BuildTrustedVerifyBatchesTxData (proof .BatchNumber - 1 , proof .BatchNumberFinal , & inputs , sender )
273
- if err != nil {
274
- log .Errorf ("Error estimating batch verification to add to eth tx manager: %v" , err )
275
- a .handleFailureToAddVerifyBatchToBeMonitored (ctx , proof )
276
- continue
277
- }
278
- monitoredTxID := buildMonitoredTxID (proof .BatchNumber , proof .BatchNumberFinal )
279
- err = a .EthTxManager .Add (ctx , ethTxManagerOwner , monitoredTxID , sender , to , nil , data , a .cfg .GasOffset , nil )
280
- if err != nil {
281
- mTxLogger := ethtxmanager .CreateLogger (ethTxManagerOwner , monitoredTxID , sender , to )
282
- mTxLogger .Errorf ("Error to add batch verification tx to eth tx manager: %v" , err )
283
- a .handleFailureToAddVerifyBatchToBeMonitored (ctx , proof )
284
- continue
282
+ switch a .cfg .SettlementBackend {
283
+ case AggLayer :
284
+ if success := a .settleWithAggLayer (ctx , proof , inputs ); ! success {
285
+ continue
286
+ }
287
+ default :
288
+ if success := a .settleDirect (ctx , proof , inputs ); ! success {
289
+ continue
290
+ }
285
291
}
286
292
287
- // process monitored batch verifications before starting a next cycle
288
- a .EthTxManager .ProcessPendingMonitoredTxs (ctx , ethTxManagerOwner , func (result ethtxmanager.MonitoredTxResult , dbTx pgx.Tx ) {
289
- a .handleMonitoredTxResult (result )
290
- }, nil )
291
-
292
293
a .resetVerifyProofTime ()
293
294
a .endProofVerification ()
294
295
}
295
296
}
296
297
}
297
298
299
+ func (a * Aggregator ) settleDirect (
300
+ ctx context.Context ,
301
+ proof * state.Proof ,
302
+ inputs ethmanTypes.FinalProofInputs ,
303
+ ) (success bool ) {
304
+ // add batch verification to be monitored
305
+ sender := common .HexToAddress (a .cfg .SenderAddress )
306
+
307
+ to , data , err := a .Ethman .BuildTrustedVerifyBatchesTxData (
308
+ proof .BatchNumber - 1 ,
309
+ proof .BatchNumberFinal ,
310
+ & inputs ,
311
+ sender ,
312
+ )
313
+ if err != nil {
314
+ log .Errorf ("Error estimating batch verification to add to eth tx manager: %v" , err )
315
+ a .handleFailureToAddVerifyBatchToBeMonitored (ctx , proof )
316
+
317
+ return false
318
+ }
319
+
320
+ monitoredTxID := buildMonitoredTxID (proof .BatchNumber , proof .BatchNumberFinal )
321
+ err = a .EthTxManager .Add (
322
+ ctx ,
323
+ ethTxManagerOwner ,
324
+ monitoredTxID ,
325
+ sender ,
326
+ to ,
327
+ nil ,
328
+ data ,
329
+ a .cfg .GasOffset ,
330
+ nil ,
331
+ )
332
+ if err != nil {
333
+ mTxLogger := ethtxmanager .CreateLogger (ethTxManagerOwner , monitoredTxID , sender , to )
334
+ mTxLogger .Errorf ("Error to add batch verification tx to eth tx manager: %v" , err )
335
+ a .handleFailureToAddVerifyBatchToBeMonitored (ctx , proof )
336
+
337
+ return false
338
+ }
339
+
340
+ // process monitored batch verifications before starting a next cycle
341
+ a .EthTxManager .ProcessPendingMonitoredTxs (
342
+ ctx ,
343
+ ethTxManagerOwner ,
344
+ func (result ethtxmanager.MonitoredTxResult , dbTx pgx.Tx ) {
345
+ a .handleMonitoredTxResult (result )
346
+ },
347
+ nil ,
348
+ )
349
+
350
+ return true
351
+ }
352
+
353
+ func (a * Aggregator ) settleWithAggLayer (
354
+ ctx context.Context ,
355
+ proof * state.Proof ,
356
+ inputs ethmanTypes.FinalProofInputs ,
357
+ ) (success bool ) {
358
+ proofStrNo0x := strings .TrimPrefix (inputs .FinalProof .Proof , "0x" )
359
+ proofBytes := common .Hex2Bytes (proofStrNo0x )
360
+ tx := tx.Tx {
361
+ LastVerifiedBatch : agglayerTypes .ArgUint64 (proof .BatchNumber - 1 ),
362
+ NewVerifiedBatch : agglayerTypes .ArgUint64 (proof .BatchNumberFinal ),
363
+ ZKP : tx.ZKP {
364
+ NewStateRoot : common .BytesToHash (inputs .NewStateRoot ),
365
+ NewLocalExitRoot : common .BytesToHash (inputs .NewLocalExitRoot ),
366
+ Proof : agglayerTypes .ArgBytes (proofBytes ),
367
+ },
368
+ RollupID : a .Ethman .GetRollupId (),
369
+ }
370
+ signedTx , err := tx .Sign (a .sequencerPrivateKey )
371
+
372
+ if err != nil {
373
+ log .Errorf ("failed to sign tx: %v" , err )
374
+ a .handleFailureToSendToAggLayer (ctx , proof )
375
+
376
+ return false
377
+ }
378
+
379
+ log .Debug ("final proof signedTx: " , signedTx .Tx .ZKP .Proof .Hex ())
380
+ txHash , err := a .AggLayerClient .SendTx (* signedTx )
381
+ if err != nil {
382
+ log .Errorf ("failed to send tx to the interop: %v" , err )
383
+ a .handleFailureToSendToAggLayer (ctx , proof )
384
+
385
+ return false
386
+ }
387
+
388
+ log .Infof ("tx %s sent to agglayer, waiting to be mined" , txHash .Hex ())
389
+ log .Debugf ("Timeout set to %f seconds" , a .cfg .AggLayerTxTimeout .Duration .Seconds ())
390
+ waitCtx , cancelFunc := context .WithDeadline (ctx , time .Now ().Add (a .cfg .AggLayerTxTimeout .Duration ))
391
+ defer cancelFunc ()
392
+ if err := a .AggLayerClient .WaitTxToBeMined (txHash , waitCtx ); err != nil {
393
+ log .Errorf ("interop didn't mine the tx: %v" , err )
394
+ a .handleFailureToSendToAggLayer (ctx , proof )
395
+
396
+ return false
397
+ }
398
+
399
+ // TODO: wait for synchronizer to catch up
400
+ return true
401
+ }
402
+
403
+ func (a * Aggregator ) handleFailureToSendToAggLayer (ctx context.Context , proof * state.Proof ) {
404
+ log := log .WithFields ("proofId" , proof .ProofID , "batches" , fmt .Sprintf ("%d-%d" , proof .BatchNumber , proof .BatchNumberFinal ))
405
+ proof .GeneratingSince = nil
406
+
407
+ err := a .State .UpdateGeneratedProof (ctx , proof , nil )
408
+ if err != nil {
409
+ log .Errorf ("Failed updating proof state (false): %v" , err )
410
+ }
411
+
412
+ a .endProofVerification ()
413
+ }
414
+
298
415
func (a * Aggregator ) handleFailureToAddVerifyBatchToBeMonitored (ctx context.Context , proof * state.Proof ) {
299
416
log := log .WithFields ("proofId" , proof .ProofID , "batches" , fmt .Sprintf ("%d-%d" , proof .BatchNumber , proof .BatchNumberFinal ))
300
417
proof .GeneratingSince = nil
0 commit comments