diff --git a/rskj-core/src/main/java/co/rsk/rpc/Web3EthModule.java b/rskj-core/src/main/java/co/rsk/rpc/Web3EthModule.java index 05374f348a4..12599b36203 100644 --- a/rskj-core/src/main/java/co/rsk/rpc/Web3EthModule.java +++ b/rskj-core/src/main/java/co/rsk/rpc/Web3EthModule.java @@ -19,33 +19,38 @@ package co.rsk.rpc; import co.rsk.rpc.modules.eth.EthModule; -import org.ethereum.rpc.CallArguments; import org.ethereum.rpc.dto.BlockResultDTO; import org.ethereum.rpc.dto.CompilationResultDTO; import org.ethereum.rpc.dto.TransactionReceiptDTO; import org.ethereum.rpc.dto.TransactionResultDTO; import org.ethereum.rpc.parameters.BlockHashParam; import org.ethereum.rpc.parameters.FilterRequestParam; +import org.ethereum.rpc.parameters.BlockIdentifierParam; +import org.ethereum.rpc.parameters.BlockRefParam; +import org.ethereum.rpc.parameters.CallArgumentsParam; +import org.ethereum.rpc.parameters.HexAddressParam; +import org.ethereum.rpc.parameters.HexDataParam; import org.ethereum.rpc.parameters.HexIndexParam; import org.ethereum.rpc.parameters.TxHashParam; import java.math.BigInteger; import java.util.Map; +@SuppressWarnings({"java:S100", "java:S112"}) public interface Web3EthModule { default String[] eth_accounts() { return getEthModule().accounts(); } - default String eth_sign(String addr, String data) { + default String eth_sign(HexAddressParam addr, HexDataParam data) { return getEthModule().sign(addr, data); } - default String eth_call(CallArguments args, String bnOrId) { + default String eth_call(CallArgumentsParam args, BlockIdentifierParam bnOrId) { return getEthModule().call(args, bnOrId); } - default String eth_estimateGas(CallArguments args) { + default String eth_estimateGas(CallArgumentsParam args) { return getEthModule().estimateGas(args); } @@ -73,21 +78,17 @@ default String eth_chainId() { String eth_blockNumber(); - String eth_call(CallArguments args, Map blockRef) throws Exception; // NOSONAR + String eth_call(CallArgumentsParam args, Map blockRef) throws Exception; // NOSONAR - String eth_getBalance(String address, String block) throws Exception; + String eth_getBalance(HexAddressParam address, BlockRefParam blockRefParam) throws Exception; - String eth_getBalance(String address) throws Exception; - - String eth_getBalance(String address, Map blockRef) throws Exception; // NOSONAR + String eth_getBalance(HexAddressParam address) throws Exception; String eth_getStorageAt(String address, String storageIdx, Map blockRef) throws Exception; // NOSONAR String eth_getStorageAt(String address, String storageIdx, String blockId) throws Exception; - String eth_getTransactionCount(String address, Map blockRef) throws Exception; // NOSONAR - - String eth_getTransactionCount(String address, String blockId) throws Exception ; + String eth_getTransactionCount(HexAddressParam address, BlockRefParam blockRefParam) throws Exception; String eth_getBlockTransactionCountByHash(BlockHashParam blockHash)throws Exception; @@ -103,11 +104,11 @@ default String eth_getCode(String address, String blockId) { String eth_getCode(String address, Map blockRef) throws Exception; // NOSONAR - default String eth_sendRawTransaction(String rawData) { + default String eth_sendRawTransaction(HexDataParam rawData) { return getEthModule().sendRawTransaction(rawData); } - default String eth_sendTransaction(CallArguments args) { + default String eth_sendTransaction(CallArgumentsParam args) { return getEthModule().sendTransaction(args); } @@ -121,7 +122,7 @@ default String eth_sendTransaction(CallArguments args) { TransactionResultDTO eth_getTransactionByBlockNumberAndIndex(String bnOrId, String index) throws Exception; - TransactionReceiptDTO eth_getTransactionReceipt(String transactionHash) throws Exception; + TransactionReceiptDTO eth_getTransactionReceipt(TxHashParam transactionHash) throws Exception; BlockResultDTO eth_getUncleByBlockHashAndIndex(BlockHashParam blockHash, HexIndexParam uncleIdx) throws Exception; diff --git a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java index 4b58b1540a6..92ff0febc7a 100644 --- a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java +++ b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java @@ -38,6 +38,10 @@ import org.ethereum.rpc.CallArguments; import org.ethereum.rpc.converters.CallArgumentsToByteArray; import org.ethereum.rpc.exception.RskJsonRpcRequestException; +import org.ethereum.rpc.parameters.BlockIdentifierParam; +import org.ethereum.rpc.parameters.CallArgumentsParam; +import org.ethereum.rpc.parameters.HexAddressParam; +import org.ethereum.rpc.parameters.HexDataParam; import org.ethereum.vm.GasCost; import org.ethereum.vm.PrecompiledContracts; import org.ethereum.vm.program.ProgramResult; @@ -120,10 +124,11 @@ public Map bridgeState() throws IOException, BlockStoreException return state.stateToMap(); } - public String call(CallArguments args, String bnOrId) { + public String call(CallArgumentsParam argsParam, BlockIdentifierParam bnOrId) { String hReturn = null; + CallArguments args = argsParam.toCallArguments(); try { - ExecutionBlockRetriever.Result result = executionBlockRetriever.retrieveExecutionBlock(bnOrId); + ExecutionBlockRetriever.Result result = executionBlockRetriever.retrieveExecutionBlock(bnOrId.getIdentifier()); Block block = result.getBlock(); Trie finalState = result.getFinalState(); ProgramResult res; @@ -150,11 +155,11 @@ public String call(CallArguments args, String bnOrId) { } } - public String estimateGas(CallArguments args) { + public String estimateGas(CallArgumentsParam args) { String estimation = null; Block bestBlock = blockchain.getBestBlock(); try { - CallArgumentsToByteArray hexArgs = new CallArgumentsToByteArray(args); + CallArgumentsToByteArray hexArgs = new CallArgumentsToByteArray(args.toCallArguments()); TransactionExecutor executor = reversibleTransactionExecutor.estimateGas( bestBlock, @@ -194,17 +199,17 @@ protected String internalEstimateGas(ProgramResult reversibleExecutionResult) { } @Override - public String sendTransaction(CallArguments args) { + public String sendTransaction(CallArgumentsParam args) { return ethModuleTransaction.sendTransaction(args); } @Override - public String sendRawTransaction(String rawData) { + public String sendRawTransaction(HexDataParam rawData) { return ethModuleTransaction.sendRawTransaction(rawData); } @Override - public String sign(String addr, String data) { + public String sign(HexAddressParam addr, HexDataParam data) { return ethModuleWallet.sign(addr, data); } diff --git a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransaction.java b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransaction.java index f3c1ea1300b..d9357b05c19 100644 --- a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransaction.java +++ b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransaction.java @@ -18,10 +18,11 @@ package co.rsk.rpc.modules.eth; -import org.ethereum.rpc.CallArguments; +import org.ethereum.rpc.parameters.CallArgumentsParam; +import org.ethereum.rpc.parameters.HexDataParam; public interface EthModuleTransaction { - String sendTransaction(CallArguments args); + String sendTransaction(CallArgumentsParam args); - String sendRawTransaction(String rawData); + String sendRawTransaction(HexDataParam rawData); } 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 6cc64af4c22..f2b7a125a86 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 @@ -30,6 +30,8 @@ import org.ethereum.core.TransactionPoolAddResult; import org.ethereum.rpc.CallArguments; import org.ethereum.rpc.exception.RskJsonRpcRequestException; +import org.ethereum.rpc.parameters.CallArgumentsParam; +import org.ethereum.rpc.parameters.HexDataParam; import org.ethereum.util.TransactionArgumentsUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,7 +39,6 @@ import co.rsk.core.RskAddress; import co.rsk.core.Wallet; import co.rsk.net.TransactionGateway; -import co.rsk.util.HexUtils; public class EthModuleTransactionBase implements EthModuleTransaction { @@ -56,7 +57,12 @@ public EthModuleTransactionBase(Constants constants, Wallet wallet, TransactionP } @Override - public synchronized String sendTransaction(CallArguments args) { + public synchronized String sendTransaction(CallArgumentsParam argsParam) { + CallArguments args = argsParam.toCallArguments(); + + if(args.getFrom() == null) { + throw invalidParamError("from is null"); + } Account senderAccount = this.wallet.getAccount(new RskAddress(args.getFrom())); @@ -97,10 +103,10 @@ public synchronized String sendTransaction(CallArguments args) { } @Override - public String sendRawTransaction(String rawData) { + public String sendRawTransaction(HexDataParam rawData) { String s = null; try { - Transaction tx = new ImmutableTransaction(HexUtils.stringHexToByteArray(rawData)); + Transaction tx = new ImmutableTransaction(rawData.getRawDataBytes()); if (null == tx.getGasLimit() || null == tx.getGasPrice() || null == tx.getValue()) { throw invalidParamError("Missing parameter, gasPrice, gas or value"); diff --git a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionDisabled.java b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionDisabled.java index 0f1bee0cad6..6b0313f6033 100644 --- a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionDisabled.java +++ b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionDisabled.java @@ -21,7 +21,7 @@ import co.rsk.net.TransactionGateway; import org.ethereum.config.Constants; import org.ethereum.core.TransactionPool; -import org.ethereum.rpc.CallArguments; +import org.ethereum.rpc.parameters.CallArgumentsParam; import static org.ethereum.rpc.exception.RskJsonRpcRequestException.invalidParamError; @@ -36,7 +36,7 @@ public EthModuleTransactionDisabled(Constants constants, TransactionPool transac } @Override - public String sendTransaction(CallArguments args) { // lgtm [java/non-sync-override] + public synchronized String sendTransaction(CallArgumentsParam args) { // lgtm [java/non-sync-override] LOGGER.debug("eth_sendTransaction({}): {}", args, null); throw invalidParamError("Local wallet is disabled in this node"); } diff --git a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionInstant.java b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionInstant.java index ee879308874..f5b08ae5dfa 100644 --- a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionInstant.java +++ b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleTransactionInstant.java @@ -27,8 +27,9 @@ import org.ethereum.core.Blockchain; import org.ethereum.core.TransactionPool; import org.ethereum.db.TransactionInfo; -import org.ethereum.rpc.CallArguments; import org.ethereum.rpc.exception.RskJsonRpcRequestException; +import org.ethereum.rpc.parameters.CallArgumentsParam; +import org.ethereum.rpc.parameters.HexDataParam; import org.ethereum.vm.program.ProgramResult; import co.rsk.core.Wallet; @@ -64,7 +65,7 @@ public EthModuleTransactionInstant( } @Override - public synchronized String sendTransaction(CallArguments args) { + public synchronized String sendTransaction(CallArgumentsParam args) { try { this.blockExecutor.setRegisterProgramResults(true); @@ -80,7 +81,7 @@ public synchronized String sendTransaction(CallArguments args) { } @Override - public String sendRawTransaction(String rawData) { + public String sendRawTransaction(HexDataParam rawData) { try { this.blockExecutor.setRegisterProgramResults(true); diff --git a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWallet.java b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWallet.java index aaf90d7ff35..eca121e7341 100644 --- a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWallet.java +++ b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWallet.java @@ -18,9 +18,12 @@ package co.rsk.rpc.modules.eth; +import org.ethereum.rpc.parameters.HexAddressParam; +import org.ethereum.rpc.parameters.HexDataParam; + public interface EthModuleWallet { String[] accounts(); - String sign(String addr, String data); + String sign(HexAddressParam addr, HexDataParam data); } \ No newline at end of file diff --git a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWalletDisabled.java b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWalletDisabled.java index 4cf03242cd2..1d29f3899be 100644 --- a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWalletDisabled.java +++ b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWalletDisabled.java @@ -18,6 +18,8 @@ package co.rsk.rpc.modules.eth; +import org.ethereum.rpc.parameters.HexAddressParam; +import org.ethereum.rpc.parameters.HexDataParam; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,7 +39,7 @@ public String[] accounts() { } @Override - public String sign(String addr, String data) { + public String sign(HexAddressParam addr, HexDataParam data) { LOGGER.debug("eth_sign({}, {}): {}", addr, data, null); throw invalidParamError("Local wallet is disabled in this node"); } diff --git a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWalletEnabled.java b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWalletEnabled.java index 2341857377c..3d254713132 100644 --- a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWalletEnabled.java +++ b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModuleWalletEnabled.java @@ -28,11 +28,12 @@ import org.ethereum.crypto.ECKey; import org.ethereum.crypto.HashUtil; import org.ethereum.crypto.signature.ECDSASignature; +import org.ethereum.rpc.parameters.HexAddressParam; +import org.ethereum.rpc.parameters.HexDataParam; import org.ethereum.util.ByteUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import co.rsk.core.RskAddress; import co.rsk.core.Wallet; import co.rsk.util.HexUtils; @@ -47,15 +48,17 @@ public EthModuleWalletEnabled(Wallet wallet) { } @Override - public String sign(String addr, String data) { + public String sign(HexAddressParam addr, HexDataParam data) { String s = null; try { - Account account = this.wallet.getAccount(new RskAddress(addr)); + Account account = this.wallet.getAccount(addr.getAddress()); if (account == null) { throw invalidParamError("Account not found"); } - return s = this.sign(data, account.getEcKey()); + s = this.sign(data.getRawDataBytes(), account.getEcKey()); + + return s; } finally { LOGGER.debug("eth_sign({}, {}): {}", addr, data, s); } @@ -71,8 +74,7 @@ public String[] accounts() { } } - private String sign(String data, ECKey ecKey) { - byte[] dataHash = HexUtils.stringHexToByteArray(data); + private String sign(byte[] dataHash, ECKey ecKey) { // 0x19 = 25, length should be an ascii decimals, message - original String prefix = (char) 25 + "Ethereum Signed Message:\n" + dataHash.length; diff --git a/rskj-core/src/main/java/org/ethereum/rpc/Web3Impl.java b/rskj-core/src/main/java/org/ethereum/rpc/Web3Impl.java index bd5fbd4a9e1..f6a5e6f8e79 100644 --- a/rskj-core/src/main/java/org/ethereum/rpc/Web3Impl.java +++ b/rskj-core/src/main/java/org/ethereum/rpc/Web3Impl.java @@ -61,6 +61,10 @@ import org.ethereum.rpc.dto.TransactionResultDTO; import org.ethereum.rpc.parameters.BlockHashParam; import org.ethereum.rpc.parameters.FilterRequestParam; +import org.ethereum.rpc.parameters.BlockIdentifierParam; +import org.ethereum.rpc.parameters.BlockRefParam; +import org.ethereum.rpc.parameters.CallArgumentsParam; +import org.ethereum.rpc.parameters.HexAddressParam; import org.ethereum.rpc.parameters.HexIndexParam; import org.ethereum.rpc.parameters.TxHashParam; import org.ethereum.util.BuildInfo; @@ -81,6 +85,7 @@ import static java.lang.Math.max; import static org.ethereum.rpc.exception.RskJsonRpcRequestException.*; +@SuppressWarnings("java:S100") public class Web3Impl implements Web3 { private static final Logger logger = LoggerFactory.getLogger("web3"); @@ -400,8 +405,8 @@ public String eth_blockNumber() { } @Override - public String eth_call(CallArguments args, Map inputs) { - return invokeByBlockRef(inputs, blockNumber -> this.eth_call(args, blockNumber)); + public String eth_call(CallArgumentsParam args, Map inputs) { + return invokeByBlockRef(inputs, blockNumber -> this.eth_call(args, new BlockIdentifierParam(blockNumber))); } @Override @@ -410,7 +415,15 @@ public String eth_getCode(String address, Map inputs) { } @Override - public String eth_getBalance(String address, String block) { + public String eth_getBalance(HexAddressParam address, BlockRefParam blockRefParam) { + if(blockRefParam.getIdentifier() != null) { + return this.eth_getBalance(address, blockRefParam.getIdentifier()); + } else { + return this.eth_getBalance(address, blockRefParam.getInputs()); + } + } + + private String eth_getBalance(HexAddressParam address, String block) { /* HEX String - an integer block number * String "earliest" for the earliest/genesis block * String "latest" - for the latest mined block @@ -419,14 +432,14 @@ public String eth_getBalance(String address, String block) { AccountInformationProvider accountInformationProvider = web3InformationRetriever.getInformationProvider(block); - RskAddress addr = new RskAddress(address); + RskAddress addr = address.getAddress(); Coin balance = accountInformationProvider.getBalance(addr); return toQuantityJsonHex(balance.asBigInteger()); } - @Override - public String eth_getBalance(String address, Map inputs) { + + private String eth_getBalance(HexAddressParam address, Map inputs) { return invokeByBlockRef(inputs, blockNumber -> this.eth_getBalance(address, blockNumber)); } @@ -440,7 +453,7 @@ private boolean isInMainChain(Block block) { } @Override - public String eth_getBalance(String address) { + public String eth_getBalance(HexAddressParam address) { return eth_getBalance(address, "latest"); } @@ -477,7 +490,15 @@ public String eth_getStorageAt(String address, String storageIdx, String blockId } @Override - public String eth_getTransactionCount(String address, Map inputs) { + public String eth_getTransactionCount(HexAddressParam address, BlockRefParam blockRefParam) { + if(blockRefParam.getIdentifier() != null) { + return this.eth_getTransactionCount(address, blockRefParam.getIdentifier()); + } else { + return this.eth_getTransactionCount(address, blockRefParam.getInputs()); + } + } + + private String eth_getTransactionCount(HexAddressParam address, Map inputs) { return invokeByBlockRef(inputs, blockNumber -> this.eth_getTransactionCount(address, blockNumber)); } @@ -511,11 +532,10 @@ private String toInvokeByBlockHash(String blockHash, boolean requireCanonical, F return toInvokeByBlockNumber.apply(toQuantityJsonHex(block.getNumber())); } - @Override - public String eth_getTransactionCount(String address, String blockId) { + private String eth_getTransactionCount(HexAddressParam address, String blockId) { String s = null; try { - RskAddress addr = new RskAddress(address); + RskAddress addr = address.getAddress(); AccountInformationProvider accountInformationProvider = web3InformationRetriever .getInformationProvider(blockId); BigInteger nonce = accountInformationProvider.getNonce(addr); @@ -777,10 +797,10 @@ public TransactionResultDTO eth_getTransactionByBlockNumberAndIndex(String bnOrI } @Override - public TransactionReceiptDTO eth_getTransactionReceipt(String transactionHash) { + public TransactionReceiptDTO eth_getTransactionReceipt(TxHashParam transactionHash) { logger.trace("eth_getTransactionReceipt({})", transactionHash); - byte[] hash = stringHexToByteArray(transactionHash); + byte[] hash = stringHexToByteArray(transactionHash.getHash().toHexString()); TransactionInfo txInfo = receiptStore.getInMainChain(hash, blockStore).orElse(null); if (txInfo == null) { diff --git a/rskj-core/src/test/java/co/rsk/mine/TransactionModuleTest.java b/rskj-core/src/test/java/co/rsk/mine/TransactionModuleTest.java index a162c59184d..94cc516fdb3 100644 --- a/rskj-core/src/test/java/co/rsk/mine/TransactionModuleTest.java +++ b/rskj-core/src/test/java/co/rsk/mine/TransactionModuleTest.java @@ -67,9 +67,11 @@ import org.ethereum.rpc.Simples.SimpleConfigCapabilities; import org.ethereum.rpc.Web3Impl; import org.ethereum.rpc.Web3Mocks; +import org.ethereum.rpc.parameters.HexDataParam; import org.ethereum.sync.SyncPool; import org.ethereum.util.BuildInfo; import org.ethereum.util.ByteUtil; +import org.ethereum.util.TransactionFactoryHelper; import org.ethereum.vm.GasCost; import org.ethereum.vm.PrecompiledContracts; import org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl; @@ -306,7 +308,7 @@ public int checkEstimateGas(int method, long expectedValue, long gasLimit, Assertions.assertNotEquals(expectedValue, gasLimit); CallArguments args = getContractCallTransactionParameters(method, gasLimit, srcAddr, contractAddress, repository); - String gas = web3.eth_estimateGas(args); + String gas = web3.eth_estimateGas(TransactionFactoryHelper.toCallArgumentsParam(args)); byte[] gasReturnedBytes = Hex.decode(gas.substring("0x".length())); BigInteger gasReturned = BigIntegers.fromUnsignedByteArray(gasReturnedBytes); int gasReturnedInt = gasReturned.intValueExact(); @@ -332,7 +334,7 @@ private String sendRawTransaction(Web3Impl web3) { String rawData = ByteUtil.toHexString(tx.getEncoded()); - return web3.eth_sendRawTransaction(rawData); + return web3.eth_sendRawTransaction(new HexDataParam(rawData)); } private Transaction getTransactionFromBlockWhichWasSend(BlockChainImpl blockchain, String tx) { @@ -350,13 +352,13 @@ private Transaction getTransactionFromBlockWhichWasSend(BlockChainImpl blockchai private String sendContractCreationTransaction(RskAddress srcaddr, Web3Impl web3, RepositorySnapshot repository) { CallArguments args = getContractCreationTransactionParameters(srcaddr, web3, repository); - return web3.eth_sendTransaction(args); + return web3.eth_sendTransaction(TransactionFactoryHelper.toCallArgumentsParam(args)); } private String sendTransaction(Web3Impl web3, RepositorySnapshot repository) { CallArguments args = getTransactionParameters(web3, repository); - return web3.eth_sendTransaction(args); + return web3.eth_sendTransaction(TransactionFactoryHelper.toCallArgumentsParam(args)); } //////////////////////////////////////////////// diff --git a/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleDSLTest.java b/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleDSLTest.java index 79fea1f08bb..cad63aba4f2 100644 --- a/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleDSLTest.java +++ b/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleDSLTest.java @@ -26,7 +26,10 @@ import org.ethereum.core.TransactionReceipt; import org.ethereum.rpc.CallArguments; import org.ethereum.rpc.exception.RskJsonRpcRequestException; +import org.ethereum.rpc.parameters.BlockIdentifierParam; +import org.ethereum.rpc.parameters.CallArgumentsParam; import org.ethereum.util.EthModuleTestUtils; +import org.ethereum.util.TransactionFactoryHelper; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Assertions; @@ -62,15 +65,17 @@ void testCall_getRevertReason() throws FileNotFoundException, DslProcessorExcept args.setValue("0"); args.setNonce("1"); args.setGas("10000000"); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + BlockIdentifierParam blockIdentifierParam = new BlockIdentifierParam("0x2"); try { - eth.call(args, "0x2"); + eth.call(callArgumentsParam, blockIdentifierParam); fail(); } catch (RskJsonRpcRequestException e) { MatcherAssert.assertThat(e.getMessage(), Matchers.containsString("Negative value.")); } args.setData("0xd96a094a0000000000000000000000000000000000000000000000000000000000000001"); // call to contract with param value = 1 - final String call = eth.call(args, "0x2"); + final String call = eth.call(TransactionFactoryHelper.toCallArgumentsParam(args), new BlockIdentifierParam("0x2")); assertEquals("0x", call); } } diff --git a/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleGasEstimationDSLTest.java b/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleGasEstimationDSLTest.java index 123f50d943b..dd98efff455 100644 --- a/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleGasEstimationDSLTest.java +++ b/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleGasEstimationDSLTest.java @@ -32,6 +32,7 @@ import org.ethereum.rpc.CallArguments; import org.ethereum.util.ByteUtil; import org.ethereum.util.EthModuleTestUtils; +import org.ethereum.util.TransactionFactoryHelper; import org.ethereum.vm.GasCost; import org.ethereum.vm.LogInfo; import org.ethereum.vm.program.InternalTransaction; @@ -288,7 +289,7 @@ void estimateGas_gasCap() throws FileNotFoundException, DslProcessorException { callArguments.setGas(HexUtils.toQuantityJsonHex(gasEstimationCap + 1_000_000_000)); // exceeding the gas cap callArguments.setData("0x31fe52e8"); // call outOfGas() - String estimatedGas = eth.estimateGas(callArguments); + String estimatedGas = eth.estimateGas(TransactionFactoryHelper.toCallArgumentsParam(callArguments)); assertEquals("0x67c280", estimatedGas); assertEquals(gasEstimationCap, Long.decode(estimatedGas).longValue()); @@ -785,7 +786,7 @@ public boolean runWithArgumentsAndBlock(EthModuleTestUtils.EthModuleGasEstimatio } private long estimateGas(EthModuleTestUtils.EthModuleGasEstimation eth, CallArguments args) { - return Long.parseLong(eth.estimateGas(args).substring("0x".length()), 16); + return Long.parseLong(eth.estimateGas(TransactionFactoryHelper.toCallArgumentsParam(args)).substring("0x".length()), 16); } // todo this is duplicated code, should be extracted into a test util diff --git a/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleTest.java b/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleTest.java index 534eb97a425..12a4c9a16d3 100644 --- a/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleTest.java +++ b/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleTest.java @@ -36,6 +36,9 @@ import org.ethereum.datasource.HashMapDB; import org.ethereum.rpc.CallArguments; import org.ethereum.rpc.exception.RskJsonRpcRequestException; +import org.ethereum.rpc.parameters.BlockIdentifierParam; +import org.ethereum.rpc.parameters.CallArgumentsParam; +import org.ethereum.rpc.parameters.HexDataParam; import org.ethereum.util.ByteUtil; import org.ethereum.util.TransactionFactoryHelper; import org.ethereum.vm.program.ProgramResult; @@ -44,7 +47,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -59,10 +61,6 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; class EthModuleTest { @@ -106,7 +104,7 @@ void callSmokeTest() { config.getCallGasCap()); String expectedResult = HexUtils.toUnformattedJsonHex(hReturn); - String actualResult = eth.call(args, "latest"); + String actualResult = eth.call(TransactionFactoryHelper.toCallArgumentsParam(args), new BlockIdentifierParam("latest")); assertEquals(expectedResult, actualResult); } @@ -146,7 +144,7 @@ void callWithoutReturn() { config.getCallGasCap()); String expectedResult = HexUtils.toUnformattedJsonHex(hReturn); - String actualResult = eth.call(args, "latest"); + String actualResult = eth.call(TransactionFactoryHelper.toCallArgumentsParam(args), new BlockIdentifierParam("latest")); assertEquals(expectedResult, actualResult); } @@ -191,7 +189,7 @@ void test_revertedTransaction() { config.getCallGasCap()); try { - eth.call(args, "latest"); + eth.call(TransactionFactoryHelper.toCallArgumentsParam(args), new BlockIdentifierParam("latest")); } catch (RskJsonRpcRequestException e) { assertThat(e.getMessage(), Matchers.containsString("deposit too big")); } @@ -222,7 +220,7 @@ void sendTransactionWithGasLimitTest() { EthModuleTransactionBase ethModuleTransaction = new EthModuleTransactionBase(constants, wallet, transactionPool, transactionGateway); // Hash of the actual transaction builded inside the sendTransaction - String txResult = ethModuleTransaction.sendTransaction(args); + String txResult = ethModuleTransaction.sendTransaction(TransactionFactoryHelper.toCallArgumentsParam(args)); assertEquals(txExpectedResult, txResult); } @@ -250,7 +248,8 @@ void sendTransactionThrowsErrorOnChainIdValidationTest() { EthModuleTransactionBase ethModuleTransaction = new EthModuleTransactionBase(constants, wallet, transactionPool, transactionGateway); - Assertions.assertThrows(RskJsonRpcRequestException.class, () -> ethModuleTransaction.sendTransaction(args)); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + Assertions.assertThrows(RskJsonRpcRequestException.class, () -> ethModuleTransaction.sendTransaction(callArgumentsParam)); } @Test @@ -279,7 +278,8 @@ void sendRawTransactionThrowsErrorOnChainIdValidationTest() { EthModuleTransactionBase ethModuleTransaction = new EthModuleTransactionBase(constants, wallet, transactionPool, transactionGateway); String rawData = ByteUtil.toHexString(tx.getEncoded()); - Assertions.assertThrows(RskJsonRpcRequestException.class, () -> ethModuleTransaction.sendRawTransaction(rawData)); + HexDataParam hexDataParam = new HexDataParam(rawData); + Assertions.assertThrows(RskJsonRpcRequestException.class, () -> ethModuleTransaction.sendRawTransaction(hexDataParam)); } @Test @@ -296,12 +296,12 @@ void sendTransaction_invalidSenderAccount_throwsRskJsonRpcRequestException() { EthModuleTransactionBase ethModuleTransaction = new EthModuleTransactionBase(constants, wallet, transactionPoolMock, transactionGatewayMock); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(argsMock); // Then try { - ethModuleTransaction.sendTransaction(argsMock); + ethModuleTransaction.sendTransaction(callArgumentsParam); fail("RskJsonRpcRequestException should be thrown"); } catch (RskJsonRpcRequestException ex) { - verify(argsMock, times(2)).getFrom(); assertEquals("Could not find account for address: " + addressFrom.toJsonString(), ex.getMessage()); } } @@ -394,7 +394,10 @@ void whenExecuteCallWithDataParameter_callExecutorWithData() { config.getGasEstimationCap(), config.getCallGasCap()); - eth.call(args, "latest"); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + BlockIdentifierParam blockIdentifierParam = new BlockIdentifierParam("latest"); + + eth.call(callArgumentsParam, blockIdentifierParam); ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(byte[].class); verify(executor, times(1)) @@ -437,7 +440,10 @@ void whenExecuteCallWithInputParameter_callExecutorWithInput() { config.getGasEstimationCap(), config.getCallGasCap()); - eth.call(args, "latest"); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + BlockIdentifierParam blockIdentifierParam = new BlockIdentifierParam("latest"); + + eth.call(callArgumentsParam, blockIdentifierParam); ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(byte[].class); verify(executor, times(1)) @@ -482,7 +488,10 @@ void whenExecuteCallWithInputAndDataParameters_callExecutorWithInput() { config.getCallGasCap()); - eth.call(args, "latest"); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + BlockIdentifierParam blockIdentifierParam = new BlockIdentifierParam("latest"); + + eth.call(callArgumentsParam, blockIdentifierParam); ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(byte[].class); verify(executor, times(1)) @@ -524,7 +533,9 @@ void whenExecuteEstimateGasWithDataParameter_callExecutorWithData() { config.getGasEstimationCap(), config.getCallGasCap()); - eth.estimateGas(args); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + + eth.estimateGas(callArgumentsParam); ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(byte[].class); verify(reversibleTransactionExecutor, times(1)) @@ -566,7 +577,9 @@ void whenExecuteEstimateGasWithInputParameter_callExecutorWithInput() { config.getGasEstimationCap(), config.getCallGasCap()); - eth.estimateGas(args); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + + eth.estimateGas(callArgumentsParam); ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(byte[].class); verify(reversibleTransactionExecutor, times(1)) @@ -609,7 +622,9 @@ void whenExecuteEstimateGasWithInputAndDataParameters_callExecutorWithInput() { config.getGasEstimationCap(), config.getCallGasCap()); - eth.estimateGas(args); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + + eth.estimateGas(callArgumentsParam); ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(byte[].class); verify(reversibleTransactionExecutor, times(1)) @@ -641,7 +656,9 @@ void whenExecuteSendTransactionWithDataParameter_callExecutorWithData() { EthModuleTransactionBase ethModuleTransaction = new EthModuleTransactionBase(constants, wallet, transactionPool, transactionGateway); - ethModuleTransaction.sendTransaction(args); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + + ethModuleTransaction.sendTransaction(callArgumentsParam); ArgumentCaptor transactionCaptor = ArgumentCaptor.forClass(Transaction.class); verify(transactionGateway, times(1)) @@ -673,7 +690,9 @@ void whenExecuteSendTransactionWithInputParameter_callExecutorWithInput() { EthModuleTransactionBase ethModuleTransaction = new EthModuleTransactionBase(constants, wallet, transactionPool, transactionGateway); - ethModuleTransaction.sendTransaction(args); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + + ethModuleTransaction.sendTransaction(callArgumentsParam); ArgumentCaptor transactionCaptor = ArgumentCaptor.forClass(Transaction.class); verify(transactionGateway, times(1)) @@ -706,7 +725,9 @@ void whenExecuteSendTransactionWithInputAndDataParameters_callExecutorWithInput( EthModuleTransactionBase ethModuleTransaction = new EthModuleTransactionBase(constants, wallet, transactionPool, transactionGateway); - ethModuleTransaction.sendTransaction(args); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + + ethModuleTransaction.sendTransaction(callArgumentsParam); ArgumentCaptor transactionCaptor = ArgumentCaptor.forClass(Transaction.class); verify(transactionGateway, times(1)) diff --git a/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplLogsTest.java b/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplLogsTest.java index 6544f79af21..adb865d4e77 100644 --- a/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplLogsTest.java +++ b/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplLogsTest.java @@ -18,6 +18,29 @@ package org.ethereum.rpc; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; + +import java.math.BigInteger; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +import org.ethereum.core.*; +import org.ethereum.datasource.HashMapDB; +import org.ethereum.db.BlockStore; +import org.ethereum.db.ReceiptStore; +import org.ethereum.facade.Ethereum; +import org.ethereum.rpc.Simples.SimpleConfigCapabilities; +import org.ethereum.rpc.dto.TransactionReceiptDTO; +import org.ethereum.rpc.exception.RskJsonRpcRequestException; +import org.ethereum.rpc.parameters.TxHashParam; +import org.ethereum.util.ByteUtil; +import org.ethereum.util.RskTestFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + import co.rsk.config.TestSystemProperties; import co.rsk.core.Coin; import co.rsk.core.Wallet; @@ -362,7 +385,7 @@ void getLogsFromBlockchainWithEventInContractCreation() throws Exception { assertEquals(1, logs.length); String txhash = ((LogFilterElement) logs[0]).transactionHash; - TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(txhash); + TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(new TxHashParam(txhash)); assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[0]).address); } @@ -380,7 +403,7 @@ void getLogsTwiceFromBlockchainWithEventInContractCreation() throws Exception { assertEquals(1, logs.length); String txhash = ((LogFilterElement) logs[0]).transactionHash; - TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(txhash); + TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(new TxHashParam(txhash)); assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[0]).address); } @@ -397,7 +420,7 @@ void getLogsFromBlockchainWithInvokeContract() throws Exception { assertEquals(2, logs.length); String txhash = ((LogFilterElement) logs[0]).transactionHash; - TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(txhash); + TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(new TxHashParam(txhash)); assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[0]).address); assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[1]).address); @@ -415,7 +438,7 @@ void getLogsKeepsCorrectOrderForReverseSearch() throws Exception { assertEquals(3, logs.length); String txHash1 = "0x" + transactions.get(0).getHash().toHexString(); - TransactionReceiptDTO txReceipt1 = web3.eth_getTransactionReceipt(txHash1); + TransactionReceiptDTO txReceipt1 = web3.eth_getTransactionReceipt(new TxHashParam(txHash1)); String contractAddress = txReceipt1.getContractAddress(); LogFilterElement logs1 = (LogFilterElement) logs[0]; assertEquals(contractAddress, logs1.address); @@ -426,7 +449,7 @@ void getLogsKeepsCorrectOrderForReverseSearch() throws Exception { assertArrayEquals(receipt1Logs.topics, logs1.topics); String txHash2 = "0x" + transactions.get(1).getHash().toHexString(); - TransactionReceiptDTO txReceipt2 = web3.eth_getTransactionReceipt(txHash2); + TransactionReceiptDTO txReceipt2 = web3.eth_getTransactionReceipt(new TxHashParam(txHash2)); LogFilterElement logs2 = (LogFilterElement) logs[1]; assertEquals(contractAddress, logs2.address); assertEquals(txHash2, logs2.transactionHash); @@ -436,7 +459,7 @@ void getLogsKeepsCorrectOrderForReverseSearch() throws Exception { assertArrayEquals(receipt2Logs.topics, logs2.topics); String txHash3 = "0x" + transactions.get(2).getHash().toHexString(); - TransactionReceiptDTO txReceipt3 = web3.eth_getTransactionReceipt(txHash3); + TransactionReceiptDTO txReceipt3 = web3.eth_getTransactionReceipt(new TxHashParam(txHash3)); LogFilterElement logs3 = (LogFilterElement) logs[2]; assertEquals(contractAddress, logs3.address); assertEquals(txHash3, logs3.transactionHash); @@ -465,7 +488,7 @@ void getLogsTwiceFromBlockchainWithInvokeContract() throws Exception { assertEquals(2, logs.length); String txhash = ((LogFilterElement) logs[0]).transactionHash; - TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(txhash); + TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(new TxHashParam(txhash)); assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[0]).address); assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[1]).address); @@ -850,7 +873,7 @@ void getLogsFromBlockchainWithEventInContractCreationReturnsAsExpectedWithBlockH assertEquals(1, logs.length); assertEquals(blockHash, ((LogFilterElement) logs[0]).blockHash); String txhash = ((LogFilterElement) logs[0]).transactionHash; - TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(txhash); + TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(new TxHashParam(txhash)); assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[0]).address); } diff --git a/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplTest.java b/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplTest.java index 8b1ce0f081a..edcddac18d4 100644 --- a/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplTest.java +++ b/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplTest.java @@ -78,10 +78,16 @@ import org.ethereum.rpc.dto.TransactionResultDTO; import org.ethereum.rpc.exception.RskJsonRpcRequestException; import org.ethereum.rpc.parameters.BlockHashParam; +import org.ethereum.rpc.parameters.BlockIdentifierParam; +import org.ethereum.rpc.parameters.BlockRefParam; +import org.ethereum.rpc.parameters.CallArgumentsParam; +import org.ethereum.rpc.parameters.HexAddressParam; +import org.ethereum.rpc.parameters.HexDataParam; import org.ethereum.rpc.parameters.HexIndexParam; import org.ethereum.rpc.parameters.TxHashParam; import org.ethereum.util.BuildInfo; import org.ethereum.util.ByteUtil; +import org.ethereum.util.TransactionFactoryHelper; import org.ethereum.vm.PrecompiledContracts; import org.ethereum.vm.program.ProgramResult; import org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl; @@ -217,7 +223,7 @@ void getBalanceWithAccount() { Web3Impl web3 = createWeb3(world); - assertEquals(BALANCE_10K_HEX, web3.eth_getBalance(ByteUtil.toHexString(acc1.getAddress().getBytes()))); + assertEquals(BALANCE_10K_HEX, web3.eth_getBalance(new HexAddressParam(ByteUtil.toHexString(acc1.getAddress().getBytes())))); } @Test @@ -227,7 +233,9 @@ void getBalanceWithAccountAndLatestBlock() { Web3Impl web3 = createWeb3(world); - assertEquals(BALANCE_10K_HEX, web3.eth_getBalance(ByteUtil.toHexString(acc1.getAddress().getBytes()), "latest")); + assertEquals(BALANCE_10K_HEX, web3.eth_getBalance( + new HexAddressParam(ByteUtil.toHexString(acc1.getAddress().getBytes())), + new BlockRefParam(new BlockRef("latest")))); } @Test @@ -239,7 +247,7 @@ void getBalanceWithAccountAndGenesisBlock() { String accountAddress = ByteUtil.toHexString(acc1.getAddress().getBytes()); - assertEquals(BALANCE_10K_HEX, web3.eth_getBalance(accountAddress, "0x0")); + assertEquals(BALANCE_10K_HEX, web3.eth_getBalance(new HexAddressParam(accountAddress), new BlockRefParam(new BlockRef("0x0")))); } @Test @@ -252,42 +260,42 @@ void getBalanceWithAccountAndBlock() { String accountAddress = ByteUtil.toHexString(acc1.getAddress().getBytes()); - assertEquals(BALANCE_10K_HEX, web3.eth_getBalance(accountAddress, "0x1")); + assertEquals(BALANCE_10K_HEX, web3.eth_getBalance(new HexAddressParam(accountAddress), new BlockRefParam(new BlockRef("0x1")))); } @Test //[ "0x
", { "blockNumber": "0x0" } -> return balance at given address in genesis block void getBalanceWithAccountAndBlockNumber() { final ChainParams chain = chainWithAccount10kBalance(false); - assertByBlockNumber(BALANCE_10K_HEX, blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertByBlockNumber(BALANCE_10K_HEX, blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test //[ "0x
", { "invalidInput": "0x0" } -> throw RskJsonRpcRequestException void getBalanceWithAccountAndInvalidInputThrowsException() { final ChainParams chain = chainWithAccount10kBalance(false); - assertInvalidInput(blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertInvalidInput(blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test //[ "0x
", { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" } -> return balance at given address in genesis block void getBalanceWithAccountAndBlockHash() { final ChainParams chain = chainWithAccount10kBalance(false); - assertByBlockHash(BALANCE_10K_HEX, chain.block, blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertByBlockHash(BALANCE_10K_HEX, chain.block, blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test //[ "0x
", { "blockHash": "0x" } -> raise block-not-found error void getBalanceWithAccountAndNonExistentBlockHash() { final ChainParams chain = chainWithAccount10kBalance(false); - assertNonExistentBlockHash(blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertNonExistentBlockHash(blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test //[ "0x
", { "blockHash": "0x", "requireCanonical": true } -> raise block-not-found error void getBalanceWithAccountAndNonExistentBlockHashWhenCanonicalIsRequired() { final ChainParams chain = chainWithAccount10kBalance(false); - assertNonBlockHashWhenCanonical(blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertNonBlockHashWhenCanonical(blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @@ -295,14 +303,14 @@ void getBalanceWithAccountAndNonExistentBlockHashWhenCanonicalIsRequired() { //[ "0x
", { "blockHash": "0x", "requireCanonical": false } -> raise block-not-found error void getBalanceWithAccountAndNonExistentBlockHashWhenCanonicalIsNotRequired() { final ChainParams chain = chainWithAccount10kBalance(false); - assertNonBlockHashWhenIsNotCanonical(blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertNonBlockHashWhenIsNotCanonical(blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test // [ "0x
", { "blockHash": "0x", "requireCanonical": true } -> raise block-not-canonical error void getBalanceWithAccountAndNonCanonicalBlockHashWhenCanonicalIsRequired() { final ChainParams chain = chainWithAccount10kBalance(true); - assertNonCanonicalBlockHashWhenCanonical(chain.block, blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertNonCanonicalBlockHashWhenCanonical(chain.block, blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @@ -310,28 +318,28 @@ void getBalanceWithAccountAndNonCanonicalBlockHashWhenCanonicalIsRequired() { //[ "0x
", { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "requireCanonical": true } -> return balance at given address in genesis block void getBalanceWithAccountAndCanonicalBlockHashWhenCanonicalIsRequired() { final ChainParams chain = chainWithAccount10kBalance(false); - assertCanonicalBlockHashWhenCanonical(BALANCE_10K_HEX, chain.block, blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertCanonicalBlockHashWhenCanonical(BALANCE_10K_HEX, chain.block, blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test //[ "0x
", { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "requireCanonical": false } -> return balance at given address in genesis block void getBalanceWithAccountAndCanonicalBlockHashWhenCanonicalIsNotRequired() { final ChainParams chain = chainWithAccount10kBalance(false); - assertCanonicalBlockHashWhenNotCanonical(BALANCE_10K_HEX, chain.block, blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertCanonicalBlockHashWhenNotCanonical(BALANCE_10K_HEX, chain.block, blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test // [ "0x
", { "blockHash": "0x", "requireCanonical": false } -> return balance at given address in specified block void getBalanceWithAccountAndNonCanonicalBlockHashWhenCanonicalIsNotRequired() { final ChainParams chain = chainWithAccount10kBalance(true); - assertNonCanonicalBlockHashWhenNotCanonical(BALANCE_10K_HEX, chain.block, blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertNonCanonicalBlockHashWhenNotCanonical(BALANCE_10K_HEX, chain.block, blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test // [ "0x
", { "blockHash": "0x" } -> return balance at given address in specified bloc void getBalanceWithAccountAndNonCanonicalBlockHash() { final ChainParams chain = chainWithAccount10kBalance(true); - assertNonCanonicalBlockHash(BALANCE_10K_HEX, chain.block, blockRef -> chain.web3.eth_getBalance(chain.accountAddress, blockRef)); + assertNonCanonicalBlockHash(BALANCE_10K_HEX, chain.block, blockRef -> chain.web3.eth_getBalance(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test @@ -355,9 +363,9 @@ void getBalanceWithAccountAndBlockWithTransaction() { String accountAddress = ByteUtil.toHexString(acc2.getAddress().getBytes()); String balanceString = BALANCE_10K_HEX; - assertEquals("0x0", web3.eth_getBalance(accountAddress, "0x0")); - assertEquals(balanceString, web3.eth_getBalance(accountAddress, "0x1")); - assertEquals(balanceString, web3.eth_getBalance(accountAddress, "pending")); + assertEquals("0x0", web3.eth_getBalance(new HexAddressParam(accountAddress), new BlockRefParam(new BlockRef("0x0")))); + assertEquals(balanceString, web3.eth_getBalance(new HexAddressParam(accountAddress), new BlockRefParam(new BlockRef("0x1")))); + assertEquals(balanceString, web3.eth_getBalance(new HexAddressParam(accountAddress), new BlockRefParam(new BlockRef("pending")))); } @Test @@ -506,70 +514,70 @@ void getCodeAtAccountAndNonCanonicalBlockHash() { //[ {argsForCall}, { "blockNumber": "0x0" } -> return contract call respond at given args for call in genesis block void callByBlockNumber() { final ChainParams chain = createChainWithACall(false); - assertByBlockNumber(CALL_RESPOND, blockRef -> chain.web3.eth_call(chain.argsForCall, blockRef)); + assertByBlockNumber(CALL_RESPOND, blockRef -> chain.web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(chain.argsForCall), blockRef)); } @Test //[ {argsForCall}, { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" } -> return contract call respond at given address in genesis block void callByBlockHash() { final ChainParams chain = createChainWithACall(false); - assertByBlockHash(CALL_RESPOND, chain.block, blockRef -> chain.web3.eth_call(chain.argsForCall, blockRef)); + assertByBlockHash(CALL_RESPOND, chain.block, blockRef -> chain.web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(chain.argsForCall), blockRef)); } @Test //[ {argsForCall}, { "blockHash": "0x" } -> raise block-not-found error void callByNonExistentBlockHash() { final ChainParams chain = createChainWithACall(false); - assertNonExistentBlockHash(blockRef -> chain.web3.eth_call(chain.argsForCall, blockRef)); + assertNonExistentBlockHash(blockRef -> chain.web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(chain.argsForCall), blockRef)); } @Test //[ {argsForCall}, { "blockHash": "0x", "requireCanonical": true } -> raise block-not-found error void callByNonExistentBlockHashWhenCanonicalIsRequired() { final ChainParams chain = createChainWithACall(false); - assertNonBlockHashWhenCanonical(blockRef -> chain.web3.eth_call(chain.argsForCall, blockRef)); + assertNonBlockHashWhenCanonical(blockRef -> chain.web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(chain.argsForCall), blockRef)); } @Test //[ {argsForCall}, { "blockHash": "0x", "requireCanonical": false } -> raise block-not-found error void callByNonExistentBlockHashWhenCanonicalIsNotRequired() { final ChainParams chain = createChainWithACall(false); - assertNonBlockHashWhenIsNotCanonical(blockRef -> chain.web3.eth_call(chain.argsForCall, blockRef)); + assertNonBlockHashWhenIsNotCanonical(blockRef -> chain.web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(chain.argsForCall), blockRef)); } @Test // [ {argsForCall} { "blockHash": "0x", "requireCanonical": true } -> raise block-not-canonical error void callByNonCanonicalBlockHashWhenCanonicalIsRequired() { final ChainParams chain = createChainWithACall(true); - assertNonCanonicalBlockHashWhenCanonical(chain.block, blockRef -> chain.web3.eth_call(chain.argsForCall, blockRef)); + assertNonCanonicalBlockHashWhenCanonical(chain.block, blockRef -> chain.web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(chain.argsForCall), blockRef)); } @Test //[ {argsForCall}, { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "requireCanonical": true } -> return contract call respond at given address in genesis block void callByCanonicalBlockHashWhenCanonicalIsRequired() { final ChainParams chain = createChainWithACall(false); - assertCanonicalBlockHashWhenCanonical(CALL_RESPOND, chain.block, blockRef -> chain.web3.eth_call(chain.argsForCall, blockRef)); + assertCanonicalBlockHashWhenCanonical(CALL_RESPOND, chain.block, blockRef -> chain.web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(chain.argsForCall), blockRef)); } @Test //[ {argsForCall}, { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "requireCanonical": false } -> return contract call respond at given address in genesis block void callByCanonicalBlockHashWhenCanonicalIsNotRequired() { final ChainParams chain = createChainWithACall(false); - assertCanonicalBlockHashWhenNotCanonical(CALL_RESPOND, chain.block, blockRef -> chain.web3.eth_call(chain.argsForCall, blockRef)); + assertCanonicalBlockHashWhenNotCanonical(CALL_RESPOND, chain.block, blockRef -> chain.web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(chain.argsForCall), blockRef)); } @Test // [ {argsForCall}, { "blockHash": "0x", "requireCanonical": false } -> return contract call respond at given address in specified block void callByNonCanonicalBlockHashWhenCanonicalIsNotRequired() { final ChainParams chain = createChainWithACall(true); - assertNonCanonicalBlockHashWhenNotCanonical(CALL_RESPOND, chain.block, blockRef -> chain.web3.eth_call(chain.argsForCall, blockRef)); + assertNonCanonicalBlockHashWhenNotCanonical(CALL_RESPOND, chain.block, blockRef -> chain.web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(chain.argsForCall), blockRef)); } @Test // [ {argsForCall}, { "blockHash": "0x" } -> return contract call respond at given address in specified bloc void callByNonCanonicalBlockHash() { final ChainParams chain = createChainWithACall(true); - assertNonCanonicalBlockHash(CALL_RESPOND, chain.block, blockRef -> chain.web3.eth_call(chain.argsForCall, blockRef)); + assertNonCanonicalBlockHash(CALL_RESPOND, chain.block, blockRef -> chain.web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(chain.argsForCall), blockRef)); } @Test @@ -719,7 +727,7 @@ void getUnknownTransactionReceipt() { String hashString = tx.getHash().toHexString(); - Assertions.assertNull(web3.eth_getTransactionReceipt(hashString)); + Assertions.assertNull(web3.eth_getTransactionReceipt(new TxHashParam(hashString))); Assertions.assertNull(web3.rsk_getRawTransactionReceiptByHash(hashString)); } @@ -738,7 +746,7 @@ void getTransactionReceipt() { String hashString = tx.getHash().toHexString(); - TransactionReceiptDTO tr = web3.eth_getTransactionReceipt(hashString); + TransactionReceiptDTO tr = web3.eth_getTransactionReceipt(new TxHashParam(hashString)); assertNotNull(tr); assertEquals("0x" + hashString, tr.getTransactionHash()); @@ -789,7 +797,7 @@ void getTransactionReceiptNotInMainBlockchain() { String hashString = tx.getHash().toHexString(); - TransactionReceiptDTO tr = web3.eth_getTransactionReceipt(hashString); + TransactionReceiptDTO tr = web3.eth_getTransactionReceipt(new TxHashParam(hashString)); Assertions.assertNull(tr); } @@ -989,12 +997,12 @@ void getTransactionCount() { String accountAddress = ByteUtil.toHexString(acc1.getAddress().getBytes()); - String count = web3.eth_getTransactionCount(accountAddress, "0x1"); + String count = web3.eth_getTransactionCount(new HexAddressParam(accountAddress), new BlockRefParam(new BlockRef("0x1"))); assertNotNull(count); assertEquals("0x1", count); - count = web3.eth_getTransactionCount(accountAddress, "0x0"); + count = web3.eth_getTransactionCount(new HexAddressParam(accountAddress), new BlockRefParam(new BlockRef("0x0"))); assertNotNull(count); assertEquals("0x0", count); @@ -1004,42 +1012,42 @@ void getTransactionCount() { //[ "0x
", { "blockNumber": "0x0" } -> return tx count at given address in genesis block void getTransactionCountByBlockNumber() { final ChainParams chain = createChainWithATransaction(false); - assertByBlockNumber("0x1", blockRef -> chain.web3.eth_getTransactionCount(chain.accountAddress, blockRef)); + assertByBlockNumber("0x1", blockRef -> chain.web3.eth_getTransactionCount(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test //[ "0x
", { "invalidInput": "0x0" } -> throw RskJsonRpcRequestException void getTransactionCountAndInvalidInputThrowsException() { final ChainParams chain = createChainWithATransaction(false); - assertInvalidInput(blockRef -> chain.web3.eth_getTransactionCount(chain.accountAddress, blockRef)); + assertInvalidInput(blockRef -> chain.web3.eth_getTransactionCount(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test //[ "0x
", { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" } -> return tx count at given address in genesis block void getTransactionCountByBlockHash() { final ChainParams chain = createChainWithATransaction(false); - assertByBlockHash("0x1", chain.block, blockRef -> chain.web3.eth_getTransactionCount(chain.accountAddress, blockRef)); + assertByBlockHash("0x1", chain.block, blockRef -> chain.web3.eth_getTransactionCount(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test //[ "0x
", { "blockHash": "0x" } -> raise block-not-found error void getTransactionCountByNonExistentBlockHash() { final ChainParams chain = chainWithAccount10kBalance(false); - assertNonExistentBlockHash(blockRef -> chain.web3.eth_getTransactionCount(chain.accountAddress, blockRef)); + assertNonExistentBlockHash(blockRef -> chain.web3.eth_getTransactionCount(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test // [ "0x
", { "blockHash": "0x", "requireCanonical": true } -> raise block-not-canonical error void getTransactionCountByNonCanonicalBlockHashWhenCanonicalIsRequired() { final ChainParams chain = createChainWithATransaction(true); - assertNonCanonicalBlockHashWhenCanonical(chain.block, blockRef -> chain.web3.eth_getTransactionCount(chain.accountAddress, blockRef)); + assertNonCanonicalBlockHashWhenCanonical(chain.block, blockRef -> chain.web3.eth_getTransactionCount(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test // [ "0x
", { "blockHash": "0x" } -> return tx count at given address in specified bloc void getTransactionCountByNonCanonicalBlockHash() { final ChainParams chain = createChainWithATransaction(true); - assertNonCanonicalBlockHash("0x1", chain.block, blockRef -> chain.web3.eth_getTransactionCount(chain.accountAddress, blockRef)); + assertNonCanonicalBlockHash("0x1", chain.block, blockRef -> chain.web3.eth_getTransactionCount(new HexAddressParam(chain.accountAddress), new BlockRefParam(new BlockRef(blockRef)))); } @Test @@ -1780,7 +1788,7 @@ function greet(string memory param) public pure returns (string memory) { argsForCall.setTo(HexUtils.toJsonHex(tx.getContractAddress().getBytes())); argsForCall.setData("0xead710c40000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000"); - String result = web3.eth_call(argsForCall, "latest"); + String result = web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(argsForCall), new BlockIdentifierParam("latest")); assertEquals("0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000", result); } @@ -1827,7 +1835,7 @@ function greet(string memory param) public pure returns (string memory) { argsForCall.setTo(HexUtils.toJsonHex(tx.getContractAddress().getBytes())); argsForCall.setData("0xead710c40000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000"); - String result = web3.eth_call(argsForCall, "latest"); + String result = web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(argsForCall), new BlockIdentifierParam("latest")); assertEquals("0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000", result); } @@ -1860,7 +1868,7 @@ void callNoneContractReturn() { argsForCall.setTo(HexUtils.toUnformattedJsonHex(tx.getContractAddress().getBytes())); argsForCall.setData(HexUtils.toUnformattedJsonHex(func.encode())); - String result = web3.eth_call(argsForCall, "latest"); + String result = web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(argsForCall), new BlockIdentifierParam("latest")); assertEquals("0x", result); } @@ -1964,7 +1972,7 @@ void eth_sign() { byte[] hash = Keccak256Helper.keccak256("this is the data to hash".getBytes()); - String signature = web3.eth_sign(addr1, "0x" + ByteUtil.toHexString(hash)); + String signature = web3.eth_sign(new HexAddressParam(addr1), new HexDataParam("0x" + ByteUtil.toHexString(hash))); MatcherAssert.assertThat( signature, @@ -1990,7 +1998,7 @@ void eth_sign_testSignatureGenerationToBeAlways32BytesLength() { byte[] hash = Keccak256Helper.keccak256("this is the data to hash".getBytes()); - String signature = web3.eth_sign(addr1, "0x" + ByteUtil.toHexString(hash)); + String signature = web3.eth_sign(new HexAddressParam(addr1), new HexDataParam("0x" + ByteUtil.toHexString(hash))); MatcherAssert.assertThat( signature, @@ -2333,8 +2341,9 @@ private void checkSendTransaction(Byte chainId) { if (chainId != null) { args.setChainId(HexUtils.toJsonHex(new byte[]{chainId})); } + CallArgumentsParam argsParam = TransactionFactoryHelper.toCallArgumentsParam(args); - String txHash = web3.eth_sendTransaction(args); + String txHash = web3.eth_sendTransaction(argsParam); // ***** Verifies tx hash String to = toAddress.substring(2); @@ -2344,7 +2353,7 @@ private void checkSendTransaction(Byte chainId) { .gasPrice(gasPrice) .gasLimit(gasLimit) .destination(Hex.decode(to)) - .data(args.getData() == null ? null : Hex.decode(args.getData())) + .data(args.getData() == null ? null : Hex.decode(args.getData().substring(2))) .chainId(config.getNetworkConstants().getChainId()) .value(value) .build(); @@ -2403,7 +2412,7 @@ void callWithoutReturn() { argsForCall.setTo(HexUtils.toJsonHex(tx.getContractAddress().getBytes())); argsForCall.setData(HexUtils.toJsonHex(noreturn.functions.get("noreturn").encodeSignature())); - String result = web3.eth_call(argsForCall, "latest"); + String result = web3.eth_call(TransactionFactoryHelper.toCallArgumentsParam(argsForCall), new BlockIdentifierParam("latest")); Assertions.assertEquals("0x", result); } @@ -3040,7 +3049,7 @@ void transactionReceiptAndResultHasTypeField() { String hashString = tx.getHash().toHexString(); TxHashParam txHashParam = new TxHashParam(hashString); - TransactionReceiptDTO txReceipt = web3.eth_getTransactionReceipt(hashString); + TransactionReceiptDTO txReceipt = web3.eth_getTransactionReceipt(new TxHashParam(hashString)); TransactionResultDTO txResult = web3.eth_getTransactionByHash(txHashParam); assertEquals("0x0", txReceipt.getType()); diff --git a/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplUnitTest.java b/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplUnitTest.java index 752d0d05d5f..8b7f3b3f8b5 100644 --- a/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplUnitTest.java +++ b/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplUnitTest.java @@ -25,7 +25,10 @@ import org.ethereum.net.server.ChannelManager; import org.ethereum.net.server.PeerServer; import org.ethereum.rpc.exception.RskJsonRpcRequestException; +import org.ethereum.rpc.parameters.BlockRefParam; +import org.ethereum.rpc.parameters.HexAddressParam; import org.ethereum.util.BuildInfo; +import org.ethereum.util.TransactionFactoryHelper; import org.ethereum.vm.DataWord; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -101,12 +104,12 @@ void eth_getBalance_stateCannotBeRetrieved() { when(retriever.getInformationProvider(id)) .thenThrow(RskJsonRpcRequestException.blockNotFound("Block not found")); TestUtils.assertThrows(RskJsonRpcRequestException.class, - () -> target.eth_getBalance(addr, id)); + () -> target.eth_getBalance(new HexAddressParam(addr), new BlockRefParam(new BlockRef(id)))); } @Test void eth_getBalance() { - String id = "id"; + String id = "0x00"; String addr = "0x0011223344556677880011223344556677889900"; RskAddress expectedAddress = new RskAddress(addr); @@ -115,7 +118,7 @@ void eth_getBalance() { when(aip.getBalance(expectedAddress)) .thenReturn(new Coin(BigInteger.ONE)); - String result = target.eth_getBalance(addr, id); + String result = target.eth_getBalance(new HexAddressParam(addr),new BlockRefParam(new BlockRef(id))); assertEquals("0x1", result); } @@ -130,7 +133,7 @@ void eth_getBalanceByBlockRef() { }; final Web3Impl spyTarget = spy(target); doReturn("0x1").when(spyTarget).invokeByBlockRef(eq(blockRef),any()); - String result = spyTarget.eth_getBalance(addr, blockRef); + String result = spyTarget.eth_getBalance(new HexAddressParam(addr), new BlockRefParam(new BlockRef(blockRef))); assertEquals("0x1", result); verify(spyTarget).invokeByBlockRef(eq(blockRef),any()); } @@ -260,7 +263,7 @@ void eth_callAtByBlockRef() { final String expectedData = "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000"; doReturn(expectedData).when(spyTarget).invokeByBlockRef(eq(blockRef),any()); - String result = spyTarget.eth_call(argsForCall,blockRef); + String result = spyTarget.eth_call(TransactionFactoryHelper.toCallArgumentsParam(argsForCall),blockRef); assertEquals(expectedData, result); verify(spyTarget).invokeByBlockRef(eq(blockRef),any()); } @@ -276,7 +279,7 @@ void eth_getBlockTransactionCountByBlockRef() { }; final Web3Impl spyTarget = spy(target); doReturn("0x1").when(spyTarget).invokeByBlockRef(eq(blockRef),any()); - String result = spyTarget.eth_getTransactionCount(addr, blockRef); + String result = spyTarget.eth_getTransactionCount(new HexAddressParam(addr), new BlockRefParam(new BlockRef(blockRef))); assertEquals("0x1", result); verify(spyTarget).invokeByBlockRef(eq(blockRef),any()); } diff --git a/rskj-core/src/test/java/org/ethereum/util/TransactionFactoryHelper.java b/rskj-core/src/test/java/org/ethereum/util/TransactionFactoryHelper.java index a92f7cd8176..bea56c4e635 100644 --- a/rskj-core/src/test/java/org/ethereum/util/TransactionFactoryHelper.java +++ b/rskj-core/src/test/java/org/ethereum/util/TransactionFactoryHelper.java @@ -1,6 +1,7 @@ package org.ethereum.util; import java.math.BigInteger; +import java.util.Optional; import org.ethereum.core.Account; import org.ethereum.core.Transaction; @@ -10,6 +11,10 @@ import co.rsk.test.builders.AccountBuilder; import co.rsk.test.builders.TransactionBuilder; import co.rsk.util.HexUtils; +import org.ethereum.rpc.parameters.CallArgumentsParam; +import org.ethereum.rpc.parameters.HexAddressParam; +import org.ethereum.rpc.parameters.HexDataParam; +import org.ethereum.rpc.parameters.HexNumberParam; /** * Created by ajlopez on 28/02/2018. @@ -116,4 +121,18 @@ public static Transaction createTransaction(CallArguments args, byte chainId, Ac return tx; } + public static CallArgumentsParam toCallArgumentsParam(CallArguments args) { + return new CallArgumentsParam( + Optional.ofNullable(args.getFrom()).filter(p -> !p.isEmpty()).map(HexAddressParam::new).orElse(null), + Optional.ofNullable(args.getTo()).filter(p -> !p.isEmpty()).map(HexAddressParam::new).orElse(null), + Optional.ofNullable(args.getGas()).filter(p -> !p.isEmpty()).map(HexNumberParam::new).orElse(null), + Optional.ofNullable(args.getGasPrice()).filter(p -> !p.isEmpty()).map(HexNumberParam::new).orElse(null), + Optional.ofNullable(args.getGasLimit()).filter(p -> !p.isEmpty()).map(HexNumberParam::new).orElse(null), + Optional.ofNullable(args.getNonce()).filter(p -> !p.isEmpty()).map(HexNumberParam::new).orElse(null), + Optional.ofNullable(args.getChainId()).filter(p -> !p.isEmpty()).map(HexNumberParam::new).orElse(null), + Optional.ofNullable(args.getValue()).filter(p -> !p.isEmpty()).map(HexNumberParam::new).orElse(null), + Optional.ofNullable(args.getData()).filter(p -> !p.isEmpty()).map(HexDataParam::new).orElse(null) + ); + } + } diff --git a/rskj-core/src/test/java/org/ethereum/vm/program/NestedContractsTest.java b/rskj-core/src/test/java/org/ethereum/vm/program/NestedContractsTest.java index acff8d03dfd..dada3866b25 100644 --- a/rskj-core/src/test/java/org/ethereum/vm/program/NestedContractsTest.java +++ b/rskj-core/src/test/java/org/ethereum/vm/program/NestedContractsTest.java @@ -34,6 +34,9 @@ import org.ethereum.core.ReceivedTxSignatureCache; import org.ethereum.rpc.CallArguments; import org.ethereum.rpc.exception.RskJsonRpcRequestException; +import org.ethereum.rpc.parameters.BlockIdentifierParam; +import org.ethereum.rpc.parameters.CallArgumentsParam; +import org.ethereum.util.TransactionFactoryHelper; import org.ethereum.vm.DataWord; import org.ethereum.vm.PrecompiledContracts; import org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl; @@ -110,8 +113,10 @@ void testNested_interfaceCall_require() throws FileNotFoundException, DslProcess //Failed Call ContractA.buy(0) -> 0 > 0 final String contractA = getContractAddressString(TX_CONTRACTA); CallArguments args = buildArgs(contractA, Hex.toHexString(BUY_FUNCTION.encode(0))); + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + BlockIdentifierParam blockIdentifierParam = new BlockIdentifierParam("latest"); try { - ethModule.call(args, "latest"); + ethModule.call(callArgumentsParam, blockIdentifierParam); fail(); } catch (RskJsonRpcRequestException e) { MatcherAssert.assertThat(e.getMessage(), Matchers.containsString("Negative value")); @@ -119,7 +124,7 @@ void testNested_interfaceCall_require() throws FileNotFoundException, DslProcess //Success Call -> 2 > 0 args = buildArgs(contractA, Hex.toHexString(BUY_FUNCTION.encode(2))); - final String call = ethModule.call(args, "latest"); + final String call = ethModule.call(TransactionFactoryHelper.toCallArgumentsParam(args), new BlockIdentifierParam("latest")); //assertEquals("0x" + DataWord.valueOf(2).toString(), call); } @@ -151,12 +156,12 @@ void testNested_ABICall_require() throws FileNotFoundException, DslProcessorExce //Failed Call ContractA.buy(0) -> 0 > 0 final String contractA = getContractAddressString("tx03"); CallArguments args = buildArgs(contractA, Hex.toHexString(BUY_FUNCTION.encode(0))); - String call = ethModule.call(args, "latest"); + String call = ethModule.call(TransactionFactoryHelper.toCallArgumentsParam(args), new BlockIdentifierParam("latest")); assertEquals("0x" + DataWord.valueOf(0).toString(), call); //Success Call -> 2 > 0 args = buildArgs(contractA, Hex.toHexString(BUY_FUNCTION.encode(2))); - call = ethModule.call(args, "latest"); + call = ethModule.call(TransactionFactoryHelper.toCallArgumentsParam(args), new BlockIdentifierParam("latest")); assertEquals("0x" + DataWord.valueOf(2).toString(), call); }