Skip to content

Commit

Permalink
Merge pull request #710 from iotaledger/feat/add-network-health-route
Browse files Browse the repository at this point in the history
Breaking: Add network health route and flag
  • Loading branch information
muXxer authored Mar 20, 2024
2 parents 705ddcc + b6fcc35 commit 3e03300
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions api/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ type (
InfoResNodeStatus struct {
// Whether the node is healthy.
IsHealthy bool `serix:""`
// Whether the network is healthy (finalization is not delayed).
IsNetworkHealthy bool `serix:""`
// The accepted tangle time.
AcceptedTangleTime time.Time `serix:""`
// The relative accepted tangle time.
Expand Down
3 changes: 3 additions & 0 deletions api/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Test_CoreAPIDeSerialize(t *testing.T) {
Version: "2.0.0",
Status: &api.InfoResNodeStatus{
IsHealthy: false,
IsNetworkHealthy: false,
AcceptedTangleTime: time.Unix(1690879505, 0).UTC(),
RelativeAcceptedTangleTime: time.Unix(1690879505, 0).UTC(),
ConfirmedTangleTime: time.Unix(1690879505, 0).UTC(),
Expand Down Expand Up @@ -301,6 +302,7 @@ func Test_CoreAPIJSONSerialization(t *testing.T) {
Version: "2.0.0",
Status: &api.InfoResNodeStatus{
IsHealthy: false,
IsNetworkHealthy: false,
AcceptedTangleTime: time.Unix(1690879505, 0).UTC(),
RelativeAcceptedTangleTime: time.Unix(1690879505, 0).UTC(),
ConfirmedTangleTime: time.Unix(1690879505, 0).UTC(),
Expand Down Expand Up @@ -330,6 +332,7 @@ func Test_CoreAPIJSONSerialization(t *testing.T) {
"version": "2.0.0",
"status": {
"isHealthy": false,
"isNetworkHealthy": false,
"acceptedTangleTime": "1690879505000000000",
"relativeAcceptedTangleTime": "1690879505000000000",
"confirmedTangleTime": "1690879505000000000",
Expand Down
5 changes: 5 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ const (
// MIMEApplicationVendorIOTASerializerV2 => bytes.
CoreEndpointInfo = "/info"

// CoreEndpointNetworkHealth is the endpoint for getting the network health.
// GET returns http status code 200 if the network is healthy (finalization is not delayed).
CoreEndpointNetworkHealth = "/network/health"

// CoreEndpointNetworkMetrics is the endpoint for getting the network metrics.
// GET returns the network metrics.
// "Accept" header:
Expand Down Expand Up @@ -289,6 +293,7 @@ const (

var (
CoreRouteInfo = route(CorePluginName, CoreEndpointInfo)
CoreRouteNetworkHealth = route(CorePluginName, CoreEndpointNetworkHealth)
CoreRouteNetworkMetrics = route(CorePluginName, CoreEndpointNetworkMetrics)
CoreRouteBlocks = route(CorePluginName, CoreEndpointBlocks)
CoreRouteBlock = route(CorePluginName, CoreEndpointBlock)
Expand Down
14 changes: 14 additions & 0 deletions nodeclient/http_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ func (client *Client) Info(ctx context.Context) (*api.InfoResponse, error) {
return res, nil
}

// NetworkHealth returns whether the network is healthy (finalization is not delayed).
func (client *Client) NetworkHealth(ctx context.Context) (bool, error) {
//nolint:bodyclose
if _, err := client.Do(ctx, http.MethodGet, api.CoreRouteNetworkHealth, nil, nil); err != nil {
if ierrors.Is(err, ErrHTTPServiceUnavailable) {
return false, nil
}

return false, err
}

return true, nil
}

// NetworkMetrics gets the current network metrics.
func (client *Client) NetworkMetrics(ctx context.Context) (*api.NetworkMetricsResponse, error) {
res := new(api.NetworkMetricsResponse)
Expand Down
24 changes: 23 additions & 1 deletion nodeclient/http_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ func nodeClient(t *testing.T) *nodeclient.Client {

ts := time.Now()
originInfo := &api.InfoResponse{
Name: "HORNET",
Name: "iota-core",
Version: "1.0.0",
Status: &api.InfoResNodeStatus{
IsHealthy: true,
IsNetworkHealthy: true,
LatestAcceptedBlockSlot: tpkg.RandSlot(),
LatestConfirmedBlockSlot: tpkg.RandSlot(),
LatestFinalizedSlot: iotago.SlotIndex(142857),
Expand Down Expand Up @@ -163,6 +164,27 @@ func TestClient_Health(t *testing.T) {
require.False(t, healthy)
}

func TestClient_NetworkHealth(t *testing.T) {
defer gock.Off()

gock.New(nodeAPIUrl).
Get(api.CoreRouteNetworkHealth).
Reply(200)

nodeAPI := nodeClient(t)
healthy, err := nodeAPI.NetworkHealth(context.Background())
require.NoError(t, err)
require.True(t, healthy)

gock.New(nodeAPIUrl).
Get(api.CoreRouteNetworkHealth).
Reply(503)

healthy, err = nodeAPI.NetworkHealth(context.Background())
require.NoError(t, err)
require.False(t, healthy)
}

func TestClient_NetworkMetrics(t *testing.T) {
defer gock.Off()

Expand Down

0 comments on commit 3e03300

Please sign in to comment.