Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
538c347
feat: implement node rewards calculation and API endpoint
0oM4R Jun 12, 2025
3ed761e
test: add test case for uptime percentage with decimal value
0oM4R Jun 12, 2025
929bad7
refactor: replace error string with proper error variable for invalid…
0oM4R Jun 15, 2025
bdcd279
docs: mention that the duration in seconds
0oM4R Jun 19, 2025
1dcc431
feat: implement node uptime calculation based on report history
0oM4R Jun 19, 2025
66b2a66
feat: add uptime percentage to rewards struct
0oM4R Jun 19, 2025
0ea416e
docs: add swagger docs for the rewards endpoint
0oM4R Jun 22, 2025
f60aa13
feat: show uptime percentage in the response of no rewards
0oM4R Jun 22, 2025
38e8082
fix: rename reward endpoint to rewards
0oM4R Jun 22, 2025
eb4aeca
feat: truncate reward values to 3 decimal places
0oM4R Jun 22, 2025
b39f7f7
feat: add uptime percentage to reward calculation in tests
0oM4R Jun 23, 2025
41c2c64
refactor: rename constants and functions to follow Go naming conventions
0oM4R Jun 23, 2025
6c5993c
Fix tests: pass duration as time.duration
0oM4R Jun 23, 2025
d80cbe0
refactor: rename and split reward calculation functions for better ma…
0oM4R Jun 23, 2025
0c6094d
feat: improve error handling for node uptime reports and add test cases
0oM4R Jun 23, 2025
04e5a9a
refactor: enhance loops
0oM4R Jun 23, 2025
ceff122
refactor: extract reward calculation logic into separate functions an…
0oM4R Jun 23, 2025
5e9f390
test: add comprehensive test suite for rewards calculation and uptime…
0oM4R Jun 23, 2025
f538295
revert adding main.go
0oM4R Jun 23, 2025
81906f9
docs: simplify uptime percentage calculation documentation with conci…
0oM4R Jun 23, 2025
27c3fe8
feat: add node capacity rewards calculation endpoint and types
0oM4R Jun 23, 2025
b69397d
fix node tests in client
Eslam-Nawara Jun 24, 2025
395a4fc
WIP: add rewards tests in client
0oM4R Jun 24, 2025
55e7ea8
remove extra println
Eslam-Nawara Jun 24, 2025
c4f1136
tests: add more test cases for the rewards
0oM4R Jun 24, 2025
10c4a79
refactor: standardize reward field names and JSON tags in NodeCapacit…
0oM4R Jun 24, 2025
e5e481c
fix node tests in client
Eslam-Nawara Jun 24, 2025
2465abc
WIP: add rewards tests in client
0oM4R Jun 24, 2025
2b5f964
remove extra println
Eslam-Nawara Jun 24, 2025
02e4c78
tests: add more test cases for the rewards
0oM4R Jun 24, 2025
6646f65
refactor: standardize reward field names and JSON tags in NodeCapacit…
0oM4R Jun 24, 2025
56109d3
Merge branch 'development_rewards' of github.com:threefoldtech/tfgrid…
0oM4R Jun 30, 2025
d0ce507
revert: remove the test file
0oM4R Jun 30, 2025
ff3487f
fix: use errors.Is() for proper error comparison in uptime calculatio…
0oM4R Jun 30, 2025
b407b77
refactor: replace floating point delta comparisons with exact truncat…
0oM4R Jun 30, 2025
cdd305f
test: replace InDelta with Equal for reward distribution percentage c…
0oM4R Jun 30, 2025
6a71bc3
refactor: simplify test assertions using require and assert packages
0oM4R Jun 30, 2025
2ae9121
fix: handle nil response in node rewards request
0oM4R Jun 30, 2025
311f49d
refactor: simplify error handling in node rewards and uptime calculat…
0oM4R Jun 30, 2025
b41cddc
Merge branch 'development' of github.com:threefoldtech/tfgridv4-sdk-g…
0oM4R Jul 14, 2025
724b052
lint
0oM4R Jul 14, 2025
036a1b2
refactor: simplify bytes conversion using explicit constants instead …
0oM4R Jul 14, 2025
f094176
refactor: consolidate reward calculation into single function with up…
0oM4R Jul 14, 2025
acfb746
test: replace assert with require for consistent error checking in re…
0oM4R Jul 14, 2025
9d4fd14
refactor: simplify reward test assertions by comparing entire struct
0oM4R Jul 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions node-registrar/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,52 @@ const docTemplate = `{
}
}
},
"/nodes/{node_id}/reward": {
"get": {
"description": "Retrieves reward calculation for a specific node based on resources and uptime",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"nodes"
],
"summary": "Get node monthly reward information",
"parameters": [
{
"type": "integer",
"description": "Node ID",
"name": "node_id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "Rewards details with the node uptime percentage",
"schema": {
"$ref": "#/definitions/server.Reward"
}
},
"400": {
"description": "Invalid node ID",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"404": {
"description": "Node not found",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
}
},
"/nodes/{node_id}/uptime": {
"post": {
"description": "Submit uptime report for a node",
Expand Down
68 changes: 67 additions & 1 deletion node-registrar/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,52 @@
}
}
},
"/nodes/{node_id}/rewards": {
"get": {
"description": "Retrieves reward calculation for a specific node based on resources and uptime",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"nodes"
],
"summary": "Get node monthly reward information",
"parameters": [
{
"type": "integer",
"description": "Node ID",
"name": "node_id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "Rewards details with the node uptime percentage",
"schema": {
"$ref": "#/definitions/server.Reward"
}
},
"400": {
"description": "Invalid node ID",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"404": {
"description": "Node not found",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
}
},
"/nodes/{node_id}/uptime": {
"post": {
"description": "Submit uptime report for a node",
Expand Down Expand Up @@ -1104,7 +1150,27 @@
}
}
},
"server.UpdateAccountRequest": {
"server.Reward": {
"type": "object",
"properties": {
"farmerReward": {
"type": "number"
},
"fpreward": {
"type": "number"
},
"tfreward": {
"type": "number"
},
"total": {
"type": "number"
},
"upTimePercentage": {
"type": "number"
}
}
},
"pkg_server.UpdateAccountRequest": {
"type": "object",
"properties": {
"relays": {
Expand Down
45 changes: 45 additions & 0 deletions node-registrar/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ definitions:
- serial_number
- twin_id
type: object
server.Reward:
properties:
farmerReward:
type: number
fpreward:
type: number
tfreward:
type: number
total:
type: number
upTimePercentage:
type: number
type: object
server.UpdateAccountRequest:
properties:
relays:
Expand Down Expand Up @@ -697,6 +710,38 @@ paths:
summary: Update node
tags:
- nodes
/nodes/{node_id}/rewards:
get:
consumes:
- application/json
description: Retrieves reward calculation for a specific node based on resources
and uptime
parameters:
- description: Node ID
in: path
name: node_id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: Rewards details with the node uptime percentage
schema:
$ref: '#/definitions/server.Reward'
"400":
description: Invalid node ID
schema:
additionalProperties: true
type: object
"404":
description: Node not found
schema:
additionalProperties: true
type: object
summary: Get node monthly reward information
tags:
- nodes
/nodes/{node_id}/uptime:
post:
consumes:
Expand Down
2 changes: 1 addition & 1 deletion node-registrar/pkg/db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (n *Node) BeforeCreate(tx *gorm.DB) (err error) {
type UptimeReport struct {
ID uint64 `gorm:"primaryKey;autoIncrement"`
NodeID uint64 `gorm:"index" json:"node_id"`
Duration time.Duration `json:"duration" swaggertype:"integer"` // Uptime duration for this period
Duration time.Duration `json:"duration" swaggertype:"integer"` // Uptime duration for this period in seconds
Timestamp time.Time `json:"timestamp" gorm:"index"`
WasRestart bool `json:"was_restart"` // True if this report followed a restart
CreatedAt time.Time `json:"created_at"`
Expand Down
52 changes: 51 additions & 1 deletion node-registrar/pkg/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,56 @@ func (s Server) getNodeHandler(c *gin.Context) {
c.JSON(http.StatusOK, node)
}

// @Summary Get node monthly reward information
// @Description Retrieves reward calculation for a specific node based on resources and uptime
// @Tags nodes
// @Accept json
// @Produce json
// @Param node_id path int true "Node ID"
// @Success 200 {object} Reward "Rewards details with the node uptime percentage"
// @Failure 400 {object} map[string]any "Invalid node ID"
// @Failure 404 {object} map[string]any "Node not found"
// @Router /nodes/{node_id}/rewards [get]
func (s Server) getNodeRewardHandler(c *gin.Context) {
nodeID := c.Param("node_id")

id, err := strconv.ParseUint(nodeID, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid node id"})
return
}

node, err := s.db.GetNode(id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

now := time.Now()
currentPeriodStart := calculateCurrentPeriodStart(now)

reports, err := s.db.GetUptimeReports(id, currentPeriodStart, now)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

upTimePercentage := calculateUpTimePercentage(reports, currentPeriodStart, now)

rewards, err := CalculateMonthlyReward(node.Resources, upTimePercentage)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, rewards)
}

type NodeRegistrationRequest struct {
TwinID uint64 `json:"twin_id" binding:"required,min=1"`
FarmID uint64 `json:"farm_id" binding:"required,min=1"`
Expand Down Expand Up @@ -422,7 +472,7 @@ func (s *Server) updateNodeHandler(c *gin.Context) {
}

type UptimeReportRequest struct {
Uptime uint64 `json:"uptime" binding:"required"`
Uptime uint64 `json:"uptime" binding:"required"` // in seconds
Timestamp int64 `json:"timestamp" binding:"required"`
}

Expand Down
Loading