Skip to content

Commit

Permalink
add ConstantPlaceholder serialization test;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Aug 7, 2020
1 parent b886dab commit 7f3272f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 27 deletions.
1 change: 1 addition & 0 deletions sigma-tree/src/ast/constant.rs
Expand Up @@ -261,6 +261,7 @@ impl Into<Constant> for Vec<i8> {
}

/// Placeholder for a constant in ErgoTree.
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ConstantPlaceholder {
/// Zero based index in ErgoTree.constants array.
pub id: u32,
Expand Down
3 changes: 2 additions & 1 deletion sigma-tree/src/ergo_tree.rs
Expand Up @@ -10,6 +10,7 @@ use crate::{
};
use io::{Cursor, Read};

use crate::serialization::constant_store::ConstantStore;
use sigma_ser::{peekable_reader::PeekableReader, vlq_encode};
use std::io;
use std::rc::Rc;
Expand Down Expand Up @@ -161,7 +162,7 @@ impl SigmaSerializable for ErgoTree {

fn sigma_parse_bytes(mut bytes: Vec<u8>) -> Result<Self, SerializationError> {
let cursor = Cursor::new(&mut bytes[..]);
let mut r = SigmaByteReader::new(PeekableReader::new(cursor));
let mut r = SigmaByteReader::new(PeekableReader::new(cursor), ConstantStore::empty());
let header = ErgoTreeHeader::sigma_parse(&mut r)?;
if header.is_constant_segregation() {
let constants_len = r.get_u32()?;
Expand Down
2 changes: 1 addition & 1 deletion sigma-tree/src/serialization.rs
Expand Up @@ -2,13 +2,13 @@

mod constant;
mod constant_placeholder;
mod constant_store;
mod data;
mod expr;
mod fold;
mod serializable;
mod sigmaboolean;

pub mod constant_store;
pub mod ergo_box;
pub mod op_code;
pub mod sigma_byte_reader;
Expand Down
47 changes: 33 additions & 14 deletions sigma-tree/src/serialization/constant_placeholder.rs
Expand Up @@ -24,17 +24,36 @@ impl SigmaSerializable for ConstantPlaceholder {
}
}

// #[cfg(test)]
// mod tests {
// use super::*;
// use crate::serialization::sigma_serialize_roundtrip;
// use proptest::prelude::*;

// proptest! {

// #[test]
// fn ser_roundtrip(v in any::<ConstantPlaceholder>()) {
// prop_assert_eq![sigma_serialize_roundtrip(&v), v];
// }
// }
// }
#[cfg(test)]
mod tests {
use super::*;
use crate::{
ast::Constant,
serialization::{
constant_store::ConstantStore, sigma_byte_reader::SigmaByteReader,
sigma_byte_writer::SigmaByteWriter,
},
};
use io::Cursor;
use proptest::prelude::*;
use sigma_ser::peekable_reader::PeekableReader;

proptest! {

#[test]
fn ser_roundtrip(c in any::<Constant>()) {
let mut data = Vec::new();
let mut cs = ConstantStore::empty();
let ph = cs.put(c);
let mut w = SigmaByteWriter::new(&mut data, Some(&mut cs));
ph.sigma_serialize(&mut w).expect("serialization failed");

let cursor = Cursor::new(&mut data[..]);
let pr = PeekableReader::new(cursor);
let mut sr = SigmaByteReader::new(pr, cs);
let ph_parsed = ConstantPlaceholder::sigma_parse(&mut sr).expect("parse failed");
prop_assert_eq![ph, ph_parsed];
}
}
}

6 changes: 4 additions & 2 deletions sigma-tree/src/serialization/constant_store.rs
Expand Up @@ -8,11 +8,14 @@ pub struct ConstantStore {
}

impl ConstantStore {
/// empty store(no constants)
pub fn empty() -> Self {
ConstantStore { constants: vec![] }
}

pub fn new(constants: Vec<Constant>) -> Self {
ConstantStore { constants }
}

pub fn get(&self, index: u32) -> Option<&Constant> {
self.constants.get(index as usize)
}
Expand All @@ -30,4 +33,3 @@ impl ConstantStore {
self.constants.clone()
}
}

9 changes: 5 additions & 4 deletions sigma-tree/src/serialization/serializable.rs
@@ -1,4 +1,5 @@
use super::{
constant_store::ConstantStore,
sigma_byte_reader::{SigmaByteRead, SigmaByteReader},
sigma_byte_writer::{SigmaByteWrite, SigmaByteWriter},
};
Expand Down Expand Up @@ -67,7 +68,7 @@ pub trait SigmaSerializable: Sized {
/// Serialize any SigmaSerializable value into bytes
fn sigma_serialise_bytes(&self) -> Vec<u8> {
let mut data = Vec::new();
let mut w = SigmaByteWriter::new(&mut data);
let mut w = SigmaByteWriter::new(&mut data, None);
self.sigma_serialize(&mut w)
// since serialization may fail only for underlying IO errors it's ok to force unwrap
.expect("serialization failed");
Expand All @@ -78,7 +79,7 @@ pub trait SigmaSerializable: Sized {
fn sigma_parse_bytes(mut bytes: Vec<u8>) -> Result<Self, SerializationError> {
let cursor = Cursor::new(&mut bytes[..]);
let pr = PeekableReader::new(cursor);
let mut sr = SigmaByteReader::new(pr);
let mut sr = SigmaByteReader::new(pr, ConstantStore::empty());
Self::sigma_parse(&mut sr)
}
}
Expand All @@ -87,10 +88,10 @@ pub trait SigmaSerializable: Sized {
#[cfg(test)]
pub fn sigma_serialize_roundtrip<T: SigmaSerializable>(v: &T) -> T {
let mut data = Vec::new();
let mut w = SigmaByteWriter::new(&mut data);
let mut w = SigmaByteWriter::new(&mut data, None);
v.sigma_serialize(&mut w).expect("serialization failed");
let cursor = Cursor::new(&mut data[..]);
let pr = PeekableReader::new(cursor);
let mut sr = SigmaByteReader::new(pr);
let mut sr = SigmaByteReader::new(pr, ConstantStore::empty());
T::sigma_parse(&mut sr).expect("parse failed")
}
4 changes: 2 additions & 2 deletions sigma-tree/src/serialization/sigma_byte_reader.rs
Expand Up @@ -9,10 +9,10 @@ pub struct SigmaByteReader<R> {

impl<R: Peekable> SigmaByteReader<R> {
/// Create new reader from PeekableReader
pub fn new(pr: R) -> SigmaByteReader<R> {
pub fn new(pr: R, constant_store: ConstantStore) -> SigmaByteReader<R> {
SigmaByteReader {
inner: pr,
constant_store: ConstantStore::empty(),
constant_store,
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions sigma-tree/src/serialization/sigma_byte_writer.rs
Expand Up @@ -4,14 +4,17 @@ use std::io::Write;

pub struct SigmaByteWriter<'a, W> {
inner: &'a mut W,
constant_store: Option<ConstantStore>,
constant_store: Option<&'a mut ConstantStore>,
}

impl<'a, W: Write> SigmaByteWriter<'a, W> {
pub fn new(w: &mut W) -> SigmaByteWriter<W> {
pub fn new(
w: &'a mut W,
constant_store: Option<&'a mut ConstantStore>,
) -> SigmaByteWriter<'a, W> {
SigmaByteWriter {
inner: w,
constant_store: None,
constant_store,
}
}
}
Expand Down

0 comments on commit 7f3272f

Please sign in to comment.