Merge pull request #936 from lzl124631x/ricl-net

rpc net module
pull/958/head
Richard Liu 6 years ago committed by GitHub
commit 2f7b534024
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      hmy/api_backend.go
  2. 7
      hmy/backend.go
  3. 2
      internal/hmyapi/backend.go
  4. 12
      internal/hmyapi/net.go
  5. 8
      node/rpc.go

@ -18,7 +18,7 @@ import (
"github.com/harmony-one/harmony/core/types" "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 { type APIBackend struct {
hmy *Harmony hmy *Harmony
} }
@ -201,3 +201,8 @@ func (b *APIBackend) GetBalance(address common.Address) (*hexutil.Big, error) {
balance, err := b.hmy.nodeAPI.GetBalanceOfAddress(address) balance, err := b.hmy.nodeAPI.GetBalanceOfAddress(address)
return (*hexutil.Big)(balance), err return (*hexutil.Big)(balance), err
} }
// NetVersion returns net version
func (b *APIBackend) NetVersion() uint64 {
return b.hmy.NetVersion()
}

@ -30,6 +30,9 @@ type Harmony struct {
APIBackend *APIBackend APIBackend *APIBackend
nodeAPI NodeAPI 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. // 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, chainDb: chainDb,
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms), bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
nodeAPI: nodeAPI, nodeAPI: nodeAPI,
networkID: 1, // TODO(ricl): this should be from config
} }
hmy.APIBackend = &APIBackend{hmy} hmy.APIBackend = &APIBackend{hmy}
@ -67,3 +71,6 @@ func (s *Harmony) TxPool() *core.TxPool { return s.txPool }
// BlockChain ... // BlockChain ...
func (s *Harmony) BlockChain() *core.BlockChain { return s.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 }

@ -20,6 +20,8 @@ import (
// implementations: // implementations:
// * hmy/api_backend.go // * hmy/api_backend.go
type Backend interface { type Backend interface {
// NOTE(ricl): this is not in ETH Backend inteface. They put it directly in eth object.
NetVersion() uint64
// General Ethereum API // General Ethereum API
// Downloader() *downloader.Downloader // Downloader() *downloader.Downloader
ProtocolVersion() int ProtocolVersion() int

@ -10,12 +10,12 @@ import (
// PublicNetAPI offers network related RPC methods // PublicNetAPI offers network related RPC methods
type PublicNetAPI struct { type PublicNetAPI struct {
net p2p.Host net p2p.Host
networkID uint64 networkVersion uint64
} }
// NewPublicNetAPI creates a new net API instance. // NewPublicNetAPI creates a new net API instance.
func NewPublicNetAPI(net p2p.Host, networkID uint64) *PublicNetAPI { func NewPublicNetAPI(net p2p.Host, networkVersion uint64) *PublicNetAPI {
return &PublicNetAPI{net, networkID} return &PublicNetAPI{net, networkVersion}
} }
// PeerCount returns the number of connected peers // PeerCount returns the number of connected peers
@ -23,7 +23,7 @@ func (s *PublicNetAPI) PeerCount() hexutil.Uint {
return hexutil.Uint(s.net.GetPeerCount()) return hexutil.Uint(s.net.GetPeerCount())
} }
// NetworkID ... // Version returns the network version, i.e. network ID identifying which network we are using
func (s *PublicNetAPI) NetworkID() string { func (s *PublicNetAPI) Version() 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) 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)
} }

@ -31,7 +31,7 @@ var (
httpEndpoint = "" httpEndpoint = ""
wsEndpoint = "" wsEndpoint = ""
httpModules = []string{"hmy"} httpModules = []string{"hmy", "net"}
httpVirtualHosts = []string{"*"} httpVirtualHosts = []string{"*"}
httpTimeouts = rpc.DefaultHTTPTimeouts httpTimeouts = rpc.DefaultHTTPTimeouts
@ -149,5 +149,11 @@ func (node *Node) APIs() []rpc.API {
Service: filters.NewPublicFilterAPI(harmony.APIBackend, false), Service: filters.NewPublicFilterAPI(harmony.APIBackend, false),
Public: true, Public: true,
}, },
{
Namespace: "net",
Version: "1.0",
Service: hmyapi.NewPublicNetAPI(node.host, harmony.APIBackend.NetVersion()),
Public: true,
},
}...) }...)
} }

Loading…
Cancel
Save