Skip to content

Commit

Permalink
iface: add abiloity to select inputs for a given state to ContractIface
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 15, 2024
1 parent c3ef1e3 commit 8f7020c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/interface/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub struct AllocatedState {
pub insufficient: Option<rgb::State>,
}

#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct StateCalc {
vm: aluvm::Vm,
abi: StateAbi,
Expand Down Expand Up @@ -189,4 +189,8 @@ impl StateCalc {
.map_err(|err| StateCalcError::ChangeCalc(ty, err))?;
self.fetch_state(ty, Reg16::Reg0)
}

pub fn is_sufficient_for(&self, ty: AssignmentType, state: &rgb::State) -> bool {
self.clone().calc_output(ty, state).is_ok()
}
}
22 changes: 21 additions & 1 deletion src/interface/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
use std::borrow::Borrow;
use std::collections::{BTreeSet, HashMap, HashSet};

use rgb::validation::Scripts;
use rgb::{AssignmentType, ContractId, OpId, Schema, State, XOutputSeal, XWitnessId};
use strict_encoding::FieldName;
use strict_types::{StrictVal, TypeSystem};

use crate::contract::{OutputAssignment, WitnessInfo};
use crate::info::ContractInfo;
use crate::interface::{AssignmentsFilter, IfaceImpl};
use crate::interface::{AssignmentsFilter, IfaceImpl, StateCalc};
use crate::persistence::ContractStateRead;

#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
Expand Down Expand Up @@ -115,6 +116,7 @@ pub struct ContractIface<S: ContractStateRead> {
pub schema: Schema,
pub iface: IfaceImpl,
pub types: TypeSystem,
pub scripts: Scripts,
pub info: ContractInfo,
}

Expand Down Expand Up @@ -175,6 +177,24 @@ impl<S: ContractStateRead> ContractIface<S> {
.filter(move |outp| outp.opout.ty == type_id))
}

pub fn assignments_fulfilling<'c, K: Ord + 'c>(
&'c self,
name: impl Into<FieldName>,
filter: impl AssignmentsFilter + 'c,
state: &'c State,
sorting: impl FnMut(&&OutputAssignment) -> K,
) -> Result<impl Iterator<Item = &'c OutputAssignment> + 'c, ContractError> {
let mut selected = self.assignments_by_type(name, filter)?.collect::<Vec<_>>();
selected.sort_by_key(sorting);
let mut calc = StateCalc::new(self.scripts.clone(), self.iface.state_abi);
Ok(selected.into_iter().take_while(move |a| {
if calc.reg_input(a.opout.ty, &a.state).is_err() {
return false;
}
calc.is_sufficient_for(a.opout.ty, state)
}))
}

pub fn history(
&self,
filter_outpoints: impl AssignmentsFilter,
Expand Down
19 changes: 13 additions & 6 deletions src/persistence/stash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ impl<P: StashProvider> Stash<P> {
.map_err(StashError::ReadProvider)
}

pub(super) fn scripts<'a>(
&self,
lib_ids: impl IntoIterator<Item = LibId>,
) -> Result<Scripts, StashError<P>> {
let mut scripts = BTreeMap::new();
for id in lib_ids {
let lib = self.provider.lib(id)?;
scripts.insert(id, lib.clone());
}
Ok(Scripts::from_checked(scripts))
}

pub(super) fn extract<'a>(
&self,
schema: &Schema,
Expand Down Expand Up @@ -359,12 +371,7 @@ impl<P: StashProvider> Stash<P> {
.ok_or(StashDataError::NoIfaceImpl(schema.schema_id(), iface.iface_id()))?;

let (types, _) = self.extract(&schema_ifaces.schema, [iface])?;
let mut scripts = BTreeMap::new();
for id in iimpl.state_abi.lib_ids() {
let lib = self.provider.lib(id)?;
scripts.insert(id, lib.clone());
}
let scripts = Scripts::from_checked(scripts);
let scripts = self.scripts(iimpl.state_abi.lib_ids())?;

let builder = if let Some(transition_name) = transition_name {
TransitionBuilder::named_transition(
Expand Down
2 changes: 2 additions & 0 deletions src/persistence/stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {
state,
schema: schema_ifaces.schema.clone(),
iface: iimpl.clone(),
scripts: self.stash.scripts(iimpl.state_abi.lib_ids())?,
types,
info,
}))
Expand All @@ -604,6 +605,7 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {
state,
schema: schema_ifaces.schema.clone(),
iface: iimpl.clone(),
scripts: self.stash.scripts(iimpl.state_abi.lib_ids())?,
types,
info,
})
Expand Down

0 comments on commit 8f7020c

Please sign in to comment.