Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SSTring type #794

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions ergotree-ir/src/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub enum Literal {
Int(i32),
/// Long
Long(i64),
/// String type
String(Arc<str>),
/// Big integer
BigInt(BigInt256),
/// Sigma property
Expand Down Expand Up @@ -115,6 +117,7 @@ impl std::fmt::Debug for Literal {
Literal::GroupElement(v) => v.fmt(f),
Literal::AvlTree(v) => v.fmt(f),
Literal::CBox(v) => v.fmt(f),
Literal::String(v) => v.fmt(f),
}
}
}
Expand Down Expand Up @@ -172,6 +175,7 @@ impl std::fmt::Display for Literal {
Literal::GroupElement(v) => v.fmt(f),
Literal::AvlTree(v) => write!(f, "AvlTree({:?})", v),
Literal::CBox(v) => write!(f, "ErgoBox({:?})", v),
Literal::String(v) => write!(f, "String({v})"),
}
}
}
Expand Down Expand Up @@ -361,6 +365,7 @@ impl<'ctx> TryFrom<Value<'ctx>> for Constant {
}
}
Value::AvlTree(a) => Ok(Constant::from(*a)),
Value::String(s) => Ok(Constant::from(s)),
Value::Context => Err("Cannot convert Value::Context into Constant".into()),
Value::Header(_) => Err("Cannot convert Value::Header(_) into Constant".into()),
Value::PreHeader(_) => Err("Cannot convert Value::PreHeader(_) into Constant".into()),
Expand Down Expand Up @@ -592,6 +597,24 @@ impl From<ADDigest> for Constant {
}
}

impl From<Arc<str>> for Constant {
fn from(s: Arc<str>) -> Self {
Constant {
tpe: SType::SString,
v: Literal::String(s),
}
}
}

impl From<String> for Constant {
fn from(s: String) -> Self {
Constant {
tpe: SType::SString,
v: Literal::String(s.into()),
}
}
}

#[allow(clippy::unwrap_used)]
#[allow(clippy::from_over_into)]
#[impl_for_tuples(2, 4)]
Expand Down Expand Up @@ -830,6 +853,19 @@ impl TryExtractFrom<Literal> for BigInt256 {
}
}

impl TryExtractFrom<Literal> for String {
fn try_extract_from(v: Literal) -> Result<Self, TryExtractFromError> {
match v {
Literal::String(s) => Ok(String::from(&*s)),
_ => Err(TryExtractFromError(format!(
"expected {:?}, found {:?}",
std::any::type_name::<Self>(),
v
))),
}
}
}

impl TryExtractFrom<Literal> for AvlTreeData {
fn try_extract_from(v: Literal) -> Result<Self, TryExtractFromError> {
match v {
Expand Down Expand Up @@ -1118,6 +1154,19 @@ pub mod tests {
test_constant_roundtrip(());
}

// test that invalid strings don't error but are instead parsed lossily to match reference impl
#[test]
fn parse_invalid_string() {
let mut bytes = Constant::from(".".to_string())
.sigma_serialize_bytes()
.unwrap();
*bytes.last_mut().unwrap() = 0xf0;
assert_eq!(
Constant::sigma_parse_bytes(&bytes).unwrap().v,
Literal::String("�".into())
);
}

proptest! {

#![proptest_config(ProptestConfig::with_cases(8))]
Expand Down Expand Up @@ -1250,5 +1299,11 @@ pub mod tests {
test_constant_roundtrip(v);
}

#[test]
fn string_roundtrip(v in any::<String>()) {
test_constant_roundtrip(v);
}


}
}
5 changes: 5 additions & 0 deletions ergotree-ir/src/mir/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ pub enum Value<'ctx> {
Tup(TupleItems<Value<'ctx>>),
/// Transaction(and blockchain) context info
Context,
/// String type
String(Arc<str>),
/// Block header
Header(Box<Header>),
/// Header with predictable data
Expand Down Expand Up @@ -238,6 +240,7 @@ impl<'ctx> Value<'ctx> {
.unwrap(),
),
Value::Context => Value::Context,
Value::String(s) => Value::String(s.clone()),
Value::Header(h) => Value::Header(h.clone()),
Value::PreHeader(h) => Value::PreHeader(h.clone()),
Value::Global => Value::Global,
Expand Down Expand Up @@ -308,6 +311,7 @@ impl From<Literal> for Value<'static> {
Literal::Int(i) => Value::Int(i),
Literal::Long(l) => Value::Long(l),
Literal::BigInt(b) => Value::BigInt(b),
Literal::String(s) => Value::String(s),
Literal::Unit => Value::Unit,
Literal::SigmaProp(s) => Value::SigmaProp(s),
Literal::GroupElement(e) => Value::GroupElement(e.into()),
Expand Down Expand Up @@ -378,6 +382,7 @@ impl std::fmt::Display for Value<'_> {
Value::Int(v) => v.fmt(f),
Value::Long(v) => write!(f, "{}L", v),
Value::BigInt(v) => v.fmt(f),
Value::String(v) => v.fmt(f),
Value::SigmaProp(v) => v.fmt(f),
Value::GroupElement(v) => v.fmt(f),
Value::AvlTree(v) => write!(f, "AvlTree({:?})", v),
Expand Down
10 changes: 10 additions & 0 deletions ergotree-ir/src/serialization/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ impl DataSerializer {
Literal::BigInt(v) => {
v.sigma_serialize(w)?;
}
Literal::String(s) => {
w.put_usize_as_u32_unwrapped(s.len())?;
w.write_all(s.as_bytes())?;
}
Literal::GroupElement(ecp) => ecp.sigma_serialize(w)?,
Literal::SigmaProp(s) => s.value().sigma_serialize(w)?,
Literal::AvlTree(a) => a.sigma_serialize(w)?,
Expand Down Expand Up @@ -94,6 +98,12 @@ impl DataSerializer {
SShort => Literal::Short(r.get_i16()?),
SInt => Literal::Int(r.get_i32()?),
SLong => Literal::Long(r.get_i64()?),
SString => {
let len = r.get_u32()?;
let mut buf = vec![0; len as usize];
r.read_exact(&mut buf)?;
Literal::String(String::from_utf8_lossy(&buf).into())
}
SBigInt => Literal::BigInt(BigInt256::sigma_parse(r)?),
SUnit => Literal::Unit,
SGroupElement => Literal::GroupElement(Arc::new(EcPoint::sigma_parse(r)?)),
Expand Down
20 changes: 13 additions & 7 deletions ergotree-ir/src/serialization/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub enum TypeCode {
SBOX = 99,
SAVL_TREE = 100,
SCONTEXT = 101,
// SSTRING = 102,
SSTRING = 102,
STYPE_VAR = 103,
SHEADER = 104,
SPRE_HEADER = 105,
Expand Down Expand Up @@ -320,6 +320,7 @@ impl SType {
TypeCode::SBOX => SBox,
TypeCode::SAVL_TREE => SAvlTree,
TypeCode::SCONTEXT => SContext,
TypeCode::SSTRING => SString,
TypeCode::STYPE_VAR => STypeVar(stype_param::STypeVar::sigma_parse(r)?),
TypeCode::SHEADER => SHeader,
TypeCode::SPRE_HEADER => SPreHeader,
Expand Down Expand Up @@ -358,6 +359,7 @@ impl SigmaSerializable for SType {
SType::SBox => TypeCode::SBOX.sigma_serialize(w),
SType::SAvlTree => TypeCode::SAVL_TREE.sigma_serialize(w),
SType::SContext => TypeCode::SCONTEXT.sigma_serialize(w),
SType::SString => TypeCode::SSTRING.sigma_serialize(w),
SType::SHeader => TypeCode::SHEADER.sigma_serialize(w),
SType::SPreHeader => TypeCode::SPRE_HEADER.sigma_serialize(w),
SType::SGlobal => TypeCode::SGLOBAL.sigma_serialize(w),
Expand All @@ -380,15 +382,16 @@ impl SigmaSerializable for SType {
SGroupElement => TypeCode::OPTION_COLL_GROUP_ELEMENT.sigma_serialize(w),
SSigmaProp => TypeCode::OPTION_COLL_SIGMAPROP.sigma_serialize(w),
STypeVar(_) | SAny | SUnit | SBox | SAvlTree | SOption(_) | SColl(_)
| STuple(_) | SFunc(_) | SContext | SHeader | SPreHeader | SGlobal => {
| STuple(_) | SFunc(_) | SContext | SString | SHeader | SPreHeader
| SGlobal => {
// if not "embeddable" type fallback to generic Option type code following
// elem type code
TypeCode::OPTION.sigma_serialize(w)?;
elem_type.sigma_serialize(w)
}
},
STypeVar(_) | SAny | SUnit | SBox | SAvlTree | SOption(_) | STuple(_)
| SFunc(_) | SContext | SHeader | SPreHeader | SGlobal => {
| SFunc(_) | SContext | SString | SHeader | SPreHeader | SGlobal => {
// if not "embeddable" type fallback to generic Option type code following
// elem type code
TypeCode::OPTION.sigma_serialize(w)?;
Expand All @@ -415,15 +418,16 @@ impl SigmaSerializable for SType {
SGroupElement => TypeCode::NESTED_COLL_GROUP_ELEMENT.sigma_serialize(w),
SSigmaProp => TypeCode::NESTED_COLL_SIGMAPROP.sigma_serialize(w),
STypeVar(_) | SAny | SUnit | SBox | SAvlTree | SOption(_) | SColl(_)
| STuple(_) | SFunc(_) | SContext | SHeader | SPreHeader | SGlobal => {
| STuple(_) | SFunc(_) | SContext | SString | SHeader | SPreHeader
| SGlobal => {
// if not "embeddable" type fallback to generic Coll type code following
// elem type code
TypeCode::COLL.sigma_serialize(w)?;
elem_type.sigma_serialize(w)
}
},
STypeVar(_) | SAny | SUnit | SBox | SAvlTree | SOption(_) | STuple(_)
| SFunc(_) | SContext | SHeader | SPreHeader | SGlobal => {
| SFunc(_) | SContext | SString | SHeader | SPreHeader | SGlobal => {
// if not "embeddable" type fallback to generic Coll type code following
// elem type code
TypeCode::COLL.sigma_serialize(w)?;
Expand Down Expand Up @@ -511,9 +515,11 @@ impl SigmaSerializable for SType {
}
(
STypeVar(_) | SAny | SUnit | SBox | SAvlTree | SOption(_) | SColl(_)
| STuple(_) | SFunc(_) | SContext | SHeader | SPreHeader | SGlobal,
| STuple(_) | SFunc(_) | SContext | SString | SHeader | SPreHeader
| SGlobal,
STypeVar(_) | SAny | SUnit | SBox | SAvlTree | SOption(_) | SColl(_)
| STuple(_) | SFunc(_) | SContext | SHeader | SPreHeader | SGlobal,
| STuple(_) | SFunc(_) | SContext | SString | SHeader | SPreHeader
| SGlobal,
) => {
// Pair of non-primitive types (`(SBox, SAvlTree)`, `((Int, Byte), (Boolean,Box))`, etc.)
TypeCode::TUPLE_PAIR1.sigma_serialize(w)?;
Expand Down
5 changes: 5 additions & 0 deletions ergotree-ir/src/types/stype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub enum SType {
SFunc(SFunc),
/// Context object ("CONTEXT" in ErgoScript)
SContext,
/// UTF-8 String type
SString,
/// Header of a block
SHeader,
/// Header of a block without solved mining puzzle
Expand Down Expand Up @@ -91,6 +93,7 @@ impl SType {
| SType::SBox
| SType::SAvlTree
| SType::SContext
| SType::SString
| SType::SBoolean
| SType::SHeader
| SType::SPreHeader
Expand Down Expand Up @@ -149,6 +152,7 @@ impl std::fmt::Display for SType {
SType::STuple(t) => write!(f, "{}", t),
SType::SFunc(t) => write!(f, "{}", t),
SType::SContext => write!(f, "Context"),
SType::SString => write!(f, "String"),
SType::SHeader => write!(f, "Header"),
SType::SPreHeader => write!(f, "PreHeader"),
SType::SGlobal => write!(f, "Global"),
Expand Down Expand Up @@ -293,6 +297,7 @@ pub(crate) mod tests {
Just(SType::SBox),
Just(SType::SAvlTree),
Just(SType::SContext),
Just(SType::SString),
Just(SType::SHeader),
Just(SType::SPreHeader),
Just(SType::SGlobal),
Expand Down
Loading