Add zero() methods for cry_state and agg_vote

This commit is contained in:
Paul Hauner 2018-07-11 14:14:22 +10:00
parent 6e0eec6543
commit 5bcb62f6c4
2 changed files with 26 additions and 12 deletions

View File

@ -10,12 +10,10 @@ pub struct AggregateVote {
} }
impl AggregateVote { impl AggregateVote {
pub fn new_for_shard(shard_id: u16, pub fn zero() -> Self {
shard_block_hash: Sha256Digest) Self {
-> AggregateVote { shard_id: 0,
AggregateVote { shard_block_hash: Sha256Digest::zero(),
shard_id: shard_id,
shard_block_hash: shard_block_hash,
notary_bitfield: Vec::new(), notary_bitfield: Vec::new(),
aggregate_sig: AggregateSignature::new() aggregate_sig: AggregateSignature::new()
} }
@ -41,12 +39,10 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_new_for_shard() { fn test_zero_fn() {
let id = 1; let v = AggregateVote::zero();
let hash = Sha256Digest::random(); // TODO: test this better
let v = AggregateVote::new_for_shard(id, hash); assert_eq!(v.shard_id, 0);
assert_eq!(v.shard_id, id);
assert_eq!(v.shard_block_hash, hash);
} }
#[test] #[test]

View File

@ -22,6 +22,24 @@ pub struct CrystallizedState {
} }
impl CrystallizedState { impl CrystallizedState {
// Returns a new instance with all values set to zero.
pub fn zero() -> Self {
Self {
active_validators: Vec::new(),
queued_validators: Vec::new(),
exited_validators: Vec::new(),
current_shuffling: Vec::new(),
current_epoch: 0,
last_justified_epoch: 0,
last_finalized_epoch: 0,
dynasty: 0,
next_shard: 0,
current_checkpoint: Sha256Digest::zero(),
crosslink_records: Vec::new(),
total_deposits: U256::zero(),
}
}
pub fn num_active_validators(&self) -> usize { pub fn num_active_validators(&self) -> usize {
self.active_validators.len() self.active_validators.len()
} }