Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

fix: nonce generation race conditions #3498

Draft
wants to merge 21 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 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
41 changes: 34 additions & 7 deletions src/chains/ethereum/ethereum/src/transaction-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ function shouldReplace(
return true;
}
}
function findHighestNonceByOrigin(set: Set<TypedTransaction>, origin: string) {
let highestNonce: bigint = null;
for (const transaction of set) {
if (
(transaction.from.toString() === origin &&
transaction.nonce.toBigInt() > highestNonce) ||
highestNonce === null
) {
highestNonce = transaction.nonce.toBigInt();
}
}
return highestNonce;
}

function byNonce(values: TypedTransaction[], a: number, b: number) {
return (
Expand Down Expand Up @@ -231,8 +244,8 @@ export default class TransactionPool extends Emittery<{ drain: undefined }> {
const queuedOriginTransactions = origins.get(origin);

let transactionPlacement = TriageOption.FutureQueue;
const executables = this.executables.pending;
let executableOriginTransactions = executables.get(origin);
const { pending, inProgress } = this.executables;
let executableOriginTransactions = pending.get(origin);

const priceBump = this.#priceBump;
let length: number;
Expand Down Expand Up @@ -284,19 +297,33 @@ export default class TransactionPool extends Emittery<{ drain: undefined }> {
// since we don't have any executable transactions at the moment, we need
// to find our nonce from the account itself...
const transactorNonce = transactor.nonce.toBigInt();

const highestNonceFromOrigin = findHighestNonceByOrigin(
inProgress,
origin
);

const useHighestNonce =
highestNonceFromOrigin !== null &&
highestNonceFromOrigin >= transactorNonce;

const effectiveNonce = useHighestNonce
? highestNonceFromOrigin + 1n
: transactorNonce || 0n;

if (txNonce === void 0) {
// if we don't have a transactionNonce, just use the account's next
// nonce and mark as executable
txNonce = transactorNonce ? transactorNonce : 0n;
txNonce = effectiveNonce;
transaction.nonce = Quantity.from(txNonce);
transactionPlacement = TriageOption.Executable;
} else if (txNonce < transactorNonce) {
} else if (txNonce < effectiveNonce) {
// it's an error if the transaction's nonce is <= the persisted nonce
throw new CodedError(
`the tx doesn't have the correct nonce. account has nonce of: ${transactorNonce} tx has nonce of: ${txNonce}`,
`the tx doesn't have the correct nonce. account has nonce of: ${effectiveNonce} tx has nonce of: ${txNonce}`,
JsonRpcErrorCode.INVALID_INPUT
);
} else if (txNonce === transactorNonce) {
} else if (txNonce === effectiveNonce) {
transactionPlacement = TriageOption.Executable;
}
}
Expand Down Expand Up @@ -352,7 +379,7 @@ export default class TransactionPool extends Emittery<{ drain: undefined }> {
} else {
// if we don't yet have an executables queue for this origin make one now
executableOriginTransactions = Heap.from(transaction, byNonce);
executables.set(origin, executableOriginTransactions);
pending.set(origin, executableOriginTransactions);
}

// Now we need to drain any queued transactions that were previously
Expand Down
Loading