Skip to content

Commit

Permalink
resolvers: add consignment resolver wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 3, 2024
1 parent 41d6170 commit 4500bbc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/containers/indexed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use rgb::{
};
use strict_types::TypeSystem;

use super::Consignment;
use super::{Consignment, XPubWitness};
use crate::containers::anchors::ToWitnessId;
use crate::SecretSeal;

Expand All @@ -45,6 +45,7 @@ pub struct IndexedConsignment<'c, const TRANSFER: bool> {
op_witness_idx: BTreeMap<OpId, XWitnessId>,
op_bundle_idx: BTreeMap<OpId, BundleId>,
extension_idx: BTreeMap<OpId, &'c Extension>,
witness_idx: BTreeMap<XWitnessId, &'c XPubWitness>,
}

impl<'c, const TRANSFER: bool> Deref for IndexedConsignment<'c, TRANSFER> {
Expand All @@ -60,7 +61,9 @@ impl<'c, const TRANSFER: bool> IndexedConsignment<'c, TRANSFER> {
let mut op_witness_idx = BTreeMap::new();
let mut op_bundle_idx = BTreeMap::new();
let mut extension_idx = BTreeMap::new();
let mut witness_idx = BTreeMap::new();
for bw in &consignment.bundles {
witness_idx.insert(bw.pub_witness.to_witness_id(), &bw.pub_witness);
for (anchor, bundle) in bw.anchored_bundles.pairs() {
let bundle_id = bundle.bundle_id();
let witness_id = bw.pub_witness.to_witness_id();
Expand Down Expand Up @@ -89,6 +92,7 @@ impl<'c, const TRANSFER: bool> IndexedConsignment<'c, TRANSFER> {
op_witness_idx,
op_bundle_idx,
extension_idx,
witness_idx,
}
}

Expand All @@ -100,6 +104,10 @@ impl<'c, const TRANSFER: bool> IndexedConsignment<'c, TRANSFER> {
.and_then(|id| self.bundle_idx.get(id))
.and_then(|bundle| bundle.known_transitions.get(&opid))
}

pub fn pub_witness(&self, id: XWitnessId) -> Option<&XPubWitness> {
self.witness_idx.get(&id).copied()
}
}

impl<'c, const TRANSFER: bool> ConsignmentApi for IndexedConsignment<'c, TRANSFER> {
Expand Down
33 changes: 32 additions & 1 deletion src/resolvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,39 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use rgb::{WitnessAnchor, XWitnessId};
use rgb::validation::{ResolveWitness, WitnessResolverError};
use rgb::{WitnessAnchor, XWitnessId, XWitnessTx};

use crate::containers::IndexedConsignment;

pub trait ResolveHeight {
fn resolve_height(&mut self, witness_id: XWitnessId) -> Result<WitnessAnchor, String>;
}

pub struct ConsignmentResolver<'cons, R: ResolveWitness + ResolveHeight, const TRANSFER: bool> {
consignment: &'cons IndexedConsignment<'cons, TRANSFER>,
fallback: R,
}

impl<'cons, R: ResolveWitness + ResolveHeight, const TRANSFER: bool> ResolveHeight
for ConsignmentResolver<'cons, R, TRANSFER>
{
fn resolve_height(&mut self, witness_id: XWitnessId) -> Result<WitnessAnchor, String> {
self.fallback.resolve_height(witness_id)
}
}

impl<'cons, R: ResolveWitness + ResolveHeight, const TRANSFER: bool> ResolveWitness
for ConsignmentResolver<'cons, R, TRANSFER>
{
fn resolve_pub_witness(
&self,
witness_id: XWitnessId,
) -> Result<XWitnessTx, WitnessResolverError> {
self.consignment
.pub_witness(witness_id)
.and_then(|p| p.map_ref(|pw| pw.tx.clone()).transpose())
.ok_or(WitnessResolverError::Unknown(witness_id))
.or_else(|_| self.fallback.resolve_pub_witness(witness_id))
}
}

0 comments on commit 4500bbc

Please sign in to comment.