From a4b3d367de509346621504bb7e43f9407da5a1e6 Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Thu, 18 Jul 2024 23:59:02 +0530 Subject: [PATCH 1/9] Tests for readonly added --- .../payload-attestation/readonly_test.go | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 consensus-types/payload-attestation/readonly_test.go diff --git a/consensus-types/payload-attestation/readonly_test.go b/consensus-types/payload-attestation/readonly_test.go new file mode 100644 index 000000000000..dbc52e77815a --- /dev/null +++ b/consensus-types/payload-attestation/readonly_test.go @@ -0,0 +1,78 @@ +package payloadattestation + +import ( + "testing" + + ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" + "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" + "github.com/prysmaticlabs/prysm/v5/testing/assert" + "github.com/prysmaticlabs/prysm/v5/testing/require" +) + + +func TestValidatePayload(t *testing.T){ + tests := []struct { + name string + bfunc func(t *testing.T) *ethpb.PayloadAttestationMessage + err error + }{ + { + name: "nil PayloadAttestationMessage", + bfunc: func(t *testing.T) *ethpb.PayloadAttestationMessage { + return nil + }, + err: errNilPayloadMessage, + }, + { + name: "nil data", + bfunc: func(t *testing.T) *ethpb.PayloadAttestationMessage { + return ðpb.PayloadAttestationMessage{ + Data: nil, + } + }, + err: errNilPayloadData, + }, + { + name: "nil signature", + bfunc: func(t *testing.T) *ethpb.PayloadAttestationMessage { + return ðpb.PayloadAttestationMessage{ + Signature: nil, + } + }, + err: errMissingPayloadSignature, + }, + } + for _, tt := range tests { + t.Run(tt.name+" ReadOnlyPayloadAtt", func(t *testing.T) { + m := tt.bfunc(t) + err := validatePayload(m) + if tt.err != nil { + require.ErrorIs(t, err, tt.err) + } else { + RoMess,err:=NewReadOnly(m) + assert.Equal(t,RoMess.message.Data,m.Data) + assert.Equal(t,RoMess.message.Signature,m.Signature) + require.NoError(t, err) + } + }) + } +} + +func TestValidatorIndex(t *testing.T){ + ValIdx:=primitives.ValidatorIndex(1) + m:=&ReadOnlyPayloadAtt{ + message: ðpb.PayloadAttestationMessage{ + ValidatorIndex: ValIdx, + }, + } + assert.Equal(t,ValIdx,m.ValidatorIndex()) +} +func TestSignature(t *testing.T){ + Sig:=[]byte{1} + m:=&ReadOnlyPayloadAtt{ + message: ðpb.PayloadAttestationMessage{ + Signature: Sig, + }, + } + assert.Equal(t,Sig,m.Signature()) +} From d155037508ee024fb179fafae65aa81531e95450 Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Fri, 19 Jul 2024 00:39:53 +0530 Subject: [PATCH 2/9] Rest tests added --- .../payload-attestation/readonly_test.go | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/consensus-types/payload-attestation/readonly_test.go b/consensus-types/payload-attestation/readonly_test.go index dbc52e77815a..0f981d0c97b4 100644 --- a/consensus-types/payload-attestation/readonly_test.go +++ b/consensus-types/payload-attestation/readonly_test.go @@ -4,6 +4,7 @@ import ( "testing" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" + fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" "github.com/prysmaticlabs/prysm/v5/testing/assert" "github.com/prysmaticlabs/prysm/v5/testing/require" @@ -41,6 +42,18 @@ func TestValidatePayload(t *testing.T){ }, err: errMissingPayloadSignature, }, + { + name: "Correct PayloadAttestationMessage", + bfunc: func(t *testing.T) *ethpb.PayloadAttestationMessage { + return ðpb.PayloadAttestationMessage{ + Signature: make([]byte, 96), + Data: ðpb.PayloadAttestationData{ + BeaconBlockRoot: make([]byte, fieldparams.RootLength), + }, + } + }, + err: nil, + }, } for _, tt := range tests { t.Run(tt.name+" ReadOnlyPayloadAtt", func(t *testing.T) { @@ -48,7 +61,7 @@ func TestValidatePayload(t *testing.T){ err := validatePayload(m) if tt.err != nil { require.ErrorIs(t, err, tt.err) - } else { + }else { RoMess,err:=NewReadOnly(m) assert.Equal(t,RoMess.message.Data,m.Data) assert.Equal(t,RoMess.message.Signature,m.Signature) @@ -68,11 +81,49 @@ func TestValidatorIndex(t *testing.T){ assert.Equal(t,ValIdx,m.ValidatorIndex()) } func TestSignature(t *testing.T){ - Sig:=[]byte{1} + Sig:=[96]byte{1} m:=&ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ - Signature: Sig, + Signature: Sig[:], }, } assert.Equal(t,Sig,m.Signature()) } +func TestBeaconBlockRoot(t *testing.T){ + root := [32]byte{1} + m:=&ReadOnlyPayloadAtt{ + message: ðpb.PayloadAttestationMessage{ + Data: ðpb.PayloadAttestationData{ + BeaconBlockRoot: root[:], + }, + }, + } + assert.Equal(t,root,m.BeaconBlockRoot()) +} + +func TestSlot(t *testing.T){ + slot:=primitives.Slot(1) + m:=&ReadOnlyPayloadAtt{ + message: ðpb.PayloadAttestationMessage{ + Data: ðpb.PayloadAttestationData{ + Slot: slot, + }, + }, + } + assert.Equal(t,slot,m.Slot()) +} +func TestPayloadStatus(t *testing.T){ + + for i:=0;i<4;i++ { + status:=primitives.PTCStatus(i) + m:=&ReadOnlyPayloadAtt{ + message: ðpb.PayloadAttestationMessage{ + Data: ðpb.PayloadAttestationData{ + PayloadStatus:status, + }, + }, + } + require.NoError(t,validatePayload(m.message)) + assert.Equal(t,status,m.PayloadStatus()) + } +} From 7564bf427ff0f658ab88d8f4f70e90420361c3dd Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Fri, 19 Jul 2024 00:57:14 +0530 Subject: [PATCH 3/9] Minor fixes in tests --- .../payload-attestation/readonly_test.go | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/consensus-types/payload-attestation/readonly_test.go b/consensus-types/payload-attestation/readonly_test.go index 0f981d0c97b4..2a63096face6 100644 --- a/consensus-types/payload-attestation/readonly_test.go +++ b/consensus-types/payload-attestation/readonly_test.go @@ -15,14 +15,14 @@ func TestValidatePayload(t *testing.T){ tests := []struct { name string bfunc func(t *testing.T) *ethpb.PayloadAttestationMessage - err error + wanterr error }{ { name: "nil PayloadAttestationMessage", bfunc: func(t *testing.T) *ethpb.PayloadAttestationMessage { return nil }, - err: errNilPayloadMessage, + wanterr: errNilPayloadMessage, }, { name: "nil data", @@ -31,7 +31,7 @@ func TestValidatePayload(t *testing.T){ Data: nil, } }, - err: errNilPayloadData, + wanterr: errNilPayloadData, }, { name: "nil signature", @@ -40,32 +40,32 @@ func TestValidatePayload(t *testing.T){ Signature: nil, } }, - err: errMissingPayloadSignature, + wanterr: errMissingPayloadSignature, }, { name: "Correct PayloadAttestationMessage", bfunc: func(t *testing.T) *ethpb.PayloadAttestationMessage { return ðpb.PayloadAttestationMessage{ - Signature: make([]byte, 96), + Signature: make([]byte, fieldparams.BLSSignatureLength), Data: ðpb.PayloadAttestationData{ BeaconBlockRoot: make([]byte, fieldparams.RootLength), }, } }, - err: nil, + wanterr: nil, }, } for _, tt := range tests { t.Run(tt.name+" ReadOnlyPayloadAtt", func(t *testing.T) { m := tt.bfunc(t) err := validatePayload(m) - if tt.err != nil { - require.ErrorIs(t, err, tt.err) + if tt.wanterr != nil { + require.ErrorIs(t, err, tt.wanterr) }else { RoMess,err:=NewReadOnly(m) + require.NoError(t, err) assert.Equal(t,RoMess.message.Data,m.Data) assert.Equal(t,RoMess.message.Signature,m.Signature) - require.NoError(t, err) } }) } @@ -81,7 +81,7 @@ func TestValidatorIndex(t *testing.T){ assert.Equal(t,ValIdx,m.ValidatorIndex()) } func TestSignature(t *testing.T){ - Sig:=[96]byte{1} + Sig:=make([]byte,fieldparams.BLSSignatureLength) m:=&ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ Signature: Sig[:], @@ -90,7 +90,7 @@ func TestSignature(t *testing.T){ assert.Equal(t,Sig,m.Signature()) } func TestBeaconBlockRoot(t *testing.T){ - root := [32]byte{1} + root := make([]byte,fieldparams.RootLength) m:=&ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ Data: ðpb.PayloadAttestationData{ @@ -113,7 +113,7 @@ func TestSlot(t *testing.T){ assert.Equal(t,slot,m.Slot()) } func TestPayloadStatus(t *testing.T){ - + for i:=0;i<4;i++ { status:=primitives.PTCStatus(i) m:=&ReadOnlyPayloadAtt{ From c0ff8399845fef1e1e69ceeafb6661550d83506d Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Fri, 19 Jul 2024 10:19:40 +0530 Subject: [PATCH 4/9] cosmetics applied --- .../payload-attestation/readonly_test.go | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/consensus-types/payload-attestation/readonly_test.go b/consensus-types/payload-attestation/readonly_test.go index 2a63096face6..b23b3760eddc 100644 --- a/consensus-types/payload-attestation/readonly_test.go +++ b/consensus-types/payload-attestation/readonly_test.go @@ -62,32 +62,33 @@ func TestValidatePayload(t *testing.T){ if tt.wanterr != nil { require.ErrorIs(t, err, tt.wanterr) }else { - RoMess,err:=NewReadOnly(m) + roMess,err:=NewReadOnly(m) require.NoError(t, err) - assert.Equal(t,RoMess.message.Data,m.Data) - assert.Equal(t,RoMess.message.Signature,m.Signature) + assert.Equal(t,roMess.message.Data,m.Data) + assert.Equal(t,roMess.message.Signature,m.Signature) } }) } } func TestValidatorIndex(t *testing.T){ - ValIdx:=primitives.ValidatorIndex(1) + valIdx:=primitives.ValidatorIndex(1) m:=&ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ - ValidatorIndex: ValIdx, + ValidatorIndex: valIdx, }, } - assert.Equal(t,ValIdx,m.ValidatorIndex()) + assert.Equal(t,valIdx,m.ValidatorIndex()) } + func TestSignature(t *testing.T){ - Sig:=make([]byte,fieldparams.BLSSignatureLength) + sig:=make([]byte,fieldparams.BLSSignatureLength) m:=&ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ - Signature: Sig[:], + Signature: sig[:], }, } - assert.Equal(t,Sig,m.Signature()) + assert.Equal(t,sig,m.Signature()) } func TestBeaconBlockRoot(t *testing.T){ root := make([]byte,fieldparams.RootLength) @@ -112,8 +113,8 @@ func TestSlot(t *testing.T){ } assert.Equal(t,slot,m.Slot()) } + func TestPayloadStatus(t *testing.T){ - for i:=0;i<4;i++ { status:=primitives.PTCStatus(i) m:=&ReadOnlyPayloadAtt{ From 8b98ae64629fd76aa148fc42647e175b495e7f61 Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Fri, 19 Jul 2024 18:15:20 +0530 Subject: [PATCH 5/9] test added for verifier.go --- .../payload-attestation/verifier_test.go | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 consensus-types/payload-attestation/verifier_test.go diff --git a/consensus-types/payload-attestation/verifier_test.go b/consensus-types/payload-attestation/verifier_test.go new file mode 100644 index 000000000000..6b837a9e75ba --- /dev/null +++ b/consensus-types/payload-attestation/verifier_test.go @@ -0,0 +1,34 @@ +package payloadattestation + +import ( + "testing" + + "github.com/prysmaticlabs/prysm/v5/beacon-chain/startup" + "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" + "github.com/prysmaticlabs/prysm/v5/testing/assert" + ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" +) + +func TestVerifyCurrentSlot(t *testing.T){ + clock:=&startup.Clock{} + verifier:=&PayloadVerifier{ + Resources: Resources{ + clock: clock, + }, + } + assert.Equal(t, verifier.VerifyCurrentSlot(), ErrMismatchCurrentSlot) +} + +func TestVerifyKnownPayloadStatus(t *testing.T){ + ptcstatus:= primitives.PTCStatus(4) + verifier:=&PayloadVerifier{ + payloadAtt: ReadOnlyPayloadAtt{ + message: ðpb.PayloadAttestationMessage{ + Data: ðpb.PayloadAttestationData{ + PayloadStatus:ptcstatus, + }, + }, + }, + } + assert.Equal(t, verifier.VerifyKnownPayloadStatus(), ErrUnknownPayloadStatus) +} \ No newline at end of file From 758b7303d5f4100e0b42fcc208e2fe8877ff91d7 Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Sat, 20 Jul 2024 01:15:45 +0530 Subject: [PATCH 6/9] space-added --- consensus-types/payload-attestation/readonly_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/consensus-types/payload-attestation/readonly_test.go b/consensus-types/payload-attestation/readonly_test.go index b23b3760eddc..40f5d0a865d7 100644 --- a/consensus-types/payload-attestation/readonly_test.go +++ b/consensus-types/payload-attestation/readonly_test.go @@ -90,6 +90,7 @@ func TestSignature(t *testing.T){ } assert.Equal(t,sig,m.Signature()) } + func TestBeaconBlockRoot(t *testing.T){ root := make([]byte,fieldparams.RootLength) m:=&ReadOnlyPayloadAtt{ From b3d46cc1083a2df7edde05716b0beb148d06c7ec Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Thu, 25 Jul 2024 00:55:14 +0530 Subject: [PATCH 7/9] added files in Build.bazel --- consensus-types/payload-attestation/BUILD.bazel | 2 ++ 1 file changed, 2 insertions(+) diff --git a/consensus-types/payload-attestation/BUILD.bazel b/consensus-types/payload-attestation/BUILD.bazel index 92fe315ff9ac..8c9cccfeb9e6 100644 --- a/consensus-types/payload-attestation/BUILD.bazel +++ b/consensus-types/payload-attestation/BUILD.bazel @@ -7,8 +7,10 @@ go_library( "log.go", "mock.go", "readonly.go", + "readonly_test.go" "requirement.go", "verifier.go", + "verifier_test.go", ], importpath = "github.com/prysmaticlabs/prysm/v5/consensus-types/payload-attestation", visibility = ["//visibility:public"], From 0b9ba9f815be98615abdb02a929b889304e3baf5 Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Thu, 25 Jul 2024 02:39:35 +0530 Subject: [PATCH 8/9] fixed bazel suggestions --- .../payload-attestation/BUILD.bazel | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/consensus-types/payload-attestation/BUILD.bazel b/consensus-types/payload-attestation/BUILD.bazel index 8c9cccfeb9e6..bed90ae88137 100644 --- a/consensus-types/payload-attestation/BUILD.bazel +++ b/consensus-types/payload-attestation/BUILD.bazel @@ -1,4 +1,4 @@ -load("@prysm//tools/go:def.bzl", "go_library") +load("@prysm//tools/go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", @@ -7,10 +7,8 @@ go_library( "log.go", "mock.go", "readonly.go", - "readonly_test.go" "requirement.go", "verifier.go", - "verifier_test.go", ], importpath = "github.com/prysmaticlabs/prysm/v5/consensus-types/payload-attestation", visibility = ["//visibility:public"], @@ -23,3 +21,20 @@ go_library( "@com_github_sirupsen_logrus//:go_default_library", ], ) + +go_test( + name = "go_default_test", + srcs = [ + "readonly_test.go", + "verifier_test.go", + ], + embed = [":go_default_library"], + deps = [ + "//beacon-chain/startup:go_default_library", + "//config/fieldparams:go_default_library", + "//consensus-types/primitives:go_default_library", + "//proto/prysm/v1alpha1:go_default_library", + "//testing/assert:go_default_library", + "//testing/require:go_default_library", + ], +) From 14226233807aaa5715a0a1dc8bc671996f1a6fcb Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Sat, 27 Jul 2024 11:59:43 +0530 Subject: [PATCH 9/9] code-refactor --- .../payload-attestation/readonly_test.go | 16 ++++++++-------- .../payload-attestation/verifier_test.go | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/consensus-types/payload-attestation/readonly_test.go b/consensus-types/payload-attestation/readonly_test.go index 40f5d0a865d7..e790c3cbf0ef 100644 --- a/consensus-types/payload-attestation/readonly_test.go +++ b/consensus-types/payload-attestation/readonly_test.go @@ -72,8 +72,8 @@ func TestValidatePayload(t *testing.T){ } func TestValidatorIndex(t *testing.T){ - valIdx:=primitives.ValidatorIndex(1) - m:=&ReadOnlyPayloadAtt{ + valIdx := primitives.ValidatorIndex(1) + m := &ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ ValidatorIndex: valIdx, }, @@ -82,8 +82,8 @@ func TestValidatorIndex(t *testing.T){ } func TestSignature(t *testing.T){ - sig:=make([]byte,fieldparams.BLSSignatureLength) - m:=&ReadOnlyPayloadAtt{ + sig := make([]byte,fieldparams.BLSSignatureLength) + m := &ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ Signature: sig[:], }, @@ -93,7 +93,7 @@ func TestSignature(t *testing.T){ func TestBeaconBlockRoot(t *testing.T){ root := make([]byte,fieldparams.RootLength) - m:=&ReadOnlyPayloadAtt{ + m := &ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ Data: ðpb.PayloadAttestationData{ BeaconBlockRoot: root[:], @@ -104,8 +104,8 @@ func TestBeaconBlockRoot(t *testing.T){ } func TestSlot(t *testing.T){ - slot:=primitives.Slot(1) - m:=&ReadOnlyPayloadAtt{ + slot := primitives.Slot(1) + m := &ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ Data: ðpb.PayloadAttestationData{ Slot: slot, @@ -118,7 +118,7 @@ func TestSlot(t *testing.T){ func TestPayloadStatus(t *testing.T){ for i:=0;i<4;i++ { status:=primitives.PTCStatus(i) - m:=&ReadOnlyPayloadAtt{ + m := &ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ Data: ðpb.PayloadAttestationData{ PayloadStatus:status, diff --git a/consensus-types/payload-attestation/verifier_test.go b/consensus-types/payload-attestation/verifier_test.go index 6b837a9e75ba..1ca1dbe9005e 100644 --- a/consensus-types/payload-attestation/verifier_test.go +++ b/consensus-types/payload-attestation/verifier_test.go @@ -10,8 +10,8 @@ import ( ) func TestVerifyCurrentSlot(t *testing.T){ - clock:=&startup.Clock{} - verifier:=&PayloadVerifier{ + clock := &startup.Clock{} + verifier := &PayloadVerifier{ Resources: Resources{ clock: clock, }, @@ -20,8 +20,8 @@ func TestVerifyCurrentSlot(t *testing.T){ } func TestVerifyKnownPayloadStatus(t *testing.T){ - ptcstatus:= primitives.PTCStatus(4) - verifier:=&PayloadVerifier{ + ptcstatus := primitives.PTCStatus(4) + verifier := &PayloadVerifier{ payloadAtt: ReadOnlyPayloadAtt{ message: ðpb.PayloadAttestationMessage{ Data: ðpb.PayloadAttestationData{