Skip to content

Commit d63b426

Browse files
authored
Cherry pick #738 into 0.13.0 (#739)
Return 32 byte 0 on empty state_changes array (#738) * Return 32 byte 0 on empty state_changes array Signed-off-by: lukelee-sl <[email protected]> * remove unnecessary optional chaining operator Signed-off-by: lukelee-sl <[email protected]> Signed-off-by: lukelee-sl <[email protected]> Signed-off-by: lukelee-sl <[email protected]>
1 parent 921114b commit d63b426

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

packages/relay/src/lib/eth.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,14 @@ export class EthImpl implements Eth {
499499
// retrieve the contract result details
500500
await this.mirrorNodeClient.getContractResultsDetails(address, contractResult.results[0].timestamp)
501501
.then(contractResultDetails => {
502-
if (EthImpl.isArrayNonEmpty(contractResultDetails?.state_changes)) {
502+
if(contractResultDetails === null) {
503+
throw predefined.RESOURCE_NOT_FOUND(`Contract result details for contract address ${address} at timestamp=${contractResult.results[0].timestamp}`);
504+
}
505+
if (EthImpl.isArrayNonEmpty(contractResultDetails.state_changes)) {
503506
// filter the state changes to match slot and return value
504507
const stateChange = contractResultDetails.state_changes.find(stateChange => stateChange.slot === slot);
505508
result = stateChange.value_written;
506-
} else {
507-
throw predefined.RESOURCE_NOT_FOUND(`Contract result details for contract address ${address} at timestamp=${contractResult.results[0].timestamp}`);
508-
}
509+
}
509510
})
510511
.catch((e: any) => {
511512
this.logger.error(

packages/relay/tests/lib/eth.spec.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,36 +2232,22 @@ describe('Eth calls using MirrorNode', async function () {
22322232
expect(hasError).to.be.true;
22332233
});
22342234

2235-
it('eth_getStorageAt should throw a predefined RESOURCE_NOT_FOUND when state_changes is null', async function () {
2235+
it('eth_getStorageAt should return EthImpl.zeroHex32Byte when state_changes is null', async function () {
22362236
defaultDetailedContractResultsNullStateChange
22372237
mock.onGet(`blocks/${blockNumber}`).reply(200, defaultBlock);
22382238
mock.onGet(`contracts/${contractAddress1}/results?timestamp=lte:${defaultBlock.timestamp.to}&limit=1&order=desc`).reply(200, defaultContractResults);
22392239
mock.onGet(`contracts/${contractAddress1}/results/${contractTimestamp1}`).reply(200, defaultDetailedContractResultsNullStateChange);
2240-
let hasError = false;
2241-
try {
2242-
await ethImpl.getStorageAt(contractAddress1, defaultDetailedContractResults.state_changes[0].slot, EthImpl.numberTo0x(blockNumber));
2243-
} catch (e: any) {
2244-
hasError = true;
2245-
expect(e.code).to.equal(predefined.RESOURCE_NOT_FOUND().code);
2246-
expect(e.name).to.equal(predefined.RESOURCE_NOT_FOUND().name);
2247-
}
2248-
expect(hasError).to.be.true;
2240+
const result = await ethImpl.getStorageAt(contractAddress1, defaultDetailedContractResults.state_changes[0].slot, EthImpl.numberTo0x(blockNumber));
2241+
expect(result).to.equal(EthImpl.zeroHex32Byte);
22492242
});
22502243

2251-
it('eth_getStorageAt should throw a predefined RESOURCE_NOT_FOUND when state_changes is an empty array', async function () {
2244+
it('eth_getStorageAt should return EthImpl.zeroHex32Byte when state_changes is an empty array', async function () {
22522245
defaultDetailedContractResultsNullStateChange
22532246
mock.onGet(`blocks/${blockNumber}`).reply(200, defaultBlock);
22542247
mock.onGet(`contracts/${contractAddress1}/results?timestamp=lte:${defaultBlock.timestamp.to}&limit=1&order=desc`).reply(200, defaultContractResults);
22552248
mock.onGet(`contracts/${contractAddress1}/results/${contractTimestamp1}`).reply(200, defaultDetailedContractResultsEmptyArrayStateChange);
2256-
let hasError = false;
2257-
try {
2258-
await ethImpl.getStorageAt(contractAddress1, defaultDetailedContractResults.state_changes[0].slot, EthImpl.numberTo0x(blockNumber));
2259-
} catch (e: any) {
2260-
hasError = true;
2261-
expect(e.code).to.equal(predefined.RESOURCE_NOT_FOUND().code);
2262-
expect(e.name).to.equal(predefined.RESOURCE_NOT_FOUND().name);
2263-
}
2264-
expect(hasError).to.be.true;
2249+
const result = await ethImpl.getStorageAt(contractAddress1, defaultDetailedContractResults.state_changes[0].slot, EthImpl.numberTo0x(blockNumber));
2250+
expect(result).to.equal(EthImpl.zeroHex32Byte);
22652251
});
22662252

22672253
it('eth_getStorageAt should throw error when contract not found', async function () {

0 commit comments

Comments
 (0)