mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-09 19:51:47 +00:00
Implement checkpoint sync (#2244)
## Issue Addressed Closes #1891 Closes #1784 ## Proposed Changes Implement checkpoint sync for Lighthouse, enabling it to start from a weak subjectivity checkpoint. ## Additional Info - [x] Return unavailable status for out-of-range blocks requested by peers (#2561) - [x] Implement sync daemon for fetching historical blocks (#2561) - [x] Verify chain hashes (either in `historical_blocks.rs` or the calling module) - [x] Consistency check for initial block + state - [x] Fetch the initial state and block from a beacon node HTTP endpoint - [x] Don't crash fetching beacon states by slot from the API - [x] Background service for state reconstruction, triggered by CLI flag or API call. Considered out of scope for this PR: - Drop the requirement to provide the `--checkpoint-block` (this would require some pretty heavy refactoring of block verification) Co-authored-by: Diva M <divma@protonmail.com>
This commit is contained in:
@@ -73,9 +73,20 @@ where
|
||||
decompressor: D,
|
||||
state: &'a BeaconState<T>,
|
||||
spec: &'a ChainSpec,
|
||||
sets: ParallelSignatureSets<'a>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ParallelSignatureSets<'a> {
|
||||
sets: Vec<SignatureSet<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> From<Vec<SignatureSet<'a>>> for ParallelSignatureSets<'a> {
|
||||
fn from(sets: Vec<SignatureSet<'a>>) -> Self {
|
||||
Self { sets }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, F, D> BlockSignatureVerifier<'a, T, F, D>
|
||||
where
|
||||
T: EthSpec,
|
||||
@@ -95,7 +106,7 @@ where
|
||||
decompressor,
|
||||
state,
|
||||
spec,
|
||||
sets: vec![],
|
||||
sets: ParallelSignatureSets::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,36 +130,6 @@ where
|
||||
verifier.verify()
|
||||
}
|
||||
|
||||
/// Verify all* the signatures that have been included in `self`, returning `Ok(())` if the
|
||||
/// signatures are all valid.
|
||||
///
|
||||
/// ## Notes
|
||||
///
|
||||
/// Signature validation will take place in accordance to the [Faster verification of multiple
|
||||
/// BLS signatures](https://ethresear.ch/t/fast-verification-of-multiple-bls-signatures/5407)
|
||||
/// optimization proposed by Vitalik Buterin.
|
||||
///
|
||||
/// It is not possible to know exactly _which_ signature is invalid here, just that
|
||||
/// _at least one_ was invalid.
|
||||
///
|
||||
/// Uses `rayon` to do a map-reduce of Vitalik's method across multiple cores.
|
||||
pub fn verify(self) -> Result<()> {
|
||||
let num_sets = self.sets.len();
|
||||
let num_chunks = std::cmp::max(1, num_sets / rayon::current_num_threads());
|
||||
let result: bool = self
|
||||
.sets
|
||||
.into_par_iter()
|
||||
.chunks(num_chunks)
|
||||
.map(|chunk| verify_signature_sets(chunk.iter()))
|
||||
.reduce(|| true, |current, this| current && this);
|
||||
|
||||
if result {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::SignatureInvalid)
|
||||
}
|
||||
}
|
||||
|
||||
/// Includes all signatures on the block (except the deposit signatures) for verification.
|
||||
pub fn include_all_signatures(
|
||||
&mut self,
|
||||
@@ -210,6 +191,7 @@ where
|
||||
/// Includes all signatures in `self.block.body.proposer_slashings` for verification.
|
||||
pub fn include_proposer_slashings(&mut self, block: &'a SignedBeaconBlock<T>) -> Result<()> {
|
||||
self.sets
|
||||
.sets
|
||||
.reserve(block.message().body().proposer_slashings().len() * 2);
|
||||
|
||||
block
|
||||
@@ -235,6 +217,7 @@ where
|
||||
/// 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
|
||||
.sets
|
||||
.reserve(block.message().body().attester_slashings().len() * 2);
|
||||
|
||||
block
|
||||
@@ -263,6 +246,7 @@ where
|
||||
block: &'a SignedBeaconBlock<T>,
|
||||
) -> Result<Vec<IndexedAttestation<T>>> {
|
||||
self.sets
|
||||
.sets
|
||||
.reserve(block.message().body().attestations().len());
|
||||
|
||||
block
|
||||
@@ -298,6 +282,7 @@ where
|
||||
/// Includes all signatures in `self.block.body.voluntary_exits` for verification.
|
||||
pub fn include_exits(&mut self, block: &'a SignedBeaconBlock<T>) -> Result<()> {
|
||||
self.sets
|
||||
.sets
|
||||
.reserve(block.message().body().voluntary_exits().len());
|
||||
|
||||
block
|
||||
@@ -331,4 +316,46 @@ where
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Verify all the signatures that have been included in `self`, returning `true` if and only if
|
||||
/// all the signatures are valid.
|
||||
///
|
||||
/// See `ParallelSignatureSets::verify` for more info.
|
||||
pub fn verify(self) -> Result<()> {
|
||||
if self.sets.verify() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::SignatureInvalid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ParallelSignatureSets<'a> {
|
||||
pub fn push(&mut self, set: SignatureSet<'a>) {
|
||||
self.sets.push(set);
|
||||
}
|
||||
|
||||
/// Verify all the signatures that have been included in `self`, returning `true` if and only if
|
||||
/// all the signatures are valid.
|
||||
///
|
||||
/// ## Notes
|
||||
///
|
||||
/// Signature validation will take place in accordance to the [Faster verification of multiple
|
||||
/// BLS signatures](https://ethresear.ch/t/fast-verification-of-multiple-bls-signatures/5407)
|
||||
/// optimization proposed by Vitalik Buterin.
|
||||
///
|
||||
/// It is not possible to know exactly _which_ signature is invalid here, just that
|
||||
/// _at least one_ was invalid.
|
||||
///
|
||||
/// Uses `rayon` to do a map-reduce of Vitalik's method across multiple cores.
|
||||
#[must_use]
|
||||
pub fn verify(self) -> bool {
|
||||
let num_sets = self.sets.len();
|
||||
let num_chunks = std::cmp::max(1, num_sets / rayon::current_num_threads());
|
||||
self.sets
|
||||
.into_par_iter()
|
||||
.chunks(num_chunks)
|
||||
.map(|chunk| verify_signature_sets(chunk.iter()))
|
||||
.reduce(|| true, |current, this| current && this)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,6 +77,45 @@ pub fn block_proposal_signature_set<'a, T, F>(
|
||||
block_root: Option<Hash256>,
|
||||
spec: &'a ChainSpec,
|
||||
) -> Result<SignatureSet<'a>>
|
||||
where
|
||||
T: EthSpec,
|
||||
F: Fn(usize) -> Option<Cow<'a, PublicKey>>,
|
||||
{
|
||||
let block = signed_block.message();
|
||||
let proposer_index = state.get_beacon_proposer_index(block.slot(), spec)? as u64;
|
||||
|
||||
if proposer_index != block.proposer_index() {
|
||||
return Err(Error::IncorrectBlockProposer {
|
||||
block: block.proposer_index(),
|
||||
local_shuffling: proposer_index,
|
||||
});
|
||||
}
|
||||
|
||||
block_proposal_signature_set_from_parts(
|
||||
signed_block,
|
||||
block_root,
|
||||
proposer_index,
|
||||
&state.fork(),
|
||||
state.genesis_validators_root(),
|
||||
get_pubkey,
|
||||
spec,
|
||||
)
|
||||
}
|
||||
|
||||
/// A signature set that is valid if a block was signed by the expected block producer.
|
||||
///
|
||||
/// Unlike `block_proposal_signature_set` this does **not** check that the proposer index is
|
||||
/// correct according to the shuffling. It should only be used if no suitable `BeaconState` is
|
||||
/// available.
|
||||
pub fn block_proposal_signature_set_from_parts<'a, T, F>(
|
||||
signed_block: &'a SignedBeaconBlock<T>,
|
||||
block_root: Option<Hash256>,
|
||||
proposer_index: u64,
|
||||
fork: &Fork,
|
||||
genesis_validators_root: Hash256,
|
||||
get_pubkey: F,
|
||||
spec: &'a ChainSpec,
|
||||
) -> Result<SignatureSet<'a>>
|
||||
where
|
||||
T: EthSpec,
|
||||
F: Fn(usize) -> Option<Cow<'a, PublicKey>>,
|
||||
@@ -87,20 +126,11 @@ where
|
||||
.map_err(Error::InconsistentBlockFork)?;
|
||||
|
||||
let block = signed_block.message();
|
||||
let proposer_index = state.get_beacon_proposer_index(block.slot(), spec)?;
|
||||
|
||||
if proposer_index as u64 != block.proposer_index() {
|
||||
return Err(Error::IncorrectBlockProposer {
|
||||
block: block.proposer_index(),
|
||||
local_shuffling: proposer_index as u64,
|
||||
});
|
||||
}
|
||||
|
||||
let domain = spec.get_domain(
|
||||
block.slot().epoch(T::slots_per_epoch()),
|
||||
Domain::BeaconProposer,
|
||||
&state.fork(),
|
||||
state.genesis_validators_root(),
|
||||
fork,
|
||||
genesis_validators_root,
|
||||
);
|
||||
|
||||
let message = if let Some(root) = block_root {
|
||||
@@ -115,7 +145,7 @@ where
|
||||
|
||||
Ok(SignatureSet::single_pubkey(
|
||||
signed_block.signature(),
|
||||
get_pubkey(proposer_index).ok_or_else(|| Error::ValidatorUnknown(proposer_index as u64))?,
|
||||
get_pubkey(proposer_index as usize).ok_or(Error::ValidatorUnknown(proposer_index))?,
|
||||
message,
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user