Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MetricReceivedProposedBlock overflow #4831

Merged
merged 4 commits into from Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions consensus/spos/worker.go
Expand Up @@ -514,6 +514,9 @@ func (wrk *Worker) processReceivedHeaderMetric(cnsDta *consensus.Message) {
}

sinceRoundStart := time.Since(wrk.roundHandler.TimeStamp())
if sinceRoundStart < 0 {
sinceRoundStart = 0
}
percent := sinceRoundStart * 100 / wrk.roundHandler.TimeDuration()
wrk.appStatusHandler.SetUInt64Value(common.MetricReceivedProposedBlock, uint64(percent))
wrk.appStatusHandler.SetStringValue(common.MetricRedundancyIsMainActive, strconv.FormatBool(wrk.nodeRedundancyHandler.IsMainMachineActive()))
Expand Down
45 changes: 37 additions & 8 deletions consensus/spos/worker_test.go
Expand Up @@ -580,6 +580,41 @@ func TestWorker_ProcessReceivedMessageNodeNotInEligibleListShouldErr(t *testing.
func TestWorker_ProcessReceivedMessageComputeReceivedProposedBlockMetric(t *testing.T) {
t.Parallel()

t.Run("normal operation", func(t *testing.T) {
t.Parallel()

roundDuration := time.Millisecond * 1000
delay := time.Millisecond * 430
roundStartTimeStamp := time.Now()

receivedValue := testWorkerProcessReceivedMessageComputeReceivedProposedBlockMetric(roundStartTimeStamp, delay, roundDuration)

minimumExpectedValue := uint64(delay * 100 / roundDuration)
assert.True(t,
receivedValue >= minimumExpectedValue,
fmt.Sprintf("minimum expected was %d, got %d", minimumExpectedValue, receivedValue),
)
})
t.Run("time.Since returns negative value", func(t *testing.T) {
// test the edgecase when the returned NTP time stored in the round handler is
// slightly advanced when comparing with time.Now.
t.Parallel()

roundDuration := time.Millisecond * 1000
delay := time.Millisecond * 430
roundStartTimeStamp := time.Now().Add(time.Minute)

receivedValue := testWorkerProcessReceivedMessageComputeReceivedProposedBlockMetric(roundStartTimeStamp, delay, roundDuration)

assert.Zero(t, receivedValue)
})
}

func testWorkerProcessReceivedMessageComputeReceivedProposedBlockMetric(
roundStartTimeStamp time.Time,
delay time.Duration,
roundDuration time.Duration,
) uint64 {
receivedValue := uint64(0)
wrk := *initWorker(&statusHandlerMock.AppStatusHandlerStub{
SetUInt64ValueHandler: func(key string, value uint64) {
Expand All @@ -599,9 +634,7 @@ func TestWorker_ProcessReceivedMessageComputeReceivedProposedBlockMetric(t *test
return nil
},
})
roundDuration := time.Millisecond * 1000
delay := time.Millisecond * 430
roundStartTimeStamp := time.Now()

wrk.SetRoundHandler(&mock.RoundHandlerMock{
RoundIndex: 0,
TimeDurationCalled: func() time.Duration {
Expand Down Expand Up @@ -639,11 +672,7 @@ func TestWorker_ProcessReceivedMessageComputeReceivedProposedBlockMetric(t *test
}
_ = wrk.ProcessReceivedMessage(msg, "")

minimumExpectedValue := uint64(delay * 100 / roundDuration)
assert.True(t,
receivedValue >= minimumExpectedValue,
fmt.Sprintf("minimum expected was %d, got %d", minimumExpectedValue, receivedValue),
)
return receivedValue
}

func TestWorker_ProcessReceivedMessageInconsistentChainIDInConsensusMessageShouldErr(t *testing.T) {
Expand Down