From 120dfab49ec2b4521f9bb84a0aec43c25500e31d Mon Sep 17 00:00:00 2001 From: Leo Date: Sun, 29 Nov 2020 13:30:18 +0100 Subject: [PATCH] bridge: type alias for readiness components --- bridge/cmd/guardiand/bridge.go | 6 +++--- bridge/pkg/common/readiness.go | 9 +++++++++ bridge/pkg/ethereum/watcher.go | 2 +- bridge/pkg/readiness/health.go | 14 ++++++++------ bridge/pkg/solana/watcher.go | 2 +- bridge/pkg/terra/watcher.go | 2 +- 6 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 bridge/pkg/common/readiness.go diff --git a/bridge/cmd/guardiand/bridge.go b/bridge/cmd/guardiand/bridge.go index f2982f293..c499e29e4 100644 --- a/bridge/cmd/guardiand/bridge.go +++ b/bridge/cmd/guardiand/bridge.go @@ -174,10 +174,10 @@ func runBridge(cmd *cobra.Command, args []string) { ipfslog.SetAllLoggers(lvl) // Register components for readiness checks. - readiness.RegisterComponent("ethSyncing") - readiness.RegisterComponent("solanaSyncing") + readiness.RegisterComponent(common.ReadinessEthSyncing) + readiness.RegisterComponent(common.ReadinessSolanaSyncing) if *terraSupport { - readiness.RegisterComponent("terraSyncing") + readiness.RegisterComponent(common.ReadinessTerraSyncing) } // In devnet mode, we automatically set a number of flags that rely on deterministic keys. diff --git a/bridge/pkg/common/readiness.go b/bridge/pkg/common/readiness.go new file mode 100644 index 000000000..a18d49101 --- /dev/null +++ b/bridge/pkg/common/readiness.go @@ -0,0 +1,9 @@ +package common + +import "github.com/certusone/wormhole/bridge/pkg/readiness" + +const ( + ReadinessEthSyncing readiness.Component = "ethSyncing" + ReadinessSolanaSyncing readiness.Component = "solanaSyncing" + ReadinessTerraSyncing readiness.Component = "terraSyncing" +) diff --git a/bridge/pkg/ethereum/watcher.go b/bridge/pkg/ethereum/watcher.go index 3e4912cfe..f06c9324c 100644 --- a/bridge/pkg/ethereum/watcher.go +++ b/bridge/pkg/ethereum/watcher.go @@ -179,7 +179,7 @@ func (e *EthBridgeWatcher) Run(ctx context.Context) error { start := time.Now() logger.Info("processing new header", zap.Stringer("block", ev.Number)) - readiness.SetReady("ethSyncing") + readiness.SetReady(common.ReadinessEthSyncing) e.pendingLocksGuard.Lock() diff --git a/bridge/pkg/readiness/health.go b/bridge/pkg/readiness/health.go index c87ec38eb..fb78990cf 100644 --- a/bridge/pkg/readiness/health.go +++ b/bridge/pkg/readiness/health.go @@ -16,21 +16,23 @@ var ( registry = map[string]bool{} ) +type Component string + // RegisterComponent registers the given component name such that it is required to be ready for the global check to succeed. -func RegisterComponent(component string) { +func RegisterComponent(component Component) { mu.Lock() - if _, ok := registry[component]; ok { + if _, ok := registry[string(component)]; ok { panic("component already registered") } - registry[component] = false + registry[string(component)] = false mu.Unlock() } // SetReady sets the given global component state. -func SetReady(component string) { +func SetReady(component Component) { mu.Lock() - if !registry[component] { - registry[component] = true + if !registry[string(component)] { + registry[string(component)] = true } mu.Unlock() } diff --git a/bridge/pkg/solana/watcher.go b/bridge/pkg/solana/watcher.go index 54121b1b7..5cf749e49 100644 --- a/bridge/pkg/solana/watcher.go +++ b/bridge/pkg/solana/watcher.go @@ -65,7 +65,7 @@ func (e *SolanaBridgeWatcher) Run(ctx context.Context) error { if err != nil { return fmt.Errorf("failed to get balance: %v", err) } - readiness.SetReady("solanaSyncing") + readiness.SetReady(common.ReadinessSolanaSyncing) logger.Info("account balance", zap.Uint64("lamports", balance.Balance)) go func() { diff --git a/bridge/pkg/terra/watcher.go b/bridge/pkg/terra/watcher.go index 83a1344d1..a8a82fc6d 100644 --- a/bridge/pkg/terra/watcher.go +++ b/bridge/pkg/terra/watcher.go @@ -81,7 +81,7 @@ func (e *BridgeWatcher) Run(ctx context.Context) error { } logger.Info("subscribed to new transaction events") - readiness.SetReady("terraSyncing") + readiness.SetReady(common.ReadinessTerraSyncing) go func() { defer close(errC)