Skip to content

Commit 695f52a

Browse files
committed
Add header variant to Literal
1 parent 426558c commit 695f52a

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

ergotree-interpreter/src/eval/sglobal.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ pub(crate) static SGLOBAL_NONE_EVAL_FN: EvalFn = |_mc, _env, _ctx, obj, _args| {
223223
#[cfg(test)]
224224
#[cfg(feature = "arbitrary")]
225225
mod tests {
226-
use ergo_chain_types::EcPoint;
226+
use ergo_chain_types::{EcPoint, Header};
227227
use ergotree_ir::bigint256::BigInt256;
228228
use ergotree_ir::ergo_tree::ErgoTreeVersion;
229229
use ergotree_ir::mir::constant::Constant;
@@ -581,5 +581,9 @@ mod tests {
581581
fn serialize_unsigned_bigint(v in any::<UnsignedBigInt>()) {
582582
assert_eq!(deserialize(&serialize(v), SType::SUnsignedBigInt), Constant::from(v));
583583
}
584+
#[test]
585+
fn serialize_header(h in any::<Header>()) {
586+
assert_eq!(deserialize(&serialize(h.clone()), SType::SHeader), Constant::from(h));
587+
}
584588
}
585589
}

ergotree-ir/src/mir/constant.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use ergo_chain_types::ADDigest;
3131
use ergo_chain_types::Base16DecodedBytes;
3232
use ergo_chain_types::Digest32;
3333
use ergo_chain_types::EcPoint;
34+
use ergo_chain_types::Header;
3435
use impl_trait_for_tuples::impl_for_tuples;
3536
use sigma_util::AsVecI8;
3637
use sigma_util::AsVecU8;
@@ -83,6 +84,8 @@ pub enum Literal {
8384
GroupElement(Arc<EcPoint>),
8485
/// AVL tree
8586
AvlTree(Box<AvlTreeData>),
87+
/// Block Header type
88+
Header(Box<Header>),
8689
/// Ergo box
8790
CBox(Ref<'static, ErgoBox>),
8891
/// Collection
@@ -165,6 +168,7 @@ impl core::fmt::Debug for Literal {
165168
Literal::GroupElement(v) => v.fmt(f),
166169
Literal::UnsignedBigInt(v) => v.fmt(f),
167170
Literal::AvlTree(v) => v.fmt(f),
171+
Literal::Header(v) => v.fmt(f),
168172
Literal::CBox(v) => v.fmt(f),
169173
Literal::String(v) => v.fmt(f),
170174
}
@@ -224,6 +228,7 @@ impl core::fmt::Display for Literal {
224228
Literal::UnsignedBigInt(v) => v.fmt(f),
225229
Literal::GroupElement(v) => v.fmt(f),
226230
Literal::AvlTree(v) => write!(f, "AvlTree({:?})", v),
231+
Literal::Header(v) => write!(f, "Header({:?})", v),
227232
Literal::CBox(v) => write!(f, "ErgoBox({:?})", v),
228233
Literal::String(v) => write!(f, "String({v})"),
229234
}
@@ -422,9 +427,9 @@ impl<'ctx> TryFrom<Value<'ctx>> for Constant {
422427
}
423428
}
424429
Value::AvlTree(a) => Ok(Constant::from(*a)),
430+
Value::Header(h) => Ok(Constant::from(*h)),
425431
Value::String(s) => Ok(Constant::from(s)),
426432
Value::Context => Err("Cannot convert Value::Context into Constant".into()),
427-
Value::Header(_) => Err("Cannot convert Value::Header(_) into Constant".into()),
428433
Value::PreHeader(_) => Err("Cannot convert Value::PreHeader(_) into Constant".into()),
429434
Value::Global => Err("Cannot convert Value::Global into Constant".into()),
430435
Value::Lambda(_) => Err("Cannot convert Value::Lambda(_) into Constant".into()),
@@ -643,6 +648,15 @@ impl From<AvlTreeData> for Constant {
643648
}
644649
}
645650

651+
impl From<Header> for Constant {
652+
fn from(h: Header) -> Self {
653+
Constant {
654+
tpe: SType::SHeader,
655+
v: Literal::Header(h.into()),
656+
}
657+
}
658+
}
659+
646660
impl From<AvlTreeFlags> for Constant {
647661
fn from(a: AvlTreeFlags) -> Self {
648662
Constant {
@@ -1209,6 +1223,8 @@ pub(crate) mod arbitrary {
12091223
#[cfg(feature = "arbitrary")]
12101224
#[allow(clippy::panic)]
12111225
mod tests {
1226+
use crate::{ergo_tree::ErgoTreeVersion, serialization::roundtrip_new_feature};
1227+
12121228
use super::*;
12131229
use core::fmt;
12141230
use proptest::prelude::*;
@@ -1424,5 +1440,10 @@ mod tests {
14241440
test_constant_roundtrip(v);
14251441
}
14261442

1443+
#[test]
1444+
fn header_ser_roundtrip(h in any::<Header>()) {
1445+
roundtrip_new_feature(&Constant::from(h), ErgoTreeVersion::V3);
1446+
}
1447+
14271448
}
14281449
}

ergotree-ir/src/mir/value.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ impl From<Literal> for Value<'static> {
386386
Value::Coll(converted_coll)
387387
}
388388
Literal::AvlTree(a) => Value::AvlTree(a),
389+
Literal::Header(h) => Value::Header(h),
389390
Literal::Opt(lit) => Value::Opt(lit.map(|boxed| *boxed).map(Value::from).map(Box::new)),
390391
Literal::Tup(t) => Value::Tup(t.mapped(Value::from)),
391392
}

ergotree-ir/src/serialization/data.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use alloc::boxed::Box;
2+
use ergo_chain_types::Header;
3+
use sigma_ser::ScorexSerializable;
24

35
use alloc::string::String;
46
use alloc::string::ToString;
@@ -93,13 +95,21 @@ impl DataSerializer {
9395
Literal::Tup(items) => items
9496
.iter()
9597
.try_for_each(|i| DataSerializer::sigma_serialize(i, w))?,
98+
Literal::Header(h) if w.tree_version() >= ErgoTreeVersion::V3 => {
99+
h.scorex_serialize(w)?;
100+
}
96101
// unsupported, see
97102
// https://github.com/ScorexFoundation/sigmastate-interpreter/issues/659
98103
Literal::Opt(_) => {
99104
return Err(SigmaSerializationError::NotSupported(
100105
"Option serialization is not supported".to_string(),
101106
));
102107
}
108+
Literal::Header(_) => {
109+
return Err(SigmaSerializationError::NotSupported(
110+
"Header serialization is not supported".to_string(),
111+
));
112+
}
103113
})
104114
}
105115

@@ -174,6 +184,9 @@ impl DataSerializer {
174184
}
175185
SBox => Literal::CBox(Arc::new(ErgoBox::sigma_parse(r)?).into()),
176186
SAvlTree => Literal::AvlTree(Box::new(AvlTreeData::sigma_parse(r)?)),
187+
SHeader if r.tree_version() >= ErgoTreeVersion::V3 => {
188+
Literal::Header(Box::new(Header::scorex_parse(r)?))
189+
}
177190
STypeVar(_) => return Err(SigmaParsingError::NotSupported("TypeVar data")),
178191
SAny => return Err(SigmaParsingError::NotSupported("SAny data")),
179192
SOption(_) => return Err(SigmaParsingError::NotSupported("SOption data")),

0 commit comments

Comments
 (0)