Skip to content

Commit

Permalink
Adjust proof interface to pass Context
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Oct 30, 2024
1 parent 3c7071c commit 1e084ad
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
6 changes: 3 additions & 3 deletions activation/handler_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@ func Test_Marriages(t *testing.T) {
verifier.EXPECT().Signature(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(d signing.Domain, nodeID types.NodeID, m []byte, sig types.EdSignature) bool {
return atxHandler.edVerifier.Verify(d, nodeID, m, sig)
}).MinTimes(1)
}).AnyTimes()

atxHandler.mMalPublish.EXPECT().Publish(
gomock.Any(),
Expand All @@ -1669,9 +1669,9 @@ func Test_Marriages(t *testing.T) {
_, ok := data.(*wire.ProofDoubleMarry)
return ok
}),
).DoAndReturn(func(_ context.Context, _ types.NodeID, proof wire.Proof) error {
).DoAndReturn(func(ctx context.Context, _ types.NodeID, proof wire.Proof) error {
malProof := proof.(*wire.ProofDoubleMarry)
nId, err := malProof.Valid(verifier)
nId, err := malProof.Valid(ctx, verifier)
require.NoError(t, err)
require.Equal(t, sig.NodeID(), nId)
return nil
Expand Down
6 changes: 3 additions & 3 deletions activation/wire/malfeasance.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package wire

import (
"context"

"github.com/spacemeshos/go-scale"

"github.com/spacemeshos/go-spacemesh/common/types"
Expand Down Expand Up @@ -81,7 +83,5 @@ type ATXProof struct {
type Proof interface {
scale.Encodable

// TODO(mafa): adjust interface to receive a context and a new validator service for validation
// (of signatures and post). Provider should be malfeasance service in activation package.
Valid(malHandler MalfeasanceValidator) (types.NodeID, error)
Valid(ctx context.Context, malHandler MalfeasanceValidator) (types.NodeID, error)
}
3 changes: 2 additions & 1 deletion activation/wire/malfeasance_double_marry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wire

import (
"context"
"errors"
"fmt"
"slices"
Expand Down Expand Up @@ -133,7 +134,7 @@ func certificateProof(certs MarriageCertificates, index uint64) ([]types.Hash32,
return proofHashes, nil
}

func (p ProofDoubleMarry) Valid(malValidator MalfeasanceValidator) (types.NodeID, error) {
func (p ProofDoubleMarry) Valid(_ context.Context, malValidator MalfeasanceValidator) (types.NodeID, error) {
if p.Proofs[0].ATXID == p.Proofs[1].ATXID {
return types.EmptyNodeID, errors.New("proofs have the same ATX ID")
}
Expand Down
21 changes: 11 additions & 10 deletions activation/wire/malfeasance_double_marry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wire

import (
"context"
"fmt"
"slices"
"testing"
Expand Down Expand Up @@ -53,7 +54,7 @@ func Test_DoubleMarryProof(t *testing.T) {
return edVerifier.Verify(d, nodeID, m, sig)
}).AnyTimes()

id, err := proof.Valid(verifier)
id, err := proof.Valid(context.Background(), verifier)
require.NoError(t, err)
require.Equal(t, otherSig.NodeID(), id)
})
Expand Down Expand Up @@ -108,7 +109,7 @@ func Test_DoubleMarryProof(t *testing.T) {
ctrl := gomock.NewController(t)
verifier := NewMockMalfeasanceValidator(ctrl)

id, err := proof.Valid(verifier)
id, err := proof.Valid(context.Background(), verifier)
require.ErrorContains(t, err, "same ATX ID")
require.Equal(t, types.EmptyNodeID, id)
})
Expand Down Expand Up @@ -154,14 +155,14 @@ func Test_DoubleMarryProof(t *testing.T) {

proof.Proofs[0].MarriageProof = slices.Clone(proof1.MarriageProof)
proof.Proofs[0].MarriageProof[0] = types.RandomHash()
id, err := proof.Valid(verifier)
id, err := proof.Valid(context.Background(), verifier)
require.ErrorContains(t, err, "proof 1 is invalid: invalid marriage proof")
require.Equal(t, types.EmptyNodeID, id)

proof.Proofs[0].MarriageProof[0] = proof1.MarriageProof[0]
proof.Proofs[1].MarriageProof = slices.Clone(proof2.MarriageProof)
proof.Proofs[1].MarriageProof[0] = types.RandomHash()
id, err = proof.Valid(verifier)
id, err = proof.Valid(context.Background(), verifier)
require.ErrorContains(t, err, "proof 2 is invalid: invalid marriage proof")
require.Equal(t, types.EmptyNodeID, id)
})
Expand Down Expand Up @@ -207,14 +208,14 @@ func Test_DoubleMarryProof(t *testing.T) {

proof.Proofs[0].CertificateProof = slices.Clone(proof1.CertificateProof)
proof.Proofs[0].CertificateProof[0] = types.RandomHash()
id, err := proof.Valid(verifier)
id, err := proof.Valid(context.Background(), verifier)
require.ErrorContains(t, err, "proof 1 is invalid: invalid certificate proof")
require.Equal(t, types.EmptyNodeID, id)

proof.Proofs[0].CertificateProof[0] = proof1.CertificateProof[0]
proof.Proofs[1].CertificateProof = slices.Clone(proof2.CertificateProof)
proof.Proofs[1].CertificateProof[0] = types.RandomHash()
id, err = proof.Valid(verifier)
id, err = proof.Valid(context.Background(), verifier)
require.ErrorContains(t, err, "proof 2 is invalid: invalid certificate proof")
require.Equal(t, types.EmptyNodeID, id)
})
Expand Down Expand Up @@ -249,13 +250,13 @@ func Test_DoubleMarryProof(t *testing.T) {
}).AnyTimes()

proof.Proofs[0].Signature = types.RandomEdSignature()
id, err := proof.Valid(verifier)
id, err := proof.Valid(context.Background(), verifier)
require.ErrorContains(t, err, "proof 1 is invalid: invalid ATX signature")
require.Equal(t, types.EmptyNodeID, id)

proof.Proofs[0].Signature = atx1.Signature
proof.Proofs[1].Signature = types.RandomEdSignature()
id, err = proof.Valid(verifier)
id, err = proof.Valid(context.Background(), verifier)
require.ErrorContains(t, err, "proof 2 is invalid: invalid ATX signature")
require.Equal(t, types.EmptyNodeID, id)
})
Expand Down Expand Up @@ -290,13 +291,13 @@ func Test_DoubleMarryProof(t *testing.T) {
}).AnyTimes()

proof.Proofs[0].CertificateSignature = types.RandomEdSignature()
id, err := proof.Valid(verifier)
id, err := proof.Valid(context.Background(), verifier)
require.ErrorContains(t, err, "proof 1 is invalid: invalid certificate signature")
require.Equal(t, types.EmptyNodeID, id)

proof.Proofs[0].CertificateSignature = atx1.Marriages[1].Signature
proof.Proofs[1].CertificateSignature = types.RandomEdSignature()
id, err = proof.Valid(verifier)
id, err = proof.Valid(context.Background(), verifier)
require.ErrorContains(t, err, "proof 2 is invalid: invalid certificate signature")
require.Equal(t, types.EmptyNodeID, id)
})
Expand Down
7 changes: 4 additions & 3 deletions activation/wire/malfeasance_invalid_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func NewInvalidPostProof(atx, initialAtx *ActivationTxV2) (*ProofInvalidPost, er

// Valid returns true if the proof is valid. It verifies that the two proofs have the same publish epoch, smesher ID,
// and a valid signature but different ATX IDs as well as that the provided merkle proofs are valid.
func (p ProofInvalidPost) Valid(malValidator MalfeasanceValidator) (types.NodeID, error) {
func (p ProofInvalidPost) Valid(ctx context.Context, malValidator MalfeasanceValidator) (types.NodeID, error) {
if err := p.Commitment.Valid(malValidator, p.NodeID); err != nil {
return types.EmptyNodeID, fmt.Errorf("invalid commitment proof: %w", err)
}

// TODO(mafa): verify p.NodeID to match the ID in the marriage ATX via the marriage index

if err := p.InvalidPost.Valid(malValidator, p.NodeID, p.Commitment.CommitmentATX); err != nil {
if err := p.InvalidPost.Valid(ctx, malValidator, p.NodeID, p.Commitment.CommitmentATX); err != nil {
return types.EmptyNodeID, fmt.Errorf("invalid invalid post proof: %w", err)
}

Expand Down Expand Up @@ -192,6 +192,7 @@ type InvalidPostProof struct {
// Valid returns no error if the proof is valid. It verifies that the signature is valid, that the merkle proofs are
// and that the provided post is invalid.
func (p InvalidPostProof) Valid(
ctx context.Context,
malValidator MalfeasanceValidator,
nodeID types.NodeID,
commitmentATX types.ATXID,
Expand Down Expand Up @@ -336,7 +337,7 @@ func (p InvalidPostProof) Valid(
}

if err := malValidator.PostIndex(
context.Background(),
ctx,
nodeID,
commitmentATX,
PostFromWireV1(&p.Post),
Expand Down

0 comments on commit 1e084ad

Please sign in to comment.