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

feat: neet_peerCount RPC Method #1240

Merged
merged 4 commits into from
Sep 5, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum RPC_METHODS {
debug_traceCall = 'debug_traceCall',
evm_mine = 'evm_mine',
web3_sha3 = 'web3_sha3',
net_peerCount = 'net_peerCount',

/**
* TO BE IMPLEMENTED METHODS:
Expand All @@ -57,7 +58,6 @@ enum RPC_METHODS {
eth_sign = 'eth_sign',
eth_submitWork = 'eth_submitWork',
net_listening = 'net_listening',
net_peerCount = 'net_peerCount',
parity_nextNonce = 'parity_nextNonce',
eth_newFilter = 'eth_newFilter',
eth_newBlockFilter = 'eth_newBlockFilter',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { VeChainSDKLogger } from '@vechain/sdk-logging';
import type { ThorClient } from '../../../../../../thor-client';
import { JSONRPCInternalError, stringifyData } from '@vechain/sdk-errors';

/**
* RPC Method net_peerCount implementation
*
* @param thorClient - The thor client instance to use.
* @param params - The standard array of rpc call parameters.
* @note:
* * params[0]: ...
* * params[1]: ...
* * params[n]: ...
*/
const netPeerCount = async (): Promise<void> => {
// To avoid eslint error
await Promise.resolve(0);

// Not implemented yet
VeChainSDKLogger('warning').log({
title: 'net_peerCount',
messages: ['Method "net_peerCount" has not been implemented yet.']
});
const netPeerCount = async (thorClient: ThorClient): Promise<number> => {
try {
const peers = await thorClient.nodes.getNodes();
return peers !== null ? peers.length : 0;
} catch (e) {
throw new JSONRPCInternalError(
'eth_netPeerCount',
-32603,
'Method "eth_netPeerCount" failed.',
{
url: thorClient.httpClient.baseURL,
innerError: stringifyData(e)
}
);
}
};

export { netPeerCount };
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ const RPCMethodsMap = (
await netListening();
},

[RPC_METHODS.net_peerCount]: async () => {
await netPeerCount();
[RPC_METHODS.net_peerCount]: async (): Promise<number> => {
return await netPeerCount(thorClient);
},

[RPC_METHODS.parity_nextNonce]: async () => {
Expand Down
9 changes: 3 additions & 6 deletions packages/network/src/thor-client/nodes/nodes-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class NodesModule {
*
* @returns A promise that resolves to the list of connected peers.
*/
public async getNodes(): Promise<ConnectedPeer | null> {
public async getNodes(): Promise<ConnectedPeer[] | null> {
return (await this.thor.httpClient.http(
'GET',
thorest.nodes.get.NODES()
)) as ConnectedPeer | null;
)) as ConnectedPeer[] | null;
}

/**
Expand All @@ -36,10 +36,7 @@ class NodesModule {
* But what if the node's disk is full, and it's not writing the new blocks to its database? In this case the node is off-sync even
* though it's technically alive and connected
* @returns A boolean indicating whether the node is healthy.
* @throws {InvalidDataTypeError} - if the timestamp key does not exist in the response from the API call to the node
* @throws {InvalidDataTypeError} - if the timestamp key exists in the response from the API call to the node but the value is not a number
* @throws {InvalidDataTypeError} - if the response from the API call to the node is not an object
* @throws {InvalidDataTypeError} - if the response from the API call to the node is null or undefined
* @throws {InvalidDataTypeError}
*/
public async isHealthy(): Promise<boolean> {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { beforeEach, describe, expect, jest, test } from '@jest/globals';
import {
RPC_METHODS,
RPCMethodsMap,
TESTNET_URL,
ThorClient
} from '../../../../../src';
import { JSONRPCInternalError } from '@vechain/sdk-errors';

/**
* RPC Mapper integration tests for 'net_peerCount' method
*
* @group integration/rpc-mapper/methods/net_peerCount
*/
describe('RPC Mapper - net_peerCount method tests', () => {
/**
* Thor client instance
*/
let thorClient: ThorClient;

/**
* Init thor client before each test
*/
beforeEach(() => {
// Init thor client
thorClient = ThorClient.fromUrl(TESTNET_URL);
});

/**
* net_peerCount RPC call tests - Negative cases
*/
describe('Negative cases', () => {
/**
* Test case where request fails
*/
test('Should throw `JSONRPCInternalError` when request fails', async () => {
// Mock the getBlock method to throw error
jest.spyOn(thorClient.nodes, 'getNodes').mockRejectedValue(
new Error()
);

await expect(
async () =>
await RPCMethodsMap(thorClient)[RPC_METHODS.net_peerCount](
[]
)
).rejects.toThrowError(JSONRPCInternalError);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
TESTNET_URL,
ThorClient
} from '../../../../../src';
import { VeChainSDKLogger } from '@vechain/sdk-logging';

/**
* RPC Mapper integration tests for 'net_peerCount' method
Expand All @@ -31,16 +30,13 @@ describe('RPC Mapper - net_peerCount method tests', () => {
*/
describe('net_peerCount - Positive cases', () => {
/**
* Positive case 1 - ... Description ...
* Should be able to get the peer count
*/
test('net_peerCount - positive case 1', async () => {
fabiorigam marked this conversation as resolved.
Show resolved Hide resolved
const logSpy = jest.spyOn(VeChainSDKLogger('warning'), 'log');

// NOT IMPLEMENTED YET!
await RPCMethodsMap(thorClient)[RPC_METHODS.net_peerCount]([-1]);

expect(logSpy).toHaveBeenCalled();
logSpy.mockRestore();
test('Should be able to get the peer count', async () => {
const peers = await RPCMethodsMap(thorClient)[
RPC_METHODS.net_peerCount
]([]);
expect(peers).toBeGreaterThanOrEqual(0);
});
});
});