Skip to content

Commit 3be0fd9

Browse files
committed
refactor: Remove Error::NoMoreWitnesses
We can remove an error path by using the respective zero value as default witness value.
1 parent 211a30f commit 3be0fd9

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ pub enum Error {
8888
Type(types::Error),
8989
// Execution error
9090
Execution(bit_machine::ExecutionError),
91-
/// Witness iterator ended early
92-
NoMoreWitnesses,
9391
/// Tried to parse a jet but the name wasn't recognized
9492
InvalidJetName(String),
9593
/// Policy error
@@ -107,7 +105,6 @@ impl fmt::Display for Error {
107105
Error::Type(ref e) => fmt::Display::fmt(e, f),
108106
Error::Execution(ref e) => fmt::Display::fmt(e, f),
109107
Error::InvalidJetName(s) => write!(f, "unknown jet `{}`", s),
110-
Error::NoMoreWitnesses => f.write_str("no more witness data available"),
111108
#[cfg(feature = "elements")]
112109
Error::Policy(ref e) => fmt::Display::fmt(e, f),
113110
}
@@ -121,7 +118,6 @@ impl std::error::Error for Error {
121118
Error::DisconnectRedeemTime => None,
122119
Error::Type(ref e) => Some(e),
123120
Error::Execution(ref e) => Some(e),
124-
Error::NoMoreWitnesses => None,
125121
Error::InvalidJetName(..) => None,
126122
#[cfg(feature = "elements")]
127123
Error::Policy(ref e) => Some(e),

src/node/convert.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ pub trait Converter<N: Marker, M: Marker> {
152152
/// Does not do any type-checking and may attach an invalid witness to a combinator.
153153
///
154154
/// If it encounters a disconnect node, it simply returns an error.
155+
///
156+
/// **This finalizer should not be used in production.
157+
/// It introduces default values ([`Value::zero`]) to handle missing witness data,
158+
/// which might trigger unexpected spending paths.**
155159
// FIXME we should do type checking, but this would require a method to check
156160
// type compatibility between a Value and a type::Final.
157161
pub struct SimpleFinalizer<W: Iterator<Item = Value>> {
@@ -169,10 +173,13 @@ impl<W: Iterator<Item = Value>, J: Jet> Converter<Commit<J>, Redeem<J>> for Simp
169173

170174
fn convert_witness(
171175
&mut self,
172-
_: &PostOrderIterItem<&CommitNode<J>>,
176+
data: &PostOrderIterItem<&CommitNode<J>>,
173177
_: &NoWitness,
174178
) -> Result<Value, Self::Error> {
175-
self.iter.next().ok_or(crate::Error::NoMoreWitnesses)
179+
Ok(self
180+
.iter
181+
.next()
182+
.unwrap_or_else(|| Value::zero(&data.node.arrow().target)))
176183
}
177184

178185
fn convert_disconnect(

0 commit comments

Comments
 (0)