Heavily restructure repo

Separate most modules into crates
This commit is contained in:
Paul Hauner
2018-10-02 16:41:10 +10:00
parent 07bfd7e97d
commit 0fbe4179b3
71 changed files with 150 additions and 2240 deletions

View File

@@ -1,10 +0,0 @@
extern crate bls_aggregates;
pub use self::bls_aggregates::AggregateSignature;
pub use self::bls_aggregates::AggregatePublicKey;
pub use self::bls_aggregates::Signature;
pub use self::bls_aggregates::Keypair;
pub use self::bls_aggregates::PublicKey;
pub use self::bls_aggregates::SecretKey;
pub const BLS_AGG_SIG_BYTE_SIZE: usize = 97;

10
lighthouse/db/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "db"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
[dependencies]
bls = { path = "../../beacon_chain/utils/bls" }
bytes = "0.4.10"
rocksdb = "0.10.1"
blake2-rfc = "0.2.18"

View File

@@ -1,12 +1,12 @@
extern crate rocksdb;
extern crate blake2_rfc as blake2;
extern crate bls;
extern crate rocksdb;
mod disk_db;
mod memory_db;
mod traits;
pub mod stores;
use super::bls;
use self::stores::COLUMNS;
pub use self::disk_db::DiskDB;

View File

@@ -1,22 +0,0 @@
#[macro_use]
extern crate slog;
extern crate slog_term;
extern crate slog_async;
extern crate ssz;
extern crate clap;
extern crate network_libp2p;
extern crate futures;
#[macro_use]
#[allow(dead_code)]
pub mod utils;
#[allow(dead_code)]
pub mod bls;
#[allow(dead_code)]
pub mod db;
pub mod client;
#[allow(dead_code)]
pub mod state;
#[allow(dead_code)]
mod sync;
mod config;

View File

@@ -2,22 +2,14 @@
extern crate slog;
extern crate slog_term;
extern crate slog_async;
extern crate ssz;
// extern crate ssz;
extern crate clap;
extern crate network_libp2p;
extern crate futures;
#[macro_use]
#[allow(dead_code)]
mod utils;
#[allow(dead_code)]
mod bls;
#[allow(dead_code)]
mod db;
extern crate db;
mod client;
#[allow(dead_code)]
mod state;
#[allow(dead_code)]
mod sync;
mod config;

View File

@@ -1,30 +0,0 @@
use super::utils::types::Hash256;
use super::attestation_record::AttestationRecord;
pub struct ActiveState {
pub pending_attestations: Vec<AttestationRecord>,
pub recent_block_hashes: Vec<Hash256>,
}
impl ActiveState {
/// Returns a new instance where all fields are empty vectors.
pub fn zero() -> Self {
Self {
pending_attestations: vec![],
recent_block_hashes: vec![],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_act_state_zero() {
let a = ActiveState::zero();
assert_eq!(a.pending_attestations.len(), 0);
assert_eq!(a.recent_block_hashes.len(), 0);
}
}

View File

@@ -1,24 +0,0 @@
use super::bls;
use super::common;
use super::db;
use super::ssz;
use super::utils;
mod structs;
mod ssz_splitter;
mod validation;
pub use self::structs::{
AttestationRecord,
MIN_SSZ_ATTESTION_RECORD_LENGTH,
};
pub use self::ssz_splitter::{
split_all_attestations,
split_one_attestation,
AttestationSplitError,
};
pub use self::validation::{
AttestationValidationContext,
AttestationValidationError,
};

View File

@@ -1,139 +0,0 @@
use super::MIN_SSZ_ATTESTION_RECORD_LENGTH as MIN_LENGTH;
use super::ssz::LENGTH_BYTES;
use super::ssz::decode::decode_length;
#[derive(Debug, PartialEq)]
pub enum AttestationSplitError {
TooShort,
}
/// Given some ssz slice, find the bounds of each serialized AttestationRecord and return a vec of
/// slices point to each.
pub fn split_all_attestations<'a>(full_ssz: &'a [u8], index: usize)
-> Result<Vec<&'a [u8]>, AttestationSplitError>
{
let mut v = vec![];
let mut index = index;
while index < full_ssz.len() - 1 {
let (slice, i) = split_one_attestation(full_ssz, index)?;
v.push(slice);
index = i;
}
Ok(v)
}
/// Given some ssz slice, find the bounds of one serialized AttestationRecord
/// and return a slice pointing to that.
pub fn split_one_attestation(full_ssz: &[u8], index: usize)
-> Result<(&[u8], usize), AttestationSplitError>
{
if full_ssz.len() < MIN_LENGTH {
return Err(AttestationSplitError::TooShort);
}
let hashes_len = decode_length(full_ssz, index + 10, LENGTH_BYTES)
.map_err(|_| AttestationSplitError::TooShort)?;
let bitfield_len = decode_length(
full_ssz, index + hashes_len + 46,
LENGTH_BYTES)
.map_err(|_| AttestationSplitError::TooShort)?;
// Subtract one because the min length assumes 1 byte of bitfield
let len = MIN_LENGTH - 1
+ hashes_len
+ bitfield_len;
if full_ssz.len() < index + len {
return Err(AttestationSplitError::TooShort);
}
Ok((&full_ssz[index..(index + len)], index + len))
}
#[cfg(test)]
mod tests {
use super::*;
use super::super::AttestationRecord;
use super::super::utils::types::{
Hash256,
Bitfield,
};
use super::super::bls::AggregateSignature;
use super::super::ssz::{
SszStream,
Decodable,
};
fn get_two_records() -> Vec<AttestationRecord> {
let a = AttestationRecord {
slot: 7,
shard_id: 9,
oblique_parent_hashes: vec![Hash256::from(&vec![14; 32][..])],
shard_block_hash: Hash256::from(&vec![15; 32][..]),
attester_bitfield: Bitfield::from(&vec![17; 42][..]),
justified_slot: 19,
justified_block_hash: Hash256::from(&vec![15; 32][..]),
aggregate_sig: AggregateSignature::new(),
};
let b = AttestationRecord {
slot: 9,
shard_id: 7,
oblique_parent_hashes: vec![Hash256::from(&vec![15; 32][..])],
shard_block_hash: Hash256::from(&vec![14; 32][..]),
attester_bitfield: Bitfield::from(&vec![19; 42][..]),
justified_slot: 15,
justified_block_hash: Hash256::from(&vec![17; 32][..]),
aggregate_sig: AggregateSignature::new(),
};
vec![a, b]
}
#[test]
fn test_attestation_ssz_split() {
let ars = get_two_records();
let a = ars[0].clone();
let b = ars[1].clone();
/*
* Test split one
*/
let mut ssz_stream = SszStream::new();
ssz_stream.append(&a);
let ssz = ssz_stream.drain();
let (a_ssz, i) = split_one_attestation(&ssz, 0).unwrap();
assert_eq!(i, ssz.len());
let (decoded_a, _) = AttestationRecord::ssz_decode(a_ssz, 0)
.unwrap();
assert_eq!(a, decoded_a);
/*
* Test split two
*/
let mut ssz_stream = SszStream::new();
ssz_stream.append(&a);
ssz_stream.append(&b);
let ssz = ssz_stream.drain();
let ssz_vec = split_all_attestations(&ssz, 0).unwrap();
let (decoded_a, _) =
AttestationRecord::ssz_decode(ssz_vec[0], 0)
.unwrap();
let (decoded_b, _) =
AttestationRecord::ssz_decode(ssz_vec[1], 0)
.unwrap();
assert_eq!(a, decoded_a);
assert_eq!(b, decoded_b);
/*
* Test split two with shortened ssz
*/
let mut ssz_stream = SszStream::new();
ssz_stream.append(&a);
ssz_stream.append(&b);
let ssz = ssz_stream.drain();
let ssz = &ssz[0..ssz.len() - 1];
assert!(split_all_attestations(&ssz, 0).is_err());
}
}

View File

@@ -1,137 +0,0 @@
use super::utils::types::{ Hash256, Bitfield };
use super::bls::{
AggregateSignature,
BLS_AGG_SIG_BYTE_SIZE,
};
use super::ssz::{
Encodable,
Decodable,
DecodeError,
decode_ssz_list,
SszStream,
};
pub const MIN_SSZ_ATTESTION_RECORD_LENGTH: usize = {
8 + // slot
2 + // shard_id
4 + // oblique_parent_hashes (empty list)
32 + // shard_block_hash
5 + // attester_bitfield (assuming 1 byte of bitfield)
8 + // justified_slot
32 + // justified_block_hash
4 + BLS_AGG_SIG_BYTE_SIZE // aggregate sig (two 256 bit points)
};
#[derive(Debug, Clone, PartialEq)]
pub struct AttestationRecord {
pub slot: u64,
pub shard_id: u16,
pub oblique_parent_hashes: Vec<Hash256>,
pub shard_block_hash: Hash256,
pub attester_bitfield: Bitfield,
pub justified_slot: u64,
pub justified_block_hash: Hash256,
pub aggregate_sig: AggregateSignature,
}
impl Encodable for AttestationRecord {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.slot);
s.append(&self.shard_id);
s.append_vec(&self.oblique_parent_hashes);
s.append(&self.shard_block_hash);
s.append_vec(&self.attester_bitfield.to_be_vec());
s.append(&self.justified_slot);
s.append(&self.justified_block_hash);
s.append_vec(&self.aggregate_sig.as_bytes());
}
}
impl Decodable for AttestationRecord {
fn ssz_decode(bytes: &[u8], i: usize)
-> Result<(Self, usize), DecodeError>
{
let (slot, i) = u64::ssz_decode(bytes, i)?;
let (shard_id, i) = u16::ssz_decode(bytes, i)?;
let (oblique_parent_hashes, i) = decode_ssz_list(bytes, i)?;
let (shard_block_hash, i) = Hash256::ssz_decode(bytes, i)?;
let (attester_bitfield, i) = Bitfield::ssz_decode(bytes, i)?;
let (justified_slot, i) = u64::ssz_decode(bytes, i)?;
let (justified_block_hash, i) = Hash256::ssz_decode(bytes, i)?;
// Do aggregate sig decoding properly.
let (agg_sig_bytes, i) = decode_ssz_list(bytes, i)?;
let aggregate_sig = AggregateSignature::from_bytes(&agg_sig_bytes)
.map_err(|_| DecodeError::TooShort)?; // also could be TooLong
let attestation_record = Self {
slot,
shard_id,
oblique_parent_hashes,
shard_block_hash,
attester_bitfield,
justified_slot,
justified_block_hash,
aggregate_sig,
};
Ok((attestation_record, i))
}
}
impl AttestationRecord {
pub fn zero() -> Self {
Self {
slot: 0,
shard_id: 0,
oblique_parent_hashes: vec![],
shard_block_hash: Hash256::zero(),
attester_bitfield: Bitfield::new(),
justified_slot: 0,
justified_block_hash: Hash256::zero(),
aggregate_sig: AggregateSignature::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::super::ssz::SszStream;
#[test]
pub fn test_attestation_record_min_ssz_length() {
let ar = AttestationRecord::zero();
let mut ssz_stream = SszStream::new();
ssz_stream.append(&ar);
let ssz = ssz_stream.drain();
assert_eq!(ssz.len(), MIN_SSZ_ATTESTION_RECORD_LENGTH);
}
#[test]
pub fn test_attestation_record_min_ssz_encode_decode() {
let original = AttestationRecord {
slot: 7,
shard_id: 9,
oblique_parent_hashes: vec![Hash256::from(&vec![14; 32][..])],
shard_block_hash: Hash256::from(&vec![15; 32][..]),
attester_bitfield: Bitfield::from(&vec![17; 42][..]),
justified_slot: 19,
justified_block_hash: Hash256::from(&vec![15; 32][..]),
aggregate_sig: AggregateSignature::new(),
};
let mut ssz_stream = SszStream::new();
ssz_stream.append(&original);
let (decoded, _) = AttestationRecord::
ssz_decode(&ssz_stream.drain(), 0).unwrap();
assert_eq!(original.slot, decoded.slot);
assert_eq!(original.shard_id, decoded.shard_id);
assert_eq!(original.oblique_parent_hashes, decoded.oblique_parent_hashes);
assert_eq!(original.shard_block_hash, decoded.shard_block_hash);
assert_eq!(original.attester_bitfield, decoded.attester_bitfield);
assert_eq!(original.justified_slot, decoded.justified_slot);
assert_eq!(original.justified_block_hash, decoded.justified_block_hash);
}
}

View File

@@ -1,208 +0,0 @@
use std::collections::HashSet;
use std::sync::Arc;
use super::structs::AttestationRecord;
use super::AttesterMap;
use super::attestation_parent_hashes::{
attestation_parent_hashes,
ParentHashesError,
};
use super::db::{
ClientDB,
DBError
};
use super::db::stores::{
BlockStore,
ValidatorStore,
};
use super::utils::types::{
Hash256,
};
use super::message_generation::generate_signed_message;
use super::signature_verification::{
verify_aggregate_signature_for_indices,
SignatureVerificationError,
};
#[derive(Debug,PartialEq)]
pub enum AttestationValidationError {
SlotTooHigh,
SlotTooLow,
JustifiedSlotIncorrect,
UnknownJustifiedBlock,
TooManyObliqueHashes,
BadCurrentHashes,
BadObliqueHashes,
BadAttesterMap,
IntWrapping,
PublicKeyCorrupt,
NoPublicKeyForValidator,
BadBitfieldLength,
InvalidBitfield,
InvalidBitfieldEndBits,
NoSignatures,
NonZeroTrailingBits,
BadAggregateSignature,
DBError(String),
}
pub struct AttestationValidationContext<T>
where T: ClientDB + Sized
{
pub block_slot: u64,
pub cycle_length: u8,
pub last_justified_slot: u64,
pub parent_hashes: Arc<Vec<Hash256>>,
pub block_store: Arc<BlockStore<T>>,
pub validator_store: Arc<ValidatorStore<T>>,
pub attester_map: Arc<AttesterMap>,
}
impl<T> AttestationValidationContext<T>
where T: ClientDB
{
pub fn validate_attestation(&self, a: &AttestationRecord)
-> Result<HashSet<usize>, AttestationValidationError>
{
/*
* The attesation slot must not be higher than the block that contained it.
*/
if a.slot > self.block_slot {
return Err(AttestationValidationError::SlotTooHigh);
}
/*
* The slot of this attestation must not be more than cycle_length + 1 distance
* from the block that contained it.
*/
if a.slot < self.block_slot
.saturating_sub(u64::from(self.cycle_length).saturating_add(1)) {
return Err(AttestationValidationError::SlotTooLow);
}
/*
* The attestation must indicate that its last justified slot is the same as the last justified
* slot known to us.
*/
if a.justified_slot != self.last_justified_slot {
return Err(AttestationValidationError::JustifiedSlotIncorrect);
}
/*
* There is no need to include more oblique parents hashes than there are blocks
* in a cycle.
*/
if a.oblique_parent_hashes.len() > usize::from(self.cycle_length) {
return Err(AttestationValidationError::TooManyObliqueHashes);
}
/*
* Retrieve the set of attestation indices for this slot and shard id.
*
* This is an array mapping the order that validators will appear in the bitfield to the
* canonincal index of a validator.
*/
let attestation_indices = self.attester_map.get(&(a.slot, a.shard_id))
.ok_or(AttestationValidationError::BadAttesterMap)?;
/*
* The bitfield must be no longer than the minimum required to represent each validator in the
* attestation indicies for this slot and shard id.
*/
if a.attester_bitfield.num_bytes() !=
bytes_for_bits(attestation_indices.len())
{
return Err(AttestationValidationError::BadBitfieldLength);
}
/*
* If there are excess bits in the bitfield because the number of a validators in not a
* multiple of 8, reject this attestation record.
*
* Allow extra set bits would permit mutliple different byte layouts (and therefore hashes) to
* refer to the same AttesationRecord.
*/
if a.attester_bitfield.len() > attestation_indices.len() {
return Err(AttestationValidationError::InvalidBitfieldEndBits)
}
/*
* The specified justified block hash must be known to us
*/
if !self.block_store.block_exists(&a.justified_block_hash)? {
return Err(AttestationValidationError::UnknownJustifiedBlock)
}
let signed_message = {
let parent_hashes = attestation_parent_hashes(
self.cycle_length,
self.block_slot,
a.slot,
&self.parent_hashes,
&a.oblique_parent_hashes)?;
generate_signed_message(
a.slot,
&parent_hashes,
a.shard_id,
&a.shard_block_hash,
a.justified_slot)
};
let voted_hashmap =
verify_aggregate_signature_for_indices(
&signed_message,
&a.aggregate_sig,
&attestation_indices,
&a.attester_bitfield,
&self.validator_store)?;
/*
* If the hashmap of voters is None, the signature verification failed.
*/
match voted_hashmap {
None => Err(AttestationValidationError::BadAggregateSignature),
Some(hashmap) => Ok(hashmap),
}
}
}
fn bytes_for_bits(bits: usize) -> usize {
(bits.saturating_sub(1) / 8) + 1
}
impl From<ParentHashesError> for AttestationValidationError {
fn from(e: ParentHashesError) -> Self {
match e {
ParentHashesError::BadCurrentHashes
=> AttestationValidationError::BadCurrentHashes,
ParentHashesError::BadObliqueHashes
=> AttestationValidationError::BadObliqueHashes,
ParentHashesError::SlotTooLow
=> AttestationValidationError::SlotTooLow,
ParentHashesError::SlotTooHigh
=> AttestationValidationError::SlotTooHigh,
ParentHashesError::IntWrapping
=> AttestationValidationError::IntWrapping
}
}
}
impl From<DBError> for AttestationValidationError {
fn from(e: DBError) -> Self {
AttestationValidationError::DBError(e.message)
}
}
impl From<SignatureVerificationError> for AttestationValidationError {
fn from(e: SignatureVerificationError) -> Self {
match e {
SignatureVerificationError::BadValidatorIndex
=> AttestationValidationError::BadAttesterMap,
SignatureVerificationError::PublicKeyCorrupt
=> AttestationValidationError::PublicKeyCorrupt,
SignatureVerificationError::NoPublicKeyForValidator
=> AttestationValidationError::NoPublicKeyForValidator,
SignatureVerificationError::DBError(s)
=> AttestationValidationError::DBError(s),
}
}
}

View File

@@ -1,70 +0,0 @@
use super::ssz::SszStream;
use super::utils::hash::canonical_hash;
use super::utils::types::Hash256;
/// Generates the message used to validate the signature provided with an AttestationRecord.
///
/// Ensures that the signer of the message has a view of the chain that is compatible with ours.
pub fn generate_signed_message(
slot: u64,
parent_hashes: &[Hash256],
shard_id: u16,
shard_block_hash: &Hash256,
justified_slot: u64)
-> Vec<u8>
{
/*
* Note: it's a little risky here to use SSZ, because the encoding is not necessarily SSZ
* (for example, SSZ might change whilst this doesn't).
*
* I have suggested switching this to ssz here:
* https://github.com/ethereum/eth2.0-specs/issues/5
*
* If this doesn't happen, it would be safer to not use SSZ at all.
*/
let mut ssz_stream = SszStream::new();
ssz_stream.append(&slot);
ssz_stream.append_vec(&parent_hashes.to_vec());
ssz_stream.append(&shard_id);
ssz_stream.append(shard_block_hash);
ssz_stream.append(&justified_slot);
let bytes = ssz_stream.drain();
canonical_hash(&bytes)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generate_signed_message() {
let slot = 93;
let parent_hashes: Vec<Hash256> = (0..12)
.map(|i| Hash256::from(i as u64))
.collect();
let shard_id = 15;
let shard_block_hash = Hash256::from("shard_block_hash".as_bytes());
let justified_slot = 18;
let output = generate_signed_message(
slot,
&parent_hashes,
shard_id,
&shard_block_hash,
justified_slot);
/*
* Note: this is not some well-known test vector, it's simply the result of running
* this and printing the output.
*
* Once well-known test vectors are established, they should be placed here.
*/
let expected = vec![
149, 99, 94, 229, 72, 144, 233, 14, 164, 16, 143, 53, 94, 48,
118, 179, 33, 181, 172, 215, 2, 191, 176, 18, 188, 172, 137,
178, 236, 66, 74, 120
];
assert_eq!(output, expected);
}
}

View File

@@ -1,16 +0,0 @@
use super::common::maps::AttesterMap;
use super::db;
use super::bls;
use super::structs;
use super::ssz;
use super::common::attestation_parent_hashes;
use super::utils;
mod attestation_validation;
mod signature_verification;
mod message_generation;
pub use self::attestation_validation::{
AttestationValidationContext,
AttestationValidationError,
};

View File

@@ -1,183 +0,0 @@
use std::collections::HashSet;
use super::bls::{
AggregateSignature,
AggregatePublicKey,
};
use super::db::ClientDB;
use super::db::stores::{
ValidatorStore,
ValidatorStoreError,
};
use super::utils::types::Bitfield;
#[derive(Debug, PartialEq)]
pub enum SignatureVerificationError {
BadValidatorIndex,
PublicKeyCorrupt,
NoPublicKeyForValidator,
DBError(String),
}
/// Verify an aggregate signature across the supplied message.
///
/// The public keys used for verification are collected by mapping
/// each true bitfield bit to canonical ValidatorRecord index through
/// the attestation_indicies map.
///
/// Each public key is loaded from the store on-demand.
pub fn verify_aggregate_signature_for_indices<T>(
message: &[u8],
agg_sig: &AggregateSignature,
attestation_indices: &[usize],
bitfield: &Bitfield,
validator_store: &ValidatorStore<T>)
-> Result<Option<HashSet<usize>>, SignatureVerificationError>
where T: ClientDB + Sized
{
let mut voters = HashSet::new();
let mut agg_pub_key = AggregatePublicKey::new();
for i in 0..attestation_indices.len() {
let voted = bitfield.get_bit(i);
if voted {
/*
* De-reference the attestation index into a canonical ValidatorRecord index.
*/
let validator = *attestation_indices.get(i)
.ok_or(SignatureVerificationError::BadValidatorIndex)?;
/*
* Load the validators public key from our store.
*/
let pub_key = validator_store
.get_public_key_by_index(validator)?
.ok_or(SignatureVerificationError::NoPublicKeyForValidator)?;
/*
* Add the validators public key to the aggregate public key.
*/
agg_pub_key.add(&pub_key);
/*
* Add to the validator to the set of voters for this attestation record.
*/
voters.insert(validator);
}
}
/*
* Verify the aggregate public key against the aggregate signature.
*
* This verification will only succeed if the exact set of public keys
* were added to the aggregate public key as those that signed the aggregate signature.
*/
if agg_sig.verify(&message, &agg_pub_key) {
Ok(Some(voters))
} else {
Ok(None)
}
}
impl From<ValidatorStoreError> for SignatureVerificationError {
fn from(error: ValidatorStoreError) -> Self {
match error {
ValidatorStoreError::DBError(s) =>
SignatureVerificationError::DBError(s),
ValidatorStoreError::DecodeError =>
SignatureVerificationError::PublicKeyCorrupt,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::super::bls::{
Keypair,
Signature,
};
use super::super::db::MemoryDB;
use std::sync::Arc;
/*
* Cases that still need testing:
*
* - No signatures.
* - Database failure.
* - Unknown validator index.
* - Extra validator on signature.
*/
#[test]
fn test_signature_verification() {
let message = "cats".as_bytes();
let signing_keypairs = vec![
Keypair::random(),
Keypair::random(),
Keypair::random(),
Keypair::random(),
Keypair::random(),
Keypair::random(),
];
let non_signing_keypairs = vec![
Keypair::random(),
Keypair::random(),
Keypair::random(),
Keypair::random(),
Keypair::random(),
Keypair::random(),
];
/*
* Signing keypairs first, then non-signing
*/
let mut all_keypairs = signing_keypairs.clone();
all_keypairs.append(&mut non_signing_keypairs.clone());
let attestation_indices: Vec<usize> = (0..all_keypairs.len())
.collect();
let mut bitfield = Bitfield::new();
for i in 0..signing_keypairs.len() {
bitfield.set_bit(i, true);
}
let db = Arc::new(MemoryDB::open());
let store = ValidatorStore::new(db);
for (i, keypair) in all_keypairs.iter().enumerate() {
store.put_public_key_by_index(i, &keypair.pk).unwrap();
}
let mut agg_sig = AggregateSignature::new();
for keypair in &signing_keypairs {
let sig = Signature::new(&message, &keypair.sk);
agg_sig.add(&sig);
}
/*
* Test using all valid parameters.
*/
let voters = verify_aggregate_signature_for_indices(
&message,
&agg_sig,
&attestation_indices,
&bitfield,
&store).unwrap();
let voters = voters.unwrap();
(0..signing_keypairs.len())
.for_each(|i| assert!(voters.contains(&i)));
(signing_keypairs.len()..non_signing_keypairs.len())
.for_each(|i| assert!(!voters.contains(&i)));
/*
* Add another validator to the bitfield, run validation will all other
* parameters the same and assert that it fails.
*/
bitfield.set_bit(signing_keypairs.len() + 1, true);
let voters = verify_aggregate_signature_for_indices(
&message,
&agg_sig,
&attestation_indices,
&bitfield,
&store).unwrap();
assert_eq!(voters, None);
}
}

View File

@@ -1,17 +0,0 @@
extern crate blake2_rfc;
use super::attestation_record;
use super::common;
use super::db;
use super::ssz;
use super::utils;
mod structs;
mod ssz_block;
pub mod validation;
pub use self::structs::Block;
pub use self::ssz_block::{
SszBlock,
SszBlockError,
};

View File

@@ -1,356 +0,0 @@
use super::ssz::decode::{
decode_length,
Decodable,
};
use super::utils::hash::canonical_hash;
use super::structs::{
MIN_SSZ_BLOCK_LENGTH,
MAX_SSZ_BLOCK_LENGTH,
};
use super::attestation_record::MIN_SSZ_ATTESTION_RECORD_LENGTH;
#[derive(Debug, PartialEq)]
pub enum SszBlockError {
TooShort,
TooLong,
}
const LENGTH_BYTES: usize = 4;
/// Allows for reading of block values directly from serialized ssz bytes.
///
/// The purpose of this struct is to provide the functionality to read block fields directly from
/// some serialized SSZ slice allowing us to read the block without fully
/// de-serializing it.
///
/// This struct should be as "zero-copy" as possible. The `ssz` field is a reference to some slice
/// and each function reads from that slice.
///
/// Use this to perform intial checks before we fully de-serialize a block. It should only really
/// be used to verify blocks that come in from the network, for internal operations we should use a
/// full `Block`.
#[derive(Debug, PartialEq)]
pub struct SszBlock<'a> {
ssz: &'a [u8],
attestation_len: usize,
pub len: usize,
}
impl<'a> SszBlock<'a> {
/// Create a new instance from a slice reference.
///
/// This function will validate the length of the ssz string, however it will not validate the
/// contents.
///
/// The returned `SszBlock` instance will contain a `len` field which can be used to determine
/// how many bytes were read from the slice. In the case of multiple, sequentually serialized
/// blocks `len` can be used to assume the location of the next serialized block.
pub fn from_slice(vec: &'a [u8])
-> Result<Self, SszBlockError>
{
let untrimmed_ssz = &vec[..];
/*
* Ensure the SSZ is long enough to be a block with
* one attestation record (not necessarily a valid
* attestation record).
*/
if vec.len() < MIN_SSZ_BLOCK_LENGTH + MIN_SSZ_ATTESTION_RECORD_LENGTH {
return Err(SszBlockError::TooShort);
}
/*
* Ensure the SSZ slice isn't longer than is possible for a block.
*/
if vec.len() > MAX_SSZ_BLOCK_LENGTH {
return Err(SszBlockError::TooLong);
}
/*
* Determine how many bytes are used to store attestation records.
*/
let attestation_len = decode_length(untrimmed_ssz, 72, LENGTH_BYTES)
.map_err(|_| SszBlockError::TooShort)?;
/*
* The block only has one variable field, `attestations`, therefore
* the size of the block must be the minimum size, plus the length
* of the attestations.
*/
let block_ssz_len = {
MIN_SSZ_BLOCK_LENGTH + attestation_len
};
if vec.len() < block_ssz_len {
return Err(SszBlockError::TooShort);
}
Ok(Self{
ssz: &untrimmed_ssz[0..block_ssz_len],
attestation_len,
len: block_ssz_len,
})
}
/// Return the canonical hash for this block.
pub fn block_hash(&self) -> Vec<u8> {
canonical_hash(self.ssz)
}
/// Return the `parent_hash` field.
pub fn parent_hash(&self) -> &[u8] {
&self.ssz[0..32]
}
/// Return the `slot_number` field.
pub fn slot_number(&self) -> u64 {
/*
* An error should be unreachable from this decode
* because we checked the length of the array at
* the initalization of this struct.
*
* If you can make this function panic, please report
* it to paul@sigmaprime.io
*/
if let Ok((n, _)) = u64::ssz_decode(&self.ssz, 32) {
n
} else {
unreachable!();
}
}
/// Return the `randao_reveal` field.
pub fn randao_reveal(&self) -> &[u8] {
&self.ssz[40..72]
}
/// Return the `attestations` field.
pub fn attestations(&self) -> &[u8] {
let start = 72 + LENGTH_BYTES;
&self.ssz[start..(start + self.attestation_len)]
}
/// Return the `pow_chain_ref` field.
pub fn pow_chain_ref(&self) -> &[u8] {
let start = self.len - (32 * 3);
&self.ssz[start..(start + 32)]
}
/// Return the `active_state_root` field.
pub fn act_state_root(&self) -> &[u8] {
let start = self.len - (32 * 2);
&self.ssz[start..(start + 32)]
}
/// Return the `active_state_root` field.
pub fn cry_state_root(&self) -> &[u8] {
let start = self.len - 32;
&self.ssz[start..(start + 32)]
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::super::structs::Block;
use super::super::attestation_record::AttestationRecord;
use super::super::ssz::SszStream;
use super::super::utils::types::Hash256;
fn get_block_ssz(b: &Block) -> Vec<u8> {
let mut ssz_stream = SszStream::new();
ssz_stream.append(b);
ssz_stream.drain()
}
fn get_attestation_record_ssz(ar: &AttestationRecord) -> Vec<u8> {
let mut ssz_stream = SszStream::new();
ssz_stream.append(ar);
ssz_stream.drain()
}
#[test]
fn test_ssz_block_zero_attestation_records() {
let mut b = Block::zero();
b.attestations = vec![];
let ssz = get_block_ssz(&b);
assert_eq!(
SszBlock::from_slice(&ssz[..]),
Err(SszBlockError::TooShort)
);
}
#[test]
fn test_ssz_block_single_attestation_record_one_byte_short() {
let mut b = Block::zero();
b.attestations = vec![AttestationRecord::zero()];
let ssz = get_block_ssz(&b);
assert_eq!(
SszBlock::from_slice(&ssz[0..(ssz.len() - 1)]),
Err(SszBlockError::TooShort)
);
}
#[test]
fn test_ssz_block_single_attestation_record_one_byte_long() {
let mut b = Block::zero();
b.attestations = vec![AttestationRecord::zero()];
let mut ssz = get_block_ssz(&b);
let original_len = ssz.len();
ssz.push(42);
let ssz_block = SszBlock::from_slice(&ssz[..]).unwrap();
assert_eq!(ssz_block.len, original_len);
}
#[test]
fn test_ssz_block_single_attestation_record() {
let mut b = Block::zero();
b.attestations = vec![AttestationRecord::zero()];
let ssz = get_block_ssz(&b);
assert!(SszBlock::from_slice(&ssz[..]).is_ok());
}
#[test]
fn test_ssz_block_attestation_length() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.attestation_len, MIN_SSZ_ATTESTION_RECORD_LENGTH);
}
#[test]
fn test_ssz_block_block_hash() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
let hash = ssz_block.block_hash();
// Note: this hash was not generated by some external program,
// it was simply printed then copied into the code. This test
// will tell us if the hash changes, not that it matches some
// canonical reference.
let expected_hash = [
64, 176, 117, 210, 228, 229, 237, 100, 66, 66, 98,
252, 31, 111, 218, 27, 160, 57, 164, 12, 15, 164,
66, 102, 142, 36, 2, 196, 121, 54, 242, 3
];
assert_eq!(hash, expected_hash);
/*
* Test if you give the SszBlock too many ssz bytes
*/
let mut too_long = serialized.clone();
too_long.push(42);
let ssz_block = SszBlock::from_slice(&too_long).unwrap();
let hash = ssz_block.block_hash();
assert_eq!(hash, expected_hash);
}
#[test]
fn test_ssz_block_parent_hash() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let reference_hash = Hash256::from([42_u8; 32]);
block.parent_hash = reference_hash.clone();
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.parent_hash(), &reference_hash.to_vec()[..]);
}
#[test]
fn test_ssz_block_slot_number() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
block.slot_number = 42;
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.slot_number(), 42);
}
#[test]
fn test_ssz_block_randao_reveal() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let reference_hash = Hash256::from([42_u8; 32]);
block.randao_reveal = reference_hash.clone();
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.randao_reveal(), &reference_hash.to_vec()[..]);
}
#[test]
fn test_ssz_block_attestations() {
/*
* Single AttestationRecord
*/
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
let ssz_ar = get_attestation_record_ssz(&AttestationRecord::zero());
assert_eq!(ssz_block.attestations(), &ssz_ar[..]);
/*
* Multiple AttestationRecords
*/
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
block.attestations.push(AttestationRecord::zero());
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
let mut ssz_ar = get_attestation_record_ssz(&AttestationRecord::zero());
ssz_ar.append(&mut get_attestation_record_ssz(&AttestationRecord::zero()));
assert_eq!(ssz_block.attestations(), &ssz_ar[..]);
}
#[test]
fn test_ssz_block_pow_chain_ref() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let reference_hash = Hash256::from([42_u8; 32]);
block.pow_chain_ref = reference_hash.clone();
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.pow_chain_ref(), &reference_hash.to_vec()[..]);
}
#[test]
fn test_ssz_block_act_state_root() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let reference_hash = Hash256::from([42_u8; 32]);
block.active_state_root = reference_hash.clone();
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.act_state_root(), &reference_hash.to_vec()[..]);
}
#[test]
fn test_ssz_block_cry_state_root() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let reference_hash = Hash256::from([42_u8; 32]);
block.crystallized_state_root = reference_hash.clone();
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.cry_state_root(), &reference_hash.to_vec()[..]);
}
}

View File

@@ -1,80 +0,0 @@
use super::utils::types::Hash256;
use super::attestation_record::AttestationRecord;
use super::ssz::{ Encodable, SszStream };
pub const MIN_SSZ_BLOCK_LENGTH: usize = {
32 + // parent_hash
8 + // slot_number
32 + // randao_reveal
4 + // attestations (assuming zero)
32 + // pow_chain_ref
32 + // active_state_root
32 // crystallized_state_root
};
pub const MAX_SSZ_BLOCK_LENGTH: usize = MIN_SSZ_BLOCK_LENGTH + (1 << 24);
#[derive(Debug, PartialEq, Clone)]
pub struct Block {
pub parent_hash: Hash256,
pub slot_number: u64,
pub randao_reveal: Hash256,
pub attestations: Vec<AttestationRecord>,
pub pow_chain_ref: Hash256,
pub active_state_root: Hash256,
pub crystallized_state_root: Hash256,
}
impl Block {
pub fn zero() -> Self {
Self {
parent_hash: Hash256::zero(),
slot_number: 0,
randao_reveal: Hash256::zero(),
attestations: vec![],
pow_chain_ref: Hash256::zero(),
active_state_root: Hash256::zero(),
crystallized_state_root: Hash256::zero(),
}
}
}
impl Encodable for Block {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.parent_hash);
s.append(&self.slot_number);
s.append(&self.randao_reveal);
s.append_vec(&self.attestations);
s.append(&self.pow_chain_ref);
s.append(&self.active_state_root);
s.append(&self.crystallized_state_root);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_block_zero() {
let b = Block::zero();
assert!(b.parent_hash.is_zero());
assert_eq!(b.slot_number, 0);
assert!(b.randao_reveal.is_zero());
assert_eq!(b.attestations.len(), 0);
assert!(b.pow_chain_ref.is_zero());
assert!(b.active_state_root.is_zero());
assert!(b.crystallized_state_root.is_zero());
}
#[test]
pub fn test_block_min_ssz_length() {
let b = Block::zero();
let mut ssz_stream = SszStream::new();
ssz_stream.append(&b);
let ssz = ssz_stream.drain();
assert_eq!(ssz.len(), MIN_SSZ_BLOCK_LENGTH);
}
}

View File

@@ -1,370 +0,0 @@
extern crate rayon;
use self::rayon::prelude::*;
use std::sync::{
Arc,
RwLock,
};
use super::attestation_record::{
AttestationValidationContext,
AttestationValidationError,
};
use super::attestation_record::{
AttestationRecord,
split_one_attestation,
split_all_attestations,
AttestationSplitError,
};
use super::{
AttesterMap,
ProposerMap,
};
use super::{
SszBlock,
SszBlockError,
Block,
};
use super::db::{
ClientDB,
DBError,
};
use super::db::stores::{
BlockStore,
PoWChainStore,
ValidatorStore,
};
use super::ssz::{
Decodable,
DecodeError,
};
use super::utils::types::Hash256;
#[derive(Debug, PartialEq)]
pub enum BlockStatus {
NewBlock,
KnownBlock,
}
#[derive(Debug, PartialEq)]
pub enum SszBlockValidationError {
FutureSlot,
SlotAlreadyFinalized,
UnknownPoWChainRef,
UnknownParentHash,
BadAttestationSsz,
AttestationValidationError(AttestationValidationError),
AttestationSignatureFailed,
ProposerAttestationHasObliqueHashes,
NoProposerSignature,
BadProposerMap,
RwLockPoisoned,
DBError(String),
}
/// The context against which a block should be validated.
pub struct BlockValidationContext<T>
where T: ClientDB + Sized
{
/// The slot as determined by the system time.
pub present_slot: u64,
/// The cycle_length as determined by the chain configuration.
pub cycle_length: u8,
/// The last justified slot as per the client's view of the canonical chain.
pub last_justified_slot: u64,
/// The last finalized slot as per the client's view of the canonical chain.
pub last_finalized_slot: u64,
/// A vec of the hashes of the blocks preceeding the present slot.
pub parent_hashes: Arc<Vec<Hash256>>,
/// A map of slots to a block proposer validation index.
pub proposer_map: Arc<ProposerMap>,
/// A map of (slot, shard_id) to the attestation set of validation indices.
pub attester_map: Arc<AttesterMap>,
/// The store containing block information.
pub block_store: Arc<BlockStore<T>>,
/// The store containing validator information.
pub validator_store: Arc<ValidatorStore<T>>,
/// The store containing information about the proof-of-work chain.
pub pow_store: Arc<PoWChainStore<T>>,
}
impl<T> BlockValidationContext<T>
where T: ClientDB
{
/// Validate some SszBlock against a block validation context. An SszBlock varies from a Block in
/// that is a read-only structure that reads directly from encoded SSZ.
///
/// The reason to validate an SzzBlock is to avoid decoding it in its entirety if there is
/// a suspicion that the block might be invalid. Such a suspicion should be applied to
/// all blocks coming from the network.
///
/// This function will determine if the block is new, already known or invalid (either
/// intrinsically or due to some application error.)
///
/// Note: this function does not implement randao_reveal checking as it is not in the
/// specification.
#[allow(dead_code)]
pub fn validate_ssz_block(&self, b: &SszBlock)
-> Result<(BlockStatus, Option<Block>), SszBlockValidationError>
where T: ClientDB + Sized
{
/*
* If this block is already known, return immediately and indicate the the block is
* known. Don't attempt to deserialize the block.
*/
let block_hash = &b.block_hash();
if self.block_store.block_exists(&block_hash)? {
return Ok((BlockStatus::KnownBlock, None));
}
/*
* If the block slot corresponds to a slot in the future, drop it.
*/
let block_slot = b.slot_number();
if block_slot > self.present_slot {
return Err(SszBlockValidationError::FutureSlot);
}
/*
* If the block is unknown (assumed unknown because we checked the db earlier in this
* function) and it comes from a slot that is already finalized, drop the block.
*
* If a slot is finalized, there's no point in considering any other blocks for that slot.
*/
if block_slot <= self.last_finalized_slot {
return Err(SszBlockValidationError::SlotAlreadyFinalized);
}
/*
* If the PoW chain hash is not known to us, drop it.
*
* We only accept blocks that reference a known PoW hash.
*
* Note: it is not clear what a "known" PoW chain ref is. Likely it means the block hash is
* "sufficienty deep in the canonical PoW chain". This should be clarified as the spec
* crystallizes.
*/
let pow_chain_ref = b.pow_chain_ref();
if !self.pow_store.block_hash_exists(b.pow_chain_ref())? {
return Err(SszBlockValidationError::UnknownPoWChainRef);
}
/*
* Store a slice of the serialized attestations from the block SSZ.
*/
let attestations_ssz = &b.attestations();
/*
* Get a slice of the first serialized attestation (the 0'th) and decode it into
* a full AttestationRecord object.
*
* The first attestation must be validated separately as it must contain a signature of the
* proposer of the previous block (this is checked later in this function).
*/
let (first_attestation_ssz, next_index) = split_one_attestation(
&attestations_ssz,
0)?;
let (first_attestation, _) = AttestationRecord::ssz_decode(
&first_attestation_ssz, 0)?;
/*
* The first attestation may not have oblique hashes.
*
* The presence of oblique hashes in the first attestation would indicate that the proposer
* of the previous block is attesting to some other block than the one they produced.
*/
if first_attestation.oblique_parent_hashes.len() > 0 {
return Err(SszBlockValidationError::ProposerAttestationHasObliqueHashes);
}
/*
* Generate the context in which attestations will be validated.
*/
let attestation_validation_context = Arc::new(AttestationValidationContext {
block_slot,
cycle_length: self.cycle_length,
last_justified_slot: self.last_justified_slot,
parent_hashes: self.parent_hashes.clone(),
block_store: self.block_store.clone(),
validator_store: self.validator_store.clone(),
attester_map: self.attester_map.clone(),
});
/*
* Validate this first attestation.
*/
let attestation_voters = attestation_validation_context
.validate_attestation(&first_attestation)?;
/*
* Read the parent hash from the block we are validating then attempt to load
* the parent block ssz from the database. If that parent doesn't exist in
* the database, reject the block.
*
* If the parent does exist in the database, read the slot of that parent. Then,
* determine the proposer of that slot (the parent slot) by looking it up
* in the proposer map.
*
* If that proposer (the proposer of the parent block) was not present in the first (0'th)
* attestation of this block, reject the block.
*/
let parent_hash = b.parent_hash();
match self.block_store.get_serialized_block(&parent_hash)? {
None => return Err(SszBlockValidationError::UnknownParentHash),
Some(ssz) => {
let parent_block = SszBlock::from_slice(&ssz[..])?;
let proposer = self.proposer_map.get(&parent_block.slot_number())
.ok_or(SszBlockValidationError::BadProposerMap)?;
if !attestation_voters.contains(&proposer) {
return Err(SszBlockValidationError::NoProposerSignature);
}
}
}
/*
* Split the remaining attestations into a vector of slices, each containing
* a single serialized attestation record.
*/
let other_attestations = split_all_attestations(attestations_ssz,
next_index)?;
/*
* Verify each other AttestationRecord.
*
* This uses the `rayon` library to do "sometimes" parallelization. Put simply,
* if there are some spare threads, the verification of attestation records will happen
* concurrently.
*
* There is a thread-safe `failure` variable which is set whenever an attestation fails
* validation. This is so all attestation validation is halted if a single bad attestation
* is found.
*/
let failure: RwLock<Option<SszBlockValidationError>> = RwLock::new(None);
let mut deserialized_attestations: Vec<AttestationRecord> = other_attestations
.par_iter()
.filter_map(|attestation_ssz| {
/*
* If some thread has set the `failure` variable to `Some(error)` the abandon
* attestation serialization and validation.
*/
if let Some(_) = *failure.read().unwrap() {
return None;
}
/*
* If there has not been a failure yet, attempt to serialize and validate the
* attestation.
*/
match AttestationRecord::ssz_decode(&attestation_ssz, 0) {
/*
* Deserialization failed, therefore the block is invalid.
*/
Err(e) => {
let mut failure = failure.write().unwrap();
*failure = Some(SszBlockValidationError::from(e));
None
}
/*
* Deserialization succeeded and the attestation should be validated.
*/
Ok((attestation, _)) => {
match attestation_validation_context.validate_attestation(&attestation) {
/*
* Attestation validation failed with some error.
*/
Err(e) => {
let mut failure = failure.write().unwrap();
*failure = Some(SszBlockValidationError::from(e));
None
}
/*
* Attestation validation succeded.
*/
Ok(_) => Some(attestation)
}
}
}
})
.collect();
match failure.into_inner() {
Err(_) => return Err(SszBlockValidationError::RwLockPoisoned),
Ok(failure) => {
match failure {
Some(error) => return Err(error),
_ => ()
}
}
}
/*
* Add the first attestation to the vec of deserialized attestations at
* index 0.
*/
deserialized_attestations.insert(0, first_attestation);
/*
* If we have reached this point, the block is a new valid block that is worthy of
* processing.
*/
let block = Block {
parent_hash: Hash256::from(parent_hash),
slot_number: block_slot,
randao_reveal: Hash256::from(b.randao_reveal()),
attestations: deserialized_attestations,
pow_chain_ref: Hash256::from(pow_chain_ref),
active_state_root: Hash256::from(b.act_state_root()),
crystallized_state_root: Hash256::from(b.cry_state_root()),
};
Ok((BlockStatus::NewBlock, Some(block)))
}
}
impl From<DBError> for SszBlockValidationError {
fn from(e: DBError) -> Self {
SszBlockValidationError::DBError(e.message)
}
}
impl From<AttestationSplitError> for SszBlockValidationError {
fn from(e: AttestationSplitError) -> Self {
match e {
AttestationSplitError::TooShort =>
SszBlockValidationError::BadAttestationSsz
}
}
}
impl From<SszBlockError> for SszBlockValidationError {
fn from(e: SszBlockError) -> Self {
match e {
SszBlockError::TooShort =>
SszBlockValidationError::DBError("Bad parent block in db.".to_string()),
SszBlockError::TooLong =>
SszBlockValidationError::DBError("Bad parent block in db.".to_string()),
}
}
}
impl From<DecodeError> for SszBlockValidationError {
fn from(e: DecodeError) -> Self {
match e {
DecodeError::TooShort =>
SszBlockValidationError::BadAttestationSsz,
DecodeError::TooLong =>
SszBlockValidationError::BadAttestationSsz,
}
}
}
impl From<AttestationValidationError> for SszBlockValidationError {
fn from(e: AttestationValidationError) -> Self {
SszBlockValidationError::AttestationValidationError(e)
}
}
/*
* Tests for block validation are contained in the root directory "tests" directory (AKA
* "integration tests directory").
*/

View File

@@ -1,21 +0,0 @@
mod block_validation;
use super::attestation_record;
use super::{
SszBlock,
SszBlockError,
Block,
};
use super::db;
use super::ssz;
use super::utils;
pub use super::common::maps::{
AttesterMap,
ProposerMap,
};
pub use self::block_validation::{
BlockValidationContext,
SszBlockValidationError,
BlockStatus,
};

View File

@@ -1,32 +0,0 @@
pub struct ChainConfig {
pub cycle_length: u8,
pub shard_count: u16,
pub min_committee_size: u64,
pub genesis_time: u64,
}
/*
* Presently this is just some arbitrary time in Sept 2018.
*/
const GENESIS_TIME: u64 = 1_537_488_655;
impl ChainConfig {
pub fn standard() -> Self {
Self {
cycle_length: 64,
shard_count: 1024,
min_committee_size: 128,
genesis_time: GENESIS_TIME, // arbitrary
}
}
#[cfg(test)]
pub fn super_fast_tests() -> Self {
Self {
cycle_length: 2,
shard_count: 2,
min_committee_size: 2,
genesis_time: GENESIS_TIME, // arbitrary
}
}
}

View File

@@ -1,226 +0,0 @@
use super::Hash256;
#[derive(Debug)]
pub enum ParentHashesError {
BadCurrentHashes,
BadObliqueHashes,
SlotTooHigh,
SlotTooLow,
IntWrapping,
}
/// This function is used to select the hashes used in
/// the signing of an AttestationRecord.
///
/// It either returns Result with a vector of length `cycle_length,` or
/// returns an Error.
///
/// This function corresponds to the `get_signed_parent_hashes` function
/// in the Python reference implentation.
///
/// See this slide for more information:
/// https://tinyurl.com/ybzn2spw
pub fn attestation_parent_hashes(
cycle_length: u8,
block_slot: u64,
attestation_slot: u64,
current_hashes: &[Hash256],
oblique_hashes: &[Hash256])
-> Result<Vec<Hash256>, ParentHashesError>
{
// This cast places a limit on cycle_length. If you change it, check math
// for overflow.
let cycle_length: u64 = u64::from(cycle_length);
if current_hashes.len() as u64 != (cycle_length * 2) {
return Err(ParentHashesError::BadCurrentHashes);
}
if oblique_hashes.len() as u64 > cycle_length {
return Err(ParentHashesError::BadObliqueHashes);
}
if attestation_slot >= block_slot {
return Err(ParentHashesError::SlotTooHigh);
}
/*
* Cannot underflow as block_slot cannot be less
* than attestation_slot.
*/
let attestation_distance = block_slot - attestation_slot;
if attestation_distance > cycle_length {
return Err(ParentHashesError::SlotTooLow);
}
/*
* Cannot underflow because attestation_distance cannot
* be larger than cycle_length.
*/
let start = cycle_length - attestation_distance;
/*
* Overflow is potentially impossible, but proof is complicated
* enough to just use checked math.
*
* Arithmetic is:
* start + cycle_length - oblique_hashes.len()
*/
let end = start.checked_add(cycle_length)
.and_then(|x| x.checked_sub(oblique_hashes.len() as u64))
.ok_or(ParentHashesError::IntWrapping)?;
let mut hashes = Vec::new();
hashes.extend_from_slice(
&current_hashes[(start as usize)..(end as usize)]);
hashes.extend_from_slice(oblique_hashes);
Ok(hashes)
}
#[cfg(test)]
mod tests {
use super::*;
fn get_range_of_hashes(from: usize, to: usize) -> Vec<Hash256> {
(from..to).map(|i| get_hash(&vec![i as u8])).collect()
}
fn get_hash(value: &[u8]) -> Hash256 {
Hash256::from_slice(value)
}
#[test]
fn test_get_signed_hashes_oblique_scenario_1() {
/*
* Two oblique hashes.
*/
let cycle_length: u8 = 8;
let block_slot: u64 = 19;
let attestation_slot: u64 = 15;
let current_hashes = get_range_of_hashes(3, 19);
let oblique_hashes = get_range_of_hashes(100, 102);
let result = attestation_parent_hashes(
cycle_length,
block_slot,
attestation_slot,
&current_hashes,
&oblique_hashes);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.len(), cycle_length as usize);
let mut expected_result = get_range_of_hashes(7, 13);
expected_result.append(&mut get_range_of_hashes(100, 102));
assert_eq!(result, expected_result);
}
#[test]
fn test_get_signed_hashes_oblique_scenario_2() {
/*
* All oblique hashes.
*/
let cycle_length: u8 = 8;
let block_slot: u64 = 19;
let attestation_slot: u64 = 15;
let current_hashes = get_range_of_hashes(3, 19);
let oblique_hashes = get_range_of_hashes(100, 108);
let result = attestation_parent_hashes(
cycle_length,
block_slot,
attestation_slot,
&current_hashes,
&oblique_hashes);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.len(), cycle_length as usize);
let expected_result = get_range_of_hashes(100, 108);
assert_eq!(result, expected_result);
}
#[test]
fn test_get_signed_hashes_scenario_1() {
/*
* Google Slides example.
* https://tinyurl.com/ybzn2spw
*/
let cycle_length: u8 = 8;
let block_slot: u64 = 19;
let attestation_slot: u64 = 15;
let current_hashes = get_range_of_hashes(3, 19);
let oblique_hashes = vec![];
let result = attestation_parent_hashes(
cycle_length,
block_slot,
attestation_slot,
&current_hashes,
&oblique_hashes);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.len(), cycle_length as usize);
let expected_result = get_range_of_hashes(7, 15);
assert_eq!(result, expected_result);
}
#[test]
fn test_get_signed_hashes_scenario_2() {
/*
* Block 1, attestation 0.
*/
let cycle_length: u8 = 8;
let block_slot: u64 = 1;
let attestation_slot: u64 = 0;
let current_hashes = get_range_of_hashes(0, 16);
let oblique_hashes = vec![];
let result = attestation_parent_hashes(
cycle_length,
block_slot,
attestation_slot,
&current_hashes,
&oblique_hashes);
let result = result.unwrap();
assert_eq!(result.len(), cycle_length as usize);
let expected_result = get_range_of_hashes(7, 15);
assert_eq!(result, expected_result);
}
#[test]
fn test_get_signed_hashes_scenario_3() {
/*
* attestation_slot too large
*/
let cycle_length: u8 = 8;
let block_slot: u64 = 100;
let attestation_slot: u64 = 100;
let current_hashes = get_range_of_hashes(0, 16);
let oblique_hashes = vec![];
let result = attestation_parent_hashes(
cycle_length,
block_slot,
attestation_slot,
&current_hashes,
&oblique_hashes);
assert!(result.is_err());
}
#[test]
fn test_get_signed_hashes_scenario_4() {
/*
* Current hashes too small
*/
let cycle_length: u8 = 8;
let block_slot: u64 = 100;
let attestation_slot: u64 = 99;
let current_hashes = get_range_of_hashes(0, 15);
let oblique_hashes = vec![];
let result = attestation_parent_hashes(
cycle_length,
block_slot,
attestation_slot,
&current_hashes,
&oblique_hashes);
assert!(result.is_err());
}
}

View File

@@ -1,62 +0,0 @@
use super::utils::errors::ParameterError;
use super::utils::types::Hash256;
/*
* Work-in-progress function: not ready for review.
*/
pub fn get_block_hash(
active_state_recent_block_hashes: &[Hash256],
current_block_slot: u64,
slot: u64,
cycle_length: u64, // convert from standard u8
) -> Result<Hash256, ParameterError> {
// active_state must have at 2*cycle_length hashes
assert_error!(
active_state_recent_block_hashes.len() as u64 == cycle_length * 2,
ParameterError::InvalidInput(String::from(
"active state has incorrect number of block hashes"
))
);
let state_start_slot = (current_block_slot)
.checked_sub(cycle_length * 2)
.unwrap_or(0);
assert_error!(
(state_start_slot <= slot) && (slot < current_block_slot),
ParameterError::InvalidInput(String::from("incorrect slot number"))
);
let index = 2 * cycle_length + slot - current_block_slot; // should always be positive
Ok(active_state_recent_block_hashes[index as usize])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_block_hash() {
let block_slot: u64 = 10;
let slot: u64 = 3;
let cycle_length: u64 = 8;
let mut block_hashes: Vec<Hash256> = Vec::new();
for _i in 0..2 * cycle_length {
block_hashes.push(Hash256::random());
}
let result = get_block_hash(
&block_hashes,
block_slot,
slot,
cycle_length)
.unwrap();
assert_eq!(
result,
block_hashes[(2 * cycle_length + slot - block_slot) as usize]
);
}
}

View File

@@ -1,3 +0,0 @@
mod block_hash;
use super::utils;

View File

@@ -1,7 +0,0 @@
use std::collections::HashMap;
/// Maps a (slot, shard_id) to attestation_indices.
pub type AttesterMap = HashMap<(u64, u16), Vec<usize>>;
/// Maps a slot to a block proposer.
pub type ProposerMap = HashMap<u64, usize>;

View File

@@ -1,9 +0,0 @@
mod delegation;
mod shuffling;
pub mod maps;
pub mod attestation_parent_hashes;
use super::utils;
use super::utils::types::Hash256;
pub use self::shuffling::shuffle;

View File

@@ -1,2 +0,0 @@
This module includes the fundamental shuffling function. It does not do the
full validator delegation amongst slots.

View File

@@ -1,52 +0,0 @@
extern crate blake2_rfc;
mod rng;
use self::rng::ShuffleRng;
#[derive(Debug)]
pub enum ShuffleErr {
ExceedsListLength,
}
/// Performs a deterministic, in-place shuffle of a vector of bytes.
/// The final order of the shuffle is determined by successive hashes
/// of the supplied `seed`.
pub fn shuffle(
seed: &[u8],
mut list: Vec<usize>)
-> Result<Vec<usize>, ShuffleErr>
{
let mut rng = ShuffleRng::new(seed);
if list.len() > rng.rand_max as usize {
return Err(ShuffleErr::ExceedsListLength);
}
for i in 0..(list.len() - 1) {
let n = list.len() - i;
let j = rng.rand_range(n as u32) as usize + i;
list.swap(i, j);
}
Ok(list)
}
#[cfg(test)]
mod tests {
use super::*;
use super::blake2_rfc::blake2s::{ blake2s, Blake2sResult };
fn hash(seed: &[u8]) -> Blake2sResult {
blake2s(32, &[], seed)
}
#[test]
fn test_shuffling() {
let seed = hash(b"4kn4driuctg8");
let list: Vec<usize> = (0..12).collect();
let s = shuffle(seed.as_bytes(), list).unwrap();
assert_eq!(
s,
vec![7, 4, 8, 6, 5, 3, 0, 11, 1, 2, 10, 9],
)
}
}

View File

@@ -1,129 +0,0 @@
use super::blake2_rfc::blake2s::{ Blake2s, Blake2sResult };
const SEED_SIZE_BYTES: usize = 32;
const RAND_BYTES: usize = 3; // 24 / 8
const RAND_MAX: u32 = 16_777_216; // 2**24
/// A pseudo-random number generator which given a seed
/// uses successive blake2s hashing to generate "entropy".
pub struct ShuffleRng {
seed: Blake2sResult,
idx: usize,
pub rand_max: u32,
}
impl ShuffleRng {
/// Create a new instance given some "seed" bytes.
pub fn new(initial_seed: &[u8]) -> Self {
Self {
seed: hash(initial_seed),
idx: 0,
rand_max: RAND_MAX,
}
}
/// "Regenerates" the seed by hashing it.
fn rehash_seed(&mut self) {
self.seed = hash(self.seed.as_bytes());
self.idx = 0;
}
/// Extracts 3 bytes from the `seed`. Rehashes seed if required.
fn rand(&mut self) -> u32 {
self.idx += RAND_BYTES;
if self.idx >= SEED_SIZE_BYTES {
self.rehash_seed();
self.rand()
} else {
int_from_byte_slice(
self.seed.as_bytes(),
self.idx - RAND_BYTES,
)
}
}
/// Generate a random u32 below the specified maximum `n`.
///
/// Provides a filtered result from a higher-level rng, by discarding
/// results which may bias the output. Because of this, execution time is
/// not linear and may potentially be infinite.
pub fn rand_range(&mut self, n: u32) -> u32 {
assert!(n < RAND_MAX, "RAND_MAX exceed");
let mut x = self.rand();
while x >= self.rand_max - (self.rand_max % n) {
x = self.rand();
}
x % n
}
}
/// Reads the next three bytes of `source`, starting from `offset` and
/// interprets those bytes as a 24 bit big-endian integer.
/// Returns that integer.
fn int_from_byte_slice(source: &[u8], offset: usize) -> u32 {
(
u32::from(source[offset + 2])) |
(u32::from(source[offset + 1]) << 8) |
(u32::from(source[offset ]) << 16
)
}
/// Peform a blake2s hash on the given bytes.
fn hash(bytes: &[u8]) -> Blake2sResult {
let mut hasher = Blake2s::new(SEED_SIZE_BYTES);
hasher.update(bytes);
hasher.finalize()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_shuffling_int_from_slice() {
let mut x = int_from_byte_slice(
&[0, 0, 1],
0);
assert_eq!((x as u32), 1);
x = int_from_byte_slice(
&[0, 1, 1],
0);
assert_eq!(x, 257);
x = int_from_byte_slice(
&[1, 1, 1],
0);
assert_eq!(x, 65793);
x = int_from_byte_slice(
&[255, 1, 1],
0);
assert_eq!(x, 16711937);
x = int_from_byte_slice(
&[255, 255, 255],
0);
assert_eq!(x, 16777215);
x = int_from_byte_slice(
&[0x8f, 0xbb, 0xc7],
0);
assert_eq!(x, 9419719);
}
#[test]
fn test_shuffling_hash_fn() {
let digest = hash(hash(b"4kn4driuctg8").as_bytes()); // double-hash is intentional
let digest_bytes = digest.as_bytes();
let expected = [
0xff, 0xff, 0xff, 0x8f, 0xbb, 0xc7, 0xab, 0x64, 0x43, 0x9a,
0xe5, 0x12, 0x44, 0xd8, 0x70, 0xcf, 0xe5, 0x79, 0xf6, 0x55,
0x6b, 0xbd, 0x81, 0x43, 0xc5, 0xcd, 0x70, 0x2b, 0xbe, 0xe3,
0x87, 0xc7,
];
assert_eq!(digest_bytes.len(), expected.len());
assert_eq!(digest_bytes, expected)
}
}

View File

@@ -1,29 +0,0 @@
use super::utils::types::Hash256;
#[derive(Clone)]
pub struct CrosslinkRecord {
pub dynasty: u64,
pub hash: Hash256,
}
impl CrosslinkRecord {
/// Generates a new instance where `dynasty` and `hash` are both zero.
pub fn zero() -> Self {
Self {
dynasty: 0,
hash: Hash256::zero(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_crosslink_record_zero() {
let c = CrosslinkRecord::zero();
assert_eq!(c.dynasty, 0);
assert!(c.hash.is_zero());
}
}

View File

@@ -1,65 +0,0 @@
use super::validator_record::ValidatorRecord;
use super::crosslink_record::CrosslinkRecord;
use super::shard_and_committee::ShardAndCommittee;
use super::ethereum_types::U256;
use super::utils::types::{ Hash256 };
pub struct CrystallizedState {
pub validators: Vec<ValidatorRecord>,
pub epoch_number: u64,
pub indicies_for_heights: Vec<ShardAndCommittee>,
pub last_justified_slot: u64,
pub justified_streak: u16,
pub last_finalized_slot: u64,
pub current_dynasty: u64,
pub crosslinking_shard_start: u16,
pub crosslink_records: Vec<CrosslinkRecord>,
pub total_deposits: U256,
pub dynasty_seed: Hash256,
pub dynasty_seed_last_reset: u64,
}
impl CrystallizedState {
/// Returns a new instance where all fields are either zero or an
/// empty vector.
pub fn zero() -> Self {
Self {
validators: vec![],
epoch_number: 0,
indicies_for_heights: vec![],
last_justified_slot: 0,
justified_streak: 0,
last_finalized_slot: 0,
current_dynasty: 0,
crosslinking_shard_start: 0,
crosslink_records: vec![],
total_deposits: U256::zero(),
dynasty_seed: Hash256::zero(),
dynasty_seed_last_reset: 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cry_state_zero() {
let c = CrystallizedState::zero();
assert_eq!(c.validators.len(), 0);
assert_eq!(c.epoch_number, 0);
assert_eq!(c.indicies_for_heights.len(), 0);
assert_eq!(c.last_justified_slot, 0);
assert_eq!(c.justified_streak, 0);
assert_eq!(c.last_finalized_slot, 0);
assert_eq!(c.current_dynasty, 0);
assert_eq!(c.crosslinking_shard_start, 0);
assert_eq!(c.crosslink_records.len(), 0);
assert!(c.total_deposits.is_zero());
assert!(c.dynasty_seed.is_zero());
assert_eq!(c.dynasty_seed_last_reset, 0);
}
}

View File

@@ -1,20 +0,0 @@
extern crate rlp;
extern crate ethereum_types;
extern crate blake2_rfc as blake2;
extern crate bytes;
extern crate ssz;
mod common;
pub mod active_state;
pub mod attestation_record;
pub mod crystallized_state;
pub mod chain_config;
pub mod block;
pub mod crosslink_record;
pub mod shard_and_committee;
pub mod validator_record;
use super::bls;
use super::db;
use super::utils;

View File

@@ -1,29 +0,0 @@
#[derive(Clone,Debug)]
pub struct ShardAndCommittee {
pub shard_id: u16,
pub committee: Vec<usize>
}
impl ShardAndCommittee {
/// Returns a new instance where the `shard_id` is zero and the
/// committee is an empty vector.
pub fn zero() -> Self {
Self {
shard_id: 0,
committee: vec![],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_shard_and_committee_zero() {
let s = ShardAndCommittee::zero();
assert_eq!(s.shard_id, 0);
assert_eq!(s.committee.len(), 0);
}
}

View File

@@ -1,62 +0,0 @@
extern crate rand;
use super::utils::types::{ Hash256, Address, U256 };
use super::bls::{ PublicKey, Keypair };
pub struct ValidatorRecord {
pub pubkey: PublicKey,
pub withdrawal_shard: u16,
pub withdrawal_address: Address,
pub randao_commitment: Hash256,
pub balance: U256,
pub start_dynasty: u64,
pub end_dynasty: u64,
}
impl ValidatorRecord {
/// Generates a new instance where the keypair is generated using
/// `rand::thread_rng` entropy and all other fields are set to zero.
///
/// Returns the new instance and new keypair.
pub fn zero_with_thread_rand_keypair() -> (Self, Keypair) {
let keypair = Keypair::random();
let s = Self {
pubkey: keypair.pk.clone(),
withdrawal_shard: 0,
withdrawal_address: Address::zero(),
randao_commitment: Hash256::zero(),
balance: U256::zero(),
start_dynasty: 0,
end_dynasty: 0,
};
(s, keypair)
}
}
impl Clone for ValidatorRecord {
fn clone(&self) -> ValidatorRecord {
ValidatorRecord {
pubkey: self.pubkey.clone(),
..*self
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validator_record_zero_rand_keypair() {
let (v, _kp) = ValidatorRecord::zero_with_thread_rand_keypair();
// TODO: check keys
assert_eq!(v.withdrawal_shard, 0);
assert!(v.withdrawal_address.is_zero());
assert!(v.randao_commitment.is_zero());
assert!(v.balance.is_zero());
assert_eq!(v.start_dynasty, 0);
assert_eq!(v.end_dynasty, 0);
}
}

View File

@@ -1,8 +0,0 @@
// Collection of custom errors
#[derive(Debug,PartialEq)]
pub enum ParameterError {
IntWrapping,
OutOfBounds,
InvalidInput(String),
}

View File

@@ -1,6 +0,0 @@
use super::blake2::blake2b::blake2b;
pub fn canonical_hash(input: &[u8]) -> Vec<u8> {
let result = blake2b(64, &[], input);
result.as_bytes()[0..32].to_vec()
}

View File

@@ -1,22 +0,0 @@
extern crate slog;
extern crate slog_term;
extern crate slog_async;
use slog::*;
pub use slog::Logger;
pub fn test_logger() -> slog::Logger {
let plain = slog_term::PlainSyncDecorator::new(slog_term::TestStdoutWriter);
Logger::root(
slog_term::FullFormat::new(plain)
.build().fuse(), o!()
)
}
pub fn get_logger() -> slog::Logger {
let decorator = slog_term::TermDecorator::new().build();
let drain = slog_term::CompactFormat::new(decorator).build().fuse();
let drain = slog_async::Async::new(drain).build().fuse();
slog::Logger::root(drain, o!())
}

View File

@@ -1,8 +0,0 @@
#[macro_export]
macro_rules! assert_error {
($exp: expr, $err: expr) => {
if !$exp {
return Err($err);
}
}
}

View File

@@ -1,11 +0,0 @@
extern crate ethereum_types;
extern crate blake2_rfc as blake2;
extern crate crypto_mac;
extern crate boolean_bitfield;
#[macro_use]
pub mod macros;
pub mod hash;
pub mod types;
pub mod logging;
pub mod errors;

View File

@@ -1,12 +0,0 @@
extern crate boolean_bitfield;
use super::ethereum_types::{ H256, H160 };
use self::boolean_bitfield::BooleanBitfield;
pub use super::ethereum_types::U256;
pub type Hash256 = H256;
pub type Address = H160;
pub type Bitfield = BooleanBitfield;