From 14ca8d882ffea0c1eaa75560b86eb16f8485ab0f Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Mon, 6 Jan 2025 14:10:29 +0100 Subject: [PATCH] persistence: fix mismatch between blank builders and their inputs Closes #284 --- src/containers/partials.rs | 4 ++++ src/persistence/stock.rs | 24 +++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/containers/partials.rs b/src/containers/partials.rs index ed2d6ee9..8bdf33d2 100644 --- a/src/containers/partials.rs +++ b/src/containers/partials.rs @@ -156,12 +156,16 @@ impl Hash for TransitionInfo { } impl TransitionInfo { + /// # Panics + /// + /// If the number of provided seals is zero. pub fn new( transition: Transition, seals: impl AsRef<[XOutputSeal]>, ) -> Result { let id = transition.id(); let seals = seals.as_ref(); + assert!(!seals.is_empty(), "empty seals provided to transition info constructor"); let inputs = Confined::, 1, U24>::try_from_iter( seals.iter().copied().map(XOutpoint::from), ) diff --git a/src/persistence/stock.rs b/src/persistence/stock.rs index 817e19f8..c05774bb 100644 --- a/src/persistence/stock.rs +++ b/src/persistence/stock.rs @@ -1214,25 +1214,27 @@ impl Stock { .map_err(|_| ComposeError::TooManyBlanks)?; } - let (first_builder, second_builder) = + let (first_builder, first_inputs, second_builder, second_inputs) = match (main_builder.has_inputs(), alt_builder.has_inputs()) { - (true, true) => (main_builder, Some(alt_builder)), - (true, false) => (main_builder, None), - (false, true) => (alt_builder, None), + (true, true) => (main_builder, main_inputs, Some(alt_builder), alt_inputs), + (true, false) => (main_builder, main_inputs, None, alt_inputs), + (false, true) => (alt_builder, alt_inputs, None, main_inputs), (false, false) => return Err(ComposeError::InsufficientState.into()), }; - let first = TransitionInfo::new(first_builder.complete_transition()?, main_inputs) + let first = TransitionInfo::new(first_builder.complete_transition()?, first_inputs) .map_err(|e| { debug_assert!(!matches!(e, TransitionInfoError::CloseMethodDivergence(_))); ComposeError::TooManyInputs })?; let second = if let Some(second_builder) = second_builder { - Some(TransitionInfo::new(second_builder.complete_transition()?, alt_inputs).map_err( - |e| { - debug_assert!(!matches!(e, TransitionInfoError::CloseMethodDivergence(_))); - ComposeError::TooManyInputs - }, - )?) + Some( + TransitionInfo::new(second_builder.complete_transition()?, second_inputs).map_err( + |e| { + debug_assert!(!matches!(e, TransitionInfoError::CloseMethodDivergence(_))); + ComposeError::TooManyInputs + }, + )?, + ) } else { None };