From fcd0dff81a7548e8447ad815a5b0f303fa482955 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Wed, 16 Sep 2020 11:43:59 -0700 Subject: [PATCH 1/2] Check that the headers are equal while ignoring the signature for proposer slashings. Resolves https://github.com/sigp/beacon-fuzz/issues/74 --- beacon-chain/core/blocks/proposer_slashing.go | 2 +- .../core/blocks/proposer_slashing_test.go | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/beacon-chain/core/blocks/proposer_slashing.go b/beacon-chain/core/blocks/proposer_slashing.go index cbc05bf486be..348029f9f4b0 100644 --- a/beacon-chain/core/blocks/proposer_slashing.go +++ b/beacon-chain/core/blocks/proposer_slashing.go @@ -79,7 +79,7 @@ func VerifyProposerSlashing( if pIdx != slashing.Header_2.Header.ProposerIndex { return fmt.Errorf("mismatched indices, received %d == %d", slashing.Header_1.Header.ProposerIndex, slashing.Header_2.Header.ProposerIndex) } - if proto.Equal(slashing.Header_1, slashing.Header_2) { + if proto.Equal(slashing.Header_1.Header, slashing.Header_2.Header) { return errors.New("expected slashing headers to differ") } proposer, err := beaconState.ValidatorAtIndexReadOnly(slashing.Header_1.Header.ProposerIndex) diff --git a/beacon-chain/core/blocks/proposer_slashing_test.go b/beacon-chain/core/blocks/proposer_slashing_test.go index 3a899ed4d660..d1e77f10066e 100644 --- a/beacon-chain/core/blocks/proposer_slashing_test.go +++ b/beacon-chain/core/blocks/proposer_slashing_test.go @@ -10,6 +10,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -181,3 +182,73 @@ func TestProcessProposerSlashings_AppliesCorrectStatus(t *testing.T) { newStateVals[1].ExitEpoch, beaconState.Validators()[1].ExitEpoch) } } + +func TestVerifyProposerSlashing(t *testing.T) { + type args struct { + beaconState *stateTrie.BeaconState + slashing *ethpb.ProposerSlashing + } + + beaconState, _ := testutil.DeterministicGenesisState(t, 2) + currentSlot := uint64(0) + require.NoError(t, beaconState.SetSlot(currentSlot)) + + tests := []struct { + name string + args args + wantErr string + }{ + { + name: "same header, no signatures", + args: args{ + slashing: ðpb.ProposerSlashing{ + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + Slot: 0, + }, + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + Slot: 0, + }, + }, + }, + beaconState: beaconState, + }, + wantErr: "expected slashing headers to differ", + }, + { // Regression test for https://github.com/sigp/beacon-fuzz/issues/74 + name: "same header, different signatures", + args: args{ + slashing: ðpb.ProposerSlashing{ + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + Slot: 0, + }, + Signature: bls.RandKey().Sign([]byte("foo")).Marshal(), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + Slot: 0, + }, + Signature: bls.RandKey().Sign([]byte("bar")).Marshal(), + }, + }, + beaconState: beaconState, + }, + wantErr: "expected slashing headers to differ", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + testutil.ResetCache() + if err := blocks.VerifyProposerSlashing(tt.args.beaconState, tt.args.slashing); (err != nil || tt.wantErr != "") && err.Error() != tt.wantErr { + t.Errorf("VerifyProposerSlashing() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From 3158250e8aaa06e7eb88ed617f3072e33198b387 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Wed, 16 Sep 2020 13:12:32 -0700 Subject: [PATCH 2/2] fix test --- beacon-chain/core/blocks/proposer_slashing_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/beacon-chain/core/blocks/proposer_slashing_test.go b/beacon-chain/core/blocks/proposer_slashing_test.go index d1e77f10066e..cddcce2d1080 100644 --- a/beacon-chain/core/blocks/proposer_slashing_test.go +++ b/beacon-chain/core/blocks/proposer_slashing_test.go @@ -100,6 +100,7 @@ func TestProcessProposerSlashings_ValidatorNotSlashable(t *testing.T) { Header: ðpb.BeaconBlockHeader{ ProposerIndex: 0, Slot: 0, + BodyRoot: []byte("foo"), }, Signature: bytesutil.PadTo([]byte("A"), 96), }, @@ -107,6 +108,7 @@ func TestProcessProposerSlashings_ValidatorNotSlashable(t *testing.T) { Header: ðpb.BeaconBlockHeader{ ProposerIndex: 0, Slot: 0, + BodyRoot: []byte("bar"), }, Signature: bytesutil.PadTo([]byte("B"), 96), },