mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-27 01:33:33 +00:00
Update SszEncoder
This commit is contained in:
@@ -32,10 +32,14 @@ pub struct SszEncoder {
|
||||
}
|
||||
|
||||
impl SszEncoder {
|
||||
pub fn list(num_fixed_bytes: usize) -> Self {
|
||||
Self::container(num_fixed_bytes)
|
||||
}
|
||||
|
||||
pub fn container(num_fixed_bytes: usize) -> Self {
|
||||
Self {
|
||||
offset: num_fixed_bytes,
|
||||
fixed_bytes: vec![],
|
||||
fixed_bytes: Vec::with_capacity(num_fixed_bytes),
|
||||
variable_bytes: vec![],
|
||||
}
|
||||
}
|
||||
@@ -51,10 +55,9 @@ impl SszEncoder {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn drain(mut self) -> Vec<u8> {
|
||||
self.fixed_bytes.append(&mut self.variable_bytes);
|
||||
|
||||
self.fixed_bytes
|
||||
pub fn drain_onto(mut self, buf: &mut Vec<u8>) {
|
||||
buf.append(&mut self.fixed_bytes);
|
||||
buf.append(&mut self.variable_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +66,7 @@ pub struct VariableLengths {
|
||||
pub variable_bytes_length: usize,
|
||||
}
|
||||
|
||||
/*
|
||||
/// Provides a buffer for appending SSZ values.
|
||||
#[derive(Default)]
|
||||
pub struct SszStream {
|
||||
@@ -152,6 +156,7 @@ impl SszStream {
|
||||
self.fixed_bytes
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/// Encode `len` as a little-endian byte vec of `BYTES_PER_LENGTH_OFFSET` length.
|
||||
///
|
||||
|
||||
@@ -31,7 +31,6 @@ impl<T: Encodable> Encodable for Vec<T> {
|
||||
}
|
||||
|
||||
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||
|
||||
if T::is_ssz_fixed_len() {
|
||||
buf.reserve(T::ssz_fixed_len() * self.len());
|
||||
|
||||
@@ -39,28 +38,13 @@ impl<T: Encodable> Encodable for Vec<T> {
|
||||
item.ssz_append(buf);
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
for item in self {
|
||||
let mut substream = SszStream::new();
|
||||
|
||||
item.ssz_append(&mut substream);
|
||||
|
||||
s.append_variable_bytes(&substream.drain());
|
||||
}
|
||||
*/
|
||||
let mut offset = self.len() * BYTES_PER_LENGTH_OFFSET;
|
||||
let mut fixed = Vec::with_capacity(offset);
|
||||
let mut variable = vec![];
|
||||
let mut encoder = SszEncoder::list(self.len() * BYTES_PER_LENGTH_OFFSET);
|
||||
|
||||
for item in self {
|
||||
fixed.append(&mut encode_length(offset));
|
||||
let mut bytes = item.as_ssz_bytes();
|
||||
offset += bytes.len();
|
||||
variable.append(&mut bytes);
|
||||
encoder.append(item);
|
||||
}
|
||||
|
||||
buf.append(&mut fixed);
|
||||
buf.append(&mut variable);
|
||||
encoder.drain_onto(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,21 +17,19 @@ mod decode;
|
||||
mod encode;
|
||||
|
||||
pub use decode::{Decodable, DecodeError, SszDecoderBuilder};
|
||||
pub use encode::{Encodable, SszEncoder, SszStream};
|
||||
pub use encode::{Encodable, SszEncoder};
|
||||
|
||||
pub const BYTES_PER_LENGTH_OFFSET: usize = 4;
|
||||
pub const MAX_LENGTH_VALUE: usize = 1 << (BYTES_PER_LENGTH_OFFSET * 8) - 1;
|
||||
|
||||
/// Convenience function to SSZ encode an object supporting ssz::Encode.
|
||||
///
|
||||
/// Equivalent to `val.as_ssz_bytes()`.
|
||||
pub fn ssz_encode<T>(val: &T) -> Vec<u8>
|
||||
where
|
||||
T: Encodable,
|
||||
{
|
||||
let mut buf = vec![];
|
||||
|
||||
val.ssz_append(&mut buf);
|
||||
|
||||
buf
|
||||
val.as_ssz_bytes()
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user