mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-24 20:37:17 +00:00
Fix Signature Verification for Attestations (#3080)
* fix bug and test * terence's review
This commit is contained in:
parent
956b07f5c1
commit
8cbd1097d7
@ -1178,11 +1178,7 @@ func TestProcessAttestations_OK(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestConvertToIndexed_OK(t *testing.T) {
|
||||
helpers.ClearActiveIndicesCache()
|
||||
helpers.ClearActiveCountCache()
|
||||
helpers.ClearStartShardCache()
|
||||
helpers.ClearShuffledValidatorCache()
|
||||
|
||||
helpers.ClearAllCaches()
|
||||
if params.BeaconConfig().SlotsPerEpoch != 64 {
|
||||
t.Errorf("SlotsPerEpoch should be 64 for these tests to pass")
|
||||
}
|
||||
@ -1259,6 +1255,101 @@ func TestConvertToIndexed_OK(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyIndexedAttestation_OK(t *testing.T) {
|
||||
helpers.ClearAllCaches()
|
||||
if params.BeaconConfig().SlotsPerEpoch != 64 {
|
||||
t.Errorf("SlotsPerEpoch should be 64 for these tests to pass")
|
||||
}
|
||||
numOfValidators := 2 * params.BeaconConfig().SlotsPerEpoch
|
||||
validators := make([]*ethpb.Validator, numOfValidators)
|
||||
_, keys := testutil.SetupInitialDeposits(t, numOfValidators)
|
||||
for i := 0; i < len(validators); i++ {
|
||||
validators[i] = ðpb.Validator{
|
||||
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
||||
PublicKey: keys[i].PublicKey().Marshal(),
|
||||
}
|
||||
}
|
||||
|
||||
state := &pb.BeaconState{
|
||||
Slot: 5,
|
||||
Validators: validators,
|
||||
Fork: &pb.Fork{
|
||||
Epoch: 0,
|
||||
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
||||
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
||||
},
|
||||
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
|
||||
ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
|
||||
}
|
||||
tests := []struct {
|
||||
attestation *ethpb.IndexedAttestation
|
||||
}{
|
||||
{attestation: ðpb.IndexedAttestation{
|
||||
Data: ðpb.AttestationData{
|
||||
Target: ðpb.Checkpoint{
|
||||
Epoch: 2,
|
||||
},
|
||||
},
|
||||
CustodyBit_0Indices: []uint64{1},
|
||||
}},
|
||||
{attestation: ðpb.IndexedAttestation{
|
||||
Data: ðpb.AttestationData{
|
||||
Target: ðpb.Checkpoint{
|
||||
Epoch: 1,
|
||||
},
|
||||
},
|
||||
CustodyBit_0Indices: []uint64{47, 99},
|
||||
}},
|
||||
{attestation: ðpb.IndexedAttestation{
|
||||
Data: ðpb.AttestationData{
|
||||
Target: ðpb.Checkpoint{
|
||||
Epoch: 4,
|
||||
},
|
||||
},
|
||||
CustodyBit_0Indices: []uint64{21, 72},
|
||||
}},
|
||||
{attestation: ðpb.IndexedAttestation{
|
||||
Data: ðpb.AttestationData{
|
||||
Target: ðpb.Checkpoint{
|
||||
Epoch: 7,
|
||||
},
|
||||
},
|
||||
CustodyBit_0Indices: []uint64{100, 121},
|
||||
}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
helpers.ClearAllCaches()
|
||||
|
||||
attDataAndCustodyBit := &pb.AttestationDataAndCustodyBit{
|
||||
Data: tt.attestation.Data,
|
||||
CustodyBit: false,
|
||||
}
|
||||
|
||||
domain := helpers.Domain(state, tt.attestation.Data.Target.Epoch, params.BeaconConfig().DomainAttestation)
|
||||
|
||||
root, err := ssz.HashTreeRoot(attDataAndCustodyBit)
|
||||
if err != nil {
|
||||
t.Errorf("Could not find the ssz root: %v", err)
|
||||
continue
|
||||
}
|
||||
var sig []*bls.Signature
|
||||
for _, idx := range tt.attestation.CustodyBit_0Indices {
|
||||
validatorSig := keys[idx].Sign(root[:], domain)
|
||||
sig = append(sig, validatorSig)
|
||||
}
|
||||
aggSig := bls.AggregateSignatures(sig)
|
||||
marshalledSig := aggSig.Marshal()
|
||||
|
||||
tt.attestation.Signature = marshalledSig
|
||||
|
||||
err = blocks.VerifyIndexedAttestation(state, tt.attestation)
|
||||
if err != nil {
|
||||
t.Errorf("failed to verify indexed attestation: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateIndexedAttestation_AboveMaxLength(t *testing.T) {
|
||||
indexedAtt1 := ðpb.IndexedAttestation{
|
||||
CustodyBit_0Indices: make([]uint64, params.BeaconConfig().MaxValidatorsPerCommittee+5),
|
||||
|
@ -88,7 +88,7 @@ func (v *validator) AttestToBlockHead(ctx context.Context, slot uint64, pk strin
|
||||
aggregationBitfield := bitfield.NewBitlist(uint64(len(assignment.Committee)))
|
||||
aggregationBitfield.SetBitAt(indexInCommittee, true)
|
||||
|
||||
domain, err := v.validatorClient.DomainData(ctx, &pb.DomainRequest{Epoch: data.Target.Epoch, Domain: params.BeaconConfig().DomainBeaconProposer})
|
||||
domain, err := v.validatorClient.DomainData(ctx, &pb.DomainRequest{Epoch: data.Target.Epoch, Domain: params.BeaconConfig().DomainAttestation})
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to get domain data from beacon node")
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user