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

Validation: check that all actual witness ids match the expected value #262

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 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 extected if {expected}.
dr-orlovsky marked this conversation as resolved.
Show resolved Hide resolved
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
Loading