Skip to content

Commit d9dbc2e

Browse files
committed
fix: remove panic in sanitized prices method in context
1 parent 254b0df commit d9dbc2e

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

core/context.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"io"
78
"path/filepath"
89
"sync"
@@ -252,23 +253,33 @@ func (c *Context) TLSKeyFile() string {
252253
}
253254

254255
// SanitizedGigabytePrices returns gigabyte prices filtered to include only valid denominations.
255-
func (c *Context) SanitizedGigabytePrices(ctx context.Context) v1.Prices {
256+
func (c *Context) SanitizedGigabytePrices(ctx context.Context) (v1.Prices, error) {
256257
params, err := c.Client().NodeParams(ctx)
257258
if err != nil {
258-
panic(err)
259+
return nil, fmt.Errorf("getting node params: %w", err)
259260
}
260261

261-
return c.sanitizePrices(c.GigabytePrices(), params.GetMinGigabytePrices())
262+
prices := c.sanitizePrices(
263+
c.GigabytePrices(),
264+
params.GetMinGigabytePrices(),
265+
)
266+
267+
return prices, nil
262268
}
263269

264270
// SanitizedHourlyPrices returns hourly prices filtered to include only valid denominations.
265-
func (c *Context) SanitizedHourlyPrices(ctx context.Context) v1.Prices {
271+
func (c *Context) SanitizedHourlyPrices(ctx context.Context) (v1.Prices, error) {
266272
params, err := c.Client().NodeParams(ctx)
267273
if err != nil {
268-
panic(err)
274+
return nil, fmt.Errorf("getting node params: %w", err)
269275
}
270276

271-
return c.sanitizePrices(c.HourlyPrices(), params.GetMinHourlyPrices())
277+
prices := c.sanitizePrices(
278+
c.HourlyPrices(),
279+
params.GetMinHourlyPrices(),
280+
)
281+
282+
return prices, nil
272283
}
273284

274285
// SetLocation sets the geolocation data in the context.

node/node.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,15 @@ 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)
83+
gigabytePrices, err := n.Context().SanitizedGigabytePrices(ctx)
84+
if err != nil {
85+
return fmt.Errorf("sanitizing gigabyte prices: %w", err)
86+
}
87+
88+
hourlyPrices, err := n.Context().SanitizedHourlyPrices(ctx)
89+
if err != nil {
90+
return fmt.Errorf("sanitizing hourly prices: %w", err)
91+
}
8592

8693
log.Info("Registering node",
8794
"gigabyte_prices", gigabytePrices,
@@ -109,8 +116,15 @@ func (n *Node) Register(ctx context.Context) error {
109116

110117
// UpdateDetails updates the node's pricing and address details on the network.
111118
func (n *Node) UpdateDetails(ctx context.Context) error {
112-
gigabytePrices := n.Context().SanitizedGigabytePrices(ctx)
113-
hourlyPrices := n.Context().SanitizedHourlyPrices(ctx)
119+
gigabytePrices, err := n.Context().SanitizedGigabytePrices(ctx)
120+
if err != nil {
121+
return fmt.Errorf("sanitizing gigabyte prices: %w", err)
122+
}
123+
124+
hourlyPrices, err := n.Context().SanitizedHourlyPrices(ctx)
125+
if err != nil {
126+
return fmt.Errorf("sanitizing hourly prices: %w", err)
127+
}
114128

115129
log.Info("Updating node details",
116130
"gigabyte_prices", gigabytePrices,

workers/node.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,24 @@ func NewNodeStatusUpdateWorker(c *core.Context, interval time.Duration) cron.Wor
4646
// NewNodePricesUpdateWorker creates a worker that periodically updates the node's prices on the blockchain.
4747
// The worker computes the current quote prices using the OracleClient and broadcasts a MsgUpdateNodeDetailsRequest.
4848
func NewNodePricesUpdateWorker(c *core.Context, interval time.Duration) cron.Worker {
49-
handlerFunc := func(ctx context.Context) error {
49+
handlerFunc := func(ctx context.Context) (err error) {
5050
client := c.OracleClient()
5151
if client == nil {
5252
return nil
5353
}
5454

55-
var gigabytePrices v1.Prices
55+
var (
56+
prices v1.Prices
57+
gigabytePrices v1.Prices
58+
hourlyPrices v1.Prices
59+
)
5660

57-
for _, price := range c.SanitizedGigabytePrices(ctx) {
61+
prices, err = c.SanitizedGigabytePrices(ctx)
62+
if err != nil {
63+
return fmt.Errorf("sanitizing gigabyte prices: %w", err)
64+
}
65+
66+
for _, price := range prices {
5867
price, err := price.UpdateQuoteValue(ctx, client.GetQuotePrice)
5968
if err != nil {
6069
return fmt.Errorf("updating quote price for denom %q: %w", price.Denom, err)
@@ -63,9 +72,12 @@ func NewNodePricesUpdateWorker(c *core.Context, interval time.Duration) cron.Wor
6372
gigabytePrices = gigabytePrices.Add(price)
6473
}
6574

66-
var hourlyPrices v1.Prices
75+
prices, err = c.SanitizedHourlyPrices(ctx)
76+
if err != nil {
77+
return fmt.Errorf("sanitizing hourly prices: %w", err)
78+
}
6779

68-
for _, price := range c.SanitizedHourlyPrices(ctx) {
80+
for _, price := range prices {
6981
price, err := price.UpdateQuoteValue(ctx, client.GetQuotePrice)
7082
if err != nil {
7183
return fmt.Errorf("updating quote price for denom %q: %w", price.Denom, err)

0 commit comments

Comments
 (0)