Add RLP encoding to part. xlink rec.

This commit is contained in:
Paul Hauner 2018-07-09 12:10:33 +10:00
parent 909d624730
commit 455aa3782a
2 changed files with 30 additions and 0 deletions

View File

@ -1,4 +1,5 @@
use super::utils::types::{ Sha256Digest, Bitfield }; use super::utils::types::{ Sha256Digest, Bitfield };
use super::rlp::{ RlpStream, Encodable };
pub struct PartialCrosslinkRecord { pub struct PartialCrosslinkRecord {
pub shard_id: u16, 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)] #[cfg(test)]
mod tests { mod tests {
use super::super::rlp;
use super::*; use super::*;
#[test] #[test]
@ -30,4 +43,20 @@ mod tests {
assert_eq!(p.shard_id, id); assert_eq!(p.shard_id, id);
assert_eq!(p.shard_block_hash, hash); 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);
}
} }

View File

@ -43,6 +43,7 @@ mod tests {
let balance_delta = 99; let balance_delta = 99;
let r = RecentPropserRecord::new(index, randao_commitment, balance_delta); let r = RecentPropserRecord::new(index, randao_commitment, balance_delta);
let e = rlp::encode(&r); let e = rlp::encode(&r);
assert_eq!(e.len(), 35);
assert_eq!(e[0], 1); assert_eq!(e[0], 1);
assert_eq!(e[1], 160); assert_eq!(e[1], 160);
assert_eq!(e[2..34], [0; 32]); assert_eq!(e[2..34], [0; 32]);