Skip to content

Commit 4c67263

Browse files
committed
fix: sanitize the prices with node module params
1 parent 66ec2f6 commit 4c67263

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

core/context.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"context"
45
"errors"
56
"io"
67
"path/filepath"
@@ -250,6 +251,26 @@ func (c *Context) TLSKeyFile() string {
250251
return filepath.Join(c.HomeDir(), "tls.key")
251252
}
252253

254+
// SanitizedGigabytePrices returns gigabyte prices filtered to include only valid denominations.
255+
func (c *Context) SanitizedGigabytePrices(ctx context.Context) v1.Prices {
256+
params, err := c.Client().NodeParams(ctx)
257+
if err != nil {
258+
panic(err)
259+
}
260+
261+
return c.sanitizePrices(c.GigabytePrices(), params.GetMinGigabytePrices())
262+
}
263+
264+
// SanitizedHourlyPrices returns hourly prices filtered to include only valid denominations.
265+
func (c *Context) SanitizedHourlyPrices(ctx context.Context) v1.Prices {
266+
params, err := c.Client().NodeParams(ctx)
267+
if err != nil {
268+
panic(err)
269+
}
270+
271+
return c.sanitizePrices(c.HourlyPrices(), params.GetMinHourlyPrices())
272+
}
273+
253274
// SetLocation sets the geolocation data in the context.
254275
func (c *Context) SetLocation(location *geoip.Location) {
255276
c.fm.Lock()
@@ -409,3 +430,15 @@ func (c *Context) checkSealed() {
409430
panic(errors.New("context is sealed"))
410431
}
411432
}
433+
434+
// sanitizePrices filters and validates a set of prices against the minimum required prices.
435+
func (c *Context) sanitizePrices(prices v1.Prices, minPrices v1.Prices) (newPrices v1.Prices) {
436+
m := minPrices.Map()
437+
for _, price := range prices {
438+
if _, ok := m[price.Denom]; ok {
439+
newPrices = newPrices.Add(price)
440+
}
441+
}
442+
443+
return
444+
}

node/node.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,20 @@ func (n *Node) Register(ctx context.Context) error {
8080
return nil
8181
}
8282

83+
gigabytePrices := n.Context().SanitizedGigabytePrices(ctx)
84+
hourlyPrices := n.Context().SanitizedHourlyPrices(ctx)
85+
8386
log.Info("Registering node",
84-
"gigabyte_price", n.Context().GigabytePrices(),
85-
"hourly_price", n.Context().HourlyPrices(),
87+
"gigabyte_prices", gigabytePrices,
88+
"hourly_price", hourlyPrices,
8689
"remote_addrs", n.Context().APIAddrs(),
8790
)
8891

8992
// Prepare a message to register the node.
9093
msg := v3.NewMsgRegisterNodeRequest(
9194
n.Context().AccAddr(),
92-
n.Context().GigabytePrices(),
93-
n.Context().HourlyPrices(),
95+
gigabytePrices,
96+
hourlyPrices,
9497
n.Context().APIAddrs(),
9598
)
9699

@@ -106,17 +109,20 @@ func (n *Node) Register(ctx context.Context) error {
106109

107110
// UpdateDetails updates the node's pricing and address details on the network.
108111
func (n *Node) UpdateDetails(ctx context.Context) error {
112+
gigabytePrices := n.Context().SanitizedGigabytePrices(ctx)
113+
hourlyPrices := n.Context().SanitizedHourlyPrices(ctx)
114+
109115
log.Info("Updating node details",
110-
"gigabyte_prices", n.Context().GigabytePrices(),
111-
"hourly_prices", n.Context().HourlyPrices(),
116+
"gigabyte_prices", gigabytePrices,
117+
"hourly_prices", hourlyPrices,
112118
"remote_addrs", n.Context().APIAddrs(),
113119
)
114120

115121
// Prepare a message to update the node's details.
116122
msg := v3.NewMsgUpdateNodeDetailsRequest(
117123
n.Context().NodeAddr(),
118-
n.Context().GigabytePrices(),
119-
n.Context().HourlyPrices(),
124+
gigabytePrices,
125+
hourlyPrices,
120126
n.Context().APIAddrs(),
121127
)
122128

workers/node.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewNodePricesUpdateWorker(c *core.Context, interval time.Duration) cron.Wor
5454

5555
var gigabytePrices v1.Prices
5656

57-
for _, price := range c.GigabytePrices() {
57+
for _, price := range c.SanitizedGigabytePrices(ctx) {
5858
price, err := price.UpdateQuoteValue(ctx, client.GetQuotePrice)
5959
if err != nil {
6060
return fmt.Errorf("updating quote price for denom %q: %w", price.Denom, err)
@@ -65,7 +65,7 @@ func NewNodePricesUpdateWorker(c *core.Context, interval time.Duration) cron.Wor
6565

6666
var hourlyPrices v1.Prices
6767

68-
for _, price := range c.HourlyPrices() {
68+
for _, price := range c.SanitizedHourlyPrices(ctx) {
6969
price, err := price.UpdateQuoteValue(ctx, client.GetQuotePrice)
7070
if err != nil {
7171
return fmt.Errorf("updating quote price for denom %q: %w", price.Denom, err)

0 commit comments

Comments
 (0)