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. 14
      internal/hmyapi/net.go
  5. 8
      node/rpc.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()
}

@ -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 }

@ -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

@ -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)
}

@ -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,
},
}...)
}

Loading…
Cancel
Save