Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for FastRawTransactionManager.resetNonce #2084

Merged
merged 2 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,9 @@
*/
package org.web3j.protocol.scenarios;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Future;

import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import org.web3j.EVMTest;
import org.web3j.NodeType;
import org.web3j.protocol.Web3j;
Expand All @@ -38,8 +27,17 @@
import org.web3j.tx.response.QueuingTransactionReceiptProcessor;
import org.web3j.utils.Convert;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Future;

import static org.junit.jupiter.api.Assertions.*;
import static org.web3j.tx.TransactionManager.DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH;

@EVMTest(type = NodeType.BESU)
Expand Down Expand Up @@ -76,11 +74,11 @@ public void testTransactionPolling() throws Exception {
}

for (int i = 0;
i < DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH && !transactionReceipts.isEmpty();
i++) {
i < DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH && !transactionReceipts.isEmpty();
i++) {

for (Iterator<Future<TransactionReceipt>> iterator = transactionReceipts.iterator();
iterator.hasNext(); ) {
iterator.hasNext(); ) {
Future<TransactionReceipt> transactionReceiptFuture = iterator.next();

if (transactionReceiptFuture.isDone()) {
Expand All @@ -96,6 +94,32 @@ 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();

BigInteger expected = transactionManager.getCurrentNonce();

transactionManager.resetNonce();
NickSneo marked this conversation as resolved.
Show resolved Hide resolved

BigInteger actual = transactionManager.getCurrentNonce();

createTransaction(transfer, gasPrice).send();

assertEquals(expected, actual);
}

@Test
public void testTransactionQueuing() throws Exception {

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

@Override
public void exception(Exception exception) {}
public void exception(Exception exception) {
}
},
DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH,
POLLING_FREQUENCY));
Expand All @@ -131,8 +156,8 @@ public void exception(Exception exception) {}
}

for (int i = 0;
i < DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH && !pendingTransactions.isEmpty();
i++) {
i < DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH && !pendingTransactions.isEmpty();
i++) {
for (TransactionReceipt transactionReceipt : transactionReceipts) {
assertFalse(transactionReceipt.getBlockHash().isEmpty());
pendingTransactions.remove(transactionReceipt.getTransactionHash());
Expand Down
Loading