From aeba374348df078b28122c413b7c104008b104cc Mon Sep 17 00:00:00 2001 From: Junsung Cho Date: Thu, 25 Jul 2024 09:34:50 +0900 Subject: [PATCH] Bug fix for FastRawTransactionManager.resetNonce Signed-off-by: Junsung Cho --- CHANGELOG.md | 1 + .../web3j/tx/FastRawTransactionManager.java | 6 ++- .../tx/FastRawTransactionManagerTest.java | 46 +++++++++++++++++++ .../FastRawTransactionManagerIT.java | 21 +++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/org/web3j/tx/FastRawTransactionManagerTest.java 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) { diff --git a/core/src/test/java/org/web3j/tx/FastRawTransactionManagerTest.java b/core/src/test/java/org/web3j/tx/FastRawTransactionManagerTest.java new file mode 100644 index 000000000..37f30a1f3 --- /dev/null +++ b/core/src/test/java/org/web3j/tx/FastRawTransactionManagerTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2024 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.tx; + +import java.io.IOException; +import java.math.BigInteger; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import org.web3j.crypto.SampleKeys; +import org.web3j.protocol.Web3j; + +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)); + } +} diff --git a/integration-tests/src/test/java/org/web3j/protocol/scenarios/FastRawTransactionManagerIT.java b/integration-tests/src/test/java/org/web3j/protocol/scenarios/FastRawTransactionManagerIT.java index c84c6e92e..fe6c98cbe 100644 --- a/integration-tests/src/test/java/org/web3j/protocol/scenarios/FastRawTransactionManagerIT.java +++ b/integration-tests/src/test/java/org/web3j/protocol/scenarios/FastRawTransactionManagerIT.java @@ -38,6 +38,7 @@ import org.web3j.tx.response.QueuingTransactionReceiptProcessor; import org.web3j.utils.Convert; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.web3j.tx.TransactionManager.DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH; @@ -96,6 +97,26 @@ public void testTransactionPolling() throws Exception { assertTrue(transactionReceipts.isEmpty()); } + @Test + public void testTransactionResetNonce() throws Exception { + FastRawTransactionManager transactionManager = + new FastRawTransactionManager( + web3j, + ALICE, + new PollingTransactionReceiptProcessor( + web3j, POLLING_FREQUENCY, DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH)); + + Transfer transfer = new Transfer(web3j, transactionManager); + BigInteger gasPrice = transfer.requestCurrentGasPrice(); + + createTransaction(transfer, gasPrice).send(); + createTransaction(transfer, gasPrice).send(); + transactionManager.resetNonce(); + createTransaction(transfer, gasPrice).send(); + + assertEquals(transactionManager.getCurrentNonce(), BigInteger.valueOf(2)); + } + @Test public void testTransactionQueuing() throws Exception {