mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-06 10:11:44 +00:00
* Add PH & MS slot clock changes * Account for genesis time * Add progress on duties refactor * Add simple is_aggregator bool to val subscription * Start work on attestation_verification.rs * Add progress on ObservedAttestations * Progress with ObservedAttestations * Fix tests * Add observed attestations to the beacon chain * Add attestation observation to processing code * Add progress on attestation verification * Add first draft of ObservedAttesters * Add more tests * Add observed attesters to beacon chain * Add observers to attestation processing * Add more attestation verification * Create ObservedAggregators map * Remove commented-out code * Add observed aggregators into chain * Add progress * Finish adding features to attestation verification * Ensure beacon chain compiles * Link attn verification into chain * Integrate new attn verification in chain * Remove old attestation processing code * Start trying to fix beacon_chain tests * Split adding into pools into two functions * Add aggregation to harness * Get test harness working again * Adjust the number of aggregators for test harness * Fix edge-case in harness * Integrate new attn processing in network * Fix compile bug in validator_client * Update validator API endpoints * Fix aggreagation in test harness * Fix enum thing * Fix attestation observation bug: * Patch failing API tests * Start adding comments to attestation verification * Remove unused attestation field * Unify "is block known" logic * Update comments * Supress fork choice errors for network processing * Add todos * Tidy * Add gossip attn tests * Disallow test harness to produce old attns * Comment out in-progress tests * Partially address pruning tests * Fix failing store test * Add aggregate tests * Add comments about which spec conditions we check * Dont re-aggregate * Split apart test harness attn production * Fix compile error in network * Make progress on commented-out test * Fix skipping attestation test * Add fork choice verification tests * Tidy attn tests, remove dead code * Remove some accidentally added code * Fix clippy lint * Rename test file * Add block tests, add cheap block proposer check * Rename block testing file * Add observed_block_producers * Tidy * Switch around block signature verification * Finish block testing * Remove gossip from signature tests * First pass of self review * Fix deviation in spec * Update test spec tags * Start moving over to hashset * Finish moving observed attesters to hashmap * Move aggregation pool over to hashmap * Make fc attn borrow again * Fix rest_api compile error * Fix missing comments * Fix monster test * Uncomment increasing slots test * Address remaining comments * Remove unsafe, use cfg test * Remove cfg test flag * Fix dodgy comment * Ignore aggregates that are already known. * Unify aggregator modulo logic * Fix typo in logs * Refactor validator subscription logic * Avoid reproducing selection proof * Skip HTTP call if no subscriptions * Rename DutyAndState -> DutyAndProof * Tidy logs * Print root as dbg * Fix compile errors in tests * Fix compile error in test
262 lines
8.6 KiB
Rust
262 lines
8.6 KiB
Rust
use crate::fork_service::ForkService;
|
|
use crate::validator_directory::{ValidatorDirectory, ValidatorDirectoryBuilder};
|
|
use parking_lot::RwLock;
|
|
use rayon::prelude::*;
|
|
use slog::{error, Logger};
|
|
use slot_clock::SlotClock;
|
|
use std::collections::HashMap;
|
|
use std::fs::read_dir;
|
|
use std::iter::FromIterator;
|
|
use std::marker::PhantomData;
|
|
use std::path::PathBuf;
|
|
use std::sync::Arc;
|
|
use tempdir::TempDir;
|
|
use types::{
|
|
Attestation, BeaconBlock, ChainSpec, Domain, Epoch, EthSpec, Fork, Hash256, PublicKey,
|
|
SelectionProof, Signature, SignedAggregateAndProof, SignedBeaconBlock, SignedRoot, Slot,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct ValidatorStore<T, E: EthSpec> {
|
|
validators: Arc<RwLock<HashMap<PublicKey, ValidatorDirectory>>>,
|
|
genesis_validators_root: Hash256,
|
|
spec: Arc<ChainSpec>,
|
|
log: Logger,
|
|
temp_dir: Option<Arc<TempDir>>,
|
|
fork_service: ForkService<T, E>,
|
|
_phantom: PhantomData<E>,
|
|
}
|
|
|
|
impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
|
|
pub fn load_from_disk(
|
|
base_dir: PathBuf,
|
|
genesis_validators_root: Hash256,
|
|
spec: ChainSpec,
|
|
fork_service: ForkService<T, E>,
|
|
log: Logger,
|
|
) -> Result<Self, String> {
|
|
let validator_key_values = read_dir(&base_dir)
|
|
.map_err(|e| format!("Failed to read base directory {:?}: {:?}", base_dir, e))?
|
|
.collect::<Vec<_>>()
|
|
.into_par_iter()
|
|
.filter_map(|validator_dir| {
|
|
let path = validator_dir.ok()?.path();
|
|
|
|
if path.is_dir() {
|
|
match ValidatorDirectory::load_for_signing(path.clone()) {
|
|
Ok(validator_directory) => Some(validator_directory),
|
|
Err(e) => {
|
|
error!(
|
|
log,
|
|
"Failed to load a validator directory";
|
|
"error" => e,
|
|
"path" => path.to_str(),
|
|
);
|
|
None
|
|
}
|
|
}
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.filter_map(|validator_directory| {
|
|
validator_directory
|
|
.voting_keypair
|
|
.clone()
|
|
.map(|voting_keypair| (voting_keypair.pk, validator_directory))
|
|
});
|
|
|
|
Ok(Self {
|
|
validators: Arc::new(RwLock::new(HashMap::from_par_iter(validator_key_values))),
|
|
genesis_validators_root,
|
|
spec: Arc::new(spec),
|
|
log,
|
|
temp_dir: None,
|
|
fork_service,
|
|
_phantom: PhantomData,
|
|
})
|
|
}
|
|
|
|
pub fn insecure_ephemeral_validators(
|
|
validator_indices: &[usize],
|
|
genesis_validators_root: Hash256,
|
|
spec: ChainSpec,
|
|
fork_service: ForkService<T, E>,
|
|
log: Logger,
|
|
) -> Result<Self, String> {
|
|
let temp_dir = TempDir::new("insecure_validator")
|
|
.map_err(|e| format!("Unable to create temp dir: {:?}", e))?;
|
|
let data_dir = PathBuf::from(temp_dir.path());
|
|
|
|
let validators = validator_indices
|
|
.par_iter()
|
|
.map(|index| {
|
|
ValidatorDirectoryBuilder::default()
|
|
.spec(spec.clone())
|
|
.full_deposit_amount()?
|
|
.insecure_keypairs(*index)
|
|
.create_directory(data_dir.clone())?
|
|
.write_keypair_files()?
|
|
.write_eth1_data_file()?
|
|
.build()
|
|
})
|
|
.collect::<Result<Vec<_>, _>>()?
|
|
.into_iter()
|
|
.filter_map(|validator_directory| {
|
|
validator_directory
|
|
.voting_keypair
|
|
.clone()
|
|
.map(|voting_keypair| (voting_keypair.pk, validator_directory))
|
|
});
|
|
|
|
Ok(Self {
|
|
validators: Arc::new(RwLock::new(HashMap::from_iter(validators))),
|
|
genesis_validators_root,
|
|
spec: Arc::new(spec),
|
|
log,
|
|
temp_dir: Some(Arc::new(temp_dir)),
|
|
fork_service,
|
|
_phantom: PhantomData,
|
|
})
|
|
}
|
|
|
|
pub fn voting_pubkeys(&self) -> Vec<PublicKey> {
|
|
self.validators
|
|
.read()
|
|
.iter()
|
|
.map(|(pubkey, _dir)| pubkey.clone())
|
|
.collect()
|
|
}
|
|
|
|
pub fn num_voting_validators(&self) -> usize {
|
|
self.validators.read().len()
|
|
}
|
|
|
|
fn fork(&self) -> Option<Fork> {
|
|
if self.fork_service.fork().is_none() {
|
|
error!(
|
|
self.log,
|
|
"Unable to get Fork for signing";
|
|
);
|
|
}
|
|
self.fork_service.fork()
|
|
}
|
|
|
|
pub fn randao_reveal(&self, validator_pubkey: &PublicKey, epoch: Epoch) -> Option<Signature> {
|
|
// TODO: check this against the slot clock to make sure it's not an early reveal?
|
|
self.validators
|
|
.read()
|
|
.get(validator_pubkey)
|
|
.and_then(|validator_dir| {
|
|
let voting_keypair = validator_dir.voting_keypair.as_ref()?;
|
|
let domain = self.spec.get_domain(
|
|
epoch,
|
|
Domain::Randao,
|
|
&self.fork()?,
|
|
self.genesis_validators_root,
|
|
);
|
|
let message = epoch.signing_root(domain);
|
|
|
|
Some(Signature::new(message.as_bytes(), &voting_keypair.sk))
|
|
})
|
|
}
|
|
|
|
pub fn sign_block(
|
|
&self,
|
|
validator_pubkey: &PublicKey,
|
|
block: BeaconBlock<E>,
|
|
) -> Option<SignedBeaconBlock<E>> {
|
|
// TODO: check for slashing.
|
|
self.validators
|
|
.read()
|
|
.get(validator_pubkey)
|
|
.and_then(|validator_dir| {
|
|
let voting_keypair = validator_dir.voting_keypair.as_ref()?;
|
|
Some(block.sign(
|
|
&voting_keypair.sk,
|
|
&self.fork()?,
|
|
self.genesis_validators_root,
|
|
&self.spec,
|
|
))
|
|
})
|
|
}
|
|
|
|
pub fn sign_attestation(
|
|
&self,
|
|
validator_pubkey: &PublicKey,
|
|
validator_committee_position: usize,
|
|
attestation: &mut Attestation<E>,
|
|
) -> Option<()> {
|
|
// TODO: check for slashing.
|
|
self.validators
|
|
.read()
|
|
.get(validator_pubkey)
|
|
.and_then(|validator_dir| {
|
|
let voting_keypair = validator_dir.voting_keypair.as_ref()?;
|
|
|
|
attestation
|
|
.sign(
|
|
&voting_keypair.sk,
|
|
validator_committee_position,
|
|
&self.fork()?,
|
|
self.genesis_validators_root,
|
|
&self.spec,
|
|
)
|
|
.map_err(|e| {
|
|
error!(
|
|
self.log,
|
|
"Error whilst signing attestation";
|
|
"error" => format!("{:?}", e)
|
|
)
|
|
})
|
|
.ok()?;
|
|
|
|
Some(())
|
|
})
|
|
}
|
|
|
|
/// Signs an `AggregateAndProof` for a given validator.
|
|
///
|
|
/// The resulting `SignedAggregateAndProof` is sent on the aggregation channel and cannot be
|
|
/// modified by actors other than the signing validator.
|
|
pub fn produce_signed_aggregate_and_proof(
|
|
&self,
|
|
validator_pubkey: &PublicKey,
|
|
validator_index: u64,
|
|
aggregate: Attestation<E>,
|
|
selection_proof: SelectionProof,
|
|
) -> Option<SignedAggregateAndProof<E>> {
|
|
let validators = self.validators.read();
|
|
let voting_keypair = validators.get(validator_pubkey)?.voting_keypair.as_ref()?;
|
|
|
|
Some(SignedAggregateAndProof::from_aggregate(
|
|
validator_index,
|
|
aggregate,
|
|
Some(selection_proof),
|
|
&voting_keypair.sk,
|
|
&self.fork()?,
|
|
self.genesis_validators_root,
|
|
&self.spec,
|
|
))
|
|
}
|
|
|
|
/// Produces a `SelectionProof` for the `slot`, signed by with corresponding secret key to
|
|
/// `validator_pubkey`.
|
|
pub fn produce_selection_proof(
|
|
&self,
|
|
validator_pubkey: &PublicKey,
|
|
slot: Slot,
|
|
) -> Option<SelectionProof> {
|
|
let validators = self.validators.read();
|
|
let voting_keypair = validators.get(validator_pubkey)?.voting_keypair.as_ref()?;
|
|
|
|
Some(SelectionProof::new::<E>(
|
|
slot,
|
|
&voting_keypair.sk,
|
|
&self.fork()?,
|
|
self.genesis_validators_root,
|
|
&self.spec,
|
|
))
|
|
}
|
|
}
|