Skip to content

Commit

Permalink
Merge pull request #2132 from rsksmart/json_rpc_param_validation_pt5
Browse files Browse the repository at this point in the history
Update methods arguments types
  • Loading branch information
Vovchyk committed Sep 15, 2023
2 parents aefe3d7 + 66589f9 commit 35df451
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 62 deletions.
18 changes: 9 additions & 9 deletions rskj-core/src/main/java/co/rsk/rpc/Web3EthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ default String eth_chainId() {

String eth_getBlockTransactionCountByHash(BlockHashParam blockHash)throws Exception;

String eth_getBlockTransactionCountByNumber(String bnOrId)throws Exception;
String eth_getBlockTransactionCountByNumber(BlockIdentifierParam bnOrId)throws Exception;

String eth_getUncleCountByBlockHash(String blockHash)throws Exception;
String eth_getUncleCountByBlockHash(BlockHashParam blockHash)throws Exception;

String eth_getUncleCountByBlockNumber(String bnOrId)throws Exception;
String eth_getUncleCountByBlockNumber(BlockIdentifierParam bnOrId)throws Exception;

default String eth_getCode(String address, String blockId) {
String eth_getCode(HexAddressParam address, BlockRefParam blockRefParam) throws Exception;

default String getCode(HexAddressParam address, String blockId) {
return getEthModule().getCode(address, blockId);
}

String eth_getCode(String address, Map<String, String> blockRef) throws Exception; // NOSONAR

default String eth_sendRawTransaction(HexDataParam rawData) {
return getEthModule().sendRawTransaction(rawData);
}
Expand All @@ -113,19 +113,19 @@ default String eth_sendTransaction(CallArgumentsParam args) {

BlockResultDTO eth_getBlockByHash(BlockHashParam blockHash, Boolean fullTransactionObjects) throws Exception;

BlockResultDTO eth_getBlockByNumber(String bnOrId, Boolean fullTransactionObjects) throws Exception;
BlockResultDTO eth_getBlockByNumber(BlockIdentifierParam bnOrId, Boolean fullTransactionObjects) throws Exception;

TransactionResultDTO eth_getTransactionByHash(TxHashParam transactionHash) throws Exception;

TransactionResultDTO eth_getTransactionByBlockHashAndIndex(BlockHashParam blockHash, HexIndexParam index) throws Exception;

TransactionResultDTO eth_getTransactionByBlockNumberAndIndex(String bnOrId, String index) throws Exception;
TransactionResultDTO eth_getTransactionByBlockNumberAndIndex(BlockIdentifierParam bnOrId, HexIndexParam index) throws Exception;

TransactionReceiptDTO eth_getTransactionReceipt(TxHashParam transactionHash) throws Exception;

BlockResultDTO eth_getUncleByBlockHashAndIndex(BlockHashParam blockHash, HexIndexParam uncleIdx) throws Exception;

BlockResultDTO eth_getUncleByBlockNumberAndIndex(String blockId, String uncleIdx) throws Exception;
BlockResultDTO eth_getUncleByBlockNumberAndIndex(BlockIdentifierParam blockId, HexIndexParam uncleIdx) throws Exception;

String[] eth_getCompilers();

Expand Down
6 changes: 3 additions & 3 deletions rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,14 @@ public String chainId() {
return HexUtils.toJsonHex(new byte[]{chainId});
}

public String getCode(String address, String blockId) {
public String getCode(HexAddressParam address, String blockId) {
if (blockId == null) {
throw new NullPointerException();
}

String s = null;
RskAddress addr = address.getAddress();
try {
RskAddress addr = new RskAddress(address);

AccountInformationProvider accountInformationProvider = getAccountInformationProvider(blockId);

Expand All @@ -242,7 +242,7 @@ public String getCode(String address, String blockId) {
return s;
} finally {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("eth_getCode({}, {}): {}", address, blockId, s);
LOGGER.debug("eth_getCode({}, {}): {}", addr.toHexString(), blockId, s);
}
}
}
Expand Down
41 changes: 28 additions & 13 deletions rskj-core/src/main/java/org/ethereum/rpc/Web3Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,16 @@ public String eth_call(CallArgumentsParam args, Map<String, String> inputs) {
}

@Override
public String eth_getCode(String address, Map<String, String> inputs) {
return invokeByBlockRef(inputs, blockNumber -> this.eth_getCode(address, blockNumber));
public String eth_getCode(HexAddressParam address, BlockRefParam blockRefParam) {
if (blockRefParam.getIdentifier() != null) {
return this.getCode(address, blockRefParam.getIdentifier());
} else {
return this.eth_getCode(address, blockRefParam.getInputs());
}
}

private String eth_getCode(HexAddressParam address, Map<String, String> inputs) {
return invokeByBlockRef(inputs, blockNumber -> this.getCode(address, blockNumber));
}

@Override
Expand Down Expand Up @@ -607,11 +615,11 @@ public static Block getBlockByNumberOrStr(String bnOrId, Blockchain blockchain)
}

@Override
public String eth_getBlockTransactionCountByNumber(String bnOrId) {
public String eth_getBlockTransactionCountByNumber(BlockIdentifierParam bnOrId) {
String s = null;
try {

List<Transaction> txs = web3InformationRetriever.getTransactions(bnOrId);
List<Transaction> txs = web3InformationRetriever.getTransactions(bnOrId.getIdentifier());

s = toQuantityJsonHex(txs.size());
return s;
Expand All @@ -623,7 +631,8 @@ public String eth_getBlockTransactionCountByNumber(String bnOrId) {
}

@Override
public String eth_getUncleCountByBlockHash(String blockHash) {
public String eth_getUncleCountByBlockHash(BlockHashParam blockHashParam) {
String blockHash = blockHashParam.getHash().toString();
Block b = getBlockByJSonHash(blockHash);
if (b == null) {
throw blockNotFound(String.format("Block with hash %s not found", blockHash));
Expand All @@ -634,12 +643,13 @@ public String eth_getUncleCountByBlockHash(String blockHash) {
}

@Override
public String eth_getUncleCountByBlockNumber(String bnOrId) {
return web3InformationRetriever.getBlock(bnOrId)
public String eth_getUncleCountByBlockNumber(BlockIdentifierParam identifierParam) {
String bnorId = identifierParam.getIdentifier();
return web3InformationRetriever.getBlock(bnorId)
.map(Block::getUncleList)
.map(List::size)
.map(HexUtils::toQuantityJsonHex)
.orElseThrow(() -> blockNotFound(String.format("Block %s not found", bnOrId)));
.orElseThrow(() -> blockNotFound(String.format("Block %s not found", bnorId)));
}

public BlockInformationResult getBlockInformationResult(BlockInformation blockInformation) {
Expand Down Expand Up @@ -694,8 +704,10 @@ public BlockResultDTO eth_getBlockByHash(BlockHashParam blockHash, Boolean fullT
}

@Override
public BlockResultDTO eth_getBlockByNumber(String bnOrId, Boolean fullTransactionObjects) {
public BlockResultDTO eth_getBlockByNumber(BlockIdentifierParam identifierParam, Boolean fullTransactionObjects) {
BlockResultDTO s = null;
String bnOrId = identifierParam.getIdentifier();

try {

s = web3InformationRetriever.getBlock(bnOrId)
Expand Down Expand Up @@ -783,15 +795,16 @@ public TransactionResultDTO eth_getTransactionByBlockHashAndIndex(BlockHashParam
}

@Override
public TransactionResultDTO eth_getTransactionByBlockNumberAndIndex(String bnOrId, String index) {
public TransactionResultDTO eth_getTransactionByBlockNumberAndIndex(BlockIdentifierParam identifierParam, HexIndexParam index) {
TransactionResultDTO s = null;
String bnOrId = identifierParam.getIdentifier();
try {
Optional<Block> block = web3InformationRetriever.getBlock(bnOrId);
if (!block.isPresent()) {
return null;
}

int idx = jsonHexToInt(index);
int idx = index.getIndex();
List<Transaction> txs = web3InformationRetriever.getTransactions(bnOrId);
if (idx >= txs.size()) {
return null;
Expand Down Expand Up @@ -864,15 +877,17 @@ private BlockResultDTO getUncleResultDTO(Integer uncleIdx, Block block) {
}

@Override
public BlockResultDTO eth_getUncleByBlockNumberAndIndex(String blockId, String uncleIdx) {
public BlockResultDTO eth_getUncleByBlockNumberAndIndex(BlockIdentifierParam identifierParam, HexIndexParam uncleIdx) {
BlockResultDTO s = null;
String blockId = identifierParam.getIdentifier();

try {
Optional<Block> block = web3InformationRetriever.getBlock(blockId);

if (!block.isPresent()) {
return null;
}
int idx = jsonHexToInt(uncleIdx);
int idx = uncleIdx.getIndex();
s = getUncleResultDTO(idx, block.get());

return s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class HexStringParam {
}

if (!HexUtils.hasHexPrefix(hexString) || !HexUtils.isHex(hexString,2)) {
throw RskJsonRpcRequestException.invalidParamError("Invalid argument \"" + hexString + "\": param should be a hex value string.");
throw RskJsonRpcRequestException.invalidParamError(String.format("Invalid argument \"%s\": param should be a hex value string.", hexString));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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.util.ByteUtil;
import org.ethereum.util.TransactionFactoryHelper;
Expand Down Expand Up @@ -336,7 +337,8 @@ void getCode() {
config.getCallGasCap()
);

String addr = eth.getCode(TestUtils.generateAddress("addr").toHexString(), "pending");
HexAddressParam addressParam = new HexAddressParam(TestUtils.generateAddress("addr").toHexString());
String addr = eth.getCode(addressParam, "pending");
MatcherAssert.assertThat(Hex.decode(addr.substring("0x".length())), is(expectedCode));
}

Expand Down

0 comments on commit 35df451

Please sign in to comment.