Skip to content

Commit

Permalink
add CachedSignatureWeightPercentage metric
Browse files Browse the repository at this point in the history
as discussed on standup
  • Loading branch information
feuGeneA committed Aug 16, 2024
1 parent fb73eea commit 6d91922
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions signature-aggregator/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func (s *SignatureAggregator) CreateSignedMessage(
}
}
s.metrics.SignatureCacheHits.Add(float64(len(signatureMap)))
s.metrics.CachedSignatureWeightPercentage.Set(
float64(utils.GetStakeWeightPercentage(
accumulatedSignatureWeight,
connectedValidators.TotalValidatorWeight,
)),
)
}
if signedMsg, err := s.aggregateIfSufficientWeight(
unsignedMessage,
Expand Down
10 changes: 10 additions & 0 deletions signature-aggregator/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var Opts = struct {
InvalidSignatureResponses prometheus.CounterOpts
SignatureCacheHits prometheus.CounterOpts
SignatureCacheMisses prometheus.CounterOpts
CachedSignatureWeightPercentage prometheus.GaugeOpts
}{
AggregateSignaturesLatencyMS: prometheus.GaugeOpts{
Name: "agg_sigs_latency_ms",
Expand Down Expand Up @@ -72,6 +73,10 @@ var Opts = struct {
Name: "signature_cache_misses",
Help: "Number of signatures that were not found in the cache",
},
CachedSignatureWeightPercentage: prometheus.GaugeOpts{
Name: "cached_signature_weight_percentage",
Help: "The percentage of stake weight represented by the cached signatures for a given warp message ID",
},
}

type SignatureAggregatorMetrics struct {
Expand All @@ -85,6 +90,7 @@ type SignatureAggregatorMetrics struct {
InvalidSignatureResponses prometheus.Counter
SignatureCacheHits prometheus.Counter
SignatureCacheMisses prometheus.Counter
CachedSignatureWeightPercentage prometheus.Gauge

// TODO: consider other failures to monitor. Issue #384 requires
// "network failures", but we probably don't handle those directly.
Expand Down Expand Up @@ -129,6 +135,9 @@ func NewSignatureAggregatorMetrics(
SignatureCacheMisses: prometheus.NewCounter(
Opts.SignatureCacheMisses,
),
CachedSignatureWeightPercentage: prometheus.NewGauge(
Opts.CachedSignatureWeightPercentage,
),
}

registerer.MustRegister(m.AggregateSignaturesLatencyMS)
Expand All @@ -141,6 +150,7 @@ func NewSignatureAggregatorMetrics(
registerer.MustRegister(m.InvalidSignatureResponses)
registerer.MustRegister(m.SignatureCacheHits)
registerer.MustRegister(m.SignatureCacheMisses)
registerer.MustRegister(m.CachedSignatureWeightPercentage)

return &m
}
Expand Down
5 changes: 5 additions & 0 deletions tests/signature_aggregator_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func SignatureAggregatorAPI(network interfaces.LocalNetwork) {
{metrics.Opts.InvalidSignatureResponses.Name, "==", 0},
{metrics.Opts.SignatureCacheHits.Name, "==", 0},
{metrics.Opts.SignatureCacheMisses.Name, "==", 0},
{metrics.Opts.CachedSignatureWeightPercentage.Name, "==", 0},
} {
Expect(metricsSample[m.name]).Should(
BeNumerically(m.op, m.value),
Expand All @@ -157,6 +158,9 @@ func SignatureAggregatorAPI(network interfaces.LocalNetwork) {
Expect(
metricsSample2[metrics.Opts.SignatureCacheMisses.Name],
).Should(Equal(metricsSample[metrics.Opts.SignatureCacheMisses.Name]))
Expect(
metricsSample2[metrics.Opts.CachedSignatureWeightPercentage.Name],
).Should(BeNumerically("==", 75))
}

// returns a map of metric names to metric samples
Expand Down Expand Up @@ -185,6 +189,7 @@ func sampleMetrics(port uint16) map[string]uint64 {
metrics.Opts.InvalidSignatureResponses.Name,
metrics.Opts.SignatureCacheHits.Name,
metrics.Opts.SignatureCacheMisses.Name,
metrics.Opts.CachedSignatureWeightPercentage.Name,
} {
if strings.HasPrefix(
line,
Expand Down
16 changes: 16 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ func CheckStakeWeightExceedsThreshold(
return scaledTotalWeight.Cmp(scaledSigWeight) != 1
}

func GetStakeWeightPercentage(
proportionalWeight *big.Int,
totalWeight uint64,
) uint64 {
totalWeightBI := new(big.Int).SetUint64(totalWeight)
scaledProportionalWeight := new(big.Int).Mul(
proportionalWeight,
new(big.Int).SetUint64(100),
)
percentage := scaledProportionalWeight.Div(
scaledProportionalWeight,
totalWeightBI,
)
return percentage.Uint64()
}

// Wrapper for CheckStakeWeightExceedThreshold with a quorumDen of 100.
func CheckStakeWeightPercentageExceedsThreshold(
accumulatedSignatureWeight *big.Int,
Expand Down

0 comments on commit 6d91922

Please sign in to comment.