updated with latest spec changes

This commit is contained in:
mjkeating
2018-12-12 13:48:54 -08:00
parent 1e4e92bf2e
commit be2c82a732
5 changed files with 34 additions and 130 deletions

View File

@@ -1,5 +1,3 @@
use super::ssz::{merkle_hash, TreeHash};
#[derive(Clone, Debug, PartialEq)]
pub struct ShardAndCommittee {
pub shard: u16,
@@ -17,22 +15,6 @@ impl ShardAndCommittee {
}
}
impl TreeHash for ShardAndCommittee {
fn tree_hash(&self) -> Vec<u8> {
let mut committee_ssz_items = Vec::new();
for c in &self.committee {
let mut h = (*c as u32).tree_hash();
h.resize(3, 0);
committee_ssz_items.push(h);
}
let mut result = Vec::new();
result.append(&mut self.shard.tree_hash());
result.append(&mut merkle_hash(&mut committee_ssz_items));
result.as_slice().tree_hash()
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -43,15 +25,4 @@ mod tests {
assert_eq!(s.shard, 0);
assert_eq!(s.committee.len(), 0);
}
#[test]
fn test_shard_and_committee_tree_hash() {
let s = ShardAndCommittee {
shard: 1,
committee: vec![1, 2, 3],
};
// should test a known hash value
assert_eq!(s.tree_hash().len(), 32);
}
}

View File

@@ -1,17 +1,6 @@
use super::bls::{Keypair, PublicKey};
use super::ssz::TreeHash;
use super::{Address, Hash256};
pub const HASH_SSZ_VALIDATOR_RECORD_LENGTH: usize = {
32 + // pubkey.to_bytes(32, 'big')
2 + // withdrawal_shard.to_bytes(2, 'big')
20 + // withdrawal_address
32 + // randao_commitment
16 + // balance.to_bytes(16, 'big')
16 + // start_dynasty.to_bytes(8, 'big')
8 // end_dynasty.to_bytes(8, 'big')
};
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ValidatorStatus {
PendingActivation = 0,
@@ -55,32 +44,6 @@ impl ValidatorRecord {
}
}
impl TreeHash for ValidatorRecord {
fn tree_hash(&self) -> Vec<u8> {
let mut ssz = Vec::with_capacity(HASH_SSZ_VALIDATOR_RECORD_LENGTH);
// From python sample: "val.pubkey.to_bytes(32, 'big')"
// TODO:
// Need to actually convert (szz) pubkey into a big-endian 32 byte
// array.
// Also, our ValidatorRecord seems to be missing the start_dynasty
// and end_dynasty fields
let pub_key_bytes = &mut self.pubkey.as_bytes();
pub_key_bytes.resize(32, 0);
ssz.append(pub_key_bytes);
ssz.append(&mut self.withdrawal_shard.tree_hash());
ssz.append(&mut self.withdrawal_address.tree_hash());
ssz.append(&mut self.randao_commitment.tree_hash());
let mut balance = self.balance.tree_hash();
balance.resize(16, 0);
ssz.append(&mut balance);
ssz.as_slice().tree_hash()
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -96,13 +59,4 @@ mod tests {
assert_eq!(v.status, 0);
assert_eq!(v.exit_slot, 0);
}
#[test]
fn test_validator_record_ree_hash() {
let (v, _kp) = ValidatorRecord::zero_with_thread_rand_keypair();
let h = v.tree_hash();
// TODO: should check a known hash result value
assert_eq!(h.len(), 32);
}
}