Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/models/logic/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ export async function removePendings(hashes: H256[]): Promise<void> {
}

export async function removeOutdatedPendings(
updatedTransactions: SignedTransaction[]
updatedTransactions: SignedTransaction[],
options: { transaction?: Sequelize.Transaction } = {}
): Promise<void> {
try {
await models.Transaction.destroy({
Expand All @@ -419,7 +420,8 @@ export async function removeOutdatedPendings(
}))
}
]
}
},
transaction: options.transaction
});
} catch (err) {
console.error(err);
Expand Down
45 changes: 33 additions & 12 deletions src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ export default class Worker {
}
console.log("%d block is indexing...", nextBlockNumber);
await this.indexNewBlock(nextBlock);
// FIXME: It's slow due to the getSignerAddress()
await TxModel.removeOutdatedPendings(nextBlock.transactions);
console.log("%d block is synchronized", nextBlockNumber);
lastIndexedBlockNumber = nextBlockNumber;
}
Expand Down Expand Up @@ -185,6 +183,11 @@ export default class Worker {

await updateCCCChange(sdk, block, miningReward, transaction);

// FIXME: It's slow due to the getSignerAddress()
await TxModel.removeOutdatedPendings(block.transactions, {
transaction
});

await transaction.commit();
} catch (err) {
await transaction.rollback();
Expand Down Expand Up @@ -215,16 +218,34 @@ export default class Worker {
`Indexed: ${indexedHashes.length} / RPC: ${transactions.length}`
);

// Remove dropped pending transactions
if (transactions.length > 0) {
await TxModel.removeOutdatedPendings(transactions);
}
const transaction = await models.sequelize.transaction({
isolationLevel:
models.Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
deferrable: models.Sequelize.Deferrable.SET_DEFERRED
});
try {
// Remove dropped pending transactions
if (transactions.length > 0) {
await TxModel.removeOutdatedPendings(transactions, {
transaction
});
}

// Index new pending transactions
const newPendingTransactions = _.filter(
transactions,
pending => !_.includes(indexedHashes, pending.hash().value)
);
await TxModel.createTransactions(newPendingTransactions, true);
// Index new pending transactions
const newPendingTransactions = _.filter(
transactions,
pending => !_.includes(indexedHashes, pending.hash().value)
);
await TxModel.createTransactions(
newPendingTransactions,
true,
undefined,
{ transaction }
);
await transaction.commit();
} catch (err) {
await transaction.rollback();
throw err;
}
};
}