forked from harmony-one/harmony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
67 lines (60 loc) · 1.79 KB
/
net.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
package rpc
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
)
// PublicNetService offers network related RPC methods
type PublicNetService struct {
net p2p.Host
chainID uint64
version Version
}
// NewPublicNetAPI creates a new net API instance.
func NewPublicNetAPI(net p2p.Host, chainID uint64, version Version) rpc.API {
// manually set different namespace to preserve legacy behavior
var namespace string
switch version {
case V1:
namespace = netV1Namespace
case V2:
namespace = netV2Namespace
case Eth:
namespace = netNamespace
default:
utils.Logger().Error().Msgf("Unknown version %v, ignoring API.", version)
return rpc.API{}
}
return rpc.API{
Namespace: namespace,
Version: APIVersion,
Service: &PublicNetService{net, chainID, version},
Public: true,
}
}
// PeerCount returns the number of connected peers
// Note that the return type is an interface to account for the different versions
func (s *PublicNetService) PeerCount(ctx context.Context) (interface{}, error) {
// Format response according to version
switch s.version {
case V1, Eth:
return hexutil.Uint(s.net.GetPeerCount()), nil
case V2:
return s.net.GetPeerCount(), nil
default:
return nil, ErrUnknownRPCVersion
}
}
// Version returns the network version, i.e. ChainID identifying which network we are using
func (s *PublicNetService) Version(ctx context.Context) interface{} {
switch s.version {
case Eth:
return hexutil.Uint64(nodeconfig.GetDefaultConfig().GetNetworkType().ChainConfig().EthCompatibleChainID.Uint64())
default:
return fmt.Sprintf("%d", s.chainID)
}
}