Skip to content

Commit

Permalink
pass in healthcheck func instead of accessing network from healthchec…
Browse files Browse the repository at this point in the history
…k package directly
  • Loading branch information
iansuvak committed Jan 27, 2025
1 parent 2bb6490 commit 280e5e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
26 changes: 3 additions & 23 deletions signature-aggregator/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,16 @@ package healthcheck

import (
"context"
"fmt"
"math/big"
"net/http"

"github.com/alexliesenfeld/health"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/icm-services/peers"
"github.com/ava-labs/icm-services/signature-aggregator/aggregator"
"github.com/ava-labs/icm-services/utils"
"github.com/ava-labs/subnet-evm/precompile/contracts/warp"
)

func HandleHealthCheckRequest(network peers.AppRequestNetwork) {
func HandleHealthCheckRequest(checkFunc func(context.Context) error) {
healthChecker := health.NewChecker(
health.WithCheck(health.Check{
Name: "signature-aggregator-health",
Check: func(context.Context) error {
connectedValidators, err := network.GetConnectedCanonicalValidators(constants.PrimaryNetworkID)
if err != nil {
return fmt.Errorf("Failed to connect to primary network validators: %w", err)
}
if !utils.CheckStakeWeightExceedsThreshold(
big.NewInt(0).SetUint64(connectedValidators.ConnectedWeight),
connectedValidators.TotalValidatorWeight,
warp.WarpDefaultQuorumNumerator,
) {
return aggregator.ErrNotEnoughConnectedStake
}
return nil
},
Name: "signature-aggregator-health",
Check: checkFunc,
}),
)

Expand Down
22 changes: 21 additions & 1 deletion signature-aggregator/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package main

import (
"context"
"errors"
"fmt"
"log"
"math/big"
"net/http"
"os"

Expand All @@ -19,6 +21,8 @@ import (
"github.com/ava-labs/icm-services/signature-aggregator/config"
"github.com/ava-labs/icm-services/signature-aggregator/healthcheck"
"github.com/ava-labs/icm-services/signature-aggregator/metrics"
"github.com/ava-labs/icm-services/utils"
"github.com/ava-labs/subnet-evm/precompile/contracts/warp"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -139,7 +143,23 @@ func main() {
metricsInstance,
signatureAggregator,
)
healthcheck.HandleHealthCheckRequest(network)

network.TrackSubnet(constants.PrimaryNetworkID)
primaryNetworkHealthCheckFunc := func(context.Context) error {
connectedValidators, err := network.GetConnectedCanonicalValidators(constants.PrimaryNetworkID)
if err != nil {
return fmt.Errorf("Failed to connect to primary network validators: %w", err)
}
if !utils.CheckStakeWeightExceedsThreshold(
big.NewInt(0).SetUint64(connectedValidators.ConnectedWeight),
connectedValidators.TotalValidatorWeight,
warp.WarpDefaultQuorumNumerator,
) {
return aggregator.ErrNotEnoughConnectedStake
}
return nil
}
healthcheck.HandleHealthCheckRequest(primaryNetworkHealthCheckFunc)

logger.Info("Initialization complete")
err = http.ListenAndServe(fmt.Sprintf(":%d", cfg.APIPort), nil)
Expand Down

0 comments on commit 280e5e4

Please # to comment.