Skip to content

Commit

Permalink
fix: net_version now reads chainId from config and return decimal value
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Jun 28, 2024
1 parent 29d2e11 commit 9e17bff
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
9 changes: 6 additions & 3 deletions packages/relay/src/lib/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
*/

import { Net } from '../index';
import constants from './constants';
import { Client } from '@hashgraph/sdk';

export class NetImpl implements Net {
private client: Client;
private readonly chainId: string;

constructor(client: Client, chainId: string) {
constructor(client: Client) {
this.client = client;
this.chainId = chainId;

const hederaNetwork: string = (process.env.HEDERA_NETWORK || '{}').toLowerCase();
this.chainId = process.env.CHAIN_ID || constants.CHAIN_IDS[hederaNetwork] || '298';
if (this.chainId.startsWith('0x')) this.chainId = parseInt(this.chainId, 16).toString();
}

/**
Expand All @@ -39,7 +43,6 @@ export class NetImpl implements Net {

/**
* This is the chain id we registered.
* TODO Support some config when launching the server for this. dotenv support?
*/
version(): string {
return this.chainId;
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/src/lib/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class RelayImpl implements Relay {
this.clientMain = hapiService.getMainClientInstance();

this.web3Impl = new Web3Impl(this.clientMain);
this.netImpl = new NetImpl(this.clientMain, chainId);
this.netImpl = new NetImpl(this.clientMain);

this.mirrorNodeClient = new MirrorNodeClient(
process.env.MIRROR_NODE_URL || '',
Expand Down
17 changes: 13 additions & 4 deletions packages/relay/tests/lib/net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,27 @@
*
*/

import pino from 'pino';
import { expect } from 'chai';
import { Registry } from 'prom-client';
import { RelayImpl } from '../../src/lib/relay';
import constants from '../../src/lib/constants';

import pino from 'pino';
const logger = pino();

const Relay = new RelayImpl(logger, new Registry());

describe('Net', async function () {
it('should execute "net_listening"', async function () {
const result = await Relay.net().listening();
it('should execute "net_listening"', function () {
const result = Relay.net().listening();
expect(result).to.eq(false);
});

it('should execute "net_version"', function () {
const hederaNetwork: string = (process.env.HEDERA_NETWORK || '{}').toLowerCase();
let expectedNetVersion = process.env.CHAIN_ID || constants.CHAIN_IDS[hederaNetwork] || '298';
if (expectedNetVersion.startsWith('0x')) expectedNetVersion = parseInt(expectedNetVersion, 16).toString();

const actualNetVersion = Relay.net().version();
expect(actualNetVersion).to.eq(expectedNetVersion);
});
});
6 changes: 5 additions & 1 deletion packages/server/tests/acceptance/rpc_batch2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,11 @@ describe('@api-batch-2 RPC Server Acceptance Tests', function () {

it('should execute "net_version"', async function () {
const res = await relay.call(RelayCalls.ETH_ENDPOINTS.NET_VERSION, [], requestId);
expect(res).to.be.equal(CHAIN_ID);

let expectedVersion = CHAIN_ID as string;
if (expectedVersion.startsWith('0x')) expectedVersion = parseInt(expectedVersion, 16).toString();

expect(res).to.be.equal(expectedVersion);
});

it('should execute "eth_getUncleByBlockHashAndIndex"', async function () {
Expand Down

0 comments on commit 9e17bff

Please sign in to comment.