Skip to content

Commit

Permalink
Merge pull request #19 from ergoplatform/i10-ergotree-for-p2pk
Browse files Browse the repository at this point in the history
Draft ErgoTree and needed IR nodes for P2PK script
  • Loading branch information
greenhat committed May 18, 2020
2 parents f98f8e7 + 5afa198 commit 18bf25f
Show file tree
Hide file tree
Showing 36 changed files with 848 additions and 78 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ members = [
"sigma-chain",
"sigma-chain-c",
"sigma-ser",
"sigma-tree",
"sigma-testutil",
]
3 changes: 3 additions & 0 deletions sigma-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]
# testing = ["sigma-tree/testing"]

[dependencies]
wasm-bindgen = "0.2"
sigma-ser = { path = "../sigma-ser" }
sigma-tree = { path = "../sigma-tree" }
indexmap = "1.3.2"
base16 = "0.2.1"

Expand All @@ -25,6 +27,7 @@ console_error_panic_hook = { version = "0.1.6", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.10"
proptest-derive = "0.1.2"
sigma-testutil = { path = "../sigma-testutil" }

[dev-dependencies.proptest]
# wasm support, via https://altsysrq.github.io/proptest-book/proptest/wasm.html
Expand Down
2 changes: 1 addition & 1 deletion sigma-chain/src/box_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl SigmaSerializable for BoxId {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;
use proptest::prelude::*;
use sigma_ser::test_helpers::*;

proptest! {

Expand Down
2 changes: 1 addition & 1 deletion sigma-chain/src/data_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl SigmaSerializable for DataInput {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;
use sigma_ser::test_helpers::*;

proptest! {

Expand Down
54 changes: 26 additions & 28 deletions sigma-chain/src/ergo_box.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Ergo box
use crate::ergo_tree::ErgoTree;
use crate::{token::TokenAmount, token::TokenId};
use indexmap::IndexSet;
use sigma_ser::serializer::SerializationError;
use sigma_ser::serializer::SigmaSerializable;
use sigma_ser::vlq_encode;
use sigma_tree::ErgoTree;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::io;
Expand Down Expand Up @@ -159,36 +159,34 @@ pub fn parse_body_with_indexed_digests<R: vlq_encode::ReadSigmaVlqExt>(
})
}

#[cfg(test)]
use proptest::{arbitrary::Arbitrary, collection::vec, prelude::*};

#[cfg(test)]
impl Arbitrary for ErgoBoxCandidate {
type Parameters = ();

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(
any::<u64>(),
any::<ErgoTree>(),
vec(any::<TokenAmount>(), 0..10),
any::<u32>(),
)
.prop_map(|(value, ergo_tree, tokens, creation_height)| Self {
value,
ergo_tree,
tokens,
additional_registers: HashMap::new(),
creation_height,
})
.boxed()
}
type Strategy = BoxedStrategy<Self>;
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;
use proptest::{arbitrary::Arbitrary, collection::vec, prelude::*};
use sigma_ser::test_helpers::*;
use sigma_testutil::generator::*;

impl Arbitrary for ErgoBoxCandidate {
type Parameters = ();

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(
any::<u64>(),
any::<ErgoTreeArb>(),
vec(any::<TokenAmount>(), 0..10),
any::<u32>(),
)
.prop_map(|(value, ergo_tree, tokens, creation_height)| Self {
value,
ergo_tree: ergo_tree.0,
tokens,
additional_registers: HashMap::new(),
creation_height,
})
.boxed()
}
type Strategy = BoxedStrategy<Self>;
}

proptest! {

Expand Down
2 changes: 1 addition & 1 deletion sigma-chain/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl SigmaSerializable for Input {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;
use sigma_ser::test_helpers::sigma_serialize_roundtrip;

proptest! {

Expand Down
4 changes: 0 additions & 4 deletions sigma-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@
mod misc;
mod utils;

#[cfg(test)]
mod test_helpers;

pub use misc::*;

pub mod box_id;
pub mod constants;
pub mod context_extension;
pub mod data_input;
pub mod ergo_box;
pub mod ergo_tree;
pub mod input;
pub mod prover_result;
pub mod token;
Expand Down
2 changes: 1 addition & 1 deletion sigma-chain/src/prover_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Arbitrary for ProverResult {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;
use sigma_ser::test_helpers::*;

proptest! {

Expand Down
8 changes: 0 additions & 8 deletions sigma-chain/src/test_helpers.rs

This file was deleted.

2 changes: 1 addition & 1 deletion sigma-chain/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ impl SigmaSerializable for TokenId {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;
use proptest::prelude::*;
use sigma_ser::test_helpers::*;

proptest! {

Expand Down
47 changes: 23 additions & 24 deletions sigma-chain/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,33 +110,32 @@ impl SigmaSerializable for Transaction {
}
}

#[cfg(test)]
use proptest::{arbitrary::Arbitrary, collection::vec, prelude::*};

#[cfg(test)]
impl Arbitrary for Transaction {
type Parameters = ();

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(
vec(any::<Input>(), 1..10),
vec(any::<DataInput>(), 0..10),
vec(any::<ErgoBoxCandidate>(), 1..10),
)
.prop_map(|(inputs, data_inputs, outputs)| Self {
inputs,
data_inputs,
outputs,
})
.boxed()
}
type Strategy = BoxedStrategy<Self>;
}

#[cfg(test)]
mod tests {

use super::*;
use crate::test_helpers::*;
use sigma_ser::test_helpers::*;

use proptest::{arbitrary::Arbitrary, collection::vec, prelude::*};

impl Arbitrary for Transaction {
type Parameters = ();

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(
vec(any::<Input>(), 1..10),
vec(any::<DataInput>(), 0..10),
vec(any::<ErgoBoxCandidate>(), 1..10),
)
.prop_map(|(inputs, data_inputs, outputs)| Self {
inputs,
data_inputs,
outputs,
})
.boxed()
}
type Strategy = BoxedStrategy<Self>;
}

proptest! {

Expand Down
3 changes: 3 additions & 0 deletions sigma-ser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ pub mod serializer;
pub mod vlq_encode;
/// ZigZag encoder
pub mod zig_zag_encode;

// #[cfg(test)]
pub mod test_helpers;
9 changes: 9 additions & 0 deletions sigma-ser/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ use thiserror::Error;
/// Ways serialization might fail
#[derive(Error, Debug)]
pub enum SerializationError {
/// Failed to parse op
#[error("op parsing error")]
InvalidOpCode,
/// Lacking support for the op
#[error("not implemented op error")]
NotImplementedOpCode(u8),
/// Failed to parse type
#[error("type parsing error")]
InvalidTypePrefix,
/// Failed to decode VLQ
#[error("vlq encode error")]
VlqEncode(vlq_encode::VlqEncodingError),
Expand Down
7 changes: 7 additions & 0 deletions sigma-ser/src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Helper function for testing purposes
/// serialization roundtrip
pub fn sigma_serialize_roundtrip<T: crate::serializer::SigmaSerializable>(v: &T) -> T {
let mut data = Vec::new();
v.sigma_serialize(&mut data).expect("serialization failed");
T::sigma_parse(&data[..]).expect("parse failed")
}
20 changes: 20 additions & 0 deletions sigma-ser/src/vlq_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ impl<W: io::Write + ?Sized> WriteSigmaVlqExt for W {}
/// for VLQ see [[https://en.wikipedia.org/wiki/Variable-length_quantity]]
/// for ZigZag see https://developers.google.com/protocol-buffers/docs/encoding#types
pub trait ReadSigmaVlqExt: io::Read {
/// Peek a u8 without advancing the position
fn peek_u8(&mut self) -> Result<u8, io::Error> {
// https://github.com/ergoplatform/sigma-rust/issues/20
todo!("implement and enable test")
// does not work (consumes the reader)
// let mut slice = [0u8; 1];
// self.take(1).read(&mut slice)?;
// Ok(slice[0])
}

/// Read i8 without decoding
fn get_i8(&mut self) -> Result<i8, io::Error> {
Self::get_u8(self).map(|v| v as i8)
Expand Down Expand Up @@ -189,6 +199,16 @@ mod tests {
assert_eq!(r.get_u8().unwrap(), 255);
}

#[ignore]
#[test]
fn test_peek_u8() {
let mut r = Cursor::new(vec![0, 1]);
assert_eq!(r.peek_u8().unwrap(), 0);
assert_eq!(r.get_u8().unwrap(), 0);
assert_eq!(r.peek_u8().unwrap(), 1);
assert_eq!(r.get_u8().unwrap(), 1);
}

// from https://github.com/ScorexFoundation/scorex-util/blob/3dc334f68ebefbfab6d33b57f2373e80245ab34d/src/test/scala/scorex/util/serialization/VLQReaderWriterSpecification.scala#L32-L32
// original source: http://github.com/google/protobuf/blob/a7252bf42df8f0841cf3a0c85fdbf1a5172adecb/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java#L239
#[allow(clippy::identity_op)]
Expand Down
28 changes: 28 additions & 0 deletions sigma-testutil/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "sigma-testutil"
version = "0.1.0"
authors = ["Denys Zadorozhnyi <denys@zadorozhnyi.com>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
sigma-tree = { path = "../sigma-tree" }

[dependencies.proptest]
# wasm support, via https://altsysrq.github.io/proptest-book/proptest/wasm.html
version = "0.9"
# The default feature set includes things like process forking which are not
# supported in Web Assembly.
default-features = false
# Enable using the `std` crate.
features = ["std"]

[dev-dependencies]
wasm-bindgen-test = "0.3.10"
proptest-derive = "0.1.2"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
16 changes: 16 additions & 0 deletions sigma-testutil/src/generator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use proptest::{arbitrary::Arbitrary, prelude::*};
use sigma_tree::ErgoTree;

#[derive(Debug)]
pub struct ErgoTreeArb(pub ErgoTree);

impl Arbitrary for ErgoTreeArb {
type Parameters = ();

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(any::<u32>(),)
.prop_map(|_| Self { 0: ErgoTree {} })
.boxed()
}
type Strategy = BoxedStrategy<Self>;
}
14 changes: 14 additions & 0 deletions sigma-testutil/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! test utilities

// Coding conventions
#![forbid(unsafe_code)]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![deny(dead_code)]
#![deny(unused_imports)]
#![deny(missing_docs)]

#[doc(hidden)]
pub mod generator;
29 changes: 29 additions & 0 deletions sigma-tree/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "sigma-tree"
version = "0.1.0"
authors = ["Denys Zadorozhnyi <denys@zadorozhnyi.com>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
sigma-ser = { path = "../sigma-ser" }

[dev-dependencies]
wasm-bindgen-test = "0.3.10"
proptest-derive = "0.1.2"
sigma-testutil = { path = "../sigma-testutil"}

[dev-dependencies.proptest]
# wasm support, via https://altsysrq.github.io/proptest-book/proptest/wasm.html
version = "0.9"
# The default feature set includes things like process forking which are not
# supported in Web Assembly.
default-features = false
# Enable using the `std` crate.
features = ["std"]

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

0 comments on commit 18bf25f

Please sign in to comment.