From fd351d714546ef2ee3d0533dca20f77683faf7d4 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 9 Jul 2018 16:25:45 +1000 Subject: [PATCH] Add RLP encoding to CrystallizedState --- src/state/crystallized_state.rs | 57 +++++++++++++++++++++++++++++++++ src/state/mod.rs | 2 ++ 2 files changed, 59 insertions(+) diff --git a/src/state/crystallized_state.rs b/src/state/crystallized_state.rs index 4bc3e5c86..d0dcc9eb6 100644 --- a/src/state/crystallized_state.rs +++ b/src/state/crystallized_state.rs @@ -1,6 +1,7 @@ use super::utils::types::Sha256Digest; use super::validator_record::ValidatorRecord; use super::crosslink_record::CrosslinkRecord; +use super::rlp::{ RlpStream, Encodable }; use super::ethereum_types::U256; 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); + } +} diff --git a/src/state/mod.rs b/src/state/mod.rs index f8c3aafdd..697730afc 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -1,8 +1,10 @@ extern crate rlp; +extern crate ethereum_types; use super::utils; pub mod active_state; +pub mod crystallized_state; pub mod aggregate_vote; pub mod block; pub mod crosslink_record;