Skip to content

Commit

Permalink
Update metrics use of malfeasance package (#6569)
Browse files Browse the repository at this point in the history
## Motivation

Merge after #6566. This updates the way we handle metrics with malfeasance.
  • Loading branch information
fasmat committed Jan 3, 2025
1 parent 1fd07d4 commit e25fda0
Show file tree
Hide file tree
Showing 28 changed files with 252 additions and 189 deletions.
25 changes: 6 additions & 19 deletions activation/malfeasance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"github.com/spacemeshos/post/shared"
"github.com/spacemeshos/post/verifying"
"go.uber.org/zap"
Expand Down Expand Up @@ -92,12 +91,8 @@ func (mh *MalfeasanceHandler) Validate(ctx context.Context, data wire.ProofData)
return types.EmptyNodeID, errors.New("invalid atx malfeasance proof")
}

func (mh *MalfeasanceHandler) ReportProof(numProofs *prometheus.CounterVec) {
numProofs.WithLabelValues(multiATXs).Inc()
}

func (mh *MalfeasanceHandler) ReportInvalidProof(numInvalidProofs *prometheus.CounterVec) {
numInvalidProofs.WithLabelValues(multiATXs).Inc()
func (mh *MalfeasanceHandler) ReportLabel() string {
return multiATXs
}

type InvalidPostIndexHandler struct {
Expand Down Expand Up @@ -169,12 +164,8 @@ func (mh *InvalidPostIndexHandler) Validate(ctx context.Context, data wire.Proof
return types.EmptyNodeID, errors.New("invalid post index malfeasance proof - POST is valid")
}

func (mh *InvalidPostIndexHandler) ReportProof(numProofs *prometheus.CounterVec) {
numProofs.WithLabelValues(invalidPostIndex).Inc()
}

func (mh *InvalidPostIndexHandler) ReportInvalidProof(numInvalidProofs *prometheus.CounterVec) {
numInvalidProofs.WithLabelValues(invalidPostIndex).Inc()
func (mh *InvalidPostIndexHandler) ReportLabel() string {
return invalidPostIndex
}

type InvalidPrevATXHandler struct {
Expand Down Expand Up @@ -241,10 +232,6 @@ func (mh *InvalidPrevATXHandler) Validate(ctx context.Context, data wire.ProofDa
return atx1.SmesherID, nil
}

func (mh *InvalidPrevATXHandler) ReportProof(numProofs *prometheus.CounterVec) {
numProofs.WithLabelValues(invalidPrevATX).Inc()
}

func (mh *InvalidPrevATXHandler) ReportInvalidProof(numInvalidProofs *prometheus.CounterVec) {
numInvalidProofs.WithLabelValues(invalidPrevATX).Inc()
func (mh *InvalidPrevATXHandler) ReportLabel() string {
return invalidPrevATX
}
10 changes: 9 additions & 1 deletion activation/malfeasance2.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ func (mh *MalfeasanceHandlerV2) Info(data []byte) (map[string]string, error) {
return nil, fmt.Errorf("decoding ATX malfeasance proof: %w", err)
}
info := proof.Info()
info["type"] = proof.String()
info["type"] = proof.TypeName()
return info, nil
}

func (mh *MalfeasanceHandlerV2) ReportLabels(data []byte) []string {
proof, err := mh.decodeProof(data)
if err != nil {
return []string{"ATX", "unknown"}
}
return []string{"ATX", proof.TypeName()}
}

func (p *MalfeasanceHandlerV2) Publish(ctx context.Context, id types.NodeID, proof wire.Proof) error {
// TODO(mafa): implement me
return nil
Expand Down
2 changes: 1 addition & 1 deletion activation/malfeasance2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestHandler_Info(t *testing.T) {
require.NoError(t, err)

expectedInfo := tc.proof.Info()
expectedInfo["type"] = tc.proof.String()
expectedInfo["type"] = tc.proof.TypeName()

info, err := th.Info(data)
require.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions activation/wire/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package wire

import (
"context"
"fmt"

"github.com/spacemeshos/go-scale"

Expand Down Expand Up @@ -34,9 +33,9 @@ type MalfeasanceValidator interface {
type Proof interface {
scale.Encodable
scale.Decodable
fmt.Stringer

Type() ProofType
TypeName() string
Info() map[string]string
Valid(ctx context.Context, malHandler MalfeasanceValidator) (types.NodeID, error)
}
2 changes: 1 addition & 1 deletion activation/wire/malfeasance_double_marry.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type ProofDoubleMarry struct {
Proof2 MarryProof
}

func (p ProofDoubleMarry) String() string {
func (p ProofDoubleMarry) TypeName() string {
return "DoubleMarryProof"
}

Expand Down
2 changes: 1 addition & 1 deletion activation/wire/malfeasance_double_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type ProofDoubleMerge struct {
SmesherID2MarryProof MarryProof
}

func (p ProofDoubleMerge) String() string {
func (p ProofDoubleMerge) TypeName() string {
return "DoubleMergeProof"
}

Expand Down
2 changes: 1 addition & 1 deletion activation/wire/malfeasance_invalid_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ProofInvalidPost struct {
InvalidPostProof InvalidPostProof
}

func (p ProofInvalidPost) String() string {
func (p ProofInvalidPost) TypeName() string {
return "InvalidPoSTProof"
}

Expand Down
4 changes: 2 additions & 2 deletions activation/wire/malfeasance_invalid_prev_atx.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ProofInvalidPrevAtxV2 struct {
Proofs [2]InvalidPrevAtxProof
}

func (p ProofInvalidPrevAtxV2) String() string {
func (p ProofInvalidPrevAtxV2) TypeName() string {
return "InvalidPreviousATXProofV2"
}

Expand Down Expand Up @@ -209,7 +209,7 @@ type ProofInvalidPrevAtxV1 struct {
ATXv1 ActivationTxV1
}

func (p ProofInvalidPrevAtxV1) String() string {
func (p ProofInvalidPrevAtxV1) TypeName() string {
return "InvalidPreviousATXProofV1"
}

Expand Down
52 changes: 26 additions & 26 deletions activation/wire/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions beacon/metrics/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/spacemeshos/fixed"
"github.com/stretchr/testify/require"

"github.com/spacemeshos/go-spacemesh/common/types"
)
Expand Down Expand Up @@ -37,7 +38,7 @@ func TestBeaconMetrics(t *testing.T) {
return observed, calculated
}, nil)

deviceExpected := `
expected := `
# HELP spacemesh_beacons_beacon_calculated_weight Weight of the beacon calculated by the node for each epoch
# TYPE spacemesh_beacons_beacon_calculated_weight counter
spacemesh_beacons_beacon_calculated_weight{beacon="speck",epoch="11"} 0
Expand All @@ -50,7 +51,5 @@ spacemesh_beacons_beacon_observed_total{beacon="rashers",epoch="10"} 321
spacemesh_beacons_beacon_observed_weight{beacon="canadian",epoch="10"} 32100
spacemesh_beacons_beacon_observed_weight{beacon="rashers",epoch="10"} 12300
`
if err := testutil.CollectAndCompare(bmc, strings.NewReader(deviceExpected)); err != nil {
t.Error(err)
}
require.NoError(t, testutil.CollectAndCompare(bmc, strings.NewReader(expected)))
}
16 changes: 8 additions & 8 deletions fetch/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ func TestFetch_GetHash(t *testing.T) {
hint2 := datastore.BallotDB

// test hash aggregation
p0, err := f.getHash(context.TODO(), h1, hint, goodReceiver)
p0, err := f.getHash(context.Background(), h1, hint, goodReceiver)
require.NoError(t, err)
p1, err := f.getHash(context.TODO(), h1, hint, goodReceiver)
p1, err := f.getHash(context.Background(), h1, hint, goodReceiver)
require.NoError(t, err)
require.Equal(t, p0.completed, p1.completed)

h2 := types.RandomHash()
p2, err := f.getHash(context.TODO(), h2, hint2, goodReceiver)
p2, err := f.getHash(context.Background(), h2, hint2, goodReceiver)
require.NoError(t, err)
require.NotEqual(t, p1.completed, p2.completed)
}
Expand Down Expand Up @@ -230,10 +230,10 @@ func TestFetch_RequestHashBatchFromPeers(t *testing.T) {
receiver = badReceiver
}
for i := 0; i < 2; i++ {
p, err := f.getHash(context.TODO(), hsh0, datastore.ProposalDB, receiver)
p, err := f.getHash(context.Background(), hsh0, datastore.ProposalDB, receiver)
require.NoError(t, err)
p0 = append(p0, p)
p, err = f.getHash(context.TODO(), hsh1, datastore.BlockDB, receiver)
p, err = f.getHash(context.Background(), hsh1, datastore.BlockDB, receiver)
require.NoError(t, err)
p1 = append(p1, p)
}
Expand Down Expand Up @@ -299,11 +299,11 @@ func TestFetch_Loop_BatchRequestMax(t *testing.T) {

defer f.Stop()
require.NoError(t, f.Start())
p1, err := f.getHash(context.TODO(), h1, hint, goodReceiver)
p1, err := f.getHash(context.Background(), h1, hint, goodReceiver)
require.NoError(t, err)
p2, err := f.getHash(context.TODO(), h2, hint, goodReceiver)
p2, err := f.getHash(context.Background(), h2, hint, goodReceiver)
require.NoError(t, err)
p3, err := f.getHash(context.TODO(), h3, hint, goodReceiver)
p3, err := f.getHash(context.Background(), h3, hint, goodReceiver)
require.NoError(t, err)
for _, p := range []*promise{p1, p2, p3} {
<-p.completed
Expand Down
2 changes: 1 addition & 1 deletion fetch/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func TestHandleMaliciousIDsReq(t *testing.T) {
require.NoError(t, identities.SetMalicious(th.cdb, nid, types.RandomBytes(11), time.Now()))
}

out, err := th.handleMaliciousIDsReq(context.TODO(), p2p.Peer(""), []byte{})
out, err := th.handleMaliciousIDsReq(context.Background(), p2p.Peer(""), []byte{})
require.NoError(t, err)
var got MaliciousIDs
require.NoError(t, codec.Decode(out, &got))
Expand Down
2 changes: 1 addition & 1 deletion hare3/eligibility/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Config struct {
// For example, if epoch size is 100 and confidence is 10 hare will use previous active set for layers 0-9
// and then generate a new activeset.
//
// This was done like that so that we have higher `confidence` that hare will succeed atleast
// This was done like that so that we have higher `confidence` that hare will succeed at least
// once during this interval. If it doesn't we have to provide centralized fallback.
ConfidenceParam uint32 `mapstructure:"eligibility-confidence-param"`
}
Expand Down
9 changes: 2 additions & 7 deletions hare3/malfeasance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/spacemeshos/go-spacemesh/common/types"
Expand Down Expand Up @@ -102,10 +101,6 @@ func (mh *MalfeasanceHandler) Validate(ctx context.Context, data wire.ProofData)
return types.EmptyNodeID, errors.New("invalid hare malfeasance proof")
}

func (mh *MalfeasanceHandler) ReportProof(numProofs *prometheus.CounterVec) {
numProofs.WithLabelValues(hareEquivocate).Inc()
}

func (mh *MalfeasanceHandler) ReportInvalidProof(numInvalidProofs *prometheus.CounterVec) {
numInvalidProofs.WithLabelValues(hareEquivocate).Inc()
func (mh *MalfeasanceHandler) ReportLabel() string {
return hareEquivocate
}
2 changes: 1 addition & 1 deletion hare4/eligibility/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Config struct {
// For example, if epoch size is 100 and confidence is 10 hare will use previous active set for layers 0-9
// and then generate a new activeset.
//
// This was done like that so that we have higher `confidence` that hare will succeed atleast
// This was done like that so that we have higher `confidence` that hare will succeed at least
// once during this interval. If it doesn't we have to provide centralized fallback.
ConfidenceParam uint32 `mapstructure:"eligibility-confidence-param"`
}
Expand Down
9 changes: 2 additions & 7 deletions hare4/malfeasance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/spacemeshos/go-spacemesh/common/types"
Expand Down Expand Up @@ -102,10 +101,6 @@ func (mh *MalfeasanceHandler) Validate(ctx context.Context, data wire.ProofData)
return types.EmptyNodeID, errors.New("invalid hare malfeasance proof")
}

func (mh *MalfeasanceHandler) ReportProof(numProofs *prometheus.CounterVec) {
numProofs.WithLabelValues(hareEquivocate).Inc()
}

func (mh *MalfeasanceHandler) ReportInvalidProof(numInvalidProofs *prometheus.CounterVec) {
numInvalidProofs.WithLabelValues(hareEquivocate).Inc()
func (mh *MalfeasanceHandler) ReportLabel() string {
return hareEquivocate
}
Loading

0 comments on commit e25fda0

Please sign in to comment.