Skip to content

Commit

Permalink
feat(core-blockchain): check tx nonce order also wrt previous blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
vasild committed Aug 15, 2019
1 parent fd95a1d commit 549a516
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions packages/core-blockchain/src/processor/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,40 +125,34 @@ export class BlockProcessor {
* increasing nonce without gaps.
*/
private blockContainsOutOfOrderNonce(block: Interfaces.IBlock): boolean {
const latestNonceBySender = {};
const nonceBySender = {};

for (const transaction of block.transactions) {
const data = transaction.data;

if (data.version < 2) {
if (latestNonceBySender[data.senderPublicKey] === undefined) {
continue;
}
continue;
}

this.logger.warn(
`Block { height: ${block.data.height.toLocaleString()}, id: ${block.data.id} } ` +
`not accepted: contains v2 transaction with nonce, followed by ` +
`v1 transaction without nonce for sender ${data.senderPublicKey}.`,
);
const sender: string = data.senderPublicKey;

return true;
if (nonceBySender[sender] === undefined) {
nonceBySender[sender] = this.blockchain.database.walletManager.getNonce(sender);
}

const currentNonce = Utils.BigNumber.make(data.nonce);

if (latestNonceBySender[data.senderPublicKey] !== undefined &&
!latestNonceBySender[data.senderPublicKey].plus(1).isEqualTo(currentNonce)) {
const currentTransactionNonce = Utils.BigNumber.make(data.nonce);

if (!nonceBySender[sender].plus(1).isEqualTo(currentTransactionNonce)) {
this.logger.warn(
`Block { height: ${block.data.height.toLocaleString()}, id: ${block.data.id} } ` +
`not accepted: transactions are out of order with respect to nonce ` +
`for sender ${data.senderPublicKey}.`,
`not accepted: invalid nonce order for sender ${sender}: ` +
`preceding nonce: ${nonceBySender[sender].toFixed()}, ` +
`transaction ${data.id} has nonce ${currentTransactionNonce.toFixed()}.`,
);

return true;
}

latestNonceBySender[data.senderPublicKey] = currentNonce;
nonceBySender[sender] = currentTransactionNonce;
}

return false;
Expand Down

0 comments on commit 549a516

Please sign in to comment.