Skip to main content

cml_multi_era/byron/block/
serialization.rs

1// This file was code-generated using an experimental CDDL to rust tool:
2// https://github.com/dcSpark/cddl-codegen
3
4use crate::byron::ByronAny;
5
6use super::*;
7use cbor_event;
8use cbor_event::de::Deserializer;
9use cbor_event::se::Serializer;
10use cml_core::error::*;
11use cml_core::serialization::*;
12use cml_crypto::RawBytesEncoding;
13use std::io::{BufRead, Seek, SeekFrom, Write};
14
15impl cbor_event::se::Serialize for BlockHeaderExtraData {
16    fn serialize<'se, W: Write>(
17        &self,
18        serializer: &'se mut Serializer<W>,
19    ) -> cbor_event::Result<&'se mut Serializer<W>> {
20        serializer.write_array(cbor_event::Len::Len(4))?;
21        self.block_version.serialize(serializer)?;
22        self.software_version.serialize(serializer)?;
23        serializer.write_map(cbor_event::Len::Len(self.byron_attributes.len() as u64))?;
24        for (key, value) in self.byron_attributes.iter() {
25            key.serialize(serializer)?;
26            value.serialize(serializer)?;
27        }
28        serializer.write_bytes(self.extra_proof.to_raw_bytes())?;
29        Ok(serializer)
30    }
31}
32
33impl Deserialize for BlockHeaderExtraData {
34    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
35        let len = raw.array()?;
36        let mut read_len = CBORReadLen::from(len);
37        read_len.read_elems(4)?;
38        read_len.finish()?;
39        (|| -> Result<_, DeserializeError> {
40            let block_version = ByronBlockVersion::deserialize(raw)
41                .map_err(|e: DeserializeError| e.annotate("block_version"))?;
42            let software_version = ByronSoftwareVersion::deserialize(raw)
43                .map_err(|e: DeserializeError| e.annotate("software_version"))?;
44            let byron_attributes = (|| -> Result<_, DeserializeError> {
45                let mut byron_attributes_table = BTreeMap::new();
46                let byron_attributes_len = raw.map()?;
47                while match byron_attributes_len {
48                    cbor_event::Len::Len(n) => (byron_attributes_table.len() as u64) < n,
49                    cbor_event::Len::Indefinite => true,
50                } {
51                    if raw.cbor_type()? == cbor_event::Type::Special {
52                        assert_eq!(raw.special()?, cbor_event::Special::Break);
53                        break;
54                    }
55                    let byron_attributes_key = ByronAny::deserialize(raw)?;
56                    let byron_attributes_value = ByronAny::deserialize(raw)?;
57                    if byron_attributes_table
58                        .insert(byron_attributes_key.clone(), byron_attributes_value)
59                        .is_some()
60                    {
61                        return Err(DeserializeFailure::DuplicateKey(Key::Str(String::from(
62                            "some complicated/unsupported type",
63                        )))
64                        .into());
65                    }
66                }
67                Ok(byron_attributes_table)
68            })()
69            .map_err(|e| e.annotate("byron_attributes"))?;
70            let extra_proof = raw
71                .bytes()
72                .map_err(Into::<DeserializeError>::into)
73                .and_then(|bytes| {
74                    Blake2b256::from_raw_bytes(&bytes)
75                        .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into())
76                })
77                .map_err(|e: DeserializeError| e.annotate("extra_proof"))?;
78            match len {
79                cbor_event::Len::Len(_) => (),
80                cbor_event::Len::Indefinite => match raw.special()? {
81                    cbor_event::Special::Break => (),
82                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
83                },
84            }
85            Ok(BlockHeaderExtraData {
86                block_version,
87                software_version,
88                byron_attributes,
89                extra_proof,
90            })
91        })()
92        .map_err(|e| e.annotate("BlockHeaderExtraData"))
93    }
94}
95
96impl cbor_event::se::Serialize for ByronBlock {
97    fn serialize<'se, W: Write>(
98        &self,
99        serializer: &'se mut Serializer<W>,
100    ) -> cbor_event::Result<&'se mut Serializer<W>> {
101        match self {
102            ByronBlock::EpochBoundary(epoch_boundary) => epoch_boundary.serialize(serializer),
103            ByronBlock::Main(main) => main.serialize(serializer),
104        }
105    }
106}
107
108impl Deserialize for ByronBlock {
109    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
110        (|| -> Result<_, DeserializeError> {
111            let initial_position = raw.as_mut_ref().stream_position().unwrap();
112            let mut errs = Vec::new();
113            let deser_variant: Result<_, DeserializeError> = ByronEbBlock::deserialize(raw);
114            match deser_variant {
115                Ok(epoch_boundary) => return Ok(Self::EpochBoundary(epoch_boundary)),
116                Err(e) => {
117                    errs.push(e.annotate("EpochBoundary"));
118                    raw.as_mut_ref()
119                        .seek(SeekFrom::Start(initial_position))
120                        .unwrap();
121                }
122            };
123            let deser_variant: Result<_, DeserializeError> = ByronMainBlock::deserialize(raw);
124            match deser_variant {
125                Ok(main) => return Ok(Self::Main(main)),
126                Err(e) => {
127                    errs.push(e.annotate("Main"));
128                    raw.as_mut_ref()
129                        .seek(SeekFrom::Start(initial_position))
130                        .unwrap();
131                }
132            };
133            Err(DeserializeError::new(
134                "ByronBlock",
135                DeserializeFailure::NoVariantMatchedWithCauses(errs),
136            ))
137        })()
138        .map_err(|e| e.annotate("ByronBlock"))
139    }
140}
141
142impl cbor_event::se::Serialize for ByronBlockBody {
143    fn serialize<'se, W: Write>(
144        &self,
145        serializer: &'se mut Serializer<W>,
146    ) -> cbor_event::Result<&'se mut Serializer<W>> {
147        serializer.write_array(cbor_event::Len::Len(4))?;
148        serializer.write_array(cbor_event::Len::Indefinite)?;
149        //serializer.write_array(cbor_event::Len::Len(self.tx_payload.len() as u64))?;
150        for element in self.tx_payload.iter() {
151            element.serialize(serializer)?;
152        }
153        serializer.write_special(cbor_event::Special::Break)?;
154        self.ssc_payload.serialize(serializer)?;
155        serializer.write_array(cbor_event::Len::Indefinite)?;
156        //serializer.write_array(cbor_event::Len::Len(self.dlg_payload.len() as u64))?;
157        for element in self.dlg_payload.iter() {
158            element.serialize(serializer)?;
159        }
160        serializer.write_special(cbor_event::Special::Break)?;
161        self.upd_payload.serialize(serializer)?;
162        Ok(serializer)
163    }
164}
165
166impl Deserialize for ByronBlockBody {
167    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
168        let len = raw.array()?;
169        let mut read_len = CBORReadLen::from(len);
170        read_len.read_elems(4)?;
171        read_len.finish()?;
172        (|| -> Result<_, DeserializeError> {
173            let tx_payload = (|| -> Result<_, DeserializeError> {
174                let mut tx_payload_arr = Vec::new();
175                let len = raw.array()?;
176                while match len {
177                    cbor_event::Len::Len(n) => (tx_payload_arr.len() as u64) < n,
178                    cbor_event::Len::Indefinite => true,
179                } {
180                    if raw.cbor_type()? == cbor_event::Type::Special {
181                        assert_eq!(raw.special()?, cbor_event::Special::Break);
182                        break;
183                    }
184                    tx_payload_arr.push(TxAux::deserialize(raw)?);
185                }
186                Ok(tx_payload_arr)
187            })()
188            .map_err(|e| e.annotate("tx_payload"))?;
189            let ssc_payload =
190                Ssc::deserialize(raw).map_err(|e: DeserializeError| e.annotate("ssc_payload"))?;
191            let dlg_payload = (|| -> Result<_, DeserializeError> {
192                let mut dlg_payload_arr = Vec::new();
193                let len = raw.array()?;
194                while match len {
195                    cbor_event::Len::Len(n) => (dlg_payload_arr.len() as u64) < n,
196                    cbor_event::Len::Indefinite => true,
197                } {
198                    if raw.cbor_type()? == cbor_event::Type::Special {
199                        assert_eq!(raw.special()?, cbor_event::Special::Break);
200                        break;
201                    }
202                    dlg_payload_arr.push(ByronDelegation::deserialize(raw)?);
203                }
204                Ok(dlg_payload_arr)
205            })()
206            .map_err(|e| e.annotate("dlg_payload"))?;
207            let upd_payload = ByronUpdate::deserialize(raw)
208                .map_err(|e: DeserializeError| e.annotate("upd_payload"))?;
209            match len {
210                cbor_event::Len::Len(_) => (),
211                cbor_event::Len::Indefinite => match raw.special()? {
212                    cbor_event::Special::Break => (),
213                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
214                },
215            }
216            Ok(ByronBlockBody {
217                tx_payload,
218                ssc_payload,
219                dlg_payload,
220                upd_payload,
221            })
222        })()
223        .map_err(|e| e.annotate("ByronBlockBody"))
224    }
225}
226
227impl cbor_event::se::Serialize for ByronBlockConsensusData {
228    fn serialize<'se, W: Write>(
229        &self,
230        serializer: &'se mut Serializer<W>,
231    ) -> cbor_event::Result<&'se mut Serializer<W>> {
232        serializer.write_array(cbor_event::Len::Len(4))?;
233        self.byron_slot_id.serialize(serializer)?;
234        serializer.write_bytes(&self.byron_pub_key)?;
235        self.byron_difficulty.serialize(serializer)?;
236        self.byron_block_signature.serialize(serializer)?;
237        Ok(serializer)
238    }
239}
240
241impl Deserialize for ByronBlockConsensusData {
242    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
243        let len = raw.array()?;
244        let mut read_len = CBORReadLen::from(len);
245        read_len.read_elems(4)?;
246        read_len.finish()?;
247        (|| -> Result<_, DeserializeError> {
248            let byron_slot_id = ByronSlotId::deserialize(raw)
249                .map_err(|e: DeserializeError| e.annotate("byron_slot_id"))?;
250            let byron_pub_key = Ok(raw.bytes()? as Vec<u8>)
251                .map_err(|e: DeserializeError| e.annotate("byron_pub_key"))?;
252            let byron_difficulty = ByronDifficulty::deserialize(raw)
253                .map_err(|e: DeserializeError| e.annotate("byron_difficulty"))?;
254            let byron_block_signature = ByronBlockSignature::deserialize(raw)
255                .map_err(|e: DeserializeError| e.annotate("byron_block_signature"))?;
256            match len {
257                cbor_event::Len::Len(_) => (),
258                cbor_event::Len::Indefinite => match raw.special()? {
259                    cbor_event::Special::Break => (),
260                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
261                },
262            }
263            Ok(ByronBlockConsensusData {
264                byron_slot_id,
265                byron_pub_key,
266                byron_difficulty,
267                byron_block_signature,
268            })
269        })()
270        .map_err(|e| e.annotate("ByronBlockConsensusData"))
271    }
272}
273
274impl cbor_event::se::Serialize for ByronBlockHeader {
275    fn serialize<'se, W: Write>(
276        &self,
277        serializer: &'se mut Serializer<W>,
278    ) -> cbor_event::Result<&'se mut Serializer<W>> {
279        serializer.write_array(cbor_event::Len::Len(5))?;
280        serializer.write_unsigned_integer(self.protocol_magic as u64)?;
281        serializer.write_bytes(self.prev_block.to_raw_bytes())?;
282        self.body_proof.serialize(serializer)?;
283        self.consensus_data.serialize(serializer)?;
284        self.extra_data.serialize(serializer)?;
285        Ok(serializer)
286    }
287}
288
289impl Deserialize for ByronBlockHeader {
290    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
291        let len = raw.array()?;
292        let mut read_len = CBORReadLen::from(len);
293        read_len.read_elems(5)?;
294        read_len.finish()?;
295        (|| -> Result<_, DeserializeError> {
296            let protocol_magic = Ok(raw.unsigned_integer()? as u32)
297                .map_err(|e: DeserializeError| e.annotate("protocol_magic"))?;
298            let prev_block = raw
299                .bytes()
300                .map_err(Into::<DeserializeError>::into)
301                .and_then(|bytes| {
302                    Blake2b256::from_raw_bytes(&bytes)
303                        .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into())
304                })
305                .map_err(|e: DeserializeError| e.annotate("prev_block"))?;
306            let body_proof = ByronBodyProof::deserialize(raw)
307                .map_err(|e: DeserializeError| e.annotate("body_proof"))?;
308            let consensus_data = ByronBlockConsensusData::deserialize(raw)
309                .map_err(|e: DeserializeError| e.annotate("consensus_data"))?;
310            let extra_data = BlockHeaderExtraData::deserialize(raw)
311                .map_err(|e: DeserializeError| e.annotate("extra_data"))?;
312            match len {
313                cbor_event::Len::Len(_) => (),
314                cbor_event::Len::Indefinite => match raw.special()? {
315                    cbor_event::Special::Break => (),
316                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
317                },
318            }
319            Ok(ByronBlockHeader {
320                protocol_magic,
321                prev_block,
322                body_proof,
323                consensus_data,
324                extra_data,
325            })
326        })()
327        .map_err(|e| e.annotate("ByronBlockHeader"))
328    }
329}
330
331impl cbor_event::se::Serialize for ByronBlockSignature {
332    fn serialize<'se, W: Write>(
333        &self,
334        serializer: &'se mut Serializer<W>,
335    ) -> cbor_event::Result<&'se mut Serializer<W>> {
336        match self {
337            ByronBlockSignature::Signature(signature) => signature.serialize(serializer),
338            ByronBlockSignature::ProxyLight(proxy_light) => proxy_light.serialize(serializer),
339            ByronBlockSignature::ProxyHeavy(proxy_heavy) => proxy_heavy.serialize(serializer),
340        }
341    }
342}
343
344impl Deserialize for ByronBlockSignature {
345    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
346        (|| -> Result<_, DeserializeError> {
347            let initial_position = raw.as_mut_ref().stream_position().unwrap();
348            let mut errs = Vec::new();
349            let deser_variant: Result<_, DeserializeError> =
350                ByronBlockSignatureNormal::deserialize(raw);
351            match deser_variant {
352                Ok(signature) => return Ok(Self::Signature(signature)),
353                Err(e) => {
354                    errs.push(e.annotate("Signature"));
355                    raw.as_mut_ref()
356                        .seek(SeekFrom::Start(initial_position))
357                        .unwrap();
358                }
359            };
360            let deser_variant: Result<_, DeserializeError> =
361                ByronBlockSignatureProxyLight::deserialize(raw);
362            match deser_variant {
363                Ok(proxy_light) => return Ok(Self::ProxyLight(proxy_light)),
364                Err(e) => {
365                    errs.push(e.annotate("ProxyLight"));
366                    raw.as_mut_ref()
367                        .seek(SeekFrom::Start(initial_position))
368                        .unwrap();
369                }
370            };
371            let deser_variant: Result<_, DeserializeError> =
372                ByronBlockSignatureProxyHeavy::deserialize(raw);
373            match deser_variant {
374                Ok(proxy_heavy) => return Ok(Self::ProxyHeavy(proxy_heavy)),
375                Err(e) => {
376                    errs.push(e.annotate("ProxyHeavy"));
377                    raw.as_mut_ref()
378                        .seek(SeekFrom::Start(initial_position))
379                        .unwrap();
380                }
381            };
382            Err(DeserializeError::new(
383                "ByronBlockSignature",
384                DeserializeFailure::NoVariantMatchedWithCauses(errs),
385            ))
386        })()
387        .map_err(|e| e.annotate("ByronBlockSignature"))
388    }
389}
390
391impl cbor_event::se::Serialize for ByronBlockSignatureNormal {
392    fn serialize<'se, W: Write>(
393        &self,
394        serializer: &'se mut Serializer<W>,
395    ) -> cbor_event::Result<&'se mut Serializer<W>> {
396        serializer.write_array(cbor_event::Len::Len(2))?;
397        serializer.write_unsigned_integer(0u64)?;
398        serializer.write_bytes(&self.signature)?;
399        Ok(serializer)
400    }
401}
402
403impl Deserialize for ByronBlockSignatureNormal {
404    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
405        let len = raw.array()?;
406        let mut read_len = CBORReadLen::from(len);
407        read_len.read_elems(2)?;
408        read_len.finish()?;
409        (|| -> Result<_, DeserializeError> {
410            (|| -> Result<_, DeserializeError> {
411                let tag_value = raw.unsigned_integer()?;
412                if tag_value != 0 {
413                    return Err(DeserializeFailure::FixedValueMismatch {
414                        found: Key::Uint(tag_value),
415                        expected: Key::Uint(0),
416                    }
417                    .into());
418                }
419                Ok(())
420            })()
421            .map_err(|e| e.annotate("tag"))?;
422            let signature = Ok(raw.bytes()? as Vec<u8>)
423                .map_err(|e: DeserializeError| e.annotate("signature"))?;
424            match len {
425                cbor_event::Len::Len(_) => (),
426                cbor_event::Len::Indefinite => match raw.special()? {
427                    cbor_event::Special::Break => (),
428                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
429                },
430            }
431            Ok(ByronBlockSignatureNormal { signature })
432        })()
433        .map_err(|e| e.annotate("ByronBlockSignatureNormal"))
434    }
435}
436
437impl cbor_event::se::Serialize for ByronBlockSignatureProxyHeavy {
438    fn serialize<'se, W: Write>(
439        &self,
440        serializer: &'se mut Serializer<W>,
441    ) -> cbor_event::Result<&'se mut Serializer<W>> {
442        serializer.write_array(cbor_event::Len::Len(2))?;
443        serializer.write_unsigned_integer(2u64)?;
444        self.signature.serialize(serializer)?;
445        Ok(serializer)
446    }
447}
448
449impl Deserialize for ByronBlockSignatureProxyHeavy {
450    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
451        let len = raw.array()?;
452        let mut read_len = CBORReadLen::from(len);
453        read_len.read_elems(2)?;
454        read_len.finish()?;
455        (|| -> Result<_, DeserializeError> {
456            (|| -> Result<_, DeserializeError> {
457                let tag_value = raw.unsigned_integer()?;
458                if tag_value != 2 {
459                    return Err(DeserializeFailure::FixedValueMismatch {
460                        found: Key::Uint(tag_value),
461                        expected: Key::Uint(2),
462                    }
463                    .into());
464                }
465                Ok(())
466            })()
467            .map_err(|e| e.annotate("tag"))?;
468            let signature = ByronDelegationSignature::deserialize(raw)
469                .map_err(|e: DeserializeError| e.annotate("signature"))?;
470            match len {
471                cbor_event::Len::Len(_) => (),
472                cbor_event::Len::Indefinite => match raw.special()? {
473                    cbor_event::Special::Break => (),
474                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
475                },
476            }
477            Ok(ByronBlockSignatureProxyHeavy { signature })
478        })()
479        .map_err(|e| e.annotate("ByronBlockSignatureProxyHeavy"))
480    }
481}
482
483impl cbor_event::se::Serialize for ByronBlockSignatureProxyLight {
484    fn serialize<'se, W: Write>(
485        &self,
486        serializer: &'se mut Serializer<W>,
487    ) -> cbor_event::Result<&'se mut Serializer<W>> {
488        serializer.write_array(cbor_event::Len::Len(2))?;
489        serializer.write_unsigned_integer(1u64)?;
490        self.signature.serialize(serializer)?;
491        Ok(serializer)
492    }
493}
494
495impl Deserialize for ByronBlockSignatureProxyLight {
496    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
497        let len = raw.array()?;
498        let mut read_len = CBORReadLen::from(len);
499        read_len.read_elems(2)?;
500        read_len.finish()?;
501        (|| -> Result<_, DeserializeError> {
502            (|| -> Result<_, DeserializeError> {
503                let tag_value = raw.unsigned_integer()?;
504                if tag_value != 1 {
505                    return Err(DeserializeFailure::FixedValueMismatch {
506                        found: Key::Uint(tag_value),
507                        expected: Key::Uint(1),
508                    }
509                    .into());
510                }
511                Ok(())
512            })()
513            .map_err(|e| e.annotate("tag"))?;
514            let signature = LightWeightDelegationSignature::deserialize(raw)
515                .map_err(|e: DeserializeError| e.annotate("signature"))?;
516            match len {
517                cbor_event::Len::Len(_) => (),
518                cbor_event::Len::Indefinite => match raw.special()? {
519                    cbor_event::Special::Break => (),
520                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
521                },
522            }
523            Ok(ByronBlockSignatureProxyLight { signature })
524        })()
525        .map_err(|e| e.annotate("ByronBlockSignatureProxyLight"))
526    }
527}
528
529impl cbor_event::se::Serialize for ByronBodyProof {
530    fn serialize<'se, W: Write>(
531        &self,
532        serializer: &'se mut Serializer<W>,
533    ) -> cbor_event::Result<&'se mut Serializer<W>> {
534        serializer.write_array(cbor_event::Len::Len(4))?;
535        self.tx_proof.serialize(serializer)?;
536        self.ssc_proof.serialize(serializer)?;
537        serializer.write_bytes(self.dlg_proof.to_raw_bytes())?;
538        serializer.write_bytes(self.upd_proof.to_raw_bytes())?;
539        Ok(serializer)
540    }
541}
542
543impl Deserialize for ByronBodyProof {
544    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
545        let len = raw.array()?;
546        let mut read_len = CBORReadLen::from(len);
547        read_len.read_elems(4)?;
548        read_len.finish()?;
549        (|| -> Result<_, DeserializeError> {
550            let tx_proof = ByronTxProof::deserialize(raw)
551                .map_err(|e: DeserializeError| e.annotate("tx_proof"))?;
552            let ssc_proof = SscProof::deserialize(raw)
553                .map_err(|e: DeserializeError| e.annotate("ssc_proof"))?;
554            let dlg_proof = raw
555                .bytes()
556                .map_err(Into::<DeserializeError>::into)
557                .and_then(|bytes| {
558                    Blake2b256::from_raw_bytes(&bytes)
559                        .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into())
560                })
561                .map_err(|e: DeserializeError| e.annotate("dlg_proof"))?;
562            let upd_proof = raw
563                .bytes()
564                .map_err(Into::<DeserializeError>::into)
565                .and_then(|bytes| {
566                    Blake2b256::from_raw_bytes(&bytes)
567                        .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into())
568                })
569                .map_err(|e: DeserializeError| e.annotate("upd_proof"))?;
570            match len {
571                cbor_event::Len::Len(_) => (),
572                cbor_event::Len::Indefinite => match raw.special()? {
573                    cbor_event::Special::Break => (),
574                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
575                },
576            }
577            Ok(ByronBodyProof {
578                tx_proof,
579                ssc_proof,
580                dlg_proof,
581                upd_proof,
582            })
583        })()
584        .map_err(|e| e.annotate("ByronBodyProof"))
585    }
586}
587
588impl cbor_event::se::Serialize for ByronDifficulty {
589    fn serialize<'se, W: Write>(
590        &self,
591        serializer: &'se mut Serializer<W>,
592    ) -> cbor_event::Result<&'se mut Serializer<W>> {
593        serializer.write_array(cbor_event::Len::Len(1))?;
594        serializer.write_unsigned_integer(self.u64)?;
595        Ok(serializer)
596    }
597}
598
599impl Deserialize for ByronDifficulty {
600    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
601        let len = raw.array()?;
602        let mut read_len = CBORReadLen::from(len);
603        read_len.read_elems(1)?;
604        read_len.finish()?;
605        (|| -> Result<_, DeserializeError> {
606            let u64 =
607                Ok(raw.unsigned_integer()?).map_err(|e: DeserializeError| e.annotate("u64"))?;
608            match len {
609                cbor_event::Len::Len(_) => (),
610                cbor_event::Len::Indefinite => match raw.special()? {
611                    cbor_event::Special::Break => (),
612                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
613                },
614            }
615            Ok(ByronDifficulty { u64 })
616        })()
617        .map_err(|e| e.annotate("ByronDifficulty"))
618    }
619}
620
621impl cbor_event::se::Serialize for ByronEbBlock {
622    fn serialize<'se, W: Write>(
623        &self,
624        serializer: &'se mut Serializer<W>,
625    ) -> cbor_event::Result<&'se mut Serializer<W>> {
626        serializer.write_array(cbor_event::Len::Len(3))?;
627        self.header.serialize(serializer)?;
628        //serializer.write_array(cbor_event::Len::Len(self.body.len() as u64))?;
629        serializer.write_array(cbor_event::Len::Indefinite)?;
630        for element in self.body.iter() {
631            serializer.write_bytes(element.to_raw_bytes())?;
632        }
633        serializer.write_special(cbor_event::Special::Break)?;
634        serializer.write_array(cbor_event::Len::Len(self.extra.len() as u64))?;
635        for element in self.extra.iter() {
636            serializer.write_map(cbor_event::Len::Len(element.len() as u64))?;
637            for (key, value) in element.iter() {
638                key.serialize(serializer)?;
639                value.serialize(serializer)?;
640            }
641        }
642        Ok(serializer)
643    }
644}
645
646impl Deserialize for ByronEbBlock {
647    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
648        let len = raw.array()?;
649        let mut read_len = CBORReadLen::from(len);
650        read_len.read_elems(3)?;
651        read_len.finish()?;
652        (|| -> Result<_, DeserializeError> {
653            let header =
654                EbbHead::deserialize(raw).map_err(|e: DeserializeError| e.annotate("header"))?;
655            let body = (|| -> Result<_, DeserializeError> {
656                let mut body_arr = Vec::new();
657                let len = raw.array()?;
658                while match len {
659                    cbor_event::Len::Len(n) => (body_arr.len() as u64) < n,
660                    cbor_event::Len::Indefinite => true,
661                } {
662                    if raw.cbor_type()? == cbor_event::Type::Special {
663                        assert_eq!(raw.special()?, cbor_event::Special::Break);
664                        break;
665                    }
666                    body_arr.push(
667                        raw.bytes()
668                            .map_err(Into::<DeserializeError>::into)
669                            .and_then(|bytes| {
670                                StakeholderId::from_raw_bytes(&bytes).map_err(|e| {
671                                    DeserializeFailure::InvalidStructure(Box::new(e)).into()
672                                })
673                            })?,
674                    );
675                }
676                Ok(body_arr)
677            })()
678            .map_err(|e| e.annotate("body"))?;
679            let extra = (|| -> Result<_, DeserializeError> {
680                let mut extra_arr = Vec::new();
681                let len = raw.array()?;
682                while match len {
683                    cbor_event::Len::Len(n) => (extra_arr.len() as u64) < n,
684                    cbor_event::Len::Indefinite => true,
685                } {
686                    if raw.cbor_type()? == cbor_event::Type::Special {
687                        assert_eq!(raw.special()?, cbor_event::Special::Break);
688                        break;
689                    }
690                    let mut extra_elem_table = BTreeMap::new();
691                    let extra_elem_len = raw.map()?;
692                    while match extra_elem_len {
693                        cbor_event::Len::Len(n) => (extra_elem_table.len() as u64) < n,
694                        cbor_event::Len::Indefinite => true,
695                    } {
696                        if raw.cbor_type()? == cbor_event::Type::Special {
697                            assert_eq!(raw.special()?, cbor_event::Special::Break);
698                            break;
699                        }
700                        let extra_elem_key = ByronAny::deserialize(raw)?;
701                        let extra_elem_value = ByronAny::deserialize(raw)?;
702                        if extra_elem_table
703                            .insert(extra_elem_key.clone(), extra_elem_value)
704                            .is_some()
705                        {
706                            return Err(DeserializeFailure::DuplicateKey(Key::Str(String::from(
707                                "some complicated/unsupported type",
708                            )))
709                            .into());
710                        }
711                    }
712                    extra_arr.push(extra_elem_table);
713                }
714                Ok(extra_arr)
715            })()
716            .map_err(|e| e.annotate("extra"))?;
717            match len {
718                cbor_event::Len::Len(_) => (),
719                cbor_event::Len::Indefinite => match raw.special()? {
720                    cbor_event::Special::Break => (),
721                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
722                },
723            }
724            Ok(ByronEbBlock {
725                header,
726                body,
727                extra,
728            })
729        })()
730        .map_err(|e| e.annotate("ByronEbBlock"))
731    }
732}
733
734impl cbor_event::se::Serialize for ByronMainBlock {
735    fn serialize<'se, W: Write>(
736        &self,
737        serializer: &'se mut Serializer<W>,
738    ) -> cbor_event::Result<&'se mut Serializer<W>> {
739        serializer.write_array(cbor_event::Len::Len(3))?;
740        self.header.serialize(serializer)?;
741        self.body.serialize(serializer)?;
742        serializer.write_array(cbor_event::Len::Len(self.extra.len() as u64))?;
743        for element in self.extra.iter() {
744            serializer.write_map(cbor_event::Len::Len(element.len() as u64))?;
745            for (key, value) in element.iter() {
746                key.serialize(serializer)?;
747                value.serialize(serializer)?;
748            }
749        }
750        Ok(serializer)
751    }
752}
753
754impl Deserialize for ByronMainBlock {
755    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
756        let len = raw.array()?;
757        let mut read_len = CBORReadLen::from(len);
758        read_len.read_elems(3)?;
759        read_len.finish()?;
760        (|| -> Result<_, DeserializeError> {
761            let header = ByronBlockHeader::deserialize(raw)
762                .map_err(|e: DeserializeError| e.annotate("header"))?;
763            let body = ByronBlockBody::deserialize(raw)
764                .map_err(|e: DeserializeError| e.annotate("body"))?;
765            let extra = (|| -> Result<_, DeserializeError> {
766                let mut extra_arr = Vec::new();
767                let len = raw.array()?;
768                while match len {
769                    cbor_event::Len::Len(n) => (extra_arr.len() as u64) < n,
770                    cbor_event::Len::Indefinite => true,
771                } {
772                    if raw.cbor_type()? == cbor_event::Type::Special {
773                        assert_eq!(raw.special()?, cbor_event::Special::Break);
774                        break;
775                    }
776                    let mut extra_elem_table = BTreeMap::new();
777                    let extra_elem_len = raw.map()?;
778                    while match extra_elem_len {
779                        cbor_event::Len::Len(n) => (extra_elem_table.len() as u64) < n,
780                        cbor_event::Len::Indefinite => true,
781                    } {
782                        if raw.cbor_type()? == cbor_event::Type::Special {
783                            assert_eq!(raw.special()?, cbor_event::Special::Break);
784                            break;
785                        }
786                        let extra_elem_key = ByronAny::deserialize(raw)?;
787                        let extra_elem_value = ByronAny::deserialize(raw)?;
788                        if extra_elem_table
789                            .insert(extra_elem_key.clone(), extra_elem_value)
790                            .is_some()
791                        {
792                            return Err(DeserializeFailure::DuplicateKey(Key::Str(String::from(
793                                "some complicated/unsupported type",
794                            )))
795                            .into());
796                        }
797                    }
798                    extra_arr.push(extra_elem_table);
799                }
800                Ok(extra_arr)
801            })()
802            .map_err(|e| e.annotate("extra"))?;
803            match len {
804                cbor_event::Len::Len(_) => (),
805                cbor_event::Len::Indefinite => match raw.special()? {
806                    cbor_event::Special::Break => (),
807                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
808                },
809            }
810            Ok(ByronMainBlock {
811                header,
812                body,
813                extra,
814            })
815        })()
816        .map_err(|e| e.annotate("ByronMainBlock"))
817    }
818}
819
820impl cbor_event::se::Serialize for EbbConsensusData {
821    fn serialize<'se, W: Write>(
822        &self,
823        serializer: &'se mut Serializer<W>,
824    ) -> cbor_event::Result<&'se mut Serializer<W>> {
825        serializer.write_array(cbor_event::Len::Len(2))?;
826        serializer.write_unsigned_integer(self.epoch_id)?;
827        self.byron_difficulty.serialize(serializer)?;
828        Ok(serializer)
829    }
830}
831
832impl Deserialize for EbbConsensusData {
833    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
834        let len = raw.array()?;
835        let mut read_len = CBORReadLen::from(len);
836        read_len.read_elems(2)?;
837        read_len.finish()?;
838        (|| -> Result<_, DeserializeError> {
839            let epoch_id = Ok(raw.unsigned_integer()?)
840                .map_err(|e: DeserializeError| e.annotate("epoch_id"))?;
841            let byron_difficulty = ByronDifficulty::deserialize(raw)
842                .map_err(|e: DeserializeError| e.annotate("byron_difficulty"))?;
843            match len {
844                cbor_event::Len::Len(_) => (),
845                cbor_event::Len::Indefinite => match raw.special()? {
846                    cbor_event::Special::Break => (),
847                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
848                },
849            }
850            Ok(EbbConsensusData {
851                epoch_id,
852                byron_difficulty,
853            })
854        })()
855        .map_err(|e| e.annotate("EbbConsensusData"))
856    }
857}
858
859impl cbor_event::se::Serialize for EbbHead {
860    fn serialize<'se, W: Write>(
861        &self,
862        serializer: &'se mut Serializer<W>,
863    ) -> cbor_event::Result<&'se mut Serializer<W>> {
864        serializer.write_array(cbor_event::Len::Len(5))?;
865        serializer.write_unsigned_integer(self.protocol_magic as u64)?;
866        serializer.write_bytes(self.prev_block.to_raw_bytes())?;
867        serializer.write_bytes(self.body_proof.to_raw_bytes())?;
868        self.consensus_data.serialize(serializer)?;
869        serializer.write_array(cbor_event::Len::Len(self.extra_data.len() as u64))?;
870        for element in self.extra_data.iter() {
871            serializer.write_map(cbor_event::Len::Len(element.len() as u64))?;
872            for (key, value) in element.iter() {
873                key.serialize(serializer)?;
874                value.serialize(serializer)?;
875            }
876        }
877        Ok(serializer)
878    }
879}
880
881impl Deserialize for EbbHead {
882    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
883        let len = raw.array()?;
884        let mut read_len = CBORReadLen::from(len);
885        read_len.read_elems(5)?;
886        read_len.finish()?;
887        (|| -> Result<_, DeserializeError> {
888            let protocol_magic = Ok(raw.unsigned_integer()? as u32)
889                .map_err(|e: DeserializeError| e.annotate("protocol_magic"))?;
890            let prev_block = raw
891                .bytes()
892                .map_err(Into::<DeserializeError>::into)
893                .and_then(|bytes| {
894                    Blake2b256::from_raw_bytes(&bytes)
895                        .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into())
896                })
897                .map_err(|e: DeserializeError| e.annotate("prev_block"))?;
898            let body_proof = raw
899                .bytes()
900                .map_err(Into::<DeserializeError>::into)
901                .and_then(|bytes| {
902                    Blake2b256::from_raw_bytes(&bytes)
903                        .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into())
904                })
905                .map_err(|e: DeserializeError| e.annotate("body_proof"))?;
906            let consensus_data = EbbConsensusData::deserialize(raw)
907                .map_err(|e: DeserializeError| e.annotate("consensus_data"))?;
908            let extra_data = (|| -> Result<_, DeserializeError> {
909                let mut extra_data_arr = Vec::new();
910                let len = raw.array()?;
911                while match len {
912                    cbor_event::Len::Len(n) => (extra_data_arr.len() as u64) < n,
913                    cbor_event::Len::Indefinite => true,
914                } {
915                    if raw.cbor_type()? == cbor_event::Type::Special {
916                        assert_eq!(raw.special()?, cbor_event::Special::Break);
917                        break;
918                    }
919                    let mut extra_data_elem_table = BTreeMap::new();
920                    let extra_data_elem_len = raw.map()?;
921                    while match extra_data_elem_len {
922                        cbor_event::Len::Len(n) => (extra_data_elem_table.len() as u64) < n,
923                        cbor_event::Len::Indefinite => true,
924                    } {
925                        if raw.cbor_type()? == cbor_event::Type::Special {
926                            assert_eq!(raw.special()?, cbor_event::Special::Break);
927                            break;
928                        }
929                        let extra_data_elem_key = ByronAny::deserialize(raw)?;
930                        let extra_data_elem_value = ByronAny::deserialize(raw)?;
931                        if extra_data_elem_table
932                            .insert(extra_data_elem_key.clone(), extra_data_elem_value)
933                            .is_some()
934                        {
935                            return Err(DeserializeFailure::DuplicateKey(Key::Str(String::from(
936                                "some complicated/unsupported type",
937                            )))
938                            .into());
939                        }
940                    }
941                    extra_data_arr.push(extra_data_elem_table);
942                }
943                Ok(extra_data_arr)
944            })()
945            .map_err(|e| e.annotate("extra_data"))?;
946            match len {
947                cbor_event::Len::Len(_) => (),
948                cbor_event::Len::Indefinite => match raw.special()? {
949                    cbor_event::Special::Break => (),
950                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
951                },
952            }
953            Ok(EbbHead {
954                protocol_magic,
955                prev_block,
956                body_proof,
957                consensus_data,
958                extra_data,
959            })
960        })()
961        .map_err(|e| e.annotate("EbbHead"))
962    }
963}
964
965impl cbor_event::se::Serialize for TxAux {
966    fn serialize<'se, W: Write>(
967        &self,
968        serializer: &'se mut Serializer<W>,
969    ) -> cbor_event::Result<&'se mut Serializer<W>> {
970        serializer.write_array(cbor_event::Len::Len(2))?;
971        self.byron_tx.serialize(serializer)?;
972        serializer.write_array(cbor_event::Len::Len(self.byron_tx_witnesss.len() as u64))?;
973        for element in self.byron_tx_witnesss.iter() {
974            element.serialize(serializer)?;
975        }
976        Ok(serializer)
977    }
978}
979
980impl Deserialize for TxAux {
981    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
982        let len = raw.array()?;
983        let mut read_len = CBORReadLen::from(len);
984        read_len.read_elems(2)?;
985        read_len.finish()?;
986        (|| -> Result<_, DeserializeError> {
987            let byron_tx =
988                ByronTx::deserialize(raw).map_err(|e: DeserializeError| e.annotate("byron_tx"))?;
989            let byron_tx_witnesss = (|| -> Result<_, DeserializeError> {
990                let mut byron_tx_witnesss_arr = Vec::new();
991                let len = raw.array()?;
992                while match len {
993                    cbor_event::Len::Len(n) => (byron_tx_witnesss_arr.len() as u64) < n,
994                    cbor_event::Len::Indefinite => true,
995                } {
996                    if raw.cbor_type()? == cbor_event::Type::Special {
997                        assert_eq!(raw.special()?, cbor_event::Special::Break);
998                        break;
999                    }
1000                    byron_tx_witnesss_arr.push(ByronTxWitness::deserialize(raw)?);
1001                }
1002                Ok(byron_tx_witnesss_arr)
1003            })()
1004            .map_err(|e| e.annotate("byron_tx_witnesss"))?;
1005            match len {
1006                cbor_event::Len::Len(_) => (),
1007                cbor_event::Len::Indefinite => match raw.special()? {
1008                    cbor_event::Special::Break => (),
1009                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
1010                },
1011            }
1012            Ok(TxAux {
1013                byron_tx,
1014                byron_tx_witnesss,
1015            })
1016        })()
1017        .map_err(|e| e.annotate("TxAux"))
1018    }
1019}