Skip to content

Commit 7a0fc37

Browse files
committed
fix: avoid saving transactions and timber while syncing
1 parent 373fbfb commit 7a0fc37

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

‎nightfall-client/src/event-handlers/block-proposed.mjs

+23-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const { ZERO, HASH_TYPE, TIMBER_HEIGHT, TXHASH_TREE_HASH_TYPE, TXHASH_TREE_HEIGH
2525
/**
2626
This handler runs whenever a BlockProposed event is emitted by the blockchain
2727
*/
28-
async function blockProposedEventHandler(data) {
28+
async function blockProposedEventHandler(data, syncing) {
2929
// ivk will be used to decrypt secrets whilst nsk will be used to calculate nullifiers for commitments and store them
3030
const { blockNumber: currentBlockCount, transactionHash: transactionHashL1 } = data;
3131
const { transactions, block } = await getProposeBlockCalldata(data);
@@ -38,15 +38,27 @@ async function blockProposedEventHandler(data) {
3838
// if ((await countCommitments(blockCommitments)) > 0) {
3939
await saveBlock({ blockNumber: currentBlockCount, transactionHashL1, ...block });
4040
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+
);
4254
// }
4355

4456
const dbUpdates = transactions.map(async transaction => {
4557
// filter out non zero commitments and nullifiers
4658
const nonZeroCommitments = transaction.commitments.flat().filter(n => n !== ZERO);
4759
const nonZeroNullifiers = transaction.nullifiers.flat().filter(n => n !== ZERO);
4860
if (
49-
(transaction.transactionType === '1' || transaction.transactionType === '2') &&
61+
(Number(transaction.transactionType) === 1 || Number(transaction.transactionType) === 2) &&
5062
(await countCommitments(nonZeroCommitments)) === 0
5163
)
5264
await decryptCommitment(transaction, ivks, nsks);
@@ -69,7 +81,14 @@ async function blockProposedEventHandler(data) {
6981
HASH_TYPE,
7082
TIMBER_HEIGHT,
7183
);
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+
7392
logger.debug(`Saved tree for L2 block ${block.blockNumberL2}`);
7493
await Promise.all(
7594
// eslint-disable-next-line consistent-return

‎nightfall-client/src/services/state-sync.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const syncState = async (fromBlock = 'earliest', toBlock = 'latest', eventFilter
4141
switch (pastStateEvents[i].event) {
4242
case 'BlockProposed':
4343
// eslint-disable-next-line no-await-in-loop
44-
await blockProposedEventHandler(pastStateEvents[i]);
44+
await blockProposedEventHandler(pastStateEvents[i], true);
4545
break;
4646
case 'Rollback':
4747
// eslint-disable-next-line no-await-in-loop

‎nightfall-client/start-client

+17-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ usage()
1616
{
1717
echo "Usage:"
1818
echo " -d or --dev; to bind mount the filesystem and use it for development"
19+
echo " -r; to remove existing volumes for mongodb and abi contracts"
1920
}
2021

2122
# delete env file
@@ -24,6 +25,8 @@ while [ -n "$1" ]; do
2425
case $1 in
2526
-d | --dev ) DEV="-f ../docker-compose.client.dev.yml"
2627
;;
28+
-r ) REMOVE_VOLUMES="true"
29+
;;
2730
-h | --help ) usage
2831
;;
2932
* ) usage
@@ -36,22 +39,25 @@ if [ -z "$FILE" ]; then
3639
usage
3740
exit 1
3841
fi
42+
43+
if [ -n "$REMOVE_VOLUMES" ]; then
44+
# if-else block checks - volume exist and then removes it.
45+
if [[ $(echo $VOLUME_LIST | grep nightfall_3_mongodb1) ]]; then
46+
echo -n 'Removing '
47+
docker volume rm nightfall_3_mongodb1
48+
fi
49+
50+
if [[ $(echo $VOLUME_LIST | grep nightfall_3_build) ]]; then
51+
echo -n 'Removing '
52+
docker volume rm nightfall_3_build
53+
fi
54+
fi
55+
3956
# shut down cleanly in the event of a cntl-c etc. We don't want to leave containers running
4057
trap "docker-compose $FILE $DEV down --remove-orphans -t 1; exit 1" SIGHUP SIGINT SIGTERM
4158

4259
docker-compose $FILE $DEV down --remove-orphans
4360

44-
# if-else block checks - volume exist and then removes it.
45-
if [[ $(echo $VOLUME_LIST | grep nightfall_3_mongodb1) ]]; then
46-
echo -n 'Removing '
47-
docker volume rm nightfall_3_mongodb1
48-
fi
49-
50-
if [[ $(echo $VOLUME_LIST | grep nightfall_3_build) ]]; then
51-
echo -n 'Removing '
52-
docker volume rm nightfall_3_build
53-
fi
54-
5561
DIR=../common-files/node_modules
5662
if [[ -d "$DIR" ]]; then
5763
rm -dr common-files/node_modules

0 commit comments

Comments
 (0)