From dac42117ee57baea807a58810210cfd91d645697 Mon Sep 17 00:00:00 2001 From: Edgar Aroutiounian Date: Fri, 27 Sep 2019 14:14:48 -0700 Subject: [PATCH] [RPC] Expose if current node leader (#1677) --- hmy/api_backend.go | 5 +++++ hmy/backend.go | 1 + internal/hmyapi/backend.go | 1 + internal/hmyapi/harmony.go | 2 ++ node/rpc.go | 6 +++++- 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/hmy/api_backend.go b/hmy/api_backend.go index cfe087a05..2f933558c 100644 --- a/hmy/api_backend.go +++ b/hmy/api_backend.go @@ -275,3 +275,8 @@ func (b *APIBackend) ResendCx(ctx context.Context, txID common.Hash) (uint64, bo success := b.hmy.CxPool().Add(entry) return blockNum, success } + +// IsLeader exposes if node is currently leader +func (b *APIBackend) IsLeader() bool { + return b.hmy.nodeAPI.IsCurrentlyLeader() +} diff --git a/hmy/backend.go b/hmy/backend.go index a5609c63a..0bf6a8b23 100644 --- a/hmy/backend.go +++ b/hmy/backend.go @@ -49,6 +49,7 @@ type NodeAPI interface { GetBalanceOfAddress(address common.Address) (*big.Int, error) GetNonceOfAddress(address common.Address) uint64 GetTransactionsHistory(address string) ([]common.Hash, error) + IsCurrentlyLeader() bool } // New creates a new Harmony object (including the diff --git a/internal/hmyapi/backend.go b/internal/hmyapi/backend.go index f0a719f1d..70ecb53b6 100644 --- a/internal/hmyapi/backend.go +++ b/internal/hmyapi/backend.go @@ -71,6 +71,7 @@ type Backend interface { GetTransactionsHistory(address string) ([]common.Hash, error) // retrieve the blockHash using txID and add blockHash to CxPool for resending ResendCx(ctx context.Context, txID common.Hash) (uint64, bool) + IsLeader() bool } // GetAPIs returns all the APIs. diff --git a/internal/hmyapi/harmony.go b/internal/hmyapi/harmony.go index 4e0a57f89..f5c7526b9 100644 --- a/internal/hmyapi/harmony.go +++ b/internal/hmyapi/harmony.go @@ -49,6 +49,7 @@ type NodeMetadata struct { Version string `json:"version"` NetworkType string `json:"network"` ChainID string `json:"chainid"` + IsLeader bool `json:"is-leader"` } // GetNodeMetadata produces a NodeMetadata record. Note the data is from the answering RPC @@ -59,5 +60,6 @@ func (s *PublicHarmonyAPI) GetNodeMetadata() NodeMetadata { nodeconfig.GetVersion(), string(cfg.GetNetworkType()), s.b.ChainConfig().ChainID.String(), + s.b.IsLeader(), } } diff --git a/node/rpc.go b/node/rpc.go index c28192a2b..1877b21ef 100644 --- a/node/rpc.go +++ b/node/rpc.go @@ -44,6 +44,11 @@ var ( harmony *hmy.Harmony ) +// IsCurrentlyLeader exposes if node is currently the leader node +func (node *Node) IsCurrentlyLeader() bool { + return node.Consensus.IsLeader() +} + // StartRPC start RPC service func (node *Node) StartRPC(nodePort string) error { // Gather all the possible APIs to surface @@ -66,7 +71,6 @@ func (node *Node) StartRPC(nodePort string) error { if err := node.startHTTP(httpEndpoint, apis, httpModules, httpOrigins, httpVirtualHosts, httpTimeouts); err != nil { return err } - wsEndpoint = fmt.Sprintf("%v:%v", ip, port+rpcWSPortOffset) if err := node.startWS(wsEndpoint, apis, wsModules, wsOrigins, true); err != nil { node.stopHTTP()