Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payload attestation tests #14242

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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){
Redidacove marked this conversation as resolved.
Show resolved Hide resolved
sig := make([]byte,fieldparams.BLSSignatureLength)
m := &ReadOnlyPayloadAtt{
message: &ethpb.PayloadAttestationMessage{
Signature: sig[:],
},
}
assert.Equal(t,sig,m.Signature())
}

func TestBeaconBlockRoot(t *testing.T){
Redidacove marked this conversation as resolved.
Show resolved Hide resolved
Redidacove marked this conversation as resolved.
Show resolved Hide resolved
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){
Redidacove marked this conversation as resolved.
Show resolved Hide resolved
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)
}
Loading