Skip to content

Commit cfbedbc

Browse files
committed
fix: chain reorg now copes with null blocks
1 parent 30db801 commit cfbedbc

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async function blockProposedEventHandler(data) {
8484
// await Promise.all(toStore);
8585
await Promise.all(dbUpdates);
8686
const updatedTimber = Timber.statelessUpdate(latestTree, blockCommitments);
87-
await saveTree(data.blockNumber, block.blockNumberL2, updatedTimber);
87+
await saveTree(transactionHashL1, block.blockNumberL2, updatedTimber);
8888
logger.debug(`Saved tree for L2 block ${block.blockNumberL2}`);
8989
await Promise.all(
9090
// eslint-disable-next-line consistent-return

‎nightfall-client/src/event-handlers/chain-reorg.mjs

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from '../services/commitment-storage.mjs';
1717
import {
1818
getBlockByTransactionHashL1,
19-
deleteTreeByBlockNumberL2,
19+
deleteTreeByTransactionHashL1,
2020
clearBlockNumberL1ForBlock,
2121
} from '../services/database.mjs';
2222

@@ -42,14 +42,12 @@ async function removeBlockProposedEventHandler(eventObject) {
4242
// so find out which L2 block has been removed by this event removal.
4343
logger.debug(`Looking for block with transactionHash, ${transactionHash}`);
4444
const block = await getBlockByTransactionHashL1(transactionHash);
45-
if (block) logger.debug(`Found L2 block ${block.blockNumberL2}`);
46-
else throw new Error(`Could not find L2 block with L1 transactionHash, ${transactionHash}`);
47-
// then we delete the Timber record associated with this block
48-
const res = await deleteTreeByBlockNumberL2(block.blockNumberL2);
49-
logger.debug(`Deleted tree with block number ${block.blockNumberL2}, ${res}`);
5045
// now we can clear the L1 blocknumber to indicate that the L2 block is no longer
51-
// on chain.
52-
return clearBlockNumberL1ForBlock(eventObject.transactionHash);
46+
// on chain (if it's a block that we have saved)
47+
if (block) await clearBlockNumberL1ForBlock(transactionHash);
48+
// then we delete the Timber record associated with this block
49+
logger.debug(`Deleting tree with proposeBlock transactionHash ${transactionHash}`);
50+
return deleteTreeByTransactionHashL1(transactionHash);
5351
}
5452

5553
export default removeBlockProposedEventHandler;

‎nightfall-client/src/services/database.mjs

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ const {
2222
Timber functions
2323
*/
2424

25-
export async function saveTree(blockNumber, blockNumberL2, timber) {
25+
export async function saveTree(transactionHashL1, blockNumberL2, timber) {
2626
const connection = await mongo.connection(MONGO_URL);
2727
const db = connection.db(COMMITMENTS_DB);
2828
return db.collection(TIMBER_COLLECTION).insertOne({
29-
_id: blockNumber,
29+
_id: transactionHashL1,
3030
blockNumberL2,
3131
frontier: timber.frontier,
3232
leafCount: timber.leafCount,
@@ -75,6 +75,12 @@ export async function deleteTreeByBlockNumberL2(blockNumberL2) {
7575
return db.collection(TIMBER_COLLECTION).deleteMany({ blockNumberL2: { $gte: blockNumberL2 } });
7676
}
7777

78+
export async function deleteTreeByTransactionHashL1(transactionHashL1) {
79+
const connection = await mongo.connection(MONGO_URL);
80+
const db = connection.db(COMMITMENTS_DB);
81+
return db.collection(TIMBER_COLLECTION).deleteOne({ transactionHashL1 });
82+
}
83+
7884
/**
7985
Blocks Collection
8086
*/

‎test/ping-pong/user-local/src/index.mjs

+16-12
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,26 @@ async function localTest() {
6969
}
7070
await nf3.deposit(ercAddress, tokenType, value, tokenId);
7171
await new Promise(resolve => setTimeout(resolve, TX_WAIT)); // this may need to be longer on a real blockchain
72+
console.log(`Completed ${i + 1} pings`);
7273
}
7374

7475
// Wait for sometime at the end to retrieve balance to include any transactions sent by the other use
7576
// This needs to be much longer than we may have waited for a transfer
76-
await new Promise(resolving => setTimeout(resolving, 20 * TX_WAIT)); // TODO get balance waiting working well
77-
const endBalance = await retrieveL2Balance(nf3);
78-
79-
if (endBalance - startBalance === 2 * value + value * TEST_LENGTH) {
80-
logger.info('Test passed');
81-
logger.info('Balance of User (2*value (2*1) + value received) ', endBalance - startBalance);
82-
logger.info('Amount sent to other User', value * TEST_LENGTH);
83-
nf3.close();
84-
} else {
85-
logger.info('The test failed because the L2 balance has not increased');
86-
process.exit(1);
87-
}
77+
let loop = 0;
78+
do {
79+
const endBalance = await retrieveL2Balance(nf3);
80+
if (endBalance - startBalance === 2 * value + value * TEST_LENGTH) {
81+
logger.info('Test passed');
82+
logger.info('Balance of User (2*value (2*1) + value received) ', endBalance - startBalance);
83+
logger.info('Amount sent to other User', value * TEST_LENGTH);
84+
nf3.close();
85+
break;
86+
} else {
87+
logger.info('The test has not yet passed because the L2 balance has not increased - waiting');
88+
await new Promise(resolving => setTimeout(resolving, 20 * TX_WAIT)); // TODO get balance waiting working well
89+
loop++;
90+
}
91+
} while (loop < 10);
8892
}
8993

9094
localTest();

0 commit comments

Comments
 (0)