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

Return properly encoded value by eth_getStorageAt RPC method for non-existing keys #2229

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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