Skip to content

Commit

Permalink
Bug fix for FastRawTransactionManager.resetNonce
Browse files Browse the repository at this point in the history
Signed-off-by: Junsung Cho <[email protected]>
  • Loading branch information
junsung-cho committed Aug 5, 2024
1 parent 8d9e8ee commit f34ea4b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
### Bug Fixes

* Bug fix for Int256 decode range [2^248, type(int256).max] and [ type(int256.min), -(2^248) )
* Bug fix for FastRawTransactionManager.resetNonce [#2084](https://github.com/hyperledger/web3j/pull/2084)

### Features

Expand Down
12 changes: 8 additions & 4 deletions core/src/main/java/org/web3j/tx/FastRawTransactionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
*/
package org.web3j.tx;

import java.io.IOException;
import java.math.BigInteger;

import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.service.TxSignService;
import org.web3j.tx.response.TransactionReceiptProcessor;

import java.io.IOException;
import java.math.BigInteger;

/**
* Simple RawTransactionManager derivative that manages nonces to facilitate multiple transactions
* per block.
Expand Down Expand Up @@ -73,7 +73,11 @@ public BigInteger getCurrentNonce() {
}

public synchronized void resetNonce() throws IOException {
nonce = super.getNonce();
nonce = super.getNonce().subtract(BigInteger.ONE);
}

public synchronized void clearNonce() {
nonce = BigInteger.valueOf(-1);
}

public synchronized void setNonce(BigInteger value) {
Expand Down
33 changes: 33 additions & 0 deletions core/src/test/java/org/web3j/tx/FastRawTransactionManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.web3j.tx;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.web3j.crypto.SampleKeys;
import org.web3j.protocol.Web3j;

import java.io.IOException;
import java.math.BigInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;

public class FastRawTransactionManagerTest {
private Web3j web3j;
private FastRawTransactionManager fastRawTransactionManager;

@BeforeEach
public void setUp() throws Exception {
web3j = mock(Web3j.class);
fastRawTransactionManager = new FastRawTransactionManager(web3j, SampleKeys.CREDENTIALS);
}

@Test
void clearNonce() throws IOException {
fastRawTransactionManager.setNonce(BigInteger.valueOf(42));

fastRawTransactionManager.clearNonce();

BigInteger currentNonce = fastRawTransactionManager.getCurrentNonce();
assertEquals(currentNonce, BigInteger.valueOf(-1));
}
}

0 comments on commit f34ea4b

Please sign in to comment.