Skip to content

Commit

Permalink
Collect supplyment features in consign (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Choi authored Jun 28, 2024
1 parent fe27923 commit 8ec5e92
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/persistence/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ impl StashReadProvider for MemStash {
.into_iter())
}

fn sigs_for(&self, content_id: &ContentId) -> Result<Option<&ContentSigs>, Self::Error> {
Ok(self.sigs.get(content_id))
}

fn geneses(&self) -> Result<impl Iterator<Item = &Genesis>, Self::Error> {
Ok(self.geneses.values())
}
Expand Down
5 changes: 3 additions & 2 deletions src/persistence/stash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use strict_types::typesys::UnknownType;
use strict_types::TypeSystem;

use crate::containers::{
BundledWitness, Consignment, ContentId, ContentRef, Kit, SealWitness, SigBlob, Supplement,
TrustLevel,
BundledWitness, Consignment, ContentId, ContentRef, ContentSigs, Kit, SealWitness, SigBlob,
Supplement, TrustLevel,
};
use crate::interface::{
ContractBuilder, Iface, IfaceClass, IfaceId, IfaceImpl, IfaceRef, TransitionBuilder,
Expand Down Expand Up @@ -625,6 +625,7 @@ pub trait StashReadProvider {
content_ref: ContentRef,
) -> Result<impl Iterator<Item = Supplement>, Self::Error>;

fn sigs_for(&self, content_id: &ContentId) -> Result<Option<&ContentSigs>, Self::Error>;
fn witness_ids(&self) -> Result<impl Iterator<Item = XWitnessId>, Self::Error>;
fn bundle_ids(&self) -> Result<impl Iterator<Item = BundleId>, Self::Error>;
fn bundle(&self, bundle_id: BundleId) -> Result<&TransitionBundle, ProviderError<Self::Error>>;
Expand Down
131 changes: 129 additions & 2 deletions src/persistence/stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider, E: Error>
#[derive(Clone, PartialEq, Eq, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum ConsignError {
/// unable to construct consignment: too many supplements provided.
TooManySupplements,

/// unable to construct consignment: too many terminals provided.
TooManyTerminals,

Expand Down Expand Up @@ -649,6 +652,13 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {
let outputs = outputs.as_ref();
let secret_seals = secret_seals.as_ref();

// initialize supplyments with btree set
let mut supplements = bset![];

// get genesis supplyment by contract id
self.stash
.supplement(ContentRef::Genesis(contract_id))?
.map(|genesis_suppl| supplements.insert(genesis_suppl.clone()));
// 1. Collect initial set of anchored bundles
// 1.1. Get all public outputs
let mut opouts = self.index.public_opouts(contract_id)?;
Expand Down Expand Up @@ -729,10 +739,24 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {
}

let genesis = self.stash.genesis(contract_id)?.clone();
// get schema supplyment by schema id
self.stash
.supplement(ContentRef::Schema(genesis.schema_id))?
.map(|schema_suppl| supplements.insert(schema_suppl.clone()));

let schema_ifaces = self.stash.schema(genesis.schema_id)?.clone();
let mut ifaces = BTreeMap::new();
for (iface_id, iimpl) in schema_ifaces.iimpls {
let iface = self.stash.iface(iface_id)?;
// get iface and iimpl supplyment by iface id and iimpl id
self.stash
.supplement(ContentRef::Iface(iface.iface_id()))?
.map(|iface_suppl| supplements.insert(iface_suppl.clone()));

self.stash
.supplement(ContentRef::IfaceImpl(iimpl.impl_id()))?
.map(|iimpl_suppl| supplements.insert(iimpl_suppl.clone()));

ifaces.insert(iface.clone(), iimpl);
}
let ifaces = Confined::from_collection_unsafe(ifaces);
Expand All @@ -756,6 +780,8 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {

let (types, scripts) = self.stash.extract(&schema_ifaces.schema, ifaces.keys())?;
let scripts = Confined::from_iter_unsafe(scripts.into_values());
let supplements =
Confined::try_from(supplements).map_err(|_| ConsignError::TooManySupplements)?;

// TODO: Conceal everything we do not need
// TODO: Add known sigs to the consignment
Expand All @@ -772,8 +798,8 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {
extensions: none!(),
attachments: none!(),

signatures: none!(), // TODO: Collect signatures
supplements: none!(), // TODO: Collect supplements
signatures: none!(), // TODO: Collect signatures
supplements,
types,
scripts,
})
Expand Down Expand Up @@ -1136,3 +1162,104 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {
Ok(self.stash.store_secret_seal(seal)?)
}
}

#[cfg(test)]
mod test {
use std::str::FromStr;

use baid64::FromBaid64Str;
use commit_verify::{DigestExt, Sha256};
use strict_encoding::TypeName;

use super::*;

#[test]
fn test_consign() {
let mut stock = Stock::<MemStash, MemState, MemIndex>::default();
let seal = XChain::with(
rgbcore::Layer1::Bitcoin,
GraphSeal::new_random_vout(bp::dbc::Method::OpretFirst, Vout::from_u32(0)),
);
let secret_seal = seal.conceal();

match stock.store_secret_seal(seal) {
Ok(_) => true,
Err(_) => false,
};
let contract_id =
ContractId::from_baid64_str("rgb:qFuT6DN8-9AuO95M-7R8R8Mc-AZvs7zG-obum1Va-BRnweKk")
.unwrap();
match stock.consign::<true>(contract_id, [], [secret_seal]) {
Ok(transfer) => println!("{:?}", transfer.supplements),
Err(_) => (),
};
}

#[test]
fn test_export_contract() {
let stock = Stock::<MemStash, MemState, MemIndex>::default();
let contract_id =
ContractId::from_baid64_str("rgb:qFuT6DN8-9AuO95M-7R8R8Mc-AZvs7zG-obum1Va-BRnweKk")
.unwrap();
match stock.export_contract(contract_id) {
Ok(contract) => println!("{:?}", contract.contract_id()),
Err(_) => (),
};
}

#[test]
fn test_export_schema() {
let stock = Stock::<MemStash, MemState, MemIndex>::default();
let hasher = Sha256::default();
let schema_id = SchemaId::from(hasher);
match stock.export_schema(schema_id) {
Ok(schema) => println!("{:?}", schema.kit_id()),
Err(_) => (),
};
}

#[test]
fn test_blank_builder_ifaceid() {
let stock = Stock::<MemStash, MemState, MemIndex>::default();
let hasher = Sha256::default();
let iface_id = IfaceId::from(hasher.clone());
let bytes_hash = hasher.finish();
let contract_id = ContractId::copy_from_slice(bytes_hash).unwrap();
match stock.blank_builder(contract_id, IfaceRef::Id(iface_id)) {
Ok(builder) => println!("{:?}", builder.transition_type()),
Err(_) => (),
};
}

#[test]
fn test_blank_builder_ifacename() {
let stock = Stock::<MemStash, MemState, MemIndex>::default();
let hasher = Sha256::default();
let bytes_hash = hasher.finish();
let contract_id = ContractId::copy_from_slice(bytes_hash).unwrap();
match stock.blank_builder(contract_id, IfaceRef::Name(TypeName::from_str("RGB20").unwrap()))
{
Ok(builder) => println!("{:?}", builder.transition_type()),
Err(_) => (),
};
}

#[test]
fn test_transition_builder() {
let stock = Stock::<MemStash, MemState, MemIndex>::default();
let hasher = Sha256::default();
let iface_id = IfaceId::from(hasher.clone());

let bytes_hash = hasher.finish();
let contract_id = ContractId::copy_from_slice(bytes_hash).unwrap();

match stock.transition_builder(
contract_id,
IfaceRef::Id(iface_id),
Some(FieldName::from_str("transfer").unwrap()),
) {
Ok(builder) => println!("{:?}", builder.transition_type()),
Err(_) => (),
};
}
}

0 comments on commit 8ec5e92

Please sign in to comment.