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
This commit is contained in:
parent
8f73d556c6
commit
8bfb6b0a67
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ fi
|
||||
|
||||
# Compile ethermint
|
||||
echo "compiling ethermint"
|
||||
make build-ethermint
|
||||
make build
|
||||
|
||||
# PID array declaration
|
||||
arr=()
|
||||
|
35
tests/rpc/net_test.go
Normal file
35
tests/rpc/net_test.go
Normal file
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user