Skip to content

Commit

Permalink
Update function names and add missing imports + metrics factorization
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronit committed Dec 18, 2023
1 parent dfeab36 commit 3ee2bd1
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 153 deletions.
7 changes: 4 additions & 3 deletions cmd/horcrux/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/strangelove-ventures/horcrux/pkg/connector"
"os"

cometlog "github.com/cometbft/cometbft/libs/log"
Expand Down Expand Up @@ -42,7 +43,7 @@ func startCmd() *cobra.Command {

acceptRisk, _ := cmd.Flags().GetBool(flagAcceptRisk)

var val signer.PrivValidator
var val connector.PrivValidator
var services []service.Service

switch config.Config.SignMode {
Expand All @@ -61,7 +62,7 @@ func startCmd() *cobra.Command {
}

if config.Config.GRPCAddr != "" {
grpcServer := signer.NewRemoteSignerGRPCServer(logger, val, config.Config.GRPCAddr)
grpcServer := connector.NewRemoteSignerGRPCServer(logger, val, config.Config.GRPCAddr)
services = append(services, grpcServer)

if err := grpcServer.Start(); err != nil {
Expand All @@ -71,7 +72,7 @@ func startCmd() *cobra.Command {

go EnableDebugAndMetrics(cmd.Context(), out)

services, err = signer.StartRemoteSigners(services, logger, val, config.Config.Nodes())
services, err = connector.StartRemoteSigners(services, logger, val, config.Config.Nodes())
if err != nil {
return fmt.Errorf("failed to start remote signer(s): %w", err)
}
Expand Down
17 changes: 11 additions & 6 deletions signer/remote_signer.go → pkg/connector/remote_signer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package signer
package connector

/*
Connector is the conections between the "sentry" (consensus nodes) and the Horcrux nodes.
*/
import (
"context"
"fmt"
"net"
"time"

"github.com/strangelove-ventures/horcrux/pkg/metrics"

"github.com/strangelove-ventures/horcrux/pkg/types"

cometcryptoed25519 "github.com/cometbft/cometbft/crypto/ed25519"
Expand Down Expand Up @@ -108,14 +113,14 @@ func (rs *ReconnRemoteSigner) loop(ctx context.Context) {
timer := time.NewTimer(connRetrySec * time.Second)
conn, err = rs.establishConnection(ctx)
if err == nil {
sentryConnectTries.WithLabelValues(rs.address).Set(0)
metrics.SentryConnectTries.WithLabelValues(rs.address).Set(0)
timer.Stop()
rs.Logger.Info("Connected to Sentry", "address", rs.address)
break
}

sentryConnectTries.WithLabelValues(rs.address).Add(1)
totalSentryConnectTries.WithLabelValues(rs.address).Inc()
metrics.SentryConnectTries.WithLabelValues(rs.address).Add(1)
metrics.TotalSentryConnectTries.WithLabelValues(rs.address).Inc()
retries++
rs.Logger.Error(
"Error establishing connection, will retry",
Expand Down Expand Up @@ -231,7 +236,7 @@ func (rs *ReconnRemoteSigner) handleSignProposalRequest(
}

func (rs *ReconnRemoteSigner) handlePubKeyRequest(chainID string) cometprotoprivval.Message {
totalPubKeyRequests.WithLabelValues(chainID).Inc()
metrics.TotalPubKeyRequests.WithLabelValues(chainID).Inc()
msgSum := &cometprotoprivval.Message_PubKeyResponse{PubKeyResponse: &cometprotoprivval.PubKeyResponse{
PubKey: cometprotocrypto.PublicKey{},
Error: nil,
Expand Down Expand Up @@ -288,7 +293,7 @@ func StartRemoteSigners(
nodes []string,
) ([]cometservice.Service, error) {
var err error
go StartMetrics()
go metrics.StartMetrics()
for _, node := range nodes {
// CometBFT requires a connection within 3 seconds of start or crashes
// A long timeout such as 30 seconds would cause the sentry to fail in loops
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package signer
package connector

import (
"context"
"net"
"time"

"github.com/strangelove-ventures/horcrux/pkg/metrics"

"github.com/strangelove-ventures/horcrux/pkg/types"

cometlog "github.com/cometbft/cometbft/libs/log"
Expand Down Expand Up @@ -62,7 +64,7 @@ func (s *RemoteSignerGRPCServer) OnStop() {
func (s *RemoteSignerGRPCServer) PubKey(ctx context.Context, req *proto.PubKeyRequest) (*proto.PubKeyResponse, error) {
chainID := req.ChainId

totalPubKeyRequests.WithLabelValues(chainID).Inc()
metrics.TotalPubKeyRequests.WithLabelValues(chainID).Inc()

pubKey, err := s.validator.GetPubKey(ctx, chainID)
if err != nil {
Expand All @@ -83,7 +85,7 @@ func (s *RemoteSignerGRPCServer) Sign(
ctx context.Context,
req *proto.SignBlockRequest,
) (*proto.SignBlockResponse, error) {
chainID, block := req.ChainID, BlockFromProto(req.Block)
chainID, block := req.ChainID, types.BlockFromProto(req.Block)

signature, timestamp, err := signAndTrack(ctx, s.logger, s.validator, chainID, block)
if err != nil {
Expand All @@ -106,16 +108,16 @@ func signAndTrack(
signature, timestamp, err := validator.Sign(ctx, chainID, block)
if err != nil {
switch typedErr := err.(type) {
case *BeyondBlockError:
case *metrics.BeyondBlockError:
logger.Debug(
"Rejecting sign request",
"type", types.SignType(block.Step),
"chain_id", chainID,
"height", block.Height,
"round", block.Round,
"reason", typedErr.msg,
"reason", typedErr.Msg,
)
beyondBlockErrors.WithLabelValues(chainID).Inc()
metrics.BeyondBlockErrors.WithLabelValues(chainID).Inc()
default:
logger.Error(
"Failed to sign",
Expand All @@ -125,7 +127,7 @@ func signAndTrack(
"round", block.Round,
"error", err,
)
failedSignVote.WithLabelValues(chainID).Inc()
metrics.FailedSignVote.WithLabelValues(chainID).Inc()
}
return nil, block.Timestamp, err
}
Expand All @@ -147,41 +149,41 @@ func signAndTrack(

switch block.Step {
case types.StepPropose:
lastProposalHeight.WithLabelValues(chainID).Set(float64(block.Height))
lastProposalRound.WithLabelValues(chainID).Set(float64(block.Round))
totalProposalsSigned.WithLabelValues(chainID).Inc()
metrics.LastProposalHeight.WithLabelValues(chainID).Set(float64(block.Height))
metrics.LastProposalRound.WithLabelValues(chainID).Set(float64(block.Round))
metrics.TotalProposalsSigned.WithLabelValues(chainID).Inc()
case types.StepPrevote:
// Determine number of heights since the last Prevote
stepSize := block.Height - previousPrevoteHeight
if previousPrevoteHeight != 0 && stepSize > 1 {
missedPrevotes.WithLabelValues(chainID).Add(float64(stepSize))
totalMissedPrevotes.WithLabelValues(chainID).Add(float64(stepSize))
stepSize := block.Height - metrics.PreviousPrevoteHeight
if metrics.PreviousPrevoteHeight != 0 && stepSize > 1 {
metrics.MissedPrevotes.WithLabelValues(chainID).Add(float64(stepSize))
metrics.TotalMissedPrevotes.WithLabelValues(chainID).Add(float64(stepSize))
} else {
missedPrevotes.WithLabelValues(chainID).Set(0)
metrics.MissedPrevotes.WithLabelValues(chainID).Set(0)
}

previousPrevoteHeight = block.Height // remember last PrevoteHeight
metrics.PreviousPrevoteHeight = block.Height // remember last PrevoteHeight

metricsTimeKeeper.SetPreviousPrevote(time.Now())
metrics.MetricsTimeKeeper.SetPreviousPrevote(time.Now())

lastPrevoteHeight.WithLabelValues(chainID).Set(float64(block.Height))
lastPrevoteRound.WithLabelValues(chainID).Set(float64(block.Round))
totalPrevotesSigned.WithLabelValues(chainID).Inc()
metrics.LastPrevoteHeight.WithLabelValues(chainID).Set(float64(block.Height))
metrics.LastPrevoteRound.WithLabelValues(chainID).Set(float64(block.Round))
metrics.TotalPrevotesSigned.WithLabelValues(chainID).Inc()
case types.StepPrecommit:
stepSize := block.Height - previousPrecommitHeight
if previousPrecommitHeight != 0 && stepSize > 1 {
missedPrecommits.WithLabelValues(chainID).Add(float64(stepSize))
totalMissedPrecommits.WithLabelValues(chainID).Add(float64(stepSize))
stepSize := block.Height - metrics.PreviousPrecommitHeight
if metrics.PreviousPrecommitHeight != 0 && stepSize > 1 {
metrics.MissedPrecommits.WithLabelValues(chainID).Add(float64(stepSize))
metrics.TotalMissedPrecommits.WithLabelValues(chainID).Add(float64(stepSize))
} else {
missedPrecommits.WithLabelValues(chainID).Set(0)
metrics.MissedPrecommits.WithLabelValues(chainID).Set(0)
}
previousPrecommitHeight = block.Height // remember last PrecommitHeight
metrics.PreviousPrecommitHeight = block.Height // remember last PrecommitHeight

metricsTimeKeeper.SetPreviousPrecommit(time.Now())
metrics.MetricsTimeKeeper.SetPreviousPrecommit(time.Now())

lastPrecommitHeight.WithLabelValues(chainID).Set(float64(block.Height))
lastPrecommitRound.WithLabelValues(chainID).Set(float64(block.Round))
totalPrecommitsSigned.WithLabelValues(chainID).Inc()
metrics.LastPrecommitHeight.WithLabelValues(chainID).Set(float64(block.Height))
metrics.LastPrecommitRound.WithLabelValues(chainID).Set(float64(block.Round))
metrics.TotalPrecommitsSigned.WithLabelValues(chainID).Inc()
}

return signature, timestamp, nil
Expand Down
Loading

0 comments on commit 3ee2bd1

Please sign in to comment.