Skip to content

Commit

Permalink
fix: clean up proposer blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyasRidhuan committed Nov 24, 2021
1 parent d6506d2 commit 1d53424
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
17 changes: 4 additions & 13 deletions nightfall-optimist/src/event-handlers/new-current-proposer.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import config from 'config';
import logger from 'common-files/utils/logger.mjs';
import { getContractInstance } from 'common-files/utils/contract.mjs';
import {
isRegisteredProposerAddressMine,
addTransactionsToMemPoolFromBlockNumberL2,
resetUnsuccessfulBlockProposedTransactions,
} from '../services/database.mjs';

const { STATE_CONTRACT_NAME } = config;

/**
This handler runs whenever a BlockProposed event is emitted by the blockchain
*/
Expand All @@ -27,14 +23,9 @@ async function newCurrentProposerEventHandler(data, args) {

// If we were the last proposer return any transactions that were removed from the mempool
// because they were included in proposed blocks that did not eventually make it on chain.
if (weWereLastProposer && !proposer.isMe) {
const stateContractInstance = await getContractInstance(STATE_CONTRACT_NAME);
const onChainBlockCount = Number(
await stateContractInstance.methods.getNumberOfL2Blocks().call(),
);
// All transactions greater or equal to this block count need to be reset.
logger.info(`Resetting Transactions from :${onChainBlockCount}`);
await addTransactionsToMemPoolFromBlockNumberL2(onChainBlockCount);
if (weWereLastProposer) {
logger.info(`Resetting Transactions`);
await resetUnsuccessfulBlockProposedTransactions();
}

// !! converts this to a "is not null" check - i.e. false if is null
Expand Down
11 changes: 5 additions & 6 deletions nightfall-optimist/src/services/database.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,13 @@ export async function getBlocks() {
.toArray();
}

/**
Function to add a set of transactions from the layer 2 mempool once a block has been rolled back
*/
export async function addTransactionsToMemPoolFromBlockNumberL2(blockNumberL2) {
// This function is useful in resetting transacations that have been marked out of the mempool because
// we have included them in blocks, but those blocks did not end up being mined on-chain.
export async function resetUnsuccessfulBlockProposedTransactions() {
const connection = await mongo.connection(MONGO_URL);
const db = connection.db(OPTIMIST_DB);
const query = { blockNumberL2: { $gte: Number(blockNumberL2) } };
const update = { $set: { mempool: true, blockNumberL2: -1 } };
const query = { blockNumberL2: -1, mempool: false }; // Transactions out of mempool but not yet on chain
const update = { $set: { mempool: true } };
return db.collection(TRANSACTIONS_COLLECTION).updateMany(query, update);
}

Expand Down

0 comments on commit 1d53424

Please sign in to comment.