From 8bfb6b0a67de384baf6cd55095e0bb80c204558a Mon Sep 17 00:00:00 2001 From: Calvin Lau <38898718+calvinaco@users.noreply.github.com> Date: Mon, 12 Jul 2021 20:26:33 +0800 Subject: [PATCH] rpc: implement `net_listening` and `net_peerCount` JSON-RPC endpoints (#252) * Add net_listening net_peerCount endpoint * Add test cases * Use RPC client from context * Fix lint issue * Revert unnecessary changes * Update JSON RPC documentation --- docs/basics/json_rpc.md | 27 +++++++++++++++++++++-- ethereum/rpc/namespaces/net/api.go | 27 +++++++++++++++++++++-- scripts/integration-test-all.sh | 2 +- tests/rpc/net_test.go | 35 ++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 tests/rpc/net_test.go diff --git a/docs/basics/json_rpc.md b/docs/basics/json_rpc.md index 37632770..f39d32b8 100644 --- a/docs/basics/json_rpc.md +++ b/docs/basics/json_rpc.md @@ -18,8 +18,8 @@ Check the JSON-RPC methods and namespaces supported on Ethermint. {synopsis} | [`web3_clientVersion`](#web3-clientversion) | Web3 | ✔ | | | [`web3_sha3`](#web3-sha3) | Web3 | ✔ | | | [`net_version`](#net-version) | Net | ✔ | | -| `net_peerCount` | Net | | | -| `net_listening` | Net | | | +| [`net_peerCount`](#net-peerCount) | Net | ✔ | | +| [`net_listening`](#net-listening) | Net | ✔ | | | [`eth_protocolVersion`](#eth-protocolversion) | Eth | ✔ | | | [`eth_syncing`](#eth-syncing) | Eth | ✔ | | | [`eth_gasPrice`](#eth-gasprice) | Eth | ✔ | | @@ -199,6 +199,29 @@ curl -X POST --data '{"jsonrpc":"2.0","method":"net_version","params":[],"id":1} {"jsonrpc":"2.0","id":1,"result":"8"} ``` +### net_peerCount + +Returns the number of peers currently connected to the client. +```json +// Request +curl -X POST --data '{"jsonrpc":"2.0","method":"net_peerCount","params":[],"id":1}' -H "Content-Type: application/json" http://localhost:8545 + +// Result +{"jsonrpc":"2.0","id":1,"result":23} +``` + +### net_listening + +Returns if client is actively listening for network connections. + +```json +// Request +curl -X POST --data '{"jsonrpc":"2.0","method":"net_listening","params":[],"id":1}' -H "Content-Type: application/json" http://localhost:8545 + +// Result +{"jsonrpc":"2.0","id":1,"result":true} +``` + ## Eth Methods ### eth_protocolVersion diff --git a/ethereum/rpc/namespaces/net/api.go b/ethereum/rpc/namespaces/net/api.go index 741c18df..a8e679ef 100644 --- a/ethereum/rpc/namespaces/net/api.go +++ b/ethereum/rpc/namespaces/net/api.go @@ -1,16 +1,18 @@ package net import ( + "context" "fmt" - ethermint "github.com/tharsis/ethermint/types" - "github.com/cosmos/cosmos-sdk/client" + rpcclient "github.com/tendermint/tendermint/rpc/client" + ethermint "github.com/tharsis/ethermint/types" ) // PublicAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec. type PublicAPI struct { networkVersion uint64 + tmClient rpcclient.Client } // NewPublicAPI creates an instance of the public Net Web3 API. @@ -23,6 +25,7 @@ func NewPublicAPI(clientCtx client.Context) *PublicAPI { return &PublicAPI{ networkVersion: chainIDEpoch.Uint64(), + tmClient: clientCtx.Client, } } @@ -30,3 +33,23 @@ func NewPublicAPI(clientCtx client.Context) *PublicAPI { func (s *PublicAPI) Version() string { return fmt.Sprintf("%d", s.networkVersion) } + +// Listening returns if client is actively listening for network connections. +func (s *PublicAPI) Listening() bool { + ctx := context.Background() + netInfo, err := s.tmClient.NetInfo(ctx) + if err != nil { + return false + } + return netInfo.Listening +} + +// PeerCount returns the number of peers currently connected to the client. +func (s *PublicAPI) PeerCount() int { + ctx := context.Background() + netInfo, err := s.tmClient.NetInfo(ctx) + if err != nil { + return 0 + } + return len(netInfo.Peers) +} diff --git a/scripts/integration-test-all.sh b/scripts/integration-test-all.sh index 2e28dca6..3c581ad9 100755 --- a/scripts/integration-test-all.sh +++ b/scripts/integration-test-all.sh @@ -61,7 +61,7 @@ fi # Compile ethermint echo "compiling ethermint" -make build-ethermint +make build # PID array declaration arr=() diff --git a/tests/rpc/net_test.go b/tests/rpc/net_test.go new file mode 100644 index 00000000..15a34ad5 --- /dev/null +++ b/tests/rpc/net_test.go @@ -0,0 +1,35 @@ +package rpc + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNet_Version(t *testing.T) { + rpcRes := Call(t, "net_version", []string{}) + + var res string + err := json.Unmarshal(rpcRes.Result, &res) + require.NoError(t, err) + require.Equal(t, "2", res) +} + +func TestNet_Listening(t *testing.T) { + rpcRes := Call(t, "net_listening", []string{}) + + var res bool + err := json.Unmarshal(rpcRes.Result, &res) + require.NoError(t, err) + require.True(t, res) +} + +func TestNet_PeerCount(t *testing.T) { + rpcRes := Call(t, "net_peerCount", []string{}) + + var res int + err := json.Unmarshal(rpcRes.Result, &res) + require.NoError(t, err) + require.Equal(t, 0, res) +}