From cb066b9edd79f0526473b76694a95a50db24cbe3 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Tue, 19 Mar 2019 13:59:20 -0700 Subject: [PATCH] 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 --- api/service/explorer/service.go | 17 ++++++++++++++--- consensus/consensus.go | 5 +++++ node/service_setup.go | 4 ++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/api/service/explorer/service.go b/api/service/explorer/service.go index d7fe98de6..7e19e5d87 100644 --- a/api/service/explorer/service.go +++ b/api/service/explorer/service.go @@ -27,16 +27,18 @@ type Service struct { router *mux.Router IP string Port string + GetNumPeers func() int storage *Storage server *http.Server messageChan chan *msg_pb.Message } // New returns explorer service. -func New(selfPeer *p2p.Peer) *Service { +func New(selfPeer *p2p.Peer, GetNumPeers func() int) *Service { return &Service{ - IP: selfPeer.IP, - Port: selfPeer.Port, + IP: selfPeer.IP, + 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").HandlerFunc(s.GetExplorerAddress) + // Set up router for nodes. + s.router.Path("/nodes").HandlerFunc(s.GetExplorerNodes) + // Do serving now. utils.GetLogInstance().Info("Listening on ", "port: ", GetExplorerPort(s.Port)) 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) } +// 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 func (s *Service) NotifyService(params map[string]interface{}) { return diff --git a/consensus/consensus.go b/consensus/consensus.go index b9bafe7ea..6e6fa46af 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -657,3 +657,8 @@ func (consensus *Consensus) SetLeaderPubKey(k []byte) error { func (consensus *Consensus) GetLeaderPubKey() *bls.PublicKey { return consensus.leader.ConsensusPubKey } + +// GetNumPeers returns the length of PublicKeys +func (consensus *Consensus) GetNumPeers() int { + return len(consensus.PublicKeys) +} diff --git a/node/service_setup.go b/node/service_setup.go index 8daa4c1ff..d9a148f4d 100644 --- a/node/service_setup.go +++ b/node/service_setup.go @@ -26,7 +26,7 @@ func (node *Node) setupForShardLeader() { node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) // 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. node.serviceManager.RegisterService(service.Consensus, consensus.New(node.BlockChannel, node.Consensus, node.startConsensus)) // Register new block service. @@ -67,7 +67,7 @@ func (node *Node) setupForBeaconLeader() { // Register randomness service node.serviceManager.RegisterService(service.Randomness, randomness.New(node.DRand)) // 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() {