|
| 1 | +//! Auxiliary Data Decoding |
| 2 | +
|
| 3 | +use minicbor::Decode; |
| 4 | + |
| 5 | +use super::{ |
| 6 | + metadatum::Metadata, |
| 7 | + metadatum_label::MetadatumLabel, |
| 8 | + metadatum_value::MetadatumValue, |
| 9 | + scripts::{MutableTransactionScriptsMap, ScriptArray, ScriptType, TransactionScripts}, |
| 10 | +}; |
| 11 | + |
| 12 | +/// Auxiliary Data (Metadata) for a single Transaction in a block |
| 13 | +#[derive(Clone, Debug)] |
| 14 | +#[allow(clippy::module_name_repetitions)] |
| 15 | +pub struct TransactionAuxData { |
| 16 | + /// Metadata attached to a transaction |
| 17 | + metadata: Metadata, |
| 18 | + /// Scripts attached to a transaction |
| 19 | + #[allow(dead_code)] |
| 20 | + scripts: TransactionScripts, |
| 21 | +} |
| 22 | + |
| 23 | +impl Decode<'_, ()> for TransactionAuxData { |
| 24 | + fn decode( |
| 25 | + d: &mut minicbor::Decoder<'_>, _ctx: &mut (), |
| 26 | + ) -> Result<Self, minicbor::decode::Error> { |
| 27 | + // Check what kind of aux data we have to deal with |
| 28 | + match d.datatype() { |
| 29 | + // Shelley: https://github.com/IntersectMBO/cardano-ledger/blob/78b32d585fd4a0340fb2b184959fb0d46f32c8d2/eras/conway/impl/cddl-files/conway.cddl#L522 |
| 30 | + Ok(minicbor::data::Type::Map) => { |
| 31 | + Ok(TransactionAuxData { |
| 32 | + metadata: Metadata::decode(d, &mut ())?, |
| 33 | + scripts: TransactionScripts::default(), |
| 34 | + }) |
| 35 | + }, |
| 36 | + // Shelley-MA: https://github.com/IntersectMBO/cardano-ledger/blob/78b32d585fd4a0340fb2b184959fb0d46f32c8d2/eras/conway/impl/cddl-files/conway.cddl#L523 |
| 37 | + Ok(minicbor::data::Type::Array) => Self::decode_shelley_ma_array(d), |
| 38 | + // Maybe Alonzo and beyond: https://github.com/IntersectMBO/cardano-ledger/blob/78b32d585fd4a0340fb2b184959fb0d46f32c8d2/eras/conway/impl/cddl-files/conway.cddl#L526 |
| 39 | + Ok(minicbor::data::Type::Tag) => Self::decode_alonzo_plus_map(d), |
| 40 | + Ok(unexpected) => { |
| 41 | + let msg = format!( |
| 42 | + "Error decoding Transaction Aux Data: Unexpected datatype {unexpected}" |
| 43 | + ); |
| 44 | + Err(minicbor::decode::Error::message(&msg)) |
| 45 | + }, |
| 46 | + Err(error) => { |
| 47 | + let msg = format!("Error decoding Transaction Aux Data: {error}"); |
| 48 | + Err(minicbor::decode::Error::message(msg)) |
| 49 | + }, |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl TransactionAuxData { |
| 55 | + /// Get metadata with the given label. |
| 56 | + #[must_use] |
| 57 | + pub fn metadata(&self, label: MetadatumLabel) -> Option<&MetadatumValue> { |
| 58 | + self.metadata.get(label) |
| 59 | + } |
| 60 | + |
| 61 | + /// Decode a Shelley-MA Auxiliary Data Array |
| 62 | + fn decode_shelley_ma_array(d: &mut minicbor::Decoder) -> Result<Self, minicbor::decode::Error> { |
| 63 | + match d.array() { |
| 64 | + Ok(Some(entries)) => { |
| 65 | + if entries != 2 { |
| 66 | + let msg = format!( |
| 67 | + "Error decoding Transaction Aux Data: Script Data Array Expected 2 entries, found {entries}." |
| 68 | + ); |
| 69 | + return Err(minicbor::decode::Error::message(&msg)); |
| 70 | + } |
| 71 | + }, |
| 72 | + Ok(None) => { |
| 73 | + return Err(minicbor::decode::Error::message( |
| 74 | + "Error decoding Transaction Aux Data: Indefinite Array found decoding Metadata. Invalid.")); |
| 75 | + }, |
| 76 | + Err(error) => { |
| 77 | + return Err(minicbor::decode::Error::message(format!( |
| 78 | + "Error decoding Transaction Aux Data: {error}." |
| 79 | + ))); |
| 80 | + }, |
| 81 | + }; |
| 82 | + |
| 83 | + let metadata = Metadata::decode(d, &mut ())?; |
| 84 | + let script_array = ScriptArray::decode(d, &mut ScriptType::Native)?; |
| 85 | + |
| 86 | + let scripts = MutableTransactionScriptsMap::default(); |
| 87 | + scripts.insert(ScriptType::Native, script_array); |
| 88 | + |
| 89 | + Ok(Self { |
| 90 | + metadata, |
| 91 | + scripts: scripts.into(), |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + /// Decode an Alonzo Plus MAP |
| 96 | + fn decode_alonzo_plus_map(d: &mut minicbor::Decoder) -> Result<Self, minicbor::decode::Error> { |
| 97 | + match d.tag() { |
| 98 | + Ok(tag) => { |
| 99 | + if tag.as_u64() != 259 { |
| 100 | + return Err(minicbor::decode::Error::message(format!( |
| 101 | + "Invalid tag for Alonzo+ Aux Data. Expected 259, found {tag}." |
| 102 | + ))); |
| 103 | + } |
| 104 | + }, |
| 105 | + Err(error) => { |
| 106 | + return Err(minicbor::decode::Error::message(format!( |
| 107 | + "Error decoding Transaction Alonzo+ Aux Data: {error}." |
| 108 | + ))); |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + let entries = match d.map() { |
| 113 | + Ok(Some(entries)) => entries, |
| 114 | + Ok(None) => { |
| 115 | + return Err(minicbor::decode::Error::message( |
| 116 | + "Indefinite Map found decoding Alonzo+ Metadata. Invalid.", |
| 117 | + )) |
| 118 | + }, |
| 119 | + Err(error) => { |
| 120 | + return Err(minicbor::decode::Error::message(format!( |
| 121 | + "Error decoding Transaction Alonzo+ Aux Data: {error}." |
| 122 | + ))) |
| 123 | + }, |
| 124 | + }; |
| 125 | + |
| 126 | + // Make the default versions of the metadata and script types |
| 127 | + let mut metadata = Metadata::default(); |
| 128 | + let scripts = MutableTransactionScriptsMap::default(); |
| 129 | + |
| 130 | + // iterate the map |
| 131 | + for _ in 0..entries { |
| 132 | + let script_type = match d.u64() { |
| 133 | + Ok(key) => { |
| 134 | + if let Ok(script_type) = ScriptType::try_from(key) { |
| 135 | + script_type |
| 136 | + } else { |
| 137 | + // Only fails if its Metadata and not a script. |
| 138 | + if metadata.is_empty() { |
| 139 | + metadata = Metadata::decode(d, &mut ())?; |
| 140 | + continue; |
| 141 | + } |
| 142 | + return Err(minicbor::decode::Error::message( |
| 143 | + "Multiple Alonzo+ Metadata entries found. Invalid.", |
| 144 | + )); |
| 145 | + } |
| 146 | + }, |
| 147 | + |
| 148 | + Err(error) => { |
| 149 | + return Err(minicbor::decode::Error::message(format!( |
| 150 | + "Error decoding Alonzo+ Metadata Aux Data Type Key: {error}" |
| 151 | + ))); |
| 152 | + }, |
| 153 | + }; |
| 154 | + |
| 155 | + let mut ctx = script_type; |
| 156 | + |
| 157 | + let script_array = ScriptArray::decode(d, &mut ctx)?; |
| 158 | + if scripts.insert(script_type, script_array).is_some() { |
| 159 | + return Err(minicbor::decode::Error::message( |
| 160 | + "Multiple Alonzo+ Script entries of type {script_type} found. Invalid.", |
| 161 | + )); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + Ok(Self { |
| 166 | + metadata, |
| 167 | + scripts: scripts.into(), |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
0 commit comments