Skip to content

Commit

Permalink
Return properties of proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Sep 26, 2024
1 parent ebd73df commit ad327df
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 153 deletions.
39 changes: 39 additions & 0 deletions activation/malfeasance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"github.com/spacemeshos/post/shared"
Expand Down Expand Up @@ -44,6 +45,19 @@ func NewMalfeasanceHandler(
}
}

func (mh *MalfeasanceHandler) Info(data wire.ProofData) (map[string]string, error) {
ap, ok := data.(*wire.AtxProof)
if !ok {
return nil, errors.New("wrong message type for multiple ATXs")

Check warning on line 51 in activation/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

activation/malfeasance.go#L48-L51

Added lines #L48 - L51 were not covered by tests
}
return map[string]string{
"atx1": ap.Messages[0].InnerMsg.MsgHash.String(),
"atx2": ap.Messages[1].InnerMsg.MsgHash.String(),
"publish_epoch": strconv.FormatUint(uint64(ap.Messages[0].InnerMsg.PublishEpoch), 10),
"smesher_id": ap.Messages[0].SmesherID.String(),
}, nil

Check warning on line 58 in activation/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

activation/malfeasance.go#L53-L58

Added lines #L53 - L58 were not covered by tests
}

func (mh *MalfeasanceHandler) Validate(ctx context.Context, data wire.ProofData) (types.NodeID, error) {
ap, ok := data.(*wire.AtxProof)
if !ok {
Expand Down Expand Up @@ -109,6 +123,18 @@ func NewInvalidPostIndexHandler(
}
}

func (mh *InvalidPostIndexHandler) Info(data wire.ProofData) (map[string]string, error) {
pp, ok := data.(*wire.InvalidPostIndexProof)
if !ok {
return nil, errors.New("wrong message type for invalid post index")

Check warning on line 129 in activation/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

activation/malfeasance.go#L126-L129

Added lines #L126 - L129 were not covered by tests
}
return map[string]string{
"atx": pp.Atx.ID().String(),
"index": strconv.FormatUint(uint64(pp.InvalidIdx), 10),
"smesher_id": pp.Atx.SmesherID.String(),
}, nil

Check warning on line 135 in activation/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

activation/malfeasance.go#L131-L135

Added lines #L131 - L135 were not covered by tests
}

func (mh *InvalidPostIndexHandler) Validate(ctx context.Context, data wire.ProofData) (types.NodeID, error) {
proof, ok := data.(*wire.InvalidPostIndexProof)
if !ok {
Expand Down Expand Up @@ -174,6 +200,19 @@ func NewInvalidPrevATXHandler(
}
}

func (mh *InvalidPrevATXHandler) Info(data wire.ProofData) (map[string]string, error) {
pp, ok := data.(*wire.InvalidPrevATXProof)
if !ok {
return nil, errors.New("wrong message type for invalid previous ATX")

Check warning on line 206 in activation/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

activation/malfeasance.go#L203-L206

Added lines #L203 - L206 were not covered by tests
}
return map[string]string{
"atx1": pp.Atx1.ID().String(),
"atx2": pp.Atx2.ID().String(),
"prev_atx": pp.Atx1.PrevATXID.String(),
"smesher_id": pp.Atx1.SmesherID.String(),
}, nil

Check warning on line 213 in activation/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

activation/malfeasance.go#L208-L213

Added lines #L208 - L213 were not covered by tests
}

func (mh *InvalidPrevATXHandler) Validate(ctx context.Context, data wire.ProofData) (types.NodeID, error) {
proof, ok := data.(*wire.InvalidPrevATXProof)
if !ok {
Expand Down
94 changes: 79 additions & 15 deletions api/grpcserver/v2alpha1/malfeasance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package v2alpha1

import (
"context"
"strconv"
"time"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
spacemeshv2alpha1 "github.com/spacemeshos/api/release/go/spacemesh/v2alpha1"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -16,12 +18,31 @@ import (
"github.com/spacemeshos/go-spacemesh/sql/identities"
)

func NewMalfeasanceService(db sql.Executor) *MalfeasanceService {
return &MalfeasanceService{db: db}
const (
Malfeasance = "malfeasance_v2alpha1"
MalfeasanceStream = "malfeasance_stream_v2alpha1"
)

type malfeasanceInfo interface {
Info(data []byte) (map[string]string, error)
}

func NewMalfeasanceService(
db sql.Executor,
logger *zap.Logger,
malfeasanceHandler malfeasanceInfo,
) *MalfeasanceService {
return &MalfeasanceService{
db: db,
logger: logger,
info: malfeasanceHandler,

Check warning on line 38 in api/grpcserver/v2alpha1/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/malfeasance.go#L34-L38

Added lines #L34 - L38 were not covered by tests
}
}

type MalfeasanceService struct {
db sql.Executor
db sql.Executor
logger *zap.Logger
info malfeasanceInfo
}

func (s *MalfeasanceService) RegisterService(server *grpc.Server) {
Expand Down Expand Up @@ -54,15 +75,11 @@ func (s *MalfeasanceService) List(

rst := make([]*spacemeshv2alpha1.MalfeasanceProof, 0, request.Limit)
if err := identities.IterateMaliciousOps(s.db, ops, func(id types.NodeID, proof []byte, received time.Time) bool {
// TODO (mafa): fetch info about proof from malfeasance service
proofType := uint32(0)
properties := make(map[string]string)
rst = append(rst, &spacemeshv2alpha1.MalfeasanceProof{
Smesher: id.Bytes(),
Domain: spacemeshv2alpha1.MalfeasanceProof_DOMAIN_UNSPECIFIED,
Type: proofType,
Properties: properties,
})
result := s.toProof(id, proof)
if result == nil {
return true

Check warning on line 80 in api/grpcserver/v2alpha1/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/malfeasance.go#L76-L80

Added lines #L76 - L80 were not covered by tests
}
rst = append(rst, result)
return true
}); err != nil {
return nil, status.Error(codes.Internal, err.Error())

Check warning on line 85 in api/grpcserver/v2alpha1/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/malfeasance.go#L82-L85

Added lines #L82 - L85 were not covered by tests
Expand All @@ -71,12 +88,59 @@ func (s *MalfeasanceService) List(
return &spacemeshv2alpha1.MalfeasanceList{Malfeasances: rst}, nil

Check warning on line 88 in api/grpcserver/v2alpha1/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/malfeasance.go#L88

Added line #L88 was not covered by tests
}

func NewMalfeasanceStreamService(db sql.Executor) *MalfeasanceStreamService {
return &MalfeasanceStreamService{db: db}
func (s *MalfeasanceService) toProof(id types.NodeID, proof []byte) *spacemeshv2alpha1.MalfeasanceProof {
properties, err := s.info.Info(proof)
if err != nil {
s.logger.Debug("failed to get malfeasance info",
zap.String("smesher", id.String()),
zap.Error(err),
)
return nil

Check warning on line 98 in api/grpcserver/v2alpha1/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/malfeasance.go#L91-L98

Added lines #L91 - L98 were not covered by tests
}
domain, err := strconv.ParseUint(properties["domain"], 10, 64)
if err != nil {
s.logger.Debug("failed to parse proof domain",
zap.String("smesher", id.String()),
zap.String("domain", properties["domain"]),
zap.Error(err),
)
return nil

Check warning on line 107 in api/grpcserver/v2alpha1/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/malfeasance.go#L100-L107

Added lines #L100 - L107 were not covered by tests
}
delete(properties, "domain")
proofType, err := strconv.ParseUint(properties["type"], 10, 32)
if err != nil {
s.logger.Debug("failed to parse proof type",
zap.String("smesher", id.String()),
zap.String("type", properties["type"]),
zap.Error(err),
)
return nil

Check warning on line 117 in api/grpcserver/v2alpha1/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/malfeasance.go#L109-L117

Added lines #L109 - L117 were not covered by tests
}
delete(properties, "type")
return &spacemeshv2alpha1.MalfeasanceProof{
Smesher: id.Bytes(),
Domain: spacemeshv2alpha1.MalfeasanceProof_MalfeasanceDomain(domain),
Type: uint32(proofType),
Properties: properties,

Check warning on line 124 in api/grpcserver/v2alpha1/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/malfeasance.go#L119-L124

Added lines #L119 - L124 were not covered by tests
}
}

func NewMalfeasanceStreamService(
db sql.Executor,
logger *zap.Logger,
malfeasanceHandler malfeasanceInfo,
) *MalfeasanceStreamService {
return &MalfeasanceStreamService{
db: db,
logger: logger,
info: malfeasanceHandler,

Check warning on line 136 in api/grpcserver/v2alpha1/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/malfeasance.go#L132-L136

Added lines #L132 - L136 were not covered by tests
}
}

type MalfeasanceStreamService struct {
db sql.Executor
db sql.Executor
logger *zap.Logger
info malfeasanceInfo
}

func (s *MalfeasanceStreamService) RegisterService(server *grpc.Server) {
Expand Down
15 changes: 15 additions & 0 deletions hare3/malfeasance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
Expand Down Expand Up @@ -52,6 +53,20 @@ func NewMalfeasanceHandler(
return mh
}

func (mh *MalfeasanceHandler) Info(data wire.ProofData) (map[string]string, error) {
hp, ok := data.(*wire.HareProof)
if !ok {
return nil, errors.New("wrong message type for hare equivocation")

Check warning on line 59 in hare3/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

hare3/malfeasance.go#L56-L59

Added lines #L56 - L59 were not covered by tests
}
return map[string]string{
"msg1": hp.Messages[0].InnerMsg.MsgHash.String(),
"msg2": hp.Messages[1].InnerMsg.MsgHash.String(),
"layer": hp.Messages[0].InnerMsg.Layer.String(),
"round": strconv.FormatUint(uint64(hp.Messages[0].InnerMsg.Round), 10),
"smesher_id": hp.Messages[0].SmesherID.String(),
}, nil

Check warning on line 67 in hare3/malfeasance.go

View check run for this annotation

Codecov / codecov/patch

hare3/malfeasance.go#L61-L67

Added lines #L61 - L67 were not covered by tests
}

func (mh *MalfeasanceHandler) Validate(ctx context.Context, data wire.ProofData) (types.NodeID, error) {
hp, ok := data.(*wire.HareProof)
if !ok {
Expand Down
19 changes: 19 additions & 0 deletions malfeasance/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"slices"
"strconv"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -92,6 +93,24 @@ func (h *Handler) countInvalidProof(p *wire.MalfeasanceProof) {
h.handlers[MalfeasanceType(p.Proof.Type)].ReportInvalidProof(numInvalidProofs)
}

func (h *Handler) Info(data []byte) (map[string]string, error) {
var p wire.MalfeasanceProof
if err := codec.Decode(data, &p); err != nil {
return nil, fmt.Errorf("decode malfeasance proof: %w", err)

Check warning on line 99 in malfeasance/handler.go

View check run for this annotation

Codecov / codecov/patch

malfeasance/handler.go#L96-L99

Added lines #L96 - L99 were not covered by tests
}
mh, ok := h.handlers[MalfeasanceType(p.Proof.Type)]
if !ok {
return nil, fmt.Errorf("unknown malfeasance type %d", p.Proof.Type)

Check warning on line 103 in malfeasance/handler.go

View check run for this annotation

Codecov / codecov/patch

malfeasance/handler.go#L101-L103

Added lines #L101 - L103 were not covered by tests
}
properties, err := mh.Info(p.Proof.Data)
if err != nil {
return nil, fmt.Errorf("malfeasance info: %w", err)

Check warning on line 107 in malfeasance/handler.go

View check run for this annotation

Codecov / codecov/patch

malfeasance/handler.go#L105-L107

Added lines #L105 - L107 were not covered by tests
}
properties["domain"] = "0" // for malfeasance V1 there are no domains
properties["type"] = strconv.FormatUint(uint64(p.Proof.Type), 10)
return properties, nil

Check warning on line 111 in malfeasance/handler.go

View check run for this annotation

Codecov / codecov/patch

malfeasance/handler.go#L109-L111

Added lines #L109 - L111 were not covered by tests
}

// HandleSyncedMalfeasanceProof is the sync validator for MalfeasanceProof.
func (h *Handler) HandleSyncedMalfeasanceProof(
ctx context.Context,
Expand Down
1 change: 1 addition & 0 deletions malfeasance/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type tortoise interface {

type MalfeasanceHandler interface {
Validate(ctx context.Context, data wire.ProofData) (types.NodeID, error)
Info(data wire.ProofData) (map[string]string, error)
ReportProof(vec *prometheus.CounterVec)
ReportInvalidProof(vec *prometheus.CounterVec)
}
Loading

0 comments on commit ad327df

Please sign in to comment.