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

Re-arrange network json rpc methods #1251

Merged
merged 2 commits into from
Mar 8, 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
146 changes: 73 additions & 73 deletions JSON_RPC_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,54 @@
## Table of Contents

- [Database API](#database-api)
- [ain_get](#ain_get)
- [ain_matchFunction](#ain_matchfunction)
- [ain_matchRule](#ain_matchrule)
- [ain_matchOwner](#ain_matchowner)
- [ain_evalRule](#ain_evalrule)
- [ain_evalOwner](#ain_evalowner)
- [ain_getStateProof](#ain_getstateproof)
- [ain_getProofHash](#ain_getproofhash)
- [ain_getStateInfo](#ain_getstateinfo)
- [ain_getStateUsage](#ain_getstateusage)
- [ain_get](#ain_get)
- [ain_matchFunction](#ain_matchfunction)
- [ain_matchRule](#ain_matchrule)
- [ain_matchOwner](#ain_matchowner)
- [ain_evalRule](#ain_evalrule)
- [ain_evalOwner](#ain_evalowner)
- [ain_getStateProof](#ain_getstateproof)
- [ain_getProofHash](#ain_getproofhash)
- [ain_getStateInfo](#ain_getstateinfo)
- [ain_getStateUsage](#ain_getstateusage)
- [Account API](#account-api)
- [ain_getAddress](#ain_getaddress)
- [ain_getBalance](#ain_getbalance)
- [ain_getNonce](#ain_getnonce)
- [ain_getTimestamp](#ain_gettimestamp)
- [ain_getBalance](#ain_getbalance)
- [ain_getNonce](#ain_getnonce)
- [ain_getTimestamp](#ain_gettimestamp)
- [Transaction API](#transaction-api)
- [ain_getPendingTransactions](#ain_getpendingtransactions)
- [ain_getTransactionPoolSizeUtilization](#ain_gettransactionpoolsizeutilization)
- [ain_getTransactionByHash](#ain_gettransactionbyhash)
- [ain_getTransactionByBlockHashAndIndex](#ain_gettransactionbyblockhashandindex)
- [ain_getTransactionByBlockNumberAndIndex](#ain_gettransactionbyblocknumberandindex)
- [ain_sendSignedTransactionDryrun](#ain_sendsignedtransactiondryrun)
- [ain_sendSignedTransaction](#ain_sendsignedtransaction)
- [ain_sendSignedTransactionBatch](#ain_sendsignedtransactionbatch)
- [ain_getPendingTransactions](#ain_getpendingtransactions)
- [ain_getTransactionPoolSizeUtilization](#ain_gettransactionpoolsizeutilization)
- [ain_getTransactionByHash](#ain_gettransactionbyhash)
- [ain_getTransactionByBlockHashAndIndex](#ain_gettransactionbyblockhashandindex)
- [ain_getTransactionByBlockNumberAndIndex](#ain_gettransactionbyblocknumberandindex)
- [ain_sendSignedTransactionDryrun](#ain_sendsignedtransactiondryrun)
- [ain_sendSignedTransaction](#ain_sendsignedtransaction)
- [ain_sendSignedTransactionBatch](#ain_sendsignedtransactionbatch)
- [Block API](#block-api)
- [ain_getLastBlock](#ain_getlastblock)
- [ain_getLastBlockNumber](#ain_getlastblocknumber)
- [ain_getBlockByNumber](#ain_getblockbynumber)
- [ain_getBlockByHash](#ain_getblockbyhash)
- [ain_getBlockList](#ain_getblocklist)
- [ain_getBlockHeadersList](#ain_getblockheaderslist)
- [ain_getBlockTransactionCountByNumber](#ain_getblocktransactioncountbynumber)
- [ain_getBlockTransactionCountByHash](#ain_getblocktransactioncountbyhash)
- [ain_getLastBlock](#ain_getlastblock)
- [ain_getLastBlockNumber](#ain_getlastblocknumber)
- [ain_getBlockByNumber](#ain_getblockbynumber)
- [ain_getBlockByHash](#ain_getblockbyhash)
- [ain_getBlockList](#ain_getblocklist)
- [ain_getBlockHeadersList](#ain_getblockheaderslist)
- [ain_getBlockTransactionCountByNumber](#ain_getblocktransactioncountbynumber)
- [ain_getBlockTransactionCountByHash](#ain_getblocktransactioncountbyhash)
- [Blockchain Node API](#blockchain-node-api)
- [ain_getValidatorInfo](#ain_getvalidatorinfo)
- [ain_getValidatorsByNumber](#ain_getvalidatorsbynumber)
- [ain_getValidatorsByHash](#ain_getvalidatorsbyhash)
- [ain_getProposerByNumber](#ain_getproposerbynumber)
- [ain_getProposerByHash](#ain_getproposerbyhash)
- [ain_getValidatorInfo](#ain_getvalidatorinfo)
- [ain_getValidatorsByNumber](#ain_getvalidatorsbynumber)
- [ain_getValidatorsByHash](#ain_getvalidatorsbyhash)
- [ain_getProposerByNumber](#ain_getproposerbynumber)
- [ain_getProposerByHash](#ain_getproposerbyhash)
- [Network API](#network-api)
- [net_listening](#net_listening)
- [net_peerCount](#net_peercount)
- [net_syncing](#net_syncing)
- [net_getNetworkId](#net_getnetworkid)
- [net_getChainId](#net_getchainid)
- [net_consensusStatus](#net_consensusstatus)
- [net_rawConsensusStatus](#net_rawconsensusstatus)
- [p2p_getPeerCandidateInfo](#p2p_getpeercandidateinfo)
- [net_getNetworkId](#net_getnetworkid)
- [net_getChainId](#net_getchainid)
- [net_listening](#net_listening)
- [net_syncing](#net_syncing)
- [net_peerCount](#net_peercount)
- [net_consensusStatus](#net_consensusstatus)
- [net_rawConsensusStatus](#net_rawconsensusstatus)
- [p2p_getPeerCandidateInfo](#p2p_getpeercandidateinfo)

---

Expand Down Expand Up @@ -2791,9 +2791,9 @@ Response

## Network API

### net_listening
### net_getNetworkId

Returns whether the node is listening for network connections.
Returns the blockchain node's network id.

**Parameters**

Expand All @@ -2803,16 +2803,19 @@ An object with properties:

**Returns**

`Boolean` - true is the node is listening for connections; otherwise, false.
`Number` - the network id.

- 0: mainnet network
- 1: testnet network

**Example**

Request
```
curl https://testnet-api.ainetwork.ai/json-rpc -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "net_listening",
"method": "net_getNetworkId",
"params": {
"protoVer": "1.1.3"
}
Expand All @@ -2825,15 +2828,15 @@ Response
"jsonrpc": "2.0",
"id": 1,
"result": {
"result": true,
"result": 0,
"protoVer": "1.1.3"
}
}
```

### net_peerCount
### net_getChainId

Returns the number of peers the node is connected to.
Returns the blockchain node's chain id.

**Parameters**

Expand All @@ -2843,16 +2846,19 @@ An object with properties:

**Returns**

`Number` - number of peers.
`Number` - the chain id.

- 0: mainnet chain
- 1: testnet chain

**Example**

Request
```
curl https://testnet-api.ainetwork.ai/json-rpc -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "net_peerCount",
"method": "net_getChainId",
"params": {
"protoVer": "1.1.3"
}
Expand All @@ -2865,15 +2871,15 @@ Response
"jsonrpc": "2.0",
"id": 1,
"result": {
"result": 3,
"result": 0,
"protoVer": "1.1.3"
}
}
```

### net_syncing
### net_listening

Returns whether the node is syncing with the network or not.
Returns whether the node is listening for network connections.

**Parameters**

Expand All @@ -2883,7 +2889,7 @@ An object with properties:

**Returns**

`Boolean` - true if the node is syncing, false otherwise.
`Boolean` - true is the node is listening for connections; otherwise, false.

**Example**

Expand All @@ -2892,7 +2898,7 @@ Request
curl https://testnet-api.ainetwork.ai/json-rpc -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "net_syncing",
"method": "net_listening",
"params": {
"protoVer": "1.1.3"
}
Expand All @@ -2905,15 +2911,15 @@ Response
"jsonrpc": "2.0",
"id": 1,
"result": {
"result": false,
"result": true,
"protoVer": "1.1.3"
}
}
```

### net_getNetworkId
### net_syncing

Returns the blockchain node's network id.
Returns whether the node is syncing with the network or not.

**Parameters**

Expand All @@ -2923,19 +2929,16 @@ An object with properties:

**Returns**

`Number` - the network id.
`Boolean` - true if the node is syncing, false otherwise.

- 0: mainnet network
- 1: testnet network

**Example**

Request
```
curl https://testnet-api.ainetwork.ai/json-rpc -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "net_getNetworkId",
"method": "net_syncing",
"params": {
"protoVer": "1.1.3"
}
Expand All @@ -2948,15 +2951,15 @@ Response
"jsonrpc": "2.0",
"id": 1,
"result": {
"result": 0,
"result": false,
"protoVer": "1.1.3"
}
}
```

### net_getChainId
### net_peerCount

Returns the blockchain node's chain id.
Returns the number of peers the node is connected to.

**Parameters**

Expand All @@ -2966,19 +2969,16 @@ An object with properties:

**Returns**

`Number` - the chain id.
`Number` - number of peers.

- 0: mainnet chain
- 1: testnet chain

**Example**

Request
```
curl https://testnet-api.ainetwork.ai/json-rpc -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "net_getChainId",
"method": "net_peerCount",
"params": {
"protoVer": "1.1.3"
}
Expand All @@ -2991,7 +2991,7 @@ Response
"jsonrpc": "2.0",
"id": 1,
"result": {
"result": 0,
"result": 3,
"protoVer": "1.1.3"
}
}
Expand Down
30 changes: 15 additions & 15 deletions json_rpc/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,47 @@ const { JSON_RPC_METHODS } = require('./constants');

module.exports = function getNetworkApis(node, p2pServer) {
return {
[JSON_RPC_METHODS.NET_LISTENING]: function(args, done) {
[JSON_RPC_METHODS.NET_GET_NETWORK_ID]: function(args, done) {
const beginTime = Date.now();
const peerCount = Object.keys(p2pServer.inbound).length;
const result = !!peerCount;
const result = node.getBlockchainParam('genesis/network_id');
const latency = Date.now() - beginTime;
trafficStatsManager.addEvent(TrafficEventTypes.JSON_RPC_GET, latency);
done(null, JsonRpcUtil.addProtocolVersion({ result }));
},

[JSON_RPC_METHODS.NET_PEER_COUNT]: function(args, done) {
[JSON_RPC_METHODS.NET_GET_CHAIN_ID]: function(args, done) {
const beginTime = Date.now();
const peerCount = Object.keys(p2pServer.inbound).length;
const result = node.getBlockchainParam('genesis/chain_id');
const latency = Date.now() - beginTime;
trafficStatsManager.addEvent(TrafficEventTypes.JSON_RPC_GET, latency);
done(null, JsonRpcUtil.addProtocolVersion({ result: peerCount }));
done(null, JsonRpcUtil.addProtocolVersion({ result }));
},

[JSON_RPC_METHODS.NET_SYNCING]: function(args, done) {
[JSON_RPC_METHODS.NET_LISTENING]: function(args, done) {
const beginTime = Date.now();
const result = (node.state === BlockchainNodeStates.CHAIN_SYNCING);
const peerCount = Object.keys(p2pServer.inbound).length;
const result = !!peerCount;
const latency = Date.now() - beginTime;
trafficStatsManager.addEvent(TrafficEventTypes.JSON_RPC_GET, latency);
// TODO(liayoo): Return { starting, latest } with block numbers
// if the node is currently syncing.
done(null, JsonRpcUtil.addProtocolVersion({ result }));
},

[JSON_RPC_METHODS.NET_GET_NETWORK_ID]: function(args, done) {
[JSON_RPC_METHODS.NET_SYNCING]: function(args, done) {
const beginTime = Date.now();
const result = node.getBlockchainParam('genesis/network_id');
const result = (node.state === BlockchainNodeStates.CHAIN_SYNCING);
const latency = Date.now() - beginTime;
trafficStatsManager.addEvent(TrafficEventTypes.JSON_RPC_GET, latency);
// TODO(liayoo): Return { starting, latest } with block numbers
// if the node is currently syncing.
done(null, JsonRpcUtil.addProtocolVersion({ result }));
},

[JSON_RPC_METHODS.NET_GET_CHAIN_ID]: function(args, done) {
[JSON_RPC_METHODS.NET_PEER_COUNT]: function(args, done) {
const beginTime = Date.now();
const result = node.getBlockchainParam('genesis/chain_id');
const peerCount = Object.keys(p2pServer.inbound).length;
const latency = Date.now() - beginTime;
trafficStatsManager.addEvent(TrafficEventTypes.JSON_RPC_GET, latency);
done(null, JsonRpcUtil.addProtocolVersion({ result }));
done(null, JsonRpcUtil.addProtocolVersion({ result: peerCount }));
},

[JSON_RPC_METHODS.NET_CONSENSUS_STATUS]: function(args, done) {
Expand Down
Loading