From 455aa3782a40d565f08748296425b43371d423dc Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 9 Jul 2018 12:10:33 +1000 Subject: [PATCH] Add RLP encoding to part. xlink rec. --- src/state/partial_crosslink_record.rs | 29 +++++++++++++++++++++++++++ src/state/recent_proposer_record.rs | 1 + 2 files changed, 30 insertions(+) diff --git a/src/state/partial_crosslink_record.rs b/src/state/partial_crosslink_record.rs index 966ca6a64..a097ddcc7 100644 --- a/src/state/partial_crosslink_record.rs +++ b/src/state/partial_crosslink_record.rs @@ -1,4 +1,5 @@ use super::utils::types::{ Sha256Digest, Bitfield }; +use super::rlp::{ RlpStream, Encodable }; pub struct PartialCrosslinkRecord { pub shard_id: u16, @@ -17,9 +18,21 @@ impl PartialCrosslinkRecord { } } +/* + * RLP Encoding + */ +impl Encodable for PartialCrosslinkRecord { + fn rlp_append(&self, s: &mut RlpStream) { + s.append(&self.shard_id); + s.append(&self.shard_block_hash); + s.append(&self.voter_bitfield); + } +} + #[cfg(test)] mod tests { + use super::super::rlp; use super::*; #[test] @@ -30,4 +43,20 @@ mod tests { assert_eq!(p.shard_id, id); assert_eq!(p.shard_block_hash, hash); } + + #[test] + fn test_serialization() { + let p = PartialCrosslinkRecord { + shard_id: 1, + shard_block_hash: Sha256Digest::zero(), + voter_bitfield: Vec::new() + }; + let e = rlp::encode(&p); + println!("{:?}", e); + assert_eq!(e.len(), 35); + assert_eq!(e[0], 1); + assert_eq!(e[1], 160); + assert_eq!(e[2..34], [0; 32]); + assert_eq!(e[34], 128); + } } diff --git a/src/state/recent_proposer_record.rs b/src/state/recent_proposer_record.rs index 9e713a96a..d08369586 100644 --- a/src/state/recent_proposer_record.rs +++ b/src/state/recent_proposer_record.rs @@ -43,6 +43,7 @@ mod tests { let balance_delta = 99; let r = RecentPropserRecord::new(index, randao_commitment, balance_delta); let e = rlp::encode(&r); + assert_eq!(e.len(), 35); assert_eq!(e[0], 1); assert_eq!(e[1], 160); assert_eq!(e[2..34], [0; 32]);