Skip to content

Commit

Permalink
containers: make Batch deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Dec 18, 2023
1 parent 84e00d4 commit b65cf9e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ pub use bindle::{Bindle, BindleContent, BindleParseError, LoadError, UniversalBi
pub use certs::{Cert, ContentId, ContentSigs, Identity};
pub use consignment::{Consignment, Contract, Transfer};
pub use disclosure::Disclosure;
pub use partials::{Batch, BatchItem, CloseMethodSet, Fascia};
pub use partials::{Batch, CloseMethodSet, Fascia, TransitionInfo};
pub use seal::{BuilderSeal, TerminalSeal, VoutSeal};
pub use util::{ContainerVer, Terminal, XchainOutpoint};
36 changes: 27 additions & 9 deletions src/containers/partials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::hash::{Hash, Hasher};
use std::ops::{BitOr, BitOrAssign};
use std::vec;

Expand Down Expand Up @@ -100,26 +102,42 @@ impl CloseMethodSet {
pub fn has_opret_first(self) -> bool { matches!(self, Self::OpretFirst | Self::Both) }
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[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 BatchItem {
pub struct TransitionInfo {
pub id: OpId,
pub inputs: Confined<Vec<XchainOutpoint>, 1, U24>,
pub transition: Transition,
pub methods: CloseMethodSet,
}

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

impl BatchItem {
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 {
pub fn new(
transition: Transition,
seals: impl AsRef<[OutputSeal]>,
Expand All @@ -139,7 +157,7 @@ impl BatchItem {
})
})
.expect("confinement guarantees at least one item");
Ok(BatchItem {
Ok(TransitionInfo {
id: transition.id(),
inputs,
transition,
Expand All @@ -160,16 +178,16 @@ impl BatchItem {
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct Batch {
pub main: BatchItem,
pub blanks: Confined<Vec<BatchItem>, 0, { U24 - 1 }>,
pub main: TransitionInfo,
pub blanks: Confined<Vec<TransitionInfo>, 0, { U24 - 1 }>,
}

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

impl IntoIterator for Batch {
type Item = BatchItem;
type IntoIter = vec::IntoIter<BatchItem>;
type Item = TransitionInfo;
type IntoIter = vec::IntoIter<TransitionInfo>;

fn into_iter(self) -> Self::IntoIter {
let mut vec = self.blanks.into_inner();
Expand Down
8 changes: 4 additions & 4 deletions src/persistence/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ use strict_encoding::TypeName;

use crate::accessors::{BundleExt, MergeRevealError, RevealError};
use crate::containers::{
Batch, BatchItem, Bindle, BuilderSeal, Cert, Consignment, ContentId, Contract, Fascia,
Terminal, Transfer,
Batch, Bindle, BuilderSeal, Cert, Consignment, ContentId, Contract, Fascia, Terminal, Transfer,
TransitionInfo,
};
use crate::interface::{
BuilderError, ContractIface, Iface, IfaceId, IfaceImpl, IfacePair, IfaceWrapper,
Expand Down Expand Up @@ -834,11 +834,11 @@ pub trait Inventory: Deref<Target = Self::Stash> {
}

let transition = blank_builder.complete_transition(contract_id)?;
blanks.push(BatchItem::new(transition, outputs)?)?;
blanks.push(TransitionInfo::new(transition, outputs)?)?;
}

Ok(Batch {
main: BatchItem::new(main_transition, main_inputs)?,
main: TransitionInfo::new(main_transition, main_inputs)?,
blanks,
})
}
Expand Down

0 comments on commit b65cf9e

Please sign in to comment.