More updates base upon the specs

This commit is contained in:
Kirk Baird
2019-01-23 18:06:25 +11:00
parent 560dbe4ae1
commit e047fbe914
10 changed files with 65 additions and 72 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

@@ -7,23 +7,23 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
8 + // slot
8 + // shard
32 + // beacon_block_hash
32 + // epoch_boundary_hash
32 + // epoch_boundary_root
32 + // shard_block_hash
32 + // latest_crosslink_hash
8 + // justified_slot
32 // justified_block_hash
32 // justified_block_root
};
#[derive(Debug, Clone, PartialEq, Default)]
pub struct AttestationData {
pub slot: u64,
pub shard: u64,
pub beacon_block_hash: Hash256,
pub epoch_boundary_hash: Hash256,
pub shard_block_hash: Hash256,
pub latest_crosslink_hash: Hash256,
pub beacon_block_root: Hash256,
pub epoch_boundary_root: 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 {
@@ -31,12 +31,12 @@ impl AttestationData {
Self {
slot: 0,
shard: 0,
beacon_block_hash: Hash256::zero(),
epoch_boundary_hash: Hash256::zero(),
shard_block_hash: Hash256::zero(),
latest_crosslink_hash: Hash256::zero(),
beacon_block_root: Hash256::zero(),
epoch_boundary_root: 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(),
}
}
@@ -51,12 +51,12 @@ impl Encodable for AttestationData {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.slot);
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.beacon_block_root);
s.append(&self.epoch_boundary_root);
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);
}
}
@@ -64,22 +64,22 @@ impl Decodable for AttestationData {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (slot, i) = u64::ssz_decode(bytes, i)?;
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 (beacon_block_root, i) = Hash256::ssz_decode(bytes, i)?;
let (epoch_boundary_root, 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,
beacon_block_root,
epoch_boundary_root,
shard_block_root,
latest_crosslink_root,
justified_slot,
justified_block_hash,
justified_block_root,
};
Ok((attestation_data, i))
}
@@ -90,12 +90,12 @@ impl<T: RngCore> TestRandom<T> for AttestationData {
Self {
slot: <_>::random_for_test(rng),
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),
beacon_block_root: <_>::random_for_test(rng),
epoch_boundary_root: <_>::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

@@ -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 eth1_data: Eth1Data,
pub signature: Signature,
pub body: BeaconBlockBody,

View File

@@ -47,8 +47,7 @@ fn status_flag_from_byte(flag: u8) -> Result<Option<StatusFlags>, StatusFlagsDec
pub struct ValidatorRecord {
pub pubkey: PublicKey,
pub withdrawal_credentials: Hash256,
pub randao_commitment: Hash256,
pub randao_layers: u64,
pub proposer_slots: u64,
pub activation_slot: u64,
pub exit_slot: u64,
pub withdrawal_slot: u64,
@@ -73,8 +72,7 @@ impl Default for ValidatorRecord {
Self {
pubkey: PublicKey::default(),
withdrawal_credentials: Hash256::default(),
randao_commitment: Hash256::default(),
randao_layers: 0,
proposer_slots: 0,
activation_slot: std::u64::MAX,
exit_slot: std::u64::MAX,
withdrawal_slot: std::u64::MAX,
@@ -99,8 +97,7 @@ impl Encodable for ValidatorRecord {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.pubkey);
s.append(&self.withdrawal_credentials);
s.append(&self.randao_commitment);
s.append(&self.randao_layers);
s.append(&self.proposer_slots);
s.append(&self.activation_slot);
s.append(&self.exit_slot);
s.append(&self.withdrawal_slot);
@@ -117,8 +114,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 (randao_commitment, i) = <_>::ssz_decode(bytes, i)?;
let (randao_layers, i) = <_>::ssz_decode(bytes, i)?;
let (proposer_slots, i) = <_>::ssz_decode(bytes, i)?;
let (activation_slot, i) = <_>::ssz_decode(bytes, i)?;
let (exit_slot, i) = <_>::ssz_decode(bytes, i)?;
let (withdrawal_slot, i) = <_>::ssz_decode(bytes, i)?;
@@ -135,8 +131,7 @@ impl Decodable for ValidatorRecord {
Self {
pubkey,
withdrawal_credentials,
randao_commitment,
randao_layers,
proposer_slots,
activation_slot,
exit_slot,
withdrawal_slot,
@@ -157,8 +152,7 @@ impl<T: RngCore> TestRandom<T> for ValidatorRecord {
Self {
pubkey: <_>::random_for_test(rng),
withdrawal_credentials: <_>::random_for_test(rng),
randao_commitment: <_>::random_for_test(rng),
randao_layers: <_>::random_for_test(rng),
proposer_slots: <_>::random_for_test(rng),
activation_slot: <_>::random_for_test(rng),
exit_slot: <_>::random_for_test(rng),
withdrawal_slot: <_>::random_for_test(rng),