add api /shard for explorer. (#667)

* update balance output

* add /shard API.
pull/673/head
Richard Liu 6 years ago committed by GitHub
parent c55cd1bdeb
commit c0b691cefc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      api/service/explorer/service.go
  2. 10
      api/service/explorer/structs.go
  3. 6
      consensus/consensus.go
  4. 4
      node/node_handler.go
  5. 4
      node/service_setup.go

@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/harmony-one/bls/ffi/go/bls"
msg_pb "github.com/harmony-one/harmony/api/proto/message" msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
@ -27,18 +28,18 @@ type Service struct {
router *mux.Router router *mux.Router
IP string IP string
Port string Port string
GetNumPeers func() int GetPeers func() []*bls.PublicKey
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, GetNumPeers func() int) *Service { func New(selfPeer *p2p.Peer, GetPeers func() []*bls.PublicKey) *Service {
return &Service{ return &Service{
IP: selfPeer.IP, IP: selfPeer.IP,
Port: selfPeer.Port, Port: selfPeer.Port,
GetNumPeers: GetNumPeers, 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").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. // Set up router for node count.
s.router.Path("/nodes").HandlerFunc(s.GetExplorerNodes) 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. // Do serving now.
utils.GetLogInstance().Info("Listening on ", "port: ", GetExplorerPort(s.Port)) 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) json.NewEncoder(w).Encode(data.Address)
} }
// GetExplorerNodes serves /nodes end-point. // GetExplorerNodeCount serves /nodes end-point.
func (s *Service) GetExplorerNodes(w http.ResponseWriter, r *http.Request) { func (s *Service) GetExplorerNodeCount(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") 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 // NotifyService notify service

@ -57,6 +57,16 @@ type RefBlock struct {
Height string `json:"height"` Height string `json:"height"`
} }
// Node ...
type Node struct {
ID string `json:"id"`
}
// Shard ...
type Shard struct {
Nodes []Node `json:"nodes"`
}
// GetTransaction ... // GetTransaction ...
func GetTransaction(tx *types.Transaction, accountBlock *types.Block) *Transaction { func GetTransaction(tx *types.Transaction, accountBlock *types.Block) *Transaction {
if tx.To() == nil { if tx.To() == nil {

@ -637,9 +637,9 @@ func (consensus *Consensus) GetLeaderPubKey() *bls.PublicKey {
return consensus.leader.ConsensusPubKey return consensus.leader.ConsensusPubKey
} }
// GetNumPeers returns the length of PublicKeys // GetPeers returns PublicKeys
func (consensus *Consensus) GetNumPeers() int { func (consensus *Consensus) GetPeers() []*bls.PublicKey {
return len(consensus.PublicKeys) return consensus.PublicKeys
} }
// GetConsensusID returns the consensus ID // GetConsensusID returns the consensus ID

@ -388,7 +388,7 @@ func (node *Node) pingMessageHandler(msgPayload []byte, sender string) int {
node.ClientPeer = peer node.ClientPeer = peer
} else { } else {
node.AddPeers([]*p2p.Peer{peer}) 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 return 1
@ -410,7 +410,7 @@ func (node *Node) SendPongMessage() {
case <-tick.C: case <-tick.C:
peers := node.Consensus.GetValidatorPeers() peers := node.Consensus.GetValidatorPeers()
numPeersNow := len(peers) numPeersNow := len(peers)
numPubKeysNow := node.Consensus.GetNumPeers() numPubKeysNow := len(node.Consensus.GetPeers())
// no peers, wait for another tick // no peers, wait for another tick
if numPeersNow == 0 || numPubKeysNow == 0 { if numPeersNow == 0 || numPubKeysNow == 0 {

@ -26,7 +26,7 @@ func (node *Node) setupForShardLeader() {
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, node.NodeConfig.GetShardGroupID(), chanPeer, nil)) node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, node.NodeConfig.GetShardGroupID(), chanPeer, nil))
// Register explorer service. // 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. // 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.
@ -70,7 +70,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.Consensus.GetNumPeers)) node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.Consensus.GetPeers))
} }
func (node *Node) setupForBeaconValidator() { func (node *Node) setupForBeaconValidator() {

Loading…
Cancel
Save