Skip to content

Commit

Permalink
Merge pull request #177 from NodeFactoryIo/mmuftic/target-block-metric
Browse files Browse the repository at this point in the history
Add target block metric
  • Loading branch information
mpetrunic authored Jan 21, 2021
2 parents f3ce3a6 + 64c70d2 commit da3cd39
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[Full Changelog](https://github.com/NodeFactoryIo/vedran/compare/v0.4.2...HEAD)

### Added
- Add target block metric [\#177](https://github.com/NodeFactoryIo/vedran/pull/177) ([MakMuftic](https://github.com/MakMuftic))

### Fix
- Fix min TLS version [\#175](https://github.com/NodeFactoryIo/vedran/pull/175) ([MakMuftic](https://github.com/MakMuftic))
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
container_name: "vedran"

vedran-daemon:
image: nodefactory/vedran-daemon:v0.3.0
image: nodefactory/vedran-daemon:v0.3.2
depends_on:
- vedran
- polkadot
Expand Down
9 changes: 9 additions & 0 deletions internal/active/active.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ func CheckIfMetricsValid(nodeID string, repos *repositories.Repos) (bool, error)
if err != nil {
return false, err
}
// check if node synced
if metrics.BestBlockHeight != metrics.TargetBlockHeight {
log.Debugf(
"Node %s not synced. Best block: %d, Target block: %d",
nodeID, metrics.BestBlockHeight, metrics.TargetBlockHeight,
)
return false, nil
}

latestBlockMetrics, err := repos.MetricsRepo.GetLatestBlockMetrics()
if err != nil {
return false, err
Expand Down
33 changes: 33 additions & 0 deletions internal/active/active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestCheckIfNodeActive(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
nodeMetricsError: nil,
Expand All @@ -60,6 +61,32 @@ func TestCheckIfNodeActive(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
nodeMetricsError: nil,
latestMetrics: &models.LatestBlockMetrics{
BestBlockHeight: 0,
FinalizedBlockHeight: 0,
},
latestMetricsError: nil,
expectedResult: false,
expectedError: nil,
},
{
name: "not active node::node not synced",
node: models.Node{ID: "1"},
nodePing: &models.Ping{
NodeId: "1",
Timestamp: time.Now(),
},
nodePingError: nil,
nodeMetrics: &models.Metrics{
NodeId: "1",
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1300,
ReadyTransactionCount: 0,
},
nodeMetricsError: nil,
Expand All @@ -84,6 +111,7 @@ func TestCheckIfNodeActive(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
nodeMetricsError: nil,
Expand All @@ -108,6 +136,7 @@ func TestCheckIfNodeActive(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 994,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
nodeMetricsError: nil,
Expand All @@ -132,6 +161,7 @@ func TestCheckIfNodeActive(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
nodeMetricsError: nil,
Expand All @@ -155,6 +185,7 @@ func TestCheckIfNodeActive(t *testing.T) {
NodeId: "1",
PeerCount: 0,
BestBlockHeight: 1000,
TargetBlockHeight: 1000,
FinalizedBlockHeight: 995,
ReadyTransactionCount: 0,
},
Expand All @@ -180,6 +211,7 @@ func TestCheckIfNodeActive(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
nodeMetricsError: errors.New("metrics-error"),
Expand All @@ -204,6 +236,7 @@ func TestCheckIfNodeActive(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
nodeMetricsError: nil,
Expand Down
2 changes: 2 additions & 0 deletions internal/controllers/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type MetricsRequest struct {
PeerCount int32 `json:"peer_count"`
BestBlockHeight int64 `json:"best_block_height"`
FinalizedBlockHeight int64 `json:"finalized_block_height"`
TargetBlockHeight int64 `json:"target_block_height"`
ReadyTransactionCount int32 `json:"ready_transaction_count"`
}

Expand Down Expand Up @@ -41,6 +42,7 @@ func (c ApiController) SaveMetricsHandler(w http.ResponseWriter, r *http.Request
PeerCount: metricsRequest.PeerCount,
BestBlockHeight: metricsRequest.BestBlockHeight,
FinalizedBlockHeight: metricsRequest.FinalizedBlockHeight,
TargetBlockHeight: metricsRequest.TargetBlockHeight,
ReadyTransactionCount: metricsRequest.ReadyTransactionCount,
})

Expand Down
48 changes: 48 additions & 0 deletions internal/controllers/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestApiController_SaveMetricsHandler(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
metricsRepoFindByIDError: nil,
Expand All @@ -92,6 +93,7 @@ func TestApiController_SaveMetricsHandler(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
nodeId: "1",
Expand Down Expand Up @@ -123,6 +125,7 @@ func TestApiController_SaveMetricsHandler(t *testing.T) {
PeerCount: 10,
BestBlockHeight: 100,
FinalizedBlockHeight: 100,
TargetBlockHeight: 100,
ReadyTransactionCount: 10,
},
nodeId: "1",
Expand Down Expand Up @@ -154,6 +157,7 @@ func TestApiController_SaveMetricsHandler(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
nodeId: "1",
Expand All @@ -173,6 +177,7 @@ func TestApiController_SaveMetricsHandler(t *testing.T) {
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
ReadyTransactionCount: 0,
},
metricsRepoFindByIDError: nil,
Expand All @@ -188,6 +193,48 @@ func TestApiController_SaveMetricsHandler(t *testing.T) {
metricsRepoSaveError: nil,
metricsRepoSaveNumOfCalls: 1,
},
{
name: "Valid metrics save request and node should not be added to active nodes as node is not synced",
metricsRequest: MetricsRequest{
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1400,
ReadyTransactionCount: 0,
},
nodeId: "1",
httpStatus: http.StatusOK,
// NodeRepo.FindByID
nodeRepoIsNodeOnCooldownReturns: false,
nodeRepoIsNodeOnCooldownError: nil,
nodeRepoIsNodeOnNumOfCalls: 1,
// NodeRepo.AddNodeToActive
nodeRepoAddNodeToActiveError: nil,
nodeRepoAddNodeToActiveNumOfCalls: 0,
// NodeRepo.IsNodeActive
nodeRepoIsNodeActiveReturn: false,
// MetricsRepo.FindByID
metricsRepoFindByIDReturn: &models.Metrics{
NodeId: "1",
PeerCount: 0,
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1400,
ReadyTransactionCount: 0,
},
metricsRepoFindByIDError: nil,
metricsRepoFindByIDNumOfCalls: 1,
// MetricsRepo.GetLatestBlockMetrics
metricsRepoGetLatestBlockMetricsReturn: &models.LatestBlockMetrics{
BestBlockHeight: 1000,
FinalizedBlockHeight: 999,
},
metricsRepoGetLatestBlockMetricsError: nil,
metricsRepoGetLatestBlockMetricsNumOfCalls: 0,
// MetricsRepo.Save
metricsRepoSaveError: nil,
metricsRepoSaveNumOfCalls: 1,
},
{
name: "Invalid metrics save request",
metricsRequest: struct{ PeerCount string }{PeerCount: "10"},
Expand All @@ -200,6 +247,7 @@ func TestApiController_SaveMetricsHandler(t *testing.T) {
PeerCount: 10,
BestBlockHeight: 100,
FinalizedBlockHeight: 100,
TargetBlockHeight: 100,
ReadyTransactionCount: 10,
},
nodeId: "1",
Expand Down
1 change: 1 addition & 0 deletions internal/models/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Metrics struct {
PeerCount int32
BestBlockHeight int64
FinalizedBlockHeight int64
TargetBlockHeight int64
ReadyTransactionCount int32
}

Expand Down
4 changes: 2 additions & 2 deletions internal/schedule/checkactive/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func scheduledTask(repos *repositories.Repos, actions actions.Actions) {
continue
}

metricsVald, err := active.CheckIfMetricsValid(node.ID, repos)
metricsValid, err := active.CheckIfMetricsValid(node.ID, repos)
if err != nil {
log.Errorf("Unable to check if node %s active because of %v", node.ID, err)
continue
}

if !metricsVald {
if !metricsValid {
err = repos.NodeRepo.RemoveNodeFromActive(node.ID)
if err != nil {
log.Errorf("Unable to remove node %s from active because of %v", node.ID, err)
Expand Down
11 changes: 11 additions & 0 deletions internal/schedule/checkactive/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Test_scheduledTask(t *testing.T) {
NodeId: "",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
},
latestMetrics: []*models.LatestBlockMetrics{
Expand Down Expand Up @@ -103,26 +104,31 @@ func Test_scheduledTask(t *testing.T) {
NodeId: "1",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
{
NodeId: "2",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
{
NodeId: "3",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
{
NodeId: "4",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
{
NodeId: "5",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
},
latestMetrics: []*models.LatestBlockMetrics{
Expand Down Expand Up @@ -187,26 +193,31 @@ func Test_scheduledTask(t *testing.T) {
NodeId: "1",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
{
NodeId: "2",
BestBlockHeight: 989,
FinalizedBlockHeight: 986,
TargetBlockHeight: 1000,
},
{
NodeId: "3",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
{
NodeId: "4",
BestBlockHeight: 989,
FinalizedBlockHeight: 986,
TargetBlockHeight: 1000,
},
{
NodeId: "5",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
},
latestMetrics: []*models.LatestBlockMetrics{
Expand Down
8 changes: 8 additions & 0 deletions internal/schedule/penalize/penalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestScheduleCheckForPenalizedNode(t *testing.T) {
NodeId: "",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
},
latestMetrics: []*models.LatestBlockMetrics{
Expand Down Expand Up @@ -91,11 +92,13 @@ func TestScheduleCheckForPenalizedNode(t *testing.T) {
NodeId: "1",
BestBlockHeight: 900,
FinalizedBlockHeight: 898,
TargetBlockHeight: 900,
},
{
NodeId: "",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
},
latestMetrics: []*models.LatestBlockMetrics{
Expand Down Expand Up @@ -142,21 +145,25 @@ func TestScheduleCheckForPenalizedNode(t *testing.T) {
NodeId: "1",
BestBlockHeight: 900,
FinalizedBlockHeight: 898,
TargetBlockHeight: 900,
},
{
NodeId: "1",
BestBlockHeight: 900,
FinalizedBlockHeight: 898,
TargetBlockHeight: 900,
},
{
NodeId: "1",
BestBlockHeight: 900,
FinalizedBlockHeight: 898,
TargetBlockHeight: 900,
},
{
NodeId: "",
BestBlockHeight: 1000,
FinalizedBlockHeight: 995,
TargetBlockHeight: 1000,
},
},
latestMetrics: []*models.LatestBlockMetrics{
Expand Down Expand Up @@ -203,6 +210,7 @@ func TestScheduleCheckForPenalizedNode(t *testing.T) {
NodeId: "1",
BestBlockHeight: 900,
FinalizedBlockHeight: 898,
TargetBlockHeight: 900,
},
},
latestMetrics: []*models.LatestBlockMetrics{
Expand Down

0 comments on commit da3cd39

Please sign in to comment.