@@ -25,7 +25,7 @@ const { ZERO, HASH_TYPE, TIMBER_HEIGHT, TXHASH_TREE_HASH_TYPE, TXHASH_TREE_HEIGH
25
25
/**
26
26
This handler runs whenever a BlockProposed event is emitted by the blockchain
27
27
*/
28
- async function blockProposedEventHandler ( data ) {
28
+ async function blockProposedEventHandler ( data , syncing ) {
29
29
// ivk will be used to decrypt secrets whilst nsk will be used to calculate nullifiers for commitments and store them
30
30
const { blockNumber : currentBlockCount , transactionHash : transactionHashL1 } = data ;
31
31
const { transactions, block } = await getProposeBlockCalldata ( data ) ;
@@ -38,15 +38,27 @@ async function blockProposedEventHandler(data) {
38
38
// if ((await countCommitments(blockCommitments)) > 0) {
39
39
await saveBlock ( { blockNumber : currentBlockCount , transactionHashL1, ...block } ) ;
40
40
logger . debug ( `Saved L2 block ${ block . blockNumberL2 } , with tx hash ${ transactionHashL1 } ` ) ;
41
- await Promise . all ( transactions . map ( t => saveTransaction ( { transactionHashL1, ...t } ) ) ) ;
41
+ await Promise . all (
42
+ transactions . map ( t =>
43
+ saveTransaction ( {
44
+ transactionHashL1,
45
+ blockNumber : data . blockNumber ,
46
+ blockNumberL2 : block . blockNumberL2 ,
47
+ ...t ,
48
+ } ) . catch ( function ( err ) {
49
+ if ( ! syncing || ! err . message . includes ( 'replay existing transaction' ) ) throw err ;
50
+ logger . warn ( 'Attempted to replay existing transaction. This is expected while syncing' ) ;
51
+ } ) ,
52
+ ) ,
53
+ ) ;
42
54
// }
43
55
44
56
const dbUpdates = transactions . map ( async transaction => {
45
57
// filter out non zero commitments and nullifiers
46
58
const nonZeroCommitments = transaction . commitments . flat ( ) . filter ( n => n !== ZERO ) ;
47
59
const nonZeroNullifiers = transaction . nullifiers . flat ( ) . filter ( n => n !== ZERO ) ;
48
60
if (
49
- ( transaction . transactionType === '1' || transaction . transactionType === '2' ) &&
61
+ ( Number ( transaction . transactionType ) === 1 || Number ( transaction . transactionType ) === 2 ) &&
50
62
( await countCommitments ( nonZeroCommitments ) ) === 0
51
63
)
52
64
await decryptCommitment ( transaction , ivks , nsks ) ;
@@ -69,7 +81,14 @@ async function blockProposedEventHandler(data) {
69
81
HASH_TYPE ,
70
82
TIMBER_HEIGHT ,
71
83
) ;
72
- await saveTree ( transactionHashL1 , block . blockNumberL2 , updatedTimber ) ;
84
+
85
+ try {
86
+ await saveTree ( transactionHashL1 , block . blockNumberL2 , updatedTimber ) ;
87
+ } catch ( err ) {
88
+ // while initial syncing we avoid duplicates errors
89
+ if ( ! syncing || ! err . message . includes ( 'duplicate key' ) ) throw err ;
90
+ }
91
+
73
92
logger . debug ( `Saved tree for L2 block ${ block . blockNumberL2 } ` ) ;
74
93
await Promise . all (
75
94
// eslint-disable-next-line consistent-return
0 commit comments