Add RLP encoding to CrystallizedState

This commit is contained in:
Paul Hauner 2018-07-09 16:25:45 +10:00
parent 02fd250ddc
commit fd351d7145
2 changed files with 59 additions and 0 deletions

View File

@ -1,6 +1,7 @@
use super::utils::types::Sha256Digest; use super::utils::types::Sha256Digest;
use super::validator_record::ValidatorRecord; use super::validator_record::ValidatorRecord;
use super::crosslink_record::CrosslinkRecord; use super::crosslink_record::CrosslinkRecord;
use super::rlp::{ RlpStream, Encodable };
use super::ethereum_types::U256; use super::ethereum_types::U256;
pub struct CrystallizedState { pub struct CrystallizedState {
@ -36,3 +37,59 @@ impl CrystallizedState {
} }
} }
/*
* RLP Encoding
*/
impl Encodable for CrystallizedState {
fn rlp_append(&self, s: &mut RlpStream) {
s.append_list(&self.active_validators);
s.append_list(&self.queued_validators);
s.append_list(&self.exited_validators);
s.append_list(&self.current_shuffling);
s.append(&self.current_epoch);
s.append(&self.last_justified_epoch);
s.append(&self.last_finalized_epoch);
s.append(&self.dynasty);
s.append(&self.next_shard);
s.append(&self.current_checkpoint);
s.append_list(&self.crosslink_records);
s.append(&self.total_deposits);
}
}
#[cfg(test)]
mod tests {
use super::super::rlp;
use super::*;
#[test]
fn test_serialization() {
let a = CrystallizedState {
active_validators: Vec::new(),
queued_validators: Vec::new(),
exited_validators: Vec::new(),
current_shuffling: Vec::new(),
current_epoch: 10,
last_justified_epoch: 8,
last_finalized_epoch: 2,
dynasty: 3,
next_shard: 12,
current_checkpoint: Sha256Digest::zero(),
crosslink_records: Vec::new(),
total_deposits: U256::zero(),
};
let e = rlp::encode(&a);
assert_eq!(e.len(), 44);
assert_eq!(e[0..4], [192; 4]);
assert_eq!(e[4], 10);
assert_eq!(e[5], 8);
assert_eq!(e[6], 2);
assert_eq!(e[7], 3);
assert_eq!(e[8], 12);
assert_eq!(e[9], 160);
assert_eq!(e[10..42], [0; 32]);
assert_eq!(e[42], 192);
assert_eq!(e[43], 128);
}
}

View File

@ -1,8 +1,10 @@
extern crate rlp; extern crate rlp;
extern crate ethereum_types;
use super::utils; use super::utils;
pub mod active_state; pub mod active_state;
pub mod crystallized_state;
pub mod aggregate_vote; pub mod aggregate_vote;
pub mod block; pub mod block;
pub mod crosslink_record; pub mod crosslink_record;