Skip to content

Commit

Permalink
Merge pull request #703 from iotaledger/feat/network-metrics-endpoint
Browse files Browse the repository at this point in the history
Add network metrics endpoint
  • Loading branch information
muXxer authored Mar 5, 2024
2 parents f6c86e8 + 44bdc04 commit dde4a52
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 62 deletions.
22 changes: 10 additions & 12 deletions api/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,6 @@ type (
Version string `serix:",lenPrefix=uint8"`
// The current status of this node.
Status *InfoResNodeStatus `serix:""`
// The metrics of this node.
Metrics *InfoResNodeMetrics `serix:""`
// The protocol parameters used by this node.
ProtocolParameters []*InfoResProtocolParameters `serix:",lenPrefix=uint8"`
// The base token of the network.
Expand Down Expand Up @@ -551,16 +549,6 @@ type (
PruningEpoch iotago.EpochIndex `serix:""`
}

// InfoResNodeMetrics defines the metrics of a node in the InfoResponse.
InfoResNodeMetrics struct {
// The current rate of new blocks per second, it's updated when a commitment is committed.
BlocksPerSecond float64 `serix:""`
// The current rate of confirmed blocks per second, it's updated when a commitment is committed.
ConfirmedBlocksPerSecond float64 `serix:""`
// The ratio of confirmed blocks in relation to new blocks up until the latest commitment is committed.
ConfirmationRate float64 `serix:""`
}

// InfoResBaseToken defines the base token of the node in the InfoResponse.
InfoResBaseToken struct {
// The base token name.
Expand All @@ -575,6 +563,16 @@ type (
Decimals uint32 `serix:""`
}

// NetworkMetricsResponse defines the network metrics response.
NetworkMetricsResponse struct {
// The current rate of new blocks per second, it's updated when a commitment is committed.
BlocksPerSecond float64 `serix:""`
// The current rate of confirmed blocks per second, it's updated when a commitment is committed.
ConfirmedBlocksPerSecond float64 `serix:""`
// The ratio of confirmed blocks in relation to new blocks up until the latest commitment is committed.
ConfirmationRate float64 `serix:""`
}

// IssuanceBlockHeaderResponse defines the response of a GET block issuance REST API call.
IssuanceBlockHeaderResponse struct {
// StrongParents are the strong parents of the block.
Expand Down
39 changes: 24 additions & 15 deletions api/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ func Test_CoreAPIDeSerialize(t *testing.T) {
LatestConfirmedBlockSlot: tpkg.RandSlot(),
PruningEpoch: tpkg.RandEpoch(),
},
Metrics: &api.InfoResNodeMetrics{
BlocksPerSecond: 1.1,
ConfirmedBlocksPerSecond: 2.2,
ConfirmationRate: 3.3,
},
ProtocolParameters: []*api.InfoResProtocolParameters{
{
StartEpoch: tpkg.RandEpoch(),
Expand All @@ -56,6 +51,17 @@ func Test_CoreAPIDeSerialize(t *testing.T) {
SeriErr: nil,
DeSeriErr: nil,
},
{
Name: "ok - NetworkMetricsResponse",
Source: &api.NetworkMetricsResponse{
BlocksPerSecond: 1.1,
ConfirmedBlocksPerSecond: 2.2,
ConfirmationRate: 3.3,
},
Target: &api.NetworkMetricsResponse{},
SeriErr: nil,
DeSeriErr: nil,
},
{
Name: "ok - IssuanceBlockHeaderResponse",
Source: &api.IssuanceBlockHeaderResponse{
Expand Down Expand Up @@ -304,11 +310,6 @@ func Test_CoreAPIJSONSerialization(t *testing.T) {
LatestConfirmedBlockSlot: 3,
PruningEpoch: 4,
},
Metrics: &api.InfoResNodeMetrics{
BlocksPerSecond: 1.1,
ConfirmedBlocksPerSecond: 2.2,
ConfirmationRate: 3.3,
},
ProtocolParameters: []*api.InfoResProtocolParameters{
{
StartEpoch: 0,
Expand Down Expand Up @@ -338,11 +339,6 @@ func Test_CoreAPIJSONSerialization(t *testing.T) {
"latestConfirmedBlockSlot": 3,
"pruningEpoch": 4
},
"metrics": {
"blocksPerSecond": "1.1E+00",
"confirmedBlocksPerSecond": "2.2E+00",
"confirmationRate": "3.3E+00"
},
"protocolParameters": [
{
"startEpoch": 0,
Expand Down Expand Up @@ -434,6 +430,19 @@ func Test_CoreAPIJSONSerialization(t *testing.T) {
"subunit": "glow",
"decimals": 6
}
}`,
},
{
Name: "ok - NetworkMetricsResponse",
Source: &api.NetworkMetricsResponse{
BlocksPerSecond: 1.1,
ConfirmedBlocksPerSecond: 2.2,
ConfirmationRate: 3.3,
},
Target: `{
"blocksPerSecond": "1.1",
"confirmedBlocksPerSecond": "2.2",
"confirmationRate": "3.3"
}`,
},
{
Expand Down
8 changes: 8 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ const (
// MIMEApplicationVendorIOTASerializerV2 => bytes.
CoreEndpointInfo = "/info"

// CoreEndpointNetworkMetrics is the endpoint for getting the network metrics.
// GET returns the network metrics.
// "Accept" header:
// MIMEApplicationJSON => json.
// MIMEApplicationVendorIOTASerializerV2 => bytes.
CoreEndpointNetworkMetrics = "/network/metrics"

// CoreEndpointBlocks is the endpoint for sending new blocks.
// POST sends a single new block and returns the new block ID.
// "Content-Type" header:
Expand Down Expand Up @@ -282,6 +289,7 @@ const (

var (
CoreRouteInfo = route(CorePluginName, CoreEndpointInfo)
CoreRouteNetworkMetrics = route(CorePluginName, CoreEndpointNetworkMetrics)
CoreRouteBlocks = route(CorePluginName, CoreEndpointBlocks)
CoreRouteBlock = route(CorePluginName, CoreEndpointBlock)
CoreRouteBlockMetadata = route(CorePluginName, CoreEndpointBlockMetadata)
Expand Down
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ go 1.22

require (
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/ethereum/go-ethereum v1.13.12
github.com/ethereum/go-ethereum v1.13.14
github.com/holiman/uint256 v1.2.4
github.com/iotaledger/hive.go/constraints v0.0.0-20240216135101-261e99d9d84a
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20240216135101-261e99d9d84a
github.com/iotaledger/hive.go/crypto v0.0.0-20240216135101-261e99d9d84a
github.com/iotaledger/hive.go/ierrors v0.0.0-20240216135101-261e99d9d84a
github.com/iotaledger/hive.go/lo v0.0.0-20240216135101-261e99d9d84a
github.com/iotaledger/hive.go/runtime v0.0.0-20240216135101-261e99d9d84a
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20240216135101-261e99d9d84a
github.com/iotaledger/hive.go/stringify v0.0.0-20240216135101-261e99d9d84a
github.com/iotaledger/hive.go/constraints v0.0.0-20240305162344-4b8be588e1cf
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20240305162344-4b8be588e1cf
github.com/iotaledger/hive.go/crypto v0.0.0-20240305162344-4b8be588e1cf
github.com/iotaledger/hive.go/ierrors v0.0.0-20240305162344-4b8be588e1cf
github.com/iotaledger/hive.go/lo v0.0.0-20240305162344-4b8be588e1cf
github.com/iotaledger/hive.go/runtime v0.0.0-20240305162344-4b8be588e1cf
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20240305162344-4b8be588e1cf
github.com/iotaledger/hive.go/stringify v0.0.0-20240305162344-4b8be588e1cf
github.com/iotaledger/iota-crypto-demo v0.0.0-20240216103559-27ca8dffd1e7
github.com/pasztorpisti/qs v0.0.0-20171216220353-8d6c33ee906c
github.com/samber/lo v1.39.0
Expand All @@ -30,7 +30,7 @@ require (
github.com/gorilla/websocket v1.5.1 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/iotaledger/hive.go/ds v0.0.0-20240216135101-261e99d9d84a // indirect
github.com/iotaledger/hive.go/ds v0.0.0-20240305162344-4b8be588e1cf // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
40 changes: 20 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etly
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
github.com/ethereum/go-ethereum v1.13.12 h1:iDr9UM2JWkngBHGovRJEQn4Kor7mT4gt9rUZqB5M29Y=
github.com/ethereum/go-ethereum v1.13.12/go.mod h1:hKL2Qcj1OvStXNSEDbucexqnEt1Wh4Cz329XsjAalZY=
github.com/ethereum/go-ethereum v1.13.14 h1:EwiY3FZP94derMCIam1iW4HFVrSgIcpsu0HwTQtm6CQ=
github.com/ethereum/go-ethereum v1.13.14/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
Expand All @@ -25,24 +25,24 @@ github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU
github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/iotaledger/hive.go/constraints v0.0.0-20240216135101-261e99d9d84a h1:9xeffzciYdw9L/ebAOPg/39Bqn3p5iyOROQhQHaAEUs=
github.com/iotaledger/hive.go/constraints v0.0.0-20240216135101-261e99d9d84a/go.mod h1:JF7jjkL6tSUOXm23SWadBzBrl7eJk1DQRLc/fNoVZ+o=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20240216135101-261e99d9d84a h1:+l3NjL4v700Iv+ZF7KjPALkygFOxzkByoRIoSK9bOzc=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20240216135101-261e99d9d84a/go.mod h1:9HM/YmzOfdlWXYHVEWVjYhKQHOlgxBVMsr5Vf5yn5IE=
github.com/iotaledger/hive.go/crypto v0.0.0-20240216135101-261e99d9d84a h1:ux81+J6mcxYw6usZpEPLZjE6J+TNs3RaRcTHa9+5ERM=
github.com/iotaledger/hive.go/crypto v0.0.0-20240216135101-261e99d9d84a/go.mod h1:7kZh98nwJInQsIybiPXSABc/on/IsDdXyeiPLfaH+2I=
github.com/iotaledger/hive.go/ds v0.0.0-20240216135101-261e99d9d84a h1:7M1+k0H53jahg2ZDN10SociepCB+jymIZoiYaSenpbQ=
github.com/iotaledger/hive.go/ds v0.0.0-20240216135101-261e99d9d84a/go.mod h1:sE/HabIH9FWTvgaHioLN6o4rrxFMf4BuqRQwloxcamo=
github.com/iotaledger/hive.go/ierrors v0.0.0-20240216135101-261e99d9d84a h1:ugcQ43xbMZo5XYTfanIHOxQGKNpxhcatcz9ZXcX6W1o=
github.com/iotaledger/hive.go/ierrors v0.0.0-20240216135101-261e99d9d84a/go.mod h1:GQY0/35sjgT9Poi1Vrs9kFVvAkuKzGXfVh4j6CBXsAA=
github.com/iotaledger/hive.go/lo v0.0.0-20240216135101-261e99d9d84a h1:eUAtyEIYRlAPeZ2qhlEA+7T8DVoYK2h9eotlst/84SU=
github.com/iotaledger/hive.go/lo v0.0.0-20240216135101-261e99d9d84a/go.mod h1:0ycE1W59kkNjQ87odL6H4x5Ik9bXhbNk88wmo+mIYt0=
github.com/iotaledger/hive.go/runtime v0.0.0-20240216135101-261e99d9d84a h1:qmfoYLK2XyPOXfldG21qHyCrbIf0sl7iJoFirvTrnO0=
github.com/iotaledger/hive.go/runtime v0.0.0-20240216135101-261e99d9d84a/go.mod h1:lAR3vbqE9g65M9VwWHiaXab2q+d89dBOFjhKgnDfK7c=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20240216135101-261e99d9d84a h1:ahUFo0X9Th+8/aE6KSWiy7Eap5p0YFL6CDapP1o8dLo=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20240216135101-261e99d9d84a/go.mod h1:yXQNGZUz++dB1T9RAHhWSresuTLzTC856ntdins9ivo=
github.com/iotaledger/hive.go/stringify v0.0.0-20240216135101-261e99d9d84a h1:xLreq/rXeSPdb86RnZNEPH3PUIWt56BQxK1+11+hM4I=
github.com/iotaledger/hive.go/stringify v0.0.0-20240216135101-261e99d9d84a/go.mod h1:O4p7UmsfoeLqtAUwrKbq0lXMxjY/MLQSpZSavvvvGig=
github.com/iotaledger/hive.go/constraints v0.0.0-20240305162344-4b8be588e1cf h1:SHtB0Nf1MdOAk+adIQfQGWZ++EcvrfFE4kCTMbZ7Qys=
github.com/iotaledger/hive.go/constraints v0.0.0-20240305162344-4b8be588e1cf/go.mod h1:JF7jjkL6tSUOXm23SWadBzBrl7eJk1DQRLc/fNoVZ+o=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20240305162344-4b8be588e1cf h1:CxqQWH3cHX+jUx94zDeeCIe93gmygem1Y5jMrcAWJA4=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20240305162344-4b8be588e1cf/go.mod h1:73ODTHUJxAoGUN0InOtuQ29LVq85pDf7GckM9d0ziqo=
github.com/iotaledger/hive.go/crypto v0.0.0-20240305162344-4b8be588e1cf h1:msheCEl+Q1y8LQjfP9VOf6t38LeCOndhHkuw3AgMfK8=
github.com/iotaledger/hive.go/crypto v0.0.0-20240305162344-4b8be588e1cf/go.mod h1:aREIB19gIhSYdY0Hl/37sg1JRH7+j3ajeJxBIQf6mig=
github.com/iotaledger/hive.go/ds v0.0.0-20240305162344-4b8be588e1cf h1:TyGz03gRMdFECQVc0gExs9W1k4lB10MPeRQohNilnmk=
github.com/iotaledger/hive.go/ds v0.0.0-20240305162344-4b8be588e1cf/go.mod h1:wfjeJj9B+MM/3yeUHfvT8Gj8bRsdl9utyh2dZg+1+B0=
github.com/iotaledger/hive.go/ierrors v0.0.0-20240305162344-4b8be588e1cf h1:aBMpx2sHnG7Esai4S7b47aUERBTV+JgoSjJKT1U4LxE=
github.com/iotaledger/hive.go/ierrors v0.0.0-20240305162344-4b8be588e1cf/go.mod h1:GQY0/35sjgT9Poi1Vrs9kFVvAkuKzGXfVh4j6CBXsAA=
github.com/iotaledger/hive.go/lo v0.0.0-20240305162344-4b8be588e1cf h1:ADrQgsSpsoMSfIlSsMHo66ush8y0z8AaFn0Nae5dztY=
github.com/iotaledger/hive.go/lo v0.0.0-20240305162344-4b8be588e1cf/go.mod h1:67oLzWYiBLGt5PN7IBVHdbt9P6oBYCx9UvMEL8ExDAc=
github.com/iotaledger/hive.go/runtime v0.0.0-20240305162344-4b8be588e1cf h1:gELL3pOfJADq6kLC6HkbEtFwAiVOm1cPuVy92noqjPk=
github.com/iotaledger/hive.go/runtime v0.0.0-20240305162344-4b8be588e1cf/go.mod h1:pueoYXud+HmTY2x9j/S6+ZX3M5ZyENFKPDrx3EtcwWs=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20240305162344-4b8be588e1cf h1:PAwjwceOCwCclmvCzbSJb0A7EolaDJsjWqw0ixRys0M=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20240305162344-4b8be588e1cf/go.mod h1:NK05G4PxwZF1m4jGANJWLhAQ2hP1Nt0L8mgCTFLsSCw=
github.com/iotaledger/hive.go/stringify v0.0.0-20240305162344-4b8be588e1cf h1:gLQTQtZZTwZBFZdx0tuM7WHpWtF1liC3c6FflZiNGXc=
github.com/iotaledger/hive.go/stringify v0.0.0-20240305162344-4b8be588e1cf/go.mod h1:O4p7UmsfoeLqtAUwrKbq0lXMxjY/MLQSpZSavvvvGig=
github.com/iotaledger/iota-crypto-demo v0.0.0-20240216103559-27ca8dffd1e7 h1:t6k4MqiUov0FrBb2o2JhKlOVSdlPbIQWM8ivYHL0G0g=
github.com/iotaledger/iota-crypto-demo v0.0.0-20240216103559-27ca8dffd1e7/go.mod h1:do+N3LpeDEi9qselEC4XcjqGoRc7cWGiqBtIeBOKEMs=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
12 changes: 12 additions & 0 deletions nodeclient/http_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ func (client *Client) Info(ctx context.Context) (*api.InfoResponse, error) {
return res, nil
}

// NetworkMetrics gets the current network metrics.
func (client *Client) NetworkMetrics(ctx context.Context) (*api.NetworkMetricsResponse, error) {
res := new(api.NetworkMetricsResponse)

//nolint:bodyclose
if _, err := do(ctx, iotago.CommonSerixAPI(), client.opts.httpClient, client.BaseURL, client.opts.userInfo, http.MethodGet, api.CoreRouteNetworkMetrics, client.opts.requestURLHook, nil, nil, res); err != nil {
return nil, err
}

return res, nil
}

// BlockIssuance gets the info to issue a block.
func (client *Client) BlockIssuance(ctx context.Context) (*api.IssuanceBlockHeaderResponse, error) {
res := new(api.IssuanceBlockHeaderResponse)
Expand Down
22 changes: 17 additions & 5 deletions nodeclient/http_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ func nodeClient(t *testing.T) *nodeclient.Client {
Subunit: "testies",
Decimals: 6,
},
Metrics: &api.InfoResNodeMetrics{
BlocksPerSecond: 20.0,
ConfirmedBlocksPerSecond: 10.0,
ConfirmationRate: 50.0,
},
}

mockGetJSON(api.CoreRouteInfo, 200, originInfo)
Expand All @@ -147,6 +142,23 @@ func nodeClient(t *testing.T) *nodeclient.Client {
return client
}

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

originMetrics := &api.NetworkMetricsResponse{
BlocksPerSecond: 20.0,
ConfirmedBlocksPerSecond: 10.0,
ConfirmationRate: 50.0,
}

mockGetJSON(api.CoreRouteNetworkMetrics, 200, originMetrics)

nodeAPI := nodeClient(t)
metrics, err := nodeAPI.NetworkMetrics(context.Background())
require.NoError(t, err)
require.EqualValues(t, originMetrics, metrics)
}

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

Expand Down

0 comments on commit dde4a52

Please sign in to comment.