Skip to content

Commit 6fa8a1d

Browse files
authored
Update eth_getCode to handle call to iHTS address (#629)
Update eth_getCode to handle call to iHTS address. This will support eth dev tool calls to address(0x167) - Add catch case for contract type and address(0x167) - Add test cases Signed-off-by: Nana Essilfie-Conduah <[email protected]>
1 parent 45cb863 commit 6fa8a1d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

packages/relay/src/lib/eth.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export class EthImpl implements Eth {
6161
static feeHistoryEmptyResponse = { baseFeePerGas: [], gasUsedRatio: [], reward: [], oldestBlock: EthImpl.zeroHex };
6262
static redirectBytecodePrefix = '6080604052348015600f57600080fd5b506000610167905077618dc65e';
6363
static redirectBytecodePostfix = '600052366000602037600080366018016008845af43d806000803e8160008114605857816000f35b816000fdfea2646970667358221220d8378feed472ba49a0005514ef7087017f707b45fb9bf56bb81bb93ff19a238b64736f6c634300080b0033';
64-
64+
static iHTSAddress = '0x0000000000000000000000000000000000000167';
65+
static invalidEVMInstruction = '0xfe';
6566

6667
// endpoint metric callerNames
6768
static ethCall = 'eth_call';
@@ -573,7 +574,14 @@ export class EthImpl implements Eth {
573574
*/
574575
async getCode(address: string, blockNumber: string | null, requestId?: string) {
575576
const requestIdPrefix = formatRequestIdMessage(requestId);
576-
// FIXME: This has to be reimplemented to get the data from the mirror node.
577+
578+
// check for static precompile cases first before consulting nodes
579+
// this also account for environments where system entitites were not yet exposed to the mirror node
580+
if (address === EthImpl.iHTSAddress) {
581+
this.logger.trace(`${requestIdPrefix} HTS precompile case, return ${EthImpl.invalidEVMInstruction} for byte code`);
582+
return EthImpl.invalidEVMInstruction;
583+
}
584+
577585
this.logger.trace(`${requestIdPrefix} getCode(address=${address}, blockNumber=${blockNumber})`);
578586

579587
const cachedLabel = `getCode.${address}.${blockNumber}`;
@@ -584,17 +592,20 @@ export class EthImpl implements Eth {
584592

585593
try {
586594
const result = await this.mirrorNodeClient.resolveEntityType(address, requestId);
587-
588-
if (result && result?.type === constants.TYPE_TOKEN) {
595+
if (result) {
596+
if (result?.type === constants.TYPE_TOKEN) {
597+
this.logger.trace(`${requestIdPrefix} Token redirect case, return redirectBytecode`);
589598
return EthImpl.redirectBytecodeAddressReplace(address);
599+
}
600+
else if (result?.type === constants.TYPE_CONTRACT) {
601+
if (result?.entity.runtime_bytecode !== EthImpl.emptyHex) {
602+
return result?.entity.runtime_bytecode;
603+
}
604+
}
590605
}
591-
else if (result && result?.type === constants.TYPE_CONTRACT && result?.entity.runtime_bytecode !== EthImpl.emptyHex) {
592-
return result?.entity.runtime_bytecode;
593-
}
594-
else{
595-
const bytecode = await this.sdkClient.getContractByteCode(0, 0, address, EthImpl.ethGetCode, requestId);
596-
return EthImpl.prepend0x(Buffer.from(bytecode).toString('hex'));
597-
}
606+
607+
const bytecode = await this.sdkClient.getContractByteCode(0, 0, address, EthImpl.ethGetCode, requestId);
608+
return EthImpl.prepend0x(Buffer.from(bytecode).toString('hex'));
598609
} catch (e: any) {
599610
if(e instanceof SDKClientError) {
600611
// handle INVALID_CONTRACT_ID

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,14 @@ describe('Eth calls using MirrorNode', async function () {
12811281
const res = await ethImpl.getCode(htsTokenAddress, null);
12821282
expect(res).to.equal(redirectBytecode);
12831283
});
1284+
1285+
it('should return the static bytecode for address(0x167) call', async () => {
1286+
mock.onGet(`contracts/${EthImpl.iHTSAddress}`).reply(200, defaultContract);
1287+
mock.onGet(`accounts/${EthImpl.iHTSAddress}`).reply(404, null);
1288+
1289+
const res = await ethImpl.getCode(EthImpl.iHTSAddress, null);
1290+
expect(res).to.equal(EthImpl.invalidEVMInstruction);
1291+
});
12841292
});
12851293

12861294
describe('eth_getLogs', async function () {

0 commit comments

Comments
 (0)