From fce69712c468fb72a41d64f8e59c7e1a4ba3d39f Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Sat, 3 Aug 2024 22:33:20 +0530 Subject: [PATCH 1/7] test-added --- .../epbs/indexed_payload_attestation_test.go | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 consensus-types/epbs/indexed_payload_attestation_test.go diff --git a/consensus-types/epbs/indexed_payload_attestation_test.go b/consensus-types/epbs/indexed_payload_attestation_test.go new file mode 100644 index 000000000000..3653e03f0c9b --- /dev/null +++ b/consensus-types/epbs/indexed_payload_attestation_test.go @@ -0,0 +1,60 @@ +package epbs + +import ( + "testing" + + "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" + eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" + "github.com/prysmaticlabs/prysm/v5/testing/require" +) + +func TestGetAttestingIndices(t *testing.T) { + attestingIndices := []primitives.ValidatorIndex{1, 2, 3} + nilAttestingIndices := []primitives.ValidatorIndex{} + pa := &IndexedPayloadAttestation{ + AttestingIndices: attestingIndices, + } + got := pa.GetAttestingIndices() + for i, v := range got { + require.Equal(t, attestingIndices[i], v) + } + pa = &IndexedPayloadAttestation{ + AttestingIndices: nilAttestingIndices, + } + got = pa.GetAttestingIndices() + require.DeepEqual(t, nilAttestingIndices, got) +} + +func TestGetData(t *testing.T) { + data := ð.PayloadAttestationData{ + Slot: 1, + } + nilData := ð.PayloadAttestationData{} + pa := &IndexedPayloadAttestation{ + Data: data, + } + got := pa.GetData() + require.Equal(t, data, got) + + pa = &IndexedPayloadAttestation{ + Data: nilData, + } + got = pa.GetData() + require.Equal(t, nilData, got) +} + +func TestGetSignature(t *testing.T) { + sig := []byte{2} + nilSig := []byte{} + pa := &IndexedPayloadAttestation{ + Signature: sig, + } + got := pa.GetSignature() + require.DeepEqual(t, sig, got) + + pa = &IndexedPayloadAttestation{ + Signature: nilSig, + } + got = pa.GetSignature() + require.DeepEqual(t, nilSig, got) +} From 800f235df8486ec6abeb9d5e1b574c8abb03d5fc Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Sat, 3 Aug 2024 23:46:26 +0530 Subject: [PATCH 2/7] nil check fix --- .../epbs/indexed_payload_attestation_test.go | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/consensus-types/epbs/indexed_payload_attestation_test.go b/consensus-types/epbs/indexed_payload_attestation_test.go index 3653e03f0c9b..cd81ae50ed81 100644 --- a/consensus-types/epbs/indexed_payload_attestation_test.go +++ b/consensus-types/epbs/indexed_payload_attestation_test.go @@ -10,17 +10,13 @@ import ( func TestGetAttestingIndices(t *testing.T) { attestingIndices := []primitives.ValidatorIndex{1, 2, 3} - nilAttestingIndices := []primitives.ValidatorIndex{} + nilAttestingIndices := []primitives.ValidatorIndex(nil) pa := &IndexedPayloadAttestation{ AttestingIndices: attestingIndices, } got := pa.GetAttestingIndices() - for i, v := range got { - require.Equal(t, attestingIndices[i], v) - } - pa = &IndexedPayloadAttestation{ - AttestingIndices: nilAttestingIndices, - } + require.DeepEqual(t, attestingIndices, got) + pa = nil got = pa.GetAttestingIndices() require.DeepEqual(t, nilAttestingIndices, got) } @@ -29,32 +25,28 @@ func TestGetData(t *testing.T) { data := ð.PayloadAttestationData{ Slot: 1, } - nilData := ð.PayloadAttestationData{} + nilData := (*eth.PayloadAttestationData)(nil) pa := &IndexedPayloadAttestation{ Data: data, } got := pa.GetData() require.Equal(t, data, got) - pa = &IndexedPayloadAttestation{ - Data: nilData, - } + pa = nil got = pa.GetData() - require.Equal(t, nilData, got) + require.DeepEqual(t, got, nilData) } func TestGetSignature(t *testing.T) { sig := []byte{2} - nilSig := []byte{} + nilSig := []byte(nil) pa := &IndexedPayloadAttestation{ Signature: sig, } got := pa.GetSignature() require.DeepEqual(t, sig, got) - pa = &IndexedPayloadAttestation{ - Signature: nilSig, - } + pa = nil got = pa.GetSignature() - require.DeepEqual(t, nilSig, got) + require.DeepEqual(t, got, nilSig) } From 1113b7c709ac823cc7035c85eed7ce7f1c446b4b Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Sun, 4 Aug 2024 00:03:54 +0530 Subject: [PATCH 3/7] randomized inputs --- .../epbs/indexed_payload_attestation_test.go | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/consensus-types/epbs/indexed_payload_attestation_test.go b/consensus-types/epbs/indexed_payload_attestation_test.go index cd81ae50ed81..a4ce23e27b80 100644 --- a/consensus-types/epbs/indexed_payload_attestation_test.go +++ b/consensus-types/epbs/indexed_payload_attestation_test.go @@ -1,6 +1,8 @@ package epbs import ( + "crypto/rand" + "encoding/binary" "testing" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" @@ -9,7 +11,7 @@ import ( ) func TestGetAttestingIndices(t *testing.T) { - attestingIndices := []primitives.ValidatorIndex{1, 2, 3} + attestingIndices := []primitives.ValidatorIndex{primitives.ValidatorIndex(randomUint64(t)), primitives.ValidatorIndex(randomUint64(t)), primitives.ValidatorIndex(randomUint64(t))} nilAttestingIndices := []primitives.ValidatorIndex(nil) pa := &IndexedPayloadAttestation{ AttestingIndices: attestingIndices, @@ -23,7 +25,7 @@ func TestGetAttestingIndices(t *testing.T) { func TestGetData(t *testing.T) { data := ð.PayloadAttestationData{ - Slot: 1, + Slot: primitives.Slot(randomUint64(t)), } nilData := (*eth.PayloadAttestationData)(nil) pa := &IndexedPayloadAttestation{ @@ -38,7 +40,7 @@ func TestGetData(t *testing.T) { } func TestGetSignature(t *testing.T) { - sig := []byte{2} + sig := randomBytes(32, t) nilSig := []byte(nil) pa := &IndexedPayloadAttestation{ Signature: sig, @@ -50,3 +52,19 @@ func TestGetSignature(t *testing.T) { got = pa.GetSignature() require.DeepEqual(t, got, nilSig) } + +func randomUint64(t *testing.T) uint64 { + var num uint64 + b := randomBytes(8, t) + num = binary.BigEndian.Uint64(b) + return num +} + +func randomBytes(n int, t *testing.T) []byte { + b := make([]byte, n) + _, err := rand.Read(b) + if err != nil { + t.Fatalf("Failed to generate random bytes: %v", err) + } + return b +} From ccbae67cd914f015a8a91a6ef5d1e76993c00ded Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Mon, 5 Aug 2024 21:08:15 +0530 Subject: [PATCH 4/7] hardcoded inputs --- .../epbs/indexed_payload_attestation_test.go | 25 +++---------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/consensus-types/epbs/indexed_payload_attestation_test.go b/consensus-types/epbs/indexed_payload_attestation_test.go index a4ce23e27b80..73c5baa426b0 100644 --- a/consensus-types/epbs/indexed_payload_attestation_test.go +++ b/consensus-types/epbs/indexed_payload_attestation_test.go @@ -1,8 +1,6 @@ package epbs import ( - "crypto/rand" - "encoding/binary" "testing" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" @@ -11,7 +9,8 @@ import ( ) func TestGetAttestingIndices(t *testing.T) { - attestingIndices := []primitives.ValidatorIndex{primitives.ValidatorIndex(randomUint64(t)), primitives.ValidatorIndex(randomUint64(t)), primitives.ValidatorIndex(randomUint64(t))} + attestingIndices := []primitives.ValidatorIndex{1, 2, 3} + nilAttestingIndices := []primitives.ValidatorIndex(nil) pa := &IndexedPayloadAttestation{ AttestingIndices: attestingIndices, @@ -25,7 +24,7 @@ func TestGetAttestingIndices(t *testing.T) { func TestGetData(t *testing.T) { data := ð.PayloadAttestationData{ - Slot: primitives.Slot(randomUint64(t)), + Slot: primitives.Slot(1), } nilData := (*eth.PayloadAttestationData)(nil) pa := &IndexedPayloadAttestation{ @@ -40,7 +39,7 @@ func TestGetData(t *testing.T) { } func TestGetSignature(t *testing.T) { - sig := randomBytes(32, t) + sig := []byte{1, 2, 3} nilSig := []byte(nil) pa := &IndexedPayloadAttestation{ Signature: sig, @@ -52,19 +51,3 @@ func TestGetSignature(t *testing.T) { got = pa.GetSignature() require.DeepEqual(t, got, nilSig) } - -func randomUint64(t *testing.T) uint64 { - var num uint64 - b := randomBytes(8, t) - num = binary.BigEndian.Uint64(b) - return num -} - -func randomBytes(n int, t *testing.T) []byte { - b := make([]byte, n) - _, err := rand.Read(b) - if err != nil { - t.Fatalf("Failed to generate random bytes: %v", err) - } - return b -} From 993a0be9974146dc1cbd9f072f59069a81346c78 Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Mon, 5 Aug 2024 21:54:07 +0530 Subject: [PATCH 5/7] suggestions applied --- .../epbs/indexed_payload_attestation_test.go | 123 +++++++++++++----- 1 file changed, 92 insertions(+), 31 deletions(-) diff --git a/consensus-types/epbs/indexed_payload_attestation_test.go b/consensus-types/epbs/indexed_payload_attestation_test.go index 73c5baa426b0..fa79199687a0 100644 --- a/consensus-types/epbs/indexed_payload_attestation_test.go +++ b/consensus-types/epbs/indexed_payload_attestation_test.go @@ -9,45 +9,106 @@ import ( ) func TestGetAttestingIndices(t *testing.T) { - attestingIndices := []primitives.ValidatorIndex{1, 2, 3} - - nilAttestingIndices := []primitives.ValidatorIndex(nil) - pa := &IndexedPayloadAttestation{ - AttestingIndices: attestingIndices, + testCases := []struct { + name string + attestingIndices []primitives.ValidatorIndex + }{ + { + name: "Test 1", + attestingIndices: []primitives.ValidatorIndex{1, 2, 3}, + }, + { + name: "Test 2", + attestingIndices: []primitives.ValidatorIndex{55,66,787}, + }, + { + name: "Empty AttestingIndices", + attestingIndices: []primitives.ValidatorIndex{}, + }, + { + name: "Nil AttestingIndices", + attestingIndices: []primitives.ValidatorIndex(nil), + }, + } + for _, tt := range testCases { + t.Run(tt.name, func(t *testing.T) { + pa := &IndexedPayloadAttestation{ + AttestingIndices: tt.attestingIndices, + } + got := pa.GetAttestingIndices() + require.DeepEqual(t, tt.attestingIndices, got) + }) } - got := pa.GetAttestingIndices() - require.DeepEqual(t, attestingIndices, got) - pa = nil - got = pa.GetAttestingIndices() - require.DeepEqual(t, nilAttestingIndices, got) } func TestGetData(t *testing.T) { - data := ð.PayloadAttestationData{ - Slot: primitives.Slot(1), + testCases := []struct { + name string + data *eth.PayloadAttestationData + }{ + { + name: "Test 1", + data: ð.PayloadAttestationData{ + Slot: primitives.Slot(1), + }, + }, + { + name: "Test 2", + data: ð.PayloadAttestationData{ + Slot: primitives.Slot(100), + }, + }, + { + name: "Zero slot", + data: ð.PayloadAttestationData{ + Slot: primitives.Slot(0), + }, + }, + { + name: "Nil AttestingIndices", + data: nil, + }, } - nilData := (*eth.PayloadAttestationData)(nil) - pa := &IndexedPayloadAttestation{ - Data: data, + for _, tt := range testCases { + t.Run(tt.name, func(t *testing.T) { + pa := &IndexedPayloadAttestation{ + Data: tt.data, + } + got := pa.GetData() + require.DeepEqual(t, tt.data, got) + }) } - got := pa.GetData() - require.Equal(t, data, got) - - pa = nil - got = pa.GetData() - require.DeepEqual(t, got, nilData) } func TestGetSignature(t *testing.T) { - sig := []byte{1, 2, 3} - nilSig := []byte(nil) - pa := &IndexedPayloadAttestation{ - Signature: sig, + testCases := []struct { + name string + sig []byte + }{ + { + name: "Test 1", + sig: []byte{1, 2}, + }, + { + name: "Test 1", + sig: []byte{29, 100}, + }, + { + name: "Zero slot", + sig: []byte{0}, + }, + { + name: "Nil signature", + sig: nil, + }, + } + for _, tt := range testCases { + t.Run(tt.name, func(t *testing.T) { + pa := &IndexedPayloadAttestation{ + Signature: tt.sig, + } + got := pa.GetSignature() + require.DeepEqual(t, tt.sig, got) + }) } - got := pa.GetSignature() - require.DeepEqual(t, sig, got) - - pa = nil - got = pa.GetSignature() - require.DeepEqual(t, got, nilSig) } From a31091bf2c0490ecca50bed1b71a12a7956ea52d Mon Sep 17 00:00:00 2001 From: Md Amaan Date: Mon, 5 Aug 2024 22:32:08 +0530 Subject: [PATCH 6/7] minor-typo fixed --- consensus-types/epbs/__debug_bin2234641590.exe | 0 consensus-types/epbs/indexed_payload_attestation_test.go | 8 ++++---- 2 files changed, 4 insertions(+), 4 deletions(-) create mode 100644 consensus-types/epbs/__debug_bin2234641590.exe diff --git a/consensus-types/epbs/__debug_bin2234641590.exe b/consensus-types/epbs/__debug_bin2234641590.exe new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/consensus-types/epbs/indexed_payload_attestation_test.go b/consensus-types/epbs/indexed_payload_attestation_test.go index fa79199687a0..1b9c4e5c6d25 100644 --- a/consensus-types/epbs/indexed_payload_attestation_test.go +++ b/consensus-types/epbs/indexed_payload_attestation_test.go @@ -19,7 +19,7 @@ func TestGetAttestingIndices(t *testing.T) { }, { name: "Test 2", - attestingIndices: []primitives.ValidatorIndex{55,66,787}, + attestingIndices: []primitives.ValidatorIndex{55, 66, 787}, }, { name: "Empty AttestingIndices", @@ -65,7 +65,7 @@ func TestGetData(t *testing.T) { }, }, { - name: "Nil AttestingIndices", + name: "Nil slot", data: nil, }, } @@ -90,11 +90,11 @@ func TestGetSignature(t *testing.T) { sig: []byte{1, 2}, }, { - name: "Test 1", + name: "Test 2", sig: []byte{29, 100}, }, { - name: "Zero slot", + name: "Zero signature", sig: []byte{0}, }, { From 1ce391af23cf1e7efdef27dfd764dba706cde229 Mon Sep 17 00:00:00 2001 From: Md Amaan <114795592+Redidacove@users.noreply.github.com> Date: Mon, 5 Aug 2024 22:39:50 +0530 Subject: [PATCH 7/7] deleted --- consensus-types/epbs/__debug_bin2234641590.exe | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 consensus-types/epbs/__debug_bin2234641590.exe diff --git a/consensus-types/epbs/__debug_bin2234641590.exe b/consensus-types/epbs/__debug_bin2234641590.exe deleted file mode 100644 index e69de29bb2d1..000000000000