Bitcoin Core 28.99.0
P2P Digital Currency
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
miniscript::Node< Key > Struct Template Reference

A node in a miniscript expression. More...

#include <miniscript.h>

Collaboration diagram for miniscript::Node< Key >:
[legend]

Public Member Functions

 ~Node ()
 
template<typename Ctx >
CScript ToScript (const Ctx &ctx) const
 
template<typename CTx >
std::optional< std::string > ToString (const CTx &ctx) const
 
template<typename Ctx >
void DuplicateKeyCheck (const Ctx &ctx) const
 Update duplicate key information in this Node. More...
 
size_t ScriptSize () const
 Return the size of the script for this expression (faster than ToScript().size()). More...
 
std::optional< uint32_t > GetOps () const
 Return the maximum number of ops needed to satisfy this script non-malleably. More...
 
uint32_t GetStaticOps () const
 Return the number of ops in the script (not counting the dynamic ones that depend on execution). More...
 
bool CheckOpsLimit () const
 Check the ops limit of this script against the consensus limit. More...
 
bool IsBKW () const
 Whether this node is of type B, K or W. More...
 
std::optional< uint32_t > GetStackSize () const
 Return the maximum number of stack elements needed to satisfy this script non-malleably. More...
 
std::optional< uint32_t > GetExecStackSize () const
 Return the maximum size of the stack during execution of this script. More...
 
bool CheckStackSize () const
 Check the maximum stack size for this script against the policy limit. More...
 
bool IsNotSatisfiable () const
 Whether no satisfaction exists for this node. More...
 
std::optional< uint32_t > GetWitnessSize () const
 Return the maximum size in bytes of a witness to satisfy this script non-malleably. More...
 
Type GetType () const
 Return the expression type. More...
 
MiniscriptContext GetMsCtx () const
 Return the script context for this node. More...
 
const NodeFindInsaneSub () const
 Find an insane subnode which has no insane children. Nullptr if there is none. More...
 
template<typename F >
bool IsSatisfiable (F fn) const
 Determine whether a Miniscript node is satisfiable. More...
 
bool IsValid () const
 Check whether this node is valid at all. More...
 
bool IsValidTopLevel () const
 Check whether this node is valid as a script on its own. More...
 
bool IsNonMalleable () const
 Check whether this script can always be satisfied in a non-malleable way. More...
 
bool NeedsSignature () const
 Check whether this script always needs a signature. More...
 
bool CheckTimeLocksMix () const
 Check whether there is no satisfaction path that contains both timelocks and heightlocks. More...
 
bool CheckDuplicateKey () const
 Check whether there is no duplicate key across this fragment and all its sub-fragments. More...
 
bool ValidSatisfactions () const
 Whether successful non-malleable satisfactions are guaranteed to be valid. More...
 
bool IsSaneSubexpression () const
 Whether the apparent policy of this node matches its script semantics. Doesn't guarantee it is a safe script on its own. More...
 
bool IsSane () const
 Check whether this node is safe as a script on its own. More...
 
template<typename Ctx >
Availability Satisfy (const Ctx &ctx, std::vector< std::vector< unsigned char > > &stack, bool nonmalleable=true) const
 Produce a witness for this script, if possible and given the information available in the context. More...
 
bool operator== (const Node< Key > &arg) const
 Equality testing. More...
 
 Node (internal::NoDupCheck, MiniscriptContext script_ctx, Fragment nt, std::vector< NodeRef< Key > > sub, std::vector< unsigned char > arg, uint32_t val=0)
 
 Node (internal::NoDupCheck, MiniscriptContext script_ctx, Fragment nt, std::vector< unsigned char > arg, uint32_t val=0)
 
 Node (internal::NoDupCheck, MiniscriptContext script_ctx, Fragment nt, std::vector< NodeRef< Key > > sub, std::vector< Key > key, uint32_t val=0)
 
 Node (internal::NoDupCheck, MiniscriptContext script_ctx, Fragment nt, std::vector< Key > key, uint32_t val=0)
 
 Node (internal::NoDupCheck, MiniscriptContext script_ctx, Fragment nt, std::vector< NodeRef< Key > > sub, uint32_t val=0)
 
 Node (internal::NoDupCheck, MiniscriptContext script_ctx, Fragment nt, uint32_t val=0)
 
template<typename Ctx >
 Node (const Ctx &ctx, Fragment nt, std::vector< NodeRef< Key > > sub, std::vector< unsigned char > arg, uint32_t val=0)
 
template<typename Ctx >
 Node (const Ctx &ctx, Fragment nt, std::vector< unsigned char > arg, uint32_t val=0)
 
template<typename Ctx >
 Node (const Ctx &ctx, Fragment nt, std::vector< NodeRef< Key > > sub, std::vector< Key > key, uint32_t val=0)
 
template<typename Ctx >
 Node (const Ctx &ctx, Fragment nt, std::vector< Key > key, uint32_t val=0)
 
template<typename Ctx >
 Node (const Ctx &ctx, Fragment nt, std::vector< NodeRef< Key > > sub, uint32_t val=0)
 
template<typename Ctx >
 Node (const Ctx &ctx, Fragment nt, uint32_t val=0)
 

Public Attributes

const Fragment fragment
 What node type this node is. More...
 
const uint32_t k = 0
 The k parameter (time for OLDER/AFTER, threshold for THRESH(_M)) More...
 
const std::vector< Key > keys
 The keys used by this expression (only for PK_K/PK_H/MULTI) More...
 
const std::vector< unsigned char > data
 The data bytes in this expression (only for HASH160/HASH256/SHA256/RIPEMD10). More...
 
std::vector< NodeRef< Key > > subs
 Subexpressions (for WRAP_*‍/AND_*‍/OR_*‍/ANDOR/THRESH) More...
 
const MiniscriptContext m_script_ctx
 The Script context for this node. Either P2WSH or Tapscript. More...
 

Private Member Functions

size_t CalcScriptLen () const
 Compute the length of the script for this miniscript (including children). More...
 
template<typename Result , typename State , typename DownFn , typename UpFn >
std::optional< Result > TreeEvalMaybe (State root_state, DownFn downfn, UpFn upfn) const
 
template<typename Result , typename UpFn >
std::optional< Result > TreeEvalMaybe (UpFn upfn) const
 Like TreeEvalMaybe, but without downfn or State type. More...
 
template<typename Result , typename State , typename DownFn , typename UpFn >
Result TreeEval (State root_state, DownFn &&downfn, UpFn upfn) const
 Like TreeEvalMaybe, but always produces a result. More...
 
template<typename Result , typename UpFn >
Result TreeEval (UpFn upfn) const
 Like TreeEval, but without downfn or State type. More...
 
Type CalcType () const
 Compute the type for this miniscript. More...
 
internal::Ops CalcOps () const
 
internal::StackSize CalcStackSize () const
 
internal::WitnessSize CalcWitnessSize () const
 
template<typename Ctx >
internal::InputResult ProduceInput (const Ctx &ctx) const
 

Private Attributes

const internal::Ops ops
 Cached ops counts. More...
 
const internal::StackSize ss
 Cached stack size bounds. More...
 
const internal::WitnessSize ws
 Cached witness size bounds. More...
 
const Type typ
 Cached expression type (computed by CalcType and fed through SanitizeType). More...
 
const size_t scriptlen
 Cached script length (computed by CalcScriptLen). More...
 
std::optional< bool > has_duplicate_keys
 Whether a public key appears more than once in this node. More...
 

Friends

int Compare (const Node< Key > &node1, const Node< Key > &node2)
 Compare two miniscript subtrees, using a non-recursive algorithm. More...
 

Detailed Description

template<typename Key>
struct miniscript::Node< Key >

A node in a miniscript expression.

Definition at line 504 of file miniscript.h.

Constructor & Destructor Documentation

◆ ~Node()

template<typename Key >
miniscript::Node< Key >::~Node ( )
inline

Definition at line 520 of file miniscript.h.

◆ Node() [1/12]

template<typename Key >
miniscript::Node< Key >::Node ( internal::NoDupCheck  ,
MiniscriptContext  script_ctx,
Fragment  nt,
std::vector< NodeRef< Key > >  sub,
std::vector< unsigned char >  arg,
uint32_t  val = 0 
)
inline

Definition at line 1640 of file miniscript.h.

◆ Node() [2/12]

template<typename Key >
miniscript::Node< Key >::Node ( internal::NoDupCheck  ,
MiniscriptContext  script_ctx,
Fragment  nt,
std::vector< unsigned char >  arg,
uint32_t  val = 0 
)
inline

Definition at line 1642 of file miniscript.h.

◆ Node() [3/12]

template<typename Key >
miniscript::Node< Key >::Node ( internal::NoDupCheck  ,
MiniscriptContext  script_ctx,
Fragment  nt,
std::vector< NodeRef< Key > >  sub,
std::vector< Key >  key,
uint32_t  val = 0 
)
inline

Definition at line 1644 of file miniscript.h.

◆ Node() [4/12]

template<typename Key >
miniscript::Node< Key >::Node ( internal::NoDupCheck  ,
MiniscriptContext  script_ctx,
Fragment  nt,
std::vector< Key >  key,
uint32_t  val = 0 
)
inline

Definition at line 1646 of file miniscript.h.

◆ Node() [5/12]

template<typename Key >
miniscript::Node< Key >::Node ( internal::NoDupCheck  ,
MiniscriptContext  script_ctx,
Fragment  nt,
std::vector< NodeRef< Key > >  sub,
uint32_t  val = 0 
)
inline

Definition at line 1648 of file miniscript.h.

◆ Node() [6/12]

template<typename Key >
miniscript::Node< Key >::Node ( internal::NoDupCheck  ,
MiniscriptContext  script_ctx,
Fragment  nt,
uint32_t  val = 0 
)
inline

Definition at line 1650 of file miniscript.h.

◆ Node() [7/12]

template<typename Key >
template<typename Ctx >
miniscript::Node< Key >::Node ( const Ctx &  ctx,
Fragment  nt,
std::vector< NodeRef< Key > >  sub,
std::vector< unsigned char >  arg,
uint32_t  val = 0 
)
inline

Definition at line 1654 of file miniscript.h.

◆ Node() [8/12]

template<typename Key >
template<typename Ctx >
miniscript::Node< Key >::Node ( const Ctx &  ctx,
Fragment  nt,
std::vector< unsigned char >  arg,
uint32_t  val = 0 
)
inline

Definition at line 1656 of file miniscript.h.

◆ Node() [9/12]

template<typename Key >
template<typename Ctx >
miniscript::Node< Key >::Node ( const Ctx &  ctx,
Fragment  nt,
std::vector< NodeRef< Key > >  sub,
std::vector< Key >  key,
uint32_t  val = 0 
)
inline

Definition at line 1658 of file miniscript.h.

◆ Node() [10/12]

template<typename Key >
template<typename Ctx >
miniscript::Node< Key >::Node ( const Ctx &  ctx,
Fragment  nt,
std::vector< Key >  key,
uint32_t  val = 0 
)
inline

Definition at line 1660 of file miniscript.h.

◆ Node() [11/12]

template<typename Key >
template<typename Ctx >
miniscript::Node< Key >::Node ( const Ctx &  ctx,
Fragment  nt,
std::vector< NodeRef< Key > >  sub,
uint32_t  val = 0 
)
inline

Definition at line 1662 of file miniscript.h.

◆ Node() [12/12]

template<typename Key >
template<typename Ctx >
miniscript::Node< Key >::Node ( const Ctx &  ctx,
Fragment  nt,
uint32_t  val = 0 
)
inline

Definition at line 1664 of file miniscript.h.

Member Function Documentation

◆ CalcOps()

template<typename Key >
internal::Ops miniscript::Node< Key >::CalcOps ( ) const
inlineprivate

Definition at line 925 of file miniscript.h.

Here is the call graph for this function:

◆ CalcScriptLen()

template<typename Key >
size_t miniscript::Node< Key >::CalcScriptLen ( ) const
inlineprivate

Compute the length of the script for this miniscript (including children).

Definition at line 551 of file miniscript.h.

Here is the call graph for this function:

◆ CalcStackSize()

template<typename Key >
internal::StackSize miniscript::Node< Key >::CalcStackSize ( ) const
inlineprivate

Definition at line 999 of file miniscript.h.

Here is the call graph for this function:

◆ CalcType()

template<typename Key >
Type miniscript::Node< Key >::CalcType ( ) const
inlineprivate

Compute the type for this miniscript.

Definition at line 712 of file miniscript.h.

Here is the call graph for this function:

◆ CalcWitnessSize()

template<typename Key >
internal::WitnessSize miniscript::Node< Key >::CalcWitnessSize ( ) const
inlineprivate

Definition at line 1114 of file miniscript.h.

Here is the call graph for this function:

◆ CheckDuplicateKey()

template<typename Key >
bool miniscript::Node< Key >::CheckDuplicateKey ( ) const
inline

Check whether there is no duplicate key across this fragment and all its sub-fragments.

Definition at line 1613 of file miniscript.h.

◆ CheckOpsLimit()

template<typename Key >
bool miniscript::Node< Key >::CheckOpsLimit ( ) const
inline

Check the ops limit of this script against the consensus limit.

Definition at line 1492 of file miniscript.h.

Here is the call graph for this function:

◆ CheckStackSize()

template<typename Key >
bool miniscript::Node< Key >::CheckStackSize ( ) const
inline

Check the maximum stack size for this script against the policy limit.

Definition at line 1516 of file miniscript.h.

Here is the call graph for this function:

◆ CheckTimeLocksMix()

template<typename Key >
bool miniscript::Node< Key >::CheckTimeLocksMix ( ) const
inline

Check whether there is no satisfaction path that contains both timelocks and heightlocks.

Definition at line 1610 of file miniscript.h.

◆ DuplicateKeyCheck()

template<typename Key >
template<typename Ctx >
void miniscript::Node< Key >::DuplicateKeyCheck ( const Ctx &  ctx) const
inline

Update duplicate key information in this Node.

This uses a custom key comparator provided by the context in order to still detect duplicates for more complicated types.

Definition at line 1421 of file miniscript.h.

◆ FindInsaneSub()

template<typename Key >
const Node * miniscript::Node< Key >::FindInsaneSub ( ) const
inline

Find an insane subnode which has no insane children. Nullptr if there is none.

Definition at line 1544 of file miniscript.h.

◆ GetExecStackSize()

template<typename Key >
std::optional< uint32_t > miniscript::Node< Key >::GetExecStackSize ( ) const
inline

Return the maximum size of the stack during execution of this script.

Definition at line 1510 of file miniscript.h.

◆ GetMsCtx()

template<typename Key >
MiniscriptContext miniscript::Node< Key >::GetMsCtx ( ) const
inline

Return the script context for this node.

Definition at line 1541 of file miniscript.h.

◆ GetOps()

template<typename Key >
std::optional< uint32_t > miniscript::Node< Key >::GetOps ( ) const
inline

Return the maximum number of ops needed to satisfy this script non-malleably.

Definition at line 1483 of file miniscript.h.

◆ GetStackSize()

template<typename Key >
std::optional< uint32_t > miniscript::Node< Key >::GetStackSize ( ) const
inline

Return the maximum number of stack elements needed to satisfy this script non-malleably.

Definition at line 1504 of file miniscript.h.

◆ GetStaticOps()

template<typename Key >
uint32_t miniscript::Node< Key >::GetStaticOps ( ) const
inline

Return the number of ops in the script (not counting the dynamic ones that depend on execution).

Definition at line 1489 of file miniscript.h.

◆ GetType()

template<typename Key >
Type miniscript::Node< Key >::GetType ( ) const
inline

Return the expression type.

Definition at line 1538 of file miniscript.h.

◆ GetWitnessSize()

template<typename Key >
std::optional< uint32_t > miniscript::Node< Key >::GetWitnessSize ( ) const
inline

Return the maximum size in bytes of a witness to satisfy this script non-malleably.

Note this does not include the witness script push.

Definition at line 1532 of file miniscript.h.

◆ IsBKW()

template<typename Key >
bool miniscript::Node< Key >::IsBKW ( ) const
inline

Whether this node is of type B, K or W.

(That is, anything but V.)

Definition at line 1499 of file miniscript.h.

◆ IsNonMalleable()

template<typename Key >
bool miniscript::Node< Key >::IsNonMalleable ( ) const
inline

Check whether this script can always be satisfied in a non-malleable way.

Definition at line 1604 of file miniscript.h.

◆ IsNotSatisfiable()

template<typename Key >
bool miniscript::Node< Key >::IsNotSatisfiable ( ) const
inline

Whether no satisfaction exists for this node.

Definition at line 1528 of file miniscript.h.

◆ IsSane()

template<typename Key >
bool miniscript::Node< Key >::IsSane ( ) const
inline

Check whether this node is safe as a script on its own.

Definition at line 1622 of file miniscript.h.

◆ IsSaneSubexpression()

template<typename Key >
bool miniscript::Node< Key >::IsSaneSubexpression ( ) const
inline

Whether the apparent policy of this node matches its script semantics. Doesn't guarantee it is a safe script on its own.

Definition at line 1619 of file miniscript.h.

◆ IsSatisfiable()

template<typename Key >
template<typename F >
bool miniscript::Node< Key >::IsSatisfiable ( fn) const
inline

Determine whether a Miniscript node is satisfiable.

fn(node) will be invoked for all key, time, and hashing nodes, and should return their satisfiability.

Definition at line 1555 of file miniscript.h.

Here is the call graph for this function:

◆ IsValid()

template<typename Key >
bool miniscript::Node< Key >::IsValid ( ) const
inline

Check whether this node is valid at all.

Definition at line 1595 of file miniscript.h.

Here is the call graph for this function:

◆ IsValidTopLevel()

template<typename Key >
bool miniscript::Node< Key >::IsValidTopLevel ( ) const
inline

Check whether this node is valid as a script on its own.

Definition at line 1601 of file miniscript.h.

◆ NeedsSignature()

template<typename Key >
bool miniscript::Node< Key >::NeedsSignature ( ) const
inline

Check whether this script always needs a signature.

Definition at line 1607 of file miniscript.h.

◆ operator==()

template<typename Key >
bool miniscript::Node< Key >::operator== ( const Node< Key > &  arg) const
inline

Equality testing.

Definition at line 1637 of file miniscript.h.

◆ ProduceInput()

template<typename Key >
template<typename Ctx >
internal::InputResult miniscript::Node< Key >::ProduceInput ( const Ctx &  ctx) const
inlineprivate

Definition at line 1168 of file miniscript.h.

Here is the call graph for this function:

◆ Satisfy()

template<typename Key >
template<typename Ctx >
Availability miniscript::Node< Key >::Satisfy ( const Ctx &  ctx,
std::vector< std::vector< unsigned char > > &  stack,
bool  nonmalleable = true 
) const
inline

Produce a witness for this script, if possible and given the information available in the context.

The non-malleable satisfaction is guaranteed to be valid if it exists, and ValidSatisfaction() is true. If IsSane() holds, this satisfaction is guaranteed to succeed in case the node's conditions are satisfied (private keys and hash preimages available, locktimes satisfied).

Definition at line 1629 of file miniscript.h.

◆ ScriptSize()

template<typename Key >
size_t miniscript::Node< Key >::ScriptSize ( ) const
inline

Return the size of the script for this expression (faster than ToScript().size()).

Definition at line 1480 of file miniscript.h.

◆ ToScript()

template<typename Key >
template<typename Ctx >
CScript miniscript::Node< Key >::ToScript ( const Ctx &  ctx) const
inline

Definition at line 731 of file miniscript.h.

Here is the call graph for this function:

◆ ToString()

template<typename Key >
template<typename CTx >
std::optional< std::string > miniscript::Node< Key >::ToString ( const CTx &  ctx) const
inline

Definition at line 810 of file miniscript.h.

Here is the call graph for this function:

◆ TreeEval() [1/2]

template<typename Key >
template<typename Result , typename State , typename DownFn , typename UpFn >
Result miniscript::Node< Key >::TreeEval ( State  root_state,
DownFn &&  downfn,
UpFn  upfn 
) const
inlineprivate

Like TreeEvalMaybe, but always produces a result.

upfn must return Result.

Definition at line 663 of file miniscript.h.

◆ TreeEval() [2/2]

template<typename Key >
template<typename Result , typename UpFn >
Result miniscript::Node< Key >::TreeEval ( UpFn  upfn) const
inlineprivate

Like TreeEval, but without downfn or State type.

upfn takes (const Node&, Span<Result>) and returns Result.

Definition at line 679 of file miniscript.h.

◆ TreeEvalMaybe() [1/2]

template<typename Key >
template<typename Result , typename State , typename DownFn , typename UpFn >
std::optional< Result > miniscript::Node< Key >::TreeEvalMaybe ( State  root_state,
DownFn  downfn,
UpFn  upfn 
) const
inlineprivate

Entries of the explicit stack tracked in this algorithm.

< The node being evaluated.

< How many children of this node have been expanded.

< The state for that node.

Definition at line 585 of file miniscript.h.

Here is the call graph for this function:

◆ TreeEvalMaybe() [2/2]

template<typename Key >
template<typename Result , typename UpFn >
std::optional< Result > miniscript::Node< Key >::TreeEvalMaybe ( UpFn  upfn) const
inlineprivate

Like TreeEvalMaybe, but without downfn or State type.

upfn takes (const Node&, Span<Result>) and returns std::optional<Result>.

Definition at line 650 of file miniscript.h.

◆ ValidSatisfactions()

template<typename Key >
bool miniscript::Node< Key >::ValidSatisfactions ( ) const
inline

Whether successful non-malleable satisfactions are guaranteed to be valid.

Definition at line 1616 of file miniscript.h.

Friends And Related Function Documentation

◆ Compare

template<typename Key >
int Compare ( const Node< Key > &  node1,
const Node< Key > &  node2 
)
friend

Compare two miniscript subtrees, using a non-recursive algorithm.

Definition at line 692 of file miniscript.h.

Member Data Documentation

◆ data

template<typename Key >
const std::vector<unsigned char> miniscript::Node< Key >::data

The data bytes in this expression (only for HASH160/HASH256/SHA256/RIPEMD10).

Definition at line 512 of file miniscript.h.

◆ fragment

template<typename Key >
const Fragment miniscript::Node< Key >::fragment

What node type this node is.

Definition at line 506 of file miniscript.h.

◆ has_duplicate_keys

template<typename Key >
std::optional<bool> miniscript::Node< Key >::has_duplicate_keys
mutableprivate

Whether a public key appears more than once in this node.

This value is initialized by all constructors except the NoDupCheck ones. The NoDupCheck ones skip the computation, requiring it to be done manually by invoking DuplicateKeyCheck(). DuplicateKeyCheck(), or a non-NoDupCheck constructor, will compute has_duplicate_keys for all subnodes as well.

Definition at line 547 of file miniscript.h.

◆ k

template<typename Key >
const uint32_t miniscript::Node< Key >::k = 0

The k parameter (time for OLDER/AFTER, threshold for THRESH(_M))

Definition at line 508 of file miniscript.h.

◆ keys

template<typename Key >
const std::vector<Key> miniscript::Node< Key >::keys

The keys used by this expression (only for PK_K/PK_H/MULTI)

Definition at line 510 of file miniscript.h.

◆ m_script_ctx

template<typename Key >
const MiniscriptContext miniscript::Node< Key >::m_script_ctx

The Script context for this node. Either P2WSH or Tapscript.

Definition at line 516 of file miniscript.h.

◆ ops

template<typename Key >
const internal::Ops miniscript::Node< Key >::ops
private

Cached ops counts.

Definition at line 533 of file miniscript.h.

◆ scriptlen

template<typename Key >
const size_t miniscript::Node< Key >::scriptlen
private

Cached script length (computed by CalcScriptLen).

Definition at line 541 of file miniscript.h.

◆ ss

template<typename Key >
const internal::StackSize miniscript::Node< Key >::ss
private

Cached stack size bounds.

Definition at line 535 of file miniscript.h.

◆ subs

template<typename Key >
std::vector<NodeRef<Key> > miniscript::Node< Key >::subs
mutable

Subexpressions (for WRAP_*‍/AND_*‍/OR_*‍/ANDOR/THRESH)

Definition at line 514 of file miniscript.h.

◆ typ

template<typename Key >
const Type miniscript::Node< Key >::typ
private

Cached expression type (computed by CalcType and fed through SanitizeType).

Definition at line 539 of file miniscript.h.

◆ ws

template<typename Key >
const internal::WitnessSize miniscript::Node< Key >::ws
private

Cached witness size bounds.

Definition at line 537 of file miniscript.h.


The documentation for this struct was generated from the following file: