Added tree_hash impl for Vec and Hashtable (list and container); plus various cleanup code

This commit is contained in:
mjkeating
2018-12-08 15:25:59 -08:00
parent f11c619ef5
commit 68629acebe
5 changed files with 99 additions and 88 deletions

View File

@@ -18,10 +18,6 @@ impl ShardAndCommittee {
}
impl TreeHash for ShardAndCommittee {
// python sample code:
// def hash_shard_and_committee(val):
// committee = merkle_hash([x.to_bytes(3, 'big') for x in val.committee])
// return hash(val.shard_id.to_bytes(2, 'big') + committee)
fn tree_hash(&self) -> Vec<u8> {
let mut committee_ssz_items = Vec::new();
for c in &self.committee {

View File

@@ -2,6 +2,16 @@ 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,
@@ -46,22 +56,15 @@ impl ValidatorRecord {
}
impl TreeHash for ValidatorRecord {
/* python sample code:
def hash_validator_record(val):
return hash(val.pubkey.to_bytes(32, 'big') + val.withdrawal_shard.to_bytes(2, 'big') + \
val.withdrawal_address + val.randao_commitment + val.balance.to_bytes(16, 'big') + \
val.start_dynasty.to_bytes(8, 'big') + val.end_dynasty.to_bytes(8, 'big'))
*/
fn tree_hash(&self) -> Vec<u8> {
// the serialized fields, to be hashed, should add up to 118 bytes in length.
// allocating it once here
let mut ssz = Vec::with_capacity(118);
let mut ssz = Vec::with_capacity(HASH_SSZ_VALIDATOR_RECORD_LENGTH);
// "val.pubkey.to_bytes(32, 'big')" logic
// From python sample: "val.pubkey.to_bytes(32, 'big')"
// TODO:
// probably all kinds of wrong here. Not sure how to convert (szz)
// pubkey into a big-endian 32 byte array. Note: as_bytes(), the only method on
// PublicKey, returns a 192 byte array.
// 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);
@@ -70,17 +73,10 @@ impl TreeHash for ValidatorRecord {
ssz.append(&mut self.withdrawal_address.tree_hash());
ssz.append(&mut self.randao_commitment.tree_hash());
// balance is a 64bit number that serializes to 8 bytes.
// Right padding here to resize to 16 bytes - not sure why
// a 16 byte array is implemented in the python code: "val.balance.to_bytes(16, 'big')"
let mut balance = self.balance.tree_hash();
balance.resize(16, 0);
ssz.append(&mut balance);
// TODO:
// ... + val.start_dynasty.to_bytes(8, 'big') + val.end_dynasty.to_bytes(8, 'big')
// Our ValidatorRecord seems to be missing the start_dynasty and end_dynasty fields
ssz.tree_hash()
}
}