Skip to content

Commit

Permalink
Merge pull request #2229 from rsksmart/fix-eth-get-storage-at-encoded…
Browse files Browse the repository at this point in the history
…-redo

Return properly encoded value by eth_getStorageAt RPC method for non-existing keys
  • Loading branch information
Vovchyk committed Jan 18, 2024
2 parents 33050a1 + 70a617b commit 12fdadd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
3 changes: 2 additions & 1 deletion rskj-core/src/main/java/org/ethereum/rpc/Web3Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class Web3Impl implements Web3 {
private static final Logger logger = LoggerFactory.getLogger("web3");

private static final String CLIENT_VERSION_PREFIX = "RskJ";
private static final String NON_EXISTING_KEY_RESPONSE = "0x0000000000000000000000000000000000000000000000000000000000000000";

private final MinerClient minerClient;
private final MinerServer minerServer;
Expand Down Expand Up @@ -484,7 +485,7 @@ private String eth_getStorageAt(HexAddressParam address, HexNumberParam storageI
response = Optional.ofNullable(accountInformationProvider.getStorageValue(address.getAddress(), key))
.map(DataWord::getData)
.map(HexUtils::toUnformattedJsonHex)
.orElse("0x0");
.orElse(NON_EXISTING_KEY_RESPONSE);
return response;
} finally {
logger.debug("eth_getStorageAt({}, {}, {}): {}", address, storageIdx, blockId, response);
Expand Down
14 changes: 7 additions & 7 deletions rskj-core/src/test/java/org/ethereum/rpc/Web3ImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@
* Created by Ruben Altman on 09/06/2016.
*/
class Web3ImplTest {

private static final String BALANCE_10K_HEX = "0x2710"; //10.000
private static final String CALL_RESPOND = "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000";
private static final String NON_EXISTING_KEY_RESPONSE = "0x0000000000000000000000000000000000000000000000000000000000000000";

private final TestSystemProperties config = new TestSystemProperties();
private final BlockFactory blockFactory = new BlockFactory(config.getActivationConfig());
Expand Down Expand Up @@ -367,7 +367,7 @@ void getBalanceWithAccountAndBlockWithTransaction() {
//[ "0x<address>", { "blockNumber": "0x0" } -> return storage at given address in genesis block
void getStorageAtAccountAndBlockNumber() {
final ChainParams chain = chainWithAccount10kBalance(false);
assertByBlockNumber("0x0", blockRef -> chain.web3.eth_getStorageAt(
assertByBlockNumber(NON_EXISTING_KEY_RESPONSE, blockRef -> chain.web3.eth_getStorageAt(
new HexAddressParam(chain.accountAddress),
new HexNumberParam("0x0"),
new BlockRefParam(blockRef)));
Expand All @@ -377,7 +377,7 @@ void getStorageAtAccountAndBlockNumber() {
//[ "0x<address>", { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" } -> return storage at given address in genesis block
void getStorageAtAccountAndBlockHash() {
final ChainParams chain = chainWithAccount10kBalance(false);
assertByBlockHash("0x0", chain.block, blockRef -> chain.web3.eth_getStorageAt(
assertByBlockHash(NON_EXISTING_KEY_RESPONSE, chain.block, blockRef -> chain.web3.eth_getStorageAt(
new HexAddressParam(chain.accountAddress),
new HexNumberParam("0x0"),
new BlockRefParam(blockRef)));
Expand Down Expand Up @@ -428,7 +428,7 @@ void getStorageAtAccountAndNonCanonicalBlockHashWhenCanonicalIsRequired() {
//[ "0x<address>", { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "requireCanonical": true } -> return storage at given address in genesis block
void getStorageAtAccountAndCanonicalBlockHashWhenCanonicalIsRequired() {
final ChainParams chain = chainWithAccount10kBalance(false);
assertCanonicalBlockHashWhenCanonical("0x0", chain.block, blockRef -> chain.web3.eth_getStorageAt(
assertCanonicalBlockHashWhenCanonical(NON_EXISTING_KEY_RESPONSE, chain.block, blockRef -> chain.web3.eth_getStorageAt(
new HexAddressParam(chain.accountAddress),
new HexNumberParam("0x0"),
new BlockRefParam(blockRef)));
Expand All @@ -438,7 +438,7 @@ void getStorageAtAccountAndCanonicalBlockHashWhenCanonicalIsRequired() {
//[ "0x<address>", { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "requireCanonical": false } -> return storage at given address in genesis block
void getStorageAtAccountAndCanonicalBlockHashWhenCanonicalIsNotRequired() {
final ChainParams chain = chainWithAccount10kBalance(false);
assertCanonicalBlockHashWhenNotCanonical("0x0", chain.block, blockRef -> chain.web3.eth_getStorageAt(
assertCanonicalBlockHashWhenNotCanonical(NON_EXISTING_KEY_RESPONSE, chain.block, blockRef -> chain.web3.eth_getStorageAt(
new HexAddressParam(chain.accountAddress),
new HexNumberParam("0x0"),
new BlockRefParam(blockRef)));
Expand All @@ -448,7 +448,7 @@ void getStorageAtAccountAndCanonicalBlockHashWhenCanonicalIsNotRequired() {
// [ "0x<address>", { "blockHash": "0x<non-canonical-block-hash>", "requireCanonical": false } -> return storage at given address in specified block
void getStorageAtAccountAndNonCanonicalBlockHashWhenCanonicalIsNotRequired() {
final ChainParams chain = chainWithAccount10kBalance(true);
assertNonCanonicalBlockHashWhenNotCanonical("0x0", chain.block, blockRef -> chain.web3.eth_getStorageAt(
assertNonCanonicalBlockHashWhenNotCanonical(NON_EXISTING_KEY_RESPONSE, chain.block, blockRef -> chain.web3.eth_getStorageAt(
new HexAddressParam(chain.accountAddress),
new HexNumberParam("0x0"),
new BlockRefParam(blockRef)));
Expand All @@ -458,7 +458,7 @@ void getStorageAtAccountAndNonCanonicalBlockHashWhenCanonicalIsNotRequired() {
// [ "0x<address>", { "blockHash": "0x<non-canonical-block-hash>" } -> return storage at given address in specified bloc
void getStorageAtAccountAndNonCanonicalBlockHash() {
final ChainParams chain = chainWithAccount10kBalance(true);
assertNonCanonicalBlockHash("0x0", chain.block, blockRef -> chain.web3.eth_getStorageAt(
assertNonCanonicalBlockHash(NON_EXISTING_KEY_RESPONSE, chain.block, blockRef -> chain.web3.eth_getStorageAt(
new HexAddressParam(chain.accountAddress),
new HexNumberParam("0x0"),
new BlockRefParam(blockRef)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ void eth_getStorageAtEmptyCell() {
.thenReturn(null);

String result = target.eth_getStorageAt(hexAddressParam, hexNumberParam, blockRefParam);
assertEquals("0x0",
result);
assertEquals("0x0000000000000000000000000000000000000000000000000000000000000000", result);
}

@Test
Expand Down

0 comments on commit 12fdadd

Please sign in to comment.