Skip to content

Commit

Permalink
Consistent nonce handling (#6941)
Browse files Browse the repository at this point in the history
Make handling of absurdly large nonces
more consistent across txpool and EVM.

Signed-off-by: Danno Ferrin <danno@numisight.com>
  • Loading branch information
shemnon authored Apr 12, 2024
1 parent bc04751 commit 27ade87
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public ValidationResult<TransactionInvalidReason> validateForSender(
upfrontCost.toQuantityHexString(), senderBalance.toQuantityHexString()));
}

if (transaction.getNonce() < senderNonce) {
if (Long.compareUnsigned(transaction.getNonce(), senderNonce) < 0) {
return ValidationResult.invalid(
TransactionInvalidReason.NONCE_TOO_LOW,
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ValidationResult<TransactionInvalidReason> validate(

LOG.debug("Validating actual nonce {}, with expected nonce {}", transactionNonce, accountNonce);

if (accountNonce > transactionNonce) {
if (Long.compareUnsigned(accountNonce, transactionNonce) > 0) {
final String errorMessage =
String.format(
"Private Transaction nonce %s, is lower than sender account nonce %s.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class MainnetTransactionValidatorTest {

private final Transaction basicTransaction =
new TransactionTestFixture()
.nonce(30)
.chainId(Optional.of(BigInteger.ONE))
.createTransaction(senderKeys);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void remove(final PendingTransaction invalidatedTx, final RemovalReason r

final var senderTxs = txsBySender.get(invalidatedTx.getSender());
final long invalidNonce = invalidatedTx.getNonce();
if (senderTxs != null && invalidNonce <= senderTxs.lastKey()) {
if (senderTxs != null && Long.compareUnsigned(invalidNonce, senderTxs.lastKey()) <= 0) {
// on sequential layers we need to push to next layer all the txs following the invalid one,
// even if it belongs to a previous layer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ public synchronized void selectTransactions(
.filter(
candidatePendingTx ->
!alreadyChecked.contains(candidatePendingTx.getHash())
&& candidatePendingTx.getNonce() <= highPrioPendingTx.getNonce())
&& Long.compareUnsigned(
candidatePendingTx.getNonce(), highPrioPendingTx.getNonce())
<= 0)
.forEach(
candidatePendingTx -> {
alreadyChecked.add(candidatePendingTx.getHash());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ protected TransactionAddedResult canAdd(
orderByGap.get(gap).add(pendingTransaction);
return gap;
}
if (pendingTransaction.getNonce() < txsBySender.get(sender).firstKey()) {
if (Long.compareUnsigned(
pendingTransaction.getNonce(), txsBySender.get(sender).firstKey())
< 0) {
orderByGap.get(currGap).remove(sender);
orderByGap.get(gap).add(pendingTransaction);
return gap;
Expand Down Expand Up @@ -431,7 +433,7 @@ protected void internalConsistencyCheck(

while (itNonce.hasNext()) {
final long currNonce = itNonce.next().getKey();
assert prevNonce < currNonce : "non incremental nonce";
assert Long.compareUnsigned(prevNonce, currNonce) < 0 : "non incremental nonce";
prevNonce = currNonce;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void trackPendingTransaction(final PendingTransaction pendingTransaction)
synchronized (pendingTransactions) {
if (!pendingTransactions.isEmpty()) {
final long expectedNext = pendingTransactions.lastKey() + 1;
if (nonce > (expectedNext) && nextGap.isEmpty()) {
if (Long.compareUnsigned(nonce, expectedNext) > 0 && nextGap.isEmpty()) {
nextGap = OptionalLong.of(expectedNext);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public static void dumpWorldState(final WorldState worldState, final PrintWriter
out.println(" },");
}
out.print(" \"balance\": \"" + account.getBalance().toShortHexString() + "\"");
if (account.getNonce() > 0) {
if (account.getNonce() != 0) {
out.println(",");
out.println(
" \"nonce\": \""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ static T8nResult runTest(
accountStorageEntry.getValue().toHexString()));
}
accountObject.put("balance", account.getBalance().toShortHexString());
if (account.getNonce() > 0) {
if (account.getNonce() != 0) {
accountObject.put(
"nonce", Bytes.ofUnsignedLong(account.getNonce()).toShortHexString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public ContractCreationProcessor(
private static boolean accountExists(final Account account) {
// The account exists if it has sent a transaction
// or already has its code initialized.
return account.getNonce() > 0 || !account.getCode().isEmpty();
return account.getNonce() != 0 || !account.getCode().isEmpty();
}

@Override
Expand Down

0 comments on commit 27ade87

Please sign in to comment.