Implement very raw state transition logic

This commit is contained in:
Paul Hauner
2019-01-23 19:25:05 +11:00
parent b555916808
commit 1256ba0d01
24 changed files with 748 additions and 159 deletions

View File

@@ -7,32 +7,32 @@ use rand::RngCore;
#[derive(Debug, Clone, PartialEq)]
pub struct Attestation {
pub data: AttestationData,
pub participation_bitfield: Bitfield,
pub aggregation_bitfield: Bitfield,
pub custody_bitfield: Bitfield,
pub aggregate_sig: AggregateSignature,
pub aggregate_signature: AggregateSignature,
}
impl Encodable for Attestation {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.data);
s.append(&self.participation_bitfield);
s.append(&self.aggregation_bitfield);
s.append(&self.custody_bitfield);
s.append(&self.aggregate_sig);
s.append(&self.aggregate_signature);
}
}
impl Decodable for Attestation {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (data, i) = AttestationData::ssz_decode(bytes, i)?;
let (participation_bitfield, i) = Bitfield::ssz_decode(bytes, i)?;
let (aggregation_bitfield, i) = Bitfield::ssz_decode(bytes, i)?;
let (custody_bitfield, i) = Bitfield::ssz_decode(bytes, i)?;
let (aggregate_sig, i) = AggregateSignature::ssz_decode(bytes, i)?;
let (aggregate_signature, i) = AggregateSignature::ssz_decode(bytes, i)?;
let attestation_record = Self {
data,
participation_bitfield,
aggregation_bitfield,
custody_bitfield,
aggregate_sig,
aggregate_signature,
};
Ok((attestation_record, i))
}
@@ -42,9 +42,9 @@ impl Attestation {
pub fn zero() -> Self {
Self {
data: AttestationData::zero(),
participation_bitfield: Bitfield::new(),
aggregation_bitfield: Bitfield::new(),
custody_bitfield: Bitfield::new(),
aggregate_sig: AggregateSignature::new(),
aggregate_signature: AggregateSignature::new(),
}
}
}
@@ -53,9 +53,9 @@ impl<T: RngCore> TestRandom<T> for Attestation {
fn random_for_test(rng: &mut T) -> Self {
Self {
data: <_>::random_for_test(rng),
participation_bitfield: <_>::random_for_test(rng),
aggregation_bitfield: <_>::random_for_test(rng),
custody_bitfield: <_>::random_for_test(rng),
aggregate_sig: <_>::random_for_test(rng),
aggregate_signature: <_>::random_for_test(rng),
}
}
}

View File

@@ -8,10 +8,10 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
8 + // shard
32 + // beacon_block_hash
32 + // epoch_boundary_hash
32 + // shard_block_hash
32 + // latest_crosslink_hash
32 + // shard_block_root
32 + // latest_crosslink_root
8 + // justified_slot
32 // justified_block_hash
32 // justified_block_root
};
#[derive(Debug, Clone, PartialEq, Default)]
@@ -20,10 +20,10 @@ pub struct AttestationData {
pub shard: u64,
pub beacon_block_hash: Hash256,
pub epoch_boundary_hash: Hash256,
pub shard_block_hash: Hash256,
pub latest_crosslink_hash: Hash256,
pub shard_block_root: Hash256,
pub latest_crosslink_root: Hash256,
pub justified_slot: u64,
pub justified_block_hash: Hash256,
pub justified_block_root: Hash256,
}
impl AttestationData {
@@ -33,10 +33,10 @@ impl AttestationData {
shard: 0,
beacon_block_hash: Hash256::zero(),
epoch_boundary_hash: Hash256::zero(),
shard_block_hash: Hash256::zero(),
latest_crosslink_hash: Hash256::zero(),
shard_block_root: Hash256::zero(),
latest_crosslink_root: Hash256::zero(),
justified_slot: 0,
justified_block_hash: Hash256::zero(),
justified_block_root: Hash256::zero(),
}
}
@@ -53,10 +53,10 @@ impl Encodable for AttestationData {
s.append(&self.shard);
s.append(&self.beacon_block_hash);
s.append(&self.epoch_boundary_hash);
s.append(&self.shard_block_hash);
s.append(&self.latest_crosslink_hash);
s.append(&self.shard_block_root);
s.append(&self.latest_crosslink_root);
s.append(&self.justified_slot);
s.append(&self.justified_block_hash);
s.append(&self.justified_block_root);
}
}
@@ -66,20 +66,20 @@ impl Decodable for AttestationData {
let (shard, i) = u64::ssz_decode(bytes, i)?;
let (beacon_block_hash, i) = Hash256::ssz_decode(bytes, i)?;
let (epoch_boundary_hash, i) = Hash256::ssz_decode(bytes, i)?;
let (shard_block_hash, i) = Hash256::ssz_decode(bytes, i)?;
let (latest_crosslink_hash, i) = Hash256::ssz_decode(bytes, i)?;
let (shard_block_root, i) = Hash256::ssz_decode(bytes, i)?;
let (latest_crosslink_root, i) = Hash256::ssz_decode(bytes, i)?;
let (justified_slot, i) = u64::ssz_decode(bytes, i)?;
let (justified_block_hash, i) = Hash256::ssz_decode(bytes, i)?;
let (justified_block_root, i) = Hash256::ssz_decode(bytes, i)?;
let attestation_data = AttestationData {
slot,
shard,
beacon_block_hash,
epoch_boundary_hash,
shard_block_hash,
latest_crosslink_hash,
shard_block_root,
latest_crosslink_root,
justified_slot,
justified_block_hash,
justified_block_root,
};
Ok((attestation_data, i))
}
@@ -92,10 +92,10 @@ impl<T: RngCore> TestRandom<T> for AttestationData {
shard: <_>::random_for_test(rng),
beacon_block_hash: <_>::random_for_test(rng),
epoch_boundary_hash: <_>::random_for_test(rng),
shard_block_hash: <_>::random_for_test(rng),
latest_crosslink_hash: <_>::random_for_test(rng),
shard_block_root: <_>::random_for_test(rng),
latest_crosslink_root: <_>::random_for_test(rng),
justified_slot: <_>::random_for_test(rng),
justified_block_hash: <_>::random_for_test(rng),
justified_block_root: <_>::random_for_test(rng),
}
}
}

View File

@@ -1,7 +1,7 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use rand::RngCore;
use crate::test_utils::TestRandom;
use super::AttestationData;
use crate::test_utils::TestRandom;
use rand::RngCore;
#[derive(Debug, Clone, PartialEq, Default)]
pub struct AttestationDataAndCustodyBit {
@@ -12,19 +12,16 @@ pub struct AttestationDataAndCustodyBit {
impl Encodable for AttestationDataAndCustodyBit {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.data);
s.append(&self.custody_bit);
// TODO: deal with bools
}
}
impl Decodable for AttestationDataAndCustodyBit {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (data, i) = <_>::ssz_decode(bytes, i)?;
let (custody_bit, i) = <_>::ssz_decode(bytes, i)?;
let custody_bit = false;
let attestation_data_and_custody_bit = AttestationDataAndCustodyBit {
data,
custody_bit,
};
let attestation_data_and_custody_bit = AttestationDataAndCustodyBit { data, custody_bit };
Ok((attestation_data_and_custody_bit, i))
}
@@ -34,16 +31,17 @@ impl<T: RngCore> TestRandom<T> for AttestationDataAndCustodyBit {
fn random_for_test(rng: &mut T) -> Self {
Self {
data: <_>::random_for_test(rng),
custody_bit: <_>::random_for_test(rng),
// TODO: deal with bools
custody_bit: false,
}
}
}
#[cfg(test)]
mod test {
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use super::*;
use super::super::ssz::ssz_encode;
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
#[test]
pub fn test_ssz_round_trip() {

View File

@@ -10,7 +10,7 @@ pub struct BeaconBlock {
pub slot: u64,
pub parent_root: Hash256,
pub state_root: Hash256,
pub randao_reveal: Hash256,
pub randao_reveal: Signature,
pub candidate_pow_receipt_root: Hash256,
pub signature: Signature,
pub body: BeaconBlockBody,

View File

@@ -1,10 +1,10 @@
use super::candidate_pow_receipt_root_record::CandidatePoWReceiptRootRecord;
use super::crosslink_record::CrosslinkRecord;
use super::fork_data::ForkData;
use super::pending_attestation_record::PendingAttestationRecord;
use super::validator_record::ValidatorRecord;
use super::Hash256;
use crate::candidate_pow_receipt_root_record::CandidatePoWReceiptRootRecord;
use crate::crosslink_record::CrosslinkRecord;
use crate::fork_data::ForkData;
use crate::pending_attestation_record::PendingAttestationRecord;
use crate::test_utils::TestRandom;
use crate::validator_record::ValidatorRecord;
use crate::Hash256;
use hashing::canonical_hash;
use rand::RngCore;
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};

View File

@@ -7,6 +7,7 @@ pub mod test_utils;
pub mod attestation;
pub mod attestation_data;
pub mod attestation_data_and_custody_bit;
pub mod beacon_block;
pub mod beacon_block_body;
pub mod beacon_state;
@@ -35,6 +36,7 @@ use std::collections::HashMap;
pub use crate::attestation::Attestation;
pub use crate::attestation_data::AttestationData;
pub use crate::attestation_data_and_custody_bit::AttestationDataAndCustodyBit;
pub use crate::beacon_block::BeaconBlock;
pub use crate::beacon_block_body::BeaconBlockBody;
pub use crate::beacon_state::BeaconState;

View File

@@ -6,7 +6,7 @@ use rand::RngCore;
#[derive(Debug, Clone, PartialEq)]
pub struct PendingAttestationRecord {
pub data: AttestationData,
pub participation_bitfield: Bitfield,
pub aggregation_bitfield: Bitfield,
pub custody_bitfield: Bitfield,
pub slot_included: u64,
}
@@ -14,7 +14,7 @@ pub struct PendingAttestationRecord {
impl Encodable for PendingAttestationRecord {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.data);
s.append(&self.participation_bitfield);
s.append(&self.aggregation_bitfield);
s.append(&self.custody_bitfield);
s.append(&self.slot_included);
}
@@ -23,14 +23,14 @@ impl Encodable for PendingAttestationRecord {
impl Decodable for PendingAttestationRecord {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (data, i) = <_>::ssz_decode(bytes, i)?;
let (participation_bitfield, i) = <_>::ssz_decode(bytes, i)?;
let (aggregation_bitfield, i) = <_>::ssz_decode(bytes, i)?;
let (custody_bitfield, i) = <_>::ssz_decode(bytes, i)?;
let (slot_included, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
data,
participation_bitfield,
aggregation_bitfield,
custody_bitfield,
slot_included,
},
@@ -43,7 +43,7 @@ impl<T: RngCore> TestRandom<T> for PendingAttestationRecord {
fn random_for_test(rng: &mut T) -> Self {
Self {
data: <_>::random_for_test(rng),
participation_bitfield: <_>::random_for_test(rng),
aggregation_bitfield: <_>::random_for_test(rng),
custody_bitfield: <_>::random_for_test(rng),
slot_included: <_>::random_for_test(rng),
}

View File

@@ -47,6 +47,7 @@ fn status_flag_from_byte(flag: u8) -> Result<Option<StatusFlags>, StatusFlagsDec
pub struct ValidatorRecord {
pub pubkey: PublicKey,
pub withdrawal_credentials: Hash256,
pub proposer_slots: u64,
pub randao_commitment: Hash256,
pub randao_layers: u64,
pub activation_slot: u64,
@@ -73,6 +74,7 @@ impl Default for ValidatorRecord {
Self {
pubkey: PublicKey::default(),
withdrawal_credentials: Hash256::default(),
proposer_slots: 0,
randao_commitment: Hash256::default(),
randao_layers: 0,
activation_slot: std::u64::MAX,
@@ -99,6 +101,7 @@ impl Encodable for ValidatorRecord {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.pubkey);
s.append(&self.withdrawal_credentials);
s.append(&self.proposer_slots);
s.append(&self.randao_commitment);
s.append(&self.randao_layers);
s.append(&self.activation_slot);
@@ -117,6 +120,7 @@ impl Decodable for ValidatorRecord {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (pubkey, i) = <_>::ssz_decode(bytes, i)?;
let (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?;
let (proposer_slots, i) = <_>::ssz_decode(bytes, i)?;
let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?;
let (randao_layers, i) = <_>::ssz_decode(bytes, i)?;
let (activation_slot, i) = <_>::ssz_decode(bytes, i)?;
@@ -135,6 +139,7 @@ impl Decodable for ValidatorRecord {
Self {
pubkey,
withdrawal_credentials,
proposer_slots,
randao_commitment,
randao_layers,
activation_slot,
@@ -157,6 +162,7 @@ impl<T: RngCore> TestRandom<T> for ValidatorRecord {
Self {
pubkey: <_>::random_for_test(rng),
withdrawal_credentials: <_>::random_for_test(rng),
proposer_slots: <_>::random_for_test(rng),
randao_commitment: <_>::random_for_test(rng),
randao_layers: <_>::random_for_test(rng),
activation_slot: <_>::random_for_test(rng),