Skip to content

Commit

Permalink
Merge pull request #241 from RGB-WG/mining
Browse files Browse the repository at this point in the history
Witness filtering with refactored state management workflow
  • Loading branch information
dr-orlovsky authored Aug 9, 2024
2 parents afe14d3 + a2ff52c commit ea0e5eb
Show file tree
Hide file tree
Showing 35 changed files with 2,208 additions and 893 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.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ features = ["all"]
[patch.crates-io]
bp-consensus = { git = "https://github.com/BP-WG/bp-core", branch = "master" }
bp-invoice = { git = "https://github.com/BP-WG/bp-std.git", branch = "master" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "master" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "mining" }
4 changes: 1 addition & 3 deletions invoice/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::num::{ParseIntError, TryFromIntError};
use std::str::FromStr;

use bp::Sats;
use rgb::{FungibleState, KnownState, RevealedValue};
use rgb::{FungibleState, RevealedValue};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use strict_encoding::{StrictDeserialize, StrictSerialize, VariantError};
Expand Down Expand Up @@ -62,8 +62,6 @@ pub struct Amount(
impl StrictSerialize for Amount {}
impl StrictDeserialize for Amount {}

impl KnownState for Amount {}

impl From<RevealedValue> for Amount {
fn from(value: RevealedValue) -> Self { Amount(value.value.as_u64()) }
}
Expand Down
4 changes: 1 addition & 3 deletions invoice/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use std::str::FromStr;

use rgb::{DataState, KnownState, RevealedData};
use rgb::{DataState, RevealedData};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use strict_encoding::{StrictDeserialize, StrictSerialize};
Expand Down Expand Up @@ -156,8 +156,6 @@ impl Allocation {
impl StrictSerialize for Allocation {}
impl StrictDeserialize for Allocation {}

impl KnownState for Allocation {}

impl From<RevealedData> for Allocation {
fn from(data: RevealedData) -> Self {
Allocation::from_strict_serialized(data.value.into()).expect("invalid allocation data")
Expand Down
5 changes: 3 additions & 2 deletions src/containers/anchors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ use bp::dbc::tapret::TapretProof;
use bp::dbc::{anchor, Anchor};
use bp::{Tx, Txid};
use commit_verify::{mpc, CommitId};
use rgb::validation::{DbcProof, EAnchor};
use rgb::{
BundleDisclosure, BundleId, ContractId, DbcProof, DiscloseHash, EAnchor, Operation, Transition,
TransitionBundle, XChain, XWitnessId,
BundleDisclosure, BundleId, ContractId, DiscloseHash, Operation, Transition, TransitionBundle,
XChain, XWitnessId,
};
use strict_encoding::StrictDumb;

Expand Down
117 changes: 56 additions & 61 deletions src/containers/consignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::{BTreeMap, BTreeSet};
use std::collections::BTreeSet;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::ops::Deref;
use std::str::FromStr;
use std::{fmt, iter};

use aluvm::library::Lib;
use amplify::confinement::{
Expand All @@ -35,8 +35,8 @@ use baid64::{Baid64ParseError, DisplayBaid64, FromBaid64Str};
use commit_verify::{CommitEncode, CommitEngine, CommitId, CommitmentId, DigestExt, Sha256};
use rgb::validation::{ResolveWitness, Validator, Validity, Warning, CONSIGNMENT_MAX_LIBS};
use rgb::{
impl_serde_baid64, validation, AttachId, BundleId, ContractHistory, ContractId, Extension,
Genesis, GraphSeal, Operation, Schema, SchemaId, XChain,
impl_serde_baid64, validation, AttachId, BundleId, ContractId, Extension, Genesis, GraphSeal,
Operation, Schema, SchemaId, XChain,
};
use rgbcore::validation::ConsignmentApi;
use strict_encoding::{StrictDeserialize, StrictDumb, StrictSerialize};
Expand All @@ -47,14 +47,44 @@ use super::{
TerminalDisclose, ASCII_ARMOR_CONSIGNMENT_TYPE, ASCII_ARMOR_CONTRACT, ASCII_ARMOR_IFACE,
ASCII_ARMOR_SCHEMA, ASCII_ARMOR_TERMINAL, ASCII_ARMOR_VERSION,
};
use crate::containers::anchors::ToWitnessId;
use crate::interface::{Iface, IfaceImpl};
use crate::resolvers::{ConsignmentResolver, ResolveHeight};
use crate::resolvers::ConsignmentResolver;
use crate::{BundleExt, SecretSeal, LIB_NAME_RGB_STD};

pub type Transfer = Consignment<true>;
pub type Contract = Consignment<false>;

pub trait ConsignmentExt {
fn contract_id(&self) -> ContractId;
fn schema_id(&self) -> SchemaId;
fn schema(&self) -> &Schema;
fn genesis(&self) -> &Genesis;
fn extensions(&self) -> impl Iterator<Item = &Extension>;
fn bundled_witnesses(&self) -> impl Iterator<Item = &BundledWitness>;
}

impl<C: ConsignmentExt> ConsignmentExt for &C {
#[inline]
fn contract_id(&self) -> ContractId { (*self).contract_id() }

#[inline]
fn schema_id(&self) -> SchemaId { (*self).schema_id() }

#[inline]
fn schema(&self) -> &Schema { (*self).schema() }

#[inline]
fn genesis(&self) -> &Genesis { (*self).genesis() }

#[inline]
fn extensions(&self) -> impl Iterator<Item = &Extension> { (*self).extensions() }

#[inline]
fn bundled_witnesses(&self) -> impl Iterator<Item = &BundledWitness> {
(*self).bundled_witnesses()
}
}

/// Interface identifier.
///
/// Interface identifier commits to all the interface data.
Expand Down Expand Up @@ -229,16 +259,33 @@ impl<const TRANSFER: bool> CommitEncode for Consignment<TRANSFER> {
}
}

impl<const TRANSFER: bool> ConsignmentExt for Consignment<TRANSFER> {
#[inline]
fn contract_id(&self) -> ContractId { self.genesis.contract_id() }

#[inline]
fn schema_id(&self) -> SchemaId { self.schema.schema_id() }

#[inline]
fn schema(&self) -> &Schema { &self.schema }

#[inline]
fn genesis(&self) -> &Genesis { &self.genesis }

#[inline]
fn extensions(&self) -> impl Iterator<Item = &Extension> { self.extensions.iter() }

#[inline]
fn bundled_witnesses(&self) -> impl Iterator<Item = &BundledWitness> { self.bundles.iter() }
}

impl<const TRANSFER: bool> Consignment<TRANSFER> {
#[inline]
pub fn consignment_id(&self) -> ConsignmentId { self.commit_id() }

#[inline]
pub fn schema_id(&self) -> SchemaId { self.schema.schema_id() }

#[inline]
pub fn contract_id(&self) -> ContractId { self.genesis.contract_id() }

pub fn terminal_secrets(&self) -> impl Iterator<Item = (BundleId, XChain<SecretSeal>)> {
self.terminals
.clone()
Expand All @@ -255,58 +302,6 @@ impl<const TRANSFER: bool> Consignment<TRANSFER> {
})
}

pub fn update_history<R: ResolveHeight>(
&self,
history: Option<ContractHistory>,
resolver: &mut R,
) -> Result<ContractHistory, String> {
let mut history = history.unwrap_or_else(|| {
ContractHistory::with(self.schema_id(), self.contract_id(), &self.genesis)
});

let mut extension_idx = self
.extensions
.iter()
.map(Extension::id)
.zip(iter::repeat(false))
.collect::<BTreeMap<_, _>>();
let mut ordered_extensions = BTreeMap::new();
for bundled_witness in &self.bundles {
for bundle in bundled_witness.anchored_bundles.bundles() {
for transition in bundle.known_transitions.values() {
let witness_anchor =
resolver.resolve_height(bundled_witness.pub_witness.to_witness_id())?;

history.add_transition(transition, witness_anchor);
for (id, used) in &mut extension_idx {
if *used {
continue;
}
for input in &transition.inputs {
if input.prev_out.op == *id {
*used = true;
if let Some(ord) = ordered_extensions.get_mut(id) {
if *ord > witness_anchor {
*ord = witness_anchor;
}
} else {
ordered_extensions.insert(*id, witness_anchor);
}
}
}
}
}
}
}
for extension in &self.extensions {
if let Some(witness_anchor) = ordered_extensions.get(&extension.id()) {
history.add_extension(extension, *witness_anchor);
}
}

Ok(history)
}

#[must_use]
pub fn reveal_bundle_seal(mut self, bundle_id: BundleId, revealed: XChain<GraphSeal>) -> Self {
// We need to clone since ordered set does not allow us to mutate members.
Expand Down
6 changes: 3 additions & 3 deletions src/containers/indexed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ use std::ops::Deref;

use amplify::confinement::Collection;
use commit_verify::Conceal;
use rgb::validation::{ConsignmentApi, Scripts};
use rgb::validation::{ConsignmentApi, EAnchor, Scripts};
use rgb::{
BundleId, EAnchor, Extension, Genesis, OpId, OpRef, Operation, Schema, Transition,
TransitionBundle, XChain, XWitnessId,
BundleId, Extension, Genesis, OpId, OpRef, Operation, Schema, Transition, TransitionBundle,
XChain, XWitnessId,
};
use strict_types::TypeSystem;

Expand Down
3 changes: 2 additions & 1 deletion src/containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub use anchors::{
AnchorSet, AnchoredBundles, BundledWitness, PubWitness, SealWitness, ToWitnessId, XPubWitness,
};
pub use consignment::{
Consignment, ConsignmentId, Contract, Transfer, ValidConsignment, ValidContract, ValidTransfer,
Consignment, ConsignmentExt, ConsignmentId, Contract, Transfer, ValidConsignment,
ValidContract, ValidTransfer,
};
pub use disclosure::Disclosure;
pub use file::{FileContent, LoadError, UniversalFile};
Expand Down
Loading

0 comments on commit ea0e5eb

Please sign in to comment.