Skip to content

Commit

Permalink
return 0x instead of 0x0 for non existing contract addresses given to… (
Browse files Browse the repository at this point in the history
#419)

* return 0x instead of 0x0 for non existing contract addresses given to eth_getCode

Signed-off-by: lukelee-sl <luke.lee@swirldslabs.com>

* update acceptance tests to reflect expectation of 0x vs 0x0

Signed-off-by: lukelee-sl <luke.lee@swirldslabs.com>

* update values to use predefined constant for 0x

Signed-off-by: lukelee-sl <luke.lee@swirldslabs.com>
  • Loading branch information
lukelee-sl authored Aug 4, 2022
1 parent 25a74f8 commit 578fc45
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ export class EthImpl implements Eth {
// handle INVALID_CONTRACT_ID
if (e.isInvalidContractId()) {
this.logger.debug('Unable to find code for contract %s in block "%s", returning 0x0, err code: %s', address, blockNumber, e.statusCode);
cache.set(cachedLabel, '0x0', constants.CACHE_TTL.ONE_HOUR);
return '0x0';
cache.set(cachedLabel, EthImpl.emptyHex, constants.CACHE_TTL.ONE_HOUR);
return EthImpl.emptyHex;
}
this.logger.error(e, 'Error raised during getCode for address %s, err code: %s', address, e.statusCode);
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/relay/tests/lib/eth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ describe('Eth calls using MirrorNode', async function () {
const resNoCache = await ethImpl.getCode(contractAddress1, null);
const resCached = await ethImpl.getCode(contractAddress1, null);
sinon.assert.calledOnce(sdkClientStub.getContractByteCode);
expect(resNoCache).to.equal(EthImpl.zeroHex);
expect(resCached).to.equal(EthImpl.zeroHex);
expect(resNoCache).to.equal(EthImpl.emptyHex);
expect(resCached).to.equal(EthImpl.emptyHex);
});

it('should return the bytecode', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/server/tests/acceptance/erc20.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ethers, BigNumber } from 'ethers';
import ERC20MockJson from '../contracts/ERC20Mock.json';
import Assertions from '../helpers/assertions';
import {Utils} from '../helpers/utils';
import { EthImpl } from "@hashgraph/json-rpc-relay/src/lib/eth";


describe('ERC20 Acceptance Tests', async function () {
Expand All @@ -50,7 +51,7 @@ describe('ERC20 Acceptance Tests', async function () {

const testTitles = [
{testName: ERC20, expectedBytecode: ERC20MockJson.deployedBytecode},
{testName: HTS, expectedBytecode: '0x0'}
{testName: HTS, expectedBytecode: EthImpl.emptyHex}
];

before(async () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/server/tests/acceptance/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import parentContractJson from '../contracts/Parent.json';
import basicContractJson from '../contracts/Basic.json';
import logsContractJson from '../contracts/Logs.json';
import { predefined } from '../../../relay/src/lib/errors/JsonRpcError';
import { EthImpl } from '@hashgraph/json-rpc-relay/src/lib/eth';

describe('RPC Server Acceptance Tests', function () {
this.timeout(240 * 1000); // 240 seconds
Expand Down Expand Up @@ -989,19 +990,19 @@ describe('RPC Server Acceptance Tests', function () {

it('should return 0x0 for non-existing contract on eth_getCode', async function () {
const res = await relay.call('eth_getCode', [NON_EXISTING_ADDRESS]);
expect(res).to.eq('0x0');
expect(res).to.eq(EthImpl.emptyHex);
});

it('should return 0x0 for account evm_address on eth_getCode', async function () {
const evmAddress = Utils.idToEvmAddress(accounts[2].accountId.toString());
const res = await relay.call('eth_getCode', [evmAddress]);
expect(res).to.eq('0x0');
expect(res).to.eq(EthImpl.emptyHex);
});

it('should return 0x0 for account alias on eth_getCode', async function () {
const alias = Utils.idToEvmAddress(accounts[2].accountId.toString());
const res = await relay.call('eth_getCode', [alias]);
expect(res).to.eq('0x0');
expect(res).to.eq(EthImpl.emptyHex);
});
});

Expand Down

0 comments on commit 578fc45

Please sign in to comment.