simplicityhl 0.7.1

Rust-like language that compiles to Simplicity bytecode.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use std::collections::HashMap;
use std::fmt;

use crate::parse::ParseFromStr;
use crate::str::WitnessName;
use crate::types::ResolvedType;
use crate::value::Value;
use crate::witness::{Arguments, UnresolvedValue, UnresolvedValues, WitnessValues};
use crate::{AbiMeta, Parameters, WitnessTypes};
use serde::{de, ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};

/// Visitor for a map from witness names to values of type `V`, rejecting duplicate names.
struct NamedMapVisitor<V>(std::marker::PhantomData<V>);

impl<V> NamedMapVisitor<V> {
    const fn new() -> Self {
        Self(std::marker::PhantomData)
    }
}

impl<'de, V: Deserialize<'de>> de::Visitor<'de> for NamedMapVisitor<V> {
    type Value = HashMap<WitnessName, V>;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a map with string keys")
    }

    fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
    where
        M: de::MapAccess<'de>,
    {
        let mut map = HashMap::new();
        while let Some((key, value)) = access.next_entry::<WitnessName, V>()? {
            if map.insert(key.shallow_clone(), value).is_some() {
                return Err(de::Error::custom(format!("Name `{key}` is assigned twice")));
            }
        }
        Ok(map)
    }
}

impl<'de> Deserialize<'de> for WitnessValues {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer
            .deserialize_map(NamedMapVisitor::<Value>::new())
            .map(Self::from)
    }
}

struct UnresolvedValueVisitor;

impl<'de> de::Visitor<'de> for UnresolvedValueVisitor {
    type Value = UnresolvedValue;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a value string or a map with \"value\" and \"type\" fields")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(UnresolvedValue::Untyped(value.to_owned()))
    }

    fn visit_map<M>(self, access: M) -> Result<Self::Value, M::Error>
    where
        M: de::MapAccess<'de>,
    {
        ValueMapVisitor
            .visit_map(access)
            .map(UnresolvedValue::Typed)
    }
}

impl<'de> Deserialize<'de> for UnresolvedValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        // deserialize_any tells a bare string and a { value, type } map apart.
        // This requires a self-describing format (JSON).
        deserializer.deserialize_any(UnresolvedValueVisitor)
    }
}

impl<'de> Deserialize<'de> for UnresolvedValues {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer
            .deserialize_map(NamedMapVisitor::<UnresolvedValue>::new())
            .map(Self::from_map)
    }
}

impl Serialize for ResolvedType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        // Enum mentions serialize as the enum's declared name, which is opaque.
        // Variants and wire encoding live in the program's declarations.
        serializer.serialize_str(&self.to_string())
    }
}

impl Serialize for WitnessName {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_ref())
    }
}

impl Serialize for AbiMeta {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: ::serde::Serializer,
    {
        use ::serde::ser::SerializeStruct;

        let mut state = serializer.serialize_struct("AbiMeta", 2)?;
        state.serialize_field("witness_types", &self.witness_types)?;
        state.serialize_field("parameter_types", &self.param_types)?;
        state.end()
    }
}

impl Serialize for Parameters {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let map_ref = self.as_ref();
        let mut map = serializer.serialize_map(Some(map_ref.len()))?;
        for (key, value) in map_ref {
            map.serialize_entry(key, value)?;
        }
        map.end()
    }
}

impl Serialize for WitnessTypes {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let map_ref = self.as_ref();
        let mut map = serializer.serialize_map(Some(map_ref.len()))?;
        for (key, value) in map_ref {
            map.serialize_entry(key, value)?;
        }
        map.end()
    }
}

impl<'de> Deserialize<'de> for Arguments {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer
            .deserialize_map(NamedMapVisitor::<Value>::new())
            .map(Self::from)
    }
}

struct ValueMapVisitor;

impl<'de> de::Visitor<'de> for ValueMapVisitor {
    type Value = Value;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a map with \"value\" and \"type\" fields")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Err(de::Error::custom(format!(
            "cannot deserialize the bare value `{value}` without the program's declared types; \
             deserialize into `UnresolvedValues` and resolve it against the program"
        )))
    }

    fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
    where
        M: de::MapAccess<'de>,
    {
        let mut value = None;
        let mut ty = None;

        while let Some(key) = access.next_key::<&str>()? {
            match key {
                "value" => {
                    if value.is_some() {
                        return Err(de::Error::duplicate_field("value"));
                    }
                    value = Some(access.next_value::<&str>()?);
                }
                "type" => {
                    if ty.is_some() {
                        return Err(de::Error::duplicate_field("type"));
                    }
                    ty = Some(access.next_value::<&str>()?);
                }
                _ => {
                    return Err(de::Error::unknown_field(key, &["value", "type"]));
                }
            }
        }

        let ty = match ty {
            Some(s) => ResolvedType::parse_from_str(s).map_err(de::Error::custom)?,
            None => return Err(de::Error::missing_field("type")),
        };
        match value {
            Some(s) => Value::parse_from_str(s, &ty).map_err(de::Error::custom),
            None => Err(de::Error::missing_field("value")),
        }
    }
}

impl<'de> Deserialize<'de> for Value {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        // deserialize_any lets a bare string (the form enum values serialize as) reach visit_str's directed error.
        // Self-describing formats only, as in `UnresolvedValue::deserialize`.
        deserializer.deserialize_any(ValueMapVisitor)
    }
}

struct ParserVisitor<A>(std::marker::PhantomData<A>);

impl<'de, A: ParseFromStr> de::Visitor<'de> for ParserVisitor<A> {
    type Value = A;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a valid string")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        A::parse_from_str(value).map_err(E::custom)
    }
}

impl<'de> Deserialize<'de> for WitnessName {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(ParserVisitor::<Self>(std::marker::PhantomData))
    }
}

struct WitnessMapSerializer<'a>(&'a HashMap<WitnessName, Value>);

impl<'a> Serialize for WitnessMapSerializer<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(Some(self.0.len()))?;
        for (name, value) in self.0 {
            // Enum values serialize as their bare value string.
            // The { value, type } form cannot express them, since its type
            // string is parsed without the program's declarations.
            // Such values round-trip through `UnresolvedValues`, not through `Deserialize<WitnessValues>`.
            //
            // TODO: Consider serializing every value as a bare string and retiring the { value, type } form.
            // That drops "witness file readable without the program" entirely.
            if value.ty().contains_enum() {
                map.serialize_entry(name.as_inner(), &value.to_string())?;
                continue;
            }
            map.serialize_entry(name.as_inner(), &ValueMapSerializer(value))?;
        }
        map.end()
    }
}

struct ValueMapSerializer<'a>(&'a Value);

impl<'a> Serialize for ValueMapSerializer<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(Some(2))?;
        map.serialize_entry("value", &self.0.to_string())?;
        map.serialize_entry("type", &self.0.ty().to_string())?;
        map.end()
    }
}

impl Serialize for WitnessValues {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        WitnessMapSerializer(self.as_inner()).serialize(serializer)
    }
}

impl Serialize for Arguments {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        WitnessMapSerializer(self.as_inner()).serialize(serializer)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn witness_serde_duplicate_assignment() {
        let s = r#"{
  "A": { "value": "42", "type": "u32" },
  "A": { "value": "43", "type": "u16" }
}"#;

        match serde_json::from_str::<WitnessValues>(s) {
            Ok(_) => panic!("Duplicate witness assignment was falsely accepted"),
            Err(error) => assert!(error.to_string().contains("Name `A` is assigned twice")),
        }
    }

    fn unit_enum(name: &str, variants: &[&str]) -> ResolvedType {
        use crate::str::Identifier;
        use crate::types::{EnumInfo, EnumVariantInfo};
        use std::sync::Arc;

        let variants: Arc<[EnumVariantInfo]> = variants
            .iter()
            .map(|name| EnumVariantInfo::new(Identifier::from_str_unchecked(name), Arc::from([])))
            .collect();
        ResolvedType::enumeration(EnumInfo::new(Arc::from(name), variants))
    }

    #[test]
    fn abi_enum_type_serializes_as_name() {
        use crate::str::WitnessName;
        use crate::types::TypeConstructible;

        let action_ty = unit_enum("Action", &["Inherit", "ColdSpend"]);
        let witness_types = WitnessTypes::from(HashMap::from([
            (WitnessName::from_str_unchecked("ACTION"), action_ty.clone()),
            (
                WitnessName::from_str_unchecked("MAYBE"),
                ResolvedType::option(action_ty.clone()),
            ),
            (
                WitnessName::from_str_unchecked("PAIR"),
                ResolvedType::tuple([action_ty, unit_enum("Reaction", &["Fast", "Slow"])]),
            ),
            (
                WitnessName::from_str_unchecked("PLAIN"),
                crate::parse::ParseFromStr::parse_from_str("u32").unwrap(),
            ),
        ]));

        let json = serde_json::to_value(&witness_types).unwrap();
        assert_eq!(json["ACTION"], "Action");
        assert_eq!(json["MAYBE"], "Option<Action>");
        assert_eq!(json["PAIR"], "(Action, Reaction)");
        assert_eq!(json["PLAIN"], "u32");
    }

    #[test]
    fn enum_witness_value_serializes_as_variant_name() {
        use crate::str::{Identifier, WitnessName};

        let action_ty = unit_enum("Action", &["Inherit", "ColdSpend"]);
        let value = Value::enum_variant(
            &action_ty,
            &Identifier::from_str_unchecked("ColdSpend"),
            vec![],
        )
        .unwrap();
        let witness = WitnessValues::from(HashMap::from([(
            WitnessName::from_str_unchecked("ACTION"),
            value,
        )]));

        // Serializes in the bare form that UnresolvedValues can resolve back.
        let json = serde_json::to_value(&witness).unwrap();
        assert_eq!(json["ACTION"], "Action::ColdSpend");

        // The bare form round-trips through UnresolvedValues + resolve.
        let text = serde_json::to_string(&witness).unwrap();
        let unresolved: UnresolvedValues = serde_json::from_str(&text).unwrap();
        let witness_types = WitnessTypes::from(HashMap::from([(
            WitnessName::from_str_unchecked("ACTION"),
            action_ty,
        )]));
        let round_tripped: WitnessValues = unresolved.resolve(&witness_types).unwrap();
        assert_eq!(witness, round_tripped);

        // Deserializing it as WitnessValues directly cannot work without the
        // program's types; the error says where to go instead.
        let err = serde_json::from_str::<WitnessValues>(&text).unwrap_err();
        assert!(
            err.to_string().contains("UnresolvedValues"),
            "error should point at the resolution path: {err}"
        );
    }

    #[test]
    fn payload_enum_witness_value_round_trips() {
        use crate::str::{Identifier, WitnessName};
        use crate::types::{EnumInfo, EnumVariantInfo};
        use crate::value::ValueConstructible;
        use std::sync::Arc;

        let u32_ty: ResolvedType = ParseFromStr::parse_from_str("u32").unwrap();
        let variants: Arc<[EnumVariantInfo]> = Arc::from([
            EnumVariantInfo::new(Identifier::from_str_unchecked("Cold"), Arc::from([])),
            EnumVariantInfo::new(
                Identifier::from_str_unchecked("Refresh"),
                Arc::from([u32_ty.clone()]),
            ),
        ]);
        let action_ty = ResolvedType::enumeration(EnumInfo::new(Arc::from("Action"), variants));
        let value = Value::enum_variant(
            &action_ty,
            &Identifier::from_str_unchecked("Refresh"),
            vec![Value::u32(42)],
        )
        .unwrap();
        let witness = WitnessValues::from(HashMap::from([(
            WitnessName::from_str_unchecked("ACTION"),
            value,
        )]));

        // Payload variants serialize as their display form and round-trip through UnresolvedValues + resolve.
        let json = serde_json::to_value(&witness).unwrap();
        assert_eq!(json["ACTION"], "Action::Refresh(42)");

        let text = serde_json::to_string(&witness).unwrap();
        let unresolved: UnresolvedValues = serde_json::from_str(&text).unwrap();
        let witness_types = WitnessTypes::from(HashMap::from([(
            WitnessName::from_str_unchecked("ACTION"),
            action_ty,
        )]));
        let round_tripped: WitnessValues = unresolved.resolve(&witness_types).unwrap();
        assert_eq!(witness, round_tripped);
    }

    #[test]
    fn nested_enum_witness_value_serializes_as_bare_string() {
        use crate::str::{Identifier, WitnessName};
        use crate::types::TypeConstructible;
        use crate::value::ValueConstructible;

        let action_ty = unit_enum("Action", &["Hot", "Cold"]);
        let option_ty = ResolvedType::option(action_ty.clone());
        let cold = Value::enum_variant(&action_ty, &Identifier::from_str_unchecked("Cold"), vec![])
            .unwrap();
        let witness = WitnessValues::from(HashMap::from([(
            WitnessName::from_str_unchecked("MAYBE"),
            Value::some(cold),
        )]));

        // A nested enum value must not fall into the { value, type } form,
        // whose type string cannot be parsed without the program.
        let json = serde_json::to_value(&witness).unwrap();
        assert_eq!(json["MAYBE"], "Some(Action::Cold)");

        let text = serde_json::to_string(&witness).unwrap();
        let unresolved: UnresolvedValues = serde_json::from_str(&text).unwrap();
        let witness_types = WitnessTypes::from(HashMap::from([(
            WitnessName::from_str_unchecked("MAYBE"),
            option_ty,
        )]));
        let round_tripped: WitnessValues = unresolved.resolve(&witness_types).unwrap();
        assert_eq!(witness, round_tripped);
    }
}