Move cheaper operations earlier into code path (#5835)

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Ivan Martinez 2020-05-13 02:37:24 -04:00 committed by GitHub
parent 578703fa05
commit ca7e057ca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,19 +97,20 @@ func (ds *Service) detectDoubleVote(
continue continue
} }
// If there are no shared indices, there is no validator to slash. if !isDoubleVote(incomingAtt, att) {
if len(sliceutil.IntersectionUint64(att.AttestingIndices, []uint64{detectionResult.ValidatorIndex})) == 0 {
continue continue
} }
if isDoubleVote(incomingAtt, att) { // If there are no shared indices, there is no validator to slash.
doubleVotesDetected.Inc() if !sliceutil.IsInUint64(detectionResult.ValidatorIndex, att.AttestingIndices) {
return &ethpb.AttesterSlashing{ continue
Attestation_1: incomingAtt,
Attestation_2: att,
}, nil
} }
doubleVotesDetected.Inc()
return &ethpb.AttesterSlashing{
Attestation_1: incomingAtt,
Attestation_2: att,
}, nil
} }
return nil, nil return nil, nil
} }
@ -135,20 +136,25 @@ func (ds *Service) detectSurroundVotes(
if att.Data == nil { if att.Data == nil {
continue continue
} }
isSurround := isSurrounding(incomingAtt, att)
isSurrounded := isSurrounding(att, incomingAtt)
if !isSurround && !isSurrounded {
continue
}
// If there are no shared indices, there is no validator to slash. // If there are no shared indices, there is no validator to slash.
if len(sliceutil.IntersectionUint64(att.AttestingIndices, []uint64{detectionResult.ValidatorIndex})) == 0 { if !sliceutil.IsInUint64(detectionResult.ValidatorIndex, att.AttestingIndices) {
continue continue
} }
// Slashings must be submitted as the incoming attestation surrounding the saved attestation. // Slashings must be submitted as the incoming attestation surrounding the saved attestation.
// So we swap the order if needed. // So we swap the order if needed.
if isSurrounding(incomingAtt, att) { if isSurround {
surroundingVotesDetected.Inc() surroundingVotesDetected.Inc()
return &ethpb.AttesterSlashing{ return &ethpb.AttesterSlashing{
Attestation_1: incomingAtt, Attestation_1: incomingAtt,
Attestation_2: att, Attestation_2: att,
}, nil }, nil
} else if isSurrounding(att, incomingAtt) { } else if isSurrounded {
surroundedVotesDetected.Inc() surroundedVotesDetected.Inc()
return &ethpb.AttesterSlashing{ return &ethpb.AttesterSlashing{
Attestation_1: att, Attestation_1: att,