Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validation issues with seal resolution #198

Merged
merged 2 commits into from
Dec 29, 2023
Merged
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
16 changes: 12 additions & 4 deletions src/validation/consignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
use std::collections::{BTreeMap, BTreeSet};
use std::rc::Rc;

use crate::{
AnchoredBundle, AssetTag, AssignmentType, BundleId, Genesis, OpId, OpRef, Operation,
SecretSeal, SubSchema,
};
use crate::{AnchoredBundle, AssetTag, AssignmentType, BundleId, Genesis, OpId, OpRef, Operation, SecretSeal, SubSchema, WitnessId};

pub struct CheckedConsignment<'consignment, C: ConsignmentApi>(&'consignment C);

Expand Down Expand Up @@ -60,6 +57,10 @@
.anchored_bundle(bundle_id)
.filter(|ab| ab.bundle.bundle_id() == bundle_id)
}

fn op_witness_id(&self, opid: OpId) -> Option<WitnessId> {
self.0.op_witness_id(opid)
}
}

/// Trait defining common data access API for all storage-related RGB structures
Expand All @@ -70,8 +71,10 @@
/// invalid or absent data, the API must always return [`None`] or empty
/// collections/iterators.
pub trait ConsignmentApi {
/// Iterator for all bundle ids present in the consignment.
type Iter<'a>: Iterator<Item = BundleId>;

/// Returns reference to the schema object used by the consignment.
fn schema(&self) -> &SubSchema;

/// Asset tags uses in the confidential asset validation.
Expand All @@ -95,7 +98,12 @@
/// state transitions represent the final state
fn terminals(&self) -> BTreeSet<(BundleId, SecretSeal)>;

/// Returns iterator over all bundle ids present in the consignment.
fn bundle_ids<'a>(&self) -> Self::Iter<'a>;

/// Returns reference to an anchored bundle given a bundle id.
fn anchored_bundle(&self, bundle_id: BundleId) -> Option<Rc<AnchoredBundle>>;

/// Returns witness id for a given operaiton.

Check warning on line 107 in src/validation/consignment.rs

View workflow job for this annotation

GitHub Actions / typos

"operaiton" should be "operation".
fn op_witness_id(&self, opid: OpId) -> Option<WitnessId>;
}
26 changes: 17 additions & 9 deletions src/validation/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::vm::AluRuntime;
use crate::{
AltLayer1, AnchorSet, BundleId, ContractId, Layer1, OpId, OpRef, OpType, Operation, Opout,
Schema, SchemaId, SchemaRoot, Script, SubSchema, Transition, TransitionBundle, TypedAssigns,
WitnessId, XAnchor,
XAnchor,
};

#[derive(Clone, Debug, Display, Error, From)]
Expand Down Expand Up @@ -310,14 +310,13 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveTx>
// For now we use just Bitcoin and Liquid as layer1, but in the
// future we may have more validation routes for other types of
// layer1 structure.
let witness_id = anchored_bundle.anchor.witness_id();
let anchors = match &anchored_bundle.anchor {
XAnchor::Bitcoin(anchor) | XAnchor::Liquid(anchor) => anchor,
};
let bundle = &anchored_bundle.bundle;

// [VALIDATION]: We validate that the seals were properly defined on BP-type layers
let (seals, input_map) = self.validate_seal_definitions(layer1, witness_id, bundle);
let (seals, input_map) = self.validate_seal_definitions(layer1, bundle);

// [VALIDATION]: We validate that the seals were properly closed on BP-type layers
let Some(witness_tx) = self.validate_commitments_bp(layer1, &seals, bundle_id, anchors)
Expand Down Expand Up @@ -435,7 +434,6 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveTx>
fn validate_seal_definitions(
&mut self,
layer1: Layer1,
witness_id: WitnessId,
bundle: &TransitionBundle,
) -> (Vec<ExplicitSeal<Txid>>, BTreeMap<OpId, BTreeSet<Outpoint>>) {
let mut input_map: BTreeMap<OpId, BTreeSet<Outpoint>> = bmap!();
Expand Down Expand Up @@ -494,11 +492,21 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveTx>
continue;
}

let seal = seal
.try_to_output_seal(witness_id)
.expect("method must be called only on BP-compatible layer 1")
.reduce_to_bp()
.expect("method must be called only on BP-compatible layer 1");
let seal = if prev_op.op_type() == OpType::StateTransition {
let Some(witness_id) = self.consignment.op_witness_id(op) else {
self.status.add_failure(Failure::OperationAbsent(op));
continue;
};

seal.try_to_output_seal(witness_id)
.expect("method must be called only on BP-compatible layer 1")
} else {
seal.to_output_seal()
.expect("genesis and state extensions must have explicit seals")
}
.reduce_to_bp()
.expect("method must be called only on BP-compatible layer 1");

seals.push(seal);
input_map
.entry(opid)
Expand Down
Loading