Skip to content

Commit

Permalink
iface: refactor OutpointFilter into AssignmentsFilter
Browse files Browse the repository at this point in the history
which accounts for witness id
  • Loading branch information
dr-orlovsky committed Sep 20, 2024
1 parent e75e07a commit 8f056b8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 45 deletions.
33 changes: 23 additions & 10 deletions src/interface/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use strict_types::{StrictVal, TypeSystem};

use crate::contract::{KnownState, OutputAssignment};
use crate::info::ContractInfo;
use crate::interface::{IfaceImpl, OutpointFilter};
use crate::interface::{AssignmentsFilter, IfaceImpl};
use crate::persistence::ContractStateRead;
use crate::LIB_NAME_RGB_STD;

Expand Down Expand Up @@ -194,7 +194,21 @@ impl<S: ContractStateRead> ContractIface<S> {
&'c self,
state: impl IntoIterator<Item = &'c OutputAssignment<A>> + 'c,
name: impl Into<FieldName>,
filter: impl OutpointFilter + 'c,
filter: impl AssignmentsFilter + 'c,
) -> Result<impl Iterator<Item = OutputAssignment<U>> + 'c, ContractError>
where
A: Clone + KnownState + 'c,
U: From<A> + KnownState + 'c,
{
Ok(self
.extract_state_unfiltered(state, name)?
.filter(move |outp| filter.should_include(outp.seal, outp.witness)))
}

Check warning on line 206 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L197-L206

Added lines #L197 - L206 were not covered by tests

fn extract_state_unfiltered<'c, A, U>(
&'c self,
state: impl IntoIterator<Item = &'c OutputAssignment<A>> + 'c,
name: impl Into<FieldName>,

Check warning on line 211 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L208-L211

Added lines #L208 - L211 were not covered by tests
) -> Result<impl Iterator<Item = OutputAssignment<U>> + 'c, ContractError>
where
A: Clone + KnownState + 'c,
Expand All @@ -208,49 +222,48 @@ impl<S: ContractStateRead> ContractIface<S> {
Ok(state
.into_iter()
.filter(move |outp| outp.opout.ty == type_id)
.filter(move |outp| filter.include_outpoint(outp.seal))
.cloned()
.map(OutputAssignment::<A>::transmute))
}

pub fn rights<'c>(
&'c self,
name: impl Into<FieldName>,
filter: impl OutpointFilter + 'c,
filter: impl AssignmentsFilter + 'c,

Check warning on line 232 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L232

Added line #L232 was not covered by tests
) -> Result<impl Iterator<Item = RightsAllocation> + 'c, ContractError> {
self.extract_state(self.state.rights_all(), name, filter)
}

pub fn fungible<'c>(
&'c self,
name: impl Into<FieldName>,
filter: impl OutpointFilter + 'c,
filter: impl AssignmentsFilter + 'c,

Check warning on line 240 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L240

Added line #L240 was not covered by tests
) -> Result<impl Iterator<Item = FungibleAllocation> + 'c, ContractError> {
self.extract_state(self.state.fungible_all(), name, filter)
}

pub fn data<'c>(
&'c self,
name: impl Into<FieldName>,
filter: impl OutpointFilter + 'c,
filter: impl AssignmentsFilter + 'c,

Check warning on line 248 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L248

Added line #L248 was not covered by tests
) -> Result<impl Iterator<Item = DataAllocation> + 'c, ContractError> {
self.extract_state(self.state.data_all(), name, filter)
}

pub fn attachments<'c>(
&'c self,
name: impl Into<FieldName>,
filter: impl OutpointFilter + 'c,
filter: impl AssignmentsFilter + 'c,

Check warning on line 256 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L256

Added line #L256 was not covered by tests
) -> Result<impl Iterator<Item = AttachAllocation> + 'c, ContractError> {
self.extract_state(self.state.attach_all(), name, filter)
}

pub fn allocations<'c>(
&'c self,
filter: impl OutpointFilter + Copy + 'c,
filter: impl AssignmentsFilter + Copy + 'c,

Check warning on line 263 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L263

Added line #L263 was not covered by tests
) -> impl Iterator<Item = OwnedAllocation> + 'c {
fn f<'a, S, U>(
filter: impl OutpointFilter + 'a,
filter: impl AssignmentsFilter + 'a,

Check warning on line 266 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L266

Added line #L266 was not covered by tests
state: impl IntoIterator<Item = &'a OutputAssignment<S>> + 'a,
) -> impl Iterator<Item = OutputAssignment<U>> + 'a
where
Expand All @@ -259,7 +272,7 @@ impl<S: ContractStateRead> ContractIface<S> {
{
state
.into_iter()
.filter(move |outp| filter.include_outpoint(outp.seal))
.filter(move |outp| filter.should_include(outp.seal, outp.witness))

Check warning on line 275 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L275

Added line #L275 was not covered by tests
.cloned()
.map(OutputAssignment::<S>::transmute)
}
Expand Down
90 changes: 56 additions & 34 deletions src/interface/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,88 +22,110 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::ops::Deref;

use rgb::XOutpoint;

pub trait OutpointFilter {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool;
use rgb::{XOutpoint, XWitnessId};

pub trait AssignmentsFilter {
fn should_include(
&self,
outpoint: impl Into<XOutpoint>,
witness_id: Option<XWitnessId>,
) -> bool;
}

pub struct FilterIncludeAll;
pub struct FilterExclude<T>(pub T);

impl OutpointFilter for FilterIncludeAll {
fn include_outpoint(&self, _: impl Into<XOutpoint>) -> bool { true }
impl AssignmentsFilter for FilterIncludeAll {
fn should_include(&self, _: impl Into<XOutpoint>, _: Option<XWitnessId>) -> bool { true }

Check warning on line 39 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L39

Added line #L39 was not covered by tests
}

impl<T: OutpointFilter> OutpointFilter for FilterExclude<T> {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
!self.0.include_outpoint(outpoint.into())
impl<T: AssignmentsFilter> AssignmentsFilter for FilterExclude<T> {
fn should_include(
&self,
outpoint: impl Into<XOutpoint>,
witness_id: Option<XWitnessId>,
) -> bool {
!self.0.should_include(outpoint.into(), witness_id)

Check warning on line 48 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L43-L48

Added lines #L43 - L48 were not covered by tests
}
}

impl<T: OutpointFilter> OutpointFilter for &T {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
(*self).include_outpoint(outpoint)
impl<T: AssignmentsFilter> AssignmentsFilter for &T {
fn should_include(
&self,
outpoint: impl Into<XOutpoint>,
witness_id: Option<XWitnessId>,
) -> bool {
(*self).should_include(outpoint, witness_id)

Check warning on line 58 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L53-L58

Added lines #L53 - L58 were not covered by tests
}
}

impl<T: OutpointFilter> OutpointFilter for &mut T {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
self.deref().include_outpoint(outpoint)
impl<T: AssignmentsFilter> AssignmentsFilter for &mut T {
fn should_include(
&self,
outpoint: impl Into<XOutpoint>,
witness_id: Option<XWitnessId>,
) -> bool {
self.deref().should_include(outpoint, witness_id)

Check warning on line 68 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L63-L68

Added lines #L63 - L68 were not covered by tests
}
}

impl<T: OutpointFilter> OutpointFilter for Option<T> {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
impl<T: AssignmentsFilter> AssignmentsFilter for Option<T> {
fn should_include(
&self,
outpoint: impl Into<XOutpoint>,
witness_id: Option<XWitnessId>,
) -> bool {

Check warning on line 77 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L73-L77

Added lines #L73 - L77 were not covered by tests
self.as_ref()
.map(|filter| filter.include_outpoint(outpoint))
.map(|filter| filter.should_include(outpoint, witness_id))

Check warning on line 79 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L79

Added line #L79 was not covered by tests
.unwrap_or(true)
}
}

impl OutpointFilter for XOutpoint {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool { *self == outpoint.into() }
impl AssignmentsFilter for XOutpoint {
fn should_include(&self, outpoint: impl Into<XOutpoint>, _: Option<XWitnessId>) -> bool {
*self == outpoint.into()
}

Check warning on line 87 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L85-L87

Added lines #L85 - L87 were not covered by tests
}

impl<const LEN: usize> OutpointFilter for [XOutpoint; LEN] {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
impl<const LEN: usize> AssignmentsFilter for [XOutpoint; LEN] {
fn should_include(&self, outpoint: impl Into<XOutpoint>, _: Option<XWitnessId>) -> bool {

Check warning on line 91 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L91

Added line #L91 was not covered by tests
self.contains(&outpoint.into())
}
}

impl OutpointFilter for &[XOutpoint] {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
impl AssignmentsFilter for &[XOutpoint] {
fn should_include(&self, outpoint: impl Into<XOutpoint>, _: Option<XWitnessId>) -> bool {

Check warning on line 97 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L97

Added line #L97 was not covered by tests
self.contains(&outpoint.into())
}
}

impl OutpointFilter for Vec<XOutpoint> {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
impl AssignmentsFilter for Vec<XOutpoint> {
fn should_include(&self, outpoint: impl Into<XOutpoint>, _: Option<XWitnessId>) -> bool {

Check warning on line 103 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L103

Added line #L103 was not covered by tests
self.contains(&outpoint.into())
}
}

impl OutpointFilter for HashSet<XOutpoint> {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
impl AssignmentsFilter for HashSet<XOutpoint> {
fn should_include(&self, outpoint: impl Into<XOutpoint>, _: Option<XWitnessId>) -> bool {

Check warning on line 109 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L109

Added line #L109 was not covered by tests
self.contains(&outpoint.into())
}
}

impl OutpointFilter for BTreeSet<XOutpoint> {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
impl AssignmentsFilter for BTreeSet<XOutpoint> {
fn should_include(&self, outpoint: impl Into<XOutpoint>, _: Option<XWitnessId>) -> bool {

Check warning on line 115 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L115

Added line #L115 was not covered by tests
self.contains(&outpoint.into())
}
}

impl<V> OutpointFilter for HashMap<XOutpoint, V> {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
impl<V> AssignmentsFilter for HashMap<XOutpoint, V> {
fn should_include(&self, outpoint: impl Into<XOutpoint>, _: Option<XWitnessId>) -> bool {

Check warning on line 121 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L121

Added line #L121 was not covered by tests
let outpoint = outpoint.into();
self.keys().any(|o| *o == outpoint)
}
}

impl<V> OutpointFilter for BTreeMap<XOutpoint, V> {
fn include_outpoint(&self, outpoint: impl Into<XOutpoint>) -> bool {
impl<V> AssignmentsFilter for BTreeMap<XOutpoint, V> {
fn should_include(&self, outpoint: impl Into<XOutpoint>, _: Option<XWitnessId>) -> bool {

Check warning on line 128 in src/interface/filter.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/filter.rs#L128

Added line #L128 was not covered by tests
let outpoint = outpoint.into();
self.keys().any(|o| *o == outpoint)
}
Expand Down
2 changes: 1 addition & 1 deletion src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use contract::{
FungibleAllocation, OwnedAllocation, RightsAllocation, StateChange,
};
pub use contractum::IfaceDisplay;
pub use filter::{FilterExclude, FilterIncludeAll, OutpointFilter};
pub use filter::{AssignmentsFilter, FilterExclude, FilterIncludeAll};
pub use iface::{
ArgMap, AssignIface, ExtensionIface, GenesisIface, GlobalIface, Iface, IfaceClass, IfaceId,
IfaceInconsistency, IfaceRef, IfaceWrapper, Modifier, OpName, OwnedIface, Req, TransitionIface,
Expand Down

0 comments on commit 8f056b8

Please sign in to comment.