Struct simplicity::node::Node

source ·
pub struct Node<N: Marker> { /* private fields */ }
Expand description

A node in a Simplicity expression.

There are three node types provided by this library: ConstructNode, CommitNode, and RedeemNode, which represent Simplicty programs during construction, at commitment time, and at redemption time, respectively.

This generic structure is used to define conversions and mapping functions over nodes and DAGs, and allows users to define their own custom node types.

For equality and hashing purposes, nodes are characterized entirely by their CMR and cached data. Users who create custom nodes should define a custom type for Marker::CachedData and think carefully about whether and how to implement the std::hash::Hash or equality traits.

Implementations§

source§

impl<J: Jet> Node<Named<Commit<J>>>

source

pub fn from_node(root: &CommitNode<J>) -> Arc<Self>

source

pub fn name(&self) -> &Arc<str>

Accessor for the node’s name

source

pub fn imr(&self) -> Option<Imr>

Accessor for the node’s name

source

pub fn arrow(&self) -> &FinalArrow

Accessor for the node’s type arrow

source

pub fn to_commit_node(&self) -> Arc<CommitNode<J>>

Forget the names, yielding an ordinary CommitNode.

source

pub fn to_witness_node( &self, witness: &HashMap<Arc<str>, Arc<Value>>, disconnect: &HashMap<Arc<str>, Arc<NamedCommitNode<J>>> ) -> Arc<WitnessNode<J>>

source

pub fn encode<W: Write>(&self, w: &mut BitWriter<W>) -> Result<usize>

Encode a Simplicity expression to bits without any witness data

source

pub fn encode_to_vec(&self) -> Vec<u8>

Encode a Simplicity program to a vector of bytes, without any witness data.

source§

impl<J: Jet> Node<Named<Construct<J>>>

source

pub fn new( name: Arc<str>, position: Position, user_source_types: Arc<[Type]>, user_target_types: Arc<[Type]>, inner: Inner<Arc<Self>, J, Arc<Self>, WitnessOrHole> ) -> Result<Self, Error>

Construct a named construct node from parts.

source

pub fn renamed(&self, new_name: Arc<str>) -> Self

Creates a copy of a node with a different name.

source

pub fn name(&self) -> &Arc<str>

Accessor for the node’s name

source

pub fn position(&self) -> Position

Accessor for the node’s position

source

pub fn arrow(&self) -> &Arrow

Accessor for the node’s arrow

source

pub fn finalize_types_main(&self) -> Result<Arc<NamedCommitNode<J>>, ErrorSet>

Finalizes the types of the underlying [ConstructNode].

source

pub fn finalize_types_non_main( &self ) -> Result<Arc<NamedCommitNode<J>>, ErrorSet>

Finalizes the types of the underlying [ConstructNode], without setting the root node’s arrow to 1->1.

source

pub fn finalize_types_inner( &self, for_main: bool ) -> Result<Arc<NamedCommitNode<J>>, ErrorSet>

source§

impl<J: Jet> Node<Commit<J>>

source

pub fn arrow(&self) -> &FinalArrow

Accessor for the node’s arrow

source

pub fn amr(&self) -> Option<Amr>

Accessor for the node’s AMR, if known

source

pub fn imr(&self) -> Option<Imr>

Accessor for the node’s IMR, if known

source

pub fn finalize<C: Converter<Commit<J>, Redeem<J>>>( &self, converter: &mut C ) -> Result<Arc<RedeemNode<J>>, C::Error>

Finalizes a DAG, by iterating through through it without sharing, attaching witnesses, and hiding branches.

This is a thin wrapper around Node::convert which fixes a few types to make it easier to use.

source

pub fn unfinalize_types(&self) -> Result<Arc<ConstructNode<J>>, Error>

Convert a CommitNode back to a ConstructNode by redoing type inference

source

pub fn decode<I: Iterator<Item = u8>>( bits: &mut BitIter<I> ) -> Result<Arc<Self>, Error>

Decode a Simplicity program from bits, without witness data.

§Usage

Use this method only if the serialization does not include the witness data. This means, the program simply has no witness during commitment, or the witness is provided by other means.

If the serialization contains the witness data, then use RedeemNode::decode().

source

pub fn encode<W: Write>(&self, w: &mut BitWriter<W>) -> Result<usize>

Encode a Simplicity expression to bits without any witness data

source

pub fn encode_to_vec(&self) -> Vec<u8>

Encode a Simplicity program to a vector of bytes, without any witness data.

source§

impl<J: Jet> Node<Construct<J>>

source

pub fn arrow(&self) -> &Arrow

Accessor for the node’s arrow

source

pub fn set_arrow_to_program(&self) -> Result<(), Error>

Sets the source and target type of the node to unit

source

pub fn finalize_types(&self) -> Result<Arc<CommitNode<J>>, Error>

Convert a ConstructNode to a CommitNode by finalizing all of the types.

Also sets the source and target type of this node to unit. This is almost certainly what you want, since the resulting CommitNode cannot be further composed, and needs to be 1->1 to go on-chain. But if you don’t, call [Self::finalize_types_without_fixing] instead.

source

pub fn finalize_types_non_program(&self) -> Result<Arc<CommitNode<J>>, Error>

Convert a ConstructNode to a CommitNode by finalizing all of the types.

Does not sets the source and target type of this node to unit.

source

pub fn decode<I: Iterator<Item = u8>>( bits: &mut BitIter<I> ) -> Result<Arc<Self>, Error>

Decode a Simplicity expression from bits, without witness data.

§Usage

Use this method only if the serialization does not include the witness data. This means, the program simply has no witness during commitment, or the witness is provided by other means.

If the serialization contains the witness data, then use [RedeemNode::decode()].

source

pub fn encode<W: Write>(&self, w: &mut BitWriter<W>) -> Result<usize>

Encode a Simplicity expression to bits, with no witness data

source§

impl<J: Jet> Node<Redeem<J>>

source

pub fn amr(&self) -> Amr

Accessor for the node’s AMR

source

pub fn imr(&self) -> Imr

Accessor for the node’s IMR

source

pub fn arrow(&self) -> &FinalArrow

Accessor for the node’s type arrow

source

pub fn bounds(&self) -> NodeBounds

Accessor for the node’s bit machine bounds

source

pub fn unfinalize(&self) -> Result<Arc<CommitNode<J>>, Error>

Convert a RedeemNode back to a CommitNode by forgetting witnesses and cached data.

source

pub fn to_witness_node(&self) -> Arc<WitnessNode<J>>

Convert a RedeemNode back into a WitnessNode by loosening the finalized types, witness data and disconnected branches.

source

pub fn decode<I: Iterator<Item = u8>>( bits: &mut BitIter<I> ) -> Result<Arc<Self>, Error>

Decode a Simplicity program from bits, including the witness data.

source

pub fn encode<W: Write>(&self, w: &mut BitWriter<W>) -> Result<usize>

Encode the program to bits.

Includes witness data. Returns the number of written bits.

source

pub fn encode_to_vec(&self) -> Vec<u8>

Encode the program to a byte vector.

Includes witness data.

source§

impl<J: Jet> Node<Witness<J>>

source

pub fn pruned(&self) -> Arc<Self>

Creates a copy of the node (and its entire DAG with the prune bit set)

source

pub fn arrow(&self) -> &Arrow

Accessor for the node’s arrow

source

pub fn must_prune(&self) -> bool

Whether the “must prune” bit is set on this node

source

pub fn prune_and_retype(&self) -> Arc<Self>

source

pub fn finalize(&self) -> Result<Arc<RedeemNode<J>>, Error>

source§

impl<N: Marker> Node<N>

source

pub fn inner(&self) -> &Inner<Arc<Node<N>>, N::Jet, N::Disconnect, N::Witness>

Accessor for the node’s “inner value”, i.e. its combinator

source

pub fn cmr(&self) -> Cmr

Accessor for the node’s CMR

source

pub fn sharing_id(&self) -> Option<N::SharingId>

Accessor for the node’s cached data

source

pub fn cached_data(&self) -> &N::CachedData

Accessor for the node’s cached data

source

pub fn from_parts( inner: Inner<Arc<Self>, N::Jet, N::Disconnect, N::Witness>, data: N::CachedData ) -> Self

Contruct a node from its constituent parts.

This method can be used to directly costruct a node. It will compute the CMR automatically based on the value of inner but requires that cached_data be provided.

If available, [`Constructible’] and its dependent traits will be easier to use.

source

pub fn convert<S, M, C>( &self, converter: &mut C ) -> Result<Arc<Node<M>>, C::Error>
where S: for<'a> SharingTracker<&'a Self> + Default, M: Marker<Jet = <N as Marker>::Jet>, C: Converter<N, M>,

Generic conversion function from one type of node to another, with the ability to prune during the conversion.

Parameterized over what kind of sharing to use when iterating over the DAG, and what conversion logic to use.

See the documentation for Converter for details.

Trait Implementations§

source§

impl<'a, N: Marker> DagLike for &'a Node<N>

§

type Node = Node<N>

The type of the DAG node, with no references or wrappers
source§

fn data(&self) -> &Node<N>

A pointer to the underlying data
source§

fn as_dag_node(&self) -> Dag<Self>

Interpret the node as a DAG node
source§

fn left_child(&self) -> Option<Self>

Accessor for the left child of the node, if one exists
source§

fn right_child(&self) -> Option<Self>

Accessor for the right child of the node, if one exists
source§

fn n_children(&self) -> usize

Number of children that the node has. Read more
source§

fn rtl_post_order_iter<S: SharingTracker<SwapChildren<Self>> + Default>( self ) -> RtlPostOrderIter<Self, S>

Obtains an iterator of all the nodes rooted at the DAG, in right-to-left post order. Read more
source§

fn pre_order_iter<S: SharingTracker<Self> + Default>( self ) -> PreOrderIter<Self, S>

Obtains an iterator of all the nodes rooted at the DAG, in pre-order.
source§

fn verbose_pre_order_iter<S: SharingTracker<Self> + Default>( self ) -> VerbosePreOrderIter<Self, S>
where Self: Clone,

Obtains a verbose iterator of all the nodes rooted at the DAG, in pre-order. Read more
source§

fn post_order_iter<S: SharingTracker<Self> + Default>( self ) -> PostOrderIter<Self, S>

Obtains an iterator of all the nodes rooted at the DAG, in post order. Read more
source§

fn is_shared_as<S: SharingTracker<Self> + Default>(self) -> bool
where Self: Clone,

Checks whether a DAG’s internal sharing (as expressed by shared pointers) matches the given target sharing.
source§

fn post_order_iter_with_tracker<S: SharingTracker<Self>>( self, tracker: S ) -> PostOrderIter<Self, S>

Obtains an post-order iterator of all the nodes rooted at DAG, using the given tracker. Read more
source§

impl<N: Marker> Debug for Node<N>
where for<'a> &'a Node<N>: DagLike,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<N: Marker> Display for Node<N>
where for<'a> &'a Node<N>: DagLike,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<N: Marker> Hash for Node<N>
where N::CachedData: Hash,

source§

fn hash<H: Hasher>(&self, h: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<N: Marker> PartialEq for Node<N>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<N: Marker> SharingTracker<&Node<N>> for MaxSharing<N>

source§

fn record(&mut self, d: &&Node<N>, index: usize) -> Option<usize>

Marks an object as having been seen, and record the index when it was seen. Read more
source§

fn seen_before(&self, d: &&Node<N>) -> Option<usize>

Check whether an object has been seen before; if so, return the index it was recorded at.
source§

impl<N: Marker> Eq for Node<N>
where N::CachedData: Eq,

Auto Trait Implementations§

§

impl<N> RefUnwindSafe for Node<N>

§

impl<N> Send for Node<N>
where <N as Marker>::CachedData: Sync + Send, <N as Marker>::Disconnect: Sync + Send, <N as Marker>::Jet: Sync + Send, <N as Marker>::Witness: Sync + Send,

§

impl<N> Sync for Node<N>
where <N as Marker>::CachedData: Sync + Send, <N as Marker>::Disconnect: Sync + Send, <N as Marker>::Jet: Sync + Send, <N as Marker>::Witness: Sync + Send,

§

impl<N> Unpin for Node<N>
where <N as Marker>::CachedData: Unpin, <N as Marker>::Disconnect: Unpin, <N as Marker>::Jet: Unpin, <N as Marker>::Witness: Unpin,

§

impl<N> UnwindSafe for Node<N>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V