add /nodes API for explorer (#585)

* add /nodes API for explorer

* Remove comment.

* pass pubkeys array pointer instead.

* pass function pointer to get nodeCount.

* rename to GetNumPeers
pull/598/head
Richard Liu 6 years ago committed by GitHub
parent 967a0c8d64
commit cb066b9edd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      api/service/explorer/service.go
  2. 5
      consensus/consensus.go
  3. 4
      node/service_setup.go

@ -27,16 +27,18 @@ type Service struct {
router *mux.Router router *mux.Router
IP string IP string
Port string Port string
GetNumPeers func() int
storage *Storage storage *Storage
server *http.Server server *http.Server
messageChan chan *msg_pb.Message messageChan chan *msg_pb.Message
} }
// New returns explorer service. // New returns explorer service.
func New(selfPeer *p2p.Peer) *Service { func New(selfPeer *p2p.Peer, GetNumPeers func() int) *Service {
return &Service{ return &Service{
IP: selfPeer.IP, IP: selfPeer.IP,
Port: selfPeer.Port, Port: selfPeer.Port,
GetNumPeers: GetNumPeers,
} }
} }
@ -89,6 +91,9 @@ func (s *Service) Run() *http.Server {
s.router.Path("/address").Queries("id", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.GetExplorerAddress).Methods("GET") s.router.Path("/address").Queries("id", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.GetExplorerAddress).Methods("GET")
s.router.Path("/address").HandlerFunc(s.GetExplorerAddress) s.router.Path("/address").HandlerFunc(s.GetExplorerAddress)
// Set up router for nodes.
s.router.Path("/nodes").HandlerFunc(s.GetExplorerNodes)
// Do serving now. // Do serving now.
utils.GetLogInstance().Info("Listening on ", "port: ", GetExplorerPort(s.Port)) utils.GetLogInstance().Info("Listening on ", "port: ", GetExplorerPort(s.Port))
server := &http.Server{Addr: addr, Handler: s.router} server := &http.Server{Addr: addr, Handler: s.router}
@ -247,6 +252,12 @@ func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(data.Address) json.NewEncoder(w).Encode(data.Address)
} }
// GetExplorerNodes serves /nodes end-point.
func (s *Service) GetExplorerNodes(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(s.GetNumPeers())
}
// NotifyService notify service // NotifyService notify service
func (s *Service) NotifyService(params map[string]interface{}) { func (s *Service) NotifyService(params map[string]interface{}) {
return return

@ -657,3 +657,8 @@ func (consensus *Consensus) SetLeaderPubKey(k []byte) error {
func (consensus *Consensus) GetLeaderPubKey() *bls.PublicKey { func (consensus *Consensus) GetLeaderPubKey() *bls.PublicKey {
return consensus.leader.ConsensusPubKey return consensus.leader.ConsensusPubKey
} }
// GetNumPeers returns the length of PublicKeys
func (consensus *Consensus) GetNumPeers() int {
return len(consensus.PublicKeys)
}

@ -26,7 +26,7 @@ func (node *Node) setupForShardLeader() {
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil))
// Register explorer service. // Register explorer service.
node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer)) node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.Consensus.GetNumPeers))
// Register consensus service. // Register consensus service.
node.serviceManager.RegisterService(service.Consensus, consensus.New(node.BlockChannel, node.Consensus, node.startConsensus)) node.serviceManager.RegisterService(service.Consensus, consensus.New(node.BlockChannel, node.Consensus, node.startConsensus))
// Register new block service. // Register new block service.
@ -67,7 +67,7 @@ func (node *Node) setupForBeaconLeader() {
// Register randomness service // Register randomness service
node.serviceManager.RegisterService(service.Randomness, randomness.New(node.DRand)) node.serviceManager.RegisterService(service.Randomness, randomness.New(node.DRand))
// Register explorer service. // Register explorer service.
node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer)) node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.Consensus.GetNumPeers))
} }
func (node *Node) setupForBeaconValidator() { func (node *Node) setupForBeaconValidator() {

Loading…
Cancel
Save