Skip to content

Commit

Permalink
chore: continue refactoring for multi-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Nov 7, 2023
1 parent 8db4140 commit 5f75ffc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 110 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.

19 changes: 9 additions & 10 deletions src/containers/consignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rgb::validation::{AnchoredBundle, ConsignmentApi};
use rgb::{
validation, AssetTag, AssignmentType, AttachId, BundleId, ContractHistory, ContractId,
Extension, Genesis, GraphSeal, OpId, OpRef, Operation, Schema, SchemaId, SealDefinition,
SecretSeal, SubSchema, Transition, TransitionBundle, WitnessAnchor,
SecretSeal, SubSchema, Transition, TransitionBundle,
};
use strict_encoding::{StrictDeserialize, StrictDumb, StrictSerialize};

Expand Down Expand Up @@ -185,10 +185,9 @@ impl<const TYPE: bool> Consignment<TYPE> {
for anchored_bundle in &self.bundles {
for item in anchored_bundle.bundle.values() {
if let Some(transition) = &item.transition {
let txid = anchored_bundle.anchor.txid;
let height = resolver.resolve_height(txid)?;
let ord_txid = WitnessAnchor::new(height, txid);
history.add_transition(transition, ord_txid);
let witness_anchor = resolver.resolve_anchor(&anchored_bundle.anchor)?;

history.add_transition(transition, witness_anchor);
for (id, used) in &mut extension_idx {
if *used {
continue;
Expand All @@ -197,11 +196,11 @@ impl<const TYPE: bool> Consignment<TYPE> {
if input.prev_out.op == *id {
*used = true;
if let Some(ord) = ordered_extensions.get_mut(id) {
if *ord > ord_txid {
*ord = ord_txid;
if *ord > witness_anchor {
*ord = witness_anchor;
}
} else {
ordered_extensions.insert(*id, ord_txid);
ordered_extensions.insert(*id, witness_anchor);
}
}
}
Expand All @@ -210,8 +209,8 @@ impl<const TYPE: bool> Consignment<TYPE> {
}
}
for extension in &self.extensions {
if let Some(ord_txid) = ordered_extensions.get(&extension.id()) {
history.add_extension(extension, *ord_txid);
if let Some(witness_anchor) = ordered_extensions.get(&extension.id()) {
history.add_extension(extension, *witness_anchor);
}
}

Expand Down
44 changes: 21 additions & 23 deletions src/interface/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use std::ops::Deref;
use amplify::confinement::{LargeOrdMap, LargeVec, SmallVec};
use bp::Outpoint;
use rgb::{
AssignmentType, AttachId, ContractId, ContractState, FungibleOutput, MediaType, RevealedAttach,
RevealedData, SealWitness,
AssignmentType, AttachId, ContractId, ContractState, FungibleOutput, MediaType, Output,
RevealedAttach, RevealedData, WitnessId,
};
use strict_encoding::FieldName;
use strict_types::typify::TypedVal;
Expand Down Expand Up @@ -76,8 +76,8 @@ impl From<RevealedAttach> for AttachedState {

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct FungibleAllocation {
pub owner: Outpoint,
pub witness: SealWitness,
pub owner: Output,
pub witness: Option<WitnessId>,
pub value: u64,
}

Expand All @@ -88,60 +88,58 @@ impl From<FungibleOutput> for FungibleAllocation {
impl From<&FungibleOutput> for FungibleAllocation {
fn from(out: &FungibleOutput) -> Self {
FungibleAllocation {
owner: out.seal,
owner: out.output,
witness: out.witness,
value: out.state.value.as_u64(),
}
}
}

pub trait OutpointFilter {
fn include_outpoint(&self, outpoint: Outpoint) -> bool;
fn include_output(&self, output: Output) -> bool;
}

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

impl<T: OutpointFilter> OutpointFilter for &T {
fn include_outpoint(&self, outpoint: Outpoint) -> bool { (*self).include_outpoint(outpoint) }
fn include_output(&self, output: Output) -> bool { (*self).include_output(output) }
}

impl<T: OutpointFilter> OutpointFilter for &mut T {
fn include_outpoint(&self, outpoint: Outpoint) -> bool {
self.deref().include_outpoint(outpoint)
}
fn include_output(&self, output: Output) -> bool { self.deref().include_output(output) }
}

impl<T: OutpointFilter> OutpointFilter for Option<T> {
fn include_outpoint(&self, outpoint: Outpoint) -> bool {
fn include_output(&self, output: Output) -> bool {
self.as_ref()
.map(|filter| filter.include_outpoint(outpoint))
.map(|filter| filter.include_output(output))
.unwrap_or(true)
}
}

impl OutpointFilter for FilterIncludeAll {
fn include_outpoint(&self, _: Outpoint) -> bool { true }
fn include_output(&self, _: Output) -> bool { true }
}

impl<T: OutpointFilter> OutpointFilter for FilterExclude<T> {
fn include_outpoint(&self, outpoint: Outpoint) -> bool { !self.0.include_outpoint(outpoint) }
fn include_output(&self, output: Output) -> bool { !self.0.include_output(output) }
}

impl OutpointFilter for &[Outpoint] {
fn include_outpoint(&self, outpoint: Outpoint) -> bool { self.contains(&outpoint) }
impl OutpointFilter for &[Output] {
fn include_output(&self, output: Output) -> bool { self.contains(&output) }
}

impl OutpointFilter for Vec<Outpoint> {
fn include_outpoint(&self, outpoint: Outpoint) -> bool { self.contains(&outpoint) }
impl OutpointFilter for Vec<Output> {
fn include_output(&self, output: Output) -> bool { self.contains(&output) }
}

impl OutpointFilter for HashSet<Outpoint> {
fn include_outpoint(&self, outpoint: Outpoint) -> bool { self.contains(&outpoint) }
impl OutpointFilter for HashSet<Output> {
fn include_output(&self, output: Output) -> bool { self.contains(&output) }
}

impl OutpointFilter for BTreeSet<Outpoint> {
fn include_outpoint(&self, outpoint: Outpoint) -> bool { self.contains(&outpoint) }
impl OutpointFilter for BTreeSet<Output> {
fn include_output(&self, output: Output) -> bool { self.contains(&output) }
}

/// Contract state is an in-memory structure providing API to read structured
Expand Down Expand Up @@ -200,7 +198,7 @@ impl ContractIface {
.fungibles()
.iter()
.filter(|outp| outp.opout.ty == type_id)
.filter(|outp| filter.include_outpoint(outp.seal))
.filter(|outp| filter.include_output(outp.output))
.map(FungibleAllocation::from);
Ok(LargeVec::try_from_iter(state).expect("same or smaller collection size"))
}
Expand Down
28 changes: 15 additions & 13 deletions src/persistence/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use bp::Txid;
use commit_verify::{mpc, Conceal};
use rgb::{
validation, Anchor, AnchoredBundle, BundleId, ContractId, ExposedSeal, GraphSeal, OpId,
Operation, Opout, SchemaId, SealDefinition, SecretSeal, SubSchema, Transition,
TransitionBundle,
Operation, Opout, Output, SchemaId, SealDefinition, SecretSeal, SubSchema, Transition,
TransitionBundle, WitnessId,
};
use strict_encoding::TypeName;

Expand All @@ -46,7 +46,6 @@ use crate::persistence::hoard::ConsumeError;
use crate::persistence::stash::StashInconsistency;
use crate::persistence::{Stash, StashError};
use crate::resolvers::ResolveHeight;
use crate::Outpoint;

#[derive(Debug, Display, Error, From)]
#[display(doc_comments)]
Expand Down Expand Up @@ -160,6 +159,9 @@ pub enum DataError {
/// you'd like to take the risc, call `import_contract_force`.
TerminalsUnmined,

/// mismatch between witness seal chain and anchor chain.
ChainMismatch,

#[display(inner)]
#[from]
Reveal(RevealError),
Expand All @@ -169,7 +171,7 @@ pub enum DataError {
Merge(MergeRevealError),

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

#[from]
Confinement(confinement::Error),
Expand Down Expand Up @@ -315,7 +317,7 @@ pub trait Inventory: Deref<Target = Self::Stash> {
&mut self,
contract_id: ContractId,
bundle: TransitionBundle,
witness_txid: Txid,
witness_id: WitnessId,
) -> Result<(), InventoryError<Self::Error>>;

/// # Safety
Expand Down Expand Up @@ -440,31 +442,31 @@ pub trait Inventory: Deref<Target = Self::Stash> {

fn transition(&self, opid: OpId) -> Result<&Transition, InventoryError<Self::Error>>;

fn contracts_by_outpoints(
fn contracts_by_outputs(
&mut self,
outpoints: impl IntoIterator<Item = impl Into<Outpoint>>,
outputs: impl IntoIterator<Item = impl Into<Output>>,
) -> Result<BTreeSet<ContractId>, InventoryError<Self::Error>>;

fn public_opouts(
&mut self,
contract_id: ContractId,
) -> Result<BTreeSet<Opout>, InventoryError<Self::Error>>;

fn opouts_by_outpoints(
fn opouts_by_outputs(
&mut self,
contract_id: ContractId,
outpoints: impl IntoIterator<Item = impl Into<Outpoint>>,
outputs: impl IntoIterator<Item = impl Into<Output>>,
) -> Result<BTreeSet<Opout>, InventoryError<Self::Error>>;

fn opouts_by_terminals(
&mut self,
terminals: impl IntoIterator<Item = SecretSeal>,
) -> Result<BTreeSet<Opout>, InventoryError<Self::Error>>;

fn state_for_outpoints(
fn state_for_outputs(
&mut self,
contract_id: ContractId,
outpoints: impl IntoIterator<Item = impl Into<Outpoint>>,
outputs: impl IntoIterator<Item = impl Into<Output>>,
) -> Result<BTreeMap<Opout, TypedState>, InventoryError<Self::Error>>;

fn store_seal_secret(
Expand Down Expand Up @@ -518,11 +520,11 @@ pub trait Inventory: Deref<Target = Self::Stash> {
let (outpoint_seals, terminal_seals) = seals
.into_iter()
.map(|seal| match seal.into() {
BuilderSeal::Revealed(seal) => (seal.outpoint(), seal.conceal()),
BuilderSeal::Revealed(seal) => (seal.output(), seal.conceal()),
BuilderSeal::Concealed(seal) => (None, seal),
})
.unzip::<_, _, Vec<_>, Vec<_>>();
opouts.extend(self.opouts_by_outpoints(contract_id, outpoint_seals.into_iter().flatten())?);
opouts.extend(self.opouts_by_outputs(contract_id, outpoint_seals.into_iter().flatten())?);
opouts.extend(self.opouts_by_terminals(terminal_seals.iter().copied())?);

// 1.1. Get all public transitions
Expand Down
Loading

0 comments on commit 5f75ffc

Please sign in to comment.