-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
72 lines (63 loc) · 2.05 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package net
import (
"fmt"
"github.com/brc20-collab/brczero/app/rpc/monitor"
ethermint "github.com/brc20-collab/brczero/app/types"
"github.com/brc20-collab/brczero/libs/cosmos-sdk/client/context"
"github.com/brc20-collab/brczero/libs/tendermint/libs/log"
rpcclient "github.com/brc20-collab/brczero/libs/tendermint/rpc/client"
"github.com/spf13/viper"
)
const (
NameSpace = "net"
)
// PublicNetAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
type PublicNetAPI struct {
networkVersion uint64
logger log.Logger
Metrics *monitor.RpcMetrics
tmClient rpcclient.Client
}
// NewAPI creates an instance of the public Net Web3 API.
func NewAPI(clientCtx context.CLIContext, log log.Logger) *PublicNetAPI {
// parse the chainID from a integer string
chainIDEpoch, err := ethermint.ParseChainID(clientCtx.ChainID)
if err != nil {
panic(err)
}
api := &PublicNetAPI{
networkVersion: chainIDEpoch.Uint64(),
logger: log.With("module", "json-rpc", "namespace", NameSpace),
tmClient: clientCtx.Client,
}
if viper.GetBool(monitor.FlagEnableMonitor) {
api.Metrics = monitor.MakeMonitorMetrics(NameSpace)
}
return api
}
// Version returns the current ethereum protocol version.
func (api *PublicNetAPI) Version() string {
monitor := monitor.GetMonitor("net_version", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd()
return fmt.Sprintf("%d", api.networkVersion)
}
// Listening returns if client is actively listening for network connections.
func (api *PublicNetAPI) Listening() bool {
monitor := monitor.GetMonitor("net_listening", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd()
netInfo, err := api.tmClient.NetInfo()
if err != nil {
return false
}
return netInfo.Listening
}
// PeerCount returns the number of peers currently connected to the client.
func (api *PublicNetAPI) PeerCount() int {
monitor := monitor.GetMonitor("net_peerCount", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd()
netInfo, err := api.tmClient.NetInfo()
if err != nil {
return 0
}
return len(netInfo.Peers)
}