|
| 1 | +//! `chain` rule type impl. |
| 2 | +
|
| 3 | +use catalyst_signed_doc_spec::{is_required::IsRequired, metadata::chain::Chain, DocSpecs}; |
| 4 | + |
| 5 | +use crate::{providers::CatalystSignedDocumentProvider, CatalystSignedDocument}; |
| 6 | + |
| 7 | +#[cfg(test)] |
| 8 | +mod tests; |
| 9 | + |
| 10 | +/// `chain` field validation rule |
| 11 | +#[derive(Debug)] |
| 12 | +pub(crate) enum ChainRule { |
| 13 | + /// Is 'chain' specified |
| 14 | + #[allow(dead_code)] |
| 15 | + Specified { |
| 16 | + /// optional flag for the `chain` field |
| 17 | + optional: bool, |
| 18 | + }, |
| 19 | + /// 'chain' is not specified |
| 20 | + NotSpecified, |
| 21 | +} |
| 22 | + |
| 23 | +impl ChainRule { |
| 24 | + /// Generating `ChainRule` from specs |
| 25 | + pub(crate) fn new( |
| 26 | + _docs: &DocSpecs, |
| 27 | + spec: &Chain, |
| 28 | + ) -> Self { |
| 29 | + let optional = match spec.required { |
| 30 | + IsRequired::Yes => false, |
| 31 | + IsRequired::Optional => true, |
| 32 | + IsRequired::Excluded => { |
| 33 | + return Self::NotSpecified; |
| 34 | + }, |
| 35 | + }; |
| 36 | + |
| 37 | + Self::Specified { optional } |
| 38 | + } |
| 39 | + |
| 40 | + /// Field validation rule |
| 41 | + #[allow(clippy::too_many_lines)] |
| 42 | + pub(crate) async fn check<Provider>( |
| 43 | + &self, |
| 44 | + doc: &CatalystSignedDocument, |
| 45 | + provider: &Provider, |
| 46 | + ) -> anyhow::Result<bool> |
| 47 | + where |
| 48 | + Provider: CatalystSignedDocumentProvider, |
| 49 | + { |
| 50 | + let chain = doc.doc_meta().chain(); |
| 51 | + |
| 52 | + // TODO: the current implementation is only for the direct chained doc, |
| 53 | + // make it recursively checks the entire chain with the same `id` docs. |
| 54 | + |
| 55 | + if let Self::Specified { optional } = self { |
| 56 | + if chain.is_none() && !optional { |
| 57 | + doc.report() |
| 58 | + .missing_field("chain", "Document must have 'chain' field"); |
| 59 | + return Ok(false); |
| 60 | + } |
| 61 | + |
| 62 | + // perform integrity validation |
| 63 | + if let Some(doc_chain) = chain { |
| 64 | + if doc_chain.document_ref().is_none() && doc_chain.height() != 0 { |
| 65 | + doc.report().functional_validation( |
| 66 | + "The chain height must be zero when there is no chained doc", |
| 67 | + "Chained Documents validation", |
| 68 | + ); |
| 69 | + return Ok(false); |
| 70 | + } |
| 71 | + if doc_chain.height() == 0 && doc_chain.document_ref().is_some() { |
| 72 | + doc.report().functional_validation( |
| 73 | + "The next Chained Document must not exist while the height is zero", |
| 74 | + "Chained Documents validation", |
| 75 | + ); |
| 76 | + return Ok(false); |
| 77 | + } |
| 78 | + |
| 79 | + if let Some(chained_ref) = doc_chain.document_ref() { |
| 80 | + let Some(chained_doc) = provider.try_get_doc(chained_ref).await? else { |
| 81 | + doc.report().other( |
| 82 | + &format!( |
| 83 | + "Cannot find the Chained Document ({chained_ref}) from the provider" |
| 84 | + ), |
| 85 | + "Chained Documents validation", |
| 86 | + ); |
| 87 | + return Ok(false); |
| 88 | + }; |
| 89 | + |
| 90 | + // have the same id as the document being chained to. |
| 91 | + if chained_doc.doc_id()? != doc.doc_id()? { |
| 92 | + doc.report().functional_validation( |
| 93 | + "Must have the same id as the document being chained to", |
| 94 | + "Chained Documents validation", |
| 95 | + ); |
| 96 | + return Ok(false); |
| 97 | + } |
| 98 | + |
| 99 | + // have a ver that is greater than the ver being chained to. |
| 100 | + if chained_doc.doc_ver()? > doc.doc_ver()? { |
| 101 | + doc.report().functional_validation( |
| 102 | + "Must have a ver that is greater than the ver being chained to", |
| 103 | + "Chained Documents validation", |
| 104 | + ); |
| 105 | + return Ok(false); |
| 106 | + } |
| 107 | + |
| 108 | + // have the same type as the chained document. |
| 109 | + if chained_doc.doc_type()? != doc.doc_type()? { |
| 110 | + doc.report().functional_validation( |
| 111 | + "Must have the same type as the chained document", |
| 112 | + "Chained Documents validation", |
| 113 | + ); |
| 114 | + return Ok(false); |
| 115 | + } |
| 116 | + |
| 117 | + if let Some(chained_height) = |
| 118 | + chained_doc.doc_meta().chain().map(crate::Chain::height) |
| 119 | + { |
| 120 | + // chain doc must not be negative |
| 121 | + if chained_height < 0 { |
| 122 | + doc.report().functional_validation( |
| 123 | + "The height of the document being chained to must be positive number", |
| 124 | + "Chained Documents validation", |
| 125 | + ); |
| 126 | + return Ok(false); |
| 127 | + } |
| 128 | + |
| 129 | + // have its absolute height exactly one more than the height of the |
| 130 | + // document being chained to. |
| 131 | + if !matches!( |
| 132 | + i32::abs(doc_chain.height()).checked_sub(i32::abs(chained_height)), |
| 133 | + Some(1) |
| 134 | + ) { |
| 135 | + doc.report().functional_validation( |
| 136 | + "Must have its absolute height exactly one more than the height of the document being chained to", |
| 137 | + "Chained Documents validation", |
| 138 | + ); |
| 139 | + return Ok(false); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + if let Self::NotSpecified = self { |
| 146 | + if chain.is_some() { |
| 147 | + doc.report().unknown_field( |
| 148 | + "chain", |
| 149 | + &doc.doc_meta() |
| 150 | + .chain() |
| 151 | + .iter() |
| 152 | + .map(ToString::to_string) |
| 153 | + .reduce(|a, b| format!("{a}, {b}")) |
| 154 | + .unwrap_or_default(), |
| 155 | + "Document does not expect to have 'chain' field", |
| 156 | + ); |
| 157 | + return Ok(false); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + Ok(true) |
| 162 | + } |
| 163 | +} |
0 commit comments