Skip to content

Commit

Permalink
add sync status api in rpc and restful interface (#1129)
Browse files Browse the repository at this point in the history
* add syncstatus in rpc

* add restful interface
  • Loading branch information
lucas7788 authored and laizy committed Nov 21, 2019
1 parent f34884e commit e19b743
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 2 deletions.
18 changes: 18 additions & 0 deletions http/base/actor/net_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ func GetConnectionCnt() (uint32, error) {
return r.Cnt, nil
}

//GetMaxPeerBlockHeight from netSever actor
func GetMaxPeerBlockHeight() (uint64, error) {
if netServerPid == nil {
return 1, nil
}
future := netServerPid.RequestFuture(&ac.GetMaxPeerBlockHeightReq{}, REQ_TIMEOUT*time.Second)
result, err := future.Result()
if err != nil {
log.Errorf(ERR_ACTOR_COMM, err)
return 0, err
}
r, ok := result.(*ac.GetMaxPeerBlockHeightRsp)
if !ok {
return 0, errors.New("fail")
}
return r.MaxPeerBlockHeight, nil
}

//GetNeighborAddrs from netSever actor
func GetNeighborAddrs() []common.PeerAddr {
if netServerPid == nil {
Expand Down
25 changes: 25 additions & 0 deletions http/base/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,28 @@ func GetAddress(str string) (common.Address, error) {
}
return address, err
}

type SyncStatus struct {
CurrentBlockHeight uint32
ConnectCount uint32
MaxPeerBlockHeight uint64
}

func GetSyncStatus() (SyncStatus, error) {
var status SyncStatus
height, err := bactor.GetMaxPeerBlockHeight()
if err != nil {
return status, err
}
cnt, err := bactor.GetConnectionCnt()
if err != nil {
return status, err
}
curBlockHeight := bactor.GetCurrentBlockHeight()

return SyncStatus{
CurrentBlockHeight: curBlockHeight,
ConnectCount: cnt,
MaxPeerBlockHeight: height,
}, nil
}
10 changes: 10 additions & 0 deletions http/base/rest/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ func GetConnectionCount(cmd map[string]interface{}) map[string]interface{} {
return resp
}

func GetNodeSyncStatus(cmd map[string]interface{}) map[string]interface{} {
resp := ResponsePack(berr.SUCCESS)
status, err := bcomn.GetSyncStatus()
if err != nil {
return ResponsePack(berr.INTERNAL_ERROR)
}
resp["Result"] = status
return resp
}

//get block height
func GetBlockHeight(cmd map[string]interface{}) map[string]interface{} {
resp := ResponsePack(berr.SUCCESS)
Expand Down
11 changes: 11 additions & 0 deletions http/base/rpc/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ func GetConnectionCount(params []interface{}) map[string]interface{} {
return responseSuccess(count)
}

//get node connection most height
func GetSyncStatus(params []interface{}) map[string]interface{} {
status, err := bcomn.GetSyncStatus()
if err != nil {
log.Errorf("GetSyncStatus error:%s", err)
return responsePack(berr.INTERNAL_ERROR, false)
}

return responseSuccess(status)
}

func GetRawMemPool(params []interface{}) map[string]interface{} {
txs := []*bcomn.Transactions{}
txpool := bactor.GetTxsFromPool(false)
Expand Down
1 change: 1 addition & 0 deletions http/jsonrpc/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func StartRPCServer() error {
rpc.HandleFunc("getblockcount", rpc.GetBlockCount)
rpc.HandleFunc("getblockhash", rpc.GetBlockHash)
rpc.HandleFunc("getconnectioncount", rpc.GetConnectionCount)
rpc.HandleFunc("getsyncstatus", rpc.GetSyncStatus)
//HandleFunc("getrawmempool", GetRawMemPool)

rpc.HandleFunc("getrawtransaction", rpc.GetRawTransaction)
Expand Down
2 changes: 2 additions & 0 deletions http/restful/restful/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type restServer struct {

const (
GET_CONN_COUNT = "/api/v1/node/connectioncount"
GET_SYNC_STATUS = "/api/v1/node/syncstatus"
GET_BLK_TXS_BY_HEIGHT = "/api/v1/block/transactions/height/:height"
GET_BLK_BY_HEIGHT = "/api/v1/block/details/height/:height"
GET_BLK_BY_HASH = "/api/v1/block/details/hash/:hash"
Expand Down Expand Up @@ -134,6 +135,7 @@ func (this *restServer) registryMethod() {

getMethodMap := map[string]Action{
GET_CONN_COUNT: {name: "getconnectioncount", handler: rest.GetConnectionCount},
GET_SYNC_STATUS: {name: "getsyncstatus", handler: rest.GetNodeSyncStatus},
GET_BLK_TXS_BY_HEIGHT: {name: "getblocktxsbyheight", handler: rest.GetBlockTxsByHeight},
GET_BLK_BY_HEIGHT: {name: "getblockbyheight", handler: rest.GetBlockByHeight},
GET_BLK_BY_HASH: {name: "getblockbyhash", handler: rest.GetBlockByHash},
Expand Down
13 changes: 13 additions & 0 deletions p2pserver/actor/server/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func (this *P2PActor) Receive(ctx actor.Context) {
this.handleGetVersionReq(ctx, msg)
case *GetConnectionCntReq:
this.handleGetConnectionCntReq(ctx, msg)
case *GetMaxPeerBlockHeightReq:
this.handleGetMaxPeerBlockHeightReq(ctx, msg)
case *GetIdReq:
this.handleGetIDReq(ctx, msg)
case *GetConnectionStateReq:
Expand Down Expand Up @@ -140,6 +142,17 @@ func (this *P2PActor) handleGetConnectionCntReq(ctx actor.Context, req *GetConne
}
}

//max peer blockheight handler
func (this *P2PActor) handleGetMaxPeerBlockHeightReq(ctx actor.Context, req *GetMaxPeerBlockHeightReq) {
height := this.server.GetMaxPeerBlockHeight()
if ctx.Sender() != nil {
resp := &GetMaxPeerBlockHeightRsp{
MaxPeerBlockHeight: height,
}
ctx.Sender().Request(resp, ctx.Self())
}
}

//get id handler
func (this *P2PActor) handleGetIDReq(ctx actor.Context, req *GetIdReq) {
id := this.server.GetID()
Expand Down
13 changes: 11 additions & 2 deletions p2pserver/actor/server/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,24 @@ type GetVersionRsp struct {
Version uint32
}

//connection count requet
//connection count request
type GetConnectionCntReq struct {
}

//response of connection count requet
//response of connection count request
type GetConnectionCntRsp struct {
Cnt uint32
}

//request of max peer block height
type GetMaxPeerBlockHeightReq struct {
}

//response of max peer block height
type GetMaxPeerBlockHeightRsp struct {
MaxPeerBlockHeight uint64
}

//get net module id
type GetIdReq struct {
}
Expand Down
5 changes: 5 additions & 0 deletions p2pserver/net/netserver/netserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ func (this *NetServer) GetConnectionCnt() uint32 {
return this.Np.GetNbrNodeCnt()
}

//GetMaxPeerBlockHeight return the most height of valid connections
func (this *NetServer) GetMaxPeerBlockHeight() uint64 {
return this.Np.GetNeighborMostHeight()
}

//AddNbrNode add peer to nbr peer list
func (this *NetServer) AddNbrNode(remotePeer *peer.Peer) {
this.Np.AddNbrNode(remotePeer)
Expand Down
1 change: 1 addition & 0 deletions p2pserver/net/protocol/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type P2P interface {
GetNeighbors() []*peer.Peer
GetNeighborAddrs() []common.PeerAddr
GetConnectionCnt() uint32
GetMaxPeerBlockHeight() uint64
GetNp() *peer.NbrPeers
GetPeer(uint64) *peer.Peer
SetHeight(uint64)
Expand Down
5 changes: 5 additions & 0 deletions p2pserver/p2pserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func (this *P2PServer) GetConnectionCnt() uint32 {
return this.network.GetConnectionCnt()
}

//GetMaxPeerBlockHeight return the established connect count
func (this *P2PServer) GetMaxPeerBlockHeight() uint64 {
return this.network.GetMaxPeerBlockHeight()
}

//Start create all services
func (this *P2PServer) Start() error {
if this.network != nil {
Expand Down
16 changes: 16 additions & 0 deletions p2pserver/peer/nbr_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ func (this *NbrPeers) GetNeighborHeights() map[uint64]uint64 {
return hm
}

//GetNeighborMostHeight return the most height of nbr peers
func (this *NbrPeers) GetNeighborMostHeight() uint64 {
this.RLock()
defer this.RUnlock()
mostHeight := uint64(0)
for _, n := range this.List {
if n.GetState() == common.ESTABLISH {
height := n.GetHeight()
if mostHeight < height {
mostHeight = height
}
}
}
return mostHeight
}

//GetNeighbors return all establish peers in nbr list
func (this *NbrPeers) GetNeighbors() []*Peer {
this.RLock()
Expand Down

0 comments on commit e19b743

Please sign in to comment.