diff --git a/api/service/explorer/service.go b/api/service/explorer/service.go index 7e19e5d87..5a5992d6f 100644 --- a/api/service/explorer/service.go +++ b/api/service/explorer/service.go @@ -11,6 +11,7 @@ import ( "github.com/ethereum/go-ethereum/rlp" "github.com/gorilla/mux" + "github.com/harmony-one/bls/ffi/go/bls" msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/internal/utils" @@ -27,18 +28,18 @@ type Service struct { router *mux.Router IP string Port string - GetNumPeers func() int + GetPeers func() []*bls.PublicKey storage *Storage server *http.Server messageChan chan *msg_pb.Message } // New returns explorer service. -func New(selfPeer *p2p.Peer, GetNumPeers func() int) *Service { +func New(selfPeer *p2p.Peer, GetPeers func() []*bls.PublicKey) *Service { return &Service{ - IP: selfPeer.IP, - Port: selfPeer.Port, - GetNumPeers: GetNumPeers, + IP: selfPeer.IP, + Port: selfPeer.Port, + GetPeers: GetPeers, } } @@ -91,8 +92,13 @@ 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) + // Set up router for node count. + s.router.Path("/nodes").HandlerFunc(s.GetExplorerNodeCount) // TODO(ricl): this is going to be replaced by /node-count + s.router.Path("/node-count").HandlerFunc(s.GetExplorerNodeCount) + + // Set up router for shard + s.router.Path("/shard").Queries("id", "{[0-9]*?}").HandlerFunc(s.GetExplorerShard).Methods("GET") + s.router.Path("/shard").HandlerFunc(s.GetExplorerShard) // Do serving now. utils.GetLogInstance().Info("Listening on ", "port: ", GetExplorerPort(s.Port)) @@ -252,10 +258,24 @@ 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) { +// GetExplorerNodeCount serves /nodes end-point. +func (s *Service) GetExplorerNodeCount(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(s.GetNumPeers()) + json.NewEncoder(w).Encode(len(s.GetPeers())) +} + +// GetExplorerShard serves /shard end-point +func (s *Service) GetExplorerShard(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + var nodes []Node + for _, node := range s.GetPeers() { + nodes = append(nodes, Node{ + ID: node.SerializeToHexStr(), + }) + } + json.NewEncoder(w).Encode(Shard{ + Nodes: nodes, + }) } // NotifyService notify service diff --git a/api/service/explorer/structs.go b/api/service/explorer/structs.go index 62227fa57..0e6dc0925 100644 --- a/api/service/explorer/structs.go +++ b/api/service/explorer/structs.go @@ -57,6 +57,16 @@ type RefBlock struct { Height string `json:"height"` } +// Node ... +type Node struct { + ID string `json:"id"` +} + +// Shard ... +type Shard struct { + Nodes []Node `json:"nodes"` +} + // GetTransaction ... func GetTransaction(tx *types.Transaction, accountBlock *types.Block) *Transaction { if tx.To() == nil { diff --git a/consensus/consensus.go b/consensus/consensus.go index 5d4cca2fd..df48b5f05 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -637,9 +637,9 @@ 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) +// GetPeers returns PublicKeys +func (consensus *Consensus) GetPeers() []*bls.PublicKey { + return consensus.PublicKeys } // GetConsensusID returns the consensus ID diff --git a/node/node_handler.go b/node/node_handler.go index b8d11158c..b1da93d32 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -388,7 +388,7 @@ func (node *Node) pingMessageHandler(msgPayload []byte, sender string) int { node.ClientPeer = peer } else { node.AddPeers([]*p2p.Peer{peer}) - utils.GetLogInstance().Info("Add Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Peer", peer, "# Peers", node.Consensus.GetNumPeers()) + utils.GetLogInstance().Info("Add Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Peer", peer, "# Peers", len(node.Consensus.GetPeers())) } return 1 @@ -410,7 +410,7 @@ func (node *Node) SendPongMessage() { case <-tick.C: peers := node.Consensus.GetValidatorPeers() numPeersNow := len(peers) - numPubKeysNow := node.Consensus.GetNumPeers() + numPubKeysNow := len(node.Consensus.GetPeers()) // no peers, wait for another tick if numPeersNow == 0 || numPubKeysNow == 0 { diff --git a/node/service_setup.go b/node/service_setup.go index 4fd8fd033..0df8d76ca 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, node.NodeConfig.GetShardGroupID(), chanPeer, nil)) // Register explorer service. - node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.Consensus.GetNumPeers)) + node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.Consensus.GetPeers)) // Register consensus service. node.serviceManager.RegisterService(service.Consensus, consensus.New(node.BlockChannel, node.Consensus, node.startConsensus)) // Register new block service. @@ -70,7 +70,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.Consensus.GetNumPeers)) + node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.Consensus.GetPeers)) } func (node *Node) setupForBeaconValidator() {