1use super::blockchain::*;
4use molecule::prelude::*;
5#[derive(Clone)]
6pub struct BoolOpt(molecule::bytes::Bytes);
7impl ::core::fmt::LowerHex for BoolOpt {
8 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9 use molecule::hex_string;
10 if f.alternate() {
11 write!(f, "0x")?;
12 }
13 write!(f, "{}", hex_string(self.as_slice()))
14 }
15}
16impl ::core::fmt::Debug for BoolOpt {
17 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18 write!(f, "{}({:#x})", Self::NAME, self)
19 }
20}
21impl ::core::fmt::Display for BoolOpt {
22 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
23 if let Some(v) = self.to_opt() {
24 write!(f, "{}(Some({}))", Self::NAME, v)
25 } else {
26 write!(f, "{}(None)", Self::NAME)
27 }
28 }
29}
30impl ::core::default::Default for BoolOpt {
31 fn default() -> Self {
32 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
33 BoolOpt::new_unchecked(v)
34 }
35}
36impl BoolOpt {
37 const DEFAULT_VALUE: [u8; 0] = [];
38 pub fn is_none(&self) -> bool {
39 self.0.is_empty()
40 }
41 pub fn is_some(&self) -> bool {
42 !self.0.is_empty()
43 }
44 pub fn to_opt(&self) -> Option<Bool> {
45 if self.is_none() {
46 None
47 } else {
48 Some(Bool::new_unchecked(self.0.clone()))
49 }
50 }
51 pub fn as_reader<'r>(&'r self) -> BoolOptReader<'r> {
52 BoolOptReader::new_unchecked(self.as_slice())
53 }
54}
55impl molecule::prelude::Entity for BoolOpt {
56 type Builder = BoolOptBuilder;
57 const NAME: &'static str = "BoolOpt";
58 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
59 BoolOpt(data)
60 }
61 fn as_bytes(&self) -> molecule::bytes::Bytes {
62 self.0.clone()
63 }
64 fn as_slice(&self) -> &[u8] {
65 &self.0[..]
66 }
67 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
68 BoolOptReader::from_slice(slice).map(|reader| reader.to_entity())
69 }
70 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
71 BoolOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
72 }
73 fn new_builder() -> Self::Builder {
74 ::core::default::Default::default()
75 }
76 fn as_builder(self) -> Self::Builder {
77 Self::new_builder().set(self.to_opt())
78 }
79}
80#[derive(Clone, Copy)]
81pub struct BoolOptReader<'r>(&'r [u8]);
82impl<'r> ::core::fmt::LowerHex for BoolOptReader<'r> {
83 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
84 use molecule::hex_string;
85 if f.alternate() {
86 write!(f, "0x")?;
87 }
88 write!(f, "{}", hex_string(self.as_slice()))
89 }
90}
91impl<'r> ::core::fmt::Debug for BoolOptReader<'r> {
92 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
93 write!(f, "{}({:#x})", Self::NAME, self)
94 }
95}
96impl<'r> ::core::fmt::Display for BoolOptReader<'r> {
97 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
98 if let Some(v) = self.to_opt() {
99 write!(f, "{}(Some({}))", Self::NAME, v)
100 } else {
101 write!(f, "{}(None)", Self::NAME)
102 }
103 }
104}
105impl<'r> BoolOptReader<'r> {
106 pub fn is_none(&self) -> bool {
107 self.0.is_empty()
108 }
109 pub fn is_some(&self) -> bool {
110 !self.0.is_empty()
111 }
112 pub fn to_opt(&self) -> Option<BoolReader<'r>> {
113 if self.is_none() {
114 None
115 } else {
116 Some(BoolReader::new_unchecked(self.as_slice()))
117 }
118 }
119}
120impl<'r> molecule::prelude::Reader<'r> for BoolOptReader<'r> {
121 type Entity = BoolOpt;
122 const NAME: &'static str = "BoolOptReader";
123 fn to_entity(&self) -> Self::Entity {
124 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
125 }
126 fn new_unchecked(slice: &'r [u8]) -> Self {
127 BoolOptReader(slice)
128 }
129 fn as_slice(&self) -> &'r [u8] {
130 self.0
131 }
132 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
133 if !slice.is_empty() {
134 BoolReader::verify(&slice[..], compatible)?;
135 }
136 Ok(())
137 }
138}
139#[derive(Clone, Debug, Default)]
140pub struct BoolOptBuilder(pub(crate) Option<Bool>);
141impl BoolOptBuilder {
142 pub fn set<T>(mut self, v: T) -> Self
143 where
144 T: ::core::convert::Into<Option<Bool>>,
145 {
146 self.0 = v.into();
147 self
148 }
149}
150impl molecule::prelude::Builder for BoolOptBuilder {
151 type Entity = BoolOpt;
152 const NAME: &'static str = "BoolOptBuilder";
153 fn expected_length(&self) -> usize {
154 self.0
155 .as_ref()
156 .map(|ref inner| inner.as_slice().len())
157 .unwrap_or(0)
158 }
159 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
160 self.0
161 .as_ref()
162 .map(|ref inner| writer.write_all(inner.as_slice()))
163 .unwrap_or(Ok(()))
164 }
165 fn build(&self) -> Self::Entity {
166 let mut inner = Vec::with_capacity(self.expected_length());
167 self.write(&mut inner)
168 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
169 BoolOpt::new_unchecked(inner.into())
170 }
171}
172impl From<Bool> for BoolOpt {
173 fn from(value: Bool) -> Self {
174 Self::new_builder().set(Some(value)).build()
175 }
176}
177#[derive(Clone)]
178pub struct Byte32Opt(molecule::bytes::Bytes);
179impl ::core::fmt::LowerHex for Byte32Opt {
180 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
181 use molecule::hex_string;
182 if f.alternate() {
183 write!(f, "0x")?;
184 }
185 write!(f, "{}", hex_string(self.as_slice()))
186 }
187}
188impl ::core::fmt::Debug for Byte32Opt {
189 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
190 write!(f, "{}({:#x})", Self::NAME, self)
191 }
192}
193impl ::core::fmt::Display for Byte32Opt {
194 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
195 if let Some(v) = self.to_opt() {
196 write!(f, "{}(Some({}))", Self::NAME, v)
197 } else {
198 write!(f, "{}(None)", Self::NAME)
199 }
200 }
201}
202impl ::core::default::Default for Byte32Opt {
203 fn default() -> Self {
204 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
205 Byte32Opt::new_unchecked(v)
206 }
207}
208impl Byte32Opt {
209 const DEFAULT_VALUE: [u8; 0] = [];
210 pub fn is_none(&self) -> bool {
211 self.0.is_empty()
212 }
213 pub fn is_some(&self) -> bool {
214 !self.0.is_empty()
215 }
216 pub fn to_opt(&self) -> Option<Byte32> {
217 if self.is_none() {
218 None
219 } else {
220 Some(Byte32::new_unchecked(self.0.clone()))
221 }
222 }
223 pub fn as_reader<'r>(&'r self) -> Byte32OptReader<'r> {
224 Byte32OptReader::new_unchecked(self.as_slice())
225 }
226}
227impl molecule::prelude::Entity for Byte32Opt {
228 type Builder = Byte32OptBuilder;
229 const NAME: &'static str = "Byte32Opt";
230 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
231 Byte32Opt(data)
232 }
233 fn as_bytes(&self) -> molecule::bytes::Bytes {
234 self.0.clone()
235 }
236 fn as_slice(&self) -> &[u8] {
237 &self.0[..]
238 }
239 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
240 Byte32OptReader::from_slice(slice).map(|reader| reader.to_entity())
241 }
242 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
243 Byte32OptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
244 }
245 fn new_builder() -> Self::Builder {
246 ::core::default::Default::default()
247 }
248 fn as_builder(self) -> Self::Builder {
249 Self::new_builder().set(self.to_opt())
250 }
251}
252#[derive(Clone, Copy)]
253pub struct Byte32OptReader<'r>(&'r [u8]);
254impl<'r> ::core::fmt::LowerHex for Byte32OptReader<'r> {
255 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
256 use molecule::hex_string;
257 if f.alternate() {
258 write!(f, "0x")?;
259 }
260 write!(f, "{}", hex_string(self.as_slice()))
261 }
262}
263impl<'r> ::core::fmt::Debug for Byte32OptReader<'r> {
264 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
265 write!(f, "{}({:#x})", Self::NAME, self)
266 }
267}
268impl<'r> ::core::fmt::Display for Byte32OptReader<'r> {
269 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
270 if let Some(v) = self.to_opt() {
271 write!(f, "{}(Some({}))", Self::NAME, v)
272 } else {
273 write!(f, "{}(None)", Self::NAME)
274 }
275 }
276}
277impl<'r> Byte32OptReader<'r> {
278 pub fn is_none(&self) -> bool {
279 self.0.is_empty()
280 }
281 pub fn is_some(&self) -> bool {
282 !self.0.is_empty()
283 }
284 pub fn to_opt(&self) -> Option<Byte32Reader<'r>> {
285 if self.is_none() {
286 None
287 } else {
288 Some(Byte32Reader::new_unchecked(self.as_slice()))
289 }
290 }
291}
292impl<'r> molecule::prelude::Reader<'r> for Byte32OptReader<'r> {
293 type Entity = Byte32Opt;
294 const NAME: &'static str = "Byte32OptReader";
295 fn to_entity(&self) -> Self::Entity {
296 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
297 }
298 fn new_unchecked(slice: &'r [u8]) -> Self {
299 Byte32OptReader(slice)
300 }
301 fn as_slice(&self) -> &'r [u8] {
302 self.0
303 }
304 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
305 if !slice.is_empty() {
306 Byte32Reader::verify(&slice[..], compatible)?;
307 }
308 Ok(())
309 }
310}
311#[derive(Clone, Debug, Default)]
312pub struct Byte32OptBuilder(pub(crate) Option<Byte32>);
313impl Byte32OptBuilder {
314 pub fn set<T>(mut self, v: T) -> Self
315 where
316 T: ::core::convert::Into<Option<Byte32>>,
317 {
318 self.0 = v.into();
319 self
320 }
321}
322impl molecule::prelude::Builder for Byte32OptBuilder {
323 type Entity = Byte32Opt;
324 const NAME: &'static str = "Byte32OptBuilder";
325 fn expected_length(&self) -> usize {
326 self.0
327 .as_ref()
328 .map(|ref inner| inner.as_slice().len())
329 .unwrap_or(0)
330 }
331 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
332 self.0
333 .as_ref()
334 .map(|ref inner| writer.write_all(inner.as_slice()))
335 .unwrap_or(Ok(()))
336 }
337 fn build(&self) -> Self::Entity {
338 let mut inner = Vec::with_capacity(self.expected_length());
339 self.write(&mut inner)
340 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
341 Byte32Opt::new_unchecked(inner.into())
342 }
343}
344impl From<Byte32> for Byte32Opt {
345 fn from(value: Byte32) -> Self {
346 Self::new_builder().set(Some(value)).build()
347 }
348}
349#[derive(Clone)]
350pub struct Bool(molecule::bytes::Bytes);
351impl ::core::fmt::LowerHex for Bool {
352 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
353 use molecule::hex_string;
354 if f.alternate() {
355 write!(f, "0x")?;
356 }
357 write!(f, "{}", hex_string(self.as_slice()))
358 }
359}
360impl ::core::fmt::Debug for Bool {
361 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
362 write!(f, "{}({:#x})", Self::NAME, self)
363 }
364}
365impl ::core::fmt::Display for Bool {
366 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
367 use molecule::hex_string;
368 let raw_data = hex_string(&self.raw_data());
369 write!(f, "{}(0x{})", Self::NAME, raw_data)
370 }
371}
372impl ::core::default::Default for Bool {
373 fn default() -> Self {
374 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
375 Bool::new_unchecked(v)
376 }
377}
378impl Bool {
379 const DEFAULT_VALUE: [u8; 1] = [0];
380 pub const TOTAL_SIZE: usize = 1;
381 pub const ITEM_SIZE: usize = 1;
382 pub const ITEM_COUNT: usize = 1;
383 pub fn nth0(&self) -> Byte {
384 Byte::new_unchecked(self.0.slice(0..1))
385 }
386 pub fn raw_data(&self) -> molecule::bytes::Bytes {
387 self.as_bytes()
388 }
389 pub fn as_reader<'r>(&'r self) -> BoolReader<'r> {
390 BoolReader::new_unchecked(self.as_slice())
391 }
392}
393impl molecule::prelude::Entity for Bool {
394 type Builder = BoolBuilder;
395 const NAME: &'static str = "Bool";
396 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
397 Bool(data)
398 }
399 fn as_bytes(&self) -> molecule::bytes::Bytes {
400 self.0.clone()
401 }
402 fn as_slice(&self) -> &[u8] {
403 &self.0[..]
404 }
405 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
406 BoolReader::from_slice(slice).map(|reader| reader.to_entity())
407 }
408 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
409 BoolReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
410 }
411 fn new_builder() -> Self::Builder {
412 ::core::default::Default::default()
413 }
414 fn as_builder(self) -> Self::Builder {
415 Self::new_builder().set([self.nth0()])
416 }
417}
418#[derive(Clone, Copy)]
419pub struct BoolReader<'r>(&'r [u8]);
420impl<'r> ::core::fmt::LowerHex for BoolReader<'r> {
421 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
422 use molecule::hex_string;
423 if f.alternate() {
424 write!(f, "0x")?;
425 }
426 write!(f, "{}", hex_string(self.as_slice()))
427 }
428}
429impl<'r> ::core::fmt::Debug for BoolReader<'r> {
430 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
431 write!(f, "{}({:#x})", Self::NAME, self)
432 }
433}
434impl<'r> ::core::fmt::Display for BoolReader<'r> {
435 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
436 use molecule::hex_string;
437 let raw_data = hex_string(&self.raw_data());
438 write!(f, "{}(0x{})", Self::NAME, raw_data)
439 }
440}
441impl<'r> BoolReader<'r> {
442 pub const TOTAL_SIZE: usize = 1;
443 pub const ITEM_SIZE: usize = 1;
444 pub const ITEM_COUNT: usize = 1;
445 pub fn nth0(&self) -> ByteReader<'r> {
446 ByteReader::new_unchecked(&self.as_slice()[0..1])
447 }
448 pub fn raw_data(&self) -> &'r [u8] {
449 self.as_slice()
450 }
451}
452impl<'r> molecule::prelude::Reader<'r> for BoolReader<'r> {
453 type Entity = Bool;
454 const NAME: &'static str = "BoolReader";
455 fn to_entity(&self) -> Self::Entity {
456 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
457 }
458 fn new_unchecked(slice: &'r [u8]) -> Self {
459 BoolReader(slice)
460 }
461 fn as_slice(&self) -> &'r [u8] {
462 self.0
463 }
464 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
465 use molecule::verification_error as ve;
466 let slice_len = slice.len();
467 if slice_len != Self::TOTAL_SIZE {
468 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
469 }
470 Ok(())
471 }
472}
473#[derive(Clone)]
474pub struct BoolBuilder(pub(crate) [Byte; 1]);
475impl ::core::fmt::Debug for BoolBuilder {
476 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
477 write!(f, "{}({:?})", Self::NAME, &self.0[..])
478 }
479}
480impl ::core::default::Default for BoolBuilder {
481 fn default() -> Self {
482 BoolBuilder([Byte::default()])
483 }
484}
485impl BoolBuilder {
486 pub const TOTAL_SIZE: usize = 1;
487 pub const ITEM_SIZE: usize = 1;
488 pub const ITEM_COUNT: usize = 1;
489 pub fn set<T>(mut self, v: T) -> Self
490 where
491 T: ::core::convert::Into<[Byte; 1]>,
492 {
493 self.0 = v.into();
494 self
495 }
496 pub fn nth0<T>(mut self, v: T) -> Self
497 where
498 T: ::core::convert::Into<Byte>,
499 {
500 self.0[0] = v.into();
501 self
502 }
503}
504impl molecule::prelude::Builder for BoolBuilder {
505 type Entity = Bool;
506 const NAME: &'static str = "BoolBuilder";
507 fn expected_length(&self) -> usize {
508 Self::TOTAL_SIZE
509 }
510 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
511 writer.write_all(self.0[0].as_slice())?;
512 Ok(())
513 }
514 fn build(&self) -> Self::Entity {
515 let mut inner = Vec::with_capacity(self.expected_length());
516 self.write(&mut inner)
517 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
518 Bool::new_unchecked(inner.into())
519 }
520}
521impl From<[Byte; 1usize]> for Bool {
522 fn from(value: [Byte; 1usize]) -> Self {
523 Self::new_builder().set(value).build()
524 }
525}
526impl ::core::convert::TryFrom<&[Byte]> for Bool {
527 type Error = ::core::array::TryFromSliceError;
528 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
529 Ok(Self::new_builder()
530 .set(<&[Byte; 1usize]>::try_from(value)?.clone())
531 .build())
532 }
533}
534impl From<Bool> for [Byte; 1usize] {
535 #[track_caller]
536 fn from(value: Bool) -> Self {
537 [value.nth0()]
538 }
539}
540impl From<[u8; 1usize]> for Bool {
541 fn from(value: [u8; 1usize]) -> Self {
542 BoolReader::new_unchecked(&value).to_entity()
543 }
544}
545impl ::core::convert::TryFrom<&[u8]> for Bool {
546 type Error = ::core::array::TryFromSliceError;
547 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
548 Ok(<[u8; 1usize]>::try_from(value)?.into())
549 }
550}
551impl From<Bool> for [u8; 1usize] {
552 #[track_caller]
553 fn from(value: Bool) -> Self {
554 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
555 }
556}
557impl<'a> From<BoolReader<'a>> for &'a [u8; 1usize] {
558 #[track_caller]
559 fn from(value: BoolReader<'a>) -> Self {
560 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
561 }
562}
563impl<'a> From<&'a BoolReader<'a>> for &'a [u8; 1usize] {
564 #[track_caller]
565 fn from(value: &'a BoolReader<'a>) -> Self {
566 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
567 }
568}
569#[derive(Clone)]
570pub struct BeUint32(molecule::bytes::Bytes);
571impl ::core::fmt::LowerHex for BeUint32 {
572 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
573 use molecule::hex_string;
574 if f.alternate() {
575 write!(f, "0x")?;
576 }
577 write!(f, "{}", hex_string(self.as_slice()))
578 }
579}
580impl ::core::fmt::Debug for BeUint32 {
581 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
582 write!(f, "{}({:#x})", Self::NAME, self)
583 }
584}
585impl ::core::fmt::Display for BeUint32 {
586 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
587 use molecule::hex_string;
588 let raw_data = hex_string(&self.raw_data());
589 write!(f, "{}(0x{})", Self::NAME, raw_data)
590 }
591}
592impl ::core::default::Default for BeUint32 {
593 fn default() -> Self {
594 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
595 BeUint32::new_unchecked(v)
596 }
597}
598impl BeUint32 {
599 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
600 pub const TOTAL_SIZE: usize = 4;
601 pub const ITEM_SIZE: usize = 1;
602 pub const ITEM_COUNT: usize = 4;
603 pub fn nth0(&self) -> Byte {
604 Byte::new_unchecked(self.0.slice(0..1))
605 }
606 pub fn nth1(&self) -> Byte {
607 Byte::new_unchecked(self.0.slice(1..2))
608 }
609 pub fn nth2(&self) -> Byte {
610 Byte::new_unchecked(self.0.slice(2..3))
611 }
612 pub fn nth3(&self) -> Byte {
613 Byte::new_unchecked(self.0.slice(3..4))
614 }
615 pub fn raw_data(&self) -> molecule::bytes::Bytes {
616 self.as_bytes()
617 }
618 pub fn as_reader<'r>(&'r self) -> BeUint32Reader<'r> {
619 BeUint32Reader::new_unchecked(self.as_slice())
620 }
621}
622impl molecule::prelude::Entity for BeUint32 {
623 type Builder = BeUint32Builder;
624 const NAME: &'static str = "BeUint32";
625 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
626 BeUint32(data)
627 }
628 fn as_bytes(&self) -> molecule::bytes::Bytes {
629 self.0.clone()
630 }
631 fn as_slice(&self) -> &[u8] {
632 &self.0[..]
633 }
634 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
635 BeUint32Reader::from_slice(slice).map(|reader| reader.to_entity())
636 }
637 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
638 BeUint32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
639 }
640 fn new_builder() -> Self::Builder {
641 ::core::default::Default::default()
642 }
643 fn as_builder(self) -> Self::Builder {
644 Self::new_builder().set([self.nth0(), self.nth1(), self.nth2(), self.nth3()])
645 }
646}
647#[derive(Clone, Copy)]
648pub struct BeUint32Reader<'r>(&'r [u8]);
649impl<'r> ::core::fmt::LowerHex for BeUint32Reader<'r> {
650 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
651 use molecule::hex_string;
652 if f.alternate() {
653 write!(f, "0x")?;
654 }
655 write!(f, "{}", hex_string(self.as_slice()))
656 }
657}
658impl<'r> ::core::fmt::Debug for BeUint32Reader<'r> {
659 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
660 write!(f, "{}({:#x})", Self::NAME, self)
661 }
662}
663impl<'r> ::core::fmt::Display for BeUint32Reader<'r> {
664 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
665 use molecule::hex_string;
666 let raw_data = hex_string(&self.raw_data());
667 write!(f, "{}(0x{})", Self::NAME, raw_data)
668 }
669}
670impl<'r> BeUint32Reader<'r> {
671 pub const TOTAL_SIZE: usize = 4;
672 pub const ITEM_SIZE: usize = 1;
673 pub const ITEM_COUNT: usize = 4;
674 pub fn nth0(&self) -> ByteReader<'r> {
675 ByteReader::new_unchecked(&self.as_slice()[0..1])
676 }
677 pub fn nth1(&self) -> ByteReader<'r> {
678 ByteReader::new_unchecked(&self.as_slice()[1..2])
679 }
680 pub fn nth2(&self) -> ByteReader<'r> {
681 ByteReader::new_unchecked(&self.as_slice()[2..3])
682 }
683 pub fn nth3(&self) -> ByteReader<'r> {
684 ByteReader::new_unchecked(&self.as_slice()[3..4])
685 }
686 pub fn raw_data(&self) -> &'r [u8] {
687 self.as_slice()
688 }
689}
690impl<'r> molecule::prelude::Reader<'r> for BeUint32Reader<'r> {
691 type Entity = BeUint32;
692 const NAME: &'static str = "BeUint32Reader";
693 fn to_entity(&self) -> Self::Entity {
694 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
695 }
696 fn new_unchecked(slice: &'r [u8]) -> Self {
697 BeUint32Reader(slice)
698 }
699 fn as_slice(&self) -> &'r [u8] {
700 self.0
701 }
702 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
703 use molecule::verification_error as ve;
704 let slice_len = slice.len();
705 if slice_len != Self::TOTAL_SIZE {
706 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
707 }
708 Ok(())
709 }
710}
711#[derive(Clone)]
712pub struct BeUint32Builder(pub(crate) [Byte; 4]);
713impl ::core::fmt::Debug for BeUint32Builder {
714 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
715 write!(f, "{}({:?})", Self::NAME, &self.0[..])
716 }
717}
718impl ::core::default::Default for BeUint32Builder {
719 fn default() -> Self {
720 BeUint32Builder([
721 Byte::default(),
722 Byte::default(),
723 Byte::default(),
724 Byte::default(),
725 ])
726 }
727}
728impl BeUint32Builder {
729 pub const TOTAL_SIZE: usize = 4;
730 pub const ITEM_SIZE: usize = 1;
731 pub const ITEM_COUNT: usize = 4;
732 pub fn set<T>(mut self, v: T) -> Self
733 where
734 T: ::core::convert::Into<[Byte; 4]>,
735 {
736 self.0 = v.into();
737 self
738 }
739 pub fn nth0<T>(mut self, v: T) -> Self
740 where
741 T: ::core::convert::Into<Byte>,
742 {
743 self.0[0] = v.into();
744 self
745 }
746 pub fn nth1<T>(mut self, v: T) -> Self
747 where
748 T: ::core::convert::Into<Byte>,
749 {
750 self.0[1] = v.into();
751 self
752 }
753 pub fn nth2<T>(mut self, v: T) -> Self
754 where
755 T: ::core::convert::Into<Byte>,
756 {
757 self.0[2] = v.into();
758 self
759 }
760 pub fn nth3<T>(mut self, v: T) -> Self
761 where
762 T: ::core::convert::Into<Byte>,
763 {
764 self.0[3] = v.into();
765 self
766 }
767}
768impl molecule::prelude::Builder for BeUint32Builder {
769 type Entity = BeUint32;
770 const NAME: &'static str = "BeUint32Builder";
771 fn expected_length(&self) -> usize {
772 Self::TOTAL_SIZE
773 }
774 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
775 writer.write_all(self.0[0].as_slice())?;
776 writer.write_all(self.0[1].as_slice())?;
777 writer.write_all(self.0[2].as_slice())?;
778 writer.write_all(self.0[3].as_slice())?;
779 Ok(())
780 }
781 fn build(&self) -> Self::Entity {
782 let mut inner = Vec::with_capacity(self.expected_length());
783 self.write(&mut inner)
784 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
785 BeUint32::new_unchecked(inner.into())
786 }
787}
788impl From<[Byte; 4usize]> for BeUint32 {
789 fn from(value: [Byte; 4usize]) -> Self {
790 Self::new_builder().set(value).build()
791 }
792}
793impl ::core::convert::TryFrom<&[Byte]> for BeUint32 {
794 type Error = ::core::array::TryFromSliceError;
795 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
796 Ok(Self::new_builder()
797 .set(<&[Byte; 4usize]>::try_from(value)?.clone())
798 .build())
799 }
800}
801impl From<BeUint32> for [Byte; 4usize] {
802 #[track_caller]
803 fn from(value: BeUint32) -> Self {
804 [value.nth0(), value.nth1(), value.nth2(), value.nth3()]
805 }
806}
807impl From<[u8; 4usize]> for BeUint32 {
808 fn from(value: [u8; 4usize]) -> Self {
809 BeUint32Reader::new_unchecked(&value).to_entity()
810 }
811}
812impl ::core::convert::TryFrom<&[u8]> for BeUint32 {
813 type Error = ::core::array::TryFromSliceError;
814 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
815 Ok(<[u8; 4usize]>::try_from(value)?.into())
816 }
817}
818impl From<BeUint32> for [u8; 4usize] {
819 #[track_caller]
820 fn from(value: BeUint32) -> Self {
821 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
822 }
823}
824impl<'a> From<BeUint32Reader<'a>> for &'a [u8; 4usize] {
825 #[track_caller]
826 fn from(value: BeUint32Reader<'a>) -> Self {
827 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
828 }
829}
830impl<'a> From<&'a BeUint32Reader<'a>> for &'a [u8; 4usize] {
831 #[track_caller]
832 fn from(value: &'a BeUint32Reader<'a>) -> Self {
833 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
834 }
835}
836#[derive(Clone)]
837pub struct BeUint64(molecule::bytes::Bytes);
838impl ::core::fmt::LowerHex for BeUint64 {
839 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
840 use molecule::hex_string;
841 if f.alternate() {
842 write!(f, "0x")?;
843 }
844 write!(f, "{}", hex_string(self.as_slice()))
845 }
846}
847impl ::core::fmt::Debug for BeUint64 {
848 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
849 write!(f, "{}({:#x})", Self::NAME, self)
850 }
851}
852impl ::core::fmt::Display for BeUint64 {
853 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
854 use molecule::hex_string;
855 let raw_data = hex_string(&self.raw_data());
856 write!(f, "{}(0x{})", Self::NAME, raw_data)
857 }
858}
859impl ::core::default::Default for BeUint64 {
860 fn default() -> Self {
861 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
862 BeUint64::new_unchecked(v)
863 }
864}
865impl BeUint64 {
866 const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
867 pub const TOTAL_SIZE: usize = 8;
868 pub const ITEM_SIZE: usize = 1;
869 pub const ITEM_COUNT: usize = 8;
870 pub fn nth0(&self) -> Byte {
871 Byte::new_unchecked(self.0.slice(0..1))
872 }
873 pub fn nth1(&self) -> Byte {
874 Byte::new_unchecked(self.0.slice(1..2))
875 }
876 pub fn nth2(&self) -> Byte {
877 Byte::new_unchecked(self.0.slice(2..3))
878 }
879 pub fn nth3(&self) -> Byte {
880 Byte::new_unchecked(self.0.slice(3..4))
881 }
882 pub fn nth4(&self) -> Byte {
883 Byte::new_unchecked(self.0.slice(4..5))
884 }
885 pub fn nth5(&self) -> Byte {
886 Byte::new_unchecked(self.0.slice(5..6))
887 }
888 pub fn nth6(&self) -> Byte {
889 Byte::new_unchecked(self.0.slice(6..7))
890 }
891 pub fn nth7(&self) -> Byte {
892 Byte::new_unchecked(self.0.slice(7..8))
893 }
894 pub fn raw_data(&self) -> molecule::bytes::Bytes {
895 self.as_bytes()
896 }
897 pub fn as_reader<'r>(&'r self) -> BeUint64Reader<'r> {
898 BeUint64Reader::new_unchecked(self.as_slice())
899 }
900}
901impl molecule::prelude::Entity for BeUint64 {
902 type Builder = BeUint64Builder;
903 const NAME: &'static str = "BeUint64";
904 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
905 BeUint64(data)
906 }
907 fn as_bytes(&self) -> molecule::bytes::Bytes {
908 self.0.clone()
909 }
910 fn as_slice(&self) -> &[u8] {
911 &self.0[..]
912 }
913 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
914 BeUint64Reader::from_slice(slice).map(|reader| reader.to_entity())
915 }
916 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
917 BeUint64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
918 }
919 fn new_builder() -> Self::Builder {
920 ::core::default::Default::default()
921 }
922 fn as_builder(self) -> Self::Builder {
923 Self::new_builder().set([
924 self.nth0(),
925 self.nth1(),
926 self.nth2(),
927 self.nth3(),
928 self.nth4(),
929 self.nth5(),
930 self.nth6(),
931 self.nth7(),
932 ])
933 }
934}
935#[derive(Clone, Copy)]
936pub struct BeUint64Reader<'r>(&'r [u8]);
937impl<'r> ::core::fmt::LowerHex for BeUint64Reader<'r> {
938 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
939 use molecule::hex_string;
940 if f.alternate() {
941 write!(f, "0x")?;
942 }
943 write!(f, "{}", hex_string(self.as_slice()))
944 }
945}
946impl<'r> ::core::fmt::Debug for BeUint64Reader<'r> {
947 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
948 write!(f, "{}({:#x})", Self::NAME, self)
949 }
950}
951impl<'r> ::core::fmt::Display for BeUint64Reader<'r> {
952 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
953 use molecule::hex_string;
954 let raw_data = hex_string(&self.raw_data());
955 write!(f, "{}(0x{})", Self::NAME, raw_data)
956 }
957}
958impl<'r> BeUint64Reader<'r> {
959 pub const TOTAL_SIZE: usize = 8;
960 pub const ITEM_SIZE: usize = 1;
961 pub const ITEM_COUNT: usize = 8;
962 pub fn nth0(&self) -> ByteReader<'r> {
963 ByteReader::new_unchecked(&self.as_slice()[0..1])
964 }
965 pub fn nth1(&self) -> ByteReader<'r> {
966 ByteReader::new_unchecked(&self.as_slice()[1..2])
967 }
968 pub fn nth2(&self) -> ByteReader<'r> {
969 ByteReader::new_unchecked(&self.as_slice()[2..3])
970 }
971 pub fn nth3(&self) -> ByteReader<'r> {
972 ByteReader::new_unchecked(&self.as_slice()[3..4])
973 }
974 pub fn nth4(&self) -> ByteReader<'r> {
975 ByteReader::new_unchecked(&self.as_slice()[4..5])
976 }
977 pub fn nth5(&self) -> ByteReader<'r> {
978 ByteReader::new_unchecked(&self.as_slice()[5..6])
979 }
980 pub fn nth6(&self) -> ByteReader<'r> {
981 ByteReader::new_unchecked(&self.as_slice()[6..7])
982 }
983 pub fn nth7(&self) -> ByteReader<'r> {
984 ByteReader::new_unchecked(&self.as_slice()[7..8])
985 }
986 pub fn raw_data(&self) -> &'r [u8] {
987 self.as_slice()
988 }
989}
990impl<'r> molecule::prelude::Reader<'r> for BeUint64Reader<'r> {
991 type Entity = BeUint64;
992 const NAME: &'static str = "BeUint64Reader";
993 fn to_entity(&self) -> Self::Entity {
994 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
995 }
996 fn new_unchecked(slice: &'r [u8]) -> Self {
997 BeUint64Reader(slice)
998 }
999 fn as_slice(&self) -> &'r [u8] {
1000 self.0
1001 }
1002 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1003 use molecule::verification_error as ve;
1004 let slice_len = slice.len();
1005 if slice_len != Self::TOTAL_SIZE {
1006 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
1007 }
1008 Ok(())
1009 }
1010}
1011#[derive(Clone)]
1012pub struct BeUint64Builder(pub(crate) [Byte; 8]);
1013impl ::core::fmt::Debug for BeUint64Builder {
1014 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1015 write!(f, "{}({:?})", Self::NAME, &self.0[..])
1016 }
1017}
1018impl ::core::default::Default for BeUint64Builder {
1019 fn default() -> Self {
1020 BeUint64Builder([
1021 Byte::default(),
1022 Byte::default(),
1023 Byte::default(),
1024 Byte::default(),
1025 Byte::default(),
1026 Byte::default(),
1027 Byte::default(),
1028 Byte::default(),
1029 ])
1030 }
1031}
1032impl BeUint64Builder {
1033 pub const TOTAL_SIZE: usize = 8;
1034 pub const ITEM_SIZE: usize = 1;
1035 pub const ITEM_COUNT: usize = 8;
1036 pub fn set<T>(mut self, v: T) -> Self
1037 where
1038 T: ::core::convert::Into<[Byte; 8]>,
1039 {
1040 self.0 = v.into();
1041 self
1042 }
1043 pub fn nth0<T>(mut self, v: T) -> Self
1044 where
1045 T: ::core::convert::Into<Byte>,
1046 {
1047 self.0[0] = v.into();
1048 self
1049 }
1050 pub fn nth1<T>(mut self, v: T) -> Self
1051 where
1052 T: ::core::convert::Into<Byte>,
1053 {
1054 self.0[1] = v.into();
1055 self
1056 }
1057 pub fn nth2<T>(mut self, v: T) -> Self
1058 where
1059 T: ::core::convert::Into<Byte>,
1060 {
1061 self.0[2] = v.into();
1062 self
1063 }
1064 pub fn nth3<T>(mut self, v: T) -> Self
1065 where
1066 T: ::core::convert::Into<Byte>,
1067 {
1068 self.0[3] = v.into();
1069 self
1070 }
1071 pub fn nth4<T>(mut self, v: T) -> Self
1072 where
1073 T: ::core::convert::Into<Byte>,
1074 {
1075 self.0[4] = v.into();
1076 self
1077 }
1078 pub fn nth5<T>(mut self, v: T) -> Self
1079 where
1080 T: ::core::convert::Into<Byte>,
1081 {
1082 self.0[5] = v.into();
1083 self
1084 }
1085 pub fn nth6<T>(mut self, v: T) -> Self
1086 where
1087 T: ::core::convert::Into<Byte>,
1088 {
1089 self.0[6] = v.into();
1090 self
1091 }
1092 pub fn nth7<T>(mut self, v: T) -> Self
1093 where
1094 T: ::core::convert::Into<Byte>,
1095 {
1096 self.0[7] = v.into();
1097 self
1098 }
1099}
1100impl molecule::prelude::Builder for BeUint64Builder {
1101 type Entity = BeUint64;
1102 const NAME: &'static str = "BeUint64Builder";
1103 fn expected_length(&self) -> usize {
1104 Self::TOTAL_SIZE
1105 }
1106 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1107 writer.write_all(self.0[0].as_slice())?;
1108 writer.write_all(self.0[1].as_slice())?;
1109 writer.write_all(self.0[2].as_slice())?;
1110 writer.write_all(self.0[3].as_slice())?;
1111 writer.write_all(self.0[4].as_slice())?;
1112 writer.write_all(self.0[5].as_slice())?;
1113 writer.write_all(self.0[6].as_slice())?;
1114 writer.write_all(self.0[7].as_slice())?;
1115 Ok(())
1116 }
1117 fn build(&self) -> Self::Entity {
1118 let mut inner = Vec::with_capacity(self.expected_length());
1119 self.write(&mut inner)
1120 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1121 BeUint64::new_unchecked(inner.into())
1122 }
1123}
1124impl From<[Byte; 8usize]> for BeUint64 {
1125 fn from(value: [Byte; 8usize]) -> Self {
1126 Self::new_builder().set(value).build()
1127 }
1128}
1129impl ::core::convert::TryFrom<&[Byte]> for BeUint64 {
1130 type Error = ::core::array::TryFromSliceError;
1131 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
1132 Ok(Self::new_builder()
1133 .set(<&[Byte; 8usize]>::try_from(value)?.clone())
1134 .build())
1135 }
1136}
1137impl From<BeUint64> for [Byte; 8usize] {
1138 #[track_caller]
1139 fn from(value: BeUint64) -> Self {
1140 [
1141 value.nth0(),
1142 value.nth1(),
1143 value.nth2(),
1144 value.nth3(),
1145 value.nth4(),
1146 value.nth5(),
1147 value.nth6(),
1148 value.nth7(),
1149 ]
1150 }
1151}
1152impl From<[u8; 8usize]> for BeUint64 {
1153 fn from(value: [u8; 8usize]) -> Self {
1154 BeUint64Reader::new_unchecked(&value).to_entity()
1155 }
1156}
1157impl ::core::convert::TryFrom<&[u8]> for BeUint64 {
1158 type Error = ::core::array::TryFromSliceError;
1159 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
1160 Ok(<[u8; 8usize]>::try_from(value)?.into())
1161 }
1162}
1163impl From<BeUint64> for [u8; 8usize] {
1164 #[track_caller]
1165 fn from(value: BeUint64) -> Self {
1166 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1167 }
1168}
1169impl<'a> From<BeUint64Reader<'a>> for &'a [u8; 8usize] {
1170 #[track_caller]
1171 fn from(value: BeUint64Reader<'a>) -> Self {
1172 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1173 }
1174}
1175impl<'a> From<&'a BeUint64Reader<'a>> for &'a [u8; 8usize] {
1176 #[track_caller]
1177 fn from(value: &'a BeUint64Reader<'a>) -> Self {
1178 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1179 }
1180}
1181#[derive(Clone)]
1182pub struct Uint32Vec(molecule::bytes::Bytes);
1183impl ::core::fmt::LowerHex for Uint32Vec {
1184 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1185 use molecule::hex_string;
1186 if f.alternate() {
1187 write!(f, "0x")?;
1188 }
1189 write!(f, "{}", hex_string(self.as_slice()))
1190 }
1191}
1192impl ::core::fmt::Debug for Uint32Vec {
1193 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1194 write!(f, "{}({:#x})", Self::NAME, self)
1195 }
1196}
1197impl ::core::fmt::Display for Uint32Vec {
1198 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1199 write!(f, "{} [", Self::NAME)?;
1200 for i in 0..self.len() {
1201 if i == 0 {
1202 write!(f, "{}", self.get_unchecked(i))?;
1203 } else {
1204 write!(f, ", {}", self.get_unchecked(i))?;
1205 }
1206 }
1207 write!(f, "]")
1208 }
1209}
1210impl ::core::default::Default for Uint32Vec {
1211 fn default() -> Self {
1212 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1213 Uint32Vec::new_unchecked(v)
1214 }
1215}
1216impl Uint32Vec {
1217 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
1218 pub const ITEM_SIZE: usize = 4;
1219 pub fn total_size(&self) -> usize {
1220 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1221 }
1222 pub fn item_count(&self) -> usize {
1223 molecule::unpack_number(self.as_slice()) as usize
1224 }
1225 pub fn len(&self) -> usize {
1226 self.item_count()
1227 }
1228 pub fn is_empty(&self) -> bool {
1229 self.len() == 0
1230 }
1231 pub fn get(&self, idx: usize) -> Option<Uint32> {
1232 if idx >= self.len() {
1233 None
1234 } else {
1235 Some(self.get_unchecked(idx))
1236 }
1237 }
1238 pub fn get_unchecked(&self, idx: usize) -> Uint32 {
1239 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1240 let end = start + Self::ITEM_SIZE;
1241 Uint32::new_unchecked(self.0.slice(start..end))
1242 }
1243 pub fn as_reader<'r>(&'r self) -> Uint32VecReader<'r> {
1244 Uint32VecReader::new_unchecked(self.as_slice())
1245 }
1246}
1247impl molecule::prelude::Entity for Uint32Vec {
1248 type Builder = Uint32VecBuilder;
1249 const NAME: &'static str = "Uint32Vec";
1250 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1251 Uint32Vec(data)
1252 }
1253 fn as_bytes(&self) -> molecule::bytes::Bytes {
1254 self.0.clone()
1255 }
1256 fn as_slice(&self) -> &[u8] {
1257 &self.0[..]
1258 }
1259 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1260 Uint32VecReader::from_slice(slice).map(|reader| reader.to_entity())
1261 }
1262 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1263 Uint32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1264 }
1265 fn new_builder() -> Self::Builder {
1266 ::core::default::Default::default()
1267 }
1268 fn as_builder(self) -> Self::Builder {
1269 Self::new_builder().extend(self.into_iter())
1270 }
1271}
1272#[derive(Clone, Copy)]
1273pub struct Uint32VecReader<'r>(&'r [u8]);
1274impl<'r> ::core::fmt::LowerHex for Uint32VecReader<'r> {
1275 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1276 use molecule::hex_string;
1277 if f.alternate() {
1278 write!(f, "0x")?;
1279 }
1280 write!(f, "{}", hex_string(self.as_slice()))
1281 }
1282}
1283impl<'r> ::core::fmt::Debug for Uint32VecReader<'r> {
1284 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1285 write!(f, "{}({:#x})", Self::NAME, self)
1286 }
1287}
1288impl<'r> ::core::fmt::Display for Uint32VecReader<'r> {
1289 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1290 write!(f, "{} [", Self::NAME)?;
1291 for i in 0..self.len() {
1292 if i == 0 {
1293 write!(f, "{}", self.get_unchecked(i))?;
1294 } else {
1295 write!(f, ", {}", self.get_unchecked(i))?;
1296 }
1297 }
1298 write!(f, "]")
1299 }
1300}
1301impl<'r> Uint32VecReader<'r> {
1302 pub const ITEM_SIZE: usize = 4;
1303 pub fn total_size(&self) -> usize {
1304 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1305 }
1306 pub fn item_count(&self) -> usize {
1307 molecule::unpack_number(self.as_slice()) as usize
1308 }
1309 pub fn len(&self) -> usize {
1310 self.item_count()
1311 }
1312 pub fn is_empty(&self) -> bool {
1313 self.len() == 0
1314 }
1315 pub fn get(&self, idx: usize) -> Option<Uint32Reader<'r>> {
1316 if idx >= self.len() {
1317 None
1318 } else {
1319 Some(self.get_unchecked(idx))
1320 }
1321 }
1322 pub fn get_unchecked(&self, idx: usize) -> Uint32Reader<'r> {
1323 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1324 let end = start + Self::ITEM_SIZE;
1325 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
1326 }
1327}
1328impl<'r> molecule::prelude::Reader<'r> for Uint32VecReader<'r> {
1329 type Entity = Uint32Vec;
1330 const NAME: &'static str = "Uint32VecReader";
1331 fn to_entity(&self) -> Self::Entity {
1332 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1333 }
1334 fn new_unchecked(slice: &'r [u8]) -> Self {
1335 Uint32VecReader(slice)
1336 }
1337 fn as_slice(&self) -> &'r [u8] {
1338 self.0
1339 }
1340 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1341 use molecule::verification_error as ve;
1342 let slice_len = slice.len();
1343 if slice_len < molecule::NUMBER_SIZE {
1344 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
1345 }
1346 let item_count = molecule::unpack_number(slice) as usize;
1347 if item_count == 0 {
1348 if slice_len != molecule::NUMBER_SIZE {
1349 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
1350 }
1351 return Ok(());
1352 }
1353 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
1354 if slice_len != total_size {
1355 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
1356 }
1357 Ok(())
1358 }
1359}
1360#[derive(Clone, Debug, Default)]
1361pub struct Uint32VecBuilder(pub(crate) Vec<Uint32>);
1362impl Uint32VecBuilder {
1363 pub const ITEM_SIZE: usize = 4;
1364 pub fn set(mut self, v: Vec<Uint32>) -> Self {
1365 self.0 = v;
1366 self
1367 }
1368 pub fn push<T>(mut self, v: T) -> Self
1369 where
1370 T: ::core::convert::Into<Uint32>,
1371 {
1372 self.0.push(v.into());
1373 self
1374 }
1375 pub fn extend<T: ::core::iter::IntoIterator<Item = Uint32>>(mut self, iter: T) -> Self {
1376 self.0.extend(iter);
1377 self
1378 }
1379 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Uint32>
1380 where
1381 T: ::core::convert::Into<Uint32>,
1382 {
1383 self.0
1384 .get_mut(index)
1385 .map(|item| ::core::mem::replace(item, v.into()))
1386 }
1387}
1388impl molecule::prelude::Builder for Uint32VecBuilder {
1389 type Entity = Uint32Vec;
1390 const NAME: &'static str = "Uint32VecBuilder";
1391 fn expected_length(&self) -> usize {
1392 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
1393 }
1394 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1395 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
1396 for inner in &self.0[..] {
1397 writer.write_all(inner.as_slice())?;
1398 }
1399 Ok(())
1400 }
1401 fn build(&self) -> Self::Entity {
1402 let mut inner = Vec::with_capacity(self.expected_length());
1403 self.write(&mut inner)
1404 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1405 Uint32Vec::new_unchecked(inner.into())
1406 }
1407}
1408pub struct Uint32VecIterator(Uint32Vec, usize, usize);
1409impl ::core::iter::Iterator for Uint32VecIterator {
1410 type Item = Uint32;
1411 fn next(&mut self) -> Option<Self::Item> {
1412 if self.1 >= self.2 {
1413 None
1414 } else {
1415 let ret = self.0.get_unchecked(self.1);
1416 self.1 += 1;
1417 Some(ret)
1418 }
1419 }
1420}
1421impl ::core::iter::ExactSizeIterator for Uint32VecIterator {
1422 fn len(&self) -> usize {
1423 self.2 - self.1
1424 }
1425}
1426impl ::core::iter::IntoIterator for Uint32Vec {
1427 type Item = Uint32;
1428 type IntoIter = Uint32VecIterator;
1429 fn into_iter(self) -> Self::IntoIter {
1430 let len = self.len();
1431 Uint32VecIterator(self, 0, len)
1432 }
1433}
1434impl<'r> Uint32VecReader<'r> {
1435 pub fn iter<'t>(&'t self) -> Uint32VecReaderIterator<'t, 'r> {
1436 Uint32VecReaderIterator(&self, 0, self.len())
1437 }
1438}
1439pub struct Uint32VecReaderIterator<'t, 'r>(&'t Uint32VecReader<'r>, usize, usize);
1440impl<'t: 'r, 'r> ::core::iter::Iterator for Uint32VecReaderIterator<'t, 'r> {
1441 type Item = Uint32Reader<'t>;
1442 fn next(&mut self) -> Option<Self::Item> {
1443 if self.1 >= self.2 {
1444 None
1445 } else {
1446 let ret = self.0.get_unchecked(self.1);
1447 self.1 += 1;
1448 Some(ret)
1449 }
1450 }
1451}
1452impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Uint32VecReaderIterator<'t, 'r> {
1453 fn len(&self) -> usize {
1454 self.2 - self.1
1455 }
1456}
1457impl<T> ::core::iter::FromIterator<T> for Uint32Vec
1458where
1459 T: Into<Uint32>,
1460{
1461 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
1462 Self::new_builder()
1463 .extend(iter.into_iter().map(Into::into))
1464 .build()
1465 }
1466}
1467impl<T> From<Vec<T>> for Uint32Vec
1468where
1469 T: Into<Uint32>,
1470{
1471 fn from(v: Vec<T>) -> Self {
1472 v.into_iter().collect()
1473 }
1474}
1475#[derive(Clone)]
1476pub struct Uint64Vec(molecule::bytes::Bytes);
1477impl ::core::fmt::LowerHex for Uint64Vec {
1478 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1479 use molecule::hex_string;
1480 if f.alternate() {
1481 write!(f, "0x")?;
1482 }
1483 write!(f, "{}", hex_string(self.as_slice()))
1484 }
1485}
1486impl ::core::fmt::Debug for Uint64Vec {
1487 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1488 write!(f, "{}({:#x})", Self::NAME, self)
1489 }
1490}
1491impl ::core::fmt::Display for Uint64Vec {
1492 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1493 write!(f, "{} [", Self::NAME)?;
1494 for i in 0..self.len() {
1495 if i == 0 {
1496 write!(f, "{}", self.get_unchecked(i))?;
1497 } else {
1498 write!(f, ", {}", self.get_unchecked(i))?;
1499 }
1500 }
1501 write!(f, "]")
1502 }
1503}
1504impl ::core::default::Default for Uint64Vec {
1505 fn default() -> Self {
1506 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1507 Uint64Vec::new_unchecked(v)
1508 }
1509}
1510impl Uint64Vec {
1511 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
1512 pub const ITEM_SIZE: usize = 8;
1513 pub fn total_size(&self) -> usize {
1514 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1515 }
1516 pub fn item_count(&self) -> usize {
1517 molecule::unpack_number(self.as_slice()) as usize
1518 }
1519 pub fn len(&self) -> usize {
1520 self.item_count()
1521 }
1522 pub fn is_empty(&self) -> bool {
1523 self.len() == 0
1524 }
1525 pub fn get(&self, idx: usize) -> Option<Uint64> {
1526 if idx >= self.len() {
1527 None
1528 } else {
1529 Some(self.get_unchecked(idx))
1530 }
1531 }
1532 pub fn get_unchecked(&self, idx: usize) -> Uint64 {
1533 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1534 let end = start + Self::ITEM_SIZE;
1535 Uint64::new_unchecked(self.0.slice(start..end))
1536 }
1537 pub fn as_reader<'r>(&'r self) -> Uint64VecReader<'r> {
1538 Uint64VecReader::new_unchecked(self.as_slice())
1539 }
1540}
1541impl molecule::prelude::Entity for Uint64Vec {
1542 type Builder = Uint64VecBuilder;
1543 const NAME: &'static str = "Uint64Vec";
1544 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1545 Uint64Vec(data)
1546 }
1547 fn as_bytes(&self) -> molecule::bytes::Bytes {
1548 self.0.clone()
1549 }
1550 fn as_slice(&self) -> &[u8] {
1551 &self.0[..]
1552 }
1553 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1554 Uint64VecReader::from_slice(slice).map(|reader| reader.to_entity())
1555 }
1556 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1557 Uint64VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1558 }
1559 fn new_builder() -> Self::Builder {
1560 ::core::default::Default::default()
1561 }
1562 fn as_builder(self) -> Self::Builder {
1563 Self::new_builder().extend(self.into_iter())
1564 }
1565}
1566#[derive(Clone, Copy)]
1567pub struct Uint64VecReader<'r>(&'r [u8]);
1568impl<'r> ::core::fmt::LowerHex for Uint64VecReader<'r> {
1569 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1570 use molecule::hex_string;
1571 if f.alternate() {
1572 write!(f, "0x")?;
1573 }
1574 write!(f, "{}", hex_string(self.as_slice()))
1575 }
1576}
1577impl<'r> ::core::fmt::Debug for Uint64VecReader<'r> {
1578 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1579 write!(f, "{}({:#x})", Self::NAME, self)
1580 }
1581}
1582impl<'r> ::core::fmt::Display for Uint64VecReader<'r> {
1583 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1584 write!(f, "{} [", Self::NAME)?;
1585 for i in 0..self.len() {
1586 if i == 0 {
1587 write!(f, "{}", self.get_unchecked(i))?;
1588 } else {
1589 write!(f, ", {}", self.get_unchecked(i))?;
1590 }
1591 }
1592 write!(f, "]")
1593 }
1594}
1595impl<'r> Uint64VecReader<'r> {
1596 pub const ITEM_SIZE: usize = 8;
1597 pub fn total_size(&self) -> usize {
1598 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1599 }
1600 pub fn item_count(&self) -> usize {
1601 molecule::unpack_number(self.as_slice()) as usize
1602 }
1603 pub fn len(&self) -> usize {
1604 self.item_count()
1605 }
1606 pub fn is_empty(&self) -> bool {
1607 self.len() == 0
1608 }
1609 pub fn get(&self, idx: usize) -> Option<Uint64Reader<'r>> {
1610 if idx >= self.len() {
1611 None
1612 } else {
1613 Some(self.get_unchecked(idx))
1614 }
1615 }
1616 pub fn get_unchecked(&self, idx: usize) -> Uint64Reader<'r> {
1617 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1618 let end = start + Self::ITEM_SIZE;
1619 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
1620 }
1621}
1622impl<'r> molecule::prelude::Reader<'r> for Uint64VecReader<'r> {
1623 type Entity = Uint64Vec;
1624 const NAME: &'static str = "Uint64VecReader";
1625 fn to_entity(&self) -> Self::Entity {
1626 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1627 }
1628 fn new_unchecked(slice: &'r [u8]) -> Self {
1629 Uint64VecReader(slice)
1630 }
1631 fn as_slice(&self) -> &'r [u8] {
1632 self.0
1633 }
1634 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1635 use molecule::verification_error as ve;
1636 let slice_len = slice.len();
1637 if slice_len < molecule::NUMBER_SIZE {
1638 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
1639 }
1640 let item_count = molecule::unpack_number(slice) as usize;
1641 if item_count == 0 {
1642 if slice_len != molecule::NUMBER_SIZE {
1643 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
1644 }
1645 return Ok(());
1646 }
1647 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
1648 if slice_len != total_size {
1649 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
1650 }
1651 Ok(())
1652 }
1653}
1654#[derive(Clone, Debug, Default)]
1655pub struct Uint64VecBuilder(pub(crate) Vec<Uint64>);
1656impl Uint64VecBuilder {
1657 pub const ITEM_SIZE: usize = 8;
1658 pub fn set(mut self, v: Vec<Uint64>) -> Self {
1659 self.0 = v;
1660 self
1661 }
1662 pub fn push<T>(mut self, v: T) -> Self
1663 where
1664 T: ::core::convert::Into<Uint64>,
1665 {
1666 self.0.push(v.into());
1667 self
1668 }
1669 pub fn extend<T: ::core::iter::IntoIterator<Item = Uint64>>(mut self, iter: T) -> Self {
1670 self.0.extend(iter);
1671 self
1672 }
1673 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Uint64>
1674 where
1675 T: ::core::convert::Into<Uint64>,
1676 {
1677 self.0
1678 .get_mut(index)
1679 .map(|item| ::core::mem::replace(item, v.into()))
1680 }
1681}
1682impl molecule::prelude::Builder for Uint64VecBuilder {
1683 type Entity = Uint64Vec;
1684 const NAME: &'static str = "Uint64VecBuilder";
1685 fn expected_length(&self) -> usize {
1686 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
1687 }
1688 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1689 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
1690 for inner in &self.0[..] {
1691 writer.write_all(inner.as_slice())?;
1692 }
1693 Ok(())
1694 }
1695 fn build(&self) -> Self::Entity {
1696 let mut inner = Vec::with_capacity(self.expected_length());
1697 self.write(&mut inner)
1698 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1699 Uint64Vec::new_unchecked(inner.into())
1700 }
1701}
1702pub struct Uint64VecIterator(Uint64Vec, usize, usize);
1703impl ::core::iter::Iterator for Uint64VecIterator {
1704 type Item = Uint64;
1705 fn next(&mut self) -> Option<Self::Item> {
1706 if self.1 >= self.2 {
1707 None
1708 } else {
1709 let ret = self.0.get_unchecked(self.1);
1710 self.1 += 1;
1711 Some(ret)
1712 }
1713 }
1714}
1715impl ::core::iter::ExactSizeIterator for Uint64VecIterator {
1716 fn len(&self) -> usize {
1717 self.2 - self.1
1718 }
1719}
1720impl ::core::iter::IntoIterator for Uint64Vec {
1721 type Item = Uint64;
1722 type IntoIter = Uint64VecIterator;
1723 fn into_iter(self) -> Self::IntoIter {
1724 let len = self.len();
1725 Uint64VecIterator(self, 0, len)
1726 }
1727}
1728impl<'r> Uint64VecReader<'r> {
1729 pub fn iter<'t>(&'t self) -> Uint64VecReaderIterator<'t, 'r> {
1730 Uint64VecReaderIterator(&self, 0, self.len())
1731 }
1732}
1733pub struct Uint64VecReaderIterator<'t, 'r>(&'t Uint64VecReader<'r>, usize, usize);
1734impl<'t: 'r, 'r> ::core::iter::Iterator for Uint64VecReaderIterator<'t, 'r> {
1735 type Item = Uint64Reader<'t>;
1736 fn next(&mut self) -> Option<Self::Item> {
1737 if self.1 >= self.2 {
1738 None
1739 } else {
1740 let ret = self.0.get_unchecked(self.1);
1741 self.1 += 1;
1742 Some(ret)
1743 }
1744 }
1745}
1746impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Uint64VecReaderIterator<'t, 'r> {
1747 fn len(&self) -> usize {
1748 self.2 - self.1
1749 }
1750}
1751impl<T> ::core::iter::FromIterator<T> for Uint64Vec
1752where
1753 T: Into<Uint64>,
1754{
1755 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
1756 Self::new_builder()
1757 .extend(iter.into_iter().map(Into::into))
1758 .build()
1759 }
1760}
1761impl<T> From<Vec<T>> for Uint64Vec
1762where
1763 T: Into<Uint64>,
1764{
1765 fn from(v: Vec<T>) -> Self {
1766 v.into_iter().collect()
1767 }
1768}
1769#[derive(Clone)]
1770pub struct Uint256Vec(molecule::bytes::Bytes);
1771impl ::core::fmt::LowerHex for Uint256Vec {
1772 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1773 use molecule::hex_string;
1774 if f.alternate() {
1775 write!(f, "0x")?;
1776 }
1777 write!(f, "{}", hex_string(self.as_slice()))
1778 }
1779}
1780impl ::core::fmt::Debug for Uint256Vec {
1781 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1782 write!(f, "{}({:#x})", Self::NAME, self)
1783 }
1784}
1785impl ::core::fmt::Display for Uint256Vec {
1786 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1787 write!(f, "{} [", Self::NAME)?;
1788 for i in 0..self.len() {
1789 if i == 0 {
1790 write!(f, "{}", self.get_unchecked(i))?;
1791 } else {
1792 write!(f, ", {}", self.get_unchecked(i))?;
1793 }
1794 }
1795 write!(f, "]")
1796 }
1797}
1798impl ::core::default::Default for Uint256Vec {
1799 fn default() -> Self {
1800 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1801 Uint256Vec::new_unchecked(v)
1802 }
1803}
1804impl Uint256Vec {
1805 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
1806 pub const ITEM_SIZE: usize = 32;
1807 pub fn total_size(&self) -> usize {
1808 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1809 }
1810 pub fn item_count(&self) -> usize {
1811 molecule::unpack_number(self.as_slice()) as usize
1812 }
1813 pub fn len(&self) -> usize {
1814 self.item_count()
1815 }
1816 pub fn is_empty(&self) -> bool {
1817 self.len() == 0
1818 }
1819 pub fn get(&self, idx: usize) -> Option<Uint256> {
1820 if idx >= self.len() {
1821 None
1822 } else {
1823 Some(self.get_unchecked(idx))
1824 }
1825 }
1826 pub fn get_unchecked(&self, idx: usize) -> Uint256 {
1827 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1828 let end = start + Self::ITEM_SIZE;
1829 Uint256::new_unchecked(self.0.slice(start..end))
1830 }
1831 pub fn as_reader<'r>(&'r self) -> Uint256VecReader<'r> {
1832 Uint256VecReader::new_unchecked(self.as_slice())
1833 }
1834}
1835impl molecule::prelude::Entity for Uint256Vec {
1836 type Builder = Uint256VecBuilder;
1837 const NAME: &'static str = "Uint256Vec";
1838 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1839 Uint256Vec(data)
1840 }
1841 fn as_bytes(&self) -> molecule::bytes::Bytes {
1842 self.0.clone()
1843 }
1844 fn as_slice(&self) -> &[u8] {
1845 &self.0[..]
1846 }
1847 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1848 Uint256VecReader::from_slice(slice).map(|reader| reader.to_entity())
1849 }
1850 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1851 Uint256VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1852 }
1853 fn new_builder() -> Self::Builder {
1854 ::core::default::Default::default()
1855 }
1856 fn as_builder(self) -> Self::Builder {
1857 Self::new_builder().extend(self.into_iter())
1858 }
1859}
1860#[derive(Clone, Copy)]
1861pub struct Uint256VecReader<'r>(&'r [u8]);
1862impl<'r> ::core::fmt::LowerHex for Uint256VecReader<'r> {
1863 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1864 use molecule::hex_string;
1865 if f.alternate() {
1866 write!(f, "0x")?;
1867 }
1868 write!(f, "{}", hex_string(self.as_slice()))
1869 }
1870}
1871impl<'r> ::core::fmt::Debug for Uint256VecReader<'r> {
1872 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1873 write!(f, "{}({:#x})", Self::NAME, self)
1874 }
1875}
1876impl<'r> ::core::fmt::Display for Uint256VecReader<'r> {
1877 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1878 write!(f, "{} [", Self::NAME)?;
1879 for i in 0..self.len() {
1880 if i == 0 {
1881 write!(f, "{}", self.get_unchecked(i))?;
1882 } else {
1883 write!(f, ", {}", self.get_unchecked(i))?;
1884 }
1885 }
1886 write!(f, "]")
1887 }
1888}
1889impl<'r> Uint256VecReader<'r> {
1890 pub const ITEM_SIZE: usize = 32;
1891 pub fn total_size(&self) -> usize {
1892 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1893 }
1894 pub fn item_count(&self) -> usize {
1895 molecule::unpack_number(self.as_slice()) as usize
1896 }
1897 pub fn len(&self) -> usize {
1898 self.item_count()
1899 }
1900 pub fn is_empty(&self) -> bool {
1901 self.len() == 0
1902 }
1903 pub fn get(&self, idx: usize) -> Option<Uint256Reader<'r>> {
1904 if idx >= self.len() {
1905 None
1906 } else {
1907 Some(self.get_unchecked(idx))
1908 }
1909 }
1910 pub fn get_unchecked(&self, idx: usize) -> Uint256Reader<'r> {
1911 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1912 let end = start + Self::ITEM_SIZE;
1913 Uint256Reader::new_unchecked(&self.as_slice()[start..end])
1914 }
1915}
1916impl<'r> molecule::prelude::Reader<'r> for Uint256VecReader<'r> {
1917 type Entity = Uint256Vec;
1918 const NAME: &'static str = "Uint256VecReader";
1919 fn to_entity(&self) -> Self::Entity {
1920 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1921 }
1922 fn new_unchecked(slice: &'r [u8]) -> Self {
1923 Uint256VecReader(slice)
1924 }
1925 fn as_slice(&self) -> &'r [u8] {
1926 self.0
1927 }
1928 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1929 use molecule::verification_error as ve;
1930 let slice_len = slice.len();
1931 if slice_len < molecule::NUMBER_SIZE {
1932 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
1933 }
1934 let item_count = molecule::unpack_number(slice) as usize;
1935 if item_count == 0 {
1936 if slice_len != molecule::NUMBER_SIZE {
1937 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
1938 }
1939 return Ok(());
1940 }
1941 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
1942 if slice_len != total_size {
1943 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
1944 }
1945 Ok(())
1946 }
1947}
1948#[derive(Clone, Debug, Default)]
1949pub struct Uint256VecBuilder(pub(crate) Vec<Uint256>);
1950impl Uint256VecBuilder {
1951 pub const ITEM_SIZE: usize = 32;
1952 pub fn set(mut self, v: Vec<Uint256>) -> Self {
1953 self.0 = v;
1954 self
1955 }
1956 pub fn push<T>(mut self, v: T) -> Self
1957 where
1958 T: ::core::convert::Into<Uint256>,
1959 {
1960 self.0.push(v.into());
1961 self
1962 }
1963 pub fn extend<T: ::core::iter::IntoIterator<Item = Uint256>>(mut self, iter: T) -> Self {
1964 self.0.extend(iter);
1965 self
1966 }
1967 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Uint256>
1968 where
1969 T: ::core::convert::Into<Uint256>,
1970 {
1971 self.0
1972 .get_mut(index)
1973 .map(|item| ::core::mem::replace(item, v.into()))
1974 }
1975}
1976impl molecule::prelude::Builder for Uint256VecBuilder {
1977 type Entity = Uint256Vec;
1978 const NAME: &'static str = "Uint256VecBuilder";
1979 fn expected_length(&self) -> usize {
1980 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
1981 }
1982 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1983 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
1984 for inner in &self.0[..] {
1985 writer.write_all(inner.as_slice())?;
1986 }
1987 Ok(())
1988 }
1989 fn build(&self) -> Self::Entity {
1990 let mut inner = Vec::with_capacity(self.expected_length());
1991 self.write(&mut inner)
1992 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1993 Uint256Vec::new_unchecked(inner.into())
1994 }
1995}
1996pub struct Uint256VecIterator(Uint256Vec, usize, usize);
1997impl ::core::iter::Iterator for Uint256VecIterator {
1998 type Item = Uint256;
1999 fn next(&mut self) -> Option<Self::Item> {
2000 if self.1 >= self.2 {
2001 None
2002 } else {
2003 let ret = self.0.get_unchecked(self.1);
2004 self.1 += 1;
2005 Some(ret)
2006 }
2007 }
2008}
2009impl ::core::iter::ExactSizeIterator for Uint256VecIterator {
2010 fn len(&self) -> usize {
2011 self.2 - self.1
2012 }
2013}
2014impl ::core::iter::IntoIterator for Uint256Vec {
2015 type Item = Uint256;
2016 type IntoIter = Uint256VecIterator;
2017 fn into_iter(self) -> Self::IntoIter {
2018 let len = self.len();
2019 Uint256VecIterator(self, 0, len)
2020 }
2021}
2022impl<'r> Uint256VecReader<'r> {
2023 pub fn iter<'t>(&'t self) -> Uint256VecReaderIterator<'t, 'r> {
2024 Uint256VecReaderIterator(&self, 0, self.len())
2025 }
2026}
2027pub struct Uint256VecReaderIterator<'t, 'r>(&'t Uint256VecReader<'r>, usize, usize);
2028impl<'t: 'r, 'r> ::core::iter::Iterator for Uint256VecReaderIterator<'t, 'r> {
2029 type Item = Uint256Reader<'t>;
2030 fn next(&mut self) -> Option<Self::Item> {
2031 if self.1 >= self.2 {
2032 None
2033 } else {
2034 let ret = self.0.get_unchecked(self.1);
2035 self.1 += 1;
2036 Some(ret)
2037 }
2038 }
2039}
2040impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Uint256VecReaderIterator<'t, 'r> {
2041 fn len(&self) -> usize {
2042 self.2 - self.1
2043 }
2044}
2045impl<T> ::core::iter::FromIterator<T> for Uint256Vec
2046where
2047 T: Into<Uint256>,
2048{
2049 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
2050 Self::new_builder()
2051 .extend(iter.into_iter().map(Into::into))
2052 .build()
2053 }
2054}
2055impl<T> From<Vec<T>> for Uint256Vec
2056where
2057 T: Into<Uint256>,
2058{
2059 fn from(v: Vec<T>) -> Self {
2060 v.into_iter().collect()
2061 }
2062}
2063#[derive(Clone)]
2064pub struct CellOutputOpt(molecule::bytes::Bytes);
2065impl ::core::fmt::LowerHex for CellOutputOpt {
2066 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2067 use molecule::hex_string;
2068 if f.alternate() {
2069 write!(f, "0x")?;
2070 }
2071 write!(f, "{}", hex_string(self.as_slice()))
2072 }
2073}
2074impl ::core::fmt::Debug for CellOutputOpt {
2075 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2076 write!(f, "{}({:#x})", Self::NAME, self)
2077 }
2078}
2079impl ::core::fmt::Display for CellOutputOpt {
2080 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2081 if let Some(v) = self.to_opt() {
2082 write!(f, "{}(Some({}))", Self::NAME, v)
2083 } else {
2084 write!(f, "{}(None)", Self::NAME)
2085 }
2086 }
2087}
2088impl ::core::default::Default for CellOutputOpt {
2089 fn default() -> Self {
2090 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2091 CellOutputOpt::new_unchecked(v)
2092 }
2093}
2094impl CellOutputOpt {
2095 const DEFAULT_VALUE: [u8; 0] = [];
2096 pub fn is_none(&self) -> bool {
2097 self.0.is_empty()
2098 }
2099 pub fn is_some(&self) -> bool {
2100 !self.0.is_empty()
2101 }
2102 pub fn to_opt(&self) -> Option<CellOutput> {
2103 if self.is_none() {
2104 None
2105 } else {
2106 Some(CellOutput::new_unchecked(self.0.clone()))
2107 }
2108 }
2109 pub fn as_reader<'r>(&'r self) -> CellOutputOptReader<'r> {
2110 CellOutputOptReader::new_unchecked(self.as_slice())
2111 }
2112}
2113impl molecule::prelude::Entity for CellOutputOpt {
2114 type Builder = CellOutputOptBuilder;
2115 const NAME: &'static str = "CellOutputOpt";
2116 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2117 CellOutputOpt(data)
2118 }
2119 fn as_bytes(&self) -> molecule::bytes::Bytes {
2120 self.0.clone()
2121 }
2122 fn as_slice(&self) -> &[u8] {
2123 &self.0[..]
2124 }
2125 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2126 CellOutputOptReader::from_slice(slice).map(|reader| reader.to_entity())
2127 }
2128 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2129 CellOutputOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2130 }
2131 fn new_builder() -> Self::Builder {
2132 ::core::default::Default::default()
2133 }
2134 fn as_builder(self) -> Self::Builder {
2135 Self::new_builder().set(self.to_opt())
2136 }
2137}
2138#[derive(Clone, Copy)]
2139pub struct CellOutputOptReader<'r>(&'r [u8]);
2140impl<'r> ::core::fmt::LowerHex for CellOutputOptReader<'r> {
2141 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2142 use molecule::hex_string;
2143 if f.alternate() {
2144 write!(f, "0x")?;
2145 }
2146 write!(f, "{}", hex_string(self.as_slice()))
2147 }
2148}
2149impl<'r> ::core::fmt::Debug for CellOutputOptReader<'r> {
2150 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2151 write!(f, "{}({:#x})", Self::NAME, self)
2152 }
2153}
2154impl<'r> ::core::fmt::Display for CellOutputOptReader<'r> {
2155 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2156 if let Some(v) = self.to_opt() {
2157 write!(f, "{}(Some({}))", Self::NAME, v)
2158 } else {
2159 write!(f, "{}(None)", Self::NAME)
2160 }
2161 }
2162}
2163impl<'r> CellOutputOptReader<'r> {
2164 pub fn is_none(&self) -> bool {
2165 self.0.is_empty()
2166 }
2167 pub fn is_some(&self) -> bool {
2168 !self.0.is_empty()
2169 }
2170 pub fn to_opt(&self) -> Option<CellOutputReader<'r>> {
2171 if self.is_none() {
2172 None
2173 } else {
2174 Some(CellOutputReader::new_unchecked(self.as_slice()))
2175 }
2176 }
2177}
2178impl<'r> molecule::prelude::Reader<'r> for CellOutputOptReader<'r> {
2179 type Entity = CellOutputOpt;
2180 const NAME: &'static str = "CellOutputOptReader";
2181 fn to_entity(&self) -> Self::Entity {
2182 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2183 }
2184 fn new_unchecked(slice: &'r [u8]) -> Self {
2185 CellOutputOptReader(slice)
2186 }
2187 fn as_slice(&self) -> &'r [u8] {
2188 self.0
2189 }
2190 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2191 if !slice.is_empty() {
2192 CellOutputReader::verify(&slice[..], compatible)?;
2193 }
2194 Ok(())
2195 }
2196}
2197#[derive(Clone, Debug, Default)]
2198pub struct CellOutputOptBuilder(pub(crate) Option<CellOutput>);
2199impl CellOutputOptBuilder {
2200 pub fn set<T>(mut self, v: T) -> Self
2201 where
2202 T: ::core::convert::Into<Option<CellOutput>>,
2203 {
2204 self.0 = v.into();
2205 self
2206 }
2207}
2208impl molecule::prelude::Builder for CellOutputOptBuilder {
2209 type Entity = CellOutputOpt;
2210 const NAME: &'static str = "CellOutputOptBuilder";
2211 fn expected_length(&self) -> usize {
2212 self.0
2213 .as_ref()
2214 .map(|ref inner| inner.as_slice().len())
2215 .unwrap_or(0)
2216 }
2217 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2218 self.0
2219 .as_ref()
2220 .map(|ref inner| writer.write_all(inner.as_slice()))
2221 .unwrap_or(Ok(()))
2222 }
2223 fn build(&self) -> Self::Entity {
2224 let mut inner = Vec::with_capacity(self.expected_length());
2225 self.write(&mut inner)
2226 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2227 CellOutputOpt::new_unchecked(inner.into())
2228 }
2229}
2230impl From<CellOutput> for CellOutputOpt {
2231 fn from(value: CellOutput) -> Self {
2232 Self::new_builder().set(Some(value)).build()
2233 }
2234}
2235#[derive(Clone)]
2236pub struct HeaderVec(molecule::bytes::Bytes);
2237impl ::core::fmt::LowerHex for HeaderVec {
2238 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2239 use molecule::hex_string;
2240 if f.alternate() {
2241 write!(f, "0x")?;
2242 }
2243 write!(f, "{}", hex_string(self.as_slice()))
2244 }
2245}
2246impl ::core::fmt::Debug for HeaderVec {
2247 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2248 write!(f, "{}({:#x})", Self::NAME, self)
2249 }
2250}
2251impl ::core::fmt::Display for HeaderVec {
2252 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2253 write!(f, "{} [", Self::NAME)?;
2254 for i in 0..self.len() {
2255 if i == 0 {
2256 write!(f, "{}", self.get_unchecked(i))?;
2257 } else {
2258 write!(f, ", {}", self.get_unchecked(i))?;
2259 }
2260 }
2261 write!(f, "]")
2262 }
2263}
2264impl ::core::default::Default for HeaderVec {
2265 fn default() -> Self {
2266 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2267 HeaderVec::new_unchecked(v)
2268 }
2269}
2270impl HeaderVec {
2271 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
2272 pub const ITEM_SIZE: usize = 208;
2273 pub fn total_size(&self) -> usize {
2274 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
2275 }
2276 pub fn item_count(&self) -> usize {
2277 molecule::unpack_number(self.as_slice()) as usize
2278 }
2279 pub fn len(&self) -> usize {
2280 self.item_count()
2281 }
2282 pub fn is_empty(&self) -> bool {
2283 self.len() == 0
2284 }
2285 pub fn get(&self, idx: usize) -> Option<Header> {
2286 if idx >= self.len() {
2287 None
2288 } else {
2289 Some(self.get_unchecked(idx))
2290 }
2291 }
2292 pub fn get_unchecked(&self, idx: usize) -> Header {
2293 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2294 let end = start + Self::ITEM_SIZE;
2295 Header::new_unchecked(self.0.slice(start..end))
2296 }
2297 pub fn as_reader<'r>(&'r self) -> HeaderVecReader<'r> {
2298 HeaderVecReader::new_unchecked(self.as_slice())
2299 }
2300}
2301impl molecule::prelude::Entity for HeaderVec {
2302 type Builder = HeaderVecBuilder;
2303 const NAME: &'static str = "HeaderVec";
2304 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2305 HeaderVec(data)
2306 }
2307 fn as_bytes(&self) -> molecule::bytes::Bytes {
2308 self.0.clone()
2309 }
2310 fn as_slice(&self) -> &[u8] {
2311 &self.0[..]
2312 }
2313 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2314 HeaderVecReader::from_slice(slice).map(|reader| reader.to_entity())
2315 }
2316 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2317 HeaderVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2318 }
2319 fn new_builder() -> Self::Builder {
2320 ::core::default::Default::default()
2321 }
2322 fn as_builder(self) -> Self::Builder {
2323 Self::new_builder().extend(self.into_iter())
2324 }
2325}
2326#[derive(Clone, Copy)]
2327pub struct HeaderVecReader<'r>(&'r [u8]);
2328impl<'r> ::core::fmt::LowerHex for HeaderVecReader<'r> {
2329 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2330 use molecule::hex_string;
2331 if f.alternate() {
2332 write!(f, "0x")?;
2333 }
2334 write!(f, "{}", hex_string(self.as_slice()))
2335 }
2336}
2337impl<'r> ::core::fmt::Debug for HeaderVecReader<'r> {
2338 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2339 write!(f, "{}({:#x})", Self::NAME, self)
2340 }
2341}
2342impl<'r> ::core::fmt::Display for HeaderVecReader<'r> {
2343 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2344 write!(f, "{} [", Self::NAME)?;
2345 for i in 0..self.len() {
2346 if i == 0 {
2347 write!(f, "{}", self.get_unchecked(i))?;
2348 } else {
2349 write!(f, ", {}", self.get_unchecked(i))?;
2350 }
2351 }
2352 write!(f, "]")
2353 }
2354}
2355impl<'r> HeaderVecReader<'r> {
2356 pub const ITEM_SIZE: usize = 208;
2357 pub fn total_size(&self) -> usize {
2358 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
2359 }
2360 pub fn item_count(&self) -> usize {
2361 molecule::unpack_number(self.as_slice()) as usize
2362 }
2363 pub fn len(&self) -> usize {
2364 self.item_count()
2365 }
2366 pub fn is_empty(&self) -> bool {
2367 self.len() == 0
2368 }
2369 pub fn get(&self, idx: usize) -> Option<HeaderReader<'r>> {
2370 if idx >= self.len() {
2371 None
2372 } else {
2373 Some(self.get_unchecked(idx))
2374 }
2375 }
2376 pub fn get_unchecked(&self, idx: usize) -> HeaderReader<'r> {
2377 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2378 let end = start + Self::ITEM_SIZE;
2379 HeaderReader::new_unchecked(&self.as_slice()[start..end])
2380 }
2381}
2382impl<'r> molecule::prelude::Reader<'r> for HeaderVecReader<'r> {
2383 type Entity = HeaderVec;
2384 const NAME: &'static str = "HeaderVecReader";
2385 fn to_entity(&self) -> Self::Entity {
2386 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2387 }
2388 fn new_unchecked(slice: &'r [u8]) -> Self {
2389 HeaderVecReader(slice)
2390 }
2391 fn as_slice(&self) -> &'r [u8] {
2392 self.0
2393 }
2394 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
2395 use molecule::verification_error as ve;
2396 let slice_len = slice.len();
2397 if slice_len < molecule::NUMBER_SIZE {
2398 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2399 }
2400 let item_count = molecule::unpack_number(slice) as usize;
2401 if item_count == 0 {
2402 if slice_len != molecule::NUMBER_SIZE {
2403 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
2404 }
2405 return Ok(());
2406 }
2407 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
2408 if slice_len != total_size {
2409 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2410 }
2411 Ok(())
2412 }
2413}
2414#[derive(Clone, Debug, Default)]
2415pub struct HeaderVecBuilder(pub(crate) Vec<Header>);
2416impl HeaderVecBuilder {
2417 pub const ITEM_SIZE: usize = 208;
2418 pub fn set(mut self, v: Vec<Header>) -> Self {
2419 self.0 = v;
2420 self
2421 }
2422 pub fn push<T>(mut self, v: T) -> Self
2423 where
2424 T: ::core::convert::Into<Header>,
2425 {
2426 self.0.push(v.into());
2427 self
2428 }
2429 pub fn extend<T: ::core::iter::IntoIterator<Item = Header>>(mut self, iter: T) -> Self {
2430 self.0.extend(iter);
2431 self
2432 }
2433 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Header>
2434 where
2435 T: ::core::convert::Into<Header>,
2436 {
2437 self.0
2438 .get_mut(index)
2439 .map(|item| ::core::mem::replace(item, v.into()))
2440 }
2441}
2442impl molecule::prelude::Builder for HeaderVecBuilder {
2443 type Entity = HeaderVec;
2444 const NAME: &'static str = "HeaderVecBuilder";
2445 fn expected_length(&self) -> usize {
2446 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
2447 }
2448 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2449 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
2450 for inner in &self.0[..] {
2451 writer.write_all(inner.as_slice())?;
2452 }
2453 Ok(())
2454 }
2455 fn build(&self) -> Self::Entity {
2456 let mut inner = Vec::with_capacity(self.expected_length());
2457 self.write(&mut inner)
2458 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2459 HeaderVec::new_unchecked(inner.into())
2460 }
2461}
2462pub struct HeaderVecIterator(HeaderVec, usize, usize);
2463impl ::core::iter::Iterator for HeaderVecIterator {
2464 type Item = Header;
2465 fn next(&mut self) -> Option<Self::Item> {
2466 if self.1 >= self.2 {
2467 None
2468 } else {
2469 let ret = self.0.get_unchecked(self.1);
2470 self.1 += 1;
2471 Some(ret)
2472 }
2473 }
2474}
2475impl ::core::iter::ExactSizeIterator for HeaderVecIterator {
2476 fn len(&self) -> usize {
2477 self.2 - self.1
2478 }
2479}
2480impl ::core::iter::IntoIterator for HeaderVec {
2481 type Item = Header;
2482 type IntoIter = HeaderVecIterator;
2483 fn into_iter(self) -> Self::IntoIter {
2484 let len = self.len();
2485 HeaderVecIterator(self, 0, len)
2486 }
2487}
2488impl<'r> HeaderVecReader<'r> {
2489 pub fn iter<'t>(&'t self) -> HeaderVecReaderIterator<'t, 'r> {
2490 HeaderVecReaderIterator(&self, 0, self.len())
2491 }
2492}
2493pub struct HeaderVecReaderIterator<'t, 'r>(&'t HeaderVecReader<'r>, usize, usize);
2494impl<'t: 'r, 'r> ::core::iter::Iterator for HeaderVecReaderIterator<'t, 'r> {
2495 type Item = HeaderReader<'t>;
2496 fn next(&mut self) -> Option<Self::Item> {
2497 if self.1 >= self.2 {
2498 None
2499 } else {
2500 let ret = self.0.get_unchecked(self.1);
2501 self.1 += 1;
2502 Some(ret)
2503 }
2504 }
2505}
2506impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for HeaderVecReaderIterator<'t, 'r> {
2507 fn len(&self) -> usize {
2508 self.2 - self.1
2509 }
2510}
2511impl<T> ::core::iter::FromIterator<T> for HeaderVec
2512where
2513 T: Into<Header>,
2514{
2515 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
2516 Self::new_builder()
2517 .extend(iter.into_iter().map(Into::into))
2518 .build()
2519 }
2520}
2521impl<T> From<Vec<T>> for HeaderVec
2522where
2523 T: Into<Header>,
2524{
2525 fn from(v: Vec<T>) -> Self {
2526 v.into_iter().collect()
2527 }
2528}
2529#[derive(Clone)]
2530pub struct OutPointVec(molecule::bytes::Bytes);
2531impl ::core::fmt::LowerHex for OutPointVec {
2532 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2533 use molecule::hex_string;
2534 if f.alternate() {
2535 write!(f, "0x")?;
2536 }
2537 write!(f, "{}", hex_string(self.as_slice()))
2538 }
2539}
2540impl ::core::fmt::Debug for OutPointVec {
2541 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2542 write!(f, "{}({:#x})", Self::NAME, self)
2543 }
2544}
2545impl ::core::fmt::Display for OutPointVec {
2546 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2547 write!(f, "{} [", Self::NAME)?;
2548 for i in 0..self.len() {
2549 if i == 0 {
2550 write!(f, "{}", self.get_unchecked(i))?;
2551 } else {
2552 write!(f, ", {}", self.get_unchecked(i))?;
2553 }
2554 }
2555 write!(f, "]")
2556 }
2557}
2558impl ::core::default::Default for OutPointVec {
2559 fn default() -> Self {
2560 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2561 OutPointVec::new_unchecked(v)
2562 }
2563}
2564impl OutPointVec {
2565 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
2566 pub const ITEM_SIZE: usize = 36;
2567 pub fn total_size(&self) -> usize {
2568 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
2569 }
2570 pub fn item_count(&self) -> usize {
2571 molecule::unpack_number(self.as_slice()) as usize
2572 }
2573 pub fn len(&self) -> usize {
2574 self.item_count()
2575 }
2576 pub fn is_empty(&self) -> bool {
2577 self.len() == 0
2578 }
2579 pub fn get(&self, idx: usize) -> Option<OutPoint> {
2580 if idx >= self.len() {
2581 None
2582 } else {
2583 Some(self.get_unchecked(idx))
2584 }
2585 }
2586 pub fn get_unchecked(&self, idx: usize) -> OutPoint {
2587 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2588 let end = start + Self::ITEM_SIZE;
2589 OutPoint::new_unchecked(self.0.slice(start..end))
2590 }
2591 pub fn as_reader<'r>(&'r self) -> OutPointVecReader<'r> {
2592 OutPointVecReader::new_unchecked(self.as_slice())
2593 }
2594}
2595impl molecule::prelude::Entity for OutPointVec {
2596 type Builder = OutPointVecBuilder;
2597 const NAME: &'static str = "OutPointVec";
2598 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2599 OutPointVec(data)
2600 }
2601 fn as_bytes(&self) -> molecule::bytes::Bytes {
2602 self.0.clone()
2603 }
2604 fn as_slice(&self) -> &[u8] {
2605 &self.0[..]
2606 }
2607 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2608 OutPointVecReader::from_slice(slice).map(|reader| reader.to_entity())
2609 }
2610 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2611 OutPointVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2612 }
2613 fn new_builder() -> Self::Builder {
2614 ::core::default::Default::default()
2615 }
2616 fn as_builder(self) -> Self::Builder {
2617 Self::new_builder().extend(self.into_iter())
2618 }
2619}
2620#[derive(Clone, Copy)]
2621pub struct OutPointVecReader<'r>(&'r [u8]);
2622impl<'r> ::core::fmt::LowerHex for OutPointVecReader<'r> {
2623 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2624 use molecule::hex_string;
2625 if f.alternate() {
2626 write!(f, "0x")?;
2627 }
2628 write!(f, "{}", hex_string(self.as_slice()))
2629 }
2630}
2631impl<'r> ::core::fmt::Debug for OutPointVecReader<'r> {
2632 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2633 write!(f, "{}({:#x})", Self::NAME, self)
2634 }
2635}
2636impl<'r> ::core::fmt::Display for OutPointVecReader<'r> {
2637 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2638 write!(f, "{} [", Self::NAME)?;
2639 for i in 0..self.len() {
2640 if i == 0 {
2641 write!(f, "{}", self.get_unchecked(i))?;
2642 } else {
2643 write!(f, ", {}", self.get_unchecked(i))?;
2644 }
2645 }
2646 write!(f, "]")
2647 }
2648}
2649impl<'r> OutPointVecReader<'r> {
2650 pub const ITEM_SIZE: usize = 36;
2651 pub fn total_size(&self) -> usize {
2652 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
2653 }
2654 pub fn item_count(&self) -> usize {
2655 molecule::unpack_number(self.as_slice()) as usize
2656 }
2657 pub fn len(&self) -> usize {
2658 self.item_count()
2659 }
2660 pub fn is_empty(&self) -> bool {
2661 self.len() == 0
2662 }
2663 pub fn get(&self, idx: usize) -> Option<OutPointReader<'r>> {
2664 if idx >= self.len() {
2665 None
2666 } else {
2667 Some(self.get_unchecked(idx))
2668 }
2669 }
2670 pub fn get_unchecked(&self, idx: usize) -> OutPointReader<'r> {
2671 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2672 let end = start + Self::ITEM_SIZE;
2673 OutPointReader::new_unchecked(&self.as_slice()[start..end])
2674 }
2675}
2676impl<'r> molecule::prelude::Reader<'r> for OutPointVecReader<'r> {
2677 type Entity = OutPointVec;
2678 const NAME: &'static str = "OutPointVecReader";
2679 fn to_entity(&self) -> Self::Entity {
2680 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2681 }
2682 fn new_unchecked(slice: &'r [u8]) -> Self {
2683 OutPointVecReader(slice)
2684 }
2685 fn as_slice(&self) -> &'r [u8] {
2686 self.0
2687 }
2688 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
2689 use molecule::verification_error as ve;
2690 let slice_len = slice.len();
2691 if slice_len < molecule::NUMBER_SIZE {
2692 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2693 }
2694 let item_count = molecule::unpack_number(slice) as usize;
2695 if item_count == 0 {
2696 if slice_len != molecule::NUMBER_SIZE {
2697 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
2698 }
2699 return Ok(());
2700 }
2701 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
2702 if slice_len != total_size {
2703 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2704 }
2705 Ok(())
2706 }
2707}
2708#[derive(Clone, Debug, Default)]
2709pub struct OutPointVecBuilder(pub(crate) Vec<OutPoint>);
2710impl OutPointVecBuilder {
2711 pub const ITEM_SIZE: usize = 36;
2712 pub fn set(mut self, v: Vec<OutPoint>) -> Self {
2713 self.0 = v;
2714 self
2715 }
2716 pub fn push<T>(mut self, v: T) -> Self
2717 where
2718 T: ::core::convert::Into<OutPoint>,
2719 {
2720 self.0.push(v.into());
2721 self
2722 }
2723 pub fn extend<T: ::core::iter::IntoIterator<Item = OutPoint>>(mut self, iter: T) -> Self {
2724 self.0.extend(iter);
2725 self
2726 }
2727 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<OutPoint>
2728 where
2729 T: ::core::convert::Into<OutPoint>,
2730 {
2731 self.0
2732 .get_mut(index)
2733 .map(|item| ::core::mem::replace(item, v.into()))
2734 }
2735}
2736impl molecule::prelude::Builder for OutPointVecBuilder {
2737 type Entity = OutPointVec;
2738 const NAME: &'static str = "OutPointVecBuilder";
2739 fn expected_length(&self) -> usize {
2740 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
2741 }
2742 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2743 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
2744 for inner in &self.0[..] {
2745 writer.write_all(inner.as_slice())?;
2746 }
2747 Ok(())
2748 }
2749 fn build(&self) -> Self::Entity {
2750 let mut inner = Vec::with_capacity(self.expected_length());
2751 self.write(&mut inner)
2752 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2753 OutPointVec::new_unchecked(inner.into())
2754 }
2755}
2756pub struct OutPointVecIterator(OutPointVec, usize, usize);
2757impl ::core::iter::Iterator for OutPointVecIterator {
2758 type Item = OutPoint;
2759 fn next(&mut self) -> Option<Self::Item> {
2760 if self.1 >= self.2 {
2761 None
2762 } else {
2763 let ret = self.0.get_unchecked(self.1);
2764 self.1 += 1;
2765 Some(ret)
2766 }
2767 }
2768}
2769impl ::core::iter::ExactSizeIterator for OutPointVecIterator {
2770 fn len(&self) -> usize {
2771 self.2 - self.1
2772 }
2773}
2774impl ::core::iter::IntoIterator for OutPointVec {
2775 type Item = OutPoint;
2776 type IntoIter = OutPointVecIterator;
2777 fn into_iter(self) -> Self::IntoIter {
2778 let len = self.len();
2779 OutPointVecIterator(self, 0, len)
2780 }
2781}
2782impl<'r> OutPointVecReader<'r> {
2783 pub fn iter<'t>(&'t self) -> OutPointVecReaderIterator<'t, 'r> {
2784 OutPointVecReaderIterator(&self, 0, self.len())
2785 }
2786}
2787pub struct OutPointVecReaderIterator<'t, 'r>(&'t OutPointVecReader<'r>, usize, usize);
2788impl<'t: 'r, 'r> ::core::iter::Iterator for OutPointVecReaderIterator<'t, 'r> {
2789 type Item = OutPointReader<'t>;
2790 fn next(&mut self) -> Option<Self::Item> {
2791 if self.1 >= self.2 {
2792 None
2793 } else {
2794 let ret = self.0.get_unchecked(self.1);
2795 self.1 += 1;
2796 Some(ret)
2797 }
2798 }
2799}
2800impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for OutPointVecReaderIterator<'t, 'r> {
2801 fn len(&self) -> usize {
2802 self.2 - self.1
2803 }
2804}
2805impl<T> ::core::iter::FromIterator<T> for OutPointVec
2806where
2807 T: Into<OutPoint>,
2808{
2809 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
2810 Self::new_builder()
2811 .extend(iter.into_iter().map(Into::into))
2812 .build()
2813 }
2814}
2815impl<T> From<Vec<T>> for OutPointVec
2816where
2817 T: Into<OutPoint>,
2818{
2819 fn from(v: Vec<T>) -> Self {
2820 v.into_iter().collect()
2821 }
2822}
2823#[derive(Clone)]
2824pub struct Uint64VecOpt(molecule::bytes::Bytes);
2825impl ::core::fmt::LowerHex for Uint64VecOpt {
2826 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2827 use molecule::hex_string;
2828 if f.alternate() {
2829 write!(f, "0x")?;
2830 }
2831 write!(f, "{}", hex_string(self.as_slice()))
2832 }
2833}
2834impl ::core::fmt::Debug for Uint64VecOpt {
2835 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2836 write!(f, "{}({:#x})", Self::NAME, self)
2837 }
2838}
2839impl ::core::fmt::Display for Uint64VecOpt {
2840 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2841 if let Some(v) = self.to_opt() {
2842 write!(f, "{}(Some({}))", Self::NAME, v)
2843 } else {
2844 write!(f, "{}(None)", Self::NAME)
2845 }
2846 }
2847}
2848impl ::core::default::Default for Uint64VecOpt {
2849 fn default() -> Self {
2850 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2851 Uint64VecOpt::new_unchecked(v)
2852 }
2853}
2854impl Uint64VecOpt {
2855 const DEFAULT_VALUE: [u8; 0] = [];
2856 pub fn is_none(&self) -> bool {
2857 self.0.is_empty()
2858 }
2859 pub fn is_some(&self) -> bool {
2860 !self.0.is_empty()
2861 }
2862 pub fn to_opt(&self) -> Option<Uint64Vec> {
2863 if self.is_none() {
2864 None
2865 } else {
2866 Some(Uint64Vec::new_unchecked(self.0.clone()))
2867 }
2868 }
2869 pub fn as_reader<'r>(&'r self) -> Uint64VecOptReader<'r> {
2870 Uint64VecOptReader::new_unchecked(self.as_slice())
2871 }
2872}
2873impl molecule::prelude::Entity for Uint64VecOpt {
2874 type Builder = Uint64VecOptBuilder;
2875 const NAME: &'static str = "Uint64VecOpt";
2876 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2877 Uint64VecOpt(data)
2878 }
2879 fn as_bytes(&self) -> molecule::bytes::Bytes {
2880 self.0.clone()
2881 }
2882 fn as_slice(&self) -> &[u8] {
2883 &self.0[..]
2884 }
2885 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2886 Uint64VecOptReader::from_slice(slice).map(|reader| reader.to_entity())
2887 }
2888 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2889 Uint64VecOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2890 }
2891 fn new_builder() -> Self::Builder {
2892 ::core::default::Default::default()
2893 }
2894 fn as_builder(self) -> Self::Builder {
2895 Self::new_builder().set(self.to_opt())
2896 }
2897}
2898#[derive(Clone, Copy)]
2899pub struct Uint64VecOptReader<'r>(&'r [u8]);
2900impl<'r> ::core::fmt::LowerHex for Uint64VecOptReader<'r> {
2901 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2902 use molecule::hex_string;
2903 if f.alternate() {
2904 write!(f, "0x")?;
2905 }
2906 write!(f, "{}", hex_string(self.as_slice()))
2907 }
2908}
2909impl<'r> ::core::fmt::Debug for Uint64VecOptReader<'r> {
2910 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2911 write!(f, "{}({:#x})", Self::NAME, self)
2912 }
2913}
2914impl<'r> ::core::fmt::Display for Uint64VecOptReader<'r> {
2915 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2916 if let Some(v) = self.to_opt() {
2917 write!(f, "{}(Some({}))", Self::NAME, v)
2918 } else {
2919 write!(f, "{}(None)", Self::NAME)
2920 }
2921 }
2922}
2923impl<'r> Uint64VecOptReader<'r> {
2924 pub fn is_none(&self) -> bool {
2925 self.0.is_empty()
2926 }
2927 pub fn is_some(&self) -> bool {
2928 !self.0.is_empty()
2929 }
2930 pub fn to_opt(&self) -> Option<Uint64VecReader<'r>> {
2931 if self.is_none() {
2932 None
2933 } else {
2934 Some(Uint64VecReader::new_unchecked(self.as_slice()))
2935 }
2936 }
2937}
2938impl<'r> molecule::prelude::Reader<'r> for Uint64VecOptReader<'r> {
2939 type Entity = Uint64VecOpt;
2940 const NAME: &'static str = "Uint64VecOptReader";
2941 fn to_entity(&self) -> Self::Entity {
2942 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2943 }
2944 fn new_unchecked(slice: &'r [u8]) -> Self {
2945 Uint64VecOptReader(slice)
2946 }
2947 fn as_slice(&self) -> &'r [u8] {
2948 self.0
2949 }
2950 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2951 if !slice.is_empty() {
2952 Uint64VecReader::verify(&slice[..], compatible)?;
2953 }
2954 Ok(())
2955 }
2956}
2957#[derive(Clone, Debug, Default)]
2958pub struct Uint64VecOptBuilder(pub(crate) Option<Uint64Vec>);
2959impl Uint64VecOptBuilder {
2960 pub fn set<T>(mut self, v: T) -> Self
2961 where
2962 T: ::core::convert::Into<Option<Uint64Vec>>,
2963 {
2964 self.0 = v.into();
2965 self
2966 }
2967}
2968impl molecule::prelude::Builder for Uint64VecOptBuilder {
2969 type Entity = Uint64VecOpt;
2970 const NAME: &'static str = "Uint64VecOptBuilder";
2971 fn expected_length(&self) -> usize {
2972 self.0
2973 .as_ref()
2974 .map(|ref inner| inner.as_slice().len())
2975 .unwrap_or(0)
2976 }
2977 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2978 self.0
2979 .as_ref()
2980 .map(|ref inner| writer.write_all(inner.as_slice()))
2981 .unwrap_or(Ok(()))
2982 }
2983 fn build(&self) -> Self::Entity {
2984 let mut inner = Vec::with_capacity(self.expected_length());
2985 self.write(&mut inner)
2986 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2987 Uint64VecOpt::new_unchecked(inner.into())
2988 }
2989}
2990impl From<Uint64Vec> for Uint64VecOpt {
2991 fn from(value: Uint64Vec) -> Self {
2992 Self::new_builder().set(Some(value)).build()
2993 }
2994}
2995#[derive(Clone)]
2996pub struct HeaderDigest(molecule::bytes::Bytes);
2997impl ::core::fmt::LowerHex for HeaderDigest {
2998 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2999 use molecule::hex_string;
3000 if f.alternate() {
3001 write!(f, "0x")?;
3002 }
3003 write!(f, "{}", hex_string(self.as_slice()))
3004 }
3005}
3006impl ::core::fmt::Debug for HeaderDigest {
3007 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3008 write!(f, "{}({:#x})", Self::NAME, self)
3009 }
3010}
3011impl ::core::fmt::Display for HeaderDigest {
3012 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3013 write!(f, "{} {{ ", Self::NAME)?;
3014 write!(f, "{}: {}", "children_hash", self.children_hash())?;
3015 write!(f, ", {}: {}", "total_difficulty", self.total_difficulty())?;
3016 write!(f, ", {}: {}", "start_number", self.start_number())?;
3017 write!(f, ", {}: {}", "end_number", self.end_number())?;
3018 write!(f, ", {}: {}", "start_epoch", self.start_epoch())?;
3019 write!(f, ", {}: {}", "end_epoch", self.end_epoch())?;
3020 write!(f, ", {}: {}", "start_timestamp", self.start_timestamp())?;
3021 write!(f, ", {}: {}", "end_timestamp", self.end_timestamp())?;
3022 write!(
3023 f,
3024 ", {}: {}",
3025 "start_compact_target",
3026 self.start_compact_target()
3027 )?;
3028 write!(
3029 f,
3030 ", {}: {}",
3031 "end_compact_target",
3032 self.end_compact_target()
3033 )?;
3034 write!(f, " }}")
3035 }
3036}
3037impl ::core::default::Default for HeaderDigest {
3038 fn default() -> Self {
3039 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3040 HeaderDigest::new_unchecked(v)
3041 }
3042}
3043impl HeaderDigest {
3044 const DEFAULT_VALUE: [u8; 120] = [
3045 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3046 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3047 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3048 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3049 ];
3050 pub const TOTAL_SIZE: usize = 120;
3051 pub const FIELD_SIZES: [usize; 10] = [32, 32, 8, 8, 8, 8, 8, 8, 4, 4];
3052 pub const FIELD_COUNT: usize = 10;
3053 pub fn children_hash(&self) -> Byte32 {
3054 Byte32::new_unchecked(self.0.slice(0..32))
3055 }
3056 pub fn total_difficulty(&self) -> Uint256 {
3057 Uint256::new_unchecked(self.0.slice(32..64))
3058 }
3059 pub fn start_number(&self) -> Uint64 {
3060 Uint64::new_unchecked(self.0.slice(64..72))
3061 }
3062 pub fn end_number(&self) -> Uint64 {
3063 Uint64::new_unchecked(self.0.slice(72..80))
3064 }
3065 pub fn start_epoch(&self) -> Uint64 {
3066 Uint64::new_unchecked(self.0.slice(80..88))
3067 }
3068 pub fn end_epoch(&self) -> Uint64 {
3069 Uint64::new_unchecked(self.0.slice(88..96))
3070 }
3071 pub fn start_timestamp(&self) -> Uint64 {
3072 Uint64::new_unchecked(self.0.slice(96..104))
3073 }
3074 pub fn end_timestamp(&self) -> Uint64 {
3075 Uint64::new_unchecked(self.0.slice(104..112))
3076 }
3077 pub fn start_compact_target(&self) -> Uint32 {
3078 Uint32::new_unchecked(self.0.slice(112..116))
3079 }
3080 pub fn end_compact_target(&self) -> Uint32 {
3081 Uint32::new_unchecked(self.0.slice(116..120))
3082 }
3083 pub fn as_reader<'r>(&'r self) -> HeaderDigestReader<'r> {
3084 HeaderDigestReader::new_unchecked(self.as_slice())
3085 }
3086}
3087impl molecule::prelude::Entity for HeaderDigest {
3088 type Builder = HeaderDigestBuilder;
3089 const NAME: &'static str = "HeaderDigest";
3090 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3091 HeaderDigest(data)
3092 }
3093 fn as_bytes(&self) -> molecule::bytes::Bytes {
3094 self.0.clone()
3095 }
3096 fn as_slice(&self) -> &[u8] {
3097 &self.0[..]
3098 }
3099 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3100 HeaderDigestReader::from_slice(slice).map(|reader| reader.to_entity())
3101 }
3102 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3103 HeaderDigestReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3104 }
3105 fn new_builder() -> Self::Builder {
3106 ::core::default::Default::default()
3107 }
3108 fn as_builder(self) -> Self::Builder {
3109 Self::new_builder()
3110 .children_hash(self.children_hash())
3111 .total_difficulty(self.total_difficulty())
3112 .start_number(self.start_number())
3113 .end_number(self.end_number())
3114 .start_epoch(self.start_epoch())
3115 .end_epoch(self.end_epoch())
3116 .start_timestamp(self.start_timestamp())
3117 .end_timestamp(self.end_timestamp())
3118 .start_compact_target(self.start_compact_target())
3119 .end_compact_target(self.end_compact_target())
3120 }
3121}
3122#[derive(Clone, Copy)]
3123pub struct HeaderDigestReader<'r>(&'r [u8]);
3124impl<'r> ::core::fmt::LowerHex for HeaderDigestReader<'r> {
3125 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3126 use molecule::hex_string;
3127 if f.alternate() {
3128 write!(f, "0x")?;
3129 }
3130 write!(f, "{}", hex_string(self.as_slice()))
3131 }
3132}
3133impl<'r> ::core::fmt::Debug for HeaderDigestReader<'r> {
3134 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3135 write!(f, "{}({:#x})", Self::NAME, self)
3136 }
3137}
3138impl<'r> ::core::fmt::Display for HeaderDigestReader<'r> {
3139 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3140 write!(f, "{} {{ ", Self::NAME)?;
3141 write!(f, "{}: {}", "children_hash", self.children_hash())?;
3142 write!(f, ", {}: {}", "total_difficulty", self.total_difficulty())?;
3143 write!(f, ", {}: {}", "start_number", self.start_number())?;
3144 write!(f, ", {}: {}", "end_number", self.end_number())?;
3145 write!(f, ", {}: {}", "start_epoch", self.start_epoch())?;
3146 write!(f, ", {}: {}", "end_epoch", self.end_epoch())?;
3147 write!(f, ", {}: {}", "start_timestamp", self.start_timestamp())?;
3148 write!(f, ", {}: {}", "end_timestamp", self.end_timestamp())?;
3149 write!(
3150 f,
3151 ", {}: {}",
3152 "start_compact_target",
3153 self.start_compact_target()
3154 )?;
3155 write!(
3156 f,
3157 ", {}: {}",
3158 "end_compact_target",
3159 self.end_compact_target()
3160 )?;
3161 write!(f, " }}")
3162 }
3163}
3164impl<'r> HeaderDigestReader<'r> {
3165 pub const TOTAL_SIZE: usize = 120;
3166 pub const FIELD_SIZES: [usize; 10] = [32, 32, 8, 8, 8, 8, 8, 8, 4, 4];
3167 pub const FIELD_COUNT: usize = 10;
3168 pub fn children_hash(&self) -> Byte32Reader<'r> {
3169 Byte32Reader::new_unchecked(&self.as_slice()[0..32])
3170 }
3171 pub fn total_difficulty(&self) -> Uint256Reader<'r> {
3172 Uint256Reader::new_unchecked(&self.as_slice()[32..64])
3173 }
3174 pub fn start_number(&self) -> Uint64Reader<'r> {
3175 Uint64Reader::new_unchecked(&self.as_slice()[64..72])
3176 }
3177 pub fn end_number(&self) -> Uint64Reader<'r> {
3178 Uint64Reader::new_unchecked(&self.as_slice()[72..80])
3179 }
3180 pub fn start_epoch(&self) -> Uint64Reader<'r> {
3181 Uint64Reader::new_unchecked(&self.as_slice()[80..88])
3182 }
3183 pub fn end_epoch(&self) -> Uint64Reader<'r> {
3184 Uint64Reader::new_unchecked(&self.as_slice()[88..96])
3185 }
3186 pub fn start_timestamp(&self) -> Uint64Reader<'r> {
3187 Uint64Reader::new_unchecked(&self.as_slice()[96..104])
3188 }
3189 pub fn end_timestamp(&self) -> Uint64Reader<'r> {
3190 Uint64Reader::new_unchecked(&self.as_slice()[104..112])
3191 }
3192 pub fn start_compact_target(&self) -> Uint32Reader<'r> {
3193 Uint32Reader::new_unchecked(&self.as_slice()[112..116])
3194 }
3195 pub fn end_compact_target(&self) -> Uint32Reader<'r> {
3196 Uint32Reader::new_unchecked(&self.as_slice()[116..120])
3197 }
3198}
3199impl<'r> molecule::prelude::Reader<'r> for HeaderDigestReader<'r> {
3200 type Entity = HeaderDigest;
3201 const NAME: &'static str = "HeaderDigestReader";
3202 fn to_entity(&self) -> Self::Entity {
3203 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3204 }
3205 fn new_unchecked(slice: &'r [u8]) -> Self {
3206 HeaderDigestReader(slice)
3207 }
3208 fn as_slice(&self) -> &'r [u8] {
3209 self.0
3210 }
3211 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
3212 use molecule::verification_error as ve;
3213 let slice_len = slice.len();
3214 if slice_len != Self::TOTAL_SIZE {
3215 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
3216 }
3217 Ok(())
3218 }
3219}
3220#[derive(Clone, Debug, Default)]
3221pub struct HeaderDigestBuilder {
3222 pub(crate) children_hash: Byte32,
3223 pub(crate) total_difficulty: Uint256,
3224 pub(crate) start_number: Uint64,
3225 pub(crate) end_number: Uint64,
3226 pub(crate) start_epoch: Uint64,
3227 pub(crate) end_epoch: Uint64,
3228 pub(crate) start_timestamp: Uint64,
3229 pub(crate) end_timestamp: Uint64,
3230 pub(crate) start_compact_target: Uint32,
3231 pub(crate) end_compact_target: Uint32,
3232}
3233impl HeaderDigestBuilder {
3234 pub const TOTAL_SIZE: usize = 120;
3235 pub const FIELD_SIZES: [usize; 10] = [32, 32, 8, 8, 8, 8, 8, 8, 4, 4];
3236 pub const FIELD_COUNT: usize = 10;
3237 pub fn children_hash<T>(mut self, v: T) -> Self
3238 where
3239 T: ::core::convert::Into<Byte32>,
3240 {
3241 self.children_hash = v.into();
3242 self
3243 }
3244 pub fn total_difficulty<T>(mut self, v: T) -> Self
3245 where
3246 T: ::core::convert::Into<Uint256>,
3247 {
3248 self.total_difficulty = v.into();
3249 self
3250 }
3251 pub fn start_number<T>(mut self, v: T) -> Self
3252 where
3253 T: ::core::convert::Into<Uint64>,
3254 {
3255 self.start_number = v.into();
3256 self
3257 }
3258 pub fn end_number<T>(mut self, v: T) -> Self
3259 where
3260 T: ::core::convert::Into<Uint64>,
3261 {
3262 self.end_number = v.into();
3263 self
3264 }
3265 pub fn start_epoch<T>(mut self, v: T) -> Self
3266 where
3267 T: ::core::convert::Into<Uint64>,
3268 {
3269 self.start_epoch = v.into();
3270 self
3271 }
3272 pub fn end_epoch<T>(mut self, v: T) -> Self
3273 where
3274 T: ::core::convert::Into<Uint64>,
3275 {
3276 self.end_epoch = v.into();
3277 self
3278 }
3279 pub fn start_timestamp<T>(mut self, v: T) -> Self
3280 where
3281 T: ::core::convert::Into<Uint64>,
3282 {
3283 self.start_timestamp = v.into();
3284 self
3285 }
3286 pub fn end_timestamp<T>(mut self, v: T) -> Self
3287 where
3288 T: ::core::convert::Into<Uint64>,
3289 {
3290 self.end_timestamp = v.into();
3291 self
3292 }
3293 pub fn start_compact_target<T>(mut self, v: T) -> Self
3294 where
3295 T: ::core::convert::Into<Uint32>,
3296 {
3297 self.start_compact_target = v.into();
3298 self
3299 }
3300 pub fn end_compact_target<T>(mut self, v: T) -> Self
3301 where
3302 T: ::core::convert::Into<Uint32>,
3303 {
3304 self.end_compact_target = v.into();
3305 self
3306 }
3307}
3308impl molecule::prelude::Builder for HeaderDigestBuilder {
3309 type Entity = HeaderDigest;
3310 const NAME: &'static str = "HeaderDigestBuilder";
3311 fn expected_length(&self) -> usize {
3312 Self::TOTAL_SIZE
3313 }
3314 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3315 writer.write_all(self.children_hash.as_slice())?;
3316 writer.write_all(self.total_difficulty.as_slice())?;
3317 writer.write_all(self.start_number.as_slice())?;
3318 writer.write_all(self.end_number.as_slice())?;
3319 writer.write_all(self.start_epoch.as_slice())?;
3320 writer.write_all(self.end_epoch.as_slice())?;
3321 writer.write_all(self.start_timestamp.as_slice())?;
3322 writer.write_all(self.end_timestamp.as_slice())?;
3323 writer.write_all(self.start_compact_target.as_slice())?;
3324 writer.write_all(self.end_compact_target.as_slice())?;
3325 Ok(())
3326 }
3327 fn build(&self) -> Self::Entity {
3328 let mut inner = Vec::with_capacity(self.expected_length());
3329 self.write(&mut inner)
3330 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3331 HeaderDigest::new_unchecked(inner.into())
3332 }
3333}
3334#[derive(Clone)]
3335pub struct HeaderView(molecule::bytes::Bytes);
3336impl ::core::fmt::LowerHex for HeaderView {
3337 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3338 use molecule::hex_string;
3339 if f.alternate() {
3340 write!(f, "0x")?;
3341 }
3342 write!(f, "{}", hex_string(self.as_slice()))
3343 }
3344}
3345impl ::core::fmt::Debug for HeaderView {
3346 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3347 write!(f, "{}({:#x})", Self::NAME, self)
3348 }
3349}
3350impl ::core::fmt::Display for HeaderView {
3351 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3352 write!(f, "{} {{ ", Self::NAME)?;
3353 write!(f, "{}: {}", "hash", self.hash())?;
3354 write!(f, ", {}: {}", "data", self.data())?;
3355 write!(f, " }}")
3356 }
3357}
3358impl ::core::default::Default for HeaderView {
3359 fn default() -> Self {
3360 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3361 HeaderView::new_unchecked(v)
3362 }
3363}
3364impl HeaderView {
3365 const DEFAULT_VALUE: [u8; 240] = [
3366 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3367 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3368 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3369 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3370 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3371 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3372 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3373 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3374 ];
3375 pub const TOTAL_SIZE: usize = 240;
3376 pub const FIELD_SIZES: [usize; 2] = [32, 208];
3377 pub const FIELD_COUNT: usize = 2;
3378 pub fn hash(&self) -> Byte32 {
3379 Byte32::new_unchecked(self.0.slice(0..32))
3380 }
3381 pub fn data(&self) -> Header {
3382 Header::new_unchecked(self.0.slice(32..240))
3383 }
3384 pub fn as_reader<'r>(&'r self) -> HeaderViewReader<'r> {
3385 HeaderViewReader::new_unchecked(self.as_slice())
3386 }
3387}
3388impl molecule::prelude::Entity for HeaderView {
3389 type Builder = HeaderViewBuilder;
3390 const NAME: &'static str = "HeaderView";
3391 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3392 HeaderView(data)
3393 }
3394 fn as_bytes(&self) -> molecule::bytes::Bytes {
3395 self.0.clone()
3396 }
3397 fn as_slice(&self) -> &[u8] {
3398 &self.0[..]
3399 }
3400 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3401 HeaderViewReader::from_slice(slice).map(|reader| reader.to_entity())
3402 }
3403 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3404 HeaderViewReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3405 }
3406 fn new_builder() -> Self::Builder {
3407 ::core::default::Default::default()
3408 }
3409 fn as_builder(self) -> Self::Builder {
3410 Self::new_builder().hash(self.hash()).data(self.data())
3411 }
3412}
3413#[derive(Clone, Copy)]
3414pub struct HeaderViewReader<'r>(&'r [u8]);
3415impl<'r> ::core::fmt::LowerHex for HeaderViewReader<'r> {
3416 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3417 use molecule::hex_string;
3418 if f.alternate() {
3419 write!(f, "0x")?;
3420 }
3421 write!(f, "{}", hex_string(self.as_slice()))
3422 }
3423}
3424impl<'r> ::core::fmt::Debug for HeaderViewReader<'r> {
3425 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3426 write!(f, "{}({:#x})", Self::NAME, self)
3427 }
3428}
3429impl<'r> ::core::fmt::Display for HeaderViewReader<'r> {
3430 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3431 write!(f, "{} {{ ", Self::NAME)?;
3432 write!(f, "{}: {}", "hash", self.hash())?;
3433 write!(f, ", {}: {}", "data", self.data())?;
3434 write!(f, " }}")
3435 }
3436}
3437impl<'r> HeaderViewReader<'r> {
3438 pub const TOTAL_SIZE: usize = 240;
3439 pub const FIELD_SIZES: [usize; 2] = [32, 208];
3440 pub const FIELD_COUNT: usize = 2;
3441 pub fn hash(&self) -> Byte32Reader<'r> {
3442 Byte32Reader::new_unchecked(&self.as_slice()[0..32])
3443 }
3444 pub fn data(&self) -> HeaderReader<'r> {
3445 HeaderReader::new_unchecked(&self.as_slice()[32..240])
3446 }
3447}
3448impl<'r> molecule::prelude::Reader<'r> for HeaderViewReader<'r> {
3449 type Entity = HeaderView;
3450 const NAME: &'static str = "HeaderViewReader";
3451 fn to_entity(&self) -> Self::Entity {
3452 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3453 }
3454 fn new_unchecked(slice: &'r [u8]) -> Self {
3455 HeaderViewReader(slice)
3456 }
3457 fn as_slice(&self) -> &'r [u8] {
3458 self.0
3459 }
3460 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
3461 use molecule::verification_error as ve;
3462 let slice_len = slice.len();
3463 if slice_len != Self::TOTAL_SIZE {
3464 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
3465 }
3466 Ok(())
3467 }
3468}
3469#[derive(Clone, Debug, Default)]
3470pub struct HeaderViewBuilder {
3471 pub(crate) hash: Byte32,
3472 pub(crate) data: Header,
3473}
3474impl HeaderViewBuilder {
3475 pub const TOTAL_SIZE: usize = 240;
3476 pub const FIELD_SIZES: [usize; 2] = [32, 208];
3477 pub const FIELD_COUNT: usize = 2;
3478 pub fn hash<T>(mut self, v: T) -> Self
3479 where
3480 T: ::core::convert::Into<Byte32>,
3481 {
3482 self.hash = v.into();
3483 self
3484 }
3485 pub fn data<T>(mut self, v: T) -> Self
3486 where
3487 T: ::core::convert::Into<Header>,
3488 {
3489 self.data = v.into();
3490 self
3491 }
3492}
3493impl molecule::prelude::Builder for HeaderViewBuilder {
3494 type Entity = HeaderView;
3495 const NAME: &'static str = "HeaderViewBuilder";
3496 fn expected_length(&self) -> usize {
3497 Self::TOTAL_SIZE
3498 }
3499 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3500 writer.write_all(self.hash.as_slice())?;
3501 writer.write_all(self.data.as_slice())?;
3502 Ok(())
3503 }
3504 fn build(&self) -> Self::Entity {
3505 let mut inner = Vec::with_capacity(self.expected_length());
3506 self.write(&mut inner)
3507 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3508 HeaderView::new_unchecked(inner.into())
3509 }
3510}
3511#[derive(Clone)]
3512pub struct UncleBlockVecView(molecule::bytes::Bytes);
3513impl ::core::fmt::LowerHex for UncleBlockVecView {
3514 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3515 use molecule::hex_string;
3516 if f.alternate() {
3517 write!(f, "0x")?;
3518 }
3519 write!(f, "{}", hex_string(self.as_slice()))
3520 }
3521}
3522impl ::core::fmt::Debug for UncleBlockVecView {
3523 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3524 write!(f, "{}({:#x})", Self::NAME, self)
3525 }
3526}
3527impl ::core::fmt::Display for UncleBlockVecView {
3528 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3529 write!(f, "{} {{ ", Self::NAME)?;
3530 write!(f, "{}: {}", "hashes", self.hashes())?;
3531 write!(f, ", {}: {}", "data", self.data())?;
3532 let extra_count = self.count_extra_fields();
3533 if extra_count != 0 {
3534 write!(f, ", .. ({} fields)", extra_count)?;
3535 }
3536 write!(f, " }}")
3537 }
3538}
3539impl ::core::default::Default for UncleBlockVecView {
3540 fn default() -> Self {
3541 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3542 UncleBlockVecView::new_unchecked(v)
3543 }
3544}
3545impl UncleBlockVecView {
3546 const DEFAULT_VALUE: [u8; 20] = [
3547 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
3548 ];
3549 pub const FIELD_COUNT: usize = 2;
3550 pub fn total_size(&self) -> usize {
3551 molecule::unpack_number(self.as_slice()) as usize
3552 }
3553 pub fn field_count(&self) -> usize {
3554 if self.total_size() == molecule::NUMBER_SIZE {
3555 0
3556 } else {
3557 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3558 }
3559 }
3560 pub fn count_extra_fields(&self) -> usize {
3561 self.field_count() - Self::FIELD_COUNT
3562 }
3563 pub fn has_extra_fields(&self) -> bool {
3564 Self::FIELD_COUNT != self.field_count()
3565 }
3566 pub fn hashes(&self) -> Byte32Vec {
3567 let slice = self.as_slice();
3568 let start = molecule::unpack_number(&slice[4..]) as usize;
3569 let end = molecule::unpack_number(&slice[8..]) as usize;
3570 Byte32Vec::new_unchecked(self.0.slice(start..end))
3571 }
3572 pub fn data(&self) -> UncleBlockVec {
3573 let slice = self.as_slice();
3574 let start = molecule::unpack_number(&slice[8..]) as usize;
3575 if self.has_extra_fields() {
3576 let end = molecule::unpack_number(&slice[12..]) as usize;
3577 UncleBlockVec::new_unchecked(self.0.slice(start..end))
3578 } else {
3579 UncleBlockVec::new_unchecked(self.0.slice(start..))
3580 }
3581 }
3582 pub fn as_reader<'r>(&'r self) -> UncleBlockVecViewReader<'r> {
3583 UncleBlockVecViewReader::new_unchecked(self.as_slice())
3584 }
3585}
3586impl molecule::prelude::Entity for UncleBlockVecView {
3587 type Builder = UncleBlockVecViewBuilder;
3588 const NAME: &'static str = "UncleBlockVecView";
3589 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3590 UncleBlockVecView(data)
3591 }
3592 fn as_bytes(&self) -> molecule::bytes::Bytes {
3593 self.0.clone()
3594 }
3595 fn as_slice(&self) -> &[u8] {
3596 &self.0[..]
3597 }
3598 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3599 UncleBlockVecViewReader::from_slice(slice).map(|reader| reader.to_entity())
3600 }
3601 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3602 UncleBlockVecViewReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3603 }
3604 fn new_builder() -> Self::Builder {
3605 ::core::default::Default::default()
3606 }
3607 fn as_builder(self) -> Self::Builder {
3608 Self::new_builder().hashes(self.hashes()).data(self.data())
3609 }
3610}
3611#[derive(Clone, Copy)]
3612pub struct UncleBlockVecViewReader<'r>(&'r [u8]);
3613impl<'r> ::core::fmt::LowerHex for UncleBlockVecViewReader<'r> {
3614 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3615 use molecule::hex_string;
3616 if f.alternate() {
3617 write!(f, "0x")?;
3618 }
3619 write!(f, "{}", hex_string(self.as_slice()))
3620 }
3621}
3622impl<'r> ::core::fmt::Debug for UncleBlockVecViewReader<'r> {
3623 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3624 write!(f, "{}({:#x})", Self::NAME, self)
3625 }
3626}
3627impl<'r> ::core::fmt::Display for UncleBlockVecViewReader<'r> {
3628 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3629 write!(f, "{} {{ ", Self::NAME)?;
3630 write!(f, "{}: {}", "hashes", self.hashes())?;
3631 write!(f, ", {}: {}", "data", self.data())?;
3632 let extra_count = self.count_extra_fields();
3633 if extra_count != 0 {
3634 write!(f, ", .. ({} fields)", extra_count)?;
3635 }
3636 write!(f, " }}")
3637 }
3638}
3639impl<'r> UncleBlockVecViewReader<'r> {
3640 pub const FIELD_COUNT: usize = 2;
3641 pub fn total_size(&self) -> usize {
3642 molecule::unpack_number(self.as_slice()) as usize
3643 }
3644 pub fn field_count(&self) -> usize {
3645 if self.total_size() == molecule::NUMBER_SIZE {
3646 0
3647 } else {
3648 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3649 }
3650 }
3651 pub fn count_extra_fields(&self) -> usize {
3652 self.field_count() - Self::FIELD_COUNT
3653 }
3654 pub fn has_extra_fields(&self) -> bool {
3655 Self::FIELD_COUNT != self.field_count()
3656 }
3657 pub fn hashes(&self) -> Byte32VecReader<'r> {
3658 let slice = self.as_slice();
3659 let start = molecule::unpack_number(&slice[4..]) as usize;
3660 let end = molecule::unpack_number(&slice[8..]) as usize;
3661 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
3662 }
3663 pub fn data(&self) -> UncleBlockVecReader<'r> {
3664 let slice = self.as_slice();
3665 let start = molecule::unpack_number(&slice[8..]) as usize;
3666 if self.has_extra_fields() {
3667 let end = molecule::unpack_number(&slice[12..]) as usize;
3668 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..end])
3669 } else {
3670 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..])
3671 }
3672 }
3673}
3674impl<'r> molecule::prelude::Reader<'r> for UncleBlockVecViewReader<'r> {
3675 type Entity = UncleBlockVecView;
3676 const NAME: &'static str = "UncleBlockVecViewReader";
3677 fn to_entity(&self) -> Self::Entity {
3678 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3679 }
3680 fn new_unchecked(slice: &'r [u8]) -> Self {
3681 UncleBlockVecViewReader(slice)
3682 }
3683 fn as_slice(&self) -> &'r [u8] {
3684 self.0
3685 }
3686 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3687 use molecule::verification_error as ve;
3688 let slice_len = slice.len();
3689 if slice_len < molecule::NUMBER_SIZE {
3690 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3691 }
3692 let total_size = molecule::unpack_number(slice) as usize;
3693 if slice_len != total_size {
3694 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3695 }
3696 if slice_len < molecule::NUMBER_SIZE * 2 {
3697 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3698 }
3699 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3700 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3701 return ve!(Self, OffsetsNotMatch);
3702 }
3703 if slice_len < offset_first {
3704 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3705 }
3706 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3707 if field_count < Self::FIELD_COUNT {
3708 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3709 } else if !compatible && field_count > Self::FIELD_COUNT {
3710 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3711 };
3712 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3713 .chunks_exact(molecule::NUMBER_SIZE)
3714 .map(|x| molecule::unpack_number(x) as usize)
3715 .collect();
3716 offsets.push(total_size);
3717 if offsets.windows(2).any(|i| i[0] > i[1]) {
3718 return ve!(Self, OffsetsNotMatch);
3719 }
3720 Byte32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
3721 UncleBlockVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
3722 Ok(())
3723 }
3724}
3725#[derive(Clone, Debug, Default)]
3726pub struct UncleBlockVecViewBuilder {
3727 pub(crate) hashes: Byte32Vec,
3728 pub(crate) data: UncleBlockVec,
3729}
3730impl UncleBlockVecViewBuilder {
3731 pub const FIELD_COUNT: usize = 2;
3732 pub fn hashes<T>(mut self, v: T) -> Self
3733 where
3734 T: ::core::convert::Into<Byte32Vec>,
3735 {
3736 self.hashes = v.into();
3737 self
3738 }
3739 pub fn data<T>(mut self, v: T) -> Self
3740 where
3741 T: ::core::convert::Into<UncleBlockVec>,
3742 {
3743 self.data = v.into();
3744 self
3745 }
3746}
3747impl molecule::prelude::Builder for UncleBlockVecViewBuilder {
3748 type Entity = UncleBlockVecView;
3749 const NAME: &'static str = "UncleBlockVecViewBuilder";
3750 fn expected_length(&self) -> usize {
3751 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
3752 + self.hashes.as_slice().len()
3753 + self.data.as_slice().len()
3754 }
3755 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3756 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
3757 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
3758 offsets.push(total_size);
3759 total_size += self.hashes.as_slice().len();
3760 offsets.push(total_size);
3761 total_size += self.data.as_slice().len();
3762 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3763 for offset in offsets.into_iter() {
3764 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3765 }
3766 writer.write_all(self.hashes.as_slice())?;
3767 writer.write_all(self.data.as_slice())?;
3768 Ok(())
3769 }
3770 fn build(&self) -> Self::Entity {
3771 let mut inner = Vec::with_capacity(self.expected_length());
3772 self.write(&mut inner)
3773 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3774 UncleBlockVecView::new_unchecked(inner.into())
3775 }
3776}
3777#[derive(Clone)]
3778pub struct TransactionView(molecule::bytes::Bytes);
3779impl ::core::fmt::LowerHex for TransactionView {
3780 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3781 use molecule::hex_string;
3782 if f.alternate() {
3783 write!(f, "0x")?;
3784 }
3785 write!(f, "{}", hex_string(self.as_slice()))
3786 }
3787}
3788impl ::core::fmt::Debug for TransactionView {
3789 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3790 write!(f, "{}({:#x})", Self::NAME, self)
3791 }
3792}
3793impl ::core::fmt::Display for TransactionView {
3794 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3795 write!(f, "{} {{ ", Self::NAME)?;
3796 write!(f, "{}: {}", "hash", self.hash())?;
3797 write!(f, ", {}: {}", "witness_hash", self.witness_hash())?;
3798 write!(f, ", {}: {}", "data", self.data())?;
3799 let extra_count = self.count_extra_fields();
3800 if extra_count != 0 {
3801 write!(f, ", .. ({} fields)", extra_count)?;
3802 }
3803 write!(f, " }}")
3804 }
3805}
3806impl ::core::default::Default for TransactionView {
3807 fn default() -> Self {
3808 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3809 TransactionView::new_unchecked(v)
3810 }
3811}
3812impl TransactionView {
3813 const DEFAULT_VALUE: [u8; 148] = [
3814 148, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3815 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3816 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 12, 0, 0, 0,
3817 64, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 44, 0, 0, 0,
3818 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0,
3819 0, 0,
3820 ];
3821 pub const FIELD_COUNT: usize = 3;
3822 pub fn total_size(&self) -> usize {
3823 molecule::unpack_number(self.as_slice()) as usize
3824 }
3825 pub fn field_count(&self) -> usize {
3826 if self.total_size() == molecule::NUMBER_SIZE {
3827 0
3828 } else {
3829 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3830 }
3831 }
3832 pub fn count_extra_fields(&self) -> usize {
3833 self.field_count() - Self::FIELD_COUNT
3834 }
3835 pub fn has_extra_fields(&self) -> bool {
3836 Self::FIELD_COUNT != self.field_count()
3837 }
3838 pub fn hash(&self) -> Byte32 {
3839 let slice = self.as_slice();
3840 let start = molecule::unpack_number(&slice[4..]) as usize;
3841 let end = molecule::unpack_number(&slice[8..]) as usize;
3842 Byte32::new_unchecked(self.0.slice(start..end))
3843 }
3844 pub fn witness_hash(&self) -> Byte32 {
3845 let slice = self.as_slice();
3846 let start = molecule::unpack_number(&slice[8..]) as usize;
3847 let end = molecule::unpack_number(&slice[12..]) as usize;
3848 Byte32::new_unchecked(self.0.slice(start..end))
3849 }
3850 pub fn data(&self) -> Transaction {
3851 let slice = self.as_slice();
3852 let start = molecule::unpack_number(&slice[12..]) as usize;
3853 if self.has_extra_fields() {
3854 let end = molecule::unpack_number(&slice[16..]) as usize;
3855 Transaction::new_unchecked(self.0.slice(start..end))
3856 } else {
3857 Transaction::new_unchecked(self.0.slice(start..))
3858 }
3859 }
3860 pub fn as_reader<'r>(&'r self) -> TransactionViewReader<'r> {
3861 TransactionViewReader::new_unchecked(self.as_slice())
3862 }
3863}
3864impl molecule::prelude::Entity for TransactionView {
3865 type Builder = TransactionViewBuilder;
3866 const NAME: &'static str = "TransactionView";
3867 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3868 TransactionView(data)
3869 }
3870 fn as_bytes(&self) -> molecule::bytes::Bytes {
3871 self.0.clone()
3872 }
3873 fn as_slice(&self) -> &[u8] {
3874 &self.0[..]
3875 }
3876 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3877 TransactionViewReader::from_slice(slice).map(|reader| reader.to_entity())
3878 }
3879 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3880 TransactionViewReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3881 }
3882 fn new_builder() -> Self::Builder {
3883 ::core::default::Default::default()
3884 }
3885 fn as_builder(self) -> Self::Builder {
3886 Self::new_builder()
3887 .hash(self.hash())
3888 .witness_hash(self.witness_hash())
3889 .data(self.data())
3890 }
3891}
3892#[derive(Clone, Copy)]
3893pub struct TransactionViewReader<'r>(&'r [u8]);
3894impl<'r> ::core::fmt::LowerHex for TransactionViewReader<'r> {
3895 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3896 use molecule::hex_string;
3897 if f.alternate() {
3898 write!(f, "0x")?;
3899 }
3900 write!(f, "{}", hex_string(self.as_slice()))
3901 }
3902}
3903impl<'r> ::core::fmt::Debug for TransactionViewReader<'r> {
3904 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3905 write!(f, "{}({:#x})", Self::NAME, self)
3906 }
3907}
3908impl<'r> ::core::fmt::Display for TransactionViewReader<'r> {
3909 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3910 write!(f, "{} {{ ", Self::NAME)?;
3911 write!(f, "{}: {}", "hash", self.hash())?;
3912 write!(f, ", {}: {}", "witness_hash", self.witness_hash())?;
3913 write!(f, ", {}: {}", "data", self.data())?;
3914 let extra_count = self.count_extra_fields();
3915 if extra_count != 0 {
3916 write!(f, ", .. ({} fields)", extra_count)?;
3917 }
3918 write!(f, " }}")
3919 }
3920}
3921impl<'r> TransactionViewReader<'r> {
3922 pub const FIELD_COUNT: usize = 3;
3923 pub fn total_size(&self) -> usize {
3924 molecule::unpack_number(self.as_slice()) as usize
3925 }
3926 pub fn field_count(&self) -> usize {
3927 if self.total_size() == molecule::NUMBER_SIZE {
3928 0
3929 } else {
3930 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3931 }
3932 }
3933 pub fn count_extra_fields(&self) -> usize {
3934 self.field_count() - Self::FIELD_COUNT
3935 }
3936 pub fn has_extra_fields(&self) -> bool {
3937 Self::FIELD_COUNT != self.field_count()
3938 }
3939 pub fn hash(&self) -> Byte32Reader<'r> {
3940 let slice = self.as_slice();
3941 let start = molecule::unpack_number(&slice[4..]) as usize;
3942 let end = molecule::unpack_number(&slice[8..]) as usize;
3943 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
3944 }
3945 pub fn witness_hash(&self) -> Byte32Reader<'r> {
3946 let slice = self.as_slice();
3947 let start = molecule::unpack_number(&slice[8..]) as usize;
3948 let end = molecule::unpack_number(&slice[12..]) as usize;
3949 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
3950 }
3951 pub fn data(&self) -> TransactionReader<'r> {
3952 let slice = self.as_slice();
3953 let start = molecule::unpack_number(&slice[12..]) as usize;
3954 if self.has_extra_fields() {
3955 let end = molecule::unpack_number(&slice[16..]) as usize;
3956 TransactionReader::new_unchecked(&self.as_slice()[start..end])
3957 } else {
3958 TransactionReader::new_unchecked(&self.as_slice()[start..])
3959 }
3960 }
3961}
3962impl<'r> molecule::prelude::Reader<'r> for TransactionViewReader<'r> {
3963 type Entity = TransactionView;
3964 const NAME: &'static str = "TransactionViewReader";
3965 fn to_entity(&self) -> Self::Entity {
3966 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3967 }
3968 fn new_unchecked(slice: &'r [u8]) -> Self {
3969 TransactionViewReader(slice)
3970 }
3971 fn as_slice(&self) -> &'r [u8] {
3972 self.0
3973 }
3974 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3975 use molecule::verification_error as ve;
3976 let slice_len = slice.len();
3977 if slice_len < molecule::NUMBER_SIZE {
3978 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3979 }
3980 let total_size = molecule::unpack_number(slice) as usize;
3981 if slice_len != total_size {
3982 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3983 }
3984 if slice_len < molecule::NUMBER_SIZE * 2 {
3985 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3986 }
3987 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3988 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3989 return ve!(Self, OffsetsNotMatch);
3990 }
3991 if slice_len < offset_first {
3992 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3993 }
3994 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3995 if field_count < Self::FIELD_COUNT {
3996 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3997 } else if !compatible && field_count > Self::FIELD_COUNT {
3998 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3999 };
4000 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4001 .chunks_exact(molecule::NUMBER_SIZE)
4002 .map(|x| molecule::unpack_number(x) as usize)
4003 .collect();
4004 offsets.push(total_size);
4005 if offsets.windows(2).any(|i| i[0] > i[1]) {
4006 return ve!(Self, OffsetsNotMatch);
4007 }
4008 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
4009 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
4010 TransactionReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
4011 Ok(())
4012 }
4013}
4014#[derive(Clone, Debug, Default)]
4015pub struct TransactionViewBuilder {
4016 pub(crate) hash: Byte32,
4017 pub(crate) witness_hash: Byte32,
4018 pub(crate) data: Transaction,
4019}
4020impl TransactionViewBuilder {
4021 pub const FIELD_COUNT: usize = 3;
4022 pub fn hash<T>(mut self, v: T) -> Self
4023 where
4024 T: ::core::convert::Into<Byte32>,
4025 {
4026 self.hash = v.into();
4027 self
4028 }
4029 pub fn witness_hash<T>(mut self, v: T) -> Self
4030 where
4031 T: ::core::convert::Into<Byte32>,
4032 {
4033 self.witness_hash = v.into();
4034 self
4035 }
4036 pub fn data<T>(mut self, v: T) -> Self
4037 where
4038 T: ::core::convert::Into<Transaction>,
4039 {
4040 self.data = v.into();
4041 self
4042 }
4043}
4044impl molecule::prelude::Builder for TransactionViewBuilder {
4045 type Entity = TransactionView;
4046 const NAME: &'static str = "TransactionViewBuilder";
4047 fn expected_length(&self) -> usize {
4048 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
4049 + self.hash.as_slice().len()
4050 + self.witness_hash.as_slice().len()
4051 + self.data.as_slice().len()
4052 }
4053 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4054 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4055 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4056 offsets.push(total_size);
4057 total_size += self.hash.as_slice().len();
4058 offsets.push(total_size);
4059 total_size += self.witness_hash.as_slice().len();
4060 offsets.push(total_size);
4061 total_size += self.data.as_slice().len();
4062 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4063 for offset in offsets.into_iter() {
4064 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4065 }
4066 writer.write_all(self.hash.as_slice())?;
4067 writer.write_all(self.witness_hash.as_slice())?;
4068 writer.write_all(self.data.as_slice())?;
4069 Ok(())
4070 }
4071 fn build(&self) -> Self::Entity {
4072 let mut inner = Vec::with_capacity(self.expected_length());
4073 self.write(&mut inner)
4074 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4075 TransactionView::new_unchecked(inner.into())
4076 }
4077}
4078#[derive(Clone)]
4079pub struct BlockExt(molecule::bytes::Bytes);
4080impl ::core::fmt::LowerHex for BlockExt {
4081 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4082 use molecule::hex_string;
4083 if f.alternate() {
4084 write!(f, "0x")?;
4085 }
4086 write!(f, "{}", hex_string(self.as_slice()))
4087 }
4088}
4089impl ::core::fmt::Debug for BlockExt {
4090 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4091 write!(f, "{}({:#x})", Self::NAME, self)
4092 }
4093}
4094impl ::core::fmt::Display for BlockExt {
4095 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4096 write!(f, "{} {{ ", Self::NAME)?;
4097 write!(f, "{}: {}", "total_difficulty", self.total_difficulty())?;
4098 write!(
4099 f,
4100 ", {}: {}",
4101 "total_uncles_count",
4102 self.total_uncles_count()
4103 )?;
4104 write!(f, ", {}: {}", "received_at", self.received_at())?;
4105 write!(f, ", {}: {}", "txs_fees", self.txs_fees())?;
4106 write!(f, ", {}: {}", "verified", self.verified())?;
4107 let extra_count = self.count_extra_fields();
4108 if extra_count != 0 {
4109 write!(f, ", .. ({} fields)", extra_count)?;
4110 }
4111 write!(f, " }}")
4112 }
4113}
4114impl ::core::default::Default for BlockExt {
4115 fn default() -> Self {
4116 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4117 BlockExt::new_unchecked(v)
4118 }
4119}
4120impl BlockExt {
4121 const DEFAULT_VALUE: [u8; 76] = [
4122 76, 0, 0, 0, 24, 0, 0, 0, 56, 0, 0, 0, 64, 0, 0, 0, 72, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 0,
4123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4125 ];
4126 pub const FIELD_COUNT: usize = 5;
4127 pub fn total_size(&self) -> usize {
4128 molecule::unpack_number(self.as_slice()) as usize
4129 }
4130 pub fn field_count(&self) -> usize {
4131 if self.total_size() == molecule::NUMBER_SIZE {
4132 0
4133 } else {
4134 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4135 }
4136 }
4137 pub fn count_extra_fields(&self) -> usize {
4138 self.field_count() - Self::FIELD_COUNT
4139 }
4140 pub fn has_extra_fields(&self) -> bool {
4141 Self::FIELD_COUNT != self.field_count()
4142 }
4143 pub fn total_difficulty(&self) -> Uint256 {
4144 let slice = self.as_slice();
4145 let start = molecule::unpack_number(&slice[4..]) as usize;
4146 let end = molecule::unpack_number(&slice[8..]) as usize;
4147 Uint256::new_unchecked(self.0.slice(start..end))
4148 }
4149 pub fn total_uncles_count(&self) -> Uint64 {
4150 let slice = self.as_slice();
4151 let start = molecule::unpack_number(&slice[8..]) as usize;
4152 let end = molecule::unpack_number(&slice[12..]) as usize;
4153 Uint64::new_unchecked(self.0.slice(start..end))
4154 }
4155 pub fn received_at(&self) -> Uint64 {
4156 let slice = self.as_slice();
4157 let start = molecule::unpack_number(&slice[12..]) as usize;
4158 let end = molecule::unpack_number(&slice[16..]) as usize;
4159 Uint64::new_unchecked(self.0.slice(start..end))
4160 }
4161 pub fn txs_fees(&self) -> Uint64Vec {
4162 let slice = self.as_slice();
4163 let start = molecule::unpack_number(&slice[16..]) as usize;
4164 let end = molecule::unpack_number(&slice[20..]) as usize;
4165 Uint64Vec::new_unchecked(self.0.slice(start..end))
4166 }
4167 pub fn verified(&self) -> BoolOpt {
4168 let slice = self.as_slice();
4169 let start = molecule::unpack_number(&slice[20..]) as usize;
4170 if self.has_extra_fields() {
4171 let end = molecule::unpack_number(&slice[24..]) as usize;
4172 BoolOpt::new_unchecked(self.0.slice(start..end))
4173 } else {
4174 BoolOpt::new_unchecked(self.0.slice(start..))
4175 }
4176 }
4177 pub fn as_reader<'r>(&'r self) -> BlockExtReader<'r> {
4178 BlockExtReader::new_unchecked(self.as_slice())
4179 }
4180}
4181impl molecule::prelude::Entity for BlockExt {
4182 type Builder = BlockExtBuilder;
4183 const NAME: &'static str = "BlockExt";
4184 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4185 BlockExt(data)
4186 }
4187 fn as_bytes(&self) -> molecule::bytes::Bytes {
4188 self.0.clone()
4189 }
4190 fn as_slice(&self) -> &[u8] {
4191 &self.0[..]
4192 }
4193 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4194 BlockExtReader::from_slice(slice).map(|reader| reader.to_entity())
4195 }
4196 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4197 BlockExtReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4198 }
4199 fn new_builder() -> Self::Builder {
4200 ::core::default::Default::default()
4201 }
4202 fn as_builder(self) -> Self::Builder {
4203 Self::new_builder()
4204 .total_difficulty(self.total_difficulty())
4205 .total_uncles_count(self.total_uncles_count())
4206 .received_at(self.received_at())
4207 .txs_fees(self.txs_fees())
4208 .verified(self.verified())
4209 }
4210}
4211#[derive(Clone, Copy)]
4212pub struct BlockExtReader<'r>(&'r [u8]);
4213impl<'r> ::core::fmt::LowerHex for BlockExtReader<'r> {
4214 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4215 use molecule::hex_string;
4216 if f.alternate() {
4217 write!(f, "0x")?;
4218 }
4219 write!(f, "{}", hex_string(self.as_slice()))
4220 }
4221}
4222impl<'r> ::core::fmt::Debug for BlockExtReader<'r> {
4223 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4224 write!(f, "{}({:#x})", Self::NAME, self)
4225 }
4226}
4227impl<'r> ::core::fmt::Display for BlockExtReader<'r> {
4228 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4229 write!(f, "{} {{ ", Self::NAME)?;
4230 write!(f, "{}: {}", "total_difficulty", self.total_difficulty())?;
4231 write!(
4232 f,
4233 ", {}: {}",
4234 "total_uncles_count",
4235 self.total_uncles_count()
4236 )?;
4237 write!(f, ", {}: {}", "received_at", self.received_at())?;
4238 write!(f, ", {}: {}", "txs_fees", self.txs_fees())?;
4239 write!(f, ", {}: {}", "verified", self.verified())?;
4240 let extra_count = self.count_extra_fields();
4241 if extra_count != 0 {
4242 write!(f, ", .. ({} fields)", extra_count)?;
4243 }
4244 write!(f, " }}")
4245 }
4246}
4247impl<'r> BlockExtReader<'r> {
4248 pub const FIELD_COUNT: usize = 5;
4249 pub fn total_size(&self) -> usize {
4250 molecule::unpack_number(self.as_slice()) as usize
4251 }
4252 pub fn field_count(&self) -> usize {
4253 if self.total_size() == molecule::NUMBER_SIZE {
4254 0
4255 } else {
4256 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4257 }
4258 }
4259 pub fn count_extra_fields(&self) -> usize {
4260 self.field_count() - Self::FIELD_COUNT
4261 }
4262 pub fn has_extra_fields(&self) -> bool {
4263 Self::FIELD_COUNT != self.field_count()
4264 }
4265 pub fn total_difficulty(&self) -> Uint256Reader<'r> {
4266 let slice = self.as_slice();
4267 let start = molecule::unpack_number(&slice[4..]) as usize;
4268 let end = molecule::unpack_number(&slice[8..]) as usize;
4269 Uint256Reader::new_unchecked(&self.as_slice()[start..end])
4270 }
4271 pub fn total_uncles_count(&self) -> Uint64Reader<'r> {
4272 let slice = self.as_slice();
4273 let start = molecule::unpack_number(&slice[8..]) as usize;
4274 let end = molecule::unpack_number(&slice[12..]) as usize;
4275 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
4276 }
4277 pub fn received_at(&self) -> Uint64Reader<'r> {
4278 let slice = self.as_slice();
4279 let start = molecule::unpack_number(&slice[12..]) as usize;
4280 let end = molecule::unpack_number(&slice[16..]) as usize;
4281 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
4282 }
4283 pub fn txs_fees(&self) -> Uint64VecReader<'r> {
4284 let slice = self.as_slice();
4285 let start = molecule::unpack_number(&slice[16..]) as usize;
4286 let end = molecule::unpack_number(&slice[20..]) as usize;
4287 Uint64VecReader::new_unchecked(&self.as_slice()[start..end])
4288 }
4289 pub fn verified(&self) -> BoolOptReader<'r> {
4290 let slice = self.as_slice();
4291 let start = molecule::unpack_number(&slice[20..]) as usize;
4292 if self.has_extra_fields() {
4293 let end = molecule::unpack_number(&slice[24..]) as usize;
4294 BoolOptReader::new_unchecked(&self.as_slice()[start..end])
4295 } else {
4296 BoolOptReader::new_unchecked(&self.as_slice()[start..])
4297 }
4298 }
4299}
4300impl<'r> molecule::prelude::Reader<'r> for BlockExtReader<'r> {
4301 type Entity = BlockExt;
4302 const NAME: &'static str = "BlockExtReader";
4303 fn to_entity(&self) -> Self::Entity {
4304 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4305 }
4306 fn new_unchecked(slice: &'r [u8]) -> Self {
4307 BlockExtReader(slice)
4308 }
4309 fn as_slice(&self) -> &'r [u8] {
4310 self.0
4311 }
4312 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4313 use molecule::verification_error as ve;
4314 let slice_len = slice.len();
4315 if slice_len < molecule::NUMBER_SIZE {
4316 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4317 }
4318 let total_size = molecule::unpack_number(slice) as usize;
4319 if slice_len != total_size {
4320 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4321 }
4322 if slice_len < molecule::NUMBER_SIZE * 2 {
4323 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
4324 }
4325 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4326 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4327 return ve!(Self, OffsetsNotMatch);
4328 }
4329 if slice_len < offset_first {
4330 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4331 }
4332 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
4333 if field_count < Self::FIELD_COUNT {
4334 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4335 } else if !compatible && field_count > Self::FIELD_COUNT {
4336 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4337 };
4338 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4339 .chunks_exact(molecule::NUMBER_SIZE)
4340 .map(|x| molecule::unpack_number(x) as usize)
4341 .collect();
4342 offsets.push(total_size);
4343 if offsets.windows(2).any(|i| i[0] > i[1]) {
4344 return ve!(Self, OffsetsNotMatch);
4345 }
4346 Uint256Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
4347 Uint64Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
4348 Uint64Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
4349 Uint64VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
4350 BoolOptReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
4351 Ok(())
4352 }
4353}
4354#[derive(Clone, Debug, Default)]
4355pub struct BlockExtBuilder {
4356 pub(crate) total_difficulty: Uint256,
4357 pub(crate) total_uncles_count: Uint64,
4358 pub(crate) received_at: Uint64,
4359 pub(crate) txs_fees: Uint64Vec,
4360 pub(crate) verified: BoolOpt,
4361}
4362impl BlockExtBuilder {
4363 pub const FIELD_COUNT: usize = 5;
4364 pub fn total_difficulty<T>(mut self, v: T) -> Self
4365 where
4366 T: ::core::convert::Into<Uint256>,
4367 {
4368 self.total_difficulty = v.into();
4369 self
4370 }
4371 pub fn total_uncles_count<T>(mut self, v: T) -> Self
4372 where
4373 T: ::core::convert::Into<Uint64>,
4374 {
4375 self.total_uncles_count = v.into();
4376 self
4377 }
4378 pub fn received_at<T>(mut self, v: T) -> Self
4379 where
4380 T: ::core::convert::Into<Uint64>,
4381 {
4382 self.received_at = v.into();
4383 self
4384 }
4385 pub fn txs_fees<T>(mut self, v: T) -> Self
4386 where
4387 T: ::core::convert::Into<Uint64Vec>,
4388 {
4389 self.txs_fees = v.into();
4390 self
4391 }
4392 pub fn verified<T>(mut self, v: T) -> Self
4393 where
4394 T: ::core::convert::Into<BoolOpt>,
4395 {
4396 self.verified = v.into();
4397 self
4398 }
4399}
4400impl molecule::prelude::Builder for BlockExtBuilder {
4401 type Entity = BlockExt;
4402 const NAME: &'static str = "BlockExtBuilder";
4403 fn expected_length(&self) -> usize {
4404 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
4405 + self.total_difficulty.as_slice().len()
4406 + self.total_uncles_count.as_slice().len()
4407 + self.received_at.as_slice().len()
4408 + self.txs_fees.as_slice().len()
4409 + self.verified.as_slice().len()
4410 }
4411 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4412 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4413 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4414 offsets.push(total_size);
4415 total_size += self.total_difficulty.as_slice().len();
4416 offsets.push(total_size);
4417 total_size += self.total_uncles_count.as_slice().len();
4418 offsets.push(total_size);
4419 total_size += self.received_at.as_slice().len();
4420 offsets.push(total_size);
4421 total_size += self.txs_fees.as_slice().len();
4422 offsets.push(total_size);
4423 total_size += self.verified.as_slice().len();
4424 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4425 for offset in offsets.into_iter() {
4426 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4427 }
4428 writer.write_all(self.total_difficulty.as_slice())?;
4429 writer.write_all(self.total_uncles_count.as_slice())?;
4430 writer.write_all(self.received_at.as_slice())?;
4431 writer.write_all(self.txs_fees.as_slice())?;
4432 writer.write_all(self.verified.as_slice())?;
4433 Ok(())
4434 }
4435 fn build(&self) -> Self::Entity {
4436 let mut inner = Vec::with_capacity(self.expected_length());
4437 self.write(&mut inner)
4438 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4439 BlockExt::new_unchecked(inner.into())
4440 }
4441}
4442#[derive(Clone)]
4443pub struct BlockExtV1(molecule::bytes::Bytes);
4444impl ::core::fmt::LowerHex for BlockExtV1 {
4445 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4446 use molecule::hex_string;
4447 if f.alternate() {
4448 write!(f, "0x")?;
4449 }
4450 write!(f, "{}", hex_string(self.as_slice()))
4451 }
4452}
4453impl ::core::fmt::Debug for BlockExtV1 {
4454 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4455 write!(f, "{}({:#x})", Self::NAME, self)
4456 }
4457}
4458impl ::core::fmt::Display for BlockExtV1 {
4459 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4460 write!(f, "{} {{ ", Self::NAME)?;
4461 write!(f, "{}: {}", "total_difficulty", self.total_difficulty())?;
4462 write!(
4463 f,
4464 ", {}: {}",
4465 "total_uncles_count",
4466 self.total_uncles_count()
4467 )?;
4468 write!(f, ", {}: {}", "received_at", self.received_at())?;
4469 write!(f, ", {}: {}", "txs_fees", self.txs_fees())?;
4470 write!(f, ", {}: {}", "verified", self.verified())?;
4471 write!(f, ", {}: {}", "cycles", self.cycles())?;
4472 write!(f, ", {}: {}", "txs_sizes", self.txs_sizes())?;
4473 let extra_count = self.count_extra_fields();
4474 if extra_count != 0 {
4475 write!(f, ", .. ({} fields)", extra_count)?;
4476 }
4477 write!(f, " }}")
4478 }
4479}
4480impl ::core::default::Default for BlockExtV1 {
4481 fn default() -> Self {
4482 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4483 BlockExtV1::new_unchecked(v)
4484 }
4485}
4486impl BlockExtV1 {
4487 const DEFAULT_VALUE: [u8; 84] = [
4488 84, 0, 0, 0, 32, 0, 0, 0, 64, 0, 0, 0, 72, 0, 0, 0, 80, 0, 0, 0, 84, 0, 0, 0, 84, 0, 0, 0,
4489 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4490 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4491 ];
4492 pub const FIELD_COUNT: usize = 7;
4493 pub fn total_size(&self) -> usize {
4494 molecule::unpack_number(self.as_slice()) as usize
4495 }
4496 pub fn field_count(&self) -> usize {
4497 if self.total_size() == molecule::NUMBER_SIZE {
4498 0
4499 } else {
4500 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4501 }
4502 }
4503 pub fn count_extra_fields(&self) -> usize {
4504 self.field_count() - Self::FIELD_COUNT
4505 }
4506 pub fn has_extra_fields(&self) -> bool {
4507 Self::FIELD_COUNT != self.field_count()
4508 }
4509 pub fn total_difficulty(&self) -> Uint256 {
4510 let slice = self.as_slice();
4511 let start = molecule::unpack_number(&slice[4..]) as usize;
4512 let end = molecule::unpack_number(&slice[8..]) as usize;
4513 Uint256::new_unchecked(self.0.slice(start..end))
4514 }
4515 pub fn total_uncles_count(&self) -> Uint64 {
4516 let slice = self.as_slice();
4517 let start = molecule::unpack_number(&slice[8..]) as usize;
4518 let end = molecule::unpack_number(&slice[12..]) as usize;
4519 Uint64::new_unchecked(self.0.slice(start..end))
4520 }
4521 pub fn received_at(&self) -> Uint64 {
4522 let slice = self.as_slice();
4523 let start = molecule::unpack_number(&slice[12..]) as usize;
4524 let end = molecule::unpack_number(&slice[16..]) as usize;
4525 Uint64::new_unchecked(self.0.slice(start..end))
4526 }
4527 pub fn txs_fees(&self) -> Uint64Vec {
4528 let slice = self.as_slice();
4529 let start = molecule::unpack_number(&slice[16..]) as usize;
4530 let end = molecule::unpack_number(&slice[20..]) as usize;
4531 Uint64Vec::new_unchecked(self.0.slice(start..end))
4532 }
4533 pub fn verified(&self) -> BoolOpt {
4534 let slice = self.as_slice();
4535 let start = molecule::unpack_number(&slice[20..]) as usize;
4536 let end = molecule::unpack_number(&slice[24..]) as usize;
4537 BoolOpt::new_unchecked(self.0.slice(start..end))
4538 }
4539 pub fn cycles(&self) -> Uint64VecOpt {
4540 let slice = self.as_slice();
4541 let start = molecule::unpack_number(&slice[24..]) as usize;
4542 let end = molecule::unpack_number(&slice[28..]) as usize;
4543 Uint64VecOpt::new_unchecked(self.0.slice(start..end))
4544 }
4545 pub fn txs_sizes(&self) -> Uint64VecOpt {
4546 let slice = self.as_slice();
4547 let start = molecule::unpack_number(&slice[28..]) as usize;
4548 if self.has_extra_fields() {
4549 let end = molecule::unpack_number(&slice[32..]) as usize;
4550 Uint64VecOpt::new_unchecked(self.0.slice(start..end))
4551 } else {
4552 Uint64VecOpt::new_unchecked(self.0.slice(start..))
4553 }
4554 }
4555 pub fn as_reader<'r>(&'r self) -> BlockExtV1Reader<'r> {
4556 BlockExtV1Reader::new_unchecked(self.as_slice())
4557 }
4558}
4559impl molecule::prelude::Entity for BlockExtV1 {
4560 type Builder = BlockExtV1Builder;
4561 const NAME: &'static str = "BlockExtV1";
4562 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4563 BlockExtV1(data)
4564 }
4565 fn as_bytes(&self) -> molecule::bytes::Bytes {
4566 self.0.clone()
4567 }
4568 fn as_slice(&self) -> &[u8] {
4569 &self.0[..]
4570 }
4571 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4572 BlockExtV1Reader::from_slice(slice).map(|reader| reader.to_entity())
4573 }
4574 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4575 BlockExtV1Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4576 }
4577 fn new_builder() -> Self::Builder {
4578 ::core::default::Default::default()
4579 }
4580 fn as_builder(self) -> Self::Builder {
4581 Self::new_builder()
4582 .total_difficulty(self.total_difficulty())
4583 .total_uncles_count(self.total_uncles_count())
4584 .received_at(self.received_at())
4585 .txs_fees(self.txs_fees())
4586 .verified(self.verified())
4587 .cycles(self.cycles())
4588 .txs_sizes(self.txs_sizes())
4589 }
4590}
4591#[derive(Clone, Copy)]
4592pub struct BlockExtV1Reader<'r>(&'r [u8]);
4593impl<'r> ::core::fmt::LowerHex for BlockExtV1Reader<'r> {
4594 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4595 use molecule::hex_string;
4596 if f.alternate() {
4597 write!(f, "0x")?;
4598 }
4599 write!(f, "{}", hex_string(self.as_slice()))
4600 }
4601}
4602impl<'r> ::core::fmt::Debug for BlockExtV1Reader<'r> {
4603 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4604 write!(f, "{}({:#x})", Self::NAME, self)
4605 }
4606}
4607impl<'r> ::core::fmt::Display for BlockExtV1Reader<'r> {
4608 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4609 write!(f, "{} {{ ", Self::NAME)?;
4610 write!(f, "{}: {}", "total_difficulty", self.total_difficulty())?;
4611 write!(
4612 f,
4613 ", {}: {}",
4614 "total_uncles_count",
4615 self.total_uncles_count()
4616 )?;
4617 write!(f, ", {}: {}", "received_at", self.received_at())?;
4618 write!(f, ", {}: {}", "txs_fees", self.txs_fees())?;
4619 write!(f, ", {}: {}", "verified", self.verified())?;
4620 write!(f, ", {}: {}", "cycles", self.cycles())?;
4621 write!(f, ", {}: {}", "txs_sizes", self.txs_sizes())?;
4622 let extra_count = self.count_extra_fields();
4623 if extra_count != 0 {
4624 write!(f, ", .. ({} fields)", extra_count)?;
4625 }
4626 write!(f, " }}")
4627 }
4628}
4629impl<'r> BlockExtV1Reader<'r> {
4630 pub const FIELD_COUNT: usize = 7;
4631 pub fn total_size(&self) -> usize {
4632 molecule::unpack_number(self.as_slice()) as usize
4633 }
4634 pub fn field_count(&self) -> usize {
4635 if self.total_size() == molecule::NUMBER_SIZE {
4636 0
4637 } else {
4638 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4639 }
4640 }
4641 pub fn count_extra_fields(&self) -> usize {
4642 self.field_count() - Self::FIELD_COUNT
4643 }
4644 pub fn has_extra_fields(&self) -> bool {
4645 Self::FIELD_COUNT != self.field_count()
4646 }
4647 pub fn total_difficulty(&self) -> Uint256Reader<'r> {
4648 let slice = self.as_slice();
4649 let start = molecule::unpack_number(&slice[4..]) as usize;
4650 let end = molecule::unpack_number(&slice[8..]) as usize;
4651 Uint256Reader::new_unchecked(&self.as_slice()[start..end])
4652 }
4653 pub fn total_uncles_count(&self) -> Uint64Reader<'r> {
4654 let slice = self.as_slice();
4655 let start = molecule::unpack_number(&slice[8..]) as usize;
4656 let end = molecule::unpack_number(&slice[12..]) as usize;
4657 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
4658 }
4659 pub fn received_at(&self) -> Uint64Reader<'r> {
4660 let slice = self.as_slice();
4661 let start = molecule::unpack_number(&slice[12..]) as usize;
4662 let end = molecule::unpack_number(&slice[16..]) as usize;
4663 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
4664 }
4665 pub fn txs_fees(&self) -> Uint64VecReader<'r> {
4666 let slice = self.as_slice();
4667 let start = molecule::unpack_number(&slice[16..]) as usize;
4668 let end = molecule::unpack_number(&slice[20..]) as usize;
4669 Uint64VecReader::new_unchecked(&self.as_slice()[start..end])
4670 }
4671 pub fn verified(&self) -> BoolOptReader<'r> {
4672 let slice = self.as_slice();
4673 let start = molecule::unpack_number(&slice[20..]) as usize;
4674 let end = molecule::unpack_number(&slice[24..]) as usize;
4675 BoolOptReader::new_unchecked(&self.as_slice()[start..end])
4676 }
4677 pub fn cycles(&self) -> Uint64VecOptReader<'r> {
4678 let slice = self.as_slice();
4679 let start = molecule::unpack_number(&slice[24..]) as usize;
4680 let end = molecule::unpack_number(&slice[28..]) as usize;
4681 Uint64VecOptReader::new_unchecked(&self.as_slice()[start..end])
4682 }
4683 pub fn txs_sizes(&self) -> Uint64VecOptReader<'r> {
4684 let slice = self.as_slice();
4685 let start = molecule::unpack_number(&slice[28..]) as usize;
4686 if self.has_extra_fields() {
4687 let end = molecule::unpack_number(&slice[32..]) as usize;
4688 Uint64VecOptReader::new_unchecked(&self.as_slice()[start..end])
4689 } else {
4690 Uint64VecOptReader::new_unchecked(&self.as_slice()[start..])
4691 }
4692 }
4693}
4694impl<'r> molecule::prelude::Reader<'r> for BlockExtV1Reader<'r> {
4695 type Entity = BlockExtV1;
4696 const NAME: &'static str = "BlockExtV1Reader";
4697 fn to_entity(&self) -> Self::Entity {
4698 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4699 }
4700 fn new_unchecked(slice: &'r [u8]) -> Self {
4701 BlockExtV1Reader(slice)
4702 }
4703 fn as_slice(&self) -> &'r [u8] {
4704 self.0
4705 }
4706 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4707 use molecule::verification_error as ve;
4708 let slice_len = slice.len();
4709 if slice_len < molecule::NUMBER_SIZE {
4710 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4711 }
4712 let total_size = molecule::unpack_number(slice) as usize;
4713 if slice_len != total_size {
4714 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4715 }
4716 if slice_len < molecule::NUMBER_SIZE * 2 {
4717 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
4718 }
4719 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4720 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4721 return ve!(Self, OffsetsNotMatch);
4722 }
4723 if slice_len < offset_first {
4724 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4725 }
4726 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
4727 if field_count < Self::FIELD_COUNT {
4728 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4729 } else if !compatible && field_count > Self::FIELD_COUNT {
4730 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4731 };
4732 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4733 .chunks_exact(molecule::NUMBER_SIZE)
4734 .map(|x| molecule::unpack_number(x) as usize)
4735 .collect();
4736 offsets.push(total_size);
4737 if offsets.windows(2).any(|i| i[0] > i[1]) {
4738 return ve!(Self, OffsetsNotMatch);
4739 }
4740 Uint256Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
4741 Uint64Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
4742 Uint64Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
4743 Uint64VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
4744 BoolOptReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
4745 Uint64VecOptReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
4746 Uint64VecOptReader::verify(&slice[offsets[6]..offsets[7]], compatible)?;
4747 Ok(())
4748 }
4749}
4750#[derive(Clone, Debug, Default)]
4751pub struct BlockExtV1Builder {
4752 pub(crate) total_difficulty: Uint256,
4753 pub(crate) total_uncles_count: Uint64,
4754 pub(crate) received_at: Uint64,
4755 pub(crate) txs_fees: Uint64Vec,
4756 pub(crate) verified: BoolOpt,
4757 pub(crate) cycles: Uint64VecOpt,
4758 pub(crate) txs_sizes: Uint64VecOpt,
4759}
4760impl BlockExtV1Builder {
4761 pub const FIELD_COUNT: usize = 7;
4762 pub fn total_difficulty<T>(mut self, v: T) -> Self
4763 where
4764 T: ::core::convert::Into<Uint256>,
4765 {
4766 self.total_difficulty = v.into();
4767 self
4768 }
4769 pub fn total_uncles_count<T>(mut self, v: T) -> Self
4770 where
4771 T: ::core::convert::Into<Uint64>,
4772 {
4773 self.total_uncles_count = v.into();
4774 self
4775 }
4776 pub fn received_at<T>(mut self, v: T) -> Self
4777 where
4778 T: ::core::convert::Into<Uint64>,
4779 {
4780 self.received_at = v.into();
4781 self
4782 }
4783 pub fn txs_fees<T>(mut self, v: T) -> Self
4784 where
4785 T: ::core::convert::Into<Uint64Vec>,
4786 {
4787 self.txs_fees = v.into();
4788 self
4789 }
4790 pub fn verified<T>(mut self, v: T) -> Self
4791 where
4792 T: ::core::convert::Into<BoolOpt>,
4793 {
4794 self.verified = v.into();
4795 self
4796 }
4797 pub fn cycles<T>(mut self, v: T) -> Self
4798 where
4799 T: ::core::convert::Into<Uint64VecOpt>,
4800 {
4801 self.cycles = v.into();
4802 self
4803 }
4804 pub fn txs_sizes<T>(mut self, v: T) -> Self
4805 where
4806 T: ::core::convert::Into<Uint64VecOpt>,
4807 {
4808 self.txs_sizes = v.into();
4809 self
4810 }
4811}
4812impl molecule::prelude::Builder for BlockExtV1Builder {
4813 type Entity = BlockExtV1;
4814 const NAME: &'static str = "BlockExtV1Builder";
4815 fn expected_length(&self) -> usize {
4816 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
4817 + self.total_difficulty.as_slice().len()
4818 + self.total_uncles_count.as_slice().len()
4819 + self.received_at.as_slice().len()
4820 + self.txs_fees.as_slice().len()
4821 + self.verified.as_slice().len()
4822 + self.cycles.as_slice().len()
4823 + self.txs_sizes.as_slice().len()
4824 }
4825 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4826 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4827 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4828 offsets.push(total_size);
4829 total_size += self.total_difficulty.as_slice().len();
4830 offsets.push(total_size);
4831 total_size += self.total_uncles_count.as_slice().len();
4832 offsets.push(total_size);
4833 total_size += self.received_at.as_slice().len();
4834 offsets.push(total_size);
4835 total_size += self.txs_fees.as_slice().len();
4836 offsets.push(total_size);
4837 total_size += self.verified.as_slice().len();
4838 offsets.push(total_size);
4839 total_size += self.cycles.as_slice().len();
4840 offsets.push(total_size);
4841 total_size += self.txs_sizes.as_slice().len();
4842 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4843 for offset in offsets.into_iter() {
4844 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4845 }
4846 writer.write_all(self.total_difficulty.as_slice())?;
4847 writer.write_all(self.total_uncles_count.as_slice())?;
4848 writer.write_all(self.received_at.as_slice())?;
4849 writer.write_all(self.txs_fees.as_slice())?;
4850 writer.write_all(self.verified.as_slice())?;
4851 writer.write_all(self.cycles.as_slice())?;
4852 writer.write_all(self.txs_sizes.as_slice())?;
4853 Ok(())
4854 }
4855 fn build(&self) -> Self::Entity {
4856 let mut inner = Vec::with_capacity(self.expected_length());
4857 self.write(&mut inner)
4858 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4859 BlockExtV1::new_unchecked(inner.into())
4860 }
4861}
4862#[derive(Clone)]
4863pub struct EpochExt(molecule::bytes::Bytes);
4864impl ::core::fmt::LowerHex for EpochExt {
4865 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4866 use molecule::hex_string;
4867 if f.alternate() {
4868 write!(f, "0x")?;
4869 }
4870 write!(f, "{}", hex_string(self.as_slice()))
4871 }
4872}
4873impl ::core::fmt::Debug for EpochExt {
4874 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4875 write!(f, "{}({:#x})", Self::NAME, self)
4876 }
4877}
4878impl ::core::fmt::Display for EpochExt {
4879 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4880 write!(f, "{} {{ ", Self::NAME)?;
4881 write!(
4882 f,
4883 "{}: {}",
4884 "previous_epoch_hash_rate",
4885 self.previous_epoch_hash_rate()
4886 )?;
4887 write!(
4888 f,
4889 ", {}: {}",
4890 "last_block_hash_in_previous_epoch",
4891 self.last_block_hash_in_previous_epoch()
4892 )?;
4893 write!(f, ", {}: {}", "compact_target", self.compact_target())?;
4894 write!(f, ", {}: {}", "number", self.number())?;
4895 write!(f, ", {}: {}", "base_block_reward", self.base_block_reward())?;
4896 write!(f, ", {}: {}", "remainder_reward", self.remainder_reward())?;
4897 write!(f, ", {}: {}", "start_number", self.start_number())?;
4898 write!(f, ", {}: {}", "length", self.length())?;
4899 write!(f, " }}")
4900 }
4901}
4902impl ::core::default::Default for EpochExt {
4903 fn default() -> Self {
4904 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4905 EpochExt::new_unchecked(v)
4906 }
4907}
4908impl EpochExt {
4909 const DEFAULT_VALUE: [u8; 108] = [
4910 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4911 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4912 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4913 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4914 ];
4915 pub const TOTAL_SIZE: usize = 108;
4916 pub const FIELD_SIZES: [usize; 8] = [32, 32, 4, 8, 8, 8, 8, 8];
4917 pub const FIELD_COUNT: usize = 8;
4918 pub fn previous_epoch_hash_rate(&self) -> Uint256 {
4919 Uint256::new_unchecked(self.0.slice(0..32))
4920 }
4921 pub fn last_block_hash_in_previous_epoch(&self) -> Byte32 {
4922 Byte32::new_unchecked(self.0.slice(32..64))
4923 }
4924 pub fn compact_target(&self) -> Uint32 {
4925 Uint32::new_unchecked(self.0.slice(64..68))
4926 }
4927 pub fn number(&self) -> Uint64 {
4928 Uint64::new_unchecked(self.0.slice(68..76))
4929 }
4930 pub fn base_block_reward(&self) -> Uint64 {
4931 Uint64::new_unchecked(self.0.slice(76..84))
4932 }
4933 pub fn remainder_reward(&self) -> Uint64 {
4934 Uint64::new_unchecked(self.0.slice(84..92))
4935 }
4936 pub fn start_number(&self) -> Uint64 {
4937 Uint64::new_unchecked(self.0.slice(92..100))
4938 }
4939 pub fn length(&self) -> Uint64 {
4940 Uint64::new_unchecked(self.0.slice(100..108))
4941 }
4942 pub fn as_reader<'r>(&'r self) -> EpochExtReader<'r> {
4943 EpochExtReader::new_unchecked(self.as_slice())
4944 }
4945}
4946impl molecule::prelude::Entity for EpochExt {
4947 type Builder = EpochExtBuilder;
4948 const NAME: &'static str = "EpochExt";
4949 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4950 EpochExt(data)
4951 }
4952 fn as_bytes(&self) -> molecule::bytes::Bytes {
4953 self.0.clone()
4954 }
4955 fn as_slice(&self) -> &[u8] {
4956 &self.0[..]
4957 }
4958 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4959 EpochExtReader::from_slice(slice).map(|reader| reader.to_entity())
4960 }
4961 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4962 EpochExtReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4963 }
4964 fn new_builder() -> Self::Builder {
4965 ::core::default::Default::default()
4966 }
4967 fn as_builder(self) -> Self::Builder {
4968 Self::new_builder()
4969 .previous_epoch_hash_rate(self.previous_epoch_hash_rate())
4970 .last_block_hash_in_previous_epoch(self.last_block_hash_in_previous_epoch())
4971 .compact_target(self.compact_target())
4972 .number(self.number())
4973 .base_block_reward(self.base_block_reward())
4974 .remainder_reward(self.remainder_reward())
4975 .start_number(self.start_number())
4976 .length(self.length())
4977 }
4978}
4979#[derive(Clone, Copy)]
4980pub struct EpochExtReader<'r>(&'r [u8]);
4981impl<'r> ::core::fmt::LowerHex for EpochExtReader<'r> {
4982 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4983 use molecule::hex_string;
4984 if f.alternate() {
4985 write!(f, "0x")?;
4986 }
4987 write!(f, "{}", hex_string(self.as_slice()))
4988 }
4989}
4990impl<'r> ::core::fmt::Debug for EpochExtReader<'r> {
4991 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4992 write!(f, "{}({:#x})", Self::NAME, self)
4993 }
4994}
4995impl<'r> ::core::fmt::Display for EpochExtReader<'r> {
4996 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4997 write!(f, "{} {{ ", Self::NAME)?;
4998 write!(
4999 f,
5000 "{}: {}",
5001 "previous_epoch_hash_rate",
5002 self.previous_epoch_hash_rate()
5003 )?;
5004 write!(
5005 f,
5006 ", {}: {}",
5007 "last_block_hash_in_previous_epoch",
5008 self.last_block_hash_in_previous_epoch()
5009 )?;
5010 write!(f, ", {}: {}", "compact_target", self.compact_target())?;
5011 write!(f, ", {}: {}", "number", self.number())?;
5012 write!(f, ", {}: {}", "base_block_reward", self.base_block_reward())?;
5013 write!(f, ", {}: {}", "remainder_reward", self.remainder_reward())?;
5014 write!(f, ", {}: {}", "start_number", self.start_number())?;
5015 write!(f, ", {}: {}", "length", self.length())?;
5016 write!(f, " }}")
5017 }
5018}
5019impl<'r> EpochExtReader<'r> {
5020 pub const TOTAL_SIZE: usize = 108;
5021 pub const FIELD_SIZES: [usize; 8] = [32, 32, 4, 8, 8, 8, 8, 8];
5022 pub const FIELD_COUNT: usize = 8;
5023 pub fn previous_epoch_hash_rate(&self) -> Uint256Reader<'r> {
5024 Uint256Reader::new_unchecked(&self.as_slice()[0..32])
5025 }
5026 pub fn last_block_hash_in_previous_epoch(&self) -> Byte32Reader<'r> {
5027 Byte32Reader::new_unchecked(&self.as_slice()[32..64])
5028 }
5029 pub fn compact_target(&self) -> Uint32Reader<'r> {
5030 Uint32Reader::new_unchecked(&self.as_slice()[64..68])
5031 }
5032 pub fn number(&self) -> Uint64Reader<'r> {
5033 Uint64Reader::new_unchecked(&self.as_slice()[68..76])
5034 }
5035 pub fn base_block_reward(&self) -> Uint64Reader<'r> {
5036 Uint64Reader::new_unchecked(&self.as_slice()[76..84])
5037 }
5038 pub fn remainder_reward(&self) -> Uint64Reader<'r> {
5039 Uint64Reader::new_unchecked(&self.as_slice()[84..92])
5040 }
5041 pub fn start_number(&self) -> Uint64Reader<'r> {
5042 Uint64Reader::new_unchecked(&self.as_slice()[92..100])
5043 }
5044 pub fn length(&self) -> Uint64Reader<'r> {
5045 Uint64Reader::new_unchecked(&self.as_slice()[100..108])
5046 }
5047}
5048impl<'r> molecule::prelude::Reader<'r> for EpochExtReader<'r> {
5049 type Entity = EpochExt;
5050 const NAME: &'static str = "EpochExtReader";
5051 fn to_entity(&self) -> Self::Entity {
5052 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5053 }
5054 fn new_unchecked(slice: &'r [u8]) -> Self {
5055 EpochExtReader(slice)
5056 }
5057 fn as_slice(&self) -> &'r [u8] {
5058 self.0
5059 }
5060 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
5061 use molecule::verification_error as ve;
5062 let slice_len = slice.len();
5063 if slice_len != Self::TOTAL_SIZE {
5064 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
5065 }
5066 Ok(())
5067 }
5068}
5069#[derive(Clone, Debug, Default)]
5070pub struct EpochExtBuilder {
5071 pub(crate) previous_epoch_hash_rate: Uint256,
5072 pub(crate) last_block_hash_in_previous_epoch: Byte32,
5073 pub(crate) compact_target: Uint32,
5074 pub(crate) number: Uint64,
5075 pub(crate) base_block_reward: Uint64,
5076 pub(crate) remainder_reward: Uint64,
5077 pub(crate) start_number: Uint64,
5078 pub(crate) length: Uint64,
5079}
5080impl EpochExtBuilder {
5081 pub const TOTAL_SIZE: usize = 108;
5082 pub const FIELD_SIZES: [usize; 8] = [32, 32, 4, 8, 8, 8, 8, 8];
5083 pub const FIELD_COUNT: usize = 8;
5084 pub fn previous_epoch_hash_rate<T>(mut self, v: T) -> Self
5085 where
5086 T: ::core::convert::Into<Uint256>,
5087 {
5088 self.previous_epoch_hash_rate = v.into();
5089 self
5090 }
5091 pub fn last_block_hash_in_previous_epoch<T>(mut self, v: T) -> Self
5092 where
5093 T: ::core::convert::Into<Byte32>,
5094 {
5095 self.last_block_hash_in_previous_epoch = v.into();
5096 self
5097 }
5098 pub fn compact_target<T>(mut self, v: T) -> Self
5099 where
5100 T: ::core::convert::Into<Uint32>,
5101 {
5102 self.compact_target = v.into();
5103 self
5104 }
5105 pub fn number<T>(mut self, v: T) -> Self
5106 where
5107 T: ::core::convert::Into<Uint64>,
5108 {
5109 self.number = v.into();
5110 self
5111 }
5112 pub fn base_block_reward<T>(mut self, v: T) -> Self
5113 where
5114 T: ::core::convert::Into<Uint64>,
5115 {
5116 self.base_block_reward = v.into();
5117 self
5118 }
5119 pub fn remainder_reward<T>(mut self, v: T) -> Self
5120 where
5121 T: ::core::convert::Into<Uint64>,
5122 {
5123 self.remainder_reward = v.into();
5124 self
5125 }
5126 pub fn start_number<T>(mut self, v: T) -> Self
5127 where
5128 T: ::core::convert::Into<Uint64>,
5129 {
5130 self.start_number = v.into();
5131 self
5132 }
5133 pub fn length<T>(mut self, v: T) -> Self
5134 where
5135 T: ::core::convert::Into<Uint64>,
5136 {
5137 self.length = v.into();
5138 self
5139 }
5140}
5141impl molecule::prelude::Builder for EpochExtBuilder {
5142 type Entity = EpochExt;
5143 const NAME: &'static str = "EpochExtBuilder";
5144 fn expected_length(&self) -> usize {
5145 Self::TOTAL_SIZE
5146 }
5147 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5148 writer.write_all(self.previous_epoch_hash_rate.as_slice())?;
5149 writer.write_all(self.last_block_hash_in_previous_epoch.as_slice())?;
5150 writer.write_all(self.compact_target.as_slice())?;
5151 writer.write_all(self.number.as_slice())?;
5152 writer.write_all(self.base_block_reward.as_slice())?;
5153 writer.write_all(self.remainder_reward.as_slice())?;
5154 writer.write_all(self.start_number.as_slice())?;
5155 writer.write_all(self.length.as_slice())?;
5156 Ok(())
5157 }
5158 fn build(&self) -> Self::Entity {
5159 let mut inner = Vec::with_capacity(self.expected_length());
5160 self.write(&mut inner)
5161 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5162 EpochExt::new_unchecked(inner.into())
5163 }
5164}
5165#[derive(Clone)]
5166pub struct TransactionKey(molecule::bytes::Bytes);
5167impl ::core::fmt::LowerHex for TransactionKey {
5168 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5169 use molecule::hex_string;
5170 if f.alternate() {
5171 write!(f, "0x")?;
5172 }
5173 write!(f, "{}", hex_string(self.as_slice()))
5174 }
5175}
5176impl ::core::fmt::Debug for TransactionKey {
5177 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5178 write!(f, "{}({:#x})", Self::NAME, self)
5179 }
5180}
5181impl ::core::fmt::Display for TransactionKey {
5182 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5183 write!(f, "{} {{ ", Self::NAME)?;
5184 write!(f, "{}: {}", "block_hash", self.block_hash())?;
5185 write!(f, ", {}: {}", "index", self.index())?;
5186 write!(f, " }}")
5187 }
5188}
5189impl ::core::default::Default for TransactionKey {
5190 fn default() -> Self {
5191 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5192 TransactionKey::new_unchecked(v)
5193 }
5194}
5195impl TransactionKey {
5196 const DEFAULT_VALUE: [u8; 36] = [
5197 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5198 0, 0, 0, 0, 0, 0,
5199 ];
5200 pub const TOTAL_SIZE: usize = 36;
5201 pub const FIELD_SIZES: [usize; 2] = [32, 4];
5202 pub const FIELD_COUNT: usize = 2;
5203 pub fn block_hash(&self) -> Byte32 {
5204 Byte32::new_unchecked(self.0.slice(0..32))
5205 }
5206 pub fn index(&self) -> BeUint32 {
5207 BeUint32::new_unchecked(self.0.slice(32..36))
5208 }
5209 pub fn as_reader<'r>(&'r self) -> TransactionKeyReader<'r> {
5210 TransactionKeyReader::new_unchecked(self.as_slice())
5211 }
5212}
5213impl molecule::prelude::Entity for TransactionKey {
5214 type Builder = TransactionKeyBuilder;
5215 const NAME: &'static str = "TransactionKey";
5216 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5217 TransactionKey(data)
5218 }
5219 fn as_bytes(&self) -> molecule::bytes::Bytes {
5220 self.0.clone()
5221 }
5222 fn as_slice(&self) -> &[u8] {
5223 &self.0[..]
5224 }
5225 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5226 TransactionKeyReader::from_slice(slice).map(|reader| reader.to_entity())
5227 }
5228 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5229 TransactionKeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5230 }
5231 fn new_builder() -> Self::Builder {
5232 ::core::default::Default::default()
5233 }
5234 fn as_builder(self) -> Self::Builder {
5235 Self::new_builder()
5236 .block_hash(self.block_hash())
5237 .index(self.index())
5238 }
5239}
5240#[derive(Clone, Copy)]
5241pub struct TransactionKeyReader<'r>(&'r [u8]);
5242impl<'r> ::core::fmt::LowerHex for TransactionKeyReader<'r> {
5243 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5244 use molecule::hex_string;
5245 if f.alternate() {
5246 write!(f, "0x")?;
5247 }
5248 write!(f, "{}", hex_string(self.as_slice()))
5249 }
5250}
5251impl<'r> ::core::fmt::Debug for TransactionKeyReader<'r> {
5252 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5253 write!(f, "{}({:#x})", Self::NAME, self)
5254 }
5255}
5256impl<'r> ::core::fmt::Display for TransactionKeyReader<'r> {
5257 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5258 write!(f, "{} {{ ", Self::NAME)?;
5259 write!(f, "{}: {}", "block_hash", self.block_hash())?;
5260 write!(f, ", {}: {}", "index", self.index())?;
5261 write!(f, " }}")
5262 }
5263}
5264impl<'r> TransactionKeyReader<'r> {
5265 pub const TOTAL_SIZE: usize = 36;
5266 pub const FIELD_SIZES: [usize; 2] = [32, 4];
5267 pub const FIELD_COUNT: usize = 2;
5268 pub fn block_hash(&self) -> Byte32Reader<'r> {
5269 Byte32Reader::new_unchecked(&self.as_slice()[0..32])
5270 }
5271 pub fn index(&self) -> BeUint32Reader<'r> {
5272 BeUint32Reader::new_unchecked(&self.as_slice()[32..36])
5273 }
5274}
5275impl<'r> molecule::prelude::Reader<'r> for TransactionKeyReader<'r> {
5276 type Entity = TransactionKey;
5277 const NAME: &'static str = "TransactionKeyReader";
5278 fn to_entity(&self) -> Self::Entity {
5279 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5280 }
5281 fn new_unchecked(slice: &'r [u8]) -> Self {
5282 TransactionKeyReader(slice)
5283 }
5284 fn as_slice(&self) -> &'r [u8] {
5285 self.0
5286 }
5287 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
5288 use molecule::verification_error as ve;
5289 let slice_len = slice.len();
5290 if slice_len != Self::TOTAL_SIZE {
5291 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
5292 }
5293 Ok(())
5294 }
5295}
5296#[derive(Clone, Debug, Default)]
5297pub struct TransactionKeyBuilder {
5298 pub(crate) block_hash: Byte32,
5299 pub(crate) index: BeUint32,
5300}
5301impl TransactionKeyBuilder {
5302 pub const TOTAL_SIZE: usize = 36;
5303 pub const FIELD_SIZES: [usize; 2] = [32, 4];
5304 pub const FIELD_COUNT: usize = 2;
5305 pub fn block_hash<T>(mut self, v: T) -> Self
5306 where
5307 T: ::core::convert::Into<Byte32>,
5308 {
5309 self.block_hash = v.into();
5310 self
5311 }
5312 pub fn index<T>(mut self, v: T) -> Self
5313 where
5314 T: ::core::convert::Into<BeUint32>,
5315 {
5316 self.index = v.into();
5317 self
5318 }
5319}
5320impl molecule::prelude::Builder for TransactionKeyBuilder {
5321 type Entity = TransactionKey;
5322 const NAME: &'static str = "TransactionKeyBuilder";
5323 fn expected_length(&self) -> usize {
5324 Self::TOTAL_SIZE
5325 }
5326 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5327 writer.write_all(self.block_hash.as_slice())?;
5328 writer.write_all(self.index.as_slice())?;
5329 Ok(())
5330 }
5331 fn build(&self) -> Self::Entity {
5332 let mut inner = Vec::with_capacity(self.expected_length());
5333 self.write(&mut inner)
5334 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5335 TransactionKey::new_unchecked(inner.into())
5336 }
5337}
5338#[derive(Clone)]
5339pub struct NumberHash(molecule::bytes::Bytes);
5340impl ::core::fmt::LowerHex for NumberHash {
5341 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5342 use molecule::hex_string;
5343 if f.alternate() {
5344 write!(f, "0x")?;
5345 }
5346 write!(f, "{}", hex_string(self.as_slice()))
5347 }
5348}
5349impl ::core::fmt::Debug for NumberHash {
5350 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5351 write!(f, "{}({:#x})", Self::NAME, self)
5352 }
5353}
5354impl ::core::fmt::Display for NumberHash {
5355 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5356 write!(f, "{} {{ ", Self::NAME)?;
5357 write!(f, "{}: {}", "number", self.number())?;
5358 write!(f, ", {}: {}", "block_hash", self.block_hash())?;
5359 write!(f, " }}")
5360 }
5361}
5362impl ::core::default::Default for NumberHash {
5363 fn default() -> Self {
5364 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5365 NumberHash::new_unchecked(v)
5366 }
5367}
5368impl NumberHash {
5369 const DEFAULT_VALUE: [u8; 40] = [
5370 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5371 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5372 ];
5373 pub const TOTAL_SIZE: usize = 40;
5374 pub const FIELD_SIZES: [usize; 2] = [8, 32];
5375 pub const FIELD_COUNT: usize = 2;
5376 pub fn number(&self) -> Uint64 {
5377 Uint64::new_unchecked(self.0.slice(0..8))
5378 }
5379 pub fn block_hash(&self) -> Byte32 {
5380 Byte32::new_unchecked(self.0.slice(8..40))
5381 }
5382 pub fn as_reader<'r>(&'r self) -> NumberHashReader<'r> {
5383 NumberHashReader::new_unchecked(self.as_slice())
5384 }
5385}
5386impl molecule::prelude::Entity for NumberHash {
5387 type Builder = NumberHashBuilder;
5388 const NAME: &'static str = "NumberHash";
5389 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5390 NumberHash(data)
5391 }
5392 fn as_bytes(&self) -> molecule::bytes::Bytes {
5393 self.0.clone()
5394 }
5395 fn as_slice(&self) -> &[u8] {
5396 &self.0[..]
5397 }
5398 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5399 NumberHashReader::from_slice(slice).map(|reader| reader.to_entity())
5400 }
5401 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5402 NumberHashReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5403 }
5404 fn new_builder() -> Self::Builder {
5405 ::core::default::Default::default()
5406 }
5407 fn as_builder(self) -> Self::Builder {
5408 Self::new_builder()
5409 .number(self.number())
5410 .block_hash(self.block_hash())
5411 }
5412}
5413#[derive(Clone, Copy)]
5414pub struct NumberHashReader<'r>(&'r [u8]);
5415impl<'r> ::core::fmt::LowerHex for NumberHashReader<'r> {
5416 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5417 use molecule::hex_string;
5418 if f.alternate() {
5419 write!(f, "0x")?;
5420 }
5421 write!(f, "{}", hex_string(self.as_slice()))
5422 }
5423}
5424impl<'r> ::core::fmt::Debug for NumberHashReader<'r> {
5425 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5426 write!(f, "{}({:#x})", Self::NAME, self)
5427 }
5428}
5429impl<'r> ::core::fmt::Display for NumberHashReader<'r> {
5430 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5431 write!(f, "{} {{ ", Self::NAME)?;
5432 write!(f, "{}: {}", "number", self.number())?;
5433 write!(f, ", {}: {}", "block_hash", self.block_hash())?;
5434 write!(f, " }}")
5435 }
5436}
5437impl<'r> NumberHashReader<'r> {
5438 pub const TOTAL_SIZE: usize = 40;
5439 pub const FIELD_SIZES: [usize; 2] = [8, 32];
5440 pub const FIELD_COUNT: usize = 2;
5441 pub fn number(&self) -> Uint64Reader<'r> {
5442 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
5443 }
5444 pub fn block_hash(&self) -> Byte32Reader<'r> {
5445 Byte32Reader::new_unchecked(&self.as_slice()[8..40])
5446 }
5447}
5448impl<'r> molecule::prelude::Reader<'r> for NumberHashReader<'r> {
5449 type Entity = NumberHash;
5450 const NAME: &'static str = "NumberHashReader";
5451 fn to_entity(&self) -> Self::Entity {
5452 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5453 }
5454 fn new_unchecked(slice: &'r [u8]) -> Self {
5455 NumberHashReader(slice)
5456 }
5457 fn as_slice(&self) -> &'r [u8] {
5458 self.0
5459 }
5460 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
5461 use molecule::verification_error as ve;
5462 let slice_len = slice.len();
5463 if slice_len != Self::TOTAL_SIZE {
5464 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
5465 }
5466 Ok(())
5467 }
5468}
5469#[derive(Clone, Debug, Default)]
5470pub struct NumberHashBuilder {
5471 pub(crate) number: Uint64,
5472 pub(crate) block_hash: Byte32,
5473}
5474impl NumberHashBuilder {
5475 pub const TOTAL_SIZE: usize = 40;
5476 pub const FIELD_SIZES: [usize; 2] = [8, 32];
5477 pub const FIELD_COUNT: usize = 2;
5478 pub fn number<T>(mut self, v: T) -> Self
5479 where
5480 T: ::core::convert::Into<Uint64>,
5481 {
5482 self.number = v.into();
5483 self
5484 }
5485 pub fn block_hash<T>(mut self, v: T) -> Self
5486 where
5487 T: ::core::convert::Into<Byte32>,
5488 {
5489 self.block_hash = v.into();
5490 self
5491 }
5492}
5493impl molecule::prelude::Builder for NumberHashBuilder {
5494 type Entity = NumberHash;
5495 const NAME: &'static str = "NumberHashBuilder";
5496 fn expected_length(&self) -> usize {
5497 Self::TOTAL_SIZE
5498 }
5499 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5500 writer.write_all(self.number.as_slice())?;
5501 writer.write_all(self.block_hash.as_slice())?;
5502 Ok(())
5503 }
5504 fn build(&self) -> Self::Entity {
5505 let mut inner = Vec::with_capacity(self.expected_length());
5506 self.write(&mut inner)
5507 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5508 NumberHash::new_unchecked(inner.into())
5509 }
5510}
5511#[derive(Clone)]
5512pub struct TransactionInfo(molecule::bytes::Bytes);
5513impl ::core::fmt::LowerHex for TransactionInfo {
5514 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5515 use molecule::hex_string;
5516 if f.alternate() {
5517 write!(f, "0x")?;
5518 }
5519 write!(f, "{}", hex_string(self.as_slice()))
5520 }
5521}
5522impl ::core::fmt::Debug for TransactionInfo {
5523 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5524 write!(f, "{}({:#x})", Self::NAME, self)
5525 }
5526}
5527impl ::core::fmt::Display for TransactionInfo {
5528 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5529 write!(f, "{} {{ ", Self::NAME)?;
5530 write!(f, "{}: {}", "block_number", self.block_number())?;
5531 write!(f, ", {}: {}", "block_epoch", self.block_epoch())?;
5532 write!(f, ", {}: {}", "key", self.key())?;
5533 write!(f, " }}")
5534 }
5535}
5536impl ::core::default::Default for TransactionInfo {
5537 fn default() -> Self {
5538 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5539 TransactionInfo::new_unchecked(v)
5540 }
5541}
5542impl TransactionInfo {
5543 const DEFAULT_VALUE: [u8; 52] = [
5544 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5545 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5546 ];
5547 pub const TOTAL_SIZE: usize = 52;
5548 pub const FIELD_SIZES: [usize; 3] = [8, 8, 36];
5549 pub const FIELD_COUNT: usize = 3;
5550 pub fn block_number(&self) -> Uint64 {
5551 Uint64::new_unchecked(self.0.slice(0..8))
5552 }
5553 pub fn block_epoch(&self) -> Uint64 {
5554 Uint64::new_unchecked(self.0.slice(8..16))
5555 }
5556 pub fn key(&self) -> TransactionKey {
5557 TransactionKey::new_unchecked(self.0.slice(16..52))
5558 }
5559 pub fn as_reader<'r>(&'r self) -> TransactionInfoReader<'r> {
5560 TransactionInfoReader::new_unchecked(self.as_slice())
5561 }
5562}
5563impl molecule::prelude::Entity for TransactionInfo {
5564 type Builder = TransactionInfoBuilder;
5565 const NAME: &'static str = "TransactionInfo";
5566 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5567 TransactionInfo(data)
5568 }
5569 fn as_bytes(&self) -> molecule::bytes::Bytes {
5570 self.0.clone()
5571 }
5572 fn as_slice(&self) -> &[u8] {
5573 &self.0[..]
5574 }
5575 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5576 TransactionInfoReader::from_slice(slice).map(|reader| reader.to_entity())
5577 }
5578 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5579 TransactionInfoReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5580 }
5581 fn new_builder() -> Self::Builder {
5582 ::core::default::Default::default()
5583 }
5584 fn as_builder(self) -> Self::Builder {
5585 Self::new_builder()
5586 .block_number(self.block_number())
5587 .block_epoch(self.block_epoch())
5588 .key(self.key())
5589 }
5590}
5591#[derive(Clone, Copy)]
5592pub struct TransactionInfoReader<'r>(&'r [u8]);
5593impl<'r> ::core::fmt::LowerHex for TransactionInfoReader<'r> {
5594 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5595 use molecule::hex_string;
5596 if f.alternate() {
5597 write!(f, "0x")?;
5598 }
5599 write!(f, "{}", hex_string(self.as_slice()))
5600 }
5601}
5602impl<'r> ::core::fmt::Debug for TransactionInfoReader<'r> {
5603 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5604 write!(f, "{}({:#x})", Self::NAME, self)
5605 }
5606}
5607impl<'r> ::core::fmt::Display for TransactionInfoReader<'r> {
5608 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5609 write!(f, "{} {{ ", Self::NAME)?;
5610 write!(f, "{}: {}", "block_number", self.block_number())?;
5611 write!(f, ", {}: {}", "block_epoch", self.block_epoch())?;
5612 write!(f, ", {}: {}", "key", self.key())?;
5613 write!(f, " }}")
5614 }
5615}
5616impl<'r> TransactionInfoReader<'r> {
5617 pub const TOTAL_SIZE: usize = 52;
5618 pub const FIELD_SIZES: [usize; 3] = [8, 8, 36];
5619 pub const FIELD_COUNT: usize = 3;
5620 pub fn block_number(&self) -> Uint64Reader<'r> {
5621 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
5622 }
5623 pub fn block_epoch(&self) -> Uint64Reader<'r> {
5624 Uint64Reader::new_unchecked(&self.as_slice()[8..16])
5625 }
5626 pub fn key(&self) -> TransactionKeyReader<'r> {
5627 TransactionKeyReader::new_unchecked(&self.as_slice()[16..52])
5628 }
5629}
5630impl<'r> molecule::prelude::Reader<'r> for TransactionInfoReader<'r> {
5631 type Entity = TransactionInfo;
5632 const NAME: &'static str = "TransactionInfoReader";
5633 fn to_entity(&self) -> Self::Entity {
5634 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5635 }
5636 fn new_unchecked(slice: &'r [u8]) -> Self {
5637 TransactionInfoReader(slice)
5638 }
5639 fn as_slice(&self) -> &'r [u8] {
5640 self.0
5641 }
5642 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
5643 use molecule::verification_error as ve;
5644 let slice_len = slice.len();
5645 if slice_len != Self::TOTAL_SIZE {
5646 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
5647 }
5648 Ok(())
5649 }
5650}
5651#[derive(Clone, Debug, Default)]
5652pub struct TransactionInfoBuilder {
5653 pub(crate) block_number: Uint64,
5654 pub(crate) block_epoch: Uint64,
5655 pub(crate) key: TransactionKey,
5656}
5657impl TransactionInfoBuilder {
5658 pub const TOTAL_SIZE: usize = 52;
5659 pub const FIELD_SIZES: [usize; 3] = [8, 8, 36];
5660 pub const FIELD_COUNT: usize = 3;
5661 pub fn block_number<T>(mut self, v: T) -> Self
5662 where
5663 T: ::core::convert::Into<Uint64>,
5664 {
5665 self.block_number = v.into();
5666 self
5667 }
5668 pub fn block_epoch<T>(mut self, v: T) -> Self
5669 where
5670 T: ::core::convert::Into<Uint64>,
5671 {
5672 self.block_epoch = v.into();
5673 self
5674 }
5675 pub fn key<T>(mut self, v: T) -> Self
5676 where
5677 T: ::core::convert::Into<TransactionKey>,
5678 {
5679 self.key = v.into();
5680 self
5681 }
5682}
5683impl molecule::prelude::Builder for TransactionInfoBuilder {
5684 type Entity = TransactionInfo;
5685 const NAME: &'static str = "TransactionInfoBuilder";
5686 fn expected_length(&self) -> usize {
5687 Self::TOTAL_SIZE
5688 }
5689 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5690 writer.write_all(self.block_number.as_slice())?;
5691 writer.write_all(self.block_epoch.as_slice())?;
5692 writer.write_all(self.key.as_slice())?;
5693 Ok(())
5694 }
5695 fn build(&self) -> Self::Entity {
5696 let mut inner = Vec::with_capacity(self.expected_length());
5697 self.write(&mut inner)
5698 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5699 TransactionInfo::new_unchecked(inner.into())
5700 }
5701}
5702#[derive(Clone)]
5703pub struct CellEntry(molecule::bytes::Bytes);
5704impl ::core::fmt::LowerHex for CellEntry {
5705 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5706 use molecule::hex_string;
5707 if f.alternate() {
5708 write!(f, "0x")?;
5709 }
5710 write!(f, "{}", hex_string(self.as_slice()))
5711 }
5712}
5713impl ::core::fmt::Debug for CellEntry {
5714 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5715 write!(f, "{}({:#x})", Self::NAME, self)
5716 }
5717}
5718impl ::core::fmt::Display for CellEntry {
5719 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5720 write!(f, "{} {{ ", Self::NAME)?;
5721 write!(f, "{}: {}", "output", self.output())?;
5722 write!(f, ", {}: {}", "block_hash", self.block_hash())?;
5723 write!(f, ", {}: {}", "block_number", self.block_number())?;
5724 write!(f, ", {}: {}", "block_epoch", self.block_epoch())?;
5725 write!(f, ", {}: {}", "index", self.index())?;
5726 write!(f, ", {}: {}", "data_size", self.data_size())?;
5727 let extra_count = self.count_extra_fields();
5728 if extra_count != 0 {
5729 write!(f, ", .. ({} fields)", extra_count)?;
5730 }
5731 write!(f, " }}")
5732 }
5733}
5734impl ::core::default::Default for CellEntry {
5735 fn default() -> Self {
5736 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5737 CellEntry::new_unchecked(v)
5738 }
5739}
5740impl CellEntry {
5741 const DEFAULT_VALUE: [u8; 165] = [
5742 165, 0, 0, 0, 28, 0, 0, 0, 105, 0, 0, 0, 137, 0, 0, 0, 145, 0, 0, 0, 153, 0, 0, 0, 157, 0,
5743 0, 0, 77, 0, 0, 0, 16, 0, 0, 0, 24, 0, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0,
5744 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5745 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5746 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5747 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5748 ];
5749 pub const FIELD_COUNT: usize = 6;
5750 pub fn total_size(&self) -> usize {
5751 molecule::unpack_number(self.as_slice()) as usize
5752 }
5753 pub fn field_count(&self) -> usize {
5754 if self.total_size() == molecule::NUMBER_SIZE {
5755 0
5756 } else {
5757 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5758 }
5759 }
5760 pub fn count_extra_fields(&self) -> usize {
5761 self.field_count() - Self::FIELD_COUNT
5762 }
5763 pub fn has_extra_fields(&self) -> bool {
5764 Self::FIELD_COUNT != self.field_count()
5765 }
5766 pub fn output(&self) -> CellOutput {
5767 let slice = self.as_slice();
5768 let start = molecule::unpack_number(&slice[4..]) as usize;
5769 let end = molecule::unpack_number(&slice[8..]) as usize;
5770 CellOutput::new_unchecked(self.0.slice(start..end))
5771 }
5772 pub fn block_hash(&self) -> Byte32 {
5773 let slice = self.as_slice();
5774 let start = molecule::unpack_number(&slice[8..]) as usize;
5775 let end = molecule::unpack_number(&slice[12..]) as usize;
5776 Byte32::new_unchecked(self.0.slice(start..end))
5777 }
5778 pub fn block_number(&self) -> Uint64 {
5779 let slice = self.as_slice();
5780 let start = molecule::unpack_number(&slice[12..]) as usize;
5781 let end = molecule::unpack_number(&slice[16..]) as usize;
5782 Uint64::new_unchecked(self.0.slice(start..end))
5783 }
5784 pub fn block_epoch(&self) -> Uint64 {
5785 let slice = self.as_slice();
5786 let start = molecule::unpack_number(&slice[16..]) as usize;
5787 let end = molecule::unpack_number(&slice[20..]) as usize;
5788 Uint64::new_unchecked(self.0.slice(start..end))
5789 }
5790 pub fn index(&self) -> Uint32 {
5791 let slice = self.as_slice();
5792 let start = molecule::unpack_number(&slice[20..]) as usize;
5793 let end = molecule::unpack_number(&slice[24..]) as usize;
5794 Uint32::new_unchecked(self.0.slice(start..end))
5795 }
5796 pub fn data_size(&self) -> Uint64 {
5797 let slice = self.as_slice();
5798 let start = molecule::unpack_number(&slice[24..]) as usize;
5799 if self.has_extra_fields() {
5800 let end = molecule::unpack_number(&slice[28..]) as usize;
5801 Uint64::new_unchecked(self.0.slice(start..end))
5802 } else {
5803 Uint64::new_unchecked(self.0.slice(start..))
5804 }
5805 }
5806 pub fn as_reader<'r>(&'r self) -> CellEntryReader<'r> {
5807 CellEntryReader::new_unchecked(self.as_slice())
5808 }
5809}
5810impl molecule::prelude::Entity for CellEntry {
5811 type Builder = CellEntryBuilder;
5812 const NAME: &'static str = "CellEntry";
5813 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5814 CellEntry(data)
5815 }
5816 fn as_bytes(&self) -> molecule::bytes::Bytes {
5817 self.0.clone()
5818 }
5819 fn as_slice(&self) -> &[u8] {
5820 &self.0[..]
5821 }
5822 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5823 CellEntryReader::from_slice(slice).map(|reader| reader.to_entity())
5824 }
5825 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5826 CellEntryReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5827 }
5828 fn new_builder() -> Self::Builder {
5829 ::core::default::Default::default()
5830 }
5831 fn as_builder(self) -> Self::Builder {
5832 Self::new_builder()
5833 .output(self.output())
5834 .block_hash(self.block_hash())
5835 .block_number(self.block_number())
5836 .block_epoch(self.block_epoch())
5837 .index(self.index())
5838 .data_size(self.data_size())
5839 }
5840}
5841#[derive(Clone, Copy)]
5842pub struct CellEntryReader<'r>(&'r [u8]);
5843impl<'r> ::core::fmt::LowerHex for CellEntryReader<'r> {
5844 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5845 use molecule::hex_string;
5846 if f.alternate() {
5847 write!(f, "0x")?;
5848 }
5849 write!(f, "{}", hex_string(self.as_slice()))
5850 }
5851}
5852impl<'r> ::core::fmt::Debug for CellEntryReader<'r> {
5853 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5854 write!(f, "{}({:#x})", Self::NAME, self)
5855 }
5856}
5857impl<'r> ::core::fmt::Display for CellEntryReader<'r> {
5858 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5859 write!(f, "{} {{ ", Self::NAME)?;
5860 write!(f, "{}: {}", "output", self.output())?;
5861 write!(f, ", {}: {}", "block_hash", self.block_hash())?;
5862 write!(f, ", {}: {}", "block_number", self.block_number())?;
5863 write!(f, ", {}: {}", "block_epoch", self.block_epoch())?;
5864 write!(f, ", {}: {}", "index", self.index())?;
5865 write!(f, ", {}: {}", "data_size", self.data_size())?;
5866 let extra_count = self.count_extra_fields();
5867 if extra_count != 0 {
5868 write!(f, ", .. ({} fields)", extra_count)?;
5869 }
5870 write!(f, " }}")
5871 }
5872}
5873impl<'r> CellEntryReader<'r> {
5874 pub const FIELD_COUNT: usize = 6;
5875 pub fn total_size(&self) -> usize {
5876 molecule::unpack_number(self.as_slice()) as usize
5877 }
5878 pub fn field_count(&self) -> usize {
5879 if self.total_size() == molecule::NUMBER_SIZE {
5880 0
5881 } else {
5882 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5883 }
5884 }
5885 pub fn count_extra_fields(&self) -> usize {
5886 self.field_count() - Self::FIELD_COUNT
5887 }
5888 pub fn has_extra_fields(&self) -> bool {
5889 Self::FIELD_COUNT != self.field_count()
5890 }
5891 pub fn output(&self) -> CellOutputReader<'r> {
5892 let slice = self.as_slice();
5893 let start = molecule::unpack_number(&slice[4..]) as usize;
5894 let end = molecule::unpack_number(&slice[8..]) as usize;
5895 CellOutputReader::new_unchecked(&self.as_slice()[start..end])
5896 }
5897 pub fn block_hash(&self) -> Byte32Reader<'r> {
5898 let slice = self.as_slice();
5899 let start = molecule::unpack_number(&slice[8..]) as usize;
5900 let end = molecule::unpack_number(&slice[12..]) as usize;
5901 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
5902 }
5903 pub fn block_number(&self) -> Uint64Reader<'r> {
5904 let slice = self.as_slice();
5905 let start = molecule::unpack_number(&slice[12..]) as usize;
5906 let end = molecule::unpack_number(&slice[16..]) as usize;
5907 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
5908 }
5909 pub fn block_epoch(&self) -> Uint64Reader<'r> {
5910 let slice = self.as_slice();
5911 let start = molecule::unpack_number(&slice[16..]) as usize;
5912 let end = molecule::unpack_number(&slice[20..]) as usize;
5913 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
5914 }
5915 pub fn index(&self) -> Uint32Reader<'r> {
5916 let slice = self.as_slice();
5917 let start = molecule::unpack_number(&slice[20..]) as usize;
5918 let end = molecule::unpack_number(&slice[24..]) as usize;
5919 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
5920 }
5921 pub fn data_size(&self) -> Uint64Reader<'r> {
5922 let slice = self.as_slice();
5923 let start = molecule::unpack_number(&slice[24..]) as usize;
5924 if self.has_extra_fields() {
5925 let end = molecule::unpack_number(&slice[28..]) as usize;
5926 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
5927 } else {
5928 Uint64Reader::new_unchecked(&self.as_slice()[start..])
5929 }
5930 }
5931}
5932impl<'r> molecule::prelude::Reader<'r> for CellEntryReader<'r> {
5933 type Entity = CellEntry;
5934 const NAME: &'static str = "CellEntryReader";
5935 fn to_entity(&self) -> Self::Entity {
5936 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5937 }
5938 fn new_unchecked(slice: &'r [u8]) -> Self {
5939 CellEntryReader(slice)
5940 }
5941 fn as_slice(&self) -> &'r [u8] {
5942 self.0
5943 }
5944 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5945 use molecule::verification_error as ve;
5946 let slice_len = slice.len();
5947 if slice_len < molecule::NUMBER_SIZE {
5948 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5949 }
5950 let total_size = molecule::unpack_number(slice) as usize;
5951 if slice_len != total_size {
5952 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5953 }
5954 if slice_len < molecule::NUMBER_SIZE * 2 {
5955 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
5956 }
5957 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
5958 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
5959 return ve!(Self, OffsetsNotMatch);
5960 }
5961 if slice_len < offset_first {
5962 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
5963 }
5964 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
5965 if field_count < Self::FIELD_COUNT {
5966 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5967 } else if !compatible && field_count > Self::FIELD_COUNT {
5968 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5969 };
5970 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
5971 .chunks_exact(molecule::NUMBER_SIZE)
5972 .map(|x| molecule::unpack_number(x) as usize)
5973 .collect();
5974 offsets.push(total_size);
5975 if offsets.windows(2).any(|i| i[0] > i[1]) {
5976 return ve!(Self, OffsetsNotMatch);
5977 }
5978 CellOutputReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
5979 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
5980 Uint64Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
5981 Uint64Reader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
5982 Uint32Reader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
5983 Uint64Reader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
5984 Ok(())
5985 }
5986}
5987#[derive(Clone, Debug, Default)]
5988pub struct CellEntryBuilder {
5989 pub(crate) output: CellOutput,
5990 pub(crate) block_hash: Byte32,
5991 pub(crate) block_number: Uint64,
5992 pub(crate) block_epoch: Uint64,
5993 pub(crate) index: Uint32,
5994 pub(crate) data_size: Uint64,
5995}
5996impl CellEntryBuilder {
5997 pub const FIELD_COUNT: usize = 6;
5998 pub fn output<T>(mut self, v: T) -> Self
5999 where
6000 T: ::core::convert::Into<CellOutput>,
6001 {
6002 self.output = v.into();
6003 self
6004 }
6005 pub fn block_hash<T>(mut self, v: T) -> Self
6006 where
6007 T: ::core::convert::Into<Byte32>,
6008 {
6009 self.block_hash = v.into();
6010 self
6011 }
6012 pub fn block_number<T>(mut self, v: T) -> Self
6013 where
6014 T: ::core::convert::Into<Uint64>,
6015 {
6016 self.block_number = v.into();
6017 self
6018 }
6019 pub fn block_epoch<T>(mut self, v: T) -> Self
6020 where
6021 T: ::core::convert::Into<Uint64>,
6022 {
6023 self.block_epoch = v.into();
6024 self
6025 }
6026 pub fn index<T>(mut self, v: T) -> Self
6027 where
6028 T: ::core::convert::Into<Uint32>,
6029 {
6030 self.index = v.into();
6031 self
6032 }
6033 pub fn data_size<T>(mut self, v: T) -> Self
6034 where
6035 T: ::core::convert::Into<Uint64>,
6036 {
6037 self.data_size = v.into();
6038 self
6039 }
6040}
6041impl molecule::prelude::Builder for CellEntryBuilder {
6042 type Entity = CellEntry;
6043 const NAME: &'static str = "CellEntryBuilder";
6044 fn expected_length(&self) -> usize {
6045 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6046 + self.output.as_slice().len()
6047 + self.block_hash.as_slice().len()
6048 + self.block_number.as_slice().len()
6049 + self.block_epoch.as_slice().len()
6050 + self.index.as_slice().len()
6051 + self.data_size.as_slice().len()
6052 }
6053 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6054 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6055 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6056 offsets.push(total_size);
6057 total_size += self.output.as_slice().len();
6058 offsets.push(total_size);
6059 total_size += self.block_hash.as_slice().len();
6060 offsets.push(total_size);
6061 total_size += self.block_number.as_slice().len();
6062 offsets.push(total_size);
6063 total_size += self.block_epoch.as_slice().len();
6064 offsets.push(total_size);
6065 total_size += self.index.as_slice().len();
6066 offsets.push(total_size);
6067 total_size += self.data_size.as_slice().len();
6068 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6069 for offset in offsets.into_iter() {
6070 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6071 }
6072 writer.write_all(self.output.as_slice())?;
6073 writer.write_all(self.block_hash.as_slice())?;
6074 writer.write_all(self.block_number.as_slice())?;
6075 writer.write_all(self.block_epoch.as_slice())?;
6076 writer.write_all(self.index.as_slice())?;
6077 writer.write_all(self.data_size.as_slice())?;
6078 Ok(())
6079 }
6080 fn build(&self) -> Self::Entity {
6081 let mut inner = Vec::with_capacity(self.expected_length());
6082 self.write(&mut inner)
6083 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6084 CellEntry::new_unchecked(inner.into())
6085 }
6086}
6087#[derive(Clone)]
6088pub struct CellDataEntry(molecule::bytes::Bytes);
6089impl ::core::fmt::LowerHex for CellDataEntry {
6090 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6091 use molecule::hex_string;
6092 if f.alternate() {
6093 write!(f, "0x")?;
6094 }
6095 write!(f, "{}", hex_string(self.as_slice()))
6096 }
6097}
6098impl ::core::fmt::Debug for CellDataEntry {
6099 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6100 write!(f, "{}({:#x})", Self::NAME, self)
6101 }
6102}
6103impl ::core::fmt::Display for CellDataEntry {
6104 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6105 write!(f, "{} {{ ", Self::NAME)?;
6106 write!(f, "{}: {}", "output_data", self.output_data())?;
6107 write!(f, ", {}: {}", "output_data_hash", self.output_data_hash())?;
6108 let extra_count = self.count_extra_fields();
6109 if extra_count != 0 {
6110 write!(f, ", .. ({} fields)", extra_count)?;
6111 }
6112 write!(f, " }}")
6113 }
6114}
6115impl ::core::default::Default for CellDataEntry {
6116 fn default() -> Self {
6117 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
6118 CellDataEntry::new_unchecked(v)
6119 }
6120}
6121impl CellDataEntry {
6122 const DEFAULT_VALUE: [u8; 48] = [
6123 48, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6125 ];
6126 pub const FIELD_COUNT: usize = 2;
6127 pub fn total_size(&self) -> usize {
6128 molecule::unpack_number(self.as_slice()) as usize
6129 }
6130 pub fn field_count(&self) -> usize {
6131 if self.total_size() == molecule::NUMBER_SIZE {
6132 0
6133 } else {
6134 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6135 }
6136 }
6137 pub fn count_extra_fields(&self) -> usize {
6138 self.field_count() - Self::FIELD_COUNT
6139 }
6140 pub fn has_extra_fields(&self) -> bool {
6141 Self::FIELD_COUNT != self.field_count()
6142 }
6143 pub fn output_data(&self) -> Bytes {
6144 let slice = self.as_slice();
6145 let start = molecule::unpack_number(&slice[4..]) as usize;
6146 let end = molecule::unpack_number(&slice[8..]) as usize;
6147 Bytes::new_unchecked(self.0.slice(start..end))
6148 }
6149 pub fn output_data_hash(&self) -> Byte32 {
6150 let slice = self.as_slice();
6151 let start = molecule::unpack_number(&slice[8..]) as usize;
6152 if self.has_extra_fields() {
6153 let end = molecule::unpack_number(&slice[12..]) as usize;
6154 Byte32::new_unchecked(self.0.slice(start..end))
6155 } else {
6156 Byte32::new_unchecked(self.0.slice(start..))
6157 }
6158 }
6159 pub fn as_reader<'r>(&'r self) -> CellDataEntryReader<'r> {
6160 CellDataEntryReader::new_unchecked(self.as_slice())
6161 }
6162}
6163impl molecule::prelude::Entity for CellDataEntry {
6164 type Builder = CellDataEntryBuilder;
6165 const NAME: &'static str = "CellDataEntry";
6166 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6167 CellDataEntry(data)
6168 }
6169 fn as_bytes(&self) -> molecule::bytes::Bytes {
6170 self.0.clone()
6171 }
6172 fn as_slice(&self) -> &[u8] {
6173 &self.0[..]
6174 }
6175 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6176 CellDataEntryReader::from_slice(slice).map(|reader| reader.to_entity())
6177 }
6178 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6179 CellDataEntryReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6180 }
6181 fn new_builder() -> Self::Builder {
6182 ::core::default::Default::default()
6183 }
6184 fn as_builder(self) -> Self::Builder {
6185 Self::new_builder()
6186 .output_data(self.output_data())
6187 .output_data_hash(self.output_data_hash())
6188 }
6189}
6190#[derive(Clone, Copy)]
6191pub struct CellDataEntryReader<'r>(&'r [u8]);
6192impl<'r> ::core::fmt::LowerHex for CellDataEntryReader<'r> {
6193 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6194 use molecule::hex_string;
6195 if f.alternate() {
6196 write!(f, "0x")?;
6197 }
6198 write!(f, "{}", hex_string(self.as_slice()))
6199 }
6200}
6201impl<'r> ::core::fmt::Debug for CellDataEntryReader<'r> {
6202 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6203 write!(f, "{}({:#x})", Self::NAME, self)
6204 }
6205}
6206impl<'r> ::core::fmt::Display for CellDataEntryReader<'r> {
6207 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6208 write!(f, "{} {{ ", Self::NAME)?;
6209 write!(f, "{}: {}", "output_data", self.output_data())?;
6210 write!(f, ", {}: {}", "output_data_hash", self.output_data_hash())?;
6211 let extra_count = self.count_extra_fields();
6212 if extra_count != 0 {
6213 write!(f, ", .. ({} fields)", extra_count)?;
6214 }
6215 write!(f, " }}")
6216 }
6217}
6218impl<'r> CellDataEntryReader<'r> {
6219 pub const FIELD_COUNT: usize = 2;
6220 pub fn total_size(&self) -> usize {
6221 molecule::unpack_number(self.as_slice()) as usize
6222 }
6223 pub fn field_count(&self) -> usize {
6224 if self.total_size() == molecule::NUMBER_SIZE {
6225 0
6226 } else {
6227 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6228 }
6229 }
6230 pub fn count_extra_fields(&self) -> usize {
6231 self.field_count() - Self::FIELD_COUNT
6232 }
6233 pub fn has_extra_fields(&self) -> bool {
6234 Self::FIELD_COUNT != self.field_count()
6235 }
6236 pub fn output_data(&self) -> BytesReader<'r> {
6237 let slice = self.as_slice();
6238 let start = molecule::unpack_number(&slice[4..]) as usize;
6239 let end = molecule::unpack_number(&slice[8..]) as usize;
6240 BytesReader::new_unchecked(&self.as_slice()[start..end])
6241 }
6242 pub fn output_data_hash(&self) -> Byte32Reader<'r> {
6243 let slice = self.as_slice();
6244 let start = molecule::unpack_number(&slice[8..]) as usize;
6245 if self.has_extra_fields() {
6246 let end = molecule::unpack_number(&slice[12..]) as usize;
6247 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
6248 } else {
6249 Byte32Reader::new_unchecked(&self.as_slice()[start..])
6250 }
6251 }
6252}
6253impl<'r> molecule::prelude::Reader<'r> for CellDataEntryReader<'r> {
6254 type Entity = CellDataEntry;
6255 const NAME: &'static str = "CellDataEntryReader";
6256 fn to_entity(&self) -> Self::Entity {
6257 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6258 }
6259 fn new_unchecked(slice: &'r [u8]) -> Self {
6260 CellDataEntryReader(slice)
6261 }
6262 fn as_slice(&self) -> &'r [u8] {
6263 self.0
6264 }
6265 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6266 use molecule::verification_error as ve;
6267 let slice_len = slice.len();
6268 if slice_len < molecule::NUMBER_SIZE {
6269 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6270 }
6271 let total_size = molecule::unpack_number(slice) as usize;
6272 if slice_len != total_size {
6273 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6274 }
6275 if slice_len < molecule::NUMBER_SIZE * 2 {
6276 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
6277 }
6278 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6279 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6280 return ve!(Self, OffsetsNotMatch);
6281 }
6282 if slice_len < offset_first {
6283 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6284 }
6285 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
6286 if field_count < Self::FIELD_COUNT {
6287 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6288 } else if !compatible && field_count > Self::FIELD_COUNT {
6289 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6290 };
6291 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6292 .chunks_exact(molecule::NUMBER_SIZE)
6293 .map(|x| molecule::unpack_number(x) as usize)
6294 .collect();
6295 offsets.push(total_size);
6296 if offsets.windows(2).any(|i| i[0] > i[1]) {
6297 return ve!(Self, OffsetsNotMatch);
6298 }
6299 BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
6300 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
6301 Ok(())
6302 }
6303}
6304#[derive(Clone, Debug, Default)]
6305pub struct CellDataEntryBuilder {
6306 pub(crate) output_data: Bytes,
6307 pub(crate) output_data_hash: Byte32,
6308}
6309impl CellDataEntryBuilder {
6310 pub const FIELD_COUNT: usize = 2;
6311 pub fn output_data<T>(mut self, v: T) -> Self
6312 where
6313 T: ::core::convert::Into<Bytes>,
6314 {
6315 self.output_data = v.into();
6316 self
6317 }
6318 pub fn output_data_hash<T>(mut self, v: T) -> Self
6319 where
6320 T: ::core::convert::Into<Byte32>,
6321 {
6322 self.output_data_hash = v.into();
6323 self
6324 }
6325}
6326impl molecule::prelude::Builder for CellDataEntryBuilder {
6327 type Entity = CellDataEntry;
6328 const NAME: &'static str = "CellDataEntryBuilder";
6329 fn expected_length(&self) -> usize {
6330 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6331 + self.output_data.as_slice().len()
6332 + self.output_data_hash.as_slice().len()
6333 }
6334 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6335 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6336 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6337 offsets.push(total_size);
6338 total_size += self.output_data.as_slice().len();
6339 offsets.push(total_size);
6340 total_size += self.output_data_hash.as_slice().len();
6341 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6342 for offset in offsets.into_iter() {
6343 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6344 }
6345 writer.write_all(self.output_data.as_slice())?;
6346 writer.write_all(self.output_data_hash.as_slice())?;
6347 Ok(())
6348 }
6349 fn build(&self) -> Self::Entity {
6350 let mut inner = Vec::with_capacity(self.expected_length());
6351 self.write(&mut inner)
6352 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6353 CellDataEntry::new_unchecked(inner.into())
6354 }
6355}
6356#[derive(Clone)]
6357pub struct RelayMessage(molecule::bytes::Bytes);
6358impl ::core::fmt::LowerHex for RelayMessage {
6359 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6360 use molecule::hex_string;
6361 if f.alternate() {
6362 write!(f, "0x")?;
6363 }
6364 write!(f, "{}", hex_string(self.as_slice()))
6365 }
6366}
6367impl ::core::fmt::Debug for RelayMessage {
6368 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6369 write!(f, "{}({:#x})", Self::NAME, self)
6370 }
6371}
6372impl ::core::fmt::Display for RelayMessage {
6373 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6374 write!(f, "{}(", Self::NAME)?;
6375 self.to_enum().display_inner(f)?;
6376 write!(f, ")")
6377 }
6378}
6379impl ::core::default::Default for RelayMessage {
6380 fn default() -> Self {
6381 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
6382 RelayMessage::new_unchecked(v)
6383 }
6384}
6385impl RelayMessage {
6386 const DEFAULT_VALUE: [u8; 252] = [
6387 0, 0, 0, 0, 248, 0, 0, 0, 24, 0, 0, 0, 232, 0, 0, 0, 236, 0, 0, 0, 240, 0, 0, 0, 244, 0, 0,
6388 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6389 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6390 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6391 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6392 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6393 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6394 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6395 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6396 ];
6397 pub const ITEMS_COUNT: usize = 8;
6398 pub fn item_id(&self) -> molecule::Number {
6399 molecule::unpack_number(self.as_slice())
6400 }
6401 pub fn to_enum(&self) -> RelayMessageUnion {
6402 let inner = self.0.slice(molecule::NUMBER_SIZE..);
6403 match self.item_id() {
6404 0 => CompactBlock::new_unchecked(inner).into(),
6405 1 => RelayTransactions::new_unchecked(inner).into(),
6406 2 => RelayTransactionHashes::new_unchecked(inner).into(),
6407 3 => GetRelayTransactions::new_unchecked(inner).into(),
6408 4 => GetBlockTransactions::new_unchecked(inner).into(),
6409 5 => BlockTransactions::new_unchecked(inner).into(),
6410 6 => GetBlockProposal::new_unchecked(inner).into(),
6411 7 => BlockProposal::new_unchecked(inner).into(),
6412 _ => panic!("{}: invalid data", Self::NAME),
6413 }
6414 }
6415 pub fn as_reader<'r>(&'r self) -> RelayMessageReader<'r> {
6416 RelayMessageReader::new_unchecked(self.as_slice())
6417 }
6418}
6419impl molecule::prelude::Entity for RelayMessage {
6420 type Builder = RelayMessageBuilder;
6421 const NAME: &'static str = "RelayMessage";
6422 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6423 RelayMessage(data)
6424 }
6425 fn as_bytes(&self) -> molecule::bytes::Bytes {
6426 self.0.clone()
6427 }
6428 fn as_slice(&self) -> &[u8] {
6429 &self.0[..]
6430 }
6431 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6432 RelayMessageReader::from_slice(slice).map(|reader| reader.to_entity())
6433 }
6434 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6435 RelayMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6436 }
6437 fn new_builder() -> Self::Builder {
6438 ::core::default::Default::default()
6439 }
6440 fn as_builder(self) -> Self::Builder {
6441 Self::new_builder().set(self.to_enum())
6442 }
6443}
6444#[derive(Clone, Copy)]
6445pub struct RelayMessageReader<'r>(&'r [u8]);
6446impl<'r> ::core::fmt::LowerHex for RelayMessageReader<'r> {
6447 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6448 use molecule::hex_string;
6449 if f.alternate() {
6450 write!(f, "0x")?;
6451 }
6452 write!(f, "{}", hex_string(self.as_slice()))
6453 }
6454}
6455impl<'r> ::core::fmt::Debug for RelayMessageReader<'r> {
6456 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6457 write!(f, "{}({:#x})", Self::NAME, self)
6458 }
6459}
6460impl<'r> ::core::fmt::Display for RelayMessageReader<'r> {
6461 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6462 write!(f, "{}(", Self::NAME)?;
6463 self.to_enum().display_inner(f)?;
6464 write!(f, ")")
6465 }
6466}
6467impl<'r> RelayMessageReader<'r> {
6468 pub const ITEMS_COUNT: usize = 8;
6469 pub fn item_id(&self) -> molecule::Number {
6470 molecule::unpack_number(self.as_slice())
6471 }
6472 pub fn to_enum(&self) -> RelayMessageUnionReader<'r> {
6473 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
6474 match self.item_id() {
6475 0 => CompactBlockReader::new_unchecked(inner).into(),
6476 1 => RelayTransactionsReader::new_unchecked(inner).into(),
6477 2 => RelayTransactionHashesReader::new_unchecked(inner).into(),
6478 3 => GetRelayTransactionsReader::new_unchecked(inner).into(),
6479 4 => GetBlockTransactionsReader::new_unchecked(inner).into(),
6480 5 => BlockTransactionsReader::new_unchecked(inner).into(),
6481 6 => GetBlockProposalReader::new_unchecked(inner).into(),
6482 7 => BlockProposalReader::new_unchecked(inner).into(),
6483 _ => panic!("{}: invalid data", Self::NAME),
6484 }
6485 }
6486}
6487impl<'r> molecule::prelude::Reader<'r> for RelayMessageReader<'r> {
6488 type Entity = RelayMessage;
6489 const NAME: &'static str = "RelayMessageReader";
6490 fn to_entity(&self) -> Self::Entity {
6491 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6492 }
6493 fn new_unchecked(slice: &'r [u8]) -> Self {
6494 RelayMessageReader(slice)
6495 }
6496 fn as_slice(&self) -> &'r [u8] {
6497 self.0
6498 }
6499 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6500 use molecule::verification_error as ve;
6501 let slice_len = slice.len();
6502 if slice_len < molecule::NUMBER_SIZE {
6503 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6504 }
6505 let item_id = molecule::unpack_number(slice);
6506 let inner_slice = &slice[molecule::NUMBER_SIZE..];
6507 match item_id {
6508 0 => CompactBlockReader::verify(inner_slice, compatible),
6509 1 => RelayTransactionsReader::verify(inner_slice, compatible),
6510 2 => RelayTransactionHashesReader::verify(inner_slice, compatible),
6511 3 => GetRelayTransactionsReader::verify(inner_slice, compatible),
6512 4 => GetBlockTransactionsReader::verify(inner_slice, compatible),
6513 5 => BlockTransactionsReader::verify(inner_slice, compatible),
6514 6 => GetBlockProposalReader::verify(inner_slice, compatible),
6515 7 => BlockProposalReader::verify(inner_slice, compatible),
6516 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
6517 }?;
6518 Ok(())
6519 }
6520}
6521#[derive(Clone, Debug, Default)]
6522pub struct RelayMessageBuilder(pub(crate) RelayMessageUnion);
6523impl RelayMessageBuilder {
6524 pub const ITEMS_COUNT: usize = 8;
6525 pub fn set<I>(mut self, v: I) -> Self
6526 where
6527 I: ::core::convert::Into<RelayMessageUnion>,
6528 {
6529 self.0 = v.into();
6530 self
6531 }
6532}
6533impl molecule::prelude::Builder for RelayMessageBuilder {
6534 type Entity = RelayMessage;
6535 const NAME: &'static str = "RelayMessageBuilder";
6536 fn expected_length(&self) -> usize {
6537 molecule::NUMBER_SIZE + self.0.as_slice().len()
6538 }
6539 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6540 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
6541 writer.write_all(self.0.as_slice())
6542 }
6543 fn build(&self) -> Self::Entity {
6544 let mut inner = Vec::with_capacity(self.expected_length());
6545 self.write(&mut inner)
6546 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6547 RelayMessage::new_unchecked(inner.into())
6548 }
6549}
6550#[derive(Debug, Clone)]
6551pub enum RelayMessageUnion {
6552 CompactBlock(CompactBlock),
6553 RelayTransactions(RelayTransactions),
6554 RelayTransactionHashes(RelayTransactionHashes),
6555 GetRelayTransactions(GetRelayTransactions),
6556 GetBlockTransactions(GetBlockTransactions),
6557 BlockTransactions(BlockTransactions),
6558 GetBlockProposal(GetBlockProposal),
6559 BlockProposal(BlockProposal),
6560}
6561#[derive(Debug, Clone, Copy)]
6562pub enum RelayMessageUnionReader<'r> {
6563 CompactBlock(CompactBlockReader<'r>),
6564 RelayTransactions(RelayTransactionsReader<'r>),
6565 RelayTransactionHashes(RelayTransactionHashesReader<'r>),
6566 GetRelayTransactions(GetRelayTransactionsReader<'r>),
6567 GetBlockTransactions(GetBlockTransactionsReader<'r>),
6568 BlockTransactions(BlockTransactionsReader<'r>),
6569 GetBlockProposal(GetBlockProposalReader<'r>),
6570 BlockProposal(BlockProposalReader<'r>),
6571}
6572impl ::core::default::Default for RelayMessageUnion {
6573 fn default() -> Self {
6574 RelayMessageUnion::CompactBlock(::core::default::Default::default())
6575 }
6576}
6577impl ::core::fmt::Display for RelayMessageUnion {
6578 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6579 match self {
6580 RelayMessageUnion::CompactBlock(ref item) => {
6581 write!(f, "{}::{}({})", Self::NAME, CompactBlock::NAME, item)
6582 }
6583 RelayMessageUnion::RelayTransactions(ref item) => {
6584 write!(f, "{}::{}({})", Self::NAME, RelayTransactions::NAME, item)
6585 }
6586 RelayMessageUnion::RelayTransactionHashes(ref item) => {
6587 write!(
6588 f,
6589 "{}::{}({})",
6590 Self::NAME,
6591 RelayTransactionHashes::NAME,
6592 item
6593 )
6594 }
6595 RelayMessageUnion::GetRelayTransactions(ref item) => {
6596 write!(
6597 f,
6598 "{}::{}({})",
6599 Self::NAME,
6600 GetRelayTransactions::NAME,
6601 item
6602 )
6603 }
6604 RelayMessageUnion::GetBlockTransactions(ref item) => {
6605 write!(
6606 f,
6607 "{}::{}({})",
6608 Self::NAME,
6609 GetBlockTransactions::NAME,
6610 item
6611 )
6612 }
6613 RelayMessageUnion::BlockTransactions(ref item) => {
6614 write!(f, "{}::{}({})", Self::NAME, BlockTransactions::NAME, item)
6615 }
6616 RelayMessageUnion::GetBlockProposal(ref item) => {
6617 write!(f, "{}::{}({})", Self::NAME, GetBlockProposal::NAME, item)
6618 }
6619 RelayMessageUnion::BlockProposal(ref item) => {
6620 write!(f, "{}::{}({})", Self::NAME, BlockProposal::NAME, item)
6621 }
6622 }
6623 }
6624}
6625impl<'r> ::core::fmt::Display for RelayMessageUnionReader<'r> {
6626 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6627 match self {
6628 RelayMessageUnionReader::CompactBlock(ref item) => {
6629 write!(f, "{}::{}({})", Self::NAME, CompactBlock::NAME, item)
6630 }
6631 RelayMessageUnionReader::RelayTransactions(ref item) => {
6632 write!(f, "{}::{}({})", Self::NAME, RelayTransactions::NAME, item)
6633 }
6634 RelayMessageUnionReader::RelayTransactionHashes(ref item) => {
6635 write!(
6636 f,
6637 "{}::{}({})",
6638 Self::NAME,
6639 RelayTransactionHashes::NAME,
6640 item
6641 )
6642 }
6643 RelayMessageUnionReader::GetRelayTransactions(ref item) => {
6644 write!(
6645 f,
6646 "{}::{}({})",
6647 Self::NAME,
6648 GetRelayTransactions::NAME,
6649 item
6650 )
6651 }
6652 RelayMessageUnionReader::GetBlockTransactions(ref item) => {
6653 write!(
6654 f,
6655 "{}::{}({})",
6656 Self::NAME,
6657 GetBlockTransactions::NAME,
6658 item
6659 )
6660 }
6661 RelayMessageUnionReader::BlockTransactions(ref item) => {
6662 write!(f, "{}::{}({})", Self::NAME, BlockTransactions::NAME, item)
6663 }
6664 RelayMessageUnionReader::GetBlockProposal(ref item) => {
6665 write!(f, "{}::{}({})", Self::NAME, GetBlockProposal::NAME, item)
6666 }
6667 RelayMessageUnionReader::BlockProposal(ref item) => {
6668 write!(f, "{}::{}({})", Self::NAME, BlockProposal::NAME, item)
6669 }
6670 }
6671 }
6672}
6673impl RelayMessageUnion {
6674 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6675 match self {
6676 RelayMessageUnion::CompactBlock(ref item) => write!(f, "{}", item),
6677 RelayMessageUnion::RelayTransactions(ref item) => write!(f, "{}", item),
6678 RelayMessageUnion::RelayTransactionHashes(ref item) => write!(f, "{}", item),
6679 RelayMessageUnion::GetRelayTransactions(ref item) => write!(f, "{}", item),
6680 RelayMessageUnion::GetBlockTransactions(ref item) => write!(f, "{}", item),
6681 RelayMessageUnion::BlockTransactions(ref item) => write!(f, "{}", item),
6682 RelayMessageUnion::GetBlockProposal(ref item) => write!(f, "{}", item),
6683 RelayMessageUnion::BlockProposal(ref item) => write!(f, "{}", item),
6684 }
6685 }
6686}
6687impl<'r> RelayMessageUnionReader<'r> {
6688 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6689 match self {
6690 RelayMessageUnionReader::CompactBlock(ref item) => write!(f, "{}", item),
6691 RelayMessageUnionReader::RelayTransactions(ref item) => write!(f, "{}", item),
6692 RelayMessageUnionReader::RelayTransactionHashes(ref item) => write!(f, "{}", item),
6693 RelayMessageUnionReader::GetRelayTransactions(ref item) => write!(f, "{}", item),
6694 RelayMessageUnionReader::GetBlockTransactions(ref item) => write!(f, "{}", item),
6695 RelayMessageUnionReader::BlockTransactions(ref item) => write!(f, "{}", item),
6696 RelayMessageUnionReader::GetBlockProposal(ref item) => write!(f, "{}", item),
6697 RelayMessageUnionReader::BlockProposal(ref item) => write!(f, "{}", item),
6698 }
6699 }
6700}
6701impl ::core::convert::From<CompactBlock> for RelayMessageUnion {
6702 fn from(item: CompactBlock) -> Self {
6703 RelayMessageUnion::CompactBlock(item)
6704 }
6705}
6706impl ::core::convert::From<RelayTransactions> for RelayMessageUnion {
6707 fn from(item: RelayTransactions) -> Self {
6708 RelayMessageUnion::RelayTransactions(item)
6709 }
6710}
6711impl ::core::convert::From<RelayTransactionHashes> for RelayMessageUnion {
6712 fn from(item: RelayTransactionHashes) -> Self {
6713 RelayMessageUnion::RelayTransactionHashes(item)
6714 }
6715}
6716impl ::core::convert::From<GetRelayTransactions> for RelayMessageUnion {
6717 fn from(item: GetRelayTransactions) -> Self {
6718 RelayMessageUnion::GetRelayTransactions(item)
6719 }
6720}
6721impl ::core::convert::From<GetBlockTransactions> for RelayMessageUnion {
6722 fn from(item: GetBlockTransactions) -> Self {
6723 RelayMessageUnion::GetBlockTransactions(item)
6724 }
6725}
6726impl ::core::convert::From<BlockTransactions> for RelayMessageUnion {
6727 fn from(item: BlockTransactions) -> Self {
6728 RelayMessageUnion::BlockTransactions(item)
6729 }
6730}
6731impl ::core::convert::From<GetBlockProposal> for RelayMessageUnion {
6732 fn from(item: GetBlockProposal) -> Self {
6733 RelayMessageUnion::GetBlockProposal(item)
6734 }
6735}
6736impl ::core::convert::From<BlockProposal> for RelayMessageUnion {
6737 fn from(item: BlockProposal) -> Self {
6738 RelayMessageUnion::BlockProposal(item)
6739 }
6740}
6741impl<'r> ::core::convert::From<CompactBlockReader<'r>> for RelayMessageUnionReader<'r> {
6742 fn from(item: CompactBlockReader<'r>) -> Self {
6743 RelayMessageUnionReader::CompactBlock(item)
6744 }
6745}
6746impl<'r> ::core::convert::From<RelayTransactionsReader<'r>> for RelayMessageUnionReader<'r> {
6747 fn from(item: RelayTransactionsReader<'r>) -> Self {
6748 RelayMessageUnionReader::RelayTransactions(item)
6749 }
6750}
6751impl<'r> ::core::convert::From<RelayTransactionHashesReader<'r>> for RelayMessageUnionReader<'r> {
6752 fn from(item: RelayTransactionHashesReader<'r>) -> Self {
6753 RelayMessageUnionReader::RelayTransactionHashes(item)
6754 }
6755}
6756impl<'r> ::core::convert::From<GetRelayTransactionsReader<'r>> for RelayMessageUnionReader<'r> {
6757 fn from(item: GetRelayTransactionsReader<'r>) -> Self {
6758 RelayMessageUnionReader::GetRelayTransactions(item)
6759 }
6760}
6761impl<'r> ::core::convert::From<GetBlockTransactionsReader<'r>> for RelayMessageUnionReader<'r> {
6762 fn from(item: GetBlockTransactionsReader<'r>) -> Self {
6763 RelayMessageUnionReader::GetBlockTransactions(item)
6764 }
6765}
6766impl<'r> ::core::convert::From<BlockTransactionsReader<'r>> for RelayMessageUnionReader<'r> {
6767 fn from(item: BlockTransactionsReader<'r>) -> Self {
6768 RelayMessageUnionReader::BlockTransactions(item)
6769 }
6770}
6771impl<'r> ::core::convert::From<GetBlockProposalReader<'r>> for RelayMessageUnionReader<'r> {
6772 fn from(item: GetBlockProposalReader<'r>) -> Self {
6773 RelayMessageUnionReader::GetBlockProposal(item)
6774 }
6775}
6776impl<'r> ::core::convert::From<BlockProposalReader<'r>> for RelayMessageUnionReader<'r> {
6777 fn from(item: BlockProposalReader<'r>) -> Self {
6778 RelayMessageUnionReader::BlockProposal(item)
6779 }
6780}
6781impl RelayMessageUnion {
6782 pub const NAME: &'static str = "RelayMessageUnion";
6783 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
6784 match self {
6785 RelayMessageUnion::CompactBlock(item) => item.as_bytes(),
6786 RelayMessageUnion::RelayTransactions(item) => item.as_bytes(),
6787 RelayMessageUnion::RelayTransactionHashes(item) => item.as_bytes(),
6788 RelayMessageUnion::GetRelayTransactions(item) => item.as_bytes(),
6789 RelayMessageUnion::GetBlockTransactions(item) => item.as_bytes(),
6790 RelayMessageUnion::BlockTransactions(item) => item.as_bytes(),
6791 RelayMessageUnion::GetBlockProposal(item) => item.as_bytes(),
6792 RelayMessageUnion::BlockProposal(item) => item.as_bytes(),
6793 }
6794 }
6795 pub fn as_slice(&self) -> &[u8] {
6796 match self {
6797 RelayMessageUnion::CompactBlock(item) => item.as_slice(),
6798 RelayMessageUnion::RelayTransactions(item) => item.as_slice(),
6799 RelayMessageUnion::RelayTransactionHashes(item) => item.as_slice(),
6800 RelayMessageUnion::GetRelayTransactions(item) => item.as_slice(),
6801 RelayMessageUnion::GetBlockTransactions(item) => item.as_slice(),
6802 RelayMessageUnion::BlockTransactions(item) => item.as_slice(),
6803 RelayMessageUnion::GetBlockProposal(item) => item.as_slice(),
6804 RelayMessageUnion::BlockProposal(item) => item.as_slice(),
6805 }
6806 }
6807 pub fn item_id(&self) -> molecule::Number {
6808 match self {
6809 RelayMessageUnion::CompactBlock(_) => 0,
6810 RelayMessageUnion::RelayTransactions(_) => 1,
6811 RelayMessageUnion::RelayTransactionHashes(_) => 2,
6812 RelayMessageUnion::GetRelayTransactions(_) => 3,
6813 RelayMessageUnion::GetBlockTransactions(_) => 4,
6814 RelayMessageUnion::BlockTransactions(_) => 5,
6815 RelayMessageUnion::GetBlockProposal(_) => 6,
6816 RelayMessageUnion::BlockProposal(_) => 7,
6817 }
6818 }
6819 pub fn item_name(&self) -> &str {
6820 match self {
6821 RelayMessageUnion::CompactBlock(_) => "CompactBlock",
6822 RelayMessageUnion::RelayTransactions(_) => "RelayTransactions",
6823 RelayMessageUnion::RelayTransactionHashes(_) => "RelayTransactionHashes",
6824 RelayMessageUnion::GetRelayTransactions(_) => "GetRelayTransactions",
6825 RelayMessageUnion::GetBlockTransactions(_) => "GetBlockTransactions",
6826 RelayMessageUnion::BlockTransactions(_) => "BlockTransactions",
6827 RelayMessageUnion::GetBlockProposal(_) => "GetBlockProposal",
6828 RelayMessageUnion::BlockProposal(_) => "BlockProposal",
6829 }
6830 }
6831 pub fn as_reader<'r>(&'r self) -> RelayMessageUnionReader<'r> {
6832 match self {
6833 RelayMessageUnion::CompactBlock(item) => item.as_reader().into(),
6834 RelayMessageUnion::RelayTransactions(item) => item.as_reader().into(),
6835 RelayMessageUnion::RelayTransactionHashes(item) => item.as_reader().into(),
6836 RelayMessageUnion::GetRelayTransactions(item) => item.as_reader().into(),
6837 RelayMessageUnion::GetBlockTransactions(item) => item.as_reader().into(),
6838 RelayMessageUnion::BlockTransactions(item) => item.as_reader().into(),
6839 RelayMessageUnion::GetBlockProposal(item) => item.as_reader().into(),
6840 RelayMessageUnion::BlockProposal(item) => item.as_reader().into(),
6841 }
6842 }
6843}
6844impl<'r> RelayMessageUnionReader<'r> {
6845 pub const NAME: &'r str = "RelayMessageUnionReader";
6846 pub fn as_slice(&self) -> &'r [u8] {
6847 match self {
6848 RelayMessageUnionReader::CompactBlock(item) => item.as_slice(),
6849 RelayMessageUnionReader::RelayTransactions(item) => item.as_slice(),
6850 RelayMessageUnionReader::RelayTransactionHashes(item) => item.as_slice(),
6851 RelayMessageUnionReader::GetRelayTransactions(item) => item.as_slice(),
6852 RelayMessageUnionReader::GetBlockTransactions(item) => item.as_slice(),
6853 RelayMessageUnionReader::BlockTransactions(item) => item.as_slice(),
6854 RelayMessageUnionReader::GetBlockProposal(item) => item.as_slice(),
6855 RelayMessageUnionReader::BlockProposal(item) => item.as_slice(),
6856 }
6857 }
6858 pub fn item_id(&self) -> molecule::Number {
6859 match self {
6860 RelayMessageUnionReader::CompactBlock(_) => 0,
6861 RelayMessageUnionReader::RelayTransactions(_) => 1,
6862 RelayMessageUnionReader::RelayTransactionHashes(_) => 2,
6863 RelayMessageUnionReader::GetRelayTransactions(_) => 3,
6864 RelayMessageUnionReader::GetBlockTransactions(_) => 4,
6865 RelayMessageUnionReader::BlockTransactions(_) => 5,
6866 RelayMessageUnionReader::GetBlockProposal(_) => 6,
6867 RelayMessageUnionReader::BlockProposal(_) => 7,
6868 }
6869 }
6870 pub fn item_name(&self) -> &str {
6871 match self {
6872 RelayMessageUnionReader::CompactBlock(_) => "CompactBlock",
6873 RelayMessageUnionReader::RelayTransactions(_) => "RelayTransactions",
6874 RelayMessageUnionReader::RelayTransactionHashes(_) => "RelayTransactionHashes",
6875 RelayMessageUnionReader::GetRelayTransactions(_) => "GetRelayTransactions",
6876 RelayMessageUnionReader::GetBlockTransactions(_) => "GetBlockTransactions",
6877 RelayMessageUnionReader::BlockTransactions(_) => "BlockTransactions",
6878 RelayMessageUnionReader::GetBlockProposal(_) => "GetBlockProposal",
6879 RelayMessageUnionReader::BlockProposal(_) => "BlockProposal",
6880 }
6881 }
6882}
6883impl From<CompactBlock> for RelayMessage {
6884 fn from(value: CompactBlock) -> Self {
6885 Self::new_builder().set(value).build()
6886 }
6887}
6888impl From<RelayTransactions> for RelayMessage {
6889 fn from(value: RelayTransactions) -> Self {
6890 Self::new_builder().set(value).build()
6891 }
6892}
6893impl From<RelayTransactionHashes> for RelayMessage {
6894 fn from(value: RelayTransactionHashes) -> Self {
6895 Self::new_builder().set(value).build()
6896 }
6897}
6898impl From<GetRelayTransactions> for RelayMessage {
6899 fn from(value: GetRelayTransactions) -> Self {
6900 Self::new_builder().set(value).build()
6901 }
6902}
6903impl From<GetBlockTransactions> for RelayMessage {
6904 fn from(value: GetBlockTransactions) -> Self {
6905 Self::new_builder().set(value).build()
6906 }
6907}
6908impl From<BlockTransactions> for RelayMessage {
6909 fn from(value: BlockTransactions) -> Self {
6910 Self::new_builder().set(value).build()
6911 }
6912}
6913impl From<GetBlockProposal> for RelayMessage {
6914 fn from(value: GetBlockProposal) -> Self {
6915 Self::new_builder().set(value).build()
6916 }
6917}
6918impl From<BlockProposal> for RelayMessage {
6919 fn from(value: BlockProposal) -> Self {
6920 Self::new_builder().set(value).build()
6921 }
6922}
6923#[derive(Clone)]
6924pub struct CompactBlock(molecule::bytes::Bytes);
6925impl ::core::fmt::LowerHex for CompactBlock {
6926 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6927 use molecule::hex_string;
6928 if f.alternate() {
6929 write!(f, "0x")?;
6930 }
6931 write!(f, "{}", hex_string(self.as_slice()))
6932 }
6933}
6934impl ::core::fmt::Debug for CompactBlock {
6935 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6936 write!(f, "{}({:#x})", Self::NAME, self)
6937 }
6938}
6939impl ::core::fmt::Display for CompactBlock {
6940 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6941 write!(f, "{} {{ ", Self::NAME)?;
6942 write!(f, "{}: {}", "header", self.header())?;
6943 write!(f, ", {}: {}", "short_ids", self.short_ids())?;
6944 write!(
6945 f,
6946 ", {}: {}",
6947 "prefilled_transactions",
6948 self.prefilled_transactions()
6949 )?;
6950 write!(f, ", {}: {}", "uncles", self.uncles())?;
6951 write!(f, ", {}: {}", "proposals", self.proposals())?;
6952 let extra_count = self.count_extra_fields();
6953 if extra_count != 0 {
6954 write!(f, ", .. ({} fields)", extra_count)?;
6955 }
6956 write!(f, " }}")
6957 }
6958}
6959impl ::core::default::Default for CompactBlock {
6960 fn default() -> Self {
6961 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
6962 CompactBlock::new_unchecked(v)
6963 }
6964}
6965impl CompactBlock {
6966 const DEFAULT_VALUE: [u8; 248] = [
6967 248, 0, 0, 0, 24, 0, 0, 0, 232, 0, 0, 0, 236, 0, 0, 0, 240, 0, 0, 0, 244, 0, 0, 0, 0, 0, 0,
6968 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6969 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6970 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6971 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6972 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6973 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6974 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
6975 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6976 ];
6977 pub const FIELD_COUNT: usize = 5;
6978 pub fn total_size(&self) -> usize {
6979 molecule::unpack_number(self.as_slice()) as usize
6980 }
6981 pub fn field_count(&self) -> usize {
6982 if self.total_size() == molecule::NUMBER_SIZE {
6983 0
6984 } else {
6985 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6986 }
6987 }
6988 pub fn count_extra_fields(&self) -> usize {
6989 self.field_count() - Self::FIELD_COUNT
6990 }
6991 pub fn has_extra_fields(&self) -> bool {
6992 Self::FIELD_COUNT != self.field_count()
6993 }
6994 pub fn header(&self) -> Header {
6995 let slice = self.as_slice();
6996 let start = molecule::unpack_number(&slice[4..]) as usize;
6997 let end = molecule::unpack_number(&slice[8..]) as usize;
6998 Header::new_unchecked(self.0.slice(start..end))
6999 }
7000 pub fn short_ids(&self) -> ProposalShortIdVec {
7001 let slice = self.as_slice();
7002 let start = molecule::unpack_number(&slice[8..]) as usize;
7003 let end = molecule::unpack_number(&slice[12..]) as usize;
7004 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
7005 }
7006 pub fn prefilled_transactions(&self) -> IndexTransactionVec {
7007 let slice = self.as_slice();
7008 let start = molecule::unpack_number(&slice[12..]) as usize;
7009 let end = molecule::unpack_number(&slice[16..]) as usize;
7010 IndexTransactionVec::new_unchecked(self.0.slice(start..end))
7011 }
7012 pub fn uncles(&self) -> Byte32Vec {
7013 let slice = self.as_slice();
7014 let start = molecule::unpack_number(&slice[16..]) as usize;
7015 let end = molecule::unpack_number(&slice[20..]) as usize;
7016 Byte32Vec::new_unchecked(self.0.slice(start..end))
7017 }
7018 pub fn proposals(&self) -> ProposalShortIdVec {
7019 let slice = self.as_slice();
7020 let start = molecule::unpack_number(&slice[20..]) as usize;
7021 if self.has_extra_fields() {
7022 let end = molecule::unpack_number(&slice[24..]) as usize;
7023 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
7024 } else {
7025 ProposalShortIdVec::new_unchecked(self.0.slice(start..))
7026 }
7027 }
7028 pub fn as_reader<'r>(&'r self) -> CompactBlockReader<'r> {
7029 CompactBlockReader::new_unchecked(self.as_slice())
7030 }
7031}
7032impl molecule::prelude::Entity for CompactBlock {
7033 type Builder = CompactBlockBuilder;
7034 const NAME: &'static str = "CompactBlock";
7035 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7036 CompactBlock(data)
7037 }
7038 fn as_bytes(&self) -> molecule::bytes::Bytes {
7039 self.0.clone()
7040 }
7041 fn as_slice(&self) -> &[u8] {
7042 &self.0[..]
7043 }
7044 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7045 CompactBlockReader::from_slice(slice).map(|reader| reader.to_entity())
7046 }
7047 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7048 CompactBlockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7049 }
7050 fn new_builder() -> Self::Builder {
7051 ::core::default::Default::default()
7052 }
7053 fn as_builder(self) -> Self::Builder {
7054 Self::new_builder()
7055 .header(self.header())
7056 .short_ids(self.short_ids())
7057 .prefilled_transactions(self.prefilled_transactions())
7058 .uncles(self.uncles())
7059 .proposals(self.proposals())
7060 }
7061}
7062#[derive(Clone, Copy)]
7063pub struct CompactBlockReader<'r>(&'r [u8]);
7064impl<'r> ::core::fmt::LowerHex for CompactBlockReader<'r> {
7065 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7066 use molecule::hex_string;
7067 if f.alternate() {
7068 write!(f, "0x")?;
7069 }
7070 write!(f, "{}", hex_string(self.as_slice()))
7071 }
7072}
7073impl<'r> ::core::fmt::Debug for CompactBlockReader<'r> {
7074 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7075 write!(f, "{}({:#x})", Self::NAME, self)
7076 }
7077}
7078impl<'r> ::core::fmt::Display for CompactBlockReader<'r> {
7079 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7080 write!(f, "{} {{ ", Self::NAME)?;
7081 write!(f, "{}: {}", "header", self.header())?;
7082 write!(f, ", {}: {}", "short_ids", self.short_ids())?;
7083 write!(
7084 f,
7085 ", {}: {}",
7086 "prefilled_transactions",
7087 self.prefilled_transactions()
7088 )?;
7089 write!(f, ", {}: {}", "uncles", self.uncles())?;
7090 write!(f, ", {}: {}", "proposals", self.proposals())?;
7091 let extra_count = self.count_extra_fields();
7092 if extra_count != 0 {
7093 write!(f, ", .. ({} fields)", extra_count)?;
7094 }
7095 write!(f, " }}")
7096 }
7097}
7098impl<'r> CompactBlockReader<'r> {
7099 pub const FIELD_COUNT: usize = 5;
7100 pub fn total_size(&self) -> usize {
7101 molecule::unpack_number(self.as_slice()) as usize
7102 }
7103 pub fn field_count(&self) -> usize {
7104 if self.total_size() == molecule::NUMBER_SIZE {
7105 0
7106 } else {
7107 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7108 }
7109 }
7110 pub fn count_extra_fields(&self) -> usize {
7111 self.field_count() - Self::FIELD_COUNT
7112 }
7113 pub fn has_extra_fields(&self) -> bool {
7114 Self::FIELD_COUNT != self.field_count()
7115 }
7116 pub fn header(&self) -> HeaderReader<'r> {
7117 let slice = self.as_slice();
7118 let start = molecule::unpack_number(&slice[4..]) as usize;
7119 let end = molecule::unpack_number(&slice[8..]) as usize;
7120 HeaderReader::new_unchecked(&self.as_slice()[start..end])
7121 }
7122 pub fn short_ids(&self) -> ProposalShortIdVecReader<'r> {
7123 let slice = self.as_slice();
7124 let start = molecule::unpack_number(&slice[8..]) as usize;
7125 let end = molecule::unpack_number(&slice[12..]) as usize;
7126 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
7127 }
7128 pub fn prefilled_transactions(&self) -> IndexTransactionVecReader<'r> {
7129 let slice = self.as_slice();
7130 let start = molecule::unpack_number(&slice[12..]) as usize;
7131 let end = molecule::unpack_number(&slice[16..]) as usize;
7132 IndexTransactionVecReader::new_unchecked(&self.as_slice()[start..end])
7133 }
7134 pub fn uncles(&self) -> Byte32VecReader<'r> {
7135 let slice = self.as_slice();
7136 let start = molecule::unpack_number(&slice[16..]) as usize;
7137 let end = molecule::unpack_number(&slice[20..]) as usize;
7138 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
7139 }
7140 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
7141 let slice = self.as_slice();
7142 let start = molecule::unpack_number(&slice[20..]) as usize;
7143 if self.has_extra_fields() {
7144 let end = molecule::unpack_number(&slice[24..]) as usize;
7145 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
7146 } else {
7147 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..])
7148 }
7149 }
7150}
7151impl<'r> molecule::prelude::Reader<'r> for CompactBlockReader<'r> {
7152 type Entity = CompactBlock;
7153 const NAME: &'static str = "CompactBlockReader";
7154 fn to_entity(&self) -> Self::Entity {
7155 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7156 }
7157 fn new_unchecked(slice: &'r [u8]) -> Self {
7158 CompactBlockReader(slice)
7159 }
7160 fn as_slice(&self) -> &'r [u8] {
7161 self.0
7162 }
7163 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
7164 use molecule::verification_error as ve;
7165 let slice_len = slice.len();
7166 if slice_len < molecule::NUMBER_SIZE {
7167 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
7168 }
7169 let total_size = molecule::unpack_number(slice) as usize;
7170 if slice_len != total_size {
7171 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7172 }
7173 if slice_len < molecule::NUMBER_SIZE * 2 {
7174 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
7175 }
7176 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7177 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7178 return ve!(Self, OffsetsNotMatch);
7179 }
7180 if slice_len < offset_first {
7181 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7182 }
7183 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
7184 if field_count < Self::FIELD_COUNT {
7185 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7186 } else if !compatible && field_count > Self::FIELD_COUNT {
7187 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7188 };
7189 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7190 .chunks_exact(molecule::NUMBER_SIZE)
7191 .map(|x| molecule::unpack_number(x) as usize)
7192 .collect();
7193 offsets.push(total_size);
7194 if offsets.windows(2).any(|i| i[0] > i[1]) {
7195 return ve!(Self, OffsetsNotMatch);
7196 }
7197 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
7198 ProposalShortIdVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
7199 IndexTransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
7200 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
7201 ProposalShortIdVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
7202 Ok(())
7203 }
7204}
7205#[derive(Clone, Debug, Default)]
7206pub struct CompactBlockBuilder {
7207 pub(crate) header: Header,
7208 pub(crate) short_ids: ProposalShortIdVec,
7209 pub(crate) prefilled_transactions: IndexTransactionVec,
7210 pub(crate) uncles: Byte32Vec,
7211 pub(crate) proposals: ProposalShortIdVec,
7212}
7213impl CompactBlockBuilder {
7214 pub const FIELD_COUNT: usize = 5;
7215 pub fn header<T>(mut self, v: T) -> Self
7216 where
7217 T: ::core::convert::Into<Header>,
7218 {
7219 self.header = v.into();
7220 self
7221 }
7222 pub fn short_ids<T>(mut self, v: T) -> Self
7223 where
7224 T: ::core::convert::Into<ProposalShortIdVec>,
7225 {
7226 self.short_ids = v.into();
7227 self
7228 }
7229 pub fn prefilled_transactions<T>(mut self, v: T) -> Self
7230 where
7231 T: ::core::convert::Into<IndexTransactionVec>,
7232 {
7233 self.prefilled_transactions = v.into();
7234 self
7235 }
7236 pub fn uncles<T>(mut self, v: T) -> Self
7237 where
7238 T: ::core::convert::Into<Byte32Vec>,
7239 {
7240 self.uncles = v.into();
7241 self
7242 }
7243 pub fn proposals<T>(mut self, v: T) -> Self
7244 where
7245 T: ::core::convert::Into<ProposalShortIdVec>,
7246 {
7247 self.proposals = v.into();
7248 self
7249 }
7250}
7251impl molecule::prelude::Builder for CompactBlockBuilder {
7252 type Entity = CompactBlock;
7253 const NAME: &'static str = "CompactBlockBuilder";
7254 fn expected_length(&self) -> usize {
7255 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
7256 + self.header.as_slice().len()
7257 + self.short_ids.as_slice().len()
7258 + self.prefilled_transactions.as_slice().len()
7259 + self.uncles.as_slice().len()
7260 + self.proposals.as_slice().len()
7261 }
7262 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7263 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
7264 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
7265 offsets.push(total_size);
7266 total_size += self.header.as_slice().len();
7267 offsets.push(total_size);
7268 total_size += self.short_ids.as_slice().len();
7269 offsets.push(total_size);
7270 total_size += self.prefilled_transactions.as_slice().len();
7271 offsets.push(total_size);
7272 total_size += self.uncles.as_slice().len();
7273 offsets.push(total_size);
7274 total_size += self.proposals.as_slice().len();
7275 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
7276 for offset in offsets.into_iter() {
7277 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
7278 }
7279 writer.write_all(self.header.as_slice())?;
7280 writer.write_all(self.short_ids.as_slice())?;
7281 writer.write_all(self.prefilled_transactions.as_slice())?;
7282 writer.write_all(self.uncles.as_slice())?;
7283 writer.write_all(self.proposals.as_slice())?;
7284 Ok(())
7285 }
7286 fn build(&self) -> Self::Entity {
7287 let mut inner = Vec::with_capacity(self.expected_length());
7288 self.write(&mut inner)
7289 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7290 CompactBlock::new_unchecked(inner.into())
7291 }
7292}
7293#[derive(Clone)]
7294pub struct CompactBlockV1(molecule::bytes::Bytes);
7295impl ::core::fmt::LowerHex for CompactBlockV1 {
7296 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7297 use molecule::hex_string;
7298 if f.alternate() {
7299 write!(f, "0x")?;
7300 }
7301 write!(f, "{}", hex_string(self.as_slice()))
7302 }
7303}
7304impl ::core::fmt::Debug for CompactBlockV1 {
7305 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7306 write!(f, "{}({:#x})", Self::NAME, self)
7307 }
7308}
7309impl ::core::fmt::Display for CompactBlockV1 {
7310 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7311 write!(f, "{} {{ ", Self::NAME)?;
7312 write!(f, "{}: {}", "header", self.header())?;
7313 write!(f, ", {}: {}", "short_ids", self.short_ids())?;
7314 write!(
7315 f,
7316 ", {}: {}",
7317 "prefilled_transactions",
7318 self.prefilled_transactions()
7319 )?;
7320 write!(f, ", {}: {}", "uncles", self.uncles())?;
7321 write!(f, ", {}: {}", "proposals", self.proposals())?;
7322 write!(f, ", {}: {}", "extension", self.extension())?;
7323 let extra_count = self.count_extra_fields();
7324 if extra_count != 0 {
7325 write!(f, ", .. ({} fields)", extra_count)?;
7326 }
7327 write!(f, " }}")
7328 }
7329}
7330impl ::core::default::Default for CompactBlockV1 {
7331 fn default() -> Self {
7332 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7333 CompactBlockV1::new_unchecked(v)
7334 }
7335}
7336impl CompactBlockV1 {
7337 const DEFAULT_VALUE: [u8; 256] = [
7338 0, 1, 0, 0, 28, 0, 0, 0, 236, 0, 0, 0, 240, 0, 0, 0, 244, 0, 0, 0, 248, 0, 0, 0, 252, 0, 0,
7339 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7340 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7341 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7342 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7343 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7344 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7345 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7346 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7347 ];
7348 pub const FIELD_COUNT: usize = 6;
7349 pub fn total_size(&self) -> usize {
7350 molecule::unpack_number(self.as_slice()) as usize
7351 }
7352 pub fn field_count(&self) -> usize {
7353 if self.total_size() == molecule::NUMBER_SIZE {
7354 0
7355 } else {
7356 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7357 }
7358 }
7359 pub fn count_extra_fields(&self) -> usize {
7360 self.field_count() - Self::FIELD_COUNT
7361 }
7362 pub fn has_extra_fields(&self) -> bool {
7363 Self::FIELD_COUNT != self.field_count()
7364 }
7365 pub fn header(&self) -> Header {
7366 let slice = self.as_slice();
7367 let start = molecule::unpack_number(&slice[4..]) as usize;
7368 let end = molecule::unpack_number(&slice[8..]) as usize;
7369 Header::new_unchecked(self.0.slice(start..end))
7370 }
7371 pub fn short_ids(&self) -> ProposalShortIdVec {
7372 let slice = self.as_slice();
7373 let start = molecule::unpack_number(&slice[8..]) as usize;
7374 let end = molecule::unpack_number(&slice[12..]) as usize;
7375 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
7376 }
7377 pub fn prefilled_transactions(&self) -> IndexTransactionVec {
7378 let slice = self.as_slice();
7379 let start = molecule::unpack_number(&slice[12..]) as usize;
7380 let end = molecule::unpack_number(&slice[16..]) as usize;
7381 IndexTransactionVec::new_unchecked(self.0.slice(start..end))
7382 }
7383 pub fn uncles(&self) -> Byte32Vec {
7384 let slice = self.as_slice();
7385 let start = molecule::unpack_number(&slice[16..]) as usize;
7386 let end = molecule::unpack_number(&slice[20..]) as usize;
7387 Byte32Vec::new_unchecked(self.0.slice(start..end))
7388 }
7389 pub fn proposals(&self) -> ProposalShortIdVec {
7390 let slice = self.as_slice();
7391 let start = molecule::unpack_number(&slice[20..]) as usize;
7392 let end = molecule::unpack_number(&slice[24..]) as usize;
7393 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
7394 }
7395 pub fn extension(&self) -> Bytes {
7396 let slice = self.as_slice();
7397 let start = molecule::unpack_number(&slice[24..]) as usize;
7398 if self.has_extra_fields() {
7399 let end = molecule::unpack_number(&slice[28..]) as usize;
7400 Bytes::new_unchecked(self.0.slice(start..end))
7401 } else {
7402 Bytes::new_unchecked(self.0.slice(start..))
7403 }
7404 }
7405 pub fn as_reader<'r>(&'r self) -> CompactBlockV1Reader<'r> {
7406 CompactBlockV1Reader::new_unchecked(self.as_slice())
7407 }
7408}
7409impl molecule::prelude::Entity for CompactBlockV1 {
7410 type Builder = CompactBlockV1Builder;
7411 const NAME: &'static str = "CompactBlockV1";
7412 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7413 CompactBlockV1(data)
7414 }
7415 fn as_bytes(&self) -> molecule::bytes::Bytes {
7416 self.0.clone()
7417 }
7418 fn as_slice(&self) -> &[u8] {
7419 &self.0[..]
7420 }
7421 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7422 CompactBlockV1Reader::from_slice(slice).map(|reader| reader.to_entity())
7423 }
7424 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7425 CompactBlockV1Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7426 }
7427 fn new_builder() -> Self::Builder {
7428 ::core::default::Default::default()
7429 }
7430 fn as_builder(self) -> Self::Builder {
7431 Self::new_builder()
7432 .header(self.header())
7433 .short_ids(self.short_ids())
7434 .prefilled_transactions(self.prefilled_transactions())
7435 .uncles(self.uncles())
7436 .proposals(self.proposals())
7437 .extension(self.extension())
7438 }
7439}
7440#[derive(Clone, Copy)]
7441pub struct CompactBlockV1Reader<'r>(&'r [u8]);
7442impl<'r> ::core::fmt::LowerHex for CompactBlockV1Reader<'r> {
7443 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7444 use molecule::hex_string;
7445 if f.alternate() {
7446 write!(f, "0x")?;
7447 }
7448 write!(f, "{}", hex_string(self.as_slice()))
7449 }
7450}
7451impl<'r> ::core::fmt::Debug for CompactBlockV1Reader<'r> {
7452 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7453 write!(f, "{}({:#x})", Self::NAME, self)
7454 }
7455}
7456impl<'r> ::core::fmt::Display for CompactBlockV1Reader<'r> {
7457 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7458 write!(f, "{} {{ ", Self::NAME)?;
7459 write!(f, "{}: {}", "header", self.header())?;
7460 write!(f, ", {}: {}", "short_ids", self.short_ids())?;
7461 write!(
7462 f,
7463 ", {}: {}",
7464 "prefilled_transactions",
7465 self.prefilled_transactions()
7466 )?;
7467 write!(f, ", {}: {}", "uncles", self.uncles())?;
7468 write!(f, ", {}: {}", "proposals", self.proposals())?;
7469 write!(f, ", {}: {}", "extension", self.extension())?;
7470 let extra_count = self.count_extra_fields();
7471 if extra_count != 0 {
7472 write!(f, ", .. ({} fields)", extra_count)?;
7473 }
7474 write!(f, " }}")
7475 }
7476}
7477impl<'r> CompactBlockV1Reader<'r> {
7478 pub const FIELD_COUNT: usize = 6;
7479 pub fn total_size(&self) -> usize {
7480 molecule::unpack_number(self.as_slice()) as usize
7481 }
7482 pub fn field_count(&self) -> usize {
7483 if self.total_size() == molecule::NUMBER_SIZE {
7484 0
7485 } else {
7486 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7487 }
7488 }
7489 pub fn count_extra_fields(&self) -> usize {
7490 self.field_count() - Self::FIELD_COUNT
7491 }
7492 pub fn has_extra_fields(&self) -> bool {
7493 Self::FIELD_COUNT != self.field_count()
7494 }
7495 pub fn header(&self) -> HeaderReader<'r> {
7496 let slice = self.as_slice();
7497 let start = molecule::unpack_number(&slice[4..]) as usize;
7498 let end = molecule::unpack_number(&slice[8..]) as usize;
7499 HeaderReader::new_unchecked(&self.as_slice()[start..end])
7500 }
7501 pub fn short_ids(&self) -> ProposalShortIdVecReader<'r> {
7502 let slice = self.as_slice();
7503 let start = molecule::unpack_number(&slice[8..]) as usize;
7504 let end = molecule::unpack_number(&slice[12..]) as usize;
7505 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
7506 }
7507 pub fn prefilled_transactions(&self) -> IndexTransactionVecReader<'r> {
7508 let slice = self.as_slice();
7509 let start = molecule::unpack_number(&slice[12..]) as usize;
7510 let end = molecule::unpack_number(&slice[16..]) as usize;
7511 IndexTransactionVecReader::new_unchecked(&self.as_slice()[start..end])
7512 }
7513 pub fn uncles(&self) -> Byte32VecReader<'r> {
7514 let slice = self.as_slice();
7515 let start = molecule::unpack_number(&slice[16..]) as usize;
7516 let end = molecule::unpack_number(&slice[20..]) as usize;
7517 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
7518 }
7519 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
7520 let slice = self.as_slice();
7521 let start = molecule::unpack_number(&slice[20..]) as usize;
7522 let end = molecule::unpack_number(&slice[24..]) as usize;
7523 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
7524 }
7525 pub fn extension(&self) -> BytesReader<'r> {
7526 let slice = self.as_slice();
7527 let start = molecule::unpack_number(&slice[24..]) as usize;
7528 if self.has_extra_fields() {
7529 let end = molecule::unpack_number(&slice[28..]) as usize;
7530 BytesReader::new_unchecked(&self.as_slice()[start..end])
7531 } else {
7532 BytesReader::new_unchecked(&self.as_slice()[start..])
7533 }
7534 }
7535}
7536impl<'r> molecule::prelude::Reader<'r> for CompactBlockV1Reader<'r> {
7537 type Entity = CompactBlockV1;
7538 const NAME: &'static str = "CompactBlockV1Reader";
7539 fn to_entity(&self) -> Self::Entity {
7540 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7541 }
7542 fn new_unchecked(slice: &'r [u8]) -> Self {
7543 CompactBlockV1Reader(slice)
7544 }
7545 fn as_slice(&self) -> &'r [u8] {
7546 self.0
7547 }
7548 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
7549 use molecule::verification_error as ve;
7550 let slice_len = slice.len();
7551 if slice_len < molecule::NUMBER_SIZE {
7552 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
7553 }
7554 let total_size = molecule::unpack_number(slice) as usize;
7555 if slice_len != total_size {
7556 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7557 }
7558 if slice_len < molecule::NUMBER_SIZE * 2 {
7559 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
7560 }
7561 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7562 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7563 return ve!(Self, OffsetsNotMatch);
7564 }
7565 if slice_len < offset_first {
7566 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7567 }
7568 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
7569 if field_count < Self::FIELD_COUNT {
7570 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7571 } else if !compatible && field_count > Self::FIELD_COUNT {
7572 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7573 };
7574 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7575 .chunks_exact(molecule::NUMBER_SIZE)
7576 .map(|x| molecule::unpack_number(x) as usize)
7577 .collect();
7578 offsets.push(total_size);
7579 if offsets.windows(2).any(|i| i[0] > i[1]) {
7580 return ve!(Self, OffsetsNotMatch);
7581 }
7582 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
7583 ProposalShortIdVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
7584 IndexTransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
7585 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
7586 ProposalShortIdVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
7587 BytesReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
7588 Ok(())
7589 }
7590}
7591#[derive(Clone, Debug, Default)]
7592pub struct CompactBlockV1Builder {
7593 pub(crate) header: Header,
7594 pub(crate) short_ids: ProposalShortIdVec,
7595 pub(crate) prefilled_transactions: IndexTransactionVec,
7596 pub(crate) uncles: Byte32Vec,
7597 pub(crate) proposals: ProposalShortIdVec,
7598 pub(crate) extension: Bytes,
7599}
7600impl CompactBlockV1Builder {
7601 pub const FIELD_COUNT: usize = 6;
7602 pub fn header<T>(mut self, v: T) -> Self
7603 where
7604 T: ::core::convert::Into<Header>,
7605 {
7606 self.header = v.into();
7607 self
7608 }
7609 pub fn short_ids<T>(mut self, v: T) -> Self
7610 where
7611 T: ::core::convert::Into<ProposalShortIdVec>,
7612 {
7613 self.short_ids = v.into();
7614 self
7615 }
7616 pub fn prefilled_transactions<T>(mut self, v: T) -> Self
7617 where
7618 T: ::core::convert::Into<IndexTransactionVec>,
7619 {
7620 self.prefilled_transactions = v.into();
7621 self
7622 }
7623 pub fn uncles<T>(mut self, v: T) -> Self
7624 where
7625 T: ::core::convert::Into<Byte32Vec>,
7626 {
7627 self.uncles = v.into();
7628 self
7629 }
7630 pub fn proposals<T>(mut self, v: T) -> Self
7631 where
7632 T: ::core::convert::Into<ProposalShortIdVec>,
7633 {
7634 self.proposals = v.into();
7635 self
7636 }
7637 pub fn extension<T>(mut self, v: T) -> Self
7638 where
7639 T: ::core::convert::Into<Bytes>,
7640 {
7641 self.extension = v.into();
7642 self
7643 }
7644}
7645impl molecule::prelude::Builder for CompactBlockV1Builder {
7646 type Entity = CompactBlockV1;
7647 const NAME: &'static str = "CompactBlockV1Builder";
7648 fn expected_length(&self) -> usize {
7649 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
7650 + self.header.as_slice().len()
7651 + self.short_ids.as_slice().len()
7652 + self.prefilled_transactions.as_slice().len()
7653 + self.uncles.as_slice().len()
7654 + self.proposals.as_slice().len()
7655 + self.extension.as_slice().len()
7656 }
7657 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7658 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
7659 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
7660 offsets.push(total_size);
7661 total_size += self.header.as_slice().len();
7662 offsets.push(total_size);
7663 total_size += self.short_ids.as_slice().len();
7664 offsets.push(total_size);
7665 total_size += self.prefilled_transactions.as_slice().len();
7666 offsets.push(total_size);
7667 total_size += self.uncles.as_slice().len();
7668 offsets.push(total_size);
7669 total_size += self.proposals.as_slice().len();
7670 offsets.push(total_size);
7671 total_size += self.extension.as_slice().len();
7672 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
7673 for offset in offsets.into_iter() {
7674 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
7675 }
7676 writer.write_all(self.header.as_slice())?;
7677 writer.write_all(self.short_ids.as_slice())?;
7678 writer.write_all(self.prefilled_transactions.as_slice())?;
7679 writer.write_all(self.uncles.as_slice())?;
7680 writer.write_all(self.proposals.as_slice())?;
7681 writer.write_all(self.extension.as_slice())?;
7682 Ok(())
7683 }
7684 fn build(&self) -> Self::Entity {
7685 let mut inner = Vec::with_capacity(self.expected_length());
7686 self.write(&mut inner)
7687 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7688 CompactBlockV1::new_unchecked(inner.into())
7689 }
7690}
7691#[derive(Clone)]
7692pub struct RelayTransaction(molecule::bytes::Bytes);
7693impl ::core::fmt::LowerHex for RelayTransaction {
7694 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7695 use molecule::hex_string;
7696 if f.alternate() {
7697 write!(f, "0x")?;
7698 }
7699 write!(f, "{}", hex_string(self.as_slice()))
7700 }
7701}
7702impl ::core::fmt::Debug for RelayTransaction {
7703 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7704 write!(f, "{}({:#x})", Self::NAME, self)
7705 }
7706}
7707impl ::core::fmt::Display for RelayTransaction {
7708 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7709 write!(f, "{} {{ ", Self::NAME)?;
7710 write!(f, "{}: {}", "cycles", self.cycles())?;
7711 write!(f, ", {}: {}", "transaction", self.transaction())?;
7712 let extra_count = self.count_extra_fields();
7713 if extra_count != 0 {
7714 write!(f, ", .. ({} fields)", extra_count)?;
7715 }
7716 write!(f, " }}")
7717 }
7718}
7719impl ::core::default::Default for RelayTransaction {
7720 fn default() -> Self {
7721 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7722 RelayTransaction::new_unchecked(v)
7723 }
7724}
7725impl RelayTransaction {
7726 const DEFAULT_VALUE: [u8; 88] = [
7727 88, 0, 0, 0, 12, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 12, 0, 0, 0,
7728 64, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 44, 0, 0, 0,
7729 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0,
7730 0, 0,
7731 ];
7732 pub const FIELD_COUNT: usize = 2;
7733 pub fn total_size(&self) -> usize {
7734 molecule::unpack_number(self.as_slice()) as usize
7735 }
7736 pub fn field_count(&self) -> usize {
7737 if self.total_size() == molecule::NUMBER_SIZE {
7738 0
7739 } else {
7740 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7741 }
7742 }
7743 pub fn count_extra_fields(&self) -> usize {
7744 self.field_count() - Self::FIELD_COUNT
7745 }
7746 pub fn has_extra_fields(&self) -> bool {
7747 Self::FIELD_COUNT != self.field_count()
7748 }
7749 pub fn cycles(&self) -> Uint64 {
7750 let slice = self.as_slice();
7751 let start = molecule::unpack_number(&slice[4..]) as usize;
7752 let end = molecule::unpack_number(&slice[8..]) as usize;
7753 Uint64::new_unchecked(self.0.slice(start..end))
7754 }
7755 pub fn transaction(&self) -> Transaction {
7756 let slice = self.as_slice();
7757 let start = molecule::unpack_number(&slice[8..]) as usize;
7758 if self.has_extra_fields() {
7759 let end = molecule::unpack_number(&slice[12..]) as usize;
7760 Transaction::new_unchecked(self.0.slice(start..end))
7761 } else {
7762 Transaction::new_unchecked(self.0.slice(start..))
7763 }
7764 }
7765 pub fn as_reader<'r>(&'r self) -> RelayTransactionReader<'r> {
7766 RelayTransactionReader::new_unchecked(self.as_slice())
7767 }
7768}
7769impl molecule::prelude::Entity for RelayTransaction {
7770 type Builder = RelayTransactionBuilder;
7771 const NAME: &'static str = "RelayTransaction";
7772 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7773 RelayTransaction(data)
7774 }
7775 fn as_bytes(&self) -> molecule::bytes::Bytes {
7776 self.0.clone()
7777 }
7778 fn as_slice(&self) -> &[u8] {
7779 &self.0[..]
7780 }
7781 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7782 RelayTransactionReader::from_slice(slice).map(|reader| reader.to_entity())
7783 }
7784 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7785 RelayTransactionReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7786 }
7787 fn new_builder() -> Self::Builder {
7788 ::core::default::Default::default()
7789 }
7790 fn as_builder(self) -> Self::Builder {
7791 Self::new_builder()
7792 .cycles(self.cycles())
7793 .transaction(self.transaction())
7794 }
7795}
7796#[derive(Clone, Copy)]
7797pub struct RelayTransactionReader<'r>(&'r [u8]);
7798impl<'r> ::core::fmt::LowerHex for RelayTransactionReader<'r> {
7799 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7800 use molecule::hex_string;
7801 if f.alternate() {
7802 write!(f, "0x")?;
7803 }
7804 write!(f, "{}", hex_string(self.as_slice()))
7805 }
7806}
7807impl<'r> ::core::fmt::Debug for RelayTransactionReader<'r> {
7808 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7809 write!(f, "{}({:#x})", Self::NAME, self)
7810 }
7811}
7812impl<'r> ::core::fmt::Display for RelayTransactionReader<'r> {
7813 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7814 write!(f, "{} {{ ", Self::NAME)?;
7815 write!(f, "{}: {}", "cycles", self.cycles())?;
7816 write!(f, ", {}: {}", "transaction", self.transaction())?;
7817 let extra_count = self.count_extra_fields();
7818 if extra_count != 0 {
7819 write!(f, ", .. ({} fields)", extra_count)?;
7820 }
7821 write!(f, " }}")
7822 }
7823}
7824impl<'r> RelayTransactionReader<'r> {
7825 pub const FIELD_COUNT: usize = 2;
7826 pub fn total_size(&self) -> usize {
7827 molecule::unpack_number(self.as_slice()) as usize
7828 }
7829 pub fn field_count(&self) -> usize {
7830 if self.total_size() == molecule::NUMBER_SIZE {
7831 0
7832 } else {
7833 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7834 }
7835 }
7836 pub fn count_extra_fields(&self) -> usize {
7837 self.field_count() - Self::FIELD_COUNT
7838 }
7839 pub fn has_extra_fields(&self) -> bool {
7840 Self::FIELD_COUNT != self.field_count()
7841 }
7842 pub fn cycles(&self) -> Uint64Reader<'r> {
7843 let slice = self.as_slice();
7844 let start = molecule::unpack_number(&slice[4..]) as usize;
7845 let end = molecule::unpack_number(&slice[8..]) as usize;
7846 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
7847 }
7848 pub fn transaction(&self) -> TransactionReader<'r> {
7849 let slice = self.as_slice();
7850 let start = molecule::unpack_number(&slice[8..]) as usize;
7851 if self.has_extra_fields() {
7852 let end = molecule::unpack_number(&slice[12..]) as usize;
7853 TransactionReader::new_unchecked(&self.as_slice()[start..end])
7854 } else {
7855 TransactionReader::new_unchecked(&self.as_slice()[start..])
7856 }
7857 }
7858}
7859impl<'r> molecule::prelude::Reader<'r> for RelayTransactionReader<'r> {
7860 type Entity = RelayTransaction;
7861 const NAME: &'static str = "RelayTransactionReader";
7862 fn to_entity(&self) -> Self::Entity {
7863 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7864 }
7865 fn new_unchecked(slice: &'r [u8]) -> Self {
7866 RelayTransactionReader(slice)
7867 }
7868 fn as_slice(&self) -> &'r [u8] {
7869 self.0
7870 }
7871 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
7872 use molecule::verification_error as ve;
7873 let slice_len = slice.len();
7874 if slice_len < molecule::NUMBER_SIZE {
7875 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
7876 }
7877 let total_size = molecule::unpack_number(slice) as usize;
7878 if slice_len != total_size {
7879 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7880 }
7881 if slice_len < molecule::NUMBER_SIZE * 2 {
7882 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
7883 }
7884 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7885 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7886 return ve!(Self, OffsetsNotMatch);
7887 }
7888 if slice_len < offset_first {
7889 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7890 }
7891 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
7892 if field_count < Self::FIELD_COUNT {
7893 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7894 } else if !compatible && field_count > Self::FIELD_COUNT {
7895 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7896 };
7897 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7898 .chunks_exact(molecule::NUMBER_SIZE)
7899 .map(|x| molecule::unpack_number(x) as usize)
7900 .collect();
7901 offsets.push(total_size);
7902 if offsets.windows(2).any(|i| i[0] > i[1]) {
7903 return ve!(Self, OffsetsNotMatch);
7904 }
7905 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
7906 TransactionReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
7907 Ok(())
7908 }
7909}
7910#[derive(Clone, Debug, Default)]
7911pub struct RelayTransactionBuilder {
7912 pub(crate) cycles: Uint64,
7913 pub(crate) transaction: Transaction,
7914}
7915impl RelayTransactionBuilder {
7916 pub const FIELD_COUNT: usize = 2;
7917 pub fn cycles<T>(mut self, v: T) -> Self
7918 where
7919 T: ::core::convert::Into<Uint64>,
7920 {
7921 self.cycles = v.into();
7922 self
7923 }
7924 pub fn transaction<T>(mut self, v: T) -> Self
7925 where
7926 T: ::core::convert::Into<Transaction>,
7927 {
7928 self.transaction = v.into();
7929 self
7930 }
7931}
7932impl molecule::prelude::Builder for RelayTransactionBuilder {
7933 type Entity = RelayTransaction;
7934 const NAME: &'static str = "RelayTransactionBuilder";
7935 fn expected_length(&self) -> usize {
7936 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
7937 + self.cycles.as_slice().len()
7938 + self.transaction.as_slice().len()
7939 }
7940 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7941 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
7942 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
7943 offsets.push(total_size);
7944 total_size += self.cycles.as_slice().len();
7945 offsets.push(total_size);
7946 total_size += self.transaction.as_slice().len();
7947 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
7948 for offset in offsets.into_iter() {
7949 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
7950 }
7951 writer.write_all(self.cycles.as_slice())?;
7952 writer.write_all(self.transaction.as_slice())?;
7953 Ok(())
7954 }
7955 fn build(&self) -> Self::Entity {
7956 let mut inner = Vec::with_capacity(self.expected_length());
7957 self.write(&mut inner)
7958 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7959 RelayTransaction::new_unchecked(inner.into())
7960 }
7961}
7962#[derive(Clone)]
7963pub struct RelayTransactionVec(molecule::bytes::Bytes);
7964impl ::core::fmt::LowerHex for RelayTransactionVec {
7965 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7966 use molecule::hex_string;
7967 if f.alternate() {
7968 write!(f, "0x")?;
7969 }
7970 write!(f, "{}", hex_string(self.as_slice()))
7971 }
7972}
7973impl ::core::fmt::Debug for RelayTransactionVec {
7974 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7975 write!(f, "{}({:#x})", Self::NAME, self)
7976 }
7977}
7978impl ::core::fmt::Display for RelayTransactionVec {
7979 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7980 write!(f, "{} [", Self::NAME)?;
7981 for i in 0..self.len() {
7982 if i == 0 {
7983 write!(f, "{}", self.get_unchecked(i))?;
7984 } else {
7985 write!(f, ", {}", self.get_unchecked(i))?;
7986 }
7987 }
7988 write!(f, "]")
7989 }
7990}
7991impl ::core::default::Default for RelayTransactionVec {
7992 fn default() -> Self {
7993 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7994 RelayTransactionVec::new_unchecked(v)
7995 }
7996}
7997impl RelayTransactionVec {
7998 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
7999 pub fn total_size(&self) -> usize {
8000 molecule::unpack_number(self.as_slice()) as usize
8001 }
8002 pub fn item_count(&self) -> usize {
8003 if self.total_size() == molecule::NUMBER_SIZE {
8004 0
8005 } else {
8006 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8007 }
8008 }
8009 pub fn len(&self) -> usize {
8010 self.item_count()
8011 }
8012 pub fn is_empty(&self) -> bool {
8013 self.len() == 0
8014 }
8015 pub fn get(&self, idx: usize) -> Option<RelayTransaction> {
8016 if idx >= self.len() {
8017 None
8018 } else {
8019 Some(self.get_unchecked(idx))
8020 }
8021 }
8022 pub fn get_unchecked(&self, idx: usize) -> RelayTransaction {
8023 let slice = self.as_slice();
8024 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
8025 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
8026 if idx == self.len() - 1 {
8027 RelayTransaction::new_unchecked(self.0.slice(start..))
8028 } else {
8029 let end_idx = start_idx + molecule::NUMBER_SIZE;
8030 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
8031 RelayTransaction::new_unchecked(self.0.slice(start..end))
8032 }
8033 }
8034 pub fn as_reader<'r>(&'r self) -> RelayTransactionVecReader<'r> {
8035 RelayTransactionVecReader::new_unchecked(self.as_slice())
8036 }
8037}
8038impl molecule::prelude::Entity for RelayTransactionVec {
8039 type Builder = RelayTransactionVecBuilder;
8040 const NAME: &'static str = "RelayTransactionVec";
8041 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8042 RelayTransactionVec(data)
8043 }
8044 fn as_bytes(&self) -> molecule::bytes::Bytes {
8045 self.0.clone()
8046 }
8047 fn as_slice(&self) -> &[u8] {
8048 &self.0[..]
8049 }
8050 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8051 RelayTransactionVecReader::from_slice(slice).map(|reader| reader.to_entity())
8052 }
8053 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8054 RelayTransactionVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8055 }
8056 fn new_builder() -> Self::Builder {
8057 ::core::default::Default::default()
8058 }
8059 fn as_builder(self) -> Self::Builder {
8060 Self::new_builder().extend(self.into_iter())
8061 }
8062}
8063#[derive(Clone, Copy)]
8064pub struct RelayTransactionVecReader<'r>(&'r [u8]);
8065impl<'r> ::core::fmt::LowerHex for RelayTransactionVecReader<'r> {
8066 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8067 use molecule::hex_string;
8068 if f.alternate() {
8069 write!(f, "0x")?;
8070 }
8071 write!(f, "{}", hex_string(self.as_slice()))
8072 }
8073}
8074impl<'r> ::core::fmt::Debug for RelayTransactionVecReader<'r> {
8075 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8076 write!(f, "{}({:#x})", Self::NAME, self)
8077 }
8078}
8079impl<'r> ::core::fmt::Display for RelayTransactionVecReader<'r> {
8080 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8081 write!(f, "{} [", Self::NAME)?;
8082 for i in 0..self.len() {
8083 if i == 0 {
8084 write!(f, "{}", self.get_unchecked(i))?;
8085 } else {
8086 write!(f, ", {}", self.get_unchecked(i))?;
8087 }
8088 }
8089 write!(f, "]")
8090 }
8091}
8092impl<'r> RelayTransactionVecReader<'r> {
8093 pub fn total_size(&self) -> usize {
8094 molecule::unpack_number(self.as_slice()) as usize
8095 }
8096 pub fn item_count(&self) -> usize {
8097 if self.total_size() == molecule::NUMBER_SIZE {
8098 0
8099 } else {
8100 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8101 }
8102 }
8103 pub fn len(&self) -> usize {
8104 self.item_count()
8105 }
8106 pub fn is_empty(&self) -> bool {
8107 self.len() == 0
8108 }
8109 pub fn get(&self, idx: usize) -> Option<RelayTransactionReader<'r>> {
8110 if idx >= self.len() {
8111 None
8112 } else {
8113 Some(self.get_unchecked(idx))
8114 }
8115 }
8116 pub fn get_unchecked(&self, idx: usize) -> RelayTransactionReader<'r> {
8117 let slice = self.as_slice();
8118 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
8119 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
8120 if idx == self.len() - 1 {
8121 RelayTransactionReader::new_unchecked(&self.as_slice()[start..])
8122 } else {
8123 let end_idx = start_idx + molecule::NUMBER_SIZE;
8124 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
8125 RelayTransactionReader::new_unchecked(&self.as_slice()[start..end])
8126 }
8127 }
8128}
8129impl<'r> molecule::prelude::Reader<'r> for RelayTransactionVecReader<'r> {
8130 type Entity = RelayTransactionVec;
8131 const NAME: &'static str = "RelayTransactionVecReader";
8132 fn to_entity(&self) -> Self::Entity {
8133 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8134 }
8135 fn new_unchecked(slice: &'r [u8]) -> Self {
8136 RelayTransactionVecReader(slice)
8137 }
8138 fn as_slice(&self) -> &'r [u8] {
8139 self.0
8140 }
8141 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8142 use molecule::verification_error as ve;
8143 let slice_len = slice.len();
8144 if slice_len < molecule::NUMBER_SIZE {
8145 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8146 }
8147 let total_size = molecule::unpack_number(slice) as usize;
8148 if slice_len != total_size {
8149 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8150 }
8151 if slice_len == molecule::NUMBER_SIZE {
8152 return Ok(());
8153 }
8154 if slice_len < molecule::NUMBER_SIZE * 2 {
8155 return ve!(
8156 Self,
8157 TotalSizeNotMatch,
8158 molecule::NUMBER_SIZE * 2,
8159 slice_len
8160 );
8161 }
8162 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8163 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8164 return ve!(Self, OffsetsNotMatch);
8165 }
8166 if slice_len < offset_first {
8167 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8168 }
8169 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8170 .chunks_exact(molecule::NUMBER_SIZE)
8171 .map(|x| molecule::unpack_number(x) as usize)
8172 .collect();
8173 offsets.push(total_size);
8174 if offsets.windows(2).any(|i| i[0] > i[1]) {
8175 return ve!(Self, OffsetsNotMatch);
8176 }
8177 for pair in offsets.windows(2) {
8178 let start = pair[0];
8179 let end = pair[1];
8180 RelayTransactionReader::verify(&slice[start..end], compatible)?;
8181 }
8182 Ok(())
8183 }
8184}
8185#[derive(Clone, Debug, Default)]
8186pub struct RelayTransactionVecBuilder(pub(crate) Vec<RelayTransaction>);
8187impl RelayTransactionVecBuilder {
8188 pub fn set(mut self, v: Vec<RelayTransaction>) -> Self {
8189 self.0 = v;
8190 self
8191 }
8192 pub fn push<T>(mut self, v: T) -> Self
8193 where
8194 T: ::core::convert::Into<RelayTransaction>,
8195 {
8196 self.0.push(v.into());
8197 self
8198 }
8199 pub fn extend<T: ::core::iter::IntoIterator<Item = RelayTransaction>>(
8200 mut self,
8201 iter: T,
8202 ) -> Self {
8203 self.0.extend(iter);
8204 self
8205 }
8206 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<RelayTransaction>
8207 where
8208 T: ::core::convert::Into<RelayTransaction>,
8209 {
8210 self.0
8211 .get_mut(index)
8212 .map(|item| ::core::mem::replace(item, v.into()))
8213 }
8214}
8215impl molecule::prelude::Builder for RelayTransactionVecBuilder {
8216 type Entity = RelayTransactionVec;
8217 const NAME: &'static str = "RelayTransactionVecBuilder";
8218 fn expected_length(&self) -> usize {
8219 molecule::NUMBER_SIZE * (self.0.len() + 1)
8220 + self
8221 .0
8222 .iter()
8223 .map(|inner| inner.as_slice().len())
8224 .sum::<usize>()
8225 }
8226 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8227 let item_count = self.0.len();
8228 if item_count == 0 {
8229 writer.write_all(&molecule::pack_number(
8230 molecule::NUMBER_SIZE as molecule::Number,
8231 ))?;
8232 } else {
8233 let (total_size, offsets) = self.0.iter().fold(
8234 (
8235 molecule::NUMBER_SIZE * (item_count + 1),
8236 Vec::with_capacity(item_count),
8237 ),
8238 |(start, mut offsets), inner| {
8239 offsets.push(start);
8240 (start + inner.as_slice().len(), offsets)
8241 },
8242 );
8243 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8244 for offset in offsets.into_iter() {
8245 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8246 }
8247 for inner in self.0.iter() {
8248 writer.write_all(inner.as_slice())?;
8249 }
8250 }
8251 Ok(())
8252 }
8253 fn build(&self) -> Self::Entity {
8254 let mut inner = Vec::with_capacity(self.expected_length());
8255 self.write(&mut inner)
8256 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8257 RelayTransactionVec::new_unchecked(inner.into())
8258 }
8259}
8260pub struct RelayTransactionVecIterator(RelayTransactionVec, usize, usize);
8261impl ::core::iter::Iterator for RelayTransactionVecIterator {
8262 type Item = RelayTransaction;
8263 fn next(&mut self) -> Option<Self::Item> {
8264 if self.1 >= self.2 {
8265 None
8266 } else {
8267 let ret = self.0.get_unchecked(self.1);
8268 self.1 += 1;
8269 Some(ret)
8270 }
8271 }
8272}
8273impl ::core::iter::ExactSizeIterator for RelayTransactionVecIterator {
8274 fn len(&self) -> usize {
8275 self.2 - self.1
8276 }
8277}
8278impl ::core::iter::IntoIterator for RelayTransactionVec {
8279 type Item = RelayTransaction;
8280 type IntoIter = RelayTransactionVecIterator;
8281 fn into_iter(self) -> Self::IntoIter {
8282 let len = self.len();
8283 RelayTransactionVecIterator(self, 0, len)
8284 }
8285}
8286impl<'r> RelayTransactionVecReader<'r> {
8287 pub fn iter<'t>(&'t self) -> RelayTransactionVecReaderIterator<'t, 'r> {
8288 RelayTransactionVecReaderIterator(&self, 0, self.len())
8289 }
8290}
8291pub struct RelayTransactionVecReaderIterator<'t, 'r>(
8292 &'t RelayTransactionVecReader<'r>,
8293 usize,
8294 usize,
8295);
8296impl<'t: 'r, 'r> ::core::iter::Iterator for RelayTransactionVecReaderIterator<'t, 'r> {
8297 type Item = RelayTransactionReader<'t>;
8298 fn next(&mut self) -> Option<Self::Item> {
8299 if self.1 >= self.2 {
8300 None
8301 } else {
8302 let ret = self.0.get_unchecked(self.1);
8303 self.1 += 1;
8304 Some(ret)
8305 }
8306 }
8307}
8308impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for RelayTransactionVecReaderIterator<'t, 'r> {
8309 fn len(&self) -> usize {
8310 self.2 - self.1
8311 }
8312}
8313impl<T> ::core::iter::FromIterator<T> for RelayTransactionVec
8314where
8315 T: Into<RelayTransaction>,
8316{
8317 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
8318 Self::new_builder()
8319 .extend(iter.into_iter().map(Into::into))
8320 .build()
8321 }
8322}
8323impl<T> From<Vec<T>> for RelayTransactionVec
8324where
8325 T: Into<RelayTransaction>,
8326{
8327 fn from(v: Vec<T>) -> Self {
8328 v.into_iter().collect()
8329 }
8330}
8331#[derive(Clone)]
8332pub struct RelayTransactions(molecule::bytes::Bytes);
8333impl ::core::fmt::LowerHex for RelayTransactions {
8334 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8335 use molecule::hex_string;
8336 if f.alternate() {
8337 write!(f, "0x")?;
8338 }
8339 write!(f, "{}", hex_string(self.as_slice()))
8340 }
8341}
8342impl ::core::fmt::Debug for RelayTransactions {
8343 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8344 write!(f, "{}({:#x})", Self::NAME, self)
8345 }
8346}
8347impl ::core::fmt::Display for RelayTransactions {
8348 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8349 write!(f, "{} {{ ", Self::NAME)?;
8350 write!(f, "{}: {}", "transactions", self.transactions())?;
8351 let extra_count = self.count_extra_fields();
8352 if extra_count != 0 {
8353 write!(f, ", .. ({} fields)", extra_count)?;
8354 }
8355 write!(f, " }}")
8356 }
8357}
8358impl ::core::default::Default for RelayTransactions {
8359 fn default() -> Self {
8360 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
8361 RelayTransactions::new_unchecked(v)
8362 }
8363}
8364impl RelayTransactions {
8365 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 4, 0, 0, 0];
8366 pub const FIELD_COUNT: usize = 1;
8367 pub fn total_size(&self) -> usize {
8368 molecule::unpack_number(self.as_slice()) as usize
8369 }
8370 pub fn field_count(&self) -> usize {
8371 if self.total_size() == molecule::NUMBER_SIZE {
8372 0
8373 } else {
8374 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8375 }
8376 }
8377 pub fn count_extra_fields(&self) -> usize {
8378 self.field_count() - Self::FIELD_COUNT
8379 }
8380 pub fn has_extra_fields(&self) -> bool {
8381 Self::FIELD_COUNT != self.field_count()
8382 }
8383 pub fn transactions(&self) -> RelayTransactionVec {
8384 let slice = self.as_slice();
8385 let start = molecule::unpack_number(&slice[4..]) as usize;
8386 if self.has_extra_fields() {
8387 let end = molecule::unpack_number(&slice[8..]) as usize;
8388 RelayTransactionVec::new_unchecked(self.0.slice(start..end))
8389 } else {
8390 RelayTransactionVec::new_unchecked(self.0.slice(start..))
8391 }
8392 }
8393 pub fn as_reader<'r>(&'r self) -> RelayTransactionsReader<'r> {
8394 RelayTransactionsReader::new_unchecked(self.as_slice())
8395 }
8396}
8397impl molecule::prelude::Entity for RelayTransactions {
8398 type Builder = RelayTransactionsBuilder;
8399 const NAME: &'static str = "RelayTransactions";
8400 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8401 RelayTransactions(data)
8402 }
8403 fn as_bytes(&self) -> molecule::bytes::Bytes {
8404 self.0.clone()
8405 }
8406 fn as_slice(&self) -> &[u8] {
8407 &self.0[..]
8408 }
8409 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8410 RelayTransactionsReader::from_slice(slice).map(|reader| reader.to_entity())
8411 }
8412 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8413 RelayTransactionsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8414 }
8415 fn new_builder() -> Self::Builder {
8416 ::core::default::Default::default()
8417 }
8418 fn as_builder(self) -> Self::Builder {
8419 Self::new_builder().transactions(self.transactions())
8420 }
8421}
8422#[derive(Clone, Copy)]
8423pub struct RelayTransactionsReader<'r>(&'r [u8]);
8424impl<'r> ::core::fmt::LowerHex for RelayTransactionsReader<'r> {
8425 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8426 use molecule::hex_string;
8427 if f.alternate() {
8428 write!(f, "0x")?;
8429 }
8430 write!(f, "{}", hex_string(self.as_slice()))
8431 }
8432}
8433impl<'r> ::core::fmt::Debug for RelayTransactionsReader<'r> {
8434 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8435 write!(f, "{}({:#x})", Self::NAME, self)
8436 }
8437}
8438impl<'r> ::core::fmt::Display for RelayTransactionsReader<'r> {
8439 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8440 write!(f, "{} {{ ", Self::NAME)?;
8441 write!(f, "{}: {}", "transactions", self.transactions())?;
8442 let extra_count = self.count_extra_fields();
8443 if extra_count != 0 {
8444 write!(f, ", .. ({} fields)", extra_count)?;
8445 }
8446 write!(f, " }}")
8447 }
8448}
8449impl<'r> RelayTransactionsReader<'r> {
8450 pub const FIELD_COUNT: usize = 1;
8451 pub fn total_size(&self) -> usize {
8452 molecule::unpack_number(self.as_slice()) as usize
8453 }
8454 pub fn field_count(&self) -> usize {
8455 if self.total_size() == molecule::NUMBER_SIZE {
8456 0
8457 } else {
8458 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8459 }
8460 }
8461 pub fn count_extra_fields(&self) -> usize {
8462 self.field_count() - Self::FIELD_COUNT
8463 }
8464 pub fn has_extra_fields(&self) -> bool {
8465 Self::FIELD_COUNT != self.field_count()
8466 }
8467 pub fn transactions(&self) -> RelayTransactionVecReader<'r> {
8468 let slice = self.as_slice();
8469 let start = molecule::unpack_number(&slice[4..]) as usize;
8470 if self.has_extra_fields() {
8471 let end = molecule::unpack_number(&slice[8..]) as usize;
8472 RelayTransactionVecReader::new_unchecked(&self.as_slice()[start..end])
8473 } else {
8474 RelayTransactionVecReader::new_unchecked(&self.as_slice()[start..])
8475 }
8476 }
8477}
8478impl<'r> molecule::prelude::Reader<'r> for RelayTransactionsReader<'r> {
8479 type Entity = RelayTransactions;
8480 const NAME: &'static str = "RelayTransactionsReader";
8481 fn to_entity(&self) -> Self::Entity {
8482 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8483 }
8484 fn new_unchecked(slice: &'r [u8]) -> Self {
8485 RelayTransactionsReader(slice)
8486 }
8487 fn as_slice(&self) -> &'r [u8] {
8488 self.0
8489 }
8490 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8491 use molecule::verification_error as ve;
8492 let slice_len = slice.len();
8493 if slice_len < molecule::NUMBER_SIZE {
8494 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8495 }
8496 let total_size = molecule::unpack_number(slice) as usize;
8497 if slice_len != total_size {
8498 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8499 }
8500 if slice_len < molecule::NUMBER_SIZE * 2 {
8501 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8502 }
8503 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8504 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8505 return ve!(Self, OffsetsNotMatch);
8506 }
8507 if slice_len < offset_first {
8508 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8509 }
8510 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8511 if field_count < Self::FIELD_COUNT {
8512 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8513 } else if !compatible && field_count > Self::FIELD_COUNT {
8514 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8515 };
8516 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8517 .chunks_exact(molecule::NUMBER_SIZE)
8518 .map(|x| molecule::unpack_number(x) as usize)
8519 .collect();
8520 offsets.push(total_size);
8521 if offsets.windows(2).any(|i| i[0] > i[1]) {
8522 return ve!(Self, OffsetsNotMatch);
8523 }
8524 RelayTransactionVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8525 Ok(())
8526 }
8527}
8528#[derive(Clone, Debug, Default)]
8529pub struct RelayTransactionsBuilder {
8530 pub(crate) transactions: RelayTransactionVec,
8531}
8532impl RelayTransactionsBuilder {
8533 pub const FIELD_COUNT: usize = 1;
8534 pub fn transactions<T>(mut self, v: T) -> Self
8535 where
8536 T: ::core::convert::Into<RelayTransactionVec>,
8537 {
8538 self.transactions = v.into();
8539 self
8540 }
8541}
8542impl molecule::prelude::Builder for RelayTransactionsBuilder {
8543 type Entity = RelayTransactions;
8544 const NAME: &'static str = "RelayTransactionsBuilder";
8545 fn expected_length(&self) -> usize {
8546 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.transactions.as_slice().len()
8547 }
8548 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8549 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8550 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8551 offsets.push(total_size);
8552 total_size += self.transactions.as_slice().len();
8553 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8554 for offset in offsets.into_iter() {
8555 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8556 }
8557 writer.write_all(self.transactions.as_slice())?;
8558 Ok(())
8559 }
8560 fn build(&self) -> Self::Entity {
8561 let mut inner = Vec::with_capacity(self.expected_length());
8562 self.write(&mut inner)
8563 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8564 RelayTransactions::new_unchecked(inner.into())
8565 }
8566}
8567#[derive(Clone)]
8568pub struct RelayTransactionHashes(molecule::bytes::Bytes);
8569impl ::core::fmt::LowerHex for RelayTransactionHashes {
8570 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8571 use molecule::hex_string;
8572 if f.alternate() {
8573 write!(f, "0x")?;
8574 }
8575 write!(f, "{}", hex_string(self.as_slice()))
8576 }
8577}
8578impl ::core::fmt::Debug for RelayTransactionHashes {
8579 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8580 write!(f, "{}({:#x})", Self::NAME, self)
8581 }
8582}
8583impl ::core::fmt::Display for RelayTransactionHashes {
8584 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8585 write!(f, "{} {{ ", Self::NAME)?;
8586 write!(f, "{}: {}", "tx_hashes", self.tx_hashes())?;
8587 let extra_count = self.count_extra_fields();
8588 if extra_count != 0 {
8589 write!(f, ", .. ({} fields)", extra_count)?;
8590 }
8591 write!(f, " }}")
8592 }
8593}
8594impl ::core::default::Default for RelayTransactionHashes {
8595 fn default() -> Self {
8596 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
8597 RelayTransactionHashes::new_unchecked(v)
8598 }
8599}
8600impl RelayTransactionHashes {
8601 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
8602 pub const FIELD_COUNT: usize = 1;
8603 pub fn total_size(&self) -> usize {
8604 molecule::unpack_number(self.as_slice()) as usize
8605 }
8606 pub fn field_count(&self) -> usize {
8607 if self.total_size() == molecule::NUMBER_SIZE {
8608 0
8609 } else {
8610 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8611 }
8612 }
8613 pub fn count_extra_fields(&self) -> usize {
8614 self.field_count() - Self::FIELD_COUNT
8615 }
8616 pub fn has_extra_fields(&self) -> bool {
8617 Self::FIELD_COUNT != self.field_count()
8618 }
8619 pub fn tx_hashes(&self) -> Byte32Vec {
8620 let slice = self.as_slice();
8621 let start = molecule::unpack_number(&slice[4..]) as usize;
8622 if self.has_extra_fields() {
8623 let end = molecule::unpack_number(&slice[8..]) as usize;
8624 Byte32Vec::new_unchecked(self.0.slice(start..end))
8625 } else {
8626 Byte32Vec::new_unchecked(self.0.slice(start..))
8627 }
8628 }
8629 pub fn as_reader<'r>(&'r self) -> RelayTransactionHashesReader<'r> {
8630 RelayTransactionHashesReader::new_unchecked(self.as_slice())
8631 }
8632}
8633impl molecule::prelude::Entity for RelayTransactionHashes {
8634 type Builder = RelayTransactionHashesBuilder;
8635 const NAME: &'static str = "RelayTransactionHashes";
8636 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8637 RelayTransactionHashes(data)
8638 }
8639 fn as_bytes(&self) -> molecule::bytes::Bytes {
8640 self.0.clone()
8641 }
8642 fn as_slice(&self) -> &[u8] {
8643 &self.0[..]
8644 }
8645 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8646 RelayTransactionHashesReader::from_slice(slice).map(|reader| reader.to_entity())
8647 }
8648 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8649 RelayTransactionHashesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8650 }
8651 fn new_builder() -> Self::Builder {
8652 ::core::default::Default::default()
8653 }
8654 fn as_builder(self) -> Self::Builder {
8655 Self::new_builder().tx_hashes(self.tx_hashes())
8656 }
8657}
8658#[derive(Clone, Copy)]
8659pub struct RelayTransactionHashesReader<'r>(&'r [u8]);
8660impl<'r> ::core::fmt::LowerHex for RelayTransactionHashesReader<'r> {
8661 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8662 use molecule::hex_string;
8663 if f.alternate() {
8664 write!(f, "0x")?;
8665 }
8666 write!(f, "{}", hex_string(self.as_slice()))
8667 }
8668}
8669impl<'r> ::core::fmt::Debug for RelayTransactionHashesReader<'r> {
8670 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8671 write!(f, "{}({:#x})", Self::NAME, self)
8672 }
8673}
8674impl<'r> ::core::fmt::Display for RelayTransactionHashesReader<'r> {
8675 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8676 write!(f, "{} {{ ", Self::NAME)?;
8677 write!(f, "{}: {}", "tx_hashes", self.tx_hashes())?;
8678 let extra_count = self.count_extra_fields();
8679 if extra_count != 0 {
8680 write!(f, ", .. ({} fields)", extra_count)?;
8681 }
8682 write!(f, " }}")
8683 }
8684}
8685impl<'r> RelayTransactionHashesReader<'r> {
8686 pub const FIELD_COUNT: usize = 1;
8687 pub fn total_size(&self) -> usize {
8688 molecule::unpack_number(self.as_slice()) as usize
8689 }
8690 pub fn field_count(&self) -> usize {
8691 if self.total_size() == molecule::NUMBER_SIZE {
8692 0
8693 } else {
8694 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8695 }
8696 }
8697 pub fn count_extra_fields(&self) -> usize {
8698 self.field_count() - Self::FIELD_COUNT
8699 }
8700 pub fn has_extra_fields(&self) -> bool {
8701 Self::FIELD_COUNT != self.field_count()
8702 }
8703 pub fn tx_hashes(&self) -> Byte32VecReader<'r> {
8704 let slice = self.as_slice();
8705 let start = molecule::unpack_number(&slice[4..]) as usize;
8706 if self.has_extra_fields() {
8707 let end = molecule::unpack_number(&slice[8..]) as usize;
8708 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
8709 } else {
8710 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
8711 }
8712 }
8713}
8714impl<'r> molecule::prelude::Reader<'r> for RelayTransactionHashesReader<'r> {
8715 type Entity = RelayTransactionHashes;
8716 const NAME: &'static str = "RelayTransactionHashesReader";
8717 fn to_entity(&self) -> Self::Entity {
8718 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8719 }
8720 fn new_unchecked(slice: &'r [u8]) -> Self {
8721 RelayTransactionHashesReader(slice)
8722 }
8723 fn as_slice(&self) -> &'r [u8] {
8724 self.0
8725 }
8726 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8727 use molecule::verification_error as ve;
8728 let slice_len = slice.len();
8729 if slice_len < molecule::NUMBER_SIZE {
8730 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8731 }
8732 let total_size = molecule::unpack_number(slice) as usize;
8733 if slice_len != total_size {
8734 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8735 }
8736 if slice_len < molecule::NUMBER_SIZE * 2 {
8737 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8738 }
8739 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8740 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8741 return ve!(Self, OffsetsNotMatch);
8742 }
8743 if slice_len < offset_first {
8744 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8745 }
8746 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8747 if field_count < Self::FIELD_COUNT {
8748 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8749 } else if !compatible && field_count > Self::FIELD_COUNT {
8750 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8751 };
8752 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8753 .chunks_exact(molecule::NUMBER_SIZE)
8754 .map(|x| molecule::unpack_number(x) as usize)
8755 .collect();
8756 offsets.push(total_size);
8757 if offsets.windows(2).any(|i| i[0] > i[1]) {
8758 return ve!(Self, OffsetsNotMatch);
8759 }
8760 Byte32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8761 Ok(())
8762 }
8763}
8764#[derive(Clone, Debug, Default)]
8765pub struct RelayTransactionHashesBuilder {
8766 pub(crate) tx_hashes: Byte32Vec,
8767}
8768impl RelayTransactionHashesBuilder {
8769 pub const FIELD_COUNT: usize = 1;
8770 pub fn tx_hashes<T>(mut self, v: T) -> Self
8771 where
8772 T: ::core::convert::Into<Byte32Vec>,
8773 {
8774 self.tx_hashes = v.into();
8775 self
8776 }
8777}
8778impl molecule::prelude::Builder for RelayTransactionHashesBuilder {
8779 type Entity = RelayTransactionHashes;
8780 const NAME: &'static str = "RelayTransactionHashesBuilder";
8781 fn expected_length(&self) -> usize {
8782 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.tx_hashes.as_slice().len()
8783 }
8784 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8785 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8786 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8787 offsets.push(total_size);
8788 total_size += self.tx_hashes.as_slice().len();
8789 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8790 for offset in offsets.into_iter() {
8791 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8792 }
8793 writer.write_all(self.tx_hashes.as_slice())?;
8794 Ok(())
8795 }
8796 fn build(&self) -> Self::Entity {
8797 let mut inner = Vec::with_capacity(self.expected_length());
8798 self.write(&mut inner)
8799 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8800 RelayTransactionHashes::new_unchecked(inner.into())
8801 }
8802}
8803#[derive(Clone)]
8804pub struct GetRelayTransactions(molecule::bytes::Bytes);
8805impl ::core::fmt::LowerHex for GetRelayTransactions {
8806 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8807 use molecule::hex_string;
8808 if f.alternate() {
8809 write!(f, "0x")?;
8810 }
8811 write!(f, "{}", hex_string(self.as_slice()))
8812 }
8813}
8814impl ::core::fmt::Debug for GetRelayTransactions {
8815 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8816 write!(f, "{}({:#x})", Self::NAME, self)
8817 }
8818}
8819impl ::core::fmt::Display for GetRelayTransactions {
8820 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8821 write!(f, "{} {{ ", Self::NAME)?;
8822 write!(f, "{}: {}", "tx_hashes", self.tx_hashes())?;
8823 let extra_count = self.count_extra_fields();
8824 if extra_count != 0 {
8825 write!(f, ", .. ({} fields)", extra_count)?;
8826 }
8827 write!(f, " }}")
8828 }
8829}
8830impl ::core::default::Default for GetRelayTransactions {
8831 fn default() -> Self {
8832 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
8833 GetRelayTransactions::new_unchecked(v)
8834 }
8835}
8836impl GetRelayTransactions {
8837 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
8838 pub const FIELD_COUNT: usize = 1;
8839 pub fn total_size(&self) -> usize {
8840 molecule::unpack_number(self.as_slice()) as usize
8841 }
8842 pub fn field_count(&self) -> usize {
8843 if self.total_size() == molecule::NUMBER_SIZE {
8844 0
8845 } else {
8846 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8847 }
8848 }
8849 pub fn count_extra_fields(&self) -> usize {
8850 self.field_count() - Self::FIELD_COUNT
8851 }
8852 pub fn has_extra_fields(&self) -> bool {
8853 Self::FIELD_COUNT != self.field_count()
8854 }
8855 pub fn tx_hashes(&self) -> Byte32Vec {
8856 let slice = self.as_slice();
8857 let start = molecule::unpack_number(&slice[4..]) as usize;
8858 if self.has_extra_fields() {
8859 let end = molecule::unpack_number(&slice[8..]) as usize;
8860 Byte32Vec::new_unchecked(self.0.slice(start..end))
8861 } else {
8862 Byte32Vec::new_unchecked(self.0.slice(start..))
8863 }
8864 }
8865 pub fn as_reader<'r>(&'r self) -> GetRelayTransactionsReader<'r> {
8866 GetRelayTransactionsReader::new_unchecked(self.as_slice())
8867 }
8868}
8869impl molecule::prelude::Entity for GetRelayTransactions {
8870 type Builder = GetRelayTransactionsBuilder;
8871 const NAME: &'static str = "GetRelayTransactions";
8872 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8873 GetRelayTransactions(data)
8874 }
8875 fn as_bytes(&self) -> molecule::bytes::Bytes {
8876 self.0.clone()
8877 }
8878 fn as_slice(&self) -> &[u8] {
8879 &self.0[..]
8880 }
8881 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8882 GetRelayTransactionsReader::from_slice(slice).map(|reader| reader.to_entity())
8883 }
8884 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8885 GetRelayTransactionsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8886 }
8887 fn new_builder() -> Self::Builder {
8888 ::core::default::Default::default()
8889 }
8890 fn as_builder(self) -> Self::Builder {
8891 Self::new_builder().tx_hashes(self.tx_hashes())
8892 }
8893}
8894#[derive(Clone, Copy)]
8895pub struct GetRelayTransactionsReader<'r>(&'r [u8]);
8896impl<'r> ::core::fmt::LowerHex for GetRelayTransactionsReader<'r> {
8897 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8898 use molecule::hex_string;
8899 if f.alternate() {
8900 write!(f, "0x")?;
8901 }
8902 write!(f, "{}", hex_string(self.as_slice()))
8903 }
8904}
8905impl<'r> ::core::fmt::Debug for GetRelayTransactionsReader<'r> {
8906 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8907 write!(f, "{}({:#x})", Self::NAME, self)
8908 }
8909}
8910impl<'r> ::core::fmt::Display for GetRelayTransactionsReader<'r> {
8911 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8912 write!(f, "{} {{ ", Self::NAME)?;
8913 write!(f, "{}: {}", "tx_hashes", self.tx_hashes())?;
8914 let extra_count = self.count_extra_fields();
8915 if extra_count != 0 {
8916 write!(f, ", .. ({} fields)", extra_count)?;
8917 }
8918 write!(f, " }}")
8919 }
8920}
8921impl<'r> GetRelayTransactionsReader<'r> {
8922 pub const FIELD_COUNT: usize = 1;
8923 pub fn total_size(&self) -> usize {
8924 molecule::unpack_number(self.as_slice()) as usize
8925 }
8926 pub fn field_count(&self) -> usize {
8927 if self.total_size() == molecule::NUMBER_SIZE {
8928 0
8929 } else {
8930 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8931 }
8932 }
8933 pub fn count_extra_fields(&self) -> usize {
8934 self.field_count() - Self::FIELD_COUNT
8935 }
8936 pub fn has_extra_fields(&self) -> bool {
8937 Self::FIELD_COUNT != self.field_count()
8938 }
8939 pub fn tx_hashes(&self) -> Byte32VecReader<'r> {
8940 let slice = self.as_slice();
8941 let start = molecule::unpack_number(&slice[4..]) as usize;
8942 if self.has_extra_fields() {
8943 let end = molecule::unpack_number(&slice[8..]) as usize;
8944 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
8945 } else {
8946 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
8947 }
8948 }
8949}
8950impl<'r> molecule::prelude::Reader<'r> for GetRelayTransactionsReader<'r> {
8951 type Entity = GetRelayTransactions;
8952 const NAME: &'static str = "GetRelayTransactionsReader";
8953 fn to_entity(&self) -> Self::Entity {
8954 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8955 }
8956 fn new_unchecked(slice: &'r [u8]) -> Self {
8957 GetRelayTransactionsReader(slice)
8958 }
8959 fn as_slice(&self) -> &'r [u8] {
8960 self.0
8961 }
8962 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8963 use molecule::verification_error as ve;
8964 let slice_len = slice.len();
8965 if slice_len < molecule::NUMBER_SIZE {
8966 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8967 }
8968 let total_size = molecule::unpack_number(slice) as usize;
8969 if slice_len != total_size {
8970 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8971 }
8972 if slice_len < molecule::NUMBER_SIZE * 2 {
8973 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8974 }
8975 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8976 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8977 return ve!(Self, OffsetsNotMatch);
8978 }
8979 if slice_len < offset_first {
8980 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8981 }
8982 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8983 if field_count < Self::FIELD_COUNT {
8984 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8985 } else if !compatible && field_count > Self::FIELD_COUNT {
8986 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8987 };
8988 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8989 .chunks_exact(molecule::NUMBER_SIZE)
8990 .map(|x| molecule::unpack_number(x) as usize)
8991 .collect();
8992 offsets.push(total_size);
8993 if offsets.windows(2).any(|i| i[0] > i[1]) {
8994 return ve!(Self, OffsetsNotMatch);
8995 }
8996 Byte32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8997 Ok(())
8998 }
8999}
9000#[derive(Clone, Debug, Default)]
9001pub struct GetRelayTransactionsBuilder {
9002 pub(crate) tx_hashes: Byte32Vec,
9003}
9004impl GetRelayTransactionsBuilder {
9005 pub const FIELD_COUNT: usize = 1;
9006 pub fn tx_hashes<T>(mut self, v: T) -> Self
9007 where
9008 T: ::core::convert::Into<Byte32Vec>,
9009 {
9010 self.tx_hashes = v.into();
9011 self
9012 }
9013}
9014impl molecule::prelude::Builder for GetRelayTransactionsBuilder {
9015 type Entity = GetRelayTransactions;
9016 const NAME: &'static str = "GetRelayTransactionsBuilder";
9017 fn expected_length(&self) -> usize {
9018 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.tx_hashes.as_slice().len()
9019 }
9020 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
9021 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
9022 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
9023 offsets.push(total_size);
9024 total_size += self.tx_hashes.as_slice().len();
9025 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
9026 for offset in offsets.into_iter() {
9027 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
9028 }
9029 writer.write_all(self.tx_hashes.as_slice())?;
9030 Ok(())
9031 }
9032 fn build(&self) -> Self::Entity {
9033 let mut inner = Vec::with_capacity(self.expected_length());
9034 self.write(&mut inner)
9035 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9036 GetRelayTransactions::new_unchecked(inner.into())
9037 }
9038}
9039#[derive(Clone)]
9040pub struct GetBlockTransactions(molecule::bytes::Bytes);
9041impl ::core::fmt::LowerHex for GetBlockTransactions {
9042 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9043 use molecule::hex_string;
9044 if f.alternate() {
9045 write!(f, "0x")?;
9046 }
9047 write!(f, "{}", hex_string(self.as_slice()))
9048 }
9049}
9050impl ::core::fmt::Debug for GetBlockTransactions {
9051 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9052 write!(f, "{}({:#x})", Self::NAME, self)
9053 }
9054}
9055impl ::core::fmt::Display for GetBlockTransactions {
9056 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9057 write!(f, "{} {{ ", Self::NAME)?;
9058 write!(f, "{}: {}", "block_hash", self.block_hash())?;
9059 write!(f, ", {}: {}", "indexes", self.indexes())?;
9060 write!(f, ", {}: {}", "uncle_indexes", self.uncle_indexes())?;
9061 let extra_count = self.count_extra_fields();
9062 if extra_count != 0 {
9063 write!(f, ", .. ({} fields)", extra_count)?;
9064 }
9065 write!(f, " }}")
9066 }
9067}
9068impl ::core::default::Default for GetBlockTransactions {
9069 fn default() -> Self {
9070 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9071 GetBlockTransactions::new_unchecked(v)
9072 }
9073}
9074impl GetBlockTransactions {
9075 const DEFAULT_VALUE: [u8; 56] = [
9076 56, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9077 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9078 ];
9079 pub const FIELD_COUNT: usize = 3;
9080 pub fn total_size(&self) -> usize {
9081 molecule::unpack_number(self.as_slice()) as usize
9082 }
9083 pub fn field_count(&self) -> usize {
9084 if self.total_size() == molecule::NUMBER_SIZE {
9085 0
9086 } else {
9087 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9088 }
9089 }
9090 pub fn count_extra_fields(&self) -> usize {
9091 self.field_count() - Self::FIELD_COUNT
9092 }
9093 pub fn has_extra_fields(&self) -> bool {
9094 Self::FIELD_COUNT != self.field_count()
9095 }
9096 pub fn block_hash(&self) -> Byte32 {
9097 let slice = self.as_slice();
9098 let start = molecule::unpack_number(&slice[4..]) as usize;
9099 let end = molecule::unpack_number(&slice[8..]) as usize;
9100 Byte32::new_unchecked(self.0.slice(start..end))
9101 }
9102 pub fn indexes(&self) -> Uint32Vec {
9103 let slice = self.as_slice();
9104 let start = molecule::unpack_number(&slice[8..]) as usize;
9105 let end = molecule::unpack_number(&slice[12..]) as usize;
9106 Uint32Vec::new_unchecked(self.0.slice(start..end))
9107 }
9108 pub fn uncle_indexes(&self) -> Uint32Vec {
9109 let slice = self.as_slice();
9110 let start = molecule::unpack_number(&slice[12..]) as usize;
9111 if self.has_extra_fields() {
9112 let end = molecule::unpack_number(&slice[16..]) as usize;
9113 Uint32Vec::new_unchecked(self.0.slice(start..end))
9114 } else {
9115 Uint32Vec::new_unchecked(self.0.slice(start..))
9116 }
9117 }
9118 pub fn as_reader<'r>(&'r self) -> GetBlockTransactionsReader<'r> {
9119 GetBlockTransactionsReader::new_unchecked(self.as_slice())
9120 }
9121}
9122impl molecule::prelude::Entity for GetBlockTransactions {
9123 type Builder = GetBlockTransactionsBuilder;
9124 const NAME: &'static str = "GetBlockTransactions";
9125 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9126 GetBlockTransactions(data)
9127 }
9128 fn as_bytes(&self) -> molecule::bytes::Bytes {
9129 self.0.clone()
9130 }
9131 fn as_slice(&self) -> &[u8] {
9132 &self.0[..]
9133 }
9134 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9135 GetBlockTransactionsReader::from_slice(slice).map(|reader| reader.to_entity())
9136 }
9137 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9138 GetBlockTransactionsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9139 }
9140 fn new_builder() -> Self::Builder {
9141 ::core::default::Default::default()
9142 }
9143 fn as_builder(self) -> Self::Builder {
9144 Self::new_builder()
9145 .block_hash(self.block_hash())
9146 .indexes(self.indexes())
9147 .uncle_indexes(self.uncle_indexes())
9148 }
9149}
9150#[derive(Clone, Copy)]
9151pub struct GetBlockTransactionsReader<'r>(&'r [u8]);
9152impl<'r> ::core::fmt::LowerHex for GetBlockTransactionsReader<'r> {
9153 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9154 use molecule::hex_string;
9155 if f.alternate() {
9156 write!(f, "0x")?;
9157 }
9158 write!(f, "{}", hex_string(self.as_slice()))
9159 }
9160}
9161impl<'r> ::core::fmt::Debug for GetBlockTransactionsReader<'r> {
9162 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9163 write!(f, "{}({:#x})", Self::NAME, self)
9164 }
9165}
9166impl<'r> ::core::fmt::Display for GetBlockTransactionsReader<'r> {
9167 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9168 write!(f, "{} {{ ", Self::NAME)?;
9169 write!(f, "{}: {}", "block_hash", self.block_hash())?;
9170 write!(f, ", {}: {}", "indexes", self.indexes())?;
9171 write!(f, ", {}: {}", "uncle_indexes", self.uncle_indexes())?;
9172 let extra_count = self.count_extra_fields();
9173 if extra_count != 0 {
9174 write!(f, ", .. ({} fields)", extra_count)?;
9175 }
9176 write!(f, " }}")
9177 }
9178}
9179impl<'r> GetBlockTransactionsReader<'r> {
9180 pub const FIELD_COUNT: usize = 3;
9181 pub fn total_size(&self) -> usize {
9182 molecule::unpack_number(self.as_slice()) as usize
9183 }
9184 pub fn field_count(&self) -> usize {
9185 if self.total_size() == molecule::NUMBER_SIZE {
9186 0
9187 } else {
9188 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9189 }
9190 }
9191 pub fn count_extra_fields(&self) -> usize {
9192 self.field_count() - Self::FIELD_COUNT
9193 }
9194 pub fn has_extra_fields(&self) -> bool {
9195 Self::FIELD_COUNT != self.field_count()
9196 }
9197 pub fn block_hash(&self) -> Byte32Reader<'r> {
9198 let slice = self.as_slice();
9199 let start = molecule::unpack_number(&slice[4..]) as usize;
9200 let end = molecule::unpack_number(&slice[8..]) as usize;
9201 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
9202 }
9203 pub fn indexes(&self) -> Uint32VecReader<'r> {
9204 let slice = self.as_slice();
9205 let start = molecule::unpack_number(&slice[8..]) as usize;
9206 let end = molecule::unpack_number(&slice[12..]) as usize;
9207 Uint32VecReader::new_unchecked(&self.as_slice()[start..end])
9208 }
9209 pub fn uncle_indexes(&self) -> Uint32VecReader<'r> {
9210 let slice = self.as_slice();
9211 let start = molecule::unpack_number(&slice[12..]) as usize;
9212 if self.has_extra_fields() {
9213 let end = molecule::unpack_number(&slice[16..]) as usize;
9214 Uint32VecReader::new_unchecked(&self.as_slice()[start..end])
9215 } else {
9216 Uint32VecReader::new_unchecked(&self.as_slice()[start..])
9217 }
9218 }
9219}
9220impl<'r> molecule::prelude::Reader<'r> for GetBlockTransactionsReader<'r> {
9221 type Entity = GetBlockTransactions;
9222 const NAME: &'static str = "GetBlockTransactionsReader";
9223 fn to_entity(&self) -> Self::Entity {
9224 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9225 }
9226 fn new_unchecked(slice: &'r [u8]) -> Self {
9227 GetBlockTransactionsReader(slice)
9228 }
9229 fn as_slice(&self) -> &'r [u8] {
9230 self.0
9231 }
9232 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9233 use molecule::verification_error as ve;
9234 let slice_len = slice.len();
9235 if slice_len < molecule::NUMBER_SIZE {
9236 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
9237 }
9238 let total_size = molecule::unpack_number(slice) as usize;
9239 if slice_len != total_size {
9240 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
9241 }
9242 if slice_len < molecule::NUMBER_SIZE * 2 {
9243 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
9244 }
9245 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
9246 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
9247 return ve!(Self, OffsetsNotMatch);
9248 }
9249 if slice_len < offset_first {
9250 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
9251 }
9252 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
9253 if field_count < Self::FIELD_COUNT {
9254 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9255 } else if !compatible && field_count > Self::FIELD_COUNT {
9256 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9257 };
9258 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
9259 .chunks_exact(molecule::NUMBER_SIZE)
9260 .map(|x| molecule::unpack_number(x) as usize)
9261 .collect();
9262 offsets.push(total_size);
9263 if offsets.windows(2).any(|i| i[0] > i[1]) {
9264 return ve!(Self, OffsetsNotMatch);
9265 }
9266 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
9267 Uint32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
9268 Uint32VecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
9269 Ok(())
9270 }
9271}
9272#[derive(Clone, Debug, Default)]
9273pub struct GetBlockTransactionsBuilder {
9274 pub(crate) block_hash: Byte32,
9275 pub(crate) indexes: Uint32Vec,
9276 pub(crate) uncle_indexes: Uint32Vec,
9277}
9278impl GetBlockTransactionsBuilder {
9279 pub const FIELD_COUNT: usize = 3;
9280 pub fn block_hash<T>(mut self, v: T) -> Self
9281 where
9282 T: ::core::convert::Into<Byte32>,
9283 {
9284 self.block_hash = v.into();
9285 self
9286 }
9287 pub fn indexes<T>(mut self, v: T) -> Self
9288 where
9289 T: ::core::convert::Into<Uint32Vec>,
9290 {
9291 self.indexes = v.into();
9292 self
9293 }
9294 pub fn uncle_indexes<T>(mut self, v: T) -> Self
9295 where
9296 T: ::core::convert::Into<Uint32Vec>,
9297 {
9298 self.uncle_indexes = v.into();
9299 self
9300 }
9301}
9302impl molecule::prelude::Builder for GetBlockTransactionsBuilder {
9303 type Entity = GetBlockTransactions;
9304 const NAME: &'static str = "GetBlockTransactionsBuilder";
9305 fn expected_length(&self) -> usize {
9306 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
9307 + self.block_hash.as_slice().len()
9308 + self.indexes.as_slice().len()
9309 + self.uncle_indexes.as_slice().len()
9310 }
9311 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
9312 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
9313 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
9314 offsets.push(total_size);
9315 total_size += self.block_hash.as_slice().len();
9316 offsets.push(total_size);
9317 total_size += self.indexes.as_slice().len();
9318 offsets.push(total_size);
9319 total_size += self.uncle_indexes.as_slice().len();
9320 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
9321 for offset in offsets.into_iter() {
9322 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
9323 }
9324 writer.write_all(self.block_hash.as_slice())?;
9325 writer.write_all(self.indexes.as_slice())?;
9326 writer.write_all(self.uncle_indexes.as_slice())?;
9327 Ok(())
9328 }
9329 fn build(&self) -> Self::Entity {
9330 let mut inner = Vec::with_capacity(self.expected_length());
9331 self.write(&mut inner)
9332 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9333 GetBlockTransactions::new_unchecked(inner.into())
9334 }
9335}
9336#[derive(Clone)]
9337pub struct BlockTransactions(molecule::bytes::Bytes);
9338impl ::core::fmt::LowerHex for BlockTransactions {
9339 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9340 use molecule::hex_string;
9341 if f.alternate() {
9342 write!(f, "0x")?;
9343 }
9344 write!(f, "{}", hex_string(self.as_slice()))
9345 }
9346}
9347impl ::core::fmt::Debug for BlockTransactions {
9348 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9349 write!(f, "{}({:#x})", Self::NAME, self)
9350 }
9351}
9352impl ::core::fmt::Display for BlockTransactions {
9353 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9354 write!(f, "{} {{ ", Self::NAME)?;
9355 write!(f, "{}: {}", "block_hash", self.block_hash())?;
9356 write!(f, ", {}: {}", "transactions", self.transactions())?;
9357 write!(f, ", {}: {}", "uncles", self.uncles())?;
9358 let extra_count = self.count_extra_fields();
9359 if extra_count != 0 {
9360 write!(f, ", .. ({} fields)", extra_count)?;
9361 }
9362 write!(f, " }}")
9363 }
9364}
9365impl ::core::default::Default for BlockTransactions {
9366 fn default() -> Self {
9367 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9368 BlockTransactions::new_unchecked(v)
9369 }
9370}
9371impl BlockTransactions {
9372 const DEFAULT_VALUE: [u8; 56] = [
9373 56, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9374 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
9375 ];
9376 pub const FIELD_COUNT: usize = 3;
9377 pub fn total_size(&self) -> usize {
9378 molecule::unpack_number(self.as_slice()) as usize
9379 }
9380 pub fn field_count(&self) -> usize {
9381 if self.total_size() == molecule::NUMBER_SIZE {
9382 0
9383 } else {
9384 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9385 }
9386 }
9387 pub fn count_extra_fields(&self) -> usize {
9388 self.field_count() - Self::FIELD_COUNT
9389 }
9390 pub fn has_extra_fields(&self) -> bool {
9391 Self::FIELD_COUNT != self.field_count()
9392 }
9393 pub fn block_hash(&self) -> Byte32 {
9394 let slice = self.as_slice();
9395 let start = molecule::unpack_number(&slice[4..]) as usize;
9396 let end = molecule::unpack_number(&slice[8..]) as usize;
9397 Byte32::new_unchecked(self.0.slice(start..end))
9398 }
9399 pub fn transactions(&self) -> TransactionVec {
9400 let slice = self.as_slice();
9401 let start = molecule::unpack_number(&slice[8..]) as usize;
9402 let end = molecule::unpack_number(&slice[12..]) as usize;
9403 TransactionVec::new_unchecked(self.0.slice(start..end))
9404 }
9405 pub fn uncles(&self) -> UncleBlockVec {
9406 let slice = self.as_slice();
9407 let start = molecule::unpack_number(&slice[12..]) as usize;
9408 if self.has_extra_fields() {
9409 let end = molecule::unpack_number(&slice[16..]) as usize;
9410 UncleBlockVec::new_unchecked(self.0.slice(start..end))
9411 } else {
9412 UncleBlockVec::new_unchecked(self.0.slice(start..))
9413 }
9414 }
9415 pub fn as_reader<'r>(&'r self) -> BlockTransactionsReader<'r> {
9416 BlockTransactionsReader::new_unchecked(self.as_slice())
9417 }
9418}
9419impl molecule::prelude::Entity for BlockTransactions {
9420 type Builder = BlockTransactionsBuilder;
9421 const NAME: &'static str = "BlockTransactions";
9422 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9423 BlockTransactions(data)
9424 }
9425 fn as_bytes(&self) -> molecule::bytes::Bytes {
9426 self.0.clone()
9427 }
9428 fn as_slice(&self) -> &[u8] {
9429 &self.0[..]
9430 }
9431 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9432 BlockTransactionsReader::from_slice(slice).map(|reader| reader.to_entity())
9433 }
9434 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9435 BlockTransactionsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9436 }
9437 fn new_builder() -> Self::Builder {
9438 ::core::default::Default::default()
9439 }
9440 fn as_builder(self) -> Self::Builder {
9441 Self::new_builder()
9442 .block_hash(self.block_hash())
9443 .transactions(self.transactions())
9444 .uncles(self.uncles())
9445 }
9446}
9447#[derive(Clone, Copy)]
9448pub struct BlockTransactionsReader<'r>(&'r [u8]);
9449impl<'r> ::core::fmt::LowerHex for BlockTransactionsReader<'r> {
9450 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9451 use molecule::hex_string;
9452 if f.alternate() {
9453 write!(f, "0x")?;
9454 }
9455 write!(f, "{}", hex_string(self.as_slice()))
9456 }
9457}
9458impl<'r> ::core::fmt::Debug for BlockTransactionsReader<'r> {
9459 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9460 write!(f, "{}({:#x})", Self::NAME, self)
9461 }
9462}
9463impl<'r> ::core::fmt::Display for BlockTransactionsReader<'r> {
9464 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9465 write!(f, "{} {{ ", Self::NAME)?;
9466 write!(f, "{}: {}", "block_hash", self.block_hash())?;
9467 write!(f, ", {}: {}", "transactions", self.transactions())?;
9468 write!(f, ", {}: {}", "uncles", self.uncles())?;
9469 let extra_count = self.count_extra_fields();
9470 if extra_count != 0 {
9471 write!(f, ", .. ({} fields)", extra_count)?;
9472 }
9473 write!(f, " }}")
9474 }
9475}
9476impl<'r> BlockTransactionsReader<'r> {
9477 pub const FIELD_COUNT: usize = 3;
9478 pub fn total_size(&self) -> usize {
9479 molecule::unpack_number(self.as_slice()) as usize
9480 }
9481 pub fn field_count(&self) -> usize {
9482 if self.total_size() == molecule::NUMBER_SIZE {
9483 0
9484 } else {
9485 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9486 }
9487 }
9488 pub fn count_extra_fields(&self) -> usize {
9489 self.field_count() - Self::FIELD_COUNT
9490 }
9491 pub fn has_extra_fields(&self) -> bool {
9492 Self::FIELD_COUNT != self.field_count()
9493 }
9494 pub fn block_hash(&self) -> Byte32Reader<'r> {
9495 let slice = self.as_slice();
9496 let start = molecule::unpack_number(&slice[4..]) as usize;
9497 let end = molecule::unpack_number(&slice[8..]) as usize;
9498 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
9499 }
9500 pub fn transactions(&self) -> TransactionVecReader<'r> {
9501 let slice = self.as_slice();
9502 let start = molecule::unpack_number(&slice[8..]) as usize;
9503 let end = molecule::unpack_number(&slice[12..]) as usize;
9504 TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
9505 }
9506 pub fn uncles(&self) -> UncleBlockVecReader<'r> {
9507 let slice = self.as_slice();
9508 let start = molecule::unpack_number(&slice[12..]) as usize;
9509 if self.has_extra_fields() {
9510 let end = molecule::unpack_number(&slice[16..]) as usize;
9511 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..end])
9512 } else {
9513 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..])
9514 }
9515 }
9516}
9517impl<'r> molecule::prelude::Reader<'r> for BlockTransactionsReader<'r> {
9518 type Entity = BlockTransactions;
9519 const NAME: &'static str = "BlockTransactionsReader";
9520 fn to_entity(&self) -> Self::Entity {
9521 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9522 }
9523 fn new_unchecked(slice: &'r [u8]) -> Self {
9524 BlockTransactionsReader(slice)
9525 }
9526 fn as_slice(&self) -> &'r [u8] {
9527 self.0
9528 }
9529 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9530 use molecule::verification_error as ve;
9531 let slice_len = slice.len();
9532 if slice_len < molecule::NUMBER_SIZE {
9533 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
9534 }
9535 let total_size = molecule::unpack_number(slice) as usize;
9536 if slice_len != total_size {
9537 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
9538 }
9539 if slice_len < molecule::NUMBER_SIZE * 2 {
9540 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
9541 }
9542 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
9543 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
9544 return ve!(Self, OffsetsNotMatch);
9545 }
9546 if slice_len < offset_first {
9547 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
9548 }
9549 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
9550 if field_count < Self::FIELD_COUNT {
9551 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9552 } else if !compatible && field_count > Self::FIELD_COUNT {
9553 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9554 };
9555 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
9556 .chunks_exact(molecule::NUMBER_SIZE)
9557 .map(|x| molecule::unpack_number(x) as usize)
9558 .collect();
9559 offsets.push(total_size);
9560 if offsets.windows(2).any(|i| i[0] > i[1]) {
9561 return ve!(Self, OffsetsNotMatch);
9562 }
9563 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
9564 TransactionVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
9565 UncleBlockVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
9566 Ok(())
9567 }
9568}
9569#[derive(Clone, Debug, Default)]
9570pub struct BlockTransactionsBuilder {
9571 pub(crate) block_hash: Byte32,
9572 pub(crate) transactions: TransactionVec,
9573 pub(crate) uncles: UncleBlockVec,
9574}
9575impl BlockTransactionsBuilder {
9576 pub const FIELD_COUNT: usize = 3;
9577 pub fn block_hash<T>(mut self, v: T) -> Self
9578 where
9579 T: ::core::convert::Into<Byte32>,
9580 {
9581 self.block_hash = v.into();
9582 self
9583 }
9584 pub fn transactions<T>(mut self, v: T) -> Self
9585 where
9586 T: ::core::convert::Into<TransactionVec>,
9587 {
9588 self.transactions = v.into();
9589 self
9590 }
9591 pub fn uncles<T>(mut self, v: T) -> Self
9592 where
9593 T: ::core::convert::Into<UncleBlockVec>,
9594 {
9595 self.uncles = v.into();
9596 self
9597 }
9598}
9599impl molecule::prelude::Builder for BlockTransactionsBuilder {
9600 type Entity = BlockTransactions;
9601 const NAME: &'static str = "BlockTransactionsBuilder";
9602 fn expected_length(&self) -> usize {
9603 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
9604 + self.block_hash.as_slice().len()
9605 + self.transactions.as_slice().len()
9606 + self.uncles.as_slice().len()
9607 }
9608 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
9609 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
9610 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
9611 offsets.push(total_size);
9612 total_size += self.block_hash.as_slice().len();
9613 offsets.push(total_size);
9614 total_size += self.transactions.as_slice().len();
9615 offsets.push(total_size);
9616 total_size += self.uncles.as_slice().len();
9617 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
9618 for offset in offsets.into_iter() {
9619 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
9620 }
9621 writer.write_all(self.block_hash.as_slice())?;
9622 writer.write_all(self.transactions.as_slice())?;
9623 writer.write_all(self.uncles.as_slice())?;
9624 Ok(())
9625 }
9626 fn build(&self) -> Self::Entity {
9627 let mut inner = Vec::with_capacity(self.expected_length());
9628 self.write(&mut inner)
9629 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9630 BlockTransactions::new_unchecked(inner.into())
9631 }
9632}
9633#[derive(Clone)]
9634pub struct GetBlockProposal(molecule::bytes::Bytes);
9635impl ::core::fmt::LowerHex for GetBlockProposal {
9636 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9637 use molecule::hex_string;
9638 if f.alternate() {
9639 write!(f, "0x")?;
9640 }
9641 write!(f, "{}", hex_string(self.as_slice()))
9642 }
9643}
9644impl ::core::fmt::Debug for GetBlockProposal {
9645 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9646 write!(f, "{}({:#x})", Self::NAME, self)
9647 }
9648}
9649impl ::core::fmt::Display for GetBlockProposal {
9650 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9651 write!(f, "{} {{ ", Self::NAME)?;
9652 write!(f, "{}: {}", "block_hash", self.block_hash())?;
9653 write!(f, ", {}: {}", "proposals", self.proposals())?;
9654 let extra_count = self.count_extra_fields();
9655 if extra_count != 0 {
9656 write!(f, ", .. ({} fields)", extra_count)?;
9657 }
9658 write!(f, " }}")
9659 }
9660}
9661impl ::core::default::Default for GetBlockProposal {
9662 fn default() -> Self {
9663 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9664 GetBlockProposal::new_unchecked(v)
9665 }
9666}
9667impl GetBlockProposal {
9668 const DEFAULT_VALUE: [u8; 48] = [
9669 48, 0, 0, 0, 12, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9670 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9671 ];
9672 pub const FIELD_COUNT: usize = 2;
9673 pub fn total_size(&self) -> usize {
9674 molecule::unpack_number(self.as_slice()) as usize
9675 }
9676 pub fn field_count(&self) -> usize {
9677 if self.total_size() == molecule::NUMBER_SIZE {
9678 0
9679 } else {
9680 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9681 }
9682 }
9683 pub fn count_extra_fields(&self) -> usize {
9684 self.field_count() - Self::FIELD_COUNT
9685 }
9686 pub fn has_extra_fields(&self) -> bool {
9687 Self::FIELD_COUNT != self.field_count()
9688 }
9689 pub fn block_hash(&self) -> Byte32 {
9690 let slice = self.as_slice();
9691 let start = molecule::unpack_number(&slice[4..]) as usize;
9692 let end = molecule::unpack_number(&slice[8..]) as usize;
9693 Byte32::new_unchecked(self.0.slice(start..end))
9694 }
9695 pub fn proposals(&self) -> ProposalShortIdVec {
9696 let slice = self.as_slice();
9697 let start = molecule::unpack_number(&slice[8..]) as usize;
9698 if self.has_extra_fields() {
9699 let end = molecule::unpack_number(&slice[12..]) as usize;
9700 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
9701 } else {
9702 ProposalShortIdVec::new_unchecked(self.0.slice(start..))
9703 }
9704 }
9705 pub fn as_reader<'r>(&'r self) -> GetBlockProposalReader<'r> {
9706 GetBlockProposalReader::new_unchecked(self.as_slice())
9707 }
9708}
9709impl molecule::prelude::Entity for GetBlockProposal {
9710 type Builder = GetBlockProposalBuilder;
9711 const NAME: &'static str = "GetBlockProposal";
9712 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9713 GetBlockProposal(data)
9714 }
9715 fn as_bytes(&self) -> molecule::bytes::Bytes {
9716 self.0.clone()
9717 }
9718 fn as_slice(&self) -> &[u8] {
9719 &self.0[..]
9720 }
9721 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9722 GetBlockProposalReader::from_slice(slice).map(|reader| reader.to_entity())
9723 }
9724 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9725 GetBlockProposalReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9726 }
9727 fn new_builder() -> Self::Builder {
9728 ::core::default::Default::default()
9729 }
9730 fn as_builder(self) -> Self::Builder {
9731 Self::new_builder()
9732 .block_hash(self.block_hash())
9733 .proposals(self.proposals())
9734 }
9735}
9736#[derive(Clone, Copy)]
9737pub struct GetBlockProposalReader<'r>(&'r [u8]);
9738impl<'r> ::core::fmt::LowerHex for GetBlockProposalReader<'r> {
9739 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9740 use molecule::hex_string;
9741 if f.alternate() {
9742 write!(f, "0x")?;
9743 }
9744 write!(f, "{}", hex_string(self.as_slice()))
9745 }
9746}
9747impl<'r> ::core::fmt::Debug for GetBlockProposalReader<'r> {
9748 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9749 write!(f, "{}({:#x})", Self::NAME, self)
9750 }
9751}
9752impl<'r> ::core::fmt::Display for GetBlockProposalReader<'r> {
9753 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9754 write!(f, "{} {{ ", Self::NAME)?;
9755 write!(f, "{}: {}", "block_hash", self.block_hash())?;
9756 write!(f, ", {}: {}", "proposals", self.proposals())?;
9757 let extra_count = self.count_extra_fields();
9758 if extra_count != 0 {
9759 write!(f, ", .. ({} fields)", extra_count)?;
9760 }
9761 write!(f, " }}")
9762 }
9763}
9764impl<'r> GetBlockProposalReader<'r> {
9765 pub const FIELD_COUNT: usize = 2;
9766 pub fn total_size(&self) -> usize {
9767 molecule::unpack_number(self.as_slice()) as usize
9768 }
9769 pub fn field_count(&self) -> usize {
9770 if self.total_size() == molecule::NUMBER_SIZE {
9771 0
9772 } else {
9773 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9774 }
9775 }
9776 pub fn count_extra_fields(&self) -> usize {
9777 self.field_count() - Self::FIELD_COUNT
9778 }
9779 pub fn has_extra_fields(&self) -> bool {
9780 Self::FIELD_COUNT != self.field_count()
9781 }
9782 pub fn block_hash(&self) -> Byte32Reader<'r> {
9783 let slice = self.as_slice();
9784 let start = molecule::unpack_number(&slice[4..]) as usize;
9785 let end = molecule::unpack_number(&slice[8..]) as usize;
9786 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
9787 }
9788 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
9789 let slice = self.as_slice();
9790 let start = molecule::unpack_number(&slice[8..]) as usize;
9791 if self.has_extra_fields() {
9792 let end = molecule::unpack_number(&slice[12..]) as usize;
9793 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
9794 } else {
9795 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..])
9796 }
9797 }
9798}
9799impl<'r> molecule::prelude::Reader<'r> for GetBlockProposalReader<'r> {
9800 type Entity = GetBlockProposal;
9801 const NAME: &'static str = "GetBlockProposalReader";
9802 fn to_entity(&self) -> Self::Entity {
9803 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9804 }
9805 fn new_unchecked(slice: &'r [u8]) -> Self {
9806 GetBlockProposalReader(slice)
9807 }
9808 fn as_slice(&self) -> &'r [u8] {
9809 self.0
9810 }
9811 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9812 use molecule::verification_error as ve;
9813 let slice_len = slice.len();
9814 if slice_len < molecule::NUMBER_SIZE {
9815 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
9816 }
9817 let total_size = molecule::unpack_number(slice) as usize;
9818 if slice_len != total_size {
9819 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
9820 }
9821 if slice_len < molecule::NUMBER_SIZE * 2 {
9822 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
9823 }
9824 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
9825 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
9826 return ve!(Self, OffsetsNotMatch);
9827 }
9828 if slice_len < offset_first {
9829 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
9830 }
9831 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
9832 if field_count < Self::FIELD_COUNT {
9833 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9834 } else if !compatible && field_count > Self::FIELD_COUNT {
9835 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9836 };
9837 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
9838 .chunks_exact(molecule::NUMBER_SIZE)
9839 .map(|x| molecule::unpack_number(x) as usize)
9840 .collect();
9841 offsets.push(total_size);
9842 if offsets.windows(2).any(|i| i[0] > i[1]) {
9843 return ve!(Self, OffsetsNotMatch);
9844 }
9845 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
9846 ProposalShortIdVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
9847 Ok(())
9848 }
9849}
9850#[derive(Clone, Debug, Default)]
9851pub struct GetBlockProposalBuilder {
9852 pub(crate) block_hash: Byte32,
9853 pub(crate) proposals: ProposalShortIdVec,
9854}
9855impl GetBlockProposalBuilder {
9856 pub const FIELD_COUNT: usize = 2;
9857 pub fn block_hash<T>(mut self, v: T) -> Self
9858 where
9859 T: ::core::convert::Into<Byte32>,
9860 {
9861 self.block_hash = v.into();
9862 self
9863 }
9864 pub fn proposals<T>(mut self, v: T) -> Self
9865 where
9866 T: ::core::convert::Into<ProposalShortIdVec>,
9867 {
9868 self.proposals = v.into();
9869 self
9870 }
9871}
9872impl molecule::prelude::Builder for GetBlockProposalBuilder {
9873 type Entity = GetBlockProposal;
9874 const NAME: &'static str = "GetBlockProposalBuilder";
9875 fn expected_length(&self) -> usize {
9876 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
9877 + self.block_hash.as_slice().len()
9878 + self.proposals.as_slice().len()
9879 }
9880 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
9881 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
9882 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
9883 offsets.push(total_size);
9884 total_size += self.block_hash.as_slice().len();
9885 offsets.push(total_size);
9886 total_size += self.proposals.as_slice().len();
9887 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
9888 for offset in offsets.into_iter() {
9889 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
9890 }
9891 writer.write_all(self.block_hash.as_slice())?;
9892 writer.write_all(self.proposals.as_slice())?;
9893 Ok(())
9894 }
9895 fn build(&self) -> Self::Entity {
9896 let mut inner = Vec::with_capacity(self.expected_length());
9897 self.write(&mut inner)
9898 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9899 GetBlockProposal::new_unchecked(inner.into())
9900 }
9901}
9902#[derive(Clone)]
9903pub struct BlockProposal(molecule::bytes::Bytes);
9904impl ::core::fmt::LowerHex for BlockProposal {
9905 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9906 use molecule::hex_string;
9907 if f.alternate() {
9908 write!(f, "0x")?;
9909 }
9910 write!(f, "{}", hex_string(self.as_slice()))
9911 }
9912}
9913impl ::core::fmt::Debug for BlockProposal {
9914 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9915 write!(f, "{}({:#x})", Self::NAME, self)
9916 }
9917}
9918impl ::core::fmt::Display for BlockProposal {
9919 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9920 write!(f, "{} {{ ", Self::NAME)?;
9921 write!(f, "{}: {}", "transactions", self.transactions())?;
9922 let extra_count = self.count_extra_fields();
9923 if extra_count != 0 {
9924 write!(f, ", .. ({} fields)", extra_count)?;
9925 }
9926 write!(f, " }}")
9927 }
9928}
9929impl ::core::default::Default for BlockProposal {
9930 fn default() -> Self {
9931 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9932 BlockProposal::new_unchecked(v)
9933 }
9934}
9935impl BlockProposal {
9936 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 4, 0, 0, 0];
9937 pub const FIELD_COUNT: usize = 1;
9938 pub fn total_size(&self) -> usize {
9939 molecule::unpack_number(self.as_slice()) as usize
9940 }
9941 pub fn field_count(&self) -> usize {
9942 if self.total_size() == molecule::NUMBER_SIZE {
9943 0
9944 } else {
9945 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9946 }
9947 }
9948 pub fn count_extra_fields(&self) -> usize {
9949 self.field_count() - Self::FIELD_COUNT
9950 }
9951 pub fn has_extra_fields(&self) -> bool {
9952 Self::FIELD_COUNT != self.field_count()
9953 }
9954 pub fn transactions(&self) -> TransactionVec {
9955 let slice = self.as_slice();
9956 let start = molecule::unpack_number(&slice[4..]) as usize;
9957 if self.has_extra_fields() {
9958 let end = molecule::unpack_number(&slice[8..]) as usize;
9959 TransactionVec::new_unchecked(self.0.slice(start..end))
9960 } else {
9961 TransactionVec::new_unchecked(self.0.slice(start..))
9962 }
9963 }
9964 pub fn as_reader<'r>(&'r self) -> BlockProposalReader<'r> {
9965 BlockProposalReader::new_unchecked(self.as_slice())
9966 }
9967}
9968impl molecule::prelude::Entity for BlockProposal {
9969 type Builder = BlockProposalBuilder;
9970 const NAME: &'static str = "BlockProposal";
9971 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9972 BlockProposal(data)
9973 }
9974 fn as_bytes(&self) -> molecule::bytes::Bytes {
9975 self.0.clone()
9976 }
9977 fn as_slice(&self) -> &[u8] {
9978 &self.0[..]
9979 }
9980 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9981 BlockProposalReader::from_slice(slice).map(|reader| reader.to_entity())
9982 }
9983 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9984 BlockProposalReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9985 }
9986 fn new_builder() -> Self::Builder {
9987 ::core::default::Default::default()
9988 }
9989 fn as_builder(self) -> Self::Builder {
9990 Self::new_builder().transactions(self.transactions())
9991 }
9992}
9993#[derive(Clone, Copy)]
9994pub struct BlockProposalReader<'r>(&'r [u8]);
9995impl<'r> ::core::fmt::LowerHex for BlockProposalReader<'r> {
9996 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9997 use molecule::hex_string;
9998 if f.alternate() {
9999 write!(f, "0x")?;
10000 }
10001 write!(f, "{}", hex_string(self.as_slice()))
10002 }
10003}
10004impl<'r> ::core::fmt::Debug for BlockProposalReader<'r> {
10005 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10006 write!(f, "{}({:#x})", Self::NAME, self)
10007 }
10008}
10009impl<'r> ::core::fmt::Display for BlockProposalReader<'r> {
10010 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10011 write!(f, "{} {{ ", Self::NAME)?;
10012 write!(f, "{}: {}", "transactions", self.transactions())?;
10013 let extra_count = self.count_extra_fields();
10014 if extra_count != 0 {
10015 write!(f, ", .. ({} fields)", extra_count)?;
10016 }
10017 write!(f, " }}")
10018 }
10019}
10020impl<'r> BlockProposalReader<'r> {
10021 pub const FIELD_COUNT: usize = 1;
10022 pub fn total_size(&self) -> usize {
10023 molecule::unpack_number(self.as_slice()) as usize
10024 }
10025 pub fn field_count(&self) -> usize {
10026 if self.total_size() == molecule::NUMBER_SIZE {
10027 0
10028 } else {
10029 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
10030 }
10031 }
10032 pub fn count_extra_fields(&self) -> usize {
10033 self.field_count() - Self::FIELD_COUNT
10034 }
10035 pub fn has_extra_fields(&self) -> bool {
10036 Self::FIELD_COUNT != self.field_count()
10037 }
10038 pub fn transactions(&self) -> TransactionVecReader<'r> {
10039 let slice = self.as_slice();
10040 let start = molecule::unpack_number(&slice[4..]) as usize;
10041 if self.has_extra_fields() {
10042 let end = molecule::unpack_number(&slice[8..]) as usize;
10043 TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
10044 } else {
10045 TransactionVecReader::new_unchecked(&self.as_slice()[start..])
10046 }
10047 }
10048}
10049impl<'r> molecule::prelude::Reader<'r> for BlockProposalReader<'r> {
10050 type Entity = BlockProposal;
10051 const NAME: &'static str = "BlockProposalReader";
10052 fn to_entity(&self) -> Self::Entity {
10053 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
10054 }
10055 fn new_unchecked(slice: &'r [u8]) -> Self {
10056 BlockProposalReader(slice)
10057 }
10058 fn as_slice(&self) -> &'r [u8] {
10059 self.0
10060 }
10061 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
10062 use molecule::verification_error as ve;
10063 let slice_len = slice.len();
10064 if slice_len < molecule::NUMBER_SIZE {
10065 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
10066 }
10067 let total_size = molecule::unpack_number(slice) as usize;
10068 if slice_len != total_size {
10069 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
10070 }
10071 if slice_len < molecule::NUMBER_SIZE * 2 {
10072 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
10073 }
10074 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
10075 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
10076 return ve!(Self, OffsetsNotMatch);
10077 }
10078 if slice_len < offset_first {
10079 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
10080 }
10081 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
10082 if field_count < Self::FIELD_COUNT {
10083 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
10084 } else if !compatible && field_count > Self::FIELD_COUNT {
10085 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
10086 };
10087 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
10088 .chunks_exact(molecule::NUMBER_SIZE)
10089 .map(|x| molecule::unpack_number(x) as usize)
10090 .collect();
10091 offsets.push(total_size);
10092 if offsets.windows(2).any(|i| i[0] > i[1]) {
10093 return ve!(Self, OffsetsNotMatch);
10094 }
10095 TransactionVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
10096 Ok(())
10097 }
10098}
10099#[derive(Clone, Debug, Default)]
10100pub struct BlockProposalBuilder {
10101 pub(crate) transactions: TransactionVec,
10102}
10103impl BlockProposalBuilder {
10104 pub const FIELD_COUNT: usize = 1;
10105 pub fn transactions<T>(mut self, v: T) -> Self
10106 where
10107 T: ::core::convert::Into<TransactionVec>,
10108 {
10109 self.transactions = v.into();
10110 self
10111 }
10112}
10113impl molecule::prelude::Builder for BlockProposalBuilder {
10114 type Entity = BlockProposal;
10115 const NAME: &'static str = "BlockProposalBuilder";
10116 fn expected_length(&self) -> usize {
10117 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.transactions.as_slice().len()
10118 }
10119 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
10120 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
10121 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
10122 offsets.push(total_size);
10123 total_size += self.transactions.as_slice().len();
10124 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
10125 for offset in offsets.into_iter() {
10126 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
10127 }
10128 writer.write_all(self.transactions.as_slice())?;
10129 Ok(())
10130 }
10131 fn build(&self) -> Self::Entity {
10132 let mut inner = Vec::with_capacity(self.expected_length());
10133 self.write(&mut inner)
10134 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
10135 BlockProposal::new_unchecked(inner.into())
10136 }
10137}
10138#[derive(Clone)]
10139pub struct IndexTransaction(molecule::bytes::Bytes);
10140impl ::core::fmt::LowerHex for IndexTransaction {
10141 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10142 use molecule::hex_string;
10143 if f.alternate() {
10144 write!(f, "0x")?;
10145 }
10146 write!(f, "{}", hex_string(self.as_slice()))
10147 }
10148}
10149impl ::core::fmt::Debug for IndexTransaction {
10150 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10151 write!(f, "{}({:#x})", Self::NAME, self)
10152 }
10153}
10154impl ::core::fmt::Display for IndexTransaction {
10155 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10156 write!(f, "{} {{ ", Self::NAME)?;
10157 write!(f, "{}: {}", "index", self.index())?;
10158 write!(f, ", {}: {}", "transaction", self.transaction())?;
10159 let extra_count = self.count_extra_fields();
10160 if extra_count != 0 {
10161 write!(f, ", .. ({} fields)", extra_count)?;
10162 }
10163 write!(f, " }}")
10164 }
10165}
10166impl ::core::default::Default for IndexTransaction {
10167 fn default() -> Self {
10168 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
10169 IndexTransaction::new_unchecked(v)
10170 }
10171}
10172impl IndexTransaction {
10173 const DEFAULT_VALUE: [u8; 84] = [
10174 84, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 12, 0, 0, 0, 64, 0, 0, 0,
10175 52, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0,
10176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
10177 ];
10178 pub const FIELD_COUNT: usize = 2;
10179 pub fn total_size(&self) -> usize {
10180 molecule::unpack_number(self.as_slice()) as usize
10181 }
10182 pub fn field_count(&self) -> usize {
10183 if self.total_size() == molecule::NUMBER_SIZE {
10184 0
10185 } else {
10186 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
10187 }
10188 }
10189 pub fn count_extra_fields(&self) -> usize {
10190 self.field_count() - Self::FIELD_COUNT
10191 }
10192 pub fn has_extra_fields(&self) -> bool {
10193 Self::FIELD_COUNT != self.field_count()
10194 }
10195 pub fn index(&self) -> Uint32 {
10196 let slice = self.as_slice();
10197 let start = molecule::unpack_number(&slice[4..]) as usize;
10198 let end = molecule::unpack_number(&slice[8..]) as usize;
10199 Uint32::new_unchecked(self.0.slice(start..end))
10200 }
10201 pub fn transaction(&self) -> Transaction {
10202 let slice = self.as_slice();
10203 let start = molecule::unpack_number(&slice[8..]) as usize;
10204 if self.has_extra_fields() {
10205 let end = molecule::unpack_number(&slice[12..]) as usize;
10206 Transaction::new_unchecked(self.0.slice(start..end))
10207 } else {
10208 Transaction::new_unchecked(self.0.slice(start..))
10209 }
10210 }
10211 pub fn as_reader<'r>(&'r self) -> IndexTransactionReader<'r> {
10212 IndexTransactionReader::new_unchecked(self.as_slice())
10213 }
10214}
10215impl molecule::prelude::Entity for IndexTransaction {
10216 type Builder = IndexTransactionBuilder;
10217 const NAME: &'static str = "IndexTransaction";
10218 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
10219 IndexTransaction(data)
10220 }
10221 fn as_bytes(&self) -> molecule::bytes::Bytes {
10222 self.0.clone()
10223 }
10224 fn as_slice(&self) -> &[u8] {
10225 &self.0[..]
10226 }
10227 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10228 IndexTransactionReader::from_slice(slice).map(|reader| reader.to_entity())
10229 }
10230 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10231 IndexTransactionReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
10232 }
10233 fn new_builder() -> Self::Builder {
10234 ::core::default::Default::default()
10235 }
10236 fn as_builder(self) -> Self::Builder {
10237 Self::new_builder()
10238 .index(self.index())
10239 .transaction(self.transaction())
10240 }
10241}
10242#[derive(Clone, Copy)]
10243pub struct IndexTransactionReader<'r>(&'r [u8]);
10244impl<'r> ::core::fmt::LowerHex for IndexTransactionReader<'r> {
10245 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10246 use molecule::hex_string;
10247 if f.alternate() {
10248 write!(f, "0x")?;
10249 }
10250 write!(f, "{}", hex_string(self.as_slice()))
10251 }
10252}
10253impl<'r> ::core::fmt::Debug for IndexTransactionReader<'r> {
10254 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10255 write!(f, "{}({:#x})", Self::NAME, self)
10256 }
10257}
10258impl<'r> ::core::fmt::Display for IndexTransactionReader<'r> {
10259 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10260 write!(f, "{} {{ ", Self::NAME)?;
10261 write!(f, "{}: {}", "index", self.index())?;
10262 write!(f, ", {}: {}", "transaction", self.transaction())?;
10263 let extra_count = self.count_extra_fields();
10264 if extra_count != 0 {
10265 write!(f, ", .. ({} fields)", extra_count)?;
10266 }
10267 write!(f, " }}")
10268 }
10269}
10270impl<'r> IndexTransactionReader<'r> {
10271 pub const FIELD_COUNT: usize = 2;
10272 pub fn total_size(&self) -> usize {
10273 molecule::unpack_number(self.as_slice()) as usize
10274 }
10275 pub fn field_count(&self) -> usize {
10276 if self.total_size() == molecule::NUMBER_SIZE {
10277 0
10278 } else {
10279 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
10280 }
10281 }
10282 pub fn count_extra_fields(&self) -> usize {
10283 self.field_count() - Self::FIELD_COUNT
10284 }
10285 pub fn has_extra_fields(&self) -> bool {
10286 Self::FIELD_COUNT != self.field_count()
10287 }
10288 pub fn index(&self) -> Uint32Reader<'r> {
10289 let slice = self.as_slice();
10290 let start = molecule::unpack_number(&slice[4..]) as usize;
10291 let end = molecule::unpack_number(&slice[8..]) as usize;
10292 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
10293 }
10294 pub fn transaction(&self) -> TransactionReader<'r> {
10295 let slice = self.as_slice();
10296 let start = molecule::unpack_number(&slice[8..]) as usize;
10297 if self.has_extra_fields() {
10298 let end = molecule::unpack_number(&slice[12..]) as usize;
10299 TransactionReader::new_unchecked(&self.as_slice()[start..end])
10300 } else {
10301 TransactionReader::new_unchecked(&self.as_slice()[start..])
10302 }
10303 }
10304}
10305impl<'r> molecule::prelude::Reader<'r> for IndexTransactionReader<'r> {
10306 type Entity = IndexTransaction;
10307 const NAME: &'static str = "IndexTransactionReader";
10308 fn to_entity(&self) -> Self::Entity {
10309 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
10310 }
10311 fn new_unchecked(slice: &'r [u8]) -> Self {
10312 IndexTransactionReader(slice)
10313 }
10314 fn as_slice(&self) -> &'r [u8] {
10315 self.0
10316 }
10317 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
10318 use molecule::verification_error as ve;
10319 let slice_len = slice.len();
10320 if slice_len < molecule::NUMBER_SIZE {
10321 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
10322 }
10323 let total_size = molecule::unpack_number(slice) as usize;
10324 if slice_len != total_size {
10325 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
10326 }
10327 if slice_len < molecule::NUMBER_SIZE * 2 {
10328 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
10329 }
10330 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
10331 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
10332 return ve!(Self, OffsetsNotMatch);
10333 }
10334 if slice_len < offset_first {
10335 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
10336 }
10337 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
10338 if field_count < Self::FIELD_COUNT {
10339 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
10340 } else if !compatible && field_count > Self::FIELD_COUNT {
10341 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
10342 };
10343 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
10344 .chunks_exact(molecule::NUMBER_SIZE)
10345 .map(|x| molecule::unpack_number(x) as usize)
10346 .collect();
10347 offsets.push(total_size);
10348 if offsets.windows(2).any(|i| i[0] > i[1]) {
10349 return ve!(Self, OffsetsNotMatch);
10350 }
10351 Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
10352 TransactionReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
10353 Ok(())
10354 }
10355}
10356#[derive(Clone, Debug, Default)]
10357pub struct IndexTransactionBuilder {
10358 pub(crate) index: Uint32,
10359 pub(crate) transaction: Transaction,
10360}
10361impl IndexTransactionBuilder {
10362 pub const FIELD_COUNT: usize = 2;
10363 pub fn index<T>(mut self, v: T) -> Self
10364 where
10365 T: ::core::convert::Into<Uint32>,
10366 {
10367 self.index = v.into();
10368 self
10369 }
10370 pub fn transaction<T>(mut self, v: T) -> Self
10371 where
10372 T: ::core::convert::Into<Transaction>,
10373 {
10374 self.transaction = v.into();
10375 self
10376 }
10377}
10378impl molecule::prelude::Builder for IndexTransactionBuilder {
10379 type Entity = IndexTransaction;
10380 const NAME: &'static str = "IndexTransactionBuilder";
10381 fn expected_length(&self) -> usize {
10382 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
10383 + self.index.as_slice().len()
10384 + self.transaction.as_slice().len()
10385 }
10386 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
10387 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
10388 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
10389 offsets.push(total_size);
10390 total_size += self.index.as_slice().len();
10391 offsets.push(total_size);
10392 total_size += self.transaction.as_slice().len();
10393 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
10394 for offset in offsets.into_iter() {
10395 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
10396 }
10397 writer.write_all(self.index.as_slice())?;
10398 writer.write_all(self.transaction.as_slice())?;
10399 Ok(())
10400 }
10401 fn build(&self) -> Self::Entity {
10402 let mut inner = Vec::with_capacity(self.expected_length());
10403 self.write(&mut inner)
10404 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
10405 IndexTransaction::new_unchecked(inner.into())
10406 }
10407}
10408#[derive(Clone)]
10409pub struct IndexTransactionVec(molecule::bytes::Bytes);
10410impl ::core::fmt::LowerHex for IndexTransactionVec {
10411 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10412 use molecule::hex_string;
10413 if f.alternate() {
10414 write!(f, "0x")?;
10415 }
10416 write!(f, "{}", hex_string(self.as_slice()))
10417 }
10418}
10419impl ::core::fmt::Debug for IndexTransactionVec {
10420 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10421 write!(f, "{}({:#x})", Self::NAME, self)
10422 }
10423}
10424impl ::core::fmt::Display for IndexTransactionVec {
10425 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10426 write!(f, "{} [", Self::NAME)?;
10427 for i in 0..self.len() {
10428 if i == 0 {
10429 write!(f, "{}", self.get_unchecked(i))?;
10430 } else {
10431 write!(f, ", {}", self.get_unchecked(i))?;
10432 }
10433 }
10434 write!(f, "]")
10435 }
10436}
10437impl ::core::default::Default for IndexTransactionVec {
10438 fn default() -> Self {
10439 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
10440 IndexTransactionVec::new_unchecked(v)
10441 }
10442}
10443impl IndexTransactionVec {
10444 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
10445 pub fn total_size(&self) -> usize {
10446 molecule::unpack_number(self.as_slice()) as usize
10447 }
10448 pub fn item_count(&self) -> usize {
10449 if self.total_size() == molecule::NUMBER_SIZE {
10450 0
10451 } else {
10452 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
10453 }
10454 }
10455 pub fn len(&self) -> usize {
10456 self.item_count()
10457 }
10458 pub fn is_empty(&self) -> bool {
10459 self.len() == 0
10460 }
10461 pub fn get(&self, idx: usize) -> Option<IndexTransaction> {
10462 if idx >= self.len() {
10463 None
10464 } else {
10465 Some(self.get_unchecked(idx))
10466 }
10467 }
10468 pub fn get_unchecked(&self, idx: usize) -> IndexTransaction {
10469 let slice = self.as_slice();
10470 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
10471 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
10472 if idx == self.len() - 1 {
10473 IndexTransaction::new_unchecked(self.0.slice(start..))
10474 } else {
10475 let end_idx = start_idx + molecule::NUMBER_SIZE;
10476 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
10477 IndexTransaction::new_unchecked(self.0.slice(start..end))
10478 }
10479 }
10480 pub fn as_reader<'r>(&'r self) -> IndexTransactionVecReader<'r> {
10481 IndexTransactionVecReader::new_unchecked(self.as_slice())
10482 }
10483}
10484impl molecule::prelude::Entity for IndexTransactionVec {
10485 type Builder = IndexTransactionVecBuilder;
10486 const NAME: &'static str = "IndexTransactionVec";
10487 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
10488 IndexTransactionVec(data)
10489 }
10490 fn as_bytes(&self) -> molecule::bytes::Bytes {
10491 self.0.clone()
10492 }
10493 fn as_slice(&self) -> &[u8] {
10494 &self.0[..]
10495 }
10496 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10497 IndexTransactionVecReader::from_slice(slice).map(|reader| reader.to_entity())
10498 }
10499 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10500 IndexTransactionVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
10501 }
10502 fn new_builder() -> Self::Builder {
10503 ::core::default::Default::default()
10504 }
10505 fn as_builder(self) -> Self::Builder {
10506 Self::new_builder().extend(self.into_iter())
10507 }
10508}
10509#[derive(Clone, Copy)]
10510pub struct IndexTransactionVecReader<'r>(&'r [u8]);
10511impl<'r> ::core::fmt::LowerHex for IndexTransactionVecReader<'r> {
10512 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10513 use molecule::hex_string;
10514 if f.alternate() {
10515 write!(f, "0x")?;
10516 }
10517 write!(f, "{}", hex_string(self.as_slice()))
10518 }
10519}
10520impl<'r> ::core::fmt::Debug for IndexTransactionVecReader<'r> {
10521 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10522 write!(f, "{}({:#x})", Self::NAME, self)
10523 }
10524}
10525impl<'r> ::core::fmt::Display for IndexTransactionVecReader<'r> {
10526 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10527 write!(f, "{} [", Self::NAME)?;
10528 for i in 0..self.len() {
10529 if i == 0 {
10530 write!(f, "{}", self.get_unchecked(i))?;
10531 } else {
10532 write!(f, ", {}", self.get_unchecked(i))?;
10533 }
10534 }
10535 write!(f, "]")
10536 }
10537}
10538impl<'r> IndexTransactionVecReader<'r> {
10539 pub fn total_size(&self) -> usize {
10540 molecule::unpack_number(self.as_slice()) as usize
10541 }
10542 pub fn item_count(&self) -> usize {
10543 if self.total_size() == molecule::NUMBER_SIZE {
10544 0
10545 } else {
10546 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
10547 }
10548 }
10549 pub fn len(&self) -> usize {
10550 self.item_count()
10551 }
10552 pub fn is_empty(&self) -> bool {
10553 self.len() == 0
10554 }
10555 pub fn get(&self, idx: usize) -> Option<IndexTransactionReader<'r>> {
10556 if idx >= self.len() {
10557 None
10558 } else {
10559 Some(self.get_unchecked(idx))
10560 }
10561 }
10562 pub fn get_unchecked(&self, idx: usize) -> IndexTransactionReader<'r> {
10563 let slice = self.as_slice();
10564 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
10565 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
10566 if idx == self.len() - 1 {
10567 IndexTransactionReader::new_unchecked(&self.as_slice()[start..])
10568 } else {
10569 let end_idx = start_idx + molecule::NUMBER_SIZE;
10570 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
10571 IndexTransactionReader::new_unchecked(&self.as_slice()[start..end])
10572 }
10573 }
10574}
10575impl<'r> molecule::prelude::Reader<'r> for IndexTransactionVecReader<'r> {
10576 type Entity = IndexTransactionVec;
10577 const NAME: &'static str = "IndexTransactionVecReader";
10578 fn to_entity(&self) -> Self::Entity {
10579 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
10580 }
10581 fn new_unchecked(slice: &'r [u8]) -> Self {
10582 IndexTransactionVecReader(slice)
10583 }
10584 fn as_slice(&self) -> &'r [u8] {
10585 self.0
10586 }
10587 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
10588 use molecule::verification_error as ve;
10589 let slice_len = slice.len();
10590 if slice_len < molecule::NUMBER_SIZE {
10591 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
10592 }
10593 let total_size = molecule::unpack_number(slice) as usize;
10594 if slice_len != total_size {
10595 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
10596 }
10597 if slice_len == molecule::NUMBER_SIZE {
10598 return Ok(());
10599 }
10600 if slice_len < molecule::NUMBER_SIZE * 2 {
10601 return ve!(
10602 Self,
10603 TotalSizeNotMatch,
10604 molecule::NUMBER_SIZE * 2,
10605 slice_len
10606 );
10607 }
10608 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
10609 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
10610 return ve!(Self, OffsetsNotMatch);
10611 }
10612 if slice_len < offset_first {
10613 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
10614 }
10615 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
10616 .chunks_exact(molecule::NUMBER_SIZE)
10617 .map(|x| molecule::unpack_number(x) as usize)
10618 .collect();
10619 offsets.push(total_size);
10620 if offsets.windows(2).any(|i| i[0] > i[1]) {
10621 return ve!(Self, OffsetsNotMatch);
10622 }
10623 for pair in offsets.windows(2) {
10624 let start = pair[0];
10625 let end = pair[1];
10626 IndexTransactionReader::verify(&slice[start..end], compatible)?;
10627 }
10628 Ok(())
10629 }
10630}
10631#[derive(Clone, Debug, Default)]
10632pub struct IndexTransactionVecBuilder(pub(crate) Vec<IndexTransaction>);
10633impl IndexTransactionVecBuilder {
10634 pub fn set(mut self, v: Vec<IndexTransaction>) -> Self {
10635 self.0 = v;
10636 self
10637 }
10638 pub fn push<T>(mut self, v: T) -> Self
10639 where
10640 T: ::core::convert::Into<IndexTransaction>,
10641 {
10642 self.0.push(v.into());
10643 self
10644 }
10645 pub fn extend<T: ::core::iter::IntoIterator<Item = IndexTransaction>>(
10646 mut self,
10647 iter: T,
10648 ) -> Self {
10649 self.0.extend(iter);
10650 self
10651 }
10652 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<IndexTransaction>
10653 where
10654 T: ::core::convert::Into<IndexTransaction>,
10655 {
10656 self.0
10657 .get_mut(index)
10658 .map(|item| ::core::mem::replace(item, v.into()))
10659 }
10660}
10661impl molecule::prelude::Builder for IndexTransactionVecBuilder {
10662 type Entity = IndexTransactionVec;
10663 const NAME: &'static str = "IndexTransactionVecBuilder";
10664 fn expected_length(&self) -> usize {
10665 molecule::NUMBER_SIZE * (self.0.len() + 1)
10666 + self
10667 .0
10668 .iter()
10669 .map(|inner| inner.as_slice().len())
10670 .sum::<usize>()
10671 }
10672 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
10673 let item_count = self.0.len();
10674 if item_count == 0 {
10675 writer.write_all(&molecule::pack_number(
10676 molecule::NUMBER_SIZE as molecule::Number,
10677 ))?;
10678 } else {
10679 let (total_size, offsets) = self.0.iter().fold(
10680 (
10681 molecule::NUMBER_SIZE * (item_count + 1),
10682 Vec::with_capacity(item_count),
10683 ),
10684 |(start, mut offsets), inner| {
10685 offsets.push(start);
10686 (start + inner.as_slice().len(), offsets)
10687 },
10688 );
10689 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
10690 for offset in offsets.into_iter() {
10691 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
10692 }
10693 for inner in self.0.iter() {
10694 writer.write_all(inner.as_slice())?;
10695 }
10696 }
10697 Ok(())
10698 }
10699 fn build(&self) -> Self::Entity {
10700 let mut inner = Vec::with_capacity(self.expected_length());
10701 self.write(&mut inner)
10702 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
10703 IndexTransactionVec::new_unchecked(inner.into())
10704 }
10705}
10706pub struct IndexTransactionVecIterator(IndexTransactionVec, usize, usize);
10707impl ::core::iter::Iterator for IndexTransactionVecIterator {
10708 type Item = IndexTransaction;
10709 fn next(&mut self) -> Option<Self::Item> {
10710 if self.1 >= self.2 {
10711 None
10712 } else {
10713 let ret = self.0.get_unchecked(self.1);
10714 self.1 += 1;
10715 Some(ret)
10716 }
10717 }
10718}
10719impl ::core::iter::ExactSizeIterator for IndexTransactionVecIterator {
10720 fn len(&self) -> usize {
10721 self.2 - self.1
10722 }
10723}
10724impl ::core::iter::IntoIterator for IndexTransactionVec {
10725 type Item = IndexTransaction;
10726 type IntoIter = IndexTransactionVecIterator;
10727 fn into_iter(self) -> Self::IntoIter {
10728 let len = self.len();
10729 IndexTransactionVecIterator(self, 0, len)
10730 }
10731}
10732impl<'r> IndexTransactionVecReader<'r> {
10733 pub fn iter<'t>(&'t self) -> IndexTransactionVecReaderIterator<'t, 'r> {
10734 IndexTransactionVecReaderIterator(&self, 0, self.len())
10735 }
10736}
10737pub struct IndexTransactionVecReaderIterator<'t, 'r>(
10738 &'t IndexTransactionVecReader<'r>,
10739 usize,
10740 usize,
10741);
10742impl<'t: 'r, 'r> ::core::iter::Iterator for IndexTransactionVecReaderIterator<'t, 'r> {
10743 type Item = IndexTransactionReader<'t>;
10744 fn next(&mut self) -> Option<Self::Item> {
10745 if self.1 >= self.2 {
10746 None
10747 } else {
10748 let ret = self.0.get_unchecked(self.1);
10749 self.1 += 1;
10750 Some(ret)
10751 }
10752 }
10753}
10754impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for IndexTransactionVecReaderIterator<'t, 'r> {
10755 fn len(&self) -> usize {
10756 self.2 - self.1
10757 }
10758}
10759impl<T> ::core::iter::FromIterator<T> for IndexTransactionVec
10760where
10761 T: Into<IndexTransaction>,
10762{
10763 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
10764 Self::new_builder()
10765 .extend(iter.into_iter().map(Into::into))
10766 .build()
10767 }
10768}
10769impl<T> From<Vec<T>> for IndexTransactionVec
10770where
10771 T: Into<IndexTransaction>,
10772{
10773 fn from(v: Vec<T>) -> Self {
10774 v.into_iter().collect()
10775 }
10776}
10777#[derive(Clone)]
10778pub struct BlockFilterMessage(molecule::bytes::Bytes);
10779impl ::core::fmt::LowerHex for BlockFilterMessage {
10780 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10781 use molecule::hex_string;
10782 if f.alternate() {
10783 write!(f, "0x")?;
10784 }
10785 write!(f, "{}", hex_string(self.as_slice()))
10786 }
10787}
10788impl ::core::fmt::Debug for BlockFilterMessage {
10789 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10790 write!(f, "{}({:#x})", Self::NAME, self)
10791 }
10792}
10793impl ::core::fmt::Display for BlockFilterMessage {
10794 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10795 write!(f, "{}(", Self::NAME)?;
10796 self.to_enum().display_inner(f)?;
10797 write!(f, ")")
10798 }
10799}
10800impl ::core::default::Default for BlockFilterMessage {
10801 fn default() -> Self {
10802 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
10803 BlockFilterMessage::new_unchecked(v)
10804 }
10805}
10806impl BlockFilterMessage {
10807 const DEFAULT_VALUE: [u8; 12] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
10808 pub const ITEMS_COUNT: usize = 6;
10809 pub fn item_id(&self) -> molecule::Number {
10810 molecule::unpack_number(self.as_slice())
10811 }
10812 pub fn to_enum(&self) -> BlockFilterMessageUnion {
10813 let inner = self.0.slice(molecule::NUMBER_SIZE..);
10814 match self.item_id() {
10815 0 => GetBlockFilters::new_unchecked(inner).into(),
10816 1 => BlockFilters::new_unchecked(inner).into(),
10817 2 => GetBlockFilterHashes::new_unchecked(inner).into(),
10818 3 => BlockFilterHashes::new_unchecked(inner).into(),
10819 4 => GetBlockFilterCheckPoints::new_unchecked(inner).into(),
10820 5 => BlockFilterCheckPoints::new_unchecked(inner).into(),
10821 _ => panic!("{}: invalid data", Self::NAME),
10822 }
10823 }
10824 pub fn as_reader<'r>(&'r self) -> BlockFilterMessageReader<'r> {
10825 BlockFilterMessageReader::new_unchecked(self.as_slice())
10826 }
10827}
10828impl molecule::prelude::Entity for BlockFilterMessage {
10829 type Builder = BlockFilterMessageBuilder;
10830 const NAME: &'static str = "BlockFilterMessage";
10831 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
10832 BlockFilterMessage(data)
10833 }
10834 fn as_bytes(&self) -> molecule::bytes::Bytes {
10835 self.0.clone()
10836 }
10837 fn as_slice(&self) -> &[u8] {
10838 &self.0[..]
10839 }
10840 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10841 BlockFilterMessageReader::from_slice(slice).map(|reader| reader.to_entity())
10842 }
10843 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10844 BlockFilterMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
10845 }
10846 fn new_builder() -> Self::Builder {
10847 ::core::default::Default::default()
10848 }
10849 fn as_builder(self) -> Self::Builder {
10850 Self::new_builder().set(self.to_enum())
10851 }
10852}
10853#[derive(Clone, Copy)]
10854pub struct BlockFilterMessageReader<'r>(&'r [u8]);
10855impl<'r> ::core::fmt::LowerHex for BlockFilterMessageReader<'r> {
10856 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10857 use molecule::hex_string;
10858 if f.alternate() {
10859 write!(f, "0x")?;
10860 }
10861 write!(f, "{}", hex_string(self.as_slice()))
10862 }
10863}
10864impl<'r> ::core::fmt::Debug for BlockFilterMessageReader<'r> {
10865 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10866 write!(f, "{}({:#x})", Self::NAME, self)
10867 }
10868}
10869impl<'r> ::core::fmt::Display for BlockFilterMessageReader<'r> {
10870 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10871 write!(f, "{}(", Self::NAME)?;
10872 self.to_enum().display_inner(f)?;
10873 write!(f, ")")
10874 }
10875}
10876impl<'r> BlockFilterMessageReader<'r> {
10877 pub const ITEMS_COUNT: usize = 6;
10878 pub fn item_id(&self) -> molecule::Number {
10879 molecule::unpack_number(self.as_slice())
10880 }
10881 pub fn to_enum(&self) -> BlockFilterMessageUnionReader<'r> {
10882 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
10883 match self.item_id() {
10884 0 => GetBlockFiltersReader::new_unchecked(inner).into(),
10885 1 => BlockFiltersReader::new_unchecked(inner).into(),
10886 2 => GetBlockFilterHashesReader::new_unchecked(inner).into(),
10887 3 => BlockFilterHashesReader::new_unchecked(inner).into(),
10888 4 => GetBlockFilterCheckPointsReader::new_unchecked(inner).into(),
10889 5 => BlockFilterCheckPointsReader::new_unchecked(inner).into(),
10890 _ => panic!("{}: invalid data", Self::NAME),
10891 }
10892 }
10893}
10894impl<'r> molecule::prelude::Reader<'r> for BlockFilterMessageReader<'r> {
10895 type Entity = BlockFilterMessage;
10896 const NAME: &'static str = "BlockFilterMessageReader";
10897 fn to_entity(&self) -> Self::Entity {
10898 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
10899 }
10900 fn new_unchecked(slice: &'r [u8]) -> Self {
10901 BlockFilterMessageReader(slice)
10902 }
10903 fn as_slice(&self) -> &'r [u8] {
10904 self.0
10905 }
10906 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
10907 use molecule::verification_error as ve;
10908 let slice_len = slice.len();
10909 if slice_len < molecule::NUMBER_SIZE {
10910 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
10911 }
10912 let item_id = molecule::unpack_number(slice);
10913 let inner_slice = &slice[molecule::NUMBER_SIZE..];
10914 match item_id {
10915 0 => GetBlockFiltersReader::verify(inner_slice, compatible),
10916 1 => BlockFiltersReader::verify(inner_slice, compatible),
10917 2 => GetBlockFilterHashesReader::verify(inner_slice, compatible),
10918 3 => BlockFilterHashesReader::verify(inner_slice, compatible),
10919 4 => GetBlockFilterCheckPointsReader::verify(inner_slice, compatible),
10920 5 => BlockFilterCheckPointsReader::verify(inner_slice, compatible),
10921 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
10922 }?;
10923 Ok(())
10924 }
10925}
10926#[derive(Clone, Debug, Default)]
10927pub struct BlockFilterMessageBuilder(pub(crate) BlockFilterMessageUnion);
10928impl BlockFilterMessageBuilder {
10929 pub const ITEMS_COUNT: usize = 6;
10930 pub fn set<I>(mut self, v: I) -> Self
10931 where
10932 I: ::core::convert::Into<BlockFilterMessageUnion>,
10933 {
10934 self.0 = v.into();
10935 self
10936 }
10937}
10938impl molecule::prelude::Builder for BlockFilterMessageBuilder {
10939 type Entity = BlockFilterMessage;
10940 const NAME: &'static str = "BlockFilterMessageBuilder";
10941 fn expected_length(&self) -> usize {
10942 molecule::NUMBER_SIZE + self.0.as_slice().len()
10943 }
10944 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
10945 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
10946 writer.write_all(self.0.as_slice())
10947 }
10948 fn build(&self) -> Self::Entity {
10949 let mut inner = Vec::with_capacity(self.expected_length());
10950 self.write(&mut inner)
10951 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
10952 BlockFilterMessage::new_unchecked(inner.into())
10953 }
10954}
10955#[derive(Debug, Clone)]
10956pub enum BlockFilterMessageUnion {
10957 GetBlockFilters(GetBlockFilters),
10958 BlockFilters(BlockFilters),
10959 GetBlockFilterHashes(GetBlockFilterHashes),
10960 BlockFilterHashes(BlockFilterHashes),
10961 GetBlockFilterCheckPoints(GetBlockFilterCheckPoints),
10962 BlockFilterCheckPoints(BlockFilterCheckPoints),
10963}
10964#[derive(Debug, Clone, Copy)]
10965pub enum BlockFilterMessageUnionReader<'r> {
10966 GetBlockFilters(GetBlockFiltersReader<'r>),
10967 BlockFilters(BlockFiltersReader<'r>),
10968 GetBlockFilterHashes(GetBlockFilterHashesReader<'r>),
10969 BlockFilterHashes(BlockFilterHashesReader<'r>),
10970 GetBlockFilterCheckPoints(GetBlockFilterCheckPointsReader<'r>),
10971 BlockFilterCheckPoints(BlockFilterCheckPointsReader<'r>),
10972}
10973impl ::core::default::Default for BlockFilterMessageUnion {
10974 fn default() -> Self {
10975 BlockFilterMessageUnion::GetBlockFilters(::core::default::Default::default())
10976 }
10977}
10978impl ::core::fmt::Display for BlockFilterMessageUnion {
10979 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10980 match self {
10981 BlockFilterMessageUnion::GetBlockFilters(ref item) => {
10982 write!(f, "{}::{}({})", Self::NAME, GetBlockFilters::NAME, item)
10983 }
10984 BlockFilterMessageUnion::BlockFilters(ref item) => {
10985 write!(f, "{}::{}({})", Self::NAME, BlockFilters::NAME, item)
10986 }
10987 BlockFilterMessageUnion::GetBlockFilterHashes(ref item) => {
10988 write!(
10989 f,
10990 "{}::{}({})",
10991 Self::NAME,
10992 GetBlockFilterHashes::NAME,
10993 item
10994 )
10995 }
10996 BlockFilterMessageUnion::BlockFilterHashes(ref item) => {
10997 write!(f, "{}::{}({})", Self::NAME, BlockFilterHashes::NAME, item)
10998 }
10999 BlockFilterMessageUnion::GetBlockFilterCheckPoints(ref item) => {
11000 write!(
11001 f,
11002 "{}::{}({})",
11003 Self::NAME,
11004 GetBlockFilterCheckPoints::NAME,
11005 item
11006 )
11007 }
11008 BlockFilterMessageUnion::BlockFilterCheckPoints(ref item) => {
11009 write!(
11010 f,
11011 "{}::{}({})",
11012 Self::NAME,
11013 BlockFilterCheckPoints::NAME,
11014 item
11015 )
11016 }
11017 }
11018 }
11019}
11020impl<'r> ::core::fmt::Display for BlockFilterMessageUnionReader<'r> {
11021 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11022 match self {
11023 BlockFilterMessageUnionReader::GetBlockFilters(ref item) => {
11024 write!(f, "{}::{}({})", Self::NAME, GetBlockFilters::NAME, item)
11025 }
11026 BlockFilterMessageUnionReader::BlockFilters(ref item) => {
11027 write!(f, "{}::{}({})", Self::NAME, BlockFilters::NAME, item)
11028 }
11029 BlockFilterMessageUnionReader::GetBlockFilterHashes(ref item) => {
11030 write!(
11031 f,
11032 "{}::{}({})",
11033 Self::NAME,
11034 GetBlockFilterHashes::NAME,
11035 item
11036 )
11037 }
11038 BlockFilterMessageUnionReader::BlockFilterHashes(ref item) => {
11039 write!(f, "{}::{}({})", Self::NAME, BlockFilterHashes::NAME, item)
11040 }
11041 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(ref item) => {
11042 write!(
11043 f,
11044 "{}::{}({})",
11045 Self::NAME,
11046 GetBlockFilterCheckPoints::NAME,
11047 item
11048 )
11049 }
11050 BlockFilterMessageUnionReader::BlockFilterCheckPoints(ref item) => {
11051 write!(
11052 f,
11053 "{}::{}({})",
11054 Self::NAME,
11055 BlockFilterCheckPoints::NAME,
11056 item
11057 )
11058 }
11059 }
11060 }
11061}
11062impl BlockFilterMessageUnion {
11063 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11064 match self {
11065 BlockFilterMessageUnion::GetBlockFilters(ref item) => write!(f, "{}", item),
11066 BlockFilterMessageUnion::BlockFilters(ref item) => write!(f, "{}", item),
11067 BlockFilterMessageUnion::GetBlockFilterHashes(ref item) => write!(f, "{}", item),
11068 BlockFilterMessageUnion::BlockFilterHashes(ref item) => write!(f, "{}", item),
11069 BlockFilterMessageUnion::GetBlockFilterCheckPoints(ref item) => write!(f, "{}", item),
11070 BlockFilterMessageUnion::BlockFilterCheckPoints(ref item) => write!(f, "{}", item),
11071 }
11072 }
11073}
11074impl<'r> BlockFilterMessageUnionReader<'r> {
11075 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11076 match self {
11077 BlockFilterMessageUnionReader::GetBlockFilters(ref item) => write!(f, "{}", item),
11078 BlockFilterMessageUnionReader::BlockFilters(ref item) => write!(f, "{}", item),
11079 BlockFilterMessageUnionReader::GetBlockFilterHashes(ref item) => write!(f, "{}", item),
11080 BlockFilterMessageUnionReader::BlockFilterHashes(ref item) => write!(f, "{}", item),
11081 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(ref item) => {
11082 write!(f, "{}", item)
11083 }
11084 BlockFilterMessageUnionReader::BlockFilterCheckPoints(ref item) => {
11085 write!(f, "{}", item)
11086 }
11087 }
11088 }
11089}
11090impl ::core::convert::From<GetBlockFilters> for BlockFilterMessageUnion {
11091 fn from(item: GetBlockFilters) -> Self {
11092 BlockFilterMessageUnion::GetBlockFilters(item)
11093 }
11094}
11095impl ::core::convert::From<BlockFilters> for BlockFilterMessageUnion {
11096 fn from(item: BlockFilters) -> Self {
11097 BlockFilterMessageUnion::BlockFilters(item)
11098 }
11099}
11100impl ::core::convert::From<GetBlockFilterHashes> for BlockFilterMessageUnion {
11101 fn from(item: GetBlockFilterHashes) -> Self {
11102 BlockFilterMessageUnion::GetBlockFilterHashes(item)
11103 }
11104}
11105impl ::core::convert::From<BlockFilterHashes> for BlockFilterMessageUnion {
11106 fn from(item: BlockFilterHashes) -> Self {
11107 BlockFilterMessageUnion::BlockFilterHashes(item)
11108 }
11109}
11110impl ::core::convert::From<GetBlockFilterCheckPoints> for BlockFilterMessageUnion {
11111 fn from(item: GetBlockFilterCheckPoints) -> Self {
11112 BlockFilterMessageUnion::GetBlockFilterCheckPoints(item)
11113 }
11114}
11115impl ::core::convert::From<BlockFilterCheckPoints> for BlockFilterMessageUnion {
11116 fn from(item: BlockFilterCheckPoints) -> Self {
11117 BlockFilterMessageUnion::BlockFilterCheckPoints(item)
11118 }
11119}
11120impl<'r> ::core::convert::From<GetBlockFiltersReader<'r>> for BlockFilterMessageUnionReader<'r> {
11121 fn from(item: GetBlockFiltersReader<'r>) -> Self {
11122 BlockFilterMessageUnionReader::GetBlockFilters(item)
11123 }
11124}
11125impl<'r> ::core::convert::From<BlockFiltersReader<'r>> for BlockFilterMessageUnionReader<'r> {
11126 fn from(item: BlockFiltersReader<'r>) -> Self {
11127 BlockFilterMessageUnionReader::BlockFilters(item)
11128 }
11129}
11130impl<'r> ::core::convert::From<GetBlockFilterHashesReader<'r>>
11131 for BlockFilterMessageUnionReader<'r>
11132{
11133 fn from(item: GetBlockFilterHashesReader<'r>) -> Self {
11134 BlockFilterMessageUnionReader::GetBlockFilterHashes(item)
11135 }
11136}
11137impl<'r> ::core::convert::From<BlockFilterHashesReader<'r>> for BlockFilterMessageUnionReader<'r> {
11138 fn from(item: BlockFilterHashesReader<'r>) -> Self {
11139 BlockFilterMessageUnionReader::BlockFilterHashes(item)
11140 }
11141}
11142impl<'r> ::core::convert::From<GetBlockFilterCheckPointsReader<'r>>
11143 for BlockFilterMessageUnionReader<'r>
11144{
11145 fn from(item: GetBlockFilterCheckPointsReader<'r>) -> Self {
11146 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(item)
11147 }
11148}
11149impl<'r> ::core::convert::From<BlockFilterCheckPointsReader<'r>>
11150 for BlockFilterMessageUnionReader<'r>
11151{
11152 fn from(item: BlockFilterCheckPointsReader<'r>) -> Self {
11153 BlockFilterMessageUnionReader::BlockFilterCheckPoints(item)
11154 }
11155}
11156impl BlockFilterMessageUnion {
11157 pub const NAME: &'static str = "BlockFilterMessageUnion";
11158 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
11159 match self {
11160 BlockFilterMessageUnion::GetBlockFilters(item) => item.as_bytes(),
11161 BlockFilterMessageUnion::BlockFilters(item) => item.as_bytes(),
11162 BlockFilterMessageUnion::GetBlockFilterHashes(item) => item.as_bytes(),
11163 BlockFilterMessageUnion::BlockFilterHashes(item) => item.as_bytes(),
11164 BlockFilterMessageUnion::GetBlockFilterCheckPoints(item) => item.as_bytes(),
11165 BlockFilterMessageUnion::BlockFilterCheckPoints(item) => item.as_bytes(),
11166 }
11167 }
11168 pub fn as_slice(&self) -> &[u8] {
11169 match self {
11170 BlockFilterMessageUnion::GetBlockFilters(item) => item.as_slice(),
11171 BlockFilterMessageUnion::BlockFilters(item) => item.as_slice(),
11172 BlockFilterMessageUnion::GetBlockFilterHashes(item) => item.as_slice(),
11173 BlockFilterMessageUnion::BlockFilterHashes(item) => item.as_slice(),
11174 BlockFilterMessageUnion::GetBlockFilterCheckPoints(item) => item.as_slice(),
11175 BlockFilterMessageUnion::BlockFilterCheckPoints(item) => item.as_slice(),
11176 }
11177 }
11178 pub fn item_id(&self) -> molecule::Number {
11179 match self {
11180 BlockFilterMessageUnion::GetBlockFilters(_) => 0,
11181 BlockFilterMessageUnion::BlockFilters(_) => 1,
11182 BlockFilterMessageUnion::GetBlockFilterHashes(_) => 2,
11183 BlockFilterMessageUnion::BlockFilterHashes(_) => 3,
11184 BlockFilterMessageUnion::GetBlockFilterCheckPoints(_) => 4,
11185 BlockFilterMessageUnion::BlockFilterCheckPoints(_) => 5,
11186 }
11187 }
11188 pub fn item_name(&self) -> &str {
11189 match self {
11190 BlockFilterMessageUnion::GetBlockFilters(_) => "GetBlockFilters",
11191 BlockFilterMessageUnion::BlockFilters(_) => "BlockFilters",
11192 BlockFilterMessageUnion::GetBlockFilterHashes(_) => "GetBlockFilterHashes",
11193 BlockFilterMessageUnion::BlockFilterHashes(_) => "BlockFilterHashes",
11194 BlockFilterMessageUnion::GetBlockFilterCheckPoints(_) => "GetBlockFilterCheckPoints",
11195 BlockFilterMessageUnion::BlockFilterCheckPoints(_) => "BlockFilterCheckPoints",
11196 }
11197 }
11198 pub fn as_reader<'r>(&'r self) -> BlockFilterMessageUnionReader<'r> {
11199 match self {
11200 BlockFilterMessageUnion::GetBlockFilters(item) => item.as_reader().into(),
11201 BlockFilterMessageUnion::BlockFilters(item) => item.as_reader().into(),
11202 BlockFilterMessageUnion::GetBlockFilterHashes(item) => item.as_reader().into(),
11203 BlockFilterMessageUnion::BlockFilterHashes(item) => item.as_reader().into(),
11204 BlockFilterMessageUnion::GetBlockFilterCheckPoints(item) => item.as_reader().into(),
11205 BlockFilterMessageUnion::BlockFilterCheckPoints(item) => item.as_reader().into(),
11206 }
11207 }
11208}
11209impl<'r> BlockFilterMessageUnionReader<'r> {
11210 pub const NAME: &'r str = "BlockFilterMessageUnionReader";
11211 pub fn as_slice(&self) -> &'r [u8] {
11212 match self {
11213 BlockFilterMessageUnionReader::GetBlockFilters(item) => item.as_slice(),
11214 BlockFilterMessageUnionReader::BlockFilters(item) => item.as_slice(),
11215 BlockFilterMessageUnionReader::GetBlockFilterHashes(item) => item.as_slice(),
11216 BlockFilterMessageUnionReader::BlockFilterHashes(item) => item.as_slice(),
11217 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(item) => item.as_slice(),
11218 BlockFilterMessageUnionReader::BlockFilterCheckPoints(item) => item.as_slice(),
11219 }
11220 }
11221 pub fn item_id(&self) -> molecule::Number {
11222 match self {
11223 BlockFilterMessageUnionReader::GetBlockFilters(_) => 0,
11224 BlockFilterMessageUnionReader::BlockFilters(_) => 1,
11225 BlockFilterMessageUnionReader::GetBlockFilterHashes(_) => 2,
11226 BlockFilterMessageUnionReader::BlockFilterHashes(_) => 3,
11227 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(_) => 4,
11228 BlockFilterMessageUnionReader::BlockFilterCheckPoints(_) => 5,
11229 }
11230 }
11231 pub fn item_name(&self) -> &str {
11232 match self {
11233 BlockFilterMessageUnionReader::GetBlockFilters(_) => "GetBlockFilters",
11234 BlockFilterMessageUnionReader::BlockFilters(_) => "BlockFilters",
11235 BlockFilterMessageUnionReader::GetBlockFilterHashes(_) => "GetBlockFilterHashes",
11236 BlockFilterMessageUnionReader::BlockFilterHashes(_) => "BlockFilterHashes",
11237 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(_) => {
11238 "GetBlockFilterCheckPoints"
11239 }
11240 BlockFilterMessageUnionReader::BlockFilterCheckPoints(_) => "BlockFilterCheckPoints",
11241 }
11242 }
11243}
11244impl From<GetBlockFilters> for BlockFilterMessage {
11245 fn from(value: GetBlockFilters) -> Self {
11246 Self::new_builder().set(value).build()
11247 }
11248}
11249impl From<BlockFilters> for BlockFilterMessage {
11250 fn from(value: BlockFilters) -> Self {
11251 Self::new_builder().set(value).build()
11252 }
11253}
11254impl From<GetBlockFilterHashes> for BlockFilterMessage {
11255 fn from(value: GetBlockFilterHashes) -> Self {
11256 Self::new_builder().set(value).build()
11257 }
11258}
11259impl From<BlockFilterHashes> for BlockFilterMessage {
11260 fn from(value: BlockFilterHashes) -> Self {
11261 Self::new_builder().set(value).build()
11262 }
11263}
11264impl From<GetBlockFilterCheckPoints> for BlockFilterMessage {
11265 fn from(value: GetBlockFilterCheckPoints) -> Self {
11266 Self::new_builder().set(value).build()
11267 }
11268}
11269impl From<BlockFilterCheckPoints> for BlockFilterMessage {
11270 fn from(value: BlockFilterCheckPoints) -> Self {
11271 Self::new_builder().set(value).build()
11272 }
11273}
11274#[derive(Clone)]
11275pub struct GetBlockFilters(molecule::bytes::Bytes);
11276impl ::core::fmt::LowerHex for GetBlockFilters {
11277 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11278 use molecule::hex_string;
11279 if f.alternate() {
11280 write!(f, "0x")?;
11281 }
11282 write!(f, "{}", hex_string(self.as_slice()))
11283 }
11284}
11285impl ::core::fmt::Debug for GetBlockFilters {
11286 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11287 write!(f, "{}({:#x})", Self::NAME, self)
11288 }
11289}
11290impl ::core::fmt::Display for GetBlockFilters {
11291 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11292 write!(f, "{} {{ ", Self::NAME)?;
11293 write!(f, "{}: {}", "start_number", self.start_number())?;
11294 write!(f, " }}")
11295 }
11296}
11297impl ::core::default::Default for GetBlockFilters {
11298 fn default() -> Self {
11299 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
11300 GetBlockFilters::new_unchecked(v)
11301 }
11302}
11303impl GetBlockFilters {
11304 const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
11305 pub const TOTAL_SIZE: usize = 8;
11306 pub const FIELD_SIZES: [usize; 1] = [8];
11307 pub const FIELD_COUNT: usize = 1;
11308 pub fn start_number(&self) -> Uint64 {
11309 Uint64::new_unchecked(self.0.slice(0..8))
11310 }
11311 pub fn as_reader<'r>(&'r self) -> GetBlockFiltersReader<'r> {
11312 GetBlockFiltersReader::new_unchecked(self.as_slice())
11313 }
11314}
11315impl molecule::prelude::Entity for GetBlockFilters {
11316 type Builder = GetBlockFiltersBuilder;
11317 const NAME: &'static str = "GetBlockFilters";
11318 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
11319 GetBlockFilters(data)
11320 }
11321 fn as_bytes(&self) -> molecule::bytes::Bytes {
11322 self.0.clone()
11323 }
11324 fn as_slice(&self) -> &[u8] {
11325 &self.0[..]
11326 }
11327 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11328 GetBlockFiltersReader::from_slice(slice).map(|reader| reader.to_entity())
11329 }
11330 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11331 GetBlockFiltersReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
11332 }
11333 fn new_builder() -> Self::Builder {
11334 ::core::default::Default::default()
11335 }
11336 fn as_builder(self) -> Self::Builder {
11337 Self::new_builder().start_number(self.start_number())
11338 }
11339}
11340#[derive(Clone, Copy)]
11341pub struct GetBlockFiltersReader<'r>(&'r [u8]);
11342impl<'r> ::core::fmt::LowerHex for GetBlockFiltersReader<'r> {
11343 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11344 use molecule::hex_string;
11345 if f.alternate() {
11346 write!(f, "0x")?;
11347 }
11348 write!(f, "{}", hex_string(self.as_slice()))
11349 }
11350}
11351impl<'r> ::core::fmt::Debug for GetBlockFiltersReader<'r> {
11352 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11353 write!(f, "{}({:#x})", Self::NAME, self)
11354 }
11355}
11356impl<'r> ::core::fmt::Display for GetBlockFiltersReader<'r> {
11357 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11358 write!(f, "{} {{ ", Self::NAME)?;
11359 write!(f, "{}: {}", "start_number", self.start_number())?;
11360 write!(f, " }}")
11361 }
11362}
11363impl<'r> GetBlockFiltersReader<'r> {
11364 pub const TOTAL_SIZE: usize = 8;
11365 pub const FIELD_SIZES: [usize; 1] = [8];
11366 pub const FIELD_COUNT: usize = 1;
11367 pub fn start_number(&self) -> Uint64Reader<'r> {
11368 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
11369 }
11370}
11371impl<'r> molecule::prelude::Reader<'r> for GetBlockFiltersReader<'r> {
11372 type Entity = GetBlockFilters;
11373 const NAME: &'static str = "GetBlockFiltersReader";
11374 fn to_entity(&self) -> Self::Entity {
11375 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
11376 }
11377 fn new_unchecked(slice: &'r [u8]) -> Self {
11378 GetBlockFiltersReader(slice)
11379 }
11380 fn as_slice(&self) -> &'r [u8] {
11381 self.0
11382 }
11383 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
11384 use molecule::verification_error as ve;
11385 let slice_len = slice.len();
11386 if slice_len != Self::TOTAL_SIZE {
11387 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
11388 }
11389 Ok(())
11390 }
11391}
11392#[derive(Clone, Debug, Default)]
11393pub struct GetBlockFiltersBuilder {
11394 pub(crate) start_number: Uint64,
11395}
11396impl GetBlockFiltersBuilder {
11397 pub const TOTAL_SIZE: usize = 8;
11398 pub const FIELD_SIZES: [usize; 1] = [8];
11399 pub const FIELD_COUNT: usize = 1;
11400 pub fn start_number<T>(mut self, v: T) -> Self
11401 where
11402 T: ::core::convert::Into<Uint64>,
11403 {
11404 self.start_number = v.into();
11405 self
11406 }
11407}
11408impl molecule::prelude::Builder for GetBlockFiltersBuilder {
11409 type Entity = GetBlockFilters;
11410 const NAME: &'static str = "GetBlockFiltersBuilder";
11411 fn expected_length(&self) -> usize {
11412 Self::TOTAL_SIZE
11413 }
11414 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
11415 writer.write_all(self.start_number.as_slice())?;
11416 Ok(())
11417 }
11418 fn build(&self) -> Self::Entity {
11419 let mut inner = Vec::with_capacity(self.expected_length());
11420 self.write(&mut inner)
11421 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
11422 GetBlockFilters::new_unchecked(inner.into())
11423 }
11424}
11425#[derive(Clone)]
11426pub struct BlockFilters(molecule::bytes::Bytes);
11427impl ::core::fmt::LowerHex for BlockFilters {
11428 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11429 use molecule::hex_string;
11430 if f.alternate() {
11431 write!(f, "0x")?;
11432 }
11433 write!(f, "{}", hex_string(self.as_slice()))
11434 }
11435}
11436impl ::core::fmt::Debug for BlockFilters {
11437 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11438 write!(f, "{}({:#x})", Self::NAME, self)
11439 }
11440}
11441impl ::core::fmt::Display for BlockFilters {
11442 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11443 write!(f, "{} {{ ", Self::NAME)?;
11444 write!(f, "{}: {}", "start_number", self.start_number())?;
11445 write!(f, ", {}: {}", "block_hashes", self.block_hashes())?;
11446 write!(f, ", {}: {}", "filters", self.filters())?;
11447 let extra_count = self.count_extra_fields();
11448 if extra_count != 0 {
11449 write!(f, ", .. ({} fields)", extra_count)?;
11450 }
11451 write!(f, " }}")
11452 }
11453}
11454impl ::core::default::Default for BlockFilters {
11455 fn default() -> Self {
11456 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
11457 BlockFilters::new_unchecked(v)
11458 }
11459}
11460impl BlockFilters {
11461 const DEFAULT_VALUE: [u8; 32] = [
11462 32, 0, 0, 0, 16, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
11463 0, 0, 0,
11464 ];
11465 pub const FIELD_COUNT: usize = 3;
11466 pub fn total_size(&self) -> usize {
11467 molecule::unpack_number(self.as_slice()) as usize
11468 }
11469 pub fn field_count(&self) -> usize {
11470 if self.total_size() == molecule::NUMBER_SIZE {
11471 0
11472 } else {
11473 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
11474 }
11475 }
11476 pub fn count_extra_fields(&self) -> usize {
11477 self.field_count() - Self::FIELD_COUNT
11478 }
11479 pub fn has_extra_fields(&self) -> bool {
11480 Self::FIELD_COUNT != self.field_count()
11481 }
11482 pub fn start_number(&self) -> Uint64 {
11483 let slice = self.as_slice();
11484 let start = molecule::unpack_number(&slice[4..]) as usize;
11485 let end = molecule::unpack_number(&slice[8..]) as usize;
11486 Uint64::new_unchecked(self.0.slice(start..end))
11487 }
11488 pub fn block_hashes(&self) -> Byte32Vec {
11489 let slice = self.as_slice();
11490 let start = molecule::unpack_number(&slice[8..]) as usize;
11491 let end = molecule::unpack_number(&slice[12..]) as usize;
11492 Byte32Vec::new_unchecked(self.0.slice(start..end))
11493 }
11494 pub fn filters(&self) -> BytesVec {
11495 let slice = self.as_slice();
11496 let start = molecule::unpack_number(&slice[12..]) as usize;
11497 if self.has_extra_fields() {
11498 let end = molecule::unpack_number(&slice[16..]) as usize;
11499 BytesVec::new_unchecked(self.0.slice(start..end))
11500 } else {
11501 BytesVec::new_unchecked(self.0.slice(start..))
11502 }
11503 }
11504 pub fn as_reader<'r>(&'r self) -> BlockFiltersReader<'r> {
11505 BlockFiltersReader::new_unchecked(self.as_slice())
11506 }
11507}
11508impl molecule::prelude::Entity for BlockFilters {
11509 type Builder = BlockFiltersBuilder;
11510 const NAME: &'static str = "BlockFilters";
11511 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
11512 BlockFilters(data)
11513 }
11514 fn as_bytes(&self) -> molecule::bytes::Bytes {
11515 self.0.clone()
11516 }
11517 fn as_slice(&self) -> &[u8] {
11518 &self.0[..]
11519 }
11520 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11521 BlockFiltersReader::from_slice(slice).map(|reader| reader.to_entity())
11522 }
11523 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11524 BlockFiltersReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
11525 }
11526 fn new_builder() -> Self::Builder {
11527 ::core::default::Default::default()
11528 }
11529 fn as_builder(self) -> Self::Builder {
11530 Self::new_builder()
11531 .start_number(self.start_number())
11532 .block_hashes(self.block_hashes())
11533 .filters(self.filters())
11534 }
11535}
11536#[derive(Clone, Copy)]
11537pub struct BlockFiltersReader<'r>(&'r [u8]);
11538impl<'r> ::core::fmt::LowerHex for BlockFiltersReader<'r> {
11539 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11540 use molecule::hex_string;
11541 if f.alternate() {
11542 write!(f, "0x")?;
11543 }
11544 write!(f, "{}", hex_string(self.as_slice()))
11545 }
11546}
11547impl<'r> ::core::fmt::Debug for BlockFiltersReader<'r> {
11548 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11549 write!(f, "{}({:#x})", Self::NAME, self)
11550 }
11551}
11552impl<'r> ::core::fmt::Display for BlockFiltersReader<'r> {
11553 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11554 write!(f, "{} {{ ", Self::NAME)?;
11555 write!(f, "{}: {}", "start_number", self.start_number())?;
11556 write!(f, ", {}: {}", "block_hashes", self.block_hashes())?;
11557 write!(f, ", {}: {}", "filters", self.filters())?;
11558 let extra_count = self.count_extra_fields();
11559 if extra_count != 0 {
11560 write!(f, ", .. ({} fields)", extra_count)?;
11561 }
11562 write!(f, " }}")
11563 }
11564}
11565impl<'r> BlockFiltersReader<'r> {
11566 pub const FIELD_COUNT: usize = 3;
11567 pub fn total_size(&self) -> usize {
11568 molecule::unpack_number(self.as_slice()) as usize
11569 }
11570 pub fn field_count(&self) -> usize {
11571 if self.total_size() == molecule::NUMBER_SIZE {
11572 0
11573 } else {
11574 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
11575 }
11576 }
11577 pub fn count_extra_fields(&self) -> usize {
11578 self.field_count() - Self::FIELD_COUNT
11579 }
11580 pub fn has_extra_fields(&self) -> bool {
11581 Self::FIELD_COUNT != self.field_count()
11582 }
11583 pub fn start_number(&self) -> Uint64Reader<'r> {
11584 let slice = self.as_slice();
11585 let start = molecule::unpack_number(&slice[4..]) as usize;
11586 let end = molecule::unpack_number(&slice[8..]) as usize;
11587 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
11588 }
11589 pub fn block_hashes(&self) -> Byte32VecReader<'r> {
11590 let slice = self.as_slice();
11591 let start = molecule::unpack_number(&slice[8..]) as usize;
11592 let end = molecule::unpack_number(&slice[12..]) as usize;
11593 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
11594 }
11595 pub fn filters(&self) -> BytesVecReader<'r> {
11596 let slice = self.as_slice();
11597 let start = molecule::unpack_number(&slice[12..]) as usize;
11598 if self.has_extra_fields() {
11599 let end = molecule::unpack_number(&slice[16..]) as usize;
11600 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
11601 } else {
11602 BytesVecReader::new_unchecked(&self.as_slice()[start..])
11603 }
11604 }
11605}
11606impl<'r> molecule::prelude::Reader<'r> for BlockFiltersReader<'r> {
11607 type Entity = BlockFilters;
11608 const NAME: &'static str = "BlockFiltersReader";
11609 fn to_entity(&self) -> Self::Entity {
11610 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
11611 }
11612 fn new_unchecked(slice: &'r [u8]) -> Self {
11613 BlockFiltersReader(slice)
11614 }
11615 fn as_slice(&self) -> &'r [u8] {
11616 self.0
11617 }
11618 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
11619 use molecule::verification_error as ve;
11620 let slice_len = slice.len();
11621 if slice_len < molecule::NUMBER_SIZE {
11622 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
11623 }
11624 let total_size = molecule::unpack_number(slice) as usize;
11625 if slice_len != total_size {
11626 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
11627 }
11628 if slice_len < molecule::NUMBER_SIZE * 2 {
11629 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
11630 }
11631 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
11632 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
11633 return ve!(Self, OffsetsNotMatch);
11634 }
11635 if slice_len < offset_first {
11636 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
11637 }
11638 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
11639 if field_count < Self::FIELD_COUNT {
11640 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
11641 } else if !compatible && field_count > Self::FIELD_COUNT {
11642 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
11643 };
11644 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
11645 .chunks_exact(molecule::NUMBER_SIZE)
11646 .map(|x| molecule::unpack_number(x) as usize)
11647 .collect();
11648 offsets.push(total_size);
11649 if offsets.windows(2).any(|i| i[0] > i[1]) {
11650 return ve!(Self, OffsetsNotMatch);
11651 }
11652 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
11653 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
11654 BytesVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
11655 Ok(())
11656 }
11657}
11658#[derive(Clone, Debug, Default)]
11659pub struct BlockFiltersBuilder {
11660 pub(crate) start_number: Uint64,
11661 pub(crate) block_hashes: Byte32Vec,
11662 pub(crate) filters: BytesVec,
11663}
11664impl BlockFiltersBuilder {
11665 pub const FIELD_COUNT: usize = 3;
11666 pub fn start_number<T>(mut self, v: T) -> Self
11667 where
11668 T: ::core::convert::Into<Uint64>,
11669 {
11670 self.start_number = v.into();
11671 self
11672 }
11673 pub fn block_hashes<T>(mut self, v: T) -> Self
11674 where
11675 T: ::core::convert::Into<Byte32Vec>,
11676 {
11677 self.block_hashes = v.into();
11678 self
11679 }
11680 pub fn filters<T>(mut self, v: T) -> Self
11681 where
11682 T: ::core::convert::Into<BytesVec>,
11683 {
11684 self.filters = v.into();
11685 self
11686 }
11687}
11688impl molecule::prelude::Builder for BlockFiltersBuilder {
11689 type Entity = BlockFilters;
11690 const NAME: &'static str = "BlockFiltersBuilder";
11691 fn expected_length(&self) -> usize {
11692 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
11693 + self.start_number.as_slice().len()
11694 + self.block_hashes.as_slice().len()
11695 + self.filters.as_slice().len()
11696 }
11697 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
11698 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
11699 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
11700 offsets.push(total_size);
11701 total_size += self.start_number.as_slice().len();
11702 offsets.push(total_size);
11703 total_size += self.block_hashes.as_slice().len();
11704 offsets.push(total_size);
11705 total_size += self.filters.as_slice().len();
11706 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
11707 for offset in offsets.into_iter() {
11708 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
11709 }
11710 writer.write_all(self.start_number.as_slice())?;
11711 writer.write_all(self.block_hashes.as_slice())?;
11712 writer.write_all(self.filters.as_slice())?;
11713 Ok(())
11714 }
11715 fn build(&self) -> Self::Entity {
11716 let mut inner = Vec::with_capacity(self.expected_length());
11717 self.write(&mut inner)
11718 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
11719 BlockFilters::new_unchecked(inner.into())
11720 }
11721}
11722#[derive(Clone)]
11723pub struct GetBlockFilterHashes(molecule::bytes::Bytes);
11724impl ::core::fmt::LowerHex for GetBlockFilterHashes {
11725 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11726 use molecule::hex_string;
11727 if f.alternate() {
11728 write!(f, "0x")?;
11729 }
11730 write!(f, "{}", hex_string(self.as_slice()))
11731 }
11732}
11733impl ::core::fmt::Debug for GetBlockFilterHashes {
11734 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11735 write!(f, "{}({:#x})", Self::NAME, self)
11736 }
11737}
11738impl ::core::fmt::Display for GetBlockFilterHashes {
11739 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11740 write!(f, "{} {{ ", Self::NAME)?;
11741 write!(f, "{}: {}", "start_number", self.start_number())?;
11742 write!(f, " }}")
11743 }
11744}
11745impl ::core::default::Default for GetBlockFilterHashes {
11746 fn default() -> Self {
11747 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
11748 GetBlockFilterHashes::new_unchecked(v)
11749 }
11750}
11751impl GetBlockFilterHashes {
11752 const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
11753 pub const TOTAL_SIZE: usize = 8;
11754 pub const FIELD_SIZES: [usize; 1] = [8];
11755 pub const FIELD_COUNT: usize = 1;
11756 pub fn start_number(&self) -> Uint64 {
11757 Uint64::new_unchecked(self.0.slice(0..8))
11758 }
11759 pub fn as_reader<'r>(&'r self) -> GetBlockFilterHashesReader<'r> {
11760 GetBlockFilterHashesReader::new_unchecked(self.as_slice())
11761 }
11762}
11763impl molecule::prelude::Entity for GetBlockFilterHashes {
11764 type Builder = GetBlockFilterHashesBuilder;
11765 const NAME: &'static str = "GetBlockFilterHashes";
11766 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
11767 GetBlockFilterHashes(data)
11768 }
11769 fn as_bytes(&self) -> molecule::bytes::Bytes {
11770 self.0.clone()
11771 }
11772 fn as_slice(&self) -> &[u8] {
11773 &self.0[..]
11774 }
11775 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11776 GetBlockFilterHashesReader::from_slice(slice).map(|reader| reader.to_entity())
11777 }
11778 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11779 GetBlockFilterHashesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
11780 }
11781 fn new_builder() -> Self::Builder {
11782 ::core::default::Default::default()
11783 }
11784 fn as_builder(self) -> Self::Builder {
11785 Self::new_builder().start_number(self.start_number())
11786 }
11787}
11788#[derive(Clone, Copy)]
11789pub struct GetBlockFilterHashesReader<'r>(&'r [u8]);
11790impl<'r> ::core::fmt::LowerHex for GetBlockFilterHashesReader<'r> {
11791 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11792 use molecule::hex_string;
11793 if f.alternate() {
11794 write!(f, "0x")?;
11795 }
11796 write!(f, "{}", hex_string(self.as_slice()))
11797 }
11798}
11799impl<'r> ::core::fmt::Debug for GetBlockFilterHashesReader<'r> {
11800 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11801 write!(f, "{}({:#x})", Self::NAME, self)
11802 }
11803}
11804impl<'r> ::core::fmt::Display for GetBlockFilterHashesReader<'r> {
11805 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11806 write!(f, "{} {{ ", Self::NAME)?;
11807 write!(f, "{}: {}", "start_number", self.start_number())?;
11808 write!(f, " }}")
11809 }
11810}
11811impl<'r> GetBlockFilterHashesReader<'r> {
11812 pub const TOTAL_SIZE: usize = 8;
11813 pub const FIELD_SIZES: [usize; 1] = [8];
11814 pub const FIELD_COUNT: usize = 1;
11815 pub fn start_number(&self) -> Uint64Reader<'r> {
11816 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
11817 }
11818}
11819impl<'r> molecule::prelude::Reader<'r> for GetBlockFilterHashesReader<'r> {
11820 type Entity = GetBlockFilterHashes;
11821 const NAME: &'static str = "GetBlockFilterHashesReader";
11822 fn to_entity(&self) -> Self::Entity {
11823 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
11824 }
11825 fn new_unchecked(slice: &'r [u8]) -> Self {
11826 GetBlockFilterHashesReader(slice)
11827 }
11828 fn as_slice(&self) -> &'r [u8] {
11829 self.0
11830 }
11831 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
11832 use molecule::verification_error as ve;
11833 let slice_len = slice.len();
11834 if slice_len != Self::TOTAL_SIZE {
11835 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
11836 }
11837 Ok(())
11838 }
11839}
11840#[derive(Clone, Debug, Default)]
11841pub struct GetBlockFilterHashesBuilder {
11842 pub(crate) start_number: Uint64,
11843}
11844impl GetBlockFilterHashesBuilder {
11845 pub const TOTAL_SIZE: usize = 8;
11846 pub const FIELD_SIZES: [usize; 1] = [8];
11847 pub const FIELD_COUNT: usize = 1;
11848 pub fn start_number<T>(mut self, v: T) -> Self
11849 where
11850 T: ::core::convert::Into<Uint64>,
11851 {
11852 self.start_number = v.into();
11853 self
11854 }
11855}
11856impl molecule::prelude::Builder for GetBlockFilterHashesBuilder {
11857 type Entity = GetBlockFilterHashes;
11858 const NAME: &'static str = "GetBlockFilterHashesBuilder";
11859 fn expected_length(&self) -> usize {
11860 Self::TOTAL_SIZE
11861 }
11862 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
11863 writer.write_all(self.start_number.as_slice())?;
11864 Ok(())
11865 }
11866 fn build(&self) -> Self::Entity {
11867 let mut inner = Vec::with_capacity(self.expected_length());
11868 self.write(&mut inner)
11869 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
11870 GetBlockFilterHashes::new_unchecked(inner.into())
11871 }
11872}
11873#[derive(Clone)]
11874pub struct BlockFilterHashes(molecule::bytes::Bytes);
11875impl ::core::fmt::LowerHex for BlockFilterHashes {
11876 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11877 use molecule::hex_string;
11878 if f.alternate() {
11879 write!(f, "0x")?;
11880 }
11881 write!(f, "{}", hex_string(self.as_slice()))
11882 }
11883}
11884impl ::core::fmt::Debug for BlockFilterHashes {
11885 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11886 write!(f, "{}({:#x})", Self::NAME, self)
11887 }
11888}
11889impl ::core::fmt::Display for BlockFilterHashes {
11890 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11891 write!(f, "{} {{ ", Self::NAME)?;
11892 write!(f, "{}: {}", "start_number", self.start_number())?;
11893 write!(
11894 f,
11895 ", {}: {}",
11896 "parent_block_filter_hash",
11897 self.parent_block_filter_hash()
11898 )?;
11899 write!(
11900 f,
11901 ", {}: {}",
11902 "block_filter_hashes",
11903 self.block_filter_hashes()
11904 )?;
11905 let extra_count = self.count_extra_fields();
11906 if extra_count != 0 {
11907 write!(f, ", .. ({} fields)", extra_count)?;
11908 }
11909 write!(f, " }}")
11910 }
11911}
11912impl ::core::default::Default for BlockFilterHashes {
11913 fn default() -> Self {
11914 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
11915 BlockFilterHashes::new_unchecked(v)
11916 }
11917}
11918impl BlockFilterHashes {
11919 const DEFAULT_VALUE: [u8; 60] = [
11920 60, 0, 0, 0, 16, 0, 0, 0, 24, 0, 0, 0, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11921 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11922 0,
11923 ];
11924 pub const FIELD_COUNT: usize = 3;
11925 pub fn total_size(&self) -> usize {
11926 molecule::unpack_number(self.as_slice()) as usize
11927 }
11928 pub fn field_count(&self) -> usize {
11929 if self.total_size() == molecule::NUMBER_SIZE {
11930 0
11931 } else {
11932 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
11933 }
11934 }
11935 pub fn count_extra_fields(&self) -> usize {
11936 self.field_count() - Self::FIELD_COUNT
11937 }
11938 pub fn has_extra_fields(&self) -> bool {
11939 Self::FIELD_COUNT != self.field_count()
11940 }
11941 pub fn start_number(&self) -> Uint64 {
11942 let slice = self.as_slice();
11943 let start = molecule::unpack_number(&slice[4..]) as usize;
11944 let end = molecule::unpack_number(&slice[8..]) as usize;
11945 Uint64::new_unchecked(self.0.slice(start..end))
11946 }
11947 pub fn parent_block_filter_hash(&self) -> Byte32 {
11948 let slice = self.as_slice();
11949 let start = molecule::unpack_number(&slice[8..]) as usize;
11950 let end = molecule::unpack_number(&slice[12..]) as usize;
11951 Byte32::new_unchecked(self.0.slice(start..end))
11952 }
11953 pub fn block_filter_hashes(&self) -> Byte32Vec {
11954 let slice = self.as_slice();
11955 let start = molecule::unpack_number(&slice[12..]) as usize;
11956 if self.has_extra_fields() {
11957 let end = molecule::unpack_number(&slice[16..]) as usize;
11958 Byte32Vec::new_unchecked(self.0.slice(start..end))
11959 } else {
11960 Byte32Vec::new_unchecked(self.0.slice(start..))
11961 }
11962 }
11963 pub fn as_reader<'r>(&'r self) -> BlockFilterHashesReader<'r> {
11964 BlockFilterHashesReader::new_unchecked(self.as_slice())
11965 }
11966}
11967impl molecule::prelude::Entity for BlockFilterHashes {
11968 type Builder = BlockFilterHashesBuilder;
11969 const NAME: &'static str = "BlockFilterHashes";
11970 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
11971 BlockFilterHashes(data)
11972 }
11973 fn as_bytes(&self) -> molecule::bytes::Bytes {
11974 self.0.clone()
11975 }
11976 fn as_slice(&self) -> &[u8] {
11977 &self.0[..]
11978 }
11979 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11980 BlockFilterHashesReader::from_slice(slice).map(|reader| reader.to_entity())
11981 }
11982 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11983 BlockFilterHashesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
11984 }
11985 fn new_builder() -> Self::Builder {
11986 ::core::default::Default::default()
11987 }
11988 fn as_builder(self) -> Self::Builder {
11989 Self::new_builder()
11990 .start_number(self.start_number())
11991 .parent_block_filter_hash(self.parent_block_filter_hash())
11992 .block_filter_hashes(self.block_filter_hashes())
11993 }
11994}
11995#[derive(Clone, Copy)]
11996pub struct BlockFilterHashesReader<'r>(&'r [u8]);
11997impl<'r> ::core::fmt::LowerHex for BlockFilterHashesReader<'r> {
11998 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11999 use molecule::hex_string;
12000 if f.alternate() {
12001 write!(f, "0x")?;
12002 }
12003 write!(f, "{}", hex_string(self.as_slice()))
12004 }
12005}
12006impl<'r> ::core::fmt::Debug for BlockFilterHashesReader<'r> {
12007 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12008 write!(f, "{}({:#x})", Self::NAME, self)
12009 }
12010}
12011impl<'r> ::core::fmt::Display for BlockFilterHashesReader<'r> {
12012 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12013 write!(f, "{} {{ ", Self::NAME)?;
12014 write!(f, "{}: {}", "start_number", self.start_number())?;
12015 write!(
12016 f,
12017 ", {}: {}",
12018 "parent_block_filter_hash",
12019 self.parent_block_filter_hash()
12020 )?;
12021 write!(
12022 f,
12023 ", {}: {}",
12024 "block_filter_hashes",
12025 self.block_filter_hashes()
12026 )?;
12027 let extra_count = self.count_extra_fields();
12028 if extra_count != 0 {
12029 write!(f, ", .. ({} fields)", extra_count)?;
12030 }
12031 write!(f, " }}")
12032 }
12033}
12034impl<'r> BlockFilterHashesReader<'r> {
12035 pub const FIELD_COUNT: usize = 3;
12036 pub fn total_size(&self) -> usize {
12037 molecule::unpack_number(self.as_slice()) as usize
12038 }
12039 pub fn field_count(&self) -> usize {
12040 if self.total_size() == molecule::NUMBER_SIZE {
12041 0
12042 } else {
12043 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
12044 }
12045 }
12046 pub fn count_extra_fields(&self) -> usize {
12047 self.field_count() - Self::FIELD_COUNT
12048 }
12049 pub fn has_extra_fields(&self) -> bool {
12050 Self::FIELD_COUNT != self.field_count()
12051 }
12052 pub fn start_number(&self) -> Uint64Reader<'r> {
12053 let slice = self.as_slice();
12054 let start = molecule::unpack_number(&slice[4..]) as usize;
12055 let end = molecule::unpack_number(&slice[8..]) as usize;
12056 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
12057 }
12058 pub fn parent_block_filter_hash(&self) -> Byte32Reader<'r> {
12059 let slice = self.as_slice();
12060 let start = molecule::unpack_number(&slice[8..]) as usize;
12061 let end = molecule::unpack_number(&slice[12..]) as usize;
12062 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
12063 }
12064 pub fn block_filter_hashes(&self) -> Byte32VecReader<'r> {
12065 let slice = self.as_slice();
12066 let start = molecule::unpack_number(&slice[12..]) as usize;
12067 if self.has_extra_fields() {
12068 let end = molecule::unpack_number(&slice[16..]) as usize;
12069 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
12070 } else {
12071 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
12072 }
12073 }
12074}
12075impl<'r> molecule::prelude::Reader<'r> for BlockFilterHashesReader<'r> {
12076 type Entity = BlockFilterHashes;
12077 const NAME: &'static str = "BlockFilterHashesReader";
12078 fn to_entity(&self) -> Self::Entity {
12079 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
12080 }
12081 fn new_unchecked(slice: &'r [u8]) -> Self {
12082 BlockFilterHashesReader(slice)
12083 }
12084 fn as_slice(&self) -> &'r [u8] {
12085 self.0
12086 }
12087 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
12088 use molecule::verification_error as ve;
12089 let slice_len = slice.len();
12090 if slice_len < molecule::NUMBER_SIZE {
12091 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
12092 }
12093 let total_size = molecule::unpack_number(slice) as usize;
12094 if slice_len != total_size {
12095 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
12096 }
12097 if slice_len < molecule::NUMBER_SIZE * 2 {
12098 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
12099 }
12100 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
12101 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
12102 return ve!(Self, OffsetsNotMatch);
12103 }
12104 if slice_len < offset_first {
12105 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
12106 }
12107 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
12108 if field_count < Self::FIELD_COUNT {
12109 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
12110 } else if !compatible && field_count > Self::FIELD_COUNT {
12111 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
12112 };
12113 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
12114 .chunks_exact(molecule::NUMBER_SIZE)
12115 .map(|x| molecule::unpack_number(x) as usize)
12116 .collect();
12117 offsets.push(total_size);
12118 if offsets.windows(2).any(|i| i[0] > i[1]) {
12119 return ve!(Self, OffsetsNotMatch);
12120 }
12121 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
12122 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
12123 Byte32VecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
12124 Ok(())
12125 }
12126}
12127#[derive(Clone, Debug, Default)]
12128pub struct BlockFilterHashesBuilder {
12129 pub(crate) start_number: Uint64,
12130 pub(crate) parent_block_filter_hash: Byte32,
12131 pub(crate) block_filter_hashes: Byte32Vec,
12132}
12133impl BlockFilterHashesBuilder {
12134 pub const FIELD_COUNT: usize = 3;
12135 pub fn start_number<T>(mut self, v: T) -> Self
12136 where
12137 T: ::core::convert::Into<Uint64>,
12138 {
12139 self.start_number = v.into();
12140 self
12141 }
12142 pub fn parent_block_filter_hash<T>(mut self, v: T) -> Self
12143 where
12144 T: ::core::convert::Into<Byte32>,
12145 {
12146 self.parent_block_filter_hash = v.into();
12147 self
12148 }
12149 pub fn block_filter_hashes<T>(mut self, v: T) -> Self
12150 where
12151 T: ::core::convert::Into<Byte32Vec>,
12152 {
12153 self.block_filter_hashes = v.into();
12154 self
12155 }
12156}
12157impl molecule::prelude::Builder for BlockFilterHashesBuilder {
12158 type Entity = BlockFilterHashes;
12159 const NAME: &'static str = "BlockFilterHashesBuilder";
12160 fn expected_length(&self) -> usize {
12161 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
12162 + self.start_number.as_slice().len()
12163 + self.parent_block_filter_hash.as_slice().len()
12164 + self.block_filter_hashes.as_slice().len()
12165 }
12166 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
12167 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
12168 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
12169 offsets.push(total_size);
12170 total_size += self.start_number.as_slice().len();
12171 offsets.push(total_size);
12172 total_size += self.parent_block_filter_hash.as_slice().len();
12173 offsets.push(total_size);
12174 total_size += self.block_filter_hashes.as_slice().len();
12175 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
12176 for offset in offsets.into_iter() {
12177 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
12178 }
12179 writer.write_all(self.start_number.as_slice())?;
12180 writer.write_all(self.parent_block_filter_hash.as_slice())?;
12181 writer.write_all(self.block_filter_hashes.as_slice())?;
12182 Ok(())
12183 }
12184 fn build(&self) -> Self::Entity {
12185 let mut inner = Vec::with_capacity(self.expected_length());
12186 self.write(&mut inner)
12187 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
12188 BlockFilterHashes::new_unchecked(inner.into())
12189 }
12190}
12191#[derive(Clone)]
12192pub struct GetBlockFilterCheckPoints(molecule::bytes::Bytes);
12193impl ::core::fmt::LowerHex for GetBlockFilterCheckPoints {
12194 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12195 use molecule::hex_string;
12196 if f.alternate() {
12197 write!(f, "0x")?;
12198 }
12199 write!(f, "{}", hex_string(self.as_slice()))
12200 }
12201}
12202impl ::core::fmt::Debug for GetBlockFilterCheckPoints {
12203 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12204 write!(f, "{}({:#x})", Self::NAME, self)
12205 }
12206}
12207impl ::core::fmt::Display for GetBlockFilterCheckPoints {
12208 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12209 write!(f, "{} {{ ", Self::NAME)?;
12210 write!(f, "{}: {}", "start_number", self.start_number())?;
12211 write!(f, " }}")
12212 }
12213}
12214impl ::core::default::Default for GetBlockFilterCheckPoints {
12215 fn default() -> Self {
12216 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
12217 GetBlockFilterCheckPoints::new_unchecked(v)
12218 }
12219}
12220impl GetBlockFilterCheckPoints {
12221 const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
12222 pub const TOTAL_SIZE: usize = 8;
12223 pub const FIELD_SIZES: [usize; 1] = [8];
12224 pub const FIELD_COUNT: usize = 1;
12225 pub fn start_number(&self) -> Uint64 {
12226 Uint64::new_unchecked(self.0.slice(0..8))
12227 }
12228 pub fn as_reader<'r>(&'r self) -> GetBlockFilterCheckPointsReader<'r> {
12229 GetBlockFilterCheckPointsReader::new_unchecked(self.as_slice())
12230 }
12231}
12232impl molecule::prelude::Entity for GetBlockFilterCheckPoints {
12233 type Builder = GetBlockFilterCheckPointsBuilder;
12234 const NAME: &'static str = "GetBlockFilterCheckPoints";
12235 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
12236 GetBlockFilterCheckPoints(data)
12237 }
12238 fn as_bytes(&self) -> molecule::bytes::Bytes {
12239 self.0.clone()
12240 }
12241 fn as_slice(&self) -> &[u8] {
12242 &self.0[..]
12243 }
12244 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12245 GetBlockFilterCheckPointsReader::from_slice(slice).map(|reader| reader.to_entity())
12246 }
12247 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12248 GetBlockFilterCheckPointsReader::from_compatible_slice(slice)
12249 .map(|reader| reader.to_entity())
12250 }
12251 fn new_builder() -> Self::Builder {
12252 ::core::default::Default::default()
12253 }
12254 fn as_builder(self) -> Self::Builder {
12255 Self::new_builder().start_number(self.start_number())
12256 }
12257}
12258#[derive(Clone, Copy)]
12259pub struct GetBlockFilterCheckPointsReader<'r>(&'r [u8]);
12260impl<'r> ::core::fmt::LowerHex for GetBlockFilterCheckPointsReader<'r> {
12261 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12262 use molecule::hex_string;
12263 if f.alternate() {
12264 write!(f, "0x")?;
12265 }
12266 write!(f, "{}", hex_string(self.as_slice()))
12267 }
12268}
12269impl<'r> ::core::fmt::Debug for GetBlockFilterCheckPointsReader<'r> {
12270 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12271 write!(f, "{}({:#x})", Self::NAME, self)
12272 }
12273}
12274impl<'r> ::core::fmt::Display for GetBlockFilterCheckPointsReader<'r> {
12275 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12276 write!(f, "{} {{ ", Self::NAME)?;
12277 write!(f, "{}: {}", "start_number", self.start_number())?;
12278 write!(f, " }}")
12279 }
12280}
12281impl<'r> GetBlockFilterCheckPointsReader<'r> {
12282 pub const TOTAL_SIZE: usize = 8;
12283 pub const FIELD_SIZES: [usize; 1] = [8];
12284 pub const FIELD_COUNT: usize = 1;
12285 pub fn start_number(&self) -> Uint64Reader<'r> {
12286 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
12287 }
12288}
12289impl<'r> molecule::prelude::Reader<'r> for GetBlockFilterCheckPointsReader<'r> {
12290 type Entity = GetBlockFilterCheckPoints;
12291 const NAME: &'static str = "GetBlockFilterCheckPointsReader";
12292 fn to_entity(&self) -> Self::Entity {
12293 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
12294 }
12295 fn new_unchecked(slice: &'r [u8]) -> Self {
12296 GetBlockFilterCheckPointsReader(slice)
12297 }
12298 fn as_slice(&self) -> &'r [u8] {
12299 self.0
12300 }
12301 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
12302 use molecule::verification_error as ve;
12303 let slice_len = slice.len();
12304 if slice_len != Self::TOTAL_SIZE {
12305 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
12306 }
12307 Ok(())
12308 }
12309}
12310#[derive(Clone, Debug, Default)]
12311pub struct GetBlockFilterCheckPointsBuilder {
12312 pub(crate) start_number: Uint64,
12313}
12314impl GetBlockFilterCheckPointsBuilder {
12315 pub const TOTAL_SIZE: usize = 8;
12316 pub const FIELD_SIZES: [usize; 1] = [8];
12317 pub const FIELD_COUNT: usize = 1;
12318 pub fn start_number<T>(mut self, v: T) -> Self
12319 where
12320 T: ::core::convert::Into<Uint64>,
12321 {
12322 self.start_number = v.into();
12323 self
12324 }
12325}
12326impl molecule::prelude::Builder for GetBlockFilterCheckPointsBuilder {
12327 type Entity = GetBlockFilterCheckPoints;
12328 const NAME: &'static str = "GetBlockFilterCheckPointsBuilder";
12329 fn expected_length(&self) -> usize {
12330 Self::TOTAL_SIZE
12331 }
12332 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
12333 writer.write_all(self.start_number.as_slice())?;
12334 Ok(())
12335 }
12336 fn build(&self) -> Self::Entity {
12337 let mut inner = Vec::with_capacity(self.expected_length());
12338 self.write(&mut inner)
12339 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
12340 GetBlockFilterCheckPoints::new_unchecked(inner.into())
12341 }
12342}
12343#[derive(Clone)]
12344pub struct BlockFilterCheckPoints(molecule::bytes::Bytes);
12345impl ::core::fmt::LowerHex for BlockFilterCheckPoints {
12346 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12347 use molecule::hex_string;
12348 if f.alternate() {
12349 write!(f, "0x")?;
12350 }
12351 write!(f, "{}", hex_string(self.as_slice()))
12352 }
12353}
12354impl ::core::fmt::Debug for BlockFilterCheckPoints {
12355 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12356 write!(f, "{}({:#x})", Self::NAME, self)
12357 }
12358}
12359impl ::core::fmt::Display for BlockFilterCheckPoints {
12360 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12361 write!(f, "{} {{ ", Self::NAME)?;
12362 write!(f, "{}: {}", "start_number", self.start_number())?;
12363 write!(
12364 f,
12365 ", {}: {}",
12366 "block_filter_hashes",
12367 self.block_filter_hashes()
12368 )?;
12369 let extra_count = self.count_extra_fields();
12370 if extra_count != 0 {
12371 write!(f, ", .. ({} fields)", extra_count)?;
12372 }
12373 write!(f, " }}")
12374 }
12375}
12376impl ::core::default::Default for BlockFilterCheckPoints {
12377 fn default() -> Self {
12378 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
12379 BlockFilterCheckPoints::new_unchecked(v)
12380 }
12381}
12382impl BlockFilterCheckPoints {
12383 const DEFAULT_VALUE: [u8; 24] = [
12384 24, 0, 0, 0, 12, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12385 ];
12386 pub const FIELD_COUNT: usize = 2;
12387 pub fn total_size(&self) -> usize {
12388 molecule::unpack_number(self.as_slice()) as usize
12389 }
12390 pub fn field_count(&self) -> usize {
12391 if self.total_size() == molecule::NUMBER_SIZE {
12392 0
12393 } else {
12394 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
12395 }
12396 }
12397 pub fn count_extra_fields(&self) -> usize {
12398 self.field_count() - Self::FIELD_COUNT
12399 }
12400 pub fn has_extra_fields(&self) -> bool {
12401 Self::FIELD_COUNT != self.field_count()
12402 }
12403 pub fn start_number(&self) -> Uint64 {
12404 let slice = self.as_slice();
12405 let start = molecule::unpack_number(&slice[4..]) as usize;
12406 let end = molecule::unpack_number(&slice[8..]) as usize;
12407 Uint64::new_unchecked(self.0.slice(start..end))
12408 }
12409 pub fn block_filter_hashes(&self) -> Byte32Vec {
12410 let slice = self.as_slice();
12411 let start = molecule::unpack_number(&slice[8..]) as usize;
12412 if self.has_extra_fields() {
12413 let end = molecule::unpack_number(&slice[12..]) as usize;
12414 Byte32Vec::new_unchecked(self.0.slice(start..end))
12415 } else {
12416 Byte32Vec::new_unchecked(self.0.slice(start..))
12417 }
12418 }
12419 pub fn as_reader<'r>(&'r self) -> BlockFilterCheckPointsReader<'r> {
12420 BlockFilterCheckPointsReader::new_unchecked(self.as_slice())
12421 }
12422}
12423impl molecule::prelude::Entity for BlockFilterCheckPoints {
12424 type Builder = BlockFilterCheckPointsBuilder;
12425 const NAME: &'static str = "BlockFilterCheckPoints";
12426 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
12427 BlockFilterCheckPoints(data)
12428 }
12429 fn as_bytes(&self) -> molecule::bytes::Bytes {
12430 self.0.clone()
12431 }
12432 fn as_slice(&self) -> &[u8] {
12433 &self.0[..]
12434 }
12435 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12436 BlockFilterCheckPointsReader::from_slice(slice).map(|reader| reader.to_entity())
12437 }
12438 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12439 BlockFilterCheckPointsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
12440 }
12441 fn new_builder() -> Self::Builder {
12442 ::core::default::Default::default()
12443 }
12444 fn as_builder(self) -> Self::Builder {
12445 Self::new_builder()
12446 .start_number(self.start_number())
12447 .block_filter_hashes(self.block_filter_hashes())
12448 }
12449}
12450#[derive(Clone, Copy)]
12451pub struct BlockFilterCheckPointsReader<'r>(&'r [u8]);
12452impl<'r> ::core::fmt::LowerHex for BlockFilterCheckPointsReader<'r> {
12453 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12454 use molecule::hex_string;
12455 if f.alternate() {
12456 write!(f, "0x")?;
12457 }
12458 write!(f, "{}", hex_string(self.as_slice()))
12459 }
12460}
12461impl<'r> ::core::fmt::Debug for BlockFilterCheckPointsReader<'r> {
12462 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12463 write!(f, "{}({:#x})", Self::NAME, self)
12464 }
12465}
12466impl<'r> ::core::fmt::Display for BlockFilterCheckPointsReader<'r> {
12467 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12468 write!(f, "{} {{ ", Self::NAME)?;
12469 write!(f, "{}: {}", "start_number", self.start_number())?;
12470 write!(
12471 f,
12472 ", {}: {}",
12473 "block_filter_hashes",
12474 self.block_filter_hashes()
12475 )?;
12476 let extra_count = self.count_extra_fields();
12477 if extra_count != 0 {
12478 write!(f, ", .. ({} fields)", extra_count)?;
12479 }
12480 write!(f, " }}")
12481 }
12482}
12483impl<'r> BlockFilterCheckPointsReader<'r> {
12484 pub const FIELD_COUNT: usize = 2;
12485 pub fn total_size(&self) -> usize {
12486 molecule::unpack_number(self.as_slice()) as usize
12487 }
12488 pub fn field_count(&self) -> usize {
12489 if self.total_size() == molecule::NUMBER_SIZE {
12490 0
12491 } else {
12492 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
12493 }
12494 }
12495 pub fn count_extra_fields(&self) -> usize {
12496 self.field_count() - Self::FIELD_COUNT
12497 }
12498 pub fn has_extra_fields(&self) -> bool {
12499 Self::FIELD_COUNT != self.field_count()
12500 }
12501 pub fn start_number(&self) -> Uint64Reader<'r> {
12502 let slice = self.as_slice();
12503 let start = molecule::unpack_number(&slice[4..]) as usize;
12504 let end = molecule::unpack_number(&slice[8..]) as usize;
12505 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
12506 }
12507 pub fn block_filter_hashes(&self) -> Byte32VecReader<'r> {
12508 let slice = self.as_slice();
12509 let start = molecule::unpack_number(&slice[8..]) as usize;
12510 if self.has_extra_fields() {
12511 let end = molecule::unpack_number(&slice[12..]) as usize;
12512 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
12513 } else {
12514 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
12515 }
12516 }
12517}
12518impl<'r> molecule::prelude::Reader<'r> for BlockFilterCheckPointsReader<'r> {
12519 type Entity = BlockFilterCheckPoints;
12520 const NAME: &'static str = "BlockFilterCheckPointsReader";
12521 fn to_entity(&self) -> Self::Entity {
12522 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
12523 }
12524 fn new_unchecked(slice: &'r [u8]) -> Self {
12525 BlockFilterCheckPointsReader(slice)
12526 }
12527 fn as_slice(&self) -> &'r [u8] {
12528 self.0
12529 }
12530 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
12531 use molecule::verification_error as ve;
12532 let slice_len = slice.len();
12533 if slice_len < molecule::NUMBER_SIZE {
12534 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
12535 }
12536 let total_size = molecule::unpack_number(slice) as usize;
12537 if slice_len != total_size {
12538 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
12539 }
12540 if slice_len < molecule::NUMBER_SIZE * 2 {
12541 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
12542 }
12543 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
12544 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
12545 return ve!(Self, OffsetsNotMatch);
12546 }
12547 if slice_len < offset_first {
12548 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
12549 }
12550 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
12551 if field_count < Self::FIELD_COUNT {
12552 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
12553 } else if !compatible && field_count > Self::FIELD_COUNT {
12554 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
12555 };
12556 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
12557 .chunks_exact(molecule::NUMBER_SIZE)
12558 .map(|x| molecule::unpack_number(x) as usize)
12559 .collect();
12560 offsets.push(total_size);
12561 if offsets.windows(2).any(|i| i[0] > i[1]) {
12562 return ve!(Self, OffsetsNotMatch);
12563 }
12564 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
12565 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
12566 Ok(())
12567 }
12568}
12569#[derive(Clone, Debug, Default)]
12570pub struct BlockFilterCheckPointsBuilder {
12571 pub(crate) start_number: Uint64,
12572 pub(crate) block_filter_hashes: Byte32Vec,
12573}
12574impl BlockFilterCheckPointsBuilder {
12575 pub const FIELD_COUNT: usize = 2;
12576 pub fn start_number<T>(mut self, v: T) -> Self
12577 where
12578 T: ::core::convert::Into<Uint64>,
12579 {
12580 self.start_number = v.into();
12581 self
12582 }
12583 pub fn block_filter_hashes<T>(mut self, v: T) -> Self
12584 where
12585 T: ::core::convert::Into<Byte32Vec>,
12586 {
12587 self.block_filter_hashes = v.into();
12588 self
12589 }
12590}
12591impl molecule::prelude::Builder for BlockFilterCheckPointsBuilder {
12592 type Entity = BlockFilterCheckPoints;
12593 const NAME: &'static str = "BlockFilterCheckPointsBuilder";
12594 fn expected_length(&self) -> usize {
12595 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
12596 + self.start_number.as_slice().len()
12597 + self.block_filter_hashes.as_slice().len()
12598 }
12599 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
12600 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
12601 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
12602 offsets.push(total_size);
12603 total_size += self.start_number.as_slice().len();
12604 offsets.push(total_size);
12605 total_size += self.block_filter_hashes.as_slice().len();
12606 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
12607 for offset in offsets.into_iter() {
12608 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
12609 }
12610 writer.write_all(self.start_number.as_slice())?;
12611 writer.write_all(self.block_filter_hashes.as_slice())?;
12612 Ok(())
12613 }
12614 fn build(&self) -> Self::Entity {
12615 let mut inner = Vec::with_capacity(self.expected_length());
12616 self.write(&mut inner)
12617 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
12618 BlockFilterCheckPoints::new_unchecked(inner.into())
12619 }
12620}
12621#[derive(Clone)]
12622pub struct SyncMessage(molecule::bytes::Bytes);
12623impl ::core::fmt::LowerHex for SyncMessage {
12624 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12625 use molecule::hex_string;
12626 if f.alternate() {
12627 write!(f, "0x")?;
12628 }
12629 write!(f, "{}", hex_string(self.as_slice()))
12630 }
12631}
12632impl ::core::fmt::Debug for SyncMessage {
12633 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12634 write!(f, "{}({:#x})", Self::NAME, self)
12635 }
12636}
12637impl ::core::fmt::Display for SyncMessage {
12638 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12639 write!(f, "{}(", Self::NAME)?;
12640 self.to_enum().display_inner(f)?;
12641 write!(f, ")")
12642 }
12643}
12644impl ::core::default::Default for SyncMessage {
12645 fn default() -> Self {
12646 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
12647 SyncMessage::new_unchecked(v)
12648 }
12649}
12650impl SyncMessage {
12651 const DEFAULT_VALUE: [u8; 52] = [
12652 0, 0, 0, 0, 48, 0, 0, 0, 12, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12653 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12654 ];
12655 pub const ITEMS_COUNT: usize = 5;
12656 pub fn item_id(&self) -> molecule::Number {
12657 molecule::unpack_number(self.as_slice())
12658 }
12659 pub fn to_enum(&self) -> SyncMessageUnion {
12660 let inner = self.0.slice(molecule::NUMBER_SIZE..);
12661 match self.item_id() {
12662 0 => GetHeaders::new_unchecked(inner).into(),
12663 1 => SendHeaders::new_unchecked(inner).into(),
12664 2 => GetBlocks::new_unchecked(inner).into(),
12665 3 => SendBlock::new_unchecked(inner).into(),
12666 8 => InIBD::new_unchecked(inner).into(),
12667 _ => panic!("{}: invalid data", Self::NAME),
12668 }
12669 }
12670 pub fn as_reader<'r>(&'r self) -> SyncMessageReader<'r> {
12671 SyncMessageReader::new_unchecked(self.as_slice())
12672 }
12673}
12674impl molecule::prelude::Entity for SyncMessage {
12675 type Builder = SyncMessageBuilder;
12676 const NAME: &'static str = "SyncMessage";
12677 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
12678 SyncMessage(data)
12679 }
12680 fn as_bytes(&self) -> molecule::bytes::Bytes {
12681 self.0.clone()
12682 }
12683 fn as_slice(&self) -> &[u8] {
12684 &self.0[..]
12685 }
12686 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12687 SyncMessageReader::from_slice(slice).map(|reader| reader.to_entity())
12688 }
12689 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12690 SyncMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
12691 }
12692 fn new_builder() -> Self::Builder {
12693 ::core::default::Default::default()
12694 }
12695 fn as_builder(self) -> Self::Builder {
12696 Self::new_builder().set(self.to_enum())
12697 }
12698}
12699#[derive(Clone, Copy)]
12700pub struct SyncMessageReader<'r>(&'r [u8]);
12701impl<'r> ::core::fmt::LowerHex for SyncMessageReader<'r> {
12702 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12703 use molecule::hex_string;
12704 if f.alternate() {
12705 write!(f, "0x")?;
12706 }
12707 write!(f, "{}", hex_string(self.as_slice()))
12708 }
12709}
12710impl<'r> ::core::fmt::Debug for SyncMessageReader<'r> {
12711 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12712 write!(f, "{}({:#x})", Self::NAME, self)
12713 }
12714}
12715impl<'r> ::core::fmt::Display for SyncMessageReader<'r> {
12716 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12717 write!(f, "{}(", Self::NAME)?;
12718 self.to_enum().display_inner(f)?;
12719 write!(f, ")")
12720 }
12721}
12722impl<'r> SyncMessageReader<'r> {
12723 pub const ITEMS_COUNT: usize = 5;
12724 pub fn item_id(&self) -> molecule::Number {
12725 molecule::unpack_number(self.as_slice())
12726 }
12727 pub fn to_enum(&self) -> SyncMessageUnionReader<'r> {
12728 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
12729 match self.item_id() {
12730 0 => GetHeadersReader::new_unchecked(inner).into(),
12731 1 => SendHeadersReader::new_unchecked(inner).into(),
12732 2 => GetBlocksReader::new_unchecked(inner).into(),
12733 3 => SendBlockReader::new_unchecked(inner).into(),
12734 8 => InIBDReader::new_unchecked(inner).into(),
12735 _ => panic!("{}: invalid data", Self::NAME),
12736 }
12737 }
12738}
12739impl<'r> molecule::prelude::Reader<'r> for SyncMessageReader<'r> {
12740 type Entity = SyncMessage;
12741 const NAME: &'static str = "SyncMessageReader";
12742 fn to_entity(&self) -> Self::Entity {
12743 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
12744 }
12745 fn new_unchecked(slice: &'r [u8]) -> Self {
12746 SyncMessageReader(slice)
12747 }
12748 fn as_slice(&self) -> &'r [u8] {
12749 self.0
12750 }
12751 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
12752 use molecule::verification_error as ve;
12753 let slice_len = slice.len();
12754 if slice_len < molecule::NUMBER_SIZE {
12755 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
12756 }
12757 let item_id = molecule::unpack_number(slice);
12758 let inner_slice = &slice[molecule::NUMBER_SIZE..];
12759 match item_id {
12760 0 => GetHeadersReader::verify(inner_slice, compatible),
12761 1 => SendHeadersReader::verify(inner_slice, compatible),
12762 2 => GetBlocksReader::verify(inner_slice, compatible),
12763 3 => SendBlockReader::verify(inner_slice, compatible),
12764 8 => InIBDReader::verify(inner_slice, compatible),
12765 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
12766 }?;
12767 Ok(())
12768 }
12769}
12770#[derive(Clone, Debug, Default)]
12771pub struct SyncMessageBuilder(pub(crate) SyncMessageUnion);
12772impl SyncMessageBuilder {
12773 pub const ITEMS_COUNT: usize = 5;
12774 pub fn set<I>(mut self, v: I) -> Self
12775 where
12776 I: ::core::convert::Into<SyncMessageUnion>,
12777 {
12778 self.0 = v.into();
12779 self
12780 }
12781}
12782impl molecule::prelude::Builder for SyncMessageBuilder {
12783 type Entity = SyncMessage;
12784 const NAME: &'static str = "SyncMessageBuilder";
12785 fn expected_length(&self) -> usize {
12786 molecule::NUMBER_SIZE + self.0.as_slice().len()
12787 }
12788 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
12789 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
12790 writer.write_all(self.0.as_slice())
12791 }
12792 fn build(&self) -> Self::Entity {
12793 let mut inner = Vec::with_capacity(self.expected_length());
12794 self.write(&mut inner)
12795 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
12796 SyncMessage::new_unchecked(inner.into())
12797 }
12798}
12799#[derive(Debug, Clone)]
12800pub enum SyncMessageUnion {
12801 GetHeaders(GetHeaders),
12802 SendHeaders(SendHeaders),
12803 GetBlocks(GetBlocks),
12804 SendBlock(SendBlock),
12805 InIBD(InIBD),
12806}
12807#[derive(Debug, Clone, Copy)]
12808pub enum SyncMessageUnionReader<'r> {
12809 GetHeaders(GetHeadersReader<'r>),
12810 SendHeaders(SendHeadersReader<'r>),
12811 GetBlocks(GetBlocksReader<'r>),
12812 SendBlock(SendBlockReader<'r>),
12813 InIBD(InIBDReader<'r>),
12814}
12815impl ::core::default::Default for SyncMessageUnion {
12816 fn default() -> Self {
12817 SyncMessageUnion::GetHeaders(::core::default::Default::default())
12818 }
12819}
12820impl ::core::fmt::Display for SyncMessageUnion {
12821 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12822 match self {
12823 SyncMessageUnion::GetHeaders(ref item) => {
12824 write!(f, "{}::{}({})", Self::NAME, GetHeaders::NAME, item)
12825 }
12826 SyncMessageUnion::SendHeaders(ref item) => {
12827 write!(f, "{}::{}({})", Self::NAME, SendHeaders::NAME, item)
12828 }
12829 SyncMessageUnion::GetBlocks(ref item) => {
12830 write!(f, "{}::{}({})", Self::NAME, GetBlocks::NAME, item)
12831 }
12832 SyncMessageUnion::SendBlock(ref item) => {
12833 write!(f, "{}::{}({})", Self::NAME, SendBlock::NAME, item)
12834 }
12835 SyncMessageUnion::InIBD(ref item) => {
12836 write!(f, "{}::{}({})", Self::NAME, InIBD::NAME, item)
12837 }
12838 }
12839 }
12840}
12841impl<'r> ::core::fmt::Display for SyncMessageUnionReader<'r> {
12842 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12843 match self {
12844 SyncMessageUnionReader::GetHeaders(ref item) => {
12845 write!(f, "{}::{}({})", Self::NAME, GetHeaders::NAME, item)
12846 }
12847 SyncMessageUnionReader::SendHeaders(ref item) => {
12848 write!(f, "{}::{}({})", Self::NAME, SendHeaders::NAME, item)
12849 }
12850 SyncMessageUnionReader::GetBlocks(ref item) => {
12851 write!(f, "{}::{}({})", Self::NAME, GetBlocks::NAME, item)
12852 }
12853 SyncMessageUnionReader::SendBlock(ref item) => {
12854 write!(f, "{}::{}({})", Self::NAME, SendBlock::NAME, item)
12855 }
12856 SyncMessageUnionReader::InIBD(ref item) => {
12857 write!(f, "{}::{}({})", Self::NAME, InIBD::NAME, item)
12858 }
12859 }
12860 }
12861}
12862impl SyncMessageUnion {
12863 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12864 match self {
12865 SyncMessageUnion::GetHeaders(ref item) => write!(f, "{}", item),
12866 SyncMessageUnion::SendHeaders(ref item) => write!(f, "{}", item),
12867 SyncMessageUnion::GetBlocks(ref item) => write!(f, "{}", item),
12868 SyncMessageUnion::SendBlock(ref item) => write!(f, "{}", item),
12869 SyncMessageUnion::InIBD(ref item) => write!(f, "{}", item),
12870 }
12871 }
12872}
12873impl<'r> SyncMessageUnionReader<'r> {
12874 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12875 match self {
12876 SyncMessageUnionReader::GetHeaders(ref item) => write!(f, "{}", item),
12877 SyncMessageUnionReader::SendHeaders(ref item) => write!(f, "{}", item),
12878 SyncMessageUnionReader::GetBlocks(ref item) => write!(f, "{}", item),
12879 SyncMessageUnionReader::SendBlock(ref item) => write!(f, "{}", item),
12880 SyncMessageUnionReader::InIBD(ref item) => write!(f, "{}", item),
12881 }
12882 }
12883}
12884impl ::core::convert::From<GetHeaders> for SyncMessageUnion {
12885 fn from(item: GetHeaders) -> Self {
12886 SyncMessageUnion::GetHeaders(item)
12887 }
12888}
12889impl ::core::convert::From<SendHeaders> for SyncMessageUnion {
12890 fn from(item: SendHeaders) -> Self {
12891 SyncMessageUnion::SendHeaders(item)
12892 }
12893}
12894impl ::core::convert::From<GetBlocks> for SyncMessageUnion {
12895 fn from(item: GetBlocks) -> Self {
12896 SyncMessageUnion::GetBlocks(item)
12897 }
12898}
12899impl ::core::convert::From<SendBlock> for SyncMessageUnion {
12900 fn from(item: SendBlock) -> Self {
12901 SyncMessageUnion::SendBlock(item)
12902 }
12903}
12904impl ::core::convert::From<InIBD> for SyncMessageUnion {
12905 fn from(item: InIBD) -> Self {
12906 SyncMessageUnion::InIBD(item)
12907 }
12908}
12909impl<'r> ::core::convert::From<GetHeadersReader<'r>> for SyncMessageUnionReader<'r> {
12910 fn from(item: GetHeadersReader<'r>) -> Self {
12911 SyncMessageUnionReader::GetHeaders(item)
12912 }
12913}
12914impl<'r> ::core::convert::From<SendHeadersReader<'r>> for SyncMessageUnionReader<'r> {
12915 fn from(item: SendHeadersReader<'r>) -> Self {
12916 SyncMessageUnionReader::SendHeaders(item)
12917 }
12918}
12919impl<'r> ::core::convert::From<GetBlocksReader<'r>> for SyncMessageUnionReader<'r> {
12920 fn from(item: GetBlocksReader<'r>) -> Self {
12921 SyncMessageUnionReader::GetBlocks(item)
12922 }
12923}
12924impl<'r> ::core::convert::From<SendBlockReader<'r>> for SyncMessageUnionReader<'r> {
12925 fn from(item: SendBlockReader<'r>) -> Self {
12926 SyncMessageUnionReader::SendBlock(item)
12927 }
12928}
12929impl<'r> ::core::convert::From<InIBDReader<'r>> for SyncMessageUnionReader<'r> {
12930 fn from(item: InIBDReader<'r>) -> Self {
12931 SyncMessageUnionReader::InIBD(item)
12932 }
12933}
12934impl SyncMessageUnion {
12935 pub const NAME: &'static str = "SyncMessageUnion";
12936 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
12937 match self {
12938 SyncMessageUnion::GetHeaders(item) => item.as_bytes(),
12939 SyncMessageUnion::SendHeaders(item) => item.as_bytes(),
12940 SyncMessageUnion::GetBlocks(item) => item.as_bytes(),
12941 SyncMessageUnion::SendBlock(item) => item.as_bytes(),
12942 SyncMessageUnion::InIBD(item) => item.as_bytes(),
12943 }
12944 }
12945 pub fn as_slice(&self) -> &[u8] {
12946 match self {
12947 SyncMessageUnion::GetHeaders(item) => item.as_slice(),
12948 SyncMessageUnion::SendHeaders(item) => item.as_slice(),
12949 SyncMessageUnion::GetBlocks(item) => item.as_slice(),
12950 SyncMessageUnion::SendBlock(item) => item.as_slice(),
12951 SyncMessageUnion::InIBD(item) => item.as_slice(),
12952 }
12953 }
12954 pub fn item_id(&self) -> molecule::Number {
12955 match self {
12956 SyncMessageUnion::GetHeaders(_) => 0,
12957 SyncMessageUnion::SendHeaders(_) => 1,
12958 SyncMessageUnion::GetBlocks(_) => 2,
12959 SyncMessageUnion::SendBlock(_) => 3,
12960 SyncMessageUnion::InIBD(_) => 8,
12961 }
12962 }
12963 pub fn item_name(&self) -> &str {
12964 match self {
12965 SyncMessageUnion::GetHeaders(_) => "GetHeaders",
12966 SyncMessageUnion::SendHeaders(_) => "SendHeaders",
12967 SyncMessageUnion::GetBlocks(_) => "GetBlocks",
12968 SyncMessageUnion::SendBlock(_) => "SendBlock",
12969 SyncMessageUnion::InIBD(_) => "InIBD",
12970 }
12971 }
12972 pub fn as_reader<'r>(&'r self) -> SyncMessageUnionReader<'r> {
12973 match self {
12974 SyncMessageUnion::GetHeaders(item) => item.as_reader().into(),
12975 SyncMessageUnion::SendHeaders(item) => item.as_reader().into(),
12976 SyncMessageUnion::GetBlocks(item) => item.as_reader().into(),
12977 SyncMessageUnion::SendBlock(item) => item.as_reader().into(),
12978 SyncMessageUnion::InIBD(item) => item.as_reader().into(),
12979 }
12980 }
12981}
12982impl<'r> SyncMessageUnionReader<'r> {
12983 pub const NAME: &'r str = "SyncMessageUnionReader";
12984 pub fn as_slice(&self) -> &'r [u8] {
12985 match self {
12986 SyncMessageUnionReader::GetHeaders(item) => item.as_slice(),
12987 SyncMessageUnionReader::SendHeaders(item) => item.as_slice(),
12988 SyncMessageUnionReader::GetBlocks(item) => item.as_slice(),
12989 SyncMessageUnionReader::SendBlock(item) => item.as_slice(),
12990 SyncMessageUnionReader::InIBD(item) => item.as_slice(),
12991 }
12992 }
12993 pub fn item_id(&self) -> molecule::Number {
12994 match self {
12995 SyncMessageUnionReader::GetHeaders(_) => 0,
12996 SyncMessageUnionReader::SendHeaders(_) => 1,
12997 SyncMessageUnionReader::GetBlocks(_) => 2,
12998 SyncMessageUnionReader::SendBlock(_) => 3,
12999 SyncMessageUnionReader::InIBD(_) => 8,
13000 }
13001 }
13002 pub fn item_name(&self) -> &str {
13003 match self {
13004 SyncMessageUnionReader::GetHeaders(_) => "GetHeaders",
13005 SyncMessageUnionReader::SendHeaders(_) => "SendHeaders",
13006 SyncMessageUnionReader::GetBlocks(_) => "GetBlocks",
13007 SyncMessageUnionReader::SendBlock(_) => "SendBlock",
13008 SyncMessageUnionReader::InIBD(_) => "InIBD",
13009 }
13010 }
13011}
13012impl From<GetHeaders> for SyncMessage {
13013 fn from(value: GetHeaders) -> Self {
13014 Self::new_builder().set(value).build()
13015 }
13016}
13017impl From<SendHeaders> for SyncMessage {
13018 fn from(value: SendHeaders) -> Self {
13019 Self::new_builder().set(value).build()
13020 }
13021}
13022impl From<GetBlocks> for SyncMessage {
13023 fn from(value: GetBlocks) -> Self {
13024 Self::new_builder().set(value).build()
13025 }
13026}
13027impl From<SendBlock> for SyncMessage {
13028 fn from(value: SendBlock) -> Self {
13029 Self::new_builder().set(value).build()
13030 }
13031}
13032impl From<InIBD> for SyncMessage {
13033 fn from(value: InIBD) -> Self {
13034 Self::new_builder().set(value).build()
13035 }
13036}
13037#[derive(Clone)]
13038pub struct GetHeaders(molecule::bytes::Bytes);
13039impl ::core::fmt::LowerHex for GetHeaders {
13040 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13041 use molecule::hex_string;
13042 if f.alternate() {
13043 write!(f, "0x")?;
13044 }
13045 write!(f, "{}", hex_string(self.as_slice()))
13046 }
13047}
13048impl ::core::fmt::Debug for GetHeaders {
13049 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13050 write!(f, "{}({:#x})", Self::NAME, self)
13051 }
13052}
13053impl ::core::fmt::Display for GetHeaders {
13054 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13055 write!(f, "{} {{ ", Self::NAME)?;
13056 write!(f, "{}: {}", "hash_stop", self.hash_stop())?;
13057 write!(
13058 f,
13059 ", {}: {}",
13060 "block_locator_hashes",
13061 self.block_locator_hashes()
13062 )?;
13063 let extra_count = self.count_extra_fields();
13064 if extra_count != 0 {
13065 write!(f, ", .. ({} fields)", extra_count)?;
13066 }
13067 write!(f, " }}")
13068 }
13069}
13070impl ::core::default::Default for GetHeaders {
13071 fn default() -> Self {
13072 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
13073 GetHeaders::new_unchecked(v)
13074 }
13075}
13076impl GetHeaders {
13077 const DEFAULT_VALUE: [u8; 48] = [
13078 48, 0, 0, 0, 12, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13079 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13080 ];
13081 pub const FIELD_COUNT: usize = 2;
13082 pub fn total_size(&self) -> usize {
13083 molecule::unpack_number(self.as_slice()) as usize
13084 }
13085 pub fn field_count(&self) -> usize {
13086 if self.total_size() == molecule::NUMBER_SIZE {
13087 0
13088 } else {
13089 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13090 }
13091 }
13092 pub fn count_extra_fields(&self) -> usize {
13093 self.field_count() - Self::FIELD_COUNT
13094 }
13095 pub fn has_extra_fields(&self) -> bool {
13096 Self::FIELD_COUNT != self.field_count()
13097 }
13098 pub fn hash_stop(&self) -> Byte32 {
13099 let slice = self.as_slice();
13100 let start = molecule::unpack_number(&slice[4..]) as usize;
13101 let end = molecule::unpack_number(&slice[8..]) as usize;
13102 Byte32::new_unchecked(self.0.slice(start..end))
13103 }
13104 pub fn block_locator_hashes(&self) -> Byte32Vec {
13105 let slice = self.as_slice();
13106 let start = molecule::unpack_number(&slice[8..]) as usize;
13107 if self.has_extra_fields() {
13108 let end = molecule::unpack_number(&slice[12..]) as usize;
13109 Byte32Vec::new_unchecked(self.0.slice(start..end))
13110 } else {
13111 Byte32Vec::new_unchecked(self.0.slice(start..))
13112 }
13113 }
13114 pub fn as_reader<'r>(&'r self) -> GetHeadersReader<'r> {
13115 GetHeadersReader::new_unchecked(self.as_slice())
13116 }
13117}
13118impl molecule::prelude::Entity for GetHeaders {
13119 type Builder = GetHeadersBuilder;
13120 const NAME: &'static str = "GetHeaders";
13121 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
13122 GetHeaders(data)
13123 }
13124 fn as_bytes(&self) -> molecule::bytes::Bytes {
13125 self.0.clone()
13126 }
13127 fn as_slice(&self) -> &[u8] {
13128 &self.0[..]
13129 }
13130 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13131 GetHeadersReader::from_slice(slice).map(|reader| reader.to_entity())
13132 }
13133 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13134 GetHeadersReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
13135 }
13136 fn new_builder() -> Self::Builder {
13137 ::core::default::Default::default()
13138 }
13139 fn as_builder(self) -> Self::Builder {
13140 Self::new_builder()
13141 .hash_stop(self.hash_stop())
13142 .block_locator_hashes(self.block_locator_hashes())
13143 }
13144}
13145#[derive(Clone, Copy)]
13146pub struct GetHeadersReader<'r>(&'r [u8]);
13147impl<'r> ::core::fmt::LowerHex for GetHeadersReader<'r> {
13148 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13149 use molecule::hex_string;
13150 if f.alternate() {
13151 write!(f, "0x")?;
13152 }
13153 write!(f, "{}", hex_string(self.as_slice()))
13154 }
13155}
13156impl<'r> ::core::fmt::Debug for GetHeadersReader<'r> {
13157 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13158 write!(f, "{}({:#x})", Self::NAME, self)
13159 }
13160}
13161impl<'r> ::core::fmt::Display for GetHeadersReader<'r> {
13162 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13163 write!(f, "{} {{ ", Self::NAME)?;
13164 write!(f, "{}: {}", "hash_stop", self.hash_stop())?;
13165 write!(
13166 f,
13167 ", {}: {}",
13168 "block_locator_hashes",
13169 self.block_locator_hashes()
13170 )?;
13171 let extra_count = self.count_extra_fields();
13172 if extra_count != 0 {
13173 write!(f, ", .. ({} fields)", extra_count)?;
13174 }
13175 write!(f, " }}")
13176 }
13177}
13178impl<'r> GetHeadersReader<'r> {
13179 pub const FIELD_COUNT: usize = 2;
13180 pub fn total_size(&self) -> usize {
13181 molecule::unpack_number(self.as_slice()) as usize
13182 }
13183 pub fn field_count(&self) -> usize {
13184 if self.total_size() == molecule::NUMBER_SIZE {
13185 0
13186 } else {
13187 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13188 }
13189 }
13190 pub fn count_extra_fields(&self) -> usize {
13191 self.field_count() - Self::FIELD_COUNT
13192 }
13193 pub fn has_extra_fields(&self) -> bool {
13194 Self::FIELD_COUNT != self.field_count()
13195 }
13196 pub fn hash_stop(&self) -> Byte32Reader<'r> {
13197 let slice = self.as_slice();
13198 let start = molecule::unpack_number(&slice[4..]) as usize;
13199 let end = molecule::unpack_number(&slice[8..]) as usize;
13200 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
13201 }
13202 pub fn block_locator_hashes(&self) -> Byte32VecReader<'r> {
13203 let slice = self.as_slice();
13204 let start = molecule::unpack_number(&slice[8..]) as usize;
13205 if self.has_extra_fields() {
13206 let end = molecule::unpack_number(&slice[12..]) as usize;
13207 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
13208 } else {
13209 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
13210 }
13211 }
13212}
13213impl<'r> molecule::prelude::Reader<'r> for GetHeadersReader<'r> {
13214 type Entity = GetHeaders;
13215 const NAME: &'static str = "GetHeadersReader";
13216 fn to_entity(&self) -> Self::Entity {
13217 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
13218 }
13219 fn new_unchecked(slice: &'r [u8]) -> Self {
13220 GetHeadersReader(slice)
13221 }
13222 fn as_slice(&self) -> &'r [u8] {
13223 self.0
13224 }
13225 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
13226 use molecule::verification_error as ve;
13227 let slice_len = slice.len();
13228 if slice_len < molecule::NUMBER_SIZE {
13229 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
13230 }
13231 let total_size = molecule::unpack_number(slice) as usize;
13232 if slice_len != total_size {
13233 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
13234 }
13235 if slice_len < molecule::NUMBER_SIZE * 2 {
13236 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
13237 }
13238 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
13239 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
13240 return ve!(Self, OffsetsNotMatch);
13241 }
13242 if slice_len < offset_first {
13243 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
13244 }
13245 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
13246 if field_count < Self::FIELD_COUNT {
13247 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13248 } else if !compatible && field_count > Self::FIELD_COUNT {
13249 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13250 };
13251 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
13252 .chunks_exact(molecule::NUMBER_SIZE)
13253 .map(|x| molecule::unpack_number(x) as usize)
13254 .collect();
13255 offsets.push(total_size);
13256 if offsets.windows(2).any(|i| i[0] > i[1]) {
13257 return ve!(Self, OffsetsNotMatch);
13258 }
13259 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
13260 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
13261 Ok(())
13262 }
13263}
13264#[derive(Clone, Debug, Default)]
13265pub struct GetHeadersBuilder {
13266 pub(crate) hash_stop: Byte32,
13267 pub(crate) block_locator_hashes: Byte32Vec,
13268}
13269impl GetHeadersBuilder {
13270 pub const FIELD_COUNT: usize = 2;
13271 pub fn hash_stop<T>(mut self, v: T) -> Self
13272 where
13273 T: ::core::convert::Into<Byte32>,
13274 {
13275 self.hash_stop = v.into();
13276 self
13277 }
13278 pub fn block_locator_hashes<T>(mut self, v: T) -> Self
13279 where
13280 T: ::core::convert::Into<Byte32Vec>,
13281 {
13282 self.block_locator_hashes = v.into();
13283 self
13284 }
13285}
13286impl molecule::prelude::Builder for GetHeadersBuilder {
13287 type Entity = GetHeaders;
13288 const NAME: &'static str = "GetHeadersBuilder";
13289 fn expected_length(&self) -> usize {
13290 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
13291 + self.hash_stop.as_slice().len()
13292 + self.block_locator_hashes.as_slice().len()
13293 }
13294 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
13295 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
13296 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
13297 offsets.push(total_size);
13298 total_size += self.hash_stop.as_slice().len();
13299 offsets.push(total_size);
13300 total_size += self.block_locator_hashes.as_slice().len();
13301 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
13302 for offset in offsets.into_iter() {
13303 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
13304 }
13305 writer.write_all(self.hash_stop.as_slice())?;
13306 writer.write_all(self.block_locator_hashes.as_slice())?;
13307 Ok(())
13308 }
13309 fn build(&self) -> Self::Entity {
13310 let mut inner = Vec::with_capacity(self.expected_length());
13311 self.write(&mut inner)
13312 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
13313 GetHeaders::new_unchecked(inner.into())
13314 }
13315}
13316#[derive(Clone)]
13317pub struct GetBlocks(molecule::bytes::Bytes);
13318impl ::core::fmt::LowerHex for GetBlocks {
13319 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13320 use molecule::hex_string;
13321 if f.alternate() {
13322 write!(f, "0x")?;
13323 }
13324 write!(f, "{}", hex_string(self.as_slice()))
13325 }
13326}
13327impl ::core::fmt::Debug for GetBlocks {
13328 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13329 write!(f, "{}({:#x})", Self::NAME, self)
13330 }
13331}
13332impl ::core::fmt::Display for GetBlocks {
13333 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13334 write!(f, "{} {{ ", Self::NAME)?;
13335 write!(f, "{}: {}", "block_hashes", self.block_hashes())?;
13336 let extra_count = self.count_extra_fields();
13337 if extra_count != 0 {
13338 write!(f, ", .. ({} fields)", extra_count)?;
13339 }
13340 write!(f, " }}")
13341 }
13342}
13343impl ::core::default::Default for GetBlocks {
13344 fn default() -> Self {
13345 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
13346 GetBlocks::new_unchecked(v)
13347 }
13348}
13349impl GetBlocks {
13350 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
13351 pub const FIELD_COUNT: usize = 1;
13352 pub fn total_size(&self) -> usize {
13353 molecule::unpack_number(self.as_slice()) as usize
13354 }
13355 pub fn field_count(&self) -> usize {
13356 if self.total_size() == molecule::NUMBER_SIZE {
13357 0
13358 } else {
13359 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13360 }
13361 }
13362 pub fn count_extra_fields(&self) -> usize {
13363 self.field_count() - Self::FIELD_COUNT
13364 }
13365 pub fn has_extra_fields(&self) -> bool {
13366 Self::FIELD_COUNT != self.field_count()
13367 }
13368 pub fn block_hashes(&self) -> Byte32Vec {
13369 let slice = self.as_slice();
13370 let start = molecule::unpack_number(&slice[4..]) as usize;
13371 if self.has_extra_fields() {
13372 let end = molecule::unpack_number(&slice[8..]) as usize;
13373 Byte32Vec::new_unchecked(self.0.slice(start..end))
13374 } else {
13375 Byte32Vec::new_unchecked(self.0.slice(start..))
13376 }
13377 }
13378 pub fn as_reader<'r>(&'r self) -> GetBlocksReader<'r> {
13379 GetBlocksReader::new_unchecked(self.as_slice())
13380 }
13381}
13382impl molecule::prelude::Entity for GetBlocks {
13383 type Builder = GetBlocksBuilder;
13384 const NAME: &'static str = "GetBlocks";
13385 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
13386 GetBlocks(data)
13387 }
13388 fn as_bytes(&self) -> molecule::bytes::Bytes {
13389 self.0.clone()
13390 }
13391 fn as_slice(&self) -> &[u8] {
13392 &self.0[..]
13393 }
13394 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13395 GetBlocksReader::from_slice(slice).map(|reader| reader.to_entity())
13396 }
13397 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13398 GetBlocksReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
13399 }
13400 fn new_builder() -> Self::Builder {
13401 ::core::default::Default::default()
13402 }
13403 fn as_builder(self) -> Self::Builder {
13404 Self::new_builder().block_hashes(self.block_hashes())
13405 }
13406}
13407#[derive(Clone, Copy)]
13408pub struct GetBlocksReader<'r>(&'r [u8]);
13409impl<'r> ::core::fmt::LowerHex for GetBlocksReader<'r> {
13410 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13411 use molecule::hex_string;
13412 if f.alternate() {
13413 write!(f, "0x")?;
13414 }
13415 write!(f, "{}", hex_string(self.as_slice()))
13416 }
13417}
13418impl<'r> ::core::fmt::Debug for GetBlocksReader<'r> {
13419 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13420 write!(f, "{}({:#x})", Self::NAME, self)
13421 }
13422}
13423impl<'r> ::core::fmt::Display for GetBlocksReader<'r> {
13424 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13425 write!(f, "{} {{ ", Self::NAME)?;
13426 write!(f, "{}: {}", "block_hashes", self.block_hashes())?;
13427 let extra_count = self.count_extra_fields();
13428 if extra_count != 0 {
13429 write!(f, ", .. ({} fields)", extra_count)?;
13430 }
13431 write!(f, " }}")
13432 }
13433}
13434impl<'r> GetBlocksReader<'r> {
13435 pub const FIELD_COUNT: usize = 1;
13436 pub fn total_size(&self) -> usize {
13437 molecule::unpack_number(self.as_slice()) as usize
13438 }
13439 pub fn field_count(&self) -> usize {
13440 if self.total_size() == molecule::NUMBER_SIZE {
13441 0
13442 } else {
13443 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13444 }
13445 }
13446 pub fn count_extra_fields(&self) -> usize {
13447 self.field_count() - Self::FIELD_COUNT
13448 }
13449 pub fn has_extra_fields(&self) -> bool {
13450 Self::FIELD_COUNT != self.field_count()
13451 }
13452 pub fn block_hashes(&self) -> Byte32VecReader<'r> {
13453 let slice = self.as_slice();
13454 let start = molecule::unpack_number(&slice[4..]) as usize;
13455 if self.has_extra_fields() {
13456 let end = molecule::unpack_number(&slice[8..]) as usize;
13457 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
13458 } else {
13459 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
13460 }
13461 }
13462}
13463impl<'r> molecule::prelude::Reader<'r> for GetBlocksReader<'r> {
13464 type Entity = GetBlocks;
13465 const NAME: &'static str = "GetBlocksReader";
13466 fn to_entity(&self) -> Self::Entity {
13467 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
13468 }
13469 fn new_unchecked(slice: &'r [u8]) -> Self {
13470 GetBlocksReader(slice)
13471 }
13472 fn as_slice(&self) -> &'r [u8] {
13473 self.0
13474 }
13475 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
13476 use molecule::verification_error as ve;
13477 let slice_len = slice.len();
13478 if slice_len < molecule::NUMBER_SIZE {
13479 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
13480 }
13481 let total_size = molecule::unpack_number(slice) as usize;
13482 if slice_len != total_size {
13483 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
13484 }
13485 if slice_len < molecule::NUMBER_SIZE * 2 {
13486 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
13487 }
13488 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
13489 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
13490 return ve!(Self, OffsetsNotMatch);
13491 }
13492 if slice_len < offset_first {
13493 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
13494 }
13495 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
13496 if field_count < Self::FIELD_COUNT {
13497 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13498 } else if !compatible && field_count > Self::FIELD_COUNT {
13499 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13500 };
13501 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
13502 .chunks_exact(molecule::NUMBER_SIZE)
13503 .map(|x| molecule::unpack_number(x) as usize)
13504 .collect();
13505 offsets.push(total_size);
13506 if offsets.windows(2).any(|i| i[0] > i[1]) {
13507 return ve!(Self, OffsetsNotMatch);
13508 }
13509 Byte32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
13510 Ok(())
13511 }
13512}
13513#[derive(Clone, Debug, Default)]
13514pub struct GetBlocksBuilder {
13515 pub(crate) block_hashes: Byte32Vec,
13516}
13517impl GetBlocksBuilder {
13518 pub const FIELD_COUNT: usize = 1;
13519 pub fn block_hashes<T>(mut self, v: T) -> Self
13520 where
13521 T: ::core::convert::Into<Byte32Vec>,
13522 {
13523 self.block_hashes = v.into();
13524 self
13525 }
13526}
13527impl molecule::prelude::Builder for GetBlocksBuilder {
13528 type Entity = GetBlocks;
13529 const NAME: &'static str = "GetBlocksBuilder";
13530 fn expected_length(&self) -> usize {
13531 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.block_hashes.as_slice().len()
13532 }
13533 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
13534 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
13535 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
13536 offsets.push(total_size);
13537 total_size += self.block_hashes.as_slice().len();
13538 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
13539 for offset in offsets.into_iter() {
13540 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
13541 }
13542 writer.write_all(self.block_hashes.as_slice())?;
13543 Ok(())
13544 }
13545 fn build(&self) -> Self::Entity {
13546 let mut inner = Vec::with_capacity(self.expected_length());
13547 self.write(&mut inner)
13548 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
13549 GetBlocks::new_unchecked(inner.into())
13550 }
13551}
13552#[derive(Clone)]
13553pub struct SendHeaders(molecule::bytes::Bytes);
13554impl ::core::fmt::LowerHex for SendHeaders {
13555 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13556 use molecule::hex_string;
13557 if f.alternate() {
13558 write!(f, "0x")?;
13559 }
13560 write!(f, "{}", hex_string(self.as_slice()))
13561 }
13562}
13563impl ::core::fmt::Debug for SendHeaders {
13564 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13565 write!(f, "{}({:#x})", Self::NAME, self)
13566 }
13567}
13568impl ::core::fmt::Display for SendHeaders {
13569 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13570 write!(f, "{} {{ ", Self::NAME)?;
13571 write!(f, "{}: {}", "headers", self.headers())?;
13572 let extra_count = self.count_extra_fields();
13573 if extra_count != 0 {
13574 write!(f, ", .. ({} fields)", extra_count)?;
13575 }
13576 write!(f, " }}")
13577 }
13578}
13579impl ::core::default::Default for SendHeaders {
13580 fn default() -> Self {
13581 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
13582 SendHeaders::new_unchecked(v)
13583 }
13584}
13585impl SendHeaders {
13586 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
13587 pub const FIELD_COUNT: usize = 1;
13588 pub fn total_size(&self) -> usize {
13589 molecule::unpack_number(self.as_slice()) as usize
13590 }
13591 pub fn field_count(&self) -> usize {
13592 if self.total_size() == molecule::NUMBER_SIZE {
13593 0
13594 } else {
13595 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13596 }
13597 }
13598 pub fn count_extra_fields(&self) -> usize {
13599 self.field_count() - Self::FIELD_COUNT
13600 }
13601 pub fn has_extra_fields(&self) -> bool {
13602 Self::FIELD_COUNT != self.field_count()
13603 }
13604 pub fn headers(&self) -> HeaderVec {
13605 let slice = self.as_slice();
13606 let start = molecule::unpack_number(&slice[4..]) as usize;
13607 if self.has_extra_fields() {
13608 let end = molecule::unpack_number(&slice[8..]) as usize;
13609 HeaderVec::new_unchecked(self.0.slice(start..end))
13610 } else {
13611 HeaderVec::new_unchecked(self.0.slice(start..))
13612 }
13613 }
13614 pub fn as_reader<'r>(&'r self) -> SendHeadersReader<'r> {
13615 SendHeadersReader::new_unchecked(self.as_slice())
13616 }
13617}
13618impl molecule::prelude::Entity for SendHeaders {
13619 type Builder = SendHeadersBuilder;
13620 const NAME: &'static str = "SendHeaders";
13621 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
13622 SendHeaders(data)
13623 }
13624 fn as_bytes(&self) -> molecule::bytes::Bytes {
13625 self.0.clone()
13626 }
13627 fn as_slice(&self) -> &[u8] {
13628 &self.0[..]
13629 }
13630 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13631 SendHeadersReader::from_slice(slice).map(|reader| reader.to_entity())
13632 }
13633 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13634 SendHeadersReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
13635 }
13636 fn new_builder() -> Self::Builder {
13637 ::core::default::Default::default()
13638 }
13639 fn as_builder(self) -> Self::Builder {
13640 Self::new_builder().headers(self.headers())
13641 }
13642}
13643#[derive(Clone, Copy)]
13644pub struct SendHeadersReader<'r>(&'r [u8]);
13645impl<'r> ::core::fmt::LowerHex for SendHeadersReader<'r> {
13646 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13647 use molecule::hex_string;
13648 if f.alternate() {
13649 write!(f, "0x")?;
13650 }
13651 write!(f, "{}", hex_string(self.as_slice()))
13652 }
13653}
13654impl<'r> ::core::fmt::Debug for SendHeadersReader<'r> {
13655 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13656 write!(f, "{}({:#x})", Self::NAME, self)
13657 }
13658}
13659impl<'r> ::core::fmt::Display for SendHeadersReader<'r> {
13660 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13661 write!(f, "{} {{ ", Self::NAME)?;
13662 write!(f, "{}: {}", "headers", self.headers())?;
13663 let extra_count = self.count_extra_fields();
13664 if extra_count != 0 {
13665 write!(f, ", .. ({} fields)", extra_count)?;
13666 }
13667 write!(f, " }}")
13668 }
13669}
13670impl<'r> SendHeadersReader<'r> {
13671 pub const FIELD_COUNT: usize = 1;
13672 pub fn total_size(&self) -> usize {
13673 molecule::unpack_number(self.as_slice()) as usize
13674 }
13675 pub fn field_count(&self) -> usize {
13676 if self.total_size() == molecule::NUMBER_SIZE {
13677 0
13678 } else {
13679 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13680 }
13681 }
13682 pub fn count_extra_fields(&self) -> usize {
13683 self.field_count() - Self::FIELD_COUNT
13684 }
13685 pub fn has_extra_fields(&self) -> bool {
13686 Self::FIELD_COUNT != self.field_count()
13687 }
13688 pub fn headers(&self) -> HeaderVecReader<'r> {
13689 let slice = self.as_slice();
13690 let start = molecule::unpack_number(&slice[4..]) as usize;
13691 if self.has_extra_fields() {
13692 let end = molecule::unpack_number(&slice[8..]) as usize;
13693 HeaderVecReader::new_unchecked(&self.as_slice()[start..end])
13694 } else {
13695 HeaderVecReader::new_unchecked(&self.as_slice()[start..])
13696 }
13697 }
13698}
13699impl<'r> molecule::prelude::Reader<'r> for SendHeadersReader<'r> {
13700 type Entity = SendHeaders;
13701 const NAME: &'static str = "SendHeadersReader";
13702 fn to_entity(&self) -> Self::Entity {
13703 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
13704 }
13705 fn new_unchecked(slice: &'r [u8]) -> Self {
13706 SendHeadersReader(slice)
13707 }
13708 fn as_slice(&self) -> &'r [u8] {
13709 self.0
13710 }
13711 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
13712 use molecule::verification_error as ve;
13713 let slice_len = slice.len();
13714 if slice_len < molecule::NUMBER_SIZE {
13715 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
13716 }
13717 let total_size = molecule::unpack_number(slice) as usize;
13718 if slice_len != total_size {
13719 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
13720 }
13721 if slice_len < molecule::NUMBER_SIZE * 2 {
13722 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
13723 }
13724 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
13725 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
13726 return ve!(Self, OffsetsNotMatch);
13727 }
13728 if slice_len < offset_first {
13729 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
13730 }
13731 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
13732 if field_count < Self::FIELD_COUNT {
13733 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13734 } else if !compatible && field_count > Self::FIELD_COUNT {
13735 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13736 };
13737 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
13738 .chunks_exact(molecule::NUMBER_SIZE)
13739 .map(|x| molecule::unpack_number(x) as usize)
13740 .collect();
13741 offsets.push(total_size);
13742 if offsets.windows(2).any(|i| i[0] > i[1]) {
13743 return ve!(Self, OffsetsNotMatch);
13744 }
13745 HeaderVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
13746 Ok(())
13747 }
13748}
13749#[derive(Clone, Debug, Default)]
13750pub struct SendHeadersBuilder {
13751 pub(crate) headers: HeaderVec,
13752}
13753impl SendHeadersBuilder {
13754 pub const FIELD_COUNT: usize = 1;
13755 pub fn headers<T>(mut self, v: T) -> Self
13756 where
13757 T: ::core::convert::Into<HeaderVec>,
13758 {
13759 self.headers = v.into();
13760 self
13761 }
13762}
13763impl molecule::prelude::Builder for SendHeadersBuilder {
13764 type Entity = SendHeaders;
13765 const NAME: &'static str = "SendHeadersBuilder";
13766 fn expected_length(&self) -> usize {
13767 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.headers.as_slice().len()
13768 }
13769 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
13770 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
13771 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
13772 offsets.push(total_size);
13773 total_size += self.headers.as_slice().len();
13774 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
13775 for offset in offsets.into_iter() {
13776 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
13777 }
13778 writer.write_all(self.headers.as_slice())?;
13779 Ok(())
13780 }
13781 fn build(&self) -> Self::Entity {
13782 let mut inner = Vec::with_capacity(self.expected_length());
13783 self.write(&mut inner)
13784 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
13785 SendHeaders::new_unchecked(inner.into())
13786 }
13787}
13788#[derive(Clone)]
13789pub struct SendBlock(molecule::bytes::Bytes);
13790impl ::core::fmt::LowerHex for SendBlock {
13791 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13792 use molecule::hex_string;
13793 if f.alternate() {
13794 write!(f, "0x")?;
13795 }
13796 write!(f, "{}", hex_string(self.as_slice()))
13797 }
13798}
13799impl ::core::fmt::Debug for SendBlock {
13800 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13801 write!(f, "{}({:#x})", Self::NAME, self)
13802 }
13803}
13804impl ::core::fmt::Display for SendBlock {
13805 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13806 write!(f, "{} {{ ", Self::NAME)?;
13807 write!(f, "{}: {}", "block", self.block())?;
13808 let extra_count = self.count_extra_fields();
13809 if extra_count != 0 {
13810 write!(f, ", .. ({} fields)", extra_count)?;
13811 }
13812 write!(f, " }}")
13813 }
13814}
13815impl ::core::default::Default for SendBlock {
13816 fn default() -> Self {
13817 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
13818 SendBlock::new_unchecked(v)
13819 }
13820}
13821impl SendBlock {
13822 const DEFAULT_VALUE: [u8; 248] = [
13823 248, 0, 0, 0, 8, 0, 0, 0, 240, 0, 0, 0, 20, 0, 0, 0, 228, 0, 0, 0, 232, 0, 0, 0, 236, 0, 0,
13824 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13825 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13826 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13827 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13828 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13829 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13830 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
13831 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
13832 ];
13833 pub const FIELD_COUNT: usize = 1;
13834 pub fn total_size(&self) -> usize {
13835 molecule::unpack_number(self.as_slice()) as usize
13836 }
13837 pub fn field_count(&self) -> usize {
13838 if self.total_size() == molecule::NUMBER_SIZE {
13839 0
13840 } else {
13841 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13842 }
13843 }
13844 pub fn count_extra_fields(&self) -> usize {
13845 self.field_count() - Self::FIELD_COUNT
13846 }
13847 pub fn has_extra_fields(&self) -> bool {
13848 Self::FIELD_COUNT != self.field_count()
13849 }
13850 pub fn block(&self) -> Block {
13851 let slice = self.as_slice();
13852 let start = molecule::unpack_number(&slice[4..]) as usize;
13853 if self.has_extra_fields() {
13854 let end = molecule::unpack_number(&slice[8..]) as usize;
13855 Block::new_unchecked(self.0.slice(start..end))
13856 } else {
13857 Block::new_unchecked(self.0.slice(start..))
13858 }
13859 }
13860 pub fn as_reader<'r>(&'r self) -> SendBlockReader<'r> {
13861 SendBlockReader::new_unchecked(self.as_slice())
13862 }
13863}
13864impl molecule::prelude::Entity for SendBlock {
13865 type Builder = SendBlockBuilder;
13866 const NAME: &'static str = "SendBlock";
13867 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
13868 SendBlock(data)
13869 }
13870 fn as_bytes(&self) -> molecule::bytes::Bytes {
13871 self.0.clone()
13872 }
13873 fn as_slice(&self) -> &[u8] {
13874 &self.0[..]
13875 }
13876 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13877 SendBlockReader::from_slice(slice).map(|reader| reader.to_entity())
13878 }
13879 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13880 SendBlockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
13881 }
13882 fn new_builder() -> Self::Builder {
13883 ::core::default::Default::default()
13884 }
13885 fn as_builder(self) -> Self::Builder {
13886 Self::new_builder().block(self.block())
13887 }
13888}
13889#[derive(Clone, Copy)]
13890pub struct SendBlockReader<'r>(&'r [u8]);
13891impl<'r> ::core::fmt::LowerHex for SendBlockReader<'r> {
13892 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13893 use molecule::hex_string;
13894 if f.alternate() {
13895 write!(f, "0x")?;
13896 }
13897 write!(f, "{}", hex_string(self.as_slice()))
13898 }
13899}
13900impl<'r> ::core::fmt::Debug for SendBlockReader<'r> {
13901 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13902 write!(f, "{}({:#x})", Self::NAME, self)
13903 }
13904}
13905impl<'r> ::core::fmt::Display for SendBlockReader<'r> {
13906 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13907 write!(f, "{} {{ ", Self::NAME)?;
13908 write!(f, "{}: {}", "block", self.block())?;
13909 let extra_count = self.count_extra_fields();
13910 if extra_count != 0 {
13911 write!(f, ", .. ({} fields)", extra_count)?;
13912 }
13913 write!(f, " }}")
13914 }
13915}
13916impl<'r> SendBlockReader<'r> {
13917 pub const FIELD_COUNT: usize = 1;
13918 pub fn total_size(&self) -> usize {
13919 molecule::unpack_number(self.as_slice()) as usize
13920 }
13921 pub fn field_count(&self) -> usize {
13922 if self.total_size() == molecule::NUMBER_SIZE {
13923 0
13924 } else {
13925 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13926 }
13927 }
13928 pub fn count_extra_fields(&self) -> usize {
13929 self.field_count() - Self::FIELD_COUNT
13930 }
13931 pub fn has_extra_fields(&self) -> bool {
13932 Self::FIELD_COUNT != self.field_count()
13933 }
13934 pub fn block(&self) -> BlockReader<'r> {
13935 let slice = self.as_slice();
13936 let start = molecule::unpack_number(&slice[4..]) as usize;
13937 if self.has_extra_fields() {
13938 let end = molecule::unpack_number(&slice[8..]) as usize;
13939 BlockReader::new_unchecked(&self.as_slice()[start..end])
13940 } else {
13941 BlockReader::new_unchecked(&self.as_slice()[start..])
13942 }
13943 }
13944}
13945impl<'r> molecule::prelude::Reader<'r> for SendBlockReader<'r> {
13946 type Entity = SendBlock;
13947 const NAME: &'static str = "SendBlockReader";
13948 fn to_entity(&self) -> Self::Entity {
13949 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
13950 }
13951 fn new_unchecked(slice: &'r [u8]) -> Self {
13952 SendBlockReader(slice)
13953 }
13954 fn as_slice(&self) -> &'r [u8] {
13955 self.0
13956 }
13957 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
13958 use molecule::verification_error as ve;
13959 let slice_len = slice.len();
13960 if slice_len < molecule::NUMBER_SIZE {
13961 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
13962 }
13963 let total_size = molecule::unpack_number(slice) as usize;
13964 if slice_len != total_size {
13965 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
13966 }
13967 if slice_len < molecule::NUMBER_SIZE * 2 {
13968 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
13969 }
13970 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
13971 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
13972 return ve!(Self, OffsetsNotMatch);
13973 }
13974 if slice_len < offset_first {
13975 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
13976 }
13977 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
13978 if field_count < Self::FIELD_COUNT {
13979 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13980 } else if !compatible && field_count > Self::FIELD_COUNT {
13981 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13982 };
13983 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
13984 .chunks_exact(molecule::NUMBER_SIZE)
13985 .map(|x| molecule::unpack_number(x) as usize)
13986 .collect();
13987 offsets.push(total_size);
13988 if offsets.windows(2).any(|i| i[0] > i[1]) {
13989 return ve!(Self, OffsetsNotMatch);
13990 }
13991 BlockReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
13992 Ok(())
13993 }
13994}
13995#[derive(Clone, Debug, Default)]
13996pub struct SendBlockBuilder {
13997 pub(crate) block: Block,
13998}
13999impl SendBlockBuilder {
14000 pub const FIELD_COUNT: usize = 1;
14001 pub fn block<T>(mut self, v: T) -> Self
14002 where
14003 T: ::core::convert::Into<Block>,
14004 {
14005 self.block = v.into();
14006 self
14007 }
14008}
14009impl molecule::prelude::Builder for SendBlockBuilder {
14010 type Entity = SendBlock;
14011 const NAME: &'static str = "SendBlockBuilder";
14012 fn expected_length(&self) -> usize {
14013 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.block.as_slice().len()
14014 }
14015 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
14016 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
14017 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
14018 offsets.push(total_size);
14019 total_size += self.block.as_slice().len();
14020 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
14021 for offset in offsets.into_iter() {
14022 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
14023 }
14024 writer.write_all(self.block.as_slice())?;
14025 Ok(())
14026 }
14027 fn build(&self) -> Self::Entity {
14028 let mut inner = Vec::with_capacity(self.expected_length());
14029 self.write(&mut inner)
14030 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
14031 SendBlock::new_unchecked(inner.into())
14032 }
14033}
14034#[derive(Clone)]
14035pub struct FilteredBlock(molecule::bytes::Bytes);
14036impl ::core::fmt::LowerHex for FilteredBlock {
14037 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14038 use molecule::hex_string;
14039 if f.alternate() {
14040 write!(f, "0x")?;
14041 }
14042 write!(f, "{}", hex_string(self.as_slice()))
14043 }
14044}
14045impl ::core::fmt::Debug for FilteredBlock {
14046 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14047 write!(f, "{}({:#x})", Self::NAME, self)
14048 }
14049}
14050impl ::core::fmt::Display for FilteredBlock {
14051 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14052 write!(f, "{} {{ ", Self::NAME)?;
14053 write!(f, "{}: {}", "header", self.header())?;
14054 write!(f, ", {}: {}", "witnesses_root", self.witnesses_root())?;
14055 write!(f, ", {}: {}", "transactions", self.transactions())?;
14056 write!(f, ", {}: {}", "proof", self.proof())?;
14057 let extra_count = self.count_extra_fields();
14058 if extra_count != 0 {
14059 write!(f, ", .. ({} fields)", extra_count)?;
14060 }
14061 write!(f, " }}")
14062 }
14063}
14064impl ::core::default::Default for FilteredBlock {
14065 fn default() -> Self {
14066 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
14067 FilteredBlock::new_unchecked(v)
14068 }
14069}
14070impl FilteredBlock {
14071 const DEFAULT_VALUE: [u8; 284] = [
14072 28, 1, 0, 0, 20, 0, 0, 0, 228, 0, 0, 0, 4, 1, 0, 0, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14073 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14074 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14075 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14076 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14077 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14078 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14079 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14080 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 20, 0, 0, 0, 12,
14081 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14082 ];
14083 pub const FIELD_COUNT: usize = 4;
14084 pub fn total_size(&self) -> usize {
14085 molecule::unpack_number(self.as_slice()) as usize
14086 }
14087 pub fn field_count(&self) -> usize {
14088 if self.total_size() == molecule::NUMBER_SIZE {
14089 0
14090 } else {
14091 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
14092 }
14093 }
14094 pub fn count_extra_fields(&self) -> usize {
14095 self.field_count() - Self::FIELD_COUNT
14096 }
14097 pub fn has_extra_fields(&self) -> bool {
14098 Self::FIELD_COUNT != self.field_count()
14099 }
14100 pub fn header(&self) -> Header {
14101 let slice = self.as_slice();
14102 let start = molecule::unpack_number(&slice[4..]) as usize;
14103 let end = molecule::unpack_number(&slice[8..]) as usize;
14104 Header::new_unchecked(self.0.slice(start..end))
14105 }
14106 pub fn witnesses_root(&self) -> Byte32 {
14107 let slice = self.as_slice();
14108 let start = molecule::unpack_number(&slice[8..]) as usize;
14109 let end = molecule::unpack_number(&slice[12..]) as usize;
14110 Byte32::new_unchecked(self.0.slice(start..end))
14111 }
14112 pub fn transactions(&self) -> TransactionVec {
14113 let slice = self.as_slice();
14114 let start = molecule::unpack_number(&slice[12..]) as usize;
14115 let end = molecule::unpack_number(&slice[16..]) as usize;
14116 TransactionVec::new_unchecked(self.0.slice(start..end))
14117 }
14118 pub fn proof(&self) -> MerkleProof {
14119 let slice = self.as_slice();
14120 let start = molecule::unpack_number(&slice[16..]) as usize;
14121 if self.has_extra_fields() {
14122 let end = molecule::unpack_number(&slice[20..]) as usize;
14123 MerkleProof::new_unchecked(self.0.slice(start..end))
14124 } else {
14125 MerkleProof::new_unchecked(self.0.slice(start..))
14126 }
14127 }
14128 pub fn as_reader<'r>(&'r self) -> FilteredBlockReader<'r> {
14129 FilteredBlockReader::new_unchecked(self.as_slice())
14130 }
14131}
14132impl molecule::prelude::Entity for FilteredBlock {
14133 type Builder = FilteredBlockBuilder;
14134 const NAME: &'static str = "FilteredBlock";
14135 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
14136 FilteredBlock(data)
14137 }
14138 fn as_bytes(&self) -> molecule::bytes::Bytes {
14139 self.0.clone()
14140 }
14141 fn as_slice(&self) -> &[u8] {
14142 &self.0[..]
14143 }
14144 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14145 FilteredBlockReader::from_slice(slice).map(|reader| reader.to_entity())
14146 }
14147 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14148 FilteredBlockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
14149 }
14150 fn new_builder() -> Self::Builder {
14151 ::core::default::Default::default()
14152 }
14153 fn as_builder(self) -> Self::Builder {
14154 Self::new_builder()
14155 .header(self.header())
14156 .witnesses_root(self.witnesses_root())
14157 .transactions(self.transactions())
14158 .proof(self.proof())
14159 }
14160}
14161#[derive(Clone, Copy)]
14162pub struct FilteredBlockReader<'r>(&'r [u8]);
14163impl<'r> ::core::fmt::LowerHex for FilteredBlockReader<'r> {
14164 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14165 use molecule::hex_string;
14166 if f.alternate() {
14167 write!(f, "0x")?;
14168 }
14169 write!(f, "{}", hex_string(self.as_slice()))
14170 }
14171}
14172impl<'r> ::core::fmt::Debug for FilteredBlockReader<'r> {
14173 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14174 write!(f, "{}({:#x})", Self::NAME, self)
14175 }
14176}
14177impl<'r> ::core::fmt::Display for FilteredBlockReader<'r> {
14178 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14179 write!(f, "{} {{ ", Self::NAME)?;
14180 write!(f, "{}: {}", "header", self.header())?;
14181 write!(f, ", {}: {}", "witnesses_root", self.witnesses_root())?;
14182 write!(f, ", {}: {}", "transactions", self.transactions())?;
14183 write!(f, ", {}: {}", "proof", self.proof())?;
14184 let extra_count = self.count_extra_fields();
14185 if extra_count != 0 {
14186 write!(f, ", .. ({} fields)", extra_count)?;
14187 }
14188 write!(f, " }}")
14189 }
14190}
14191impl<'r> FilteredBlockReader<'r> {
14192 pub const FIELD_COUNT: usize = 4;
14193 pub fn total_size(&self) -> usize {
14194 molecule::unpack_number(self.as_slice()) as usize
14195 }
14196 pub fn field_count(&self) -> usize {
14197 if self.total_size() == molecule::NUMBER_SIZE {
14198 0
14199 } else {
14200 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
14201 }
14202 }
14203 pub fn count_extra_fields(&self) -> usize {
14204 self.field_count() - Self::FIELD_COUNT
14205 }
14206 pub fn has_extra_fields(&self) -> bool {
14207 Self::FIELD_COUNT != self.field_count()
14208 }
14209 pub fn header(&self) -> HeaderReader<'r> {
14210 let slice = self.as_slice();
14211 let start = molecule::unpack_number(&slice[4..]) as usize;
14212 let end = molecule::unpack_number(&slice[8..]) as usize;
14213 HeaderReader::new_unchecked(&self.as_slice()[start..end])
14214 }
14215 pub fn witnesses_root(&self) -> Byte32Reader<'r> {
14216 let slice = self.as_slice();
14217 let start = molecule::unpack_number(&slice[8..]) as usize;
14218 let end = molecule::unpack_number(&slice[12..]) as usize;
14219 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
14220 }
14221 pub fn transactions(&self) -> TransactionVecReader<'r> {
14222 let slice = self.as_slice();
14223 let start = molecule::unpack_number(&slice[12..]) as usize;
14224 let end = molecule::unpack_number(&slice[16..]) as usize;
14225 TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
14226 }
14227 pub fn proof(&self) -> MerkleProofReader<'r> {
14228 let slice = self.as_slice();
14229 let start = molecule::unpack_number(&slice[16..]) as usize;
14230 if self.has_extra_fields() {
14231 let end = molecule::unpack_number(&slice[20..]) as usize;
14232 MerkleProofReader::new_unchecked(&self.as_slice()[start..end])
14233 } else {
14234 MerkleProofReader::new_unchecked(&self.as_slice()[start..])
14235 }
14236 }
14237}
14238impl<'r> molecule::prelude::Reader<'r> for FilteredBlockReader<'r> {
14239 type Entity = FilteredBlock;
14240 const NAME: &'static str = "FilteredBlockReader";
14241 fn to_entity(&self) -> Self::Entity {
14242 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
14243 }
14244 fn new_unchecked(slice: &'r [u8]) -> Self {
14245 FilteredBlockReader(slice)
14246 }
14247 fn as_slice(&self) -> &'r [u8] {
14248 self.0
14249 }
14250 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
14251 use molecule::verification_error as ve;
14252 let slice_len = slice.len();
14253 if slice_len < molecule::NUMBER_SIZE {
14254 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
14255 }
14256 let total_size = molecule::unpack_number(slice) as usize;
14257 if slice_len != total_size {
14258 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
14259 }
14260 if slice_len < molecule::NUMBER_SIZE * 2 {
14261 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
14262 }
14263 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
14264 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
14265 return ve!(Self, OffsetsNotMatch);
14266 }
14267 if slice_len < offset_first {
14268 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
14269 }
14270 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
14271 if field_count < Self::FIELD_COUNT {
14272 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
14273 } else if !compatible && field_count > Self::FIELD_COUNT {
14274 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
14275 };
14276 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
14277 .chunks_exact(molecule::NUMBER_SIZE)
14278 .map(|x| molecule::unpack_number(x) as usize)
14279 .collect();
14280 offsets.push(total_size);
14281 if offsets.windows(2).any(|i| i[0] > i[1]) {
14282 return ve!(Self, OffsetsNotMatch);
14283 }
14284 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
14285 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
14286 TransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
14287 MerkleProofReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
14288 Ok(())
14289 }
14290}
14291#[derive(Clone, Debug, Default)]
14292pub struct FilteredBlockBuilder {
14293 pub(crate) header: Header,
14294 pub(crate) witnesses_root: Byte32,
14295 pub(crate) transactions: TransactionVec,
14296 pub(crate) proof: MerkleProof,
14297}
14298impl FilteredBlockBuilder {
14299 pub const FIELD_COUNT: usize = 4;
14300 pub fn header<T>(mut self, v: T) -> Self
14301 where
14302 T: ::core::convert::Into<Header>,
14303 {
14304 self.header = v.into();
14305 self
14306 }
14307 pub fn witnesses_root<T>(mut self, v: T) -> Self
14308 where
14309 T: ::core::convert::Into<Byte32>,
14310 {
14311 self.witnesses_root = v.into();
14312 self
14313 }
14314 pub fn transactions<T>(mut self, v: T) -> Self
14315 where
14316 T: ::core::convert::Into<TransactionVec>,
14317 {
14318 self.transactions = v.into();
14319 self
14320 }
14321 pub fn proof<T>(mut self, v: T) -> Self
14322 where
14323 T: ::core::convert::Into<MerkleProof>,
14324 {
14325 self.proof = v.into();
14326 self
14327 }
14328}
14329impl molecule::prelude::Builder for FilteredBlockBuilder {
14330 type Entity = FilteredBlock;
14331 const NAME: &'static str = "FilteredBlockBuilder";
14332 fn expected_length(&self) -> usize {
14333 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
14334 + self.header.as_slice().len()
14335 + self.witnesses_root.as_slice().len()
14336 + self.transactions.as_slice().len()
14337 + self.proof.as_slice().len()
14338 }
14339 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
14340 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
14341 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
14342 offsets.push(total_size);
14343 total_size += self.header.as_slice().len();
14344 offsets.push(total_size);
14345 total_size += self.witnesses_root.as_slice().len();
14346 offsets.push(total_size);
14347 total_size += self.transactions.as_slice().len();
14348 offsets.push(total_size);
14349 total_size += self.proof.as_slice().len();
14350 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
14351 for offset in offsets.into_iter() {
14352 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
14353 }
14354 writer.write_all(self.header.as_slice())?;
14355 writer.write_all(self.witnesses_root.as_slice())?;
14356 writer.write_all(self.transactions.as_slice())?;
14357 writer.write_all(self.proof.as_slice())?;
14358 Ok(())
14359 }
14360 fn build(&self) -> Self::Entity {
14361 let mut inner = Vec::with_capacity(self.expected_length());
14362 self.write(&mut inner)
14363 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
14364 FilteredBlock::new_unchecked(inner.into())
14365 }
14366}
14367#[derive(Clone)]
14368pub struct MerkleProof(molecule::bytes::Bytes);
14369impl ::core::fmt::LowerHex for MerkleProof {
14370 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14371 use molecule::hex_string;
14372 if f.alternate() {
14373 write!(f, "0x")?;
14374 }
14375 write!(f, "{}", hex_string(self.as_slice()))
14376 }
14377}
14378impl ::core::fmt::Debug for MerkleProof {
14379 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14380 write!(f, "{}({:#x})", Self::NAME, self)
14381 }
14382}
14383impl ::core::fmt::Display for MerkleProof {
14384 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14385 write!(f, "{} {{ ", Self::NAME)?;
14386 write!(f, "{}: {}", "indices", self.indices())?;
14387 write!(f, ", {}: {}", "lemmas", self.lemmas())?;
14388 let extra_count = self.count_extra_fields();
14389 if extra_count != 0 {
14390 write!(f, ", .. ({} fields)", extra_count)?;
14391 }
14392 write!(f, " }}")
14393 }
14394}
14395impl ::core::default::Default for MerkleProof {
14396 fn default() -> Self {
14397 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
14398 MerkleProof::new_unchecked(v)
14399 }
14400}
14401impl MerkleProof {
14402 const DEFAULT_VALUE: [u8; 20] = [
14403 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14404 ];
14405 pub const FIELD_COUNT: usize = 2;
14406 pub fn total_size(&self) -> usize {
14407 molecule::unpack_number(self.as_slice()) as usize
14408 }
14409 pub fn field_count(&self) -> usize {
14410 if self.total_size() == molecule::NUMBER_SIZE {
14411 0
14412 } else {
14413 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
14414 }
14415 }
14416 pub fn count_extra_fields(&self) -> usize {
14417 self.field_count() - Self::FIELD_COUNT
14418 }
14419 pub fn has_extra_fields(&self) -> bool {
14420 Self::FIELD_COUNT != self.field_count()
14421 }
14422 pub fn indices(&self) -> Uint32Vec {
14423 let slice = self.as_slice();
14424 let start = molecule::unpack_number(&slice[4..]) as usize;
14425 let end = molecule::unpack_number(&slice[8..]) as usize;
14426 Uint32Vec::new_unchecked(self.0.slice(start..end))
14427 }
14428 pub fn lemmas(&self) -> Byte32Vec {
14429 let slice = self.as_slice();
14430 let start = molecule::unpack_number(&slice[8..]) as usize;
14431 if self.has_extra_fields() {
14432 let end = molecule::unpack_number(&slice[12..]) as usize;
14433 Byte32Vec::new_unchecked(self.0.slice(start..end))
14434 } else {
14435 Byte32Vec::new_unchecked(self.0.slice(start..))
14436 }
14437 }
14438 pub fn as_reader<'r>(&'r self) -> MerkleProofReader<'r> {
14439 MerkleProofReader::new_unchecked(self.as_slice())
14440 }
14441}
14442impl molecule::prelude::Entity for MerkleProof {
14443 type Builder = MerkleProofBuilder;
14444 const NAME: &'static str = "MerkleProof";
14445 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
14446 MerkleProof(data)
14447 }
14448 fn as_bytes(&self) -> molecule::bytes::Bytes {
14449 self.0.clone()
14450 }
14451 fn as_slice(&self) -> &[u8] {
14452 &self.0[..]
14453 }
14454 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14455 MerkleProofReader::from_slice(slice).map(|reader| reader.to_entity())
14456 }
14457 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14458 MerkleProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
14459 }
14460 fn new_builder() -> Self::Builder {
14461 ::core::default::Default::default()
14462 }
14463 fn as_builder(self) -> Self::Builder {
14464 Self::new_builder()
14465 .indices(self.indices())
14466 .lemmas(self.lemmas())
14467 }
14468}
14469#[derive(Clone, Copy)]
14470pub struct MerkleProofReader<'r>(&'r [u8]);
14471impl<'r> ::core::fmt::LowerHex for MerkleProofReader<'r> {
14472 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14473 use molecule::hex_string;
14474 if f.alternate() {
14475 write!(f, "0x")?;
14476 }
14477 write!(f, "{}", hex_string(self.as_slice()))
14478 }
14479}
14480impl<'r> ::core::fmt::Debug for MerkleProofReader<'r> {
14481 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14482 write!(f, "{}({:#x})", Self::NAME, self)
14483 }
14484}
14485impl<'r> ::core::fmt::Display for MerkleProofReader<'r> {
14486 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14487 write!(f, "{} {{ ", Self::NAME)?;
14488 write!(f, "{}: {}", "indices", self.indices())?;
14489 write!(f, ", {}: {}", "lemmas", self.lemmas())?;
14490 let extra_count = self.count_extra_fields();
14491 if extra_count != 0 {
14492 write!(f, ", .. ({} fields)", extra_count)?;
14493 }
14494 write!(f, " }}")
14495 }
14496}
14497impl<'r> MerkleProofReader<'r> {
14498 pub const FIELD_COUNT: usize = 2;
14499 pub fn total_size(&self) -> usize {
14500 molecule::unpack_number(self.as_slice()) as usize
14501 }
14502 pub fn field_count(&self) -> usize {
14503 if self.total_size() == molecule::NUMBER_SIZE {
14504 0
14505 } else {
14506 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
14507 }
14508 }
14509 pub fn count_extra_fields(&self) -> usize {
14510 self.field_count() - Self::FIELD_COUNT
14511 }
14512 pub fn has_extra_fields(&self) -> bool {
14513 Self::FIELD_COUNT != self.field_count()
14514 }
14515 pub fn indices(&self) -> Uint32VecReader<'r> {
14516 let slice = self.as_slice();
14517 let start = molecule::unpack_number(&slice[4..]) as usize;
14518 let end = molecule::unpack_number(&slice[8..]) as usize;
14519 Uint32VecReader::new_unchecked(&self.as_slice()[start..end])
14520 }
14521 pub fn lemmas(&self) -> Byte32VecReader<'r> {
14522 let slice = self.as_slice();
14523 let start = molecule::unpack_number(&slice[8..]) as usize;
14524 if self.has_extra_fields() {
14525 let end = molecule::unpack_number(&slice[12..]) as usize;
14526 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
14527 } else {
14528 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
14529 }
14530 }
14531}
14532impl<'r> molecule::prelude::Reader<'r> for MerkleProofReader<'r> {
14533 type Entity = MerkleProof;
14534 const NAME: &'static str = "MerkleProofReader";
14535 fn to_entity(&self) -> Self::Entity {
14536 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
14537 }
14538 fn new_unchecked(slice: &'r [u8]) -> Self {
14539 MerkleProofReader(slice)
14540 }
14541 fn as_slice(&self) -> &'r [u8] {
14542 self.0
14543 }
14544 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
14545 use molecule::verification_error as ve;
14546 let slice_len = slice.len();
14547 if slice_len < molecule::NUMBER_SIZE {
14548 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
14549 }
14550 let total_size = molecule::unpack_number(slice) as usize;
14551 if slice_len != total_size {
14552 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
14553 }
14554 if slice_len < molecule::NUMBER_SIZE * 2 {
14555 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
14556 }
14557 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
14558 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
14559 return ve!(Self, OffsetsNotMatch);
14560 }
14561 if slice_len < offset_first {
14562 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
14563 }
14564 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
14565 if field_count < Self::FIELD_COUNT {
14566 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
14567 } else if !compatible && field_count > Self::FIELD_COUNT {
14568 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
14569 };
14570 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
14571 .chunks_exact(molecule::NUMBER_SIZE)
14572 .map(|x| molecule::unpack_number(x) as usize)
14573 .collect();
14574 offsets.push(total_size);
14575 if offsets.windows(2).any(|i| i[0] > i[1]) {
14576 return ve!(Self, OffsetsNotMatch);
14577 }
14578 Uint32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
14579 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
14580 Ok(())
14581 }
14582}
14583#[derive(Clone, Debug, Default)]
14584pub struct MerkleProofBuilder {
14585 pub(crate) indices: Uint32Vec,
14586 pub(crate) lemmas: Byte32Vec,
14587}
14588impl MerkleProofBuilder {
14589 pub const FIELD_COUNT: usize = 2;
14590 pub fn indices<T>(mut self, v: T) -> Self
14591 where
14592 T: ::core::convert::Into<Uint32Vec>,
14593 {
14594 self.indices = v.into();
14595 self
14596 }
14597 pub fn lemmas<T>(mut self, v: T) -> Self
14598 where
14599 T: ::core::convert::Into<Byte32Vec>,
14600 {
14601 self.lemmas = v.into();
14602 self
14603 }
14604}
14605impl molecule::prelude::Builder for MerkleProofBuilder {
14606 type Entity = MerkleProof;
14607 const NAME: &'static str = "MerkleProofBuilder";
14608 fn expected_length(&self) -> usize {
14609 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
14610 + self.indices.as_slice().len()
14611 + self.lemmas.as_slice().len()
14612 }
14613 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
14614 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
14615 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
14616 offsets.push(total_size);
14617 total_size += self.indices.as_slice().len();
14618 offsets.push(total_size);
14619 total_size += self.lemmas.as_slice().len();
14620 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
14621 for offset in offsets.into_iter() {
14622 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
14623 }
14624 writer.write_all(self.indices.as_slice())?;
14625 writer.write_all(self.lemmas.as_slice())?;
14626 Ok(())
14627 }
14628 fn build(&self) -> Self::Entity {
14629 let mut inner = Vec::with_capacity(self.expected_length());
14630 self.write(&mut inner)
14631 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
14632 MerkleProof::new_unchecked(inner.into())
14633 }
14634}
14635#[derive(Clone)]
14636pub struct InIBD(molecule::bytes::Bytes);
14637impl ::core::fmt::LowerHex for InIBD {
14638 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14639 use molecule::hex_string;
14640 if f.alternate() {
14641 write!(f, "0x")?;
14642 }
14643 write!(f, "{}", hex_string(self.as_slice()))
14644 }
14645}
14646impl ::core::fmt::Debug for InIBD {
14647 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14648 write!(f, "{}({:#x})", Self::NAME, self)
14649 }
14650}
14651impl ::core::fmt::Display for InIBD {
14652 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14653 write!(f, "{} {{ ", Self::NAME)?;
14654 let extra_count = self.count_extra_fields();
14655 if extra_count != 0 {
14656 write!(f, ".. ({} fields)", extra_count)?;
14657 }
14658 write!(f, " }}")
14659 }
14660}
14661impl ::core::default::Default for InIBD {
14662 fn default() -> Self {
14663 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
14664 InIBD::new_unchecked(v)
14665 }
14666}
14667impl InIBD {
14668 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
14669 pub const FIELD_COUNT: usize = 0;
14670 pub fn total_size(&self) -> usize {
14671 molecule::unpack_number(self.as_slice()) as usize
14672 }
14673 pub fn field_count(&self) -> usize {
14674 if self.total_size() == molecule::NUMBER_SIZE {
14675 0
14676 } else {
14677 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
14678 }
14679 }
14680 pub fn count_extra_fields(&self) -> usize {
14681 self.field_count() - Self::FIELD_COUNT
14682 }
14683 pub fn has_extra_fields(&self) -> bool {
14684 Self::FIELD_COUNT != self.field_count()
14685 }
14686 pub fn as_reader<'r>(&'r self) -> InIBDReader<'r> {
14687 InIBDReader::new_unchecked(self.as_slice())
14688 }
14689}
14690impl molecule::prelude::Entity for InIBD {
14691 type Builder = InIBDBuilder;
14692 const NAME: &'static str = "InIBD";
14693 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
14694 InIBD(data)
14695 }
14696 fn as_bytes(&self) -> molecule::bytes::Bytes {
14697 self.0.clone()
14698 }
14699 fn as_slice(&self) -> &[u8] {
14700 &self.0[..]
14701 }
14702 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14703 InIBDReader::from_slice(slice).map(|reader| reader.to_entity())
14704 }
14705 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14706 InIBDReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
14707 }
14708 fn new_builder() -> Self::Builder {
14709 ::core::default::Default::default()
14710 }
14711 fn as_builder(self) -> Self::Builder {
14712 Self::new_builder()
14713 }
14714}
14715#[derive(Clone, Copy)]
14716pub struct InIBDReader<'r>(&'r [u8]);
14717impl<'r> ::core::fmt::LowerHex for InIBDReader<'r> {
14718 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14719 use molecule::hex_string;
14720 if f.alternate() {
14721 write!(f, "0x")?;
14722 }
14723 write!(f, "{}", hex_string(self.as_slice()))
14724 }
14725}
14726impl<'r> ::core::fmt::Debug for InIBDReader<'r> {
14727 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14728 write!(f, "{}({:#x})", Self::NAME, self)
14729 }
14730}
14731impl<'r> ::core::fmt::Display for InIBDReader<'r> {
14732 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14733 write!(f, "{} {{ ", Self::NAME)?;
14734 let extra_count = self.count_extra_fields();
14735 if extra_count != 0 {
14736 write!(f, ".. ({} fields)", extra_count)?;
14737 }
14738 write!(f, " }}")
14739 }
14740}
14741impl<'r> InIBDReader<'r> {
14742 pub const FIELD_COUNT: usize = 0;
14743 pub fn total_size(&self) -> usize {
14744 molecule::unpack_number(self.as_slice()) as usize
14745 }
14746 pub fn field_count(&self) -> usize {
14747 if self.total_size() == molecule::NUMBER_SIZE {
14748 0
14749 } else {
14750 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
14751 }
14752 }
14753 pub fn count_extra_fields(&self) -> usize {
14754 self.field_count() - Self::FIELD_COUNT
14755 }
14756 pub fn has_extra_fields(&self) -> bool {
14757 Self::FIELD_COUNT != self.field_count()
14758 }
14759}
14760impl<'r> molecule::prelude::Reader<'r> for InIBDReader<'r> {
14761 type Entity = InIBD;
14762 const NAME: &'static str = "InIBDReader";
14763 fn to_entity(&self) -> Self::Entity {
14764 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
14765 }
14766 fn new_unchecked(slice: &'r [u8]) -> Self {
14767 InIBDReader(slice)
14768 }
14769 fn as_slice(&self) -> &'r [u8] {
14770 self.0
14771 }
14772 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
14773 use molecule::verification_error as ve;
14774 let slice_len = slice.len();
14775 if slice_len < molecule::NUMBER_SIZE {
14776 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
14777 }
14778 let total_size = molecule::unpack_number(slice) as usize;
14779 if slice_len != total_size {
14780 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
14781 }
14782 if slice_len > molecule::NUMBER_SIZE && !compatible {
14783 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, !0);
14784 }
14785 Ok(())
14786 }
14787}
14788#[derive(Clone, Debug, Default)]
14789pub struct InIBDBuilder {}
14790impl InIBDBuilder {
14791 pub const FIELD_COUNT: usize = 0;
14792}
14793impl molecule::prelude::Builder for InIBDBuilder {
14794 type Entity = InIBD;
14795 const NAME: &'static str = "InIBDBuilder";
14796 fn expected_length(&self) -> usize {
14797 molecule::NUMBER_SIZE
14798 }
14799 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
14800 writer.write_all(&molecule::pack_number(
14801 molecule::NUMBER_SIZE as molecule::Number,
14802 ))?;
14803 Ok(())
14804 }
14805 fn build(&self) -> Self::Entity {
14806 let mut inner = Vec::with_capacity(self.expected_length());
14807 self.write(&mut inner)
14808 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
14809 InIBD::new_unchecked(inner.into())
14810 }
14811}
14812#[derive(Clone)]
14813pub struct HeaderDigestVec(molecule::bytes::Bytes);
14814impl ::core::fmt::LowerHex for HeaderDigestVec {
14815 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14816 use molecule::hex_string;
14817 if f.alternate() {
14818 write!(f, "0x")?;
14819 }
14820 write!(f, "{}", hex_string(self.as_slice()))
14821 }
14822}
14823impl ::core::fmt::Debug for HeaderDigestVec {
14824 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14825 write!(f, "{}({:#x})", Self::NAME, self)
14826 }
14827}
14828impl ::core::fmt::Display for HeaderDigestVec {
14829 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14830 write!(f, "{} [", Self::NAME)?;
14831 for i in 0..self.len() {
14832 if i == 0 {
14833 write!(f, "{}", self.get_unchecked(i))?;
14834 } else {
14835 write!(f, ", {}", self.get_unchecked(i))?;
14836 }
14837 }
14838 write!(f, "]")
14839 }
14840}
14841impl ::core::default::Default for HeaderDigestVec {
14842 fn default() -> Self {
14843 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
14844 HeaderDigestVec::new_unchecked(v)
14845 }
14846}
14847impl HeaderDigestVec {
14848 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
14849 pub const ITEM_SIZE: usize = 120;
14850 pub fn total_size(&self) -> usize {
14851 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
14852 }
14853 pub fn item_count(&self) -> usize {
14854 molecule::unpack_number(self.as_slice()) as usize
14855 }
14856 pub fn len(&self) -> usize {
14857 self.item_count()
14858 }
14859 pub fn is_empty(&self) -> bool {
14860 self.len() == 0
14861 }
14862 pub fn get(&self, idx: usize) -> Option<HeaderDigest> {
14863 if idx >= self.len() {
14864 None
14865 } else {
14866 Some(self.get_unchecked(idx))
14867 }
14868 }
14869 pub fn get_unchecked(&self, idx: usize) -> HeaderDigest {
14870 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
14871 let end = start + Self::ITEM_SIZE;
14872 HeaderDigest::new_unchecked(self.0.slice(start..end))
14873 }
14874 pub fn as_reader<'r>(&'r self) -> HeaderDigestVecReader<'r> {
14875 HeaderDigestVecReader::new_unchecked(self.as_slice())
14876 }
14877}
14878impl molecule::prelude::Entity for HeaderDigestVec {
14879 type Builder = HeaderDigestVecBuilder;
14880 const NAME: &'static str = "HeaderDigestVec";
14881 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
14882 HeaderDigestVec(data)
14883 }
14884 fn as_bytes(&self) -> molecule::bytes::Bytes {
14885 self.0.clone()
14886 }
14887 fn as_slice(&self) -> &[u8] {
14888 &self.0[..]
14889 }
14890 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14891 HeaderDigestVecReader::from_slice(slice).map(|reader| reader.to_entity())
14892 }
14893 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14894 HeaderDigestVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
14895 }
14896 fn new_builder() -> Self::Builder {
14897 ::core::default::Default::default()
14898 }
14899 fn as_builder(self) -> Self::Builder {
14900 Self::new_builder().extend(self.into_iter())
14901 }
14902}
14903#[derive(Clone, Copy)]
14904pub struct HeaderDigestVecReader<'r>(&'r [u8]);
14905impl<'r> ::core::fmt::LowerHex for HeaderDigestVecReader<'r> {
14906 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14907 use molecule::hex_string;
14908 if f.alternate() {
14909 write!(f, "0x")?;
14910 }
14911 write!(f, "{}", hex_string(self.as_slice()))
14912 }
14913}
14914impl<'r> ::core::fmt::Debug for HeaderDigestVecReader<'r> {
14915 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14916 write!(f, "{}({:#x})", Self::NAME, self)
14917 }
14918}
14919impl<'r> ::core::fmt::Display for HeaderDigestVecReader<'r> {
14920 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14921 write!(f, "{} [", Self::NAME)?;
14922 for i in 0..self.len() {
14923 if i == 0 {
14924 write!(f, "{}", self.get_unchecked(i))?;
14925 } else {
14926 write!(f, ", {}", self.get_unchecked(i))?;
14927 }
14928 }
14929 write!(f, "]")
14930 }
14931}
14932impl<'r> HeaderDigestVecReader<'r> {
14933 pub const ITEM_SIZE: usize = 120;
14934 pub fn total_size(&self) -> usize {
14935 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
14936 }
14937 pub fn item_count(&self) -> usize {
14938 molecule::unpack_number(self.as_slice()) as usize
14939 }
14940 pub fn len(&self) -> usize {
14941 self.item_count()
14942 }
14943 pub fn is_empty(&self) -> bool {
14944 self.len() == 0
14945 }
14946 pub fn get(&self, idx: usize) -> Option<HeaderDigestReader<'r>> {
14947 if idx >= self.len() {
14948 None
14949 } else {
14950 Some(self.get_unchecked(idx))
14951 }
14952 }
14953 pub fn get_unchecked(&self, idx: usize) -> HeaderDigestReader<'r> {
14954 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
14955 let end = start + Self::ITEM_SIZE;
14956 HeaderDigestReader::new_unchecked(&self.as_slice()[start..end])
14957 }
14958}
14959impl<'r> molecule::prelude::Reader<'r> for HeaderDigestVecReader<'r> {
14960 type Entity = HeaderDigestVec;
14961 const NAME: &'static str = "HeaderDigestVecReader";
14962 fn to_entity(&self) -> Self::Entity {
14963 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
14964 }
14965 fn new_unchecked(slice: &'r [u8]) -> Self {
14966 HeaderDigestVecReader(slice)
14967 }
14968 fn as_slice(&self) -> &'r [u8] {
14969 self.0
14970 }
14971 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
14972 use molecule::verification_error as ve;
14973 let slice_len = slice.len();
14974 if slice_len < molecule::NUMBER_SIZE {
14975 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
14976 }
14977 let item_count = molecule::unpack_number(slice) as usize;
14978 if item_count == 0 {
14979 if slice_len != molecule::NUMBER_SIZE {
14980 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
14981 }
14982 return Ok(());
14983 }
14984 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
14985 if slice_len != total_size {
14986 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
14987 }
14988 Ok(())
14989 }
14990}
14991#[derive(Clone, Debug, Default)]
14992pub struct HeaderDigestVecBuilder(pub(crate) Vec<HeaderDigest>);
14993impl HeaderDigestVecBuilder {
14994 pub const ITEM_SIZE: usize = 120;
14995 pub fn set(mut self, v: Vec<HeaderDigest>) -> Self {
14996 self.0 = v;
14997 self
14998 }
14999 pub fn push<T>(mut self, v: T) -> Self
15000 where
15001 T: ::core::convert::Into<HeaderDigest>,
15002 {
15003 self.0.push(v.into());
15004 self
15005 }
15006 pub fn extend<T: ::core::iter::IntoIterator<Item = HeaderDigest>>(mut self, iter: T) -> Self {
15007 self.0.extend(iter);
15008 self
15009 }
15010 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<HeaderDigest>
15011 where
15012 T: ::core::convert::Into<HeaderDigest>,
15013 {
15014 self.0
15015 .get_mut(index)
15016 .map(|item| ::core::mem::replace(item, v.into()))
15017 }
15018}
15019impl molecule::prelude::Builder for HeaderDigestVecBuilder {
15020 type Entity = HeaderDigestVec;
15021 const NAME: &'static str = "HeaderDigestVecBuilder";
15022 fn expected_length(&self) -> usize {
15023 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
15024 }
15025 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
15026 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
15027 for inner in &self.0[..] {
15028 writer.write_all(inner.as_slice())?;
15029 }
15030 Ok(())
15031 }
15032 fn build(&self) -> Self::Entity {
15033 let mut inner = Vec::with_capacity(self.expected_length());
15034 self.write(&mut inner)
15035 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
15036 HeaderDigestVec::new_unchecked(inner.into())
15037 }
15038}
15039pub struct HeaderDigestVecIterator(HeaderDigestVec, usize, usize);
15040impl ::core::iter::Iterator for HeaderDigestVecIterator {
15041 type Item = HeaderDigest;
15042 fn next(&mut self) -> Option<Self::Item> {
15043 if self.1 >= self.2 {
15044 None
15045 } else {
15046 let ret = self.0.get_unchecked(self.1);
15047 self.1 += 1;
15048 Some(ret)
15049 }
15050 }
15051}
15052impl ::core::iter::ExactSizeIterator for HeaderDigestVecIterator {
15053 fn len(&self) -> usize {
15054 self.2 - self.1
15055 }
15056}
15057impl ::core::iter::IntoIterator for HeaderDigestVec {
15058 type Item = HeaderDigest;
15059 type IntoIter = HeaderDigestVecIterator;
15060 fn into_iter(self) -> Self::IntoIter {
15061 let len = self.len();
15062 HeaderDigestVecIterator(self, 0, len)
15063 }
15064}
15065impl<'r> HeaderDigestVecReader<'r> {
15066 pub fn iter<'t>(&'t self) -> HeaderDigestVecReaderIterator<'t, 'r> {
15067 HeaderDigestVecReaderIterator(&self, 0, self.len())
15068 }
15069}
15070pub struct HeaderDigestVecReaderIterator<'t, 'r>(&'t HeaderDigestVecReader<'r>, usize, usize);
15071impl<'t: 'r, 'r> ::core::iter::Iterator for HeaderDigestVecReaderIterator<'t, 'r> {
15072 type Item = HeaderDigestReader<'t>;
15073 fn next(&mut self) -> Option<Self::Item> {
15074 if self.1 >= self.2 {
15075 None
15076 } else {
15077 let ret = self.0.get_unchecked(self.1);
15078 self.1 += 1;
15079 Some(ret)
15080 }
15081 }
15082}
15083impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for HeaderDigestVecReaderIterator<'t, 'r> {
15084 fn len(&self) -> usize {
15085 self.2 - self.1
15086 }
15087}
15088impl<T> ::core::iter::FromIterator<T> for HeaderDigestVec
15089where
15090 T: Into<HeaderDigest>,
15091{
15092 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
15093 Self::new_builder()
15094 .extend(iter.into_iter().map(Into::into))
15095 .build()
15096 }
15097}
15098impl<T> From<Vec<T>> for HeaderDigestVec
15099where
15100 T: Into<HeaderDigest>,
15101{
15102 fn from(v: Vec<T>) -> Self {
15103 v.into_iter().collect()
15104 }
15105}
15106#[derive(Clone)]
15107pub struct VerifiableHeader(molecule::bytes::Bytes);
15108impl ::core::fmt::LowerHex for VerifiableHeader {
15109 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15110 use molecule::hex_string;
15111 if f.alternate() {
15112 write!(f, "0x")?;
15113 }
15114 write!(f, "{}", hex_string(self.as_slice()))
15115 }
15116}
15117impl ::core::fmt::Debug for VerifiableHeader {
15118 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15119 write!(f, "{}({:#x})", Self::NAME, self)
15120 }
15121}
15122impl ::core::fmt::Display for VerifiableHeader {
15123 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15124 write!(f, "{} {{ ", Self::NAME)?;
15125 write!(f, "{}: {}", "header", self.header())?;
15126 write!(f, ", {}: {}", "uncles_hash", self.uncles_hash())?;
15127 write!(f, ", {}: {}", "extension", self.extension())?;
15128 write!(f, ", {}: {}", "parent_chain_root", self.parent_chain_root())?;
15129 let extra_count = self.count_extra_fields();
15130 if extra_count != 0 {
15131 write!(f, ", .. ({} fields)", extra_count)?;
15132 }
15133 write!(f, " }}")
15134 }
15135}
15136impl ::core::default::Default for VerifiableHeader {
15137 fn default() -> Self {
15138 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
15139 VerifiableHeader::new_unchecked(v)
15140 }
15141}
15142impl VerifiableHeader {
15143 const DEFAULT_VALUE: [u8; 380] = [
15144 124, 1, 0, 0, 20, 0, 0, 0, 228, 0, 0, 0, 4, 1, 0, 0, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15145 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15150 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15152 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15153 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15154 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15157 ];
15158 pub const FIELD_COUNT: usize = 4;
15159 pub fn total_size(&self) -> usize {
15160 molecule::unpack_number(self.as_slice()) as usize
15161 }
15162 pub fn field_count(&self) -> usize {
15163 if self.total_size() == molecule::NUMBER_SIZE {
15164 0
15165 } else {
15166 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
15167 }
15168 }
15169 pub fn count_extra_fields(&self) -> usize {
15170 self.field_count() - Self::FIELD_COUNT
15171 }
15172 pub fn has_extra_fields(&self) -> bool {
15173 Self::FIELD_COUNT != self.field_count()
15174 }
15175 pub fn header(&self) -> Header {
15176 let slice = self.as_slice();
15177 let start = molecule::unpack_number(&slice[4..]) as usize;
15178 let end = molecule::unpack_number(&slice[8..]) as usize;
15179 Header::new_unchecked(self.0.slice(start..end))
15180 }
15181 pub fn uncles_hash(&self) -> Byte32 {
15182 let slice = self.as_slice();
15183 let start = molecule::unpack_number(&slice[8..]) as usize;
15184 let end = molecule::unpack_number(&slice[12..]) as usize;
15185 Byte32::new_unchecked(self.0.slice(start..end))
15186 }
15187 pub fn extension(&self) -> BytesOpt {
15188 let slice = self.as_slice();
15189 let start = molecule::unpack_number(&slice[12..]) as usize;
15190 let end = molecule::unpack_number(&slice[16..]) as usize;
15191 BytesOpt::new_unchecked(self.0.slice(start..end))
15192 }
15193 pub fn parent_chain_root(&self) -> HeaderDigest {
15194 let slice = self.as_slice();
15195 let start = molecule::unpack_number(&slice[16..]) as usize;
15196 if self.has_extra_fields() {
15197 let end = molecule::unpack_number(&slice[20..]) as usize;
15198 HeaderDigest::new_unchecked(self.0.slice(start..end))
15199 } else {
15200 HeaderDigest::new_unchecked(self.0.slice(start..))
15201 }
15202 }
15203 pub fn as_reader<'r>(&'r self) -> VerifiableHeaderReader<'r> {
15204 VerifiableHeaderReader::new_unchecked(self.as_slice())
15205 }
15206}
15207impl molecule::prelude::Entity for VerifiableHeader {
15208 type Builder = VerifiableHeaderBuilder;
15209 const NAME: &'static str = "VerifiableHeader";
15210 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
15211 VerifiableHeader(data)
15212 }
15213 fn as_bytes(&self) -> molecule::bytes::Bytes {
15214 self.0.clone()
15215 }
15216 fn as_slice(&self) -> &[u8] {
15217 &self.0[..]
15218 }
15219 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15220 VerifiableHeaderReader::from_slice(slice).map(|reader| reader.to_entity())
15221 }
15222 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15223 VerifiableHeaderReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
15224 }
15225 fn new_builder() -> Self::Builder {
15226 ::core::default::Default::default()
15227 }
15228 fn as_builder(self) -> Self::Builder {
15229 Self::new_builder()
15230 .header(self.header())
15231 .uncles_hash(self.uncles_hash())
15232 .extension(self.extension())
15233 .parent_chain_root(self.parent_chain_root())
15234 }
15235}
15236#[derive(Clone, Copy)]
15237pub struct VerifiableHeaderReader<'r>(&'r [u8]);
15238impl<'r> ::core::fmt::LowerHex for VerifiableHeaderReader<'r> {
15239 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15240 use molecule::hex_string;
15241 if f.alternate() {
15242 write!(f, "0x")?;
15243 }
15244 write!(f, "{}", hex_string(self.as_slice()))
15245 }
15246}
15247impl<'r> ::core::fmt::Debug for VerifiableHeaderReader<'r> {
15248 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15249 write!(f, "{}({:#x})", Self::NAME, self)
15250 }
15251}
15252impl<'r> ::core::fmt::Display for VerifiableHeaderReader<'r> {
15253 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15254 write!(f, "{} {{ ", Self::NAME)?;
15255 write!(f, "{}: {}", "header", self.header())?;
15256 write!(f, ", {}: {}", "uncles_hash", self.uncles_hash())?;
15257 write!(f, ", {}: {}", "extension", self.extension())?;
15258 write!(f, ", {}: {}", "parent_chain_root", self.parent_chain_root())?;
15259 let extra_count = self.count_extra_fields();
15260 if extra_count != 0 {
15261 write!(f, ", .. ({} fields)", extra_count)?;
15262 }
15263 write!(f, " }}")
15264 }
15265}
15266impl<'r> VerifiableHeaderReader<'r> {
15267 pub const FIELD_COUNT: usize = 4;
15268 pub fn total_size(&self) -> usize {
15269 molecule::unpack_number(self.as_slice()) as usize
15270 }
15271 pub fn field_count(&self) -> usize {
15272 if self.total_size() == molecule::NUMBER_SIZE {
15273 0
15274 } else {
15275 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
15276 }
15277 }
15278 pub fn count_extra_fields(&self) -> usize {
15279 self.field_count() - Self::FIELD_COUNT
15280 }
15281 pub fn has_extra_fields(&self) -> bool {
15282 Self::FIELD_COUNT != self.field_count()
15283 }
15284 pub fn header(&self) -> HeaderReader<'r> {
15285 let slice = self.as_slice();
15286 let start = molecule::unpack_number(&slice[4..]) as usize;
15287 let end = molecule::unpack_number(&slice[8..]) as usize;
15288 HeaderReader::new_unchecked(&self.as_slice()[start..end])
15289 }
15290 pub fn uncles_hash(&self) -> Byte32Reader<'r> {
15291 let slice = self.as_slice();
15292 let start = molecule::unpack_number(&slice[8..]) as usize;
15293 let end = molecule::unpack_number(&slice[12..]) as usize;
15294 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
15295 }
15296 pub fn extension(&self) -> BytesOptReader<'r> {
15297 let slice = self.as_slice();
15298 let start = molecule::unpack_number(&slice[12..]) as usize;
15299 let end = molecule::unpack_number(&slice[16..]) as usize;
15300 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
15301 }
15302 pub fn parent_chain_root(&self) -> HeaderDigestReader<'r> {
15303 let slice = self.as_slice();
15304 let start = molecule::unpack_number(&slice[16..]) as usize;
15305 if self.has_extra_fields() {
15306 let end = molecule::unpack_number(&slice[20..]) as usize;
15307 HeaderDigestReader::new_unchecked(&self.as_slice()[start..end])
15308 } else {
15309 HeaderDigestReader::new_unchecked(&self.as_slice()[start..])
15310 }
15311 }
15312}
15313impl<'r> molecule::prelude::Reader<'r> for VerifiableHeaderReader<'r> {
15314 type Entity = VerifiableHeader;
15315 const NAME: &'static str = "VerifiableHeaderReader";
15316 fn to_entity(&self) -> Self::Entity {
15317 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
15318 }
15319 fn new_unchecked(slice: &'r [u8]) -> Self {
15320 VerifiableHeaderReader(slice)
15321 }
15322 fn as_slice(&self) -> &'r [u8] {
15323 self.0
15324 }
15325 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
15326 use molecule::verification_error as ve;
15327 let slice_len = slice.len();
15328 if slice_len < molecule::NUMBER_SIZE {
15329 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
15330 }
15331 let total_size = molecule::unpack_number(slice) as usize;
15332 if slice_len != total_size {
15333 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
15334 }
15335 if slice_len < molecule::NUMBER_SIZE * 2 {
15336 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
15337 }
15338 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
15339 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
15340 return ve!(Self, OffsetsNotMatch);
15341 }
15342 if slice_len < offset_first {
15343 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
15344 }
15345 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
15346 if field_count < Self::FIELD_COUNT {
15347 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
15348 } else if !compatible && field_count > Self::FIELD_COUNT {
15349 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
15350 };
15351 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
15352 .chunks_exact(molecule::NUMBER_SIZE)
15353 .map(|x| molecule::unpack_number(x) as usize)
15354 .collect();
15355 offsets.push(total_size);
15356 if offsets.windows(2).any(|i| i[0] > i[1]) {
15357 return ve!(Self, OffsetsNotMatch);
15358 }
15359 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
15360 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
15361 BytesOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
15362 HeaderDigestReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
15363 Ok(())
15364 }
15365}
15366#[derive(Clone, Debug, Default)]
15367pub struct VerifiableHeaderBuilder {
15368 pub(crate) header: Header,
15369 pub(crate) uncles_hash: Byte32,
15370 pub(crate) extension: BytesOpt,
15371 pub(crate) parent_chain_root: HeaderDigest,
15372}
15373impl VerifiableHeaderBuilder {
15374 pub const FIELD_COUNT: usize = 4;
15375 pub fn header<T>(mut self, v: T) -> Self
15376 where
15377 T: ::core::convert::Into<Header>,
15378 {
15379 self.header = v.into();
15380 self
15381 }
15382 pub fn uncles_hash<T>(mut self, v: T) -> Self
15383 where
15384 T: ::core::convert::Into<Byte32>,
15385 {
15386 self.uncles_hash = v.into();
15387 self
15388 }
15389 pub fn extension<T>(mut self, v: T) -> Self
15390 where
15391 T: ::core::convert::Into<BytesOpt>,
15392 {
15393 self.extension = v.into();
15394 self
15395 }
15396 pub fn parent_chain_root<T>(mut self, v: T) -> Self
15397 where
15398 T: ::core::convert::Into<HeaderDigest>,
15399 {
15400 self.parent_chain_root = v.into();
15401 self
15402 }
15403}
15404impl molecule::prelude::Builder for VerifiableHeaderBuilder {
15405 type Entity = VerifiableHeader;
15406 const NAME: &'static str = "VerifiableHeaderBuilder";
15407 fn expected_length(&self) -> usize {
15408 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
15409 + self.header.as_slice().len()
15410 + self.uncles_hash.as_slice().len()
15411 + self.extension.as_slice().len()
15412 + self.parent_chain_root.as_slice().len()
15413 }
15414 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
15415 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
15416 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
15417 offsets.push(total_size);
15418 total_size += self.header.as_slice().len();
15419 offsets.push(total_size);
15420 total_size += self.uncles_hash.as_slice().len();
15421 offsets.push(total_size);
15422 total_size += self.extension.as_slice().len();
15423 offsets.push(total_size);
15424 total_size += self.parent_chain_root.as_slice().len();
15425 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
15426 for offset in offsets.into_iter() {
15427 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
15428 }
15429 writer.write_all(self.header.as_slice())?;
15430 writer.write_all(self.uncles_hash.as_slice())?;
15431 writer.write_all(self.extension.as_slice())?;
15432 writer.write_all(self.parent_chain_root.as_slice())?;
15433 Ok(())
15434 }
15435 fn build(&self) -> Self::Entity {
15436 let mut inner = Vec::with_capacity(self.expected_length());
15437 self.write(&mut inner)
15438 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
15439 VerifiableHeader::new_unchecked(inner.into())
15440 }
15441}
15442#[derive(Clone)]
15443pub struct VerifiableHeaderVec(molecule::bytes::Bytes);
15444impl ::core::fmt::LowerHex for VerifiableHeaderVec {
15445 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15446 use molecule::hex_string;
15447 if f.alternate() {
15448 write!(f, "0x")?;
15449 }
15450 write!(f, "{}", hex_string(self.as_slice()))
15451 }
15452}
15453impl ::core::fmt::Debug for VerifiableHeaderVec {
15454 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15455 write!(f, "{}({:#x})", Self::NAME, self)
15456 }
15457}
15458impl ::core::fmt::Display for VerifiableHeaderVec {
15459 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15460 write!(f, "{} [", Self::NAME)?;
15461 for i in 0..self.len() {
15462 if i == 0 {
15463 write!(f, "{}", self.get_unchecked(i))?;
15464 } else {
15465 write!(f, ", {}", self.get_unchecked(i))?;
15466 }
15467 }
15468 write!(f, "]")
15469 }
15470}
15471impl ::core::default::Default for VerifiableHeaderVec {
15472 fn default() -> Self {
15473 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
15474 VerifiableHeaderVec::new_unchecked(v)
15475 }
15476}
15477impl VerifiableHeaderVec {
15478 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
15479 pub fn total_size(&self) -> usize {
15480 molecule::unpack_number(self.as_slice()) as usize
15481 }
15482 pub fn item_count(&self) -> usize {
15483 if self.total_size() == molecule::NUMBER_SIZE {
15484 0
15485 } else {
15486 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
15487 }
15488 }
15489 pub fn len(&self) -> usize {
15490 self.item_count()
15491 }
15492 pub fn is_empty(&self) -> bool {
15493 self.len() == 0
15494 }
15495 pub fn get(&self, idx: usize) -> Option<VerifiableHeader> {
15496 if idx >= self.len() {
15497 None
15498 } else {
15499 Some(self.get_unchecked(idx))
15500 }
15501 }
15502 pub fn get_unchecked(&self, idx: usize) -> VerifiableHeader {
15503 let slice = self.as_slice();
15504 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
15505 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
15506 if idx == self.len() - 1 {
15507 VerifiableHeader::new_unchecked(self.0.slice(start..))
15508 } else {
15509 let end_idx = start_idx + molecule::NUMBER_SIZE;
15510 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
15511 VerifiableHeader::new_unchecked(self.0.slice(start..end))
15512 }
15513 }
15514 pub fn as_reader<'r>(&'r self) -> VerifiableHeaderVecReader<'r> {
15515 VerifiableHeaderVecReader::new_unchecked(self.as_slice())
15516 }
15517}
15518impl molecule::prelude::Entity for VerifiableHeaderVec {
15519 type Builder = VerifiableHeaderVecBuilder;
15520 const NAME: &'static str = "VerifiableHeaderVec";
15521 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
15522 VerifiableHeaderVec(data)
15523 }
15524 fn as_bytes(&self) -> molecule::bytes::Bytes {
15525 self.0.clone()
15526 }
15527 fn as_slice(&self) -> &[u8] {
15528 &self.0[..]
15529 }
15530 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15531 VerifiableHeaderVecReader::from_slice(slice).map(|reader| reader.to_entity())
15532 }
15533 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15534 VerifiableHeaderVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
15535 }
15536 fn new_builder() -> Self::Builder {
15537 ::core::default::Default::default()
15538 }
15539 fn as_builder(self) -> Self::Builder {
15540 Self::new_builder().extend(self.into_iter())
15541 }
15542}
15543#[derive(Clone, Copy)]
15544pub struct VerifiableHeaderVecReader<'r>(&'r [u8]);
15545impl<'r> ::core::fmt::LowerHex for VerifiableHeaderVecReader<'r> {
15546 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15547 use molecule::hex_string;
15548 if f.alternate() {
15549 write!(f, "0x")?;
15550 }
15551 write!(f, "{}", hex_string(self.as_slice()))
15552 }
15553}
15554impl<'r> ::core::fmt::Debug for VerifiableHeaderVecReader<'r> {
15555 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15556 write!(f, "{}({:#x})", Self::NAME, self)
15557 }
15558}
15559impl<'r> ::core::fmt::Display for VerifiableHeaderVecReader<'r> {
15560 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15561 write!(f, "{} [", Self::NAME)?;
15562 for i in 0..self.len() {
15563 if i == 0 {
15564 write!(f, "{}", self.get_unchecked(i))?;
15565 } else {
15566 write!(f, ", {}", self.get_unchecked(i))?;
15567 }
15568 }
15569 write!(f, "]")
15570 }
15571}
15572impl<'r> VerifiableHeaderVecReader<'r> {
15573 pub fn total_size(&self) -> usize {
15574 molecule::unpack_number(self.as_slice()) as usize
15575 }
15576 pub fn item_count(&self) -> usize {
15577 if self.total_size() == molecule::NUMBER_SIZE {
15578 0
15579 } else {
15580 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
15581 }
15582 }
15583 pub fn len(&self) -> usize {
15584 self.item_count()
15585 }
15586 pub fn is_empty(&self) -> bool {
15587 self.len() == 0
15588 }
15589 pub fn get(&self, idx: usize) -> Option<VerifiableHeaderReader<'r>> {
15590 if idx >= self.len() {
15591 None
15592 } else {
15593 Some(self.get_unchecked(idx))
15594 }
15595 }
15596 pub fn get_unchecked(&self, idx: usize) -> VerifiableHeaderReader<'r> {
15597 let slice = self.as_slice();
15598 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
15599 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
15600 if idx == self.len() - 1 {
15601 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..])
15602 } else {
15603 let end_idx = start_idx + molecule::NUMBER_SIZE;
15604 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
15605 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
15606 }
15607 }
15608}
15609impl<'r> molecule::prelude::Reader<'r> for VerifiableHeaderVecReader<'r> {
15610 type Entity = VerifiableHeaderVec;
15611 const NAME: &'static str = "VerifiableHeaderVecReader";
15612 fn to_entity(&self) -> Self::Entity {
15613 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
15614 }
15615 fn new_unchecked(slice: &'r [u8]) -> Self {
15616 VerifiableHeaderVecReader(slice)
15617 }
15618 fn as_slice(&self) -> &'r [u8] {
15619 self.0
15620 }
15621 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
15622 use molecule::verification_error as ve;
15623 let slice_len = slice.len();
15624 if slice_len < molecule::NUMBER_SIZE {
15625 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
15626 }
15627 let total_size = molecule::unpack_number(slice) as usize;
15628 if slice_len != total_size {
15629 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
15630 }
15631 if slice_len == molecule::NUMBER_SIZE {
15632 return Ok(());
15633 }
15634 if slice_len < molecule::NUMBER_SIZE * 2 {
15635 return ve!(
15636 Self,
15637 TotalSizeNotMatch,
15638 molecule::NUMBER_SIZE * 2,
15639 slice_len
15640 );
15641 }
15642 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
15643 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
15644 return ve!(Self, OffsetsNotMatch);
15645 }
15646 if slice_len < offset_first {
15647 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
15648 }
15649 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
15650 .chunks_exact(molecule::NUMBER_SIZE)
15651 .map(|x| molecule::unpack_number(x) as usize)
15652 .collect();
15653 offsets.push(total_size);
15654 if offsets.windows(2).any(|i| i[0] > i[1]) {
15655 return ve!(Self, OffsetsNotMatch);
15656 }
15657 for pair in offsets.windows(2) {
15658 let start = pair[0];
15659 let end = pair[1];
15660 VerifiableHeaderReader::verify(&slice[start..end], compatible)?;
15661 }
15662 Ok(())
15663 }
15664}
15665#[derive(Clone, Debug, Default)]
15666pub struct VerifiableHeaderVecBuilder(pub(crate) Vec<VerifiableHeader>);
15667impl VerifiableHeaderVecBuilder {
15668 pub fn set(mut self, v: Vec<VerifiableHeader>) -> Self {
15669 self.0 = v;
15670 self
15671 }
15672 pub fn push<T>(mut self, v: T) -> Self
15673 where
15674 T: ::core::convert::Into<VerifiableHeader>,
15675 {
15676 self.0.push(v.into());
15677 self
15678 }
15679 pub fn extend<T: ::core::iter::IntoIterator<Item = VerifiableHeader>>(
15680 mut self,
15681 iter: T,
15682 ) -> Self {
15683 self.0.extend(iter);
15684 self
15685 }
15686 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<VerifiableHeader>
15687 where
15688 T: ::core::convert::Into<VerifiableHeader>,
15689 {
15690 self.0
15691 .get_mut(index)
15692 .map(|item| ::core::mem::replace(item, v.into()))
15693 }
15694}
15695impl molecule::prelude::Builder for VerifiableHeaderVecBuilder {
15696 type Entity = VerifiableHeaderVec;
15697 const NAME: &'static str = "VerifiableHeaderVecBuilder";
15698 fn expected_length(&self) -> usize {
15699 molecule::NUMBER_SIZE * (self.0.len() + 1)
15700 + self
15701 .0
15702 .iter()
15703 .map(|inner| inner.as_slice().len())
15704 .sum::<usize>()
15705 }
15706 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
15707 let item_count = self.0.len();
15708 if item_count == 0 {
15709 writer.write_all(&molecule::pack_number(
15710 molecule::NUMBER_SIZE as molecule::Number,
15711 ))?;
15712 } else {
15713 let (total_size, offsets) = self.0.iter().fold(
15714 (
15715 molecule::NUMBER_SIZE * (item_count + 1),
15716 Vec::with_capacity(item_count),
15717 ),
15718 |(start, mut offsets), inner| {
15719 offsets.push(start);
15720 (start + inner.as_slice().len(), offsets)
15721 },
15722 );
15723 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
15724 for offset in offsets.into_iter() {
15725 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
15726 }
15727 for inner in self.0.iter() {
15728 writer.write_all(inner.as_slice())?;
15729 }
15730 }
15731 Ok(())
15732 }
15733 fn build(&self) -> Self::Entity {
15734 let mut inner = Vec::with_capacity(self.expected_length());
15735 self.write(&mut inner)
15736 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
15737 VerifiableHeaderVec::new_unchecked(inner.into())
15738 }
15739}
15740pub struct VerifiableHeaderVecIterator(VerifiableHeaderVec, usize, usize);
15741impl ::core::iter::Iterator for VerifiableHeaderVecIterator {
15742 type Item = VerifiableHeader;
15743 fn next(&mut self) -> Option<Self::Item> {
15744 if self.1 >= self.2 {
15745 None
15746 } else {
15747 let ret = self.0.get_unchecked(self.1);
15748 self.1 += 1;
15749 Some(ret)
15750 }
15751 }
15752}
15753impl ::core::iter::ExactSizeIterator for VerifiableHeaderVecIterator {
15754 fn len(&self) -> usize {
15755 self.2 - self.1
15756 }
15757}
15758impl ::core::iter::IntoIterator for VerifiableHeaderVec {
15759 type Item = VerifiableHeader;
15760 type IntoIter = VerifiableHeaderVecIterator;
15761 fn into_iter(self) -> Self::IntoIter {
15762 let len = self.len();
15763 VerifiableHeaderVecIterator(self, 0, len)
15764 }
15765}
15766impl<'r> VerifiableHeaderVecReader<'r> {
15767 pub fn iter<'t>(&'t self) -> VerifiableHeaderVecReaderIterator<'t, 'r> {
15768 VerifiableHeaderVecReaderIterator(&self, 0, self.len())
15769 }
15770}
15771pub struct VerifiableHeaderVecReaderIterator<'t, 'r>(
15772 &'t VerifiableHeaderVecReader<'r>,
15773 usize,
15774 usize,
15775);
15776impl<'t: 'r, 'r> ::core::iter::Iterator for VerifiableHeaderVecReaderIterator<'t, 'r> {
15777 type Item = VerifiableHeaderReader<'t>;
15778 fn next(&mut self) -> Option<Self::Item> {
15779 if self.1 >= self.2 {
15780 None
15781 } else {
15782 let ret = self.0.get_unchecked(self.1);
15783 self.1 += 1;
15784 Some(ret)
15785 }
15786 }
15787}
15788impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for VerifiableHeaderVecReaderIterator<'t, 'r> {
15789 fn len(&self) -> usize {
15790 self.2 - self.1
15791 }
15792}
15793impl<T> ::core::iter::FromIterator<T> for VerifiableHeaderVec
15794where
15795 T: Into<VerifiableHeader>,
15796{
15797 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
15798 Self::new_builder()
15799 .extend(iter.into_iter().map(Into::into))
15800 .build()
15801 }
15802}
15803impl<T> From<Vec<T>> for VerifiableHeaderVec
15804where
15805 T: Into<VerifiableHeader>,
15806{
15807 fn from(v: Vec<T>) -> Self {
15808 v.into_iter().collect()
15809 }
15810}
15811#[derive(Clone)]
15812pub struct FilteredBlockVec(molecule::bytes::Bytes);
15813impl ::core::fmt::LowerHex for FilteredBlockVec {
15814 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15815 use molecule::hex_string;
15816 if f.alternate() {
15817 write!(f, "0x")?;
15818 }
15819 write!(f, "{}", hex_string(self.as_slice()))
15820 }
15821}
15822impl ::core::fmt::Debug for FilteredBlockVec {
15823 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15824 write!(f, "{}({:#x})", Self::NAME, self)
15825 }
15826}
15827impl ::core::fmt::Display for FilteredBlockVec {
15828 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15829 write!(f, "{} [", Self::NAME)?;
15830 for i in 0..self.len() {
15831 if i == 0 {
15832 write!(f, "{}", self.get_unchecked(i))?;
15833 } else {
15834 write!(f, ", {}", self.get_unchecked(i))?;
15835 }
15836 }
15837 write!(f, "]")
15838 }
15839}
15840impl ::core::default::Default for FilteredBlockVec {
15841 fn default() -> Self {
15842 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
15843 FilteredBlockVec::new_unchecked(v)
15844 }
15845}
15846impl FilteredBlockVec {
15847 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
15848 pub fn total_size(&self) -> usize {
15849 molecule::unpack_number(self.as_slice()) as usize
15850 }
15851 pub fn item_count(&self) -> usize {
15852 if self.total_size() == molecule::NUMBER_SIZE {
15853 0
15854 } else {
15855 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
15856 }
15857 }
15858 pub fn len(&self) -> usize {
15859 self.item_count()
15860 }
15861 pub fn is_empty(&self) -> bool {
15862 self.len() == 0
15863 }
15864 pub fn get(&self, idx: usize) -> Option<FilteredBlock> {
15865 if idx >= self.len() {
15866 None
15867 } else {
15868 Some(self.get_unchecked(idx))
15869 }
15870 }
15871 pub fn get_unchecked(&self, idx: usize) -> FilteredBlock {
15872 let slice = self.as_slice();
15873 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
15874 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
15875 if idx == self.len() - 1 {
15876 FilteredBlock::new_unchecked(self.0.slice(start..))
15877 } else {
15878 let end_idx = start_idx + molecule::NUMBER_SIZE;
15879 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
15880 FilteredBlock::new_unchecked(self.0.slice(start..end))
15881 }
15882 }
15883 pub fn as_reader<'r>(&'r self) -> FilteredBlockVecReader<'r> {
15884 FilteredBlockVecReader::new_unchecked(self.as_slice())
15885 }
15886}
15887impl molecule::prelude::Entity for FilteredBlockVec {
15888 type Builder = FilteredBlockVecBuilder;
15889 const NAME: &'static str = "FilteredBlockVec";
15890 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
15891 FilteredBlockVec(data)
15892 }
15893 fn as_bytes(&self) -> molecule::bytes::Bytes {
15894 self.0.clone()
15895 }
15896 fn as_slice(&self) -> &[u8] {
15897 &self.0[..]
15898 }
15899 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15900 FilteredBlockVecReader::from_slice(slice).map(|reader| reader.to_entity())
15901 }
15902 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15903 FilteredBlockVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
15904 }
15905 fn new_builder() -> Self::Builder {
15906 ::core::default::Default::default()
15907 }
15908 fn as_builder(self) -> Self::Builder {
15909 Self::new_builder().extend(self.into_iter())
15910 }
15911}
15912#[derive(Clone, Copy)]
15913pub struct FilteredBlockVecReader<'r>(&'r [u8]);
15914impl<'r> ::core::fmt::LowerHex for FilteredBlockVecReader<'r> {
15915 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15916 use molecule::hex_string;
15917 if f.alternate() {
15918 write!(f, "0x")?;
15919 }
15920 write!(f, "{}", hex_string(self.as_slice()))
15921 }
15922}
15923impl<'r> ::core::fmt::Debug for FilteredBlockVecReader<'r> {
15924 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15925 write!(f, "{}({:#x})", Self::NAME, self)
15926 }
15927}
15928impl<'r> ::core::fmt::Display for FilteredBlockVecReader<'r> {
15929 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15930 write!(f, "{} [", Self::NAME)?;
15931 for i in 0..self.len() {
15932 if i == 0 {
15933 write!(f, "{}", self.get_unchecked(i))?;
15934 } else {
15935 write!(f, ", {}", self.get_unchecked(i))?;
15936 }
15937 }
15938 write!(f, "]")
15939 }
15940}
15941impl<'r> FilteredBlockVecReader<'r> {
15942 pub fn total_size(&self) -> usize {
15943 molecule::unpack_number(self.as_slice()) as usize
15944 }
15945 pub fn item_count(&self) -> usize {
15946 if self.total_size() == molecule::NUMBER_SIZE {
15947 0
15948 } else {
15949 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
15950 }
15951 }
15952 pub fn len(&self) -> usize {
15953 self.item_count()
15954 }
15955 pub fn is_empty(&self) -> bool {
15956 self.len() == 0
15957 }
15958 pub fn get(&self, idx: usize) -> Option<FilteredBlockReader<'r>> {
15959 if idx >= self.len() {
15960 None
15961 } else {
15962 Some(self.get_unchecked(idx))
15963 }
15964 }
15965 pub fn get_unchecked(&self, idx: usize) -> FilteredBlockReader<'r> {
15966 let slice = self.as_slice();
15967 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
15968 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
15969 if idx == self.len() - 1 {
15970 FilteredBlockReader::new_unchecked(&self.as_slice()[start..])
15971 } else {
15972 let end_idx = start_idx + molecule::NUMBER_SIZE;
15973 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
15974 FilteredBlockReader::new_unchecked(&self.as_slice()[start..end])
15975 }
15976 }
15977}
15978impl<'r> molecule::prelude::Reader<'r> for FilteredBlockVecReader<'r> {
15979 type Entity = FilteredBlockVec;
15980 const NAME: &'static str = "FilteredBlockVecReader";
15981 fn to_entity(&self) -> Self::Entity {
15982 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
15983 }
15984 fn new_unchecked(slice: &'r [u8]) -> Self {
15985 FilteredBlockVecReader(slice)
15986 }
15987 fn as_slice(&self) -> &'r [u8] {
15988 self.0
15989 }
15990 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
15991 use molecule::verification_error as ve;
15992 let slice_len = slice.len();
15993 if slice_len < molecule::NUMBER_SIZE {
15994 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
15995 }
15996 let total_size = molecule::unpack_number(slice) as usize;
15997 if slice_len != total_size {
15998 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
15999 }
16000 if slice_len == molecule::NUMBER_SIZE {
16001 return Ok(());
16002 }
16003 if slice_len < molecule::NUMBER_SIZE * 2 {
16004 return ve!(
16005 Self,
16006 TotalSizeNotMatch,
16007 molecule::NUMBER_SIZE * 2,
16008 slice_len
16009 );
16010 }
16011 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
16012 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
16013 return ve!(Self, OffsetsNotMatch);
16014 }
16015 if slice_len < offset_first {
16016 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
16017 }
16018 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
16019 .chunks_exact(molecule::NUMBER_SIZE)
16020 .map(|x| molecule::unpack_number(x) as usize)
16021 .collect();
16022 offsets.push(total_size);
16023 if offsets.windows(2).any(|i| i[0] > i[1]) {
16024 return ve!(Self, OffsetsNotMatch);
16025 }
16026 for pair in offsets.windows(2) {
16027 let start = pair[0];
16028 let end = pair[1];
16029 FilteredBlockReader::verify(&slice[start..end], compatible)?;
16030 }
16031 Ok(())
16032 }
16033}
16034#[derive(Clone, Debug, Default)]
16035pub struct FilteredBlockVecBuilder(pub(crate) Vec<FilteredBlock>);
16036impl FilteredBlockVecBuilder {
16037 pub fn set(mut self, v: Vec<FilteredBlock>) -> Self {
16038 self.0 = v;
16039 self
16040 }
16041 pub fn push<T>(mut self, v: T) -> Self
16042 where
16043 T: ::core::convert::Into<FilteredBlock>,
16044 {
16045 self.0.push(v.into());
16046 self
16047 }
16048 pub fn extend<T: ::core::iter::IntoIterator<Item = FilteredBlock>>(mut self, iter: T) -> Self {
16049 self.0.extend(iter);
16050 self
16051 }
16052 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<FilteredBlock>
16053 where
16054 T: ::core::convert::Into<FilteredBlock>,
16055 {
16056 self.0
16057 .get_mut(index)
16058 .map(|item| ::core::mem::replace(item, v.into()))
16059 }
16060}
16061impl molecule::prelude::Builder for FilteredBlockVecBuilder {
16062 type Entity = FilteredBlockVec;
16063 const NAME: &'static str = "FilteredBlockVecBuilder";
16064 fn expected_length(&self) -> usize {
16065 molecule::NUMBER_SIZE * (self.0.len() + 1)
16066 + self
16067 .0
16068 .iter()
16069 .map(|inner| inner.as_slice().len())
16070 .sum::<usize>()
16071 }
16072 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
16073 let item_count = self.0.len();
16074 if item_count == 0 {
16075 writer.write_all(&molecule::pack_number(
16076 molecule::NUMBER_SIZE as molecule::Number,
16077 ))?;
16078 } else {
16079 let (total_size, offsets) = self.0.iter().fold(
16080 (
16081 molecule::NUMBER_SIZE * (item_count + 1),
16082 Vec::with_capacity(item_count),
16083 ),
16084 |(start, mut offsets), inner| {
16085 offsets.push(start);
16086 (start + inner.as_slice().len(), offsets)
16087 },
16088 );
16089 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
16090 for offset in offsets.into_iter() {
16091 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
16092 }
16093 for inner in self.0.iter() {
16094 writer.write_all(inner.as_slice())?;
16095 }
16096 }
16097 Ok(())
16098 }
16099 fn build(&self) -> Self::Entity {
16100 let mut inner = Vec::with_capacity(self.expected_length());
16101 self.write(&mut inner)
16102 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
16103 FilteredBlockVec::new_unchecked(inner.into())
16104 }
16105}
16106pub struct FilteredBlockVecIterator(FilteredBlockVec, usize, usize);
16107impl ::core::iter::Iterator for FilteredBlockVecIterator {
16108 type Item = FilteredBlock;
16109 fn next(&mut self) -> Option<Self::Item> {
16110 if self.1 >= self.2 {
16111 None
16112 } else {
16113 let ret = self.0.get_unchecked(self.1);
16114 self.1 += 1;
16115 Some(ret)
16116 }
16117 }
16118}
16119impl ::core::iter::ExactSizeIterator for FilteredBlockVecIterator {
16120 fn len(&self) -> usize {
16121 self.2 - self.1
16122 }
16123}
16124impl ::core::iter::IntoIterator for FilteredBlockVec {
16125 type Item = FilteredBlock;
16126 type IntoIter = FilteredBlockVecIterator;
16127 fn into_iter(self) -> Self::IntoIter {
16128 let len = self.len();
16129 FilteredBlockVecIterator(self, 0, len)
16130 }
16131}
16132impl<'r> FilteredBlockVecReader<'r> {
16133 pub fn iter<'t>(&'t self) -> FilteredBlockVecReaderIterator<'t, 'r> {
16134 FilteredBlockVecReaderIterator(&self, 0, self.len())
16135 }
16136}
16137pub struct FilteredBlockVecReaderIterator<'t, 'r>(&'t FilteredBlockVecReader<'r>, usize, usize);
16138impl<'t: 'r, 'r> ::core::iter::Iterator for FilteredBlockVecReaderIterator<'t, 'r> {
16139 type Item = FilteredBlockReader<'t>;
16140 fn next(&mut self) -> Option<Self::Item> {
16141 if self.1 >= self.2 {
16142 None
16143 } else {
16144 let ret = self.0.get_unchecked(self.1);
16145 self.1 += 1;
16146 Some(ret)
16147 }
16148 }
16149}
16150impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for FilteredBlockVecReaderIterator<'t, 'r> {
16151 fn len(&self) -> usize {
16152 self.2 - self.1
16153 }
16154}
16155impl<T> ::core::iter::FromIterator<T> for FilteredBlockVec
16156where
16157 T: Into<FilteredBlock>,
16158{
16159 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
16160 Self::new_builder()
16161 .extend(iter.into_iter().map(Into::into))
16162 .build()
16163 }
16164}
16165impl<T> From<Vec<T>> for FilteredBlockVec
16166where
16167 T: Into<FilteredBlock>,
16168{
16169 fn from(v: Vec<T>) -> Self {
16170 v.into_iter().collect()
16171 }
16172}
16173#[derive(Clone)]
16174pub struct LightClientMessage(molecule::bytes::Bytes);
16175impl ::core::fmt::LowerHex for LightClientMessage {
16176 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16177 use molecule::hex_string;
16178 if f.alternate() {
16179 write!(f, "0x")?;
16180 }
16181 write!(f, "{}", hex_string(self.as_slice()))
16182 }
16183}
16184impl ::core::fmt::Debug for LightClientMessage {
16185 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16186 write!(f, "{}({:#x})", Self::NAME, self)
16187 }
16188}
16189impl ::core::fmt::Display for LightClientMessage {
16190 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16191 write!(f, "{}(", Self::NAME)?;
16192 self.to_enum().display_inner(f)?;
16193 write!(f, ")")
16194 }
16195}
16196impl ::core::default::Default for LightClientMessage {
16197 fn default() -> Self {
16198 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
16199 LightClientMessage::new_unchecked(v)
16200 }
16201}
16202impl LightClientMessage {
16203 const DEFAULT_VALUE: [u8; 13] = [0, 0, 0, 0, 9, 0, 0, 0, 8, 0, 0, 0, 0];
16204 pub const ITEMS_COUNT: usize = 8;
16205 pub fn item_id(&self) -> molecule::Number {
16206 molecule::unpack_number(self.as_slice())
16207 }
16208 pub fn to_enum(&self) -> LightClientMessageUnion {
16209 let inner = self.0.slice(molecule::NUMBER_SIZE..);
16210 match self.item_id() {
16211 0 => GetLastState::new_unchecked(inner).into(),
16212 1 => SendLastState::new_unchecked(inner).into(),
16213 2 => GetLastStateProof::new_unchecked(inner).into(),
16214 3 => SendLastStateProof::new_unchecked(inner).into(),
16215 4 => GetBlocksProof::new_unchecked(inner).into(),
16216 5 => SendBlocksProof::new_unchecked(inner).into(),
16217 6 => GetTransactionsProof::new_unchecked(inner).into(),
16218 7 => SendTransactionsProof::new_unchecked(inner).into(),
16219 _ => panic!("{}: invalid data", Self::NAME),
16220 }
16221 }
16222 pub fn as_reader<'r>(&'r self) -> LightClientMessageReader<'r> {
16223 LightClientMessageReader::new_unchecked(self.as_slice())
16224 }
16225}
16226impl molecule::prelude::Entity for LightClientMessage {
16227 type Builder = LightClientMessageBuilder;
16228 const NAME: &'static str = "LightClientMessage";
16229 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
16230 LightClientMessage(data)
16231 }
16232 fn as_bytes(&self) -> molecule::bytes::Bytes {
16233 self.0.clone()
16234 }
16235 fn as_slice(&self) -> &[u8] {
16236 &self.0[..]
16237 }
16238 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
16239 LightClientMessageReader::from_slice(slice).map(|reader| reader.to_entity())
16240 }
16241 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
16242 LightClientMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
16243 }
16244 fn new_builder() -> Self::Builder {
16245 ::core::default::Default::default()
16246 }
16247 fn as_builder(self) -> Self::Builder {
16248 Self::new_builder().set(self.to_enum())
16249 }
16250}
16251#[derive(Clone, Copy)]
16252pub struct LightClientMessageReader<'r>(&'r [u8]);
16253impl<'r> ::core::fmt::LowerHex for LightClientMessageReader<'r> {
16254 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16255 use molecule::hex_string;
16256 if f.alternate() {
16257 write!(f, "0x")?;
16258 }
16259 write!(f, "{}", hex_string(self.as_slice()))
16260 }
16261}
16262impl<'r> ::core::fmt::Debug for LightClientMessageReader<'r> {
16263 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16264 write!(f, "{}({:#x})", Self::NAME, self)
16265 }
16266}
16267impl<'r> ::core::fmt::Display for LightClientMessageReader<'r> {
16268 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16269 write!(f, "{}(", Self::NAME)?;
16270 self.to_enum().display_inner(f)?;
16271 write!(f, ")")
16272 }
16273}
16274impl<'r> LightClientMessageReader<'r> {
16275 pub const ITEMS_COUNT: usize = 8;
16276 pub fn item_id(&self) -> molecule::Number {
16277 molecule::unpack_number(self.as_slice())
16278 }
16279 pub fn to_enum(&self) -> LightClientMessageUnionReader<'r> {
16280 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
16281 match self.item_id() {
16282 0 => GetLastStateReader::new_unchecked(inner).into(),
16283 1 => SendLastStateReader::new_unchecked(inner).into(),
16284 2 => GetLastStateProofReader::new_unchecked(inner).into(),
16285 3 => SendLastStateProofReader::new_unchecked(inner).into(),
16286 4 => GetBlocksProofReader::new_unchecked(inner).into(),
16287 5 => SendBlocksProofReader::new_unchecked(inner).into(),
16288 6 => GetTransactionsProofReader::new_unchecked(inner).into(),
16289 7 => SendTransactionsProofReader::new_unchecked(inner).into(),
16290 _ => panic!("{}: invalid data", Self::NAME),
16291 }
16292 }
16293}
16294impl<'r> molecule::prelude::Reader<'r> for LightClientMessageReader<'r> {
16295 type Entity = LightClientMessage;
16296 const NAME: &'static str = "LightClientMessageReader";
16297 fn to_entity(&self) -> Self::Entity {
16298 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
16299 }
16300 fn new_unchecked(slice: &'r [u8]) -> Self {
16301 LightClientMessageReader(slice)
16302 }
16303 fn as_slice(&self) -> &'r [u8] {
16304 self.0
16305 }
16306 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
16307 use molecule::verification_error as ve;
16308 let slice_len = slice.len();
16309 if slice_len < molecule::NUMBER_SIZE {
16310 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
16311 }
16312 let item_id = molecule::unpack_number(slice);
16313 let inner_slice = &slice[molecule::NUMBER_SIZE..];
16314 match item_id {
16315 0 => GetLastStateReader::verify(inner_slice, compatible),
16316 1 => SendLastStateReader::verify(inner_slice, compatible),
16317 2 => GetLastStateProofReader::verify(inner_slice, compatible),
16318 3 => SendLastStateProofReader::verify(inner_slice, compatible),
16319 4 => GetBlocksProofReader::verify(inner_slice, compatible),
16320 5 => SendBlocksProofReader::verify(inner_slice, compatible),
16321 6 => GetTransactionsProofReader::verify(inner_slice, compatible),
16322 7 => SendTransactionsProofReader::verify(inner_slice, compatible),
16323 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
16324 }?;
16325 Ok(())
16326 }
16327}
16328#[derive(Clone, Debug, Default)]
16329pub struct LightClientMessageBuilder(pub(crate) LightClientMessageUnion);
16330impl LightClientMessageBuilder {
16331 pub const ITEMS_COUNT: usize = 8;
16332 pub fn set<I>(mut self, v: I) -> Self
16333 where
16334 I: ::core::convert::Into<LightClientMessageUnion>,
16335 {
16336 self.0 = v.into();
16337 self
16338 }
16339}
16340impl molecule::prelude::Builder for LightClientMessageBuilder {
16341 type Entity = LightClientMessage;
16342 const NAME: &'static str = "LightClientMessageBuilder";
16343 fn expected_length(&self) -> usize {
16344 molecule::NUMBER_SIZE + self.0.as_slice().len()
16345 }
16346 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
16347 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
16348 writer.write_all(self.0.as_slice())
16349 }
16350 fn build(&self) -> Self::Entity {
16351 let mut inner = Vec::with_capacity(self.expected_length());
16352 self.write(&mut inner)
16353 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
16354 LightClientMessage::new_unchecked(inner.into())
16355 }
16356}
16357#[derive(Debug, Clone)]
16358pub enum LightClientMessageUnion {
16359 GetLastState(GetLastState),
16360 SendLastState(SendLastState),
16361 GetLastStateProof(GetLastStateProof),
16362 SendLastStateProof(SendLastStateProof),
16363 GetBlocksProof(GetBlocksProof),
16364 SendBlocksProof(SendBlocksProof),
16365 GetTransactionsProof(GetTransactionsProof),
16366 SendTransactionsProof(SendTransactionsProof),
16367}
16368#[derive(Debug, Clone, Copy)]
16369pub enum LightClientMessageUnionReader<'r> {
16370 GetLastState(GetLastStateReader<'r>),
16371 SendLastState(SendLastStateReader<'r>),
16372 GetLastStateProof(GetLastStateProofReader<'r>),
16373 SendLastStateProof(SendLastStateProofReader<'r>),
16374 GetBlocksProof(GetBlocksProofReader<'r>),
16375 SendBlocksProof(SendBlocksProofReader<'r>),
16376 GetTransactionsProof(GetTransactionsProofReader<'r>),
16377 SendTransactionsProof(SendTransactionsProofReader<'r>),
16378}
16379impl ::core::default::Default for LightClientMessageUnion {
16380 fn default() -> Self {
16381 LightClientMessageUnion::GetLastState(::core::default::Default::default())
16382 }
16383}
16384impl ::core::fmt::Display for LightClientMessageUnion {
16385 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16386 match self {
16387 LightClientMessageUnion::GetLastState(ref item) => {
16388 write!(f, "{}::{}({})", Self::NAME, GetLastState::NAME, item)
16389 }
16390 LightClientMessageUnion::SendLastState(ref item) => {
16391 write!(f, "{}::{}({})", Self::NAME, SendLastState::NAME, item)
16392 }
16393 LightClientMessageUnion::GetLastStateProof(ref item) => {
16394 write!(f, "{}::{}({})", Self::NAME, GetLastStateProof::NAME, item)
16395 }
16396 LightClientMessageUnion::SendLastStateProof(ref item) => {
16397 write!(f, "{}::{}({})", Self::NAME, SendLastStateProof::NAME, item)
16398 }
16399 LightClientMessageUnion::GetBlocksProof(ref item) => {
16400 write!(f, "{}::{}({})", Self::NAME, GetBlocksProof::NAME, item)
16401 }
16402 LightClientMessageUnion::SendBlocksProof(ref item) => {
16403 write!(f, "{}::{}({})", Self::NAME, SendBlocksProof::NAME, item)
16404 }
16405 LightClientMessageUnion::GetTransactionsProof(ref item) => {
16406 write!(
16407 f,
16408 "{}::{}({})",
16409 Self::NAME,
16410 GetTransactionsProof::NAME,
16411 item
16412 )
16413 }
16414 LightClientMessageUnion::SendTransactionsProof(ref item) => {
16415 write!(
16416 f,
16417 "{}::{}({})",
16418 Self::NAME,
16419 SendTransactionsProof::NAME,
16420 item
16421 )
16422 }
16423 }
16424 }
16425}
16426impl<'r> ::core::fmt::Display for LightClientMessageUnionReader<'r> {
16427 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16428 match self {
16429 LightClientMessageUnionReader::GetLastState(ref item) => {
16430 write!(f, "{}::{}({})", Self::NAME, GetLastState::NAME, item)
16431 }
16432 LightClientMessageUnionReader::SendLastState(ref item) => {
16433 write!(f, "{}::{}({})", Self::NAME, SendLastState::NAME, item)
16434 }
16435 LightClientMessageUnionReader::GetLastStateProof(ref item) => {
16436 write!(f, "{}::{}({})", Self::NAME, GetLastStateProof::NAME, item)
16437 }
16438 LightClientMessageUnionReader::SendLastStateProof(ref item) => {
16439 write!(f, "{}::{}({})", Self::NAME, SendLastStateProof::NAME, item)
16440 }
16441 LightClientMessageUnionReader::GetBlocksProof(ref item) => {
16442 write!(f, "{}::{}({})", Self::NAME, GetBlocksProof::NAME, item)
16443 }
16444 LightClientMessageUnionReader::SendBlocksProof(ref item) => {
16445 write!(f, "{}::{}({})", Self::NAME, SendBlocksProof::NAME, item)
16446 }
16447 LightClientMessageUnionReader::GetTransactionsProof(ref item) => {
16448 write!(
16449 f,
16450 "{}::{}({})",
16451 Self::NAME,
16452 GetTransactionsProof::NAME,
16453 item
16454 )
16455 }
16456 LightClientMessageUnionReader::SendTransactionsProof(ref item) => {
16457 write!(
16458 f,
16459 "{}::{}({})",
16460 Self::NAME,
16461 SendTransactionsProof::NAME,
16462 item
16463 )
16464 }
16465 }
16466 }
16467}
16468impl LightClientMessageUnion {
16469 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16470 match self {
16471 LightClientMessageUnion::GetLastState(ref item) => write!(f, "{}", item),
16472 LightClientMessageUnion::SendLastState(ref item) => write!(f, "{}", item),
16473 LightClientMessageUnion::GetLastStateProof(ref item) => write!(f, "{}", item),
16474 LightClientMessageUnion::SendLastStateProof(ref item) => write!(f, "{}", item),
16475 LightClientMessageUnion::GetBlocksProof(ref item) => write!(f, "{}", item),
16476 LightClientMessageUnion::SendBlocksProof(ref item) => write!(f, "{}", item),
16477 LightClientMessageUnion::GetTransactionsProof(ref item) => write!(f, "{}", item),
16478 LightClientMessageUnion::SendTransactionsProof(ref item) => write!(f, "{}", item),
16479 }
16480 }
16481}
16482impl<'r> LightClientMessageUnionReader<'r> {
16483 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16484 match self {
16485 LightClientMessageUnionReader::GetLastState(ref item) => write!(f, "{}", item),
16486 LightClientMessageUnionReader::SendLastState(ref item) => write!(f, "{}", item),
16487 LightClientMessageUnionReader::GetLastStateProof(ref item) => write!(f, "{}", item),
16488 LightClientMessageUnionReader::SendLastStateProof(ref item) => write!(f, "{}", item),
16489 LightClientMessageUnionReader::GetBlocksProof(ref item) => write!(f, "{}", item),
16490 LightClientMessageUnionReader::SendBlocksProof(ref item) => write!(f, "{}", item),
16491 LightClientMessageUnionReader::GetTransactionsProof(ref item) => write!(f, "{}", item),
16492 LightClientMessageUnionReader::SendTransactionsProof(ref item) => write!(f, "{}", item),
16493 }
16494 }
16495}
16496impl ::core::convert::From<GetLastState> for LightClientMessageUnion {
16497 fn from(item: GetLastState) -> Self {
16498 LightClientMessageUnion::GetLastState(item)
16499 }
16500}
16501impl ::core::convert::From<SendLastState> for LightClientMessageUnion {
16502 fn from(item: SendLastState) -> Self {
16503 LightClientMessageUnion::SendLastState(item)
16504 }
16505}
16506impl ::core::convert::From<GetLastStateProof> for LightClientMessageUnion {
16507 fn from(item: GetLastStateProof) -> Self {
16508 LightClientMessageUnion::GetLastStateProof(item)
16509 }
16510}
16511impl ::core::convert::From<SendLastStateProof> for LightClientMessageUnion {
16512 fn from(item: SendLastStateProof) -> Self {
16513 LightClientMessageUnion::SendLastStateProof(item)
16514 }
16515}
16516impl ::core::convert::From<GetBlocksProof> for LightClientMessageUnion {
16517 fn from(item: GetBlocksProof) -> Self {
16518 LightClientMessageUnion::GetBlocksProof(item)
16519 }
16520}
16521impl ::core::convert::From<SendBlocksProof> for LightClientMessageUnion {
16522 fn from(item: SendBlocksProof) -> Self {
16523 LightClientMessageUnion::SendBlocksProof(item)
16524 }
16525}
16526impl ::core::convert::From<GetTransactionsProof> for LightClientMessageUnion {
16527 fn from(item: GetTransactionsProof) -> Self {
16528 LightClientMessageUnion::GetTransactionsProof(item)
16529 }
16530}
16531impl ::core::convert::From<SendTransactionsProof> for LightClientMessageUnion {
16532 fn from(item: SendTransactionsProof) -> Self {
16533 LightClientMessageUnion::SendTransactionsProof(item)
16534 }
16535}
16536impl<'r> ::core::convert::From<GetLastStateReader<'r>> for LightClientMessageUnionReader<'r> {
16537 fn from(item: GetLastStateReader<'r>) -> Self {
16538 LightClientMessageUnionReader::GetLastState(item)
16539 }
16540}
16541impl<'r> ::core::convert::From<SendLastStateReader<'r>> for LightClientMessageUnionReader<'r> {
16542 fn from(item: SendLastStateReader<'r>) -> Self {
16543 LightClientMessageUnionReader::SendLastState(item)
16544 }
16545}
16546impl<'r> ::core::convert::From<GetLastStateProofReader<'r>> for LightClientMessageUnionReader<'r> {
16547 fn from(item: GetLastStateProofReader<'r>) -> Self {
16548 LightClientMessageUnionReader::GetLastStateProof(item)
16549 }
16550}
16551impl<'r> ::core::convert::From<SendLastStateProofReader<'r>> for LightClientMessageUnionReader<'r> {
16552 fn from(item: SendLastStateProofReader<'r>) -> Self {
16553 LightClientMessageUnionReader::SendLastStateProof(item)
16554 }
16555}
16556impl<'r> ::core::convert::From<GetBlocksProofReader<'r>> for LightClientMessageUnionReader<'r> {
16557 fn from(item: GetBlocksProofReader<'r>) -> Self {
16558 LightClientMessageUnionReader::GetBlocksProof(item)
16559 }
16560}
16561impl<'r> ::core::convert::From<SendBlocksProofReader<'r>> for LightClientMessageUnionReader<'r> {
16562 fn from(item: SendBlocksProofReader<'r>) -> Self {
16563 LightClientMessageUnionReader::SendBlocksProof(item)
16564 }
16565}
16566impl<'r> ::core::convert::From<GetTransactionsProofReader<'r>>
16567 for LightClientMessageUnionReader<'r>
16568{
16569 fn from(item: GetTransactionsProofReader<'r>) -> Self {
16570 LightClientMessageUnionReader::GetTransactionsProof(item)
16571 }
16572}
16573impl<'r> ::core::convert::From<SendTransactionsProofReader<'r>>
16574 for LightClientMessageUnionReader<'r>
16575{
16576 fn from(item: SendTransactionsProofReader<'r>) -> Self {
16577 LightClientMessageUnionReader::SendTransactionsProof(item)
16578 }
16579}
16580impl LightClientMessageUnion {
16581 pub const NAME: &'static str = "LightClientMessageUnion";
16582 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
16583 match self {
16584 LightClientMessageUnion::GetLastState(item) => item.as_bytes(),
16585 LightClientMessageUnion::SendLastState(item) => item.as_bytes(),
16586 LightClientMessageUnion::GetLastStateProof(item) => item.as_bytes(),
16587 LightClientMessageUnion::SendLastStateProof(item) => item.as_bytes(),
16588 LightClientMessageUnion::GetBlocksProof(item) => item.as_bytes(),
16589 LightClientMessageUnion::SendBlocksProof(item) => item.as_bytes(),
16590 LightClientMessageUnion::GetTransactionsProof(item) => item.as_bytes(),
16591 LightClientMessageUnion::SendTransactionsProof(item) => item.as_bytes(),
16592 }
16593 }
16594 pub fn as_slice(&self) -> &[u8] {
16595 match self {
16596 LightClientMessageUnion::GetLastState(item) => item.as_slice(),
16597 LightClientMessageUnion::SendLastState(item) => item.as_slice(),
16598 LightClientMessageUnion::GetLastStateProof(item) => item.as_slice(),
16599 LightClientMessageUnion::SendLastStateProof(item) => item.as_slice(),
16600 LightClientMessageUnion::GetBlocksProof(item) => item.as_slice(),
16601 LightClientMessageUnion::SendBlocksProof(item) => item.as_slice(),
16602 LightClientMessageUnion::GetTransactionsProof(item) => item.as_slice(),
16603 LightClientMessageUnion::SendTransactionsProof(item) => item.as_slice(),
16604 }
16605 }
16606 pub fn item_id(&self) -> molecule::Number {
16607 match self {
16608 LightClientMessageUnion::GetLastState(_) => 0,
16609 LightClientMessageUnion::SendLastState(_) => 1,
16610 LightClientMessageUnion::GetLastStateProof(_) => 2,
16611 LightClientMessageUnion::SendLastStateProof(_) => 3,
16612 LightClientMessageUnion::GetBlocksProof(_) => 4,
16613 LightClientMessageUnion::SendBlocksProof(_) => 5,
16614 LightClientMessageUnion::GetTransactionsProof(_) => 6,
16615 LightClientMessageUnion::SendTransactionsProof(_) => 7,
16616 }
16617 }
16618 pub fn item_name(&self) -> &str {
16619 match self {
16620 LightClientMessageUnion::GetLastState(_) => "GetLastState",
16621 LightClientMessageUnion::SendLastState(_) => "SendLastState",
16622 LightClientMessageUnion::GetLastStateProof(_) => "GetLastStateProof",
16623 LightClientMessageUnion::SendLastStateProof(_) => "SendLastStateProof",
16624 LightClientMessageUnion::GetBlocksProof(_) => "GetBlocksProof",
16625 LightClientMessageUnion::SendBlocksProof(_) => "SendBlocksProof",
16626 LightClientMessageUnion::GetTransactionsProof(_) => "GetTransactionsProof",
16627 LightClientMessageUnion::SendTransactionsProof(_) => "SendTransactionsProof",
16628 }
16629 }
16630 pub fn as_reader<'r>(&'r self) -> LightClientMessageUnionReader<'r> {
16631 match self {
16632 LightClientMessageUnion::GetLastState(item) => item.as_reader().into(),
16633 LightClientMessageUnion::SendLastState(item) => item.as_reader().into(),
16634 LightClientMessageUnion::GetLastStateProof(item) => item.as_reader().into(),
16635 LightClientMessageUnion::SendLastStateProof(item) => item.as_reader().into(),
16636 LightClientMessageUnion::GetBlocksProof(item) => item.as_reader().into(),
16637 LightClientMessageUnion::SendBlocksProof(item) => item.as_reader().into(),
16638 LightClientMessageUnion::GetTransactionsProof(item) => item.as_reader().into(),
16639 LightClientMessageUnion::SendTransactionsProof(item) => item.as_reader().into(),
16640 }
16641 }
16642}
16643impl<'r> LightClientMessageUnionReader<'r> {
16644 pub const NAME: &'r str = "LightClientMessageUnionReader";
16645 pub fn as_slice(&self) -> &'r [u8] {
16646 match self {
16647 LightClientMessageUnionReader::GetLastState(item) => item.as_slice(),
16648 LightClientMessageUnionReader::SendLastState(item) => item.as_slice(),
16649 LightClientMessageUnionReader::GetLastStateProof(item) => item.as_slice(),
16650 LightClientMessageUnionReader::SendLastStateProof(item) => item.as_slice(),
16651 LightClientMessageUnionReader::GetBlocksProof(item) => item.as_slice(),
16652 LightClientMessageUnionReader::SendBlocksProof(item) => item.as_slice(),
16653 LightClientMessageUnionReader::GetTransactionsProof(item) => item.as_slice(),
16654 LightClientMessageUnionReader::SendTransactionsProof(item) => item.as_slice(),
16655 }
16656 }
16657 pub fn item_id(&self) -> molecule::Number {
16658 match self {
16659 LightClientMessageUnionReader::GetLastState(_) => 0,
16660 LightClientMessageUnionReader::SendLastState(_) => 1,
16661 LightClientMessageUnionReader::GetLastStateProof(_) => 2,
16662 LightClientMessageUnionReader::SendLastStateProof(_) => 3,
16663 LightClientMessageUnionReader::GetBlocksProof(_) => 4,
16664 LightClientMessageUnionReader::SendBlocksProof(_) => 5,
16665 LightClientMessageUnionReader::GetTransactionsProof(_) => 6,
16666 LightClientMessageUnionReader::SendTransactionsProof(_) => 7,
16667 }
16668 }
16669 pub fn item_name(&self) -> &str {
16670 match self {
16671 LightClientMessageUnionReader::GetLastState(_) => "GetLastState",
16672 LightClientMessageUnionReader::SendLastState(_) => "SendLastState",
16673 LightClientMessageUnionReader::GetLastStateProof(_) => "GetLastStateProof",
16674 LightClientMessageUnionReader::SendLastStateProof(_) => "SendLastStateProof",
16675 LightClientMessageUnionReader::GetBlocksProof(_) => "GetBlocksProof",
16676 LightClientMessageUnionReader::SendBlocksProof(_) => "SendBlocksProof",
16677 LightClientMessageUnionReader::GetTransactionsProof(_) => "GetTransactionsProof",
16678 LightClientMessageUnionReader::SendTransactionsProof(_) => "SendTransactionsProof",
16679 }
16680 }
16681}
16682impl From<GetLastState> for LightClientMessage {
16683 fn from(value: GetLastState) -> Self {
16684 Self::new_builder().set(value).build()
16685 }
16686}
16687impl From<SendLastState> for LightClientMessage {
16688 fn from(value: SendLastState) -> Self {
16689 Self::new_builder().set(value).build()
16690 }
16691}
16692impl From<GetLastStateProof> for LightClientMessage {
16693 fn from(value: GetLastStateProof) -> Self {
16694 Self::new_builder().set(value).build()
16695 }
16696}
16697impl From<SendLastStateProof> for LightClientMessage {
16698 fn from(value: SendLastStateProof) -> Self {
16699 Self::new_builder().set(value).build()
16700 }
16701}
16702impl From<GetBlocksProof> for LightClientMessage {
16703 fn from(value: GetBlocksProof) -> Self {
16704 Self::new_builder().set(value).build()
16705 }
16706}
16707impl From<SendBlocksProof> for LightClientMessage {
16708 fn from(value: SendBlocksProof) -> Self {
16709 Self::new_builder().set(value).build()
16710 }
16711}
16712impl From<GetTransactionsProof> for LightClientMessage {
16713 fn from(value: GetTransactionsProof) -> Self {
16714 Self::new_builder().set(value).build()
16715 }
16716}
16717impl From<SendTransactionsProof> for LightClientMessage {
16718 fn from(value: SendTransactionsProof) -> Self {
16719 Self::new_builder().set(value).build()
16720 }
16721}
16722#[derive(Clone)]
16723pub struct GetLastState(molecule::bytes::Bytes);
16724impl ::core::fmt::LowerHex for GetLastState {
16725 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16726 use molecule::hex_string;
16727 if f.alternate() {
16728 write!(f, "0x")?;
16729 }
16730 write!(f, "{}", hex_string(self.as_slice()))
16731 }
16732}
16733impl ::core::fmt::Debug for GetLastState {
16734 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16735 write!(f, "{}({:#x})", Self::NAME, self)
16736 }
16737}
16738impl ::core::fmt::Display for GetLastState {
16739 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16740 write!(f, "{} {{ ", Self::NAME)?;
16741 write!(f, "{}: {}", "subscribe", self.subscribe())?;
16742 let extra_count = self.count_extra_fields();
16743 if extra_count != 0 {
16744 write!(f, ", .. ({} fields)", extra_count)?;
16745 }
16746 write!(f, " }}")
16747 }
16748}
16749impl ::core::default::Default for GetLastState {
16750 fn default() -> Self {
16751 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
16752 GetLastState::new_unchecked(v)
16753 }
16754}
16755impl GetLastState {
16756 const DEFAULT_VALUE: [u8; 9] = [9, 0, 0, 0, 8, 0, 0, 0, 0];
16757 pub const FIELD_COUNT: usize = 1;
16758 pub fn total_size(&self) -> usize {
16759 molecule::unpack_number(self.as_slice()) as usize
16760 }
16761 pub fn field_count(&self) -> usize {
16762 if self.total_size() == molecule::NUMBER_SIZE {
16763 0
16764 } else {
16765 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
16766 }
16767 }
16768 pub fn count_extra_fields(&self) -> usize {
16769 self.field_count() - Self::FIELD_COUNT
16770 }
16771 pub fn has_extra_fields(&self) -> bool {
16772 Self::FIELD_COUNT != self.field_count()
16773 }
16774 pub fn subscribe(&self) -> Bool {
16775 let slice = self.as_slice();
16776 let start = molecule::unpack_number(&slice[4..]) as usize;
16777 if self.has_extra_fields() {
16778 let end = molecule::unpack_number(&slice[8..]) as usize;
16779 Bool::new_unchecked(self.0.slice(start..end))
16780 } else {
16781 Bool::new_unchecked(self.0.slice(start..))
16782 }
16783 }
16784 pub fn as_reader<'r>(&'r self) -> GetLastStateReader<'r> {
16785 GetLastStateReader::new_unchecked(self.as_slice())
16786 }
16787}
16788impl molecule::prelude::Entity for GetLastState {
16789 type Builder = GetLastStateBuilder;
16790 const NAME: &'static str = "GetLastState";
16791 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
16792 GetLastState(data)
16793 }
16794 fn as_bytes(&self) -> molecule::bytes::Bytes {
16795 self.0.clone()
16796 }
16797 fn as_slice(&self) -> &[u8] {
16798 &self.0[..]
16799 }
16800 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
16801 GetLastStateReader::from_slice(slice).map(|reader| reader.to_entity())
16802 }
16803 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
16804 GetLastStateReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
16805 }
16806 fn new_builder() -> Self::Builder {
16807 ::core::default::Default::default()
16808 }
16809 fn as_builder(self) -> Self::Builder {
16810 Self::new_builder().subscribe(self.subscribe())
16811 }
16812}
16813#[derive(Clone, Copy)]
16814pub struct GetLastStateReader<'r>(&'r [u8]);
16815impl<'r> ::core::fmt::LowerHex for GetLastStateReader<'r> {
16816 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16817 use molecule::hex_string;
16818 if f.alternate() {
16819 write!(f, "0x")?;
16820 }
16821 write!(f, "{}", hex_string(self.as_slice()))
16822 }
16823}
16824impl<'r> ::core::fmt::Debug for GetLastStateReader<'r> {
16825 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16826 write!(f, "{}({:#x})", Self::NAME, self)
16827 }
16828}
16829impl<'r> ::core::fmt::Display for GetLastStateReader<'r> {
16830 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16831 write!(f, "{} {{ ", Self::NAME)?;
16832 write!(f, "{}: {}", "subscribe", self.subscribe())?;
16833 let extra_count = self.count_extra_fields();
16834 if extra_count != 0 {
16835 write!(f, ", .. ({} fields)", extra_count)?;
16836 }
16837 write!(f, " }}")
16838 }
16839}
16840impl<'r> GetLastStateReader<'r> {
16841 pub const FIELD_COUNT: usize = 1;
16842 pub fn total_size(&self) -> usize {
16843 molecule::unpack_number(self.as_slice()) as usize
16844 }
16845 pub fn field_count(&self) -> usize {
16846 if self.total_size() == molecule::NUMBER_SIZE {
16847 0
16848 } else {
16849 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
16850 }
16851 }
16852 pub fn count_extra_fields(&self) -> usize {
16853 self.field_count() - Self::FIELD_COUNT
16854 }
16855 pub fn has_extra_fields(&self) -> bool {
16856 Self::FIELD_COUNT != self.field_count()
16857 }
16858 pub fn subscribe(&self) -> BoolReader<'r> {
16859 let slice = self.as_slice();
16860 let start = molecule::unpack_number(&slice[4..]) as usize;
16861 if self.has_extra_fields() {
16862 let end = molecule::unpack_number(&slice[8..]) as usize;
16863 BoolReader::new_unchecked(&self.as_slice()[start..end])
16864 } else {
16865 BoolReader::new_unchecked(&self.as_slice()[start..])
16866 }
16867 }
16868}
16869impl<'r> molecule::prelude::Reader<'r> for GetLastStateReader<'r> {
16870 type Entity = GetLastState;
16871 const NAME: &'static str = "GetLastStateReader";
16872 fn to_entity(&self) -> Self::Entity {
16873 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
16874 }
16875 fn new_unchecked(slice: &'r [u8]) -> Self {
16876 GetLastStateReader(slice)
16877 }
16878 fn as_slice(&self) -> &'r [u8] {
16879 self.0
16880 }
16881 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
16882 use molecule::verification_error as ve;
16883 let slice_len = slice.len();
16884 if slice_len < molecule::NUMBER_SIZE {
16885 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
16886 }
16887 let total_size = molecule::unpack_number(slice) as usize;
16888 if slice_len != total_size {
16889 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
16890 }
16891 if slice_len < molecule::NUMBER_SIZE * 2 {
16892 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
16893 }
16894 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
16895 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
16896 return ve!(Self, OffsetsNotMatch);
16897 }
16898 if slice_len < offset_first {
16899 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
16900 }
16901 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
16902 if field_count < Self::FIELD_COUNT {
16903 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
16904 } else if !compatible && field_count > Self::FIELD_COUNT {
16905 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
16906 };
16907 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
16908 .chunks_exact(molecule::NUMBER_SIZE)
16909 .map(|x| molecule::unpack_number(x) as usize)
16910 .collect();
16911 offsets.push(total_size);
16912 if offsets.windows(2).any(|i| i[0] > i[1]) {
16913 return ve!(Self, OffsetsNotMatch);
16914 }
16915 BoolReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
16916 Ok(())
16917 }
16918}
16919#[derive(Clone, Debug, Default)]
16920pub struct GetLastStateBuilder {
16921 pub(crate) subscribe: Bool,
16922}
16923impl GetLastStateBuilder {
16924 pub const FIELD_COUNT: usize = 1;
16925 pub fn subscribe<T>(mut self, v: T) -> Self
16926 where
16927 T: ::core::convert::Into<Bool>,
16928 {
16929 self.subscribe = v.into();
16930 self
16931 }
16932}
16933impl molecule::prelude::Builder for GetLastStateBuilder {
16934 type Entity = GetLastState;
16935 const NAME: &'static str = "GetLastStateBuilder";
16936 fn expected_length(&self) -> usize {
16937 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.subscribe.as_slice().len()
16938 }
16939 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
16940 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
16941 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
16942 offsets.push(total_size);
16943 total_size += self.subscribe.as_slice().len();
16944 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
16945 for offset in offsets.into_iter() {
16946 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
16947 }
16948 writer.write_all(self.subscribe.as_slice())?;
16949 Ok(())
16950 }
16951 fn build(&self) -> Self::Entity {
16952 let mut inner = Vec::with_capacity(self.expected_length());
16953 self.write(&mut inner)
16954 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
16955 GetLastState::new_unchecked(inner.into())
16956 }
16957}
16958#[derive(Clone)]
16959pub struct SendLastState(molecule::bytes::Bytes);
16960impl ::core::fmt::LowerHex for SendLastState {
16961 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16962 use molecule::hex_string;
16963 if f.alternate() {
16964 write!(f, "0x")?;
16965 }
16966 write!(f, "{}", hex_string(self.as_slice()))
16967 }
16968}
16969impl ::core::fmt::Debug for SendLastState {
16970 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16971 write!(f, "{}({:#x})", Self::NAME, self)
16972 }
16973}
16974impl ::core::fmt::Display for SendLastState {
16975 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16976 write!(f, "{} {{ ", Self::NAME)?;
16977 write!(f, "{}: {}", "last_header", self.last_header())?;
16978 let extra_count = self.count_extra_fields();
16979 if extra_count != 0 {
16980 write!(f, ", .. ({} fields)", extra_count)?;
16981 }
16982 write!(f, " }}")
16983 }
16984}
16985impl ::core::default::Default for SendLastState {
16986 fn default() -> Self {
16987 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
16988 SendLastState::new_unchecked(v)
16989 }
16990}
16991impl SendLastState {
16992 const DEFAULT_VALUE: [u8; 388] = [
16993 132, 1, 0, 0, 8, 0, 0, 0, 124, 1, 0, 0, 20, 0, 0, 0, 228, 0, 0, 0, 4, 1, 0, 0, 4, 1, 0, 0,
16994 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16995 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16996 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16997 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16998 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16999 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17000 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17001 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17002 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17003 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17004 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17005 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17006 ];
17007 pub const FIELD_COUNT: usize = 1;
17008 pub fn total_size(&self) -> usize {
17009 molecule::unpack_number(self.as_slice()) as usize
17010 }
17011 pub fn field_count(&self) -> usize {
17012 if self.total_size() == molecule::NUMBER_SIZE {
17013 0
17014 } else {
17015 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17016 }
17017 }
17018 pub fn count_extra_fields(&self) -> usize {
17019 self.field_count() - Self::FIELD_COUNT
17020 }
17021 pub fn has_extra_fields(&self) -> bool {
17022 Self::FIELD_COUNT != self.field_count()
17023 }
17024 pub fn last_header(&self) -> VerifiableHeader {
17025 let slice = self.as_slice();
17026 let start = molecule::unpack_number(&slice[4..]) as usize;
17027 if self.has_extra_fields() {
17028 let end = molecule::unpack_number(&slice[8..]) as usize;
17029 VerifiableHeader::new_unchecked(self.0.slice(start..end))
17030 } else {
17031 VerifiableHeader::new_unchecked(self.0.slice(start..))
17032 }
17033 }
17034 pub fn as_reader<'r>(&'r self) -> SendLastStateReader<'r> {
17035 SendLastStateReader::new_unchecked(self.as_slice())
17036 }
17037}
17038impl molecule::prelude::Entity for SendLastState {
17039 type Builder = SendLastStateBuilder;
17040 const NAME: &'static str = "SendLastState";
17041 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
17042 SendLastState(data)
17043 }
17044 fn as_bytes(&self) -> molecule::bytes::Bytes {
17045 self.0.clone()
17046 }
17047 fn as_slice(&self) -> &[u8] {
17048 &self.0[..]
17049 }
17050 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17051 SendLastStateReader::from_slice(slice).map(|reader| reader.to_entity())
17052 }
17053 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17054 SendLastStateReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
17055 }
17056 fn new_builder() -> Self::Builder {
17057 ::core::default::Default::default()
17058 }
17059 fn as_builder(self) -> Self::Builder {
17060 Self::new_builder().last_header(self.last_header())
17061 }
17062}
17063#[derive(Clone, Copy)]
17064pub struct SendLastStateReader<'r>(&'r [u8]);
17065impl<'r> ::core::fmt::LowerHex for SendLastStateReader<'r> {
17066 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17067 use molecule::hex_string;
17068 if f.alternate() {
17069 write!(f, "0x")?;
17070 }
17071 write!(f, "{}", hex_string(self.as_slice()))
17072 }
17073}
17074impl<'r> ::core::fmt::Debug for SendLastStateReader<'r> {
17075 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17076 write!(f, "{}({:#x})", Self::NAME, self)
17077 }
17078}
17079impl<'r> ::core::fmt::Display for SendLastStateReader<'r> {
17080 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17081 write!(f, "{} {{ ", Self::NAME)?;
17082 write!(f, "{}: {}", "last_header", self.last_header())?;
17083 let extra_count = self.count_extra_fields();
17084 if extra_count != 0 {
17085 write!(f, ", .. ({} fields)", extra_count)?;
17086 }
17087 write!(f, " }}")
17088 }
17089}
17090impl<'r> SendLastStateReader<'r> {
17091 pub const FIELD_COUNT: usize = 1;
17092 pub fn total_size(&self) -> usize {
17093 molecule::unpack_number(self.as_slice()) as usize
17094 }
17095 pub fn field_count(&self) -> usize {
17096 if self.total_size() == molecule::NUMBER_SIZE {
17097 0
17098 } else {
17099 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17100 }
17101 }
17102 pub fn count_extra_fields(&self) -> usize {
17103 self.field_count() - Self::FIELD_COUNT
17104 }
17105 pub fn has_extra_fields(&self) -> bool {
17106 Self::FIELD_COUNT != self.field_count()
17107 }
17108 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
17109 let slice = self.as_slice();
17110 let start = molecule::unpack_number(&slice[4..]) as usize;
17111 if self.has_extra_fields() {
17112 let end = molecule::unpack_number(&slice[8..]) as usize;
17113 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
17114 } else {
17115 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..])
17116 }
17117 }
17118}
17119impl<'r> molecule::prelude::Reader<'r> for SendLastStateReader<'r> {
17120 type Entity = SendLastState;
17121 const NAME: &'static str = "SendLastStateReader";
17122 fn to_entity(&self) -> Self::Entity {
17123 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
17124 }
17125 fn new_unchecked(slice: &'r [u8]) -> Self {
17126 SendLastStateReader(slice)
17127 }
17128 fn as_slice(&self) -> &'r [u8] {
17129 self.0
17130 }
17131 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
17132 use molecule::verification_error as ve;
17133 let slice_len = slice.len();
17134 if slice_len < molecule::NUMBER_SIZE {
17135 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
17136 }
17137 let total_size = molecule::unpack_number(slice) as usize;
17138 if slice_len != total_size {
17139 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
17140 }
17141 if slice_len < molecule::NUMBER_SIZE * 2 {
17142 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
17143 }
17144 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
17145 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
17146 return ve!(Self, OffsetsNotMatch);
17147 }
17148 if slice_len < offset_first {
17149 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
17150 }
17151 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
17152 if field_count < Self::FIELD_COUNT {
17153 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17154 } else if !compatible && field_count > Self::FIELD_COUNT {
17155 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17156 };
17157 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
17158 .chunks_exact(molecule::NUMBER_SIZE)
17159 .map(|x| molecule::unpack_number(x) as usize)
17160 .collect();
17161 offsets.push(total_size);
17162 if offsets.windows(2).any(|i| i[0] > i[1]) {
17163 return ve!(Self, OffsetsNotMatch);
17164 }
17165 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
17166 Ok(())
17167 }
17168}
17169#[derive(Clone, Debug, Default)]
17170pub struct SendLastStateBuilder {
17171 pub(crate) last_header: VerifiableHeader,
17172}
17173impl SendLastStateBuilder {
17174 pub const FIELD_COUNT: usize = 1;
17175 pub fn last_header<T>(mut self, v: T) -> Self
17176 where
17177 T: ::core::convert::Into<VerifiableHeader>,
17178 {
17179 self.last_header = v.into();
17180 self
17181 }
17182}
17183impl molecule::prelude::Builder for SendLastStateBuilder {
17184 type Entity = SendLastState;
17185 const NAME: &'static str = "SendLastStateBuilder";
17186 fn expected_length(&self) -> usize {
17187 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.last_header.as_slice().len()
17188 }
17189 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
17190 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
17191 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
17192 offsets.push(total_size);
17193 total_size += self.last_header.as_slice().len();
17194 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
17195 for offset in offsets.into_iter() {
17196 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
17197 }
17198 writer.write_all(self.last_header.as_slice())?;
17199 Ok(())
17200 }
17201 fn build(&self) -> Self::Entity {
17202 let mut inner = Vec::with_capacity(self.expected_length());
17203 self.write(&mut inner)
17204 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
17205 SendLastState::new_unchecked(inner.into())
17206 }
17207}
17208#[derive(Clone)]
17209pub struct GetLastStateProof(molecule::bytes::Bytes);
17210impl ::core::fmt::LowerHex for GetLastStateProof {
17211 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17212 use molecule::hex_string;
17213 if f.alternate() {
17214 write!(f, "0x")?;
17215 }
17216 write!(f, "{}", hex_string(self.as_slice()))
17217 }
17218}
17219impl ::core::fmt::Debug for GetLastStateProof {
17220 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17221 write!(f, "{}({:#x})", Self::NAME, self)
17222 }
17223}
17224impl ::core::fmt::Display for GetLastStateProof {
17225 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17226 write!(f, "{} {{ ", Self::NAME)?;
17227 write!(f, "{}: {}", "last_hash", self.last_hash())?;
17228 write!(f, ", {}: {}", "start_hash", self.start_hash())?;
17229 write!(f, ", {}: {}", "start_number", self.start_number())?;
17230 write!(f, ", {}: {}", "last_n_blocks", self.last_n_blocks())?;
17231 write!(
17232 f,
17233 ", {}: {}",
17234 "difficulty_boundary",
17235 self.difficulty_boundary()
17236 )?;
17237 write!(f, ", {}: {}", "difficulties", self.difficulties())?;
17238 let extra_count = self.count_extra_fields();
17239 if extra_count != 0 {
17240 write!(f, ", .. ({} fields)", extra_count)?;
17241 }
17242 write!(f, " }}")
17243 }
17244}
17245impl ::core::default::Default for GetLastStateProof {
17246 fn default() -> Self {
17247 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
17248 GetLastStateProof::new_unchecked(v)
17249 }
17250}
17251impl GetLastStateProof {
17252 const DEFAULT_VALUE: [u8; 144] = [
17253 144, 0, 0, 0, 28, 0, 0, 0, 60, 0, 0, 0, 92, 0, 0, 0, 100, 0, 0, 0, 108, 0, 0, 0, 140, 0, 0,
17254 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17255 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17258 ];
17259 pub const FIELD_COUNT: usize = 6;
17260 pub fn total_size(&self) -> usize {
17261 molecule::unpack_number(self.as_slice()) as usize
17262 }
17263 pub fn field_count(&self) -> usize {
17264 if self.total_size() == molecule::NUMBER_SIZE {
17265 0
17266 } else {
17267 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17268 }
17269 }
17270 pub fn count_extra_fields(&self) -> usize {
17271 self.field_count() - Self::FIELD_COUNT
17272 }
17273 pub fn has_extra_fields(&self) -> bool {
17274 Self::FIELD_COUNT != self.field_count()
17275 }
17276 pub fn last_hash(&self) -> Byte32 {
17277 let slice = self.as_slice();
17278 let start = molecule::unpack_number(&slice[4..]) as usize;
17279 let end = molecule::unpack_number(&slice[8..]) as usize;
17280 Byte32::new_unchecked(self.0.slice(start..end))
17281 }
17282 pub fn start_hash(&self) -> Byte32 {
17283 let slice = self.as_slice();
17284 let start = molecule::unpack_number(&slice[8..]) as usize;
17285 let end = molecule::unpack_number(&slice[12..]) as usize;
17286 Byte32::new_unchecked(self.0.slice(start..end))
17287 }
17288 pub fn start_number(&self) -> Uint64 {
17289 let slice = self.as_slice();
17290 let start = molecule::unpack_number(&slice[12..]) as usize;
17291 let end = molecule::unpack_number(&slice[16..]) as usize;
17292 Uint64::new_unchecked(self.0.slice(start..end))
17293 }
17294 pub fn last_n_blocks(&self) -> Uint64 {
17295 let slice = self.as_slice();
17296 let start = molecule::unpack_number(&slice[16..]) as usize;
17297 let end = molecule::unpack_number(&slice[20..]) as usize;
17298 Uint64::new_unchecked(self.0.slice(start..end))
17299 }
17300 pub fn difficulty_boundary(&self) -> Uint256 {
17301 let slice = self.as_slice();
17302 let start = molecule::unpack_number(&slice[20..]) as usize;
17303 let end = molecule::unpack_number(&slice[24..]) as usize;
17304 Uint256::new_unchecked(self.0.slice(start..end))
17305 }
17306 pub fn difficulties(&self) -> Uint256Vec {
17307 let slice = self.as_slice();
17308 let start = molecule::unpack_number(&slice[24..]) as usize;
17309 if self.has_extra_fields() {
17310 let end = molecule::unpack_number(&slice[28..]) as usize;
17311 Uint256Vec::new_unchecked(self.0.slice(start..end))
17312 } else {
17313 Uint256Vec::new_unchecked(self.0.slice(start..))
17314 }
17315 }
17316 pub fn as_reader<'r>(&'r self) -> GetLastStateProofReader<'r> {
17317 GetLastStateProofReader::new_unchecked(self.as_slice())
17318 }
17319}
17320impl molecule::prelude::Entity for GetLastStateProof {
17321 type Builder = GetLastStateProofBuilder;
17322 const NAME: &'static str = "GetLastStateProof";
17323 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
17324 GetLastStateProof(data)
17325 }
17326 fn as_bytes(&self) -> molecule::bytes::Bytes {
17327 self.0.clone()
17328 }
17329 fn as_slice(&self) -> &[u8] {
17330 &self.0[..]
17331 }
17332 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17333 GetLastStateProofReader::from_slice(slice).map(|reader| reader.to_entity())
17334 }
17335 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17336 GetLastStateProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
17337 }
17338 fn new_builder() -> Self::Builder {
17339 ::core::default::Default::default()
17340 }
17341 fn as_builder(self) -> Self::Builder {
17342 Self::new_builder()
17343 .last_hash(self.last_hash())
17344 .start_hash(self.start_hash())
17345 .start_number(self.start_number())
17346 .last_n_blocks(self.last_n_blocks())
17347 .difficulty_boundary(self.difficulty_boundary())
17348 .difficulties(self.difficulties())
17349 }
17350}
17351#[derive(Clone, Copy)]
17352pub struct GetLastStateProofReader<'r>(&'r [u8]);
17353impl<'r> ::core::fmt::LowerHex for GetLastStateProofReader<'r> {
17354 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17355 use molecule::hex_string;
17356 if f.alternate() {
17357 write!(f, "0x")?;
17358 }
17359 write!(f, "{}", hex_string(self.as_slice()))
17360 }
17361}
17362impl<'r> ::core::fmt::Debug for GetLastStateProofReader<'r> {
17363 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17364 write!(f, "{}({:#x})", Self::NAME, self)
17365 }
17366}
17367impl<'r> ::core::fmt::Display for GetLastStateProofReader<'r> {
17368 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17369 write!(f, "{} {{ ", Self::NAME)?;
17370 write!(f, "{}: {}", "last_hash", self.last_hash())?;
17371 write!(f, ", {}: {}", "start_hash", self.start_hash())?;
17372 write!(f, ", {}: {}", "start_number", self.start_number())?;
17373 write!(f, ", {}: {}", "last_n_blocks", self.last_n_blocks())?;
17374 write!(
17375 f,
17376 ", {}: {}",
17377 "difficulty_boundary",
17378 self.difficulty_boundary()
17379 )?;
17380 write!(f, ", {}: {}", "difficulties", self.difficulties())?;
17381 let extra_count = self.count_extra_fields();
17382 if extra_count != 0 {
17383 write!(f, ", .. ({} fields)", extra_count)?;
17384 }
17385 write!(f, " }}")
17386 }
17387}
17388impl<'r> GetLastStateProofReader<'r> {
17389 pub const FIELD_COUNT: usize = 6;
17390 pub fn total_size(&self) -> usize {
17391 molecule::unpack_number(self.as_slice()) as usize
17392 }
17393 pub fn field_count(&self) -> usize {
17394 if self.total_size() == molecule::NUMBER_SIZE {
17395 0
17396 } else {
17397 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17398 }
17399 }
17400 pub fn count_extra_fields(&self) -> usize {
17401 self.field_count() - Self::FIELD_COUNT
17402 }
17403 pub fn has_extra_fields(&self) -> bool {
17404 Self::FIELD_COUNT != self.field_count()
17405 }
17406 pub fn last_hash(&self) -> Byte32Reader<'r> {
17407 let slice = self.as_slice();
17408 let start = molecule::unpack_number(&slice[4..]) as usize;
17409 let end = molecule::unpack_number(&slice[8..]) as usize;
17410 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
17411 }
17412 pub fn start_hash(&self) -> Byte32Reader<'r> {
17413 let slice = self.as_slice();
17414 let start = molecule::unpack_number(&slice[8..]) as usize;
17415 let end = molecule::unpack_number(&slice[12..]) as usize;
17416 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
17417 }
17418 pub fn start_number(&self) -> Uint64Reader<'r> {
17419 let slice = self.as_slice();
17420 let start = molecule::unpack_number(&slice[12..]) as usize;
17421 let end = molecule::unpack_number(&slice[16..]) as usize;
17422 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
17423 }
17424 pub fn last_n_blocks(&self) -> Uint64Reader<'r> {
17425 let slice = self.as_slice();
17426 let start = molecule::unpack_number(&slice[16..]) as usize;
17427 let end = molecule::unpack_number(&slice[20..]) as usize;
17428 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
17429 }
17430 pub fn difficulty_boundary(&self) -> Uint256Reader<'r> {
17431 let slice = self.as_slice();
17432 let start = molecule::unpack_number(&slice[20..]) as usize;
17433 let end = molecule::unpack_number(&slice[24..]) as usize;
17434 Uint256Reader::new_unchecked(&self.as_slice()[start..end])
17435 }
17436 pub fn difficulties(&self) -> Uint256VecReader<'r> {
17437 let slice = self.as_slice();
17438 let start = molecule::unpack_number(&slice[24..]) as usize;
17439 if self.has_extra_fields() {
17440 let end = molecule::unpack_number(&slice[28..]) as usize;
17441 Uint256VecReader::new_unchecked(&self.as_slice()[start..end])
17442 } else {
17443 Uint256VecReader::new_unchecked(&self.as_slice()[start..])
17444 }
17445 }
17446}
17447impl<'r> molecule::prelude::Reader<'r> for GetLastStateProofReader<'r> {
17448 type Entity = GetLastStateProof;
17449 const NAME: &'static str = "GetLastStateProofReader";
17450 fn to_entity(&self) -> Self::Entity {
17451 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
17452 }
17453 fn new_unchecked(slice: &'r [u8]) -> Self {
17454 GetLastStateProofReader(slice)
17455 }
17456 fn as_slice(&self) -> &'r [u8] {
17457 self.0
17458 }
17459 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
17460 use molecule::verification_error as ve;
17461 let slice_len = slice.len();
17462 if slice_len < molecule::NUMBER_SIZE {
17463 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
17464 }
17465 let total_size = molecule::unpack_number(slice) as usize;
17466 if slice_len != total_size {
17467 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
17468 }
17469 if slice_len < molecule::NUMBER_SIZE * 2 {
17470 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
17471 }
17472 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
17473 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
17474 return ve!(Self, OffsetsNotMatch);
17475 }
17476 if slice_len < offset_first {
17477 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
17478 }
17479 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
17480 if field_count < Self::FIELD_COUNT {
17481 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17482 } else if !compatible && field_count > Self::FIELD_COUNT {
17483 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17484 };
17485 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
17486 .chunks_exact(molecule::NUMBER_SIZE)
17487 .map(|x| molecule::unpack_number(x) as usize)
17488 .collect();
17489 offsets.push(total_size);
17490 if offsets.windows(2).any(|i| i[0] > i[1]) {
17491 return ve!(Self, OffsetsNotMatch);
17492 }
17493 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
17494 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
17495 Uint64Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
17496 Uint64Reader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
17497 Uint256Reader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
17498 Uint256VecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
17499 Ok(())
17500 }
17501}
17502#[derive(Clone, Debug, Default)]
17503pub struct GetLastStateProofBuilder {
17504 pub(crate) last_hash: Byte32,
17505 pub(crate) start_hash: Byte32,
17506 pub(crate) start_number: Uint64,
17507 pub(crate) last_n_blocks: Uint64,
17508 pub(crate) difficulty_boundary: Uint256,
17509 pub(crate) difficulties: Uint256Vec,
17510}
17511impl GetLastStateProofBuilder {
17512 pub const FIELD_COUNT: usize = 6;
17513 pub fn last_hash<T>(mut self, v: T) -> Self
17514 where
17515 T: ::core::convert::Into<Byte32>,
17516 {
17517 self.last_hash = v.into();
17518 self
17519 }
17520 pub fn start_hash<T>(mut self, v: T) -> Self
17521 where
17522 T: ::core::convert::Into<Byte32>,
17523 {
17524 self.start_hash = v.into();
17525 self
17526 }
17527 pub fn start_number<T>(mut self, v: T) -> Self
17528 where
17529 T: ::core::convert::Into<Uint64>,
17530 {
17531 self.start_number = v.into();
17532 self
17533 }
17534 pub fn last_n_blocks<T>(mut self, v: T) -> Self
17535 where
17536 T: ::core::convert::Into<Uint64>,
17537 {
17538 self.last_n_blocks = v.into();
17539 self
17540 }
17541 pub fn difficulty_boundary<T>(mut self, v: T) -> Self
17542 where
17543 T: ::core::convert::Into<Uint256>,
17544 {
17545 self.difficulty_boundary = v.into();
17546 self
17547 }
17548 pub fn difficulties<T>(mut self, v: T) -> Self
17549 where
17550 T: ::core::convert::Into<Uint256Vec>,
17551 {
17552 self.difficulties = v.into();
17553 self
17554 }
17555}
17556impl molecule::prelude::Builder for GetLastStateProofBuilder {
17557 type Entity = GetLastStateProof;
17558 const NAME: &'static str = "GetLastStateProofBuilder";
17559 fn expected_length(&self) -> usize {
17560 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
17561 + self.last_hash.as_slice().len()
17562 + self.start_hash.as_slice().len()
17563 + self.start_number.as_slice().len()
17564 + self.last_n_blocks.as_slice().len()
17565 + self.difficulty_boundary.as_slice().len()
17566 + self.difficulties.as_slice().len()
17567 }
17568 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
17569 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
17570 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
17571 offsets.push(total_size);
17572 total_size += self.last_hash.as_slice().len();
17573 offsets.push(total_size);
17574 total_size += self.start_hash.as_slice().len();
17575 offsets.push(total_size);
17576 total_size += self.start_number.as_slice().len();
17577 offsets.push(total_size);
17578 total_size += self.last_n_blocks.as_slice().len();
17579 offsets.push(total_size);
17580 total_size += self.difficulty_boundary.as_slice().len();
17581 offsets.push(total_size);
17582 total_size += self.difficulties.as_slice().len();
17583 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
17584 for offset in offsets.into_iter() {
17585 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
17586 }
17587 writer.write_all(self.last_hash.as_slice())?;
17588 writer.write_all(self.start_hash.as_slice())?;
17589 writer.write_all(self.start_number.as_slice())?;
17590 writer.write_all(self.last_n_blocks.as_slice())?;
17591 writer.write_all(self.difficulty_boundary.as_slice())?;
17592 writer.write_all(self.difficulties.as_slice())?;
17593 Ok(())
17594 }
17595 fn build(&self) -> Self::Entity {
17596 let mut inner = Vec::with_capacity(self.expected_length());
17597 self.write(&mut inner)
17598 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
17599 GetLastStateProof::new_unchecked(inner.into())
17600 }
17601}
17602#[derive(Clone)]
17603pub struct SendLastStateProof(molecule::bytes::Bytes);
17604impl ::core::fmt::LowerHex for SendLastStateProof {
17605 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17606 use molecule::hex_string;
17607 if f.alternate() {
17608 write!(f, "0x")?;
17609 }
17610 write!(f, "{}", hex_string(self.as_slice()))
17611 }
17612}
17613impl ::core::fmt::Debug for SendLastStateProof {
17614 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17615 write!(f, "{}({:#x})", Self::NAME, self)
17616 }
17617}
17618impl ::core::fmt::Display for SendLastStateProof {
17619 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17620 write!(f, "{} {{ ", Self::NAME)?;
17621 write!(f, "{}: {}", "last_header", self.last_header())?;
17622 write!(f, ", {}: {}", "proof", self.proof())?;
17623 write!(f, ", {}: {}", "headers", self.headers())?;
17624 let extra_count = self.count_extra_fields();
17625 if extra_count != 0 {
17626 write!(f, ", .. ({} fields)", extra_count)?;
17627 }
17628 write!(f, " }}")
17629 }
17630}
17631impl ::core::default::Default for SendLastStateProof {
17632 fn default() -> Self {
17633 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
17634 SendLastStateProof::new_unchecked(v)
17635 }
17636}
17637impl SendLastStateProof {
17638 const DEFAULT_VALUE: [u8; 404] = [
17639 148, 1, 0, 0, 16, 0, 0, 0, 140, 1, 0, 0, 144, 1, 0, 0, 124, 1, 0, 0, 20, 0, 0, 0, 228, 0,
17640 0, 0, 4, 1, 0, 0, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17641 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17642 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17643 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17644 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17645 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17646 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17647 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17648 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17649 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17650 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17651 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17652 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
17653 ];
17654 pub const FIELD_COUNT: usize = 3;
17655 pub fn total_size(&self) -> usize {
17656 molecule::unpack_number(self.as_slice()) as usize
17657 }
17658 pub fn field_count(&self) -> usize {
17659 if self.total_size() == molecule::NUMBER_SIZE {
17660 0
17661 } else {
17662 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17663 }
17664 }
17665 pub fn count_extra_fields(&self) -> usize {
17666 self.field_count() - Self::FIELD_COUNT
17667 }
17668 pub fn has_extra_fields(&self) -> bool {
17669 Self::FIELD_COUNT != self.field_count()
17670 }
17671 pub fn last_header(&self) -> VerifiableHeader {
17672 let slice = self.as_slice();
17673 let start = molecule::unpack_number(&slice[4..]) as usize;
17674 let end = molecule::unpack_number(&slice[8..]) as usize;
17675 VerifiableHeader::new_unchecked(self.0.slice(start..end))
17676 }
17677 pub fn proof(&self) -> HeaderDigestVec {
17678 let slice = self.as_slice();
17679 let start = molecule::unpack_number(&slice[8..]) as usize;
17680 let end = molecule::unpack_number(&slice[12..]) as usize;
17681 HeaderDigestVec::new_unchecked(self.0.slice(start..end))
17682 }
17683 pub fn headers(&self) -> VerifiableHeaderVec {
17684 let slice = self.as_slice();
17685 let start = molecule::unpack_number(&slice[12..]) as usize;
17686 if self.has_extra_fields() {
17687 let end = molecule::unpack_number(&slice[16..]) as usize;
17688 VerifiableHeaderVec::new_unchecked(self.0.slice(start..end))
17689 } else {
17690 VerifiableHeaderVec::new_unchecked(self.0.slice(start..))
17691 }
17692 }
17693 pub fn as_reader<'r>(&'r self) -> SendLastStateProofReader<'r> {
17694 SendLastStateProofReader::new_unchecked(self.as_slice())
17695 }
17696}
17697impl molecule::prelude::Entity for SendLastStateProof {
17698 type Builder = SendLastStateProofBuilder;
17699 const NAME: &'static str = "SendLastStateProof";
17700 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
17701 SendLastStateProof(data)
17702 }
17703 fn as_bytes(&self) -> molecule::bytes::Bytes {
17704 self.0.clone()
17705 }
17706 fn as_slice(&self) -> &[u8] {
17707 &self.0[..]
17708 }
17709 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17710 SendLastStateProofReader::from_slice(slice).map(|reader| reader.to_entity())
17711 }
17712 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17713 SendLastStateProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
17714 }
17715 fn new_builder() -> Self::Builder {
17716 ::core::default::Default::default()
17717 }
17718 fn as_builder(self) -> Self::Builder {
17719 Self::new_builder()
17720 .last_header(self.last_header())
17721 .proof(self.proof())
17722 .headers(self.headers())
17723 }
17724}
17725#[derive(Clone, Copy)]
17726pub struct SendLastStateProofReader<'r>(&'r [u8]);
17727impl<'r> ::core::fmt::LowerHex for SendLastStateProofReader<'r> {
17728 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17729 use molecule::hex_string;
17730 if f.alternate() {
17731 write!(f, "0x")?;
17732 }
17733 write!(f, "{}", hex_string(self.as_slice()))
17734 }
17735}
17736impl<'r> ::core::fmt::Debug for SendLastStateProofReader<'r> {
17737 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17738 write!(f, "{}({:#x})", Self::NAME, self)
17739 }
17740}
17741impl<'r> ::core::fmt::Display for SendLastStateProofReader<'r> {
17742 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17743 write!(f, "{} {{ ", Self::NAME)?;
17744 write!(f, "{}: {}", "last_header", self.last_header())?;
17745 write!(f, ", {}: {}", "proof", self.proof())?;
17746 write!(f, ", {}: {}", "headers", self.headers())?;
17747 let extra_count = self.count_extra_fields();
17748 if extra_count != 0 {
17749 write!(f, ", .. ({} fields)", extra_count)?;
17750 }
17751 write!(f, " }}")
17752 }
17753}
17754impl<'r> SendLastStateProofReader<'r> {
17755 pub const FIELD_COUNT: usize = 3;
17756 pub fn total_size(&self) -> usize {
17757 molecule::unpack_number(self.as_slice()) as usize
17758 }
17759 pub fn field_count(&self) -> usize {
17760 if self.total_size() == molecule::NUMBER_SIZE {
17761 0
17762 } else {
17763 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17764 }
17765 }
17766 pub fn count_extra_fields(&self) -> usize {
17767 self.field_count() - Self::FIELD_COUNT
17768 }
17769 pub fn has_extra_fields(&self) -> bool {
17770 Self::FIELD_COUNT != self.field_count()
17771 }
17772 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
17773 let slice = self.as_slice();
17774 let start = molecule::unpack_number(&slice[4..]) as usize;
17775 let end = molecule::unpack_number(&slice[8..]) as usize;
17776 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
17777 }
17778 pub fn proof(&self) -> HeaderDigestVecReader<'r> {
17779 let slice = self.as_slice();
17780 let start = molecule::unpack_number(&slice[8..]) as usize;
17781 let end = molecule::unpack_number(&slice[12..]) as usize;
17782 HeaderDigestVecReader::new_unchecked(&self.as_slice()[start..end])
17783 }
17784 pub fn headers(&self) -> VerifiableHeaderVecReader<'r> {
17785 let slice = self.as_slice();
17786 let start = molecule::unpack_number(&slice[12..]) as usize;
17787 if self.has_extra_fields() {
17788 let end = molecule::unpack_number(&slice[16..]) as usize;
17789 VerifiableHeaderVecReader::new_unchecked(&self.as_slice()[start..end])
17790 } else {
17791 VerifiableHeaderVecReader::new_unchecked(&self.as_slice()[start..])
17792 }
17793 }
17794}
17795impl<'r> molecule::prelude::Reader<'r> for SendLastStateProofReader<'r> {
17796 type Entity = SendLastStateProof;
17797 const NAME: &'static str = "SendLastStateProofReader";
17798 fn to_entity(&self) -> Self::Entity {
17799 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
17800 }
17801 fn new_unchecked(slice: &'r [u8]) -> Self {
17802 SendLastStateProofReader(slice)
17803 }
17804 fn as_slice(&self) -> &'r [u8] {
17805 self.0
17806 }
17807 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
17808 use molecule::verification_error as ve;
17809 let slice_len = slice.len();
17810 if slice_len < molecule::NUMBER_SIZE {
17811 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
17812 }
17813 let total_size = molecule::unpack_number(slice) as usize;
17814 if slice_len != total_size {
17815 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
17816 }
17817 if slice_len < molecule::NUMBER_SIZE * 2 {
17818 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
17819 }
17820 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
17821 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
17822 return ve!(Self, OffsetsNotMatch);
17823 }
17824 if slice_len < offset_first {
17825 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
17826 }
17827 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
17828 if field_count < Self::FIELD_COUNT {
17829 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17830 } else if !compatible && field_count > Self::FIELD_COUNT {
17831 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17832 };
17833 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
17834 .chunks_exact(molecule::NUMBER_SIZE)
17835 .map(|x| molecule::unpack_number(x) as usize)
17836 .collect();
17837 offsets.push(total_size);
17838 if offsets.windows(2).any(|i| i[0] > i[1]) {
17839 return ve!(Self, OffsetsNotMatch);
17840 }
17841 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
17842 HeaderDigestVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
17843 VerifiableHeaderVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
17844 Ok(())
17845 }
17846}
17847#[derive(Clone, Debug, Default)]
17848pub struct SendLastStateProofBuilder {
17849 pub(crate) last_header: VerifiableHeader,
17850 pub(crate) proof: HeaderDigestVec,
17851 pub(crate) headers: VerifiableHeaderVec,
17852}
17853impl SendLastStateProofBuilder {
17854 pub const FIELD_COUNT: usize = 3;
17855 pub fn last_header<T>(mut self, v: T) -> Self
17856 where
17857 T: ::core::convert::Into<VerifiableHeader>,
17858 {
17859 self.last_header = v.into();
17860 self
17861 }
17862 pub fn proof<T>(mut self, v: T) -> Self
17863 where
17864 T: ::core::convert::Into<HeaderDigestVec>,
17865 {
17866 self.proof = v.into();
17867 self
17868 }
17869 pub fn headers<T>(mut self, v: T) -> Self
17870 where
17871 T: ::core::convert::Into<VerifiableHeaderVec>,
17872 {
17873 self.headers = v.into();
17874 self
17875 }
17876}
17877impl molecule::prelude::Builder for SendLastStateProofBuilder {
17878 type Entity = SendLastStateProof;
17879 const NAME: &'static str = "SendLastStateProofBuilder";
17880 fn expected_length(&self) -> usize {
17881 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
17882 + self.last_header.as_slice().len()
17883 + self.proof.as_slice().len()
17884 + self.headers.as_slice().len()
17885 }
17886 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
17887 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
17888 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
17889 offsets.push(total_size);
17890 total_size += self.last_header.as_slice().len();
17891 offsets.push(total_size);
17892 total_size += self.proof.as_slice().len();
17893 offsets.push(total_size);
17894 total_size += self.headers.as_slice().len();
17895 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
17896 for offset in offsets.into_iter() {
17897 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
17898 }
17899 writer.write_all(self.last_header.as_slice())?;
17900 writer.write_all(self.proof.as_slice())?;
17901 writer.write_all(self.headers.as_slice())?;
17902 Ok(())
17903 }
17904 fn build(&self) -> Self::Entity {
17905 let mut inner = Vec::with_capacity(self.expected_length());
17906 self.write(&mut inner)
17907 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
17908 SendLastStateProof::new_unchecked(inner.into())
17909 }
17910}
17911#[derive(Clone)]
17912pub struct GetBlocksProof(molecule::bytes::Bytes);
17913impl ::core::fmt::LowerHex for GetBlocksProof {
17914 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17915 use molecule::hex_string;
17916 if f.alternate() {
17917 write!(f, "0x")?;
17918 }
17919 write!(f, "{}", hex_string(self.as_slice()))
17920 }
17921}
17922impl ::core::fmt::Debug for GetBlocksProof {
17923 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17924 write!(f, "{}({:#x})", Self::NAME, self)
17925 }
17926}
17927impl ::core::fmt::Display for GetBlocksProof {
17928 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17929 write!(f, "{} {{ ", Self::NAME)?;
17930 write!(f, "{}: {}", "last_hash", self.last_hash())?;
17931 write!(f, ", {}: {}", "block_hashes", self.block_hashes())?;
17932 let extra_count = self.count_extra_fields();
17933 if extra_count != 0 {
17934 write!(f, ", .. ({} fields)", extra_count)?;
17935 }
17936 write!(f, " }}")
17937 }
17938}
17939impl ::core::default::Default for GetBlocksProof {
17940 fn default() -> Self {
17941 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
17942 GetBlocksProof::new_unchecked(v)
17943 }
17944}
17945impl GetBlocksProof {
17946 const DEFAULT_VALUE: [u8; 48] = [
17947 48, 0, 0, 0, 12, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17948 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17949 ];
17950 pub const FIELD_COUNT: usize = 2;
17951 pub fn total_size(&self) -> usize {
17952 molecule::unpack_number(self.as_slice()) as usize
17953 }
17954 pub fn field_count(&self) -> usize {
17955 if self.total_size() == molecule::NUMBER_SIZE {
17956 0
17957 } else {
17958 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17959 }
17960 }
17961 pub fn count_extra_fields(&self) -> usize {
17962 self.field_count() - Self::FIELD_COUNT
17963 }
17964 pub fn has_extra_fields(&self) -> bool {
17965 Self::FIELD_COUNT != self.field_count()
17966 }
17967 pub fn last_hash(&self) -> Byte32 {
17968 let slice = self.as_slice();
17969 let start = molecule::unpack_number(&slice[4..]) as usize;
17970 let end = molecule::unpack_number(&slice[8..]) as usize;
17971 Byte32::new_unchecked(self.0.slice(start..end))
17972 }
17973 pub fn block_hashes(&self) -> Byte32Vec {
17974 let slice = self.as_slice();
17975 let start = molecule::unpack_number(&slice[8..]) as usize;
17976 if self.has_extra_fields() {
17977 let end = molecule::unpack_number(&slice[12..]) as usize;
17978 Byte32Vec::new_unchecked(self.0.slice(start..end))
17979 } else {
17980 Byte32Vec::new_unchecked(self.0.slice(start..))
17981 }
17982 }
17983 pub fn as_reader<'r>(&'r self) -> GetBlocksProofReader<'r> {
17984 GetBlocksProofReader::new_unchecked(self.as_slice())
17985 }
17986}
17987impl molecule::prelude::Entity for GetBlocksProof {
17988 type Builder = GetBlocksProofBuilder;
17989 const NAME: &'static str = "GetBlocksProof";
17990 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
17991 GetBlocksProof(data)
17992 }
17993 fn as_bytes(&self) -> molecule::bytes::Bytes {
17994 self.0.clone()
17995 }
17996 fn as_slice(&self) -> &[u8] {
17997 &self.0[..]
17998 }
17999 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18000 GetBlocksProofReader::from_slice(slice).map(|reader| reader.to_entity())
18001 }
18002 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18003 GetBlocksProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
18004 }
18005 fn new_builder() -> Self::Builder {
18006 ::core::default::Default::default()
18007 }
18008 fn as_builder(self) -> Self::Builder {
18009 Self::new_builder()
18010 .last_hash(self.last_hash())
18011 .block_hashes(self.block_hashes())
18012 }
18013}
18014#[derive(Clone, Copy)]
18015pub struct GetBlocksProofReader<'r>(&'r [u8]);
18016impl<'r> ::core::fmt::LowerHex for GetBlocksProofReader<'r> {
18017 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18018 use molecule::hex_string;
18019 if f.alternate() {
18020 write!(f, "0x")?;
18021 }
18022 write!(f, "{}", hex_string(self.as_slice()))
18023 }
18024}
18025impl<'r> ::core::fmt::Debug for GetBlocksProofReader<'r> {
18026 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18027 write!(f, "{}({:#x})", Self::NAME, self)
18028 }
18029}
18030impl<'r> ::core::fmt::Display for GetBlocksProofReader<'r> {
18031 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18032 write!(f, "{} {{ ", Self::NAME)?;
18033 write!(f, "{}: {}", "last_hash", self.last_hash())?;
18034 write!(f, ", {}: {}", "block_hashes", self.block_hashes())?;
18035 let extra_count = self.count_extra_fields();
18036 if extra_count != 0 {
18037 write!(f, ", .. ({} fields)", extra_count)?;
18038 }
18039 write!(f, " }}")
18040 }
18041}
18042impl<'r> GetBlocksProofReader<'r> {
18043 pub const FIELD_COUNT: usize = 2;
18044 pub fn total_size(&self) -> usize {
18045 molecule::unpack_number(self.as_slice()) as usize
18046 }
18047 pub fn field_count(&self) -> usize {
18048 if self.total_size() == molecule::NUMBER_SIZE {
18049 0
18050 } else {
18051 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18052 }
18053 }
18054 pub fn count_extra_fields(&self) -> usize {
18055 self.field_count() - Self::FIELD_COUNT
18056 }
18057 pub fn has_extra_fields(&self) -> bool {
18058 Self::FIELD_COUNT != self.field_count()
18059 }
18060 pub fn last_hash(&self) -> Byte32Reader<'r> {
18061 let slice = self.as_slice();
18062 let start = molecule::unpack_number(&slice[4..]) as usize;
18063 let end = molecule::unpack_number(&slice[8..]) as usize;
18064 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
18065 }
18066 pub fn block_hashes(&self) -> Byte32VecReader<'r> {
18067 let slice = self.as_slice();
18068 let start = molecule::unpack_number(&slice[8..]) as usize;
18069 if self.has_extra_fields() {
18070 let end = molecule::unpack_number(&slice[12..]) as usize;
18071 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
18072 } else {
18073 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
18074 }
18075 }
18076}
18077impl<'r> molecule::prelude::Reader<'r> for GetBlocksProofReader<'r> {
18078 type Entity = GetBlocksProof;
18079 const NAME: &'static str = "GetBlocksProofReader";
18080 fn to_entity(&self) -> Self::Entity {
18081 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
18082 }
18083 fn new_unchecked(slice: &'r [u8]) -> Self {
18084 GetBlocksProofReader(slice)
18085 }
18086 fn as_slice(&self) -> &'r [u8] {
18087 self.0
18088 }
18089 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
18090 use molecule::verification_error as ve;
18091 let slice_len = slice.len();
18092 if slice_len < molecule::NUMBER_SIZE {
18093 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
18094 }
18095 let total_size = molecule::unpack_number(slice) as usize;
18096 if slice_len != total_size {
18097 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
18098 }
18099 if slice_len < molecule::NUMBER_SIZE * 2 {
18100 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
18101 }
18102 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
18103 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
18104 return ve!(Self, OffsetsNotMatch);
18105 }
18106 if slice_len < offset_first {
18107 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
18108 }
18109 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
18110 if field_count < Self::FIELD_COUNT {
18111 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18112 } else if !compatible && field_count > Self::FIELD_COUNT {
18113 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18114 };
18115 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
18116 .chunks_exact(molecule::NUMBER_SIZE)
18117 .map(|x| molecule::unpack_number(x) as usize)
18118 .collect();
18119 offsets.push(total_size);
18120 if offsets.windows(2).any(|i| i[0] > i[1]) {
18121 return ve!(Self, OffsetsNotMatch);
18122 }
18123 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
18124 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
18125 Ok(())
18126 }
18127}
18128#[derive(Clone, Debug, Default)]
18129pub struct GetBlocksProofBuilder {
18130 pub(crate) last_hash: Byte32,
18131 pub(crate) block_hashes: Byte32Vec,
18132}
18133impl GetBlocksProofBuilder {
18134 pub const FIELD_COUNT: usize = 2;
18135 pub fn last_hash<T>(mut self, v: T) -> Self
18136 where
18137 T: ::core::convert::Into<Byte32>,
18138 {
18139 self.last_hash = v.into();
18140 self
18141 }
18142 pub fn block_hashes<T>(mut self, v: T) -> Self
18143 where
18144 T: ::core::convert::Into<Byte32Vec>,
18145 {
18146 self.block_hashes = v.into();
18147 self
18148 }
18149}
18150impl molecule::prelude::Builder for GetBlocksProofBuilder {
18151 type Entity = GetBlocksProof;
18152 const NAME: &'static str = "GetBlocksProofBuilder";
18153 fn expected_length(&self) -> usize {
18154 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
18155 + self.last_hash.as_slice().len()
18156 + self.block_hashes.as_slice().len()
18157 }
18158 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
18159 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
18160 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
18161 offsets.push(total_size);
18162 total_size += self.last_hash.as_slice().len();
18163 offsets.push(total_size);
18164 total_size += self.block_hashes.as_slice().len();
18165 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
18166 for offset in offsets.into_iter() {
18167 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
18168 }
18169 writer.write_all(self.last_hash.as_slice())?;
18170 writer.write_all(self.block_hashes.as_slice())?;
18171 Ok(())
18172 }
18173 fn build(&self) -> Self::Entity {
18174 let mut inner = Vec::with_capacity(self.expected_length());
18175 self.write(&mut inner)
18176 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
18177 GetBlocksProof::new_unchecked(inner.into())
18178 }
18179}
18180#[derive(Clone)]
18181pub struct SendBlocksProof(molecule::bytes::Bytes);
18182impl ::core::fmt::LowerHex for SendBlocksProof {
18183 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18184 use molecule::hex_string;
18185 if f.alternate() {
18186 write!(f, "0x")?;
18187 }
18188 write!(f, "{}", hex_string(self.as_slice()))
18189 }
18190}
18191impl ::core::fmt::Debug for SendBlocksProof {
18192 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18193 write!(f, "{}({:#x})", Self::NAME, self)
18194 }
18195}
18196impl ::core::fmt::Display for SendBlocksProof {
18197 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18198 write!(f, "{} {{ ", Self::NAME)?;
18199 write!(f, "{}: {}", "last_header", self.last_header())?;
18200 write!(f, ", {}: {}", "proof", self.proof())?;
18201 write!(f, ", {}: {}", "headers", self.headers())?;
18202 write!(
18203 f,
18204 ", {}: {}",
18205 "missing_block_hashes",
18206 self.missing_block_hashes()
18207 )?;
18208 let extra_count = self.count_extra_fields();
18209 if extra_count != 0 {
18210 write!(f, ", .. ({} fields)", extra_count)?;
18211 }
18212 write!(f, " }}")
18213 }
18214}
18215impl ::core::default::Default for SendBlocksProof {
18216 fn default() -> Self {
18217 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
18218 SendBlocksProof::new_unchecked(v)
18219 }
18220}
18221impl SendBlocksProof {
18222 const DEFAULT_VALUE: [u8; 412] = [
18223 156, 1, 0, 0, 20, 0, 0, 0, 144, 1, 0, 0, 148, 1, 0, 0, 152, 1, 0, 0, 124, 1, 0, 0, 20, 0,
18224 0, 0, 228, 0, 0, 0, 4, 1, 0, 0, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18225 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18226 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18227 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18228 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18229 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18230 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18231 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18232 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18233 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18234 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18235 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18236 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18237 ];
18238 pub const FIELD_COUNT: usize = 4;
18239 pub fn total_size(&self) -> usize {
18240 molecule::unpack_number(self.as_slice()) as usize
18241 }
18242 pub fn field_count(&self) -> usize {
18243 if self.total_size() == molecule::NUMBER_SIZE {
18244 0
18245 } else {
18246 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18247 }
18248 }
18249 pub fn count_extra_fields(&self) -> usize {
18250 self.field_count() - Self::FIELD_COUNT
18251 }
18252 pub fn has_extra_fields(&self) -> bool {
18253 Self::FIELD_COUNT != self.field_count()
18254 }
18255 pub fn last_header(&self) -> VerifiableHeader {
18256 let slice = self.as_slice();
18257 let start = molecule::unpack_number(&slice[4..]) as usize;
18258 let end = molecule::unpack_number(&slice[8..]) as usize;
18259 VerifiableHeader::new_unchecked(self.0.slice(start..end))
18260 }
18261 pub fn proof(&self) -> HeaderDigestVec {
18262 let slice = self.as_slice();
18263 let start = molecule::unpack_number(&slice[8..]) as usize;
18264 let end = molecule::unpack_number(&slice[12..]) as usize;
18265 HeaderDigestVec::new_unchecked(self.0.slice(start..end))
18266 }
18267 pub fn headers(&self) -> HeaderVec {
18268 let slice = self.as_slice();
18269 let start = molecule::unpack_number(&slice[12..]) as usize;
18270 let end = molecule::unpack_number(&slice[16..]) as usize;
18271 HeaderVec::new_unchecked(self.0.slice(start..end))
18272 }
18273 pub fn missing_block_hashes(&self) -> Byte32Vec {
18274 let slice = self.as_slice();
18275 let start = molecule::unpack_number(&slice[16..]) as usize;
18276 if self.has_extra_fields() {
18277 let end = molecule::unpack_number(&slice[20..]) as usize;
18278 Byte32Vec::new_unchecked(self.0.slice(start..end))
18279 } else {
18280 Byte32Vec::new_unchecked(self.0.slice(start..))
18281 }
18282 }
18283 pub fn as_reader<'r>(&'r self) -> SendBlocksProofReader<'r> {
18284 SendBlocksProofReader::new_unchecked(self.as_slice())
18285 }
18286}
18287impl molecule::prelude::Entity for SendBlocksProof {
18288 type Builder = SendBlocksProofBuilder;
18289 const NAME: &'static str = "SendBlocksProof";
18290 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
18291 SendBlocksProof(data)
18292 }
18293 fn as_bytes(&self) -> molecule::bytes::Bytes {
18294 self.0.clone()
18295 }
18296 fn as_slice(&self) -> &[u8] {
18297 &self.0[..]
18298 }
18299 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18300 SendBlocksProofReader::from_slice(slice).map(|reader| reader.to_entity())
18301 }
18302 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18303 SendBlocksProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
18304 }
18305 fn new_builder() -> Self::Builder {
18306 ::core::default::Default::default()
18307 }
18308 fn as_builder(self) -> Self::Builder {
18309 Self::new_builder()
18310 .last_header(self.last_header())
18311 .proof(self.proof())
18312 .headers(self.headers())
18313 .missing_block_hashes(self.missing_block_hashes())
18314 }
18315}
18316#[derive(Clone, Copy)]
18317pub struct SendBlocksProofReader<'r>(&'r [u8]);
18318impl<'r> ::core::fmt::LowerHex for SendBlocksProofReader<'r> {
18319 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18320 use molecule::hex_string;
18321 if f.alternate() {
18322 write!(f, "0x")?;
18323 }
18324 write!(f, "{}", hex_string(self.as_slice()))
18325 }
18326}
18327impl<'r> ::core::fmt::Debug for SendBlocksProofReader<'r> {
18328 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18329 write!(f, "{}({:#x})", Self::NAME, self)
18330 }
18331}
18332impl<'r> ::core::fmt::Display for SendBlocksProofReader<'r> {
18333 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18334 write!(f, "{} {{ ", Self::NAME)?;
18335 write!(f, "{}: {}", "last_header", self.last_header())?;
18336 write!(f, ", {}: {}", "proof", self.proof())?;
18337 write!(f, ", {}: {}", "headers", self.headers())?;
18338 write!(
18339 f,
18340 ", {}: {}",
18341 "missing_block_hashes",
18342 self.missing_block_hashes()
18343 )?;
18344 let extra_count = self.count_extra_fields();
18345 if extra_count != 0 {
18346 write!(f, ", .. ({} fields)", extra_count)?;
18347 }
18348 write!(f, " }}")
18349 }
18350}
18351impl<'r> SendBlocksProofReader<'r> {
18352 pub const FIELD_COUNT: usize = 4;
18353 pub fn total_size(&self) -> usize {
18354 molecule::unpack_number(self.as_slice()) as usize
18355 }
18356 pub fn field_count(&self) -> usize {
18357 if self.total_size() == molecule::NUMBER_SIZE {
18358 0
18359 } else {
18360 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18361 }
18362 }
18363 pub fn count_extra_fields(&self) -> usize {
18364 self.field_count() - Self::FIELD_COUNT
18365 }
18366 pub fn has_extra_fields(&self) -> bool {
18367 Self::FIELD_COUNT != self.field_count()
18368 }
18369 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
18370 let slice = self.as_slice();
18371 let start = molecule::unpack_number(&slice[4..]) as usize;
18372 let end = molecule::unpack_number(&slice[8..]) as usize;
18373 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
18374 }
18375 pub fn proof(&self) -> HeaderDigestVecReader<'r> {
18376 let slice = self.as_slice();
18377 let start = molecule::unpack_number(&slice[8..]) as usize;
18378 let end = molecule::unpack_number(&slice[12..]) as usize;
18379 HeaderDigestVecReader::new_unchecked(&self.as_slice()[start..end])
18380 }
18381 pub fn headers(&self) -> HeaderVecReader<'r> {
18382 let slice = self.as_slice();
18383 let start = molecule::unpack_number(&slice[12..]) as usize;
18384 let end = molecule::unpack_number(&slice[16..]) as usize;
18385 HeaderVecReader::new_unchecked(&self.as_slice()[start..end])
18386 }
18387 pub fn missing_block_hashes(&self) -> Byte32VecReader<'r> {
18388 let slice = self.as_slice();
18389 let start = molecule::unpack_number(&slice[16..]) as usize;
18390 if self.has_extra_fields() {
18391 let end = molecule::unpack_number(&slice[20..]) as usize;
18392 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
18393 } else {
18394 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
18395 }
18396 }
18397}
18398impl<'r> molecule::prelude::Reader<'r> for SendBlocksProofReader<'r> {
18399 type Entity = SendBlocksProof;
18400 const NAME: &'static str = "SendBlocksProofReader";
18401 fn to_entity(&self) -> Self::Entity {
18402 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
18403 }
18404 fn new_unchecked(slice: &'r [u8]) -> Self {
18405 SendBlocksProofReader(slice)
18406 }
18407 fn as_slice(&self) -> &'r [u8] {
18408 self.0
18409 }
18410 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
18411 use molecule::verification_error as ve;
18412 let slice_len = slice.len();
18413 if slice_len < molecule::NUMBER_SIZE {
18414 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
18415 }
18416 let total_size = molecule::unpack_number(slice) as usize;
18417 if slice_len != total_size {
18418 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
18419 }
18420 if slice_len < molecule::NUMBER_SIZE * 2 {
18421 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
18422 }
18423 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
18424 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
18425 return ve!(Self, OffsetsNotMatch);
18426 }
18427 if slice_len < offset_first {
18428 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
18429 }
18430 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
18431 if field_count < Self::FIELD_COUNT {
18432 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18433 } else if !compatible && field_count > Self::FIELD_COUNT {
18434 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18435 };
18436 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
18437 .chunks_exact(molecule::NUMBER_SIZE)
18438 .map(|x| molecule::unpack_number(x) as usize)
18439 .collect();
18440 offsets.push(total_size);
18441 if offsets.windows(2).any(|i| i[0] > i[1]) {
18442 return ve!(Self, OffsetsNotMatch);
18443 }
18444 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
18445 HeaderDigestVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
18446 HeaderVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
18447 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
18448 Ok(())
18449 }
18450}
18451#[derive(Clone, Debug, Default)]
18452pub struct SendBlocksProofBuilder {
18453 pub(crate) last_header: VerifiableHeader,
18454 pub(crate) proof: HeaderDigestVec,
18455 pub(crate) headers: HeaderVec,
18456 pub(crate) missing_block_hashes: Byte32Vec,
18457}
18458impl SendBlocksProofBuilder {
18459 pub const FIELD_COUNT: usize = 4;
18460 pub fn last_header<T>(mut self, v: T) -> Self
18461 where
18462 T: ::core::convert::Into<VerifiableHeader>,
18463 {
18464 self.last_header = v.into();
18465 self
18466 }
18467 pub fn proof<T>(mut self, v: T) -> Self
18468 where
18469 T: ::core::convert::Into<HeaderDigestVec>,
18470 {
18471 self.proof = v.into();
18472 self
18473 }
18474 pub fn headers<T>(mut self, v: T) -> Self
18475 where
18476 T: ::core::convert::Into<HeaderVec>,
18477 {
18478 self.headers = v.into();
18479 self
18480 }
18481 pub fn missing_block_hashes<T>(mut self, v: T) -> Self
18482 where
18483 T: ::core::convert::Into<Byte32Vec>,
18484 {
18485 self.missing_block_hashes = v.into();
18486 self
18487 }
18488}
18489impl molecule::prelude::Builder for SendBlocksProofBuilder {
18490 type Entity = SendBlocksProof;
18491 const NAME: &'static str = "SendBlocksProofBuilder";
18492 fn expected_length(&self) -> usize {
18493 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
18494 + self.last_header.as_slice().len()
18495 + self.proof.as_slice().len()
18496 + self.headers.as_slice().len()
18497 + self.missing_block_hashes.as_slice().len()
18498 }
18499 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
18500 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
18501 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
18502 offsets.push(total_size);
18503 total_size += self.last_header.as_slice().len();
18504 offsets.push(total_size);
18505 total_size += self.proof.as_slice().len();
18506 offsets.push(total_size);
18507 total_size += self.headers.as_slice().len();
18508 offsets.push(total_size);
18509 total_size += self.missing_block_hashes.as_slice().len();
18510 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
18511 for offset in offsets.into_iter() {
18512 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
18513 }
18514 writer.write_all(self.last_header.as_slice())?;
18515 writer.write_all(self.proof.as_slice())?;
18516 writer.write_all(self.headers.as_slice())?;
18517 writer.write_all(self.missing_block_hashes.as_slice())?;
18518 Ok(())
18519 }
18520 fn build(&self) -> Self::Entity {
18521 let mut inner = Vec::with_capacity(self.expected_length());
18522 self.write(&mut inner)
18523 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
18524 SendBlocksProof::new_unchecked(inner.into())
18525 }
18526}
18527#[derive(Clone)]
18528pub struct SendBlocksProofV1(molecule::bytes::Bytes);
18529impl ::core::fmt::LowerHex for SendBlocksProofV1 {
18530 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18531 use molecule::hex_string;
18532 if f.alternate() {
18533 write!(f, "0x")?;
18534 }
18535 write!(f, "{}", hex_string(self.as_slice()))
18536 }
18537}
18538impl ::core::fmt::Debug for SendBlocksProofV1 {
18539 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18540 write!(f, "{}({:#x})", Self::NAME, self)
18541 }
18542}
18543impl ::core::fmt::Display for SendBlocksProofV1 {
18544 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18545 write!(f, "{} {{ ", Self::NAME)?;
18546 write!(f, "{}: {}", "last_header", self.last_header())?;
18547 write!(f, ", {}: {}", "proof", self.proof())?;
18548 write!(f, ", {}: {}", "headers", self.headers())?;
18549 write!(
18550 f,
18551 ", {}: {}",
18552 "missing_block_hashes",
18553 self.missing_block_hashes()
18554 )?;
18555 write!(
18556 f,
18557 ", {}: {}",
18558 "blocks_uncles_hash",
18559 self.blocks_uncles_hash()
18560 )?;
18561 write!(f, ", {}: {}", "blocks_extension", self.blocks_extension())?;
18562 let extra_count = self.count_extra_fields();
18563 if extra_count != 0 {
18564 write!(f, ", .. ({} fields)", extra_count)?;
18565 }
18566 write!(f, " }}")
18567 }
18568}
18569impl ::core::default::Default for SendBlocksProofV1 {
18570 fn default() -> Self {
18571 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
18572 SendBlocksProofV1::new_unchecked(v)
18573 }
18574}
18575impl SendBlocksProofV1 {
18576 const DEFAULT_VALUE: [u8; 428] = [
18577 172, 1, 0, 0, 28, 0, 0, 0, 152, 1, 0, 0, 156, 1, 0, 0, 160, 1, 0, 0, 164, 1, 0, 0, 168, 1,
18578 0, 0, 124, 1, 0, 0, 20, 0, 0, 0, 228, 0, 0, 0, 4, 1, 0, 0, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18579 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18582 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18583 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18587 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18588 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18589 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18590 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18591 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
18592 ];
18593 pub const FIELD_COUNT: usize = 6;
18594 pub fn total_size(&self) -> usize {
18595 molecule::unpack_number(self.as_slice()) as usize
18596 }
18597 pub fn field_count(&self) -> usize {
18598 if self.total_size() == molecule::NUMBER_SIZE {
18599 0
18600 } else {
18601 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18602 }
18603 }
18604 pub fn count_extra_fields(&self) -> usize {
18605 self.field_count() - Self::FIELD_COUNT
18606 }
18607 pub fn has_extra_fields(&self) -> bool {
18608 Self::FIELD_COUNT != self.field_count()
18609 }
18610 pub fn last_header(&self) -> VerifiableHeader {
18611 let slice = self.as_slice();
18612 let start = molecule::unpack_number(&slice[4..]) as usize;
18613 let end = molecule::unpack_number(&slice[8..]) as usize;
18614 VerifiableHeader::new_unchecked(self.0.slice(start..end))
18615 }
18616 pub fn proof(&self) -> HeaderDigestVec {
18617 let slice = self.as_slice();
18618 let start = molecule::unpack_number(&slice[8..]) as usize;
18619 let end = molecule::unpack_number(&slice[12..]) as usize;
18620 HeaderDigestVec::new_unchecked(self.0.slice(start..end))
18621 }
18622 pub fn headers(&self) -> HeaderVec {
18623 let slice = self.as_slice();
18624 let start = molecule::unpack_number(&slice[12..]) as usize;
18625 let end = molecule::unpack_number(&slice[16..]) as usize;
18626 HeaderVec::new_unchecked(self.0.slice(start..end))
18627 }
18628 pub fn missing_block_hashes(&self) -> Byte32Vec {
18629 let slice = self.as_slice();
18630 let start = molecule::unpack_number(&slice[16..]) as usize;
18631 let end = molecule::unpack_number(&slice[20..]) as usize;
18632 Byte32Vec::new_unchecked(self.0.slice(start..end))
18633 }
18634 pub fn blocks_uncles_hash(&self) -> Byte32Vec {
18635 let slice = self.as_slice();
18636 let start = molecule::unpack_number(&slice[20..]) as usize;
18637 let end = molecule::unpack_number(&slice[24..]) as usize;
18638 Byte32Vec::new_unchecked(self.0.slice(start..end))
18639 }
18640 pub fn blocks_extension(&self) -> BytesOptVec {
18641 let slice = self.as_slice();
18642 let start = molecule::unpack_number(&slice[24..]) as usize;
18643 if self.has_extra_fields() {
18644 let end = molecule::unpack_number(&slice[28..]) as usize;
18645 BytesOptVec::new_unchecked(self.0.slice(start..end))
18646 } else {
18647 BytesOptVec::new_unchecked(self.0.slice(start..))
18648 }
18649 }
18650 pub fn as_reader<'r>(&'r self) -> SendBlocksProofV1Reader<'r> {
18651 SendBlocksProofV1Reader::new_unchecked(self.as_slice())
18652 }
18653}
18654impl molecule::prelude::Entity for SendBlocksProofV1 {
18655 type Builder = SendBlocksProofV1Builder;
18656 const NAME: &'static str = "SendBlocksProofV1";
18657 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
18658 SendBlocksProofV1(data)
18659 }
18660 fn as_bytes(&self) -> molecule::bytes::Bytes {
18661 self.0.clone()
18662 }
18663 fn as_slice(&self) -> &[u8] {
18664 &self.0[..]
18665 }
18666 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18667 SendBlocksProofV1Reader::from_slice(slice).map(|reader| reader.to_entity())
18668 }
18669 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18670 SendBlocksProofV1Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
18671 }
18672 fn new_builder() -> Self::Builder {
18673 ::core::default::Default::default()
18674 }
18675 fn as_builder(self) -> Self::Builder {
18676 Self::new_builder()
18677 .last_header(self.last_header())
18678 .proof(self.proof())
18679 .headers(self.headers())
18680 .missing_block_hashes(self.missing_block_hashes())
18681 .blocks_uncles_hash(self.blocks_uncles_hash())
18682 .blocks_extension(self.blocks_extension())
18683 }
18684}
18685#[derive(Clone, Copy)]
18686pub struct SendBlocksProofV1Reader<'r>(&'r [u8]);
18687impl<'r> ::core::fmt::LowerHex for SendBlocksProofV1Reader<'r> {
18688 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18689 use molecule::hex_string;
18690 if f.alternate() {
18691 write!(f, "0x")?;
18692 }
18693 write!(f, "{}", hex_string(self.as_slice()))
18694 }
18695}
18696impl<'r> ::core::fmt::Debug for SendBlocksProofV1Reader<'r> {
18697 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18698 write!(f, "{}({:#x})", Self::NAME, self)
18699 }
18700}
18701impl<'r> ::core::fmt::Display for SendBlocksProofV1Reader<'r> {
18702 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18703 write!(f, "{} {{ ", Self::NAME)?;
18704 write!(f, "{}: {}", "last_header", self.last_header())?;
18705 write!(f, ", {}: {}", "proof", self.proof())?;
18706 write!(f, ", {}: {}", "headers", self.headers())?;
18707 write!(
18708 f,
18709 ", {}: {}",
18710 "missing_block_hashes",
18711 self.missing_block_hashes()
18712 )?;
18713 write!(
18714 f,
18715 ", {}: {}",
18716 "blocks_uncles_hash",
18717 self.blocks_uncles_hash()
18718 )?;
18719 write!(f, ", {}: {}", "blocks_extension", self.blocks_extension())?;
18720 let extra_count = self.count_extra_fields();
18721 if extra_count != 0 {
18722 write!(f, ", .. ({} fields)", extra_count)?;
18723 }
18724 write!(f, " }}")
18725 }
18726}
18727impl<'r> SendBlocksProofV1Reader<'r> {
18728 pub const FIELD_COUNT: usize = 6;
18729 pub fn total_size(&self) -> usize {
18730 molecule::unpack_number(self.as_slice()) as usize
18731 }
18732 pub fn field_count(&self) -> usize {
18733 if self.total_size() == molecule::NUMBER_SIZE {
18734 0
18735 } else {
18736 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18737 }
18738 }
18739 pub fn count_extra_fields(&self) -> usize {
18740 self.field_count() - Self::FIELD_COUNT
18741 }
18742 pub fn has_extra_fields(&self) -> bool {
18743 Self::FIELD_COUNT != self.field_count()
18744 }
18745 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
18746 let slice = self.as_slice();
18747 let start = molecule::unpack_number(&slice[4..]) as usize;
18748 let end = molecule::unpack_number(&slice[8..]) as usize;
18749 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
18750 }
18751 pub fn proof(&self) -> HeaderDigestVecReader<'r> {
18752 let slice = self.as_slice();
18753 let start = molecule::unpack_number(&slice[8..]) as usize;
18754 let end = molecule::unpack_number(&slice[12..]) as usize;
18755 HeaderDigestVecReader::new_unchecked(&self.as_slice()[start..end])
18756 }
18757 pub fn headers(&self) -> HeaderVecReader<'r> {
18758 let slice = self.as_slice();
18759 let start = molecule::unpack_number(&slice[12..]) as usize;
18760 let end = molecule::unpack_number(&slice[16..]) as usize;
18761 HeaderVecReader::new_unchecked(&self.as_slice()[start..end])
18762 }
18763 pub fn missing_block_hashes(&self) -> Byte32VecReader<'r> {
18764 let slice = self.as_slice();
18765 let start = molecule::unpack_number(&slice[16..]) as usize;
18766 let end = molecule::unpack_number(&slice[20..]) as usize;
18767 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
18768 }
18769 pub fn blocks_uncles_hash(&self) -> Byte32VecReader<'r> {
18770 let slice = self.as_slice();
18771 let start = molecule::unpack_number(&slice[20..]) as usize;
18772 let end = molecule::unpack_number(&slice[24..]) as usize;
18773 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
18774 }
18775 pub fn blocks_extension(&self) -> BytesOptVecReader<'r> {
18776 let slice = self.as_slice();
18777 let start = molecule::unpack_number(&slice[24..]) as usize;
18778 if self.has_extra_fields() {
18779 let end = molecule::unpack_number(&slice[28..]) as usize;
18780 BytesOptVecReader::new_unchecked(&self.as_slice()[start..end])
18781 } else {
18782 BytesOptVecReader::new_unchecked(&self.as_slice()[start..])
18783 }
18784 }
18785}
18786impl<'r> molecule::prelude::Reader<'r> for SendBlocksProofV1Reader<'r> {
18787 type Entity = SendBlocksProofV1;
18788 const NAME: &'static str = "SendBlocksProofV1Reader";
18789 fn to_entity(&self) -> Self::Entity {
18790 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
18791 }
18792 fn new_unchecked(slice: &'r [u8]) -> Self {
18793 SendBlocksProofV1Reader(slice)
18794 }
18795 fn as_slice(&self) -> &'r [u8] {
18796 self.0
18797 }
18798 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
18799 use molecule::verification_error as ve;
18800 let slice_len = slice.len();
18801 if slice_len < molecule::NUMBER_SIZE {
18802 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
18803 }
18804 let total_size = molecule::unpack_number(slice) as usize;
18805 if slice_len != total_size {
18806 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
18807 }
18808 if slice_len < molecule::NUMBER_SIZE * 2 {
18809 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
18810 }
18811 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
18812 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
18813 return ve!(Self, OffsetsNotMatch);
18814 }
18815 if slice_len < offset_first {
18816 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
18817 }
18818 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
18819 if field_count < Self::FIELD_COUNT {
18820 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18821 } else if !compatible && field_count > Self::FIELD_COUNT {
18822 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18823 };
18824 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
18825 .chunks_exact(molecule::NUMBER_SIZE)
18826 .map(|x| molecule::unpack_number(x) as usize)
18827 .collect();
18828 offsets.push(total_size);
18829 if offsets.windows(2).any(|i| i[0] > i[1]) {
18830 return ve!(Self, OffsetsNotMatch);
18831 }
18832 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
18833 HeaderDigestVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
18834 HeaderVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
18835 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
18836 Byte32VecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
18837 BytesOptVecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
18838 Ok(())
18839 }
18840}
18841#[derive(Clone, Debug, Default)]
18842pub struct SendBlocksProofV1Builder {
18843 pub(crate) last_header: VerifiableHeader,
18844 pub(crate) proof: HeaderDigestVec,
18845 pub(crate) headers: HeaderVec,
18846 pub(crate) missing_block_hashes: Byte32Vec,
18847 pub(crate) blocks_uncles_hash: Byte32Vec,
18848 pub(crate) blocks_extension: BytesOptVec,
18849}
18850impl SendBlocksProofV1Builder {
18851 pub const FIELD_COUNT: usize = 6;
18852 pub fn last_header<T>(mut self, v: T) -> Self
18853 where
18854 T: ::core::convert::Into<VerifiableHeader>,
18855 {
18856 self.last_header = v.into();
18857 self
18858 }
18859 pub fn proof<T>(mut self, v: T) -> Self
18860 where
18861 T: ::core::convert::Into<HeaderDigestVec>,
18862 {
18863 self.proof = v.into();
18864 self
18865 }
18866 pub fn headers<T>(mut self, v: T) -> Self
18867 where
18868 T: ::core::convert::Into<HeaderVec>,
18869 {
18870 self.headers = v.into();
18871 self
18872 }
18873 pub fn missing_block_hashes<T>(mut self, v: T) -> Self
18874 where
18875 T: ::core::convert::Into<Byte32Vec>,
18876 {
18877 self.missing_block_hashes = v.into();
18878 self
18879 }
18880 pub fn blocks_uncles_hash<T>(mut self, v: T) -> Self
18881 where
18882 T: ::core::convert::Into<Byte32Vec>,
18883 {
18884 self.blocks_uncles_hash = v.into();
18885 self
18886 }
18887 pub fn blocks_extension<T>(mut self, v: T) -> Self
18888 where
18889 T: ::core::convert::Into<BytesOptVec>,
18890 {
18891 self.blocks_extension = v.into();
18892 self
18893 }
18894}
18895impl molecule::prelude::Builder for SendBlocksProofV1Builder {
18896 type Entity = SendBlocksProofV1;
18897 const NAME: &'static str = "SendBlocksProofV1Builder";
18898 fn expected_length(&self) -> usize {
18899 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
18900 + self.last_header.as_slice().len()
18901 + self.proof.as_slice().len()
18902 + self.headers.as_slice().len()
18903 + self.missing_block_hashes.as_slice().len()
18904 + self.blocks_uncles_hash.as_slice().len()
18905 + self.blocks_extension.as_slice().len()
18906 }
18907 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
18908 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
18909 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
18910 offsets.push(total_size);
18911 total_size += self.last_header.as_slice().len();
18912 offsets.push(total_size);
18913 total_size += self.proof.as_slice().len();
18914 offsets.push(total_size);
18915 total_size += self.headers.as_slice().len();
18916 offsets.push(total_size);
18917 total_size += self.missing_block_hashes.as_slice().len();
18918 offsets.push(total_size);
18919 total_size += self.blocks_uncles_hash.as_slice().len();
18920 offsets.push(total_size);
18921 total_size += self.blocks_extension.as_slice().len();
18922 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
18923 for offset in offsets.into_iter() {
18924 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
18925 }
18926 writer.write_all(self.last_header.as_slice())?;
18927 writer.write_all(self.proof.as_slice())?;
18928 writer.write_all(self.headers.as_slice())?;
18929 writer.write_all(self.missing_block_hashes.as_slice())?;
18930 writer.write_all(self.blocks_uncles_hash.as_slice())?;
18931 writer.write_all(self.blocks_extension.as_slice())?;
18932 Ok(())
18933 }
18934 fn build(&self) -> Self::Entity {
18935 let mut inner = Vec::with_capacity(self.expected_length());
18936 self.write(&mut inner)
18937 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
18938 SendBlocksProofV1::new_unchecked(inner.into())
18939 }
18940}
18941#[derive(Clone)]
18942pub struct GetTransactionsProof(molecule::bytes::Bytes);
18943impl ::core::fmt::LowerHex for GetTransactionsProof {
18944 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18945 use molecule::hex_string;
18946 if f.alternate() {
18947 write!(f, "0x")?;
18948 }
18949 write!(f, "{}", hex_string(self.as_slice()))
18950 }
18951}
18952impl ::core::fmt::Debug for GetTransactionsProof {
18953 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18954 write!(f, "{}({:#x})", Self::NAME, self)
18955 }
18956}
18957impl ::core::fmt::Display for GetTransactionsProof {
18958 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18959 write!(f, "{} {{ ", Self::NAME)?;
18960 write!(f, "{}: {}", "last_hash", self.last_hash())?;
18961 write!(f, ", {}: {}", "tx_hashes", self.tx_hashes())?;
18962 let extra_count = self.count_extra_fields();
18963 if extra_count != 0 {
18964 write!(f, ", .. ({} fields)", extra_count)?;
18965 }
18966 write!(f, " }}")
18967 }
18968}
18969impl ::core::default::Default for GetTransactionsProof {
18970 fn default() -> Self {
18971 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
18972 GetTransactionsProof::new_unchecked(v)
18973 }
18974}
18975impl GetTransactionsProof {
18976 const DEFAULT_VALUE: [u8; 48] = [
18977 48, 0, 0, 0, 12, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18978 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18979 ];
18980 pub const FIELD_COUNT: usize = 2;
18981 pub fn total_size(&self) -> usize {
18982 molecule::unpack_number(self.as_slice()) as usize
18983 }
18984 pub fn field_count(&self) -> usize {
18985 if self.total_size() == molecule::NUMBER_SIZE {
18986 0
18987 } else {
18988 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18989 }
18990 }
18991 pub fn count_extra_fields(&self) -> usize {
18992 self.field_count() - Self::FIELD_COUNT
18993 }
18994 pub fn has_extra_fields(&self) -> bool {
18995 Self::FIELD_COUNT != self.field_count()
18996 }
18997 pub fn last_hash(&self) -> Byte32 {
18998 let slice = self.as_slice();
18999 let start = molecule::unpack_number(&slice[4..]) as usize;
19000 let end = molecule::unpack_number(&slice[8..]) as usize;
19001 Byte32::new_unchecked(self.0.slice(start..end))
19002 }
19003 pub fn tx_hashes(&self) -> Byte32Vec {
19004 let slice = self.as_slice();
19005 let start = molecule::unpack_number(&slice[8..]) as usize;
19006 if self.has_extra_fields() {
19007 let end = molecule::unpack_number(&slice[12..]) as usize;
19008 Byte32Vec::new_unchecked(self.0.slice(start..end))
19009 } else {
19010 Byte32Vec::new_unchecked(self.0.slice(start..))
19011 }
19012 }
19013 pub fn as_reader<'r>(&'r self) -> GetTransactionsProofReader<'r> {
19014 GetTransactionsProofReader::new_unchecked(self.as_slice())
19015 }
19016}
19017impl molecule::prelude::Entity for GetTransactionsProof {
19018 type Builder = GetTransactionsProofBuilder;
19019 const NAME: &'static str = "GetTransactionsProof";
19020 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
19021 GetTransactionsProof(data)
19022 }
19023 fn as_bytes(&self) -> molecule::bytes::Bytes {
19024 self.0.clone()
19025 }
19026 fn as_slice(&self) -> &[u8] {
19027 &self.0[..]
19028 }
19029 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19030 GetTransactionsProofReader::from_slice(slice).map(|reader| reader.to_entity())
19031 }
19032 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19033 GetTransactionsProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
19034 }
19035 fn new_builder() -> Self::Builder {
19036 ::core::default::Default::default()
19037 }
19038 fn as_builder(self) -> Self::Builder {
19039 Self::new_builder()
19040 .last_hash(self.last_hash())
19041 .tx_hashes(self.tx_hashes())
19042 }
19043}
19044#[derive(Clone, Copy)]
19045pub struct GetTransactionsProofReader<'r>(&'r [u8]);
19046impl<'r> ::core::fmt::LowerHex for GetTransactionsProofReader<'r> {
19047 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19048 use molecule::hex_string;
19049 if f.alternate() {
19050 write!(f, "0x")?;
19051 }
19052 write!(f, "{}", hex_string(self.as_slice()))
19053 }
19054}
19055impl<'r> ::core::fmt::Debug for GetTransactionsProofReader<'r> {
19056 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19057 write!(f, "{}({:#x})", Self::NAME, self)
19058 }
19059}
19060impl<'r> ::core::fmt::Display for GetTransactionsProofReader<'r> {
19061 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19062 write!(f, "{} {{ ", Self::NAME)?;
19063 write!(f, "{}: {}", "last_hash", self.last_hash())?;
19064 write!(f, ", {}: {}", "tx_hashes", self.tx_hashes())?;
19065 let extra_count = self.count_extra_fields();
19066 if extra_count != 0 {
19067 write!(f, ", .. ({} fields)", extra_count)?;
19068 }
19069 write!(f, " }}")
19070 }
19071}
19072impl<'r> GetTransactionsProofReader<'r> {
19073 pub const FIELD_COUNT: usize = 2;
19074 pub fn total_size(&self) -> usize {
19075 molecule::unpack_number(self.as_slice()) as usize
19076 }
19077 pub fn field_count(&self) -> usize {
19078 if self.total_size() == molecule::NUMBER_SIZE {
19079 0
19080 } else {
19081 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19082 }
19083 }
19084 pub fn count_extra_fields(&self) -> usize {
19085 self.field_count() - Self::FIELD_COUNT
19086 }
19087 pub fn has_extra_fields(&self) -> bool {
19088 Self::FIELD_COUNT != self.field_count()
19089 }
19090 pub fn last_hash(&self) -> Byte32Reader<'r> {
19091 let slice = self.as_slice();
19092 let start = molecule::unpack_number(&slice[4..]) as usize;
19093 let end = molecule::unpack_number(&slice[8..]) as usize;
19094 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
19095 }
19096 pub fn tx_hashes(&self) -> Byte32VecReader<'r> {
19097 let slice = self.as_slice();
19098 let start = molecule::unpack_number(&slice[8..]) as usize;
19099 if self.has_extra_fields() {
19100 let end = molecule::unpack_number(&slice[12..]) as usize;
19101 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
19102 } else {
19103 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
19104 }
19105 }
19106}
19107impl<'r> molecule::prelude::Reader<'r> for GetTransactionsProofReader<'r> {
19108 type Entity = GetTransactionsProof;
19109 const NAME: &'static str = "GetTransactionsProofReader";
19110 fn to_entity(&self) -> Self::Entity {
19111 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
19112 }
19113 fn new_unchecked(slice: &'r [u8]) -> Self {
19114 GetTransactionsProofReader(slice)
19115 }
19116 fn as_slice(&self) -> &'r [u8] {
19117 self.0
19118 }
19119 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
19120 use molecule::verification_error as ve;
19121 let slice_len = slice.len();
19122 if slice_len < molecule::NUMBER_SIZE {
19123 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
19124 }
19125 let total_size = molecule::unpack_number(slice) as usize;
19126 if slice_len != total_size {
19127 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
19128 }
19129 if slice_len < molecule::NUMBER_SIZE * 2 {
19130 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
19131 }
19132 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
19133 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
19134 return ve!(Self, OffsetsNotMatch);
19135 }
19136 if slice_len < offset_first {
19137 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
19138 }
19139 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
19140 if field_count < Self::FIELD_COUNT {
19141 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19142 } else if !compatible && field_count > Self::FIELD_COUNT {
19143 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19144 };
19145 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
19146 .chunks_exact(molecule::NUMBER_SIZE)
19147 .map(|x| molecule::unpack_number(x) as usize)
19148 .collect();
19149 offsets.push(total_size);
19150 if offsets.windows(2).any(|i| i[0] > i[1]) {
19151 return ve!(Self, OffsetsNotMatch);
19152 }
19153 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
19154 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
19155 Ok(())
19156 }
19157}
19158#[derive(Clone, Debug, Default)]
19159pub struct GetTransactionsProofBuilder {
19160 pub(crate) last_hash: Byte32,
19161 pub(crate) tx_hashes: Byte32Vec,
19162}
19163impl GetTransactionsProofBuilder {
19164 pub const FIELD_COUNT: usize = 2;
19165 pub fn last_hash<T>(mut self, v: T) -> Self
19166 where
19167 T: ::core::convert::Into<Byte32>,
19168 {
19169 self.last_hash = v.into();
19170 self
19171 }
19172 pub fn tx_hashes<T>(mut self, v: T) -> Self
19173 where
19174 T: ::core::convert::Into<Byte32Vec>,
19175 {
19176 self.tx_hashes = v.into();
19177 self
19178 }
19179}
19180impl molecule::prelude::Builder for GetTransactionsProofBuilder {
19181 type Entity = GetTransactionsProof;
19182 const NAME: &'static str = "GetTransactionsProofBuilder";
19183 fn expected_length(&self) -> usize {
19184 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
19185 + self.last_hash.as_slice().len()
19186 + self.tx_hashes.as_slice().len()
19187 }
19188 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
19189 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
19190 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
19191 offsets.push(total_size);
19192 total_size += self.last_hash.as_slice().len();
19193 offsets.push(total_size);
19194 total_size += self.tx_hashes.as_slice().len();
19195 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
19196 for offset in offsets.into_iter() {
19197 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
19198 }
19199 writer.write_all(self.last_hash.as_slice())?;
19200 writer.write_all(self.tx_hashes.as_slice())?;
19201 Ok(())
19202 }
19203 fn build(&self) -> Self::Entity {
19204 let mut inner = Vec::with_capacity(self.expected_length());
19205 self.write(&mut inner)
19206 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
19207 GetTransactionsProof::new_unchecked(inner.into())
19208 }
19209}
19210#[derive(Clone)]
19211pub struct SendTransactionsProof(molecule::bytes::Bytes);
19212impl ::core::fmt::LowerHex for SendTransactionsProof {
19213 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19214 use molecule::hex_string;
19215 if f.alternate() {
19216 write!(f, "0x")?;
19217 }
19218 write!(f, "{}", hex_string(self.as_slice()))
19219 }
19220}
19221impl ::core::fmt::Debug for SendTransactionsProof {
19222 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19223 write!(f, "{}({:#x})", Self::NAME, self)
19224 }
19225}
19226impl ::core::fmt::Display for SendTransactionsProof {
19227 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19228 write!(f, "{} {{ ", Self::NAME)?;
19229 write!(f, "{}: {}", "last_header", self.last_header())?;
19230 write!(f, ", {}: {}", "proof", self.proof())?;
19231 write!(f, ", {}: {}", "filtered_blocks", self.filtered_blocks())?;
19232 write!(f, ", {}: {}", "missing_tx_hashes", self.missing_tx_hashes())?;
19233 let extra_count = self.count_extra_fields();
19234 if extra_count != 0 {
19235 write!(f, ", .. ({} fields)", extra_count)?;
19236 }
19237 write!(f, " }}")
19238 }
19239}
19240impl ::core::default::Default for SendTransactionsProof {
19241 fn default() -> Self {
19242 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
19243 SendTransactionsProof::new_unchecked(v)
19244 }
19245}
19246impl SendTransactionsProof {
19247 const DEFAULT_VALUE: [u8; 412] = [
19248 156, 1, 0, 0, 20, 0, 0, 0, 144, 1, 0, 0, 148, 1, 0, 0, 152, 1, 0, 0, 124, 1, 0, 0, 20, 0,
19249 0, 0, 228, 0, 0, 0, 4, 1, 0, 0, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19250 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19251 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19252 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19254 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19255 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19259 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19261 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
19262 ];
19263 pub const FIELD_COUNT: usize = 4;
19264 pub fn total_size(&self) -> usize {
19265 molecule::unpack_number(self.as_slice()) as usize
19266 }
19267 pub fn field_count(&self) -> usize {
19268 if self.total_size() == molecule::NUMBER_SIZE {
19269 0
19270 } else {
19271 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19272 }
19273 }
19274 pub fn count_extra_fields(&self) -> usize {
19275 self.field_count() - Self::FIELD_COUNT
19276 }
19277 pub fn has_extra_fields(&self) -> bool {
19278 Self::FIELD_COUNT != self.field_count()
19279 }
19280 pub fn last_header(&self) -> VerifiableHeader {
19281 let slice = self.as_slice();
19282 let start = molecule::unpack_number(&slice[4..]) as usize;
19283 let end = molecule::unpack_number(&slice[8..]) as usize;
19284 VerifiableHeader::new_unchecked(self.0.slice(start..end))
19285 }
19286 pub fn proof(&self) -> HeaderDigestVec {
19287 let slice = self.as_slice();
19288 let start = molecule::unpack_number(&slice[8..]) as usize;
19289 let end = molecule::unpack_number(&slice[12..]) as usize;
19290 HeaderDigestVec::new_unchecked(self.0.slice(start..end))
19291 }
19292 pub fn filtered_blocks(&self) -> FilteredBlockVec {
19293 let slice = self.as_slice();
19294 let start = molecule::unpack_number(&slice[12..]) as usize;
19295 let end = molecule::unpack_number(&slice[16..]) as usize;
19296 FilteredBlockVec::new_unchecked(self.0.slice(start..end))
19297 }
19298 pub fn missing_tx_hashes(&self) -> Byte32Vec {
19299 let slice = self.as_slice();
19300 let start = molecule::unpack_number(&slice[16..]) as usize;
19301 if self.has_extra_fields() {
19302 let end = molecule::unpack_number(&slice[20..]) as usize;
19303 Byte32Vec::new_unchecked(self.0.slice(start..end))
19304 } else {
19305 Byte32Vec::new_unchecked(self.0.slice(start..))
19306 }
19307 }
19308 pub fn as_reader<'r>(&'r self) -> SendTransactionsProofReader<'r> {
19309 SendTransactionsProofReader::new_unchecked(self.as_slice())
19310 }
19311}
19312impl molecule::prelude::Entity for SendTransactionsProof {
19313 type Builder = SendTransactionsProofBuilder;
19314 const NAME: &'static str = "SendTransactionsProof";
19315 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
19316 SendTransactionsProof(data)
19317 }
19318 fn as_bytes(&self) -> molecule::bytes::Bytes {
19319 self.0.clone()
19320 }
19321 fn as_slice(&self) -> &[u8] {
19322 &self.0[..]
19323 }
19324 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19325 SendTransactionsProofReader::from_slice(slice).map(|reader| reader.to_entity())
19326 }
19327 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19328 SendTransactionsProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
19329 }
19330 fn new_builder() -> Self::Builder {
19331 ::core::default::Default::default()
19332 }
19333 fn as_builder(self) -> Self::Builder {
19334 Self::new_builder()
19335 .last_header(self.last_header())
19336 .proof(self.proof())
19337 .filtered_blocks(self.filtered_blocks())
19338 .missing_tx_hashes(self.missing_tx_hashes())
19339 }
19340}
19341#[derive(Clone, Copy)]
19342pub struct SendTransactionsProofReader<'r>(&'r [u8]);
19343impl<'r> ::core::fmt::LowerHex for SendTransactionsProofReader<'r> {
19344 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19345 use molecule::hex_string;
19346 if f.alternate() {
19347 write!(f, "0x")?;
19348 }
19349 write!(f, "{}", hex_string(self.as_slice()))
19350 }
19351}
19352impl<'r> ::core::fmt::Debug for SendTransactionsProofReader<'r> {
19353 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19354 write!(f, "{}({:#x})", Self::NAME, self)
19355 }
19356}
19357impl<'r> ::core::fmt::Display for SendTransactionsProofReader<'r> {
19358 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19359 write!(f, "{} {{ ", Self::NAME)?;
19360 write!(f, "{}: {}", "last_header", self.last_header())?;
19361 write!(f, ", {}: {}", "proof", self.proof())?;
19362 write!(f, ", {}: {}", "filtered_blocks", self.filtered_blocks())?;
19363 write!(f, ", {}: {}", "missing_tx_hashes", self.missing_tx_hashes())?;
19364 let extra_count = self.count_extra_fields();
19365 if extra_count != 0 {
19366 write!(f, ", .. ({} fields)", extra_count)?;
19367 }
19368 write!(f, " }}")
19369 }
19370}
19371impl<'r> SendTransactionsProofReader<'r> {
19372 pub const FIELD_COUNT: usize = 4;
19373 pub fn total_size(&self) -> usize {
19374 molecule::unpack_number(self.as_slice()) as usize
19375 }
19376 pub fn field_count(&self) -> usize {
19377 if self.total_size() == molecule::NUMBER_SIZE {
19378 0
19379 } else {
19380 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19381 }
19382 }
19383 pub fn count_extra_fields(&self) -> usize {
19384 self.field_count() - Self::FIELD_COUNT
19385 }
19386 pub fn has_extra_fields(&self) -> bool {
19387 Self::FIELD_COUNT != self.field_count()
19388 }
19389 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
19390 let slice = self.as_slice();
19391 let start = molecule::unpack_number(&slice[4..]) as usize;
19392 let end = molecule::unpack_number(&slice[8..]) as usize;
19393 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
19394 }
19395 pub fn proof(&self) -> HeaderDigestVecReader<'r> {
19396 let slice = self.as_slice();
19397 let start = molecule::unpack_number(&slice[8..]) as usize;
19398 let end = molecule::unpack_number(&slice[12..]) as usize;
19399 HeaderDigestVecReader::new_unchecked(&self.as_slice()[start..end])
19400 }
19401 pub fn filtered_blocks(&self) -> FilteredBlockVecReader<'r> {
19402 let slice = self.as_slice();
19403 let start = molecule::unpack_number(&slice[12..]) as usize;
19404 let end = molecule::unpack_number(&slice[16..]) as usize;
19405 FilteredBlockVecReader::new_unchecked(&self.as_slice()[start..end])
19406 }
19407 pub fn missing_tx_hashes(&self) -> Byte32VecReader<'r> {
19408 let slice = self.as_slice();
19409 let start = molecule::unpack_number(&slice[16..]) as usize;
19410 if self.has_extra_fields() {
19411 let end = molecule::unpack_number(&slice[20..]) as usize;
19412 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
19413 } else {
19414 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
19415 }
19416 }
19417}
19418impl<'r> molecule::prelude::Reader<'r> for SendTransactionsProofReader<'r> {
19419 type Entity = SendTransactionsProof;
19420 const NAME: &'static str = "SendTransactionsProofReader";
19421 fn to_entity(&self) -> Self::Entity {
19422 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
19423 }
19424 fn new_unchecked(slice: &'r [u8]) -> Self {
19425 SendTransactionsProofReader(slice)
19426 }
19427 fn as_slice(&self) -> &'r [u8] {
19428 self.0
19429 }
19430 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
19431 use molecule::verification_error as ve;
19432 let slice_len = slice.len();
19433 if slice_len < molecule::NUMBER_SIZE {
19434 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
19435 }
19436 let total_size = molecule::unpack_number(slice) as usize;
19437 if slice_len != total_size {
19438 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
19439 }
19440 if slice_len < molecule::NUMBER_SIZE * 2 {
19441 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
19442 }
19443 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
19444 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
19445 return ve!(Self, OffsetsNotMatch);
19446 }
19447 if slice_len < offset_first {
19448 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
19449 }
19450 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
19451 if field_count < Self::FIELD_COUNT {
19452 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19453 } else if !compatible && field_count > Self::FIELD_COUNT {
19454 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19455 };
19456 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
19457 .chunks_exact(molecule::NUMBER_SIZE)
19458 .map(|x| molecule::unpack_number(x) as usize)
19459 .collect();
19460 offsets.push(total_size);
19461 if offsets.windows(2).any(|i| i[0] > i[1]) {
19462 return ve!(Self, OffsetsNotMatch);
19463 }
19464 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
19465 HeaderDigestVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
19466 FilteredBlockVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
19467 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
19468 Ok(())
19469 }
19470}
19471#[derive(Clone, Debug, Default)]
19472pub struct SendTransactionsProofBuilder {
19473 pub(crate) last_header: VerifiableHeader,
19474 pub(crate) proof: HeaderDigestVec,
19475 pub(crate) filtered_blocks: FilteredBlockVec,
19476 pub(crate) missing_tx_hashes: Byte32Vec,
19477}
19478impl SendTransactionsProofBuilder {
19479 pub const FIELD_COUNT: usize = 4;
19480 pub fn last_header<T>(mut self, v: T) -> Self
19481 where
19482 T: ::core::convert::Into<VerifiableHeader>,
19483 {
19484 self.last_header = v.into();
19485 self
19486 }
19487 pub fn proof<T>(mut self, v: T) -> Self
19488 where
19489 T: ::core::convert::Into<HeaderDigestVec>,
19490 {
19491 self.proof = v.into();
19492 self
19493 }
19494 pub fn filtered_blocks<T>(mut self, v: T) -> Self
19495 where
19496 T: ::core::convert::Into<FilteredBlockVec>,
19497 {
19498 self.filtered_blocks = v.into();
19499 self
19500 }
19501 pub fn missing_tx_hashes<T>(mut self, v: T) -> Self
19502 where
19503 T: ::core::convert::Into<Byte32Vec>,
19504 {
19505 self.missing_tx_hashes = v.into();
19506 self
19507 }
19508}
19509impl molecule::prelude::Builder for SendTransactionsProofBuilder {
19510 type Entity = SendTransactionsProof;
19511 const NAME: &'static str = "SendTransactionsProofBuilder";
19512 fn expected_length(&self) -> usize {
19513 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
19514 + self.last_header.as_slice().len()
19515 + self.proof.as_slice().len()
19516 + self.filtered_blocks.as_slice().len()
19517 + self.missing_tx_hashes.as_slice().len()
19518 }
19519 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
19520 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
19521 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
19522 offsets.push(total_size);
19523 total_size += self.last_header.as_slice().len();
19524 offsets.push(total_size);
19525 total_size += self.proof.as_slice().len();
19526 offsets.push(total_size);
19527 total_size += self.filtered_blocks.as_slice().len();
19528 offsets.push(total_size);
19529 total_size += self.missing_tx_hashes.as_slice().len();
19530 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
19531 for offset in offsets.into_iter() {
19532 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
19533 }
19534 writer.write_all(self.last_header.as_slice())?;
19535 writer.write_all(self.proof.as_slice())?;
19536 writer.write_all(self.filtered_blocks.as_slice())?;
19537 writer.write_all(self.missing_tx_hashes.as_slice())?;
19538 Ok(())
19539 }
19540 fn build(&self) -> Self::Entity {
19541 let mut inner = Vec::with_capacity(self.expected_length());
19542 self.write(&mut inner)
19543 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
19544 SendTransactionsProof::new_unchecked(inner.into())
19545 }
19546}
19547#[derive(Clone)]
19548pub struct SendTransactionsProofV1(molecule::bytes::Bytes);
19549impl ::core::fmt::LowerHex for SendTransactionsProofV1 {
19550 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19551 use molecule::hex_string;
19552 if f.alternate() {
19553 write!(f, "0x")?;
19554 }
19555 write!(f, "{}", hex_string(self.as_slice()))
19556 }
19557}
19558impl ::core::fmt::Debug for SendTransactionsProofV1 {
19559 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19560 write!(f, "{}({:#x})", Self::NAME, self)
19561 }
19562}
19563impl ::core::fmt::Display for SendTransactionsProofV1 {
19564 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19565 write!(f, "{} {{ ", Self::NAME)?;
19566 write!(f, "{}: {}", "last_header", self.last_header())?;
19567 write!(f, ", {}: {}", "proof", self.proof())?;
19568 write!(f, ", {}: {}", "filtered_blocks", self.filtered_blocks())?;
19569 write!(f, ", {}: {}", "missing_tx_hashes", self.missing_tx_hashes())?;
19570 write!(
19571 f,
19572 ", {}: {}",
19573 "blocks_uncles_hash",
19574 self.blocks_uncles_hash()
19575 )?;
19576 write!(f, ", {}: {}", "blocks_extension", self.blocks_extension())?;
19577 let extra_count = self.count_extra_fields();
19578 if extra_count != 0 {
19579 write!(f, ", .. ({} fields)", extra_count)?;
19580 }
19581 write!(f, " }}")
19582 }
19583}
19584impl ::core::default::Default for SendTransactionsProofV1 {
19585 fn default() -> Self {
19586 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
19587 SendTransactionsProofV1::new_unchecked(v)
19588 }
19589}
19590impl SendTransactionsProofV1 {
19591 const DEFAULT_VALUE: [u8; 428] = [
19592 172, 1, 0, 0, 28, 0, 0, 0, 152, 1, 0, 0, 156, 1, 0, 0, 160, 1, 0, 0, 164, 1, 0, 0, 168, 1,
19593 0, 0, 124, 1, 0, 0, 20, 0, 0, 0, 228, 0, 0, 0, 4, 1, 0, 0, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19594 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19595 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19596 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19597 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19598 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19599 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19600 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19601 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19602 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19603 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19604 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19605 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0,
19606 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
19607 ];
19608 pub const FIELD_COUNT: usize = 6;
19609 pub fn total_size(&self) -> usize {
19610 molecule::unpack_number(self.as_slice()) as usize
19611 }
19612 pub fn field_count(&self) -> usize {
19613 if self.total_size() == molecule::NUMBER_SIZE {
19614 0
19615 } else {
19616 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19617 }
19618 }
19619 pub fn count_extra_fields(&self) -> usize {
19620 self.field_count() - Self::FIELD_COUNT
19621 }
19622 pub fn has_extra_fields(&self) -> bool {
19623 Self::FIELD_COUNT != self.field_count()
19624 }
19625 pub fn last_header(&self) -> VerifiableHeader {
19626 let slice = self.as_slice();
19627 let start = molecule::unpack_number(&slice[4..]) as usize;
19628 let end = molecule::unpack_number(&slice[8..]) as usize;
19629 VerifiableHeader::new_unchecked(self.0.slice(start..end))
19630 }
19631 pub fn proof(&self) -> HeaderDigestVec {
19632 let slice = self.as_slice();
19633 let start = molecule::unpack_number(&slice[8..]) as usize;
19634 let end = molecule::unpack_number(&slice[12..]) as usize;
19635 HeaderDigestVec::new_unchecked(self.0.slice(start..end))
19636 }
19637 pub fn filtered_blocks(&self) -> FilteredBlockVec {
19638 let slice = self.as_slice();
19639 let start = molecule::unpack_number(&slice[12..]) as usize;
19640 let end = molecule::unpack_number(&slice[16..]) as usize;
19641 FilteredBlockVec::new_unchecked(self.0.slice(start..end))
19642 }
19643 pub fn missing_tx_hashes(&self) -> Byte32Vec {
19644 let slice = self.as_slice();
19645 let start = molecule::unpack_number(&slice[16..]) as usize;
19646 let end = molecule::unpack_number(&slice[20..]) as usize;
19647 Byte32Vec::new_unchecked(self.0.slice(start..end))
19648 }
19649 pub fn blocks_uncles_hash(&self) -> Byte32Vec {
19650 let slice = self.as_slice();
19651 let start = molecule::unpack_number(&slice[20..]) as usize;
19652 let end = molecule::unpack_number(&slice[24..]) as usize;
19653 Byte32Vec::new_unchecked(self.0.slice(start..end))
19654 }
19655 pub fn blocks_extension(&self) -> BytesOptVec {
19656 let slice = self.as_slice();
19657 let start = molecule::unpack_number(&slice[24..]) as usize;
19658 if self.has_extra_fields() {
19659 let end = molecule::unpack_number(&slice[28..]) as usize;
19660 BytesOptVec::new_unchecked(self.0.slice(start..end))
19661 } else {
19662 BytesOptVec::new_unchecked(self.0.slice(start..))
19663 }
19664 }
19665 pub fn as_reader<'r>(&'r self) -> SendTransactionsProofV1Reader<'r> {
19666 SendTransactionsProofV1Reader::new_unchecked(self.as_slice())
19667 }
19668}
19669impl molecule::prelude::Entity for SendTransactionsProofV1 {
19670 type Builder = SendTransactionsProofV1Builder;
19671 const NAME: &'static str = "SendTransactionsProofV1";
19672 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
19673 SendTransactionsProofV1(data)
19674 }
19675 fn as_bytes(&self) -> molecule::bytes::Bytes {
19676 self.0.clone()
19677 }
19678 fn as_slice(&self) -> &[u8] {
19679 &self.0[..]
19680 }
19681 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19682 SendTransactionsProofV1Reader::from_slice(slice).map(|reader| reader.to_entity())
19683 }
19684 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19685 SendTransactionsProofV1Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
19686 }
19687 fn new_builder() -> Self::Builder {
19688 ::core::default::Default::default()
19689 }
19690 fn as_builder(self) -> Self::Builder {
19691 Self::new_builder()
19692 .last_header(self.last_header())
19693 .proof(self.proof())
19694 .filtered_blocks(self.filtered_blocks())
19695 .missing_tx_hashes(self.missing_tx_hashes())
19696 .blocks_uncles_hash(self.blocks_uncles_hash())
19697 .blocks_extension(self.blocks_extension())
19698 }
19699}
19700#[derive(Clone, Copy)]
19701pub struct SendTransactionsProofV1Reader<'r>(&'r [u8]);
19702impl<'r> ::core::fmt::LowerHex for SendTransactionsProofV1Reader<'r> {
19703 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19704 use molecule::hex_string;
19705 if f.alternate() {
19706 write!(f, "0x")?;
19707 }
19708 write!(f, "{}", hex_string(self.as_slice()))
19709 }
19710}
19711impl<'r> ::core::fmt::Debug for SendTransactionsProofV1Reader<'r> {
19712 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19713 write!(f, "{}({:#x})", Self::NAME, self)
19714 }
19715}
19716impl<'r> ::core::fmt::Display for SendTransactionsProofV1Reader<'r> {
19717 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19718 write!(f, "{} {{ ", Self::NAME)?;
19719 write!(f, "{}: {}", "last_header", self.last_header())?;
19720 write!(f, ", {}: {}", "proof", self.proof())?;
19721 write!(f, ", {}: {}", "filtered_blocks", self.filtered_blocks())?;
19722 write!(f, ", {}: {}", "missing_tx_hashes", self.missing_tx_hashes())?;
19723 write!(
19724 f,
19725 ", {}: {}",
19726 "blocks_uncles_hash",
19727 self.blocks_uncles_hash()
19728 )?;
19729 write!(f, ", {}: {}", "blocks_extension", self.blocks_extension())?;
19730 let extra_count = self.count_extra_fields();
19731 if extra_count != 0 {
19732 write!(f, ", .. ({} fields)", extra_count)?;
19733 }
19734 write!(f, " }}")
19735 }
19736}
19737impl<'r> SendTransactionsProofV1Reader<'r> {
19738 pub const FIELD_COUNT: usize = 6;
19739 pub fn total_size(&self) -> usize {
19740 molecule::unpack_number(self.as_slice()) as usize
19741 }
19742 pub fn field_count(&self) -> usize {
19743 if self.total_size() == molecule::NUMBER_SIZE {
19744 0
19745 } else {
19746 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19747 }
19748 }
19749 pub fn count_extra_fields(&self) -> usize {
19750 self.field_count() - Self::FIELD_COUNT
19751 }
19752 pub fn has_extra_fields(&self) -> bool {
19753 Self::FIELD_COUNT != self.field_count()
19754 }
19755 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
19756 let slice = self.as_slice();
19757 let start = molecule::unpack_number(&slice[4..]) as usize;
19758 let end = molecule::unpack_number(&slice[8..]) as usize;
19759 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
19760 }
19761 pub fn proof(&self) -> HeaderDigestVecReader<'r> {
19762 let slice = self.as_slice();
19763 let start = molecule::unpack_number(&slice[8..]) as usize;
19764 let end = molecule::unpack_number(&slice[12..]) as usize;
19765 HeaderDigestVecReader::new_unchecked(&self.as_slice()[start..end])
19766 }
19767 pub fn filtered_blocks(&self) -> FilteredBlockVecReader<'r> {
19768 let slice = self.as_slice();
19769 let start = molecule::unpack_number(&slice[12..]) as usize;
19770 let end = molecule::unpack_number(&slice[16..]) as usize;
19771 FilteredBlockVecReader::new_unchecked(&self.as_slice()[start..end])
19772 }
19773 pub fn missing_tx_hashes(&self) -> Byte32VecReader<'r> {
19774 let slice = self.as_slice();
19775 let start = molecule::unpack_number(&slice[16..]) as usize;
19776 let end = molecule::unpack_number(&slice[20..]) as usize;
19777 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
19778 }
19779 pub fn blocks_uncles_hash(&self) -> Byte32VecReader<'r> {
19780 let slice = self.as_slice();
19781 let start = molecule::unpack_number(&slice[20..]) as usize;
19782 let end = molecule::unpack_number(&slice[24..]) as usize;
19783 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
19784 }
19785 pub fn blocks_extension(&self) -> BytesOptVecReader<'r> {
19786 let slice = self.as_slice();
19787 let start = molecule::unpack_number(&slice[24..]) as usize;
19788 if self.has_extra_fields() {
19789 let end = molecule::unpack_number(&slice[28..]) as usize;
19790 BytesOptVecReader::new_unchecked(&self.as_slice()[start..end])
19791 } else {
19792 BytesOptVecReader::new_unchecked(&self.as_slice()[start..])
19793 }
19794 }
19795}
19796impl<'r> molecule::prelude::Reader<'r> for SendTransactionsProofV1Reader<'r> {
19797 type Entity = SendTransactionsProofV1;
19798 const NAME: &'static str = "SendTransactionsProofV1Reader";
19799 fn to_entity(&self) -> Self::Entity {
19800 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
19801 }
19802 fn new_unchecked(slice: &'r [u8]) -> Self {
19803 SendTransactionsProofV1Reader(slice)
19804 }
19805 fn as_slice(&self) -> &'r [u8] {
19806 self.0
19807 }
19808 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
19809 use molecule::verification_error as ve;
19810 let slice_len = slice.len();
19811 if slice_len < molecule::NUMBER_SIZE {
19812 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
19813 }
19814 let total_size = molecule::unpack_number(slice) as usize;
19815 if slice_len != total_size {
19816 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
19817 }
19818 if slice_len < molecule::NUMBER_SIZE * 2 {
19819 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
19820 }
19821 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
19822 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
19823 return ve!(Self, OffsetsNotMatch);
19824 }
19825 if slice_len < offset_first {
19826 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
19827 }
19828 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
19829 if field_count < Self::FIELD_COUNT {
19830 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19831 } else if !compatible && field_count > Self::FIELD_COUNT {
19832 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19833 };
19834 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
19835 .chunks_exact(molecule::NUMBER_SIZE)
19836 .map(|x| molecule::unpack_number(x) as usize)
19837 .collect();
19838 offsets.push(total_size);
19839 if offsets.windows(2).any(|i| i[0] > i[1]) {
19840 return ve!(Self, OffsetsNotMatch);
19841 }
19842 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
19843 HeaderDigestVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
19844 FilteredBlockVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
19845 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
19846 Byte32VecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
19847 BytesOptVecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
19848 Ok(())
19849 }
19850}
19851#[derive(Clone, Debug, Default)]
19852pub struct SendTransactionsProofV1Builder {
19853 pub(crate) last_header: VerifiableHeader,
19854 pub(crate) proof: HeaderDigestVec,
19855 pub(crate) filtered_blocks: FilteredBlockVec,
19856 pub(crate) missing_tx_hashes: Byte32Vec,
19857 pub(crate) blocks_uncles_hash: Byte32Vec,
19858 pub(crate) blocks_extension: BytesOptVec,
19859}
19860impl SendTransactionsProofV1Builder {
19861 pub const FIELD_COUNT: usize = 6;
19862 pub fn last_header<T>(mut self, v: T) -> Self
19863 where
19864 T: ::core::convert::Into<VerifiableHeader>,
19865 {
19866 self.last_header = v.into();
19867 self
19868 }
19869 pub fn proof<T>(mut self, v: T) -> Self
19870 where
19871 T: ::core::convert::Into<HeaderDigestVec>,
19872 {
19873 self.proof = v.into();
19874 self
19875 }
19876 pub fn filtered_blocks<T>(mut self, v: T) -> Self
19877 where
19878 T: ::core::convert::Into<FilteredBlockVec>,
19879 {
19880 self.filtered_blocks = v.into();
19881 self
19882 }
19883 pub fn missing_tx_hashes<T>(mut self, v: T) -> Self
19884 where
19885 T: ::core::convert::Into<Byte32Vec>,
19886 {
19887 self.missing_tx_hashes = v.into();
19888 self
19889 }
19890 pub fn blocks_uncles_hash<T>(mut self, v: T) -> Self
19891 where
19892 T: ::core::convert::Into<Byte32Vec>,
19893 {
19894 self.blocks_uncles_hash = v.into();
19895 self
19896 }
19897 pub fn blocks_extension<T>(mut self, v: T) -> Self
19898 where
19899 T: ::core::convert::Into<BytesOptVec>,
19900 {
19901 self.blocks_extension = v.into();
19902 self
19903 }
19904}
19905impl molecule::prelude::Builder for SendTransactionsProofV1Builder {
19906 type Entity = SendTransactionsProofV1;
19907 const NAME: &'static str = "SendTransactionsProofV1Builder";
19908 fn expected_length(&self) -> usize {
19909 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
19910 + self.last_header.as_slice().len()
19911 + self.proof.as_slice().len()
19912 + self.filtered_blocks.as_slice().len()
19913 + self.missing_tx_hashes.as_slice().len()
19914 + self.blocks_uncles_hash.as_slice().len()
19915 + self.blocks_extension.as_slice().len()
19916 }
19917 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
19918 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
19919 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
19920 offsets.push(total_size);
19921 total_size += self.last_header.as_slice().len();
19922 offsets.push(total_size);
19923 total_size += self.proof.as_slice().len();
19924 offsets.push(total_size);
19925 total_size += self.filtered_blocks.as_slice().len();
19926 offsets.push(total_size);
19927 total_size += self.missing_tx_hashes.as_slice().len();
19928 offsets.push(total_size);
19929 total_size += self.blocks_uncles_hash.as_slice().len();
19930 offsets.push(total_size);
19931 total_size += self.blocks_extension.as_slice().len();
19932 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
19933 for offset in offsets.into_iter() {
19934 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
19935 }
19936 writer.write_all(self.last_header.as_slice())?;
19937 writer.write_all(self.proof.as_slice())?;
19938 writer.write_all(self.filtered_blocks.as_slice())?;
19939 writer.write_all(self.missing_tx_hashes.as_slice())?;
19940 writer.write_all(self.blocks_uncles_hash.as_slice())?;
19941 writer.write_all(self.blocks_extension.as_slice())?;
19942 Ok(())
19943 }
19944 fn build(&self) -> Self::Entity {
19945 let mut inner = Vec::with_capacity(self.expected_length());
19946 self.write(&mut inner)
19947 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
19948 SendTransactionsProofV1::new_unchecked(inner.into())
19949 }
19950}
19951#[derive(Clone)]
19952pub struct Time(molecule::bytes::Bytes);
19953impl ::core::fmt::LowerHex for Time {
19954 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19955 use molecule::hex_string;
19956 if f.alternate() {
19957 write!(f, "0x")?;
19958 }
19959 write!(f, "{}", hex_string(self.as_slice()))
19960 }
19961}
19962impl ::core::fmt::Debug for Time {
19963 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19964 write!(f, "{}({:#x})", Self::NAME, self)
19965 }
19966}
19967impl ::core::fmt::Display for Time {
19968 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19969 write!(f, "{} {{ ", Self::NAME)?;
19970 write!(f, "{}: {}", "timestamp", self.timestamp())?;
19971 let extra_count = self.count_extra_fields();
19972 if extra_count != 0 {
19973 write!(f, ", .. ({} fields)", extra_count)?;
19974 }
19975 write!(f, " }}")
19976 }
19977}
19978impl ::core::default::Default for Time {
19979 fn default() -> Self {
19980 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
19981 Time::new_unchecked(v)
19982 }
19983}
19984impl Time {
19985 const DEFAULT_VALUE: [u8; 16] = [16, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
19986 pub const FIELD_COUNT: usize = 1;
19987 pub fn total_size(&self) -> usize {
19988 molecule::unpack_number(self.as_slice()) as usize
19989 }
19990 pub fn field_count(&self) -> usize {
19991 if self.total_size() == molecule::NUMBER_SIZE {
19992 0
19993 } else {
19994 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19995 }
19996 }
19997 pub fn count_extra_fields(&self) -> usize {
19998 self.field_count() - Self::FIELD_COUNT
19999 }
20000 pub fn has_extra_fields(&self) -> bool {
20001 Self::FIELD_COUNT != self.field_count()
20002 }
20003 pub fn timestamp(&self) -> Uint64 {
20004 let slice = self.as_slice();
20005 let start = molecule::unpack_number(&slice[4..]) as usize;
20006 if self.has_extra_fields() {
20007 let end = molecule::unpack_number(&slice[8..]) as usize;
20008 Uint64::new_unchecked(self.0.slice(start..end))
20009 } else {
20010 Uint64::new_unchecked(self.0.slice(start..))
20011 }
20012 }
20013 pub fn as_reader<'r>(&'r self) -> TimeReader<'r> {
20014 TimeReader::new_unchecked(self.as_slice())
20015 }
20016}
20017impl molecule::prelude::Entity for Time {
20018 type Builder = TimeBuilder;
20019 const NAME: &'static str = "Time";
20020 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
20021 Time(data)
20022 }
20023 fn as_bytes(&self) -> molecule::bytes::Bytes {
20024 self.0.clone()
20025 }
20026 fn as_slice(&self) -> &[u8] {
20027 &self.0[..]
20028 }
20029 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
20030 TimeReader::from_slice(slice).map(|reader| reader.to_entity())
20031 }
20032 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
20033 TimeReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
20034 }
20035 fn new_builder() -> Self::Builder {
20036 ::core::default::Default::default()
20037 }
20038 fn as_builder(self) -> Self::Builder {
20039 Self::new_builder().timestamp(self.timestamp())
20040 }
20041}
20042#[derive(Clone, Copy)]
20043pub struct TimeReader<'r>(&'r [u8]);
20044impl<'r> ::core::fmt::LowerHex for TimeReader<'r> {
20045 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20046 use molecule::hex_string;
20047 if f.alternate() {
20048 write!(f, "0x")?;
20049 }
20050 write!(f, "{}", hex_string(self.as_slice()))
20051 }
20052}
20053impl<'r> ::core::fmt::Debug for TimeReader<'r> {
20054 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20055 write!(f, "{}({:#x})", Self::NAME, self)
20056 }
20057}
20058impl<'r> ::core::fmt::Display for TimeReader<'r> {
20059 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20060 write!(f, "{} {{ ", Self::NAME)?;
20061 write!(f, "{}: {}", "timestamp", self.timestamp())?;
20062 let extra_count = self.count_extra_fields();
20063 if extra_count != 0 {
20064 write!(f, ", .. ({} fields)", extra_count)?;
20065 }
20066 write!(f, " }}")
20067 }
20068}
20069impl<'r> TimeReader<'r> {
20070 pub const FIELD_COUNT: usize = 1;
20071 pub fn total_size(&self) -> usize {
20072 molecule::unpack_number(self.as_slice()) as usize
20073 }
20074 pub fn field_count(&self) -> usize {
20075 if self.total_size() == molecule::NUMBER_SIZE {
20076 0
20077 } else {
20078 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
20079 }
20080 }
20081 pub fn count_extra_fields(&self) -> usize {
20082 self.field_count() - Self::FIELD_COUNT
20083 }
20084 pub fn has_extra_fields(&self) -> bool {
20085 Self::FIELD_COUNT != self.field_count()
20086 }
20087 pub fn timestamp(&self) -> Uint64Reader<'r> {
20088 let slice = self.as_slice();
20089 let start = molecule::unpack_number(&slice[4..]) as usize;
20090 if self.has_extra_fields() {
20091 let end = molecule::unpack_number(&slice[8..]) as usize;
20092 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
20093 } else {
20094 Uint64Reader::new_unchecked(&self.as_slice()[start..])
20095 }
20096 }
20097}
20098impl<'r> molecule::prelude::Reader<'r> for TimeReader<'r> {
20099 type Entity = Time;
20100 const NAME: &'static str = "TimeReader";
20101 fn to_entity(&self) -> Self::Entity {
20102 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
20103 }
20104 fn new_unchecked(slice: &'r [u8]) -> Self {
20105 TimeReader(slice)
20106 }
20107 fn as_slice(&self) -> &'r [u8] {
20108 self.0
20109 }
20110 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
20111 use molecule::verification_error as ve;
20112 let slice_len = slice.len();
20113 if slice_len < molecule::NUMBER_SIZE {
20114 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
20115 }
20116 let total_size = molecule::unpack_number(slice) as usize;
20117 if slice_len != total_size {
20118 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
20119 }
20120 if slice_len < molecule::NUMBER_SIZE * 2 {
20121 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
20122 }
20123 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
20124 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
20125 return ve!(Self, OffsetsNotMatch);
20126 }
20127 if slice_len < offset_first {
20128 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
20129 }
20130 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
20131 if field_count < Self::FIELD_COUNT {
20132 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
20133 } else if !compatible && field_count > Self::FIELD_COUNT {
20134 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
20135 };
20136 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
20137 .chunks_exact(molecule::NUMBER_SIZE)
20138 .map(|x| molecule::unpack_number(x) as usize)
20139 .collect();
20140 offsets.push(total_size);
20141 if offsets.windows(2).any(|i| i[0] > i[1]) {
20142 return ve!(Self, OffsetsNotMatch);
20143 }
20144 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
20145 Ok(())
20146 }
20147}
20148#[derive(Clone, Debug, Default)]
20149pub struct TimeBuilder {
20150 pub(crate) timestamp: Uint64,
20151}
20152impl TimeBuilder {
20153 pub const FIELD_COUNT: usize = 1;
20154 pub fn timestamp<T>(mut self, v: T) -> Self
20155 where
20156 T: ::core::convert::Into<Uint64>,
20157 {
20158 self.timestamp = v.into();
20159 self
20160 }
20161}
20162impl molecule::prelude::Builder for TimeBuilder {
20163 type Entity = Time;
20164 const NAME: &'static str = "TimeBuilder";
20165 fn expected_length(&self) -> usize {
20166 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.timestamp.as_slice().len()
20167 }
20168 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
20169 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
20170 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
20171 offsets.push(total_size);
20172 total_size += self.timestamp.as_slice().len();
20173 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
20174 for offset in offsets.into_iter() {
20175 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
20176 }
20177 writer.write_all(self.timestamp.as_slice())?;
20178 Ok(())
20179 }
20180 fn build(&self) -> Self::Entity {
20181 let mut inner = Vec::with_capacity(self.expected_length());
20182 self.write(&mut inner)
20183 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
20184 Time::new_unchecked(inner.into())
20185 }
20186}
20187#[derive(Clone)]
20188pub struct RawAlert(molecule::bytes::Bytes);
20189impl ::core::fmt::LowerHex for RawAlert {
20190 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20191 use molecule::hex_string;
20192 if f.alternate() {
20193 write!(f, "0x")?;
20194 }
20195 write!(f, "{}", hex_string(self.as_slice()))
20196 }
20197}
20198impl ::core::fmt::Debug for RawAlert {
20199 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20200 write!(f, "{}({:#x})", Self::NAME, self)
20201 }
20202}
20203impl ::core::fmt::Display for RawAlert {
20204 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20205 write!(f, "{} {{ ", Self::NAME)?;
20206 write!(f, "{}: {}", "notice_until", self.notice_until())?;
20207 write!(f, ", {}: {}", "id", self.id())?;
20208 write!(f, ", {}: {}", "cancel", self.cancel())?;
20209 write!(f, ", {}: {}", "priority", self.priority())?;
20210 write!(f, ", {}: {}", "message", self.message())?;
20211 write!(f, ", {}: {}", "min_version", self.min_version())?;
20212 write!(f, ", {}: {}", "max_version", self.max_version())?;
20213 let extra_count = self.count_extra_fields();
20214 if extra_count != 0 {
20215 write!(f, ", .. ({} fields)", extra_count)?;
20216 }
20217 write!(f, " }}")
20218 }
20219}
20220impl ::core::default::Default for RawAlert {
20221 fn default() -> Self {
20222 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
20223 RawAlert::new_unchecked(v)
20224 }
20225}
20226impl RawAlert {
20227 const DEFAULT_VALUE: [u8; 56] = [
20228 56, 0, 0, 0, 32, 0, 0, 0, 40, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0,
20229 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20230 ];
20231 pub const FIELD_COUNT: usize = 7;
20232 pub fn total_size(&self) -> usize {
20233 molecule::unpack_number(self.as_slice()) as usize
20234 }
20235 pub fn field_count(&self) -> usize {
20236 if self.total_size() == molecule::NUMBER_SIZE {
20237 0
20238 } else {
20239 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
20240 }
20241 }
20242 pub fn count_extra_fields(&self) -> usize {
20243 self.field_count() - Self::FIELD_COUNT
20244 }
20245 pub fn has_extra_fields(&self) -> bool {
20246 Self::FIELD_COUNT != self.field_count()
20247 }
20248 pub fn notice_until(&self) -> Uint64 {
20249 let slice = self.as_slice();
20250 let start = molecule::unpack_number(&slice[4..]) as usize;
20251 let end = molecule::unpack_number(&slice[8..]) as usize;
20252 Uint64::new_unchecked(self.0.slice(start..end))
20253 }
20254 pub fn id(&self) -> Uint32 {
20255 let slice = self.as_slice();
20256 let start = molecule::unpack_number(&slice[8..]) as usize;
20257 let end = molecule::unpack_number(&slice[12..]) as usize;
20258 Uint32::new_unchecked(self.0.slice(start..end))
20259 }
20260 pub fn cancel(&self) -> Uint32 {
20261 let slice = self.as_slice();
20262 let start = molecule::unpack_number(&slice[12..]) as usize;
20263 let end = molecule::unpack_number(&slice[16..]) as usize;
20264 Uint32::new_unchecked(self.0.slice(start..end))
20265 }
20266 pub fn priority(&self) -> Uint32 {
20267 let slice = self.as_slice();
20268 let start = molecule::unpack_number(&slice[16..]) as usize;
20269 let end = molecule::unpack_number(&slice[20..]) as usize;
20270 Uint32::new_unchecked(self.0.slice(start..end))
20271 }
20272 pub fn message(&self) -> Bytes {
20273 let slice = self.as_slice();
20274 let start = molecule::unpack_number(&slice[20..]) as usize;
20275 let end = molecule::unpack_number(&slice[24..]) as usize;
20276 Bytes::new_unchecked(self.0.slice(start..end))
20277 }
20278 pub fn min_version(&self) -> BytesOpt {
20279 let slice = self.as_slice();
20280 let start = molecule::unpack_number(&slice[24..]) as usize;
20281 let end = molecule::unpack_number(&slice[28..]) as usize;
20282 BytesOpt::new_unchecked(self.0.slice(start..end))
20283 }
20284 pub fn max_version(&self) -> BytesOpt {
20285 let slice = self.as_slice();
20286 let start = molecule::unpack_number(&slice[28..]) as usize;
20287 if self.has_extra_fields() {
20288 let end = molecule::unpack_number(&slice[32..]) as usize;
20289 BytesOpt::new_unchecked(self.0.slice(start..end))
20290 } else {
20291 BytesOpt::new_unchecked(self.0.slice(start..))
20292 }
20293 }
20294 pub fn as_reader<'r>(&'r self) -> RawAlertReader<'r> {
20295 RawAlertReader::new_unchecked(self.as_slice())
20296 }
20297}
20298impl molecule::prelude::Entity for RawAlert {
20299 type Builder = RawAlertBuilder;
20300 const NAME: &'static str = "RawAlert";
20301 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
20302 RawAlert(data)
20303 }
20304 fn as_bytes(&self) -> molecule::bytes::Bytes {
20305 self.0.clone()
20306 }
20307 fn as_slice(&self) -> &[u8] {
20308 &self.0[..]
20309 }
20310 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
20311 RawAlertReader::from_slice(slice).map(|reader| reader.to_entity())
20312 }
20313 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
20314 RawAlertReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
20315 }
20316 fn new_builder() -> Self::Builder {
20317 ::core::default::Default::default()
20318 }
20319 fn as_builder(self) -> Self::Builder {
20320 Self::new_builder()
20321 .notice_until(self.notice_until())
20322 .id(self.id())
20323 .cancel(self.cancel())
20324 .priority(self.priority())
20325 .message(self.message())
20326 .min_version(self.min_version())
20327 .max_version(self.max_version())
20328 }
20329}
20330#[derive(Clone, Copy)]
20331pub struct RawAlertReader<'r>(&'r [u8]);
20332impl<'r> ::core::fmt::LowerHex for RawAlertReader<'r> {
20333 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20334 use molecule::hex_string;
20335 if f.alternate() {
20336 write!(f, "0x")?;
20337 }
20338 write!(f, "{}", hex_string(self.as_slice()))
20339 }
20340}
20341impl<'r> ::core::fmt::Debug for RawAlertReader<'r> {
20342 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20343 write!(f, "{}({:#x})", Self::NAME, self)
20344 }
20345}
20346impl<'r> ::core::fmt::Display for RawAlertReader<'r> {
20347 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20348 write!(f, "{} {{ ", Self::NAME)?;
20349 write!(f, "{}: {}", "notice_until", self.notice_until())?;
20350 write!(f, ", {}: {}", "id", self.id())?;
20351 write!(f, ", {}: {}", "cancel", self.cancel())?;
20352 write!(f, ", {}: {}", "priority", self.priority())?;
20353 write!(f, ", {}: {}", "message", self.message())?;
20354 write!(f, ", {}: {}", "min_version", self.min_version())?;
20355 write!(f, ", {}: {}", "max_version", self.max_version())?;
20356 let extra_count = self.count_extra_fields();
20357 if extra_count != 0 {
20358 write!(f, ", .. ({} fields)", extra_count)?;
20359 }
20360 write!(f, " }}")
20361 }
20362}
20363impl<'r> RawAlertReader<'r> {
20364 pub const FIELD_COUNT: usize = 7;
20365 pub fn total_size(&self) -> usize {
20366 molecule::unpack_number(self.as_slice()) as usize
20367 }
20368 pub fn field_count(&self) -> usize {
20369 if self.total_size() == molecule::NUMBER_SIZE {
20370 0
20371 } else {
20372 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
20373 }
20374 }
20375 pub fn count_extra_fields(&self) -> usize {
20376 self.field_count() - Self::FIELD_COUNT
20377 }
20378 pub fn has_extra_fields(&self) -> bool {
20379 Self::FIELD_COUNT != self.field_count()
20380 }
20381 pub fn notice_until(&self) -> Uint64Reader<'r> {
20382 let slice = self.as_slice();
20383 let start = molecule::unpack_number(&slice[4..]) as usize;
20384 let end = molecule::unpack_number(&slice[8..]) as usize;
20385 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
20386 }
20387 pub fn id(&self) -> Uint32Reader<'r> {
20388 let slice = self.as_slice();
20389 let start = molecule::unpack_number(&slice[8..]) as usize;
20390 let end = molecule::unpack_number(&slice[12..]) as usize;
20391 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
20392 }
20393 pub fn cancel(&self) -> Uint32Reader<'r> {
20394 let slice = self.as_slice();
20395 let start = molecule::unpack_number(&slice[12..]) as usize;
20396 let end = molecule::unpack_number(&slice[16..]) as usize;
20397 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
20398 }
20399 pub fn priority(&self) -> Uint32Reader<'r> {
20400 let slice = self.as_slice();
20401 let start = molecule::unpack_number(&slice[16..]) as usize;
20402 let end = molecule::unpack_number(&slice[20..]) as usize;
20403 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
20404 }
20405 pub fn message(&self) -> BytesReader<'r> {
20406 let slice = self.as_slice();
20407 let start = molecule::unpack_number(&slice[20..]) as usize;
20408 let end = molecule::unpack_number(&slice[24..]) as usize;
20409 BytesReader::new_unchecked(&self.as_slice()[start..end])
20410 }
20411 pub fn min_version(&self) -> BytesOptReader<'r> {
20412 let slice = self.as_slice();
20413 let start = molecule::unpack_number(&slice[24..]) as usize;
20414 let end = molecule::unpack_number(&slice[28..]) as usize;
20415 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
20416 }
20417 pub fn max_version(&self) -> BytesOptReader<'r> {
20418 let slice = self.as_slice();
20419 let start = molecule::unpack_number(&slice[28..]) as usize;
20420 if self.has_extra_fields() {
20421 let end = molecule::unpack_number(&slice[32..]) as usize;
20422 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
20423 } else {
20424 BytesOptReader::new_unchecked(&self.as_slice()[start..])
20425 }
20426 }
20427}
20428impl<'r> molecule::prelude::Reader<'r> for RawAlertReader<'r> {
20429 type Entity = RawAlert;
20430 const NAME: &'static str = "RawAlertReader";
20431 fn to_entity(&self) -> Self::Entity {
20432 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
20433 }
20434 fn new_unchecked(slice: &'r [u8]) -> Self {
20435 RawAlertReader(slice)
20436 }
20437 fn as_slice(&self) -> &'r [u8] {
20438 self.0
20439 }
20440 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
20441 use molecule::verification_error as ve;
20442 let slice_len = slice.len();
20443 if slice_len < molecule::NUMBER_SIZE {
20444 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
20445 }
20446 let total_size = molecule::unpack_number(slice) as usize;
20447 if slice_len != total_size {
20448 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
20449 }
20450 if slice_len < molecule::NUMBER_SIZE * 2 {
20451 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
20452 }
20453 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
20454 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
20455 return ve!(Self, OffsetsNotMatch);
20456 }
20457 if slice_len < offset_first {
20458 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
20459 }
20460 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
20461 if field_count < Self::FIELD_COUNT {
20462 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
20463 } else if !compatible && field_count > Self::FIELD_COUNT {
20464 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
20465 };
20466 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
20467 .chunks_exact(molecule::NUMBER_SIZE)
20468 .map(|x| molecule::unpack_number(x) as usize)
20469 .collect();
20470 offsets.push(total_size);
20471 if offsets.windows(2).any(|i| i[0] > i[1]) {
20472 return ve!(Self, OffsetsNotMatch);
20473 }
20474 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
20475 Uint32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
20476 Uint32Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
20477 Uint32Reader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
20478 BytesReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
20479 BytesOptReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
20480 BytesOptReader::verify(&slice[offsets[6]..offsets[7]], compatible)?;
20481 Ok(())
20482 }
20483}
20484#[derive(Clone, Debug, Default)]
20485pub struct RawAlertBuilder {
20486 pub(crate) notice_until: Uint64,
20487 pub(crate) id: Uint32,
20488 pub(crate) cancel: Uint32,
20489 pub(crate) priority: Uint32,
20490 pub(crate) message: Bytes,
20491 pub(crate) min_version: BytesOpt,
20492 pub(crate) max_version: BytesOpt,
20493}
20494impl RawAlertBuilder {
20495 pub const FIELD_COUNT: usize = 7;
20496 pub fn notice_until<T>(mut self, v: T) -> Self
20497 where
20498 T: ::core::convert::Into<Uint64>,
20499 {
20500 self.notice_until = v.into();
20501 self
20502 }
20503 pub fn id<T>(mut self, v: T) -> Self
20504 where
20505 T: ::core::convert::Into<Uint32>,
20506 {
20507 self.id = v.into();
20508 self
20509 }
20510 pub fn cancel<T>(mut self, v: T) -> Self
20511 where
20512 T: ::core::convert::Into<Uint32>,
20513 {
20514 self.cancel = v.into();
20515 self
20516 }
20517 pub fn priority<T>(mut self, v: T) -> Self
20518 where
20519 T: ::core::convert::Into<Uint32>,
20520 {
20521 self.priority = v.into();
20522 self
20523 }
20524 pub fn message<T>(mut self, v: T) -> Self
20525 where
20526 T: ::core::convert::Into<Bytes>,
20527 {
20528 self.message = v.into();
20529 self
20530 }
20531 pub fn min_version<T>(mut self, v: T) -> Self
20532 where
20533 T: ::core::convert::Into<BytesOpt>,
20534 {
20535 self.min_version = v.into();
20536 self
20537 }
20538 pub fn max_version<T>(mut self, v: T) -> Self
20539 where
20540 T: ::core::convert::Into<BytesOpt>,
20541 {
20542 self.max_version = v.into();
20543 self
20544 }
20545}
20546impl molecule::prelude::Builder for RawAlertBuilder {
20547 type Entity = RawAlert;
20548 const NAME: &'static str = "RawAlertBuilder";
20549 fn expected_length(&self) -> usize {
20550 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
20551 + self.notice_until.as_slice().len()
20552 + self.id.as_slice().len()
20553 + self.cancel.as_slice().len()
20554 + self.priority.as_slice().len()
20555 + self.message.as_slice().len()
20556 + self.min_version.as_slice().len()
20557 + self.max_version.as_slice().len()
20558 }
20559 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
20560 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
20561 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
20562 offsets.push(total_size);
20563 total_size += self.notice_until.as_slice().len();
20564 offsets.push(total_size);
20565 total_size += self.id.as_slice().len();
20566 offsets.push(total_size);
20567 total_size += self.cancel.as_slice().len();
20568 offsets.push(total_size);
20569 total_size += self.priority.as_slice().len();
20570 offsets.push(total_size);
20571 total_size += self.message.as_slice().len();
20572 offsets.push(total_size);
20573 total_size += self.min_version.as_slice().len();
20574 offsets.push(total_size);
20575 total_size += self.max_version.as_slice().len();
20576 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
20577 for offset in offsets.into_iter() {
20578 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
20579 }
20580 writer.write_all(self.notice_until.as_slice())?;
20581 writer.write_all(self.id.as_slice())?;
20582 writer.write_all(self.cancel.as_slice())?;
20583 writer.write_all(self.priority.as_slice())?;
20584 writer.write_all(self.message.as_slice())?;
20585 writer.write_all(self.min_version.as_slice())?;
20586 writer.write_all(self.max_version.as_slice())?;
20587 Ok(())
20588 }
20589 fn build(&self) -> Self::Entity {
20590 let mut inner = Vec::with_capacity(self.expected_length());
20591 self.write(&mut inner)
20592 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
20593 RawAlert::new_unchecked(inner.into())
20594 }
20595}
20596#[derive(Clone)]
20597pub struct Alert(molecule::bytes::Bytes);
20598impl ::core::fmt::LowerHex for Alert {
20599 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20600 use molecule::hex_string;
20601 if f.alternate() {
20602 write!(f, "0x")?;
20603 }
20604 write!(f, "{}", hex_string(self.as_slice()))
20605 }
20606}
20607impl ::core::fmt::Debug for Alert {
20608 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20609 write!(f, "{}({:#x})", Self::NAME, self)
20610 }
20611}
20612impl ::core::fmt::Display for Alert {
20613 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20614 write!(f, "{} {{ ", Self::NAME)?;
20615 write!(f, "{}: {}", "raw", self.raw())?;
20616 write!(f, ", {}: {}", "signatures", self.signatures())?;
20617 let extra_count = self.count_extra_fields();
20618 if extra_count != 0 {
20619 write!(f, ", .. ({} fields)", extra_count)?;
20620 }
20621 write!(f, " }}")
20622 }
20623}
20624impl ::core::default::Default for Alert {
20625 fn default() -> Self {
20626 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
20627 Alert::new_unchecked(v)
20628 }
20629}
20630impl Alert {
20631 const DEFAULT_VALUE: [u8; 72] = [
20632 72, 0, 0, 0, 12, 0, 0, 0, 68, 0, 0, 0, 56, 0, 0, 0, 32, 0, 0, 0, 40, 0, 0, 0, 44, 0, 0, 0,
20633 48, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20634 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
20635 ];
20636 pub const FIELD_COUNT: usize = 2;
20637 pub fn total_size(&self) -> usize {
20638 molecule::unpack_number(self.as_slice()) as usize
20639 }
20640 pub fn field_count(&self) -> usize {
20641 if self.total_size() == molecule::NUMBER_SIZE {
20642 0
20643 } else {
20644 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
20645 }
20646 }
20647 pub fn count_extra_fields(&self) -> usize {
20648 self.field_count() - Self::FIELD_COUNT
20649 }
20650 pub fn has_extra_fields(&self) -> bool {
20651 Self::FIELD_COUNT != self.field_count()
20652 }
20653 pub fn raw(&self) -> RawAlert {
20654 let slice = self.as_slice();
20655 let start = molecule::unpack_number(&slice[4..]) as usize;
20656 let end = molecule::unpack_number(&slice[8..]) as usize;
20657 RawAlert::new_unchecked(self.0.slice(start..end))
20658 }
20659 pub fn signatures(&self) -> BytesVec {
20660 let slice = self.as_slice();
20661 let start = molecule::unpack_number(&slice[8..]) as usize;
20662 if self.has_extra_fields() {
20663 let end = molecule::unpack_number(&slice[12..]) as usize;
20664 BytesVec::new_unchecked(self.0.slice(start..end))
20665 } else {
20666 BytesVec::new_unchecked(self.0.slice(start..))
20667 }
20668 }
20669 pub fn as_reader<'r>(&'r self) -> AlertReader<'r> {
20670 AlertReader::new_unchecked(self.as_slice())
20671 }
20672}
20673impl molecule::prelude::Entity for Alert {
20674 type Builder = AlertBuilder;
20675 const NAME: &'static str = "Alert";
20676 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
20677 Alert(data)
20678 }
20679 fn as_bytes(&self) -> molecule::bytes::Bytes {
20680 self.0.clone()
20681 }
20682 fn as_slice(&self) -> &[u8] {
20683 &self.0[..]
20684 }
20685 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
20686 AlertReader::from_slice(slice).map(|reader| reader.to_entity())
20687 }
20688 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
20689 AlertReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
20690 }
20691 fn new_builder() -> Self::Builder {
20692 ::core::default::Default::default()
20693 }
20694 fn as_builder(self) -> Self::Builder {
20695 Self::new_builder()
20696 .raw(self.raw())
20697 .signatures(self.signatures())
20698 }
20699}
20700#[derive(Clone, Copy)]
20701pub struct AlertReader<'r>(&'r [u8]);
20702impl<'r> ::core::fmt::LowerHex for AlertReader<'r> {
20703 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20704 use molecule::hex_string;
20705 if f.alternate() {
20706 write!(f, "0x")?;
20707 }
20708 write!(f, "{}", hex_string(self.as_slice()))
20709 }
20710}
20711impl<'r> ::core::fmt::Debug for AlertReader<'r> {
20712 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20713 write!(f, "{}({:#x})", Self::NAME, self)
20714 }
20715}
20716impl<'r> ::core::fmt::Display for AlertReader<'r> {
20717 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20718 write!(f, "{} {{ ", Self::NAME)?;
20719 write!(f, "{}: {}", "raw", self.raw())?;
20720 write!(f, ", {}: {}", "signatures", self.signatures())?;
20721 let extra_count = self.count_extra_fields();
20722 if extra_count != 0 {
20723 write!(f, ", .. ({} fields)", extra_count)?;
20724 }
20725 write!(f, " }}")
20726 }
20727}
20728impl<'r> AlertReader<'r> {
20729 pub const FIELD_COUNT: usize = 2;
20730 pub fn total_size(&self) -> usize {
20731 molecule::unpack_number(self.as_slice()) as usize
20732 }
20733 pub fn field_count(&self) -> usize {
20734 if self.total_size() == molecule::NUMBER_SIZE {
20735 0
20736 } else {
20737 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
20738 }
20739 }
20740 pub fn count_extra_fields(&self) -> usize {
20741 self.field_count() - Self::FIELD_COUNT
20742 }
20743 pub fn has_extra_fields(&self) -> bool {
20744 Self::FIELD_COUNT != self.field_count()
20745 }
20746 pub fn raw(&self) -> RawAlertReader<'r> {
20747 let slice = self.as_slice();
20748 let start = molecule::unpack_number(&slice[4..]) as usize;
20749 let end = molecule::unpack_number(&slice[8..]) as usize;
20750 RawAlertReader::new_unchecked(&self.as_slice()[start..end])
20751 }
20752 pub fn signatures(&self) -> BytesVecReader<'r> {
20753 let slice = self.as_slice();
20754 let start = molecule::unpack_number(&slice[8..]) as usize;
20755 if self.has_extra_fields() {
20756 let end = molecule::unpack_number(&slice[12..]) as usize;
20757 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
20758 } else {
20759 BytesVecReader::new_unchecked(&self.as_slice()[start..])
20760 }
20761 }
20762}
20763impl<'r> molecule::prelude::Reader<'r> for AlertReader<'r> {
20764 type Entity = Alert;
20765 const NAME: &'static str = "AlertReader";
20766 fn to_entity(&self) -> Self::Entity {
20767 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
20768 }
20769 fn new_unchecked(slice: &'r [u8]) -> Self {
20770 AlertReader(slice)
20771 }
20772 fn as_slice(&self) -> &'r [u8] {
20773 self.0
20774 }
20775 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
20776 use molecule::verification_error as ve;
20777 let slice_len = slice.len();
20778 if slice_len < molecule::NUMBER_SIZE {
20779 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
20780 }
20781 let total_size = molecule::unpack_number(slice) as usize;
20782 if slice_len != total_size {
20783 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
20784 }
20785 if slice_len < molecule::NUMBER_SIZE * 2 {
20786 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
20787 }
20788 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
20789 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
20790 return ve!(Self, OffsetsNotMatch);
20791 }
20792 if slice_len < offset_first {
20793 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
20794 }
20795 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
20796 if field_count < Self::FIELD_COUNT {
20797 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
20798 } else if !compatible && field_count > Self::FIELD_COUNT {
20799 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
20800 };
20801 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
20802 .chunks_exact(molecule::NUMBER_SIZE)
20803 .map(|x| molecule::unpack_number(x) as usize)
20804 .collect();
20805 offsets.push(total_size);
20806 if offsets.windows(2).any(|i| i[0] > i[1]) {
20807 return ve!(Self, OffsetsNotMatch);
20808 }
20809 RawAlertReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
20810 BytesVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
20811 Ok(())
20812 }
20813}
20814#[derive(Clone, Debug, Default)]
20815pub struct AlertBuilder {
20816 pub(crate) raw: RawAlert,
20817 pub(crate) signatures: BytesVec,
20818}
20819impl AlertBuilder {
20820 pub const FIELD_COUNT: usize = 2;
20821 pub fn raw<T>(mut self, v: T) -> Self
20822 where
20823 T: ::core::convert::Into<RawAlert>,
20824 {
20825 self.raw = v.into();
20826 self
20827 }
20828 pub fn signatures<T>(mut self, v: T) -> Self
20829 where
20830 T: ::core::convert::Into<BytesVec>,
20831 {
20832 self.signatures = v.into();
20833 self
20834 }
20835}
20836impl molecule::prelude::Builder for AlertBuilder {
20837 type Entity = Alert;
20838 const NAME: &'static str = "AlertBuilder";
20839 fn expected_length(&self) -> usize {
20840 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
20841 + self.raw.as_slice().len()
20842 + self.signatures.as_slice().len()
20843 }
20844 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
20845 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
20846 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
20847 offsets.push(total_size);
20848 total_size += self.raw.as_slice().len();
20849 offsets.push(total_size);
20850 total_size += self.signatures.as_slice().len();
20851 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
20852 for offset in offsets.into_iter() {
20853 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
20854 }
20855 writer.write_all(self.raw.as_slice())?;
20856 writer.write_all(self.signatures.as_slice())?;
20857 Ok(())
20858 }
20859 fn build(&self) -> Self::Entity {
20860 let mut inner = Vec::with_capacity(self.expected_length());
20861 self.write(&mut inner)
20862 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
20863 Alert::new_unchecked(inner.into())
20864 }
20865}
20866#[derive(Clone)]
20867pub struct Identify(molecule::bytes::Bytes);
20868impl ::core::fmt::LowerHex for Identify {
20869 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20870 use molecule::hex_string;
20871 if f.alternate() {
20872 write!(f, "0x")?;
20873 }
20874 write!(f, "{}", hex_string(self.as_slice()))
20875 }
20876}
20877impl ::core::fmt::Debug for Identify {
20878 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20879 write!(f, "{}({:#x})", Self::NAME, self)
20880 }
20881}
20882impl ::core::fmt::Display for Identify {
20883 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20884 write!(f, "{} {{ ", Self::NAME)?;
20885 write!(f, "{}: {}", "flag", self.flag())?;
20886 write!(f, ", {}: {}", "name", self.name())?;
20887 write!(f, ", {}: {}", "client_version", self.client_version())?;
20888 let extra_count = self.count_extra_fields();
20889 if extra_count != 0 {
20890 write!(f, ", .. ({} fields)", extra_count)?;
20891 }
20892 write!(f, " }}")
20893 }
20894}
20895impl ::core::default::Default for Identify {
20896 fn default() -> Self {
20897 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
20898 Identify::new_unchecked(v)
20899 }
20900}
20901impl Identify {
20902 const DEFAULT_VALUE: [u8; 32] = [
20903 32, 0, 0, 0, 16, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20904 0, 0, 0,
20905 ];
20906 pub const FIELD_COUNT: usize = 3;
20907 pub fn total_size(&self) -> usize {
20908 molecule::unpack_number(self.as_slice()) as usize
20909 }
20910 pub fn field_count(&self) -> usize {
20911 if self.total_size() == molecule::NUMBER_SIZE {
20912 0
20913 } else {
20914 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
20915 }
20916 }
20917 pub fn count_extra_fields(&self) -> usize {
20918 self.field_count() - Self::FIELD_COUNT
20919 }
20920 pub fn has_extra_fields(&self) -> bool {
20921 Self::FIELD_COUNT != self.field_count()
20922 }
20923 pub fn flag(&self) -> Uint64 {
20924 let slice = self.as_slice();
20925 let start = molecule::unpack_number(&slice[4..]) as usize;
20926 let end = molecule::unpack_number(&slice[8..]) as usize;
20927 Uint64::new_unchecked(self.0.slice(start..end))
20928 }
20929 pub fn name(&self) -> Bytes {
20930 let slice = self.as_slice();
20931 let start = molecule::unpack_number(&slice[8..]) as usize;
20932 let end = molecule::unpack_number(&slice[12..]) as usize;
20933 Bytes::new_unchecked(self.0.slice(start..end))
20934 }
20935 pub fn client_version(&self) -> Bytes {
20936 let slice = self.as_slice();
20937 let start = molecule::unpack_number(&slice[12..]) as usize;
20938 if self.has_extra_fields() {
20939 let end = molecule::unpack_number(&slice[16..]) as usize;
20940 Bytes::new_unchecked(self.0.slice(start..end))
20941 } else {
20942 Bytes::new_unchecked(self.0.slice(start..))
20943 }
20944 }
20945 pub fn as_reader<'r>(&'r self) -> IdentifyReader<'r> {
20946 IdentifyReader::new_unchecked(self.as_slice())
20947 }
20948}
20949impl molecule::prelude::Entity for Identify {
20950 type Builder = IdentifyBuilder;
20951 const NAME: &'static str = "Identify";
20952 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
20953 Identify(data)
20954 }
20955 fn as_bytes(&self) -> molecule::bytes::Bytes {
20956 self.0.clone()
20957 }
20958 fn as_slice(&self) -> &[u8] {
20959 &self.0[..]
20960 }
20961 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
20962 IdentifyReader::from_slice(slice).map(|reader| reader.to_entity())
20963 }
20964 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
20965 IdentifyReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
20966 }
20967 fn new_builder() -> Self::Builder {
20968 ::core::default::Default::default()
20969 }
20970 fn as_builder(self) -> Self::Builder {
20971 Self::new_builder()
20972 .flag(self.flag())
20973 .name(self.name())
20974 .client_version(self.client_version())
20975 }
20976}
20977#[derive(Clone, Copy)]
20978pub struct IdentifyReader<'r>(&'r [u8]);
20979impl<'r> ::core::fmt::LowerHex for IdentifyReader<'r> {
20980 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20981 use molecule::hex_string;
20982 if f.alternate() {
20983 write!(f, "0x")?;
20984 }
20985 write!(f, "{}", hex_string(self.as_slice()))
20986 }
20987}
20988impl<'r> ::core::fmt::Debug for IdentifyReader<'r> {
20989 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20990 write!(f, "{}({:#x})", Self::NAME, self)
20991 }
20992}
20993impl<'r> ::core::fmt::Display for IdentifyReader<'r> {
20994 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
20995 write!(f, "{} {{ ", Self::NAME)?;
20996 write!(f, "{}: {}", "flag", self.flag())?;
20997 write!(f, ", {}: {}", "name", self.name())?;
20998 write!(f, ", {}: {}", "client_version", self.client_version())?;
20999 let extra_count = self.count_extra_fields();
21000 if extra_count != 0 {
21001 write!(f, ", .. ({} fields)", extra_count)?;
21002 }
21003 write!(f, " }}")
21004 }
21005}
21006impl<'r> IdentifyReader<'r> {
21007 pub const FIELD_COUNT: usize = 3;
21008 pub fn total_size(&self) -> usize {
21009 molecule::unpack_number(self.as_slice()) as usize
21010 }
21011 pub fn field_count(&self) -> usize {
21012 if self.total_size() == molecule::NUMBER_SIZE {
21013 0
21014 } else {
21015 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
21016 }
21017 }
21018 pub fn count_extra_fields(&self) -> usize {
21019 self.field_count() - Self::FIELD_COUNT
21020 }
21021 pub fn has_extra_fields(&self) -> bool {
21022 Self::FIELD_COUNT != self.field_count()
21023 }
21024 pub fn flag(&self) -> Uint64Reader<'r> {
21025 let slice = self.as_slice();
21026 let start = molecule::unpack_number(&slice[4..]) as usize;
21027 let end = molecule::unpack_number(&slice[8..]) as usize;
21028 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
21029 }
21030 pub fn name(&self) -> BytesReader<'r> {
21031 let slice = self.as_slice();
21032 let start = molecule::unpack_number(&slice[8..]) as usize;
21033 let end = molecule::unpack_number(&slice[12..]) as usize;
21034 BytesReader::new_unchecked(&self.as_slice()[start..end])
21035 }
21036 pub fn client_version(&self) -> BytesReader<'r> {
21037 let slice = self.as_slice();
21038 let start = molecule::unpack_number(&slice[12..]) as usize;
21039 if self.has_extra_fields() {
21040 let end = molecule::unpack_number(&slice[16..]) as usize;
21041 BytesReader::new_unchecked(&self.as_slice()[start..end])
21042 } else {
21043 BytesReader::new_unchecked(&self.as_slice()[start..])
21044 }
21045 }
21046}
21047impl<'r> molecule::prelude::Reader<'r> for IdentifyReader<'r> {
21048 type Entity = Identify;
21049 const NAME: &'static str = "IdentifyReader";
21050 fn to_entity(&self) -> Self::Entity {
21051 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
21052 }
21053 fn new_unchecked(slice: &'r [u8]) -> Self {
21054 IdentifyReader(slice)
21055 }
21056 fn as_slice(&self) -> &'r [u8] {
21057 self.0
21058 }
21059 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
21060 use molecule::verification_error as ve;
21061 let slice_len = slice.len();
21062 if slice_len < molecule::NUMBER_SIZE {
21063 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
21064 }
21065 let total_size = molecule::unpack_number(slice) as usize;
21066 if slice_len != total_size {
21067 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
21068 }
21069 if slice_len < molecule::NUMBER_SIZE * 2 {
21070 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
21071 }
21072 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
21073 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
21074 return ve!(Self, OffsetsNotMatch);
21075 }
21076 if slice_len < offset_first {
21077 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
21078 }
21079 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
21080 if field_count < Self::FIELD_COUNT {
21081 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
21082 } else if !compatible && field_count > Self::FIELD_COUNT {
21083 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
21084 };
21085 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
21086 .chunks_exact(molecule::NUMBER_SIZE)
21087 .map(|x| molecule::unpack_number(x) as usize)
21088 .collect();
21089 offsets.push(total_size);
21090 if offsets.windows(2).any(|i| i[0] > i[1]) {
21091 return ve!(Self, OffsetsNotMatch);
21092 }
21093 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
21094 BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
21095 BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
21096 Ok(())
21097 }
21098}
21099#[derive(Clone, Debug, Default)]
21100pub struct IdentifyBuilder {
21101 pub(crate) flag: Uint64,
21102 pub(crate) name: Bytes,
21103 pub(crate) client_version: Bytes,
21104}
21105impl IdentifyBuilder {
21106 pub const FIELD_COUNT: usize = 3;
21107 pub fn flag<T>(mut self, v: T) -> Self
21108 where
21109 T: ::core::convert::Into<Uint64>,
21110 {
21111 self.flag = v.into();
21112 self
21113 }
21114 pub fn name<T>(mut self, v: T) -> Self
21115 where
21116 T: ::core::convert::Into<Bytes>,
21117 {
21118 self.name = v.into();
21119 self
21120 }
21121 pub fn client_version<T>(mut self, v: T) -> Self
21122 where
21123 T: ::core::convert::Into<Bytes>,
21124 {
21125 self.client_version = v.into();
21126 self
21127 }
21128}
21129impl molecule::prelude::Builder for IdentifyBuilder {
21130 type Entity = Identify;
21131 const NAME: &'static str = "IdentifyBuilder";
21132 fn expected_length(&self) -> usize {
21133 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
21134 + self.flag.as_slice().len()
21135 + self.name.as_slice().len()
21136 + self.client_version.as_slice().len()
21137 }
21138 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
21139 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
21140 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
21141 offsets.push(total_size);
21142 total_size += self.flag.as_slice().len();
21143 offsets.push(total_size);
21144 total_size += self.name.as_slice().len();
21145 offsets.push(total_size);
21146 total_size += self.client_version.as_slice().len();
21147 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
21148 for offset in offsets.into_iter() {
21149 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
21150 }
21151 writer.write_all(self.flag.as_slice())?;
21152 writer.write_all(self.name.as_slice())?;
21153 writer.write_all(self.client_version.as_slice())?;
21154 Ok(())
21155 }
21156 fn build(&self) -> Self::Entity {
21157 let mut inner = Vec::with_capacity(self.expected_length());
21158 self.write(&mut inner)
21159 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
21160 Identify::new_unchecked(inner.into())
21161 }
21162}