Skip to content

Commit 7e3465b

Browse files
authored
Bug fix for FastRawTransactionManager.resetNonce (#2084)
* Bug fix for FastRawTransactionManager.resetNonce Signed-off-by: Junsung Cho <[email protected]> * Add check nonce after reset Signed-off-by: Junsung Cho <[email protected]> --------- Signed-off-by: Junsung Cho <[email protected]>
1 parent fbcbf82 commit 7e3465b

File tree

4 files changed

+96
-20
lines changed

4 files changed

+96
-20
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
99

1010
* Bug fix for Int256 decode range [#2070](https://github.com/hyperledger/web3j/pull/2070)
1111
* Bug fix for BytesType.bytes32PaddedLength [#2089](https://github.com/hyperledger/web3j/pull/2089)
12+
* Bug fix for FastRawTransactionManager.resetNonce [#2084](https://github.com/hyperledger/web3j/pull/2084)
1213

1314
### Features
1415

core/src/main/java/org/web3j/tx/FastRawTransactionManager.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ public BigInteger getCurrentNonce() {
7373
}
7474

7575
public synchronized void resetNonce() throws IOException {
76-
nonce = super.getNonce();
76+
nonce = super.getNonce().subtract(BigInteger.ONE);
77+
}
78+
79+
public synchronized void clearNonce() {
80+
nonce = BigInteger.valueOf(-1);
7781
}
7882

7983
public synchronized void setNonce(BigInteger value) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2024 Web3 Labs Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.web3j.tx;
14+
15+
import java.io.IOException;
16+
import java.math.BigInteger;
17+
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.web3j.crypto.SampleKeys;
22+
import org.web3j.protocol.Web3j;
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.mockito.Mockito.mock;
26+
27+
public class FastRawTransactionManagerTest {
28+
private Web3j web3j;
29+
private FastRawTransactionManager fastRawTransactionManager;
30+
31+
@BeforeEach
32+
public void setUp() throws Exception {
33+
web3j = mock(Web3j.class);
34+
fastRawTransactionManager = new FastRawTransactionManager(web3j, SampleKeys.CREDENTIALS);
35+
}
36+
37+
@Test
38+
void clearNonce() throws IOException {
39+
fastRawTransactionManager.setNonce(BigInteger.valueOf(42));
40+
41+
fastRawTransactionManager.clearNonce();
42+
43+
BigInteger currentNonce = fastRawTransactionManager.getCurrentNonce();
44+
assertEquals(currentNonce, BigInteger.valueOf(-1));
45+
}
46+
}

integration-tests/src/test/java/org/web3j/protocol/scenarios/FastRawTransactionManagerIT.java

+44-19
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,9 @@
1212
*/
1313
package org.web3j.protocol.scenarios;
1414

15-
import java.math.BigDecimal;
16-
import java.math.BigInteger;
17-
import java.util.Iterator;
18-
import java.util.LinkedList;
19-
import java.util.List;
20-
import java.util.Map;
21-
import java.util.concurrent.ConcurrentHashMap;
22-
import java.util.concurrent.ConcurrentLinkedQueue;
23-
import java.util.concurrent.Future;
24-
2515
import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
2616
import org.junit.jupiter.api.BeforeAll;
2717
import org.junit.jupiter.api.Test;
28-
2918
import org.web3j.EVMTest;
3019
import org.web3j.NodeType;
3120
import org.web3j.protocol.Web3j;
@@ -38,8 +27,17 @@
3827
import org.web3j.tx.response.QueuingTransactionReceiptProcessor;
3928
import org.web3j.utils.Convert;
4029

41-
import static org.junit.jupiter.api.Assertions.assertFalse;
42-
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
import java.math.BigDecimal;
31+
import java.math.BigInteger;
32+
import java.util.Iterator;
33+
import java.util.LinkedList;
34+
import java.util.List;
35+
import java.util.Map;
36+
import java.util.concurrent.ConcurrentHashMap;
37+
import java.util.concurrent.ConcurrentLinkedQueue;
38+
import java.util.concurrent.Future;
39+
40+
import static org.junit.jupiter.api.Assertions.*;
4341
import static org.web3j.tx.TransactionManager.DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH;
4442

4543
@EVMTest(type = NodeType.BESU)
@@ -76,11 +74,11 @@ public void testTransactionPolling() throws Exception {
7674
}
7775

7876
for (int i = 0;
79-
i < DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH && !transactionReceipts.isEmpty();
80-
i++) {
77+
i < DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH && !transactionReceipts.isEmpty();
78+
i++) {
8179

8280
for (Iterator<Future<TransactionReceipt>> iterator = transactionReceipts.iterator();
83-
iterator.hasNext(); ) {
81+
iterator.hasNext(); ) {
8482
Future<TransactionReceipt> transactionReceiptFuture = iterator.next();
8583

8684
if (transactionReceiptFuture.isDone()) {
@@ -96,6 +94,32 @@ public void testTransactionPolling() throws Exception {
9694
assertTrue(transactionReceipts.isEmpty());
9795
}
9896

97+
@Test
98+
public void testTransactionResetNonce() throws Exception {
99+
FastRawTransactionManager transactionManager =
100+
new FastRawTransactionManager(
101+
web3j,
102+
ALICE,
103+
new PollingTransactionReceiptProcessor(
104+
web3j, POLLING_FREQUENCY, DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH));
105+
106+
Transfer transfer = new Transfer(web3j, transactionManager);
107+
BigInteger gasPrice = transfer.requestCurrentGasPrice();
108+
109+
createTransaction(transfer, gasPrice).send();
110+
createTransaction(transfer, gasPrice).send();
111+
112+
BigInteger expected = transactionManager.getCurrentNonce();
113+
114+
transactionManager.resetNonce();
115+
116+
BigInteger actual = transactionManager.getCurrentNonce();
117+
118+
createTransaction(transfer, gasPrice).send();
119+
120+
assertEquals(expected, actual);
121+
}
122+
99123
@Test
100124
public void testTransactionQueuing() throws Exception {
101125

@@ -116,7 +140,8 @@ public void accept(TransactionReceipt transactionReceipt) {
116140
}
117141

118142
@Override
119-
public void exception(Exception exception) {}
143+
public void exception(Exception exception) {
144+
}
120145
},
121146
DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH,
122147
POLLING_FREQUENCY));
@@ -131,8 +156,8 @@ public void exception(Exception exception) {}
131156
}
132157

133158
for (int i = 0;
134-
i < DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH && !pendingTransactions.isEmpty();
135-
i++) {
159+
i < DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH && !pendingTransactions.isEmpty();
160+
i++) {
136161
for (TransactionReceipt transactionReceipt : transactionReceipts) {
137162
assertFalse(transactionReceipt.getBlockHash().isEmpty());
138163
pendingTransactions.remove(transactionReceipt.getTransactionHash());

0 commit comments

Comments
 (0)