From 4fe04501175496b3990632a13b8e375763b3dfcc Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Tue, 11 Aug 2020 12:00:25 +0300 Subject: [PATCH] add an option to substitute constant placeholders on ErgoTree parsing; --- sigma-tree/src/ergo_tree.rs | 8 +++++--- sigma-tree/src/serialization/expr.rs | 9 ++++++++- .../src/serialization/sigma_byte_reader.rs | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/sigma-tree/src/ergo_tree.rs b/sigma-tree/src/ergo_tree.rs index 4d7459765..69f8a3f34 100644 --- a/sigma-tree/src/ergo_tree.rs +++ b/sigma-tree/src/ergo_tree.rs @@ -85,9 +85,11 @@ impl ErgoTree { root.sigma_serialize(&mut w).unwrap(); let cursor = Cursor::new(&mut data[..]); let pr = PeekableReader::new(cursor); - // TODO make reader substitute constants - let mut sr = - SigmaByteReader::new(pr, ConstantStore::new(self.tree.clone().unwrap().constants)); + // TODO: make reader substitute constants + let mut sr = SigmaByteReader::new_with_substitute_placeholders( + pr, + ConstantStore::new(self.tree.clone().unwrap().constants), + ); let parsed_expr = Expr::sigma_parse(&mut sr).unwrap(); // todo!("substitute placeholders: {:?}", self.tree); Ok(Rc::new(parsed_expr)) diff --git a/sigma-tree/src/serialization/expr.rs b/sigma-tree/src/serialization/expr.rs index 6072e6a44..25db8d542 100644 --- a/sigma-tree/src/serialization/expr.rs +++ b/sigma-tree/src/serialization/expr.rs @@ -49,7 +49,14 @@ impl SigmaSerializable for Expr { FoldSerializer::OP_CODE => FoldSerializer::sigma_parse(r), ConstantPlaceholder::OP_CODE => { let cp = ConstantPlaceholder::sigma_parse(r)?; - Ok(Expr::ConstPlaceholder(cp)) + if r.substitute_placeholders() { + // ConstantPlaceholder itself can be created only if a corresponding + // constant is in the constant_store, thus unwrap() is safe here + let c = r.constant_store().get(cp.id).unwrap(); + Ok(Expr::Const(c.clone())) + } else { + Ok(Expr::ConstPlaceholder(cp)) + } } o => Err(SerializationError::NotImplementedOpCode(o.value())), } diff --git a/sigma-tree/src/serialization/sigma_byte_reader.rs b/sigma-tree/src/serialization/sigma_byte_reader.rs index 0d2a6f8db..fd784a669 100644 --- a/sigma-tree/src/serialization/sigma_byte_reader.rs +++ b/sigma-tree/src/serialization/sigma_byte_reader.rs @@ -5,6 +5,7 @@ use std::io::Read; pub struct SigmaByteReader { inner: R, constant_store: ConstantStore, + substitute_placeholders: bool, } impl SigmaByteReader { @@ -13,12 +14,26 @@ impl SigmaByteReader { SigmaByteReader { inner: pr, constant_store, + substitute_placeholders: false, + } + } + + pub fn new_with_substitute_placeholders( + pr: R, + constant_store: ConstantStore, + ) -> SigmaByteReader { + SigmaByteReader { + inner: pr, + constant_store, + substitute_placeholders: true, } } } pub trait SigmaByteRead: ReadSigmaVlqExt { fn constant_store(&mut self) -> &mut ConstantStore; + + fn substitute_placeholders(&self) -> bool; } impl Read for SigmaByteReader { @@ -37,4 +52,8 @@ impl SigmaByteRead for SigmaByteReader { fn constant_store(&mut self) -> &mut ConstantStore { &mut self.constant_store } + + fn substitute_placeholders(&self) -> bool { + self.substitute_placeholders + } }