Skip to content

Commit

Permalink
Payload attestation tests (#14242)
Browse files Browse the repository at this point in the history
  • Loading branch information
Redidacove authored Jul 29, 2024
1 parent bc4ea40 commit 53aaf2b
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 1 deletion.
19 changes: 18 additions & 1 deletion consensus-types/payload-attestation/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -21,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",
],
)
131 changes: 131 additions & 0 deletions consensus-types/payload-attestation/readonly_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package payloadattestation

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"
)


func TestValidatePayload(t *testing.T){
tests := []struct {
name string
bfunc func(t *testing.T) *ethpb.PayloadAttestationMessage
wanterr error
}{
{
name: "nil PayloadAttestationMessage",
bfunc: func(t *testing.T) *ethpb.PayloadAttestationMessage {
return nil
},
wanterr: errNilPayloadMessage,
},
{
name: "nil data",
bfunc: func(t *testing.T) *ethpb.PayloadAttestationMessage {
return &ethpb.PayloadAttestationMessage{
Data: nil,
}
},
wanterr: errNilPayloadData,
},
{
name: "nil signature",
bfunc: func(t *testing.T) *ethpb.PayloadAttestationMessage {
return &ethpb.PayloadAttestationMessage{
Signature: nil,
}
},
wanterr: errMissingPayloadSignature,
},
{
name: "Correct PayloadAttestationMessage",
bfunc: func(t *testing.T) *ethpb.PayloadAttestationMessage {
return &ethpb.PayloadAttestationMessage{
Signature: make([]byte, fieldparams.BLSSignatureLength),
Data: &ethpb.PayloadAttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
},
}
},
wanterr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name+" ReadOnlyPayloadAtt", func(t *testing.T) {
m := tt.bfunc(t)
err := validatePayload(m)
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)
}
})
}
}

func TestValidatorIndex(t *testing.T){
valIdx := primitives.ValidatorIndex(1)
m := &ReadOnlyPayloadAtt{
message: &ethpb.PayloadAttestationMessage{
ValidatorIndex: valIdx,
},
}
assert.Equal(t,valIdx,m.ValidatorIndex())
}

func TestSignature(t *testing.T){
sig := make([]byte,fieldparams.BLSSignatureLength)
m := &ReadOnlyPayloadAtt{
message: &ethpb.PayloadAttestationMessage{
Signature: sig[:],
},
}
assert.Equal(t,sig,m.Signature())
}

func TestBeaconBlockRoot(t *testing.T){
root := make([]byte,fieldparams.RootLength)
m := &ReadOnlyPayloadAtt{
message: &ethpb.PayloadAttestationMessage{
Data: &ethpb.PayloadAttestationData{
BeaconBlockRoot: root[:],
},
},
}
assert.Equal(t,root,m.BeaconBlockRoot())
}

func TestSlot(t *testing.T){
slot := primitives.Slot(1)
m := &ReadOnlyPayloadAtt{
message: &ethpb.PayloadAttestationMessage{
Data: &ethpb.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: &ethpb.PayloadAttestationMessage{
Data: &ethpb.PayloadAttestationData{
PayloadStatus:status,
},
},
}
require.NoError(t,validatePayload(m.message))
assert.Equal(t,status,m.PayloadStatus())
}
}
34 changes: 34 additions & 0 deletions consensus-types/payload-attestation/verifier_test.go
Original file line number Diff line number Diff line change
@@ -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: &ethpb.PayloadAttestationMessage{
Data: &ethpb.PayloadAttestationData{
PayloadStatus:ptcstatus,
},
},
},
}
assert.Equal(t, verifier.VerifyKnownPayloadStatus(), ErrUnknownPayloadStatus)
}

0 comments on commit 53aaf2b

Please sign in to comment.