From 469fed36c8fd77c2d8ef788d49e0f1c5f4b7eea4 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Sun, 2 Jun 2019 22:42:13 -0700 Subject: [PATCH 1/3] rpc net module --- hmy/api_backend.go | 7 ++++++- hmy/backend.go | 6 ++++++ internal/hmyapi/backend.go | 2 ++ internal/hmyapi/net.go | 14 +++++++------- node/rpc.go | 8 +++++++- 5 files changed, 28 insertions(+), 9 deletions(-) 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..59c60e0a0 100644 --- a/hmy/backend.go +++ b/hmy/backend.go @@ -30,6 +30,8 @@ type Harmony struct { APIBackend *APIBackend nodeAPI NodeAPI + + networkID uint64 } // NodeAPI is the list of functions from node used to call rpc apis. @@ -55,6 +57,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 +70,6 @@ func (s *Harmony) TxPool() *core.TxPool { return s.txPool } // BlockChain ... func (s *Harmony) BlockChain() *core.BlockChain { return s.blockchain } + +// NetVersion returns net version -- the network ID +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..f9b316db9 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 ... +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, + }, }...) } From 8a0d3f26f3c64c00b971d10306e6cb3560dd26a3 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Tue, 4 Jun 2019 22:15:47 -0700 Subject: [PATCH 2/3] add comment --- internal/hmyapi/net.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/hmyapi/net.go b/internal/hmyapi/net.go index f9b316db9..8ff61a804 100644 --- a/internal/hmyapi/net.go +++ b/internal/hmyapi/net.go @@ -23,7 +23,7 @@ func (s *PublicNetAPI) PeerCount() hexutil.Uint { return hexutil.Uint(s.net.GetPeerCount()) } -// 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) } From 13cdd9d067f2e7cfb55e0dd58cca611a2cbcd947 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Tue, 4 Jun 2019 22:17:41 -0700 Subject: [PATCH 3/3] update comments --- hmy/backend.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hmy/backend.go b/hmy/backend.go index 59c60e0a0..c43704eec 100644 --- a/hmy/backend.go +++ b/hmy/backend.go @@ -31,6 +31,7 @@ type Harmony struct { nodeAPI NodeAPI + // aka network version, which is used to identify which network we are using networkID uint64 } @@ -71,5 +72,5 @@ func (s *Harmony) TxPool() *core.TxPool { return s.txPool } // BlockChain ... func (s *Harmony) BlockChain() *core.BlockChain { return s.blockchain } -// NetVersion returns net version -- the network ID +// NetVersion returns the network version, i.e. network ID identifying which network we are using func (s *Harmony) NetVersion() uint64 { return s.networkID }