Skip to content

Commit 9a96134

Browse files
authored
Merge pull request #2938 from rsksmart/improve_ethModule_data_parsing
Extract unnecessary logic out of the synchronized
2 parents de8bc5d + 9d11977 commit 9a96134

File tree

2 files changed

+32
-38
lines changed

2 files changed

+32
-38
lines changed

rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionBase.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@
1818

1919
package co.rsk.rpc.modules.eth;
2020

21-
import static org.ethereum.rpc.exception.RskJsonRpcRequestException.invalidParamError;
22-
21+
import co.rsk.core.RskAddress;
22+
import co.rsk.core.Wallet;
23+
import co.rsk.net.TransactionGateway;
2324
import co.rsk.util.RLPException;
2425
import org.ethereum.config.Constants;
25-
import org.ethereum.core.Account;
26-
import org.ethereum.core.ImmutableTransaction;
27-
import org.ethereum.core.Transaction;
28-
import org.ethereum.core.TransactionArguments;
29-
import org.ethereum.core.TransactionPool;
30-
import org.ethereum.core.TransactionPoolAddResult;
26+
import org.ethereum.core.*;
3127
import org.ethereum.rpc.CallArguments;
3228
import org.ethereum.rpc.exception.RskJsonRpcRequestException;
3329
import org.ethereum.rpc.parameters.CallArgumentsParam;
@@ -36,9 +32,7 @@
3632
import org.slf4j.Logger;
3733
import org.slf4j.LoggerFactory;
3834

39-
import co.rsk.core.RskAddress;
40-
import co.rsk.core.Wallet;
41-
import co.rsk.net.TransactionGateway;
35+
import static org.ethereum.rpc.exception.RskJsonRpcRequestException.invalidParamError;
4236

4337
public class EthModuleTransactionBase implements EthModuleTransaction {
4438

@@ -60,7 +54,7 @@ public EthModuleTransactionBase(Constants constants, Wallet wallet, TransactionP
6054
public synchronized String sendTransaction(CallArgumentsParam argsParam) {
6155
CallArguments args = argsParam.toCallArguments();
6256

63-
if(args.getFrom() == null) {
57+
if (args.getFrom() == null) {
6458
throw invalidParamError("from is null");
6559
}
6660

@@ -73,19 +67,17 @@ public synchronized String sendTransaction(CallArgumentsParam argsParam) {
7367
String txHash = null;
7468

7569
try {
70+
TransactionArguments txArgs = TransactionArgumentsUtil.processArguments(args, constants.getChainId());
7671

7772
synchronized (transactionPool) {
78-
79-
TransactionArguments txArgs = TransactionArgumentsUtil.processArguments(args, transactionPool, senderAccount, constants.getChainId());
80-
73+
if (txArgs.getNonce() == null) {
74+
txArgs.setNonce(transactionPool.getPendingState().getNonce(senderAccount.getAddress()));
75+
}
8176
Transaction tx = Transaction.builder().withTransactionArguments(txArgs).build();
82-
8377
tx.sign(senderAccount.getEcKey().getPrivKeyBytes());
84-
8578
if (!tx.acceptTransactionSignature(constants.getChainId())) {
8679
throw RskJsonRpcRequestException.invalidParamError(TransactionArgumentsUtil.ERR_INVALID_CHAIN_ID + args.getChainId());
8780
}
88-
8981
TransactionPoolAddResult result = transactionGateway.receiveTransaction(tx.toImmutableTransaction());
9082

9183
if (!result.transactionsWereAdded()) {

rskj-core/src/main/java/org/ethereum/util/TransactionArgumentsUtil.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818

1919
package org.ethereum.util;
2020

21-
import java.math.BigInteger;
22-
import java.util.Optional;
23-
import java.util.function.Supplier;
24-
21+
import co.rsk.util.HexUtils;
2522
import org.ethereum.core.Account;
2623
import org.ethereum.core.TransactionArguments;
2724
import org.ethereum.core.TransactionPool;
2825
import org.ethereum.rpc.CallArguments;
2926
import org.ethereum.rpc.exception.RskJsonRpcRequestException;
3027
import org.ethereum.vm.GasCost;
3128

32-
import co.rsk.util.HexUtils;
29+
import java.math.BigInteger;
30+
import java.util.Optional;
31+
import java.util.function.Supplier;
3332

3433
public class TransactionArgumentsUtil {
3534

@@ -38,10 +37,13 @@ public class TransactionArgumentsUtil {
3837
public static final String ERR_INVALID_CHAIN_ID = "Invalid chainId: ";
3938
public static final String ERR_COULD_NOT_FIND_ACCOUNT = "Could not find account for address: ";
4039

41-
/**
42-
* transform the Web3.CallArguments in TransactionArguments that can be used in
43-
* the TransactionBuilder
44-
*/
40+
public static TransactionArguments processArguments(CallArguments argsParam, byte defaultChainId) {
41+
return processArguments(argsParam, null, null, defaultChainId);
42+
}
43+
/**
44+
* transform the Web3.CallArguments in TransactionArguments that can be used in
45+
* the TransactionBuilder
46+
*/
4547
public static TransactionArguments processArguments(CallArguments argsParam, TransactionPool transactionPool, Account senderAccount, byte defaultChainId) {
4648

4749
TransactionArguments argsRet = new TransactionArguments();
@@ -50,7 +52,13 @@ public static TransactionArguments processArguments(CallArguments argsParam, Tra
5052

5153
argsRet.setTo(stringHexToByteArray(argsParam.getTo()));
5254

53-
argsRet.setNonce(strHexOrStrNumberToBigInteger(argsParam.getNonce(), () -> transactionPool.getPendingState().getNonce(senderAccount.getAddress())));
55+
if(transactionPool == null || senderAccount == null) {
56+
argsRet.setNonce(Optional.ofNullable(argsParam.getNonce())
57+
.map(HexUtils::strHexOrStrNumberToBigInteger)
58+
.orElse(null));
59+
} else {
60+
argsRet.setNonce(strHexOrStrNumberToBigInteger(argsParam.getNonce(), () -> transactionPool.getPendingState().getNonce(senderAccount.getAddress())));
61+
}
5462

5563
argsRet.setValue(strHexOrStrNumberToBigInteger(argsParam.getValue(), () -> BigInteger.ZERO));
5664

@@ -75,18 +83,12 @@ public static TransactionArguments processArguments(CallArguments argsParam, Tra
7583
return argsRet;
7684
}
7785

78-
private static byte[] stringHexToByteArray(String value) {
79-
80-
byte[] ret = Optional.ofNullable(value).map(HexUtils::stringHexToByteArray).orElse(null);
81-
82-
return ret;
83-
}
86+
private static byte[] stringHexToByteArray(String value) {
87+
return Optional.ofNullable(value).map(HexUtils::stringHexToByteArray).orElse(null);
88+
}
8489

8590
private static BigInteger strHexOrStrNumberToBigInteger(String value, Supplier<BigInteger> getDefaultValue) {
86-
87-
BigInteger ret = Optional.ofNullable(value).map(HexUtils::strHexOrStrNumberToBigInteger).orElseGet(getDefaultValue);
88-
89-
return ret;
91+
return Optional.ofNullable(value).map(HexUtils::strHexOrStrNumberToBigInteger).orElseGet(getDefaultValue);
9092
}
9193

9294
private static byte hexToChainId(String hex) {

0 commit comments

Comments
 (0)