Update ssz

- Add u16 and u8.
 - Add comments
 - Consildate functions
This commit is contained in:
Paul Hauner 2018-09-10 07:50:35 +02:00
parent 3399b02393
commit 67bbc75c86
3 changed files with 108 additions and 69 deletions

View File

@ -4,3 +4,4 @@ version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"] authors = ["Paul Hauner <paul@paulhauner.com>"]
[dependencies] [dependencies]
ssz = { path = "../ssz" }

View File

@ -15,6 +15,7 @@ pub struct BooleanBitfield{
} }
impl BooleanBitfield { impl BooleanBitfield {
/// Create a new bitfield with a length of zero.
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
len: 0, len: 0,
@ -22,52 +23,63 @@ impl BooleanBitfield {
} }
} }
/// Create a new bitfield of a certain capacity
pub fn with_capacity(capacity: usize) -> Self { pub fn with_capacity(capacity: usize) -> Self {
Self { Self {
len: 0, len: 0,
vec: Vec::with_capacity(capacity) vec: Vec::with_capacity(capacity / 8 + 1)
} }
} }
// Output the bitfield as a big-endian vec of u8. /// Read the value of a bit.
pub fn to_be_vec(&self) -> Vec<u8> { ///
let mut o = self.vec.clone(); /// Will return `true` if the bit has been set to `true`
o.reverse(); /// without then being set to `False`.
o
}
pub fn get_bit(&self, i: &usize) -> bool { pub fn get_bit(&self, i: &usize) -> bool {
self.get_bit_on_byte(*i % 8, *i / 8) let bit = |i: &usize| *i % 8;
} let byte = |i: &usize| *i / 8;
fn get_bit_on_byte(&self, bit: usize, byte: usize) -> bool { if byte(i) >= self.vec.len() {
assert!(bit < 8);
if byte >= self.vec.len() {
false false
} else { } else {
self.vec[byte] & (1 << (bit as u8)) != 0 self.vec[byte(i)] & (1 << (bit(i) as u8)) != 0
} }
} }
pub fn set_bit(&mut self, bit: &usize, to: &bool) { /// Set the value of a bit.
self.len = max(self.len, *bit + 1); ///
self.set_bit_on_byte(*bit % 8, *bit / 8, to); /// If this bit is larger than the length of the underlying byte
} /// array it will be extended.
pub fn set_bit(&mut self, i: &usize, to: &bool) {
let bit = |i: &usize| *i % 8;
let byte = |i: &usize| *i / 8;
fn set_bit_on_byte(&mut self, bit: usize, byte: usize, val: &bool) { self.len = max(self.len, i + 1);
assert!(bit < 8);
if byte >= self.vec.len() { if byte(i) >= self.vec.len() {
self.vec.resize(byte + 1, 0); self.vec.resize(byte(i) + 1, 0);
}
match to {
true => {
self.vec[byte(i)] =
self.vec[byte(i)] | (1 << (bit(i) as u8))
}
false => {
self.vec[byte(i)] =
self.vec[byte(i)] & !(1 << (bit(i) as u8))
} }
match val {
true => self.vec[byte] = self.vec[byte] | (1 << (bit as u8)),
false => self.vec[byte] = self.vec[byte] & !(1 << (bit as u8))
} }
} }
/// Return the "length" of this bitfield. Length is defined as
/// the highest bit that has been set.
///
/// Note: this is distinct from the length of the underlying
/// vector.
pub fn len(&self) -> usize { self.len } pub fn len(&self) -> usize { self.len }
// Return the total number of bits set to true. /// Iterate through the underlying vector and count the number of
/// true bits.
pub fn num_true_bits(&self) -> u64 { pub fn num_true_bits(&self) -> u64 {
let mut count: u64 = 0; let mut count: u64 = 0;
for byte in &self.vec { for byte in &self.vec {
@ -79,6 +91,13 @@ impl BooleanBitfield {
} }
count count
} }
/// Clone and return the underlying byte array (`Vec<u8>`).
pub fn to_vec(&self) -> Vec<u8> {
let mut o = self.vec.clone();
o.reverse();
o
}
} }
impl PartialEq for BooleanBitfield { impl PartialEq for BooleanBitfield {
@ -101,49 +120,49 @@ impl Clone for BooleanBitfield {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use super::rlp;
#[test] #[test]
fn test_bitfield_set() { fn test_bitfield_set() {
let mut b = BooleanBitfield::new(); let mut b = BooleanBitfield::new();
b.set_bit(&0, &false); b.set_bit(&0, &false);
assert_eq!(b.to_be_vec(), [0]); assert_eq!(b.to_vec(), [0]);
b = BooleanBitfield::new(); b = BooleanBitfield::new();
b.set_bit(&7, &true); b.set_bit(&7, &true);
assert_eq!(b.to_be_vec(), [128]); assert_eq!(b.to_vec(), [128]);
b.set_bit(&7, &false); b.set_bit(&7, &false);
assert_eq!(b.to_be_vec(), [0]); assert_eq!(b.to_vec(), [0]);
assert_eq!(b.len(), 8); assert_eq!(b.len(), 8);
b = BooleanBitfield::new(); b = BooleanBitfield::new();
b.set_bit(&7, &true); b.set_bit(&7, &true);
b.set_bit(&0, &true); b.set_bit(&0, &true);
assert_eq!(b.to_be_vec(), [129]); assert_eq!(b.to_vec(), [129]);
b.set_bit(&7, &false); b.set_bit(&7, &false);
assert_eq!(b.to_be_vec(), [1]); assert_eq!(b.to_vec(), [1]);
assert_eq!(b.len(), 8); assert_eq!(b.len(), 8);
b = BooleanBitfield::new(); b = BooleanBitfield::new();
b.set_bit(&8, &true); b.set_bit(&8, &true);
assert_eq!(b.to_be_vec(), [1, 0]); assert_eq!(b.to_vec(), [1, 0]);
assert_eq!(b.len(), 9);
b.set_bit(&8, &false); b.set_bit(&8, &false);
assert_eq!(b.to_be_vec(), [0, 0]); assert_eq!(b.to_vec(), [0, 0]);
assert_eq!(b.len(), 9); assert_eq!(b.len(), 9);
b = BooleanBitfield::new(); b = BooleanBitfield::new();
b.set_bit(&15, &true); b.set_bit(&15, &true);
assert_eq!(b.to_be_vec(), [128, 0]); assert_eq!(b.to_vec(), [128, 0]);
b.set_bit(&15, &false); b.set_bit(&15, &false);
assert_eq!(b.to_be_vec(), [0, 0]); assert_eq!(b.to_vec(), [0, 0]);
assert_eq!(b.len(), 16); assert_eq!(b.len(), 16);
b = BooleanBitfield::new(); b = BooleanBitfield::new();
b.set_bit(&8, &true); b.set_bit(&8, &true);
b.set_bit(&15, &true); b.set_bit(&15, &true);
assert_eq!(b.to_be_vec(), [129, 0]); assert_eq!(b.to_vec(), [129, 0]);
b.set_bit(&15, &false); b.set_bit(&15, &false);
assert_eq!(b.to_be_vec(), [1, 0]); assert_eq!(b.to_vec(), [1, 0]);
assert_eq!(b.len(), 16); assert_eq!(b.len(), 16);
} }

View File

@ -1,8 +1,8 @@
/* /*
* This is a WIP of implementing an alternative * This is a WIP of implementing an alternative
* serialization strategy. It attempts to follow Vitalik's * serialization strategy. It attempts to follow Vitalik's
* "ssz" format here: * "simpleserialize" format here:
* https://github.com/ethereum/research/tree/master/py_ssz * https://github.com/ethereum/beacon_chain/blob/master/beacon_chain/utils/simpleserialize.py
* *
* This implementation is not final and would almost certainly * This implementation is not final and would almost certainly
* have issues. * have issues.
@ -24,12 +24,14 @@ pub struct SszStream {
} }
impl SszStream { impl SszStream {
/// Create a new, empty steam for writing ssz values.
pub fn new() -> Self { pub fn new() -> Self {
SszStream { SszStream {
buffer: Vec::new() buffer: Vec::new()
} }
} }
/// Append some ssz encodable value to the stream.
pub fn append<E>(&mut self, value: &E) -> &mut Self pub fn append<E>(&mut self, value: &E) -> &mut Self
where E: Encodable where E: Encodable
{ {
@ -37,30 +39,34 @@ impl SszStream {
self self
} }
fn append_encoded_vec(&mut self, v: &mut Vec<u8>) { pub fn extend_buffer(&mut self, vec: &mut Vec<u8>) {
self.buffer.append(&mut encode_length(v.len(), LENGTH_BYTES)); self.buffer.append(&mut encode_length(vec.len(), LENGTH_BYTES));
self.buffer.append(v) ; self.buffer.append(vec);
} }
fn append_encoded_array(&mut self, a: &mut [u8]) { /// Append some vector (list) of encoded values to the stream.
pub fn append_vec<E>(&mut self, vec: &mut Vec<E>)
where E: Encodable
{
self.buffer.append(&mut encode_length(vec.len(), LENGTH_BYTES));
for v in vec {
v.ssz_append(self);
}
}
/// Append some array (list) of encoded values to the stream.
pub fn append_encoded_array(&mut self, a: &mut [u8]) {
let len = a.len(); let len = a.len();
self.buffer.append(&mut encode_length(len, LENGTH_BYTES)); self.buffer.append(&mut encode_length(len, LENGTH_BYTES));
self.buffer.extend_from_slice(&a[0..len]); self.buffer.extend_from_slice(&a[0..len]);
} }
/// Consume the stream and return the underlying bytes.
pub fn drain(self) -> Vec<u8> { pub fn drain(self) -> Vec<u8> {
self.buffer self.buffer
} }
} }
pub fn encode<E>(value: &E) -> Vec<u8>
where E: Encodable
{
let mut stream = SszStream::new();
stream.append(value);
stream.drain()
}
fn encode_length(len: usize, length_bytes: usize) -> Vec<u8> { fn encode_length(len: usize, length_bytes: usize) -> Vec<u8> {
assert!(length_bytes > 0); // For sanity assert!(length_bytes > 0); // For sanity
assert!((len as usize) < 2usize.pow(length_bytes as u32 * 8)); assert!((len as usize) < 2usize.pow(length_bytes as u32 * 8));
@ -75,12 +81,25 @@ fn encode_length(len: usize, length_bytes: usize) -> Vec<u8> {
/* /*
* Implementations for various types * Implementations for various types
*/ */
impl Encodable for u8 {
fn ssz_append(&self, s: &mut SszStream) {
s.buffer.append(&mut vec![*self]);
}
}
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());
}
}
impl Encodable for u32 { impl Encodable for u32 {
fn ssz_append(&self, s: &mut SszStream) { fn ssz_append(&self, s: &mut SszStream) {
let mut buf = BytesMut::with_capacity(32/8); let mut buf = BytesMut::with_capacity(32/8);
buf.put_u32_be(*self); buf.put_u32_be(*self);
s.append_encoded_vec(&mut buf.to_vec()); s.extend_buffer(&mut buf.to_vec());
} }
} }
@ -88,13 +107,13 @@ impl Encodable for u64 {
fn ssz_append(&self, s: &mut SszStream) { fn ssz_append(&self, s: &mut SszStream) {
let mut buf = BytesMut::with_capacity(64/8); let mut buf = BytesMut::with_capacity(64/8);
buf.put_u64_be(*self); buf.put_u64_be(*self);
s.append_encoded_vec(&mut buf.to_vec()); s.extend_buffer(&mut buf.to_vec());
} }
} }
impl Encodable for H256 { impl Encodable for H256 {
fn ssz_append(&self, s: &mut SszStream) { fn ssz_append(&self, s: &mut SszStream) {
s.append_encoded_vec(&mut self.to_vec()); s.extend_buffer(&mut self.to_vec());
} }
} }