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

[Merged by Bors] - Avoid some allocations in BlockSignatureVerifier #1922

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::integer_arithmetic)]

use super::signature_sets::{Error as SignatureSetError, Result as SignatureSetResult, *};
use super::signature_sets::{Error as SignatureSetError, *};
use crate::common::get_indexed_attestation;
use crate::per_block_processing::errors::{AttestationInvalid, BlockOperationError};
use bls::{verify_signature_sets, PublicKey, SignatureSet};
Expand Down Expand Up @@ -203,31 +203,34 @@ where

/// Includes all signatures in `self.block.body.proposer_slashings` for verification.
pub fn include_proposer_slashings(&mut self, block: &'a SignedBeaconBlock<T>) -> Result<()> {
let mut sets: Vec<SignatureSet> = block
self.sets
.reserve(block.message.body.proposer_slashings.len() * 2);

block
.message
.body
.proposer_slashings
.iter()
.map(|proposer_slashing| {
.try_for_each(|proposer_slashing| {
let (set_1, set_2) = proposer_slashing_signature_set(
self.state,
self.get_pubkey.clone(),
proposer_slashing,
self.spec,
)?;
Ok(vec![set_1, set_2])
})
.collect::<SignatureSetResult<Vec<Vec<SignatureSet>>>>()?
.into_iter()
.flatten()
.collect();

self.sets.append(&mut sets);
Ok(())
self.sets.push(set_1);
self.sets.push(set_2);

Ok(())
})
}

/// Includes all signatures in `self.block.body.attester_slashings` for verification.
pub fn include_attester_slashings(&mut self, block: &'a SignedBeaconBlock<T>) -> Result<()> {
self.sets
.reserve(block.message.body.attester_slashings.len() * 2);

block
.message
.body
Expand All @@ -253,44 +256,54 @@ where
&mut self,
block: &'a SignedBeaconBlock<T>,
) -> Result<Vec<IndexedAttestation<T>>> {
self.sets.reserve(block.message.body.attestations.len());

block
.message
.body
.attestations
.iter()
.map(|attestation| {
let committee = self
.state
.get_beacon_committee(attestation.data.slot, attestation.data.index)?;
let indexed_attestation =
get_indexed_attestation(committee.committee, attestation)?;

self.sets.push(indexed_attestation_signature_set(
&self.state,
self.get_pubkey.clone(),
&attestation.signature,
&indexed_attestation,
&self.spec,
)?);
.try_fold(
Vec::with_capacity(block.message.body.attestations.len()),
|mut vec, attestation| {
let committee = self
.state
.get_beacon_committee(attestation.data.slot, attestation.data.index)?;
let indexed_attestation =
get_indexed_attestation(committee.committee, attestation)?;

Ok(indexed_attestation)
})
.collect::<Result<_>>()
.map_err(Into::into)
self.sets.push(indexed_attestation_signature_set(
&self.state,
self.get_pubkey.clone(),
&attestation.signature,
&indexed_attestation,
&self.spec,
)?);

vec.push(indexed_attestation);

Ok(vec)
},
)
.map_err(Error::into)
}

/// Includes all signatures in `self.block.body.voluntary_exits` for verification.
pub fn include_exits(&mut self, block: &'a SignedBeaconBlock<T>) -> Result<()> {
let mut sets = block
self.sets.reserve(block.message.body.voluntary_exits.len());

block
.message
.body
.voluntary_exits
.iter()
.map(|exit| exit_signature_set(&self.state, self.get_pubkey.clone(), exit, &self.spec))
.collect::<SignatureSetResult<_>>()?;
.try_for_each(|exit| {
let exit =
exit_signature_set(&self.state, self.get_pubkey.clone(), exit, &self.spec)?;

self.sets.append(&mut sets);
self.sets.push(exit);

Ok(())
Ok(())
})
}
}