-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: development
Are you sure you want to change the base?
Conversation
… uptime percentage
|
||
upTimePercentage, err := calculateUpTimePercentage(reports, periodStart, now) | ||
if err != nil { | ||
if err == ErrReportsNotInAscOrder { |
There was a problem hiding this comment.
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 ==
node-registrar/test/main.go
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the test file
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 |
There was a problem hiding this comment.
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
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 | |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it thank you
10c4a79
to
6646f65
Compare
|
||
// Error check | ||
if tt.wantError { | ||
if err == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use require.Error
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
// secondsSinceCurrentPeriodStart := (tt.inputTime.Unix() - FirstPeriodStartTimestamp) % StandardPeriodDuration | ||
// manualCalculation := time.Unix(FirstPeriodStartTimestamp+secondsSinceCurrentPeriodStart, 0) | ||
// assert.Equal(t, manualCalculation.Unix(), result.Unix()) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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
node-registrar/client/node.go
Outdated
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") | ||
} |
There was a problem hiding this comment.
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?
…v4-sdk-go into development_rewards
…ed value checks in rewards tests
…ions
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

Related Issues
Checklist