From 31d654b977093cc992b6b707ff338ca1a82b1944 Mon Sep 17 00:00:00 2001 From: Matthias <5011972+fasmat@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:51:41 +0000 Subject: [PATCH] Fix open TODO --- activation/handler_v2_test.go | 4 ++-- activation/wire/malfeasance.go | 6 +++--- activation/wire/malfeasance_double_marry.go | 3 ++- .../wire/malfeasance_double_marry_test.go | 21 ++++++++++--------- activation/wire/malfeasance_invalid_post.go | 7 ++++--- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/activation/handler_v2_test.go b/activation/handler_v2_test.go index c27ab6b494..d8024c5c1b 100644 --- a/activation/handler_v2_test.go +++ b/activation/handler_v2_test.go @@ -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 diff --git a/activation/wire/malfeasance.go b/activation/wire/malfeasance.go index 4dbe77e437..45a96fc04c 100644 --- a/activation/wire/malfeasance.go +++ b/activation/wire/malfeasance.go @@ -1,6 +1,8 @@ package wire import ( + "context" + "github.com/spacemeshos/go-scale" "github.com/spacemeshos/go-spacemesh/common/types" @@ -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) } diff --git a/activation/wire/malfeasance_double_marry.go b/activation/wire/malfeasance_double_marry.go index 4e7f78bba9..eca7c20954 100644 --- a/activation/wire/malfeasance_double_marry.go +++ b/activation/wire/malfeasance_double_marry.go @@ -1,6 +1,7 @@ package wire import ( + "context" "errors" "fmt" "slices" @@ -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") } diff --git a/activation/wire/malfeasance_double_marry_test.go b/activation/wire/malfeasance_double_marry_test.go index 0876a60288..ca8823eacf 100644 --- a/activation/wire/malfeasance_double_marry_test.go +++ b/activation/wire/malfeasance_double_marry_test.go @@ -1,6 +1,7 @@ package wire import ( + "context" "fmt" "slices" "testing" @@ -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) }) @@ -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) }) @@ -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) }) @@ -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) }) @@ -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) }) @@ -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) }) diff --git a/activation/wire/malfeasance_invalid_post.go b/activation/wire/malfeasance_invalid_post.go index 6e5ec329d6..4010110d23 100644 --- a/activation/wire/malfeasance_invalid_post.go +++ b/activation/wire/malfeasance_invalid_post.go @@ -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) } @@ -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, @@ -336,7 +337,7 @@ func (p InvalidPostProof) Valid( } if err := malValidator.PostIndex( - context.Background(), + ctx, nodeID, commitmentATX, PostFromWireV1(&p.Post),