Skip to content

Commit

Permalink
Merge pull request #262 from RGB-WG/validity
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky authored Jul 23, 2024
2 parents 431c58a + 4ac2692 commit 60377cf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/validation/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use strict_types::SemId;

use crate::contract::Opout;
use crate::schema::{self, SchemaId};
use crate::validation::WitnessResolverError;
use crate::{
AssignmentType, BundleId, ContractId, Layer1, OccurrencesMismatch, OpFullType, OpId, OpType,
SecretSeal, StateType, Vin, XChain, XGraphSeal, XOutputSeal, XWitnessId,
Expand Down Expand Up @@ -287,7 +288,7 @@ pub enum Failure {
/// confidential and can't be validated.
ConfidentialSeal(Opout),
/// public witness {0} is not known to the resolver.
SealNoPubWitness(XWitnessId),
SealNoPubWitness(XWitnessId, WitnessResolverError),
/// witness layer 1 {anchor} doesn't match seal definition {seal}.
SealWitnessLayer1Mismatch { seal: Layer1, anchor: Layer1 },
/// seal {1} is defined on {0} which is not in the set of layers allowed
Expand Down
45 changes: 40 additions & 5 deletions src/validation/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,19 @@ use crate::{
XWitnessId, XWitnessTx,
};

#[derive(Clone, Debug, Display, Error, From)]
#[derive(Clone, PartialEq, Eq, Debug, Display, Error, From)]
#[display(doc_comments)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub enum WitnessResolverError {
/// actual witness id {actual} doesn't match expected id {expected}.
IdMismatch {
actual: XWitnessId,
expected: XWitnessId,
},
/// witness {0} does not exist.
Unknown(XWitnessId),
/// unable to retrieve witness {0}, {1}
Expand All @@ -63,6 +73,31 @@ impl<T: ResolveWitness> ResolveWitness for &T {
}
}

struct CheckedWitnessResolver<R: ResolveWitness> {
inner: R,
}

impl<R: ResolveWitness> From<R> for CheckedWitnessResolver<R> {
fn from(inner: R) -> Self { Self { inner } }
}

impl<R: ResolveWitness> ResolveWitness for CheckedWitnessResolver<R> {
fn resolve_pub_witness(
&self,
witness_id: XWitnessId,
) -> Result<XWitnessTx, WitnessResolverError> {
let witness = self.inner.resolve_pub_witness(witness_id)?;
let actual_id = witness.witness_id();
if actual_id != witness_id {
return Err(WitnessResolverError::IdMismatch {
actual: actual_id,
expected: witness_id,
});
}
Ok(witness)
}
}

pub struct Validator<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitness> {
consignment: CheckedConsignment<'consignment, C>,

Expand All @@ -76,7 +111,7 @@ pub struct Validator<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitne
validated_op_seals: RefCell<BTreeSet<OpId>>,
validated_op_state: RefCell<BTreeSet<OpId>>,

resolver: &'resolver R,
resolver: CheckedWitnessResolver<&'resolver R>,
}

impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitness>
Expand Down Expand Up @@ -136,7 +171,7 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitness>
layers1,
validated_op_state,
validated_op_seals,
resolver,
resolver: CheckedWitnessResolver::from(resolver),
}
}

Expand Down Expand Up @@ -417,7 +452,7 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitness>
// Here the method can do SPV proof instead of querying the indexer. The SPV
// proofs can be part of the consignments, but do not require .
match self.resolver.resolve_pub_witness(witness_id) {
Err(_) => {
Err(err) => {
// We wre unable to retrieve corresponding transaction, so can't check.
// Reporting this incident and continuing further. Why this happens? No
// connection to Bitcoin Core, Electrum or other backend etc. So this is not a
Expand All @@ -429,7 +464,7 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitness>
// failure!)
self.status
.borrow_mut()
.add_failure(Failure::SealNoPubWitness(witness_id));
.add_failure(Failure::SealNoPubWitness(witness_id, err));
None
}
Ok(pub_witness) => {
Expand Down

0 comments on commit 60377cf

Please sign in to comment.