Skip to content

Commit

Permalink
Merge pull request RGB-WG#148 from RGB-WG/v0.11
Browse files Browse the repository at this point in the history
New transfer id workflows
  • Loading branch information
dr-orlovsky authored Feb 28, 2024
2 parents 1f84bbb + 0a9c92e commit 6801aa5
Show file tree
Hide file tree
Showing 40 changed files with 3,125 additions and 1,779 deletions.
263 changes: 138 additions & 125 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 7 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ edition = "2021"
license = "Apache-2.0"

[workspace.dependencies]
amplify = "4.5.0"
amplify = "4.6.0"
baid58 = "0.4.4"
strict_encoding = "2.6.2"
strict_types = "1.6.3"
commit_verify = { version = "0.11.0-beta.3", features = ["stl"] }
bp-core = { version = "0.11.0-beta.3", features = ["stl"] }
bp-invoice = { version = "0.11.0-beta.3.1" }
strict_encoding = "2.7.0-beta.1"
strict_types = "2.7.0-beta.1"
commit_verify = { version = "0.11.0-beta.4", features = ["stl"] }
bp-core = { version = "0.11.0-beta.4", features = ["stl"] }
bp-invoice = { version = "0.11.0-beta.4" }
rgb-core = { version = "0.11.0-beta.4", features = ["stl"] }
indexmap = "2.0.2"
serde_crate = { package = "serde", version = "1", features = ["derive"] }
Expand Down Expand Up @@ -93,9 +93,4 @@ wasm-bindgen-test = "0.3"
features = [ "all" ]

[patch.crates-io]
bp-consensus = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" }
bp-dbc = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" }
bp-seals = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" }
bp-core = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" }
bp-invoice = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "v0.11" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "master" }
18 changes: 9 additions & 9 deletions src/accessors/merge_reveal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use std::collections::BTreeMap;
use amplify::confinement::Confined;
use amplify::Wrapper;
use bp::dbc::anchor::MergeError;
use commit_verify::{mpc, CommitmentId};
use commit_verify::mpc;
use rgb::{
AnchorSet, Assign, Assignments, ExposedSeal, ExposedState, Extension, Genesis, OpId,
AnchorSet, Assign, Assignments, ExposedSeal, ExposedState, Extension, Genesis, OpId, Operation,
Transition, TransitionBundle, TypedAssigns, XAnchor,
};

Expand Down Expand Up @@ -186,7 +186,7 @@ impl<Seal: ExposedSeal> MergeReveal for Assignments<Seal> {

impl MergeReveal for TransitionBundle {
fn merge_reveal(mut self, other: Self) -> Result<Self, MergeRevealError> {
debug_assert_eq!(self.commitment_id(), other.commitment_id());
debug_assert_eq!(self.bundle_id(), other.bundle_id());

let mut self_transitions = self.known_transitions.into_inner();
for (opid, other_transition) in other.known_transitions {
Expand Down Expand Up @@ -264,8 +264,8 @@ impl MergeReveal for AnchorSet<mpc::MerkleBlock> {

impl MergeReveal for Genesis {
fn merge_reveal(mut self, other: Self) -> Result<Self, MergeRevealError> {
let self_id = self.commitment_id();
let other_id = other.commitment_id();
let self_id = self.id();
let other_id = other.id();
if self_id != other_id {
return Err(MergeRevealError::OperationMismatch(
OpId::from_inner(self_id.into_inner()),
Expand All @@ -279,8 +279,8 @@ impl MergeReveal for Genesis {

impl MergeReveal for Transition {
fn merge_reveal(mut self, other: Self) -> Result<Self, MergeRevealError> {
let self_id = self.commitment_id();
let other_id = other.commitment_id();
let self_id = self.id();
let other_id = other.id();
if self_id != other_id {
return Err(MergeRevealError::OperationMismatch(self_id, other_id));
}
Expand All @@ -291,8 +291,8 @@ impl MergeReveal for Transition {

impl MergeReveal for Extension {
fn merge_reveal(mut self, other: Self) -> Result<Self, MergeRevealError> {
let self_id = self.commitment_id();
let other_id = other.commitment_id();
let self_id = self.id();
let other_id = other.id();
if self_id != other_id {
return Err(MergeRevealError::OperationMismatch(self_id, other_id));
}
Expand Down
43 changes: 23 additions & 20 deletions src/containers/bindle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
use std::collections::BTreeMap;
use std::fmt::{Debug, Display};
use std::io::{self, Read};
use std::ops::{Deref, DerefMut};
use std::ops::Deref;
use std::str::FromStr;

use amplify::confinement::{self, Confined, TinyVec, U24};
use amplify::confinement::{self, Confined, TinyVec, U24 as U24MAX};
use baid58::Baid58ParseError;
use rgb::{BundleId, ContractId, Schema, SchemaId, SchemaRoot, SubSchema};
use strict_encoding::{
StrictDecode, StrictDeserialize, StrictDumb, StrictEncode, StrictReader, StrictSerialize,
StreamReader, StrictDecode, StrictDeserialize, StrictDumb, StrictEncode, StrictSerialize,
StrictType,
};

Expand All @@ -42,6 +42,8 @@ use crate::containers::{Cert, Contract, Transfer};
use crate::interface::{Iface, IfaceId, IfaceImpl, ImplId};
use crate::LIB_NAME_RGB_STD;

pub const BINDLE_MAX_LEN: usize = U24MAX;

// TODO: Move to UBIDECO crate
pub trait BindleContent: StrictSerialize + StrictDeserialize + StrictDumb {
/// Magic bytes used in saving/restoring container from a file.
Expand Down Expand Up @@ -155,10 +157,6 @@ impl<C: BindleContent> Deref for Bindle<C> {
fn deref(&self) -> &Self::Target { &self.data }
}

impl<C: BindleContent> DerefMut for Bindle<C> {
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.data }
}

impl<C: BindleContent> From<C> for Bindle<C> {
fn from(data: C) -> Self { Bindle::new(data) }
}
Expand Down Expand Up @@ -229,7 +227,7 @@ impl<C: BindleContent> FromStr for Bindle<C> {
}
let armor = lines.filter(|l| !l.is_empty()).collect::<String>();
let data = base85::decode(&armor)?;
let data = C::from_strict_serialized::<U24>(Confined::try_from(data)?)?;
let data = C::from_strict_serialized::<BINDLE_MAX_LEN>(Confined::try_from(data)?)?;
let id = data.bindle_id();
if let Some(header_id) = header_id {
if header_id != id {
Expand Down Expand Up @@ -265,7 +263,10 @@ impl<C: BindleContent> Display for Bindle<C> {
writeln!(f)?;

// TODO: Replace with streamed writer
let data = self.data.to_strict_serialized::<U24>().expect("in-memory");
let data = self
.data
.to_strict_serialized::<BINDLE_MAX_LEN>()
.expect("in-memory");
let data = base85::encode(&data);
let mut data = data.as_str();
while data.len() >= 64 {
Expand All @@ -289,8 +290,9 @@ impl<C: BindleContent> Bindle<C> {
if rgb != *b"RGB" || magic != C::MAGIC {
return Err(LoadError::InvalidMagic);
}
let mut reader = StrictReader::with(usize::MAX, data);
let me = Self::strict_decode(&mut reader)?;
const USIZE_MAX: usize = usize::MAX;
let reader = StreamReader::new::<USIZE_MAX>(data);
let me = Self::strict_read(reader)?;
Ok(me)
}
}
Expand Down Expand Up @@ -330,13 +332,14 @@ impl UniversalBindle {
if rgb != *b"RGB" {
return Err(LoadError::InvalidMagic);
}
let mut reader = StrictReader::with(usize::MAX, data);
const USIZE_MAX: usize = usize::MAX;
let mut reader = StreamReader::new::<USIZE_MAX>(data);
Ok(match magic {
x if x == Iface::MAGIC => Bindle::<Iface>::strict_decode(&mut reader)?.into(),
x if x == SubSchema::MAGIC => Bindle::<SubSchema>::strict_decode(&mut reader)?.into(),
x if x == IfaceImpl::MAGIC => Bindle::<IfaceImpl>::strict_decode(&mut reader)?.into(),
x if x == Contract::MAGIC => Bindle::<Contract>::strict_decode(&mut reader)?.into(),
x if x == Transfer::MAGIC => Bindle::<Transfer>::strict_decode(&mut reader)?.into(),
x if x == Iface::MAGIC => Bindle::<Iface>::strict_read(&mut reader)?.into(),
x if x == SubSchema::MAGIC => Bindle::<SubSchema>::strict_read(&mut reader)?.into(),
x if x == IfaceImpl::MAGIC => Bindle::<IfaceImpl>::strict_read(&mut reader)?.into(),
x if x == Contract::MAGIC => Bindle::<Contract>::strict_read(&mut reader)?.into(),
x if x == Transfer::MAGIC => Bindle::<Transfer>::strict_read(&mut reader)?.into(),
_ => return Err(LoadError::InvalidMagic),
})
}
Expand All @@ -360,7 +363,7 @@ mod _fs {
use std::path::Path;
use std::{fs, io};

use strict_encoding::{StrictEncode, StrictWriter};
use strict_encoding::{StreamWriter, StrictEncode};

use super::*;

Expand All @@ -374,8 +377,8 @@ mod _fs {
let mut file = fs::File::create(path)?;
file.write_all(b"RGB")?;
file.write_all(&C::MAGIC)?;
let writer = StrictWriter::with(usize::MAX, file);
self.strict_encode(writer)?;
let writer = StreamWriter::new::<BINDLE_MAX_LEN>(file);
self.strict_write(writer)?;
Ok(())
}
}
Expand Down
35 changes: 28 additions & 7 deletions src/containers/consignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
use std::collections::BTreeMap;
use std::iter;

use amplify::confinement::{LargeVec, MediumBlob, SmallOrdMap, TinyOrdMap, TinyOrdSet};
use amplify::confinement::{LargeOrdSet, MediumBlob, SmallOrdMap, TinyOrdMap, TinyOrdSet};
use bp::Tx;
use rgb::validation::{self};
use rgb::{
AnchoredBundle, AssetTag, AssignmentType, AttachId, BundleId, ContractHistory, ContractId,
Expand All @@ -31,7 +32,9 @@ use rgb::{
};
use strict_encoding::{StrictDeserialize, StrictDumb, StrictSerialize};

use super::{Bindle, BindleContent, ContainerVer, ContentId, ContentSigs, Terminal};
use super::{
Bindle, BindleContent, ContainerVer, ContentId, ContentSigs, Terminal, TerminalDisclose,
};
use crate::accessors::BundleExt;
use crate::interface::{ContractSuppl, IfaceId, IfacePair};
use crate::resolvers::ResolveHeight;
Expand Down Expand Up @@ -90,13 +93,13 @@ pub struct Consignment<const TYPE: bool> {
pub genesis: Genesis,

/// Set of seals which are history terminals.
pub terminals: SmallOrdMap<BundleId, XChain<Terminal>>,
pub terminals: SmallOrdMap<BundleId, Terminal>,

/// Data on all anchored state transitions contained in the consignments.
pub bundles: LargeVec<AnchoredBundle>,
pub bundles: LargeOrdSet<AnchoredBundle>,

/// Data on all state extensions contained in the consignments.
pub extensions: LargeVec<Extension>,
pub extensions: LargeOrdSet<Extension>,

/// Data containers coming with this consignment. For the purposes of
/// in-memory consignments we are restricting the size of the containers to
Expand Down Expand Up @@ -167,6 +170,19 @@ impl<const TYPE: bool> Consignment<TYPE> {
.find(|&extension| extension.id() == opid)
}

pub fn terminals_disclose(&self) -> impl Iterator<Item = TerminalDisclose> + '_ {
self.terminals.iter().flat_map(|(id, term)| {
term.seals.iter().map(|seal| TerminalDisclose {
bundle_id: *id,
seal: *seal,
witness_id: term
.witness_tx
.as_ref()
.map(|witness| witness.map_ref(Tx::txid)),
})
})
}

pub fn validation_status(&self) -> Option<&validation::Status> {
self.validation_status.as_ref()
}
Expand Down Expand Up @@ -227,12 +243,17 @@ impl<const TYPE: bool> Consignment<TYPE> {
Ok(history)
}

pub fn reveal_bundle_seal(&mut self, bundle_id: BundleId, revealed: XChain<GraphSeal>) {
for anchored_bundle in &mut self.bundles {
#[must_use]
pub fn reveal_bundle_seal(mut self, bundle_id: BundleId, revealed: XChain<GraphSeal>) -> Self {
let mut bundles = LargeOrdSet::with_capacity(self.bundles.len());
for mut anchored_bundle in self.bundles {
if anchored_bundle.bundle.bundle_id() == bundle_id {
anchored_bundle.bundle.reveal_seal(revealed);
}
bundles.push(anchored_bundle).ok();
}
self.bundles = bundles;
self
}

pub fn into_contract(self) -> Contract {
Expand Down
18 changes: 4 additions & 14 deletions src/containers/indexed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::{BTreeMap, BTreeSet};
use std::collections::{btree_set, BTreeMap, BTreeSet};
use std::ops::Deref;
use std::rc::Rc;
use std::vec;

use amplify::confinement::Collection;
use commit_verify::Conceal;
Expand Down Expand Up @@ -85,17 +84,8 @@ impl<'c, const TYPE: bool> ConsignmentApi for IndexedConsignment<'c, TYPE> {
fn terminals(&self) -> BTreeSet<(BundleId, XChain<SecretSeal>)> {
let mut set = BTreeSet::new();
for (bundle_id, terminal) in &self.terminals {
match terminal {
XChain::Bitcoin(term) => {
for seal in &term.seals {
set.push((*bundle_id, XChain::Bitcoin(seal.conceal())));
}
}
XChain::Liquid(term) => {
for seal in &term.seals {
set.push((*bundle_id, XChain::Liquid(seal.conceal())));
}
}
for seal in &terminal.seals {
set.push((*bundle_id, seal.conceal()));
}
}
set
Expand All @@ -115,7 +105,7 @@ impl<'c, const TYPE: bool> ConsignmentApi for IndexedConsignment<'c, TYPE> {
}

#[derive(Debug)]
pub struct BundleIdIter(vec::IntoIter<AnchoredBundle>);
pub struct BundleIdIter(btree_set::IntoIter<AnchoredBundle>);

impl Iterator for BundleIdIter {
type Item = BundleId;
Expand Down
2 changes: 1 addition & 1 deletion src/containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ pub use disclosure::Disclosure;
pub use indexed::IndexedConsignment;
pub use partials::{Batch, CloseMethodSet, Fascia, TransitionInfo};
pub use seal::{BuilderSeal, TerminalSeal, VoutSeal};
pub use util::{ContainerVer, Terminal};
pub use util::{ContainerVer, Terminal, TerminalDisclose};
Loading

0 comments on commit 6801aa5

Please sign in to comment.