Skip to content

Commit

Permalink
Merge pull request #291 from RGB-WG/fix/290
Browse files Browse the repository at this point in the history
Use XOupoint instead of XOutputSeal in the APIs
  • Loading branch information
dr-orlovsky authored Jan 7, 2025
2 parents b374468 + af5a31b commit c00aa3b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
4 changes: 4 additions & 0 deletions src/containers/partials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, TransitionInfoError> {
let id = transition.id();
let seals = seals.as_ref();
assert!(!seals.is_empty(), "empty seals provided to transition info constructor");
let inputs = Confined::<BTreeSet<_>, 1, U24>::try_from_iter(
seals.iter().copied().map(XOutpoint::from),
)
Expand Down
12 changes: 6 additions & 6 deletions src/persistence/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use amplify::confinement;
use nonasync::persistence::{CloneNoPersistence, Persisting};
use rgb::{
Assign, AssignmentType, BundleId, ContractId, ExposedState, Extension, Genesis, GenesisSeal,
GraphSeal, OpId, Operation, Opout, TransitionBundle, TypedAssigns, XChain, XOutputSeal,
GraphSeal, OpId, Operation, Opout, TransitionBundle, TypedAssigns, XChain, XOutpoint,
XWitnessId,
};

Expand Down Expand Up @@ -99,7 +99,7 @@ pub enum IndexInconsistency {
BundleAbsent(OpId),

/// outpoint {0} is not part of the contract {1}.
OutpointUnknown(XOutputSeal, ContractId),
OutpointUnknown(XOutpoint, ContractId),

/// index already contains information about bundle {bundle_id} which
/// specifies contract {present} instead of contract {expected}.
Expand Down Expand Up @@ -297,7 +297,7 @@ impl<P: IndexProvider> Index<P> {

pub(super) fn contracts_assigning(
&self,
outputs: BTreeSet<XOutputSeal>,
outputs: BTreeSet<XOutpoint>,
) -> Result<impl Iterator<Item = ContractId> + '_, IndexError<P>> {
self.provider
.contracts_assigning(outputs)
Expand All @@ -314,7 +314,7 @@ impl<P: IndexProvider> Index<P> {
pub(super) fn opouts_by_outputs(
&self,
contract_id: ContractId,
outputs: impl IntoIterator<Item = impl Into<XOutputSeal>>,
outputs: impl IntoIterator<Item = impl Into<XOutpoint>>,
) -> Result<BTreeSet<Opout>, IndexError<P>> {
Ok(self.provider.opouts_by_outputs(contract_id, outputs)?)
}
Expand Down Expand Up @@ -368,7 +368,7 @@ pub trait IndexReadProvider {

fn contracts_assigning(
&self,
outputs: BTreeSet<XOutputSeal>,
outputs: BTreeSet<XOutpoint>,
) -> Result<impl Iterator<Item = ContractId> + '_, Self::Error>;

fn public_opouts(
Expand All @@ -379,7 +379,7 @@ pub trait IndexReadProvider {
fn opouts_by_outputs(
&self,
contract_id: ContractId,
outputs: impl IntoIterator<Item = impl Into<XOutputSeal>>,
outputs: impl IntoIterator<Item = impl Into<XOutpoint>>,
) -> Result<BTreeSet<Opout>, IndexReadError<Self::Error>>;

fn opouts_by_terminals(
Expand Down
18 changes: 12 additions & 6 deletions src/persistence/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,14 +1273,18 @@ impl IndexReadProvider for MemIndex {

fn contracts_assigning(
&self,
outputs: BTreeSet<XOutputSeal>,
outpoints: BTreeSet<XOutpoint>,
) -> Result<impl Iterator<Item = ContractId> + '_, Self::Error> {
Ok(self
.contract_index
.iter()
.flat_map(move |(contract_id, index)| {
outputs.clone().into_iter().filter_map(|outpoint| {
if index.outpoint_opouts.contains_key(&outpoint) {
outpoints.clone().into_iter().filter_map(|outpoint| {
if index
.outpoint_opouts
.keys()
.any(|seal| seal.to_outpoint() == outpoint)
{
Some(*contract_id)
} else {
None
Expand All @@ -1303,17 +1307,19 @@ impl IndexReadProvider for MemIndex {
fn opouts_by_outputs(
&self,
contract_id: ContractId,
outputs: impl IntoIterator<Item = impl Into<XOutputSeal>>,
outpoints: impl IntoIterator<Item = impl Into<XOutpoint>>,
) -> Result<BTreeSet<Opout>, IndexReadError<Self::Error>> {
let index = self
.contract_index
.get(&contract_id)
.ok_or(IndexInconsistency::ContractAbsent(contract_id))?;
let mut opouts = BTreeSet::new();
for output in outputs.into_iter().map(|o| o.into()) {
for output in outpoints.into_iter().map(|o| o.into()) {
let set = index
.outpoint_opouts
.get(&output)
.iter()
.find(|(seal, _)| seal.to_outpoint() == output)
.map(|(_, set)| set.to_unconfined())
.ok_or(IndexInconsistency::OutpointUnknown(output, contract_id))?;
opouts.extend(set)
}
Expand Down
26 changes: 14 additions & 12 deletions src/persistence/stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {
/// output seals.
pub fn contracts_assigning(
&self,
outputs: impl IntoIterator<Item = impl Into<XOutputSeal>>,
outputs: impl IntoIterator<Item = impl Into<XOutpoint>>,
) -> Result<impl Iterator<Item = ContractId> + '_, StockError<S, H, P>> {
let outputs = outputs
.into_iter()
Expand Down Expand Up @@ -1214,25 +1214,27 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {
.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
};
Expand Down

0 comments on commit c00aa3b

Please sign in to comment.