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 Node<Commit>
impl Node<Commit>
Sourcepub fn arrow(&self) -> &FinalArrow
pub fn arrow(&self) -> &FinalArrow
Accessor for the node’s arrow
Sourcepub fn finalize<C: Converter<Commit, Redeem>>(
&self,
converter: &mut C,
) -> Result<Arc<RedeemNode>, C::Error>
pub fn finalize<C: Converter<Commit, Redeem>>( &self, converter: &mut C, ) -> Result<Arc<RedeemNode>, 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.
Sourcepub fn unfinalize_types<'brand>(
&self,
inference_context: &Context<'brand>,
) -> Result<Arc<ConstructNode<'brand>>, Error>
pub fn unfinalize_types<'brand>( &self, inference_context: &Context<'brand>, ) -> Result<Arc<ConstructNode<'brand>>, Error>
Convert a CommitNode back to a ConstructNode by redoing type inference
Sourcepub fn decode<I: Iterator<Item = u8>, J: Jet>(
bits: BitIter<I>,
) -> Result<Arc<Self>, DecodeError>
pub fn decode<I: Iterator<Item = u8>, J: Jet>( bits: BitIter<I>, ) -> Result<Arc<Self>, DecodeError>
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().
Sourcepub fn encode(&self, w: &mut BitWriter<&mut dyn Write>) -> Result<usize>
👎Deprecated since 0.5.0: use Self::encode_without_witness instead
pub fn encode(&self, w: &mut BitWriter<&mut dyn Write>) -> Result<usize>
use Self::encode_without_witness instead
Encode a Simplicity expression to bits without any witness data
Sourcepub fn encode_to_vec(&self) -> Vec<u8> ⓘ
👎Deprecated since 0.5.0: use Self::to_vec_without_witness instead
pub fn encode_to_vec(&self) -> Vec<u8> ⓘ
use Self::to_vec_without_witness instead
Encode a Simplicity program to a vector of bytes, without any witness data.
Source§impl<'brand> Node<Construct<'brand>>
impl<'brand> Node<Construct<'brand>>
Sourcepub fn set_arrow_to_program(&self) -> Result<(), Error>
pub fn set_arrow_to_program(&self) -> Result<(), Error>
Sets the source and target type of the node to unit
Sourcepub fn finalize_types(&self) -> Result<Arc<CommitNode>, Error>
pub fn finalize_types(&self) -> Result<Arc<CommitNode>, 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_non_program instead.
Sourcepub fn finalize_types_non_program(&self) -> Result<Arc<CommitNode>, Error>
pub fn finalize_types_non_program(&self) -> Result<Arc<CommitNode>, 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.
Sourcepub fn finalize_unpruned(&self) -> Result<Arc<RedeemNode>, FinalizeError>
pub fn finalize_unpruned(&self) -> Result<Arc<RedeemNode>, FinalizeError>
Finalize the witness program as an unpruned redeem program.
Witness nodes must be populated with sufficient data, to ensure that the resulting redeem program successfully runs on the Bit Machine. Furthermore, all disconnected branches must be populated, even those that are not executed.
The resulting redeem program is not pruned.
§See
Sourcepub fn finalize_pruned<JE: JetEnvironment>(
&self,
env: &JE,
) -> Result<Arc<RedeemNode>, FinalizeError>
pub fn finalize_pruned<JE: JetEnvironment>( &self, env: &JE, ) -> Result<Arc<RedeemNode>, FinalizeError>
Finalize the witness program as a pruned redeem program.
Witness nodes must be populated with sufficient data, to ensure that the resulting redeem program successfully runs on the Bit Machine. Furthermore, all disconnected branches must be populated, even those that are not executed.
The resulting redeem program is pruned based on the given transaction environment.
§See
Sourcepub fn decode<I: Iterator<Item = u8>, J: Jet>(
context: &Context<'brand>,
bits: BitIter<I>,
) -> Result<Arc<Self>, Error>
pub fn decode<I: Iterator<Item = u8>, J: Jet>( context: &Context<'brand>, bits: 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 crate::RedeemNode::decode().
Source§impl Node<Redeem>
impl Node<Redeem>
Sourcepub fn arrow(&self) -> &FinalArrow
pub fn arrow(&self) -> &FinalArrow
Accessor for the node’s type arrow
Sourcepub fn bounds(&self) -> NodeBounds
pub fn bounds(&self) -> NodeBounds
Accessor for the node’s bit machine bounds
Sourcepub fn unfinalize(&self) -> Result<Arc<CommitNode>, Error>
pub fn unfinalize(&self) -> Result<Arc<CommitNode>, Error>
Convert a RedeemNode back to a CommitNode by forgetting witnesses
and cached data.
Sourcepub fn to_construct_node<'brand>(
&self,
inference_context: &Context<'brand>,
) -> Arc<ConstructNode<'brand>> ⓘ
pub fn to_construct_node<'brand>( &self, inference_context: &Context<'brand>, ) -> Arc<ConstructNode<'brand>> ⓘ
Convert a RedeemNode back into a ConstructNode
by loosening the finalized types, witness data and disconnected branches.
Sourcepub fn prune<JE: JetEnvironment>(
&self,
env: &JE,
) -> Result<Arc<RedeemNode>, ExecutionError>
pub fn prune<JE: JetEnvironment>( &self, env: &JE, ) -> Result<Arc<RedeemNode>, ExecutionError>
Prune the redeem program for the given transaction environment.
Pruning works as follows:
- Run the redeem program on the Bit Machine.
- Mark all (un)used case branches using the IHR of the case node.
- Rebuild the program and omit unused branches.
The pruning result depends on the witness data (which is already part of the redeem program) and on the transaction environment. These two inputs determine which case branches are used and which are not. Pruning must be done for each transaction environment separately, starting from the same original, unpruned program. Pruning is a lossy process, so pruning an already pruned program is not sound.
Pruning fails if the original, unpruned program fails to run on the Bit Machine (step 1). In this case, the witness data needs to be revised. The other pruning steps (2 & 3) never fail.
Sourcepub fn prune_with_tracker<JE: JetEnvironment, T: PruneTracker>(
&self,
env: &JE,
tracker: &mut T,
) -> Result<Arc<RedeemNode>, ExecutionError>
pub fn prune_with_tracker<JE: JetEnvironment, T: PruneTracker>( &self, env: &JE, tracker: &mut T, ) -> Result<Arc<RedeemNode>, ExecutionError>
Prune the redeem program, as in Self::prune, but with a custom tracker which
can introspect or control pruning.
See crate::bit_machine::StderrTracker as an example which outputs the IHR of
each case combinator that we prune a child of.
Sourcepub fn decode<I1, I2, J: Jet>(
program: BitIter<I1>,
witness: BitIter<I2>,
) -> Result<Arc<Self>, DecodeError>
pub fn decode<I1, I2, J: Jet>( program: BitIter<I1>, witness: BitIter<I2>, ) -> Result<Arc<Self>, DecodeError>
Decode a Simplicity program from bits, including the witness data.
Source§impl<N: Marker> Node<N>
impl<N: Marker> Node<N>
Sourcepub fn inner(&self) -> &Inner<Arc<Node<N>>, N::Disconnect, N::Witness>
pub fn inner(&self) -> &Inner<Arc<Node<N>>, N::Disconnect, N::Witness>
Accessor for the node’s “inner value”, i.e. its combinator
Sourcepub fn sharing_id(&self) -> Option<N::SharingId>
pub fn sharing_id(&self) -> Option<N::SharingId>
Accessor for the node’s cached data
Sourcepub fn cached_data(&self) -> &N::CachedData
pub fn cached_data(&self) -> &N::CachedData
Accessor for the node’s cached data
Sourcepub fn from_parts(
inner: Inner<Arc<Self>, N::Disconnect, N::Witness>,
data: N::CachedData,
) -> Self
pub fn from_parts( inner: Inner<Arc<Self>, 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.
Sourcepub fn convert<S, M, C>(
&self,
converter: &mut C,
) -> Result<Arc<Node<M>>, C::Error>
pub fn convert<S, M, C>( &self, converter: &mut C, ) -> Result<Arc<Node<M>>, C::Error>
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.
pub fn display(&self) -> Display<'_, N>
Sourcepub fn display_expr(&self) -> DisplayExpr<'_, N>
pub fn display_expr(&self) -> DisplayExpr<'_, N>
Display the Simplicity expression as a linear string.
The linear string has no sharing and may be exponentially larger than the originally shared expression!
Sourcepub fn display_as_graph(
&self,
format: GraphFormat,
sharing_level: SharingLevel,
) -> DisplayAsGraph<'_, N>
pub fn display_as_graph( &self, format: GraphFormat, sharing_level: SharingLevel, ) -> DisplayAsGraph<'_, N>
Display the Simplicity expression as a graph in the given format and sharing level.
This is the general form of display_as_dot and
display_as_mermaid. Use those convenience methods for
the common case of DOT or Mermaid output with no sharing.
The format field of the returned DisplayAsGraph can be changed after construction,
and the fmt::Display impl will use whatever format and sharing are set at that
point. See also DisplayAsGraph::to_dot_string and DisplayAsGraph::to_mermaid_string
to render to a specific format regardless of the stored format field.
Sourcepub fn display_as_dot(&self) -> DisplayAsGraph<'_, N>
pub fn display_as_dot(&self) -> DisplayAsGraph<'_, N>
Display the Simplicity expression as a Graphviz DOT graph.
The DOT output can be rendered with dot -Tsvg or similar tools.
Shared nodes appear once in the graph with multiple incoming edges.
Sourcepub fn display_as_mermaid(&self) -> DisplayAsGraph<'_, N>
pub fn display_as_mermaid(&self) -> DisplayAsGraph<'_, N>
Display the Simplicity expression as a Mermaid diagram.
The Mermaid output can be rendered in Markdown or the Mermaid live editor. Shared nodes appear once in the diagram with multiple incoming edges.
Sourcepub fn encode_without_witness(&self, prog: &mut dyn Write) -> Result<usize>
pub fn encode_without_witness(&self, prog: &mut dyn Write) -> Result<usize>
Encode a Simplicity expression to bits without any witness data.
Sourcepub fn to_vec_without_witness(&self) -> Vec<u8> ⓘ
pub fn to_vec_without_witness(&self) -> Vec<u8> ⓘ
Encode a Simplicity expression to a vector of bytes, without any witness data.