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

Add missing network methods to ain-js #160

Merged
merged 5 commits into from
Mar 22, 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
4 changes: 1 addition & 3 deletions __tests__/__snapshots__/ain.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,4 @@ Object {
],
},
}
`;

exports[`ain-js Network should set provider 1`] = `true`;
`;
42 changes: 40 additions & 2 deletions __tests__/ain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,50 @@ describe('ain-js', function() {

it('should set provider', async function() {
ain.setProvider(test_node_2);
expect(ain.provider).not.toBeNull();
expect(ain.net).not.toBeNull();
});

it('getNetworkId', async function() {
expect(await ain.net.getNetworkId()).toBe(0);
expect(await ain.net.isListening()).toMatchSnapshot();
expect(await ain.net.getPeerCount()).toBeGreaterThan(0);
});

it('getChainId', async function() {
expect(await ain.net.getChainId()).toBe(0);
});

it('isListening', async function() {
expect(await ain.net.isListening()).toBe(true);
});

it('isSyncing', async function() {
expect(await ain.net.isSyncing()).toBe(false);
});

it('getPeerCount', async function() {
expect(await ain.net.getPeerCount()).toBeGreaterThan(0);
});

it('getConsensusStatus', async function() {
const status = await ain.net.getConsensusStatus();
expect(status).not.toBeNull();
expect(status.state).toBe('RUNNING');
});

it('getRawConsensusStatus', async function() {
const status = await ain.net.getRawConsensusStatus();
expect(status).not.toBeNull();
expect(status.consensus).not.toBeNull();
expect(status.consensus.state).toBe('RUNNING');
});

it('getPeerCandidateInfo', async function() {
const info = await ain.net.getPeerCandidateInfo();
expect(info.address).not.toBeNull();
expect(info.isAvailableForConnection).toBe(true);
expect(info.peerCandidateJsonRpcUrlList).not.toBeNull();
});

it('getProtocolVersion', async function() {
await ain.net.getProtocolVersion()
.then(res => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/test_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export const test_pw = 'ainetwork';
export const test_seed = 'receive ocean impact unaware march just dragon easy power muscle together flip';

export const test_node_1 = 'http://localhost:8081';
export const test_node_2 = 'http://localhost:8081';
export const test_node_2 = 'http://localhost:8082';
export const test_event_handler_node = 'http://localhost:8083';
65 changes: 48 additions & 17 deletions src/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,33 @@ export default class Network {
}

/**
* Checks whether the blockchain node is listening for network connections.
* @returns {Promise<boolean>}
* Fetches the ID of the network the blokchain node is connected to.
*/
isListening(): Promise<boolean> {
return this.provider.send('net_listening');
getNetworkId(): Promise<string> {
return this.provider.send('net_getNetworkId');
}

/**
* Fetches the ID of the network the blokchain node is connected to.
* Fetches the ID of the chain the blokchain node is validating.
*/
getNetworkId(): Promise<string> {
return this.provider.send('net_getNetworkId');
getChainId(): Promise<string> {
return this.provider.send('net_getChainId');
}

/**
* Checks the protocol version compatibility with the blockchain node.
* Checks whether the blockchain node is listening for network connections.
* @returns {Promise<boolean>}
*/
async checkProtocolVersion(): Promise<any> {
return this.provider.send('ain_checkProtocolVersion');
isListening(): Promise<boolean> {
return this.provider.send('net_listening');
}

/**
* Fetches the protocol version of the blockchain node.
* Checks whether the blockchain node is syncing with the network or not.
* @returns {Promise<boolean>}
*/
getProtocolVersion(): Promise<any> {
return this.provider.send('ain_getProtocolVersion');
isSyncing(): Promise<boolean> {
jiyoung-an marked this conversation as resolved.
Show resolved Hide resolved
return this.provider.send('net_syncing');
}

/**
Expand All @@ -58,11 +59,41 @@ export default class Network {
}

/**
* Checks whether the blockchain node is syncing with the network or not.
* @returns {Promise<boolean>}
* Fetches the consensus status of the network.
* @returns {Promise<any>}
*/
isSyncing(): Promise<boolean> {
return this.provider.send('net_syncing');
getConsensusStatus(): Promise<any> {
return this.provider.send('net_consensusStatus');
}

/**
* Fetches the consensus status raw data of the network.
* @returns {Promise<any>}
*/
getRawConsensusStatus(): Promise<any> {
return this.provider.send('net_rawConsensusStatus');
}

/**
* Fetches the peer candidate information of the blockchain node.
* @returns {Promise<any>}
*/
getPeerCandidateInfo(): Promise<any> {
return this.provider.send('p2p_getPeerCandidateInfo');
}

/**
* Checks the protocol version compatibility with the blockchain node.
*/
async checkProtocolVersion(): Promise<any> {
jiyoung-an marked this conversation as resolved.
Show resolved Hide resolved
return this.provider.send('ain_checkProtocolVersion');
}

/**
* Fetches the protocol version of the blockchain node.
*/
getProtocolVersion(): Promise<any> {
return this.provider.send('ain_getProtocolVersion');
}

/**
Expand Down
Loading