Skip to content

Commit

Permalink
Derive Hash for all types
Browse files Browse the repository at this point in the history
Derive hash for all types that currently can.

It is not immediately apparent why we have types that do not derive
`Debug`, this patch however explicitly does not change this.

Fix: #394
  • Loading branch information
tcharding committed May 15, 2023
1 parent be27637 commit a27abb2
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::str::FromStr;
use crate::prelude::*;
use crate::{errstr, Error, MAX_RECURSION_DEPTH};

#[derive(Debug)]
#[derive(Debug, Hash)]
/// A token of the form `x(...)` or `x`
pub struct Tree<'a> {
/// The name `x`
Expand All @@ -28,6 +28,7 @@ pub trait FromTree: Sized {
fn from_tree(top: &Tree) -> Result<Self, Error>;
}

#[derive(Hash)]
enum Found {
Nothing,
LBracket(usize), // Either a left ( or {
Expand Down
6 changes: 3 additions & 3 deletions src/interpreter/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn script_from_stack_elem<Ctx: ScriptContext>(
}

/// Helper type to indicate the origin of the bare pubkey that the interpereter uses
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum PubkeyType {
Pk,
Pkh,
Expand All @@ -66,7 +66,7 @@ pub enum PubkeyType {
}

/// Helper type to indicate the origin of the bare miniscript that the interpereter uses
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum ScriptType {
Bare,
Sh,
Expand All @@ -76,7 +76,7 @@ pub enum ScriptType {
}

/// Structure representing a script under evaluation as a Miniscript
#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub(super) enum Inner {
/// The script being evaluated is a simple public key check (pay-to-pk,
/// pay-to-pkhash or pay-to-witness-pkhash)
Expand Down
8 changes: 5 additions & 3 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use self::stack::Stack;
use crate::MiniscriptKey;

/// An iterable Miniscript-structured representation of the spending of a coin
#[derive(Hash)]
pub struct Interpreter<'txin> {
inner: inner::Inner,
stack: Stack<'txin>,
Expand All @@ -43,7 +44,7 @@ pub struct Interpreter<'txin> {
// Ecdsa and Schnorr signatures

/// A type for representing signatures supported as of bitcoin core 22.0
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeySigPair {
/// A Full public key and corresponding Ecdsa signature
Ecdsa(bitcoin::PublicKey, bitcoin::ecdsa::Signature),
Expand Down Expand Up @@ -452,7 +453,7 @@ impl<'txin> Interpreter<'txin> {
}

/// Type of HashLock used for SatisfiedConstraint structure
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum HashLockType {
///SHA 256 hashlock
Sha256(sha256::Hash),
Expand All @@ -467,7 +468,7 @@ pub enum HashLockType {
/// A satisfied Miniscript condition (Signature, Hashlock, Timelock)
/// 'intp represents the lifetime of descriptor and `stack represents
/// the lifetime of witness
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum SatisfiedConstraint {
///Public key and corresponding signature
PublicKey {
Expand Down Expand Up @@ -506,6 +507,7 @@ pub enum SatisfiedConstraint {
///the top of the stack, we need to decide whether to execute right child or not.
///This is also useful for wrappers and thresholds which push a value on the stack
///depending on evaluation of the children.
#[derive(Hash)]
struct NodeEvaluationState<'intp> {
///The node which is being evaluated
node: &'intp Miniscript<BitcoinKey, NoChecks>,
Expand Down
2 changes: 1 addition & 1 deletion src/miniscript/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod private {
impl Sealed for super::bitcoin::secp256k1::XOnlyPublicKey {}
}

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Hash)]
enum NonTerm {
Expression,
WExpression,
Expand Down
2 changes: 1 addition & 1 deletion src/miniscript/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::Error;
use crate::prelude::*;

/// Atom of a tokenized version of a script
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum Token<'s> {
BoolAnd,
Expand Down
4 changes: 2 additions & 2 deletions src/miniscript/satisfy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ impl_tuple_satisfier!(A, B, C, D, E, F, G);
impl_tuple_satisfier!(A, B, C, D, E, F, G, H);

/// A witness, if available, for a Miniscript fragment
#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum Witness {
/// Witness Available and the value of the witness
Stack(Vec<Vec<u8>>),
Expand Down Expand Up @@ -708,7 +708,7 @@ impl Witness {
}

/// A (dis)satisfaction of a Miniscript fragment
#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct Satisfaction {
/// The actual witness stack
pub stack: Witness,
Expand Down
4 changes: 2 additions & 2 deletions src/policy/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl hash::Hash for OrdF64 {

/// Compilation key: This represents the state of the best possible compilation
/// of a given policy(implicitly keyed).
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
struct CompilationKey {
/// The type of the compilation result
ty: Type,
Expand Down Expand Up @@ -536,7 +536,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> AstElemExt<Pk, Ctx> {
}

/// Different types of casts possible for each node.
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Hash)]
struct Cast<Pk: MiniscriptKey, Ctx: ScriptContext> {
node: fn(Arc<Miniscript<Pk, Ctx>>) -> Terminal<Pk, Ctx>,
ast_type: fn(types::Type) -> Result<types::Type, ErrorKind>,
Expand Down
1 change: 1 addition & 0 deletions src/psbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl From<bitcoin::key::Error> for InputError {
/// the same psbt structure
/// All operations on this structure will panic if index
/// is more than number of inputs in pbst
#[derive(Hash)]
pub struct PsbtInputSatisfier<'psbt> {
/// pbst
pub psbt: &'psbt Psbt,
Expand Down

0 comments on commit a27abb2

Please sign in to comment.