From be1a51f27b07423e6cdb43fc3a027f95e59b54ac Mon Sep 17 00:00:00 2001 From: Angel Soto Date: Fri, 17 Jan 2025 12:06:54 +0100 Subject: [PATCH 1/2] Extract parameter handling logic out of the sync block --- .../modules/eth/EthModuleTransactionBase.java | 28 +++++-------- .../util/TransactionArgumentsUtil.java | 42 ++++++++++--------- 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionBase.java b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionBase.java index f2b7a125a86..26f277a92a8 100644 --- a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionBase.java +++ b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionBase.java @@ -18,16 +18,12 @@ package co.rsk.rpc.modules.eth; -import static org.ethereum.rpc.exception.RskJsonRpcRequestException.invalidParamError; - +import co.rsk.core.RskAddress; +import co.rsk.core.Wallet; +import co.rsk.net.TransactionGateway; import co.rsk.util.RLPException; import org.ethereum.config.Constants; -import org.ethereum.core.Account; -import org.ethereum.core.ImmutableTransaction; -import org.ethereum.core.Transaction; -import org.ethereum.core.TransactionArguments; -import org.ethereum.core.TransactionPool; -import org.ethereum.core.TransactionPoolAddResult; +import org.ethereum.core.*; import org.ethereum.rpc.CallArguments; import org.ethereum.rpc.exception.RskJsonRpcRequestException; import org.ethereum.rpc.parameters.CallArgumentsParam; @@ -36,9 +32,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import co.rsk.core.RskAddress; -import co.rsk.core.Wallet; -import co.rsk.net.TransactionGateway; +import static org.ethereum.rpc.exception.RskJsonRpcRequestException.invalidParamError; public class EthModuleTransactionBase implements EthModuleTransaction { @@ -60,7 +54,7 @@ public EthModuleTransactionBase(Constants constants, Wallet wallet, TransactionP public synchronized String sendTransaction(CallArgumentsParam argsParam) { CallArguments args = argsParam.toCallArguments(); - if(args.getFrom() == null) { + if (args.getFrom() == null) { throw invalidParamError("from is null"); } @@ -73,19 +67,17 @@ public synchronized String sendTransaction(CallArgumentsParam argsParam) { String txHash = null; try { + TransactionArguments txArgs = TransactionArgumentsUtil.processArguments(args, constants.getChainId()); synchronized (transactionPool) { - - TransactionArguments txArgs = TransactionArgumentsUtil.processArguments(args, transactionPool, senderAccount, constants.getChainId()); - + if (txArgs.getNonce() == null) { + txArgs.setNonce(transactionPool.getPendingState().getNonce(senderAccount.getAddress())); + } Transaction tx = Transaction.builder().withTransactionArguments(txArgs).build(); - tx.sign(senderAccount.getEcKey().getPrivKeyBytes()); - if (!tx.acceptTransactionSignature(constants.getChainId())) { throw RskJsonRpcRequestException.invalidParamError(TransactionArgumentsUtil.ERR_INVALID_CHAIN_ID + args.getChainId()); } - TransactionPoolAddResult result = transactionGateway.receiveTransaction(tx.toImmutableTransaction()); if (!result.transactionsWereAdded()) { diff --git a/rskj-core/src/main/java/org/ethereum/util/TransactionArgumentsUtil.java b/rskj-core/src/main/java/org/ethereum/util/TransactionArgumentsUtil.java index efe1cbf09f8..ac1d21dbce9 100644 --- a/rskj-core/src/main/java/org/ethereum/util/TransactionArgumentsUtil.java +++ b/rskj-core/src/main/java/org/ethereum/util/TransactionArgumentsUtil.java @@ -18,10 +18,7 @@ package org.ethereum.util; -import java.math.BigInteger; -import java.util.Optional; -import java.util.function.Supplier; - +import co.rsk.util.HexUtils; import org.ethereum.core.Account; import org.ethereum.core.TransactionArguments; import org.ethereum.core.TransactionPool; @@ -29,7 +26,9 @@ import org.ethereum.rpc.exception.RskJsonRpcRequestException; import org.ethereum.vm.GasCost; -import co.rsk.util.HexUtils; +import java.math.BigInteger; +import java.util.Optional; +import java.util.function.Supplier; public class TransactionArgumentsUtil { @@ -38,10 +37,13 @@ public class TransactionArgumentsUtil { public static final String ERR_INVALID_CHAIN_ID = "Invalid chainId: "; public static final String ERR_COULD_NOT_FIND_ACCOUNT = "Could not find account for address: "; - /** - * transform the Web3.CallArguments in TransactionArguments that can be used in - * the TransactionBuilder - */ + public static TransactionArguments processArguments(CallArguments argsParam, byte defaultChainId) { + return processArguments(argsParam, null, null, defaultChainId); + } + /** + * transform the Web3.CallArguments in TransactionArguments that can be used in + * the TransactionBuilder + */ public static TransactionArguments processArguments(CallArguments argsParam, TransactionPool transactionPool, Account senderAccount, byte defaultChainId) { TransactionArguments argsRet = new TransactionArguments(); @@ -50,7 +52,13 @@ public static TransactionArguments processArguments(CallArguments argsParam, Tra argsRet.setTo(stringHexToByteArray(argsParam.getTo())); - argsRet.setNonce(strHexOrStrNumberToBigInteger(argsParam.getNonce(), () -> transactionPool.getPendingState().getNonce(senderAccount.getAddress()))); + if(transactionPool == null || senderAccount == null) { + argsRet.setNonce(Optional.ofNullable(argsParam.getNonce()) + .map(HexUtils::strHexOrStrNumberToBigInteger) + .orElse(null)); + }else { + argsRet.setNonce(strHexOrStrNumberToBigInteger(argsParam.getNonce(), () -> transactionPool.getPendingState().getNonce(senderAccount.getAddress()))); + } argsRet.setValue(strHexOrStrNumberToBigInteger(argsParam.getValue(), () -> BigInteger.ZERO)); @@ -75,18 +83,12 @@ public static TransactionArguments processArguments(CallArguments argsParam, Tra return argsRet; } - private static byte[] stringHexToByteArray(String value) { - - byte[] ret = Optional.ofNullable(value).map(HexUtils::stringHexToByteArray).orElse(null); - - return ret; - } + private static byte[] stringHexToByteArray(String value) { + return Optional.ofNullable(value).map(HexUtils::stringHexToByteArray).orElse(null); + } private static BigInteger strHexOrStrNumberToBigInteger(String value, Supplier getDefaultValue) { - - BigInteger ret = Optional.ofNullable(value).map(HexUtils::strHexOrStrNumberToBigInteger).orElseGet(getDefaultValue); - - return ret; + return Optional.ofNullable(value).map(HexUtils::strHexOrStrNumberToBigInteger).orElseGet(getDefaultValue); } private static byte hexToChainId(String hex) { From 13c4ce2ab466ad4447bd32221067766f4c6097be Mon Sep 17 00:00:00 2001 From: Angel Soto <112646151+asoto-iov@users.noreply.github.com> Date: Wed, 22 Jan 2025 21:19:57 +0100 Subject: [PATCH 2/2] minor style fix Co-authored-by: Vovchyk --- .../main/java/org/ethereum/util/TransactionArgumentsUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rskj-core/src/main/java/org/ethereum/util/TransactionArgumentsUtil.java b/rskj-core/src/main/java/org/ethereum/util/TransactionArgumentsUtil.java index ac1d21dbce9..f09e9584c9a 100644 --- a/rskj-core/src/main/java/org/ethereum/util/TransactionArgumentsUtil.java +++ b/rskj-core/src/main/java/org/ethereum/util/TransactionArgumentsUtil.java @@ -56,7 +56,7 @@ public static TransactionArguments processArguments(CallArguments argsParam, Tra argsRet.setNonce(Optional.ofNullable(argsParam.getNonce()) .map(HexUtils::strHexOrStrNumberToBigInteger) .orElse(null)); - }else { + } else { argsRet.setNonce(strHexOrStrNumberToBigInteger(argsParam.getNonce(), () -> transactionPool.getPendingState().getNonce(senderAccount.getAddress()))); }