Skip to content
Snippets Groups Projects
Commit deeae6a4 authored by Wei Tang's avatar Wei Tang
Browse files

beacon: compact_committees_root

parent 965927c0
Branches
No related merge requests found
......@@ -21,7 +21,7 @@ use serde::{Serialize, Deserialize};
use crate::primitives::{H256, Uint, Signature, ValidatorId};
/// BLS operations
pub trait BLSVerification: Clone + 'static {
pub trait BLSVerification: Default + Clone + 'static {
/// Verify BLS signature.
fn verify(pubkey: &ValidatorId, message: &H256, signature: &Signature, domain: u64) -> bool;
/// Aggregate BLS public keys.
......@@ -33,7 +33,7 @@ pub trait BLSVerification: Clone + 'static {
}
/// Run bls without any verification.
#[derive(Clone, PartialEq, Eq)]
#[derive(Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct BLSNoVerification;
......@@ -53,7 +53,7 @@ impl BLSVerification for BLSNoVerification {
}
/// Constants used in beacon block.
pub trait Config: Clone + 'static {
pub trait Config: Default + Clone + 'static {
/// Digest hash function.
type Digest: Digest<OutputSize=typenum::U32>;
type MaxValidatorsPerCommittee: Unsigned + core::fmt::Debug + Clone + Eq + PartialEq + Default;
......@@ -212,7 +212,7 @@ pub trait Config: Clone + 'static {
}
}
#[derive(Clone, PartialEq, Eq)]
#[derive(Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MinimalConfig<BLS>(PhantomData<BLS>);
......@@ -300,7 +300,7 @@ impl<BLS: BLSVerification> Config for MinimalConfig<BLS> {
}
}
#[derive(Clone, PartialEq, Eq)]
#[derive(Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MainnetConfig<BLS>(PhantomData<BLS>);
......
use crate::types::*;
use crate::primitives::*;
use crate::{BeaconState, Config, Error, utils};
use vecarray::VecArray;
use bm_le::tree_root;
use core::cmp::{max, min};
impl<C: Config> BeaconState<C> {
......@@ -165,6 +167,25 @@ impl<C: Config> BeaconState<C> {
offset / (committee_count / C::slots_per_epoch()))
}
pub fn compact_committees_root(&self, epoch: Uint) -> Result<H256, Error> {
let mut committees = VecArray::<CompactCommittee<C>, C::ShardCount>::default();
let start_shard = self.start_shard(epoch)?;
for committee_number in 0..self.committee_count(epoch) {
let shard = (start_shard + committee_number) % C::shard_count();
for index in self.crosslink_committee(epoch, shard)? {
let validator = &self.validators[index as usize];
committees[shard as usize].pubkeys.push(validator.pubkey.clone());
let compact_balance = validator.effective_balance / C::effective_balance_increment();
let compact_validator = (index << 16) +
(if validator.slashed { 1 } else { 0 } << 15) + compact_balance;
committees[shard as usize].compact_validators.append(compact_validator);
}
}
Ok(tree_root::<C::Digest, _>(&committees))
}
pub fn domain(&self, domain_type: Uint, message_epoch: Option<Uint>) -> Uint {
let epoch = message_epoch.unwrap_or(self.current_epoch());
let fork_version = if epoch < self.fork.epoch {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment