Change ssz to use extend_from_slice()

This is instead of `append` which empties the source vector. This
doens't really ssem suitable for what we're doing.
This commit is contained in:
Paul Hauner 2018-09-10 08:52:21 +02:00
parent e12a93783d
commit ffaffbcd90
3 changed files with 17 additions and 13 deletions

View File

@ -18,7 +18,7 @@ impl Encodable for AttestationRecord {
s.append(&self.shard_id);
s.append_vec(&self.oblique_parent_hashes);
s.append(&self.shard_block_hash);
s.append(&self.attester_bitfield);
s.append_vec(&self.attester_bitfield.to_be_vec());
// TODO: add aggregate signature
}
}

View File

@ -47,11 +47,10 @@ impl Block {
impl Encodable for Block {
fn ssz_append(&self, s: &mut SszStream) {
let mut s = SszStream::new();
s.append(&self.parent_hash);
s.append(&self.slot_number);
s.append(&self.randao_reveal);
s.append(&self.attestations);
s.append_vec(&self.attestations);
s.append(&self.pow_chain_ref);
s.append(&self.active_state_root);
s.append(&self.crystallized_state_root);

View File

@ -39,16 +39,18 @@ impl SszStream {
self
}
pub fn extend_buffer(&mut self, vec: &mut Vec<u8>) {
self.buffer.append(&mut encode_length(vec.len(), LENGTH_BYTES));
self.buffer.append(vec);
pub fn extend_buffer(&mut self, vec: &Vec<u8>) {
self.buffer.extend_from_slice(
&encode_length(vec.len(),
LENGTH_BYTES));
self.buffer.extend_from_slice(&vec);
}
/// Append some vector (list) of encoded values to the stream.
pub fn append_vec<E>(&mut self, vec: &mut Vec<E>)
pub fn append_vec<E>(&mut self, vec: &Vec<E>)
where E: Encodable
{
self.buffer.append(&mut encode_length(vec.len(), LENGTH_BYTES));
self.buffer.extend_from_slice(&encode_length(vec.len(), LENGTH_BYTES));
for v in vec {
v.ssz_append(self);
}
@ -91,7 +93,7 @@ impl Encodable for u16 {
fn ssz_append(&self, s: &mut SszStream) {
let mut buf = BytesMut::with_capacity(16/8);
buf.put_u16_be(*self);
s.extend_buffer(&mut buf.to_vec());
s.extend_buffer(&buf.to_vec());
}
}
@ -99,7 +101,7 @@ impl Encodable for u32 {
fn ssz_append(&self, s: &mut SszStream) {
let mut buf = BytesMut::with_capacity(32/8);
buf.put_u32_be(*self);
s.extend_buffer(&mut buf.to_vec());
s.extend_buffer(&buf.to_vec());
}
}
@ -107,13 +109,13 @@ impl Encodable for u64 {
fn ssz_append(&self, s: &mut SszStream) {
let mut buf = BytesMut::with_capacity(64/8);
buf.put_u64_be(*self);
s.extend_buffer(&mut buf.to_vec());
s.extend_buffer(&buf.to_vec());
}
}
impl Encodable for H256 {
fn ssz_append(&self, s: &mut SszStream) {
s.extend_buffer(&mut self.to_vec());
s.extend_buffer(&self.to_vec());
}
}
@ -188,7 +190,10 @@ mod tests {
three: 100
};
let e = encode(&t);
let mut s = SszStream::new();
s.append(&t);
let e = s.drain();
assert_eq!(e[0..4], [0, 0, 0, 4]);
assert_eq!(e[4..8], [0, 0, 0, 1]);
assert_eq!(e[8..12], [0, 0, 0, 32]);