Repository: LFDT-web3j/web3j (Java)

Issue:
FastRawTransactionManager's resetNonce() will bring its nonce field out of sync
# FastRawTransactionManager's function resetNonce() will bring its nonce field out of sync

## Steps To Reproduce
1. Using FastRawTransactionManager to instantiate a web3j contract instance.
MyContract myContractInstance = MyContract.load(myContractAddress, myWeb3jInstance, myFastRawTxManager, myGasProvider); 

2. Call resetNonce on the FastRawTransactionManager instance:
myFastRawTxManager.resetNonce();

3. Send a contract transaction:
RemoteFunctionCall<TransactionReceipt> rfc = myContractInstance.myMethod().send();

4. The result is that the transaction times out:
"Transaction receipt was not generated after 40 seconds for transaction: 0x21d063bb9057abc172bb7b590a3ea03d3a76eabc5409675fed961269a9d3f1e7"

### Expected behavior
The expectation is that the transaction will succeed.

### Actual behavior
Originally:
- the FastRawTransactionManager's nonce is "-1" 
- the network's TransactionCount for the current user is "n".

Calling resetNonce() will result in:
- the FastRawTransactionManager's nonce is "n" 
- the network's TransactionCount for the current user is "n".

Creating the TX will perform a call to:
```
    protected synchronized BigInteger getNonce() throws IOException {
        if (nonce.signum() == -1) {
            // obtain lock
            nonce = super.getNonce();
        } else {
            nonce = nonce.add(BigInteger.ONE);
        }
        return nonce;
    }
```
that will return an incremented nonce (one with value "n+1"), so the transaction will remain in the node's Transaction Pool until another transaction with a nonce with value "n" is sent. 
In the most scenario's this will occur only after:
- restarting the application containing the web3j instance;
or
- calling FastRawTransactionManager's setNonce (-1); // of course with casting to BigInteger
or
- calling FastRawTransactionManager's setNonce (n-1); // of course with casting to BigInteger

## Environment
_Describe the environment in which the issue occurs_
- Web3j 4.10.3
- Java 17
- ubuntu 22.04


## Additional context
_Add any other context about the problem here._
- Logs
- Sample code and/or code snippets
- Unit/integration tests to highlight the issue
- [etherscan](https://etherscan.io/) references  





Discussion: I had the same problem. As a workaround I solved it by using the `setNonce(BigInteger.valueOf(-1L))` instead of resetNonce().
This will trigger the super.getNonce() in the next call to getNonce(), which makes the call to the blockchain, when the next transaction is processed. 



Fix (patch):
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a526e3316..ff707a791 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
 
 * Bug fix for Int256 decode range [#2070](https://github.com/hyperledger/web3j/pull/2070)
 * Bug fix for BytesType.bytes32PaddedLength [#2089](https://github.com/hyperledger/web3j/pull/2089)
+* Bug fix for FastRawTransactionManager.resetNonce [#2084](https://github.com/hyperledger/web3j/pull/2084)
 
 ### Features
 
diff --git a/core/src/main/java/org/web3j/tx/FastRawTransactionManager.java b/core/src/main/java/org/web3j/tx/FastRawTransactionManager.java
index 3ccd2b876..0cbf1ea6b 100644
--- a/core/src/main/java/org/web3j/tx/FastRawTransactionManager.java
+++ b/core/src/main/java/org/web3j/tx/FastRawTransactionManager.java
@@ -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) {