Skip to content

feat: implement node rewards calculation and API endpoint #64

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

Open
wants to merge 39 commits into
base: development
Choose a base branch
from

Conversation

0oM4R
Copy link

@0oM4R 0oM4R commented Jun 12, 2025

Description

This PR introduces a new API endpoint to calculate real-time node rewards based on node resources and uptime reports.

Please note that the network usage is not included
And please consider those concerns, comment

Changes

  • New Endpoint:
    GET /nodes/{node_id}/rewards
    Returns: Rewards

  • New Handler:
    getNodeRewardsHandler – handles input validation and response logic.

New Reward Logic:
Added a pkg/server/rewards.go file with the logic to:
- Get uptime reports for the current month
- Calculate uptime percentage
- Apply reward formula
- Return reward values (or zeroes if uptime is too low)

Swagger Update:
API documentation updated with the new endpoint and response schema.

Tests

Add unit tests, create a fake node, and submit some reports
image

Related Issues

Checklist

  • Tests included
  • Build pass
  • Documentation
  • Code format and docstring

Sorry, something went wrong.

0oM4R added 10 commits June 12, 2025 20:03

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
… uptime percentage

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
0oM4R added 10 commits June 23, 2025 12:03

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
…intainability

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
…d add test suite

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
… tracking

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
…se explanation
0oM4R and others added 6 commits June 23, 2025 19:39

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
…yReward struct

upTimePercentage, err := calculateUpTimePercentage(reports, periodStart, now)
if err != nil {
if err == ErrReportsNotInAscOrder {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use errors.Is instead of ==

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the test file

Comment on lines 327 to 336
if err == ErrReportsNotInAscOrder {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if err == ErrNoReportsAvailable {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you return status internal server error in case of ErrReportNotInAscOrder and in the default case then maybe you should just remove the check for ErrReportNotInAscOrder and only use the default

Suggested change
if err == ErrReportsNotInAscOrder {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if err == ErrNoReportsAvailable {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
if errors.Is(err, ErrNoReportsAvailable) {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are passing the error, so it may help us in debugging.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're passing the error in both cases

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it thank you

Eslam-Nawara and others added 5 commits June 29, 2025 15:26

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
…yReward struct
@Eslam-Nawara Eslam-Nawara force-pushed the development_rewards branch from 10c4a79 to 6646f65 Compare June 29, 2025 12:26

// Error check
if tt.wantError {
if err == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use require.Error

Comment on lines 110 to 112
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use require.NoError


// Use precise floating point comparison
const delta = 1e-9 // Very small acceptable difference
assert.InDelta(t, expected.FarmerReward, got.FarmerReward, delta)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all this values are rounded no?

Comment on lines +201 to +203
// secondsSinceCurrentPeriodStart := (tt.inputTime.Unix() - FirstPeriodStartTimestamp) % StandardPeriodDuration
// manualCalculation := time.Unix(FirstPeriodStartTimestamp+secondsSinceCurrentPeriodStart, 0)
// assert.Equal(t, manualCalculation.Unix(), result.Unix())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove extra comments

}
}

func AssertCapacityReward(t testing.TB, resources db.Resources, upTimePercentage float64, got Reward) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the values we're testing against be pre-calculated?

if err != nil {
return reward, errors.Wrap(err, "failed to send request to rewards endpoint")
}
defer resp.Body.Close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check that resp not nil

Comment on lines 343 to 350
if resp.StatusCode == http.StatusUnprocessableEntity {
return reward, parseResponseError(resp.Body)
}

if resp.StatusCode != http.StatusOK {
err = parseResponseError(resp.Body)
return reward, errors.Wrap(err, "failed to get node rewards")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the client responds the same for the two cases, why not removing the fist one?

0oM4R added 8 commits June 30, 2025 12:38

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
…v4-sdk-go into development_rewards

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
…n handler

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
…ed value checks in rewards tests

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
…hecks

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem

Verified

This commit was signed with the committer’s verified signature.
0oM4R Omar Kassem
…ions
@0oM4R 0oM4R requested a review from Eslam-Nawara July 8, 2025 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants