diff --git a/hmy/api_backend.go b/hmy/api_backend.go index 92141b2fb..143ccd333 100644 --- a/hmy/api_backend.go +++ b/hmy/api_backend.go @@ -18,7 +18,7 @@ import ( "github.com/harmony-one/harmony/core/types" ) -// APIBackend An implementation of Backend. Full client. +// APIBackend An implementation of internal/hmyapi/Backend. Full client. type APIBackend struct { hmy *Harmony } @@ -201,3 +201,8 @@ func (b *APIBackend) GetBalance(address common.Address) (*hexutil.Big, error) { balance, err := b.hmy.nodeAPI.GetBalanceOfAddress(address) return (*hexutil.Big)(balance), err } + +// NetVersion returns net version +func (b *APIBackend) NetVersion() uint64 { + return b.hmy.NetVersion() +} diff --git a/hmy/backend.go b/hmy/backend.go index 49e14ddc5..c43704eec 100644 --- a/hmy/backend.go +++ b/hmy/backend.go @@ -30,6 +30,9 @@ type Harmony struct { APIBackend *APIBackend nodeAPI NodeAPI + + // aka network version, which is used to identify which network we are using + networkID uint64 } // NodeAPI is the list of functions from node used to call rpc apis. @@ -55,6 +58,7 @@ func New(nodeAPI NodeAPI, txPool *core.TxPool, eventMux *event.TypeMux) (*Harmon chainDb: chainDb, bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms), nodeAPI: nodeAPI, + networkID: 1, // TODO(ricl): this should be from config } hmy.APIBackend = &APIBackend{hmy} @@ -67,3 +71,6 @@ func (s *Harmony) TxPool() *core.TxPool { return s.txPool } // BlockChain ... func (s *Harmony) BlockChain() *core.BlockChain { return s.blockchain } + +// NetVersion returns the network version, i.e. network ID identifying which network we are using +func (s *Harmony) NetVersion() uint64 { return s.networkID } diff --git a/internal/hmyapi/backend.go b/internal/hmyapi/backend.go index ceafa5b99..cba2745be 100644 --- a/internal/hmyapi/backend.go +++ b/internal/hmyapi/backend.go @@ -20,6 +20,8 @@ import ( // implementations: // * hmy/api_backend.go type Backend interface { + // NOTE(ricl): this is not in ETH Backend inteface. They put it directly in eth object. + NetVersion() uint64 // General Ethereum API // Downloader() *downloader.Downloader ProtocolVersion() int diff --git a/internal/hmyapi/net.go b/internal/hmyapi/net.go index 4d9fdec09..8ff61a804 100644 --- a/internal/hmyapi/net.go +++ b/internal/hmyapi/net.go @@ -9,13 +9,13 @@ import ( // PublicNetAPI offers network related RPC methods type PublicNetAPI struct { - net p2p.Host - networkID uint64 + net p2p.Host + networkVersion uint64 } // NewPublicNetAPI creates a new net API instance. -func NewPublicNetAPI(net p2p.Host, networkID uint64) *PublicNetAPI { - return &PublicNetAPI{net, networkID} +func NewPublicNetAPI(net p2p.Host, networkVersion uint64) *PublicNetAPI { + return &PublicNetAPI{net, networkVersion} } // PeerCount returns the number of connected peers @@ -23,7 +23,7 @@ func (s *PublicNetAPI) PeerCount() hexutil.Uint { return hexutil.Uint(s.net.GetPeerCount()) } -// NetworkID ... -func (s *PublicNetAPI) NetworkID() string { - return fmt.Sprintf("%d", 1) // TODO(ricl): we should add support for network id (https://github.com/ethereum/wiki/wiki/JSON-RPC#net_version) +// Version returns the network version, i.e. network ID identifying which network we are using +func (s *PublicNetAPI) Version() string { + return fmt.Sprintf("%d", s.networkVersion) // TODO(ricl): we should add support for network id (https://github.com/ethereum/wiki/wiki/JSON-RPC#net_version) } diff --git a/node/rpc.go b/node/rpc.go index d684f3764..d7b81360b 100644 --- a/node/rpc.go +++ b/node/rpc.go @@ -31,7 +31,7 @@ var ( httpEndpoint = "" wsEndpoint = "" - httpModules = []string{"hmy"} + httpModules = []string{"hmy", "net"} httpVirtualHosts = []string{"*"} httpTimeouts = rpc.DefaultHTTPTimeouts @@ -149,5 +149,11 @@ func (node *Node) APIs() []rpc.API { Service: filters.NewPublicFilterAPI(harmony.APIBackend, false), Public: true, }, + { + Namespace: "net", + Version: "1.0", + Service: hmyapi.NewPublicNetAPI(node.host, harmony.APIBackend.NetVersion()), + Public: true, + }, }...) }