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>>>
impl<J: Jet> Node<Named<Commit<J>>>
pub fn from_node(root: &CommitNode<J>) -> Arc<Self>
Sourcepub fn arrow(&self) -> &FinalArrow
pub fn arrow(&self) -> &FinalArrow
Accessor for the node’s type arrow
Sourcepub fn to_commit_node(&self) -> Arc<CommitNode<J>> ⓘ
pub fn to_commit_node(&self) -> Arc<CommitNode<J>> ⓘ
Forget the names, yielding an ordinary CommitNode
.
pub fn to_construct_node( &self, witness: &HashMap<Arc<str>, Value>, disconnect: &HashMap<Arc<str>, Arc<NamedCommitNode<J>>>, ) -> Arc<ConstructNode<J>> ⓘ
Sourcepub fn encode<W: Write>(&self, w: &mut BitWriter<W>) -> Result<usize>
👎Deprecated since 0.5.0: use Self::encode_without_witness instead
pub fn encode<W: Write>(&self, w: &mut BitWriter<W>) -> Result<usize>
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> ⓘ
Encode a Simplicity program to a vector of bytes, without any witness data.
Source§impl<J: Jet> Node<Named<Construct<J>>>
impl<J: Jet> Node<Named<Construct<J>>>
Sourcepub fn new(
inference_context: &Context,
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>
pub fn new( inference_context: &Context, 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.
Sourcepub fn renamed(&self, new_name: Arc<str>) -> Self
pub fn renamed(&self, new_name: Arc<str>) -> Self
Creates a copy of a node with a different name.
Sourcepub fn inference_context(&self) -> &Context
pub fn inference_context(&self) -> &Context
Accessor for the node’s type inference context.
Sourcepub fn finalize_types_main(&self) -> Result<Arc<NamedCommitNode<J>>, ErrorSet>
pub fn finalize_types_main(&self) -> Result<Arc<NamedCommitNode<J>>, ErrorSet>
Finalizes the types of the underlying crate::ConstructNode
.
Sourcepub fn finalize_types_non_main(
&self,
) -> Result<Arc<NamedCommitNode<J>>, ErrorSet>
pub fn finalize_types_non_main( &self, ) -> Result<Arc<NamedCommitNode<J>>, ErrorSet>
Finalizes the types of the underlying crate::ConstructNode
, without setting
the root node’s arrow to 1->1.
pub fn finalize_types_inner( &self, for_main: bool, ) -> Result<Arc<NamedCommitNode<J>>, ErrorSet>
Source§impl<J: Jet> Node<Commit<J>>
impl<J: Jet> Node<Commit<J>>
Sourcepub fn arrow(&self) -> &FinalArrow
pub fn arrow(&self) -> &FinalArrow
Accessor for the node’s arrow
Sourcepub fn finalize<C: Converter<Commit<J>, Redeem<J>>>(
&self,
converter: &mut C,
) -> Result<Arc<RedeemNode<J>>, C::Error>
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.
Sourcepub fn unfinalize_types(&self) -> Result<Arc<ConstructNode<J>>, Error>
pub fn unfinalize_types(&self) -> Result<Arc<ConstructNode<J>>, Error>
Convert a CommitNode
back to a ConstructNode
by redoing type inference
Sourcepub fn decode<I: Iterator<Item = u8>>(
bits: BitIter<I>,
) -> Result<Arc<Self>, DecodeError>
pub fn decode<I: Iterator<Item = u8>>( 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<W: Write>(&self, w: &mut BitWriter<W>) -> Result<usize>
👎Deprecated since 0.5.0: use Self::encode_without_witness instead
pub fn encode<W: Write>(&self, w: &mut BitWriter<W>) -> Result<usize>
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> ⓘ
Encode a Simplicity program to a vector of bytes, without any witness data.
Source§impl<J: Jet> Node<Construct<J>>
impl<J: Jet> Node<Construct<J>>
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<J>>, Error>
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_non_program
instead.
Sourcepub fn finalize_types_non_program(&self) -> Result<Arc<CommitNode<J>>, Error>
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.
Sourcepub fn finalize_unpruned(&self) -> Result<Arc<RedeemNode<J>>, FinalizeError>
pub fn finalize_unpruned(&self) -> Result<Arc<RedeemNode<J>>, 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(
&self,
env: &J::Environment,
) -> Result<Arc<RedeemNode<J>>, FinalizeError>
pub fn finalize_pruned( &self, env: &J::Environment, ) -> Result<Arc<RedeemNode<J>>, 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>>(
bits: BitIter<I>,
) -> Result<Arc<Self>, Error>
pub fn decode<I: Iterator<Item = u8>>( 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<J: Jet> Node<Redeem<J>>
impl<J: Jet> Node<Redeem<J>>
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<J>>, Error>
pub fn unfinalize(&self) -> Result<Arc<CommitNode<J>>, Error>
Convert a RedeemNode
back to a CommitNode
by forgetting witnesses
and cached data.
Sourcepub fn to_construct_node(&self) -> Arc<ConstructNode<J>> ⓘ
pub fn to_construct_node(&self) -> Arc<ConstructNode<J>> ⓘ
Convert a RedeemNode
back into a ConstructNode
by loosening the finalized types, witness data and disconnected branches.
Sourcepub fn prune(
&self,
env: &J::Environment,
) -> Result<Arc<RedeemNode<J>>, ExecutionError>
pub fn prune( &self, env: &J::Environment, ) -> Result<Arc<RedeemNode<J>>, 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 decode<I1, I2>(
program: BitIter<I1>,
witness: BitIter<I2>,
) -> Result<Arc<Self>, DecodeError>
pub fn decode<I1, I2>( 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::Jet, N::Disconnect, N::Witness>
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
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::Jet, N::Disconnect, N::Witness>,
data: N::CachedData,
) -> Self
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.
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 encode_without_witness<W: Write>(&self, prog: W) -> Result<usize>
pub fn encode_without_witness<W: Write>(&self, prog: W) -> 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.