|
| 1 | +//! These two `batch_...` functions provide verification of batches of attestations. They provide |
| 2 | +//! significant CPU-time savings by performing batch verification of BLS signatures. |
| 3 | +//! |
| 4 | +//! In each function, attestations are "indexed" (i.e., the `IndexedAttestation` is computed), to |
| 5 | +//! determine if they should progress to signature verification. Then, all attestations which were |
| 6 | +//! successfully indexed have their signatures verified in a batch. If that signature batch fails |
| 7 | +//! then all attestation signatures are verified independently. |
| 8 | +//! |
| 9 | +//! The outcome of each function is a `Vec<Result>` with a one-to-one mapping to the attestations |
| 10 | +//! supplied as input. Each result provides the exact success or failure result of the corresponding |
| 11 | +//! attestation, with no loss of fidelity when compared to individual verification. |
| 12 | +use super::{ |
| 13 | + CheckAttestationSignature, Error, IndexedAggregatedAttestation, IndexedUnaggregatedAttestation, |
| 14 | + VerifiedAggregatedAttestation, VerifiedUnaggregatedAttestation, |
| 15 | +}; |
| 16 | +use crate::{ |
| 17 | + beacon_chain::VALIDATOR_PUBKEY_CACHE_LOCK_TIMEOUT, metrics, BeaconChain, BeaconChainError, |
| 18 | + BeaconChainTypes, |
| 19 | +}; |
| 20 | +use bls::verify_signature_sets; |
| 21 | +use state_processing::signature_sets::{ |
| 22 | + indexed_attestation_signature_set_from_pubkeys, signed_aggregate_selection_proof_signature_set, |
| 23 | + signed_aggregate_signature_set, |
| 24 | +}; |
| 25 | +use std::borrow::Cow; |
| 26 | +use types::*; |
| 27 | + |
| 28 | +/// Verify aggregated attestations using batch BLS signature verification. |
| 29 | +/// |
| 30 | +/// See module-level docs for more info. |
| 31 | +pub fn batch_verify_aggregated_attestations<'a, T, I>( |
| 32 | + aggregates: I, |
| 33 | + chain: &BeaconChain<T>, |
| 34 | +) -> Result<Vec<Result<VerifiedAggregatedAttestation<'a, T>, Error>>, Error> |
| 35 | +where |
| 36 | + T: BeaconChainTypes, |
| 37 | + I: Iterator<Item = &'a SignedAggregateAndProof<T::EthSpec>> + ExactSizeIterator, |
| 38 | +{ |
| 39 | + let mut num_indexed = 0; |
| 40 | + let mut num_failed = 0; |
| 41 | + |
| 42 | + // Perform indexing of all attestations, collecting the results. |
| 43 | + let indexing_results = aggregates |
| 44 | + .map(|aggregate| { |
| 45 | + let result = IndexedAggregatedAttestation::verify(aggregate, chain); |
| 46 | + if result.is_ok() { |
| 47 | + num_indexed += 1; |
| 48 | + } else { |
| 49 | + num_failed += 1; |
| 50 | + } |
| 51 | + result |
| 52 | + }) |
| 53 | + .collect::<Vec<_>>(); |
| 54 | + |
| 55 | + // May be set to `No` if batch verification succeeds. |
| 56 | + let mut check_signatures = CheckAttestationSignature::Yes; |
| 57 | + |
| 58 | + // Perform batch BLS verification, if any attestation signatures are worth checking. |
| 59 | + if num_indexed > 0 { |
| 60 | + let signature_setup_timer = |
| 61 | + metrics::start_timer(&metrics::ATTESTATION_PROCESSING_BATCH_AGG_SIGNATURE_SETUP_TIMES); |
| 62 | + |
| 63 | + let pubkey_cache = chain |
| 64 | + .validator_pubkey_cache |
| 65 | + .try_read_for(VALIDATOR_PUBKEY_CACHE_LOCK_TIMEOUT) |
| 66 | + .ok_or(BeaconChainError::ValidatorPubkeyCacheLockTimeout)?; |
| 67 | + |
| 68 | + let fork = chain.with_head(|head| Ok::<_, BeaconChainError>(head.beacon_state.fork()))?; |
| 69 | + |
| 70 | + let mut signature_sets = Vec::with_capacity(num_indexed * 3); |
| 71 | + |
| 72 | + // Iterate, flattening to get only the `Ok` values. |
| 73 | + for indexed in indexing_results.iter().flatten() { |
| 74 | + let signed_aggregate = &indexed.signed_aggregate; |
| 75 | + let indexed_attestation = &indexed.indexed_attestation; |
| 76 | + |
| 77 | + signature_sets.push( |
| 78 | + signed_aggregate_selection_proof_signature_set( |
| 79 | + |validator_index| pubkey_cache.get(validator_index).map(Cow::Borrowed), |
| 80 | + signed_aggregate, |
| 81 | + &fork, |
| 82 | + chain.genesis_validators_root, |
| 83 | + &chain.spec, |
| 84 | + ) |
| 85 | + .map_err(BeaconChainError::SignatureSetError)?, |
| 86 | + ); |
| 87 | + signature_sets.push( |
| 88 | + signed_aggregate_signature_set( |
| 89 | + |validator_index| pubkey_cache.get(validator_index).map(Cow::Borrowed), |
| 90 | + signed_aggregate, |
| 91 | + &fork, |
| 92 | + chain.genesis_validators_root, |
| 93 | + &chain.spec, |
| 94 | + ) |
| 95 | + .map_err(BeaconChainError::SignatureSetError)?, |
| 96 | + ); |
| 97 | + signature_sets.push( |
| 98 | + indexed_attestation_signature_set_from_pubkeys( |
| 99 | + |validator_index| pubkey_cache.get(validator_index).map(Cow::Borrowed), |
| 100 | + &indexed_attestation.signature, |
| 101 | + indexed_attestation, |
| 102 | + &fork, |
| 103 | + chain.genesis_validators_root, |
| 104 | + &chain.spec, |
| 105 | + ) |
| 106 | + .map_err(BeaconChainError::SignatureSetError)?, |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + metrics::stop_timer(signature_setup_timer); |
| 111 | + |
| 112 | + let _signature_verification_timer = |
| 113 | + metrics::start_timer(&metrics::ATTESTATION_PROCESSING_BATCH_AGG_SIGNATURE_TIMES); |
| 114 | + |
| 115 | + if verify_signature_sets(signature_sets.iter()) { |
| 116 | + // Since all the signatures verified in a batch, there's no reason for them to be |
| 117 | + // checked again later. |
| 118 | + check_signatures = CheckAttestationSignature::No |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Complete the attestation verification, potentially verifying all signatures independently. |
| 123 | + let final_results = indexing_results |
| 124 | + .into_iter() |
| 125 | + .map(|result| match result { |
| 126 | + Ok(indexed) => { |
| 127 | + VerifiedAggregatedAttestation::from_indexed(indexed, chain, check_signatures) |
| 128 | + } |
| 129 | + Err(e) => Err(e), |
| 130 | + }) |
| 131 | + .collect(); |
| 132 | + |
| 133 | + Ok(final_results) |
| 134 | +} |
| 135 | + |
| 136 | +/// Verify unaggregated attestations using batch BLS signature verification. |
| 137 | +/// |
| 138 | +/// See module-level docs for more info. |
| 139 | +pub fn batch_verify_unaggregated_attestations<'a, T, I>( |
| 140 | + attestations: I, |
| 141 | + chain: &BeaconChain<T>, |
| 142 | +) -> Result<Vec<Result<VerifiedUnaggregatedAttestation<'a, T>, Error>>, Error> |
| 143 | +where |
| 144 | + T: BeaconChainTypes, |
| 145 | + I: Iterator<Item = (&'a Attestation<T::EthSpec>, Option<SubnetId>)> + ExactSizeIterator, |
| 146 | +{ |
| 147 | + let mut num_partially_verified = 0; |
| 148 | + let mut num_failed = 0; |
| 149 | + |
| 150 | + // Perform partial verification of all attestations, collecting the results. |
| 151 | + let partial_results = attestations |
| 152 | + .map(|(attn, subnet_opt)| { |
| 153 | + let result = IndexedUnaggregatedAttestation::verify(attn, subnet_opt, chain); |
| 154 | + if result.is_ok() { |
| 155 | + num_partially_verified += 1; |
| 156 | + } else { |
| 157 | + num_failed += 1; |
| 158 | + } |
| 159 | + result |
| 160 | + }) |
| 161 | + .collect::<Vec<_>>(); |
| 162 | + |
| 163 | + // May be set to `No` if batch verification succeeds. |
| 164 | + let mut check_signatures = CheckAttestationSignature::Yes; |
| 165 | + |
| 166 | + // Perform batch BLS verification, if any attestation signatures are worth checking. |
| 167 | + if num_partially_verified > 0 { |
| 168 | + let signature_setup_timer = metrics::start_timer( |
| 169 | + &metrics::ATTESTATION_PROCESSING_BATCH_UNAGG_SIGNATURE_SETUP_TIMES, |
| 170 | + ); |
| 171 | + |
| 172 | + let pubkey_cache = chain |
| 173 | + .validator_pubkey_cache |
| 174 | + .try_read_for(VALIDATOR_PUBKEY_CACHE_LOCK_TIMEOUT) |
| 175 | + .ok_or(BeaconChainError::ValidatorPubkeyCacheLockTimeout)?; |
| 176 | + |
| 177 | + let fork = chain.with_head(|head| Ok::<_, BeaconChainError>(head.beacon_state.fork()))?; |
| 178 | + |
| 179 | + let mut signature_sets = Vec::with_capacity(num_partially_verified); |
| 180 | + |
| 181 | + // Iterate, flattening to get only the `Ok` values. |
| 182 | + for partially_verified in partial_results.iter().flatten() { |
| 183 | + let indexed_attestation = &partially_verified.indexed_attestation; |
| 184 | + |
| 185 | + let signature_set = indexed_attestation_signature_set_from_pubkeys( |
| 186 | + |validator_index| pubkey_cache.get(validator_index).map(Cow::Borrowed), |
| 187 | + &indexed_attestation.signature, |
| 188 | + indexed_attestation, |
| 189 | + &fork, |
| 190 | + chain.genesis_validators_root, |
| 191 | + &chain.spec, |
| 192 | + ) |
| 193 | + .map_err(BeaconChainError::SignatureSetError)?; |
| 194 | + |
| 195 | + signature_sets.push(signature_set); |
| 196 | + } |
| 197 | + |
| 198 | + metrics::stop_timer(signature_setup_timer); |
| 199 | + |
| 200 | + let _signature_verification_timer = |
| 201 | + metrics::start_timer(&metrics::ATTESTATION_PROCESSING_BATCH_UNAGG_SIGNATURE_TIMES); |
| 202 | + |
| 203 | + if verify_signature_sets(signature_sets.iter()) { |
| 204 | + // Since all the signatures verified in a batch, there's no reason for them to be |
| 205 | + // checked again later. |
| 206 | + check_signatures = CheckAttestationSignature::No |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + // Complete the attestation verification, potentially verifying all signatures independently. |
| 211 | + let final_results = partial_results |
| 212 | + .into_iter() |
| 213 | + .map(|result| match result { |
| 214 | + Ok(partial) => { |
| 215 | + VerifiedUnaggregatedAttestation::from_indexed(partial, chain, check_signatures) |
| 216 | + } |
| 217 | + Err(e) => Err(e), |
| 218 | + }) |
| 219 | + .collect(); |
| 220 | + |
| 221 | + Ok(final_results) |
| 222 | +} |
0 commit comments