From 615b39b918ad6141cfe73e89255aa2a0c2862647 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Thu, 11 Jul 2024 11:20:19 -0700 Subject: [PATCH 1/3] cmd: remove fatal panic when explorer is not configured correctly or offline --- cmd/hostd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/hostd/main.go b/cmd/hostd/main.go index 134cbd1b..5d41bcf9 100644 --- a/cmd/hostd/main.go +++ b/cmd/hostd/main.go @@ -396,7 +396,7 @@ func main() { defer cancel() if _, err := ex.SiacoinExchangeRate(ctx, "usd"); err != nil { - log.Fatal("failed to get exchange rate", zap.Error(err)) + log.Error("failed to get exchange rate. explorer features may not work correctly", zap.Error(err)) } } From 765dbc191decfc26a7e0aec091a7dc2d0bc605f8 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Thu, 11 Jul 2024 11:29:20 -0700 Subject: [PATCH 2/3] cmd,pin: add alert for price update failures --- cmd/hostd/node.go | 1 + host/settings/pin/options.go | 6 ++++++ host/settings/pin/pin.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/cmd/hostd/node.go b/cmd/hostd/node.go index a2bf8f19..1a3c07bf 100644 --- a/cmd/hostd/node.go +++ b/cmd/hostd/node.go @@ -187,6 +187,7 @@ func newNode(ctx context.Context, walletKey types.PrivateKey, ex *explorer.Explo var pm *pin.Manager if !cfg.Explorer.Disable { pm, err = pin.NewManager( + pin.WithAlerts(am), pin.WithStore(db), pin.WithSettings(sr), pin.WithExchangeRateRetriever(ex), diff --git a/host/settings/pin/options.go b/host/settings/pin/options.go index d0dbf8f2..28113f13 100644 --- a/host/settings/pin/options.go +++ b/host/settings/pin/options.go @@ -16,6 +16,12 @@ func WithLogger(log *zap.Logger) Option { } } +func WithAlerts(a Alerts) Option { + return func(m *Manager) { + m.alerts = a + } +} + // WithFrequency sets the frequency at which the manager updates the host's // settings based on the current exchange rate. func WithFrequency(frequency time.Duration) Option { diff --git a/host/settings/pin/pin.go b/host/settings/pin/pin.go index 94f56375..3a8edca3 100644 --- a/host/settings/pin/pin.go +++ b/host/settings/pin/pin.go @@ -9,10 +9,14 @@ import ( "github.com/shopspring/decimal" "go.sia.tech/core/types" + "go.sia.tech/hostd/alerts" "go.sia.tech/hostd/host/settings" "go.uber.org/zap" + "lukechampine.com/frand" ) +var pinAlertID = frand.Entropy256() + type ( // A Pin is a pinned price in an external currency. Pin struct { @@ -44,6 +48,12 @@ type ( MaxCollateral Pin `json:"maxCollateral"` } + // Alerts registers global alerts. + Alerts interface { + Register(alerts.Alert) + Dismiss(...types.Hash256) + } + // A SettingsManager updates and retrieves the host's settings. SettingsManager interface { Settings() settings.Settings @@ -67,6 +77,7 @@ type ( Manager struct { log *zap.Logger store Store + alerts Alerts explorer ExchangeRateRetriever sm SettingsManager @@ -99,6 +110,26 @@ func averageRate(rates []decimal.Decimal) decimal.Decimal { return sum.Div(decimal.NewFromInt(int64(len(rates)))) } +func (m *Manager) registerPinFailureAlert(err error) { + if m.alerts != nil && err != nil { + m.alerts.Register(alerts.Alert{ + ID: pinAlertID, + Severity: alerts.SeverityError, + Message: "failed to update prices", + Timestamp: time.Now(), + Data: map[string]interface{}{ + "error": err.Error(), + }, + }) + } +} + +func (m *Manager) dismissPinFailureAlert() { + if m.alerts != nil { + m.alerts.Dismiss(pinAlertID) + } +} + func (m *Manager) updatePrices(ctx context.Context, force bool) error { ctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() @@ -227,6 +258,7 @@ func (m *Manager) Run(ctx context.Context) error { // update prices immediately if err := m.updatePrices(ctx, true); err != nil { + m.registerPinFailureAlert(err) m.log.Error("failed to update prices", zap.Error(err)) } @@ -237,6 +269,9 @@ func (m *Manager) Run(ctx context.Context) error { case <-t.C: if err := m.updatePrices(ctx, false); err != nil { m.log.Error("failed to update prices", zap.Error(err)) + m.registerPinFailureAlert(err) + } else { + m.dismissPinFailureAlert() } } } From ad69b44f0d3838066e131f848b4bfd21e72feaf1 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Thu, 11 Jul 2024 11:32:54 -0700 Subject: [PATCH 3/3] pin: fix lint issues --- host/settings/pin/options.go | 1 + 1 file changed, 1 insertion(+) diff --git a/host/settings/pin/options.go b/host/settings/pin/options.go index 28113f13..3842ea58 100644 --- a/host/settings/pin/options.go +++ b/host/settings/pin/options.go @@ -16,6 +16,7 @@ func WithLogger(log *zap.Logger) Option { } } +// WithAlerts sets the alerts manager for the pinner to register alerts with. func WithAlerts(a Alerts) Option { return func(m *Manager) { m.alerts = a