pallas-codec 1.3.0

Pallas common CBOR encoding interface and utilities
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
//! Stack-safe decoding and traversal of recursive structures.
//!
//! Recursive types such as native scripts or Plutus data can nest thousands
//! of levels deep inside a small payload, and any operation that recurses per
//! level exhausts the thread stack. Everything here drives a heap-backed
//! stack instead.
//!
//! - [`decode_tree`](crate::tree::decode_tree) builds a value from CBOR. A
//!   type opts in by implementing [`TreeDecode`](crate::tree::TreeDecode),
//!   which splits decoding a node into a header, a sequence of children and
//!   a footer.
//! - [`map_tree`](crate::tree::map_tree), [`walk_tree`](crate::tree::walk_tree),
//!   [`eq_tree`](crate::tree::eq_tree) and
//!   [`drop_children`](crate::tree::drop_children) operate on an existing
//!   value. A type opts in by implementing [`TreeNode`](crate::tree::TreeNode),
//!   which exposes its child list; they cover copying into another tree
//!   (clone, protobuf or JSON values), emitting a linear encoding, comparison
//!   and destruction.

use minicbor::{Decoder, data::Type, decode::Error};

/// Number of children that follow a node's header in the input.
pub enum Arity {
    Leaf,
    Fixed(u64),
    /// Children continue until a CBOR break.
    Indefinite,
}

impl From<Option<u64>> for Arity {
    /// Converts the result of [`Decoder::array`] or [`Decoder::map`].
    fn from(len: Option<u64>) -> Self {
        match len {
            Some(n) => Self::Fixed(n),
            None => Self::Indefinite,
        }
    }
}

/// A recursive type whose nodes can be decoded without recursion.
///
/// The driver never reserves memory from a CBOR length header; builders grow
/// only as children are actually decoded from the input.
pub trait TreeDecode<'b, C>: Sized {
    /// Partially decoded node, accumulating children until [`end`].
    ///
    /// [`end`]: TreeDecode::end
    type Builder;

    /// Decode a node's header: everything up to and including the header of
    /// its child list.
    fn begin(d: &mut Decoder<'b>, ctx: &mut C) -> Result<(Self::Builder, Arity), Error>;

    /// Attach the next fully decoded child. Map-like nodes receive keys and
    /// values alternately.
    fn child(builder: &mut Self::Builder, child: Self) -> Result<(), Error>;

    /// Decode anything that follows the child list and finish the node.
    fn end(builder: Self::Builder, d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error>;
}

struct Frame<B> {
    builder: B,
    arity: Arity,
}

impl<B> Frame<B> {
    fn begin<'b, C, T>(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error>
    where
        T: TreeDecode<'b, C, Builder = B>,
    {
        let (builder, arity) = T::begin(d, ctx)?;
        Ok(Self { builder, arity })
    }

    fn expects_child(&mut self, d: &mut Decoder<'_>) -> Result<bool, Error> {
        match self.arity {
            Arity::Leaf | Arity::Fixed(0) => Ok(false),
            Arity::Fixed(_) => Ok(true),
            Arity::Indefinite if d.datatype()? == Type::Break => {
                d.skip()?;
                self.arity = Arity::Leaf;
                Ok(false)
            }
            Arity::Indefinite => Ok(true),
        }
    }
}

/// Decode a [`TreeDecode`] value using a heap-backed stack of open nodes.
pub fn decode_tree<'b, C, T>(d: &mut Decoder<'b>, ctx: &mut C) -> Result<T, Error>
where
    T: TreeDecode<'b, C>,
{
    let mut parents: Vec<Frame<T::Builder>> = Vec::new();
    let mut current = Frame::begin::<C, T>(d, ctx)?;
    loop {
        if current.expects_child(d)? {
            parents.push(current);
            current = Frame::begin::<C, T>(d, ctx)?;
            continue;
        }
        let node = T::end(current.builder, d, ctx)?;
        let Some(mut parent) = parents.pop() else {
            return Ok(node);
        };
        T::child(&mut parent.builder, node)?;
        if let Arity::Fixed(remaining) = &mut parent.arity {
            *remaining -= 1;
        }
        current = parent;
    }
}

/// A recursive type whose children live in a `Vec`.
pub trait TreeNode: Sized {
    fn children(&self) -> &[Self];

    /// The child list, or `None` for leaf variants.
    fn children_mut(&mut self) -> Option<&mut Vec<Self>>;
}

/// Build a target tree from a source tree without recursion.
///
/// `shallow` maps one node to its target with an empty child list, and
/// `target_children` exposes that list so the driver can fill it. The target
/// needs no trait: any type with a `Vec` of children fits.
pub fn map_tree<S, T>(
    root: &S,
    shallow: impl Fn(&S) -> T,
    target_children: impl Fn(&mut T) -> Option<&mut Vec<T>>,
) -> T
where
    S: TreeNode,
{
    let mut target_root = shallow(root);
    let mut pending = vec![(root, &mut target_root)];
    while let Some((source, target)) = pending.pop() {
        let Some(children) = target_children(target) else {
            continue;
        };
        let source_children = source.children();
        *children = source_children.iter().map(&shallow).collect();
        pending.extend(source_children.iter().zip(children.iter_mut()));
    }
    target_root
}

/// One step of a [`walk_tree`] traversal.
pub enum Visit<'a, S> {
    /// A node, before any of its children.
    Enter(&'a S),
    /// The parent, before each of its children but the first.
    Between(&'a S),
    /// A node, after all of its children.
    Exit(&'a S),
}

/// Pre-order traversal without recursion, for encoders and renderers.
pub fn walk_tree<S, E>(
    root: &S,
    mut visit: impl FnMut(Visit<'_, S>) -> Result<(), E>,
) -> Result<(), E>
where
    S: TreeNode,
{
    let mut stack = vec![Visit::Enter(root)];
    while let Some(step) = stack.pop() {
        if let Visit::Enter(node) = step {
            visit(Visit::Enter(node))?;
            stack.push(Visit::Exit(node));
            for (i, child) in node.children().iter().enumerate().rev() {
                stack.push(Visit::Enter(child));
                if i > 0 {
                    stack.push(Visit::Between(node));
                }
            }
        } else {
            visit(step)?;
        }
    }
    Ok(())
}

/// Structural equality without recursion. `same_node` compares two nodes'
/// own data, ignoring their children.
pub fn eq_tree<S>(left: &S, right: &S, same_node: impl Fn(&S, &S) -> bool) -> bool
where
    S: TreeNode,
{
    let mut pending = vec![(left, right)];
    while let Some((left, right)) = pending.pop() {
        if !same_node(left, right) || left.children().len() != right.children().len() {
            return false;
        }
        pending.extend(left.children().iter().zip(right.children()));
    }
    true
}

/// Detach and destroy a node's descendants without recursion. Call from a
/// `Drop` impl; the node's own drop then has no children left to recurse
/// into.
pub fn drop_children<S: TreeNode>(node: &mut S) {
    let Some(children) = node.children_mut() else {
        return;
    };
    let mut pending = std::mem::take(children);
    while let Some(mut child) = pending.pop() {
        if let Some(children) = child.children_mut() {
            pending.append(children);
        }
    }
}

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

    #[derive(Debug, PartialEq)]
    enum Node {
        Leaf(u64),
        List(Vec<Node>),
    }

    impl TreeNode for Node {
        fn children(&self) -> &[Self] {
            match self {
                Node::List(children) => children,
                Node::Leaf(_) => &[],
            }
        }

        fn children_mut(&mut self) -> Option<&mut Vec<Self>> {
            match self {
                Node::List(children) => Some(children),
                Node::Leaf(_) => None,
            }
        }
    }

    impl Drop for Node {
        fn drop(&mut self) {
            drop_children(self);
        }
    }

    impl<'b, C> TreeDecode<'b, C> for Node {
        type Builder = Node;

        fn begin(d: &mut Decoder<'b>, _: &mut C) -> Result<(Node, Arity), Error> {
            match d.datatype()? {
                Type::Array | Type::ArrayIndef => Ok((Node::List(vec![]), d.array()?.into())),
                _ => Ok((Node::Leaf(d.u64()?), Arity::Leaf)),
            }
        }

        fn child(builder: &mut Node, child: Node) -> Result<(), Error> {
            let Node::List(children) = builder else {
                unreachable!()
            };
            children.push(child);
            Ok(())
        }

        fn end(builder: Node, _: &mut Decoder<'b>, _: &mut C) -> Result<Node, Error> {
            Ok(builder)
        }
    }

    fn decode(bytes: &[u8]) -> Result<Node, Error> {
        let mut d = Decoder::new(bytes);
        let node = decode_tree(&mut d, &mut ())?;
        assert_eq!(d.position(), bytes.len());
        Ok(node)
    }

    #[test]
    fn decodes_mixed_definite_and_indefinite_lists() {
        // [1, [], [2, [3]], []_]
        let node = decode(&[0x84, 0x01, 0x80, 0x82, 0x02, 0x81, 0x03, 0x9f, 0xff]).unwrap();
        let expected = Node::List(vec![
            Node::Leaf(1),
            Node::List(vec![]),
            Node::List(vec![Node::Leaf(2), Node::List(vec![Node::Leaf(3)])]),
            Node::List(vec![]),
        ]);
        assert_eq!(node, expected);
    }

    #[test]
    fn rejects_truncated_input() {
        assert!(decode(&[0x82, 0x01]).is_err());
        assert!(decode(&[0x9f, 0x01]).is_err());
        assert!(decode(&[0x81]).is_err());
    }

    #[test]
    fn decodes_deep_nesting_on_a_small_stack() {
        std::thread::Builder::new()
            .stack_size(64 * 1024)
            .spawn(|| {
                let depth = 100_000;
                let mut bytes = Vec::new();
                for i in 0..depth {
                    bytes.push(if i % 2 == 0 { 0x81 } else { 0x9f });
                }
                bytes.push(0x00);
                bytes.extend((0..depth).filter(|i| i % 2 == 1).map(|_| 0xff));
                let mut cursor = &decode(&bytes).unwrap();
                let mut seen = 0;
                while let Node::List(children) = cursor {
                    assert_eq!(children.len(), 1);
                    cursor = &children[0];
                    seen += 1;
                }
                assert_eq!(seen, depth);
                bytes.pop();
                assert!(decode(&bytes).is_err());
            })
            .unwrap()
            .join()
            .unwrap();
    }

    fn mixed() -> Node {
        Node::List(vec![
            Node::Leaf(1),
            Node::List(vec![]),
            Node::List(vec![Node::Leaf(2), Node::List(vec![Node::Leaf(3)])]),
            Node::List(vec![]),
        ])
    }

    fn chain(depth: usize) -> Node {
        let mut node = Node::Leaf(0);
        for _ in 0..depth {
            node = Node::List(vec![node]);
        }
        node
    }

    fn render(node: &Node) -> String {
        let mut out = String::new();
        walk_tree::<_, std::fmt::Error>(node, |visit| {
            match visit {
                Visit::Enter(Node::Leaf(n)) => out.push_str(&n.to_string()),
                Visit::Enter(Node::List(_)) => out.push('['),
                Visit::Between(_) => out.push(','),
                Visit::Exit(Node::List(_)) => out.push(']'),
                Visit::Exit(Node::Leaf(_)) => {}
            }
            Ok(())
        })
        .unwrap();
        out
    }

    #[test]
    fn walks_mixed_shapes_in_order() {
        assert_eq!(render(&mixed()), "[1,[],[2,[3]],[]]");
        assert_eq!(render(&Node::Leaf(7)), "7");
        assert_eq!(render(&Node::List(vec![])), "[]");
    }

    #[test]
    fn walk_propagates_errors() {
        let result = walk_tree(&mixed(), |visit| match visit {
            Visit::Enter(Node::Leaf(3)) => Err("three"),
            _ => Ok(()),
        });
        assert_eq!(result, Err("three"));
    }

    #[test]
    fn maps_mixed_shapes_positionally() {
        // Same shape, leaves doubled, into an unrelated target type.
        #[derive(Debug, PartialEq)]
        enum Target {
            Leaf(u64),
            List(Vec<Target>),
        }
        let mapped = map_tree(
            &mixed(),
            |node| match node {
                Node::Leaf(n) => Target::Leaf(n * 2),
                Node::List(_) => Target::List(vec![]),
            },
            |target| match target {
                Target::List(children) => Some(children),
                Target::Leaf(_) => None,
            },
        );
        let expected = Target::List(vec![
            Target::Leaf(2),
            Target::List(vec![]),
            Target::List(vec![Target::Leaf(4), Target::List(vec![Target::Leaf(6)])]),
            Target::List(vec![]),
        ]);
        assert_eq!(mapped, expected);
    }

    #[test]
    fn compares_structure_and_node_data() {
        let same = |a: &Node, b: &Node| match (a, b) {
            (Node::Leaf(a), Node::Leaf(b)) => a == b,
            (Node::List(_), Node::List(_)) => true,
            _ => false,
        };
        assert!(eq_tree(&mixed(), &mixed(), same));
        assert!(!eq_tree(&mixed(), &Node::List(vec![]), same));
        assert!(!eq_tree(&chain(3), &chain(4), same));
        assert!(!eq_tree(&Node::Leaf(1), &Node::Leaf(2), same));
    }

    #[test]
    fn traverses_deep_nesting_on_a_small_stack() {
        std::thread::Builder::new()
            .stack_size(64 * 1024)
            .spawn(|| {
                let depth = 100_000;
                let node = chain(depth);
                let text = render(&node);
                assert_eq!(text, format!("{}0{}", "[".repeat(depth), "]".repeat(depth)));

                let copy = map_tree(
                    &node,
                    |node| match node {
                        Node::Leaf(n) => Node::Leaf(*n),
                        Node::List(_) => Node::List(vec![]),
                    },
                    Node::children_mut,
                );
                assert!(eq_tree(&node, &copy, |a, b| matches!(
                    (a, b),
                    (Node::Leaf(_), Node::Leaf(_)) | (Node::List(_), Node::List(_))
                )));
                drop(copy);
                drop(node);
            })
            .unwrap()
            .join()
            .unwrap();
    }
}