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

Extract unnecessary logic out of the synchronized #2938

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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");
}

Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@

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;
import org.ethereum.rpc.CallArguments;
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 {

Expand All @@ -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();
Expand All @@ -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));

Expand All @@ -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<BigInteger> 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) {
Expand Down
Loading