From dde0e91940173b59585682119b57029fcaf30a5d Mon Sep 17 00:00:00 2001 From: mpetrun5 Date: Mon, 16 Mar 2026 16:10:06 +0100 Subject: [PATCH 1/2] feat: restart signer on new config --- app/app.go | 20 ++++++++++-- config/watcher.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 config/watcher.go diff --git a/app/app.go b/app/app.go index 87a56a6e..dff57e17 100644 --- a/app/app.go +++ b/app/app.go @@ -157,6 +157,9 @@ func Run() error { solverConfig, err := solverConfig.FetchSolverConfig(ctx, solverConfigOpts...) panicOnError(err) + configChanged, err := config.StartConfigWatcher(ctx, solverConfig, solverConfigOpts) + panicOnError(err) + keyshare, err := keyshareStore.GetKeyshare() var mpcAddress common.Address if err == nil { @@ -432,9 +435,20 @@ func Run() error { statusHandler, confirmationsHandler) - sig := <-sysErr - log.Info().Msgf("terminating got ` [%v] signal", sig) - return nil + for { + select { + case sig := <-sysErr: + { + log.Info().Msgf("terminating got ` [%v] signal", sig) + return nil + } + case <-configChanged: + { + log.Info().Msgf("terminating to reload config") + return nil + } + } + } } func panicOnError(err error) { diff --git a/config/watcher.go b/config/watcher.go new file mode 100644 index 00000000..189bab23 --- /dev/null +++ b/config/watcher.go @@ -0,0 +1,82 @@ +package config + +import ( + "context" + "crypto/sha256" + "encoding/json" + "fmt" + "log/slog" + "time" + + "github.com/rs/zerolog/log" + + solverConfig "github.com/sprintertech/solver-config/go/config" +) + +const ( + ConfigWatcherInterval = time.Minute * 1 +) + +// StartConfigWatcher starts a goroutine that periodically checks for config changes. +// Panics to induce a restart if the config has changed +func StartConfigWatcher(ctx context.Context, config *solverConfig.SolverConfig, opts []solverConfig.Option) (<-chan struct{}, error) { + configChanged := make(chan struct{}, 1) + configHash, err := calculateConfigHash(config) + if err != nil { + slog.Error("Failed to calculate initial config hash", slog.String("error", err.Error())) + return configChanged, err + } + + go func() { + ticker := time.NewTicker(ConfigWatcherInterval) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + hasChanged, err := hasConfigChanged(ctx, configHash, opts) + if err != nil { + log.Warn().Msgf("Failed checking has config changed: %s", err) + continue + } + + if hasChanged { + configChanged <- struct{}{} + } + case <-ctx.Done(): + return + } + } + }() + + return configChanged, nil +} + +// hasConfigChanged fetches config and returns true if it has changed +func hasConfigChanged(ctx context.Context, initialHash string, opts []solverConfig.Option) (bool, error) { + solverConfig, err := solverConfig.FetchSolverConfig(ctx, opts...) + if err != nil { + return false, err + } + + newHash, err := calculateConfigHash(solverConfig) + if err != nil { + return false, err + } + + if initialHash == newHash { + return false, nil + } + + return true, nil +} + +func calculateConfigHash(config *solverConfig.SolverConfig) (string, error) { + jsonBytes, err := json.Marshal(config) + if err != nil { + return "", fmt.Errorf("failed to marshal config: %w", err) + } + + hash := sha256.Sum256(jsonBytes) + return fmt.Sprintf("%x", hash), nil +} From 41b9451be45edc8a33ea495004213772fa36edaf Mon Sep 17 00:00:00 2001 From: mpetrun5 Date: Mon, 16 Mar 2026 16:11:23 +0100 Subject: [PATCH 2/2] Remote extra log --- config/watcher.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/watcher.go b/config/watcher.go index 189bab23..c22d8b2e 100644 --- a/config/watcher.go +++ b/config/watcher.go @@ -5,7 +5,6 @@ import ( "crypto/sha256" "encoding/json" "fmt" - "log/slog" "time" "github.com/rs/zerolog/log" @@ -23,7 +22,6 @@ func StartConfigWatcher(ctx context.Context, config *solverConfig.SolverConfig, configChanged := make(chan struct{}, 1) configHash, err := calculateConfigHash(config) if err != nil { - slog.Error("Failed to calculate initial config hash", slog.String("error", err.Error())) return configChanged, err }