Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,6 @@ wasm-bindgen-test = "0.3"

[package.metadata.docs.rs]
features = ["all"]

[patch.crates-io]
rgb-core = { git = "https://github.com/zoedberg/rgb-core", branch = "0.11.1-4" }
6 changes: 3 additions & 3 deletions invoice/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ impl Display for RgbTransport {
match self {
RgbTransport::JsonRpc { tls, host } => {
let s = if *tls { "s" } else { "" };
write!(f, "rpc{s}{TRANSPORT_HOST_SEP}{}", host)?;
write!(f, "rpc{s}{TRANSPORT_HOST_SEP}{host}")?;
}
RgbTransport::RestHttp { tls, host } => {
let s = if *tls { "s" } else { "" };
write!(f, "http{s}{TRANSPORT_HOST_SEP}{}", host)?;
write!(f, "http{s}{TRANSPORT_HOST_SEP}{host}")?;
}
RgbTransport::WebSockets { tls, host } => {
let s = if *tls { "s" } else { "" };
write!(f, "ws{s}{TRANSPORT_HOST_SEP}{}", host)?;
write!(f, "ws{s}{TRANSPORT_HOST_SEP}{host}")?;
}
RgbTransport::Storm {} => {
write!(f, "storm{TRANSPORT_HOST_SEP}_/")?;
Expand Down
24 changes: 15 additions & 9 deletions src/containers/consignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::ops::Deref;
use std::str::FromStr;

use aluvm::library::Lib;
use amplify::confinement::{Confined, LargeOrdSet, SmallOrdMap, SmallOrdSet};
use amplify::confinement::{Confined, LargeVec, SmallOrdMap, SmallOrdSet};
use amplify::{ByteArray, Bytes32};
use armor::{ArmorHeader, AsciiArmor, StrictArmor, StrictArmorError};
use baid64::{Baid64ParseError, DisplayBaid64, FromBaid64Str};
Expand Down Expand Up @@ -185,7 +185,7 @@ pub struct Consignment<const TRANSFER: bool> {

/// All bundled state transitions contained in the consignment, together
/// with their witness data.
pub bundles: LargeOrdSet<WitnessBundle>,
pub bundles: LargeVec<WitnessBundle>,

/// Schema (plus root schema, if any) under which contract is issued.
pub schema: Schema,
Expand All @@ -210,7 +210,7 @@ impl<const TRANSFER: bool> CommitEncode for Consignment<TRANSFER> {
e.commit_to_serialized(&self.contract_id());
e.commit_to_serialized(&self.genesis.disclose_hash());

e.commit_to_set(&LargeOrdSet::from_iter_checked(
e.commit_to_list(&LargeVec::from_iter_checked(
self.bundles.iter().map(WitnessBundle::commit_id),
));
e.commit_to_map(&self.terminals);
Expand Down Expand Up @@ -249,7 +249,7 @@ impl<const TRANSFER: bool> Consignment<TRANSFER> {
f: impl Fn(SecretSeal) -> Result<Option<GraphSeal>, E>,
) -> Result<Self, E> {
// We need to clone since ordered set does not allow us to mutate members.
let mut bundles = LargeOrdSet::with_capacity(self.bundles.len());
let mut bundles = LargeVec::with_capacity(self.bundles.len());
for mut witness_bundle in self.bundles {
for (bundle_id, secrets) in &self.terminals {
for secret in secrets {
Expand Down Expand Up @@ -280,8 +280,14 @@ impl<const TRANSFER: bool> Consignment<TRANSFER> {
pub fn replace_transitions_input_ops(&self) -> BTreeSet<OpId> {
self.bundles
.iter()
.flat_map(|b| b.bundle().known_transitions.values())
.filter(|t| t.transition_type.is_replace())
.flat_map(|b| b.bundle().known_transitions.as_unconfined())
.filter_map(|kt| {
if kt.transition.transition_type.is_replace() {
Some(&kt.transition)
} else {
None
}
})
.flat_map(|t| t.inputs.iter())
.filter(|i| i.ty.is_asset())
.map(|i| i.op)
Expand Down Expand Up @@ -343,18 +349,18 @@ impl<const TRANSFER: bool> Consignment<TRANSFER> {
pub fn modify_bundle<F>(&mut self, witness_id: Txid, modifier: F) -> bool
where F: Fn(&mut WitnessBundle) {
let mut found = false;
let mut modified_bundles = BTreeSet::new();
let mut modified_bundles = Vec::new();

let bundles: Vec<_> = self.bundles.iter().cloned().collect();

for bundle in bundles {
if bundle.witness_id() == witness_id {
let mut modified_bundle = bundle.clone();
modifier(&mut modified_bundle);
modified_bundles.insert(modified_bundle);
modified_bundles.push(modified_bundle);
found = true;
} else {
modified_bundles.insert(bundle);
modified_bundles.push(bundle);
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/containers/indexed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ impl<'c, const TRANSFER: bool> IndexedConsignment<'c, TRANSFER> {
let bundle_id = bundle.bundle_id();
bundle_idx.insert(bundle_id, bundle);
anchor_idx.insert(bundle_id, (witness_id, anchor));
for opid in bundle.known_transitions.keys() {
op_witness_idx.insert(*opid, witness_id);
op_bundle_idx.insert(*opid, bundle_id);
for opid in bundle.known_transitions_opids() {
op_witness_idx.insert(opid, witness_id);
op_bundle_idx.insert(opid, bundle_id);
}
}
let scripts = Scripts::from_iter_checked(
Expand All @@ -89,7 +89,7 @@ impl<'c, const TRANSFER: bool> IndexedConsignment<'c, TRANSFER> {
self.op_bundle_idx
.get(&opid)
.and_then(|id| self.bundle_idx.get(id))
.and_then(|bundle| bundle.known_transitions.get(&opid))
.and_then(|bundle| bundle.get_transition(opid))
}

pub fn pub_witness(&self, id: Txid) -> Option<&PubWitness> {
Expand All @@ -113,6 +113,14 @@ impl<const TRANSFER: bool> ConsignmentApi for IndexedConsignment<'_, TRANSFER> {

fn genesis(&self) -> &Genesis { &self.genesis }

fn bundles<'iter>(&self) -> impl Iterator<Item = TransitionBundle> + 'iter {
self.consignment
.bundles
.clone()
.into_iter()
.map(|wb| wb.bundle)
}

fn bundle_ids<'iter>(&self) -> impl Iterator<Item = BundleId> + 'iter {
self.bundle_idx
.keys()
Expand Down
2 changes: 1 addition & 1 deletion src/containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub use consignment::{
pub use file::{FileContent, LoadError, UniversalFile};
pub use indexed::IndexedConsignment;
pub use kit::{Kit, KitId, ValidKit};
pub use partials::{Batch, Fascia, TransitionInfo, TransitionInfoError};
pub use partials::{Batch, Fascia};
pub use seal::{BuilderSeal, VoutSeal};
pub use util::{ContainerVer, SecretSeals};

Expand Down
84 changes: 7 additions & 77 deletions src/containers/partials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,85 +19,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::hash::{Hash, Hasher};

use amplify::confinement::{Confined, NonEmptyOrdMap, U24};
use bp::Outpoint;
use rgb::{ContractId, OpId, Operation, OutputSeal, Transition, TransitionBundle, Txid};
use rgb::{ContractId, Transition, TransitionBundle, Txid};
use strict_encoding::{
StrictDecode, StrictDeserialize, StrictDumb, StrictEncode, StrictSerialize, StrictType,
};

use super::SealWitness;
use crate::LIB_NAME_RGB_STD;

#[derive(Clone, Eq, Debug)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STD)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct TransitionInfo {
pub id: OpId,
pub inputs: Confined<BTreeSet<Outpoint>, 1, U24>,
pub transition: Transition,
}

impl StrictDumb for TransitionInfo {
fn strict_dumb() -> Self { Self::new(strict_dumb!(), [strict_dumb!()]).unwrap() }
}

impl PartialEq for TransitionInfo {
fn eq(&self, other: &Self) -> bool { self.id.eq(&other.id) }
}

impl Ord for TransitionInfo {
fn cmp(&self, other: &Self) -> Ordering { self.id.cmp(&other.id) }
}

impl PartialOrd for TransitionInfo {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
}

impl Hash for TransitionInfo {
fn hash<H: Hasher>(&self, state: &mut H) { state.write(self.id.as_slice()) }
}

impl TransitionInfo {
/// # Panics
///
/// If the number of provided seals is zero.
pub fn new(
transition: Transition,
seals: impl AsRef<[OutputSeal]>,
) -> Result<Self, TransitionInfoError> {
let id = transition.id();
let seals = seals.as_ref();
assert!(!seals.is_empty(), "empty seals provided to transition info constructor");
let inputs = Confined::<BTreeSet<_>, 1, U24>::try_from_iter(
seals.iter().copied().map(Outpoint::from),
)
.map_err(|_| TransitionInfoError::TooMany(id))?;
Ok(TransitionInfo {
id,
inputs,
transition,
})
}
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
#[display(doc_comments)]
pub enum TransitionInfoError {
/// the operation produces too many state transitions which can't fit the
/// container requirements.
TooMany(OpId),
}

/// A batch of state transitions under different contracts which are associated
/// with some specific transfer and will be anchored within a single layer 1
/// transaction.
Expand All @@ -110,15 +40,15 @@ pub enum TransitionInfoError {
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct Batch {
pub main: TransitionInfo,
pub extras: Confined<Vec<TransitionInfo>, 0, { U24 - 1 }>,
pub main: Transition,
pub extras: Confined<Vec<Transition>, 0, { U24 - 1 }>,
}

impl StrictSerialize for Batch {}
impl StrictDeserialize for Batch {}

impl IntoIterator for Batch {
type Item = TransitionInfo;
type Item = Transition;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
Expand All @@ -130,9 +60,9 @@ impl IntoIterator for Batch {

impl Batch {
pub fn set_priority(&mut self, priority: u64) {
self.main.transition.nonce = priority;
for info in &mut self.extras {
info.transition.nonce = priority;
self.main.nonce = priority;
for transition in &mut self.extras {
transition.nonce = priority;
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/contract/merge_reveal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ impl MergeReveal for TransitionBundle {
debug_assert_eq!(self.bundle_id(), other.bundle_id());

let mut self_transitions = self.known_transitions.to_unconfined();
for (opid, other_transition) in &other.known_transitions {
if let Some(transition) = self_transitions.get_mut(opid) {
transition.merge_reveal(other_transition)?;
for other in &other.known_transitions {
if let Some(kt) = self_transitions.iter_mut().find(|kt| kt.opid == other.opid) {
kt.transition.merge_reveal(&other.transition)?;
} else {
self_transitions.push(other.clone());
}
}
self.known_transitions = Confined::from_checked(self_transitions);
Expand Down
4 changes: 2 additions & 2 deletions src/persistence/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use bp::{Outpoint, Txid};
use nonasync::persistence::{CloneNoPersistence, Persisting};
use rgb::{
Assign, AssignmentType, BundleId, ContractId, ExposedState, Genesis, GenesisSeal, GraphSeal,
OpId, Operation, Opout, TransitionBundle, TypedAssigns,
KnownTransition, OpId, Operation, Opout, TransitionBundle, TypedAssigns,
};

use crate::containers::{ConsignmentExt, ToWitnessId, WitnessBundle};
Expand Down Expand Up @@ -211,7 +211,7 @@ impl<P: IndexProvider> Index<P> {
self.provider
.register_bundle(bundle_id, witness_id, contract_id)?;

for (opid, transition) in &bundle.known_transitions {
for KnownTransition { opid, transition } in &bundle.known_transitions {
self.provider.register_operation(*opid, bundle_id)?;
for input in &transition.inputs {
self.provider.register_spending(input.op, bundle_id)?;
Expand Down
8 changes: 4 additions & 4 deletions src/persistence/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use nonasync::persistence::{CloneNoPersistence, Persisting};
use rgb::validation::{ResolveWitness, WitnessResolverError};
use rgb::vm::{ContractStateAccess, WitnessOrd};
use rgb::{
BundleId, ContractId, Genesis, RevealedData, RevealedValue, Schema, SchemaId, Transition,
TransitionBundle, Txid, VoidState,
BundleId, ContractId, Genesis, KnownTransition, RevealedData, RevealedValue, Schema, SchemaId,
Transition, TransitionBundle, Txid, VoidState,
};

use crate::containers::{ConsignmentExt, ToWitnessId};
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<P: StateProvider> State<P> {
.map_err(StateError::WriteProvider)?
.ok_or(StateInconsistency::UnknownContract(contract_id))?;
let bundle_id = bundle.bundle_id();
for transition in bundle.known_transitions.values() {
for KnownTransition { transition, .. } in &bundle.known_transitions {
let ord = resolver
.resolve_pub_witness_ord(witness_id)
.map_err(|e| StateError::Resolver(witness_id, e))?;
Expand All @@ -175,7 +175,7 @@ impl<P: StateProvider> State<P> {
for witness_bundle in consignment.bundled_witnesses() {
let bundle = witness_bundle.bundle();
let bundle_id = bundle.bundle_id();
for (_, transition) in &bundle.known_transitions {
for KnownTransition { transition, .. } in &bundle.known_transitions {
let witness_id = witness_bundle.pub_witness.to_witness_id();
let witness_ord = resolver
.resolve_pub_witness_ord(witness_id)
Expand Down
Loading
Loading