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(Debug, Default)]
140pub struct BoolOptBuilder(pub(crate) Option<Bool>);
141impl BoolOptBuilder {
142 pub fn set(mut self, v: Option<Bool>) -> Self {
143 self.0 = v;
144 self
145 }
146}
147impl molecule::prelude::Builder for BoolOptBuilder {
148 type Entity = BoolOpt;
149 const NAME: &'static str = "BoolOptBuilder";
150 fn expected_length(&self) -> usize {
151 self.0
152 .as_ref()
153 .map(|ref inner| inner.as_slice().len())
154 .unwrap_or(0)
155 }
156 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
157 self.0
158 .as_ref()
159 .map(|ref inner| writer.write_all(inner.as_slice()))
160 .unwrap_or(Ok(()))
161 }
162 fn build(&self) -> Self::Entity {
163 let mut inner = Vec::with_capacity(self.expected_length());
164 self.write(&mut inner)
165 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
166 BoolOpt::new_unchecked(inner.into())
167 }
168}
169#[derive(Clone)]
170pub struct Byte32Opt(molecule::bytes::Bytes);
171impl ::core::fmt::LowerHex for Byte32Opt {
172 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
173 use molecule::hex_string;
174 if f.alternate() {
175 write!(f, "0x")?;
176 }
177 write!(f, "{}", hex_string(self.as_slice()))
178 }
179}
180impl ::core::fmt::Debug for Byte32Opt {
181 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
182 write!(f, "{}({:#x})", Self::NAME, self)
183 }
184}
185impl ::core::fmt::Display for Byte32Opt {
186 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
187 if let Some(v) = self.to_opt() {
188 write!(f, "{}(Some({}))", Self::NAME, v)
189 } else {
190 write!(f, "{}(None)", Self::NAME)
191 }
192 }
193}
194impl ::core::default::Default for Byte32Opt {
195 fn default() -> Self {
196 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
197 Byte32Opt::new_unchecked(v)
198 }
199}
200impl Byte32Opt {
201 const DEFAULT_VALUE: [u8; 0] = [];
202 pub fn is_none(&self) -> bool {
203 self.0.is_empty()
204 }
205 pub fn is_some(&self) -> bool {
206 !self.0.is_empty()
207 }
208 pub fn to_opt(&self) -> Option<Byte32> {
209 if self.is_none() {
210 None
211 } else {
212 Some(Byte32::new_unchecked(self.0.clone()))
213 }
214 }
215 pub fn as_reader<'r>(&'r self) -> Byte32OptReader<'r> {
216 Byte32OptReader::new_unchecked(self.as_slice())
217 }
218}
219impl molecule::prelude::Entity for Byte32Opt {
220 type Builder = Byte32OptBuilder;
221 const NAME: &'static str = "Byte32Opt";
222 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
223 Byte32Opt(data)
224 }
225 fn as_bytes(&self) -> molecule::bytes::Bytes {
226 self.0.clone()
227 }
228 fn as_slice(&self) -> &[u8] {
229 &self.0[..]
230 }
231 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
232 Byte32OptReader::from_slice(slice).map(|reader| reader.to_entity())
233 }
234 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
235 Byte32OptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
236 }
237 fn new_builder() -> Self::Builder {
238 ::core::default::Default::default()
239 }
240 fn as_builder(self) -> Self::Builder {
241 Self::new_builder().set(self.to_opt())
242 }
243}
244#[derive(Clone, Copy)]
245pub struct Byte32OptReader<'r>(&'r [u8]);
246impl<'r> ::core::fmt::LowerHex for Byte32OptReader<'r> {
247 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
248 use molecule::hex_string;
249 if f.alternate() {
250 write!(f, "0x")?;
251 }
252 write!(f, "{}", hex_string(self.as_slice()))
253 }
254}
255impl<'r> ::core::fmt::Debug for Byte32OptReader<'r> {
256 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
257 write!(f, "{}({:#x})", Self::NAME, self)
258 }
259}
260impl<'r> ::core::fmt::Display for Byte32OptReader<'r> {
261 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
262 if let Some(v) = self.to_opt() {
263 write!(f, "{}(Some({}))", Self::NAME, v)
264 } else {
265 write!(f, "{}(None)", Self::NAME)
266 }
267 }
268}
269impl<'r> Byte32OptReader<'r> {
270 pub fn is_none(&self) -> bool {
271 self.0.is_empty()
272 }
273 pub fn is_some(&self) -> bool {
274 !self.0.is_empty()
275 }
276 pub fn to_opt(&self) -> Option<Byte32Reader<'r>> {
277 if self.is_none() {
278 None
279 } else {
280 Some(Byte32Reader::new_unchecked(self.as_slice()))
281 }
282 }
283}
284impl<'r> molecule::prelude::Reader<'r> for Byte32OptReader<'r> {
285 type Entity = Byte32Opt;
286 const NAME: &'static str = "Byte32OptReader";
287 fn to_entity(&self) -> Self::Entity {
288 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
289 }
290 fn new_unchecked(slice: &'r [u8]) -> Self {
291 Byte32OptReader(slice)
292 }
293 fn as_slice(&self) -> &'r [u8] {
294 self.0
295 }
296 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
297 if !slice.is_empty() {
298 Byte32Reader::verify(&slice[..], compatible)?;
299 }
300 Ok(())
301 }
302}
303#[derive(Debug, Default)]
304pub struct Byte32OptBuilder(pub(crate) Option<Byte32>);
305impl Byte32OptBuilder {
306 pub fn set(mut self, v: Option<Byte32>) -> Self {
307 self.0 = v;
308 self
309 }
310}
311impl molecule::prelude::Builder for Byte32OptBuilder {
312 type Entity = Byte32Opt;
313 const NAME: &'static str = "Byte32OptBuilder";
314 fn expected_length(&self) -> usize {
315 self.0
316 .as_ref()
317 .map(|ref inner| inner.as_slice().len())
318 .unwrap_or(0)
319 }
320 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
321 self.0
322 .as_ref()
323 .map(|ref inner| writer.write_all(inner.as_slice()))
324 .unwrap_or(Ok(()))
325 }
326 fn build(&self) -> Self::Entity {
327 let mut inner = Vec::with_capacity(self.expected_length());
328 self.write(&mut inner)
329 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
330 Byte32Opt::new_unchecked(inner.into())
331 }
332}
333#[derive(Clone)]
334pub struct Bool(molecule::bytes::Bytes);
335impl ::core::fmt::LowerHex for Bool {
336 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
337 use molecule::hex_string;
338 if f.alternate() {
339 write!(f, "0x")?;
340 }
341 write!(f, "{}", hex_string(self.as_slice()))
342 }
343}
344impl ::core::fmt::Debug for Bool {
345 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
346 write!(f, "{}({:#x})", Self::NAME, self)
347 }
348}
349impl ::core::fmt::Display for Bool {
350 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
351 use molecule::hex_string;
352 let raw_data = hex_string(&self.raw_data());
353 write!(f, "{}(0x{})", Self::NAME, raw_data)
354 }
355}
356impl ::core::default::Default for Bool {
357 fn default() -> Self {
358 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
359 Bool::new_unchecked(v)
360 }
361}
362impl Bool {
363 const DEFAULT_VALUE: [u8; 1] = [0];
364 pub const TOTAL_SIZE: usize = 1;
365 pub const ITEM_SIZE: usize = 1;
366 pub const ITEM_COUNT: usize = 1;
367 pub fn nth0(&self) -> Byte {
368 Byte::new_unchecked(self.0.slice(0..1))
369 }
370 pub fn raw_data(&self) -> molecule::bytes::Bytes {
371 self.as_bytes()
372 }
373 pub fn as_reader<'r>(&'r self) -> BoolReader<'r> {
374 BoolReader::new_unchecked(self.as_slice())
375 }
376}
377impl molecule::prelude::Entity for Bool {
378 type Builder = BoolBuilder;
379 const NAME: &'static str = "Bool";
380 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
381 Bool(data)
382 }
383 fn as_bytes(&self) -> molecule::bytes::Bytes {
384 self.0.clone()
385 }
386 fn as_slice(&self) -> &[u8] {
387 &self.0[..]
388 }
389 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
390 BoolReader::from_slice(slice).map(|reader| reader.to_entity())
391 }
392 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
393 BoolReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
394 }
395 fn new_builder() -> Self::Builder {
396 ::core::default::Default::default()
397 }
398 fn as_builder(self) -> Self::Builder {
399 Self::new_builder().set([self.nth0()])
400 }
401}
402#[derive(Clone, Copy)]
403pub struct BoolReader<'r>(&'r [u8]);
404impl<'r> ::core::fmt::LowerHex for BoolReader<'r> {
405 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
406 use molecule::hex_string;
407 if f.alternate() {
408 write!(f, "0x")?;
409 }
410 write!(f, "{}", hex_string(self.as_slice()))
411 }
412}
413impl<'r> ::core::fmt::Debug for BoolReader<'r> {
414 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
415 write!(f, "{}({:#x})", Self::NAME, self)
416 }
417}
418impl<'r> ::core::fmt::Display for BoolReader<'r> {
419 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
420 use molecule::hex_string;
421 let raw_data = hex_string(&self.raw_data());
422 write!(f, "{}(0x{})", Self::NAME, raw_data)
423 }
424}
425impl<'r> BoolReader<'r> {
426 pub const TOTAL_SIZE: usize = 1;
427 pub const ITEM_SIZE: usize = 1;
428 pub const ITEM_COUNT: usize = 1;
429 pub fn nth0(&self) -> ByteReader<'r> {
430 ByteReader::new_unchecked(&self.as_slice()[0..1])
431 }
432 pub fn raw_data(&self) -> &'r [u8] {
433 self.as_slice()
434 }
435}
436impl<'r> molecule::prelude::Reader<'r> for BoolReader<'r> {
437 type Entity = Bool;
438 const NAME: &'static str = "BoolReader";
439 fn to_entity(&self) -> Self::Entity {
440 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
441 }
442 fn new_unchecked(slice: &'r [u8]) -> Self {
443 BoolReader(slice)
444 }
445 fn as_slice(&self) -> &'r [u8] {
446 self.0
447 }
448 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
449 use molecule::verification_error as ve;
450 let slice_len = slice.len();
451 if slice_len != Self::TOTAL_SIZE {
452 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
453 }
454 Ok(())
455 }
456}
457pub struct BoolBuilder(pub(crate) [Byte; 1]);
458impl ::core::fmt::Debug for BoolBuilder {
459 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
460 write!(f, "{}({:?})", Self::NAME, &self.0[..])
461 }
462}
463impl ::core::default::Default for BoolBuilder {
464 fn default() -> Self {
465 BoolBuilder([Byte::default()])
466 }
467}
468impl BoolBuilder {
469 pub const TOTAL_SIZE: usize = 1;
470 pub const ITEM_SIZE: usize = 1;
471 pub const ITEM_COUNT: usize = 1;
472 pub fn set(mut self, v: [Byte; 1]) -> Self {
473 self.0 = v;
474 self
475 }
476 pub fn nth0(mut self, v: Byte) -> Self {
477 self.0[0] = v;
478 self
479 }
480}
481impl molecule::prelude::Builder for BoolBuilder {
482 type Entity = Bool;
483 const NAME: &'static str = "BoolBuilder";
484 fn expected_length(&self) -> usize {
485 Self::TOTAL_SIZE
486 }
487 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
488 writer.write_all(self.0[0].as_slice())?;
489 Ok(())
490 }
491 fn build(&self) -> Self::Entity {
492 let mut inner = Vec::with_capacity(self.expected_length());
493 self.write(&mut inner)
494 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
495 Bool::new_unchecked(inner.into())
496 }
497}
498#[derive(Clone)]
499pub struct BeUint32(molecule::bytes::Bytes);
500impl ::core::fmt::LowerHex for BeUint32 {
501 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
502 use molecule::hex_string;
503 if f.alternate() {
504 write!(f, "0x")?;
505 }
506 write!(f, "{}", hex_string(self.as_slice()))
507 }
508}
509impl ::core::fmt::Debug for BeUint32 {
510 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
511 write!(f, "{}({:#x})", Self::NAME, self)
512 }
513}
514impl ::core::fmt::Display for BeUint32 {
515 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
516 use molecule::hex_string;
517 let raw_data = hex_string(&self.raw_data());
518 write!(f, "{}(0x{})", Self::NAME, raw_data)
519 }
520}
521impl ::core::default::Default for BeUint32 {
522 fn default() -> Self {
523 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
524 BeUint32::new_unchecked(v)
525 }
526}
527impl BeUint32 {
528 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
529 pub const TOTAL_SIZE: usize = 4;
530 pub const ITEM_SIZE: usize = 1;
531 pub const ITEM_COUNT: usize = 4;
532 pub fn nth0(&self) -> Byte {
533 Byte::new_unchecked(self.0.slice(0..1))
534 }
535 pub fn nth1(&self) -> Byte {
536 Byte::new_unchecked(self.0.slice(1..2))
537 }
538 pub fn nth2(&self) -> Byte {
539 Byte::new_unchecked(self.0.slice(2..3))
540 }
541 pub fn nth3(&self) -> Byte {
542 Byte::new_unchecked(self.0.slice(3..4))
543 }
544 pub fn raw_data(&self) -> molecule::bytes::Bytes {
545 self.as_bytes()
546 }
547 pub fn as_reader<'r>(&'r self) -> BeUint32Reader<'r> {
548 BeUint32Reader::new_unchecked(self.as_slice())
549 }
550}
551impl molecule::prelude::Entity for BeUint32 {
552 type Builder = BeUint32Builder;
553 const NAME: &'static str = "BeUint32";
554 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
555 BeUint32(data)
556 }
557 fn as_bytes(&self) -> molecule::bytes::Bytes {
558 self.0.clone()
559 }
560 fn as_slice(&self) -> &[u8] {
561 &self.0[..]
562 }
563 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
564 BeUint32Reader::from_slice(slice).map(|reader| reader.to_entity())
565 }
566 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
567 BeUint32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
568 }
569 fn new_builder() -> Self::Builder {
570 ::core::default::Default::default()
571 }
572 fn as_builder(self) -> Self::Builder {
573 Self::new_builder().set([self.nth0(), self.nth1(), self.nth2(), self.nth3()])
574 }
575}
576#[derive(Clone, Copy)]
577pub struct BeUint32Reader<'r>(&'r [u8]);
578impl<'r> ::core::fmt::LowerHex for BeUint32Reader<'r> {
579 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
580 use molecule::hex_string;
581 if f.alternate() {
582 write!(f, "0x")?;
583 }
584 write!(f, "{}", hex_string(self.as_slice()))
585 }
586}
587impl<'r> ::core::fmt::Debug for BeUint32Reader<'r> {
588 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
589 write!(f, "{}({:#x})", Self::NAME, self)
590 }
591}
592impl<'r> ::core::fmt::Display for BeUint32Reader<'r> {
593 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
594 use molecule::hex_string;
595 let raw_data = hex_string(&self.raw_data());
596 write!(f, "{}(0x{})", Self::NAME, raw_data)
597 }
598}
599impl<'r> BeUint32Reader<'r> {
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) -> ByteReader<'r> {
604 ByteReader::new_unchecked(&self.as_slice()[0..1])
605 }
606 pub fn nth1(&self) -> ByteReader<'r> {
607 ByteReader::new_unchecked(&self.as_slice()[1..2])
608 }
609 pub fn nth2(&self) -> ByteReader<'r> {
610 ByteReader::new_unchecked(&self.as_slice()[2..3])
611 }
612 pub fn nth3(&self) -> ByteReader<'r> {
613 ByteReader::new_unchecked(&self.as_slice()[3..4])
614 }
615 pub fn raw_data(&self) -> &'r [u8] {
616 self.as_slice()
617 }
618}
619impl<'r> molecule::prelude::Reader<'r> for BeUint32Reader<'r> {
620 type Entity = BeUint32;
621 const NAME: &'static str = "BeUint32Reader";
622 fn to_entity(&self) -> Self::Entity {
623 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
624 }
625 fn new_unchecked(slice: &'r [u8]) -> Self {
626 BeUint32Reader(slice)
627 }
628 fn as_slice(&self) -> &'r [u8] {
629 self.0
630 }
631 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
632 use molecule::verification_error as ve;
633 let slice_len = slice.len();
634 if slice_len != Self::TOTAL_SIZE {
635 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
636 }
637 Ok(())
638 }
639}
640pub struct BeUint32Builder(pub(crate) [Byte; 4]);
641impl ::core::fmt::Debug for BeUint32Builder {
642 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
643 write!(f, "{}({:?})", Self::NAME, &self.0[..])
644 }
645}
646impl ::core::default::Default for BeUint32Builder {
647 fn default() -> Self {
648 BeUint32Builder([
649 Byte::default(),
650 Byte::default(),
651 Byte::default(),
652 Byte::default(),
653 ])
654 }
655}
656impl BeUint32Builder {
657 pub const TOTAL_SIZE: usize = 4;
658 pub const ITEM_SIZE: usize = 1;
659 pub const ITEM_COUNT: usize = 4;
660 pub fn set(mut self, v: [Byte; 4]) -> Self {
661 self.0 = v;
662 self
663 }
664 pub fn nth0(mut self, v: Byte) -> Self {
665 self.0[0] = v;
666 self
667 }
668 pub fn nth1(mut self, v: Byte) -> Self {
669 self.0[1] = v;
670 self
671 }
672 pub fn nth2(mut self, v: Byte) -> Self {
673 self.0[2] = v;
674 self
675 }
676 pub fn nth3(mut self, v: Byte) -> Self {
677 self.0[3] = v;
678 self
679 }
680}
681impl molecule::prelude::Builder for BeUint32Builder {
682 type Entity = BeUint32;
683 const NAME: &'static str = "BeUint32Builder";
684 fn expected_length(&self) -> usize {
685 Self::TOTAL_SIZE
686 }
687 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
688 writer.write_all(self.0[0].as_slice())?;
689 writer.write_all(self.0[1].as_slice())?;
690 writer.write_all(self.0[2].as_slice())?;
691 writer.write_all(self.0[3].as_slice())?;
692 Ok(())
693 }
694 fn build(&self) -> Self::Entity {
695 let mut inner = Vec::with_capacity(self.expected_length());
696 self.write(&mut inner)
697 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
698 BeUint32::new_unchecked(inner.into())
699 }
700}
701#[derive(Clone)]
702pub struct BeUint64(molecule::bytes::Bytes);
703impl ::core::fmt::LowerHex for BeUint64 {
704 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
705 use molecule::hex_string;
706 if f.alternate() {
707 write!(f, "0x")?;
708 }
709 write!(f, "{}", hex_string(self.as_slice()))
710 }
711}
712impl ::core::fmt::Debug for BeUint64 {
713 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
714 write!(f, "{}({:#x})", Self::NAME, self)
715 }
716}
717impl ::core::fmt::Display for BeUint64 {
718 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
719 use molecule::hex_string;
720 let raw_data = hex_string(&self.raw_data());
721 write!(f, "{}(0x{})", Self::NAME, raw_data)
722 }
723}
724impl ::core::default::Default for BeUint64 {
725 fn default() -> Self {
726 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
727 BeUint64::new_unchecked(v)
728 }
729}
730impl BeUint64 {
731 const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
732 pub const TOTAL_SIZE: usize = 8;
733 pub const ITEM_SIZE: usize = 1;
734 pub const ITEM_COUNT: usize = 8;
735 pub fn nth0(&self) -> Byte {
736 Byte::new_unchecked(self.0.slice(0..1))
737 }
738 pub fn nth1(&self) -> Byte {
739 Byte::new_unchecked(self.0.slice(1..2))
740 }
741 pub fn nth2(&self) -> Byte {
742 Byte::new_unchecked(self.0.slice(2..3))
743 }
744 pub fn nth3(&self) -> Byte {
745 Byte::new_unchecked(self.0.slice(3..4))
746 }
747 pub fn nth4(&self) -> Byte {
748 Byte::new_unchecked(self.0.slice(4..5))
749 }
750 pub fn nth5(&self) -> Byte {
751 Byte::new_unchecked(self.0.slice(5..6))
752 }
753 pub fn nth6(&self) -> Byte {
754 Byte::new_unchecked(self.0.slice(6..7))
755 }
756 pub fn nth7(&self) -> Byte {
757 Byte::new_unchecked(self.0.slice(7..8))
758 }
759 pub fn raw_data(&self) -> molecule::bytes::Bytes {
760 self.as_bytes()
761 }
762 pub fn as_reader<'r>(&'r self) -> BeUint64Reader<'r> {
763 BeUint64Reader::new_unchecked(self.as_slice())
764 }
765}
766impl molecule::prelude::Entity for BeUint64 {
767 type Builder = BeUint64Builder;
768 const NAME: &'static str = "BeUint64";
769 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
770 BeUint64(data)
771 }
772 fn as_bytes(&self) -> molecule::bytes::Bytes {
773 self.0.clone()
774 }
775 fn as_slice(&self) -> &[u8] {
776 &self.0[..]
777 }
778 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
779 BeUint64Reader::from_slice(slice).map(|reader| reader.to_entity())
780 }
781 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
782 BeUint64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
783 }
784 fn new_builder() -> Self::Builder {
785 ::core::default::Default::default()
786 }
787 fn as_builder(self) -> Self::Builder {
788 Self::new_builder().set([
789 self.nth0(),
790 self.nth1(),
791 self.nth2(),
792 self.nth3(),
793 self.nth4(),
794 self.nth5(),
795 self.nth6(),
796 self.nth7(),
797 ])
798 }
799}
800#[derive(Clone, Copy)]
801pub struct BeUint64Reader<'r>(&'r [u8]);
802impl<'r> ::core::fmt::LowerHex for BeUint64Reader<'r> {
803 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
804 use molecule::hex_string;
805 if f.alternate() {
806 write!(f, "0x")?;
807 }
808 write!(f, "{}", hex_string(self.as_slice()))
809 }
810}
811impl<'r> ::core::fmt::Debug for BeUint64Reader<'r> {
812 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
813 write!(f, "{}({:#x})", Self::NAME, self)
814 }
815}
816impl<'r> ::core::fmt::Display for BeUint64Reader<'r> {
817 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
818 use molecule::hex_string;
819 let raw_data = hex_string(&self.raw_data());
820 write!(f, "{}(0x{})", Self::NAME, raw_data)
821 }
822}
823impl<'r> BeUint64Reader<'r> {
824 pub const TOTAL_SIZE: usize = 8;
825 pub const ITEM_SIZE: usize = 1;
826 pub const ITEM_COUNT: usize = 8;
827 pub fn nth0(&self) -> ByteReader<'r> {
828 ByteReader::new_unchecked(&self.as_slice()[0..1])
829 }
830 pub fn nth1(&self) -> ByteReader<'r> {
831 ByteReader::new_unchecked(&self.as_slice()[1..2])
832 }
833 pub fn nth2(&self) -> ByteReader<'r> {
834 ByteReader::new_unchecked(&self.as_slice()[2..3])
835 }
836 pub fn nth3(&self) -> ByteReader<'r> {
837 ByteReader::new_unchecked(&self.as_slice()[3..4])
838 }
839 pub fn nth4(&self) -> ByteReader<'r> {
840 ByteReader::new_unchecked(&self.as_slice()[4..5])
841 }
842 pub fn nth5(&self) -> ByteReader<'r> {
843 ByteReader::new_unchecked(&self.as_slice()[5..6])
844 }
845 pub fn nth6(&self) -> ByteReader<'r> {
846 ByteReader::new_unchecked(&self.as_slice()[6..7])
847 }
848 pub fn nth7(&self) -> ByteReader<'r> {
849 ByteReader::new_unchecked(&self.as_slice()[7..8])
850 }
851 pub fn raw_data(&self) -> &'r [u8] {
852 self.as_slice()
853 }
854}
855impl<'r> molecule::prelude::Reader<'r> for BeUint64Reader<'r> {
856 type Entity = BeUint64;
857 const NAME: &'static str = "BeUint64Reader";
858 fn to_entity(&self) -> Self::Entity {
859 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
860 }
861 fn new_unchecked(slice: &'r [u8]) -> Self {
862 BeUint64Reader(slice)
863 }
864 fn as_slice(&self) -> &'r [u8] {
865 self.0
866 }
867 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
868 use molecule::verification_error as ve;
869 let slice_len = slice.len();
870 if slice_len != Self::TOTAL_SIZE {
871 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
872 }
873 Ok(())
874 }
875}
876pub struct BeUint64Builder(pub(crate) [Byte; 8]);
877impl ::core::fmt::Debug for BeUint64Builder {
878 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
879 write!(f, "{}({:?})", Self::NAME, &self.0[..])
880 }
881}
882impl ::core::default::Default for BeUint64Builder {
883 fn default() -> Self {
884 BeUint64Builder([
885 Byte::default(),
886 Byte::default(),
887 Byte::default(),
888 Byte::default(),
889 Byte::default(),
890 Byte::default(),
891 Byte::default(),
892 Byte::default(),
893 ])
894 }
895}
896impl BeUint64Builder {
897 pub const TOTAL_SIZE: usize = 8;
898 pub const ITEM_SIZE: usize = 1;
899 pub const ITEM_COUNT: usize = 8;
900 pub fn set(mut self, v: [Byte; 8]) -> Self {
901 self.0 = v;
902 self
903 }
904 pub fn nth0(mut self, v: Byte) -> Self {
905 self.0[0] = v;
906 self
907 }
908 pub fn nth1(mut self, v: Byte) -> Self {
909 self.0[1] = v;
910 self
911 }
912 pub fn nth2(mut self, v: Byte) -> Self {
913 self.0[2] = v;
914 self
915 }
916 pub fn nth3(mut self, v: Byte) -> Self {
917 self.0[3] = v;
918 self
919 }
920 pub fn nth4(mut self, v: Byte) -> Self {
921 self.0[4] = v;
922 self
923 }
924 pub fn nth5(mut self, v: Byte) -> Self {
925 self.0[5] = v;
926 self
927 }
928 pub fn nth6(mut self, v: Byte) -> Self {
929 self.0[6] = v;
930 self
931 }
932 pub fn nth7(mut self, v: Byte) -> Self {
933 self.0[7] = v;
934 self
935 }
936}
937impl molecule::prelude::Builder for BeUint64Builder {
938 type Entity = BeUint64;
939 const NAME: &'static str = "BeUint64Builder";
940 fn expected_length(&self) -> usize {
941 Self::TOTAL_SIZE
942 }
943 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
944 writer.write_all(self.0[0].as_slice())?;
945 writer.write_all(self.0[1].as_slice())?;
946 writer.write_all(self.0[2].as_slice())?;
947 writer.write_all(self.0[3].as_slice())?;
948 writer.write_all(self.0[4].as_slice())?;
949 writer.write_all(self.0[5].as_slice())?;
950 writer.write_all(self.0[6].as_slice())?;
951 writer.write_all(self.0[7].as_slice())?;
952 Ok(())
953 }
954 fn build(&self) -> Self::Entity {
955 let mut inner = Vec::with_capacity(self.expected_length());
956 self.write(&mut inner)
957 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
958 BeUint64::new_unchecked(inner.into())
959 }
960}
961#[derive(Clone)]
962pub struct Uint32Vec(molecule::bytes::Bytes);
963impl ::core::fmt::LowerHex for Uint32Vec {
964 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
965 use molecule::hex_string;
966 if f.alternate() {
967 write!(f, "0x")?;
968 }
969 write!(f, "{}", hex_string(self.as_slice()))
970 }
971}
972impl ::core::fmt::Debug for Uint32Vec {
973 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
974 write!(f, "{}({:#x})", Self::NAME, self)
975 }
976}
977impl ::core::fmt::Display for Uint32Vec {
978 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
979 write!(f, "{} [", Self::NAME)?;
980 for i in 0..self.len() {
981 if i == 0 {
982 write!(f, "{}", self.get_unchecked(i))?;
983 } else {
984 write!(f, ", {}", self.get_unchecked(i))?;
985 }
986 }
987 write!(f, "]")
988 }
989}
990impl ::core::default::Default for Uint32Vec {
991 fn default() -> Self {
992 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
993 Uint32Vec::new_unchecked(v)
994 }
995}
996impl Uint32Vec {
997 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
998 pub const ITEM_SIZE: usize = 4;
999 pub fn total_size(&self) -> usize {
1000 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1001 }
1002 pub fn item_count(&self) -> usize {
1003 molecule::unpack_number(self.as_slice()) as usize
1004 }
1005 pub fn len(&self) -> usize {
1006 self.item_count()
1007 }
1008 pub fn is_empty(&self) -> bool {
1009 self.len() == 0
1010 }
1011 pub fn get(&self, idx: usize) -> Option<Uint32> {
1012 if idx >= self.len() {
1013 None
1014 } else {
1015 Some(self.get_unchecked(idx))
1016 }
1017 }
1018 pub fn get_unchecked(&self, idx: usize) -> Uint32 {
1019 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1020 let end = start + Self::ITEM_SIZE;
1021 Uint32::new_unchecked(self.0.slice(start..end))
1022 }
1023 pub fn as_reader<'r>(&'r self) -> Uint32VecReader<'r> {
1024 Uint32VecReader::new_unchecked(self.as_slice())
1025 }
1026}
1027impl molecule::prelude::Entity for Uint32Vec {
1028 type Builder = Uint32VecBuilder;
1029 const NAME: &'static str = "Uint32Vec";
1030 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1031 Uint32Vec(data)
1032 }
1033 fn as_bytes(&self) -> molecule::bytes::Bytes {
1034 self.0.clone()
1035 }
1036 fn as_slice(&self) -> &[u8] {
1037 &self.0[..]
1038 }
1039 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1040 Uint32VecReader::from_slice(slice).map(|reader| reader.to_entity())
1041 }
1042 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1043 Uint32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1044 }
1045 fn new_builder() -> Self::Builder {
1046 ::core::default::Default::default()
1047 }
1048 fn as_builder(self) -> Self::Builder {
1049 Self::new_builder().extend(self.into_iter())
1050 }
1051}
1052#[derive(Clone, Copy)]
1053pub struct Uint32VecReader<'r>(&'r [u8]);
1054impl<'r> ::core::fmt::LowerHex for Uint32VecReader<'r> {
1055 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1056 use molecule::hex_string;
1057 if f.alternate() {
1058 write!(f, "0x")?;
1059 }
1060 write!(f, "{}", hex_string(self.as_slice()))
1061 }
1062}
1063impl<'r> ::core::fmt::Debug for Uint32VecReader<'r> {
1064 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1065 write!(f, "{}({:#x})", Self::NAME, self)
1066 }
1067}
1068impl<'r> ::core::fmt::Display for Uint32VecReader<'r> {
1069 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1070 write!(f, "{} [", Self::NAME)?;
1071 for i in 0..self.len() {
1072 if i == 0 {
1073 write!(f, "{}", self.get_unchecked(i))?;
1074 } else {
1075 write!(f, ", {}", self.get_unchecked(i))?;
1076 }
1077 }
1078 write!(f, "]")
1079 }
1080}
1081impl<'r> Uint32VecReader<'r> {
1082 pub const ITEM_SIZE: usize = 4;
1083 pub fn total_size(&self) -> usize {
1084 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1085 }
1086 pub fn item_count(&self) -> usize {
1087 molecule::unpack_number(self.as_slice()) as usize
1088 }
1089 pub fn len(&self) -> usize {
1090 self.item_count()
1091 }
1092 pub fn is_empty(&self) -> bool {
1093 self.len() == 0
1094 }
1095 pub fn get(&self, idx: usize) -> Option<Uint32Reader<'r>> {
1096 if idx >= self.len() {
1097 None
1098 } else {
1099 Some(self.get_unchecked(idx))
1100 }
1101 }
1102 pub fn get_unchecked(&self, idx: usize) -> Uint32Reader<'r> {
1103 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1104 let end = start + Self::ITEM_SIZE;
1105 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
1106 }
1107}
1108impl<'r> molecule::prelude::Reader<'r> for Uint32VecReader<'r> {
1109 type Entity = Uint32Vec;
1110 const NAME: &'static str = "Uint32VecReader";
1111 fn to_entity(&self) -> Self::Entity {
1112 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1113 }
1114 fn new_unchecked(slice: &'r [u8]) -> Self {
1115 Uint32VecReader(slice)
1116 }
1117 fn as_slice(&self) -> &'r [u8] {
1118 self.0
1119 }
1120 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1121 use molecule::verification_error as ve;
1122 let slice_len = slice.len();
1123 if slice_len < molecule::NUMBER_SIZE {
1124 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
1125 }
1126 let item_count = molecule::unpack_number(slice) as usize;
1127 if item_count == 0 {
1128 if slice_len != molecule::NUMBER_SIZE {
1129 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
1130 }
1131 return Ok(());
1132 }
1133 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
1134 if slice_len != total_size {
1135 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
1136 }
1137 Ok(())
1138 }
1139}
1140#[derive(Debug, Default)]
1141pub struct Uint32VecBuilder(pub(crate) Vec<Uint32>);
1142impl Uint32VecBuilder {
1143 pub const ITEM_SIZE: usize = 4;
1144 pub fn set(mut self, v: Vec<Uint32>) -> Self {
1145 self.0 = v;
1146 self
1147 }
1148 pub fn push(mut self, v: Uint32) -> Self {
1149 self.0.push(v);
1150 self
1151 }
1152 pub fn extend<T: ::core::iter::IntoIterator<Item = Uint32>>(mut self, iter: T) -> Self {
1153 for elem in iter {
1154 self.0.push(elem);
1155 }
1156 self
1157 }
1158 pub fn replace(&mut self, index: usize, v: Uint32) -> Option<Uint32> {
1159 self.0
1160 .get_mut(index)
1161 .map(|item| ::core::mem::replace(item, v))
1162 }
1163}
1164impl molecule::prelude::Builder for Uint32VecBuilder {
1165 type Entity = Uint32Vec;
1166 const NAME: &'static str = "Uint32VecBuilder";
1167 fn expected_length(&self) -> usize {
1168 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
1169 }
1170 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1171 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
1172 for inner in &self.0[..] {
1173 writer.write_all(inner.as_slice())?;
1174 }
1175 Ok(())
1176 }
1177 fn build(&self) -> Self::Entity {
1178 let mut inner = Vec::with_capacity(self.expected_length());
1179 self.write(&mut inner)
1180 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1181 Uint32Vec::new_unchecked(inner.into())
1182 }
1183}
1184pub struct Uint32VecIterator(Uint32Vec, usize, usize);
1185impl ::core::iter::Iterator for Uint32VecIterator {
1186 type Item = Uint32;
1187 fn next(&mut self) -> Option<Self::Item> {
1188 if self.1 >= self.2 {
1189 None
1190 } else {
1191 let ret = self.0.get_unchecked(self.1);
1192 self.1 += 1;
1193 Some(ret)
1194 }
1195 }
1196}
1197impl ::core::iter::ExactSizeIterator for Uint32VecIterator {
1198 fn len(&self) -> usize {
1199 self.2 - self.1
1200 }
1201}
1202impl ::core::iter::IntoIterator for Uint32Vec {
1203 type Item = Uint32;
1204 type IntoIter = Uint32VecIterator;
1205 fn into_iter(self) -> Self::IntoIter {
1206 let len = self.len();
1207 Uint32VecIterator(self, 0, len)
1208 }
1209}
1210impl<'r> Uint32VecReader<'r> {
1211 pub fn iter<'t>(&'t self) -> Uint32VecReaderIterator<'t, 'r> {
1212 Uint32VecReaderIterator(&self, 0, self.len())
1213 }
1214}
1215pub struct Uint32VecReaderIterator<'t, 'r>(&'t Uint32VecReader<'r>, usize, usize);
1216impl<'t: 'r, 'r> ::core::iter::Iterator for Uint32VecReaderIterator<'t, 'r> {
1217 type Item = Uint32Reader<'t>;
1218 fn next(&mut self) -> Option<Self::Item> {
1219 if self.1 >= self.2 {
1220 None
1221 } else {
1222 let ret = self.0.get_unchecked(self.1);
1223 self.1 += 1;
1224 Some(ret)
1225 }
1226 }
1227}
1228impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Uint32VecReaderIterator<'t, 'r> {
1229 fn len(&self) -> usize {
1230 self.2 - self.1
1231 }
1232}
1233#[derive(Clone)]
1234pub struct Uint64Vec(molecule::bytes::Bytes);
1235impl ::core::fmt::LowerHex for Uint64Vec {
1236 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1237 use molecule::hex_string;
1238 if f.alternate() {
1239 write!(f, "0x")?;
1240 }
1241 write!(f, "{}", hex_string(self.as_slice()))
1242 }
1243}
1244impl ::core::fmt::Debug for Uint64Vec {
1245 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1246 write!(f, "{}({:#x})", Self::NAME, self)
1247 }
1248}
1249impl ::core::fmt::Display for Uint64Vec {
1250 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1251 write!(f, "{} [", Self::NAME)?;
1252 for i in 0..self.len() {
1253 if i == 0 {
1254 write!(f, "{}", self.get_unchecked(i))?;
1255 } else {
1256 write!(f, ", {}", self.get_unchecked(i))?;
1257 }
1258 }
1259 write!(f, "]")
1260 }
1261}
1262impl ::core::default::Default for Uint64Vec {
1263 fn default() -> Self {
1264 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1265 Uint64Vec::new_unchecked(v)
1266 }
1267}
1268impl Uint64Vec {
1269 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
1270 pub const ITEM_SIZE: usize = 8;
1271 pub fn total_size(&self) -> usize {
1272 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1273 }
1274 pub fn item_count(&self) -> usize {
1275 molecule::unpack_number(self.as_slice()) as usize
1276 }
1277 pub fn len(&self) -> usize {
1278 self.item_count()
1279 }
1280 pub fn is_empty(&self) -> bool {
1281 self.len() == 0
1282 }
1283 pub fn get(&self, idx: usize) -> Option<Uint64> {
1284 if idx >= self.len() {
1285 None
1286 } else {
1287 Some(self.get_unchecked(idx))
1288 }
1289 }
1290 pub fn get_unchecked(&self, idx: usize) -> Uint64 {
1291 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1292 let end = start + Self::ITEM_SIZE;
1293 Uint64::new_unchecked(self.0.slice(start..end))
1294 }
1295 pub fn as_reader<'r>(&'r self) -> Uint64VecReader<'r> {
1296 Uint64VecReader::new_unchecked(self.as_slice())
1297 }
1298}
1299impl molecule::prelude::Entity for Uint64Vec {
1300 type Builder = Uint64VecBuilder;
1301 const NAME: &'static str = "Uint64Vec";
1302 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1303 Uint64Vec(data)
1304 }
1305 fn as_bytes(&self) -> molecule::bytes::Bytes {
1306 self.0.clone()
1307 }
1308 fn as_slice(&self) -> &[u8] {
1309 &self.0[..]
1310 }
1311 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1312 Uint64VecReader::from_slice(slice).map(|reader| reader.to_entity())
1313 }
1314 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1315 Uint64VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1316 }
1317 fn new_builder() -> Self::Builder {
1318 ::core::default::Default::default()
1319 }
1320 fn as_builder(self) -> Self::Builder {
1321 Self::new_builder().extend(self.into_iter())
1322 }
1323}
1324#[derive(Clone, Copy)]
1325pub struct Uint64VecReader<'r>(&'r [u8]);
1326impl<'r> ::core::fmt::LowerHex for Uint64VecReader<'r> {
1327 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1328 use molecule::hex_string;
1329 if f.alternate() {
1330 write!(f, "0x")?;
1331 }
1332 write!(f, "{}", hex_string(self.as_slice()))
1333 }
1334}
1335impl<'r> ::core::fmt::Debug for Uint64VecReader<'r> {
1336 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1337 write!(f, "{}({:#x})", Self::NAME, self)
1338 }
1339}
1340impl<'r> ::core::fmt::Display for Uint64VecReader<'r> {
1341 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1342 write!(f, "{} [", Self::NAME)?;
1343 for i in 0..self.len() {
1344 if i == 0 {
1345 write!(f, "{}", self.get_unchecked(i))?;
1346 } else {
1347 write!(f, ", {}", self.get_unchecked(i))?;
1348 }
1349 }
1350 write!(f, "]")
1351 }
1352}
1353impl<'r> Uint64VecReader<'r> {
1354 pub const ITEM_SIZE: usize = 8;
1355 pub fn total_size(&self) -> usize {
1356 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1357 }
1358 pub fn item_count(&self) -> usize {
1359 molecule::unpack_number(self.as_slice()) as usize
1360 }
1361 pub fn len(&self) -> usize {
1362 self.item_count()
1363 }
1364 pub fn is_empty(&self) -> bool {
1365 self.len() == 0
1366 }
1367 pub fn get(&self, idx: usize) -> Option<Uint64Reader<'r>> {
1368 if idx >= self.len() {
1369 None
1370 } else {
1371 Some(self.get_unchecked(idx))
1372 }
1373 }
1374 pub fn get_unchecked(&self, idx: usize) -> Uint64Reader<'r> {
1375 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1376 let end = start + Self::ITEM_SIZE;
1377 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
1378 }
1379}
1380impl<'r> molecule::prelude::Reader<'r> for Uint64VecReader<'r> {
1381 type Entity = Uint64Vec;
1382 const NAME: &'static str = "Uint64VecReader";
1383 fn to_entity(&self) -> Self::Entity {
1384 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1385 }
1386 fn new_unchecked(slice: &'r [u8]) -> Self {
1387 Uint64VecReader(slice)
1388 }
1389 fn as_slice(&self) -> &'r [u8] {
1390 self.0
1391 }
1392 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1393 use molecule::verification_error as ve;
1394 let slice_len = slice.len();
1395 if slice_len < molecule::NUMBER_SIZE {
1396 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
1397 }
1398 let item_count = molecule::unpack_number(slice) as usize;
1399 if item_count == 0 {
1400 if slice_len != molecule::NUMBER_SIZE {
1401 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
1402 }
1403 return Ok(());
1404 }
1405 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
1406 if slice_len != total_size {
1407 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
1408 }
1409 Ok(())
1410 }
1411}
1412#[derive(Debug, Default)]
1413pub struct Uint64VecBuilder(pub(crate) Vec<Uint64>);
1414impl Uint64VecBuilder {
1415 pub const ITEM_SIZE: usize = 8;
1416 pub fn set(mut self, v: Vec<Uint64>) -> Self {
1417 self.0 = v;
1418 self
1419 }
1420 pub fn push(mut self, v: Uint64) -> Self {
1421 self.0.push(v);
1422 self
1423 }
1424 pub fn extend<T: ::core::iter::IntoIterator<Item = Uint64>>(mut self, iter: T) -> Self {
1425 for elem in iter {
1426 self.0.push(elem);
1427 }
1428 self
1429 }
1430 pub fn replace(&mut self, index: usize, v: Uint64) -> Option<Uint64> {
1431 self.0
1432 .get_mut(index)
1433 .map(|item| ::core::mem::replace(item, v))
1434 }
1435}
1436impl molecule::prelude::Builder for Uint64VecBuilder {
1437 type Entity = Uint64Vec;
1438 const NAME: &'static str = "Uint64VecBuilder";
1439 fn expected_length(&self) -> usize {
1440 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
1441 }
1442 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1443 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
1444 for inner in &self.0[..] {
1445 writer.write_all(inner.as_slice())?;
1446 }
1447 Ok(())
1448 }
1449 fn build(&self) -> Self::Entity {
1450 let mut inner = Vec::with_capacity(self.expected_length());
1451 self.write(&mut inner)
1452 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1453 Uint64Vec::new_unchecked(inner.into())
1454 }
1455}
1456pub struct Uint64VecIterator(Uint64Vec, usize, usize);
1457impl ::core::iter::Iterator for Uint64VecIterator {
1458 type Item = Uint64;
1459 fn next(&mut self) -> Option<Self::Item> {
1460 if self.1 >= self.2 {
1461 None
1462 } else {
1463 let ret = self.0.get_unchecked(self.1);
1464 self.1 += 1;
1465 Some(ret)
1466 }
1467 }
1468}
1469impl ::core::iter::ExactSizeIterator for Uint64VecIterator {
1470 fn len(&self) -> usize {
1471 self.2 - self.1
1472 }
1473}
1474impl ::core::iter::IntoIterator for Uint64Vec {
1475 type Item = Uint64;
1476 type IntoIter = Uint64VecIterator;
1477 fn into_iter(self) -> Self::IntoIter {
1478 let len = self.len();
1479 Uint64VecIterator(self, 0, len)
1480 }
1481}
1482impl<'r> Uint64VecReader<'r> {
1483 pub fn iter<'t>(&'t self) -> Uint64VecReaderIterator<'t, 'r> {
1484 Uint64VecReaderIterator(&self, 0, self.len())
1485 }
1486}
1487pub struct Uint64VecReaderIterator<'t, 'r>(&'t Uint64VecReader<'r>, usize, usize);
1488impl<'t: 'r, 'r> ::core::iter::Iterator for Uint64VecReaderIterator<'t, 'r> {
1489 type Item = Uint64Reader<'t>;
1490 fn next(&mut self) -> Option<Self::Item> {
1491 if self.1 >= self.2 {
1492 None
1493 } else {
1494 let ret = self.0.get_unchecked(self.1);
1495 self.1 += 1;
1496 Some(ret)
1497 }
1498 }
1499}
1500impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Uint64VecReaderIterator<'t, 'r> {
1501 fn len(&self) -> usize {
1502 self.2 - self.1
1503 }
1504}
1505#[derive(Clone)]
1506pub struct Uint256Vec(molecule::bytes::Bytes);
1507impl ::core::fmt::LowerHex for Uint256Vec {
1508 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1509 use molecule::hex_string;
1510 if f.alternate() {
1511 write!(f, "0x")?;
1512 }
1513 write!(f, "{}", hex_string(self.as_slice()))
1514 }
1515}
1516impl ::core::fmt::Debug for Uint256Vec {
1517 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1518 write!(f, "{}({:#x})", Self::NAME, self)
1519 }
1520}
1521impl ::core::fmt::Display for Uint256Vec {
1522 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1523 write!(f, "{} [", Self::NAME)?;
1524 for i in 0..self.len() {
1525 if i == 0 {
1526 write!(f, "{}", self.get_unchecked(i))?;
1527 } else {
1528 write!(f, ", {}", self.get_unchecked(i))?;
1529 }
1530 }
1531 write!(f, "]")
1532 }
1533}
1534impl ::core::default::Default for Uint256Vec {
1535 fn default() -> Self {
1536 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1537 Uint256Vec::new_unchecked(v)
1538 }
1539}
1540impl Uint256Vec {
1541 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
1542 pub const ITEM_SIZE: usize = 32;
1543 pub fn total_size(&self) -> usize {
1544 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1545 }
1546 pub fn item_count(&self) -> usize {
1547 molecule::unpack_number(self.as_slice()) as usize
1548 }
1549 pub fn len(&self) -> usize {
1550 self.item_count()
1551 }
1552 pub fn is_empty(&self) -> bool {
1553 self.len() == 0
1554 }
1555 pub fn get(&self, idx: usize) -> Option<Uint256> {
1556 if idx >= self.len() {
1557 None
1558 } else {
1559 Some(self.get_unchecked(idx))
1560 }
1561 }
1562 pub fn get_unchecked(&self, idx: usize) -> Uint256 {
1563 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1564 let end = start + Self::ITEM_SIZE;
1565 Uint256::new_unchecked(self.0.slice(start..end))
1566 }
1567 pub fn as_reader<'r>(&'r self) -> Uint256VecReader<'r> {
1568 Uint256VecReader::new_unchecked(self.as_slice())
1569 }
1570}
1571impl molecule::prelude::Entity for Uint256Vec {
1572 type Builder = Uint256VecBuilder;
1573 const NAME: &'static str = "Uint256Vec";
1574 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1575 Uint256Vec(data)
1576 }
1577 fn as_bytes(&self) -> molecule::bytes::Bytes {
1578 self.0.clone()
1579 }
1580 fn as_slice(&self) -> &[u8] {
1581 &self.0[..]
1582 }
1583 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1584 Uint256VecReader::from_slice(slice).map(|reader| reader.to_entity())
1585 }
1586 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1587 Uint256VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1588 }
1589 fn new_builder() -> Self::Builder {
1590 ::core::default::Default::default()
1591 }
1592 fn as_builder(self) -> Self::Builder {
1593 Self::new_builder().extend(self.into_iter())
1594 }
1595}
1596#[derive(Clone, Copy)]
1597pub struct Uint256VecReader<'r>(&'r [u8]);
1598impl<'r> ::core::fmt::LowerHex for Uint256VecReader<'r> {
1599 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1600 use molecule::hex_string;
1601 if f.alternate() {
1602 write!(f, "0x")?;
1603 }
1604 write!(f, "{}", hex_string(self.as_slice()))
1605 }
1606}
1607impl<'r> ::core::fmt::Debug for Uint256VecReader<'r> {
1608 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1609 write!(f, "{}({:#x})", Self::NAME, self)
1610 }
1611}
1612impl<'r> ::core::fmt::Display for Uint256VecReader<'r> {
1613 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1614 write!(f, "{} [", Self::NAME)?;
1615 for i in 0..self.len() {
1616 if i == 0 {
1617 write!(f, "{}", self.get_unchecked(i))?;
1618 } else {
1619 write!(f, ", {}", self.get_unchecked(i))?;
1620 }
1621 }
1622 write!(f, "]")
1623 }
1624}
1625impl<'r> Uint256VecReader<'r> {
1626 pub const ITEM_SIZE: usize = 32;
1627 pub fn total_size(&self) -> usize {
1628 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1629 }
1630 pub fn item_count(&self) -> usize {
1631 molecule::unpack_number(self.as_slice()) as usize
1632 }
1633 pub fn len(&self) -> usize {
1634 self.item_count()
1635 }
1636 pub fn is_empty(&self) -> bool {
1637 self.len() == 0
1638 }
1639 pub fn get(&self, idx: usize) -> Option<Uint256Reader<'r>> {
1640 if idx >= self.len() {
1641 None
1642 } else {
1643 Some(self.get_unchecked(idx))
1644 }
1645 }
1646 pub fn get_unchecked(&self, idx: usize) -> Uint256Reader<'r> {
1647 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
1648 let end = start + Self::ITEM_SIZE;
1649 Uint256Reader::new_unchecked(&self.as_slice()[start..end])
1650 }
1651}
1652impl<'r> molecule::prelude::Reader<'r> for Uint256VecReader<'r> {
1653 type Entity = Uint256Vec;
1654 const NAME: &'static str = "Uint256VecReader";
1655 fn to_entity(&self) -> Self::Entity {
1656 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1657 }
1658 fn new_unchecked(slice: &'r [u8]) -> Self {
1659 Uint256VecReader(slice)
1660 }
1661 fn as_slice(&self) -> &'r [u8] {
1662 self.0
1663 }
1664 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1665 use molecule::verification_error as ve;
1666 let slice_len = slice.len();
1667 if slice_len < molecule::NUMBER_SIZE {
1668 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
1669 }
1670 let item_count = molecule::unpack_number(slice) as usize;
1671 if item_count == 0 {
1672 if slice_len != molecule::NUMBER_SIZE {
1673 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
1674 }
1675 return Ok(());
1676 }
1677 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
1678 if slice_len != total_size {
1679 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
1680 }
1681 Ok(())
1682 }
1683}
1684#[derive(Debug, Default)]
1685pub struct Uint256VecBuilder(pub(crate) Vec<Uint256>);
1686impl Uint256VecBuilder {
1687 pub const ITEM_SIZE: usize = 32;
1688 pub fn set(mut self, v: Vec<Uint256>) -> Self {
1689 self.0 = v;
1690 self
1691 }
1692 pub fn push(mut self, v: Uint256) -> Self {
1693 self.0.push(v);
1694 self
1695 }
1696 pub fn extend<T: ::core::iter::IntoIterator<Item = Uint256>>(mut self, iter: T) -> Self {
1697 for elem in iter {
1698 self.0.push(elem);
1699 }
1700 self
1701 }
1702 pub fn replace(&mut self, index: usize, v: Uint256) -> Option<Uint256> {
1703 self.0
1704 .get_mut(index)
1705 .map(|item| ::core::mem::replace(item, v))
1706 }
1707}
1708impl molecule::prelude::Builder for Uint256VecBuilder {
1709 type Entity = Uint256Vec;
1710 const NAME: &'static str = "Uint256VecBuilder";
1711 fn expected_length(&self) -> usize {
1712 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
1713 }
1714 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1715 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
1716 for inner in &self.0[..] {
1717 writer.write_all(inner.as_slice())?;
1718 }
1719 Ok(())
1720 }
1721 fn build(&self) -> Self::Entity {
1722 let mut inner = Vec::with_capacity(self.expected_length());
1723 self.write(&mut inner)
1724 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1725 Uint256Vec::new_unchecked(inner.into())
1726 }
1727}
1728pub struct Uint256VecIterator(Uint256Vec, usize, usize);
1729impl ::core::iter::Iterator for Uint256VecIterator {
1730 type Item = Uint256;
1731 fn next(&mut self) -> Option<Self::Item> {
1732 if self.1 >= self.2 {
1733 None
1734 } else {
1735 let ret = self.0.get_unchecked(self.1);
1736 self.1 += 1;
1737 Some(ret)
1738 }
1739 }
1740}
1741impl ::core::iter::ExactSizeIterator for Uint256VecIterator {
1742 fn len(&self) -> usize {
1743 self.2 - self.1
1744 }
1745}
1746impl ::core::iter::IntoIterator for Uint256Vec {
1747 type Item = Uint256;
1748 type IntoIter = Uint256VecIterator;
1749 fn into_iter(self) -> Self::IntoIter {
1750 let len = self.len();
1751 Uint256VecIterator(self, 0, len)
1752 }
1753}
1754impl<'r> Uint256VecReader<'r> {
1755 pub fn iter<'t>(&'t self) -> Uint256VecReaderIterator<'t, 'r> {
1756 Uint256VecReaderIterator(&self, 0, self.len())
1757 }
1758}
1759pub struct Uint256VecReaderIterator<'t, 'r>(&'t Uint256VecReader<'r>, usize, usize);
1760impl<'t: 'r, 'r> ::core::iter::Iterator for Uint256VecReaderIterator<'t, 'r> {
1761 type Item = Uint256Reader<'t>;
1762 fn next(&mut self) -> Option<Self::Item> {
1763 if self.1 >= self.2 {
1764 None
1765 } else {
1766 let ret = self.0.get_unchecked(self.1);
1767 self.1 += 1;
1768 Some(ret)
1769 }
1770 }
1771}
1772impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Uint256VecReaderIterator<'t, 'r> {
1773 fn len(&self) -> usize {
1774 self.2 - self.1
1775 }
1776}
1777#[derive(Clone)]
1778pub struct CellOutputOpt(molecule::bytes::Bytes);
1779impl ::core::fmt::LowerHex for CellOutputOpt {
1780 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1781 use molecule::hex_string;
1782 if f.alternate() {
1783 write!(f, "0x")?;
1784 }
1785 write!(f, "{}", hex_string(self.as_slice()))
1786 }
1787}
1788impl ::core::fmt::Debug for CellOutputOpt {
1789 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1790 write!(f, "{}({:#x})", Self::NAME, self)
1791 }
1792}
1793impl ::core::fmt::Display for CellOutputOpt {
1794 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1795 if let Some(v) = self.to_opt() {
1796 write!(f, "{}(Some({}))", Self::NAME, v)
1797 } else {
1798 write!(f, "{}(None)", Self::NAME)
1799 }
1800 }
1801}
1802impl ::core::default::Default for CellOutputOpt {
1803 fn default() -> Self {
1804 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1805 CellOutputOpt::new_unchecked(v)
1806 }
1807}
1808impl CellOutputOpt {
1809 const DEFAULT_VALUE: [u8; 0] = [];
1810 pub fn is_none(&self) -> bool {
1811 self.0.is_empty()
1812 }
1813 pub fn is_some(&self) -> bool {
1814 !self.0.is_empty()
1815 }
1816 pub fn to_opt(&self) -> Option<CellOutput> {
1817 if self.is_none() {
1818 None
1819 } else {
1820 Some(CellOutput::new_unchecked(self.0.clone()))
1821 }
1822 }
1823 pub fn as_reader<'r>(&'r self) -> CellOutputOptReader<'r> {
1824 CellOutputOptReader::new_unchecked(self.as_slice())
1825 }
1826}
1827impl molecule::prelude::Entity for CellOutputOpt {
1828 type Builder = CellOutputOptBuilder;
1829 const NAME: &'static str = "CellOutputOpt";
1830 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1831 CellOutputOpt(data)
1832 }
1833 fn as_bytes(&self) -> molecule::bytes::Bytes {
1834 self.0.clone()
1835 }
1836 fn as_slice(&self) -> &[u8] {
1837 &self.0[..]
1838 }
1839 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1840 CellOutputOptReader::from_slice(slice).map(|reader| reader.to_entity())
1841 }
1842 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1843 CellOutputOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1844 }
1845 fn new_builder() -> Self::Builder {
1846 ::core::default::Default::default()
1847 }
1848 fn as_builder(self) -> Self::Builder {
1849 Self::new_builder().set(self.to_opt())
1850 }
1851}
1852#[derive(Clone, Copy)]
1853pub struct CellOutputOptReader<'r>(&'r [u8]);
1854impl<'r> ::core::fmt::LowerHex for CellOutputOptReader<'r> {
1855 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1856 use molecule::hex_string;
1857 if f.alternate() {
1858 write!(f, "0x")?;
1859 }
1860 write!(f, "{}", hex_string(self.as_slice()))
1861 }
1862}
1863impl<'r> ::core::fmt::Debug for CellOutputOptReader<'r> {
1864 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1865 write!(f, "{}({:#x})", Self::NAME, self)
1866 }
1867}
1868impl<'r> ::core::fmt::Display for CellOutputOptReader<'r> {
1869 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1870 if let Some(v) = self.to_opt() {
1871 write!(f, "{}(Some({}))", Self::NAME, v)
1872 } else {
1873 write!(f, "{}(None)", Self::NAME)
1874 }
1875 }
1876}
1877impl<'r> CellOutputOptReader<'r> {
1878 pub fn is_none(&self) -> bool {
1879 self.0.is_empty()
1880 }
1881 pub fn is_some(&self) -> bool {
1882 !self.0.is_empty()
1883 }
1884 pub fn to_opt(&self) -> Option<CellOutputReader<'r>> {
1885 if self.is_none() {
1886 None
1887 } else {
1888 Some(CellOutputReader::new_unchecked(self.as_slice()))
1889 }
1890 }
1891}
1892impl<'r> molecule::prelude::Reader<'r> for CellOutputOptReader<'r> {
1893 type Entity = CellOutputOpt;
1894 const NAME: &'static str = "CellOutputOptReader";
1895 fn to_entity(&self) -> Self::Entity {
1896 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1897 }
1898 fn new_unchecked(slice: &'r [u8]) -> Self {
1899 CellOutputOptReader(slice)
1900 }
1901 fn as_slice(&self) -> &'r [u8] {
1902 self.0
1903 }
1904 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
1905 if !slice.is_empty() {
1906 CellOutputReader::verify(&slice[..], compatible)?;
1907 }
1908 Ok(())
1909 }
1910}
1911#[derive(Debug, Default)]
1912pub struct CellOutputOptBuilder(pub(crate) Option<CellOutput>);
1913impl CellOutputOptBuilder {
1914 pub fn set(mut self, v: Option<CellOutput>) -> Self {
1915 self.0 = v;
1916 self
1917 }
1918}
1919impl molecule::prelude::Builder for CellOutputOptBuilder {
1920 type Entity = CellOutputOpt;
1921 const NAME: &'static str = "CellOutputOptBuilder";
1922 fn expected_length(&self) -> usize {
1923 self.0
1924 .as_ref()
1925 .map(|ref inner| inner.as_slice().len())
1926 .unwrap_or(0)
1927 }
1928 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1929 self.0
1930 .as_ref()
1931 .map(|ref inner| writer.write_all(inner.as_slice()))
1932 .unwrap_or(Ok(()))
1933 }
1934 fn build(&self) -> Self::Entity {
1935 let mut inner = Vec::with_capacity(self.expected_length());
1936 self.write(&mut inner)
1937 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1938 CellOutputOpt::new_unchecked(inner.into())
1939 }
1940}
1941#[derive(Clone)]
1942pub struct HeaderVec(molecule::bytes::Bytes);
1943impl ::core::fmt::LowerHex for HeaderVec {
1944 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1945 use molecule::hex_string;
1946 if f.alternate() {
1947 write!(f, "0x")?;
1948 }
1949 write!(f, "{}", hex_string(self.as_slice()))
1950 }
1951}
1952impl ::core::fmt::Debug for HeaderVec {
1953 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1954 write!(f, "{}({:#x})", Self::NAME, self)
1955 }
1956}
1957impl ::core::fmt::Display for HeaderVec {
1958 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1959 write!(f, "{} [", Self::NAME)?;
1960 for i in 0..self.len() {
1961 if i == 0 {
1962 write!(f, "{}", self.get_unchecked(i))?;
1963 } else {
1964 write!(f, ", {}", self.get_unchecked(i))?;
1965 }
1966 }
1967 write!(f, "]")
1968 }
1969}
1970impl ::core::default::Default for HeaderVec {
1971 fn default() -> Self {
1972 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1973 HeaderVec::new_unchecked(v)
1974 }
1975}
1976impl HeaderVec {
1977 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
1978 pub const ITEM_SIZE: usize = 208;
1979 pub fn total_size(&self) -> usize {
1980 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
1981 }
1982 pub fn item_count(&self) -> usize {
1983 molecule::unpack_number(self.as_slice()) as usize
1984 }
1985 pub fn len(&self) -> usize {
1986 self.item_count()
1987 }
1988 pub fn is_empty(&self) -> bool {
1989 self.len() == 0
1990 }
1991 pub fn get(&self, idx: usize) -> Option<Header> {
1992 if idx >= self.len() {
1993 None
1994 } else {
1995 Some(self.get_unchecked(idx))
1996 }
1997 }
1998 pub fn get_unchecked(&self, idx: usize) -> Header {
1999 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2000 let end = start + Self::ITEM_SIZE;
2001 Header::new_unchecked(self.0.slice(start..end))
2002 }
2003 pub fn as_reader<'r>(&'r self) -> HeaderVecReader<'r> {
2004 HeaderVecReader::new_unchecked(self.as_slice())
2005 }
2006}
2007impl molecule::prelude::Entity for HeaderVec {
2008 type Builder = HeaderVecBuilder;
2009 const NAME: &'static str = "HeaderVec";
2010 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2011 HeaderVec(data)
2012 }
2013 fn as_bytes(&self) -> molecule::bytes::Bytes {
2014 self.0.clone()
2015 }
2016 fn as_slice(&self) -> &[u8] {
2017 &self.0[..]
2018 }
2019 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2020 HeaderVecReader::from_slice(slice).map(|reader| reader.to_entity())
2021 }
2022 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2023 HeaderVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2024 }
2025 fn new_builder() -> Self::Builder {
2026 ::core::default::Default::default()
2027 }
2028 fn as_builder(self) -> Self::Builder {
2029 Self::new_builder().extend(self.into_iter())
2030 }
2031}
2032#[derive(Clone, Copy)]
2033pub struct HeaderVecReader<'r>(&'r [u8]);
2034impl<'r> ::core::fmt::LowerHex for HeaderVecReader<'r> {
2035 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2036 use molecule::hex_string;
2037 if f.alternate() {
2038 write!(f, "0x")?;
2039 }
2040 write!(f, "{}", hex_string(self.as_slice()))
2041 }
2042}
2043impl<'r> ::core::fmt::Debug for HeaderVecReader<'r> {
2044 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2045 write!(f, "{}({:#x})", Self::NAME, self)
2046 }
2047}
2048impl<'r> ::core::fmt::Display for HeaderVecReader<'r> {
2049 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2050 write!(f, "{} [", Self::NAME)?;
2051 for i in 0..self.len() {
2052 if i == 0 {
2053 write!(f, "{}", self.get_unchecked(i))?;
2054 } else {
2055 write!(f, ", {}", self.get_unchecked(i))?;
2056 }
2057 }
2058 write!(f, "]")
2059 }
2060}
2061impl<'r> HeaderVecReader<'r> {
2062 pub const ITEM_SIZE: usize = 208;
2063 pub fn total_size(&self) -> usize {
2064 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
2065 }
2066 pub fn item_count(&self) -> usize {
2067 molecule::unpack_number(self.as_slice()) as usize
2068 }
2069 pub fn len(&self) -> usize {
2070 self.item_count()
2071 }
2072 pub fn is_empty(&self) -> bool {
2073 self.len() == 0
2074 }
2075 pub fn get(&self, idx: usize) -> Option<HeaderReader<'r>> {
2076 if idx >= self.len() {
2077 None
2078 } else {
2079 Some(self.get_unchecked(idx))
2080 }
2081 }
2082 pub fn get_unchecked(&self, idx: usize) -> HeaderReader<'r> {
2083 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2084 let end = start + Self::ITEM_SIZE;
2085 HeaderReader::new_unchecked(&self.as_slice()[start..end])
2086 }
2087}
2088impl<'r> molecule::prelude::Reader<'r> for HeaderVecReader<'r> {
2089 type Entity = HeaderVec;
2090 const NAME: &'static str = "HeaderVecReader";
2091 fn to_entity(&self) -> Self::Entity {
2092 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2093 }
2094 fn new_unchecked(slice: &'r [u8]) -> Self {
2095 HeaderVecReader(slice)
2096 }
2097 fn as_slice(&self) -> &'r [u8] {
2098 self.0
2099 }
2100 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
2101 use molecule::verification_error as ve;
2102 let slice_len = slice.len();
2103 if slice_len < molecule::NUMBER_SIZE {
2104 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2105 }
2106 let item_count = molecule::unpack_number(slice) as usize;
2107 if item_count == 0 {
2108 if slice_len != molecule::NUMBER_SIZE {
2109 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
2110 }
2111 return Ok(());
2112 }
2113 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
2114 if slice_len != total_size {
2115 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2116 }
2117 Ok(())
2118 }
2119}
2120#[derive(Debug, Default)]
2121pub struct HeaderVecBuilder(pub(crate) Vec<Header>);
2122impl HeaderVecBuilder {
2123 pub const ITEM_SIZE: usize = 208;
2124 pub fn set(mut self, v: Vec<Header>) -> Self {
2125 self.0 = v;
2126 self
2127 }
2128 pub fn push(mut self, v: Header) -> Self {
2129 self.0.push(v);
2130 self
2131 }
2132 pub fn extend<T: ::core::iter::IntoIterator<Item = Header>>(mut self, iter: T) -> Self {
2133 for elem in iter {
2134 self.0.push(elem);
2135 }
2136 self
2137 }
2138 pub fn replace(&mut self, index: usize, v: Header) -> Option<Header> {
2139 self.0
2140 .get_mut(index)
2141 .map(|item| ::core::mem::replace(item, v))
2142 }
2143}
2144impl molecule::prelude::Builder for HeaderVecBuilder {
2145 type Entity = HeaderVec;
2146 const NAME: &'static str = "HeaderVecBuilder";
2147 fn expected_length(&self) -> usize {
2148 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
2149 }
2150 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2151 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
2152 for inner in &self.0[..] {
2153 writer.write_all(inner.as_slice())?;
2154 }
2155 Ok(())
2156 }
2157 fn build(&self) -> Self::Entity {
2158 let mut inner = Vec::with_capacity(self.expected_length());
2159 self.write(&mut inner)
2160 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2161 HeaderVec::new_unchecked(inner.into())
2162 }
2163}
2164pub struct HeaderVecIterator(HeaderVec, usize, usize);
2165impl ::core::iter::Iterator for HeaderVecIterator {
2166 type Item = Header;
2167 fn next(&mut self) -> Option<Self::Item> {
2168 if self.1 >= self.2 {
2169 None
2170 } else {
2171 let ret = self.0.get_unchecked(self.1);
2172 self.1 += 1;
2173 Some(ret)
2174 }
2175 }
2176}
2177impl ::core::iter::ExactSizeIterator for HeaderVecIterator {
2178 fn len(&self) -> usize {
2179 self.2 - self.1
2180 }
2181}
2182impl ::core::iter::IntoIterator for HeaderVec {
2183 type Item = Header;
2184 type IntoIter = HeaderVecIterator;
2185 fn into_iter(self) -> Self::IntoIter {
2186 let len = self.len();
2187 HeaderVecIterator(self, 0, len)
2188 }
2189}
2190impl<'r> HeaderVecReader<'r> {
2191 pub fn iter<'t>(&'t self) -> HeaderVecReaderIterator<'t, 'r> {
2192 HeaderVecReaderIterator(&self, 0, self.len())
2193 }
2194}
2195pub struct HeaderVecReaderIterator<'t, 'r>(&'t HeaderVecReader<'r>, usize, usize);
2196impl<'t: 'r, 'r> ::core::iter::Iterator for HeaderVecReaderIterator<'t, 'r> {
2197 type Item = HeaderReader<'t>;
2198 fn next(&mut self) -> Option<Self::Item> {
2199 if self.1 >= self.2 {
2200 None
2201 } else {
2202 let ret = self.0.get_unchecked(self.1);
2203 self.1 += 1;
2204 Some(ret)
2205 }
2206 }
2207}
2208impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for HeaderVecReaderIterator<'t, 'r> {
2209 fn len(&self) -> usize {
2210 self.2 - self.1
2211 }
2212}
2213#[derive(Clone)]
2214pub struct OutPointVec(molecule::bytes::Bytes);
2215impl ::core::fmt::LowerHex for OutPointVec {
2216 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2217 use molecule::hex_string;
2218 if f.alternate() {
2219 write!(f, "0x")?;
2220 }
2221 write!(f, "{}", hex_string(self.as_slice()))
2222 }
2223}
2224impl ::core::fmt::Debug for OutPointVec {
2225 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2226 write!(f, "{}({:#x})", Self::NAME, self)
2227 }
2228}
2229impl ::core::fmt::Display for OutPointVec {
2230 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2231 write!(f, "{} [", Self::NAME)?;
2232 for i in 0..self.len() {
2233 if i == 0 {
2234 write!(f, "{}", self.get_unchecked(i))?;
2235 } else {
2236 write!(f, ", {}", self.get_unchecked(i))?;
2237 }
2238 }
2239 write!(f, "]")
2240 }
2241}
2242impl ::core::default::Default for OutPointVec {
2243 fn default() -> Self {
2244 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2245 OutPointVec::new_unchecked(v)
2246 }
2247}
2248impl OutPointVec {
2249 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
2250 pub const ITEM_SIZE: usize = 36;
2251 pub fn total_size(&self) -> usize {
2252 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
2253 }
2254 pub fn item_count(&self) -> usize {
2255 molecule::unpack_number(self.as_slice()) as usize
2256 }
2257 pub fn len(&self) -> usize {
2258 self.item_count()
2259 }
2260 pub fn is_empty(&self) -> bool {
2261 self.len() == 0
2262 }
2263 pub fn get(&self, idx: usize) -> Option<OutPoint> {
2264 if idx >= self.len() {
2265 None
2266 } else {
2267 Some(self.get_unchecked(idx))
2268 }
2269 }
2270 pub fn get_unchecked(&self, idx: usize) -> OutPoint {
2271 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2272 let end = start + Self::ITEM_SIZE;
2273 OutPoint::new_unchecked(self.0.slice(start..end))
2274 }
2275 pub fn as_reader<'r>(&'r self) -> OutPointVecReader<'r> {
2276 OutPointVecReader::new_unchecked(self.as_slice())
2277 }
2278}
2279impl molecule::prelude::Entity for OutPointVec {
2280 type Builder = OutPointVecBuilder;
2281 const NAME: &'static str = "OutPointVec";
2282 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2283 OutPointVec(data)
2284 }
2285 fn as_bytes(&self) -> molecule::bytes::Bytes {
2286 self.0.clone()
2287 }
2288 fn as_slice(&self) -> &[u8] {
2289 &self.0[..]
2290 }
2291 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2292 OutPointVecReader::from_slice(slice).map(|reader| reader.to_entity())
2293 }
2294 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2295 OutPointVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2296 }
2297 fn new_builder() -> Self::Builder {
2298 ::core::default::Default::default()
2299 }
2300 fn as_builder(self) -> Self::Builder {
2301 Self::new_builder().extend(self.into_iter())
2302 }
2303}
2304#[derive(Clone, Copy)]
2305pub struct OutPointVecReader<'r>(&'r [u8]);
2306impl<'r> ::core::fmt::LowerHex for OutPointVecReader<'r> {
2307 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2308 use molecule::hex_string;
2309 if f.alternate() {
2310 write!(f, "0x")?;
2311 }
2312 write!(f, "{}", hex_string(self.as_slice()))
2313 }
2314}
2315impl<'r> ::core::fmt::Debug for OutPointVecReader<'r> {
2316 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2317 write!(f, "{}({:#x})", Self::NAME, self)
2318 }
2319}
2320impl<'r> ::core::fmt::Display for OutPointVecReader<'r> {
2321 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2322 write!(f, "{} [", Self::NAME)?;
2323 for i in 0..self.len() {
2324 if i == 0 {
2325 write!(f, "{}", self.get_unchecked(i))?;
2326 } else {
2327 write!(f, ", {}", self.get_unchecked(i))?;
2328 }
2329 }
2330 write!(f, "]")
2331 }
2332}
2333impl<'r> OutPointVecReader<'r> {
2334 pub const ITEM_SIZE: usize = 36;
2335 pub fn total_size(&self) -> usize {
2336 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
2337 }
2338 pub fn item_count(&self) -> usize {
2339 molecule::unpack_number(self.as_slice()) as usize
2340 }
2341 pub fn len(&self) -> usize {
2342 self.item_count()
2343 }
2344 pub fn is_empty(&self) -> bool {
2345 self.len() == 0
2346 }
2347 pub fn get(&self, idx: usize) -> Option<OutPointReader<'r>> {
2348 if idx >= self.len() {
2349 None
2350 } else {
2351 Some(self.get_unchecked(idx))
2352 }
2353 }
2354 pub fn get_unchecked(&self, idx: usize) -> OutPointReader<'r> {
2355 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2356 let end = start + Self::ITEM_SIZE;
2357 OutPointReader::new_unchecked(&self.as_slice()[start..end])
2358 }
2359}
2360impl<'r> molecule::prelude::Reader<'r> for OutPointVecReader<'r> {
2361 type Entity = OutPointVec;
2362 const NAME: &'static str = "OutPointVecReader";
2363 fn to_entity(&self) -> Self::Entity {
2364 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2365 }
2366 fn new_unchecked(slice: &'r [u8]) -> Self {
2367 OutPointVecReader(slice)
2368 }
2369 fn as_slice(&self) -> &'r [u8] {
2370 self.0
2371 }
2372 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
2373 use molecule::verification_error as ve;
2374 let slice_len = slice.len();
2375 if slice_len < molecule::NUMBER_SIZE {
2376 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2377 }
2378 let item_count = molecule::unpack_number(slice) as usize;
2379 if item_count == 0 {
2380 if slice_len != molecule::NUMBER_SIZE {
2381 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
2382 }
2383 return Ok(());
2384 }
2385 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
2386 if slice_len != total_size {
2387 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2388 }
2389 Ok(())
2390 }
2391}
2392#[derive(Debug, Default)]
2393pub struct OutPointVecBuilder(pub(crate) Vec<OutPoint>);
2394impl OutPointVecBuilder {
2395 pub const ITEM_SIZE: usize = 36;
2396 pub fn set(mut self, v: Vec<OutPoint>) -> Self {
2397 self.0 = v;
2398 self
2399 }
2400 pub fn push(mut self, v: OutPoint) -> Self {
2401 self.0.push(v);
2402 self
2403 }
2404 pub fn extend<T: ::core::iter::IntoIterator<Item = OutPoint>>(mut self, iter: T) -> Self {
2405 for elem in iter {
2406 self.0.push(elem);
2407 }
2408 self
2409 }
2410 pub fn replace(&mut self, index: usize, v: OutPoint) -> Option<OutPoint> {
2411 self.0
2412 .get_mut(index)
2413 .map(|item| ::core::mem::replace(item, v))
2414 }
2415}
2416impl molecule::prelude::Builder for OutPointVecBuilder {
2417 type Entity = OutPointVec;
2418 const NAME: &'static str = "OutPointVecBuilder";
2419 fn expected_length(&self) -> usize {
2420 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
2421 }
2422 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2423 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
2424 for inner in &self.0[..] {
2425 writer.write_all(inner.as_slice())?;
2426 }
2427 Ok(())
2428 }
2429 fn build(&self) -> Self::Entity {
2430 let mut inner = Vec::with_capacity(self.expected_length());
2431 self.write(&mut inner)
2432 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2433 OutPointVec::new_unchecked(inner.into())
2434 }
2435}
2436pub struct OutPointVecIterator(OutPointVec, usize, usize);
2437impl ::core::iter::Iterator for OutPointVecIterator {
2438 type Item = OutPoint;
2439 fn next(&mut self) -> Option<Self::Item> {
2440 if self.1 >= self.2 {
2441 None
2442 } else {
2443 let ret = self.0.get_unchecked(self.1);
2444 self.1 += 1;
2445 Some(ret)
2446 }
2447 }
2448}
2449impl ::core::iter::ExactSizeIterator for OutPointVecIterator {
2450 fn len(&self) -> usize {
2451 self.2 - self.1
2452 }
2453}
2454impl ::core::iter::IntoIterator for OutPointVec {
2455 type Item = OutPoint;
2456 type IntoIter = OutPointVecIterator;
2457 fn into_iter(self) -> Self::IntoIter {
2458 let len = self.len();
2459 OutPointVecIterator(self, 0, len)
2460 }
2461}
2462impl<'r> OutPointVecReader<'r> {
2463 pub fn iter<'t>(&'t self) -> OutPointVecReaderIterator<'t, 'r> {
2464 OutPointVecReaderIterator(&self, 0, self.len())
2465 }
2466}
2467pub struct OutPointVecReaderIterator<'t, 'r>(&'t OutPointVecReader<'r>, usize, usize);
2468impl<'t: 'r, 'r> ::core::iter::Iterator for OutPointVecReaderIterator<'t, 'r> {
2469 type Item = OutPointReader<'t>;
2470 fn next(&mut self) -> Option<Self::Item> {
2471 if self.1 >= self.2 {
2472 None
2473 } else {
2474 let ret = self.0.get_unchecked(self.1);
2475 self.1 += 1;
2476 Some(ret)
2477 }
2478 }
2479}
2480impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for OutPointVecReaderIterator<'t, 'r> {
2481 fn len(&self) -> usize {
2482 self.2 - self.1
2483 }
2484}
2485#[derive(Clone)]
2486pub struct Uint64VecOpt(molecule::bytes::Bytes);
2487impl ::core::fmt::LowerHex for Uint64VecOpt {
2488 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2489 use molecule::hex_string;
2490 if f.alternate() {
2491 write!(f, "0x")?;
2492 }
2493 write!(f, "{}", hex_string(self.as_slice()))
2494 }
2495}
2496impl ::core::fmt::Debug for Uint64VecOpt {
2497 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2498 write!(f, "{}({:#x})", Self::NAME, self)
2499 }
2500}
2501impl ::core::fmt::Display for Uint64VecOpt {
2502 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2503 if let Some(v) = self.to_opt() {
2504 write!(f, "{}(Some({}))", Self::NAME, v)
2505 } else {
2506 write!(f, "{}(None)", Self::NAME)
2507 }
2508 }
2509}
2510impl ::core::default::Default for Uint64VecOpt {
2511 fn default() -> Self {
2512 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2513 Uint64VecOpt::new_unchecked(v)
2514 }
2515}
2516impl Uint64VecOpt {
2517 const DEFAULT_VALUE: [u8; 0] = [];
2518 pub fn is_none(&self) -> bool {
2519 self.0.is_empty()
2520 }
2521 pub fn is_some(&self) -> bool {
2522 !self.0.is_empty()
2523 }
2524 pub fn to_opt(&self) -> Option<Uint64Vec> {
2525 if self.is_none() {
2526 None
2527 } else {
2528 Some(Uint64Vec::new_unchecked(self.0.clone()))
2529 }
2530 }
2531 pub fn as_reader<'r>(&'r self) -> Uint64VecOptReader<'r> {
2532 Uint64VecOptReader::new_unchecked(self.as_slice())
2533 }
2534}
2535impl molecule::prelude::Entity for Uint64VecOpt {
2536 type Builder = Uint64VecOptBuilder;
2537 const NAME: &'static str = "Uint64VecOpt";
2538 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2539 Uint64VecOpt(data)
2540 }
2541 fn as_bytes(&self) -> molecule::bytes::Bytes {
2542 self.0.clone()
2543 }
2544 fn as_slice(&self) -> &[u8] {
2545 &self.0[..]
2546 }
2547 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2548 Uint64VecOptReader::from_slice(slice).map(|reader| reader.to_entity())
2549 }
2550 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2551 Uint64VecOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2552 }
2553 fn new_builder() -> Self::Builder {
2554 ::core::default::Default::default()
2555 }
2556 fn as_builder(self) -> Self::Builder {
2557 Self::new_builder().set(self.to_opt())
2558 }
2559}
2560#[derive(Clone, Copy)]
2561pub struct Uint64VecOptReader<'r>(&'r [u8]);
2562impl<'r> ::core::fmt::LowerHex for Uint64VecOptReader<'r> {
2563 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2564 use molecule::hex_string;
2565 if f.alternate() {
2566 write!(f, "0x")?;
2567 }
2568 write!(f, "{}", hex_string(self.as_slice()))
2569 }
2570}
2571impl<'r> ::core::fmt::Debug for Uint64VecOptReader<'r> {
2572 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2573 write!(f, "{}({:#x})", Self::NAME, self)
2574 }
2575}
2576impl<'r> ::core::fmt::Display for Uint64VecOptReader<'r> {
2577 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2578 if let Some(v) = self.to_opt() {
2579 write!(f, "{}(Some({}))", Self::NAME, v)
2580 } else {
2581 write!(f, "{}(None)", Self::NAME)
2582 }
2583 }
2584}
2585impl<'r> Uint64VecOptReader<'r> {
2586 pub fn is_none(&self) -> bool {
2587 self.0.is_empty()
2588 }
2589 pub fn is_some(&self) -> bool {
2590 !self.0.is_empty()
2591 }
2592 pub fn to_opt(&self) -> Option<Uint64VecReader<'r>> {
2593 if self.is_none() {
2594 None
2595 } else {
2596 Some(Uint64VecReader::new_unchecked(self.as_slice()))
2597 }
2598 }
2599}
2600impl<'r> molecule::prelude::Reader<'r> for Uint64VecOptReader<'r> {
2601 type Entity = Uint64VecOpt;
2602 const NAME: &'static str = "Uint64VecOptReader";
2603 fn to_entity(&self) -> Self::Entity {
2604 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2605 }
2606 fn new_unchecked(slice: &'r [u8]) -> Self {
2607 Uint64VecOptReader(slice)
2608 }
2609 fn as_slice(&self) -> &'r [u8] {
2610 self.0
2611 }
2612 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2613 if !slice.is_empty() {
2614 Uint64VecReader::verify(&slice[..], compatible)?;
2615 }
2616 Ok(())
2617 }
2618}
2619#[derive(Debug, Default)]
2620pub struct Uint64VecOptBuilder(pub(crate) Option<Uint64Vec>);
2621impl Uint64VecOptBuilder {
2622 pub fn set(mut self, v: Option<Uint64Vec>) -> Self {
2623 self.0 = v;
2624 self
2625 }
2626}
2627impl molecule::prelude::Builder for Uint64VecOptBuilder {
2628 type Entity = Uint64VecOpt;
2629 const NAME: &'static str = "Uint64VecOptBuilder";
2630 fn expected_length(&self) -> usize {
2631 self.0
2632 .as_ref()
2633 .map(|ref inner| inner.as_slice().len())
2634 .unwrap_or(0)
2635 }
2636 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2637 self.0
2638 .as_ref()
2639 .map(|ref inner| writer.write_all(inner.as_slice()))
2640 .unwrap_or(Ok(()))
2641 }
2642 fn build(&self) -> Self::Entity {
2643 let mut inner = Vec::with_capacity(self.expected_length());
2644 self.write(&mut inner)
2645 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2646 Uint64VecOpt::new_unchecked(inner.into())
2647 }
2648}
2649#[derive(Clone)]
2650pub struct HeaderDigest(molecule::bytes::Bytes);
2651impl ::core::fmt::LowerHex for HeaderDigest {
2652 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2653 use molecule::hex_string;
2654 if f.alternate() {
2655 write!(f, "0x")?;
2656 }
2657 write!(f, "{}", hex_string(self.as_slice()))
2658 }
2659}
2660impl ::core::fmt::Debug for HeaderDigest {
2661 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2662 write!(f, "{}({:#x})", Self::NAME, self)
2663 }
2664}
2665impl ::core::fmt::Display for HeaderDigest {
2666 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2667 write!(f, "{} {{ ", Self::NAME)?;
2668 write!(f, "{}: {}", "children_hash", self.children_hash())?;
2669 write!(f, ", {}: {}", "total_difficulty", self.total_difficulty())?;
2670 write!(f, ", {}: {}", "start_number", self.start_number())?;
2671 write!(f, ", {}: {}", "end_number", self.end_number())?;
2672 write!(f, ", {}: {}", "start_epoch", self.start_epoch())?;
2673 write!(f, ", {}: {}", "end_epoch", self.end_epoch())?;
2674 write!(f, ", {}: {}", "start_timestamp", self.start_timestamp())?;
2675 write!(f, ", {}: {}", "end_timestamp", self.end_timestamp())?;
2676 write!(
2677 f,
2678 ", {}: {}",
2679 "start_compact_target",
2680 self.start_compact_target()
2681 )?;
2682 write!(
2683 f,
2684 ", {}: {}",
2685 "end_compact_target",
2686 self.end_compact_target()
2687 )?;
2688 write!(f, " }}")
2689 }
2690}
2691impl ::core::default::Default for HeaderDigest {
2692 fn default() -> Self {
2693 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2694 HeaderDigest::new_unchecked(v)
2695 }
2696}
2697impl HeaderDigest {
2698 const DEFAULT_VALUE: [u8; 120] = [
2699 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2700 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2701 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2702 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2703 ];
2704 pub const TOTAL_SIZE: usize = 120;
2705 pub const FIELD_SIZES: [usize; 10] = [32, 32, 8, 8, 8, 8, 8, 8, 4, 4];
2706 pub const FIELD_COUNT: usize = 10;
2707 pub fn children_hash(&self) -> Byte32 {
2708 Byte32::new_unchecked(self.0.slice(0..32))
2709 }
2710 pub fn total_difficulty(&self) -> Uint256 {
2711 Uint256::new_unchecked(self.0.slice(32..64))
2712 }
2713 pub fn start_number(&self) -> Uint64 {
2714 Uint64::new_unchecked(self.0.slice(64..72))
2715 }
2716 pub fn end_number(&self) -> Uint64 {
2717 Uint64::new_unchecked(self.0.slice(72..80))
2718 }
2719 pub fn start_epoch(&self) -> Uint64 {
2720 Uint64::new_unchecked(self.0.slice(80..88))
2721 }
2722 pub fn end_epoch(&self) -> Uint64 {
2723 Uint64::new_unchecked(self.0.slice(88..96))
2724 }
2725 pub fn start_timestamp(&self) -> Uint64 {
2726 Uint64::new_unchecked(self.0.slice(96..104))
2727 }
2728 pub fn end_timestamp(&self) -> Uint64 {
2729 Uint64::new_unchecked(self.0.slice(104..112))
2730 }
2731 pub fn start_compact_target(&self) -> Uint32 {
2732 Uint32::new_unchecked(self.0.slice(112..116))
2733 }
2734 pub fn end_compact_target(&self) -> Uint32 {
2735 Uint32::new_unchecked(self.0.slice(116..120))
2736 }
2737 pub fn as_reader<'r>(&'r self) -> HeaderDigestReader<'r> {
2738 HeaderDigestReader::new_unchecked(self.as_slice())
2739 }
2740}
2741impl molecule::prelude::Entity for HeaderDigest {
2742 type Builder = HeaderDigestBuilder;
2743 const NAME: &'static str = "HeaderDigest";
2744 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2745 HeaderDigest(data)
2746 }
2747 fn as_bytes(&self) -> molecule::bytes::Bytes {
2748 self.0.clone()
2749 }
2750 fn as_slice(&self) -> &[u8] {
2751 &self.0[..]
2752 }
2753 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2754 HeaderDigestReader::from_slice(slice).map(|reader| reader.to_entity())
2755 }
2756 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2757 HeaderDigestReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2758 }
2759 fn new_builder() -> Self::Builder {
2760 ::core::default::Default::default()
2761 }
2762 fn as_builder(self) -> Self::Builder {
2763 Self::new_builder()
2764 .children_hash(self.children_hash())
2765 .total_difficulty(self.total_difficulty())
2766 .start_number(self.start_number())
2767 .end_number(self.end_number())
2768 .start_epoch(self.start_epoch())
2769 .end_epoch(self.end_epoch())
2770 .start_timestamp(self.start_timestamp())
2771 .end_timestamp(self.end_timestamp())
2772 .start_compact_target(self.start_compact_target())
2773 .end_compact_target(self.end_compact_target())
2774 }
2775}
2776#[derive(Clone, Copy)]
2777pub struct HeaderDigestReader<'r>(&'r [u8]);
2778impl<'r> ::core::fmt::LowerHex for HeaderDigestReader<'r> {
2779 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2780 use molecule::hex_string;
2781 if f.alternate() {
2782 write!(f, "0x")?;
2783 }
2784 write!(f, "{}", hex_string(self.as_slice()))
2785 }
2786}
2787impl<'r> ::core::fmt::Debug for HeaderDigestReader<'r> {
2788 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2789 write!(f, "{}({:#x})", Self::NAME, self)
2790 }
2791}
2792impl<'r> ::core::fmt::Display for HeaderDigestReader<'r> {
2793 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2794 write!(f, "{} {{ ", Self::NAME)?;
2795 write!(f, "{}: {}", "children_hash", self.children_hash())?;
2796 write!(f, ", {}: {}", "total_difficulty", self.total_difficulty())?;
2797 write!(f, ", {}: {}", "start_number", self.start_number())?;
2798 write!(f, ", {}: {}", "end_number", self.end_number())?;
2799 write!(f, ", {}: {}", "start_epoch", self.start_epoch())?;
2800 write!(f, ", {}: {}", "end_epoch", self.end_epoch())?;
2801 write!(f, ", {}: {}", "start_timestamp", self.start_timestamp())?;
2802 write!(f, ", {}: {}", "end_timestamp", self.end_timestamp())?;
2803 write!(
2804 f,
2805 ", {}: {}",
2806 "start_compact_target",
2807 self.start_compact_target()
2808 )?;
2809 write!(
2810 f,
2811 ", {}: {}",
2812 "end_compact_target",
2813 self.end_compact_target()
2814 )?;
2815 write!(f, " }}")
2816 }
2817}
2818impl<'r> HeaderDigestReader<'r> {
2819 pub const TOTAL_SIZE: usize = 120;
2820 pub const FIELD_SIZES: [usize; 10] = [32, 32, 8, 8, 8, 8, 8, 8, 4, 4];
2821 pub const FIELD_COUNT: usize = 10;
2822 pub fn children_hash(&self) -> Byte32Reader<'r> {
2823 Byte32Reader::new_unchecked(&self.as_slice()[0..32])
2824 }
2825 pub fn total_difficulty(&self) -> Uint256Reader<'r> {
2826 Uint256Reader::new_unchecked(&self.as_slice()[32..64])
2827 }
2828 pub fn start_number(&self) -> Uint64Reader<'r> {
2829 Uint64Reader::new_unchecked(&self.as_slice()[64..72])
2830 }
2831 pub fn end_number(&self) -> Uint64Reader<'r> {
2832 Uint64Reader::new_unchecked(&self.as_slice()[72..80])
2833 }
2834 pub fn start_epoch(&self) -> Uint64Reader<'r> {
2835 Uint64Reader::new_unchecked(&self.as_slice()[80..88])
2836 }
2837 pub fn end_epoch(&self) -> Uint64Reader<'r> {
2838 Uint64Reader::new_unchecked(&self.as_slice()[88..96])
2839 }
2840 pub fn start_timestamp(&self) -> Uint64Reader<'r> {
2841 Uint64Reader::new_unchecked(&self.as_slice()[96..104])
2842 }
2843 pub fn end_timestamp(&self) -> Uint64Reader<'r> {
2844 Uint64Reader::new_unchecked(&self.as_slice()[104..112])
2845 }
2846 pub fn start_compact_target(&self) -> Uint32Reader<'r> {
2847 Uint32Reader::new_unchecked(&self.as_slice()[112..116])
2848 }
2849 pub fn end_compact_target(&self) -> Uint32Reader<'r> {
2850 Uint32Reader::new_unchecked(&self.as_slice()[116..120])
2851 }
2852}
2853impl<'r> molecule::prelude::Reader<'r> for HeaderDigestReader<'r> {
2854 type Entity = HeaderDigest;
2855 const NAME: &'static str = "HeaderDigestReader";
2856 fn to_entity(&self) -> Self::Entity {
2857 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2858 }
2859 fn new_unchecked(slice: &'r [u8]) -> Self {
2860 HeaderDigestReader(slice)
2861 }
2862 fn as_slice(&self) -> &'r [u8] {
2863 self.0
2864 }
2865 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
2866 use molecule::verification_error as ve;
2867 let slice_len = slice.len();
2868 if slice_len != Self::TOTAL_SIZE {
2869 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
2870 }
2871 Ok(())
2872 }
2873}
2874#[derive(Debug, Default)]
2875pub struct HeaderDigestBuilder {
2876 pub(crate) children_hash: Byte32,
2877 pub(crate) total_difficulty: Uint256,
2878 pub(crate) start_number: Uint64,
2879 pub(crate) end_number: Uint64,
2880 pub(crate) start_epoch: Uint64,
2881 pub(crate) end_epoch: Uint64,
2882 pub(crate) start_timestamp: Uint64,
2883 pub(crate) end_timestamp: Uint64,
2884 pub(crate) start_compact_target: Uint32,
2885 pub(crate) end_compact_target: Uint32,
2886}
2887impl HeaderDigestBuilder {
2888 pub const TOTAL_SIZE: usize = 120;
2889 pub const FIELD_SIZES: [usize; 10] = [32, 32, 8, 8, 8, 8, 8, 8, 4, 4];
2890 pub const FIELD_COUNT: usize = 10;
2891 pub fn children_hash(mut self, v: Byte32) -> Self {
2892 self.children_hash = v;
2893 self
2894 }
2895 pub fn total_difficulty(mut self, v: Uint256) -> Self {
2896 self.total_difficulty = v;
2897 self
2898 }
2899 pub fn start_number(mut self, v: Uint64) -> Self {
2900 self.start_number = v;
2901 self
2902 }
2903 pub fn end_number(mut self, v: Uint64) -> Self {
2904 self.end_number = v;
2905 self
2906 }
2907 pub fn start_epoch(mut self, v: Uint64) -> Self {
2908 self.start_epoch = v;
2909 self
2910 }
2911 pub fn end_epoch(mut self, v: Uint64) -> Self {
2912 self.end_epoch = v;
2913 self
2914 }
2915 pub fn start_timestamp(mut self, v: Uint64) -> Self {
2916 self.start_timestamp = v;
2917 self
2918 }
2919 pub fn end_timestamp(mut self, v: Uint64) -> Self {
2920 self.end_timestamp = v;
2921 self
2922 }
2923 pub fn start_compact_target(mut self, v: Uint32) -> Self {
2924 self.start_compact_target = v;
2925 self
2926 }
2927 pub fn end_compact_target(mut self, v: Uint32) -> Self {
2928 self.end_compact_target = v;
2929 self
2930 }
2931}
2932impl molecule::prelude::Builder for HeaderDigestBuilder {
2933 type Entity = HeaderDigest;
2934 const NAME: &'static str = "HeaderDigestBuilder";
2935 fn expected_length(&self) -> usize {
2936 Self::TOTAL_SIZE
2937 }
2938 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2939 writer.write_all(self.children_hash.as_slice())?;
2940 writer.write_all(self.total_difficulty.as_slice())?;
2941 writer.write_all(self.start_number.as_slice())?;
2942 writer.write_all(self.end_number.as_slice())?;
2943 writer.write_all(self.start_epoch.as_slice())?;
2944 writer.write_all(self.end_epoch.as_slice())?;
2945 writer.write_all(self.start_timestamp.as_slice())?;
2946 writer.write_all(self.end_timestamp.as_slice())?;
2947 writer.write_all(self.start_compact_target.as_slice())?;
2948 writer.write_all(self.end_compact_target.as_slice())?;
2949 Ok(())
2950 }
2951 fn build(&self) -> Self::Entity {
2952 let mut inner = Vec::with_capacity(self.expected_length());
2953 self.write(&mut inner)
2954 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2955 HeaderDigest::new_unchecked(inner.into())
2956 }
2957}
2958#[derive(Clone)]
2959pub struct HeaderView(molecule::bytes::Bytes);
2960impl ::core::fmt::LowerHex for HeaderView {
2961 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2962 use molecule::hex_string;
2963 if f.alternate() {
2964 write!(f, "0x")?;
2965 }
2966 write!(f, "{}", hex_string(self.as_slice()))
2967 }
2968}
2969impl ::core::fmt::Debug for HeaderView {
2970 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2971 write!(f, "{}({:#x})", Self::NAME, self)
2972 }
2973}
2974impl ::core::fmt::Display for HeaderView {
2975 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2976 write!(f, "{} {{ ", Self::NAME)?;
2977 write!(f, "{}: {}", "hash", self.hash())?;
2978 write!(f, ", {}: {}", "data", self.data())?;
2979 write!(f, " }}")
2980 }
2981}
2982impl ::core::default::Default for HeaderView {
2983 fn default() -> Self {
2984 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2985 HeaderView::new_unchecked(v)
2986 }
2987}
2988impl HeaderView {
2989 const DEFAULT_VALUE: [u8; 240] = [
2990 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2991 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2992 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2993 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2994 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2995 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2996 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2997 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2998 ];
2999 pub const TOTAL_SIZE: usize = 240;
3000 pub const FIELD_SIZES: [usize; 2] = [32, 208];
3001 pub const FIELD_COUNT: usize = 2;
3002 pub fn hash(&self) -> Byte32 {
3003 Byte32::new_unchecked(self.0.slice(0..32))
3004 }
3005 pub fn data(&self) -> Header {
3006 Header::new_unchecked(self.0.slice(32..240))
3007 }
3008 pub fn as_reader<'r>(&'r self) -> HeaderViewReader<'r> {
3009 HeaderViewReader::new_unchecked(self.as_slice())
3010 }
3011}
3012impl molecule::prelude::Entity for HeaderView {
3013 type Builder = HeaderViewBuilder;
3014 const NAME: &'static str = "HeaderView";
3015 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3016 HeaderView(data)
3017 }
3018 fn as_bytes(&self) -> molecule::bytes::Bytes {
3019 self.0.clone()
3020 }
3021 fn as_slice(&self) -> &[u8] {
3022 &self.0[..]
3023 }
3024 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3025 HeaderViewReader::from_slice(slice).map(|reader| reader.to_entity())
3026 }
3027 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3028 HeaderViewReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3029 }
3030 fn new_builder() -> Self::Builder {
3031 ::core::default::Default::default()
3032 }
3033 fn as_builder(self) -> Self::Builder {
3034 Self::new_builder().hash(self.hash()).data(self.data())
3035 }
3036}
3037#[derive(Clone, Copy)]
3038pub struct HeaderViewReader<'r>(&'r [u8]);
3039impl<'r> ::core::fmt::LowerHex for HeaderViewReader<'r> {
3040 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3041 use molecule::hex_string;
3042 if f.alternate() {
3043 write!(f, "0x")?;
3044 }
3045 write!(f, "{}", hex_string(self.as_slice()))
3046 }
3047}
3048impl<'r> ::core::fmt::Debug for HeaderViewReader<'r> {
3049 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3050 write!(f, "{}({:#x})", Self::NAME, self)
3051 }
3052}
3053impl<'r> ::core::fmt::Display for HeaderViewReader<'r> {
3054 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3055 write!(f, "{} {{ ", Self::NAME)?;
3056 write!(f, "{}: {}", "hash", self.hash())?;
3057 write!(f, ", {}: {}", "data", self.data())?;
3058 write!(f, " }}")
3059 }
3060}
3061impl<'r> HeaderViewReader<'r> {
3062 pub const TOTAL_SIZE: usize = 240;
3063 pub const FIELD_SIZES: [usize; 2] = [32, 208];
3064 pub const FIELD_COUNT: usize = 2;
3065 pub fn hash(&self) -> Byte32Reader<'r> {
3066 Byte32Reader::new_unchecked(&self.as_slice()[0..32])
3067 }
3068 pub fn data(&self) -> HeaderReader<'r> {
3069 HeaderReader::new_unchecked(&self.as_slice()[32..240])
3070 }
3071}
3072impl<'r> molecule::prelude::Reader<'r> for HeaderViewReader<'r> {
3073 type Entity = HeaderView;
3074 const NAME: &'static str = "HeaderViewReader";
3075 fn to_entity(&self) -> Self::Entity {
3076 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3077 }
3078 fn new_unchecked(slice: &'r [u8]) -> Self {
3079 HeaderViewReader(slice)
3080 }
3081 fn as_slice(&self) -> &'r [u8] {
3082 self.0
3083 }
3084 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
3085 use molecule::verification_error as ve;
3086 let slice_len = slice.len();
3087 if slice_len != Self::TOTAL_SIZE {
3088 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
3089 }
3090 Ok(())
3091 }
3092}
3093#[derive(Debug, Default)]
3094pub struct HeaderViewBuilder {
3095 pub(crate) hash: Byte32,
3096 pub(crate) data: Header,
3097}
3098impl HeaderViewBuilder {
3099 pub const TOTAL_SIZE: usize = 240;
3100 pub const FIELD_SIZES: [usize; 2] = [32, 208];
3101 pub const FIELD_COUNT: usize = 2;
3102 pub fn hash(mut self, v: Byte32) -> Self {
3103 self.hash = v;
3104 self
3105 }
3106 pub fn data(mut self, v: Header) -> Self {
3107 self.data = v;
3108 self
3109 }
3110}
3111impl molecule::prelude::Builder for HeaderViewBuilder {
3112 type Entity = HeaderView;
3113 const NAME: &'static str = "HeaderViewBuilder";
3114 fn expected_length(&self) -> usize {
3115 Self::TOTAL_SIZE
3116 }
3117 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3118 writer.write_all(self.hash.as_slice())?;
3119 writer.write_all(self.data.as_slice())?;
3120 Ok(())
3121 }
3122 fn build(&self) -> Self::Entity {
3123 let mut inner = Vec::with_capacity(self.expected_length());
3124 self.write(&mut inner)
3125 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3126 HeaderView::new_unchecked(inner.into())
3127 }
3128}
3129#[derive(Clone)]
3130pub struct UncleBlockVecView(molecule::bytes::Bytes);
3131impl ::core::fmt::LowerHex for UncleBlockVecView {
3132 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3133 use molecule::hex_string;
3134 if f.alternate() {
3135 write!(f, "0x")?;
3136 }
3137 write!(f, "{}", hex_string(self.as_slice()))
3138 }
3139}
3140impl ::core::fmt::Debug for UncleBlockVecView {
3141 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3142 write!(f, "{}({:#x})", Self::NAME, self)
3143 }
3144}
3145impl ::core::fmt::Display for UncleBlockVecView {
3146 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3147 write!(f, "{} {{ ", Self::NAME)?;
3148 write!(f, "{}: {}", "hashes", self.hashes())?;
3149 write!(f, ", {}: {}", "data", self.data())?;
3150 let extra_count = self.count_extra_fields();
3151 if extra_count != 0 {
3152 write!(f, ", .. ({} fields)", extra_count)?;
3153 }
3154 write!(f, " }}")
3155 }
3156}
3157impl ::core::default::Default for UncleBlockVecView {
3158 fn default() -> Self {
3159 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3160 UncleBlockVecView::new_unchecked(v)
3161 }
3162}
3163impl UncleBlockVecView {
3164 const DEFAULT_VALUE: [u8; 20] = [
3165 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
3166 ];
3167 pub const FIELD_COUNT: usize = 2;
3168 pub fn total_size(&self) -> usize {
3169 molecule::unpack_number(self.as_slice()) as usize
3170 }
3171 pub fn field_count(&self) -> usize {
3172 if self.total_size() == molecule::NUMBER_SIZE {
3173 0
3174 } else {
3175 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3176 }
3177 }
3178 pub fn count_extra_fields(&self) -> usize {
3179 self.field_count() - Self::FIELD_COUNT
3180 }
3181 pub fn has_extra_fields(&self) -> bool {
3182 Self::FIELD_COUNT != self.field_count()
3183 }
3184 pub fn hashes(&self) -> Byte32Vec {
3185 let slice = self.as_slice();
3186 let start = molecule::unpack_number(&slice[4..]) as usize;
3187 let end = molecule::unpack_number(&slice[8..]) as usize;
3188 Byte32Vec::new_unchecked(self.0.slice(start..end))
3189 }
3190 pub fn data(&self) -> UncleBlockVec {
3191 let slice = self.as_slice();
3192 let start = molecule::unpack_number(&slice[8..]) as usize;
3193 if self.has_extra_fields() {
3194 let end = molecule::unpack_number(&slice[12..]) as usize;
3195 UncleBlockVec::new_unchecked(self.0.slice(start..end))
3196 } else {
3197 UncleBlockVec::new_unchecked(self.0.slice(start..))
3198 }
3199 }
3200 pub fn as_reader<'r>(&'r self) -> UncleBlockVecViewReader<'r> {
3201 UncleBlockVecViewReader::new_unchecked(self.as_slice())
3202 }
3203}
3204impl molecule::prelude::Entity for UncleBlockVecView {
3205 type Builder = UncleBlockVecViewBuilder;
3206 const NAME: &'static str = "UncleBlockVecView";
3207 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3208 UncleBlockVecView(data)
3209 }
3210 fn as_bytes(&self) -> molecule::bytes::Bytes {
3211 self.0.clone()
3212 }
3213 fn as_slice(&self) -> &[u8] {
3214 &self.0[..]
3215 }
3216 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3217 UncleBlockVecViewReader::from_slice(slice).map(|reader| reader.to_entity())
3218 }
3219 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3220 UncleBlockVecViewReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3221 }
3222 fn new_builder() -> Self::Builder {
3223 ::core::default::Default::default()
3224 }
3225 fn as_builder(self) -> Self::Builder {
3226 Self::new_builder().hashes(self.hashes()).data(self.data())
3227 }
3228}
3229#[derive(Clone, Copy)]
3230pub struct UncleBlockVecViewReader<'r>(&'r [u8]);
3231impl<'r> ::core::fmt::LowerHex for UncleBlockVecViewReader<'r> {
3232 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3233 use molecule::hex_string;
3234 if f.alternate() {
3235 write!(f, "0x")?;
3236 }
3237 write!(f, "{}", hex_string(self.as_slice()))
3238 }
3239}
3240impl<'r> ::core::fmt::Debug for UncleBlockVecViewReader<'r> {
3241 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3242 write!(f, "{}({:#x})", Self::NAME, self)
3243 }
3244}
3245impl<'r> ::core::fmt::Display for UncleBlockVecViewReader<'r> {
3246 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3247 write!(f, "{} {{ ", Self::NAME)?;
3248 write!(f, "{}: {}", "hashes", self.hashes())?;
3249 write!(f, ", {}: {}", "data", self.data())?;
3250 let extra_count = self.count_extra_fields();
3251 if extra_count != 0 {
3252 write!(f, ", .. ({} fields)", extra_count)?;
3253 }
3254 write!(f, " }}")
3255 }
3256}
3257impl<'r> UncleBlockVecViewReader<'r> {
3258 pub const FIELD_COUNT: usize = 2;
3259 pub fn total_size(&self) -> usize {
3260 molecule::unpack_number(self.as_slice()) as usize
3261 }
3262 pub fn field_count(&self) -> usize {
3263 if self.total_size() == molecule::NUMBER_SIZE {
3264 0
3265 } else {
3266 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3267 }
3268 }
3269 pub fn count_extra_fields(&self) -> usize {
3270 self.field_count() - Self::FIELD_COUNT
3271 }
3272 pub fn has_extra_fields(&self) -> bool {
3273 Self::FIELD_COUNT != self.field_count()
3274 }
3275 pub fn hashes(&self) -> Byte32VecReader<'r> {
3276 let slice = self.as_slice();
3277 let start = molecule::unpack_number(&slice[4..]) as usize;
3278 let end = molecule::unpack_number(&slice[8..]) as usize;
3279 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
3280 }
3281 pub fn data(&self) -> UncleBlockVecReader<'r> {
3282 let slice = self.as_slice();
3283 let start = molecule::unpack_number(&slice[8..]) as usize;
3284 if self.has_extra_fields() {
3285 let end = molecule::unpack_number(&slice[12..]) as usize;
3286 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..end])
3287 } else {
3288 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..])
3289 }
3290 }
3291}
3292impl<'r> molecule::prelude::Reader<'r> for UncleBlockVecViewReader<'r> {
3293 type Entity = UncleBlockVecView;
3294 const NAME: &'static str = "UncleBlockVecViewReader";
3295 fn to_entity(&self) -> Self::Entity {
3296 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3297 }
3298 fn new_unchecked(slice: &'r [u8]) -> Self {
3299 UncleBlockVecViewReader(slice)
3300 }
3301 fn as_slice(&self) -> &'r [u8] {
3302 self.0
3303 }
3304 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3305 use molecule::verification_error as ve;
3306 let slice_len = slice.len();
3307 if slice_len < molecule::NUMBER_SIZE {
3308 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3309 }
3310 let total_size = molecule::unpack_number(slice) as usize;
3311 if slice_len != total_size {
3312 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3313 }
3314 if slice_len < molecule::NUMBER_SIZE * 2 {
3315 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3316 }
3317 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3318 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3319 return ve!(Self, OffsetsNotMatch);
3320 }
3321 if slice_len < offset_first {
3322 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3323 }
3324 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3325 if field_count < Self::FIELD_COUNT {
3326 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3327 } else if !compatible && field_count > Self::FIELD_COUNT {
3328 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3329 };
3330 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3331 .chunks_exact(molecule::NUMBER_SIZE)
3332 .map(|x| molecule::unpack_number(x) as usize)
3333 .collect();
3334 offsets.push(total_size);
3335 if offsets.windows(2).any(|i| i[0] > i[1]) {
3336 return ve!(Self, OffsetsNotMatch);
3337 }
3338 Byte32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
3339 UncleBlockVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
3340 Ok(())
3341 }
3342}
3343#[derive(Debug, Default)]
3344pub struct UncleBlockVecViewBuilder {
3345 pub(crate) hashes: Byte32Vec,
3346 pub(crate) data: UncleBlockVec,
3347}
3348impl UncleBlockVecViewBuilder {
3349 pub const FIELD_COUNT: usize = 2;
3350 pub fn hashes(mut self, v: Byte32Vec) -> Self {
3351 self.hashes = v;
3352 self
3353 }
3354 pub fn data(mut self, v: UncleBlockVec) -> Self {
3355 self.data = v;
3356 self
3357 }
3358}
3359impl molecule::prelude::Builder for UncleBlockVecViewBuilder {
3360 type Entity = UncleBlockVecView;
3361 const NAME: &'static str = "UncleBlockVecViewBuilder";
3362 fn expected_length(&self) -> usize {
3363 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
3364 + self.hashes.as_slice().len()
3365 + self.data.as_slice().len()
3366 }
3367 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3368 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
3369 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
3370 offsets.push(total_size);
3371 total_size += self.hashes.as_slice().len();
3372 offsets.push(total_size);
3373 total_size += self.data.as_slice().len();
3374 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3375 for offset in offsets.into_iter() {
3376 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3377 }
3378 writer.write_all(self.hashes.as_slice())?;
3379 writer.write_all(self.data.as_slice())?;
3380 Ok(())
3381 }
3382 fn build(&self) -> Self::Entity {
3383 let mut inner = Vec::with_capacity(self.expected_length());
3384 self.write(&mut inner)
3385 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3386 UncleBlockVecView::new_unchecked(inner.into())
3387 }
3388}
3389#[derive(Clone)]
3390pub struct TransactionView(molecule::bytes::Bytes);
3391impl ::core::fmt::LowerHex for TransactionView {
3392 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3393 use molecule::hex_string;
3394 if f.alternate() {
3395 write!(f, "0x")?;
3396 }
3397 write!(f, "{}", hex_string(self.as_slice()))
3398 }
3399}
3400impl ::core::fmt::Debug for TransactionView {
3401 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3402 write!(f, "{}({:#x})", Self::NAME, self)
3403 }
3404}
3405impl ::core::fmt::Display for TransactionView {
3406 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3407 write!(f, "{} {{ ", Self::NAME)?;
3408 write!(f, "{}: {}", "hash", self.hash())?;
3409 write!(f, ", {}: {}", "witness_hash", self.witness_hash())?;
3410 write!(f, ", {}: {}", "data", self.data())?;
3411 let extra_count = self.count_extra_fields();
3412 if extra_count != 0 {
3413 write!(f, ", .. ({} fields)", extra_count)?;
3414 }
3415 write!(f, " }}")
3416 }
3417}
3418impl ::core::default::Default for TransactionView {
3419 fn default() -> Self {
3420 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3421 TransactionView::new_unchecked(v)
3422 }
3423}
3424impl TransactionView {
3425 const DEFAULT_VALUE: [u8; 148] = [
3426 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,
3427 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3428 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,
3429 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,
3430 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,
3431 0, 0,
3432 ];
3433 pub const FIELD_COUNT: usize = 3;
3434 pub fn total_size(&self) -> usize {
3435 molecule::unpack_number(self.as_slice()) as usize
3436 }
3437 pub fn field_count(&self) -> usize {
3438 if self.total_size() == molecule::NUMBER_SIZE {
3439 0
3440 } else {
3441 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3442 }
3443 }
3444 pub fn count_extra_fields(&self) -> usize {
3445 self.field_count() - Self::FIELD_COUNT
3446 }
3447 pub fn has_extra_fields(&self) -> bool {
3448 Self::FIELD_COUNT != self.field_count()
3449 }
3450 pub fn hash(&self) -> Byte32 {
3451 let slice = self.as_slice();
3452 let start = molecule::unpack_number(&slice[4..]) as usize;
3453 let end = molecule::unpack_number(&slice[8..]) as usize;
3454 Byte32::new_unchecked(self.0.slice(start..end))
3455 }
3456 pub fn witness_hash(&self) -> Byte32 {
3457 let slice = self.as_slice();
3458 let start = molecule::unpack_number(&slice[8..]) as usize;
3459 let end = molecule::unpack_number(&slice[12..]) as usize;
3460 Byte32::new_unchecked(self.0.slice(start..end))
3461 }
3462 pub fn data(&self) -> Transaction {
3463 let slice = self.as_slice();
3464 let start = molecule::unpack_number(&slice[12..]) as usize;
3465 if self.has_extra_fields() {
3466 let end = molecule::unpack_number(&slice[16..]) as usize;
3467 Transaction::new_unchecked(self.0.slice(start..end))
3468 } else {
3469 Transaction::new_unchecked(self.0.slice(start..))
3470 }
3471 }
3472 pub fn as_reader<'r>(&'r self) -> TransactionViewReader<'r> {
3473 TransactionViewReader::new_unchecked(self.as_slice())
3474 }
3475}
3476impl molecule::prelude::Entity for TransactionView {
3477 type Builder = TransactionViewBuilder;
3478 const NAME: &'static str = "TransactionView";
3479 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3480 TransactionView(data)
3481 }
3482 fn as_bytes(&self) -> molecule::bytes::Bytes {
3483 self.0.clone()
3484 }
3485 fn as_slice(&self) -> &[u8] {
3486 &self.0[..]
3487 }
3488 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3489 TransactionViewReader::from_slice(slice).map(|reader| reader.to_entity())
3490 }
3491 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3492 TransactionViewReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3493 }
3494 fn new_builder() -> Self::Builder {
3495 ::core::default::Default::default()
3496 }
3497 fn as_builder(self) -> Self::Builder {
3498 Self::new_builder()
3499 .hash(self.hash())
3500 .witness_hash(self.witness_hash())
3501 .data(self.data())
3502 }
3503}
3504#[derive(Clone, Copy)]
3505pub struct TransactionViewReader<'r>(&'r [u8]);
3506impl<'r> ::core::fmt::LowerHex for TransactionViewReader<'r> {
3507 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3508 use molecule::hex_string;
3509 if f.alternate() {
3510 write!(f, "0x")?;
3511 }
3512 write!(f, "{}", hex_string(self.as_slice()))
3513 }
3514}
3515impl<'r> ::core::fmt::Debug for TransactionViewReader<'r> {
3516 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3517 write!(f, "{}({:#x})", Self::NAME, self)
3518 }
3519}
3520impl<'r> ::core::fmt::Display for TransactionViewReader<'r> {
3521 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3522 write!(f, "{} {{ ", Self::NAME)?;
3523 write!(f, "{}: {}", "hash", self.hash())?;
3524 write!(f, ", {}: {}", "witness_hash", self.witness_hash())?;
3525 write!(f, ", {}: {}", "data", self.data())?;
3526 let extra_count = self.count_extra_fields();
3527 if extra_count != 0 {
3528 write!(f, ", .. ({} fields)", extra_count)?;
3529 }
3530 write!(f, " }}")
3531 }
3532}
3533impl<'r> TransactionViewReader<'r> {
3534 pub const FIELD_COUNT: usize = 3;
3535 pub fn total_size(&self) -> usize {
3536 molecule::unpack_number(self.as_slice()) as usize
3537 }
3538 pub fn field_count(&self) -> usize {
3539 if self.total_size() == molecule::NUMBER_SIZE {
3540 0
3541 } else {
3542 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3543 }
3544 }
3545 pub fn count_extra_fields(&self) -> usize {
3546 self.field_count() - Self::FIELD_COUNT
3547 }
3548 pub fn has_extra_fields(&self) -> bool {
3549 Self::FIELD_COUNT != self.field_count()
3550 }
3551 pub fn hash(&self) -> Byte32Reader<'r> {
3552 let slice = self.as_slice();
3553 let start = molecule::unpack_number(&slice[4..]) as usize;
3554 let end = molecule::unpack_number(&slice[8..]) as usize;
3555 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
3556 }
3557 pub fn witness_hash(&self) -> Byte32Reader<'r> {
3558 let slice = self.as_slice();
3559 let start = molecule::unpack_number(&slice[8..]) as usize;
3560 let end = molecule::unpack_number(&slice[12..]) as usize;
3561 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
3562 }
3563 pub fn data(&self) -> TransactionReader<'r> {
3564 let slice = self.as_slice();
3565 let start = molecule::unpack_number(&slice[12..]) as usize;
3566 if self.has_extra_fields() {
3567 let end = molecule::unpack_number(&slice[16..]) as usize;
3568 TransactionReader::new_unchecked(&self.as_slice()[start..end])
3569 } else {
3570 TransactionReader::new_unchecked(&self.as_slice()[start..])
3571 }
3572 }
3573}
3574impl<'r> molecule::prelude::Reader<'r> for TransactionViewReader<'r> {
3575 type Entity = TransactionView;
3576 const NAME: &'static str = "TransactionViewReader";
3577 fn to_entity(&self) -> Self::Entity {
3578 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3579 }
3580 fn new_unchecked(slice: &'r [u8]) -> Self {
3581 TransactionViewReader(slice)
3582 }
3583 fn as_slice(&self) -> &'r [u8] {
3584 self.0
3585 }
3586 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3587 use molecule::verification_error as ve;
3588 let slice_len = slice.len();
3589 if slice_len < molecule::NUMBER_SIZE {
3590 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3591 }
3592 let total_size = molecule::unpack_number(slice) as usize;
3593 if slice_len != total_size {
3594 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3595 }
3596 if slice_len < molecule::NUMBER_SIZE * 2 {
3597 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3598 }
3599 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3600 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3601 return ve!(Self, OffsetsNotMatch);
3602 }
3603 if slice_len < offset_first {
3604 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3605 }
3606 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3607 if field_count < Self::FIELD_COUNT {
3608 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3609 } else if !compatible && field_count > Self::FIELD_COUNT {
3610 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3611 };
3612 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3613 .chunks_exact(molecule::NUMBER_SIZE)
3614 .map(|x| molecule::unpack_number(x) as usize)
3615 .collect();
3616 offsets.push(total_size);
3617 if offsets.windows(2).any(|i| i[0] > i[1]) {
3618 return ve!(Self, OffsetsNotMatch);
3619 }
3620 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
3621 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
3622 TransactionReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
3623 Ok(())
3624 }
3625}
3626#[derive(Debug, Default)]
3627pub struct TransactionViewBuilder {
3628 pub(crate) hash: Byte32,
3629 pub(crate) witness_hash: Byte32,
3630 pub(crate) data: Transaction,
3631}
3632impl TransactionViewBuilder {
3633 pub const FIELD_COUNT: usize = 3;
3634 pub fn hash(mut self, v: Byte32) -> Self {
3635 self.hash = v;
3636 self
3637 }
3638 pub fn witness_hash(mut self, v: Byte32) -> Self {
3639 self.witness_hash = v;
3640 self
3641 }
3642 pub fn data(mut self, v: Transaction) -> Self {
3643 self.data = v;
3644 self
3645 }
3646}
3647impl molecule::prelude::Builder for TransactionViewBuilder {
3648 type Entity = TransactionView;
3649 const NAME: &'static str = "TransactionViewBuilder";
3650 fn expected_length(&self) -> usize {
3651 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
3652 + self.hash.as_slice().len()
3653 + self.witness_hash.as_slice().len()
3654 + self.data.as_slice().len()
3655 }
3656 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3657 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
3658 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
3659 offsets.push(total_size);
3660 total_size += self.hash.as_slice().len();
3661 offsets.push(total_size);
3662 total_size += self.witness_hash.as_slice().len();
3663 offsets.push(total_size);
3664 total_size += self.data.as_slice().len();
3665 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3666 for offset in offsets.into_iter() {
3667 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3668 }
3669 writer.write_all(self.hash.as_slice())?;
3670 writer.write_all(self.witness_hash.as_slice())?;
3671 writer.write_all(self.data.as_slice())?;
3672 Ok(())
3673 }
3674 fn build(&self) -> Self::Entity {
3675 let mut inner = Vec::with_capacity(self.expected_length());
3676 self.write(&mut inner)
3677 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3678 TransactionView::new_unchecked(inner.into())
3679 }
3680}
3681#[derive(Clone)]
3682pub struct BlockExt(molecule::bytes::Bytes);
3683impl ::core::fmt::LowerHex for BlockExt {
3684 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3685 use molecule::hex_string;
3686 if f.alternate() {
3687 write!(f, "0x")?;
3688 }
3689 write!(f, "{}", hex_string(self.as_slice()))
3690 }
3691}
3692impl ::core::fmt::Debug for BlockExt {
3693 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3694 write!(f, "{}({:#x})", Self::NAME, self)
3695 }
3696}
3697impl ::core::fmt::Display for BlockExt {
3698 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3699 write!(f, "{} {{ ", Self::NAME)?;
3700 write!(f, "{}: {}", "total_difficulty", self.total_difficulty())?;
3701 write!(
3702 f,
3703 ", {}: {}",
3704 "total_uncles_count",
3705 self.total_uncles_count()
3706 )?;
3707 write!(f, ", {}: {}", "received_at", self.received_at())?;
3708 write!(f, ", {}: {}", "txs_fees", self.txs_fees())?;
3709 write!(f, ", {}: {}", "verified", self.verified())?;
3710 let extra_count = self.count_extra_fields();
3711 if extra_count != 0 {
3712 write!(f, ", .. ({} fields)", extra_count)?;
3713 }
3714 write!(f, " }}")
3715 }
3716}
3717impl ::core::default::Default for BlockExt {
3718 fn default() -> Self {
3719 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3720 BlockExt::new_unchecked(v)
3721 }
3722}
3723impl BlockExt {
3724 const DEFAULT_VALUE: [u8; 76] = [
3725 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,
3726 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3727 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3728 ];
3729 pub const FIELD_COUNT: usize = 5;
3730 pub fn total_size(&self) -> usize {
3731 molecule::unpack_number(self.as_slice()) as usize
3732 }
3733 pub fn field_count(&self) -> usize {
3734 if self.total_size() == molecule::NUMBER_SIZE {
3735 0
3736 } else {
3737 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3738 }
3739 }
3740 pub fn count_extra_fields(&self) -> usize {
3741 self.field_count() - Self::FIELD_COUNT
3742 }
3743 pub fn has_extra_fields(&self) -> bool {
3744 Self::FIELD_COUNT != self.field_count()
3745 }
3746 pub fn total_difficulty(&self) -> Uint256 {
3747 let slice = self.as_slice();
3748 let start = molecule::unpack_number(&slice[4..]) as usize;
3749 let end = molecule::unpack_number(&slice[8..]) as usize;
3750 Uint256::new_unchecked(self.0.slice(start..end))
3751 }
3752 pub fn total_uncles_count(&self) -> Uint64 {
3753 let slice = self.as_slice();
3754 let start = molecule::unpack_number(&slice[8..]) as usize;
3755 let end = molecule::unpack_number(&slice[12..]) as usize;
3756 Uint64::new_unchecked(self.0.slice(start..end))
3757 }
3758 pub fn received_at(&self) -> Uint64 {
3759 let slice = self.as_slice();
3760 let start = molecule::unpack_number(&slice[12..]) as usize;
3761 let end = molecule::unpack_number(&slice[16..]) as usize;
3762 Uint64::new_unchecked(self.0.slice(start..end))
3763 }
3764 pub fn txs_fees(&self) -> Uint64Vec {
3765 let slice = self.as_slice();
3766 let start = molecule::unpack_number(&slice[16..]) as usize;
3767 let end = molecule::unpack_number(&slice[20..]) as usize;
3768 Uint64Vec::new_unchecked(self.0.slice(start..end))
3769 }
3770 pub fn verified(&self) -> BoolOpt {
3771 let slice = self.as_slice();
3772 let start = molecule::unpack_number(&slice[20..]) as usize;
3773 if self.has_extra_fields() {
3774 let end = molecule::unpack_number(&slice[24..]) as usize;
3775 BoolOpt::new_unchecked(self.0.slice(start..end))
3776 } else {
3777 BoolOpt::new_unchecked(self.0.slice(start..))
3778 }
3779 }
3780 pub fn as_reader<'r>(&'r self) -> BlockExtReader<'r> {
3781 BlockExtReader::new_unchecked(self.as_slice())
3782 }
3783}
3784impl molecule::prelude::Entity for BlockExt {
3785 type Builder = BlockExtBuilder;
3786 const NAME: &'static str = "BlockExt";
3787 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3788 BlockExt(data)
3789 }
3790 fn as_bytes(&self) -> molecule::bytes::Bytes {
3791 self.0.clone()
3792 }
3793 fn as_slice(&self) -> &[u8] {
3794 &self.0[..]
3795 }
3796 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3797 BlockExtReader::from_slice(slice).map(|reader| reader.to_entity())
3798 }
3799 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3800 BlockExtReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3801 }
3802 fn new_builder() -> Self::Builder {
3803 ::core::default::Default::default()
3804 }
3805 fn as_builder(self) -> Self::Builder {
3806 Self::new_builder()
3807 .total_difficulty(self.total_difficulty())
3808 .total_uncles_count(self.total_uncles_count())
3809 .received_at(self.received_at())
3810 .txs_fees(self.txs_fees())
3811 .verified(self.verified())
3812 }
3813}
3814#[derive(Clone, Copy)]
3815pub struct BlockExtReader<'r>(&'r [u8]);
3816impl<'r> ::core::fmt::LowerHex for BlockExtReader<'r> {
3817 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3818 use molecule::hex_string;
3819 if f.alternate() {
3820 write!(f, "0x")?;
3821 }
3822 write!(f, "{}", hex_string(self.as_slice()))
3823 }
3824}
3825impl<'r> ::core::fmt::Debug for BlockExtReader<'r> {
3826 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3827 write!(f, "{}({:#x})", Self::NAME, self)
3828 }
3829}
3830impl<'r> ::core::fmt::Display for BlockExtReader<'r> {
3831 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3832 write!(f, "{} {{ ", Self::NAME)?;
3833 write!(f, "{}: {}", "total_difficulty", self.total_difficulty())?;
3834 write!(
3835 f,
3836 ", {}: {}",
3837 "total_uncles_count",
3838 self.total_uncles_count()
3839 )?;
3840 write!(f, ", {}: {}", "received_at", self.received_at())?;
3841 write!(f, ", {}: {}", "txs_fees", self.txs_fees())?;
3842 write!(f, ", {}: {}", "verified", self.verified())?;
3843 let extra_count = self.count_extra_fields();
3844 if extra_count != 0 {
3845 write!(f, ", .. ({} fields)", extra_count)?;
3846 }
3847 write!(f, " }}")
3848 }
3849}
3850impl<'r> BlockExtReader<'r> {
3851 pub const FIELD_COUNT: usize = 5;
3852 pub fn total_size(&self) -> usize {
3853 molecule::unpack_number(self.as_slice()) as usize
3854 }
3855 pub fn field_count(&self) -> usize {
3856 if self.total_size() == molecule::NUMBER_SIZE {
3857 0
3858 } else {
3859 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3860 }
3861 }
3862 pub fn count_extra_fields(&self) -> usize {
3863 self.field_count() - Self::FIELD_COUNT
3864 }
3865 pub fn has_extra_fields(&self) -> bool {
3866 Self::FIELD_COUNT != self.field_count()
3867 }
3868 pub fn total_difficulty(&self) -> Uint256Reader<'r> {
3869 let slice = self.as_slice();
3870 let start = molecule::unpack_number(&slice[4..]) as usize;
3871 let end = molecule::unpack_number(&slice[8..]) as usize;
3872 Uint256Reader::new_unchecked(&self.as_slice()[start..end])
3873 }
3874 pub fn total_uncles_count(&self) -> Uint64Reader<'r> {
3875 let slice = self.as_slice();
3876 let start = molecule::unpack_number(&slice[8..]) as usize;
3877 let end = molecule::unpack_number(&slice[12..]) as usize;
3878 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
3879 }
3880 pub fn received_at(&self) -> Uint64Reader<'r> {
3881 let slice = self.as_slice();
3882 let start = molecule::unpack_number(&slice[12..]) as usize;
3883 let end = molecule::unpack_number(&slice[16..]) as usize;
3884 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
3885 }
3886 pub fn txs_fees(&self) -> Uint64VecReader<'r> {
3887 let slice = self.as_slice();
3888 let start = molecule::unpack_number(&slice[16..]) as usize;
3889 let end = molecule::unpack_number(&slice[20..]) as usize;
3890 Uint64VecReader::new_unchecked(&self.as_slice()[start..end])
3891 }
3892 pub fn verified(&self) -> BoolOptReader<'r> {
3893 let slice = self.as_slice();
3894 let start = molecule::unpack_number(&slice[20..]) as usize;
3895 if self.has_extra_fields() {
3896 let end = molecule::unpack_number(&slice[24..]) as usize;
3897 BoolOptReader::new_unchecked(&self.as_slice()[start..end])
3898 } else {
3899 BoolOptReader::new_unchecked(&self.as_slice()[start..])
3900 }
3901 }
3902}
3903impl<'r> molecule::prelude::Reader<'r> for BlockExtReader<'r> {
3904 type Entity = BlockExt;
3905 const NAME: &'static str = "BlockExtReader";
3906 fn to_entity(&self) -> Self::Entity {
3907 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3908 }
3909 fn new_unchecked(slice: &'r [u8]) -> Self {
3910 BlockExtReader(slice)
3911 }
3912 fn as_slice(&self) -> &'r [u8] {
3913 self.0
3914 }
3915 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3916 use molecule::verification_error as ve;
3917 let slice_len = slice.len();
3918 if slice_len < molecule::NUMBER_SIZE {
3919 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3920 }
3921 let total_size = molecule::unpack_number(slice) as usize;
3922 if slice_len != total_size {
3923 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3924 }
3925 if slice_len < molecule::NUMBER_SIZE * 2 {
3926 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3927 }
3928 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3929 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3930 return ve!(Self, OffsetsNotMatch);
3931 }
3932 if slice_len < offset_first {
3933 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3934 }
3935 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3936 if field_count < Self::FIELD_COUNT {
3937 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3938 } else if !compatible && field_count > Self::FIELD_COUNT {
3939 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3940 };
3941 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3942 .chunks_exact(molecule::NUMBER_SIZE)
3943 .map(|x| molecule::unpack_number(x) as usize)
3944 .collect();
3945 offsets.push(total_size);
3946 if offsets.windows(2).any(|i| i[0] > i[1]) {
3947 return ve!(Self, OffsetsNotMatch);
3948 }
3949 Uint256Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
3950 Uint64Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
3951 Uint64Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
3952 Uint64VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
3953 BoolOptReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
3954 Ok(())
3955 }
3956}
3957#[derive(Debug, Default)]
3958pub struct BlockExtBuilder {
3959 pub(crate) total_difficulty: Uint256,
3960 pub(crate) total_uncles_count: Uint64,
3961 pub(crate) received_at: Uint64,
3962 pub(crate) txs_fees: Uint64Vec,
3963 pub(crate) verified: BoolOpt,
3964}
3965impl BlockExtBuilder {
3966 pub const FIELD_COUNT: usize = 5;
3967 pub fn total_difficulty(mut self, v: Uint256) -> Self {
3968 self.total_difficulty = v;
3969 self
3970 }
3971 pub fn total_uncles_count(mut self, v: Uint64) -> Self {
3972 self.total_uncles_count = v;
3973 self
3974 }
3975 pub fn received_at(mut self, v: Uint64) -> Self {
3976 self.received_at = v;
3977 self
3978 }
3979 pub fn txs_fees(mut self, v: Uint64Vec) -> Self {
3980 self.txs_fees = v;
3981 self
3982 }
3983 pub fn verified(mut self, v: BoolOpt) -> Self {
3984 self.verified = v;
3985 self
3986 }
3987}
3988impl molecule::prelude::Builder for BlockExtBuilder {
3989 type Entity = BlockExt;
3990 const NAME: &'static str = "BlockExtBuilder";
3991 fn expected_length(&self) -> usize {
3992 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
3993 + self.total_difficulty.as_slice().len()
3994 + self.total_uncles_count.as_slice().len()
3995 + self.received_at.as_slice().len()
3996 + self.txs_fees.as_slice().len()
3997 + self.verified.as_slice().len()
3998 }
3999 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4000 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4001 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4002 offsets.push(total_size);
4003 total_size += self.total_difficulty.as_slice().len();
4004 offsets.push(total_size);
4005 total_size += self.total_uncles_count.as_slice().len();
4006 offsets.push(total_size);
4007 total_size += self.received_at.as_slice().len();
4008 offsets.push(total_size);
4009 total_size += self.txs_fees.as_slice().len();
4010 offsets.push(total_size);
4011 total_size += self.verified.as_slice().len();
4012 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4013 for offset in offsets.into_iter() {
4014 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4015 }
4016 writer.write_all(self.total_difficulty.as_slice())?;
4017 writer.write_all(self.total_uncles_count.as_slice())?;
4018 writer.write_all(self.received_at.as_slice())?;
4019 writer.write_all(self.txs_fees.as_slice())?;
4020 writer.write_all(self.verified.as_slice())?;
4021 Ok(())
4022 }
4023 fn build(&self) -> Self::Entity {
4024 let mut inner = Vec::with_capacity(self.expected_length());
4025 self.write(&mut inner)
4026 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4027 BlockExt::new_unchecked(inner.into())
4028 }
4029}
4030#[derive(Clone)]
4031pub struct BlockExtV1(molecule::bytes::Bytes);
4032impl ::core::fmt::LowerHex for BlockExtV1 {
4033 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4034 use molecule::hex_string;
4035 if f.alternate() {
4036 write!(f, "0x")?;
4037 }
4038 write!(f, "{}", hex_string(self.as_slice()))
4039 }
4040}
4041impl ::core::fmt::Debug for BlockExtV1 {
4042 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4043 write!(f, "{}({:#x})", Self::NAME, self)
4044 }
4045}
4046impl ::core::fmt::Display for BlockExtV1 {
4047 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4048 write!(f, "{} {{ ", Self::NAME)?;
4049 write!(f, "{}: {}", "total_difficulty", self.total_difficulty())?;
4050 write!(
4051 f,
4052 ", {}: {}",
4053 "total_uncles_count",
4054 self.total_uncles_count()
4055 )?;
4056 write!(f, ", {}: {}", "received_at", self.received_at())?;
4057 write!(f, ", {}: {}", "txs_fees", self.txs_fees())?;
4058 write!(f, ", {}: {}", "verified", self.verified())?;
4059 write!(f, ", {}: {}", "cycles", self.cycles())?;
4060 write!(f, ", {}: {}", "txs_sizes", self.txs_sizes())?;
4061 let extra_count = self.count_extra_fields();
4062 if extra_count != 0 {
4063 write!(f, ", .. ({} fields)", extra_count)?;
4064 }
4065 write!(f, " }}")
4066 }
4067}
4068impl ::core::default::Default for BlockExtV1 {
4069 fn default() -> Self {
4070 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4071 BlockExtV1::new_unchecked(v)
4072 }
4073}
4074impl BlockExtV1 {
4075 const DEFAULT_VALUE: [u8; 84] = [
4076 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,
4077 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,
4078 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4079 ];
4080 pub const FIELD_COUNT: usize = 7;
4081 pub fn total_size(&self) -> usize {
4082 molecule::unpack_number(self.as_slice()) as usize
4083 }
4084 pub fn field_count(&self) -> usize {
4085 if self.total_size() == molecule::NUMBER_SIZE {
4086 0
4087 } else {
4088 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4089 }
4090 }
4091 pub fn count_extra_fields(&self) -> usize {
4092 self.field_count() - Self::FIELD_COUNT
4093 }
4094 pub fn has_extra_fields(&self) -> bool {
4095 Self::FIELD_COUNT != self.field_count()
4096 }
4097 pub fn total_difficulty(&self) -> Uint256 {
4098 let slice = self.as_slice();
4099 let start = molecule::unpack_number(&slice[4..]) as usize;
4100 let end = molecule::unpack_number(&slice[8..]) as usize;
4101 Uint256::new_unchecked(self.0.slice(start..end))
4102 }
4103 pub fn total_uncles_count(&self) -> Uint64 {
4104 let slice = self.as_slice();
4105 let start = molecule::unpack_number(&slice[8..]) as usize;
4106 let end = molecule::unpack_number(&slice[12..]) as usize;
4107 Uint64::new_unchecked(self.0.slice(start..end))
4108 }
4109 pub fn received_at(&self) -> Uint64 {
4110 let slice = self.as_slice();
4111 let start = molecule::unpack_number(&slice[12..]) as usize;
4112 let end = molecule::unpack_number(&slice[16..]) as usize;
4113 Uint64::new_unchecked(self.0.slice(start..end))
4114 }
4115 pub fn txs_fees(&self) -> Uint64Vec {
4116 let slice = self.as_slice();
4117 let start = molecule::unpack_number(&slice[16..]) as usize;
4118 let end = molecule::unpack_number(&slice[20..]) as usize;
4119 Uint64Vec::new_unchecked(self.0.slice(start..end))
4120 }
4121 pub fn verified(&self) -> BoolOpt {
4122 let slice = self.as_slice();
4123 let start = molecule::unpack_number(&slice[20..]) as usize;
4124 let end = molecule::unpack_number(&slice[24..]) as usize;
4125 BoolOpt::new_unchecked(self.0.slice(start..end))
4126 }
4127 pub fn cycles(&self) -> Uint64VecOpt {
4128 let slice = self.as_slice();
4129 let start = molecule::unpack_number(&slice[24..]) as usize;
4130 let end = molecule::unpack_number(&slice[28..]) as usize;
4131 Uint64VecOpt::new_unchecked(self.0.slice(start..end))
4132 }
4133 pub fn txs_sizes(&self) -> Uint64VecOpt {
4134 let slice = self.as_slice();
4135 let start = molecule::unpack_number(&slice[28..]) as usize;
4136 if self.has_extra_fields() {
4137 let end = molecule::unpack_number(&slice[32..]) as usize;
4138 Uint64VecOpt::new_unchecked(self.0.slice(start..end))
4139 } else {
4140 Uint64VecOpt::new_unchecked(self.0.slice(start..))
4141 }
4142 }
4143 pub fn as_reader<'r>(&'r self) -> BlockExtV1Reader<'r> {
4144 BlockExtV1Reader::new_unchecked(self.as_slice())
4145 }
4146}
4147impl molecule::prelude::Entity for BlockExtV1 {
4148 type Builder = BlockExtV1Builder;
4149 const NAME: &'static str = "BlockExtV1";
4150 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4151 BlockExtV1(data)
4152 }
4153 fn as_bytes(&self) -> molecule::bytes::Bytes {
4154 self.0.clone()
4155 }
4156 fn as_slice(&self) -> &[u8] {
4157 &self.0[..]
4158 }
4159 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4160 BlockExtV1Reader::from_slice(slice).map(|reader| reader.to_entity())
4161 }
4162 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4163 BlockExtV1Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4164 }
4165 fn new_builder() -> Self::Builder {
4166 ::core::default::Default::default()
4167 }
4168 fn as_builder(self) -> Self::Builder {
4169 Self::new_builder()
4170 .total_difficulty(self.total_difficulty())
4171 .total_uncles_count(self.total_uncles_count())
4172 .received_at(self.received_at())
4173 .txs_fees(self.txs_fees())
4174 .verified(self.verified())
4175 .cycles(self.cycles())
4176 .txs_sizes(self.txs_sizes())
4177 }
4178}
4179#[derive(Clone, Copy)]
4180pub struct BlockExtV1Reader<'r>(&'r [u8]);
4181impl<'r> ::core::fmt::LowerHex for BlockExtV1Reader<'r> {
4182 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4183 use molecule::hex_string;
4184 if f.alternate() {
4185 write!(f, "0x")?;
4186 }
4187 write!(f, "{}", hex_string(self.as_slice()))
4188 }
4189}
4190impl<'r> ::core::fmt::Debug for BlockExtV1Reader<'r> {
4191 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4192 write!(f, "{}({:#x})", Self::NAME, self)
4193 }
4194}
4195impl<'r> ::core::fmt::Display for BlockExtV1Reader<'r> {
4196 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4197 write!(f, "{} {{ ", Self::NAME)?;
4198 write!(f, "{}: {}", "total_difficulty", self.total_difficulty())?;
4199 write!(
4200 f,
4201 ", {}: {}",
4202 "total_uncles_count",
4203 self.total_uncles_count()
4204 )?;
4205 write!(f, ", {}: {}", "received_at", self.received_at())?;
4206 write!(f, ", {}: {}", "txs_fees", self.txs_fees())?;
4207 write!(f, ", {}: {}", "verified", self.verified())?;
4208 write!(f, ", {}: {}", "cycles", self.cycles())?;
4209 write!(f, ", {}: {}", "txs_sizes", self.txs_sizes())?;
4210 let extra_count = self.count_extra_fields();
4211 if extra_count != 0 {
4212 write!(f, ", .. ({} fields)", extra_count)?;
4213 }
4214 write!(f, " }}")
4215 }
4216}
4217impl<'r> BlockExtV1Reader<'r> {
4218 pub const FIELD_COUNT: usize = 7;
4219 pub fn total_size(&self) -> usize {
4220 molecule::unpack_number(self.as_slice()) as usize
4221 }
4222 pub fn field_count(&self) -> usize {
4223 if self.total_size() == molecule::NUMBER_SIZE {
4224 0
4225 } else {
4226 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4227 }
4228 }
4229 pub fn count_extra_fields(&self) -> usize {
4230 self.field_count() - Self::FIELD_COUNT
4231 }
4232 pub fn has_extra_fields(&self) -> bool {
4233 Self::FIELD_COUNT != self.field_count()
4234 }
4235 pub fn total_difficulty(&self) -> Uint256Reader<'r> {
4236 let slice = self.as_slice();
4237 let start = molecule::unpack_number(&slice[4..]) as usize;
4238 let end = molecule::unpack_number(&slice[8..]) as usize;
4239 Uint256Reader::new_unchecked(&self.as_slice()[start..end])
4240 }
4241 pub fn total_uncles_count(&self) -> Uint64Reader<'r> {
4242 let slice = self.as_slice();
4243 let start = molecule::unpack_number(&slice[8..]) as usize;
4244 let end = molecule::unpack_number(&slice[12..]) as usize;
4245 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
4246 }
4247 pub fn received_at(&self) -> Uint64Reader<'r> {
4248 let slice = self.as_slice();
4249 let start = molecule::unpack_number(&slice[12..]) as usize;
4250 let end = molecule::unpack_number(&slice[16..]) as usize;
4251 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
4252 }
4253 pub fn txs_fees(&self) -> Uint64VecReader<'r> {
4254 let slice = self.as_slice();
4255 let start = molecule::unpack_number(&slice[16..]) as usize;
4256 let end = molecule::unpack_number(&slice[20..]) as usize;
4257 Uint64VecReader::new_unchecked(&self.as_slice()[start..end])
4258 }
4259 pub fn verified(&self) -> BoolOptReader<'r> {
4260 let slice = self.as_slice();
4261 let start = molecule::unpack_number(&slice[20..]) as usize;
4262 let end = molecule::unpack_number(&slice[24..]) as usize;
4263 BoolOptReader::new_unchecked(&self.as_slice()[start..end])
4264 }
4265 pub fn cycles(&self) -> Uint64VecOptReader<'r> {
4266 let slice = self.as_slice();
4267 let start = molecule::unpack_number(&slice[24..]) as usize;
4268 let end = molecule::unpack_number(&slice[28..]) as usize;
4269 Uint64VecOptReader::new_unchecked(&self.as_slice()[start..end])
4270 }
4271 pub fn txs_sizes(&self) -> Uint64VecOptReader<'r> {
4272 let slice = self.as_slice();
4273 let start = molecule::unpack_number(&slice[28..]) as usize;
4274 if self.has_extra_fields() {
4275 let end = molecule::unpack_number(&slice[32..]) as usize;
4276 Uint64VecOptReader::new_unchecked(&self.as_slice()[start..end])
4277 } else {
4278 Uint64VecOptReader::new_unchecked(&self.as_slice()[start..])
4279 }
4280 }
4281}
4282impl<'r> molecule::prelude::Reader<'r> for BlockExtV1Reader<'r> {
4283 type Entity = BlockExtV1;
4284 const NAME: &'static str = "BlockExtV1Reader";
4285 fn to_entity(&self) -> Self::Entity {
4286 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4287 }
4288 fn new_unchecked(slice: &'r [u8]) -> Self {
4289 BlockExtV1Reader(slice)
4290 }
4291 fn as_slice(&self) -> &'r [u8] {
4292 self.0
4293 }
4294 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4295 use molecule::verification_error as ve;
4296 let slice_len = slice.len();
4297 if slice_len < molecule::NUMBER_SIZE {
4298 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4299 }
4300 let total_size = molecule::unpack_number(slice) as usize;
4301 if slice_len != total_size {
4302 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4303 }
4304 if slice_len < molecule::NUMBER_SIZE * 2 {
4305 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
4306 }
4307 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4308 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4309 return ve!(Self, OffsetsNotMatch);
4310 }
4311 if slice_len < offset_first {
4312 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4313 }
4314 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
4315 if field_count < Self::FIELD_COUNT {
4316 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4317 } else if !compatible && field_count > Self::FIELD_COUNT {
4318 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4319 };
4320 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4321 .chunks_exact(molecule::NUMBER_SIZE)
4322 .map(|x| molecule::unpack_number(x) as usize)
4323 .collect();
4324 offsets.push(total_size);
4325 if offsets.windows(2).any(|i| i[0] > i[1]) {
4326 return ve!(Self, OffsetsNotMatch);
4327 }
4328 Uint256Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
4329 Uint64Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
4330 Uint64Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
4331 Uint64VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
4332 BoolOptReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
4333 Uint64VecOptReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
4334 Uint64VecOptReader::verify(&slice[offsets[6]..offsets[7]], compatible)?;
4335 Ok(())
4336 }
4337}
4338#[derive(Debug, Default)]
4339pub struct BlockExtV1Builder {
4340 pub(crate) total_difficulty: Uint256,
4341 pub(crate) total_uncles_count: Uint64,
4342 pub(crate) received_at: Uint64,
4343 pub(crate) txs_fees: Uint64Vec,
4344 pub(crate) verified: BoolOpt,
4345 pub(crate) cycles: Uint64VecOpt,
4346 pub(crate) txs_sizes: Uint64VecOpt,
4347}
4348impl BlockExtV1Builder {
4349 pub const FIELD_COUNT: usize = 7;
4350 pub fn total_difficulty(mut self, v: Uint256) -> Self {
4351 self.total_difficulty = v;
4352 self
4353 }
4354 pub fn total_uncles_count(mut self, v: Uint64) -> Self {
4355 self.total_uncles_count = v;
4356 self
4357 }
4358 pub fn received_at(mut self, v: Uint64) -> Self {
4359 self.received_at = v;
4360 self
4361 }
4362 pub fn txs_fees(mut self, v: Uint64Vec) -> Self {
4363 self.txs_fees = v;
4364 self
4365 }
4366 pub fn verified(mut self, v: BoolOpt) -> Self {
4367 self.verified = v;
4368 self
4369 }
4370 pub fn cycles(mut self, v: Uint64VecOpt) -> Self {
4371 self.cycles = v;
4372 self
4373 }
4374 pub fn txs_sizes(mut self, v: Uint64VecOpt) -> Self {
4375 self.txs_sizes = v;
4376 self
4377 }
4378}
4379impl molecule::prelude::Builder for BlockExtV1Builder {
4380 type Entity = BlockExtV1;
4381 const NAME: &'static str = "BlockExtV1Builder";
4382 fn expected_length(&self) -> usize {
4383 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
4384 + self.total_difficulty.as_slice().len()
4385 + self.total_uncles_count.as_slice().len()
4386 + self.received_at.as_slice().len()
4387 + self.txs_fees.as_slice().len()
4388 + self.verified.as_slice().len()
4389 + self.cycles.as_slice().len()
4390 + self.txs_sizes.as_slice().len()
4391 }
4392 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4393 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4394 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4395 offsets.push(total_size);
4396 total_size += self.total_difficulty.as_slice().len();
4397 offsets.push(total_size);
4398 total_size += self.total_uncles_count.as_slice().len();
4399 offsets.push(total_size);
4400 total_size += self.received_at.as_slice().len();
4401 offsets.push(total_size);
4402 total_size += self.txs_fees.as_slice().len();
4403 offsets.push(total_size);
4404 total_size += self.verified.as_slice().len();
4405 offsets.push(total_size);
4406 total_size += self.cycles.as_slice().len();
4407 offsets.push(total_size);
4408 total_size += self.txs_sizes.as_slice().len();
4409 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4410 for offset in offsets.into_iter() {
4411 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4412 }
4413 writer.write_all(self.total_difficulty.as_slice())?;
4414 writer.write_all(self.total_uncles_count.as_slice())?;
4415 writer.write_all(self.received_at.as_slice())?;
4416 writer.write_all(self.txs_fees.as_slice())?;
4417 writer.write_all(self.verified.as_slice())?;
4418 writer.write_all(self.cycles.as_slice())?;
4419 writer.write_all(self.txs_sizes.as_slice())?;
4420 Ok(())
4421 }
4422 fn build(&self) -> Self::Entity {
4423 let mut inner = Vec::with_capacity(self.expected_length());
4424 self.write(&mut inner)
4425 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4426 BlockExtV1::new_unchecked(inner.into())
4427 }
4428}
4429#[derive(Clone)]
4430pub struct EpochExt(molecule::bytes::Bytes);
4431impl ::core::fmt::LowerHex for EpochExt {
4432 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4433 use molecule::hex_string;
4434 if f.alternate() {
4435 write!(f, "0x")?;
4436 }
4437 write!(f, "{}", hex_string(self.as_slice()))
4438 }
4439}
4440impl ::core::fmt::Debug for EpochExt {
4441 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4442 write!(f, "{}({:#x})", Self::NAME, self)
4443 }
4444}
4445impl ::core::fmt::Display for EpochExt {
4446 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4447 write!(f, "{} {{ ", Self::NAME)?;
4448 write!(
4449 f,
4450 "{}: {}",
4451 "previous_epoch_hash_rate",
4452 self.previous_epoch_hash_rate()
4453 )?;
4454 write!(
4455 f,
4456 ", {}: {}",
4457 "last_block_hash_in_previous_epoch",
4458 self.last_block_hash_in_previous_epoch()
4459 )?;
4460 write!(f, ", {}: {}", "compact_target", self.compact_target())?;
4461 write!(f, ", {}: {}", "number", self.number())?;
4462 write!(f, ", {}: {}", "base_block_reward", self.base_block_reward())?;
4463 write!(f, ", {}: {}", "remainder_reward", self.remainder_reward())?;
4464 write!(f, ", {}: {}", "start_number", self.start_number())?;
4465 write!(f, ", {}: {}", "length", self.length())?;
4466 write!(f, " }}")
4467 }
4468}
4469impl ::core::default::Default for EpochExt {
4470 fn default() -> Self {
4471 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4472 EpochExt::new_unchecked(v)
4473 }
4474}
4475impl EpochExt {
4476 const DEFAULT_VALUE: [u8; 108] = [
4477 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4478 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4479 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4480 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4481 ];
4482 pub const TOTAL_SIZE: usize = 108;
4483 pub const FIELD_SIZES: [usize; 8] = [32, 32, 4, 8, 8, 8, 8, 8];
4484 pub const FIELD_COUNT: usize = 8;
4485 pub fn previous_epoch_hash_rate(&self) -> Uint256 {
4486 Uint256::new_unchecked(self.0.slice(0..32))
4487 }
4488 pub fn last_block_hash_in_previous_epoch(&self) -> Byte32 {
4489 Byte32::new_unchecked(self.0.slice(32..64))
4490 }
4491 pub fn compact_target(&self) -> Uint32 {
4492 Uint32::new_unchecked(self.0.slice(64..68))
4493 }
4494 pub fn number(&self) -> Uint64 {
4495 Uint64::new_unchecked(self.0.slice(68..76))
4496 }
4497 pub fn base_block_reward(&self) -> Uint64 {
4498 Uint64::new_unchecked(self.0.slice(76..84))
4499 }
4500 pub fn remainder_reward(&self) -> Uint64 {
4501 Uint64::new_unchecked(self.0.slice(84..92))
4502 }
4503 pub fn start_number(&self) -> Uint64 {
4504 Uint64::new_unchecked(self.0.slice(92..100))
4505 }
4506 pub fn length(&self) -> Uint64 {
4507 Uint64::new_unchecked(self.0.slice(100..108))
4508 }
4509 pub fn as_reader<'r>(&'r self) -> EpochExtReader<'r> {
4510 EpochExtReader::new_unchecked(self.as_slice())
4511 }
4512}
4513impl molecule::prelude::Entity for EpochExt {
4514 type Builder = EpochExtBuilder;
4515 const NAME: &'static str = "EpochExt";
4516 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4517 EpochExt(data)
4518 }
4519 fn as_bytes(&self) -> molecule::bytes::Bytes {
4520 self.0.clone()
4521 }
4522 fn as_slice(&self) -> &[u8] {
4523 &self.0[..]
4524 }
4525 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4526 EpochExtReader::from_slice(slice).map(|reader| reader.to_entity())
4527 }
4528 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4529 EpochExtReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4530 }
4531 fn new_builder() -> Self::Builder {
4532 ::core::default::Default::default()
4533 }
4534 fn as_builder(self) -> Self::Builder {
4535 Self::new_builder()
4536 .previous_epoch_hash_rate(self.previous_epoch_hash_rate())
4537 .last_block_hash_in_previous_epoch(self.last_block_hash_in_previous_epoch())
4538 .compact_target(self.compact_target())
4539 .number(self.number())
4540 .base_block_reward(self.base_block_reward())
4541 .remainder_reward(self.remainder_reward())
4542 .start_number(self.start_number())
4543 .length(self.length())
4544 }
4545}
4546#[derive(Clone, Copy)]
4547pub struct EpochExtReader<'r>(&'r [u8]);
4548impl<'r> ::core::fmt::LowerHex for EpochExtReader<'r> {
4549 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4550 use molecule::hex_string;
4551 if f.alternate() {
4552 write!(f, "0x")?;
4553 }
4554 write!(f, "{}", hex_string(self.as_slice()))
4555 }
4556}
4557impl<'r> ::core::fmt::Debug for EpochExtReader<'r> {
4558 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4559 write!(f, "{}({:#x})", Self::NAME, self)
4560 }
4561}
4562impl<'r> ::core::fmt::Display for EpochExtReader<'r> {
4563 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4564 write!(f, "{} {{ ", Self::NAME)?;
4565 write!(
4566 f,
4567 "{}: {}",
4568 "previous_epoch_hash_rate",
4569 self.previous_epoch_hash_rate()
4570 )?;
4571 write!(
4572 f,
4573 ", {}: {}",
4574 "last_block_hash_in_previous_epoch",
4575 self.last_block_hash_in_previous_epoch()
4576 )?;
4577 write!(f, ", {}: {}", "compact_target", self.compact_target())?;
4578 write!(f, ", {}: {}", "number", self.number())?;
4579 write!(f, ", {}: {}", "base_block_reward", self.base_block_reward())?;
4580 write!(f, ", {}: {}", "remainder_reward", self.remainder_reward())?;
4581 write!(f, ", {}: {}", "start_number", self.start_number())?;
4582 write!(f, ", {}: {}", "length", self.length())?;
4583 write!(f, " }}")
4584 }
4585}
4586impl<'r> EpochExtReader<'r> {
4587 pub const TOTAL_SIZE: usize = 108;
4588 pub const FIELD_SIZES: [usize; 8] = [32, 32, 4, 8, 8, 8, 8, 8];
4589 pub const FIELD_COUNT: usize = 8;
4590 pub fn previous_epoch_hash_rate(&self) -> Uint256Reader<'r> {
4591 Uint256Reader::new_unchecked(&self.as_slice()[0..32])
4592 }
4593 pub fn last_block_hash_in_previous_epoch(&self) -> Byte32Reader<'r> {
4594 Byte32Reader::new_unchecked(&self.as_slice()[32..64])
4595 }
4596 pub fn compact_target(&self) -> Uint32Reader<'r> {
4597 Uint32Reader::new_unchecked(&self.as_slice()[64..68])
4598 }
4599 pub fn number(&self) -> Uint64Reader<'r> {
4600 Uint64Reader::new_unchecked(&self.as_slice()[68..76])
4601 }
4602 pub fn base_block_reward(&self) -> Uint64Reader<'r> {
4603 Uint64Reader::new_unchecked(&self.as_slice()[76..84])
4604 }
4605 pub fn remainder_reward(&self) -> Uint64Reader<'r> {
4606 Uint64Reader::new_unchecked(&self.as_slice()[84..92])
4607 }
4608 pub fn start_number(&self) -> Uint64Reader<'r> {
4609 Uint64Reader::new_unchecked(&self.as_slice()[92..100])
4610 }
4611 pub fn length(&self) -> Uint64Reader<'r> {
4612 Uint64Reader::new_unchecked(&self.as_slice()[100..108])
4613 }
4614}
4615impl<'r> molecule::prelude::Reader<'r> for EpochExtReader<'r> {
4616 type Entity = EpochExt;
4617 const NAME: &'static str = "EpochExtReader";
4618 fn to_entity(&self) -> Self::Entity {
4619 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4620 }
4621 fn new_unchecked(slice: &'r [u8]) -> Self {
4622 EpochExtReader(slice)
4623 }
4624 fn as_slice(&self) -> &'r [u8] {
4625 self.0
4626 }
4627 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
4628 use molecule::verification_error as ve;
4629 let slice_len = slice.len();
4630 if slice_len != Self::TOTAL_SIZE {
4631 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
4632 }
4633 Ok(())
4634 }
4635}
4636#[derive(Debug, Default)]
4637pub struct EpochExtBuilder {
4638 pub(crate) previous_epoch_hash_rate: Uint256,
4639 pub(crate) last_block_hash_in_previous_epoch: Byte32,
4640 pub(crate) compact_target: Uint32,
4641 pub(crate) number: Uint64,
4642 pub(crate) base_block_reward: Uint64,
4643 pub(crate) remainder_reward: Uint64,
4644 pub(crate) start_number: Uint64,
4645 pub(crate) length: Uint64,
4646}
4647impl EpochExtBuilder {
4648 pub const TOTAL_SIZE: usize = 108;
4649 pub const FIELD_SIZES: [usize; 8] = [32, 32, 4, 8, 8, 8, 8, 8];
4650 pub const FIELD_COUNT: usize = 8;
4651 pub fn previous_epoch_hash_rate(mut self, v: Uint256) -> Self {
4652 self.previous_epoch_hash_rate = v;
4653 self
4654 }
4655 pub fn last_block_hash_in_previous_epoch(mut self, v: Byte32) -> Self {
4656 self.last_block_hash_in_previous_epoch = v;
4657 self
4658 }
4659 pub fn compact_target(mut self, v: Uint32) -> Self {
4660 self.compact_target = v;
4661 self
4662 }
4663 pub fn number(mut self, v: Uint64) -> Self {
4664 self.number = v;
4665 self
4666 }
4667 pub fn base_block_reward(mut self, v: Uint64) -> Self {
4668 self.base_block_reward = v;
4669 self
4670 }
4671 pub fn remainder_reward(mut self, v: Uint64) -> Self {
4672 self.remainder_reward = v;
4673 self
4674 }
4675 pub fn start_number(mut self, v: Uint64) -> Self {
4676 self.start_number = v;
4677 self
4678 }
4679 pub fn length(mut self, v: Uint64) -> Self {
4680 self.length = v;
4681 self
4682 }
4683}
4684impl molecule::prelude::Builder for EpochExtBuilder {
4685 type Entity = EpochExt;
4686 const NAME: &'static str = "EpochExtBuilder";
4687 fn expected_length(&self) -> usize {
4688 Self::TOTAL_SIZE
4689 }
4690 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4691 writer.write_all(self.previous_epoch_hash_rate.as_slice())?;
4692 writer.write_all(self.last_block_hash_in_previous_epoch.as_slice())?;
4693 writer.write_all(self.compact_target.as_slice())?;
4694 writer.write_all(self.number.as_slice())?;
4695 writer.write_all(self.base_block_reward.as_slice())?;
4696 writer.write_all(self.remainder_reward.as_slice())?;
4697 writer.write_all(self.start_number.as_slice())?;
4698 writer.write_all(self.length.as_slice())?;
4699 Ok(())
4700 }
4701 fn build(&self) -> Self::Entity {
4702 let mut inner = Vec::with_capacity(self.expected_length());
4703 self.write(&mut inner)
4704 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4705 EpochExt::new_unchecked(inner.into())
4706 }
4707}
4708#[derive(Clone)]
4709pub struct TransactionKey(molecule::bytes::Bytes);
4710impl ::core::fmt::LowerHex for TransactionKey {
4711 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4712 use molecule::hex_string;
4713 if f.alternate() {
4714 write!(f, "0x")?;
4715 }
4716 write!(f, "{}", hex_string(self.as_slice()))
4717 }
4718}
4719impl ::core::fmt::Debug for TransactionKey {
4720 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4721 write!(f, "{}({:#x})", Self::NAME, self)
4722 }
4723}
4724impl ::core::fmt::Display for TransactionKey {
4725 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4726 write!(f, "{} {{ ", Self::NAME)?;
4727 write!(f, "{}: {}", "block_hash", self.block_hash())?;
4728 write!(f, ", {}: {}", "index", self.index())?;
4729 write!(f, " }}")
4730 }
4731}
4732impl ::core::default::Default for TransactionKey {
4733 fn default() -> Self {
4734 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4735 TransactionKey::new_unchecked(v)
4736 }
4737}
4738impl TransactionKey {
4739 const DEFAULT_VALUE: [u8; 36] = [
4740 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4741 0, 0, 0, 0, 0, 0,
4742 ];
4743 pub const TOTAL_SIZE: usize = 36;
4744 pub const FIELD_SIZES: [usize; 2] = [32, 4];
4745 pub const FIELD_COUNT: usize = 2;
4746 pub fn block_hash(&self) -> Byte32 {
4747 Byte32::new_unchecked(self.0.slice(0..32))
4748 }
4749 pub fn index(&self) -> BeUint32 {
4750 BeUint32::new_unchecked(self.0.slice(32..36))
4751 }
4752 pub fn as_reader<'r>(&'r self) -> TransactionKeyReader<'r> {
4753 TransactionKeyReader::new_unchecked(self.as_slice())
4754 }
4755}
4756impl molecule::prelude::Entity for TransactionKey {
4757 type Builder = TransactionKeyBuilder;
4758 const NAME: &'static str = "TransactionKey";
4759 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4760 TransactionKey(data)
4761 }
4762 fn as_bytes(&self) -> molecule::bytes::Bytes {
4763 self.0.clone()
4764 }
4765 fn as_slice(&self) -> &[u8] {
4766 &self.0[..]
4767 }
4768 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4769 TransactionKeyReader::from_slice(slice).map(|reader| reader.to_entity())
4770 }
4771 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4772 TransactionKeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4773 }
4774 fn new_builder() -> Self::Builder {
4775 ::core::default::Default::default()
4776 }
4777 fn as_builder(self) -> Self::Builder {
4778 Self::new_builder()
4779 .block_hash(self.block_hash())
4780 .index(self.index())
4781 }
4782}
4783#[derive(Clone, Copy)]
4784pub struct TransactionKeyReader<'r>(&'r [u8]);
4785impl<'r> ::core::fmt::LowerHex for TransactionKeyReader<'r> {
4786 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4787 use molecule::hex_string;
4788 if f.alternate() {
4789 write!(f, "0x")?;
4790 }
4791 write!(f, "{}", hex_string(self.as_slice()))
4792 }
4793}
4794impl<'r> ::core::fmt::Debug for TransactionKeyReader<'r> {
4795 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4796 write!(f, "{}({:#x})", Self::NAME, self)
4797 }
4798}
4799impl<'r> ::core::fmt::Display for TransactionKeyReader<'r> {
4800 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4801 write!(f, "{} {{ ", Self::NAME)?;
4802 write!(f, "{}: {}", "block_hash", self.block_hash())?;
4803 write!(f, ", {}: {}", "index", self.index())?;
4804 write!(f, " }}")
4805 }
4806}
4807impl<'r> TransactionKeyReader<'r> {
4808 pub const TOTAL_SIZE: usize = 36;
4809 pub const FIELD_SIZES: [usize; 2] = [32, 4];
4810 pub const FIELD_COUNT: usize = 2;
4811 pub fn block_hash(&self) -> Byte32Reader<'r> {
4812 Byte32Reader::new_unchecked(&self.as_slice()[0..32])
4813 }
4814 pub fn index(&self) -> BeUint32Reader<'r> {
4815 BeUint32Reader::new_unchecked(&self.as_slice()[32..36])
4816 }
4817}
4818impl<'r> molecule::prelude::Reader<'r> for TransactionKeyReader<'r> {
4819 type Entity = TransactionKey;
4820 const NAME: &'static str = "TransactionKeyReader";
4821 fn to_entity(&self) -> Self::Entity {
4822 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4823 }
4824 fn new_unchecked(slice: &'r [u8]) -> Self {
4825 TransactionKeyReader(slice)
4826 }
4827 fn as_slice(&self) -> &'r [u8] {
4828 self.0
4829 }
4830 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
4831 use molecule::verification_error as ve;
4832 let slice_len = slice.len();
4833 if slice_len != Self::TOTAL_SIZE {
4834 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
4835 }
4836 Ok(())
4837 }
4838}
4839#[derive(Debug, Default)]
4840pub struct TransactionKeyBuilder {
4841 pub(crate) block_hash: Byte32,
4842 pub(crate) index: BeUint32,
4843}
4844impl TransactionKeyBuilder {
4845 pub const TOTAL_SIZE: usize = 36;
4846 pub const FIELD_SIZES: [usize; 2] = [32, 4];
4847 pub const FIELD_COUNT: usize = 2;
4848 pub fn block_hash(mut self, v: Byte32) -> Self {
4849 self.block_hash = v;
4850 self
4851 }
4852 pub fn index(mut self, v: BeUint32) -> Self {
4853 self.index = v;
4854 self
4855 }
4856}
4857impl molecule::prelude::Builder for TransactionKeyBuilder {
4858 type Entity = TransactionKey;
4859 const NAME: &'static str = "TransactionKeyBuilder";
4860 fn expected_length(&self) -> usize {
4861 Self::TOTAL_SIZE
4862 }
4863 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4864 writer.write_all(self.block_hash.as_slice())?;
4865 writer.write_all(self.index.as_slice())?;
4866 Ok(())
4867 }
4868 fn build(&self) -> Self::Entity {
4869 let mut inner = Vec::with_capacity(self.expected_length());
4870 self.write(&mut inner)
4871 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4872 TransactionKey::new_unchecked(inner.into())
4873 }
4874}
4875#[derive(Clone)]
4876pub struct NumberHash(molecule::bytes::Bytes);
4877impl ::core::fmt::LowerHex for NumberHash {
4878 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4879 use molecule::hex_string;
4880 if f.alternate() {
4881 write!(f, "0x")?;
4882 }
4883 write!(f, "{}", hex_string(self.as_slice()))
4884 }
4885}
4886impl ::core::fmt::Debug for NumberHash {
4887 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4888 write!(f, "{}({:#x})", Self::NAME, self)
4889 }
4890}
4891impl ::core::fmt::Display for NumberHash {
4892 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4893 write!(f, "{} {{ ", Self::NAME)?;
4894 write!(f, "{}: {}", "number", self.number())?;
4895 write!(f, ", {}: {}", "block_hash", self.block_hash())?;
4896 write!(f, " }}")
4897 }
4898}
4899impl ::core::default::Default for NumberHash {
4900 fn default() -> Self {
4901 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4902 NumberHash::new_unchecked(v)
4903 }
4904}
4905impl NumberHash {
4906 const DEFAULT_VALUE: [u8; 40] = [
4907 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4908 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4909 ];
4910 pub const TOTAL_SIZE: usize = 40;
4911 pub const FIELD_SIZES: [usize; 2] = [8, 32];
4912 pub const FIELD_COUNT: usize = 2;
4913 pub fn number(&self) -> Uint64 {
4914 Uint64::new_unchecked(self.0.slice(0..8))
4915 }
4916 pub fn block_hash(&self) -> Byte32 {
4917 Byte32::new_unchecked(self.0.slice(8..40))
4918 }
4919 pub fn as_reader<'r>(&'r self) -> NumberHashReader<'r> {
4920 NumberHashReader::new_unchecked(self.as_slice())
4921 }
4922}
4923impl molecule::prelude::Entity for NumberHash {
4924 type Builder = NumberHashBuilder;
4925 const NAME: &'static str = "NumberHash";
4926 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4927 NumberHash(data)
4928 }
4929 fn as_bytes(&self) -> molecule::bytes::Bytes {
4930 self.0.clone()
4931 }
4932 fn as_slice(&self) -> &[u8] {
4933 &self.0[..]
4934 }
4935 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4936 NumberHashReader::from_slice(slice).map(|reader| reader.to_entity())
4937 }
4938 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4939 NumberHashReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4940 }
4941 fn new_builder() -> Self::Builder {
4942 ::core::default::Default::default()
4943 }
4944 fn as_builder(self) -> Self::Builder {
4945 Self::new_builder()
4946 .number(self.number())
4947 .block_hash(self.block_hash())
4948 }
4949}
4950#[derive(Clone, Copy)]
4951pub struct NumberHashReader<'r>(&'r [u8]);
4952impl<'r> ::core::fmt::LowerHex for NumberHashReader<'r> {
4953 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4954 use molecule::hex_string;
4955 if f.alternate() {
4956 write!(f, "0x")?;
4957 }
4958 write!(f, "{}", hex_string(self.as_slice()))
4959 }
4960}
4961impl<'r> ::core::fmt::Debug for NumberHashReader<'r> {
4962 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4963 write!(f, "{}({:#x})", Self::NAME, self)
4964 }
4965}
4966impl<'r> ::core::fmt::Display for NumberHashReader<'r> {
4967 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4968 write!(f, "{} {{ ", Self::NAME)?;
4969 write!(f, "{}: {}", "number", self.number())?;
4970 write!(f, ", {}: {}", "block_hash", self.block_hash())?;
4971 write!(f, " }}")
4972 }
4973}
4974impl<'r> NumberHashReader<'r> {
4975 pub const TOTAL_SIZE: usize = 40;
4976 pub const FIELD_SIZES: [usize; 2] = [8, 32];
4977 pub const FIELD_COUNT: usize = 2;
4978 pub fn number(&self) -> Uint64Reader<'r> {
4979 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
4980 }
4981 pub fn block_hash(&self) -> Byte32Reader<'r> {
4982 Byte32Reader::new_unchecked(&self.as_slice()[8..40])
4983 }
4984}
4985impl<'r> molecule::prelude::Reader<'r> for NumberHashReader<'r> {
4986 type Entity = NumberHash;
4987 const NAME: &'static str = "NumberHashReader";
4988 fn to_entity(&self) -> Self::Entity {
4989 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4990 }
4991 fn new_unchecked(slice: &'r [u8]) -> Self {
4992 NumberHashReader(slice)
4993 }
4994 fn as_slice(&self) -> &'r [u8] {
4995 self.0
4996 }
4997 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
4998 use molecule::verification_error as ve;
4999 let slice_len = slice.len();
5000 if slice_len != Self::TOTAL_SIZE {
5001 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
5002 }
5003 Ok(())
5004 }
5005}
5006#[derive(Debug, Default)]
5007pub struct NumberHashBuilder {
5008 pub(crate) number: Uint64,
5009 pub(crate) block_hash: Byte32,
5010}
5011impl NumberHashBuilder {
5012 pub const TOTAL_SIZE: usize = 40;
5013 pub const FIELD_SIZES: [usize; 2] = [8, 32];
5014 pub const FIELD_COUNT: usize = 2;
5015 pub fn number(mut self, v: Uint64) -> Self {
5016 self.number = v;
5017 self
5018 }
5019 pub fn block_hash(mut self, v: Byte32) -> Self {
5020 self.block_hash = v;
5021 self
5022 }
5023}
5024impl molecule::prelude::Builder for NumberHashBuilder {
5025 type Entity = NumberHash;
5026 const NAME: &'static str = "NumberHashBuilder";
5027 fn expected_length(&self) -> usize {
5028 Self::TOTAL_SIZE
5029 }
5030 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5031 writer.write_all(self.number.as_slice())?;
5032 writer.write_all(self.block_hash.as_slice())?;
5033 Ok(())
5034 }
5035 fn build(&self) -> Self::Entity {
5036 let mut inner = Vec::with_capacity(self.expected_length());
5037 self.write(&mut inner)
5038 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5039 NumberHash::new_unchecked(inner.into())
5040 }
5041}
5042#[derive(Clone)]
5043pub struct TransactionInfo(molecule::bytes::Bytes);
5044impl ::core::fmt::LowerHex for TransactionInfo {
5045 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5046 use molecule::hex_string;
5047 if f.alternate() {
5048 write!(f, "0x")?;
5049 }
5050 write!(f, "{}", hex_string(self.as_slice()))
5051 }
5052}
5053impl ::core::fmt::Debug for TransactionInfo {
5054 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5055 write!(f, "{}({:#x})", Self::NAME, self)
5056 }
5057}
5058impl ::core::fmt::Display for TransactionInfo {
5059 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5060 write!(f, "{} {{ ", Self::NAME)?;
5061 write!(f, "{}: {}", "block_number", self.block_number())?;
5062 write!(f, ", {}: {}", "block_epoch", self.block_epoch())?;
5063 write!(f, ", {}: {}", "key", self.key())?;
5064 write!(f, " }}")
5065 }
5066}
5067impl ::core::default::Default for TransactionInfo {
5068 fn default() -> Self {
5069 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5070 TransactionInfo::new_unchecked(v)
5071 }
5072}
5073impl TransactionInfo {
5074 const DEFAULT_VALUE: [u8; 52] = [
5075 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5076 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5077 ];
5078 pub const TOTAL_SIZE: usize = 52;
5079 pub const FIELD_SIZES: [usize; 3] = [8, 8, 36];
5080 pub const FIELD_COUNT: usize = 3;
5081 pub fn block_number(&self) -> Uint64 {
5082 Uint64::new_unchecked(self.0.slice(0..8))
5083 }
5084 pub fn block_epoch(&self) -> Uint64 {
5085 Uint64::new_unchecked(self.0.slice(8..16))
5086 }
5087 pub fn key(&self) -> TransactionKey {
5088 TransactionKey::new_unchecked(self.0.slice(16..52))
5089 }
5090 pub fn as_reader<'r>(&'r self) -> TransactionInfoReader<'r> {
5091 TransactionInfoReader::new_unchecked(self.as_slice())
5092 }
5093}
5094impl molecule::prelude::Entity for TransactionInfo {
5095 type Builder = TransactionInfoBuilder;
5096 const NAME: &'static str = "TransactionInfo";
5097 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5098 TransactionInfo(data)
5099 }
5100 fn as_bytes(&self) -> molecule::bytes::Bytes {
5101 self.0.clone()
5102 }
5103 fn as_slice(&self) -> &[u8] {
5104 &self.0[..]
5105 }
5106 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5107 TransactionInfoReader::from_slice(slice).map(|reader| reader.to_entity())
5108 }
5109 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5110 TransactionInfoReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5111 }
5112 fn new_builder() -> Self::Builder {
5113 ::core::default::Default::default()
5114 }
5115 fn as_builder(self) -> Self::Builder {
5116 Self::new_builder()
5117 .block_number(self.block_number())
5118 .block_epoch(self.block_epoch())
5119 .key(self.key())
5120 }
5121}
5122#[derive(Clone, Copy)]
5123pub struct TransactionInfoReader<'r>(&'r [u8]);
5124impl<'r> ::core::fmt::LowerHex for TransactionInfoReader<'r> {
5125 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5126 use molecule::hex_string;
5127 if f.alternate() {
5128 write!(f, "0x")?;
5129 }
5130 write!(f, "{}", hex_string(self.as_slice()))
5131 }
5132}
5133impl<'r> ::core::fmt::Debug for TransactionInfoReader<'r> {
5134 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5135 write!(f, "{}({:#x})", Self::NAME, self)
5136 }
5137}
5138impl<'r> ::core::fmt::Display for TransactionInfoReader<'r> {
5139 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5140 write!(f, "{} {{ ", Self::NAME)?;
5141 write!(f, "{}: {}", "block_number", self.block_number())?;
5142 write!(f, ", {}: {}", "block_epoch", self.block_epoch())?;
5143 write!(f, ", {}: {}", "key", self.key())?;
5144 write!(f, " }}")
5145 }
5146}
5147impl<'r> TransactionInfoReader<'r> {
5148 pub const TOTAL_SIZE: usize = 52;
5149 pub const FIELD_SIZES: [usize; 3] = [8, 8, 36];
5150 pub const FIELD_COUNT: usize = 3;
5151 pub fn block_number(&self) -> Uint64Reader<'r> {
5152 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
5153 }
5154 pub fn block_epoch(&self) -> Uint64Reader<'r> {
5155 Uint64Reader::new_unchecked(&self.as_slice()[8..16])
5156 }
5157 pub fn key(&self) -> TransactionKeyReader<'r> {
5158 TransactionKeyReader::new_unchecked(&self.as_slice()[16..52])
5159 }
5160}
5161impl<'r> molecule::prelude::Reader<'r> for TransactionInfoReader<'r> {
5162 type Entity = TransactionInfo;
5163 const NAME: &'static str = "TransactionInfoReader";
5164 fn to_entity(&self) -> Self::Entity {
5165 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5166 }
5167 fn new_unchecked(slice: &'r [u8]) -> Self {
5168 TransactionInfoReader(slice)
5169 }
5170 fn as_slice(&self) -> &'r [u8] {
5171 self.0
5172 }
5173 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
5174 use molecule::verification_error as ve;
5175 let slice_len = slice.len();
5176 if slice_len != Self::TOTAL_SIZE {
5177 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
5178 }
5179 Ok(())
5180 }
5181}
5182#[derive(Debug, Default)]
5183pub struct TransactionInfoBuilder {
5184 pub(crate) block_number: Uint64,
5185 pub(crate) block_epoch: Uint64,
5186 pub(crate) key: TransactionKey,
5187}
5188impl TransactionInfoBuilder {
5189 pub const TOTAL_SIZE: usize = 52;
5190 pub const FIELD_SIZES: [usize; 3] = [8, 8, 36];
5191 pub const FIELD_COUNT: usize = 3;
5192 pub fn block_number(mut self, v: Uint64) -> Self {
5193 self.block_number = v;
5194 self
5195 }
5196 pub fn block_epoch(mut self, v: Uint64) -> Self {
5197 self.block_epoch = v;
5198 self
5199 }
5200 pub fn key(mut self, v: TransactionKey) -> Self {
5201 self.key = v;
5202 self
5203 }
5204}
5205impl molecule::prelude::Builder for TransactionInfoBuilder {
5206 type Entity = TransactionInfo;
5207 const NAME: &'static str = "TransactionInfoBuilder";
5208 fn expected_length(&self) -> usize {
5209 Self::TOTAL_SIZE
5210 }
5211 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5212 writer.write_all(self.block_number.as_slice())?;
5213 writer.write_all(self.block_epoch.as_slice())?;
5214 writer.write_all(self.key.as_slice())?;
5215 Ok(())
5216 }
5217 fn build(&self) -> Self::Entity {
5218 let mut inner = Vec::with_capacity(self.expected_length());
5219 self.write(&mut inner)
5220 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5221 TransactionInfo::new_unchecked(inner.into())
5222 }
5223}
5224#[derive(Clone)]
5225pub struct CellEntry(molecule::bytes::Bytes);
5226impl ::core::fmt::LowerHex for CellEntry {
5227 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5228 use molecule::hex_string;
5229 if f.alternate() {
5230 write!(f, "0x")?;
5231 }
5232 write!(f, "{}", hex_string(self.as_slice()))
5233 }
5234}
5235impl ::core::fmt::Debug for CellEntry {
5236 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5237 write!(f, "{}({:#x})", Self::NAME, self)
5238 }
5239}
5240impl ::core::fmt::Display for CellEntry {
5241 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5242 write!(f, "{} {{ ", Self::NAME)?;
5243 write!(f, "{}: {}", "output", self.output())?;
5244 write!(f, ", {}: {}", "block_hash", self.block_hash())?;
5245 write!(f, ", {}: {}", "block_number", self.block_number())?;
5246 write!(f, ", {}: {}", "block_epoch", self.block_epoch())?;
5247 write!(f, ", {}: {}", "index", self.index())?;
5248 write!(f, ", {}: {}", "data_size", self.data_size())?;
5249 let extra_count = self.count_extra_fields();
5250 if extra_count != 0 {
5251 write!(f, ", .. ({} fields)", extra_count)?;
5252 }
5253 write!(f, " }}")
5254 }
5255}
5256impl ::core::default::Default for CellEntry {
5257 fn default() -> Self {
5258 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5259 CellEntry::new_unchecked(v)
5260 }
5261}
5262impl CellEntry {
5263 const DEFAULT_VALUE: [u8; 165] = [
5264 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,
5265 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,
5266 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,
5267 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5269 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5270 ];
5271 pub const FIELD_COUNT: usize = 6;
5272 pub fn total_size(&self) -> usize {
5273 molecule::unpack_number(self.as_slice()) as usize
5274 }
5275 pub fn field_count(&self) -> usize {
5276 if self.total_size() == molecule::NUMBER_SIZE {
5277 0
5278 } else {
5279 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5280 }
5281 }
5282 pub fn count_extra_fields(&self) -> usize {
5283 self.field_count() - Self::FIELD_COUNT
5284 }
5285 pub fn has_extra_fields(&self) -> bool {
5286 Self::FIELD_COUNT != self.field_count()
5287 }
5288 pub fn output(&self) -> CellOutput {
5289 let slice = self.as_slice();
5290 let start = molecule::unpack_number(&slice[4..]) as usize;
5291 let end = molecule::unpack_number(&slice[8..]) as usize;
5292 CellOutput::new_unchecked(self.0.slice(start..end))
5293 }
5294 pub fn block_hash(&self) -> Byte32 {
5295 let slice = self.as_slice();
5296 let start = molecule::unpack_number(&slice[8..]) as usize;
5297 let end = molecule::unpack_number(&slice[12..]) as usize;
5298 Byte32::new_unchecked(self.0.slice(start..end))
5299 }
5300 pub fn block_number(&self) -> Uint64 {
5301 let slice = self.as_slice();
5302 let start = molecule::unpack_number(&slice[12..]) as usize;
5303 let end = molecule::unpack_number(&slice[16..]) as usize;
5304 Uint64::new_unchecked(self.0.slice(start..end))
5305 }
5306 pub fn block_epoch(&self) -> Uint64 {
5307 let slice = self.as_slice();
5308 let start = molecule::unpack_number(&slice[16..]) as usize;
5309 let end = molecule::unpack_number(&slice[20..]) as usize;
5310 Uint64::new_unchecked(self.0.slice(start..end))
5311 }
5312 pub fn index(&self) -> Uint32 {
5313 let slice = self.as_slice();
5314 let start = molecule::unpack_number(&slice[20..]) as usize;
5315 let end = molecule::unpack_number(&slice[24..]) as usize;
5316 Uint32::new_unchecked(self.0.slice(start..end))
5317 }
5318 pub fn data_size(&self) -> Uint64 {
5319 let slice = self.as_slice();
5320 let start = molecule::unpack_number(&slice[24..]) as usize;
5321 if self.has_extra_fields() {
5322 let end = molecule::unpack_number(&slice[28..]) as usize;
5323 Uint64::new_unchecked(self.0.slice(start..end))
5324 } else {
5325 Uint64::new_unchecked(self.0.slice(start..))
5326 }
5327 }
5328 pub fn as_reader<'r>(&'r self) -> CellEntryReader<'r> {
5329 CellEntryReader::new_unchecked(self.as_slice())
5330 }
5331}
5332impl molecule::prelude::Entity for CellEntry {
5333 type Builder = CellEntryBuilder;
5334 const NAME: &'static str = "CellEntry";
5335 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5336 CellEntry(data)
5337 }
5338 fn as_bytes(&self) -> molecule::bytes::Bytes {
5339 self.0.clone()
5340 }
5341 fn as_slice(&self) -> &[u8] {
5342 &self.0[..]
5343 }
5344 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5345 CellEntryReader::from_slice(slice).map(|reader| reader.to_entity())
5346 }
5347 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5348 CellEntryReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5349 }
5350 fn new_builder() -> Self::Builder {
5351 ::core::default::Default::default()
5352 }
5353 fn as_builder(self) -> Self::Builder {
5354 Self::new_builder()
5355 .output(self.output())
5356 .block_hash(self.block_hash())
5357 .block_number(self.block_number())
5358 .block_epoch(self.block_epoch())
5359 .index(self.index())
5360 .data_size(self.data_size())
5361 }
5362}
5363#[derive(Clone, Copy)]
5364pub struct CellEntryReader<'r>(&'r [u8]);
5365impl<'r> ::core::fmt::LowerHex for CellEntryReader<'r> {
5366 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5367 use molecule::hex_string;
5368 if f.alternate() {
5369 write!(f, "0x")?;
5370 }
5371 write!(f, "{}", hex_string(self.as_slice()))
5372 }
5373}
5374impl<'r> ::core::fmt::Debug for CellEntryReader<'r> {
5375 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5376 write!(f, "{}({:#x})", Self::NAME, self)
5377 }
5378}
5379impl<'r> ::core::fmt::Display for CellEntryReader<'r> {
5380 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5381 write!(f, "{} {{ ", Self::NAME)?;
5382 write!(f, "{}: {}", "output", self.output())?;
5383 write!(f, ", {}: {}", "block_hash", self.block_hash())?;
5384 write!(f, ", {}: {}", "block_number", self.block_number())?;
5385 write!(f, ", {}: {}", "block_epoch", self.block_epoch())?;
5386 write!(f, ", {}: {}", "index", self.index())?;
5387 write!(f, ", {}: {}", "data_size", self.data_size())?;
5388 let extra_count = self.count_extra_fields();
5389 if extra_count != 0 {
5390 write!(f, ", .. ({} fields)", extra_count)?;
5391 }
5392 write!(f, " }}")
5393 }
5394}
5395impl<'r> CellEntryReader<'r> {
5396 pub const FIELD_COUNT: usize = 6;
5397 pub fn total_size(&self) -> usize {
5398 molecule::unpack_number(self.as_slice()) as usize
5399 }
5400 pub fn field_count(&self) -> usize {
5401 if self.total_size() == molecule::NUMBER_SIZE {
5402 0
5403 } else {
5404 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5405 }
5406 }
5407 pub fn count_extra_fields(&self) -> usize {
5408 self.field_count() - Self::FIELD_COUNT
5409 }
5410 pub fn has_extra_fields(&self) -> bool {
5411 Self::FIELD_COUNT != self.field_count()
5412 }
5413 pub fn output(&self) -> CellOutputReader<'r> {
5414 let slice = self.as_slice();
5415 let start = molecule::unpack_number(&slice[4..]) as usize;
5416 let end = molecule::unpack_number(&slice[8..]) as usize;
5417 CellOutputReader::new_unchecked(&self.as_slice()[start..end])
5418 }
5419 pub fn block_hash(&self) -> Byte32Reader<'r> {
5420 let slice = self.as_slice();
5421 let start = molecule::unpack_number(&slice[8..]) as usize;
5422 let end = molecule::unpack_number(&slice[12..]) as usize;
5423 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
5424 }
5425 pub fn block_number(&self) -> Uint64Reader<'r> {
5426 let slice = self.as_slice();
5427 let start = molecule::unpack_number(&slice[12..]) as usize;
5428 let end = molecule::unpack_number(&slice[16..]) as usize;
5429 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
5430 }
5431 pub fn block_epoch(&self) -> Uint64Reader<'r> {
5432 let slice = self.as_slice();
5433 let start = molecule::unpack_number(&slice[16..]) as usize;
5434 let end = molecule::unpack_number(&slice[20..]) as usize;
5435 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
5436 }
5437 pub fn index(&self) -> Uint32Reader<'r> {
5438 let slice = self.as_slice();
5439 let start = molecule::unpack_number(&slice[20..]) as usize;
5440 let end = molecule::unpack_number(&slice[24..]) as usize;
5441 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
5442 }
5443 pub fn data_size(&self) -> Uint64Reader<'r> {
5444 let slice = self.as_slice();
5445 let start = molecule::unpack_number(&slice[24..]) as usize;
5446 if self.has_extra_fields() {
5447 let end = molecule::unpack_number(&slice[28..]) as usize;
5448 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
5449 } else {
5450 Uint64Reader::new_unchecked(&self.as_slice()[start..])
5451 }
5452 }
5453}
5454impl<'r> molecule::prelude::Reader<'r> for CellEntryReader<'r> {
5455 type Entity = CellEntry;
5456 const NAME: &'static str = "CellEntryReader";
5457 fn to_entity(&self) -> Self::Entity {
5458 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5459 }
5460 fn new_unchecked(slice: &'r [u8]) -> Self {
5461 CellEntryReader(slice)
5462 }
5463 fn as_slice(&self) -> &'r [u8] {
5464 self.0
5465 }
5466 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5467 use molecule::verification_error as ve;
5468 let slice_len = slice.len();
5469 if slice_len < molecule::NUMBER_SIZE {
5470 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5471 }
5472 let total_size = molecule::unpack_number(slice) as usize;
5473 if slice_len != total_size {
5474 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5475 }
5476 if slice_len < molecule::NUMBER_SIZE * 2 {
5477 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
5478 }
5479 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
5480 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
5481 return ve!(Self, OffsetsNotMatch);
5482 }
5483 if slice_len < offset_first {
5484 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
5485 }
5486 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
5487 if field_count < Self::FIELD_COUNT {
5488 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5489 } else if !compatible && field_count > Self::FIELD_COUNT {
5490 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5491 };
5492 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
5493 .chunks_exact(molecule::NUMBER_SIZE)
5494 .map(|x| molecule::unpack_number(x) as usize)
5495 .collect();
5496 offsets.push(total_size);
5497 if offsets.windows(2).any(|i| i[0] > i[1]) {
5498 return ve!(Self, OffsetsNotMatch);
5499 }
5500 CellOutputReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
5501 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
5502 Uint64Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
5503 Uint64Reader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
5504 Uint32Reader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
5505 Uint64Reader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
5506 Ok(())
5507 }
5508}
5509#[derive(Debug, Default)]
5510pub struct CellEntryBuilder {
5511 pub(crate) output: CellOutput,
5512 pub(crate) block_hash: Byte32,
5513 pub(crate) block_number: Uint64,
5514 pub(crate) block_epoch: Uint64,
5515 pub(crate) index: Uint32,
5516 pub(crate) data_size: Uint64,
5517}
5518impl CellEntryBuilder {
5519 pub const FIELD_COUNT: usize = 6;
5520 pub fn output(mut self, v: CellOutput) -> Self {
5521 self.output = v;
5522 self
5523 }
5524 pub fn block_hash(mut self, v: Byte32) -> Self {
5525 self.block_hash = v;
5526 self
5527 }
5528 pub fn block_number(mut self, v: Uint64) -> Self {
5529 self.block_number = v;
5530 self
5531 }
5532 pub fn block_epoch(mut self, v: Uint64) -> Self {
5533 self.block_epoch = v;
5534 self
5535 }
5536 pub fn index(mut self, v: Uint32) -> Self {
5537 self.index = v;
5538 self
5539 }
5540 pub fn data_size(mut self, v: Uint64) -> Self {
5541 self.data_size = v;
5542 self
5543 }
5544}
5545impl molecule::prelude::Builder for CellEntryBuilder {
5546 type Entity = CellEntry;
5547 const NAME: &'static str = "CellEntryBuilder";
5548 fn expected_length(&self) -> usize {
5549 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
5550 + self.output.as_slice().len()
5551 + self.block_hash.as_slice().len()
5552 + self.block_number.as_slice().len()
5553 + self.block_epoch.as_slice().len()
5554 + self.index.as_slice().len()
5555 + self.data_size.as_slice().len()
5556 }
5557 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5558 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
5559 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
5560 offsets.push(total_size);
5561 total_size += self.output.as_slice().len();
5562 offsets.push(total_size);
5563 total_size += self.block_hash.as_slice().len();
5564 offsets.push(total_size);
5565 total_size += self.block_number.as_slice().len();
5566 offsets.push(total_size);
5567 total_size += self.block_epoch.as_slice().len();
5568 offsets.push(total_size);
5569 total_size += self.index.as_slice().len();
5570 offsets.push(total_size);
5571 total_size += self.data_size.as_slice().len();
5572 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
5573 for offset in offsets.into_iter() {
5574 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
5575 }
5576 writer.write_all(self.output.as_slice())?;
5577 writer.write_all(self.block_hash.as_slice())?;
5578 writer.write_all(self.block_number.as_slice())?;
5579 writer.write_all(self.block_epoch.as_slice())?;
5580 writer.write_all(self.index.as_slice())?;
5581 writer.write_all(self.data_size.as_slice())?;
5582 Ok(())
5583 }
5584 fn build(&self) -> Self::Entity {
5585 let mut inner = Vec::with_capacity(self.expected_length());
5586 self.write(&mut inner)
5587 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5588 CellEntry::new_unchecked(inner.into())
5589 }
5590}
5591#[derive(Clone)]
5592pub struct CellDataEntry(molecule::bytes::Bytes);
5593impl ::core::fmt::LowerHex for CellDataEntry {
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 ::core::fmt::Debug for CellDataEntry {
5603 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5604 write!(f, "{}({:#x})", Self::NAME, self)
5605 }
5606}
5607impl ::core::fmt::Display for CellDataEntry {
5608 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5609 write!(f, "{} {{ ", Self::NAME)?;
5610 write!(f, "{}: {}", "output_data", self.output_data())?;
5611 write!(f, ", {}: {}", "output_data_hash", self.output_data_hash())?;
5612 let extra_count = self.count_extra_fields();
5613 if extra_count != 0 {
5614 write!(f, ", .. ({} fields)", extra_count)?;
5615 }
5616 write!(f, " }}")
5617 }
5618}
5619impl ::core::default::Default for CellDataEntry {
5620 fn default() -> Self {
5621 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5622 CellDataEntry::new_unchecked(v)
5623 }
5624}
5625impl CellDataEntry {
5626 const DEFAULT_VALUE: [u8; 48] = [
5627 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,
5628 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5629 ];
5630 pub const FIELD_COUNT: usize = 2;
5631 pub fn total_size(&self) -> usize {
5632 molecule::unpack_number(self.as_slice()) as usize
5633 }
5634 pub fn field_count(&self) -> usize {
5635 if self.total_size() == molecule::NUMBER_SIZE {
5636 0
5637 } else {
5638 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5639 }
5640 }
5641 pub fn count_extra_fields(&self) -> usize {
5642 self.field_count() - Self::FIELD_COUNT
5643 }
5644 pub fn has_extra_fields(&self) -> bool {
5645 Self::FIELD_COUNT != self.field_count()
5646 }
5647 pub fn output_data(&self) -> Bytes {
5648 let slice = self.as_slice();
5649 let start = molecule::unpack_number(&slice[4..]) as usize;
5650 let end = molecule::unpack_number(&slice[8..]) as usize;
5651 Bytes::new_unchecked(self.0.slice(start..end))
5652 }
5653 pub fn output_data_hash(&self) -> Byte32 {
5654 let slice = self.as_slice();
5655 let start = molecule::unpack_number(&slice[8..]) as usize;
5656 if self.has_extra_fields() {
5657 let end = molecule::unpack_number(&slice[12..]) as usize;
5658 Byte32::new_unchecked(self.0.slice(start..end))
5659 } else {
5660 Byte32::new_unchecked(self.0.slice(start..))
5661 }
5662 }
5663 pub fn as_reader<'r>(&'r self) -> CellDataEntryReader<'r> {
5664 CellDataEntryReader::new_unchecked(self.as_slice())
5665 }
5666}
5667impl molecule::prelude::Entity for CellDataEntry {
5668 type Builder = CellDataEntryBuilder;
5669 const NAME: &'static str = "CellDataEntry";
5670 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5671 CellDataEntry(data)
5672 }
5673 fn as_bytes(&self) -> molecule::bytes::Bytes {
5674 self.0.clone()
5675 }
5676 fn as_slice(&self) -> &[u8] {
5677 &self.0[..]
5678 }
5679 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5680 CellDataEntryReader::from_slice(slice).map(|reader| reader.to_entity())
5681 }
5682 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5683 CellDataEntryReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5684 }
5685 fn new_builder() -> Self::Builder {
5686 ::core::default::Default::default()
5687 }
5688 fn as_builder(self) -> Self::Builder {
5689 Self::new_builder()
5690 .output_data(self.output_data())
5691 .output_data_hash(self.output_data_hash())
5692 }
5693}
5694#[derive(Clone, Copy)]
5695pub struct CellDataEntryReader<'r>(&'r [u8]);
5696impl<'r> ::core::fmt::LowerHex for CellDataEntryReader<'r> {
5697 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5698 use molecule::hex_string;
5699 if f.alternate() {
5700 write!(f, "0x")?;
5701 }
5702 write!(f, "{}", hex_string(self.as_slice()))
5703 }
5704}
5705impl<'r> ::core::fmt::Debug for CellDataEntryReader<'r> {
5706 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5707 write!(f, "{}({:#x})", Self::NAME, self)
5708 }
5709}
5710impl<'r> ::core::fmt::Display for CellDataEntryReader<'r> {
5711 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5712 write!(f, "{} {{ ", Self::NAME)?;
5713 write!(f, "{}: {}", "output_data", self.output_data())?;
5714 write!(f, ", {}: {}", "output_data_hash", self.output_data_hash())?;
5715 let extra_count = self.count_extra_fields();
5716 if extra_count != 0 {
5717 write!(f, ", .. ({} fields)", extra_count)?;
5718 }
5719 write!(f, " }}")
5720 }
5721}
5722impl<'r> CellDataEntryReader<'r> {
5723 pub const FIELD_COUNT: usize = 2;
5724 pub fn total_size(&self) -> usize {
5725 molecule::unpack_number(self.as_slice()) as usize
5726 }
5727 pub fn field_count(&self) -> usize {
5728 if self.total_size() == molecule::NUMBER_SIZE {
5729 0
5730 } else {
5731 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5732 }
5733 }
5734 pub fn count_extra_fields(&self) -> usize {
5735 self.field_count() - Self::FIELD_COUNT
5736 }
5737 pub fn has_extra_fields(&self) -> bool {
5738 Self::FIELD_COUNT != self.field_count()
5739 }
5740 pub fn output_data(&self) -> BytesReader<'r> {
5741 let slice = self.as_slice();
5742 let start = molecule::unpack_number(&slice[4..]) as usize;
5743 let end = molecule::unpack_number(&slice[8..]) as usize;
5744 BytesReader::new_unchecked(&self.as_slice()[start..end])
5745 }
5746 pub fn output_data_hash(&self) -> Byte32Reader<'r> {
5747 let slice = self.as_slice();
5748 let start = molecule::unpack_number(&slice[8..]) as usize;
5749 if self.has_extra_fields() {
5750 let end = molecule::unpack_number(&slice[12..]) as usize;
5751 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
5752 } else {
5753 Byte32Reader::new_unchecked(&self.as_slice()[start..])
5754 }
5755 }
5756}
5757impl<'r> molecule::prelude::Reader<'r> for CellDataEntryReader<'r> {
5758 type Entity = CellDataEntry;
5759 const NAME: &'static str = "CellDataEntryReader";
5760 fn to_entity(&self) -> Self::Entity {
5761 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5762 }
5763 fn new_unchecked(slice: &'r [u8]) -> Self {
5764 CellDataEntryReader(slice)
5765 }
5766 fn as_slice(&self) -> &'r [u8] {
5767 self.0
5768 }
5769 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5770 use molecule::verification_error as ve;
5771 let slice_len = slice.len();
5772 if slice_len < molecule::NUMBER_SIZE {
5773 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5774 }
5775 let total_size = molecule::unpack_number(slice) as usize;
5776 if slice_len != total_size {
5777 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5778 }
5779 if slice_len < molecule::NUMBER_SIZE * 2 {
5780 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
5781 }
5782 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
5783 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
5784 return ve!(Self, OffsetsNotMatch);
5785 }
5786 if slice_len < offset_first {
5787 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
5788 }
5789 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
5790 if field_count < Self::FIELD_COUNT {
5791 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5792 } else if !compatible && field_count > Self::FIELD_COUNT {
5793 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5794 };
5795 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
5796 .chunks_exact(molecule::NUMBER_SIZE)
5797 .map(|x| molecule::unpack_number(x) as usize)
5798 .collect();
5799 offsets.push(total_size);
5800 if offsets.windows(2).any(|i| i[0] > i[1]) {
5801 return ve!(Self, OffsetsNotMatch);
5802 }
5803 BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
5804 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
5805 Ok(())
5806 }
5807}
5808#[derive(Debug, Default)]
5809pub struct CellDataEntryBuilder {
5810 pub(crate) output_data: Bytes,
5811 pub(crate) output_data_hash: Byte32,
5812}
5813impl CellDataEntryBuilder {
5814 pub const FIELD_COUNT: usize = 2;
5815 pub fn output_data(mut self, v: Bytes) -> Self {
5816 self.output_data = v;
5817 self
5818 }
5819 pub fn output_data_hash(mut self, v: Byte32) -> Self {
5820 self.output_data_hash = v;
5821 self
5822 }
5823}
5824impl molecule::prelude::Builder for CellDataEntryBuilder {
5825 type Entity = CellDataEntry;
5826 const NAME: &'static str = "CellDataEntryBuilder";
5827 fn expected_length(&self) -> usize {
5828 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
5829 + self.output_data.as_slice().len()
5830 + self.output_data_hash.as_slice().len()
5831 }
5832 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5833 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
5834 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
5835 offsets.push(total_size);
5836 total_size += self.output_data.as_slice().len();
5837 offsets.push(total_size);
5838 total_size += self.output_data_hash.as_slice().len();
5839 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
5840 for offset in offsets.into_iter() {
5841 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
5842 }
5843 writer.write_all(self.output_data.as_slice())?;
5844 writer.write_all(self.output_data_hash.as_slice())?;
5845 Ok(())
5846 }
5847 fn build(&self) -> Self::Entity {
5848 let mut inner = Vec::with_capacity(self.expected_length());
5849 self.write(&mut inner)
5850 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5851 CellDataEntry::new_unchecked(inner.into())
5852 }
5853}
5854#[derive(Clone)]
5855pub struct RelayMessage(molecule::bytes::Bytes);
5856impl ::core::fmt::LowerHex for RelayMessage {
5857 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5858 use molecule::hex_string;
5859 if f.alternate() {
5860 write!(f, "0x")?;
5861 }
5862 write!(f, "{}", hex_string(self.as_slice()))
5863 }
5864}
5865impl ::core::fmt::Debug for RelayMessage {
5866 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5867 write!(f, "{}({:#x})", Self::NAME, self)
5868 }
5869}
5870impl ::core::fmt::Display for RelayMessage {
5871 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5872 write!(f, "{}(", Self::NAME)?;
5873 self.to_enum().display_inner(f)?;
5874 write!(f, ")")
5875 }
5876}
5877impl ::core::default::Default for RelayMessage {
5878 fn default() -> Self {
5879 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5880 RelayMessage::new_unchecked(v)
5881 }
5882}
5883impl RelayMessage {
5884 const DEFAULT_VALUE: [u8; 252] = [
5885 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,
5886 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5887 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5888 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5889 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5890 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5891 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5892 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5893 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5894 ];
5895 pub const ITEMS_COUNT: usize = 8;
5896 pub fn item_id(&self) -> molecule::Number {
5897 molecule::unpack_number(self.as_slice())
5898 }
5899 pub fn to_enum(&self) -> RelayMessageUnion {
5900 let inner = self.0.slice(molecule::NUMBER_SIZE..);
5901 match self.item_id() {
5902 0 => CompactBlock::new_unchecked(inner).into(),
5903 1 => RelayTransactions::new_unchecked(inner).into(),
5904 2 => RelayTransactionHashes::new_unchecked(inner).into(),
5905 3 => GetRelayTransactions::new_unchecked(inner).into(),
5906 4 => GetBlockTransactions::new_unchecked(inner).into(),
5907 5 => BlockTransactions::new_unchecked(inner).into(),
5908 6 => GetBlockProposal::new_unchecked(inner).into(),
5909 7 => BlockProposal::new_unchecked(inner).into(),
5910 _ => panic!("{}: invalid data", Self::NAME),
5911 }
5912 }
5913 pub fn as_reader<'r>(&'r self) -> RelayMessageReader<'r> {
5914 RelayMessageReader::new_unchecked(self.as_slice())
5915 }
5916}
5917impl molecule::prelude::Entity for RelayMessage {
5918 type Builder = RelayMessageBuilder;
5919 const NAME: &'static str = "RelayMessage";
5920 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5921 RelayMessage(data)
5922 }
5923 fn as_bytes(&self) -> molecule::bytes::Bytes {
5924 self.0.clone()
5925 }
5926 fn as_slice(&self) -> &[u8] {
5927 &self.0[..]
5928 }
5929 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5930 RelayMessageReader::from_slice(slice).map(|reader| reader.to_entity())
5931 }
5932 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5933 RelayMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5934 }
5935 fn new_builder() -> Self::Builder {
5936 ::core::default::Default::default()
5937 }
5938 fn as_builder(self) -> Self::Builder {
5939 Self::new_builder().set(self.to_enum())
5940 }
5941}
5942#[derive(Clone, Copy)]
5943pub struct RelayMessageReader<'r>(&'r [u8]);
5944impl<'r> ::core::fmt::LowerHex for RelayMessageReader<'r> {
5945 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5946 use molecule::hex_string;
5947 if f.alternate() {
5948 write!(f, "0x")?;
5949 }
5950 write!(f, "{}", hex_string(self.as_slice()))
5951 }
5952}
5953impl<'r> ::core::fmt::Debug for RelayMessageReader<'r> {
5954 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5955 write!(f, "{}({:#x})", Self::NAME, self)
5956 }
5957}
5958impl<'r> ::core::fmt::Display for RelayMessageReader<'r> {
5959 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5960 write!(f, "{}(", Self::NAME)?;
5961 self.to_enum().display_inner(f)?;
5962 write!(f, ")")
5963 }
5964}
5965impl<'r> RelayMessageReader<'r> {
5966 pub const ITEMS_COUNT: usize = 8;
5967 pub fn item_id(&self) -> molecule::Number {
5968 molecule::unpack_number(self.as_slice())
5969 }
5970 pub fn to_enum(&self) -> RelayMessageUnionReader<'r> {
5971 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
5972 match self.item_id() {
5973 0 => CompactBlockReader::new_unchecked(inner).into(),
5974 1 => RelayTransactionsReader::new_unchecked(inner).into(),
5975 2 => RelayTransactionHashesReader::new_unchecked(inner).into(),
5976 3 => GetRelayTransactionsReader::new_unchecked(inner).into(),
5977 4 => GetBlockTransactionsReader::new_unchecked(inner).into(),
5978 5 => BlockTransactionsReader::new_unchecked(inner).into(),
5979 6 => GetBlockProposalReader::new_unchecked(inner).into(),
5980 7 => BlockProposalReader::new_unchecked(inner).into(),
5981 _ => panic!("{}: invalid data", Self::NAME),
5982 }
5983 }
5984}
5985impl<'r> molecule::prelude::Reader<'r> for RelayMessageReader<'r> {
5986 type Entity = RelayMessage;
5987 const NAME: &'static str = "RelayMessageReader";
5988 fn to_entity(&self) -> Self::Entity {
5989 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5990 }
5991 fn new_unchecked(slice: &'r [u8]) -> Self {
5992 RelayMessageReader(slice)
5993 }
5994 fn as_slice(&self) -> &'r [u8] {
5995 self.0
5996 }
5997 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5998 use molecule::verification_error as ve;
5999 let slice_len = slice.len();
6000 if slice_len < molecule::NUMBER_SIZE {
6001 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6002 }
6003 let item_id = molecule::unpack_number(slice);
6004 let inner_slice = &slice[molecule::NUMBER_SIZE..];
6005 match item_id {
6006 0 => CompactBlockReader::verify(inner_slice, compatible),
6007 1 => RelayTransactionsReader::verify(inner_slice, compatible),
6008 2 => RelayTransactionHashesReader::verify(inner_slice, compatible),
6009 3 => GetRelayTransactionsReader::verify(inner_slice, compatible),
6010 4 => GetBlockTransactionsReader::verify(inner_slice, compatible),
6011 5 => BlockTransactionsReader::verify(inner_slice, compatible),
6012 6 => GetBlockProposalReader::verify(inner_slice, compatible),
6013 7 => BlockProposalReader::verify(inner_slice, compatible),
6014 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
6015 }?;
6016 Ok(())
6017 }
6018}
6019#[derive(Debug, Default)]
6020pub struct RelayMessageBuilder(pub(crate) RelayMessageUnion);
6021impl RelayMessageBuilder {
6022 pub const ITEMS_COUNT: usize = 8;
6023 pub fn set<I>(mut self, v: I) -> Self
6024 where
6025 I: ::core::convert::Into<RelayMessageUnion>,
6026 {
6027 self.0 = v.into();
6028 self
6029 }
6030}
6031impl molecule::prelude::Builder for RelayMessageBuilder {
6032 type Entity = RelayMessage;
6033 const NAME: &'static str = "RelayMessageBuilder";
6034 fn expected_length(&self) -> usize {
6035 molecule::NUMBER_SIZE + self.0.as_slice().len()
6036 }
6037 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6038 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
6039 writer.write_all(self.0.as_slice())
6040 }
6041 fn build(&self) -> Self::Entity {
6042 let mut inner = Vec::with_capacity(self.expected_length());
6043 self.write(&mut inner)
6044 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6045 RelayMessage::new_unchecked(inner.into())
6046 }
6047}
6048#[derive(Debug, Clone)]
6049pub enum RelayMessageUnion {
6050 CompactBlock(CompactBlock),
6051 RelayTransactions(RelayTransactions),
6052 RelayTransactionHashes(RelayTransactionHashes),
6053 GetRelayTransactions(GetRelayTransactions),
6054 GetBlockTransactions(GetBlockTransactions),
6055 BlockTransactions(BlockTransactions),
6056 GetBlockProposal(GetBlockProposal),
6057 BlockProposal(BlockProposal),
6058}
6059#[derive(Debug, Clone, Copy)]
6060pub enum RelayMessageUnionReader<'r> {
6061 CompactBlock(CompactBlockReader<'r>),
6062 RelayTransactions(RelayTransactionsReader<'r>),
6063 RelayTransactionHashes(RelayTransactionHashesReader<'r>),
6064 GetRelayTransactions(GetRelayTransactionsReader<'r>),
6065 GetBlockTransactions(GetBlockTransactionsReader<'r>),
6066 BlockTransactions(BlockTransactionsReader<'r>),
6067 GetBlockProposal(GetBlockProposalReader<'r>),
6068 BlockProposal(BlockProposalReader<'r>),
6069}
6070impl ::core::default::Default for RelayMessageUnion {
6071 fn default() -> Self {
6072 RelayMessageUnion::CompactBlock(::core::default::Default::default())
6073 }
6074}
6075impl ::core::fmt::Display for RelayMessageUnion {
6076 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6077 match self {
6078 RelayMessageUnion::CompactBlock(ref item) => {
6079 write!(f, "{}::{}({})", Self::NAME, CompactBlock::NAME, item)
6080 }
6081 RelayMessageUnion::RelayTransactions(ref item) => {
6082 write!(f, "{}::{}({})", Self::NAME, RelayTransactions::NAME, item)
6083 }
6084 RelayMessageUnion::RelayTransactionHashes(ref item) => {
6085 write!(
6086 f,
6087 "{}::{}({})",
6088 Self::NAME,
6089 RelayTransactionHashes::NAME,
6090 item
6091 )
6092 }
6093 RelayMessageUnion::GetRelayTransactions(ref item) => {
6094 write!(
6095 f,
6096 "{}::{}({})",
6097 Self::NAME,
6098 GetRelayTransactions::NAME,
6099 item
6100 )
6101 }
6102 RelayMessageUnion::GetBlockTransactions(ref item) => {
6103 write!(
6104 f,
6105 "{}::{}({})",
6106 Self::NAME,
6107 GetBlockTransactions::NAME,
6108 item
6109 )
6110 }
6111 RelayMessageUnion::BlockTransactions(ref item) => {
6112 write!(f, "{}::{}({})", Self::NAME, BlockTransactions::NAME, item)
6113 }
6114 RelayMessageUnion::GetBlockProposal(ref item) => {
6115 write!(f, "{}::{}({})", Self::NAME, GetBlockProposal::NAME, item)
6116 }
6117 RelayMessageUnion::BlockProposal(ref item) => {
6118 write!(f, "{}::{}({})", Self::NAME, BlockProposal::NAME, item)
6119 }
6120 }
6121 }
6122}
6123impl<'r> ::core::fmt::Display for RelayMessageUnionReader<'r> {
6124 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6125 match self {
6126 RelayMessageUnionReader::CompactBlock(ref item) => {
6127 write!(f, "{}::{}({})", Self::NAME, CompactBlock::NAME, item)
6128 }
6129 RelayMessageUnionReader::RelayTransactions(ref item) => {
6130 write!(f, "{}::{}({})", Self::NAME, RelayTransactions::NAME, item)
6131 }
6132 RelayMessageUnionReader::RelayTransactionHashes(ref item) => {
6133 write!(
6134 f,
6135 "{}::{}({})",
6136 Self::NAME,
6137 RelayTransactionHashes::NAME,
6138 item
6139 )
6140 }
6141 RelayMessageUnionReader::GetRelayTransactions(ref item) => {
6142 write!(
6143 f,
6144 "{}::{}({})",
6145 Self::NAME,
6146 GetRelayTransactions::NAME,
6147 item
6148 )
6149 }
6150 RelayMessageUnionReader::GetBlockTransactions(ref item) => {
6151 write!(
6152 f,
6153 "{}::{}({})",
6154 Self::NAME,
6155 GetBlockTransactions::NAME,
6156 item
6157 )
6158 }
6159 RelayMessageUnionReader::BlockTransactions(ref item) => {
6160 write!(f, "{}::{}({})", Self::NAME, BlockTransactions::NAME, item)
6161 }
6162 RelayMessageUnionReader::GetBlockProposal(ref item) => {
6163 write!(f, "{}::{}({})", Self::NAME, GetBlockProposal::NAME, item)
6164 }
6165 RelayMessageUnionReader::BlockProposal(ref item) => {
6166 write!(f, "{}::{}({})", Self::NAME, BlockProposal::NAME, item)
6167 }
6168 }
6169 }
6170}
6171impl RelayMessageUnion {
6172 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6173 match self {
6174 RelayMessageUnion::CompactBlock(ref item) => write!(f, "{}", item),
6175 RelayMessageUnion::RelayTransactions(ref item) => write!(f, "{}", item),
6176 RelayMessageUnion::RelayTransactionHashes(ref item) => write!(f, "{}", item),
6177 RelayMessageUnion::GetRelayTransactions(ref item) => write!(f, "{}", item),
6178 RelayMessageUnion::GetBlockTransactions(ref item) => write!(f, "{}", item),
6179 RelayMessageUnion::BlockTransactions(ref item) => write!(f, "{}", item),
6180 RelayMessageUnion::GetBlockProposal(ref item) => write!(f, "{}", item),
6181 RelayMessageUnion::BlockProposal(ref item) => write!(f, "{}", item),
6182 }
6183 }
6184}
6185impl<'r> RelayMessageUnionReader<'r> {
6186 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6187 match self {
6188 RelayMessageUnionReader::CompactBlock(ref item) => write!(f, "{}", item),
6189 RelayMessageUnionReader::RelayTransactions(ref item) => write!(f, "{}", item),
6190 RelayMessageUnionReader::RelayTransactionHashes(ref item) => write!(f, "{}", item),
6191 RelayMessageUnionReader::GetRelayTransactions(ref item) => write!(f, "{}", item),
6192 RelayMessageUnionReader::GetBlockTransactions(ref item) => write!(f, "{}", item),
6193 RelayMessageUnionReader::BlockTransactions(ref item) => write!(f, "{}", item),
6194 RelayMessageUnionReader::GetBlockProposal(ref item) => write!(f, "{}", item),
6195 RelayMessageUnionReader::BlockProposal(ref item) => write!(f, "{}", item),
6196 }
6197 }
6198}
6199impl ::core::convert::From<CompactBlock> for RelayMessageUnion {
6200 fn from(item: CompactBlock) -> Self {
6201 RelayMessageUnion::CompactBlock(item)
6202 }
6203}
6204impl ::core::convert::From<RelayTransactions> for RelayMessageUnion {
6205 fn from(item: RelayTransactions) -> Self {
6206 RelayMessageUnion::RelayTransactions(item)
6207 }
6208}
6209impl ::core::convert::From<RelayTransactionHashes> for RelayMessageUnion {
6210 fn from(item: RelayTransactionHashes) -> Self {
6211 RelayMessageUnion::RelayTransactionHashes(item)
6212 }
6213}
6214impl ::core::convert::From<GetRelayTransactions> for RelayMessageUnion {
6215 fn from(item: GetRelayTransactions) -> Self {
6216 RelayMessageUnion::GetRelayTransactions(item)
6217 }
6218}
6219impl ::core::convert::From<GetBlockTransactions> for RelayMessageUnion {
6220 fn from(item: GetBlockTransactions) -> Self {
6221 RelayMessageUnion::GetBlockTransactions(item)
6222 }
6223}
6224impl ::core::convert::From<BlockTransactions> for RelayMessageUnion {
6225 fn from(item: BlockTransactions) -> Self {
6226 RelayMessageUnion::BlockTransactions(item)
6227 }
6228}
6229impl ::core::convert::From<GetBlockProposal> for RelayMessageUnion {
6230 fn from(item: GetBlockProposal) -> Self {
6231 RelayMessageUnion::GetBlockProposal(item)
6232 }
6233}
6234impl ::core::convert::From<BlockProposal> for RelayMessageUnion {
6235 fn from(item: BlockProposal) -> Self {
6236 RelayMessageUnion::BlockProposal(item)
6237 }
6238}
6239impl<'r> ::core::convert::From<CompactBlockReader<'r>> for RelayMessageUnionReader<'r> {
6240 fn from(item: CompactBlockReader<'r>) -> Self {
6241 RelayMessageUnionReader::CompactBlock(item)
6242 }
6243}
6244impl<'r> ::core::convert::From<RelayTransactionsReader<'r>> for RelayMessageUnionReader<'r> {
6245 fn from(item: RelayTransactionsReader<'r>) -> Self {
6246 RelayMessageUnionReader::RelayTransactions(item)
6247 }
6248}
6249impl<'r> ::core::convert::From<RelayTransactionHashesReader<'r>> for RelayMessageUnionReader<'r> {
6250 fn from(item: RelayTransactionHashesReader<'r>) -> Self {
6251 RelayMessageUnionReader::RelayTransactionHashes(item)
6252 }
6253}
6254impl<'r> ::core::convert::From<GetRelayTransactionsReader<'r>> for RelayMessageUnionReader<'r> {
6255 fn from(item: GetRelayTransactionsReader<'r>) -> Self {
6256 RelayMessageUnionReader::GetRelayTransactions(item)
6257 }
6258}
6259impl<'r> ::core::convert::From<GetBlockTransactionsReader<'r>> for RelayMessageUnionReader<'r> {
6260 fn from(item: GetBlockTransactionsReader<'r>) -> Self {
6261 RelayMessageUnionReader::GetBlockTransactions(item)
6262 }
6263}
6264impl<'r> ::core::convert::From<BlockTransactionsReader<'r>> for RelayMessageUnionReader<'r> {
6265 fn from(item: BlockTransactionsReader<'r>) -> Self {
6266 RelayMessageUnionReader::BlockTransactions(item)
6267 }
6268}
6269impl<'r> ::core::convert::From<GetBlockProposalReader<'r>> for RelayMessageUnionReader<'r> {
6270 fn from(item: GetBlockProposalReader<'r>) -> Self {
6271 RelayMessageUnionReader::GetBlockProposal(item)
6272 }
6273}
6274impl<'r> ::core::convert::From<BlockProposalReader<'r>> for RelayMessageUnionReader<'r> {
6275 fn from(item: BlockProposalReader<'r>) -> Self {
6276 RelayMessageUnionReader::BlockProposal(item)
6277 }
6278}
6279impl RelayMessageUnion {
6280 pub const NAME: &'static str = "RelayMessageUnion";
6281 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
6282 match self {
6283 RelayMessageUnion::CompactBlock(item) => item.as_bytes(),
6284 RelayMessageUnion::RelayTransactions(item) => item.as_bytes(),
6285 RelayMessageUnion::RelayTransactionHashes(item) => item.as_bytes(),
6286 RelayMessageUnion::GetRelayTransactions(item) => item.as_bytes(),
6287 RelayMessageUnion::GetBlockTransactions(item) => item.as_bytes(),
6288 RelayMessageUnion::BlockTransactions(item) => item.as_bytes(),
6289 RelayMessageUnion::GetBlockProposal(item) => item.as_bytes(),
6290 RelayMessageUnion::BlockProposal(item) => item.as_bytes(),
6291 }
6292 }
6293 pub fn as_slice(&self) -> &[u8] {
6294 match self {
6295 RelayMessageUnion::CompactBlock(item) => item.as_slice(),
6296 RelayMessageUnion::RelayTransactions(item) => item.as_slice(),
6297 RelayMessageUnion::RelayTransactionHashes(item) => item.as_slice(),
6298 RelayMessageUnion::GetRelayTransactions(item) => item.as_slice(),
6299 RelayMessageUnion::GetBlockTransactions(item) => item.as_slice(),
6300 RelayMessageUnion::BlockTransactions(item) => item.as_slice(),
6301 RelayMessageUnion::GetBlockProposal(item) => item.as_slice(),
6302 RelayMessageUnion::BlockProposal(item) => item.as_slice(),
6303 }
6304 }
6305 pub fn item_id(&self) -> molecule::Number {
6306 match self {
6307 RelayMessageUnion::CompactBlock(_) => 0,
6308 RelayMessageUnion::RelayTransactions(_) => 1,
6309 RelayMessageUnion::RelayTransactionHashes(_) => 2,
6310 RelayMessageUnion::GetRelayTransactions(_) => 3,
6311 RelayMessageUnion::GetBlockTransactions(_) => 4,
6312 RelayMessageUnion::BlockTransactions(_) => 5,
6313 RelayMessageUnion::GetBlockProposal(_) => 6,
6314 RelayMessageUnion::BlockProposal(_) => 7,
6315 }
6316 }
6317 pub fn item_name(&self) -> &str {
6318 match self {
6319 RelayMessageUnion::CompactBlock(_) => "CompactBlock",
6320 RelayMessageUnion::RelayTransactions(_) => "RelayTransactions",
6321 RelayMessageUnion::RelayTransactionHashes(_) => "RelayTransactionHashes",
6322 RelayMessageUnion::GetRelayTransactions(_) => "GetRelayTransactions",
6323 RelayMessageUnion::GetBlockTransactions(_) => "GetBlockTransactions",
6324 RelayMessageUnion::BlockTransactions(_) => "BlockTransactions",
6325 RelayMessageUnion::GetBlockProposal(_) => "GetBlockProposal",
6326 RelayMessageUnion::BlockProposal(_) => "BlockProposal",
6327 }
6328 }
6329 pub fn as_reader<'r>(&'r self) -> RelayMessageUnionReader<'r> {
6330 match self {
6331 RelayMessageUnion::CompactBlock(item) => item.as_reader().into(),
6332 RelayMessageUnion::RelayTransactions(item) => item.as_reader().into(),
6333 RelayMessageUnion::RelayTransactionHashes(item) => item.as_reader().into(),
6334 RelayMessageUnion::GetRelayTransactions(item) => item.as_reader().into(),
6335 RelayMessageUnion::GetBlockTransactions(item) => item.as_reader().into(),
6336 RelayMessageUnion::BlockTransactions(item) => item.as_reader().into(),
6337 RelayMessageUnion::GetBlockProposal(item) => item.as_reader().into(),
6338 RelayMessageUnion::BlockProposal(item) => item.as_reader().into(),
6339 }
6340 }
6341}
6342impl<'r> RelayMessageUnionReader<'r> {
6343 pub const NAME: &'r str = "RelayMessageUnionReader";
6344 pub fn as_slice(&self) -> &'r [u8] {
6345 match self {
6346 RelayMessageUnionReader::CompactBlock(item) => item.as_slice(),
6347 RelayMessageUnionReader::RelayTransactions(item) => item.as_slice(),
6348 RelayMessageUnionReader::RelayTransactionHashes(item) => item.as_slice(),
6349 RelayMessageUnionReader::GetRelayTransactions(item) => item.as_slice(),
6350 RelayMessageUnionReader::GetBlockTransactions(item) => item.as_slice(),
6351 RelayMessageUnionReader::BlockTransactions(item) => item.as_slice(),
6352 RelayMessageUnionReader::GetBlockProposal(item) => item.as_slice(),
6353 RelayMessageUnionReader::BlockProposal(item) => item.as_slice(),
6354 }
6355 }
6356 pub fn item_id(&self) -> molecule::Number {
6357 match self {
6358 RelayMessageUnionReader::CompactBlock(_) => 0,
6359 RelayMessageUnionReader::RelayTransactions(_) => 1,
6360 RelayMessageUnionReader::RelayTransactionHashes(_) => 2,
6361 RelayMessageUnionReader::GetRelayTransactions(_) => 3,
6362 RelayMessageUnionReader::GetBlockTransactions(_) => 4,
6363 RelayMessageUnionReader::BlockTransactions(_) => 5,
6364 RelayMessageUnionReader::GetBlockProposal(_) => 6,
6365 RelayMessageUnionReader::BlockProposal(_) => 7,
6366 }
6367 }
6368 pub fn item_name(&self) -> &str {
6369 match self {
6370 RelayMessageUnionReader::CompactBlock(_) => "CompactBlock",
6371 RelayMessageUnionReader::RelayTransactions(_) => "RelayTransactions",
6372 RelayMessageUnionReader::RelayTransactionHashes(_) => "RelayTransactionHashes",
6373 RelayMessageUnionReader::GetRelayTransactions(_) => "GetRelayTransactions",
6374 RelayMessageUnionReader::GetBlockTransactions(_) => "GetBlockTransactions",
6375 RelayMessageUnionReader::BlockTransactions(_) => "BlockTransactions",
6376 RelayMessageUnionReader::GetBlockProposal(_) => "GetBlockProposal",
6377 RelayMessageUnionReader::BlockProposal(_) => "BlockProposal",
6378 }
6379 }
6380}
6381#[derive(Clone)]
6382pub struct CompactBlock(molecule::bytes::Bytes);
6383impl ::core::fmt::LowerHex for CompactBlock {
6384 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6385 use molecule::hex_string;
6386 if f.alternate() {
6387 write!(f, "0x")?;
6388 }
6389 write!(f, "{}", hex_string(self.as_slice()))
6390 }
6391}
6392impl ::core::fmt::Debug for CompactBlock {
6393 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6394 write!(f, "{}({:#x})", Self::NAME, self)
6395 }
6396}
6397impl ::core::fmt::Display for CompactBlock {
6398 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6399 write!(f, "{} {{ ", Self::NAME)?;
6400 write!(f, "{}: {}", "header", self.header())?;
6401 write!(f, ", {}: {}", "short_ids", self.short_ids())?;
6402 write!(
6403 f,
6404 ", {}: {}",
6405 "prefilled_transactions",
6406 self.prefilled_transactions()
6407 )?;
6408 write!(f, ", {}: {}", "uncles", self.uncles())?;
6409 write!(f, ", {}: {}", "proposals", self.proposals())?;
6410 let extra_count = self.count_extra_fields();
6411 if extra_count != 0 {
6412 write!(f, ", .. ({} fields)", extra_count)?;
6413 }
6414 write!(f, " }}")
6415 }
6416}
6417impl ::core::default::Default for CompactBlock {
6418 fn default() -> Self {
6419 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
6420 CompactBlock::new_unchecked(v)
6421 }
6422}
6423impl CompactBlock {
6424 const DEFAULT_VALUE: [u8; 248] = [
6425 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,
6426 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6427 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6428 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6429 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6430 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6431 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6432 0, 0, 0, 0, 0, 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,
6433 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6434 ];
6435 pub const FIELD_COUNT: usize = 5;
6436 pub fn total_size(&self) -> usize {
6437 molecule::unpack_number(self.as_slice()) as usize
6438 }
6439 pub fn field_count(&self) -> usize {
6440 if self.total_size() == molecule::NUMBER_SIZE {
6441 0
6442 } else {
6443 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6444 }
6445 }
6446 pub fn count_extra_fields(&self) -> usize {
6447 self.field_count() - Self::FIELD_COUNT
6448 }
6449 pub fn has_extra_fields(&self) -> bool {
6450 Self::FIELD_COUNT != self.field_count()
6451 }
6452 pub fn header(&self) -> Header {
6453 let slice = self.as_slice();
6454 let start = molecule::unpack_number(&slice[4..]) as usize;
6455 let end = molecule::unpack_number(&slice[8..]) as usize;
6456 Header::new_unchecked(self.0.slice(start..end))
6457 }
6458 pub fn short_ids(&self) -> ProposalShortIdVec {
6459 let slice = self.as_slice();
6460 let start = molecule::unpack_number(&slice[8..]) as usize;
6461 let end = molecule::unpack_number(&slice[12..]) as usize;
6462 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
6463 }
6464 pub fn prefilled_transactions(&self) -> IndexTransactionVec {
6465 let slice = self.as_slice();
6466 let start = molecule::unpack_number(&slice[12..]) as usize;
6467 let end = molecule::unpack_number(&slice[16..]) as usize;
6468 IndexTransactionVec::new_unchecked(self.0.slice(start..end))
6469 }
6470 pub fn uncles(&self) -> Byte32Vec {
6471 let slice = self.as_slice();
6472 let start = molecule::unpack_number(&slice[16..]) as usize;
6473 let end = molecule::unpack_number(&slice[20..]) as usize;
6474 Byte32Vec::new_unchecked(self.0.slice(start..end))
6475 }
6476 pub fn proposals(&self) -> ProposalShortIdVec {
6477 let slice = self.as_slice();
6478 let start = molecule::unpack_number(&slice[20..]) as usize;
6479 if self.has_extra_fields() {
6480 let end = molecule::unpack_number(&slice[24..]) as usize;
6481 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
6482 } else {
6483 ProposalShortIdVec::new_unchecked(self.0.slice(start..))
6484 }
6485 }
6486 pub fn as_reader<'r>(&'r self) -> CompactBlockReader<'r> {
6487 CompactBlockReader::new_unchecked(self.as_slice())
6488 }
6489}
6490impl molecule::prelude::Entity for CompactBlock {
6491 type Builder = CompactBlockBuilder;
6492 const NAME: &'static str = "CompactBlock";
6493 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6494 CompactBlock(data)
6495 }
6496 fn as_bytes(&self) -> molecule::bytes::Bytes {
6497 self.0.clone()
6498 }
6499 fn as_slice(&self) -> &[u8] {
6500 &self.0[..]
6501 }
6502 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6503 CompactBlockReader::from_slice(slice).map(|reader| reader.to_entity())
6504 }
6505 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6506 CompactBlockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6507 }
6508 fn new_builder() -> Self::Builder {
6509 ::core::default::Default::default()
6510 }
6511 fn as_builder(self) -> Self::Builder {
6512 Self::new_builder()
6513 .header(self.header())
6514 .short_ids(self.short_ids())
6515 .prefilled_transactions(self.prefilled_transactions())
6516 .uncles(self.uncles())
6517 .proposals(self.proposals())
6518 }
6519}
6520#[derive(Clone, Copy)]
6521pub struct CompactBlockReader<'r>(&'r [u8]);
6522impl<'r> ::core::fmt::LowerHex for CompactBlockReader<'r> {
6523 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6524 use molecule::hex_string;
6525 if f.alternate() {
6526 write!(f, "0x")?;
6527 }
6528 write!(f, "{}", hex_string(self.as_slice()))
6529 }
6530}
6531impl<'r> ::core::fmt::Debug for CompactBlockReader<'r> {
6532 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6533 write!(f, "{}({:#x})", Self::NAME, self)
6534 }
6535}
6536impl<'r> ::core::fmt::Display for CompactBlockReader<'r> {
6537 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6538 write!(f, "{} {{ ", Self::NAME)?;
6539 write!(f, "{}: {}", "header", self.header())?;
6540 write!(f, ", {}: {}", "short_ids", self.short_ids())?;
6541 write!(
6542 f,
6543 ", {}: {}",
6544 "prefilled_transactions",
6545 self.prefilled_transactions()
6546 )?;
6547 write!(f, ", {}: {}", "uncles", self.uncles())?;
6548 write!(f, ", {}: {}", "proposals", self.proposals())?;
6549 let extra_count = self.count_extra_fields();
6550 if extra_count != 0 {
6551 write!(f, ", .. ({} fields)", extra_count)?;
6552 }
6553 write!(f, " }}")
6554 }
6555}
6556impl<'r> CompactBlockReader<'r> {
6557 pub const FIELD_COUNT: usize = 5;
6558 pub fn total_size(&self) -> usize {
6559 molecule::unpack_number(self.as_slice()) as usize
6560 }
6561 pub fn field_count(&self) -> usize {
6562 if self.total_size() == molecule::NUMBER_SIZE {
6563 0
6564 } else {
6565 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6566 }
6567 }
6568 pub fn count_extra_fields(&self) -> usize {
6569 self.field_count() - Self::FIELD_COUNT
6570 }
6571 pub fn has_extra_fields(&self) -> bool {
6572 Self::FIELD_COUNT != self.field_count()
6573 }
6574 pub fn header(&self) -> HeaderReader<'r> {
6575 let slice = self.as_slice();
6576 let start = molecule::unpack_number(&slice[4..]) as usize;
6577 let end = molecule::unpack_number(&slice[8..]) as usize;
6578 HeaderReader::new_unchecked(&self.as_slice()[start..end])
6579 }
6580 pub fn short_ids(&self) -> ProposalShortIdVecReader<'r> {
6581 let slice = self.as_slice();
6582 let start = molecule::unpack_number(&slice[8..]) as usize;
6583 let end = molecule::unpack_number(&slice[12..]) as usize;
6584 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
6585 }
6586 pub fn prefilled_transactions(&self) -> IndexTransactionVecReader<'r> {
6587 let slice = self.as_slice();
6588 let start = molecule::unpack_number(&slice[12..]) as usize;
6589 let end = molecule::unpack_number(&slice[16..]) as usize;
6590 IndexTransactionVecReader::new_unchecked(&self.as_slice()[start..end])
6591 }
6592 pub fn uncles(&self) -> Byte32VecReader<'r> {
6593 let slice = self.as_slice();
6594 let start = molecule::unpack_number(&slice[16..]) as usize;
6595 let end = molecule::unpack_number(&slice[20..]) as usize;
6596 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
6597 }
6598 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
6599 let slice = self.as_slice();
6600 let start = molecule::unpack_number(&slice[20..]) as usize;
6601 if self.has_extra_fields() {
6602 let end = molecule::unpack_number(&slice[24..]) as usize;
6603 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
6604 } else {
6605 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..])
6606 }
6607 }
6608}
6609impl<'r> molecule::prelude::Reader<'r> for CompactBlockReader<'r> {
6610 type Entity = CompactBlock;
6611 const NAME: &'static str = "CompactBlockReader";
6612 fn to_entity(&self) -> Self::Entity {
6613 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6614 }
6615 fn new_unchecked(slice: &'r [u8]) -> Self {
6616 CompactBlockReader(slice)
6617 }
6618 fn as_slice(&self) -> &'r [u8] {
6619 self.0
6620 }
6621 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6622 use molecule::verification_error as ve;
6623 let slice_len = slice.len();
6624 if slice_len < molecule::NUMBER_SIZE {
6625 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6626 }
6627 let total_size = molecule::unpack_number(slice) as usize;
6628 if slice_len != total_size {
6629 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6630 }
6631 if slice_len < molecule::NUMBER_SIZE * 2 {
6632 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
6633 }
6634 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6635 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6636 return ve!(Self, OffsetsNotMatch);
6637 }
6638 if slice_len < offset_first {
6639 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6640 }
6641 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
6642 if field_count < Self::FIELD_COUNT {
6643 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6644 } else if !compatible && field_count > Self::FIELD_COUNT {
6645 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6646 };
6647 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6648 .chunks_exact(molecule::NUMBER_SIZE)
6649 .map(|x| molecule::unpack_number(x) as usize)
6650 .collect();
6651 offsets.push(total_size);
6652 if offsets.windows(2).any(|i| i[0] > i[1]) {
6653 return ve!(Self, OffsetsNotMatch);
6654 }
6655 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
6656 ProposalShortIdVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
6657 IndexTransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
6658 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
6659 ProposalShortIdVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
6660 Ok(())
6661 }
6662}
6663#[derive(Debug, Default)]
6664pub struct CompactBlockBuilder {
6665 pub(crate) header: Header,
6666 pub(crate) short_ids: ProposalShortIdVec,
6667 pub(crate) prefilled_transactions: IndexTransactionVec,
6668 pub(crate) uncles: Byte32Vec,
6669 pub(crate) proposals: ProposalShortIdVec,
6670}
6671impl CompactBlockBuilder {
6672 pub const FIELD_COUNT: usize = 5;
6673 pub fn header(mut self, v: Header) -> Self {
6674 self.header = v;
6675 self
6676 }
6677 pub fn short_ids(mut self, v: ProposalShortIdVec) -> Self {
6678 self.short_ids = v;
6679 self
6680 }
6681 pub fn prefilled_transactions(mut self, v: IndexTransactionVec) -> Self {
6682 self.prefilled_transactions = v;
6683 self
6684 }
6685 pub fn uncles(mut self, v: Byte32Vec) -> Self {
6686 self.uncles = v;
6687 self
6688 }
6689 pub fn proposals(mut self, v: ProposalShortIdVec) -> Self {
6690 self.proposals = v;
6691 self
6692 }
6693}
6694impl molecule::prelude::Builder for CompactBlockBuilder {
6695 type Entity = CompactBlock;
6696 const NAME: &'static str = "CompactBlockBuilder";
6697 fn expected_length(&self) -> usize {
6698 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6699 + self.header.as_slice().len()
6700 + self.short_ids.as_slice().len()
6701 + self.prefilled_transactions.as_slice().len()
6702 + self.uncles.as_slice().len()
6703 + self.proposals.as_slice().len()
6704 }
6705 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6706 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6707 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6708 offsets.push(total_size);
6709 total_size += self.header.as_slice().len();
6710 offsets.push(total_size);
6711 total_size += self.short_ids.as_slice().len();
6712 offsets.push(total_size);
6713 total_size += self.prefilled_transactions.as_slice().len();
6714 offsets.push(total_size);
6715 total_size += self.uncles.as_slice().len();
6716 offsets.push(total_size);
6717 total_size += self.proposals.as_slice().len();
6718 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6719 for offset in offsets.into_iter() {
6720 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6721 }
6722 writer.write_all(self.header.as_slice())?;
6723 writer.write_all(self.short_ids.as_slice())?;
6724 writer.write_all(self.prefilled_transactions.as_slice())?;
6725 writer.write_all(self.uncles.as_slice())?;
6726 writer.write_all(self.proposals.as_slice())?;
6727 Ok(())
6728 }
6729 fn build(&self) -> Self::Entity {
6730 let mut inner = Vec::with_capacity(self.expected_length());
6731 self.write(&mut inner)
6732 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6733 CompactBlock::new_unchecked(inner.into())
6734 }
6735}
6736#[derive(Clone)]
6737pub struct CompactBlockV1(molecule::bytes::Bytes);
6738impl ::core::fmt::LowerHex for CompactBlockV1 {
6739 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6740 use molecule::hex_string;
6741 if f.alternate() {
6742 write!(f, "0x")?;
6743 }
6744 write!(f, "{}", hex_string(self.as_slice()))
6745 }
6746}
6747impl ::core::fmt::Debug for CompactBlockV1 {
6748 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6749 write!(f, "{}({:#x})", Self::NAME, self)
6750 }
6751}
6752impl ::core::fmt::Display for CompactBlockV1 {
6753 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6754 write!(f, "{} {{ ", Self::NAME)?;
6755 write!(f, "{}: {}", "header", self.header())?;
6756 write!(f, ", {}: {}", "short_ids", self.short_ids())?;
6757 write!(
6758 f,
6759 ", {}: {}",
6760 "prefilled_transactions",
6761 self.prefilled_transactions()
6762 )?;
6763 write!(f, ", {}: {}", "uncles", self.uncles())?;
6764 write!(f, ", {}: {}", "proposals", self.proposals())?;
6765 write!(f, ", {}: {}", "extension", self.extension())?;
6766 let extra_count = self.count_extra_fields();
6767 if extra_count != 0 {
6768 write!(f, ", .. ({} fields)", extra_count)?;
6769 }
6770 write!(f, " }}")
6771 }
6772}
6773impl ::core::default::Default for CompactBlockV1 {
6774 fn default() -> Self {
6775 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
6776 CompactBlockV1::new_unchecked(v)
6777 }
6778}
6779impl CompactBlockV1 {
6780 const DEFAULT_VALUE: [u8; 256] = [
6781 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,
6782 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6783 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6784 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6785 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6786 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6787 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6788 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6789 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6790 ];
6791 pub const FIELD_COUNT: usize = 6;
6792 pub fn total_size(&self) -> usize {
6793 molecule::unpack_number(self.as_slice()) as usize
6794 }
6795 pub fn field_count(&self) -> usize {
6796 if self.total_size() == molecule::NUMBER_SIZE {
6797 0
6798 } else {
6799 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6800 }
6801 }
6802 pub fn count_extra_fields(&self) -> usize {
6803 self.field_count() - Self::FIELD_COUNT
6804 }
6805 pub fn has_extra_fields(&self) -> bool {
6806 Self::FIELD_COUNT != self.field_count()
6807 }
6808 pub fn header(&self) -> Header {
6809 let slice = self.as_slice();
6810 let start = molecule::unpack_number(&slice[4..]) as usize;
6811 let end = molecule::unpack_number(&slice[8..]) as usize;
6812 Header::new_unchecked(self.0.slice(start..end))
6813 }
6814 pub fn short_ids(&self) -> ProposalShortIdVec {
6815 let slice = self.as_slice();
6816 let start = molecule::unpack_number(&slice[8..]) as usize;
6817 let end = molecule::unpack_number(&slice[12..]) as usize;
6818 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
6819 }
6820 pub fn prefilled_transactions(&self) -> IndexTransactionVec {
6821 let slice = self.as_slice();
6822 let start = molecule::unpack_number(&slice[12..]) as usize;
6823 let end = molecule::unpack_number(&slice[16..]) as usize;
6824 IndexTransactionVec::new_unchecked(self.0.slice(start..end))
6825 }
6826 pub fn uncles(&self) -> Byte32Vec {
6827 let slice = self.as_slice();
6828 let start = molecule::unpack_number(&slice[16..]) as usize;
6829 let end = molecule::unpack_number(&slice[20..]) as usize;
6830 Byte32Vec::new_unchecked(self.0.slice(start..end))
6831 }
6832 pub fn proposals(&self) -> ProposalShortIdVec {
6833 let slice = self.as_slice();
6834 let start = molecule::unpack_number(&slice[20..]) as usize;
6835 let end = molecule::unpack_number(&slice[24..]) as usize;
6836 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
6837 }
6838 pub fn extension(&self) -> Bytes {
6839 let slice = self.as_slice();
6840 let start = molecule::unpack_number(&slice[24..]) as usize;
6841 if self.has_extra_fields() {
6842 let end = molecule::unpack_number(&slice[28..]) as usize;
6843 Bytes::new_unchecked(self.0.slice(start..end))
6844 } else {
6845 Bytes::new_unchecked(self.0.slice(start..))
6846 }
6847 }
6848 pub fn as_reader<'r>(&'r self) -> CompactBlockV1Reader<'r> {
6849 CompactBlockV1Reader::new_unchecked(self.as_slice())
6850 }
6851}
6852impl molecule::prelude::Entity for CompactBlockV1 {
6853 type Builder = CompactBlockV1Builder;
6854 const NAME: &'static str = "CompactBlockV1";
6855 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6856 CompactBlockV1(data)
6857 }
6858 fn as_bytes(&self) -> molecule::bytes::Bytes {
6859 self.0.clone()
6860 }
6861 fn as_slice(&self) -> &[u8] {
6862 &self.0[..]
6863 }
6864 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6865 CompactBlockV1Reader::from_slice(slice).map(|reader| reader.to_entity())
6866 }
6867 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6868 CompactBlockV1Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6869 }
6870 fn new_builder() -> Self::Builder {
6871 ::core::default::Default::default()
6872 }
6873 fn as_builder(self) -> Self::Builder {
6874 Self::new_builder()
6875 .header(self.header())
6876 .short_ids(self.short_ids())
6877 .prefilled_transactions(self.prefilled_transactions())
6878 .uncles(self.uncles())
6879 .proposals(self.proposals())
6880 .extension(self.extension())
6881 }
6882}
6883#[derive(Clone, Copy)]
6884pub struct CompactBlockV1Reader<'r>(&'r [u8]);
6885impl<'r> ::core::fmt::LowerHex for CompactBlockV1Reader<'r> {
6886 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6887 use molecule::hex_string;
6888 if f.alternate() {
6889 write!(f, "0x")?;
6890 }
6891 write!(f, "{}", hex_string(self.as_slice()))
6892 }
6893}
6894impl<'r> ::core::fmt::Debug for CompactBlockV1Reader<'r> {
6895 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6896 write!(f, "{}({:#x})", Self::NAME, self)
6897 }
6898}
6899impl<'r> ::core::fmt::Display for CompactBlockV1Reader<'r> {
6900 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6901 write!(f, "{} {{ ", Self::NAME)?;
6902 write!(f, "{}: {}", "header", self.header())?;
6903 write!(f, ", {}: {}", "short_ids", self.short_ids())?;
6904 write!(
6905 f,
6906 ", {}: {}",
6907 "prefilled_transactions",
6908 self.prefilled_transactions()
6909 )?;
6910 write!(f, ", {}: {}", "uncles", self.uncles())?;
6911 write!(f, ", {}: {}", "proposals", self.proposals())?;
6912 write!(f, ", {}: {}", "extension", self.extension())?;
6913 let extra_count = self.count_extra_fields();
6914 if extra_count != 0 {
6915 write!(f, ", .. ({} fields)", extra_count)?;
6916 }
6917 write!(f, " }}")
6918 }
6919}
6920impl<'r> CompactBlockV1Reader<'r> {
6921 pub const FIELD_COUNT: usize = 6;
6922 pub fn total_size(&self) -> usize {
6923 molecule::unpack_number(self.as_slice()) as usize
6924 }
6925 pub fn field_count(&self) -> usize {
6926 if self.total_size() == molecule::NUMBER_SIZE {
6927 0
6928 } else {
6929 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6930 }
6931 }
6932 pub fn count_extra_fields(&self) -> usize {
6933 self.field_count() - Self::FIELD_COUNT
6934 }
6935 pub fn has_extra_fields(&self) -> bool {
6936 Self::FIELD_COUNT != self.field_count()
6937 }
6938 pub fn header(&self) -> HeaderReader<'r> {
6939 let slice = self.as_slice();
6940 let start = molecule::unpack_number(&slice[4..]) as usize;
6941 let end = molecule::unpack_number(&slice[8..]) as usize;
6942 HeaderReader::new_unchecked(&self.as_slice()[start..end])
6943 }
6944 pub fn short_ids(&self) -> ProposalShortIdVecReader<'r> {
6945 let slice = self.as_slice();
6946 let start = molecule::unpack_number(&slice[8..]) as usize;
6947 let end = molecule::unpack_number(&slice[12..]) as usize;
6948 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
6949 }
6950 pub fn prefilled_transactions(&self) -> IndexTransactionVecReader<'r> {
6951 let slice = self.as_slice();
6952 let start = molecule::unpack_number(&slice[12..]) as usize;
6953 let end = molecule::unpack_number(&slice[16..]) as usize;
6954 IndexTransactionVecReader::new_unchecked(&self.as_slice()[start..end])
6955 }
6956 pub fn uncles(&self) -> Byte32VecReader<'r> {
6957 let slice = self.as_slice();
6958 let start = molecule::unpack_number(&slice[16..]) as usize;
6959 let end = molecule::unpack_number(&slice[20..]) as usize;
6960 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
6961 }
6962 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
6963 let slice = self.as_slice();
6964 let start = molecule::unpack_number(&slice[20..]) as usize;
6965 let end = molecule::unpack_number(&slice[24..]) as usize;
6966 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
6967 }
6968 pub fn extension(&self) -> BytesReader<'r> {
6969 let slice = self.as_slice();
6970 let start = molecule::unpack_number(&slice[24..]) as usize;
6971 if self.has_extra_fields() {
6972 let end = molecule::unpack_number(&slice[28..]) as usize;
6973 BytesReader::new_unchecked(&self.as_slice()[start..end])
6974 } else {
6975 BytesReader::new_unchecked(&self.as_slice()[start..])
6976 }
6977 }
6978}
6979impl<'r> molecule::prelude::Reader<'r> for CompactBlockV1Reader<'r> {
6980 type Entity = CompactBlockV1;
6981 const NAME: &'static str = "CompactBlockV1Reader";
6982 fn to_entity(&self) -> Self::Entity {
6983 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6984 }
6985 fn new_unchecked(slice: &'r [u8]) -> Self {
6986 CompactBlockV1Reader(slice)
6987 }
6988 fn as_slice(&self) -> &'r [u8] {
6989 self.0
6990 }
6991 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6992 use molecule::verification_error as ve;
6993 let slice_len = slice.len();
6994 if slice_len < molecule::NUMBER_SIZE {
6995 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6996 }
6997 let total_size = molecule::unpack_number(slice) as usize;
6998 if slice_len != total_size {
6999 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7000 }
7001 if slice_len < molecule::NUMBER_SIZE * 2 {
7002 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
7003 }
7004 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7005 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7006 return ve!(Self, OffsetsNotMatch);
7007 }
7008 if slice_len < offset_first {
7009 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7010 }
7011 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
7012 if field_count < Self::FIELD_COUNT {
7013 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7014 } else if !compatible && field_count > Self::FIELD_COUNT {
7015 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7016 };
7017 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7018 .chunks_exact(molecule::NUMBER_SIZE)
7019 .map(|x| molecule::unpack_number(x) as usize)
7020 .collect();
7021 offsets.push(total_size);
7022 if offsets.windows(2).any(|i| i[0] > i[1]) {
7023 return ve!(Self, OffsetsNotMatch);
7024 }
7025 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
7026 ProposalShortIdVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
7027 IndexTransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
7028 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
7029 ProposalShortIdVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
7030 BytesReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
7031 Ok(())
7032 }
7033}
7034#[derive(Debug, Default)]
7035pub struct CompactBlockV1Builder {
7036 pub(crate) header: Header,
7037 pub(crate) short_ids: ProposalShortIdVec,
7038 pub(crate) prefilled_transactions: IndexTransactionVec,
7039 pub(crate) uncles: Byte32Vec,
7040 pub(crate) proposals: ProposalShortIdVec,
7041 pub(crate) extension: Bytes,
7042}
7043impl CompactBlockV1Builder {
7044 pub const FIELD_COUNT: usize = 6;
7045 pub fn header(mut self, v: Header) -> Self {
7046 self.header = v;
7047 self
7048 }
7049 pub fn short_ids(mut self, v: ProposalShortIdVec) -> Self {
7050 self.short_ids = v;
7051 self
7052 }
7053 pub fn prefilled_transactions(mut self, v: IndexTransactionVec) -> Self {
7054 self.prefilled_transactions = v;
7055 self
7056 }
7057 pub fn uncles(mut self, v: Byte32Vec) -> Self {
7058 self.uncles = v;
7059 self
7060 }
7061 pub fn proposals(mut self, v: ProposalShortIdVec) -> Self {
7062 self.proposals = v;
7063 self
7064 }
7065 pub fn extension(mut self, v: Bytes) -> Self {
7066 self.extension = v;
7067 self
7068 }
7069}
7070impl molecule::prelude::Builder for CompactBlockV1Builder {
7071 type Entity = CompactBlockV1;
7072 const NAME: &'static str = "CompactBlockV1Builder";
7073 fn expected_length(&self) -> usize {
7074 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
7075 + self.header.as_slice().len()
7076 + self.short_ids.as_slice().len()
7077 + self.prefilled_transactions.as_slice().len()
7078 + self.uncles.as_slice().len()
7079 + self.proposals.as_slice().len()
7080 + self.extension.as_slice().len()
7081 }
7082 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7083 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
7084 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
7085 offsets.push(total_size);
7086 total_size += self.header.as_slice().len();
7087 offsets.push(total_size);
7088 total_size += self.short_ids.as_slice().len();
7089 offsets.push(total_size);
7090 total_size += self.prefilled_transactions.as_slice().len();
7091 offsets.push(total_size);
7092 total_size += self.uncles.as_slice().len();
7093 offsets.push(total_size);
7094 total_size += self.proposals.as_slice().len();
7095 offsets.push(total_size);
7096 total_size += self.extension.as_slice().len();
7097 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
7098 for offset in offsets.into_iter() {
7099 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
7100 }
7101 writer.write_all(self.header.as_slice())?;
7102 writer.write_all(self.short_ids.as_slice())?;
7103 writer.write_all(self.prefilled_transactions.as_slice())?;
7104 writer.write_all(self.uncles.as_slice())?;
7105 writer.write_all(self.proposals.as_slice())?;
7106 writer.write_all(self.extension.as_slice())?;
7107 Ok(())
7108 }
7109 fn build(&self) -> Self::Entity {
7110 let mut inner = Vec::with_capacity(self.expected_length());
7111 self.write(&mut inner)
7112 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7113 CompactBlockV1::new_unchecked(inner.into())
7114 }
7115}
7116#[derive(Clone)]
7117pub struct RelayTransaction(molecule::bytes::Bytes);
7118impl ::core::fmt::LowerHex for RelayTransaction {
7119 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7120 use molecule::hex_string;
7121 if f.alternate() {
7122 write!(f, "0x")?;
7123 }
7124 write!(f, "{}", hex_string(self.as_slice()))
7125 }
7126}
7127impl ::core::fmt::Debug for RelayTransaction {
7128 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7129 write!(f, "{}({:#x})", Self::NAME, self)
7130 }
7131}
7132impl ::core::fmt::Display for RelayTransaction {
7133 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7134 write!(f, "{} {{ ", Self::NAME)?;
7135 write!(f, "{}: {}", "cycles", self.cycles())?;
7136 write!(f, ", {}: {}", "transaction", self.transaction())?;
7137 let extra_count = self.count_extra_fields();
7138 if extra_count != 0 {
7139 write!(f, ", .. ({} fields)", extra_count)?;
7140 }
7141 write!(f, " }}")
7142 }
7143}
7144impl ::core::default::Default for RelayTransaction {
7145 fn default() -> Self {
7146 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7147 RelayTransaction::new_unchecked(v)
7148 }
7149}
7150impl RelayTransaction {
7151 const DEFAULT_VALUE: [u8; 88] = [
7152 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,
7153 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,
7154 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,
7155 0, 0,
7156 ];
7157 pub const FIELD_COUNT: usize = 2;
7158 pub fn total_size(&self) -> usize {
7159 molecule::unpack_number(self.as_slice()) as usize
7160 }
7161 pub fn field_count(&self) -> usize {
7162 if self.total_size() == molecule::NUMBER_SIZE {
7163 0
7164 } else {
7165 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7166 }
7167 }
7168 pub fn count_extra_fields(&self) -> usize {
7169 self.field_count() - Self::FIELD_COUNT
7170 }
7171 pub fn has_extra_fields(&self) -> bool {
7172 Self::FIELD_COUNT != self.field_count()
7173 }
7174 pub fn cycles(&self) -> Uint64 {
7175 let slice = self.as_slice();
7176 let start = molecule::unpack_number(&slice[4..]) as usize;
7177 let end = molecule::unpack_number(&slice[8..]) as usize;
7178 Uint64::new_unchecked(self.0.slice(start..end))
7179 }
7180 pub fn transaction(&self) -> Transaction {
7181 let slice = self.as_slice();
7182 let start = molecule::unpack_number(&slice[8..]) as usize;
7183 if self.has_extra_fields() {
7184 let end = molecule::unpack_number(&slice[12..]) as usize;
7185 Transaction::new_unchecked(self.0.slice(start..end))
7186 } else {
7187 Transaction::new_unchecked(self.0.slice(start..))
7188 }
7189 }
7190 pub fn as_reader<'r>(&'r self) -> RelayTransactionReader<'r> {
7191 RelayTransactionReader::new_unchecked(self.as_slice())
7192 }
7193}
7194impl molecule::prelude::Entity for RelayTransaction {
7195 type Builder = RelayTransactionBuilder;
7196 const NAME: &'static str = "RelayTransaction";
7197 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7198 RelayTransaction(data)
7199 }
7200 fn as_bytes(&self) -> molecule::bytes::Bytes {
7201 self.0.clone()
7202 }
7203 fn as_slice(&self) -> &[u8] {
7204 &self.0[..]
7205 }
7206 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7207 RelayTransactionReader::from_slice(slice).map(|reader| reader.to_entity())
7208 }
7209 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7210 RelayTransactionReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7211 }
7212 fn new_builder() -> Self::Builder {
7213 ::core::default::Default::default()
7214 }
7215 fn as_builder(self) -> Self::Builder {
7216 Self::new_builder()
7217 .cycles(self.cycles())
7218 .transaction(self.transaction())
7219 }
7220}
7221#[derive(Clone, Copy)]
7222pub struct RelayTransactionReader<'r>(&'r [u8]);
7223impl<'r> ::core::fmt::LowerHex for RelayTransactionReader<'r> {
7224 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7225 use molecule::hex_string;
7226 if f.alternate() {
7227 write!(f, "0x")?;
7228 }
7229 write!(f, "{}", hex_string(self.as_slice()))
7230 }
7231}
7232impl<'r> ::core::fmt::Debug for RelayTransactionReader<'r> {
7233 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7234 write!(f, "{}({:#x})", Self::NAME, self)
7235 }
7236}
7237impl<'r> ::core::fmt::Display for RelayTransactionReader<'r> {
7238 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7239 write!(f, "{} {{ ", Self::NAME)?;
7240 write!(f, "{}: {}", "cycles", self.cycles())?;
7241 write!(f, ", {}: {}", "transaction", self.transaction())?;
7242 let extra_count = self.count_extra_fields();
7243 if extra_count != 0 {
7244 write!(f, ", .. ({} fields)", extra_count)?;
7245 }
7246 write!(f, " }}")
7247 }
7248}
7249impl<'r> RelayTransactionReader<'r> {
7250 pub const FIELD_COUNT: usize = 2;
7251 pub fn total_size(&self) -> usize {
7252 molecule::unpack_number(self.as_slice()) as usize
7253 }
7254 pub fn field_count(&self) -> usize {
7255 if self.total_size() == molecule::NUMBER_SIZE {
7256 0
7257 } else {
7258 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7259 }
7260 }
7261 pub fn count_extra_fields(&self) -> usize {
7262 self.field_count() - Self::FIELD_COUNT
7263 }
7264 pub fn has_extra_fields(&self) -> bool {
7265 Self::FIELD_COUNT != self.field_count()
7266 }
7267 pub fn cycles(&self) -> Uint64Reader<'r> {
7268 let slice = self.as_slice();
7269 let start = molecule::unpack_number(&slice[4..]) as usize;
7270 let end = molecule::unpack_number(&slice[8..]) as usize;
7271 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
7272 }
7273 pub fn transaction(&self) -> TransactionReader<'r> {
7274 let slice = self.as_slice();
7275 let start = molecule::unpack_number(&slice[8..]) as usize;
7276 if self.has_extra_fields() {
7277 let end = molecule::unpack_number(&slice[12..]) as usize;
7278 TransactionReader::new_unchecked(&self.as_slice()[start..end])
7279 } else {
7280 TransactionReader::new_unchecked(&self.as_slice()[start..])
7281 }
7282 }
7283}
7284impl<'r> molecule::prelude::Reader<'r> for RelayTransactionReader<'r> {
7285 type Entity = RelayTransaction;
7286 const NAME: &'static str = "RelayTransactionReader";
7287 fn to_entity(&self) -> Self::Entity {
7288 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7289 }
7290 fn new_unchecked(slice: &'r [u8]) -> Self {
7291 RelayTransactionReader(slice)
7292 }
7293 fn as_slice(&self) -> &'r [u8] {
7294 self.0
7295 }
7296 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
7297 use molecule::verification_error as ve;
7298 let slice_len = slice.len();
7299 if slice_len < molecule::NUMBER_SIZE {
7300 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
7301 }
7302 let total_size = molecule::unpack_number(slice) as usize;
7303 if slice_len != total_size {
7304 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7305 }
7306 if slice_len < molecule::NUMBER_SIZE * 2 {
7307 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
7308 }
7309 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7310 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7311 return ve!(Self, OffsetsNotMatch);
7312 }
7313 if slice_len < offset_first {
7314 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7315 }
7316 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
7317 if field_count < Self::FIELD_COUNT {
7318 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7319 } else if !compatible && field_count > Self::FIELD_COUNT {
7320 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7321 };
7322 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7323 .chunks_exact(molecule::NUMBER_SIZE)
7324 .map(|x| molecule::unpack_number(x) as usize)
7325 .collect();
7326 offsets.push(total_size);
7327 if offsets.windows(2).any(|i| i[0] > i[1]) {
7328 return ve!(Self, OffsetsNotMatch);
7329 }
7330 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
7331 TransactionReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
7332 Ok(())
7333 }
7334}
7335#[derive(Debug, Default)]
7336pub struct RelayTransactionBuilder {
7337 pub(crate) cycles: Uint64,
7338 pub(crate) transaction: Transaction,
7339}
7340impl RelayTransactionBuilder {
7341 pub const FIELD_COUNT: usize = 2;
7342 pub fn cycles(mut self, v: Uint64) -> Self {
7343 self.cycles = v;
7344 self
7345 }
7346 pub fn transaction(mut self, v: Transaction) -> Self {
7347 self.transaction = v;
7348 self
7349 }
7350}
7351impl molecule::prelude::Builder for RelayTransactionBuilder {
7352 type Entity = RelayTransaction;
7353 const NAME: &'static str = "RelayTransactionBuilder";
7354 fn expected_length(&self) -> usize {
7355 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
7356 + self.cycles.as_slice().len()
7357 + self.transaction.as_slice().len()
7358 }
7359 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7360 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
7361 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
7362 offsets.push(total_size);
7363 total_size += self.cycles.as_slice().len();
7364 offsets.push(total_size);
7365 total_size += self.transaction.as_slice().len();
7366 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
7367 for offset in offsets.into_iter() {
7368 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
7369 }
7370 writer.write_all(self.cycles.as_slice())?;
7371 writer.write_all(self.transaction.as_slice())?;
7372 Ok(())
7373 }
7374 fn build(&self) -> Self::Entity {
7375 let mut inner = Vec::with_capacity(self.expected_length());
7376 self.write(&mut inner)
7377 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7378 RelayTransaction::new_unchecked(inner.into())
7379 }
7380}
7381#[derive(Clone)]
7382pub struct RelayTransactionVec(molecule::bytes::Bytes);
7383impl ::core::fmt::LowerHex for RelayTransactionVec {
7384 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7385 use molecule::hex_string;
7386 if f.alternate() {
7387 write!(f, "0x")?;
7388 }
7389 write!(f, "{}", hex_string(self.as_slice()))
7390 }
7391}
7392impl ::core::fmt::Debug for RelayTransactionVec {
7393 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7394 write!(f, "{}({:#x})", Self::NAME, self)
7395 }
7396}
7397impl ::core::fmt::Display for RelayTransactionVec {
7398 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7399 write!(f, "{} [", Self::NAME)?;
7400 for i in 0..self.len() {
7401 if i == 0 {
7402 write!(f, "{}", self.get_unchecked(i))?;
7403 } else {
7404 write!(f, ", {}", self.get_unchecked(i))?;
7405 }
7406 }
7407 write!(f, "]")
7408 }
7409}
7410impl ::core::default::Default for RelayTransactionVec {
7411 fn default() -> Self {
7412 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7413 RelayTransactionVec::new_unchecked(v)
7414 }
7415}
7416impl RelayTransactionVec {
7417 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
7418 pub fn total_size(&self) -> usize {
7419 molecule::unpack_number(self.as_slice()) as usize
7420 }
7421 pub fn item_count(&self) -> usize {
7422 if self.total_size() == molecule::NUMBER_SIZE {
7423 0
7424 } else {
7425 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7426 }
7427 }
7428 pub fn len(&self) -> usize {
7429 self.item_count()
7430 }
7431 pub fn is_empty(&self) -> bool {
7432 self.len() == 0
7433 }
7434 pub fn get(&self, idx: usize) -> Option<RelayTransaction> {
7435 if idx >= self.len() {
7436 None
7437 } else {
7438 Some(self.get_unchecked(idx))
7439 }
7440 }
7441 pub fn get_unchecked(&self, idx: usize) -> RelayTransaction {
7442 let slice = self.as_slice();
7443 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
7444 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
7445 if idx == self.len() - 1 {
7446 RelayTransaction::new_unchecked(self.0.slice(start..))
7447 } else {
7448 let end_idx = start_idx + molecule::NUMBER_SIZE;
7449 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
7450 RelayTransaction::new_unchecked(self.0.slice(start..end))
7451 }
7452 }
7453 pub fn as_reader<'r>(&'r self) -> RelayTransactionVecReader<'r> {
7454 RelayTransactionVecReader::new_unchecked(self.as_slice())
7455 }
7456}
7457impl molecule::prelude::Entity for RelayTransactionVec {
7458 type Builder = RelayTransactionVecBuilder;
7459 const NAME: &'static str = "RelayTransactionVec";
7460 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7461 RelayTransactionVec(data)
7462 }
7463 fn as_bytes(&self) -> molecule::bytes::Bytes {
7464 self.0.clone()
7465 }
7466 fn as_slice(&self) -> &[u8] {
7467 &self.0[..]
7468 }
7469 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7470 RelayTransactionVecReader::from_slice(slice).map(|reader| reader.to_entity())
7471 }
7472 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7473 RelayTransactionVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7474 }
7475 fn new_builder() -> Self::Builder {
7476 ::core::default::Default::default()
7477 }
7478 fn as_builder(self) -> Self::Builder {
7479 Self::new_builder().extend(self.into_iter())
7480 }
7481}
7482#[derive(Clone, Copy)]
7483pub struct RelayTransactionVecReader<'r>(&'r [u8]);
7484impl<'r> ::core::fmt::LowerHex for RelayTransactionVecReader<'r> {
7485 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7486 use molecule::hex_string;
7487 if f.alternate() {
7488 write!(f, "0x")?;
7489 }
7490 write!(f, "{}", hex_string(self.as_slice()))
7491 }
7492}
7493impl<'r> ::core::fmt::Debug for RelayTransactionVecReader<'r> {
7494 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7495 write!(f, "{}({:#x})", Self::NAME, self)
7496 }
7497}
7498impl<'r> ::core::fmt::Display for RelayTransactionVecReader<'r> {
7499 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7500 write!(f, "{} [", Self::NAME)?;
7501 for i in 0..self.len() {
7502 if i == 0 {
7503 write!(f, "{}", self.get_unchecked(i))?;
7504 } else {
7505 write!(f, ", {}", self.get_unchecked(i))?;
7506 }
7507 }
7508 write!(f, "]")
7509 }
7510}
7511impl<'r> RelayTransactionVecReader<'r> {
7512 pub fn total_size(&self) -> usize {
7513 molecule::unpack_number(self.as_slice()) as usize
7514 }
7515 pub fn item_count(&self) -> usize {
7516 if self.total_size() == molecule::NUMBER_SIZE {
7517 0
7518 } else {
7519 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7520 }
7521 }
7522 pub fn len(&self) -> usize {
7523 self.item_count()
7524 }
7525 pub fn is_empty(&self) -> bool {
7526 self.len() == 0
7527 }
7528 pub fn get(&self, idx: usize) -> Option<RelayTransactionReader<'r>> {
7529 if idx >= self.len() {
7530 None
7531 } else {
7532 Some(self.get_unchecked(idx))
7533 }
7534 }
7535 pub fn get_unchecked(&self, idx: usize) -> RelayTransactionReader<'r> {
7536 let slice = self.as_slice();
7537 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
7538 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
7539 if idx == self.len() - 1 {
7540 RelayTransactionReader::new_unchecked(&self.as_slice()[start..])
7541 } else {
7542 let end_idx = start_idx + molecule::NUMBER_SIZE;
7543 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
7544 RelayTransactionReader::new_unchecked(&self.as_slice()[start..end])
7545 }
7546 }
7547}
7548impl<'r> molecule::prelude::Reader<'r> for RelayTransactionVecReader<'r> {
7549 type Entity = RelayTransactionVec;
7550 const NAME: &'static str = "RelayTransactionVecReader";
7551 fn to_entity(&self) -> Self::Entity {
7552 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7553 }
7554 fn new_unchecked(slice: &'r [u8]) -> Self {
7555 RelayTransactionVecReader(slice)
7556 }
7557 fn as_slice(&self) -> &'r [u8] {
7558 self.0
7559 }
7560 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
7561 use molecule::verification_error as ve;
7562 let slice_len = slice.len();
7563 if slice_len < molecule::NUMBER_SIZE {
7564 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
7565 }
7566 let total_size = molecule::unpack_number(slice) as usize;
7567 if slice_len != total_size {
7568 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7569 }
7570 if slice_len == molecule::NUMBER_SIZE {
7571 return Ok(());
7572 }
7573 if slice_len < molecule::NUMBER_SIZE * 2 {
7574 return ve!(
7575 Self,
7576 TotalSizeNotMatch,
7577 molecule::NUMBER_SIZE * 2,
7578 slice_len
7579 );
7580 }
7581 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7582 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7583 return ve!(Self, OffsetsNotMatch);
7584 }
7585 if slice_len < offset_first {
7586 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7587 }
7588 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7589 .chunks_exact(molecule::NUMBER_SIZE)
7590 .map(|x| molecule::unpack_number(x) as usize)
7591 .collect();
7592 offsets.push(total_size);
7593 if offsets.windows(2).any(|i| i[0] > i[1]) {
7594 return ve!(Self, OffsetsNotMatch);
7595 }
7596 for pair in offsets.windows(2) {
7597 let start = pair[0];
7598 let end = pair[1];
7599 RelayTransactionReader::verify(&slice[start..end], compatible)?;
7600 }
7601 Ok(())
7602 }
7603}
7604#[derive(Debug, Default)]
7605pub struct RelayTransactionVecBuilder(pub(crate) Vec<RelayTransaction>);
7606impl RelayTransactionVecBuilder {
7607 pub fn set(mut self, v: Vec<RelayTransaction>) -> Self {
7608 self.0 = v;
7609 self
7610 }
7611 pub fn push(mut self, v: RelayTransaction) -> Self {
7612 self.0.push(v);
7613 self
7614 }
7615 pub fn extend<T: ::core::iter::IntoIterator<Item = RelayTransaction>>(
7616 mut self,
7617 iter: T,
7618 ) -> Self {
7619 for elem in iter {
7620 self.0.push(elem);
7621 }
7622 self
7623 }
7624 pub fn replace(&mut self, index: usize, v: RelayTransaction) -> Option<RelayTransaction> {
7625 self.0
7626 .get_mut(index)
7627 .map(|item| ::core::mem::replace(item, v))
7628 }
7629}
7630impl molecule::prelude::Builder for RelayTransactionVecBuilder {
7631 type Entity = RelayTransactionVec;
7632 const NAME: &'static str = "RelayTransactionVecBuilder";
7633 fn expected_length(&self) -> usize {
7634 molecule::NUMBER_SIZE * (self.0.len() + 1)
7635 + self
7636 .0
7637 .iter()
7638 .map(|inner| inner.as_slice().len())
7639 .sum::<usize>()
7640 }
7641 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7642 let item_count = self.0.len();
7643 if item_count == 0 {
7644 writer.write_all(&molecule::pack_number(
7645 molecule::NUMBER_SIZE as molecule::Number,
7646 ))?;
7647 } else {
7648 let (total_size, offsets) = self.0.iter().fold(
7649 (
7650 molecule::NUMBER_SIZE * (item_count + 1),
7651 Vec::with_capacity(item_count),
7652 ),
7653 |(start, mut offsets), inner| {
7654 offsets.push(start);
7655 (start + inner.as_slice().len(), offsets)
7656 },
7657 );
7658 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
7659 for offset in offsets.into_iter() {
7660 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
7661 }
7662 for inner in self.0.iter() {
7663 writer.write_all(inner.as_slice())?;
7664 }
7665 }
7666 Ok(())
7667 }
7668 fn build(&self) -> Self::Entity {
7669 let mut inner = Vec::with_capacity(self.expected_length());
7670 self.write(&mut inner)
7671 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7672 RelayTransactionVec::new_unchecked(inner.into())
7673 }
7674}
7675pub struct RelayTransactionVecIterator(RelayTransactionVec, usize, usize);
7676impl ::core::iter::Iterator for RelayTransactionVecIterator {
7677 type Item = RelayTransaction;
7678 fn next(&mut self) -> Option<Self::Item> {
7679 if self.1 >= self.2 {
7680 None
7681 } else {
7682 let ret = self.0.get_unchecked(self.1);
7683 self.1 += 1;
7684 Some(ret)
7685 }
7686 }
7687}
7688impl ::core::iter::ExactSizeIterator for RelayTransactionVecIterator {
7689 fn len(&self) -> usize {
7690 self.2 - self.1
7691 }
7692}
7693impl ::core::iter::IntoIterator for RelayTransactionVec {
7694 type Item = RelayTransaction;
7695 type IntoIter = RelayTransactionVecIterator;
7696 fn into_iter(self) -> Self::IntoIter {
7697 let len = self.len();
7698 RelayTransactionVecIterator(self, 0, len)
7699 }
7700}
7701impl<'r> RelayTransactionVecReader<'r> {
7702 pub fn iter<'t>(&'t self) -> RelayTransactionVecReaderIterator<'t, 'r> {
7703 RelayTransactionVecReaderIterator(&self, 0, self.len())
7704 }
7705}
7706pub struct RelayTransactionVecReaderIterator<'t, 'r>(
7707 &'t RelayTransactionVecReader<'r>,
7708 usize,
7709 usize,
7710);
7711impl<'t: 'r, 'r> ::core::iter::Iterator for RelayTransactionVecReaderIterator<'t, 'r> {
7712 type Item = RelayTransactionReader<'t>;
7713 fn next(&mut self) -> Option<Self::Item> {
7714 if self.1 >= self.2 {
7715 None
7716 } else {
7717 let ret = self.0.get_unchecked(self.1);
7718 self.1 += 1;
7719 Some(ret)
7720 }
7721 }
7722}
7723impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for RelayTransactionVecReaderIterator<'t, 'r> {
7724 fn len(&self) -> usize {
7725 self.2 - self.1
7726 }
7727}
7728#[derive(Clone)]
7729pub struct RelayTransactions(molecule::bytes::Bytes);
7730impl ::core::fmt::LowerHex for RelayTransactions {
7731 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7732 use molecule::hex_string;
7733 if f.alternate() {
7734 write!(f, "0x")?;
7735 }
7736 write!(f, "{}", hex_string(self.as_slice()))
7737 }
7738}
7739impl ::core::fmt::Debug for RelayTransactions {
7740 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7741 write!(f, "{}({:#x})", Self::NAME, self)
7742 }
7743}
7744impl ::core::fmt::Display for RelayTransactions {
7745 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7746 write!(f, "{} {{ ", Self::NAME)?;
7747 write!(f, "{}: {}", "transactions", self.transactions())?;
7748 let extra_count = self.count_extra_fields();
7749 if extra_count != 0 {
7750 write!(f, ", .. ({} fields)", extra_count)?;
7751 }
7752 write!(f, " }}")
7753 }
7754}
7755impl ::core::default::Default for RelayTransactions {
7756 fn default() -> Self {
7757 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7758 RelayTransactions::new_unchecked(v)
7759 }
7760}
7761impl RelayTransactions {
7762 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 4, 0, 0, 0];
7763 pub const FIELD_COUNT: usize = 1;
7764 pub fn total_size(&self) -> usize {
7765 molecule::unpack_number(self.as_slice()) as usize
7766 }
7767 pub fn field_count(&self) -> usize {
7768 if self.total_size() == molecule::NUMBER_SIZE {
7769 0
7770 } else {
7771 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7772 }
7773 }
7774 pub fn count_extra_fields(&self) -> usize {
7775 self.field_count() - Self::FIELD_COUNT
7776 }
7777 pub fn has_extra_fields(&self) -> bool {
7778 Self::FIELD_COUNT != self.field_count()
7779 }
7780 pub fn transactions(&self) -> RelayTransactionVec {
7781 let slice = self.as_slice();
7782 let start = molecule::unpack_number(&slice[4..]) as usize;
7783 if self.has_extra_fields() {
7784 let end = molecule::unpack_number(&slice[8..]) as usize;
7785 RelayTransactionVec::new_unchecked(self.0.slice(start..end))
7786 } else {
7787 RelayTransactionVec::new_unchecked(self.0.slice(start..))
7788 }
7789 }
7790 pub fn as_reader<'r>(&'r self) -> RelayTransactionsReader<'r> {
7791 RelayTransactionsReader::new_unchecked(self.as_slice())
7792 }
7793}
7794impl molecule::prelude::Entity for RelayTransactions {
7795 type Builder = RelayTransactionsBuilder;
7796 const NAME: &'static str = "RelayTransactions";
7797 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7798 RelayTransactions(data)
7799 }
7800 fn as_bytes(&self) -> molecule::bytes::Bytes {
7801 self.0.clone()
7802 }
7803 fn as_slice(&self) -> &[u8] {
7804 &self.0[..]
7805 }
7806 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7807 RelayTransactionsReader::from_slice(slice).map(|reader| reader.to_entity())
7808 }
7809 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7810 RelayTransactionsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7811 }
7812 fn new_builder() -> Self::Builder {
7813 ::core::default::Default::default()
7814 }
7815 fn as_builder(self) -> Self::Builder {
7816 Self::new_builder().transactions(self.transactions())
7817 }
7818}
7819#[derive(Clone, Copy)]
7820pub struct RelayTransactionsReader<'r>(&'r [u8]);
7821impl<'r> ::core::fmt::LowerHex for RelayTransactionsReader<'r> {
7822 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7823 use molecule::hex_string;
7824 if f.alternate() {
7825 write!(f, "0x")?;
7826 }
7827 write!(f, "{}", hex_string(self.as_slice()))
7828 }
7829}
7830impl<'r> ::core::fmt::Debug for RelayTransactionsReader<'r> {
7831 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7832 write!(f, "{}({:#x})", Self::NAME, self)
7833 }
7834}
7835impl<'r> ::core::fmt::Display for RelayTransactionsReader<'r> {
7836 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7837 write!(f, "{} {{ ", Self::NAME)?;
7838 write!(f, "{}: {}", "transactions", self.transactions())?;
7839 let extra_count = self.count_extra_fields();
7840 if extra_count != 0 {
7841 write!(f, ", .. ({} fields)", extra_count)?;
7842 }
7843 write!(f, " }}")
7844 }
7845}
7846impl<'r> RelayTransactionsReader<'r> {
7847 pub const FIELD_COUNT: usize = 1;
7848 pub fn total_size(&self) -> usize {
7849 molecule::unpack_number(self.as_slice()) as usize
7850 }
7851 pub fn field_count(&self) -> usize {
7852 if self.total_size() == molecule::NUMBER_SIZE {
7853 0
7854 } else {
7855 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7856 }
7857 }
7858 pub fn count_extra_fields(&self) -> usize {
7859 self.field_count() - Self::FIELD_COUNT
7860 }
7861 pub fn has_extra_fields(&self) -> bool {
7862 Self::FIELD_COUNT != self.field_count()
7863 }
7864 pub fn transactions(&self) -> RelayTransactionVecReader<'r> {
7865 let slice = self.as_slice();
7866 let start = molecule::unpack_number(&slice[4..]) as usize;
7867 if self.has_extra_fields() {
7868 let end = molecule::unpack_number(&slice[8..]) as usize;
7869 RelayTransactionVecReader::new_unchecked(&self.as_slice()[start..end])
7870 } else {
7871 RelayTransactionVecReader::new_unchecked(&self.as_slice()[start..])
7872 }
7873 }
7874}
7875impl<'r> molecule::prelude::Reader<'r> for RelayTransactionsReader<'r> {
7876 type Entity = RelayTransactions;
7877 const NAME: &'static str = "RelayTransactionsReader";
7878 fn to_entity(&self) -> Self::Entity {
7879 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7880 }
7881 fn new_unchecked(slice: &'r [u8]) -> Self {
7882 RelayTransactionsReader(slice)
7883 }
7884 fn as_slice(&self) -> &'r [u8] {
7885 self.0
7886 }
7887 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
7888 use molecule::verification_error as ve;
7889 let slice_len = slice.len();
7890 if slice_len < molecule::NUMBER_SIZE {
7891 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
7892 }
7893 let total_size = molecule::unpack_number(slice) as usize;
7894 if slice_len != total_size {
7895 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7896 }
7897 if slice_len < molecule::NUMBER_SIZE * 2 {
7898 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
7899 }
7900 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7901 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7902 return ve!(Self, OffsetsNotMatch);
7903 }
7904 if slice_len < offset_first {
7905 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7906 }
7907 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
7908 if field_count < Self::FIELD_COUNT {
7909 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7910 } else if !compatible && field_count > Self::FIELD_COUNT {
7911 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7912 };
7913 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7914 .chunks_exact(molecule::NUMBER_SIZE)
7915 .map(|x| molecule::unpack_number(x) as usize)
7916 .collect();
7917 offsets.push(total_size);
7918 if offsets.windows(2).any(|i| i[0] > i[1]) {
7919 return ve!(Self, OffsetsNotMatch);
7920 }
7921 RelayTransactionVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
7922 Ok(())
7923 }
7924}
7925#[derive(Debug, Default)]
7926pub struct RelayTransactionsBuilder {
7927 pub(crate) transactions: RelayTransactionVec,
7928}
7929impl RelayTransactionsBuilder {
7930 pub const FIELD_COUNT: usize = 1;
7931 pub fn transactions(mut self, v: RelayTransactionVec) -> Self {
7932 self.transactions = v;
7933 self
7934 }
7935}
7936impl molecule::prelude::Builder for RelayTransactionsBuilder {
7937 type Entity = RelayTransactions;
7938 const NAME: &'static str = "RelayTransactionsBuilder";
7939 fn expected_length(&self) -> usize {
7940 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.transactions.as_slice().len()
7941 }
7942 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7943 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
7944 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
7945 offsets.push(total_size);
7946 total_size += self.transactions.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.transactions.as_slice())?;
7952 Ok(())
7953 }
7954 fn build(&self) -> Self::Entity {
7955 let mut inner = Vec::with_capacity(self.expected_length());
7956 self.write(&mut inner)
7957 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7958 RelayTransactions::new_unchecked(inner.into())
7959 }
7960}
7961#[derive(Clone)]
7962pub struct RelayTransactionHashes(molecule::bytes::Bytes);
7963impl ::core::fmt::LowerHex for RelayTransactionHashes {
7964 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7965 use molecule::hex_string;
7966 if f.alternate() {
7967 write!(f, "0x")?;
7968 }
7969 write!(f, "{}", hex_string(self.as_slice()))
7970 }
7971}
7972impl ::core::fmt::Debug for RelayTransactionHashes {
7973 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7974 write!(f, "{}({:#x})", Self::NAME, self)
7975 }
7976}
7977impl ::core::fmt::Display for RelayTransactionHashes {
7978 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7979 write!(f, "{} {{ ", Self::NAME)?;
7980 write!(f, "{}: {}", "tx_hashes", self.tx_hashes())?;
7981 let extra_count = self.count_extra_fields();
7982 if extra_count != 0 {
7983 write!(f, ", .. ({} fields)", extra_count)?;
7984 }
7985 write!(f, " }}")
7986 }
7987}
7988impl ::core::default::Default for RelayTransactionHashes {
7989 fn default() -> Self {
7990 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7991 RelayTransactionHashes::new_unchecked(v)
7992 }
7993}
7994impl RelayTransactionHashes {
7995 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
7996 pub const FIELD_COUNT: usize = 1;
7997 pub fn total_size(&self) -> usize {
7998 molecule::unpack_number(self.as_slice()) as usize
7999 }
8000 pub fn field_count(&self) -> usize {
8001 if self.total_size() == molecule::NUMBER_SIZE {
8002 0
8003 } else {
8004 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8005 }
8006 }
8007 pub fn count_extra_fields(&self) -> usize {
8008 self.field_count() - Self::FIELD_COUNT
8009 }
8010 pub fn has_extra_fields(&self) -> bool {
8011 Self::FIELD_COUNT != self.field_count()
8012 }
8013 pub fn tx_hashes(&self) -> Byte32Vec {
8014 let slice = self.as_slice();
8015 let start = molecule::unpack_number(&slice[4..]) as usize;
8016 if self.has_extra_fields() {
8017 let end = molecule::unpack_number(&slice[8..]) as usize;
8018 Byte32Vec::new_unchecked(self.0.slice(start..end))
8019 } else {
8020 Byte32Vec::new_unchecked(self.0.slice(start..))
8021 }
8022 }
8023 pub fn as_reader<'r>(&'r self) -> RelayTransactionHashesReader<'r> {
8024 RelayTransactionHashesReader::new_unchecked(self.as_slice())
8025 }
8026}
8027impl molecule::prelude::Entity for RelayTransactionHashes {
8028 type Builder = RelayTransactionHashesBuilder;
8029 const NAME: &'static str = "RelayTransactionHashes";
8030 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8031 RelayTransactionHashes(data)
8032 }
8033 fn as_bytes(&self) -> molecule::bytes::Bytes {
8034 self.0.clone()
8035 }
8036 fn as_slice(&self) -> &[u8] {
8037 &self.0[..]
8038 }
8039 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8040 RelayTransactionHashesReader::from_slice(slice).map(|reader| reader.to_entity())
8041 }
8042 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8043 RelayTransactionHashesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8044 }
8045 fn new_builder() -> Self::Builder {
8046 ::core::default::Default::default()
8047 }
8048 fn as_builder(self) -> Self::Builder {
8049 Self::new_builder().tx_hashes(self.tx_hashes())
8050 }
8051}
8052#[derive(Clone, Copy)]
8053pub struct RelayTransactionHashesReader<'r>(&'r [u8]);
8054impl<'r> ::core::fmt::LowerHex for RelayTransactionHashesReader<'r> {
8055 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8056 use molecule::hex_string;
8057 if f.alternate() {
8058 write!(f, "0x")?;
8059 }
8060 write!(f, "{}", hex_string(self.as_slice()))
8061 }
8062}
8063impl<'r> ::core::fmt::Debug for RelayTransactionHashesReader<'r> {
8064 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8065 write!(f, "{}({:#x})", Self::NAME, self)
8066 }
8067}
8068impl<'r> ::core::fmt::Display for RelayTransactionHashesReader<'r> {
8069 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8070 write!(f, "{} {{ ", Self::NAME)?;
8071 write!(f, "{}: {}", "tx_hashes", self.tx_hashes())?;
8072 let extra_count = self.count_extra_fields();
8073 if extra_count != 0 {
8074 write!(f, ", .. ({} fields)", extra_count)?;
8075 }
8076 write!(f, " }}")
8077 }
8078}
8079impl<'r> RelayTransactionHashesReader<'r> {
8080 pub const FIELD_COUNT: usize = 1;
8081 pub fn total_size(&self) -> usize {
8082 molecule::unpack_number(self.as_slice()) as usize
8083 }
8084 pub fn field_count(&self) -> usize {
8085 if self.total_size() == molecule::NUMBER_SIZE {
8086 0
8087 } else {
8088 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8089 }
8090 }
8091 pub fn count_extra_fields(&self) -> usize {
8092 self.field_count() - Self::FIELD_COUNT
8093 }
8094 pub fn has_extra_fields(&self) -> bool {
8095 Self::FIELD_COUNT != self.field_count()
8096 }
8097 pub fn tx_hashes(&self) -> Byte32VecReader<'r> {
8098 let slice = self.as_slice();
8099 let start = molecule::unpack_number(&slice[4..]) as usize;
8100 if self.has_extra_fields() {
8101 let end = molecule::unpack_number(&slice[8..]) as usize;
8102 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
8103 } else {
8104 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
8105 }
8106 }
8107}
8108impl<'r> molecule::prelude::Reader<'r> for RelayTransactionHashesReader<'r> {
8109 type Entity = RelayTransactionHashes;
8110 const NAME: &'static str = "RelayTransactionHashesReader";
8111 fn to_entity(&self) -> Self::Entity {
8112 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8113 }
8114 fn new_unchecked(slice: &'r [u8]) -> Self {
8115 RelayTransactionHashesReader(slice)
8116 }
8117 fn as_slice(&self) -> &'r [u8] {
8118 self.0
8119 }
8120 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8121 use molecule::verification_error as ve;
8122 let slice_len = slice.len();
8123 if slice_len < molecule::NUMBER_SIZE {
8124 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8125 }
8126 let total_size = molecule::unpack_number(slice) as usize;
8127 if slice_len != total_size {
8128 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8129 }
8130 if slice_len < molecule::NUMBER_SIZE * 2 {
8131 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8132 }
8133 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8134 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8135 return ve!(Self, OffsetsNotMatch);
8136 }
8137 if slice_len < offset_first {
8138 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8139 }
8140 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8141 if field_count < Self::FIELD_COUNT {
8142 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8143 } else if !compatible && field_count > Self::FIELD_COUNT {
8144 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8145 };
8146 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8147 .chunks_exact(molecule::NUMBER_SIZE)
8148 .map(|x| molecule::unpack_number(x) as usize)
8149 .collect();
8150 offsets.push(total_size);
8151 if offsets.windows(2).any(|i| i[0] > i[1]) {
8152 return ve!(Self, OffsetsNotMatch);
8153 }
8154 Byte32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8155 Ok(())
8156 }
8157}
8158#[derive(Debug, Default)]
8159pub struct RelayTransactionHashesBuilder {
8160 pub(crate) tx_hashes: Byte32Vec,
8161}
8162impl RelayTransactionHashesBuilder {
8163 pub const FIELD_COUNT: usize = 1;
8164 pub fn tx_hashes(mut self, v: Byte32Vec) -> Self {
8165 self.tx_hashes = v;
8166 self
8167 }
8168}
8169impl molecule::prelude::Builder for RelayTransactionHashesBuilder {
8170 type Entity = RelayTransactionHashes;
8171 const NAME: &'static str = "RelayTransactionHashesBuilder";
8172 fn expected_length(&self) -> usize {
8173 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.tx_hashes.as_slice().len()
8174 }
8175 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8176 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8177 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8178 offsets.push(total_size);
8179 total_size += self.tx_hashes.as_slice().len();
8180 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8181 for offset in offsets.into_iter() {
8182 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8183 }
8184 writer.write_all(self.tx_hashes.as_slice())?;
8185 Ok(())
8186 }
8187 fn build(&self) -> Self::Entity {
8188 let mut inner = Vec::with_capacity(self.expected_length());
8189 self.write(&mut inner)
8190 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8191 RelayTransactionHashes::new_unchecked(inner.into())
8192 }
8193}
8194#[derive(Clone)]
8195pub struct GetRelayTransactions(molecule::bytes::Bytes);
8196impl ::core::fmt::LowerHex for GetRelayTransactions {
8197 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8198 use molecule::hex_string;
8199 if f.alternate() {
8200 write!(f, "0x")?;
8201 }
8202 write!(f, "{}", hex_string(self.as_slice()))
8203 }
8204}
8205impl ::core::fmt::Debug for GetRelayTransactions {
8206 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8207 write!(f, "{}({:#x})", Self::NAME, self)
8208 }
8209}
8210impl ::core::fmt::Display for GetRelayTransactions {
8211 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8212 write!(f, "{} {{ ", Self::NAME)?;
8213 write!(f, "{}: {}", "tx_hashes", self.tx_hashes())?;
8214 let extra_count = self.count_extra_fields();
8215 if extra_count != 0 {
8216 write!(f, ", .. ({} fields)", extra_count)?;
8217 }
8218 write!(f, " }}")
8219 }
8220}
8221impl ::core::default::Default for GetRelayTransactions {
8222 fn default() -> Self {
8223 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
8224 GetRelayTransactions::new_unchecked(v)
8225 }
8226}
8227impl GetRelayTransactions {
8228 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
8229 pub const FIELD_COUNT: usize = 1;
8230 pub fn total_size(&self) -> usize {
8231 molecule::unpack_number(self.as_slice()) as usize
8232 }
8233 pub fn field_count(&self) -> usize {
8234 if self.total_size() == molecule::NUMBER_SIZE {
8235 0
8236 } else {
8237 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8238 }
8239 }
8240 pub fn count_extra_fields(&self) -> usize {
8241 self.field_count() - Self::FIELD_COUNT
8242 }
8243 pub fn has_extra_fields(&self) -> bool {
8244 Self::FIELD_COUNT != self.field_count()
8245 }
8246 pub fn tx_hashes(&self) -> Byte32Vec {
8247 let slice = self.as_slice();
8248 let start = molecule::unpack_number(&slice[4..]) as usize;
8249 if self.has_extra_fields() {
8250 let end = molecule::unpack_number(&slice[8..]) as usize;
8251 Byte32Vec::new_unchecked(self.0.slice(start..end))
8252 } else {
8253 Byte32Vec::new_unchecked(self.0.slice(start..))
8254 }
8255 }
8256 pub fn as_reader<'r>(&'r self) -> GetRelayTransactionsReader<'r> {
8257 GetRelayTransactionsReader::new_unchecked(self.as_slice())
8258 }
8259}
8260impl molecule::prelude::Entity for GetRelayTransactions {
8261 type Builder = GetRelayTransactionsBuilder;
8262 const NAME: &'static str = "GetRelayTransactions";
8263 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8264 GetRelayTransactions(data)
8265 }
8266 fn as_bytes(&self) -> molecule::bytes::Bytes {
8267 self.0.clone()
8268 }
8269 fn as_slice(&self) -> &[u8] {
8270 &self.0[..]
8271 }
8272 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8273 GetRelayTransactionsReader::from_slice(slice).map(|reader| reader.to_entity())
8274 }
8275 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8276 GetRelayTransactionsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8277 }
8278 fn new_builder() -> Self::Builder {
8279 ::core::default::Default::default()
8280 }
8281 fn as_builder(self) -> Self::Builder {
8282 Self::new_builder().tx_hashes(self.tx_hashes())
8283 }
8284}
8285#[derive(Clone, Copy)]
8286pub struct GetRelayTransactionsReader<'r>(&'r [u8]);
8287impl<'r> ::core::fmt::LowerHex for GetRelayTransactionsReader<'r> {
8288 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8289 use molecule::hex_string;
8290 if f.alternate() {
8291 write!(f, "0x")?;
8292 }
8293 write!(f, "{}", hex_string(self.as_slice()))
8294 }
8295}
8296impl<'r> ::core::fmt::Debug for GetRelayTransactionsReader<'r> {
8297 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8298 write!(f, "{}({:#x})", Self::NAME, self)
8299 }
8300}
8301impl<'r> ::core::fmt::Display for GetRelayTransactionsReader<'r> {
8302 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8303 write!(f, "{} {{ ", Self::NAME)?;
8304 write!(f, "{}: {}", "tx_hashes", self.tx_hashes())?;
8305 let extra_count = self.count_extra_fields();
8306 if extra_count != 0 {
8307 write!(f, ", .. ({} fields)", extra_count)?;
8308 }
8309 write!(f, " }}")
8310 }
8311}
8312impl<'r> GetRelayTransactionsReader<'r> {
8313 pub const FIELD_COUNT: usize = 1;
8314 pub fn total_size(&self) -> usize {
8315 molecule::unpack_number(self.as_slice()) as usize
8316 }
8317 pub fn field_count(&self) -> usize {
8318 if self.total_size() == molecule::NUMBER_SIZE {
8319 0
8320 } else {
8321 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8322 }
8323 }
8324 pub fn count_extra_fields(&self) -> usize {
8325 self.field_count() - Self::FIELD_COUNT
8326 }
8327 pub fn has_extra_fields(&self) -> bool {
8328 Self::FIELD_COUNT != self.field_count()
8329 }
8330 pub fn tx_hashes(&self) -> Byte32VecReader<'r> {
8331 let slice = self.as_slice();
8332 let start = molecule::unpack_number(&slice[4..]) as usize;
8333 if self.has_extra_fields() {
8334 let end = molecule::unpack_number(&slice[8..]) as usize;
8335 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
8336 } else {
8337 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
8338 }
8339 }
8340}
8341impl<'r> molecule::prelude::Reader<'r> for GetRelayTransactionsReader<'r> {
8342 type Entity = GetRelayTransactions;
8343 const NAME: &'static str = "GetRelayTransactionsReader";
8344 fn to_entity(&self) -> Self::Entity {
8345 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8346 }
8347 fn new_unchecked(slice: &'r [u8]) -> Self {
8348 GetRelayTransactionsReader(slice)
8349 }
8350 fn as_slice(&self) -> &'r [u8] {
8351 self.0
8352 }
8353 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8354 use molecule::verification_error as ve;
8355 let slice_len = slice.len();
8356 if slice_len < molecule::NUMBER_SIZE {
8357 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8358 }
8359 let total_size = molecule::unpack_number(slice) as usize;
8360 if slice_len != total_size {
8361 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8362 }
8363 if slice_len < molecule::NUMBER_SIZE * 2 {
8364 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8365 }
8366 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8367 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8368 return ve!(Self, OffsetsNotMatch);
8369 }
8370 if slice_len < offset_first {
8371 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8372 }
8373 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8374 if field_count < Self::FIELD_COUNT {
8375 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8376 } else if !compatible && field_count > Self::FIELD_COUNT {
8377 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8378 };
8379 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8380 .chunks_exact(molecule::NUMBER_SIZE)
8381 .map(|x| molecule::unpack_number(x) as usize)
8382 .collect();
8383 offsets.push(total_size);
8384 if offsets.windows(2).any(|i| i[0] > i[1]) {
8385 return ve!(Self, OffsetsNotMatch);
8386 }
8387 Byte32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8388 Ok(())
8389 }
8390}
8391#[derive(Debug, Default)]
8392pub struct GetRelayTransactionsBuilder {
8393 pub(crate) tx_hashes: Byte32Vec,
8394}
8395impl GetRelayTransactionsBuilder {
8396 pub const FIELD_COUNT: usize = 1;
8397 pub fn tx_hashes(mut self, v: Byte32Vec) -> Self {
8398 self.tx_hashes = v;
8399 self
8400 }
8401}
8402impl molecule::prelude::Builder for GetRelayTransactionsBuilder {
8403 type Entity = GetRelayTransactions;
8404 const NAME: &'static str = "GetRelayTransactionsBuilder";
8405 fn expected_length(&self) -> usize {
8406 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.tx_hashes.as_slice().len()
8407 }
8408 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8409 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8410 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8411 offsets.push(total_size);
8412 total_size += self.tx_hashes.as_slice().len();
8413 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8414 for offset in offsets.into_iter() {
8415 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8416 }
8417 writer.write_all(self.tx_hashes.as_slice())?;
8418 Ok(())
8419 }
8420 fn build(&self) -> Self::Entity {
8421 let mut inner = Vec::with_capacity(self.expected_length());
8422 self.write(&mut inner)
8423 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8424 GetRelayTransactions::new_unchecked(inner.into())
8425 }
8426}
8427#[derive(Clone)]
8428pub struct GetBlockTransactions(molecule::bytes::Bytes);
8429impl ::core::fmt::LowerHex for GetBlockTransactions {
8430 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8431 use molecule::hex_string;
8432 if f.alternate() {
8433 write!(f, "0x")?;
8434 }
8435 write!(f, "{}", hex_string(self.as_slice()))
8436 }
8437}
8438impl ::core::fmt::Debug for GetBlockTransactions {
8439 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8440 write!(f, "{}({:#x})", Self::NAME, self)
8441 }
8442}
8443impl ::core::fmt::Display for GetBlockTransactions {
8444 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8445 write!(f, "{} {{ ", Self::NAME)?;
8446 write!(f, "{}: {}", "block_hash", self.block_hash())?;
8447 write!(f, ", {}: {}", "indexes", self.indexes())?;
8448 write!(f, ", {}: {}", "uncle_indexes", self.uncle_indexes())?;
8449 let extra_count = self.count_extra_fields();
8450 if extra_count != 0 {
8451 write!(f, ", .. ({} fields)", extra_count)?;
8452 }
8453 write!(f, " }}")
8454 }
8455}
8456impl ::core::default::Default for GetBlockTransactions {
8457 fn default() -> Self {
8458 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
8459 GetBlockTransactions::new_unchecked(v)
8460 }
8461}
8462impl GetBlockTransactions {
8463 const DEFAULT_VALUE: [u8; 56] = [
8464 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,
8465 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8466 ];
8467 pub const FIELD_COUNT: usize = 3;
8468 pub fn total_size(&self) -> usize {
8469 molecule::unpack_number(self.as_slice()) as usize
8470 }
8471 pub fn field_count(&self) -> usize {
8472 if self.total_size() == molecule::NUMBER_SIZE {
8473 0
8474 } else {
8475 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8476 }
8477 }
8478 pub fn count_extra_fields(&self) -> usize {
8479 self.field_count() - Self::FIELD_COUNT
8480 }
8481 pub fn has_extra_fields(&self) -> bool {
8482 Self::FIELD_COUNT != self.field_count()
8483 }
8484 pub fn block_hash(&self) -> Byte32 {
8485 let slice = self.as_slice();
8486 let start = molecule::unpack_number(&slice[4..]) as usize;
8487 let end = molecule::unpack_number(&slice[8..]) as usize;
8488 Byte32::new_unchecked(self.0.slice(start..end))
8489 }
8490 pub fn indexes(&self) -> Uint32Vec {
8491 let slice = self.as_slice();
8492 let start = molecule::unpack_number(&slice[8..]) as usize;
8493 let end = molecule::unpack_number(&slice[12..]) as usize;
8494 Uint32Vec::new_unchecked(self.0.slice(start..end))
8495 }
8496 pub fn uncle_indexes(&self) -> Uint32Vec {
8497 let slice = self.as_slice();
8498 let start = molecule::unpack_number(&slice[12..]) as usize;
8499 if self.has_extra_fields() {
8500 let end = molecule::unpack_number(&slice[16..]) as usize;
8501 Uint32Vec::new_unchecked(self.0.slice(start..end))
8502 } else {
8503 Uint32Vec::new_unchecked(self.0.slice(start..))
8504 }
8505 }
8506 pub fn as_reader<'r>(&'r self) -> GetBlockTransactionsReader<'r> {
8507 GetBlockTransactionsReader::new_unchecked(self.as_slice())
8508 }
8509}
8510impl molecule::prelude::Entity for GetBlockTransactions {
8511 type Builder = GetBlockTransactionsBuilder;
8512 const NAME: &'static str = "GetBlockTransactions";
8513 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8514 GetBlockTransactions(data)
8515 }
8516 fn as_bytes(&self) -> molecule::bytes::Bytes {
8517 self.0.clone()
8518 }
8519 fn as_slice(&self) -> &[u8] {
8520 &self.0[..]
8521 }
8522 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8523 GetBlockTransactionsReader::from_slice(slice).map(|reader| reader.to_entity())
8524 }
8525 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8526 GetBlockTransactionsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8527 }
8528 fn new_builder() -> Self::Builder {
8529 ::core::default::Default::default()
8530 }
8531 fn as_builder(self) -> Self::Builder {
8532 Self::new_builder()
8533 .block_hash(self.block_hash())
8534 .indexes(self.indexes())
8535 .uncle_indexes(self.uncle_indexes())
8536 }
8537}
8538#[derive(Clone, Copy)]
8539pub struct GetBlockTransactionsReader<'r>(&'r [u8]);
8540impl<'r> ::core::fmt::LowerHex for GetBlockTransactionsReader<'r> {
8541 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8542 use molecule::hex_string;
8543 if f.alternate() {
8544 write!(f, "0x")?;
8545 }
8546 write!(f, "{}", hex_string(self.as_slice()))
8547 }
8548}
8549impl<'r> ::core::fmt::Debug for GetBlockTransactionsReader<'r> {
8550 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8551 write!(f, "{}({:#x})", Self::NAME, self)
8552 }
8553}
8554impl<'r> ::core::fmt::Display for GetBlockTransactionsReader<'r> {
8555 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8556 write!(f, "{} {{ ", Self::NAME)?;
8557 write!(f, "{}: {}", "block_hash", self.block_hash())?;
8558 write!(f, ", {}: {}", "indexes", self.indexes())?;
8559 write!(f, ", {}: {}", "uncle_indexes", self.uncle_indexes())?;
8560 let extra_count = self.count_extra_fields();
8561 if extra_count != 0 {
8562 write!(f, ", .. ({} fields)", extra_count)?;
8563 }
8564 write!(f, " }}")
8565 }
8566}
8567impl<'r> GetBlockTransactionsReader<'r> {
8568 pub const FIELD_COUNT: usize = 3;
8569 pub fn total_size(&self) -> usize {
8570 molecule::unpack_number(self.as_slice()) as usize
8571 }
8572 pub fn field_count(&self) -> usize {
8573 if self.total_size() == molecule::NUMBER_SIZE {
8574 0
8575 } else {
8576 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8577 }
8578 }
8579 pub fn count_extra_fields(&self) -> usize {
8580 self.field_count() - Self::FIELD_COUNT
8581 }
8582 pub fn has_extra_fields(&self) -> bool {
8583 Self::FIELD_COUNT != self.field_count()
8584 }
8585 pub fn block_hash(&self) -> Byte32Reader<'r> {
8586 let slice = self.as_slice();
8587 let start = molecule::unpack_number(&slice[4..]) as usize;
8588 let end = molecule::unpack_number(&slice[8..]) as usize;
8589 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
8590 }
8591 pub fn indexes(&self) -> Uint32VecReader<'r> {
8592 let slice = self.as_slice();
8593 let start = molecule::unpack_number(&slice[8..]) as usize;
8594 let end = molecule::unpack_number(&slice[12..]) as usize;
8595 Uint32VecReader::new_unchecked(&self.as_slice()[start..end])
8596 }
8597 pub fn uncle_indexes(&self) -> Uint32VecReader<'r> {
8598 let slice = self.as_slice();
8599 let start = molecule::unpack_number(&slice[12..]) as usize;
8600 if self.has_extra_fields() {
8601 let end = molecule::unpack_number(&slice[16..]) as usize;
8602 Uint32VecReader::new_unchecked(&self.as_slice()[start..end])
8603 } else {
8604 Uint32VecReader::new_unchecked(&self.as_slice()[start..])
8605 }
8606 }
8607}
8608impl<'r> molecule::prelude::Reader<'r> for GetBlockTransactionsReader<'r> {
8609 type Entity = GetBlockTransactions;
8610 const NAME: &'static str = "GetBlockTransactionsReader";
8611 fn to_entity(&self) -> Self::Entity {
8612 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8613 }
8614 fn new_unchecked(slice: &'r [u8]) -> Self {
8615 GetBlockTransactionsReader(slice)
8616 }
8617 fn as_slice(&self) -> &'r [u8] {
8618 self.0
8619 }
8620 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8621 use molecule::verification_error as ve;
8622 let slice_len = slice.len();
8623 if slice_len < molecule::NUMBER_SIZE {
8624 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8625 }
8626 let total_size = molecule::unpack_number(slice) as usize;
8627 if slice_len != total_size {
8628 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8629 }
8630 if slice_len < molecule::NUMBER_SIZE * 2 {
8631 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8632 }
8633 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8634 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8635 return ve!(Self, OffsetsNotMatch);
8636 }
8637 if slice_len < offset_first {
8638 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8639 }
8640 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8641 if field_count < Self::FIELD_COUNT {
8642 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8643 } else if !compatible && field_count > Self::FIELD_COUNT {
8644 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8645 };
8646 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8647 .chunks_exact(molecule::NUMBER_SIZE)
8648 .map(|x| molecule::unpack_number(x) as usize)
8649 .collect();
8650 offsets.push(total_size);
8651 if offsets.windows(2).any(|i| i[0] > i[1]) {
8652 return ve!(Self, OffsetsNotMatch);
8653 }
8654 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8655 Uint32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
8656 Uint32VecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
8657 Ok(())
8658 }
8659}
8660#[derive(Debug, Default)]
8661pub struct GetBlockTransactionsBuilder {
8662 pub(crate) block_hash: Byte32,
8663 pub(crate) indexes: Uint32Vec,
8664 pub(crate) uncle_indexes: Uint32Vec,
8665}
8666impl GetBlockTransactionsBuilder {
8667 pub const FIELD_COUNT: usize = 3;
8668 pub fn block_hash(mut self, v: Byte32) -> Self {
8669 self.block_hash = v;
8670 self
8671 }
8672 pub fn indexes(mut self, v: Uint32Vec) -> Self {
8673 self.indexes = v;
8674 self
8675 }
8676 pub fn uncle_indexes(mut self, v: Uint32Vec) -> Self {
8677 self.uncle_indexes = v;
8678 self
8679 }
8680}
8681impl molecule::prelude::Builder for GetBlockTransactionsBuilder {
8682 type Entity = GetBlockTransactions;
8683 const NAME: &'static str = "GetBlockTransactionsBuilder";
8684 fn expected_length(&self) -> usize {
8685 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
8686 + self.block_hash.as_slice().len()
8687 + self.indexes.as_slice().len()
8688 + self.uncle_indexes.as_slice().len()
8689 }
8690 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8691 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8692 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8693 offsets.push(total_size);
8694 total_size += self.block_hash.as_slice().len();
8695 offsets.push(total_size);
8696 total_size += self.indexes.as_slice().len();
8697 offsets.push(total_size);
8698 total_size += self.uncle_indexes.as_slice().len();
8699 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8700 for offset in offsets.into_iter() {
8701 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8702 }
8703 writer.write_all(self.block_hash.as_slice())?;
8704 writer.write_all(self.indexes.as_slice())?;
8705 writer.write_all(self.uncle_indexes.as_slice())?;
8706 Ok(())
8707 }
8708 fn build(&self) -> Self::Entity {
8709 let mut inner = Vec::with_capacity(self.expected_length());
8710 self.write(&mut inner)
8711 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8712 GetBlockTransactions::new_unchecked(inner.into())
8713 }
8714}
8715#[derive(Clone)]
8716pub struct BlockTransactions(molecule::bytes::Bytes);
8717impl ::core::fmt::LowerHex for BlockTransactions {
8718 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8719 use molecule::hex_string;
8720 if f.alternate() {
8721 write!(f, "0x")?;
8722 }
8723 write!(f, "{}", hex_string(self.as_slice()))
8724 }
8725}
8726impl ::core::fmt::Debug for BlockTransactions {
8727 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8728 write!(f, "{}({:#x})", Self::NAME, self)
8729 }
8730}
8731impl ::core::fmt::Display for BlockTransactions {
8732 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8733 write!(f, "{} {{ ", Self::NAME)?;
8734 write!(f, "{}: {}", "block_hash", self.block_hash())?;
8735 write!(f, ", {}: {}", "transactions", self.transactions())?;
8736 write!(f, ", {}: {}", "uncles", self.uncles())?;
8737 let extra_count = self.count_extra_fields();
8738 if extra_count != 0 {
8739 write!(f, ", .. ({} fields)", extra_count)?;
8740 }
8741 write!(f, " }}")
8742 }
8743}
8744impl ::core::default::Default for BlockTransactions {
8745 fn default() -> Self {
8746 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
8747 BlockTransactions::new_unchecked(v)
8748 }
8749}
8750impl BlockTransactions {
8751 const DEFAULT_VALUE: [u8; 56] = [
8752 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,
8753 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,
8754 ];
8755 pub const FIELD_COUNT: usize = 3;
8756 pub fn total_size(&self) -> usize {
8757 molecule::unpack_number(self.as_slice()) as usize
8758 }
8759 pub fn field_count(&self) -> usize {
8760 if self.total_size() == molecule::NUMBER_SIZE {
8761 0
8762 } else {
8763 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8764 }
8765 }
8766 pub fn count_extra_fields(&self) -> usize {
8767 self.field_count() - Self::FIELD_COUNT
8768 }
8769 pub fn has_extra_fields(&self) -> bool {
8770 Self::FIELD_COUNT != self.field_count()
8771 }
8772 pub fn block_hash(&self) -> Byte32 {
8773 let slice = self.as_slice();
8774 let start = molecule::unpack_number(&slice[4..]) as usize;
8775 let end = molecule::unpack_number(&slice[8..]) as usize;
8776 Byte32::new_unchecked(self.0.slice(start..end))
8777 }
8778 pub fn transactions(&self) -> TransactionVec {
8779 let slice = self.as_slice();
8780 let start = molecule::unpack_number(&slice[8..]) as usize;
8781 let end = molecule::unpack_number(&slice[12..]) as usize;
8782 TransactionVec::new_unchecked(self.0.slice(start..end))
8783 }
8784 pub fn uncles(&self) -> UncleBlockVec {
8785 let slice = self.as_slice();
8786 let start = molecule::unpack_number(&slice[12..]) as usize;
8787 if self.has_extra_fields() {
8788 let end = molecule::unpack_number(&slice[16..]) as usize;
8789 UncleBlockVec::new_unchecked(self.0.slice(start..end))
8790 } else {
8791 UncleBlockVec::new_unchecked(self.0.slice(start..))
8792 }
8793 }
8794 pub fn as_reader<'r>(&'r self) -> BlockTransactionsReader<'r> {
8795 BlockTransactionsReader::new_unchecked(self.as_slice())
8796 }
8797}
8798impl molecule::prelude::Entity for BlockTransactions {
8799 type Builder = BlockTransactionsBuilder;
8800 const NAME: &'static str = "BlockTransactions";
8801 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8802 BlockTransactions(data)
8803 }
8804 fn as_bytes(&self) -> molecule::bytes::Bytes {
8805 self.0.clone()
8806 }
8807 fn as_slice(&self) -> &[u8] {
8808 &self.0[..]
8809 }
8810 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8811 BlockTransactionsReader::from_slice(slice).map(|reader| reader.to_entity())
8812 }
8813 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8814 BlockTransactionsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8815 }
8816 fn new_builder() -> Self::Builder {
8817 ::core::default::Default::default()
8818 }
8819 fn as_builder(self) -> Self::Builder {
8820 Self::new_builder()
8821 .block_hash(self.block_hash())
8822 .transactions(self.transactions())
8823 .uncles(self.uncles())
8824 }
8825}
8826#[derive(Clone, Copy)]
8827pub struct BlockTransactionsReader<'r>(&'r [u8]);
8828impl<'r> ::core::fmt::LowerHex for BlockTransactionsReader<'r> {
8829 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8830 use molecule::hex_string;
8831 if f.alternate() {
8832 write!(f, "0x")?;
8833 }
8834 write!(f, "{}", hex_string(self.as_slice()))
8835 }
8836}
8837impl<'r> ::core::fmt::Debug for BlockTransactionsReader<'r> {
8838 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8839 write!(f, "{}({:#x})", Self::NAME, self)
8840 }
8841}
8842impl<'r> ::core::fmt::Display for BlockTransactionsReader<'r> {
8843 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8844 write!(f, "{} {{ ", Self::NAME)?;
8845 write!(f, "{}: {}", "block_hash", self.block_hash())?;
8846 write!(f, ", {}: {}", "transactions", self.transactions())?;
8847 write!(f, ", {}: {}", "uncles", self.uncles())?;
8848 let extra_count = self.count_extra_fields();
8849 if extra_count != 0 {
8850 write!(f, ", .. ({} fields)", extra_count)?;
8851 }
8852 write!(f, " }}")
8853 }
8854}
8855impl<'r> BlockTransactionsReader<'r> {
8856 pub const FIELD_COUNT: usize = 3;
8857 pub fn total_size(&self) -> usize {
8858 molecule::unpack_number(self.as_slice()) as usize
8859 }
8860 pub fn field_count(&self) -> usize {
8861 if self.total_size() == molecule::NUMBER_SIZE {
8862 0
8863 } else {
8864 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8865 }
8866 }
8867 pub fn count_extra_fields(&self) -> usize {
8868 self.field_count() - Self::FIELD_COUNT
8869 }
8870 pub fn has_extra_fields(&self) -> bool {
8871 Self::FIELD_COUNT != self.field_count()
8872 }
8873 pub fn block_hash(&self) -> Byte32Reader<'r> {
8874 let slice = self.as_slice();
8875 let start = molecule::unpack_number(&slice[4..]) as usize;
8876 let end = molecule::unpack_number(&slice[8..]) as usize;
8877 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
8878 }
8879 pub fn transactions(&self) -> TransactionVecReader<'r> {
8880 let slice = self.as_slice();
8881 let start = molecule::unpack_number(&slice[8..]) as usize;
8882 let end = molecule::unpack_number(&slice[12..]) as usize;
8883 TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
8884 }
8885 pub fn uncles(&self) -> UncleBlockVecReader<'r> {
8886 let slice = self.as_slice();
8887 let start = molecule::unpack_number(&slice[12..]) as usize;
8888 if self.has_extra_fields() {
8889 let end = molecule::unpack_number(&slice[16..]) as usize;
8890 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..end])
8891 } else {
8892 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..])
8893 }
8894 }
8895}
8896impl<'r> molecule::prelude::Reader<'r> for BlockTransactionsReader<'r> {
8897 type Entity = BlockTransactions;
8898 const NAME: &'static str = "BlockTransactionsReader";
8899 fn to_entity(&self) -> Self::Entity {
8900 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8901 }
8902 fn new_unchecked(slice: &'r [u8]) -> Self {
8903 BlockTransactionsReader(slice)
8904 }
8905 fn as_slice(&self) -> &'r [u8] {
8906 self.0
8907 }
8908 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8909 use molecule::verification_error as ve;
8910 let slice_len = slice.len();
8911 if slice_len < molecule::NUMBER_SIZE {
8912 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8913 }
8914 let total_size = molecule::unpack_number(slice) as usize;
8915 if slice_len != total_size {
8916 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8917 }
8918 if slice_len < molecule::NUMBER_SIZE * 2 {
8919 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8920 }
8921 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8922 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8923 return ve!(Self, OffsetsNotMatch);
8924 }
8925 if slice_len < offset_first {
8926 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8927 }
8928 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8929 if field_count < Self::FIELD_COUNT {
8930 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8931 } else if !compatible && field_count > Self::FIELD_COUNT {
8932 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8933 };
8934 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8935 .chunks_exact(molecule::NUMBER_SIZE)
8936 .map(|x| molecule::unpack_number(x) as usize)
8937 .collect();
8938 offsets.push(total_size);
8939 if offsets.windows(2).any(|i| i[0] > i[1]) {
8940 return ve!(Self, OffsetsNotMatch);
8941 }
8942 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8943 TransactionVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
8944 UncleBlockVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
8945 Ok(())
8946 }
8947}
8948#[derive(Debug, Default)]
8949pub struct BlockTransactionsBuilder {
8950 pub(crate) block_hash: Byte32,
8951 pub(crate) transactions: TransactionVec,
8952 pub(crate) uncles: UncleBlockVec,
8953}
8954impl BlockTransactionsBuilder {
8955 pub const FIELD_COUNT: usize = 3;
8956 pub fn block_hash(mut self, v: Byte32) -> Self {
8957 self.block_hash = v;
8958 self
8959 }
8960 pub fn transactions(mut self, v: TransactionVec) -> Self {
8961 self.transactions = v;
8962 self
8963 }
8964 pub fn uncles(mut self, v: UncleBlockVec) -> Self {
8965 self.uncles = v;
8966 self
8967 }
8968}
8969impl molecule::prelude::Builder for BlockTransactionsBuilder {
8970 type Entity = BlockTransactions;
8971 const NAME: &'static str = "BlockTransactionsBuilder";
8972 fn expected_length(&self) -> usize {
8973 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
8974 + self.block_hash.as_slice().len()
8975 + self.transactions.as_slice().len()
8976 + self.uncles.as_slice().len()
8977 }
8978 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8979 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8980 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8981 offsets.push(total_size);
8982 total_size += self.block_hash.as_slice().len();
8983 offsets.push(total_size);
8984 total_size += self.transactions.as_slice().len();
8985 offsets.push(total_size);
8986 total_size += self.uncles.as_slice().len();
8987 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8988 for offset in offsets.into_iter() {
8989 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8990 }
8991 writer.write_all(self.block_hash.as_slice())?;
8992 writer.write_all(self.transactions.as_slice())?;
8993 writer.write_all(self.uncles.as_slice())?;
8994 Ok(())
8995 }
8996 fn build(&self) -> Self::Entity {
8997 let mut inner = Vec::with_capacity(self.expected_length());
8998 self.write(&mut inner)
8999 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9000 BlockTransactions::new_unchecked(inner.into())
9001 }
9002}
9003#[derive(Clone)]
9004pub struct GetBlockProposal(molecule::bytes::Bytes);
9005impl ::core::fmt::LowerHex for GetBlockProposal {
9006 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9007 use molecule::hex_string;
9008 if f.alternate() {
9009 write!(f, "0x")?;
9010 }
9011 write!(f, "{}", hex_string(self.as_slice()))
9012 }
9013}
9014impl ::core::fmt::Debug for GetBlockProposal {
9015 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9016 write!(f, "{}({:#x})", Self::NAME, self)
9017 }
9018}
9019impl ::core::fmt::Display for GetBlockProposal {
9020 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9021 write!(f, "{} {{ ", Self::NAME)?;
9022 write!(f, "{}: {}", "block_hash", self.block_hash())?;
9023 write!(f, ", {}: {}", "proposals", self.proposals())?;
9024 let extra_count = self.count_extra_fields();
9025 if extra_count != 0 {
9026 write!(f, ", .. ({} fields)", extra_count)?;
9027 }
9028 write!(f, " }}")
9029 }
9030}
9031impl ::core::default::Default for GetBlockProposal {
9032 fn default() -> Self {
9033 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9034 GetBlockProposal::new_unchecked(v)
9035 }
9036}
9037impl GetBlockProposal {
9038 const DEFAULT_VALUE: [u8; 48] = [
9039 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,
9040 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9041 ];
9042 pub const FIELD_COUNT: usize = 2;
9043 pub fn total_size(&self) -> usize {
9044 molecule::unpack_number(self.as_slice()) as usize
9045 }
9046 pub fn field_count(&self) -> usize {
9047 if self.total_size() == molecule::NUMBER_SIZE {
9048 0
9049 } else {
9050 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9051 }
9052 }
9053 pub fn count_extra_fields(&self) -> usize {
9054 self.field_count() - Self::FIELD_COUNT
9055 }
9056 pub fn has_extra_fields(&self) -> bool {
9057 Self::FIELD_COUNT != self.field_count()
9058 }
9059 pub fn block_hash(&self) -> Byte32 {
9060 let slice = self.as_slice();
9061 let start = molecule::unpack_number(&slice[4..]) as usize;
9062 let end = molecule::unpack_number(&slice[8..]) as usize;
9063 Byte32::new_unchecked(self.0.slice(start..end))
9064 }
9065 pub fn proposals(&self) -> ProposalShortIdVec {
9066 let slice = self.as_slice();
9067 let start = molecule::unpack_number(&slice[8..]) as usize;
9068 if self.has_extra_fields() {
9069 let end = molecule::unpack_number(&slice[12..]) as usize;
9070 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
9071 } else {
9072 ProposalShortIdVec::new_unchecked(self.0.slice(start..))
9073 }
9074 }
9075 pub fn as_reader<'r>(&'r self) -> GetBlockProposalReader<'r> {
9076 GetBlockProposalReader::new_unchecked(self.as_slice())
9077 }
9078}
9079impl molecule::prelude::Entity for GetBlockProposal {
9080 type Builder = GetBlockProposalBuilder;
9081 const NAME: &'static str = "GetBlockProposal";
9082 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9083 GetBlockProposal(data)
9084 }
9085 fn as_bytes(&self) -> molecule::bytes::Bytes {
9086 self.0.clone()
9087 }
9088 fn as_slice(&self) -> &[u8] {
9089 &self.0[..]
9090 }
9091 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9092 GetBlockProposalReader::from_slice(slice).map(|reader| reader.to_entity())
9093 }
9094 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9095 GetBlockProposalReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9096 }
9097 fn new_builder() -> Self::Builder {
9098 ::core::default::Default::default()
9099 }
9100 fn as_builder(self) -> Self::Builder {
9101 Self::new_builder()
9102 .block_hash(self.block_hash())
9103 .proposals(self.proposals())
9104 }
9105}
9106#[derive(Clone, Copy)]
9107pub struct GetBlockProposalReader<'r>(&'r [u8]);
9108impl<'r> ::core::fmt::LowerHex for GetBlockProposalReader<'r> {
9109 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9110 use molecule::hex_string;
9111 if f.alternate() {
9112 write!(f, "0x")?;
9113 }
9114 write!(f, "{}", hex_string(self.as_slice()))
9115 }
9116}
9117impl<'r> ::core::fmt::Debug for GetBlockProposalReader<'r> {
9118 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9119 write!(f, "{}({:#x})", Self::NAME, self)
9120 }
9121}
9122impl<'r> ::core::fmt::Display for GetBlockProposalReader<'r> {
9123 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9124 write!(f, "{} {{ ", Self::NAME)?;
9125 write!(f, "{}: {}", "block_hash", self.block_hash())?;
9126 write!(f, ", {}: {}", "proposals", self.proposals())?;
9127 let extra_count = self.count_extra_fields();
9128 if extra_count != 0 {
9129 write!(f, ", .. ({} fields)", extra_count)?;
9130 }
9131 write!(f, " }}")
9132 }
9133}
9134impl<'r> GetBlockProposalReader<'r> {
9135 pub const FIELD_COUNT: usize = 2;
9136 pub fn total_size(&self) -> usize {
9137 molecule::unpack_number(self.as_slice()) as usize
9138 }
9139 pub fn field_count(&self) -> usize {
9140 if self.total_size() == molecule::NUMBER_SIZE {
9141 0
9142 } else {
9143 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9144 }
9145 }
9146 pub fn count_extra_fields(&self) -> usize {
9147 self.field_count() - Self::FIELD_COUNT
9148 }
9149 pub fn has_extra_fields(&self) -> bool {
9150 Self::FIELD_COUNT != self.field_count()
9151 }
9152 pub fn block_hash(&self) -> Byte32Reader<'r> {
9153 let slice = self.as_slice();
9154 let start = molecule::unpack_number(&slice[4..]) as usize;
9155 let end = molecule::unpack_number(&slice[8..]) as usize;
9156 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
9157 }
9158 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
9159 let slice = self.as_slice();
9160 let start = molecule::unpack_number(&slice[8..]) as usize;
9161 if self.has_extra_fields() {
9162 let end = molecule::unpack_number(&slice[12..]) as usize;
9163 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
9164 } else {
9165 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..])
9166 }
9167 }
9168}
9169impl<'r> molecule::prelude::Reader<'r> for GetBlockProposalReader<'r> {
9170 type Entity = GetBlockProposal;
9171 const NAME: &'static str = "GetBlockProposalReader";
9172 fn to_entity(&self) -> Self::Entity {
9173 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9174 }
9175 fn new_unchecked(slice: &'r [u8]) -> Self {
9176 GetBlockProposalReader(slice)
9177 }
9178 fn as_slice(&self) -> &'r [u8] {
9179 self.0
9180 }
9181 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9182 use molecule::verification_error as ve;
9183 let slice_len = slice.len();
9184 if slice_len < molecule::NUMBER_SIZE {
9185 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
9186 }
9187 let total_size = molecule::unpack_number(slice) as usize;
9188 if slice_len != total_size {
9189 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
9190 }
9191 if slice_len < molecule::NUMBER_SIZE * 2 {
9192 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
9193 }
9194 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
9195 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
9196 return ve!(Self, OffsetsNotMatch);
9197 }
9198 if slice_len < offset_first {
9199 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
9200 }
9201 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
9202 if field_count < Self::FIELD_COUNT {
9203 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9204 } else if !compatible && field_count > Self::FIELD_COUNT {
9205 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9206 };
9207 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
9208 .chunks_exact(molecule::NUMBER_SIZE)
9209 .map(|x| molecule::unpack_number(x) as usize)
9210 .collect();
9211 offsets.push(total_size);
9212 if offsets.windows(2).any(|i| i[0] > i[1]) {
9213 return ve!(Self, OffsetsNotMatch);
9214 }
9215 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
9216 ProposalShortIdVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
9217 Ok(())
9218 }
9219}
9220#[derive(Debug, Default)]
9221pub struct GetBlockProposalBuilder {
9222 pub(crate) block_hash: Byte32,
9223 pub(crate) proposals: ProposalShortIdVec,
9224}
9225impl GetBlockProposalBuilder {
9226 pub const FIELD_COUNT: usize = 2;
9227 pub fn block_hash(mut self, v: Byte32) -> Self {
9228 self.block_hash = v;
9229 self
9230 }
9231 pub fn proposals(mut self, v: ProposalShortIdVec) -> Self {
9232 self.proposals = v;
9233 self
9234 }
9235}
9236impl molecule::prelude::Builder for GetBlockProposalBuilder {
9237 type Entity = GetBlockProposal;
9238 const NAME: &'static str = "GetBlockProposalBuilder";
9239 fn expected_length(&self) -> usize {
9240 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
9241 + self.block_hash.as_slice().len()
9242 + self.proposals.as_slice().len()
9243 }
9244 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
9245 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
9246 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
9247 offsets.push(total_size);
9248 total_size += self.block_hash.as_slice().len();
9249 offsets.push(total_size);
9250 total_size += self.proposals.as_slice().len();
9251 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
9252 for offset in offsets.into_iter() {
9253 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
9254 }
9255 writer.write_all(self.block_hash.as_slice())?;
9256 writer.write_all(self.proposals.as_slice())?;
9257 Ok(())
9258 }
9259 fn build(&self) -> Self::Entity {
9260 let mut inner = Vec::with_capacity(self.expected_length());
9261 self.write(&mut inner)
9262 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9263 GetBlockProposal::new_unchecked(inner.into())
9264 }
9265}
9266#[derive(Clone)]
9267pub struct BlockProposal(molecule::bytes::Bytes);
9268impl ::core::fmt::LowerHex for BlockProposal {
9269 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9270 use molecule::hex_string;
9271 if f.alternate() {
9272 write!(f, "0x")?;
9273 }
9274 write!(f, "{}", hex_string(self.as_slice()))
9275 }
9276}
9277impl ::core::fmt::Debug for BlockProposal {
9278 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9279 write!(f, "{}({:#x})", Self::NAME, self)
9280 }
9281}
9282impl ::core::fmt::Display for BlockProposal {
9283 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9284 write!(f, "{} {{ ", Self::NAME)?;
9285 write!(f, "{}: {}", "transactions", self.transactions())?;
9286 let extra_count = self.count_extra_fields();
9287 if extra_count != 0 {
9288 write!(f, ", .. ({} fields)", extra_count)?;
9289 }
9290 write!(f, " }}")
9291 }
9292}
9293impl ::core::default::Default for BlockProposal {
9294 fn default() -> Self {
9295 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9296 BlockProposal::new_unchecked(v)
9297 }
9298}
9299impl BlockProposal {
9300 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 4, 0, 0, 0];
9301 pub const FIELD_COUNT: usize = 1;
9302 pub fn total_size(&self) -> usize {
9303 molecule::unpack_number(self.as_slice()) as usize
9304 }
9305 pub fn field_count(&self) -> usize {
9306 if self.total_size() == molecule::NUMBER_SIZE {
9307 0
9308 } else {
9309 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9310 }
9311 }
9312 pub fn count_extra_fields(&self) -> usize {
9313 self.field_count() - Self::FIELD_COUNT
9314 }
9315 pub fn has_extra_fields(&self) -> bool {
9316 Self::FIELD_COUNT != self.field_count()
9317 }
9318 pub fn transactions(&self) -> TransactionVec {
9319 let slice = self.as_slice();
9320 let start = molecule::unpack_number(&slice[4..]) as usize;
9321 if self.has_extra_fields() {
9322 let end = molecule::unpack_number(&slice[8..]) as usize;
9323 TransactionVec::new_unchecked(self.0.slice(start..end))
9324 } else {
9325 TransactionVec::new_unchecked(self.0.slice(start..))
9326 }
9327 }
9328 pub fn as_reader<'r>(&'r self) -> BlockProposalReader<'r> {
9329 BlockProposalReader::new_unchecked(self.as_slice())
9330 }
9331}
9332impl molecule::prelude::Entity for BlockProposal {
9333 type Builder = BlockProposalBuilder;
9334 const NAME: &'static str = "BlockProposal";
9335 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9336 BlockProposal(data)
9337 }
9338 fn as_bytes(&self) -> molecule::bytes::Bytes {
9339 self.0.clone()
9340 }
9341 fn as_slice(&self) -> &[u8] {
9342 &self.0[..]
9343 }
9344 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9345 BlockProposalReader::from_slice(slice).map(|reader| reader.to_entity())
9346 }
9347 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9348 BlockProposalReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9349 }
9350 fn new_builder() -> Self::Builder {
9351 ::core::default::Default::default()
9352 }
9353 fn as_builder(self) -> Self::Builder {
9354 Self::new_builder().transactions(self.transactions())
9355 }
9356}
9357#[derive(Clone, Copy)]
9358pub struct BlockProposalReader<'r>(&'r [u8]);
9359impl<'r> ::core::fmt::LowerHex for BlockProposalReader<'r> {
9360 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9361 use molecule::hex_string;
9362 if f.alternate() {
9363 write!(f, "0x")?;
9364 }
9365 write!(f, "{}", hex_string(self.as_slice()))
9366 }
9367}
9368impl<'r> ::core::fmt::Debug for BlockProposalReader<'r> {
9369 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9370 write!(f, "{}({:#x})", Self::NAME, self)
9371 }
9372}
9373impl<'r> ::core::fmt::Display for BlockProposalReader<'r> {
9374 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9375 write!(f, "{} {{ ", Self::NAME)?;
9376 write!(f, "{}: {}", "transactions", self.transactions())?;
9377 let extra_count = self.count_extra_fields();
9378 if extra_count != 0 {
9379 write!(f, ", .. ({} fields)", extra_count)?;
9380 }
9381 write!(f, " }}")
9382 }
9383}
9384impl<'r> BlockProposalReader<'r> {
9385 pub const FIELD_COUNT: usize = 1;
9386 pub fn total_size(&self) -> usize {
9387 molecule::unpack_number(self.as_slice()) as usize
9388 }
9389 pub fn field_count(&self) -> usize {
9390 if self.total_size() == molecule::NUMBER_SIZE {
9391 0
9392 } else {
9393 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9394 }
9395 }
9396 pub fn count_extra_fields(&self) -> usize {
9397 self.field_count() - Self::FIELD_COUNT
9398 }
9399 pub fn has_extra_fields(&self) -> bool {
9400 Self::FIELD_COUNT != self.field_count()
9401 }
9402 pub fn transactions(&self) -> TransactionVecReader<'r> {
9403 let slice = self.as_slice();
9404 let start = molecule::unpack_number(&slice[4..]) as usize;
9405 if self.has_extra_fields() {
9406 let end = molecule::unpack_number(&slice[8..]) as usize;
9407 TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
9408 } else {
9409 TransactionVecReader::new_unchecked(&self.as_slice()[start..])
9410 }
9411 }
9412}
9413impl<'r> molecule::prelude::Reader<'r> for BlockProposalReader<'r> {
9414 type Entity = BlockProposal;
9415 const NAME: &'static str = "BlockProposalReader";
9416 fn to_entity(&self) -> Self::Entity {
9417 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9418 }
9419 fn new_unchecked(slice: &'r [u8]) -> Self {
9420 BlockProposalReader(slice)
9421 }
9422 fn as_slice(&self) -> &'r [u8] {
9423 self.0
9424 }
9425 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9426 use molecule::verification_error as ve;
9427 let slice_len = slice.len();
9428 if slice_len < molecule::NUMBER_SIZE {
9429 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
9430 }
9431 let total_size = molecule::unpack_number(slice) as usize;
9432 if slice_len != total_size {
9433 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
9434 }
9435 if slice_len < molecule::NUMBER_SIZE * 2 {
9436 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
9437 }
9438 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
9439 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
9440 return ve!(Self, OffsetsNotMatch);
9441 }
9442 if slice_len < offset_first {
9443 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
9444 }
9445 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
9446 if field_count < Self::FIELD_COUNT {
9447 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9448 } else if !compatible && field_count > Self::FIELD_COUNT {
9449 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9450 };
9451 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
9452 .chunks_exact(molecule::NUMBER_SIZE)
9453 .map(|x| molecule::unpack_number(x) as usize)
9454 .collect();
9455 offsets.push(total_size);
9456 if offsets.windows(2).any(|i| i[0] > i[1]) {
9457 return ve!(Self, OffsetsNotMatch);
9458 }
9459 TransactionVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
9460 Ok(())
9461 }
9462}
9463#[derive(Debug, Default)]
9464pub struct BlockProposalBuilder {
9465 pub(crate) transactions: TransactionVec,
9466}
9467impl BlockProposalBuilder {
9468 pub const FIELD_COUNT: usize = 1;
9469 pub fn transactions(mut self, v: TransactionVec) -> Self {
9470 self.transactions = v;
9471 self
9472 }
9473}
9474impl molecule::prelude::Builder for BlockProposalBuilder {
9475 type Entity = BlockProposal;
9476 const NAME: &'static str = "BlockProposalBuilder";
9477 fn expected_length(&self) -> usize {
9478 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.transactions.as_slice().len()
9479 }
9480 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
9481 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
9482 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
9483 offsets.push(total_size);
9484 total_size += self.transactions.as_slice().len();
9485 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
9486 for offset in offsets.into_iter() {
9487 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
9488 }
9489 writer.write_all(self.transactions.as_slice())?;
9490 Ok(())
9491 }
9492 fn build(&self) -> Self::Entity {
9493 let mut inner = Vec::with_capacity(self.expected_length());
9494 self.write(&mut inner)
9495 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9496 BlockProposal::new_unchecked(inner.into())
9497 }
9498}
9499#[derive(Clone)]
9500pub struct IndexTransaction(molecule::bytes::Bytes);
9501impl ::core::fmt::LowerHex for IndexTransaction {
9502 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9503 use molecule::hex_string;
9504 if f.alternate() {
9505 write!(f, "0x")?;
9506 }
9507 write!(f, "{}", hex_string(self.as_slice()))
9508 }
9509}
9510impl ::core::fmt::Debug for IndexTransaction {
9511 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9512 write!(f, "{}({:#x})", Self::NAME, self)
9513 }
9514}
9515impl ::core::fmt::Display for IndexTransaction {
9516 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9517 write!(f, "{} {{ ", Self::NAME)?;
9518 write!(f, "{}: {}", "index", self.index())?;
9519 write!(f, ", {}: {}", "transaction", self.transaction())?;
9520 let extra_count = self.count_extra_fields();
9521 if extra_count != 0 {
9522 write!(f, ", .. ({} fields)", extra_count)?;
9523 }
9524 write!(f, " }}")
9525 }
9526}
9527impl ::core::default::Default for IndexTransaction {
9528 fn default() -> Self {
9529 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9530 IndexTransaction::new_unchecked(v)
9531 }
9532}
9533impl IndexTransaction {
9534 const DEFAULT_VALUE: [u8; 84] = [
9535 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,
9536 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,
9537 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,
9538 ];
9539 pub const FIELD_COUNT: usize = 2;
9540 pub fn total_size(&self) -> usize {
9541 molecule::unpack_number(self.as_slice()) as usize
9542 }
9543 pub fn field_count(&self) -> usize {
9544 if self.total_size() == molecule::NUMBER_SIZE {
9545 0
9546 } else {
9547 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9548 }
9549 }
9550 pub fn count_extra_fields(&self) -> usize {
9551 self.field_count() - Self::FIELD_COUNT
9552 }
9553 pub fn has_extra_fields(&self) -> bool {
9554 Self::FIELD_COUNT != self.field_count()
9555 }
9556 pub fn index(&self) -> Uint32 {
9557 let slice = self.as_slice();
9558 let start = molecule::unpack_number(&slice[4..]) as usize;
9559 let end = molecule::unpack_number(&slice[8..]) as usize;
9560 Uint32::new_unchecked(self.0.slice(start..end))
9561 }
9562 pub fn transaction(&self) -> Transaction {
9563 let slice = self.as_slice();
9564 let start = molecule::unpack_number(&slice[8..]) as usize;
9565 if self.has_extra_fields() {
9566 let end = molecule::unpack_number(&slice[12..]) as usize;
9567 Transaction::new_unchecked(self.0.slice(start..end))
9568 } else {
9569 Transaction::new_unchecked(self.0.slice(start..))
9570 }
9571 }
9572 pub fn as_reader<'r>(&'r self) -> IndexTransactionReader<'r> {
9573 IndexTransactionReader::new_unchecked(self.as_slice())
9574 }
9575}
9576impl molecule::prelude::Entity for IndexTransaction {
9577 type Builder = IndexTransactionBuilder;
9578 const NAME: &'static str = "IndexTransaction";
9579 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9580 IndexTransaction(data)
9581 }
9582 fn as_bytes(&self) -> molecule::bytes::Bytes {
9583 self.0.clone()
9584 }
9585 fn as_slice(&self) -> &[u8] {
9586 &self.0[..]
9587 }
9588 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9589 IndexTransactionReader::from_slice(slice).map(|reader| reader.to_entity())
9590 }
9591 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9592 IndexTransactionReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9593 }
9594 fn new_builder() -> Self::Builder {
9595 ::core::default::Default::default()
9596 }
9597 fn as_builder(self) -> Self::Builder {
9598 Self::new_builder()
9599 .index(self.index())
9600 .transaction(self.transaction())
9601 }
9602}
9603#[derive(Clone, Copy)]
9604pub struct IndexTransactionReader<'r>(&'r [u8]);
9605impl<'r> ::core::fmt::LowerHex for IndexTransactionReader<'r> {
9606 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9607 use molecule::hex_string;
9608 if f.alternate() {
9609 write!(f, "0x")?;
9610 }
9611 write!(f, "{}", hex_string(self.as_slice()))
9612 }
9613}
9614impl<'r> ::core::fmt::Debug for IndexTransactionReader<'r> {
9615 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9616 write!(f, "{}({:#x})", Self::NAME, self)
9617 }
9618}
9619impl<'r> ::core::fmt::Display for IndexTransactionReader<'r> {
9620 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9621 write!(f, "{} {{ ", Self::NAME)?;
9622 write!(f, "{}: {}", "index", self.index())?;
9623 write!(f, ", {}: {}", "transaction", self.transaction())?;
9624 let extra_count = self.count_extra_fields();
9625 if extra_count != 0 {
9626 write!(f, ", .. ({} fields)", extra_count)?;
9627 }
9628 write!(f, " }}")
9629 }
9630}
9631impl<'r> IndexTransactionReader<'r> {
9632 pub const FIELD_COUNT: usize = 2;
9633 pub fn total_size(&self) -> usize {
9634 molecule::unpack_number(self.as_slice()) as usize
9635 }
9636 pub fn field_count(&self) -> usize {
9637 if self.total_size() == molecule::NUMBER_SIZE {
9638 0
9639 } else {
9640 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9641 }
9642 }
9643 pub fn count_extra_fields(&self) -> usize {
9644 self.field_count() - Self::FIELD_COUNT
9645 }
9646 pub fn has_extra_fields(&self) -> bool {
9647 Self::FIELD_COUNT != self.field_count()
9648 }
9649 pub fn index(&self) -> Uint32Reader<'r> {
9650 let slice = self.as_slice();
9651 let start = molecule::unpack_number(&slice[4..]) as usize;
9652 let end = molecule::unpack_number(&slice[8..]) as usize;
9653 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
9654 }
9655 pub fn transaction(&self) -> TransactionReader<'r> {
9656 let slice = self.as_slice();
9657 let start = molecule::unpack_number(&slice[8..]) as usize;
9658 if self.has_extra_fields() {
9659 let end = molecule::unpack_number(&slice[12..]) as usize;
9660 TransactionReader::new_unchecked(&self.as_slice()[start..end])
9661 } else {
9662 TransactionReader::new_unchecked(&self.as_slice()[start..])
9663 }
9664 }
9665}
9666impl<'r> molecule::prelude::Reader<'r> for IndexTransactionReader<'r> {
9667 type Entity = IndexTransaction;
9668 const NAME: &'static str = "IndexTransactionReader";
9669 fn to_entity(&self) -> Self::Entity {
9670 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9671 }
9672 fn new_unchecked(slice: &'r [u8]) -> Self {
9673 IndexTransactionReader(slice)
9674 }
9675 fn as_slice(&self) -> &'r [u8] {
9676 self.0
9677 }
9678 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9679 use molecule::verification_error as ve;
9680 let slice_len = slice.len();
9681 if slice_len < molecule::NUMBER_SIZE {
9682 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
9683 }
9684 let total_size = molecule::unpack_number(slice) as usize;
9685 if slice_len != total_size {
9686 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
9687 }
9688 if slice_len < molecule::NUMBER_SIZE * 2 {
9689 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
9690 }
9691 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
9692 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
9693 return ve!(Self, OffsetsNotMatch);
9694 }
9695 if slice_len < offset_first {
9696 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
9697 }
9698 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
9699 if field_count < Self::FIELD_COUNT {
9700 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9701 } else if !compatible && field_count > Self::FIELD_COUNT {
9702 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9703 };
9704 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
9705 .chunks_exact(molecule::NUMBER_SIZE)
9706 .map(|x| molecule::unpack_number(x) as usize)
9707 .collect();
9708 offsets.push(total_size);
9709 if offsets.windows(2).any(|i| i[0] > i[1]) {
9710 return ve!(Self, OffsetsNotMatch);
9711 }
9712 Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
9713 TransactionReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
9714 Ok(())
9715 }
9716}
9717#[derive(Debug, Default)]
9718pub struct IndexTransactionBuilder {
9719 pub(crate) index: Uint32,
9720 pub(crate) transaction: Transaction,
9721}
9722impl IndexTransactionBuilder {
9723 pub const FIELD_COUNT: usize = 2;
9724 pub fn index(mut self, v: Uint32) -> Self {
9725 self.index = v;
9726 self
9727 }
9728 pub fn transaction(mut self, v: Transaction) -> Self {
9729 self.transaction = v;
9730 self
9731 }
9732}
9733impl molecule::prelude::Builder for IndexTransactionBuilder {
9734 type Entity = IndexTransaction;
9735 const NAME: &'static str = "IndexTransactionBuilder";
9736 fn expected_length(&self) -> usize {
9737 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
9738 + self.index.as_slice().len()
9739 + self.transaction.as_slice().len()
9740 }
9741 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
9742 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
9743 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
9744 offsets.push(total_size);
9745 total_size += self.index.as_slice().len();
9746 offsets.push(total_size);
9747 total_size += self.transaction.as_slice().len();
9748 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
9749 for offset in offsets.into_iter() {
9750 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
9751 }
9752 writer.write_all(self.index.as_slice())?;
9753 writer.write_all(self.transaction.as_slice())?;
9754 Ok(())
9755 }
9756 fn build(&self) -> Self::Entity {
9757 let mut inner = Vec::with_capacity(self.expected_length());
9758 self.write(&mut inner)
9759 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9760 IndexTransaction::new_unchecked(inner.into())
9761 }
9762}
9763#[derive(Clone)]
9764pub struct IndexTransactionVec(molecule::bytes::Bytes);
9765impl ::core::fmt::LowerHex for IndexTransactionVec {
9766 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9767 use molecule::hex_string;
9768 if f.alternate() {
9769 write!(f, "0x")?;
9770 }
9771 write!(f, "{}", hex_string(self.as_slice()))
9772 }
9773}
9774impl ::core::fmt::Debug for IndexTransactionVec {
9775 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9776 write!(f, "{}({:#x})", Self::NAME, self)
9777 }
9778}
9779impl ::core::fmt::Display for IndexTransactionVec {
9780 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9781 write!(f, "{} [", Self::NAME)?;
9782 for i in 0..self.len() {
9783 if i == 0 {
9784 write!(f, "{}", self.get_unchecked(i))?;
9785 } else {
9786 write!(f, ", {}", self.get_unchecked(i))?;
9787 }
9788 }
9789 write!(f, "]")
9790 }
9791}
9792impl ::core::default::Default for IndexTransactionVec {
9793 fn default() -> Self {
9794 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9795 IndexTransactionVec::new_unchecked(v)
9796 }
9797}
9798impl IndexTransactionVec {
9799 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
9800 pub fn total_size(&self) -> usize {
9801 molecule::unpack_number(self.as_slice()) as usize
9802 }
9803 pub fn item_count(&self) -> usize {
9804 if self.total_size() == molecule::NUMBER_SIZE {
9805 0
9806 } else {
9807 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9808 }
9809 }
9810 pub fn len(&self) -> usize {
9811 self.item_count()
9812 }
9813 pub fn is_empty(&self) -> bool {
9814 self.len() == 0
9815 }
9816 pub fn get(&self, idx: usize) -> Option<IndexTransaction> {
9817 if idx >= self.len() {
9818 None
9819 } else {
9820 Some(self.get_unchecked(idx))
9821 }
9822 }
9823 pub fn get_unchecked(&self, idx: usize) -> IndexTransaction {
9824 let slice = self.as_slice();
9825 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
9826 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
9827 if idx == self.len() - 1 {
9828 IndexTransaction::new_unchecked(self.0.slice(start..))
9829 } else {
9830 let end_idx = start_idx + molecule::NUMBER_SIZE;
9831 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
9832 IndexTransaction::new_unchecked(self.0.slice(start..end))
9833 }
9834 }
9835 pub fn as_reader<'r>(&'r self) -> IndexTransactionVecReader<'r> {
9836 IndexTransactionVecReader::new_unchecked(self.as_slice())
9837 }
9838}
9839impl molecule::prelude::Entity for IndexTransactionVec {
9840 type Builder = IndexTransactionVecBuilder;
9841 const NAME: &'static str = "IndexTransactionVec";
9842 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9843 IndexTransactionVec(data)
9844 }
9845 fn as_bytes(&self) -> molecule::bytes::Bytes {
9846 self.0.clone()
9847 }
9848 fn as_slice(&self) -> &[u8] {
9849 &self.0[..]
9850 }
9851 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9852 IndexTransactionVecReader::from_slice(slice).map(|reader| reader.to_entity())
9853 }
9854 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9855 IndexTransactionVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9856 }
9857 fn new_builder() -> Self::Builder {
9858 ::core::default::Default::default()
9859 }
9860 fn as_builder(self) -> Self::Builder {
9861 Self::new_builder().extend(self.into_iter())
9862 }
9863}
9864#[derive(Clone, Copy)]
9865pub struct IndexTransactionVecReader<'r>(&'r [u8]);
9866impl<'r> ::core::fmt::LowerHex for IndexTransactionVecReader<'r> {
9867 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9868 use molecule::hex_string;
9869 if f.alternate() {
9870 write!(f, "0x")?;
9871 }
9872 write!(f, "{}", hex_string(self.as_slice()))
9873 }
9874}
9875impl<'r> ::core::fmt::Debug for IndexTransactionVecReader<'r> {
9876 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9877 write!(f, "{}({:#x})", Self::NAME, self)
9878 }
9879}
9880impl<'r> ::core::fmt::Display for IndexTransactionVecReader<'r> {
9881 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9882 write!(f, "{} [", Self::NAME)?;
9883 for i in 0..self.len() {
9884 if i == 0 {
9885 write!(f, "{}", self.get_unchecked(i))?;
9886 } else {
9887 write!(f, ", {}", self.get_unchecked(i))?;
9888 }
9889 }
9890 write!(f, "]")
9891 }
9892}
9893impl<'r> IndexTransactionVecReader<'r> {
9894 pub fn total_size(&self) -> usize {
9895 molecule::unpack_number(self.as_slice()) as usize
9896 }
9897 pub fn item_count(&self) -> usize {
9898 if self.total_size() == molecule::NUMBER_SIZE {
9899 0
9900 } else {
9901 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9902 }
9903 }
9904 pub fn len(&self) -> usize {
9905 self.item_count()
9906 }
9907 pub fn is_empty(&self) -> bool {
9908 self.len() == 0
9909 }
9910 pub fn get(&self, idx: usize) -> Option<IndexTransactionReader<'r>> {
9911 if idx >= self.len() {
9912 None
9913 } else {
9914 Some(self.get_unchecked(idx))
9915 }
9916 }
9917 pub fn get_unchecked(&self, idx: usize) -> IndexTransactionReader<'r> {
9918 let slice = self.as_slice();
9919 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
9920 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
9921 if idx == self.len() - 1 {
9922 IndexTransactionReader::new_unchecked(&self.as_slice()[start..])
9923 } else {
9924 let end_idx = start_idx + molecule::NUMBER_SIZE;
9925 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
9926 IndexTransactionReader::new_unchecked(&self.as_slice()[start..end])
9927 }
9928 }
9929}
9930impl<'r> molecule::prelude::Reader<'r> for IndexTransactionVecReader<'r> {
9931 type Entity = IndexTransactionVec;
9932 const NAME: &'static str = "IndexTransactionVecReader";
9933 fn to_entity(&self) -> Self::Entity {
9934 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9935 }
9936 fn new_unchecked(slice: &'r [u8]) -> Self {
9937 IndexTransactionVecReader(slice)
9938 }
9939 fn as_slice(&self) -> &'r [u8] {
9940 self.0
9941 }
9942 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9943 use molecule::verification_error as ve;
9944 let slice_len = slice.len();
9945 if slice_len < molecule::NUMBER_SIZE {
9946 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
9947 }
9948 let total_size = molecule::unpack_number(slice) as usize;
9949 if slice_len != total_size {
9950 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
9951 }
9952 if slice_len == molecule::NUMBER_SIZE {
9953 return Ok(());
9954 }
9955 if slice_len < molecule::NUMBER_SIZE * 2 {
9956 return ve!(
9957 Self,
9958 TotalSizeNotMatch,
9959 molecule::NUMBER_SIZE * 2,
9960 slice_len
9961 );
9962 }
9963 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
9964 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
9965 return ve!(Self, OffsetsNotMatch);
9966 }
9967 if slice_len < offset_first {
9968 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
9969 }
9970 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
9971 .chunks_exact(molecule::NUMBER_SIZE)
9972 .map(|x| molecule::unpack_number(x) as usize)
9973 .collect();
9974 offsets.push(total_size);
9975 if offsets.windows(2).any(|i| i[0] > i[1]) {
9976 return ve!(Self, OffsetsNotMatch);
9977 }
9978 for pair in offsets.windows(2) {
9979 let start = pair[0];
9980 let end = pair[1];
9981 IndexTransactionReader::verify(&slice[start..end], compatible)?;
9982 }
9983 Ok(())
9984 }
9985}
9986#[derive(Debug, Default)]
9987pub struct IndexTransactionVecBuilder(pub(crate) Vec<IndexTransaction>);
9988impl IndexTransactionVecBuilder {
9989 pub fn set(mut self, v: Vec<IndexTransaction>) -> Self {
9990 self.0 = v;
9991 self
9992 }
9993 pub fn push(mut self, v: IndexTransaction) -> Self {
9994 self.0.push(v);
9995 self
9996 }
9997 pub fn extend<T: ::core::iter::IntoIterator<Item = IndexTransaction>>(
9998 mut self,
9999 iter: T,
10000 ) -> Self {
10001 for elem in iter {
10002 self.0.push(elem);
10003 }
10004 self
10005 }
10006 pub fn replace(&mut self, index: usize, v: IndexTransaction) -> Option<IndexTransaction> {
10007 self.0
10008 .get_mut(index)
10009 .map(|item| ::core::mem::replace(item, v))
10010 }
10011}
10012impl molecule::prelude::Builder for IndexTransactionVecBuilder {
10013 type Entity = IndexTransactionVec;
10014 const NAME: &'static str = "IndexTransactionVecBuilder";
10015 fn expected_length(&self) -> usize {
10016 molecule::NUMBER_SIZE * (self.0.len() + 1)
10017 + self
10018 .0
10019 .iter()
10020 .map(|inner| inner.as_slice().len())
10021 .sum::<usize>()
10022 }
10023 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
10024 let item_count = self.0.len();
10025 if item_count == 0 {
10026 writer.write_all(&molecule::pack_number(
10027 molecule::NUMBER_SIZE as molecule::Number,
10028 ))?;
10029 } else {
10030 let (total_size, offsets) = self.0.iter().fold(
10031 (
10032 molecule::NUMBER_SIZE * (item_count + 1),
10033 Vec::with_capacity(item_count),
10034 ),
10035 |(start, mut offsets), inner| {
10036 offsets.push(start);
10037 (start + inner.as_slice().len(), offsets)
10038 },
10039 );
10040 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
10041 for offset in offsets.into_iter() {
10042 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
10043 }
10044 for inner in self.0.iter() {
10045 writer.write_all(inner.as_slice())?;
10046 }
10047 }
10048 Ok(())
10049 }
10050 fn build(&self) -> Self::Entity {
10051 let mut inner = Vec::with_capacity(self.expected_length());
10052 self.write(&mut inner)
10053 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
10054 IndexTransactionVec::new_unchecked(inner.into())
10055 }
10056}
10057pub struct IndexTransactionVecIterator(IndexTransactionVec, usize, usize);
10058impl ::core::iter::Iterator for IndexTransactionVecIterator {
10059 type Item = IndexTransaction;
10060 fn next(&mut self) -> Option<Self::Item> {
10061 if self.1 >= self.2 {
10062 None
10063 } else {
10064 let ret = self.0.get_unchecked(self.1);
10065 self.1 += 1;
10066 Some(ret)
10067 }
10068 }
10069}
10070impl ::core::iter::ExactSizeIterator for IndexTransactionVecIterator {
10071 fn len(&self) -> usize {
10072 self.2 - self.1
10073 }
10074}
10075impl ::core::iter::IntoIterator for IndexTransactionVec {
10076 type Item = IndexTransaction;
10077 type IntoIter = IndexTransactionVecIterator;
10078 fn into_iter(self) -> Self::IntoIter {
10079 let len = self.len();
10080 IndexTransactionVecIterator(self, 0, len)
10081 }
10082}
10083impl<'r> IndexTransactionVecReader<'r> {
10084 pub fn iter<'t>(&'t self) -> IndexTransactionVecReaderIterator<'t, 'r> {
10085 IndexTransactionVecReaderIterator(&self, 0, self.len())
10086 }
10087}
10088pub struct IndexTransactionVecReaderIterator<'t, 'r>(
10089 &'t IndexTransactionVecReader<'r>,
10090 usize,
10091 usize,
10092);
10093impl<'t: 'r, 'r> ::core::iter::Iterator for IndexTransactionVecReaderIterator<'t, 'r> {
10094 type Item = IndexTransactionReader<'t>;
10095 fn next(&mut self) -> Option<Self::Item> {
10096 if self.1 >= self.2 {
10097 None
10098 } else {
10099 let ret = self.0.get_unchecked(self.1);
10100 self.1 += 1;
10101 Some(ret)
10102 }
10103 }
10104}
10105impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for IndexTransactionVecReaderIterator<'t, 'r> {
10106 fn len(&self) -> usize {
10107 self.2 - self.1
10108 }
10109}
10110#[derive(Clone)]
10111pub struct BlockFilterMessage(molecule::bytes::Bytes);
10112impl ::core::fmt::LowerHex for BlockFilterMessage {
10113 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10114 use molecule::hex_string;
10115 if f.alternate() {
10116 write!(f, "0x")?;
10117 }
10118 write!(f, "{}", hex_string(self.as_slice()))
10119 }
10120}
10121impl ::core::fmt::Debug for BlockFilterMessage {
10122 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10123 write!(f, "{}({:#x})", Self::NAME, self)
10124 }
10125}
10126impl ::core::fmt::Display for BlockFilterMessage {
10127 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10128 write!(f, "{}(", Self::NAME)?;
10129 self.to_enum().display_inner(f)?;
10130 write!(f, ")")
10131 }
10132}
10133impl ::core::default::Default for BlockFilterMessage {
10134 fn default() -> Self {
10135 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
10136 BlockFilterMessage::new_unchecked(v)
10137 }
10138}
10139impl BlockFilterMessage {
10140 const DEFAULT_VALUE: [u8; 12] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
10141 pub const ITEMS_COUNT: usize = 6;
10142 pub fn item_id(&self) -> molecule::Number {
10143 molecule::unpack_number(self.as_slice())
10144 }
10145 pub fn to_enum(&self) -> BlockFilterMessageUnion {
10146 let inner = self.0.slice(molecule::NUMBER_SIZE..);
10147 match self.item_id() {
10148 0 => GetBlockFilters::new_unchecked(inner).into(),
10149 1 => BlockFilters::new_unchecked(inner).into(),
10150 2 => GetBlockFilterHashes::new_unchecked(inner).into(),
10151 3 => BlockFilterHashes::new_unchecked(inner).into(),
10152 4 => GetBlockFilterCheckPoints::new_unchecked(inner).into(),
10153 5 => BlockFilterCheckPoints::new_unchecked(inner).into(),
10154 _ => panic!("{}: invalid data", Self::NAME),
10155 }
10156 }
10157 pub fn as_reader<'r>(&'r self) -> BlockFilterMessageReader<'r> {
10158 BlockFilterMessageReader::new_unchecked(self.as_slice())
10159 }
10160}
10161impl molecule::prelude::Entity for BlockFilterMessage {
10162 type Builder = BlockFilterMessageBuilder;
10163 const NAME: &'static str = "BlockFilterMessage";
10164 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
10165 BlockFilterMessage(data)
10166 }
10167 fn as_bytes(&self) -> molecule::bytes::Bytes {
10168 self.0.clone()
10169 }
10170 fn as_slice(&self) -> &[u8] {
10171 &self.0[..]
10172 }
10173 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10174 BlockFilterMessageReader::from_slice(slice).map(|reader| reader.to_entity())
10175 }
10176 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10177 BlockFilterMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
10178 }
10179 fn new_builder() -> Self::Builder {
10180 ::core::default::Default::default()
10181 }
10182 fn as_builder(self) -> Self::Builder {
10183 Self::new_builder().set(self.to_enum())
10184 }
10185}
10186#[derive(Clone, Copy)]
10187pub struct BlockFilterMessageReader<'r>(&'r [u8]);
10188impl<'r> ::core::fmt::LowerHex for BlockFilterMessageReader<'r> {
10189 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10190 use molecule::hex_string;
10191 if f.alternate() {
10192 write!(f, "0x")?;
10193 }
10194 write!(f, "{}", hex_string(self.as_slice()))
10195 }
10196}
10197impl<'r> ::core::fmt::Debug for BlockFilterMessageReader<'r> {
10198 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10199 write!(f, "{}({:#x})", Self::NAME, self)
10200 }
10201}
10202impl<'r> ::core::fmt::Display for BlockFilterMessageReader<'r> {
10203 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10204 write!(f, "{}(", Self::NAME)?;
10205 self.to_enum().display_inner(f)?;
10206 write!(f, ")")
10207 }
10208}
10209impl<'r> BlockFilterMessageReader<'r> {
10210 pub const ITEMS_COUNT: usize = 6;
10211 pub fn item_id(&self) -> molecule::Number {
10212 molecule::unpack_number(self.as_slice())
10213 }
10214 pub fn to_enum(&self) -> BlockFilterMessageUnionReader<'r> {
10215 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
10216 match self.item_id() {
10217 0 => GetBlockFiltersReader::new_unchecked(inner).into(),
10218 1 => BlockFiltersReader::new_unchecked(inner).into(),
10219 2 => GetBlockFilterHashesReader::new_unchecked(inner).into(),
10220 3 => BlockFilterHashesReader::new_unchecked(inner).into(),
10221 4 => GetBlockFilterCheckPointsReader::new_unchecked(inner).into(),
10222 5 => BlockFilterCheckPointsReader::new_unchecked(inner).into(),
10223 _ => panic!("{}: invalid data", Self::NAME),
10224 }
10225 }
10226}
10227impl<'r> molecule::prelude::Reader<'r> for BlockFilterMessageReader<'r> {
10228 type Entity = BlockFilterMessage;
10229 const NAME: &'static str = "BlockFilterMessageReader";
10230 fn to_entity(&self) -> Self::Entity {
10231 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
10232 }
10233 fn new_unchecked(slice: &'r [u8]) -> Self {
10234 BlockFilterMessageReader(slice)
10235 }
10236 fn as_slice(&self) -> &'r [u8] {
10237 self.0
10238 }
10239 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
10240 use molecule::verification_error as ve;
10241 let slice_len = slice.len();
10242 if slice_len < molecule::NUMBER_SIZE {
10243 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
10244 }
10245 let item_id = molecule::unpack_number(slice);
10246 let inner_slice = &slice[molecule::NUMBER_SIZE..];
10247 match item_id {
10248 0 => GetBlockFiltersReader::verify(inner_slice, compatible),
10249 1 => BlockFiltersReader::verify(inner_slice, compatible),
10250 2 => GetBlockFilterHashesReader::verify(inner_slice, compatible),
10251 3 => BlockFilterHashesReader::verify(inner_slice, compatible),
10252 4 => GetBlockFilterCheckPointsReader::verify(inner_slice, compatible),
10253 5 => BlockFilterCheckPointsReader::verify(inner_slice, compatible),
10254 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
10255 }?;
10256 Ok(())
10257 }
10258}
10259#[derive(Debug, Default)]
10260pub struct BlockFilterMessageBuilder(pub(crate) BlockFilterMessageUnion);
10261impl BlockFilterMessageBuilder {
10262 pub const ITEMS_COUNT: usize = 6;
10263 pub fn set<I>(mut self, v: I) -> Self
10264 where
10265 I: ::core::convert::Into<BlockFilterMessageUnion>,
10266 {
10267 self.0 = v.into();
10268 self
10269 }
10270}
10271impl molecule::prelude::Builder for BlockFilterMessageBuilder {
10272 type Entity = BlockFilterMessage;
10273 const NAME: &'static str = "BlockFilterMessageBuilder";
10274 fn expected_length(&self) -> usize {
10275 molecule::NUMBER_SIZE + self.0.as_slice().len()
10276 }
10277 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
10278 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
10279 writer.write_all(self.0.as_slice())
10280 }
10281 fn build(&self) -> Self::Entity {
10282 let mut inner = Vec::with_capacity(self.expected_length());
10283 self.write(&mut inner)
10284 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
10285 BlockFilterMessage::new_unchecked(inner.into())
10286 }
10287}
10288#[derive(Debug, Clone)]
10289pub enum BlockFilterMessageUnion {
10290 GetBlockFilters(GetBlockFilters),
10291 BlockFilters(BlockFilters),
10292 GetBlockFilterHashes(GetBlockFilterHashes),
10293 BlockFilterHashes(BlockFilterHashes),
10294 GetBlockFilterCheckPoints(GetBlockFilterCheckPoints),
10295 BlockFilterCheckPoints(BlockFilterCheckPoints),
10296}
10297#[derive(Debug, Clone, Copy)]
10298pub enum BlockFilterMessageUnionReader<'r> {
10299 GetBlockFilters(GetBlockFiltersReader<'r>),
10300 BlockFilters(BlockFiltersReader<'r>),
10301 GetBlockFilterHashes(GetBlockFilterHashesReader<'r>),
10302 BlockFilterHashes(BlockFilterHashesReader<'r>),
10303 GetBlockFilterCheckPoints(GetBlockFilterCheckPointsReader<'r>),
10304 BlockFilterCheckPoints(BlockFilterCheckPointsReader<'r>),
10305}
10306impl ::core::default::Default for BlockFilterMessageUnion {
10307 fn default() -> Self {
10308 BlockFilterMessageUnion::GetBlockFilters(::core::default::Default::default())
10309 }
10310}
10311impl ::core::fmt::Display for BlockFilterMessageUnion {
10312 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10313 match self {
10314 BlockFilterMessageUnion::GetBlockFilters(ref item) => {
10315 write!(f, "{}::{}({})", Self::NAME, GetBlockFilters::NAME, item)
10316 }
10317 BlockFilterMessageUnion::BlockFilters(ref item) => {
10318 write!(f, "{}::{}({})", Self::NAME, BlockFilters::NAME, item)
10319 }
10320 BlockFilterMessageUnion::GetBlockFilterHashes(ref item) => {
10321 write!(
10322 f,
10323 "{}::{}({})",
10324 Self::NAME,
10325 GetBlockFilterHashes::NAME,
10326 item
10327 )
10328 }
10329 BlockFilterMessageUnion::BlockFilterHashes(ref item) => {
10330 write!(f, "{}::{}({})", Self::NAME, BlockFilterHashes::NAME, item)
10331 }
10332 BlockFilterMessageUnion::GetBlockFilterCheckPoints(ref item) => {
10333 write!(
10334 f,
10335 "{}::{}({})",
10336 Self::NAME,
10337 GetBlockFilterCheckPoints::NAME,
10338 item
10339 )
10340 }
10341 BlockFilterMessageUnion::BlockFilterCheckPoints(ref item) => {
10342 write!(
10343 f,
10344 "{}::{}({})",
10345 Self::NAME,
10346 BlockFilterCheckPoints::NAME,
10347 item
10348 )
10349 }
10350 }
10351 }
10352}
10353impl<'r> ::core::fmt::Display for BlockFilterMessageUnionReader<'r> {
10354 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10355 match self {
10356 BlockFilterMessageUnionReader::GetBlockFilters(ref item) => {
10357 write!(f, "{}::{}({})", Self::NAME, GetBlockFilters::NAME, item)
10358 }
10359 BlockFilterMessageUnionReader::BlockFilters(ref item) => {
10360 write!(f, "{}::{}({})", Self::NAME, BlockFilters::NAME, item)
10361 }
10362 BlockFilterMessageUnionReader::GetBlockFilterHashes(ref item) => {
10363 write!(
10364 f,
10365 "{}::{}({})",
10366 Self::NAME,
10367 GetBlockFilterHashes::NAME,
10368 item
10369 )
10370 }
10371 BlockFilterMessageUnionReader::BlockFilterHashes(ref item) => {
10372 write!(f, "{}::{}({})", Self::NAME, BlockFilterHashes::NAME, item)
10373 }
10374 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(ref item) => {
10375 write!(
10376 f,
10377 "{}::{}({})",
10378 Self::NAME,
10379 GetBlockFilterCheckPoints::NAME,
10380 item
10381 )
10382 }
10383 BlockFilterMessageUnionReader::BlockFilterCheckPoints(ref item) => {
10384 write!(
10385 f,
10386 "{}::{}({})",
10387 Self::NAME,
10388 BlockFilterCheckPoints::NAME,
10389 item
10390 )
10391 }
10392 }
10393 }
10394}
10395impl BlockFilterMessageUnion {
10396 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10397 match self {
10398 BlockFilterMessageUnion::GetBlockFilters(ref item) => write!(f, "{}", item),
10399 BlockFilterMessageUnion::BlockFilters(ref item) => write!(f, "{}", item),
10400 BlockFilterMessageUnion::GetBlockFilterHashes(ref item) => write!(f, "{}", item),
10401 BlockFilterMessageUnion::BlockFilterHashes(ref item) => write!(f, "{}", item),
10402 BlockFilterMessageUnion::GetBlockFilterCheckPoints(ref item) => write!(f, "{}", item),
10403 BlockFilterMessageUnion::BlockFilterCheckPoints(ref item) => write!(f, "{}", item),
10404 }
10405 }
10406}
10407impl<'r> BlockFilterMessageUnionReader<'r> {
10408 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10409 match self {
10410 BlockFilterMessageUnionReader::GetBlockFilters(ref item) => write!(f, "{}", item),
10411 BlockFilterMessageUnionReader::BlockFilters(ref item) => write!(f, "{}", item),
10412 BlockFilterMessageUnionReader::GetBlockFilterHashes(ref item) => write!(f, "{}", item),
10413 BlockFilterMessageUnionReader::BlockFilterHashes(ref item) => write!(f, "{}", item),
10414 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(ref item) => {
10415 write!(f, "{}", item)
10416 }
10417 BlockFilterMessageUnionReader::BlockFilterCheckPoints(ref item) => {
10418 write!(f, "{}", item)
10419 }
10420 }
10421 }
10422}
10423impl ::core::convert::From<GetBlockFilters> for BlockFilterMessageUnion {
10424 fn from(item: GetBlockFilters) -> Self {
10425 BlockFilterMessageUnion::GetBlockFilters(item)
10426 }
10427}
10428impl ::core::convert::From<BlockFilters> for BlockFilterMessageUnion {
10429 fn from(item: BlockFilters) -> Self {
10430 BlockFilterMessageUnion::BlockFilters(item)
10431 }
10432}
10433impl ::core::convert::From<GetBlockFilterHashes> for BlockFilterMessageUnion {
10434 fn from(item: GetBlockFilterHashes) -> Self {
10435 BlockFilterMessageUnion::GetBlockFilterHashes(item)
10436 }
10437}
10438impl ::core::convert::From<BlockFilterHashes> for BlockFilterMessageUnion {
10439 fn from(item: BlockFilterHashes) -> Self {
10440 BlockFilterMessageUnion::BlockFilterHashes(item)
10441 }
10442}
10443impl ::core::convert::From<GetBlockFilterCheckPoints> for BlockFilterMessageUnion {
10444 fn from(item: GetBlockFilterCheckPoints) -> Self {
10445 BlockFilterMessageUnion::GetBlockFilterCheckPoints(item)
10446 }
10447}
10448impl ::core::convert::From<BlockFilterCheckPoints> for BlockFilterMessageUnion {
10449 fn from(item: BlockFilterCheckPoints) -> Self {
10450 BlockFilterMessageUnion::BlockFilterCheckPoints(item)
10451 }
10452}
10453impl<'r> ::core::convert::From<GetBlockFiltersReader<'r>> for BlockFilterMessageUnionReader<'r> {
10454 fn from(item: GetBlockFiltersReader<'r>) -> Self {
10455 BlockFilterMessageUnionReader::GetBlockFilters(item)
10456 }
10457}
10458impl<'r> ::core::convert::From<BlockFiltersReader<'r>> for BlockFilterMessageUnionReader<'r> {
10459 fn from(item: BlockFiltersReader<'r>) -> Self {
10460 BlockFilterMessageUnionReader::BlockFilters(item)
10461 }
10462}
10463impl<'r> ::core::convert::From<GetBlockFilterHashesReader<'r>>
10464 for BlockFilterMessageUnionReader<'r>
10465{
10466 fn from(item: GetBlockFilterHashesReader<'r>) -> Self {
10467 BlockFilterMessageUnionReader::GetBlockFilterHashes(item)
10468 }
10469}
10470impl<'r> ::core::convert::From<BlockFilterHashesReader<'r>> for BlockFilterMessageUnionReader<'r> {
10471 fn from(item: BlockFilterHashesReader<'r>) -> Self {
10472 BlockFilterMessageUnionReader::BlockFilterHashes(item)
10473 }
10474}
10475impl<'r> ::core::convert::From<GetBlockFilterCheckPointsReader<'r>>
10476 for BlockFilterMessageUnionReader<'r>
10477{
10478 fn from(item: GetBlockFilterCheckPointsReader<'r>) -> Self {
10479 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(item)
10480 }
10481}
10482impl<'r> ::core::convert::From<BlockFilterCheckPointsReader<'r>>
10483 for BlockFilterMessageUnionReader<'r>
10484{
10485 fn from(item: BlockFilterCheckPointsReader<'r>) -> Self {
10486 BlockFilterMessageUnionReader::BlockFilterCheckPoints(item)
10487 }
10488}
10489impl BlockFilterMessageUnion {
10490 pub const NAME: &'static str = "BlockFilterMessageUnion";
10491 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
10492 match self {
10493 BlockFilterMessageUnion::GetBlockFilters(item) => item.as_bytes(),
10494 BlockFilterMessageUnion::BlockFilters(item) => item.as_bytes(),
10495 BlockFilterMessageUnion::GetBlockFilterHashes(item) => item.as_bytes(),
10496 BlockFilterMessageUnion::BlockFilterHashes(item) => item.as_bytes(),
10497 BlockFilterMessageUnion::GetBlockFilterCheckPoints(item) => item.as_bytes(),
10498 BlockFilterMessageUnion::BlockFilterCheckPoints(item) => item.as_bytes(),
10499 }
10500 }
10501 pub fn as_slice(&self) -> &[u8] {
10502 match self {
10503 BlockFilterMessageUnion::GetBlockFilters(item) => item.as_slice(),
10504 BlockFilterMessageUnion::BlockFilters(item) => item.as_slice(),
10505 BlockFilterMessageUnion::GetBlockFilterHashes(item) => item.as_slice(),
10506 BlockFilterMessageUnion::BlockFilterHashes(item) => item.as_slice(),
10507 BlockFilterMessageUnion::GetBlockFilterCheckPoints(item) => item.as_slice(),
10508 BlockFilterMessageUnion::BlockFilterCheckPoints(item) => item.as_slice(),
10509 }
10510 }
10511 pub fn item_id(&self) -> molecule::Number {
10512 match self {
10513 BlockFilterMessageUnion::GetBlockFilters(_) => 0,
10514 BlockFilterMessageUnion::BlockFilters(_) => 1,
10515 BlockFilterMessageUnion::GetBlockFilterHashes(_) => 2,
10516 BlockFilterMessageUnion::BlockFilterHashes(_) => 3,
10517 BlockFilterMessageUnion::GetBlockFilterCheckPoints(_) => 4,
10518 BlockFilterMessageUnion::BlockFilterCheckPoints(_) => 5,
10519 }
10520 }
10521 pub fn item_name(&self) -> &str {
10522 match self {
10523 BlockFilterMessageUnion::GetBlockFilters(_) => "GetBlockFilters",
10524 BlockFilterMessageUnion::BlockFilters(_) => "BlockFilters",
10525 BlockFilterMessageUnion::GetBlockFilterHashes(_) => "GetBlockFilterHashes",
10526 BlockFilterMessageUnion::BlockFilterHashes(_) => "BlockFilterHashes",
10527 BlockFilterMessageUnion::GetBlockFilterCheckPoints(_) => "GetBlockFilterCheckPoints",
10528 BlockFilterMessageUnion::BlockFilterCheckPoints(_) => "BlockFilterCheckPoints",
10529 }
10530 }
10531 pub fn as_reader<'r>(&'r self) -> BlockFilterMessageUnionReader<'r> {
10532 match self {
10533 BlockFilterMessageUnion::GetBlockFilters(item) => item.as_reader().into(),
10534 BlockFilterMessageUnion::BlockFilters(item) => item.as_reader().into(),
10535 BlockFilterMessageUnion::GetBlockFilterHashes(item) => item.as_reader().into(),
10536 BlockFilterMessageUnion::BlockFilterHashes(item) => item.as_reader().into(),
10537 BlockFilterMessageUnion::GetBlockFilterCheckPoints(item) => item.as_reader().into(),
10538 BlockFilterMessageUnion::BlockFilterCheckPoints(item) => item.as_reader().into(),
10539 }
10540 }
10541}
10542impl<'r> BlockFilterMessageUnionReader<'r> {
10543 pub const NAME: &'r str = "BlockFilterMessageUnionReader";
10544 pub fn as_slice(&self) -> &'r [u8] {
10545 match self {
10546 BlockFilterMessageUnionReader::GetBlockFilters(item) => item.as_slice(),
10547 BlockFilterMessageUnionReader::BlockFilters(item) => item.as_slice(),
10548 BlockFilterMessageUnionReader::GetBlockFilterHashes(item) => item.as_slice(),
10549 BlockFilterMessageUnionReader::BlockFilterHashes(item) => item.as_slice(),
10550 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(item) => item.as_slice(),
10551 BlockFilterMessageUnionReader::BlockFilterCheckPoints(item) => item.as_slice(),
10552 }
10553 }
10554 pub fn item_id(&self) -> molecule::Number {
10555 match self {
10556 BlockFilterMessageUnionReader::GetBlockFilters(_) => 0,
10557 BlockFilterMessageUnionReader::BlockFilters(_) => 1,
10558 BlockFilterMessageUnionReader::GetBlockFilterHashes(_) => 2,
10559 BlockFilterMessageUnionReader::BlockFilterHashes(_) => 3,
10560 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(_) => 4,
10561 BlockFilterMessageUnionReader::BlockFilterCheckPoints(_) => 5,
10562 }
10563 }
10564 pub fn item_name(&self) -> &str {
10565 match self {
10566 BlockFilterMessageUnionReader::GetBlockFilters(_) => "GetBlockFilters",
10567 BlockFilterMessageUnionReader::BlockFilters(_) => "BlockFilters",
10568 BlockFilterMessageUnionReader::GetBlockFilterHashes(_) => "GetBlockFilterHashes",
10569 BlockFilterMessageUnionReader::BlockFilterHashes(_) => "BlockFilterHashes",
10570 BlockFilterMessageUnionReader::GetBlockFilterCheckPoints(_) => {
10571 "GetBlockFilterCheckPoints"
10572 }
10573 BlockFilterMessageUnionReader::BlockFilterCheckPoints(_) => "BlockFilterCheckPoints",
10574 }
10575 }
10576}
10577#[derive(Clone)]
10578pub struct GetBlockFilters(molecule::bytes::Bytes);
10579impl ::core::fmt::LowerHex for GetBlockFilters {
10580 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10581 use molecule::hex_string;
10582 if f.alternate() {
10583 write!(f, "0x")?;
10584 }
10585 write!(f, "{}", hex_string(self.as_slice()))
10586 }
10587}
10588impl ::core::fmt::Debug for GetBlockFilters {
10589 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10590 write!(f, "{}({:#x})", Self::NAME, self)
10591 }
10592}
10593impl ::core::fmt::Display for GetBlockFilters {
10594 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10595 write!(f, "{} {{ ", Self::NAME)?;
10596 write!(f, "{}: {}", "start_number", self.start_number())?;
10597 write!(f, " }}")
10598 }
10599}
10600impl ::core::default::Default for GetBlockFilters {
10601 fn default() -> Self {
10602 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
10603 GetBlockFilters::new_unchecked(v)
10604 }
10605}
10606impl GetBlockFilters {
10607 const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
10608 pub const TOTAL_SIZE: usize = 8;
10609 pub const FIELD_SIZES: [usize; 1] = [8];
10610 pub const FIELD_COUNT: usize = 1;
10611 pub fn start_number(&self) -> Uint64 {
10612 Uint64::new_unchecked(self.0.slice(0..8))
10613 }
10614 pub fn as_reader<'r>(&'r self) -> GetBlockFiltersReader<'r> {
10615 GetBlockFiltersReader::new_unchecked(self.as_slice())
10616 }
10617}
10618impl molecule::prelude::Entity for GetBlockFilters {
10619 type Builder = GetBlockFiltersBuilder;
10620 const NAME: &'static str = "GetBlockFilters";
10621 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
10622 GetBlockFilters(data)
10623 }
10624 fn as_bytes(&self) -> molecule::bytes::Bytes {
10625 self.0.clone()
10626 }
10627 fn as_slice(&self) -> &[u8] {
10628 &self.0[..]
10629 }
10630 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10631 GetBlockFiltersReader::from_slice(slice).map(|reader| reader.to_entity())
10632 }
10633 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10634 GetBlockFiltersReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
10635 }
10636 fn new_builder() -> Self::Builder {
10637 ::core::default::Default::default()
10638 }
10639 fn as_builder(self) -> Self::Builder {
10640 Self::new_builder().start_number(self.start_number())
10641 }
10642}
10643#[derive(Clone, Copy)]
10644pub struct GetBlockFiltersReader<'r>(&'r [u8]);
10645impl<'r> ::core::fmt::LowerHex for GetBlockFiltersReader<'r> {
10646 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10647 use molecule::hex_string;
10648 if f.alternate() {
10649 write!(f, "0x")?;
10650 }
10651 write!(f, "{}", hex_string(self.as_slice()))
10652 }
10653}
10654impl<'r> ::core::fmt::Debug for GetBlockFiltersReader<'r> {
10655 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10656 write!(f, "{}({:#x})", Self::NAME, self)
10657 }
10658}
10659impl<'r> ::core::fmt::Display for GetBlockFiltersReader<'r> {
10660 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10661 write!(f, "{} {{ ", Self::NAME)?;
10662 write!(f, "{}: {}", "start_number", self.start_number())?;
10663 write!(f, " }}")
10664 }
10665}
10666impl<'r> GetBlockFiltersReader<'r> {
10667 pub const TOTAL_SIZE: usize = 8;
10668 pub const FIELD_SIZES: [usize; 1] = [8];
10669 pub const FIELD_COUNT: usize = 1;
10670 pub fn start_number(&self) -> Uint64Reader<'r> {
10671 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
10672 }
10673}
10674impl<'r> molecule::prelude::Reader<'r> for GetBlockFiltersReader<'r> {
10675 type Entity = GetBlockFilters;
10676 const NAME: &'static str = "GetBlockFiltersReader";
10677 fn to_entity(&self) -> Self::Entity {
10678 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
10679 }
10680 fn new_unchecked(slice: &'r [u8]) -> Self {
10681 GetBlockFiltersReader(slice)
10682 }
10683 fn as_slice(&self) -> &'r [u8] {
10684 self.0
10685 }
10686 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
10687 use molecule::verification_error as ve;
10688 let slice_len = slice.len();
10689 if slice_len != Self::TOTAL_SIZE {
10690 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
10691 }
10692 Ok(())
10693 }
10694}
10695#[derive(Debug, Default)]
10696pub struct GetBlockFiltersBuilder {
10697 pub(crate) start_number: Uint64,
10698}
10699impl GetBlockFiltersBuilder {
10700 pub const TOTAL_SIZE: usize = 8;
10701 pub const FIELD_SIZES: [usize; 1] = [8];
10702 pub const FIELD_COUNT: usize = 1;
10703 pub fn start_number(mut self, v: Uint64) -> Self {
10704 self.start_number = v;
10705 self
10706 }
10707}
10708impl molecule::prelude::Builder for GetBlockFiltersBuilder {
10709 type Entity = GetBlockFilters;
10710 const NAME: &'static str = "GetBlockFiltersBuilder";
10711 fn expected_length(&self) -> usize {
10712 Self::TOTAL_SIZE
10713 }
10714 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
10715 writer.write_all(self.start_number.as_slice())?;
10716 Ok(())
10717 }
10718 fn build(&self) -> Self::Entity {
10719 let mut inner = Vec::with_capacity(self.expected_length());
10720 self.write(&mut inner)
10721 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
10722 GetBlockFilters::new_unchecked(inner.into())
10723 }
10724}
10725#[derive(Clone)]
10726pub struct BlockFilters(molecule::bytes::Bytes);
10727impl ::core::fmt::LowerHex for BlockFilters {
10728 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10729 use molecule::hex_string;
10730 if f.alternate() {
10731 write!(f, "0x")?;
10732 }
10733 write!(f, "{}", hex_string(self.as_slice()))
10734 }
10735}
10736impl ::core::fmt::Debug for BlockFilters {
10737 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10738 write!(f, "{}({:#x})", Self::NAME, self)
10739 }
10740}
10741impl ::core::fmt::Display for BlockFilters {
10742 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10743 write!(f, "{} {{ ", Self::NAME)?;
10744 write!(f, "{}: {}", "start_number", self.start_number())?;
10745 write!(f, ", {}: {}", "block_hashes", self.block_hashes())?;
10746 write!(f, ", {}: {}", "filters", self.filters())?;
10747 let extra_count = self.count_extra_fields();
10748 if extra_count != 0 {
10749 write!(f, ", .. ({} fields)", extra_count)?;
10750 }
10751 write!(f, " }}")
10752 }
10753}
10754impl ::core::default::Default for BlockFilters {
10755 fn default() -> Self {
10756 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
10757 BlockFilters::new_unchecked(v)
10758 }
10759}
10760impl BlockFilters {
10761 const DEFAULT_VALUE: [u8; 32] = [
10762 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,
10763 0, 0, 0,
10764 ];
10765 pub const FIELD_COUNT: usize = 3;
10766 pub fn total_size(&self) -> usize {
10767 molecule::unpack_number(self.as_slice()) as usize
10768 }
10769 pub fn field_count(&self) -> usize {
10770 if self.total_size() == molecule::NUMBER_SIZE {
10771 0
10772 } else {
10773 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
10774 }
10775 }
10776 pub fn count_extra_fields(&self) -> usize {
10777 self.field_count() - Self::FIELD_COUNT
10778 }
10779 pub fn has_extra_fields(&self) -> bool {
10780 Self::FIELD_COUNT != self.field_count()
10781 }
10782 pub fn start_number(&self) -> Uint64 {
10783 let slice = self.as_slice();
10784 let start = molecule::unpack_number(&slice[4..]) as usize;
10785 let end = molecule::unpack_number(&slice[8..]) as usize;
10786 Uint64::new_unchecked(self.0.slice(start..end))
10787 }
10788 pub fn block_hashes(&self) -> Byte32Vec {
10789 let slice = self.as_slice();
10790 let start = molecule::unpack_number(&slice[8..]) as usize;
10791 let end = molecule::unpack_number(&slice[12..]) as usize;
10792 Byte32Vec::new_unchecked(self.0.slice(start..end))
10793 }
10794 pub fn filters(&self) -> BytesVec {
10795 let slice = self.as_slice();
10796 let start = molecule::unpack_number(&slice[12..]) as usize;
10797 if self.has_extra_fields() {
10798 let end = molecule::unpack_number(&slice[16..]) as usize;
10799 BytesVec::new_unchecked(self.0.slice(start..end))
10800 } else {
10801 BytesVec::new_unchecked(self.0.slice(start..))
10802 }
10803 }
10804 pub fn as_reader<'r>(&'r self) -> BlockFiltersReader<'r> {
10805 BlockFiltersReader::new_unchecked(self.as_slice())
10806 }
10807}
10808impl molecule::prelude::Entity for BlockFilters {
10809 type Builder = BlockFiltersBuilder;
10810 const NAME: &'static str = "BlockFilters";
10811 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
10812 BlockFilters(data)
10813 }
10814 fn as_bytes(&self) -> molecule::bytes::Bytes {
10815 self.0.clone()
10816 }
10817 fn as_slice(&self) -> &[u8] {
10818 &self.0[..]
10819 }
10820 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10821 BlockFiltersReader::from_slice(slice).map(|reader| reader.to_entity())
10822 }
10823 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10824 BlockFiltersReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
10825 }
10826 fn new_builder() -> Self::Builder {
10827 ::core::default::Default::default()
10828 }
10829 fn as_builder(self) -> Self::Builder {
10830 Self::new_builder()
10831 .start_number(self.start_number())
10832 .block_hashes(self.block_hashes())
10833 .filters(self.filters())
10834 }
10835}
10836#[derive(Clone, Copy)]
10837pub struct BlockFiltersReader<'r>(&'r [u8]);
10838impl<'r> ::core::fmt::LowerHex for BlockFiltersReader<'r> {
10839 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10840 use molecule::hex_string;
10841 if f.alternate() {
10842 write!(f, "0x")?;
10843 }
10844 write!(f, "{}", hex_string(self.as_slice()))
10845 }
10846}
10847impl<'r> ::core::fmt::Debug for BlockFiltersReader<'r> {
10848 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10849 write!(f, "{}({:#x})", Self::NAME, self)
10850 }
10851}
10852impl<'r> ::core::fmt::Display for BlockFiltersReader<'r> {
10853 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10854 write!(f, "{} {{ ", Self::NAME)?;
10855 write!(f, "{}: {}", "start_number", self.start_number())?;
10856 write!(f, ", {}: {}", "block_hashes", self.block_hashes())?;
10857 write!(f, ", {}: {}", "filters", self.filters())?;
10858 let extra_count = self.count_extra_fields();
10859 if extra_count != 0 {
10860 write!(f, ", .. ({} fields)", extra_count)?;
10861 }
10862 write!(f, " }}")
10863 }
10864}
10865impl<'r> BlockFiltersReader<'r> {
10866 pub const FIELD_COUNT: usize = 3;
10867 pub fn total_size(&self) -> usize {
10868 molecule::unpack_number(self.as_slice()) as usize
10869 }
10870 pub fn field_count(&self) -> usize {
10871 if self.total_size() == molecule::NUMBER_SIZE {
10872 0
10873 } else {
10874 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
10875 }
10876 }
10877 pub fn count_extra_fields(&self) -> usize {
10878 self.field_count() - Self::FIELD_COUNT
10879 }
10880 pub fn has_extra_fields(&self) -> bool {
10881 Self::FIELD_COUNT != self.field_count()
10882 }
10883 pub fn start_number(&self) -> Uint64Reader<'r> {
10884 let slice = self.as_slice();
10885 let start = molecule::unpack_number(&slice[4..]) as usize;
10886 let end = molecule::unpack_number(&slice[8..]) as usize;
10887 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
10888 }
10889 pub fn block_hashes(&self) -> Byte32VecReader<'r> {
10890 let slice = self.as_slice();
10891 let start = molecule::unpack_number(&slice[8..]) as usize;
10892 let end = molecule::unpack_number(&slice[12..]) as usize;
10893 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
10894 }
10895 pub fn filters(&self) -> BytesVecReader<'r> {
10896 let slice = self.as_slice();
10897 let start = molecule::unpack_number(&slice[12..]) as usize;
10898 if self.has_extra_fields() {
10899 let end = molecule::unpack_number(&slice[16..]) as usize;
10900 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
10901 } else {
10902 BytesVecReader::new_unchecked(&self.as_slice()[start..])
10903 }
10904 }
10905}
10906impl<'r> molecule::prelude::Reader<'r> for BlockFiltersReader<'r> {
10907 type Entity = BlockFilters;
10908 const NAME: &'static str = "BlockFiltersReader";
10909 fn to_entity(&self) -> Self::Entity {
10910 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
10911 }
10912 fn new_unchecked(slice: &'r [u8]) -> Self {
10913 BlockFiltersReader(slice)
10914 }
10915 fn as_slice(&self) -> &'r [u8] {
10916 self.0
10917 }
10918 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
10919 use molecule::verification_error as ve;
10920 let slice_len = slice.len();
10921 if slice_len < molecule::NUMBER_SIZE {
10922 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
10923 }
10924 let total_size = molecule::unpack_number(slice) as usize;
10925 if slice_len != total_size {
10926 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
10927 }
10928 if slice_len < molecule::NUMBER_SIZE * 2 {
10929 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
10930 }
10931 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
10932 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
10933 return ve!(Self, OffsetsNotMatch);
10934 }
10935 if slice_len < offset_first {
10936 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
10937 }
10938 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
10939 if field_count < Self::FIELD_COUNT {
10940 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
10941 } else if !compatible && field_count > Self::FIELD_COUNT {
10942 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
10943 };
10944 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
10945 .chunks_exact(molecule::NUMBER_SIZE)
10946 .map(|x| molecule::unpack_number(x) as usize)
10947 .collect();
10948 offsets.push(total_size);
10949 if offsets.windows(2).any(|i| i[0] > i[1]) {
10950 return ve!(Self, OffsetsNotMatch);
10951 }
10952 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
10953 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
10954 BytesVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
10955 Ok(())
10956 }
10957}
10958#[derive(Debug, Default)]
10959pub struct BlockFiltersBuilder {
10960 pub(crate) start_number: Uint64,
10961 pub(crate) block_hashes: Byte32Vec,
10962 pub(crate) filters: BytesVec,
10963}
10964impl BlockFiltersBuilder {
10965 pub const FIELD_COUNT: usize = 3;
10966 pub fn start_number(mut self, v: Uint64) -> Self {
10967 self.start_number = v;
10968 self
10969 }
10970 pub fn block_hashes(mut self, v: Byte32Vec) -> Self {
10971 self.block_hashes = v;
10972 self
10973 }
10974 pub fn filters(mut self, v: BytesVec) -> Self {
10975 self.filters = v;
10976 self
10977 }
10978}
10979impl molecule::prelude::Builder for BlockFiltersBuilder {
10980 type Entity = BlockFilters;
10981 const NAME: &'static str = "BlockFiltersBuilder";
10982 fn expected_length(&self) -> usize {
10983 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
10984 + self.start_number.as_slice().len()
10985 + self.block_hashes.as_slice().len()
10986 + self.filters.as_slice().len()
10987 }
10988 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
10989 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
10990 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
10991 offsets.push(total_size);
10992 total_size += self.start_number.as_slice().len();
10993 offsets.push(total_size);
10994 total_size += self.block_hashes.as_slice().len();
10995 offsets.push(total_size);
10996 total_size += self.filters.as_slice().len();
10997 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
10998 for offset in offsets.into_iter() {
10999 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
11000 }
11001 writer.write_all(self.start_number.as_slice())?;
11002 writer.write_all(self.block_hashes.as_slice())?;
11003 writer.write_all(self.filters.as_slice())?;
11004 Ok(())
11005 }
11006 fn build(&self) -> Self::Entity {
11007 let mut inner = Vec::with_capacity(self.expected_length());
11008 self.write(&mut inner)
11009 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
11010 BlockFilters::new_unchecked(inner.into())
11011 }
11012}
11013#[derive(Clone)]
11014pub struct GetBlockFilterHashes(molecule::bytes::Bytes);
11015impl ::core::fmt::LowerHex for GetBlockFilterHashes {
11016 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11017 use molecule::hex_string;
11018 if f.alternate() {
11019 write!(f, "0x")?;
11020 }
11021 write!(f, "{}", hex_string(self.as_slice()))
11022 }
11023}
11024impl ::core::fmt::Debug for GetBlockFilterHashes {
11025 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11026 write!(f, "{}({:#x})", Self::NAME, self)
11027 }
11028}
11029impl ::core::fmt::Display for GetBlockFilterHashes {
11030 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11031 write!(f, "{} {{ ", Self::NAME)?;
11032 write!(f, "{}: {}", "start_number", self.start_number())?;
11033 write!(f, " }}")
11034 }
11035}
11036impl ::core::default::Default for GetBlockFilterHashes {
11037 fn default() -> Self {
11038 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
11039 GetBlockFilterHashes::new_unchecked(v)
11040 }
11041}
11042impl GetBlockFilterHashes {
11043 const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
11044 pub const TOTAL_SIZE: usize = 8;
11045 pub const FIELD_SIZES: [usize; 1] = [8];
11046 pub const FIELD_COUNT: usize = 1;
11047 pub fn start_number(&self) -> Uint64 {
11048 Uint64::new_unchecked(self.0.slice(0..8))
11049 }
11050 pub fn as_reader<'r>(&'r self) -> GetBlockFilterHashesReader<'r> {
11051 GetBlockFilterHashesReader::new_unchecked(self.as_slice())
11052 }
11053}
11054impl molecule::prelude::Entity for GetBlockFilterHashes {
11055 type Builder = GetBlockFilterHashesBuilder;
11056 const NAME: &'static str = "GetBlockFilterHashes";
11057 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
11058 GetBlockFilterHashes(data)
11059 }
11060 fn as_bytes(&self) -> molecule::bytes::Bytes {
11061 self.0.clone()
11062 }
11063 fn as_slice(&self) -> &[u8] {
11064 &self.0[..]
11065 }
11066 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11067 GetBlockFilterHashesReader::from_slice(slice).map(|reader| reader.to_entity())
11068 }
11069 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11070 GetBlockFilterHashesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
11071 }
11072 fn new_builder() -> Self::Builder {
11073 ::core::default::Default::default()
11074 }
11075 fn as_builder(self) -> Self::Builder {
11076 Self::new_builder().start_number(self.start_number())
11077 }
11078}
11079#[derive(Clone, Copy)]
11080pub struct GetBlockFilterHashesReader<'r>(&'r [u8]);
11081impl<'r> ::core::fmt::LowerHex for GetBlockFilterHashesReader<'r> {
11082 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11083 use molecule::hex_string;
11084 if f.alternate() {
11085 write!(f, "0x")?;
11086 }
11087 write!(f, "{}", hex_string(self.as_slice()))
11088 }
11089}
11090impl<'r> ::core::fmt::Debug for GetBlockFilterHashesReader<'r> {
11091 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11092 write!(f, "{}({:#x})", Self::NAME, self)
11093 }
11094}
11095impl<'r> ::core::fmt::Display for GetBlockFilterHashesReader<'r> {
11096 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11097 write!(f, "{} {{ ", Self::NAME)?;
11098 write!(f, "{}: {}", "start_number", self.start_number())?;
11099 write!(f, " }}")
11100 }
11101}
11102impl<'r> GetBlockFilterHashesReader<'r> {
11103 pub const TOTAL_SIZE: usize = 8;
11104 pub const FIELD_SIZES: [usize; 1] = [8];
11105 pub const FIELD_COUNT: usize = 1;
11106 pub fn start_number(&self) -> Uint64Reader<'r> {
11107 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
11108 }
11109}
11110impl<'r> molecule::prelude::Reader<'r> for GetBlockFilterHashesReader<'r> {
11111 type Entity = GetBlockFilterHashes;
11112 const NAME: &'static str = "GetBlockFilterHashesReader";
11113 fn to_entity(&self) -> Self::Entity {
11114 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
11115 }
11116 fn new_unchecked(slice: &'r [u8]) -> Self {
11117 GetBlockFilterHashesReader(slice)
11118 }
11119 fn as_slice(&self) -> &'r [u8] {
11120 self.0
11121 }
11122 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
11123 use molecule::verification_error as ve;
11124 let slice_len = slice.len();
11125 if slice_len != Self::TOTAL_SIZE {
11126 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
11127 }
11128 Ok(())
11129 }
11130}
11131#[derive(Debug, Default)]
11132pub struct GetBlockFilterHashesBuilder {
11133 pub(crate) start_number: Uint64,
11134}
11135impl GetBlockFilterHashesBuilder {
11136 pub const TOTAL_SIZE: usize = 8;
11137 pub const FIELD_SIZES: [usize; 1] = [8];
11138 pub const FIELD_COUNT: usize = 1;
11139 pub fn start_number(mut self, v: Uint64) -> Self {
11140 self.start_number = v;
11141 self
11142 }
11143}
11144impl molecule::prelude::Builder for GetBlockFilterHashesBuilder {
11145 type Entity = GetBlockFilterHashes;
11146 const NAME: &'static str = "GetBlockFilterHashesBuilder";
11147 fn expected_length(&self) -> usize {
11148 Self::TOTAL_SIZE
11149 }
11150 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
11151 writer.write_all(self.start_number.as_slice())?;
11152 Ok(())
11153 }
11154 fn build(&self) -> Self::Entity {
11155 let mut inner = Vec::with_capacity(self.expected_length());
11156 self.write(&mut inner)
11157 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
11158 GetBlockFilterHashes::new_unchecked(inner.into())
11159 }
11160}
11161#[derive(Clone)]
11162pub struct BlockFilterHashes(molecule::bytes::Bytes);
11163impl ::core::fmt::LowerHex for BlockFilterHashes {
11164 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11165 use molecule::hex_string;
11166 if f.alternate() {
11167 write!(f, "0x")?;
11168 }
11169 write!(f, "{}", hex_string(self.as_slice()))
11170 }
11171}
11172impl ::core::fmt::Debug for BlockFilterHashes {
11173 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11174 write!(f, "{}({:#x})", Self::NAME, self)
11175 }
11176}
11177impl ::core::fmt::Display for BlockFilterHashes {
11178 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11179 write!(f, "{} {{ ", Self::NAME)?;
11180 write!(f, "{}: {}", "start_number", self.start_number())?;
11181 write!(
11182 f,
11183 ", {}: {}",
11184 "parent_block_filter_hash",
11185 self.parent_block_filter_hash()
11186 )?;
11187 write!(
11188 f,
11189 ", {}: {}",
11190 "block_filter_hashes",
11191 self.block_filter_hashes()
11192 )?;
11193 let extra_count = self.count_extra_fields();
11194 if extra_count != 0 {
11195 write!(f, ", .. ({} fields)", extra_count)?;
11196 }
11197 write!(f, " }}")
11198 }
11199}
11200impl ::core::default::Default for BlockFilterHashes {
11201 fn default() -> Self {
11202 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
11203 BlockFilterHashes::new_unchecked(v)
11204 }
11205}
11206impl BlockFilterHashes {
11207 const DEFAULT_VALUE: [u8; 60] = [
11208 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,
11209 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11210 0,
11211 ];
11212 pub const FIELD_COUNT: usize = 3;
11213 pub fn total_size(&self) -> usize {
11214 molecule::unpack_number(self.as_slice()) as usize
11215 }
11216 pub fn field_count(&self) -> usize {
11217 if self.total_size() == molecule::NUMBER_SIZE {
11218 0
11219 } else {
11220 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
11221 }
11222 }
11223 pub fn count_extra_fields(&self) -> usize {
11224 self.field_count() - Self::FIELD_COUNT
11225 }
11226 pub fn has_extra_fields(&self) -> bool {
11227 Self::FIELD_COUNT != self.field_count()
11228 }
11229 pub fn start_number(&self) -> Uint64 {
11230 let slice = self.as_slice();
11231 let start = molecule::unpack_number(&slice[4..]) as usize;
11232 let end = molecule::unpack_number(&slice[8..]) as usize;
11233 Uint64::new_unchecked(self.0.slice(start..end))
11234 }
11235 pub fn parent_block_filter_hash(&self) -> Byte32 {
11236 let slice = self.as_slice();
11237 let start = molecule::unpack_number(&slice[8..]) as usize;
11238 let end = molecule::unpack_number(&slice[12..]) as usize;
11239 Byte32::new_unchecked(self.0.slice(start..end))
11240 }
11241 pub fn block_filter_hashes(&self) -> Byte32Vec {
11242 let slice = self.as_slice();
11243 let start = molecule::unpack_number(&slice[12..]) as usize;
11244 if self.has_extra_fields() {
11245 let end = molecule::unpack_number(&slice[16..]) as usize;
11246 Byte32Vec::new_unchecked(self.0.slice(start..end))
11247 } else {
11248 Byte32Vec::new_unchecked(self.0.slice(start..))
11249 }
11250 }
11251 pub fn as_reader<'r>(&'r self) -> BlockFilterHashesReader<'r> {
11252 BlockFilterHashesReader::new_unchecked(self.as_slice())
11253 }
11254}
11255impl molecule::prelude::Entity for BlockFilterHashes {
11256 type Builder = BlockFilterHashesBuilder;
11257 const NAME: &'static str = "BlockFilterHashes";
11258 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
11259 BlockFilterHashes(data)
11260 }
11261 fn as_bytes(&self) -> molecule::bytes::Bytes {
11262 self.0.clone()
11263 }
11264 fn as_slice(&self) -> &[u8] {
11265 &self.0[..]
11266 }
11267 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11268 BlockFilterHashesReader::from_slice(slice).map(|reader| reader.to_entity())
11269 }
11270 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11271 BlockFilterHashesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
11272 }
11273 fn new_builder() -> Self::Builder {
11274 ::core::default::Default::default()
11275 }
11276 fn as_builder(self) -> Self::Builder {
11277 Self::new_builder()
11278 .start_number(self.start_number())
11279 .parent_block_filter_hash(self.parent_block_filter_hash())
11280 .block_filter_hashes(self.block_filter_hashes())
11281 }
11282}
11283#[derive(Clone, Copy)]
11284pub struct BlockFilterHashesReader<'r>(&'r [u8]);
11285impl<'r> ::core::fmt::LowerHex for BlockFilterHashesReader<'r> {
11286 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11287 use molecule::hex_string;
11288 if f.alternate() {
11289 write!(f, "0x")?;
11290 }
11291 write!(f, "{}", hex_string(self.as_slice()))
11292 }
11293}
11294impl<'r> ::core::fmt::Debug for BlockFilterHashesReader<'r> {
11295 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11296 write!(f, "{}({:#x})", Self::NAME, self)
11297 }
11298}
11299impl<'r> ::core::fmt::Display for BlockFilterHashesReader<'r> {
11300 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11301 write!(f, "{} {{ ", Self::NAME)?;
11302 write!(f, "{}: {}", "start_number", self.start_number())?;
11303 write!(
11304 f,
11305 ", {}: {}",
11306 "parent_block_filter_hash",
11307 self.parent_block_filter_hash()
11308 )?;
11309 write!(
11310 f,
11311 ", {}: {}",
11312 "block_filter_hashes",
11313 self.block_filter_hashes()
11314 )?;
11315 let extra_count = self.count_extra_fields();
11316 if extra_count != 0 {
11317 write!(f, ", .. ({} fields)", extra_count)?;
11318 }
11319 write!(f, " }}")
11320 }
11321}
11322impl<'r> BlockFilterHashesReader<'r> {
11323 pub const FIELD_COUNT: usize = 3;
11324 pub fn total_size(&self) -> usize {
11325 molecule::unpack_number(self.as_slice()) as usize
11326 }
11327 pub fn field_count(&self) -> usize {
11328 if self.total_size() == molecule::NUMBER_SIZE {
11329 0
11330 } else {
11331 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
11332 }
11333 }
11334 pub fn count_extra_fields(&self) -> usize {
11335 self.field_count() - Self::FIELD_COUNT
11336 }
11337 pub fn has_extra_fields(&self) -> bool {
11338 Self::FIELD_COUNT != self.field_count()
11339 }
11340 pub fn start_number(&self) -> Uint64Reader<'r> {
11341 let slice = self.as_slice();
11342 let start = molecule::unpack_number(&slice[4..]) as usize;
11343 let end = molecule::unpack_number(&slice[8..]) as usize;
11344 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
11345 }
11346 pub fn parent_block_filter_hash(&self) -> Byte32Reader<'r> {
11347 let slice = self.as_slice();
11348 let start = molecule::unpack_number(&slice[8..]) as usize;
11349 let end = molecule::unpack_number(&slice[12..]) as usize;
11350 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
11351 }
11352 pub fn block_filter_hashes(&self) -> Byte32VecReader<'r> {
11353 let slice = self.as_slice();
11354 let start = molecule::unpack_number(&slice[12..]) as usize;
11355 if self.has_extra_fields() {
11356 let end = molecule::unpack_number(&slice[16..]) as usize;
11357 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
11358 } else {
11359 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
11360 }
11361 }
11362}
11363impl<'r> molecule::prelude::Reader<'r> for BlockFilterHashesReader<'r> {
11364 type Entity = BlockFilterHashes;
11365 const NAME: &'static str = "BlockFilterHashesReader";
11366 fn to_entity(&self) -> Self::Entity {
11367 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
11368 }
11369 fn new_unchecked(slice: &'r [u8]) -> Self {
11370 BlockFilterHashesReader(slice)
11371 }
11372 fn as_slice(&self) -> &'r [u8] {
11373 self.0
11374 }
11375 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
11376 use molecule::verification_error as ve;
11377 let slice_len = slice.len();
11378 if slice_len < molecule::NUMBER_SIZE {
11379 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
11380 }
11381 let total_size = molecule::unpack_number(slice) as usize;
11382 if slice_len != total_size {
11383 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
11384 }
11385 if slice_len < molecule::NUMBER_SIZE * 2 {
11386 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
11387 }
11388 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
11389 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
11390 return ve!(Self, OffsetsNotMatch);
11391 }
11392 if slice_len < offset_first {
11393 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
11394 }
11395 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
11396 if field_count < Self::FIELD_COUNT {
11397 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
11398 } else if !compatible && field_count > Self::FIELD_COUNT {
11399 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
11400 };
11401 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
11402 .chunks_exact(molecule::NUMBER_SIZE)
11403 .map(|x| molecule::unpack_number(x) as usize)
11404 .collect();
11405 offsets.push(total_size);
11406 if offsets.windows(2).any(|i| i[0] > i[1]) {
11407 return ve!(Self, OffsetsNotMatch);
11408 }
11409 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
11410 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
11411 Byte32VecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
11412 Ok(())
11413 }
11414}
11415#[derive(Debug, Default)]
11416pub struct BlockFilterHashesBuilder {
11417 pub(crate) start_number: Uint64,
11418 pub(crate) parent_block_filter_hash: Byte32,
11419 pub(crate) block_filter_hashes: Byte32Vec,
11420}
11421impl BlockFilterHashesBuilder {
11422 pub const FIELD_COUNT: usize = 3;
11423 pub fn start_number(mut self, v: Uint64) -> Self {
11424 self.start_number = v;
11425 self
11426 }
11427 pub fn parent_block_filter_hash(mut self, v: Byte32) -> Self {
11428 self.parent_block_filter_hash = v;
11429 self
11430 }
11431 pub fn block_filter_hashes(mut self, v: Byte32Vec) -> Self {
11432 self.block_filter_hashes = v;
11433 self
11434 }
11435}
11436impl molecule::prelude::Builder for BlockFilterHashesBuilder {
11437 type Entity = BlockFilterHashes;
11438 const NAME: &'static str = "BlockFilterHashesBuilder";
11439 fn expected_length(&self) -> usize {
11440 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
11441 + self.start_number.as_slice().len()
11442 + self.parent_block_filter_hash.as_slice().len()
11443 + self.block_filter_hashes.as_slice().len()
11444 }
11445 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
11446 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
11447 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
11448 offsets.push(total_size);
11449 total_size += self.start_number.as_slice().len();
11450 offsets.push(total_size);
11451 total_size += self.parent_block_filter_hash.as_slice().len();
11452 offsets.push(total_size);
11453 total_size += self.block_filter_hashes.as_slice().len();
11454 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
11455 for offset in offsets.into_iter() {
11456 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
11457 }
11458 writer.write_all(self.start_number.as_slice())?;
11459 writer.write_all(self.parent_block_filter_hash.as_slice())?;
11460 writer.write_all(self.block_filter_hashes.as_slice())?;
11461 Ok(())
11462 }
11463 fn build(&self) -> Self::Entity {
11464 let mut inner = Vec::with_capacity(self.expected_length());
11465 self.write(&mut inner)
11466 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
11467 BlockFilterHashes::new_unchecked(inner.into())
11468 }
11469}
11470#[derive(Clone)]
11471pub struct GetBlockFilterCheckPoints(molecule::bytes::Bytes);
11472impl ::core::fmt::LowerHex for GetBlockFilterCheckPoints {
11473 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11474 use molecule::hex_string;
11475 if f.alternate() {
11476 write!(f, "0x")?;
11477 }
11478 write!(f, "{}", hex_string(self.as_slice()))
11479 }
11480}
11481impl ::core::fmt::Debug for GetBlockFilterCheckPoints {
11482 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11483 write!(f, "{}({:#x})", Self::NAME, self)
11484 }
11485}
11486impl ::core::fmt::Display for GetBlockFilterCheckPoints {
11487 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11488 write!(f, "{} {{ ", Self::NAME)?;
11489 write!(f, "{}: {}", "start_number", self.start_number())?;
11490 write!(f, " }}")
11491 }
11492}
11493impl ::core::default::Default for GetBlockFilterCheckPoints {
11494 fn default() -> Self {
11495 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
11496 GetBlockFilterCheckPoints::new_unchecked(v)
11497 }
11498}
11499impl GetBlockFilterCheckPoints {
11500 const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
11501 pub const TOTAL_SIZE: usize = 8;
11502 pub const FIELD_SIZES: [usize; 1] = [8];
11503 pub const FIELD_COUNT: usize = 1;
11504 pub fn start_number(&self) -> Uint64 {
11505 Uint64::new_unchecked(self.0.slice(0..8))
11506 }
11507 pub fn as_reader<'r>(&'r self) -> GetBlockFilterCheckPointsReader<'r> {
11508 GetBlockFilterCheckPointsReader::new_unchecked(self.as_slice())
11509 }
11510}
11511impl molecule::prelude::Entity for GetBlockFilterCheckPoints {
11512 type Builder = GetBlockFilterCheckPointsBuilder;
11513 const NAME: &'static str = "GetBlockFilterCheckPoints";
11514 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
11515 GetBlockFilterCheckPoints(data)
11516 }
11517 fn as_bytes(&self) -> molecule::bytes::Bytes {
11518 self.0.clone()
11519 }
11520 fn as_slice(&self) -> &[u8] {
11521 &self.0[..]
11522 }
11523 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11524 GetBlockFilterCheckPointsReader::from_slice(slice).map(|reader| reader.to_entity())
11525 }
11526 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11527 GetBlockFilterCheckPointsReader::from_compatible_slice(slice)
11528 .map(|reader| reader.to_entity())
11529 }
11530 fn new_builder() -> Self::Builder {
11531 ::core::default::Default::default()
11532 }
11533 fn as_builder(self) -> Self::Builder {
11534 Self::new_builder().start_number(self.start_number())
11535 }
11536}
11537#[derive(Clone, Copy)]
11538pub struct GetBlockFilterCheckPointsReader<'r>(&'r [u8]);
11539impl<'r> ::core::fmt::LowerHex for GetBlockFilterCheckPointsReader<'r> {
11540 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11541 use molecule::hex_string;
11542 if f.alternate() {
11543 write!(f, "0x")?;
11544 }
11545 write!(f, "{}", hex_string(self.as_slice()))
11546 }
11547}
11548impl<'r> ::core::fmt::Debug for GetBlockFilterCheckPointsReader<'r> {
11549 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11550 write!(f, "{}({:#x})", Self::NAME, self)
11551 }
11552}
11553impl<'r> ::core::fmt::Display for GetBlockFilterCheckPointsReader<'r> {
11554 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11555 write!(f, "{} {{ ", Self::NAME)?;
11556 write!(f, "{}: {}", "start_number", self.start_number())?;
11557 write!(f, " }}")
11558 }
11559}
11560impl<'r> GetBlockFilterCheckPointsReader<'r> {
11561 pub const TOTAL_SIZE: usize = 8;
11562 pub const FIELD_SIZES: [usize; 1] = [8];
11563 pub const FIELD_COUNT: usize = 1;
11564 pub fn start_number(&self) -> Uint64Reader<'r> {
11565 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
11566 }
11567}
11568impl<'r> molecule::prelude::Reader<'r> for GetBlockFilterCheckPointsReader<'r> {
11569 type Entity = GetBlockFilterCheckPoints;
11570 const NAME: &'static str = "GetBlockFilterCheckPointsReader";
11571 fn to_entity(&self) -> Self::Entity {
11572 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
11573 }
11574 fn new_unchecked(slice: &'r [u8]) -> Self {
11575 GetBlockFilterCheckPointsReader(slice)
11576 }
11577 fn as_slice(&self) -> &'r [u8] {
11578 self.0
11579 }
11580 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
11581 use molecule::verification_error as ve;
11582 let slice_len = slice.len();
11583 if slice_len != Self::TOTAL_SIZE {
11584 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
11585 }
11586 Ok(())
11587 }
11588}
11589#[derive(Debug, Default)]
11590pub struct GetBlockFilterCheckPointsBuilder {
11591 pub(crate) start_number: Uint64,
11592}
11593impl GetBlockFilterCheckPointsBuilder {
11594 pub const TOTAL_SIZE: usize = 8;
11595 pub const FIELD_SIZES: [usize; 1] = [8];
11596 pub const FIELD_COUNT: usize = 1;
11597 pub fn start_number(mut self, v: Uint64) -> Self {
11598 self.start_number = v;
11599 self
11600 }
11601}
11602impl molecule::prelude::Builder for GetBlockFilterCheckPointsBuilder {
11603 type Entity = GetBlockFilterCheckPoints;
11604 const NAME: &'static str = "GetBlockFilterCheckPointsBuilder";
11605 fn expected_length(&self) -> usize {
11606 Self::TOTAL_SIZE
11607 }
11608 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
11609 writer.write_all(self.start_number.as_slice())?;
11610 Ok(())
11611 }
11612 fn build(&self) -> Self::Entity {
11613 let mut inner = Vec::with_capacity(self.expected_length());
11614 self.write(&mut inner)
11615 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
11616 GetBlockFilterCheckPoints::new_unchecked(inner.into())
11617 }
11618}
11619#[derive(Clone)]
11620pub struct BlockFilterCheckPoints(molecule::bytes::Bytes);
11621impl ::core::fmt::LowerHex for BlockFilterCheckPoints {
11622 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11623 use molecule::hex_string;
11624 if f.alternate() {
11625 write!(f, "0x")?;
11626 }
11627 write!(f, "{}", hex_string(self.as_slice()))
11628 }
11629}
11630impl ::core::fmt::Debug for BlockFilterCheckPoints {
11631 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11632 write!(f, "{}({:#x})", Self::NAME, self)
11633 }
11634}
11635impl ::core::fmt::Display for BlockFilterCheckPoints {
11636 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11637 write!(f, "{} {{ ", Self::NAME)?;
11638 write!(f, "{}: {}", "start_number", self.start_number())?;
11639 write!(
11640 f,
11641 ", {}: {}",
11642 "block_filter_hashes",
11643 self.block_filter_hashes()
11644 )?;
11645 let extra_count = self.count_extra_fields();
11646 if extra_count != 0 {
11647 write!(f, ", .. ({} fields)", extra_count)?;
11648 }
11649 write!(f, " }}")
11650 }
11651}
11652impl ::core::default::Default for BlockFilterCheckPoints {
11653 fn default() -> Self {
11654 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
11655 BlockFilterCheckPoints::new_unchecked(v)
11656 }
11657}
11658impl BlockFilterCheckPoints {
11659 const DEFAULT_VALUE: [u8; 24] = [
11660 24, 0, 0, 0, 12, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11661 ];
11662 pub const FIELD_COUNT: usize = 2;
11663 pub fn total_size(&self) -> usize {
11664 molecule::unpack_number(self.as_slice()) as usize
11665 }
11666 pub fn field_count(&self) -> usize {
11667 if self.total_size() == molecule::NUMBER_SIZE {
11668 0
11669 } else {
11670 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
11671 }
11672 }
11673 pub fn count_extra_fields(&self) -> usize {
11674 self.field_count() - Self::FIELD_COUNT
11675 }
11676 pub fn has_extra_fields(&self) -> bool {
11677 Self::FIELD_COUNT != self.field_count()
11678 }
11679 pub fn start_number(&self) -> Uint64 {
11680 let slice = self.as_slice();
11681 let start = molecule::unpack_number(&slice[4..]) as usize;
11682 let end = molecule::unpack_number(&slice[8..]) as usize;
11683 Uint64::new_unchecked(self.0.slice(start..end))
11684 }
11685 pub fn block_filter_hashes(&self) -> Byte32Vec {
11686 let slice = self.as_slice();
11687 let start = molecule::unpack_number(&slice[8..]) as usize;
11688 if self.has_extra_fields() {
11689 let end = molecule::unpack_number(&slice[12..]) as usize;
11690 Byte32Vec::new_unchecked(self.0.slice(start..end))
11691 } else {
11692 Byte32Vec::new_unchecked(self.0.slice(start..))
11693 }
11694 }
11695 pub fn as_reader<'r>(&'r self) -> BlockFilterCheckPointsReader<'r> {
11696 BlockFilterCheckPointsReader::new_unchecked(self.as_slice())
11697 }
11698}
11699impl molecule::prelude::Entity for BlockFilterCheckPoints {
11700 type Builder = BlockFilterCheckPointsBuilder;
11701 const NAME: &'static str = "BlockFilterCheckPoints";
11702 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
11703 BlockFilterCheckPoints(data)
11704 }
11705 fn as_bytes(&self) -> molecule::bytes::Bytes {
11706 self.0.clone()
11707 }
11708 fn as_slice(&self) -> &[u8] {
11709 &self.0[..]
11710 }
11711 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11712 BlockFilterCheckPointsReader::from_slice(slice).map(|reader| reader.to_entity())
11713 }
11714 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11715 BlockFilterCheckPointsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
11716 }
11717 fn new_builder() -> Self::Builder {
11718 ::core::default::Default::default()
11719 }
11720 fn as_builder(self) -> Self::Builder {
11721 Self::new_builder()
11722 .start_number(self.start_number())
11723 .block_filter_hashes(self.block_filter_hashes())
11724 }
11725}
11726#[derive(Clone, Copy)]
11727pub struct BlockFilterCheckPointsReader<'r>(&'r [u8]);
11728impl<'r> ::core::fmt::LowerHex for BlockFilterCheckPointsReader<'r> {
11729 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11730 use molecule::hex_string;
11731 if f.alternate() {
11732 write!(f, "0x")?;
11733 }
11734 write!(f, "{}", hex_string(self.as_slice()))
11735 }
11736}
11737impl<'r> ::core::fmt::Debug for BlockFilterCheckPointsReader<'r> {
11738 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11739 write!(f, "{}({:#x})", Self::NAME, self)
11740 }
11741}
11742impl<'r> ::core::fmt::Display for BlockFilterCheckPointsReader<'r> {
11743 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11744 write!(f, "{} {{ ", Self::NAME)?;
11745 write!(f, "{}: {}", "start_number", self.start_number())?;
11746 write!(
11747 f,
11748 ", {}: {}",
11749 "block_filter_hashes",
11750 self.block_filter_hashes()
11751 )?;
11752 let extra_count = self.count_extra_fields();
11753 if extra_count != 0 {
11754 write!(f, ", .. ({} fields)", extra_count)?;
11755 }
11756 write!(f, " }}")
11757 }
11758}
11759impl<'r> BlockFilterCheckPointsReader<'r> {
11760 pub const FIELD_COUNT: usize = 2;
11761 pub fn total_size(&self) -> usize {
11762 molecule::unpack_number(self.as_slice()) as usize
11763 }
11764 pub fn field_count(&self) -> usize {
11765 if self.total_size() == molecule::NUMBER_SIZE {
11766 0
11767 } else {
11768 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
11769 }
11770 }
11771 pub fn count_extra_fields(&self) -> usize {
11772 self.field_count() - Self::FIELD_COUNT
11773 }
11774 pub fn has_extra_fields(&self) -> bool {
11775 Self::FIELD_COUNT != self.field_count()
11776 }
11777 pub fn start_number(&self) -> Uint64Reader<'r> {
11778 let slice = self.as_slice();
11779 let start = molecule::unpack_number(&slice[4..]) as usize;
11780 let end = molecule::unpack_number(&slice[8..]) as usize;
11781 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
11782 }
11783 pub fn block_filter_hashes(&self) -> Byte32VecReader<'r> {
11784 let slice = self.as_slice();
11785 let start = molecule::unpack_number(&slice[8..]) as usize;
11786 if self.has_extra_fields() {
11787 let end = molecule::unpack_number(&slice[12..]) as usize;
11788 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
11789 } else {
11790 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
11791 }
11792 }
11793}
11794impl<'r> molecule::prelude::Reader<'r> for BlockFilterCheckPointsReader<'r> {
11795 type Entity = BlockFilterCheckPoints;
11796 const NAME: &'static str = "BlockFilterCheckPointsReader";
11797 fn to_entity(&self) -> Self::Entity {
11798 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
11799 }
11800 fn new_unchecked(slice: &'r [u8]) -> Self {
11801 BlockFilterCheckPointsReader(slice)
11802 }
11803 fn as_slice(&self) -> &'r [u8] {
11804 self.0
11805 }
11806 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
11807 use molecule::verification_error as ve;
11808 let slice_len = slice.len();
11809 if slice_len < molecule::NUMBER_SIZE {
11810 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
11811 }
11812 let total_size = molecule::unpack_number(slice) as usize;
11813 if slice_len != total_size {
11814 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
11815 }
11816 if slice_len < molecule::NUMBER_SIZE * 2 {
11817 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
11818 }
11819 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
11820 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
11821 return ve!(Self, OffsetsNotMatch);
11822 }
11823 if slice_len < offset_first {
11824 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
11825 }
11826 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
11827 if field_count < Self::FIELD_COUNT {
11828 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
11829 } else if !compatible && field_count > Self::FIELD_COUNT {
11830 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
11831 };
11832 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
11833 .chunks_exact(molecule::NUMBER_SIZE)
11834 .map(|x| molecule::unpack_number(x) as usize)
11835 .collect();
11836 offsets.push(total_size);
11837 if offsets.windows(2).any(|i| i[0] > i[1]) {
11838 return ve!(Self, OffsetsNotMatch);
11839 }
11840 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
11841 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
11842 Ok(())
11843 }
11844}
11845#[derive(Debug, Default)]
11846pub struct BlockFilterCheckPointsBuilder {
11847 pub(crate) start_number: Uint64,
11848 pub(crate) block_filter_hashes: Byte32Vec,
11849}
11850impl BlockFilterCheckPointsBuilder {
11851 pub const FIELD_COUNT: usize = 2;
11852 pub fn start_number(mut self, v: Uint64) -> Self {
11853 self.start_number = v;
11854 self
11855 }
11856 pub fn block_filter_hashes(mut self, v: Byte32Vec) -> Self {
11857 self.block_filter_hashes = v;
11858 self
11859 }
11860}
11861impl molecule::prelude::Builder for BlockFilterCheckPointsBuilder {
11862 type Entity = BlockFilterCheckPoints;
11863 const NAME: &'static str = "BlockFilterCheckPointsBuilder";
11864 fn expected_length(&self) -> usize {
11865 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
11866 + self.start_number.as_slice().len()
11867 + self.block_filter_hashes.as_slice().len()
11868 }
11869 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
11870 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
11871 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
11872 offsets.push(total_size);
11873 total_size += self.start_number.as_slice().len();
11874 offsets.push(total_size);
11875 total_size += self.block_filter_hashes.as_slice().len();
11876 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
11877 for offset in offsets.into_iter() {
11878 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
11879 }
11880 writer.write_all(self.start_number.as_slice())?;
11881 writer.write_all(self.block_filter_hashes.as_slice())?;
11882 Ok(())
11883 }
11884 fn build(&self) -> Self::Entity {
11885 let mut inner = Vec::with_capacity(self.expected_length());
11886 self.write(&mut inner)
11887 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
11888 BlockFilterCheckPoints::new_unchecked(inner.into())
11889 }
11890}
11891#[derive(Clone)]
11892pub struct SyncMessage(molecule::bytes::Bytes);
11893impl ::core::fmt::LowerHex for SyncMessage {
11894 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11895 use molecule::hex_string;
11896 if f.alternate() {
11897 write!(f, "0x")?;
11898 }
11899 write!(f, "{}", hex_string(self.as_slice()))
11900 }
11901}
11902impl ::core::fmt::Debug for SyncMessage {
11903 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11904 write!(f, "{}({:#x})", Self::NAME, self)
11905 }
11906}
11907impl ::core::fmt::Display for SyncMessage {
11908 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11909 write!(f, "{}(", Self::NAME)?;
11910 self.to_enum().display_inner(f)?;
11911 write!(f, ")")
11912 }
11913}
11914impl ::core::default::Default for SyncMessage {
11915 fn default() -> Self {
11916 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
11917 SyncMessage::new_unchecked(v)
11918 }
11919}
11920impl SyncMessage {
11921 const DEFAULT_VALUE: [u8; 52] = [
11922 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,
11923 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11924 ];
11925 pub const ITEMS_COUNT: usize = 5;
11926 pub fn item_id(&self) -> molecule::Number {
11927 molecule::unpack_number(self.as_slice())
11928 }
11929 pub fn to_enum(&self) -> SyncMessageUnion {
11930 let inner = self.0.slice(molecule::NUMBER_SIZE..);
11931 match self.item_id() {
11932 0 => GetHeaders::new_unchecked(inner).into(),
11933 1 => SendHeaders::new_unchecked(inner).into(),
11934 2 => GetBlocks::new_unchecked(inner).into(),
11935 3 => SendBlock::new_unchecked(inner).into(),
11936 8 => InIBD::new_unchecked(inner).into(),
11937 _ => panic!("{}: invalid data", Self::NAME),
11938 }
11939 }
11940 pub fn as_reader<'r>(&'r self) -> SyncMessageReader<'r> {
11941 SyncMessageReader::new_unchecked(self.as_slice())
11942 }
11943}
11944impl molecule::prelude::Entity for SyncMessage {
11945 type Builder = SyncMessageBuilder;
11946 const NAME: &'static str = "SyncMessage";
11947 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
11948 SyncMessage(data)
11949 }
11950 fn as_bytes(&self) -> molecule::bytes::Bytes {
11951 self.0.clone()
11952 }
11953 fn as_slice(&self) -> &[u8] {
11954 &self.0[..]
11955 }
11956 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11957 SyncMessageReader::from_slice(slice).map(|reader| reader.to_entity())
11958 }
11959 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
11960 SyncMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
11961 }
11962 fn new_builder() -> Self::Builder {
11963 ::core::default::Default::default()
11964 }
11965 fn as_builder(self) -> Self::Builder {
11966 Self::new_builder().set(self.to_enum())
11967 }
11968}
11969#[derive(Clone, Copy)]
11970pub struct SyncMessageReader<'r>(&'r [u8]);
11971impl<'r> ::core::fmt::LowerHex for SyncMessageReader<'r> {
11972 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11973 use molecule::hex_string;
11974 if f.alternate() {
11975 write!(f, "0x")?;
11976 }
11977 write!(f, "{}", hex_string(self.as_slice()))
11978 }
11979}
11980impl<'r> ::core::fmt::Debug for SyncMessageReader<'r> {
11981 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11982 write!(f, "{}({:#x})", Self::NAME, self)
11983 }
11984}
11985impl<'r> ::core::fmt::Display for SyncMessageReader<'r> {
11986 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
11987 write!(f, "{}(", Self::NAME)?;
11988 self.to_enum().display_inner(f)?;
11989 write!(f, ")")
11990 }
11991}
11992impl<'r> SyncMessageReader<'r> {
11993 pub const ITEMS_COUNT: usize = 5;
11994 pub fn item_id(&self) -> molecule::Number {
11995 molecule::unpack_number(self.as_slice())
11996 }
11997 pub fn to_enum(&self) -> SyncMessageUnionReader<'r> {
11998 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
11999 match self.item_id() {
12000 0 => GetHeadersReader::new_unchecked(inner).into(),
12001 1 => SendHeadersReader::new_unchecked(inner).into(),
12002 2 => GetBlocksReader::new_unchecked(inner).into(),
12003 3 => SendBlockReader::new_unchecked(inner).into(),
12004 8 => InIBDReader::new_unchecked(inner).into(),
12005 _ => panic!("{}: invalid data", Self::NAME),
12006 }
12007 }
12008}
12009impl<'r> molecule::prelude::Reader<'r> for SyncMessageReader<'r> {
12010 type Entity = SyncMessage;
12011 const NAME: &'static str = "SyncMessageReader";
12012 fn to_entity(&self) -> Self::Entity {
12013 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
12014 }
12015 fn new_unchecked(slice: &'r [u8]) -> Self {
12016 SyncMessageReader(slice)
12017 }
12018 fn as_slice(&self) -> &'r [u8] {
12019 self.0
12020 }
12021 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
12022 use molecule::verification_error as ve;
12023 let slice_len = slice.len();
12024 if slice_len < molecule::NUMBER_SIZE {
12025 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
12026 }
12027 let item_id = molecule::unpack_number(slice);
12028 let inner_slice = &slice[molecule::NUMBER_SIZE..];
12029 match item_id {
12030 0 => GetHeadersReader::verify(inner_slice, compatible),
12031 1 => SendHeadersReader::verify(inner_slice, compatible),
12032 2 => GetBlocksReader::verify(inner_slice, compatible),
12033 3 => SendBlockReader::verify(inner_slice, compatible),
12034 8 => InIBDReader::verify(inner_slice, compatible),
12035 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
12036 }?;
12037 Ok(())
12038 }
12039}
12040#[derive(Debug, Default)]
12041pub struct SyncMessageBuilder(pub(crate) SyncMessageUnion);
12042impl SyncMessageBuilder {
12043 pub const ITEMS_COUNT: usize = 5;
12044 pub fn set<I>(mut self, v: I) -> Self
12045 where
12046 I: ::core::convert::Into<SyncMessageUnion>,
12047 {
12048 self.0 = v.into();
12049 self
12050 }
12051}
12052impl molecule::prelude::Builder for SyncMessageBuilder {
12053 type Entity = SyncMessage;
12054 const NAME: &'static str = "SyncMessageBuilder";
12055 fn expected_length(&self) -> usize {
12056 molecule::NUMBER_SIZE + self.0.as_slice().len()
12057 }
12058 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
12059 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
12060 writer.write_all(self.0.as_slice())
12061 }
12062 fn build(&self) -> Self::Entity {
12063 let mut inner = Vec::with_capacity(self.expected_length());
12064 self.write(&mut inner)
12065 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
12066 SyncMessage::new_unchecked(inner.into())
12067 }
12068}
12069#[derive(Debug, Clone)]
12070pub enum SyncMessageUnion {
12071 GetHeaders(GetHeaders),
12072 SendHeaders(SendHeaders),
12073 GetBlocks(GetBlocks),
12074 SendBlock(SendBlock),
12075 InIBD(InIBD),
12076}
12077#[derive(Debug, Clone, Copy)]
12078pub enum SyncMessageUnionReader<'r> {
12079 GetHeaders(GetHeadersReader<'r>),
12080 SendHeaders(SendHeadersReader<'r>),
12081 GetBlocks(GetBlocksReader<'r>),
12082 SendBlock(SendBlockReader<'r>),
12083 InIBD(InIBDReader<'r>),
12084}
12085impl ::core::default::Default for SyncMessageUnion {
12086 fn default() -> Self {
12087 SyncMessageUnion::GetHeaders(::core::default::Default::default())
12088 }
12089}
12090impl ::core::fmt::Display for SyncMessageUnion {
12091 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12092 match self {
12093 SyncMessageUnion::GetHeaders(ref item) => {
12094 write!(f, "{}::{}({})", Self::NAME, GetHeaders::NAME, item)
12095 }
12096 SyncMessageUnion::SendHeaders(ref item) => {
12097 write!(f, "{}::{}({})", Self::NAME, SendHeaders::NAME, item)
12098 }
12099 SyncMessageUnion::GetBlocks(ref item) => {
12100 write!(f, "{}::{}({})", Self::NAME, GetBlocks::NAME, item)
12101 }
12102 SyncMessageUnion::SendBlock(ref item) => {
12103 write!(f, "{}::{}({})", Self::NAME, SendBlock::NAME, item)
12104 }
12105 SyncMessageUnion::InIBD(ref item) => {
12106 write!(f, "{}::{}({})", Self::NAME, InIBD::NAME, item)
12107 }
12108 }
12109 }
12110}
12111impl<'r> ::core::fmt::Display for SyncMessageUnionReader<'r> {
12112 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12113 match self {
12114 SyncMessageUnionReader::GetHeaders(ref item) => {
12115 write!(f, "{}::{}({})", Self::NAME, GetHeaders::NAME, item)
12116 }
12117 SyncMessageUnionReader::SendHeaders(ref item) => {
12118 write!(f, "{}::{}({})", Self::NAME, SendHeaders::NAME, item)
12119 }
12120 SyncMessageUnionReader::GetBlocks(ref item) => {
12121 write!(f, "{}::{}({})", Self::NAME, GetBlocks::NAME, item)
12122 }
12123 SyncMessageUnionReader::SendBlock(ref item) => {
12124 write!(f, "{}::{}({})", Self::NAME, SendBlock::NAME, item)
12125 }
12126 SyncMessageUnionReader::InIBD(ref item) => {
12127 write!(f, "{}::{}({})", Self::NAME, InIBD::NAME, item)
12128 }
12129 }
12130 }
12131}
12132impl SyncMessageUnion {
12133 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12134 match self {
12135 SyncMessageUnion::GetHeaders(ref item) => write!(f, "{}", item),
12136 SyncMessageUnion::SendHeaders(ref item) => write!(f, "{}", item),
12137 SyncMessageUnion::GetBlocks(ref item) => write!(f, "{}", item),
12138 SyncMessageUnion::SendBlock(ref item) => write!(f, "{}", item),
12139 SyncMessageUnion::InIBD(ref item) => write!(f, "{}", item),
12140 }
12141 }
12142}
12143impl<'r> SyncMessageUnionReader<'r> {
12144 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12145 match self {
12146 SyncMessageUnionReader::GetHeaders(ref item) => write!(f, "{}", item),
12147 SyncMessageUnionReader::SendHeaders(ref item) => write!(f, "{}", item),
12148 SyncMessageUnionReader::GetBlocks(ref item) => write!(f, "{}", item),
12149 SyncMessageUnionReader::SendBlock(ref item) => write!(f, "{}", item),
12150 SyncMessageUnionReader::InIBD(ref item) => write!(f, "{}", item),
12151 }
12152 }
12153}
12154impl ::core::convert::From<GetHeaders> for SyncMessageUnion {
12155 fn from(item: GetHeaders) -> Self {
12156 SyncMessageUnion::GetHeaders(item)
12157 }
12158}
12159impl ::core::convert::From<SendHeaders> for SyncMessageUnion {
12160 fn from(item: SendHeaders) -> Self {
12161 SyncMessageUnion::SendHeaders(item)
12162 }
12163}
12164impl ::core::convert::From<GetBlocks> for SyncMessageUnion {
12165 fn from(item: GetBlocks) -> Self {
12166 SyncMessageUnion::GetBlocks(item)
12167 }
12168}
12169impl ::core::convert::From<SendBlock> for SyncMessageUnion {
12170 fn from(item: SendBlock) -> Self {
12171 SyncMessageUnion::SendBlock(item)
12172 }
12173}
12174impl ::core::convert::From<InIBD> for SyncMessageUnion {
12175 fn from(item: InIBD) -> Self {
12176 SyncMessageUnion::InIBD(item)
12177 }
12178}
12179impl<'r> ::core::convert::From<GetHeadersReader<'r>> for SyncMessageUnionReader<'r> {
12180 fn from(item: GetHeadersReader<'r>) -> Self {
12181 SyncMessageUnionReader::GetHeaders(item)
12182 }
12183}
12184impl<'r> ::core::convert::From<SendHeadersReader<'r>> for SyncMessageUnionReader<'r> {
12185 fn from(item: SendHeadersReader<'r>) -> Self {
12186 SyncMessageUnionReader::SendHeaders(item)
12187 }
12188}
12189impl<'r> ::core::convert::From<GetBlocksReader<'r>> for SyncMessageUnionReader<'r> {
12190 fn from(item: GetBlocksReader<'r>) -> Self {
12191 SyncMessageUnionReader::GetBlocks(item)
12192 }
12193}
12194impl<'r> ::core::convert::From<SendBlockReader<'r>> for SyncMessageUnionReader<'r> {
12195 fn from(item: SendBlockReader<'r>) -> Self {
12196 SyncMessageUnionReader::SendBlock(item)
12197 }
12198}
12199impl<'r> ::core::convert::From<InIBDReader<'r>> for SyncMessageUnionReader<'r> {
12200 fn from(item: InIBDReader<'r>) -> Self {
12201 SyncMessageUnionReader::InIBD(item)
12202 }
12203}
12204impl SyncMessageUnion {
12205 pub const NAME: &'static str = "SyncMessageUnion";
12206 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
12207 match self {
12208 SyncMessageUnion::GetHeaders(item) => item.as_bytes(),
12209 SyncMessageUnion::SendHeaders(item) => item.as_bytes(),
12210 SyncMessageUnion::GetBlocks(item) => item.as_bytes(),
12211 SyncMessageUnion::SendBlock(item) => item.as_bytes(),
12212 SyncMessageUnion::InIBD(item) => item.as_bytes(),
12213 }
12214 }
12215 pub fn as_slice(&self) -> &[u8] {
12216 match self {
12217 SyncMessageUnion::GetHeaders(item) => item.as_slice(),
12218 SyncMessageUnion::SendHeaders(item) => item.as_slice(),
12219 SyncMessageUnion::GetBlocks(item) => item.as_slice(),
12220 SyncMessageUnion::SendBlock(item) => item.as_slice(),
12221 SyncMessageUnion::InIBD(item) => item.as_slice(),
12222 }
12223 }
12224 pub fn item_id(&self) -> molecule::Number {
12225 match self {
12226 SyncMessageUnion::GetHeaders(_) => 0,
12227 SyncMessageUnion::SendHeaders(_) => 1,
12228 SyncMessageUnion::GetBlocks(_) => 2,
12229 SyncMessageUnion::SendBlock(_) => 3,
12230 SyncMessageUnion::InIBD(_) => 8,
12231 }
12232 }
12233 pub fn item_name(&self) -> &str {
12234 match self {
12235 SyncMessageUnion::GetHeaders(_) => "GetHeaders",
12236 SyncMessageUnion::SendHeaders(_) => "SendHeaders",
12237 SyncMessageUnion::GetBlocks(_) => "GetBlocks",
12238 SyncMessageUnion::SendBlock(_) => "SendBlock",
12239 SyncMessageUnion::InIBD(_) => "InIBD",
12240 }
12241 }
12242 pub fn as_reader<'r>(&'r self) -> SyncMessageUnionReader<'r> {
12243 match self {
12244 SyncMessageUnion::GetHeaders(item) => item.as_reader().into(),
12245 SyncMessageUnion::SendHeaders(item) => item.as_reader().into(),
12246 SyncMessageUnion::GetBlocks(item) => item.as_reader().into(),
12247 SyncMessageUnion::SendBlock(item) => item.as_reader().into(),
12248 SyncMessageUnion::InIBD(item) => item.as_reader().into(),
12249 }
12250 }
12251}
12252impl<'r> SyncMessageUnionReader<'r> {
12253 pub const NAME: &'r str = "SyncMessageUnionReader";
12254 pub fn as_slice(&self) -> &'r [u8] {
12255 match self {
12256 SyncMessageUnionReader::GetHeaders(item) => item.as_slice(),
12257 SyncMessageUnionReader::SendHeaders(item) => item.as_slice(),
12258 SyncMessageUnionReader::GetBlocks(item) => item.as_slice(),
12259 SyncMessageUnionReader::SendBlock(item) => item.as_slice(),
12260 SyncMessageUnionReader::InIBD(item) => item.as_slice(),
12261 }
12262 }
12263 pub fn item_id(&self) -> molecule::Number {
12264 match self {
12265 SyncMessageUnionReader::GetHeaders(_) => 0,
12266 SyncMessageUnionReader::SendHeaders(_) => 1,
12267 SyncMessageUnionReader::GetBlocks(_) => 2,
12268 SyncMessageUnionReader::SendBlock(_) => 3,
12269 SyncMessageUnionReader::InIBD(_) => 8,
12270 }
12271 }
12272 pub fn item_name(&self) -> &str {
12273 match self {
12274 SyncMessageUnionReader::GetHeaders(_) => "GetHeaders",
12275 SyncMessageUnionReader::SendHeaders(_) => "SendHeaders",
12276 SyncMessageUnionReader::GetBlocks(_) => "GetBlocks",
12277 SyncMessageUnionReader::SendBlock(_) => "SendBlock",
12278 SyncMessageUnionReader::InIBD(_) => "InIBD",
12279 }
12280 }
12281}
12282#[derive(Clone)]
12283pub struct GetHeaders(molecule::bytes::Bytes);
12284impl ::core::fmt::LowerHex for GetHeaders {
12285 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12286 use molecule::hex_string;
12287 if f.alternate() {
12288 write!(f, "0x")?;
12289 }
12290 write!(f, "{}", hex_string(self.as_slice()))
12291 }
12292}
12293impl ::core::fmt::Debug for GetHeaders {
12294 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12295 write!(f, "{}({:#x})", Self::NAME, self)
12296 }
12297}
12298impl ::core::fmt::Display for GetHeaders {
12299 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12300 write!(f, "{} {{ ", Self::NAME)?;
12301 write!(f, "{}: {}", "hash_stop", self.hash_stop())?;
12302 write!(
12303 f,
12304 ", {}: {}",
12305 "block_locator_hashes",
12306 self.block_locator_hashes()
12307 )?;
12308 let extra_count = self.count_extra_fields();
12309 if extra_count != 0 {
12310 write!(f, ", .. ({} fields)", extra_count)?;
12311 }
12312 write!(f, " }}")
12313 }
12314}
12315impl ::core::default::Default for GetHeaders {
12316 fn default() -> Self {
12317 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
12318 GetHeaders::new_unchecked(v)
12319 }
12320}
12321impl GetHeaders {
12322 const DEFAULT_VALUE: [u8; 48] = [
12323 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,
12324 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12325 ];
12326 pub const FIELD_COUNT: usize = 2;
12327 pub fn total_size(&self) -> usize {
12328 molecule::unpack_number(self.as_slice()) as usize
12329 }
12330 pub fn field_count(&self) -> usize {
12331 if self.total_size() == molecule::NUMBER_SIZE {
12332 0
12333 } else {
12334 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
12335 }
12336 }
12337 pub fn count_extra_fields(&self) -> usize {
12338 self.field_count() - Self::FIELD_COUNT
12339 }
12340 pub fn has_extra_fields(&self) -> bool {
12341 Self::FIELD_COUNT != self.field_count()
12342 }
12343 pub fn hash_stop(&self) -> Byte32 {
12344 let slice = self.as_slice();
12345 let start = molecule::unpack_number(&slice[4..]) as usize;
12346 let end = molecule::unpack_number(&slice[8..]) as usize;
12347 Byte32::new_unchecked(self.0.slice(start..end))
12348 }
12349 pub fn block_locator_hashes(&self) -> Byte32Vec {
12350 let slice = self.as_slice();
12351 let start = molecule::unpack_number(&slice[8..]) as usize;
12352 if self.has_extra_fields() {
12353 let end = molecule::unpack_number(&slice[12..]) as usize;
12354 Byte32Vec::new_unchecked(self.0.slice(start..end))
12355 } else {
12356 Byte32Vec::new_unchecked(self.0.slice(start..))
12357 }
12358 }
12359 pub fn as_reader<'r>(&'r self) -> GetHeadersReader<'r> {
12360 GetHeadersReader::new_unchecked(self.as_slice())
12361 }
12362}
12363impl molecule::prelude::Entity for GetHeaders {
12364 type Builder = GetHeadersBuilder;
12365 const NAME: &'static str = "GetHeaders";
12366 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
12367 GetHeaders(data)
12368 }
12369 fn as_bytes(&self) -> molecule::bytes::Bytes {
12370 self.0.clone()
12371 }
12372 fn as_slice(&self) -> &[u8] {
12373 &self.0[..]
12374 }
12375 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12376 GetHeadersReader::from_slice(slice).map(|reader| reader.to_entity())
12377 }
12378 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12379 GetHeadersReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
12380 }
12381 fn new_builder() -> Self::Builder {
12382 ::core::default::Default::default()
12383 }
12384 fn as_builder(self) -> Self::Builder {
12385 Self::new_builder()
12386 .hash_stop(self.hash_stop())
12387 .block_locator_hashes(self.block_locator_hashes())
12388 }
12389}
12390#[derive(Clone, Copy)]
12391pub struct GetHeadersReader<'r>(&'r [u8]);
12392impl<'r> ::core::fmt::LowerHex for GetHeadersReader<'r> {
12393 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12394 use molecule::hex_string;
12395 if f.alternate() {
12396 write!(f, "0x")?;
12397 }
12398 write!(f, "{}", hex_string(self.as_slice()))
12399 }
12400}
12401impl<'r> ::core::fmt::Debug for GetHeadersReader<'r> {
12402 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12403 write!(f, "{}({:#x})", Self::NAME, self)
12404 }
12405}
12406impl<'r> ::core::fmt::Display for GetHeadersReader<'r> {
12407 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12408 write!(f, "{} {{ ", Self::NAME)?;
12409 write!(f, "{}: {}", "hash_stop", self.hash_stop())?;
12410 write!(
12411 f,
12412 ", {}: {}",
12413 "block_locator_hashes",
12414 self.block_locator_hashes()
12415 )?;
12416 let extra_count = self.count_extra_fields();
12417 if extra_count != 0 {
12418 write!(f, ", .. ({} fields)", extra_count)?;
12419 }
12420 write!(f, " }}")
12421 }
12422}
12423impl<'r> GetHeadersReader<'r> {
12424 pub const FIELD_COUNT: usize = 2;
12425 pub fn total_size(&self) -> usize {
12426 molecule::unpack_number(self.as_slice()) as usize
12427 }
12428 pub fn field_count(&self) -> usize {
12429 if self.total_size() == molecule::NUMBER_SIZE {
12430 0
12431 } else {
12432 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
12433 }
12434 }
12435 pub fn count_extra_fields(&self) -> usize {
12436 self.field_count() - Self::FIELD_COUNT
12437 }
12438 pub fn has_extra_fields(&self) -> bool {
12439 Self::FIELD_COUNT != self.field_count()
12440 }
12441 pub fn hash_stop(&self) -> Byte32Reader<'r> {
12442 let slice = self.as_slice();
12443 let start = molecule::unpack_number(&slice[4..]) as usize;
12444 let end = molecule::unpack_number(&slice[8..]) as usize;
12445 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
12446 }
12447 pub fn block_locator_hashes(&self) -> Byte32VecReader<'r> {
12448 let slice = self.as_slice();
12449 let start = molecule::unpack_number(&slice[8..]) as usize;
12450 if self.has_extra_fields() {
12451 let end = molecule::unpack_number(&slice[12..]) as usize;
12452 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
12453 } else {
12454 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
12455 }
12456 }
12457}
12458impl<'r> molecule::prelude::Reader<'r> for GetHeadersReader<'r> {
12459 type Entity = GetHeaders;
12460 const NAME: &'static str = "GetHeadersReader";
12461 fn to_entity(&self) -> Self::Entity {
12462 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
12463 }
12464 fn new_unchecked(slice: &'r [u8]) -> Self {
12465 GetHeadersReader(slice)
12466 }
12467 fn as_slice(&self) -> &'r [u8] {
12468 self.0
12469 }
12470 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
12471 use molecule::verification_error as ve;
12472 let slice_len = slice.len();
12473 if slice_len < molecule::NUMBER_SIZE {
12474 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
12475 }
12476 let total_size = molecule::unpack_number(slice) as usize;
12477 if slice_len != total_size {
12478 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
12479 }
12480 if slice_len < molecule::NUMBER_SIZE * 2 {
12481 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
12482 }
12483 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
12484 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
12485 return ve!(Self, OffsetsNotMatch);
12486 }
12487 if slice_len < offset_first {
12488 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
12489 }
12490 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
12491 if field_count < Self::FIELD_COUNT {
12492 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
12493 } else if !compatible && field_count > Self::FIELD_COUNT {
12494 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
12495 };
12496 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
12497 .chunks_exact(molecule::NUMBER_SIZE)
12498 .map(|x| molecule::unpack_number(x) as usize)
12499 .collect();
12500 offsets.push(total_size);
12501 if offsets.windows(2).any(|i| i[0] > i[1]) {
12502 return ve!(Self, OffsetsNotMatch);
12503 }
12504 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
12505 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
12506 Ok(())
12507 }
12508}
12509#[derive(Debug, Default)]
12510pub struct GetHeadersBuilder {
12511 pub(crate) hash_stop: Byte32,
12512 pub(crate) block_locator_hashes: Byte32Vec,
12513}
12514impl GetHeadersBuilder {
12515 pub const FIELD_COUNT: usize = 2;
12516 pub fn hash_stop(mut self, v: Byte32) -> Self {
12517 self.hash_stop = v;
12518 self
12519 }
12520 pub fn block_locator_hashes(mut self, v: Byte32Vec) -> Self {
12521 self.block_locator_hashes = v;
12522 self
12523 }
12524}
12525impl molecule::prelude::Builder for GetHeadersBuilder {
12526 type Entity = GetHeaders;
12527 const NAME: &'static str = "GetHeadersBuilder";
12528 fn expected_length(&self) -> usize {
12529 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
12530 + self.hash_stop.as_slice().len()
12531 + self.block_locator_hashes.as_slice().len()
12532 }
12533 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
12534 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
12535 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
12536 offsets.push(total_size);
12537 total_size += self.hash_stop.as_slice().len();
12538 offsets.push(total_size);
12539 total_size += self.block_locator_hashes.as_slice().len();
12540 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
12541 for offset in offsets.into_iter() {
12542 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
12543 }
12544 writer.write_all(self.hash_stop.as_slice())?;
12545 writer.write_all(self.block_locator_hashes.as_slice())?;
12546 Ok(())
12547 }
12548 fn build(&self) -> Self::Entity {
12549 let mut inner = Vec::with_capacity(self.expected_length());
12550 self.write(&mut inner)
12551 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
12552 GetHeaders::new_unchecked(inner.into())
12553 }
12554}
12555#[derive(Clone)]
12556pub struct GetBlocks(molecule::bytes::Bytes);
12557impl ::core::fmt::LowerHex for GetBlocks {
12558 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12559 use molecule::hex_string;
12560 if f.alternate() {
12561 write!(f, "0x")?;
12562 }
12563 write!(f, "{}", hex_string(self.as_slice()))
12564 }
12565}
12566impl ::core::fmt::Debug for GetBlocks {
12567 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12568 write!(f, "{}({:#x})", Self::NAME, self)
12569 }
12570}
12571impl ::core::fmt::Display for GetBlocks {
12572 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12573 write!(f, "{} {{ ", Self::NAME)?;
12574 write!(f, "{}: {}", "block_hashes", self.block_hashes())?;
12575 let extra_count = self.count_extra_fields();
12576 if extra_count != 0 {
12577 write!(f, ", .. ({} fields)", extra_count)?;
12578 }
12579 write!(f, " }}")
12580 }
12581}
12582impl ::core::default::Default for GetBlocks {
12583 fn default() -> Self {
12584 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
12585 GetBlocks::new_unchecked(v)
12586 }
12587}
12588impl GetBlocks {
12589 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
12590 pub const FIELD_COUNT: usize = 1;
12591 pub fn total_size(&self) -> usize {
12592 molecule::unpack_number(self.as_slice()) as usize
12593 }
12594 pub fn field_count(&self) -> usize {
12595 if self.total_size() == molecule::NUMBER_SIZE {
12596 0
12597 } else {
12598 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
12599 }
12600 }
12601 pub fn count_extra_fields(&self) -> usize {
12602 self.field_count() - Self::FIELD_COUNT
12603 }
12604 pub fn has_extra_fields(&self) -> bool {
12605 Self::FIELD_COUNT != self.field_count()
12606 }
12607 pub fn block_hashes(&self) -> Byte32Vec {
12608 let slice = self.as_slice();
12609 let start = molecule::unpack_number(&slice[4..]) as usize;
12610 if self.has_extra_fields() {
12611 let end = molecule::unpack_number(&slice[8..]) as usize;
12612 Byte32Vec::new_unchecked(self.0.slice(start..end))
12613 } else {
12614 Byte32Vec::new_unchecked(self.0.slice(start..))
12615 }
12616 }
12617 pub fn as_reader<'r>(&'r self) -> GetBlocksReader<'r> {
12618 GetBlocksReader::new_unchecked(self.as_slice())
12619 }
12620}
12621impl molecule::prelude::Entity for GetBlocks {
12622 type Builder = GetBlocksBuilder;
12623 const NAME: &'static str = "GetBlocks";
12624 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
12625 GetBlocks(data)
12626 }
12627 fn as_bytes(&self) -> molecule::bytes::Bytes {
12628 self.0.clone()
12629 }
12630 fn as_slice(&self) -> &[u8] {
12631 &self.0[..]
12632 }
12633 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12634 GetBlocksReader::from_slice(slice).map(|reader| reader.to_entity())
12635 }
12636 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12637 GetBlocksReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
12638 }
12639 fn new_builder() -> Self::Builder {
12640 ::core::default::Default::default()
12641 }
12642 fn as_builder(self) -> Self::Builder {
12643 Self::new_builder().block_hashes(self.block_hashes())
12644 }
12645}
12646#[derive(Clone, Copy)]
12647pub struct GetBlocksReader<'r>(&'r [u8]);
12648impl<'r> ::core::fmt::LowerHex for GetBlocksReader<'r> {
12649 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12650 use molecule::hex_string;
12651 if f.alternate() {
12652 write!(f, "0x")?;
12653 }
12654 write!(f, "{}", hex_string(self.as_slice()))
12655 }
12656}
12657impl<'r> ::core::fmt::Debug for GetBlocksReader<'r> {
12658 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12659 write!(f, "{}({:#x})", Self::NAME, self)
12660 }
12661}
12662impl<'r> ::core::fmt::Display for GetBlocksReader<'r> {
12663 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12664 write!(f, "{} {{ ", Self::NAME)?;
12665 write!(f, "{}: {}", "block_hashes", self.block_hashes())?;
12666 let extra_count = self.count_extra_fields();
12667 if extra_count != 0 {
12668 write!(f, ", .. ({} fields)", extra_count)?;
12669 }
12670 write!(f, " }}")
12671 }
12672}
12673impl<'r> GetBlocksReader<'r> {
12674 pub const FIELD_COUNT: usize = 1;
12675 pub fn total_size(&self) -> usize {
12676 molecule::unpack_number(self.as_slice()) as usize
12677 }
12678 pub fn field_count(&self) -> usize {
12679 if self.total_size() == molecule::NUMBER_SIZE {
12680 0
12681 } else {
12682 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
12683 }
12684 }
12685 pub fn count_extra_fields(&self) -> usize {
12686 self.field_count() - Self::FIELD_COUNT
12687 }
12688 pub fn has_extra_fields(&self) -> bool {
12689 Self::FIELD_COUNT != self.field_count()
12690 }
12691 pub fn block_hashes(&self) -> Byte32VecReader<'r> {
12692 let slice = self.as_slice();
12693 let start = molecule::unpack_number(&slice[4..]) as usize;
12694 if self.has_extra_fields() {
12695 let end = molecule::unpack_number(&slice[8..]) as usize;
12696 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
12697 } else {
12698 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
12699 }
12700 }
12701}
12702impl<'r> molecule::prelude::Reader<'r> for GetBlocksReader<'r> {
12703 type Entity = GetBlocks;
12704 const NAME: &'static str = "GetBlocksReader";
12705 fn to_entity(&self) -> Self::Entity {
12706 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
12707 }
12708 fn new_unchecked(slice: &'r [u8]) -> Self {
12709 GetBlocksReader(slice)
12710 }
12711 fn as_slice(&self) -> &'r [u8] {
12712 self.0
12713 }
12714 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
12715 use molecule::verification_error as ve;
12716 let slice_len = slice.len();
12717 if slice_len < molecule::NUMBER_SIZE {
12718 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
12719 }
12720 let total_size = molecule::unpack_number(slice) as usize;
12721 if slice_len != total_size {
12722 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
12723 }
12724 if slice_len < molecule::NUMBER_SIZE * 2 {
12725 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
12726 }
12727 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
12728 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
12729 return ve!(Self, OffsetsNotMatch);
12730 }
12731 if slice_len < offset_first {
12732 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
12733 }
12734 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
12735 if field_count < Self::FIELD_COUNT {
12736 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
12737 } else if !compatible && field_count > Self::FIELD_COUNT {
12738 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
12739 };
12740 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
12741 .chunks_exact(molecule::NUMBER_SIZE)
12742 .map(|x| molecule::unpack_number(x) as usize)
12743 .collect();
12744 offsets.push(total_size);
12745 if offsets.windows(2).any(|i| i[0] > i[1]) {
12746 return ve!(Self, OffsetsNotMatch);
12747 }
12748 Byte32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
12749 Ok(())
12750 }
12751}
12752#[derive(Debug, Default)]
12753pub struct GetBlocksBuilder {
12754 pub(crate) block_hashes: Byte32Vec,
12755}
12756impl GetBlocksBuilder {
12757 pub const FIELD_COUNT: usize = 1;
12758 pub fn block_hashes(mut self, v: Byte32Vec) -> Self {
12759 self.block_hashes = v;
12760 self
12761 }
12762}
12763impl molecule::prelude::Builder for GetBlocksBuilder {
12764 type Entity = GetBlocks;
12765 const NAME: &'static str = "GetBlocksBuilder";
12766 fn expected_length(&self) -> usize {
12767 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.block_hashes.as_slice().len()
12768 }
12769 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
12770 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
12771 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
12772 offsets.push(total_size);
12773 total_size += self.block_hashes.as_slice().len();
12774 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
12775 for offset in offsets.into_iter() {
12776 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
12777 }
12778 writer.write_all(self.block_hashes.as_slice())?;
12779 Ok(())
12780 }
12781 fn build(&self) -> Self::Entity {
12782 let mut inner = Vec::with_capacity(self.expected_length());
12783 self.write(&mut inner)
12784 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
12785 GetBlocks::new_unchecked(inner.into())
12786 }
12787}
12788#[derive(Clone)]
12789pub struct SendHeaders(molecule::bytes::Bytes);
12790impl ::core::fmt::LowerHex for SendHeaders {
12791 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12792 use molecule::hex_string;
12793 if f.alternate() {
12794 write!(f, "0x")?;
12795 }
12796 write!(f, "{}", hex_string(self.as_slice()))
12797 }
12798}
12799impl ::core::fmt::Debug for SendHeaders {
12800 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12801 write!(f, "{}({:#x})", Self::NAME, self)
12802 }
12803}
12804impl ::core::fmt::Display for SendHeaders {
12805 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12806 write!(f, "{} {{ ", Self::NAME)?;
12807 write!(f, "{}: {}", "headers", self.headers())?;
12808 let extra_count = self.count_extra_fields();
12809 if extra_count != 0 {
12810 write!(f, ", .. ({} fields)", extra_count)?;
12811 }
12812 write!(f, " }}")
12813 }
12814}
12815impl ::core::default::Default for SendHeaders {
12816 fn default() -> Self {
12817 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
12818 SendHeaders::new_unchecked(v)
12819 }
12820}
12821impl SendHeaders {
12822 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
12823 pub const FIELD_COUNT: usize = 1;
12824 pub fn total_size(&self) -> usize {
12825 molecule::unpack_number(self.as_slice()) as usize
12826 }
12827 pub fn field_count(&self) -> usize {
12828 if self.total_size() == molecule::NUMBER_SIZE {
12829 0
12830 } else {
12831 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
12832 }
12833 }
12834 pub fn count_extra_fields(&self) -> usize {
12835 self.field_count() - Self::FIELD_COUNT
12836 }
12837 pub fn has_extra_fields(&self) -> bool {
12838 Self::FIELD_COUNT != self.field_count()
12839 }
12840 pub fn headers(&self) -> HeaderVec {
12841 let slice = self.as_slice();
12842 let start = molecule::unpack_number(&slice[4..]) as usize;
12843 if self.has_extra_fields() {
12844 let end = molecule::unpack_number(&slice[8..]) as usize;
12845 HeaderVec::new_unchecked(self.0.slice(start..end))
12846 } else {
12847 HeaderVec::new_unchecked(self.0.slice(start..))
12848 }
12849 }
12850 pub fn as_reader<'r>(&'r self) -> SendHeadersReader<'r> {
12851 SendHeadersReader::new_unchecked(self.as_slice())
12852 }
12853}
12854impl molecule::prelude::Entity for SendHeaders {
12855 type Builder = SendHeadersBuilder;
12856 const NAME: &'static str = "SendHeaders";
12857 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
12858 SendHeaders(data)
12859 }
12860 fn as_bytes(&self) -> molecule::bytes::Bytes {
12861 self.0.clone()
12862 }
12863 fn as_slice(&self) -> &[u8] {
12864 &self.0[..]
12865 }
12866 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12867 SendHeadersReader::from_slice(slice).map(|reader| reader.to_entity())
12868 }
12869 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
12870 SendHeadersReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
12871 }
12872 fn new_builder() -> Self::Builder {
12873 ::core::default::Default::default()
12874 }
12875 fn as_builder(self) -> Self::Builder {
12876 Self::new_builder().headers(self.headers())
12877 }
12878}
12879#[derive(Clone, Copy)]
12880pub struct SendHeadersReader<'r>(&'r [u8]);
12881impl<'r> ::core::fmt::LowerHex for SendHeadersReader<'r> {
12882 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12883 use molecule::hex_string;
12884 if f.alternate() {
12885 write!(f, "0x")?;
12886 }
12887 write!(f, "{}", hex_string(self.as_slice()))
12888 }
12889}
12890impl<'r> ::core::fmt::Debug for SendHeadersReader<'r> {
12891 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12892 write!(f, "{}({:#x})", Self::NAME, self)
12893 }
12894}
12895impl<'r> ::core::fmt::Display for SendHeadersReader<'r> {
12896 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
12897 write!(f, "{} {{ ", Self::NAME)?;
12898 write!(f, "{}: {}", "headers", self.headers())?;
12899 let extra_count = self.count_extra_fields();
12900 if extra_count != 0 {
12901 write!(f, ", .. ({} fields)", extra_count)?;
12902 }
12903 write!(f, " }}")
12904 }
12905}
12906impl<'r> SendHeadersReader<'r> {
12907 pub const FIELD_COUNT: usize = 1;
12908 pub fn total_size(&self) -> usize {
12909 molecule::unpack_number(self.as_slice()) as usize
12910 }
12911 pub fn field_count(&self) -> usize {
12912 if self.total_size() == molecule::NUMBER_SIZE {
12913 0
12914 } else {
12915 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
12916 }
12917 }
12918 pub fn count_extra_fields(&self) -> usize {
12919 self.field_count() - Self::FIELD_COUNT
12920 }
12921 pub fn has_extra_fields(&self) -> bool {
12922 Self::FIELD_COUNT != self.field_count()
12923 }
12924 pub fn headers(&self) -> HeaderVecReader<'r> {
12925 let slice = self.as_slice();
12926 let start = molecule::unpack_number(&slice[4..]) as usize;
12927 if self.has_extra_fields() {
12928 let end = molecule::unpack_number(&slice[8..]) as usize;
12929 HeaderVecReader::new_unchecked(&self.as_slice()[start..end])
12930 } else {
12931 HeaderVecReader::new_unchecked(&self.as_slice()[start..])
12932 }
12933 }
12934}
12935impl<'r> molecule::prelude::Reader<'r> for SendHeadersReader<'r> {
12936 type Entity = SendHeaders;
12937 const NAME: &'static str = "SendHeadersReader";
12938 fn to_entity(&self) -> Self::Entity {
12939 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
12940 }
12941 fn new_unchecked(slice: &'r [u8]) -> Self {
12942 SendHeadersReader(slice)
12943 }
12944 fn as_slice(&self) -> &'r [u8] {
12945 self.0
12946 }
12947 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
12948 use molecule::verification_error as ve;
12949 let slice_len = slice.len();
12950 if slice_len < molecule::NUMBER_SIZE {
12951 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
12952 }
12953 let total_size = molecule::unpack_number(slice) as usize;
12954 if slice_len != total_size {
12955 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
12956 }
12957 if slice_len < molecule::NUMBER_SIZE * 2 {
12958 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
12959 }
12960 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
12961 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
12962 return ve!(Self, OffsetsNotMatch);
12963 }
12964 if slice_len < offset_first {
12965 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
12966 }
12967 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
12968 if field_count < Self::FIELD_COUNT {
12969 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
12970 } else if !compatible && field_count > Self::FIELD_COUNT {
12971 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
12972 };
12973 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
12974 .chunks_exact(molecule::NUMBER_SIZE)
12975 .map(|x| molecule::unpack_number(x) as usize)
12976 .collect();
12977 offsets.push(total_size);
12978 if offsets.windows(2).any(|i| i[0] > i[1]) {
12979 return ve!(Self, OffsetsNotMatch);
12980 }
12981 HeaderVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
12982 Ok(())
12983 }
12984}
12985#[derive(Debug, Default)]
12986pub struct SendHeadersBuilder {
12987 pub(crate) headers: HeaderVec,
12988}
12989impl SendHeadersBuilder {
12990 pub const FIELD_COUNT: usize = 1;
12991 pub fn headers(mut self, v: HeaderVec) -> Self {
12992 self.headers = v;
12993 self
12994 }
12995}
12996impl molecule::prelude::Builder for SendHeadersBuilder {
12997 type Entity = SendHeaders;
12998 const NAME: &'static str = "SendHeadersBuilder";
12999 fn expected_length(&self) -> usize {
13000 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.headers.as_slice().len()
13001 }
13002 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
13003 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
13004 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
13005 offsets.push(total_size);
13006 total_size += self.headers.as_slice().len();
13007 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
13008 for offset in offsets.into_iter() {
13009 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
13010 }
13011 writer.write_all(self.headers.as_slice())?;
13012 Ok(())
13013 }
13014 fn build(&self) -> Self::Entity {
13015 let mut inner = Vec::with_capacity(self.expected_length());
13016 self.write(&mut inner)
13017 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
13018 SendHeaders::new_unchecked(inner.into())
13019 }
13020}
13021#[derive(Clone)]
13022pub struct SendBlock(molecule::bytes::Bytes);
13023impl ::core::fmt::LowerHex for SendBlock {
13024 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13025 use molecule::hex_string;
13026 if f.alternate() {
13027 write!(f, "0x")?;
13028 }
13029 write!(f, "{}", hex_string(self.as_slice()))
13030 }
13031}
13032impl ::core::fmt::Debug for SendBlock {
13033 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13034 write!(f, "{}({:#x})", Self::NAME, self)
13035 }
13036}
13037impl ::core::fmt::Display for SendBlock {
13038 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13039 write!(f, "{} {{ ", Self::NAME)?;
13040 write!(f, "{}: {}", "block", self.block())?;
13041 let extra_count = self.count_extra_fields();
13042 if extra_count != 0 {
13043 write!(f, ", .. ({} fields)", extra_count)?;
13044 }
13045 write!(f, " }}")
13046 }
13047}
13048impl ::core::default::Default for SendBlock {
13049 fn default() -> Self {
13050 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
13051 SendBlock::new_unchecked(v)
13052 }
13053}
13054impl SendBlock {
13055 const DEFAULT_VALUE: [u8; 248] = [
13056 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,
13057 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13058 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13059 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13060 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13061 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13062 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13063 0, 0, 0, 0, 0, 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,
13064 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
13065 ];
13066 pub const FIELD_COUNT: usize = 1;
13067 pub fn total_size(&self) -> usize {
13068 molecule::unpack_number(self.as_slice()) as usize
13069 }
13070 pub fn field_count(&self) -> usize {
13071 if self.total_size() == molecule::NUMBER_SIZE {
13072 0
13073 } else {
13074 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13075 }
13076 }
13077 pub fn count_extra_fields(&self) -> usize {
13078 self.field_count() - Self::FIELD_COUNT
13079 }
13080 pub fn has_extra_fields(&self) -> bool {
13081 Self::FIELD_COUNT != self.field_count()
13082 }
13083 pub fn block(&self) -> Block {
13084 let slice = self.as_slice();
13085 let start = molecule::unpack_number(&slice[4..]) as usize;
13086 if self.has_extra_fields() {
13087 let end = molecule::unpack_number(&slice[8..]) as usize;
13088 Block::new_unchecked(self.0.slice(start..end))
13089 } else {
13090 Block::new_unchecked(self.0.slice(start..))
13091 }
13092 }
13093 pub fn as_reader<'r>(&'r self) -> SendBlockReader<'r> {
13094 SendBlockReader::new_unchecked(self.as_slice())
13095 }
13096}
13097impl molecule::prelude::Entity for SendBlock {
13098 type Builder = SendBlockBuilder;
13099 const NAME: &'static str = "SendBlock";
13100 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
13101 SendBlock(data)
13102 }
13103 fn as_bytes(&self) -> molecule::bytes::Bytes {
13104 self.0.clone()
13105 }
13106 fn as_slice(&self) -> &[u8] {
13107 &self.0[..]
13108 }
13109 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13110 SendBlockReader::from_slice(slice).map(|reader| reader.to_entity())
13111 }
13112 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13113 SendBlockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
13114 }
13115 fn new_builder() -> Self::Builder {
13116 ::core::default::Default::default()
13117 }
13118 fn as_builder(self) -> Self::Builder {
13119 Self::new_builder().block(self.block())
13120 }
13121}
13122#[derive(Clone, Copy)]
13123pub struct SendBlockReader<'r>(&'r [u8]);
13124impl<'r> ::core::fmt::LowerHex for SendBlockReader<'r> {
13125 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13126 use molecule::hex_string;
13127 if f.alternate() {
13128 write!(f, "0x")?;
13129 }
13130 write!(f, "{}", hex_string(self.as_slice()))
13131 }
13132}
13133impl<'r> ::core::fmt::Debug for SendBlockReader<'r> {
13134 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13135 write!(f, "{}({:#x})", Self::NAME, self)
13136 }
13137}
13138impl<'r> ::core::fmt::Display for SendBlockReader<'r> {
13139 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13140 write!(f, "{} {{ ", Self::NAME)?;
13141 write!(f, "{}: {}", "block", self.block())?;
13142 let extra_count = self.count_extra_fields();
13143 if extra_count != 0 {
13144 write!(f, ", .. ({} fields)", extra_count)?;
13145 }
13146 write!(f, " }}")
13147 }
13148}
13149impl<'r> SendBlockReader<'r> {
13150 pub const FIELD_COUNT: usize = 1;
13151 pub fn total_size(&self) -> usize {
13152 molecule::unpack_number(self.as_slice()) as usize
13153 }
13154 pub fn field_count(&self) -> usize {
13155 if self.total_size() == molecule::NUMBER_SIZE {
13156 0
13157 } else {
13158 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13159 }
13160 }
13161 pub fn count_extra_fields(&self) -> usize {
13162 self.field_count() - Self::FIELD_COUNT
13163 }
13164 pub fn has_extra_fields(&self) -> bool {
13165 Self::FIELD_COUNT != self.field_count()
13166 }
13167 pub fn block(&self) -> BlockReader<'r> {
13168 let slice = self.as_slice();
13169 let start = molecule::unpack_number(&slice[4..]) as usize;
13170 if self.has_extra_fields() {
13171 let end = molecule::unpack_number(&slice[8..]) as usize;
13172 BlockReader::new_unchecked(&self.as_slice()[start..end])
13173 } else {
13174 BlockReader::new_unchecked(&self.as_slice()[start..])
13175 }
13176 }
13177}
13178impl<'r> molecule::prelude::Reader<'r> for SendBlockReader<'r> {
13179 type Entity = SendBlock;
13180 const NAME: &'static str = "SendBlockReader";
13181 fn to_entity(&self) -> Self::Entity {
13182 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
13183 }
13184 fn new_unchecked(slice: &'r [u8]) -> Self {
13185 SendBlockReader(slice)
13186 }
13187 fn as_slice(&self) -> &'r [u8] {
13188 self.0
13189 }
13190 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
13191 use molecule::verification_error as ve;
13192 let slice_len = slice.len();
13193 if slice_len < molecule::NUMBER_SIZE {
13194 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
13195 }
13196 let total_size = molecule::unpack_number(slice) as usize;
13197 if slice_len != total_size {
13198 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
13199 }
13200 if slice_len < molecule::NUMBER_SIZE * 2 {
13201 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
13202 }
13203 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
13204 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
13205 return ve!(Self, OffsetsNotMatch);
13206 }
13207 if slice_len < offset_first {
13208 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
13209 }
13210 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
13211 if field_count < Self::FIELD_COUNT {
13212 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13213 } else if !compatible && field_count > Self::FIELD_COUNT {
13214 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13215 };
13216 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
13217 .chunks_exact(molecule::NUMBER_SIZE)
13218 .map(|x| molecule::unpack_number(x) as usize)
13219 .collect();
13220 offsets.push(total_size);
13221 if offsets.windows(2).any(|i| i[0] > i[1]) {
13222 return ve!(Self, OffsetsNotMatch);
13223 }
13224 BlockReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
13225 Ok(())
13226 }
13227}
13228#[derive(Debug, Default)]
13229pub struct SendBlockBuilder {
13230 pub(crate) block: Block,
13231}
13232impl SendBlockBuilder {
13233 pub const FIELD_COUNT: usize = 1;
13234 pub fn block(mut self, v: Block) -> Self {
13235 self.block = v;
13236 self
13237 }
13238}
13239impl molecule::prelude::Builder for SendBlockBuilder {
13240 type Entity = SendBlock;
13241 const NAME: &'static str = "SendBlockBuilder";
13242 fn expected_length(&self) -> usize {
13243 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.block.as_slice().len()
13244 }
13245 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
13246 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
13247 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
13248 offsets.push(total_size);
13249 total_size += self.block.as_slice().len();
13250 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
13251 for offset in offsets.into_iter() {
13252 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
13253 }
13254 writer.write_all(self.block.as_slice())?;
13255 Ok(())
13256 }
13257 fn build(&self) -> Self::Entity {
13258 let mut inner = Vec::with_capacity(self.expected_length());
13259 self.write(&mut inner)
13260 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
13261 SendBlock::new_unchecked(inner.into())
13262 }
13263}
13264#[derive(Clone)]
13265pub struct FilteredBlock(molecule::bytes::Bytes);
13266impl ::core::fmt::LowerHex for FilteredBlock {
13267 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13268 use molecule::hex_string;
13269 if f.alternate() {
13270 write!(f, "0x")?;
13271 }
13272 write!(f, "{}", hex_string(self.as_slice()))
13273 }
13274}
13275impl ::core::fmt::Debug for FilteredBlock {
13276 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13277 write!(f, "{}({:#x})", Self::NAME, self)
13278 }
13279}
13280impl ::core::fmt::Display for FilteredBlock {
13281 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13282 write!(f, "{} {{ ", Self::NAME)?;
13283 write!(f, "{}: {}", "header", self.header())?;
13284 write!(f, ", {}: {}", "witnesses_root", self.witnesses_root())?;
13285 write!(f, ", {}: {}", "transactions", self.transactions())?;
13286 write!(f, ", {}: {}", "proof", self.proof())?;
13287 let extra_count = self.count_extra_fields();
13288 if extra_count != 0 {
13289 write!(f, ", .. ({} fields)", extra_count)?;
13290 }
13291 write!(f, " }}")
13292 }
13293}
13294impl ::core::default::Default for FilteredBlock {
13295 fn default() -> Self {
13296 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
13297 FilteredBlock::new_unchecked(v)
13298 }
13299}
13300impl FilteredBlock {
13301 const DEFAULT_VALUE: [u8; 284] = [
13302 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,
13303 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13304 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13305 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13306 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13307 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13308 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13309 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13310 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,
13311 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13312 ];
13313 pub const FIELD_COUNT: usize = 4;
13314 pub fn total_size(&self) -> usize {
13315 molecule::unpack_number(self.as_slice()) as usize
13316 }
13317 pub fn field_count(&self) -> usize {
13318 if self.total_size() == molecule::NUMBER_SIZE {
13319 0
13320 } else {
13321 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13322 }
13323 }
13324 pub fn count_extra_fields(&self) -> usize {
13325 self.field_count() - Self::FIELD_COUNT
13326 }
13327 pub fn has_extra_fields(&self) -> bool {
13328 Self::FIELD_COUNT != self.field_count()
13329 }
13330 pub fn header(&self) -> Header {
13331 let slice = self.as_slice();
13332 let start = molecule::unpack_number(&slice[4..]) as usize;
13333 let end = molecule::unpack_number(&slice[8..]) as usize;
13334 Header::new_unchecked(self.0.slice(start..end))
13335 }
13336 pub fn witnesses_root(&self) -> Byte32 {
13337 let slice = self.as_slice();
13338 let start = molecule::unpack_number(&slice[8..]) as usize;
13339 let end = molecule::unpack_number(&slice[12..]) as usize;
13340 Byte32::new_unchecked(self.0.slice(start..end))
13341 }
13342 pub fn transactions(&self) -> TransactionVec {
13343 let slice = self.as_slice();
13344 let start = molecule::unpack_number(&slice[12..]) as usize;
13345 let end = molecule::unpack_number(&slice[16..]) as usize;
13346 TransactionVec::new_unchecked(self.0.slice(start..end))
13347 }
13348 pub fn proof(&self) -> MerkleProof {
13349 let slice = self.as_slice();
13350 let start = molecule::unpack_number(&slice[16..]) as usize;
13351 if self.has_extra_fields() {
13352 let end = molecule::unpack_number(&slice[20..]) as usize;
13353 MerkleProof::new_unchecked(self.0.slice(start..end))
13354 } else {
13355 MerkleProof::new_unchecked(self.0.slice(start..))
13356 }
13357 }
13358 pub fn as_reader<'r>(&'r self) -> FilteredBlockReader<'r> {
13359 FilteredBlockReader::new_unchecked(self.as_slice())
13360 }
13361}
13362impl molecule::prelude::Entity for FilteredBlock {
13363 type Builder = FilteredBlockBuilder;
13364 const NAME: &'static str = "FilteredBlock";
13365 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
13366 FilteredBlock(data)
13367 }
13368 fn as_bytes(&self) -> molecule::bytes::Bytes {
13369 self.0.clone()
13370 }
13371 fn as_slice(&self) -> &[u8] {
13372 &self.0[..]
13373 }
13374 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13375 FilteredBlockReader::from_slice(slice).map(|reader| reader.to_entity())
13376 }
13377 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13378 FilteredBlockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
13379 }
13380 fn new_builder() -> Self::Builder {
13381 ::core::default::Default::default()
13382 }
13383 fn as_builder(self) -> Self::Builder {
13384 Self::new_builder()
13385 .header(self.header())
13386 .witnesses_root(self.witnesses_root())
13387 .transactions(self.transactions())
13388 .proof(self.proof())
13389 }
13390}
13391#[derive(Clone, Copy)]
13392pub struct FilteredBlockReader<'r>(&'r [u8]);
13393impl<'r> ::core::fmt::LowerHex for FilteredBlockReader<'r> {
13394 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13395 use molecule::hex_string;
13396 if f.alternate() {
13397 write!(f, "0x")?;
13398 }
13399 write!(f, "{}", hex_string(self.as_slice()))
13400 }
13401}
13402impl<'r> ::core::fmt::Debug for FilteredBlockReader<'r> {
13403 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13404 write!(f, "{}({:#x})", Self::NAME, self)
13405 }
13406}
13407impl<'r> ::core::fmt::Display for FilteredBlockReader<'r> {
13408 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13409 write!(f, "{} {{ ", Self::NAME)?;
13410 write!(f, "{}: {}", "header", self.header())?;
13411 write!(f, ", {}: {}", "witnesses_root", self.witnesses_root())?;
13412 write!(f, ", {}: {}", "transactions", self.transactions())?;
13413 write!(f, ", {}: {}", "proof", self.proof())?;
13414 let extra_count = self.count_extra_fields();
13415 if extra_count != 0 {
13416 write!(f, ", .. ({} fields)", extra_count)?;
13417 }
13418 write!(f, " }}")
13419 }
13420}
13421impl<'r> FilteredBlockReader<'r> {
13422 pub const FIELD_COUNT: usize = 4;
13423 pub fn total_size(&self) -> usize {
13424 molecule::unpack_number(self.as_slice()) as usize
13425 }
13426 pub fn field_count(&self) -> usize {
13427 if self.total_size() == molecule::NUMBER_SIZE {
13428 0
13429 } else {
13430 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13431 }
13432 }
13433 pub fn count_extra_fields(&self) -> usize {
13434 self.field_count() - Self::FIELD_COUNT
13435 }
13436 pub fn has_extra_fields(&self) -> bool {
13437 Self::FIELD_COUNT != self.field_count()
13438 }
13439 pub fn header(&self) -> HeaderReader<'r> {
13440 let slice = self.as_slice();
13441 let start = molecule::unpack_number(&slice[4..]) as usize;
13442 let end = molecule::unpack_number(&slice[8..]) as usize;
13443 HeaderReader::new_unchecked(&self.as_slice()[start..end])
13444 }
13445 pub fn witnesses_root(&self) -> Byte32Reader<'r> {
13446 let slice = self.as_slice();
13447 let start = molecule::unpack_number(&slice[8..]) as usize;
13448 let end = molecule::unpack_number(&slice[12..]) as usize;
13449 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
13450 }
13451 pub fn transactions(&self) -> TransactionVecReader<'r> {
13452 let slice = self.as_slice();
13453 let start = molecule::unpack_number(&slice[12..]) as usize;
13454 let end = molecule::unpack_number(&slice[16..]) as usize;
13455 TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
13456 }
13457 pub fn proof(&self) -> MerkleProofReader<'r> {
13458 let slice = self.as_slice();
13459 let start = molecule::unpack_number(&slice[16..]) as usize;
13460 if self.has_extra_fields() {
13461 let end = molecule::unpack_number(&slice[20..]) as usize;
13462 MerkleProofReader::new_unchecked(&self.as_slice()[start..end])
13463 } else {
13464 MerkleProofReader::new_unchecked(&self.as_slice()[start..])
13465 }
13466 }
13467}
13468impl<'r> molecule::prelude::Reader<'r> for FilteredBlockReader<'r> {
13469 type Entity = FilteredBlock;
13470 const NAME: &'static str = "FilteredBlockReader";
13471 fn to_entity(&self) -> Self::Entity {
13472 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
13473 }
13474 fn new_unchecked(slice: &'r [u8]) -> Self {
13475 FilteredBlockReader(slice)
13476 }
13477 fn as_slice(&self) -> &'r [u8] {
13478 self.0
13479 }
13480 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
13481 use molecule::verification_error as ve;
13482 let slice_len = slice.len();
13483 if slice_len < molecule::NUMBER_SIZE {
13484 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
13485 }
13486 let total_size = molecule::unpack_number(slice) as usize;
13487 if slice_len != total_size {
13488 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
13489 }
13490 if slice_len < molecule::NUMBER_SIZE * 2 {
13491 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
13492 }
13493 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
13494 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
13495 return ve!(Self, OffsetsNotMatch);
13496 }
13497 if slice_len < offset_first {
13498 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
13499 }
13500 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
13501 if field_count < Self::FIELD_COUNT {
13502 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13503 } else if !compatible && field_count > Self::FIELD_COUNT {
13504 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13505 };
13506 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
13507 .chunks_exact(molecule::NUMBER_SIZE)
13508 .map(|x| molecule::unpack_number(x) as usize)
13509 .collect();
13510 offsets.push(total_size);
13511 if offsets.windows(2).any(|i| i[0] > i[1]) {
13512 return ve!(Self, OffsetsNotMatch);
13513 }
13514 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
13515 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
13516 TransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
13517 MerkleProofReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
13518 Ok(())
13519 }
13520}
13521#[derive(Debug, Default)]
13522pub struct FilteredBlockBuilder {
13523 pub(crate) header: Header,
13524 pub(crate) witnesses_root: Byte32,
13525 pub(crate) transactions: TransactionVec,
13526 pub(crate) proof: MerkleProof,
13527}
13528impl FilteredBlockBuilder {
13529 pub const FIELD_COUNT: usize = 4;
13530 pub fn header(mut self, v: Header) -> Self {
13531 self.header = v;
13532 self
13533 }
13534 pub fn witnesses_root(mut self, v: Byte32) -> Self {
13535 self.witnesses_root = v;
13536 self
13537 }
13538 pub fn transactions(mut self, v: TransactionVec) -> Self {
13539 self.transactions = v;
13540 self
13541 }
13542 pub fn proof(mut self, v: MerkleProof) -> Self {
13543 self.proof = v;
13544 self
13545 }
13546}
13547impl molecule::prelude::Builder for FilteredBlockBuilder {
13548 type Entity = FilteredBlock;
13549 const NAME: &'static str = "FilteredBlockBuilder";
13550 fn expected_length(&self) -> usize {
13551 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
13552 + self.header.as_slice().len()
13553 + self.witnesses_root.as_slice().len()
13554 + self.transactions.as_slice().len()
13555 + self.proof.as_slice().len()
13556 }
13557 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
13558 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
13559 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
13560 offsets.push(total_size);
13561 total_size += self.header.as_slice().len();
13562 offsets.push(total_size);
13563 total_size += self.witnesses_root.as_slice().len();
13564 offsets.push(total_size);
13565 total_size += self.transactions.as_slice().len();
13566 offsets.push(total_size);
13567 total_size += self.proof.as_slice().len();
13568 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
13569 for offset in offsets.into_iter() {
13570 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
13571 }
13572 writer.write_all(self.header.as_slice())?;
13573 writer.write_all(self.witnesses_root.as_slice())?;
13574 writer.write_all(self.transactions.as_slice())?;
13575 writer.write_all(self.proof.as_slice())?;
13576 Ok(())
13577 }
13578 fn build(&self) -> Self::Entity {
13579 let mut inner = Vec::with_capacity(self.expected_length());
13580 self.write(&mut inner)
13581 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
13582 FilteredBlock::new_unchecked(inner.into())
13583 }
13584}
13585#[derive(Clone)]
13586pub struct MerkleProof(molecule::bytes::Bytes);
13587impl ::core::fmt::LowerHex for MerkleProof {
13588 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13589 use molecule::hex_string;
13590 if f.alternate() {
13591 write!(f, "0x")?;
13592 }
13593 write!(f, "{}", hex_string(self.as_slice()))
13594 }
13595}
13596impl ::core::fmt::Debug for MerkleProof {
13597 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13598 write!(f, "{}({:#x})", Self::NAME, self)
13599 }
13600}
13601impl ::core::fmt::Display for MerkleProof {
13602 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13603 write!(f, "{} {{ ", Self::NAME)?;
13604 write!(f, "{}: {}", "indices", self.indices())?;
13605 write!(f, ", {}: {}", "lemmas", self.lemmas())?;
13606 let extra_count = self.count_extra_fields();
13607 if extra_count != 0 {
13608 write!(f, ", .. ({} fields)", extra_count)?;
13609 }
13610 write!(f, " }}")
13611 }
13612}
13613impl ::core::default::Default for MerkleProof {
13614 fn default() -> Self {
13615 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
13616 MerkleProof::new_unchecked(v)
13617 }
13618}
13619impl MerkleProof {
13620 const DEFAULT_VALUE: [u8; 20] = [
13621 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13622 ];
13623 pub const FIELD_COUNT: usize = 2;
13624 pub fn total_size(&self) -> usize {
13625 molecule::unpack_number(self.as_slice()) as usize
13626 }
13627 pub fn field_count(&self) -> usize {
13628 if self.total_size() == molecule::NUMBER_SIZE {
13629 0
13630 } else {
13631 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13632 }
13633 }
13634 pub fn count_extra_fields(&self) -> usize {
13635 self.field_count() - Self::FIELD_COUNT
13636 }
13637 pub fn has_extra_fields(&self) -> bool {
13638 Self::FIELD_COUNT != self.field_count()
13639 }
13640 pub fn indices(&self) -> Uint32Vec {
13641 let slice = self.as_slice();
13642 let start = molecule::unpack_number(&slice[4..]) as usize;
13643 let end = molecule::unpack_number(&slice[8..]) as usize;
13644 Uint32Vec::new_unchecked(self.0.slice(start..end))
13645 }
13646 pub fn lemmas(&self) -> Byte32Vec {
13647 let slice = self.as_slice();
13648 let start = molecule::unpack_number(&slice[8..]) as usize;
13649 if self.has_extra_fields() {
13650 let end = molecule::unpack_number(&slice[12..]) as usize;
13651 Byte32Vec::new_unchecked(self.0.slice(start..end))
13652 } else {
13653 Byte32Vec::new_unchecked(self.0.slice(start..))
13654 }
13655 }
13656 pub fn as_reader<'r>(&'r self) -> MerkleProofReader<'r> {
13657 MerkleProofReader::new_unchecked(self.as_slice())
13658 }
13659}
13660impl molecule::prelude::Entity for MerkleProof {
13661 type Builder = MerkleProofBuilder;
13662 const NAME: &'static str = "MerkleProof";
13663 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
13664 MerkleProof(data)
13665 }
13666 fn as_bytes(&self) -> molecule::bytes::Bytes {
13667 self.0.clone()
13668 }
13669 fn as_slice(&self) -> &[u8] {
13670 &self.0[..]
13671 }
13672 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13673 MerkleProofReader::from_slice(slice).map(|reader| reader.to_entity())
13674 }
13675 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13676 MerkleProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
13677 }
13678 fn new_builder() -> Self::Builder {
13679 ::core::default::Default::default()
13680 }
13681 fn as_builder(self) -> Self::Builder {
13682 Self::new_builder()
13683 .indices(self.indices())
13684 .lemmas(self.lemmas())
13685 }
13686}
13687#[derive(Clone, Copy)]
13688pub struct MerkleProofReader<'r>(&'r [u8]);
13689impl<'r> ::core::fmt::LowerHex for MerkleProofReader<'r> {
13690 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13691 use molecule::hex_string;
13692 if f.alternate() {
13693 write!(f, "0x")?;
13694 }
13695 write!(f, "{}", hex_string(self.as_slice()))
13696 }
13697}
13698impl<'r> ::core::fmt::Debug for MerkleProofReader<'r> {
13699 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13700 write!(f, "{}({:#x})", Self::NAME, self)
13701 }
13702}
13703impl<'r> ::core::fmt::Display for MerkleProofReader<'r> {
13704 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13705 write!(f, "{} {{ ", Self::NAME)?;
13706 write!(f, "{}: {}", "indices", self.indices())?;
13707 write!(f, ", {}: {}", "lemmas", self.lemmas())?;
13708 let extra_count = self.count_extra_fields();
13709 if extra_count != 0 {
13710 write!(f, ", .. ({} fields)", extra_count)?;
13711 }
13712 write!(f, " }}")
13713 }
13714}
13715impl<'r> MerkleProofReader<'r> {
13716 pub const FIELD_COUNT: usize = 2;
13717 pub fn total_size(&self) -> usize {
13718 molecule::unpack_number(self.as_slice()) as usize
13719 }
13720 pub fn field_count(&self) -> usize {
13721 if self.total_size() == molecule::NUMBER_SIZE {
13722 0
13723 } else {
13724 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13725 }
13726 }
13727 pub fn count_extra_fields(&self) -> usize {
13728 self.field_count() - Self::FIELD_COUNT
13729 }
13730 pub fn has_extra_fields(&self) -> bool {
13731 Self::FIELD_COUNT != self.field_count()
13732 }
13733 pub fn indices(&self) -> Uint32VecReader<'r> {
13734 let slice = self.as_slice();
13735 let start = molecule::unpack_number(&slice[4..]) as usize;
13736 let end = molecule::unpack_number(&slice[8..]) as usize;
13737 Uint32VecReader::new_unchecked(&self.as_slice()[start..end])
13738 }
13739 pub fn lemmas(&self) -> Byte32VecReader<'r> {
13740 let slice = self.as_slice();
13741 let start = molecule::unpack_number(&slice[8..]) as usize;
13742 if self.has_extra_fields() {
13743 let end = molecule::unpack_number(&slice[12..]) as usize;
13744 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
13745 } else {
13746 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
13747 }
13748 }
13749}
13750impl<'r> molecule::prelude::Reader<'r> for MerkleProofReader<'r> {
13751 type Entity = MerkleProof;
13752 const NAME: &'static str = "MerkleProofReader";
13753 fn to_entity(&self) -> Self::Entity {
13754 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
13755 }
13756 fn new_unchecked(slice: &'r [u8]) -> Self {
13757 MerkleProofReader(slice)
13758 }
13759 fn as_slice(&self) -> &'r [u8] {
13760 self.0
13761 }
13762 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
13763 use molecule::verification_error as ve;
13764 let slice_len = slice.len();
13765 if slice_len < molecule::NUMBER_SIZE {
13766 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
13767 }
13768 let total_size = molecule::unpack_number(slice) as usize;
13769 if slice_len != total_size {
13770 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
13771 }
13772 if slice_len < molecule::NUMBER_SIZE * 2 {
13773 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
13774 }
13775 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
13776 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
13777 return ve!(Self, OffsetsNotMatch);
13778 }
13779 if slice_len < offset_first {
13780 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
13781 }
13782 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
13783 if field_count < Self::FIELD_COUNT {
13784 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13785 } else if !compatible && field_count > Self::FIELD_COUNT {
13786 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
13787 };
13788 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
13789 .chunks_exact(molecule::NUMBER_SIZE)
13790 .map(|x| molecule::unpack_number(x) as usize)
13791 .collect();
13792 offsets.push(total_size);
13793 if offsets.windows(2).any(|i| i[0] > i[1]) {
13794 return ve!(Self, OffsetsNotMatch);
13795 }
13796 Uint32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
13797 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
13798 Ok(())
13799 }
13800}
13801#[derive(Debug, Default)]
13802pub struct MerkleProofBuilder {
13803 pub(crate) indices: Uint32Vec,
13804 pub(crate) lemmas: Byte32Vec,
13805}
13806impl MerkleProofBuilder {
13807 pub const FIELD_COUNT: usize = 2;
13808 pub fn indices(mut self, v: Uint32Vec) -> Self {
13809 self.indices = v;
13810 self
13811 }
13812 pub fn lemmas(mut self, v: Byte32Vec) -> Self {
13813 self.lemmas = v;
13814 self
13815 }
13816}
13817impl molecule::prelude::Builder for MerkleProofBuilder {
13818 type Entity = MerkleProof;
13819 const NAME: &'static str = "MerkleProofBuilder";
13820 fn expected_length(&self) -> usize {
13821 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
13822 + self.indices.as_slice().len()
13823 + self.lemmas.as_slice().len()
13824 }
13825 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
13826 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
13827 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
13828 offsets.push(total_size);
13829 total_size += self.indices.as_slice().len();
13830 offsets.push(total_size);
13831 total_size += self.lemmas.as_slice().len();
13832 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
13833 for offset in offsets.into_iter() {
13834 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
13835 }
13836 writer.write_all(self.indices.as_slice())?;
13837 writer.write_all(self.lemmas.as_slice())?;
13838 Ok(())
13839 }
13840 fn build(&self) -> Self::Entity {
13841 let mut inner = Vec::with_capacity(self.expected_length());
13842 self.write(&mut inner)
13843 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
13844 MerkleProof::new_unchecked(inner.into())
13845 }
13846}
13847#[derive(Clone)]
13848pub struct InIBD(molecule::bytes::Bytes);
13849impl ::core::fmt::LowerHex for InIBD {
13850 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13851 use molecule::hex_string;
13852 if f.alternate() {
13853 write!(f, "0x")?;
13854 }
13855 write!(f, "{}", hex_string(self.as_slice()))
13856 }
13857}
13858impl ::core::fmt::Debug for InIBD {
13859 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13860 write!(f, "{}({:#x})", Self::NAME, self)
13861 }
13862}
13863impl ::core::fmt::Display for InIBD {
13864 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13865 write!(f, "{} {{ ", Self::NAME)?;
13866 let extra_count = self.count_extra_fields();
13867 if extra_count != 0 {
13868 write!(f, ".. ({} fields)", extra_count)?;
13869 }
13870 write!(f, " }}")
13871 }
13872}
13873impl ::core::default::Default for InIBD {
13874 fn default() -> Self {
13875 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
13876 InIBD::new_unchecked(v)
13877 }
13878}
13879impl InIBD {
13880 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
13881 pub const FIELD_COUNT: usize = 0;
13882 pub fn total_size(&self) -> usize {
13883 molecule::unpack_number(self.as_slice()) as usize
13884 }
13885 pub fn field_count(&self) -> usize {
13886 if self.total_size() == molecule::NUMBER_SIZE {
13887 0
13888 } else {
13889 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13890 }
13891 }
13892 pub fn count_extra_fields(&self) -> usize {
13893 self.field_count() - Self::FIELD_COUNT
13894 }
13895 pub fn has_extra_fields(&self) -> bool {
13896 Self::FIELD_COUNT != self.field_count()
13897 }
13898 pub fn as_reader<'r>(&'r self) -> InIBDReader<'r> {
13899 InIBDReader::new_unchecked(self.as_slice())
13900 }
13901}
13902impl molecule::prelude::Entity for InIBD {
13903 type Builder = InIBDBuilder;
13904 const NAME: &'static str = "InIBD";
13905 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
13906 InIBD(data)
13907 }
13908 fn as_bytes(&self) -> molecule::bytes::Bytes {
13909 self.0.clone()
13910 }
13911 fn as_slice(&self) -> &[u8] {
13912 &self.0[..]
13913 }
13914 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13915 InIBDReader::from_slice(slice).map(|reader| reader.to_entity())
13916 }
13917 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
13918 InIBDReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
13919 }
13920 fn new_builder() -> Self::Builder {
13921 ::core::default::Default::default()
13922 }
13923 fn as_builder(self) -> Self::Builder {
13924 Self::new_builder()
13925 }
13926}
13927#[derive(Clone, Copy)]
13928pub struct InIBDReader<'r>(&'r [u8]);
13929impl<'r> ::core::fmt::LowerHex for InIBDReader<'r> {
13930 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13931 use molecule::hex_string;
13932 if f.alternate() {
13933 write!(f, "0x")?;
13934 }
13935 write!(f, "{}", hex_string(self.as_slice()))
13936 }
13937}
13938impl<'r> ::core::fmt::Debug for InIBDReader<'r> {
13939 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13940 write!(f, "{}({:#x})", Self::NAME, self)
13941 }
13942}
13943impl<'r> ::core::fmt::Display for InIBDReader<'r> {
13944 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13945 write!(f, "{} {{ ", Self::NAME)?;
13946 let extra_count = self.count_extra_fields();
13947 if extra_count != 0 {
13948 write!(f, ".. ({} fields)", extra_count)?;
13949 }
13950 write!(f, " }}")
13951 }
13952}
13953impl<'r> InIBDReader<'r> {
13954 pub const FIELD_COUNT: usize = 0;
13955 pub fn total_size(&self) -> usize {
13956 molecule::unpack_number(self.as_slice()) as usize
13957 }
13958 pub fn field_count(&self) -> usize {
13959 if self.total_size() == molecule::NUMBER_SIZE {
13960 0
13961 } else {
13962 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
13963 }
13964 }
13965 pub fn count_extra_fields(&self) -> usize {
13966 self.field_count() - Self::FIELD_COUNT
13967 }
13968 pub fn has_extra_fields(&self) -> bool {
13969 Self::FIELD_COUNT != self.field_count()
13970 }
13971}
13972impl<'r> molecule::prelude::Reader<'r> for InIBDReader<'r> {
13973 type Entity = InIBD;
13974 const NAME: &'static str = "InIBDReader";
13975 fn to_entity(&self) -> Self::Entity {
13976 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
13977 }
13978 fn new_unchecked(slice: &'r [u8]) -> Self {
13979 InIBDReader(slice)
13980 }
13981 fn as_slice(&self) -> &'r [u8] {
13982 self.0
13983 }
13984 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
13985 use molecule::verification_error as ve;
13986 let slice_len = slice.len();
13987 if slice_len < molecule::NUMBER_SIZE {
13988 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
13989 }
13990 let total_size = molecule::unpack_number(slice) as usize;
13991 if slice_len != total_size {
13992 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
13993 }
13994 if slice_len > molecule::NUMBER_SIZE && !compatible {
13995 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, !0);
13996 }
13997 Ok(())
13998 }
13999}
14000#[derive(Debug, Default)]
14001pub struct InIBDBuilder {}
14002impl InIBDBuilder {
14003 pub const FIELD_COUNT: usize = 0;
14004}
14005impl molecule::prelude::Builder for InIBDBuilder {
14006 type Entity = InIBD;
14007 const NAME: &'static str = "InIBDBuilder";
14008 fn expected_length(&self) -> usize {
14009 molecule::NUMBER_SIZE
14010 }
14011 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
14012 writer.write_all(&molecule::pack_number(
14013 molecule::NUMBER_SIZE as molecule::Number,
14014 ))?;
14015 Ok(())
14016 }
14017 fn build(&self) -> Self::Entity {
14018 let mut inner = Vec::with_capacity(self.expected_length());
14019 self.write(&mut inner)
14020 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
14021 InIBD::new_unchecked(inner.into())
14022 }
14023}
14024#[derive(Clone)]
14025pub struct HeaderDigestVec(molecule::bytes::Bytes);
14026impl ::core::fmt::LowerHex for HeaderDigestVec {
14027 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14028 use molecule::hex_string;
14029 if f.alternate() {
14030 write!(f, "0x")?;
14031 }
14032 write!(f, "{}", hex_string(self.as_slice()))
14033 }
14034}
14035impl ::core::fmt::Debug for HeaderDigestVec {
14036 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14037 write!(f, "{}({:#x})", Self::NAME, self)
14038 }
14039}
14040impl ::core::fmt::Display for HeaderDigestVec {
14041 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14042 write!(f, "{} [", Self::NAME)?;
14043 for i in 0..self.len() {
14044 if i == 0 {
14045 write!(f, "{}", self.get_unchecked(i))?;
14046 } else {
14047 write!(f, ", {}", self.get_unchecked(i))?;
14048 }
14049 }
14050 write!(f, "]")
14051 }
14052}
14053impl ::core::default::Default for HeaderDigestVec {
14054 fn default() -> Self {
14055 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
14056 HeaderDigestVec::new_unchecked(v)
14057 }
14058}
14059impl HeaderDigestVec {
14060 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
14061 pub const ITEM_SIZE: usize = 120;
14062 pub fn total_size(&self) -> usize {
14063 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
14064 }
14065 pub fn item_count(&self) -> usize {
14066 molecule::unpack_number(self.as_slice()) as usize
14067 }
14068 pub fn len(&self) -> usize {
14069 self.item_count()
14070 }
14071 pub fn is_empty(&self) -> bool {
14072 self.len() == 0
14073 }
14074 pub fn get(&self, idx: usize) -> Option<HeaderDigest> {
14075 if idx >= self.len() {
14076 None
14077 } else {
14078 Some(self.get_unchecked(idx))
14079 }
14080 }
14081 pub fn get_unchecked(&self, idx: usize) -> HeaderDigest {
14082 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
14083 let end = start + Self::ITEM_SIZE;
14084 HeaderDigest::new_unchecked(self.0.slice(start..end))
14085 }
14086 pub fn as_reader<'r>(&'r self) -> HeaderDigestVecReader<'r> {
14087 HeaderDigestVecReader::new_unchecked(self.as_slice())
14088 }
14089}
14090impl molecule::prelude::Entity for HeaderDigestVec {
14091 type Builder = HeaderDigestVecBuilder;
14092 const NAME: &'static str = "HeaderDigestVec";
14093 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
14094 HeaderDigestVec(data)
14095 }
14096 fn as_bytes(&self) -> molecule::bytes::Bytes {
14097 self.0.clone()
14098 }
14099 fn as_slice(&self) -> &[u8] {
14100 &self.0[..]
14101 }
14102 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14103 HeaderDigestVecReader::from_slice(slice).map(|reader| reader.to_entity())
14104 }
14105 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14106 HeaderDigestVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
14107 }
14108 fn new_builder() -> Self::Builder {
14109 ::core::default::Default::default()
14110 }
14111 fn as_builder(self) -> Self::Builder {
14112 Self::new_builder().extend(self.into_iter())
14113 }
14114}
14115#[derive(Clone, Copy)]
14116pub struct HeaderDigestVecReader<'r>(&'r [u8]);
14117impl<'r> ::core::fmt::LowerHex for HeaderDigestVecReader<'r> {
14118 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14119 use molecule::hex_string;
14120 if f.alternate() {
14121 write!(f, "0x")?;
14122 }
14123 write!(f, "{}", hex_string(self.as_slice()))
14124 }
14125}
14126impl<'r> ::core::fmt::Debug for HeaderDigestVecReader<'r> {
14127 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14128 write!(f, "{}({:#x})", Self::NAME, self)
14129 }
14130}
14131impl<'r> ::core::fmt::Display for HeaderDigestVecReader<'r> {
14132 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14133 write!(f, "{} [", Self::NAME)?;
14134 for i in 0..self.len() {
14135 if i == 0 {
14136 write!(f, "{}", self.get_unchecked(i))?;
14137 } else {
14138 write!(f, ", {}", self.get_unchecked(i))?;
14139 }
14140 }
14141 write!(f, "]")
14142 }
14143}
14144impl<'r> HeaderDigestVecReader<'r> {
14145 pub const ITEM_SIZE: usize = 120;
14146 pub fn total_size(&self) -> usize {
14147 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
14148 }
14149 pub fn item_count(&self) -> usize {
14150 molecule::unpack_number(self.as_slice()) as usize
14151 }
14152 pub fn len(&self) -> usize {
14153 self.item_count()
14154 }
14155 pub fn is_empty(&self) -> bool {
14156 self.len() == 0
14157 }
14158 pub fn get(&self, idx: usize) -> Option<HeaderDigestReader<'r>> {
14159 if idx >= self.len() {
14160 None
14161 } else {
14162 Some(self.get_unchecked(idx))
14163 }
14164 }
14165 pub fn get_unchecked(&self, idx: usize) -> HeaderDigestReader<'r> {
14166 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
14167 let end = start + Self::ITEM_SIZE;
14168 HeaderDigestReader::new_unchecked(&self.as_slice()[start..end])
14169 }
14170}
14171impl<'r> molecule::prelude::Reader<'r> for HeaderDigestVecReader<'r> {
14172 type Entity = HeaderDigestVec;
14173 const NAME: &'static str = "HeaderDigestVecReader";
14174 fn to_entity(&self) -> Self::Entity {
14175 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
14176 }
14177 fn new_unchecked(slice: &'r [u8]) -> Self {
14178 HeaderDigestVecReader(slice)
14179 }
14180 fn as_slice(&self) -> &'r [u8] {
14181 self.0
14182 }
14183 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
14184 use molecule::verification_error as ve;
14185 let slice_len = slice.len();
14186 if slice_len < molecule::NUMBER_SIZE {
14187 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
14188 }
14189 let item_count = molecule::unpack_number(slice) as usize;
14190 if item_count == 0 {
14191 if slice_len != molecule::NUMBER_SIZE {
14192 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
14193 }
14194 return Ok(());
14195 }
14196 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
14197 if slice_len != total_size {
14198 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
14199 }
14200 Ok(())
14201 }
14202}
14203#[derive(Debug, Default)]
14204pub struct HeaderDigestVecBuilder(pub(crate) Vec<HeaderDigest>);
14205impl HeaderDigestVecBuilder {
14206 pub const ITEM_SIZE: usize = 120;
14207 pub fn set(mut self, v: Vec<HeaderDigest>) -> Self {
14208 self.0 = v;
14209 self
14210 }
14211 pub fn push(mut self, v: HeaderDigest) -> Self {
14212 self.0.push(v);
14213 self
14214 }
14215 pub fn extend<T: ::core::iter::IntoIterator<Item = HeaderDigest>>(mut self, iter: T) -> Self {
14216 for elem in iter {
14217 self.0.push(elem);
14218 }
14219 self
14220 }
14221 pub fn replace(&mut self, index: usize, v: HeaderDigest) -> Option<HeaderDigest> {
14222 self.0
14223 .get_mut(index)
14224 .map(|item| ::core::mem::replace(item, v))
14225 }
14226}
14227impl molecule::prelude::Builder for HeaderDigestVecBuilder {
14228 type Entity = HeaderDigestVec;
14229 const NAME: &'static str = "HeaderDigestVecBuilder";
14230 fn expected_length(&self) -> usize {
14231 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
14232 }
14233 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
14234 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
14235 for inner in &self.0[..] {
14236 writer.write_all(inner.as_slice())?;
14237 }
14238 Ok(())
14239 }
14240 fn build(&self) -> Self::Entity {
14241 let mut inner = Vec::with_capacity(self.expected_length());
14242 self.write(&mut inner)
14243 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
14244 HeaderDigestVec::new_unchecked(inner.into())
14245 }
14246}
14247pub struct HeaderDigestVecIterator(HeaderDigestVec, usize, usize);
14248impl ::core::iter::Iterator for HeaderDigestVecIterator {
14249 type Item = HeaderDigest;
14250 fn next(&mut self) -> Option<Self::Item> {
14251 if self.1 >= self.2 {
14252 None
14253 } else {
14254 let ret = self.0.get_unchecked(self.1);
14255 self.1 += 1;
14256 Some(ret)
14257 }
14258 }
14259}
14260impl ::core::iter::ExactSizeIterator for HeaderDigestVecIterator {
14261 fn len(&self) -> usize {
14262 self.2 - self.1
14263 }
14264}
14265impl ::core::iter::IntoIterator for HeaderDigestVec {
14266 type Item = HeaderDigest;
14267 type IntoIter = HeaderDigestVecIterator;
14268 fn into_iter(self) -> Self::IntoIter {
14269 let len = self.len();
14270 HeaderDigestVecIterator(self, 0, len)
14271 }
14272}
14273impl<'r> HeaderDigestVecReader<'r> {
14274 pub fn iter<'t>(&'t self) -> HeaderDigestVecReaderIterator<'t, 'r> {
14275 HeaderDigestVecReaderIterator(&self, 0, self.len())
14276 }
14277}
14278pub struct HeaderDigestVecReaderIterator<'t, 'r>(&'t HeaderDigestVecReader<'r>, usize, usize);
14279impl<'t: 'r, 'r> ::core::iter::Iterator for HeaderDigestVecReaderIterator<'t, 'r> {
14280 type Item = HeaderDigestReader<'t>;
14281 fn next(&mut self) -> Option<Self::Item> {
14282 if self.1 >= self.2 {
14283 None
14284 } else {
14285 let ret = self.0.get_unchecked(self.1);
14286 self.1 += 1;
14287 Some(ret)
14288 }
14289 }
14290}
14291impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for HeaderDigestVecReaderIterator<'t, 'r> {
14292 fn len(&self) -> usize {
14293 self.2 - self.1
14294 }
14295}
14296#[derive(Clone)]
14297pub struct VerifiableHeader(molecule::bytes::Bytes);
14298impl ::core::fmt::LowerHex for VerifiableHeader {
14299 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14300 use molecule::hex_string;
14301 if f.alternate() {
14302 write!(f, "0x")?;
14303 }
14304 write!(f, "{}", hex_string(self.as_slice()))
14305 }
14306}
14307impl ::core::fmt::Debug for VerifiableHeader {
14308 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14309 write!(f, "{}({:#x})", Self::NAME, self)
14310 }
14311}
14312impl ::core::fmt::Display for VerifiableHeader {
14313 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14314 write!(f, "{} {{ ", Self::NAME)?;
14315 write!(f, "{}: {}", "header", self.header())?;
14316 write!(f, ", {}: {}", "uncles_hash", self.uncles_hash())?;
14317 write!(f, ", {}: {}", "extension", self.extension())?;
14318 write!(f, ", {}: {}", "parent_chain_root", self.parent_chain_root())?;
14319 let extra_count = self.count_extra_fields();
14320 if extra_count != 0 {
14321 write!(f, ", .. ({} fields)", extra_count)?;
14322 }
14323 write!(f, " }}")
14324 }
14325}
14326impl ::core::default::Default for VerifiableHeader {
14327 fn default() -> Self {
14328 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
14329 VerifiableHeader::new_unchecked(v)
14330 }
14331}
14332impl VerifiableHeader {
14333 const DEFAULT_VALUE: [u8; 380] = [
14334 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,
14335 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14336 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14337 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14338 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14339 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14340 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14341 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14342 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14343 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14344 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14345 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14346 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14347 ];
14348 pub const FIELD_COUNT: usize = 4;
14349 pub fn total_size(&self) -> usize {
14350 molecule::unpack_number(self.as_slice()) as usize
14351 }
14352 pub fn field_count(&self) -> usize {
14353 if self.total_size() == molecule::NUMBER_SIZE {
14354 0
14355 } else {
14356 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
14357 }
14358 }
14359 pub fn count_extra_fields(&self) -> usize {
14360 self.field_count() - Self::FIELD_COUNT
14361 }
14362 pub fn has_extra_fields(&self) -> bool {
14363 Self::FIELD_COUNT != self.field_count()
14364 }
14365 pub fn header(&self) -> Header {
14366 let slice = self.as_slice();
14367 let start = molecule::unpack_number(&slice[4..]) as usize;
14368 let end = molecule::unpack_number(&slice[8..]) as usize;
14369 Header::new_unchecked(self.0.slice(start..end))
14370 }
14371 pub fn uncles_hash(&self) -> Byte32 {
14372 let slice = self.as_slice();
14373 let start = molecule::unpack_number(&slice[8..]) as usize;
14374 let end = molecule::unpack_number(&slice[12..]) as usize;
14375 Byte32::new_unchecked(self.0.slice(start..end))
14376 }
14377 pub fn extension(&self) -> BytesOpt {
14378 let slice = self.as_slice();
14379 let start = molecule::unpack_number(&slice[12..]) as usize;
14380 let end = molecule::unpack_number(&slice[16..]) as usize;
14381 BytesOpt::new_unchecked(self.0.slice(start..end))
14382 }
14383 pub fn parent_chain_root(&self) -> HeaderDigest {
14384 let slice = self.as_slice();
14385 let start = molecule::unpack_number(&slice[16..]) as usize;
14386 if self.has_extra_fields() {
14387 let end = molecule::unpack_number(&slice[20..]) as usize;
14388 HeaderDigest::new_unchecked(self.0.slice(start..end))
14389 } else {
14390 HeaderDigest::new_unchecked(self.0.slice(start..))
14391 }
14392 }
14393 pub fn as_reader<'r>(&'r self) -> VerifiableHeaderReader<'r> {
14394 VerifiableHeaderReader::new_unchecked(self.as_slice())
14395 }
14396}
14397impl molecule::prelude::Entity for VerifiableHeader {
14398 type Builder = VerifiableHeaderBuilder;
14399 const NAME: &'static str = "VerifiableHeader";
14400 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
14401 VerifiableHeader(data)
14402 }
14403 fn as_bytes(&self) -> molecule::bytes::Bytes {
14404 self.0.clone()
14405 }
14406 fn as_slice(&self) -> &[u8] {
14407 &self.0[..]
14408 }
14409 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14410 VerifiableHeaderReader::from_slice(slice).map(|reader| reader.to_entity())
14411 }
14412 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14413 VerifiableHeaderReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
14414 }
14415 fn new_builder() -> Self::Builder {
14416 ::core::default::Default::default()
14417 }
14418 fn as_builder(self) -> Self::Builder {
14419 Self::new_builder()
14420 .header(self.header())
14421 .uncles_hash(self.uncles_hash())
14422 .extension(self.extension())
14423 .parent_chain_root(self.parent_chain_root())
14424 }
14425}
14426#[derive(Clone, Copy)]
14427pub struct VerifiableHeaderReader<'r>(&'r [u8]);
14428impl<'r> ::core::fmt::LowerHex for VerifiableHeaderReader<'r> {
14429 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14430 use molecule::hex_string;
14431 if f.alternate() {
14432 write!(f, "0x")?;
14433 }
14434 write!(f, "{}", hex_string(self.as_slice()))
14435 }
14436}
14437impl<'r> ::core::fmt::Debug for VerifiableHeaderReader<'r> {
14438 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14439 write!(f, "{}({:#x})", Self::NAME, self)
14440 }
14441}
14442impl<'r> ::core::fmt::Display for VerifiableHeaderReader<'r> {
14443 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14444 write!(f, "{} {{ ", Self::NAME)?;
14445 write!(f, "{}: {}", "header", self.header())?;
14446 write!(f, ", {}: {}", "uncles_hash", self.uncles_hash())?;
14447 write!(f, ", {}: {}", "extension", self.extension())?;
14448 write!(f, ", {}: {}", "parent_chain_root", self.parent_chain_root())?;
14449 let extra_count = self.count_extra_fields();
14450 if extra_count != 0 {
14451 write!(f, ", .. ({} fields)", extra_count)?;
14452 }
14453 write!(f, " }}")
14454 }
14455}
14456impl<'r> VerifiableHeaderReader<'r> {
14457 pub const FIELD_COUNT: usize = 4;
14458 pub fn total_size(&self) -> usize {
14459 molecule::unpack_number(self.as_slice()) as usize
14460 }
14461 pub fn field_count(&self) -> usize {
14462 if self.total_size() == molecule::NUMBER_SIZE {
14463 0
14464 } else {
14465 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
14466 }
14467 }
14468 pub fn count_extra_fields(&self) -> usize {
14469 self.field_count() - Self::FIELD_COUNT
14470 }
14471 pub fn has_extra_fields(&self) -> bool {
14472 Self::FIELD_COUNT != self.field_count()
14473 }
14474 pub fn header(&self) -> HeaderReader<'r> {
14475 let slice = self.as_slice();
14476 let start = molecule::unpack_number(&slice[4..]) as usize;
14477 let end = molecule::unpack_number(&slice[8..]) as usize;
14478 HeaderReader::new_unchecked(&self.as_slice()[start..end])
14479 }
14480 pub fn uncles_hash(&self) -> Byte32Reader<'r> {
14481 let slice = self.as_slice();
14482 let start = molecule::unpack_number(&slice[8..]) as usize;
14483 let end = molecule::unpack_number(&slice[12..]) as usize;
14484 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
14485 }
14486 pub fn extension(&self) -> BytesOptReader<'r> {
14487 let slice = self.as_slice();
14488 let start = molecule::unpack_number(&slice[12..]) as usize;
14489 let end = molecule::unpack_number(&slice[16..]) as usize;
14490 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
14491 }
14492 pub fn parent_chain_root(&self) -> HeaderDigestReader<'r> {
14493 let slice = self.as_slice();
14494 let start = molecule::unpack_number(&slice[16..]) as usize;
14495 if self.has_extra_fields() {
14496 let end = molecule::unpack_number(&slice[20..]) as usize;
14497 HeaderDigestReader::new_unchecked(&self.as_slice()[start..end])
14498 } else {
14499 HeaderDigestReader::new_unchecked(&self.as_slice()[start..])
14500 }
14501 }
14502}
14503impl<'r> molecule::prelude::Reader<'r> for VerifiableHeaderReader<'r> {
14504 type Entity = VerifiableHeader;
14505 const NAME: &'static str = "VerifiableHeaderReader";
14506 fn to_entity(&self) -> Self::Entity {
14507 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
14508 }
14509 fn new_unchecked(slice: &'r [u8]) -> Self {
14510 VerifiableHeaderReader(slice)
14511 }
14512 fn as_slice(&self) -> &'r [u8] {
14513 self.0
14514 }
14515 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
14516 use molecule::verification_error as ve;
14517 let slice_len = slice.len();
14518 if slice_len < molecule::NUMBER_SIZE {
14519 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
14520 }
14521 let total_size = molecule::unpack_number(slice) as usize;
14522 if slice_len != total_size {
14523 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
14524 }
14525 if slice_len < molecule::NUMBER_SIZE * 2 {
14526 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
14527 }
14528 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
14529 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
14530 return ve!(Self, OffsetsNotMatch);
14531 }
14532 if slice_len < offset_first {
14533 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
14534 }
14535 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
14536 if field_count < Self::FIELD_COUNT {
14537 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
14538 } else if !compatible && field_count > Self::FIELD_COUNT {
14539 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
14540 };
14541 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
14542 .chunks_exact(molecule::NUMBER_SIZE)
14543 .map(|x| molecule::unpack_number(x) as usize)
14544 .collect();
14545 offsets.push(total_size);
14546 if offsets.windows(2).any(|i| i[0] > i[1]) {
14547 return ve!(Self, OffsetsNotMatch);
14548 }
14549 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
14550 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
14551 BytesOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
14552 HeaderDigestReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
14553 Ok(())
14554 }
14555}
14556#[derive(Debug, Default)]
14557pub struct VerifiableHeaderBuilder {
14558 pub(crate) header: Header,
14559 pub(crate) uncles_hash: Byte32,
14560 pub(crate) extension: BytesOpt,
14561 pub(crate) parent_chain_root: HeaderDigest,
14562}
14563impl VerifiableHeaderBuilder {
14564 pub const FIELD_COUNT: usize = 4;
14565 pub fn header(mut self, v: Header) -> Self {
14566 self.header = v;
14567 self
14568 }
14569 pub fn uncles_hash(mut self, v: Byte32) -> Self {
14570 self.uncles_hash = v;
14571 self
14572 }
14573 pub fn extension(mut self, v: BytesOpt) -> Self {
14574 self.extension = v;
14575 self
14576 }
14577 pub fn parent_chain_root(mut self, v: HeaderDigest) -> Self {
14578 self.parent_chain_root = v;
14579 self
14580 }
14581}
14582impl molecule::prelude::Builder for VerifiableHeaderBuilder {
14583 type Entity = VerifiableHeader;
14584 const NAME: &'static str = "VerifiableHeaderBuilder";
14585 fn expected_length(&self) -> usize {
14586 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
14587 + self.header.as_slice().len()
14588 + self.uncles_hash.as_slice().len()
14589 + self.extension.as_slice().len()
14590 + self.parent_chain_root.as_slice().len()
14591 }
14592 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
14593 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
14594 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
14595 offsets.push(total_size);
14596 total_size += self.header.as_slice().len();
14597 offsets.push(total_size);
14598 total_size += self.uncles_hash.as_slice().len();
14599 offsets.push(total_size);
14600 total_size += self.extension.as_slice().len();
14601 offsets.push(total_size);
14602 total_size += self.parent_chain_root.as_slice().len();
14603 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
14604 for offset in offsets.into_iter() {
14605 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
14606 }
14607 writer.write_all(self.header.as_slice())?;
14608 writer.write_all(self.uncles_hash.as_slice())?;
14609 writer.write_all(self.extension.as_slice())?;
14610 writer.write_all(self.parent_chain_root.as_slice())?;
14611 Ok(())
14612 }
14613 fn build(&self) -> Self::Entity {
14614 let mut inner = Vec::with_capacity(self.expected_length());
14615 self.write(&mut inner)
14616 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
14617 VerifiableHeader::new_unchecked(inner.into())
14618 }
14619}
14620#[derive(Clone)]
14621pub struct VerifiableHeaderVec(molecule::bytes::Bytes);
14622impl ::core::fmt::LowerHex for VerifiableHeaderVec {
14623 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14624 use molecule::hex_string;
14625 if f.alternate() {
14626 write!(f, "0x")?;
14627 }
14628 write!(f, "{}", hex_string(self.as_slice()))
14629 }
14630}
14631impl ::core::fmt::Debug for VerifiableHeaderVec {
14632 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14633 write!(f, "{}({:#x})", Self::NAME, self)
14634 }
14635}
14636impl ::core::fmt::Display for VerifiableHeaderVec {
14637 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14638 write!(f, "{} [", Self::NAME)?;
14639 for i in 0..self.len() {
14640 if i == 0 {
14641 write!(f, "{}", self.get_unchecked(i))?;
14642 } else {
14643 write!(f, ", {}", self.get_unchecked(i))?;
14644 }
14645 }
14646 write!(f, "]")
14647 }
14648}
14649impl ::core::default::Default for VerifiableHeaderVec {
14650 fn default() -> Self {
14651 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
14652 VerifiableHeaderVec::new_unchecked(v)
14653 }
14654}
14655impl VerifiableHeaderVec {
14656 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
14657 pub fn total_size(&self) -> usize {
14658 molecule::unpack_number(self.as_slice()) as usize
14659 }
14660 pub fn item_count(&self) -> usize {
14661 if self.total_size() == molecule::NUMBER_SIZE {
14662 0
14663 } else {
14664 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
14665 }
14666 }
14667 pub fn len(&self) -> usize {
14668 self.item_count()
14669 }
14670 pub fn is_empty(&self) -> bool {
14671 self.len() == 0
14672 }
14673 pub fn get(&self, idx: usize) -> Option<VerifiableHeader> {
14674 if idx >= self.len() {
14675 None
14676 } else {
14677 Some(self.get_unchecked(idx))
14678 }
14679 }
14680 pub fn get_unchecked(&self, idx: usize) -> VerifiableHeader {
14681 let slice = self.as_slice();
14682 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
14683 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
14684 if idx == self.len() - 1 {
14685 VerifiableHeader::new_unchecked(self.0.slice(start..))
14686 } else {
14687 let end_idx = start_idx + molecule::NUMBER_SIZE;
14688 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
14689 VerifiableHeader::new_unchecked(self.0.slice(start..end))
14690 }
14691 }
14692 pub fn as_reader<'r>(&'r self) -> VerifiableHeaderVecReader<'r> {
14693 VerifiableHeaderVecReader::new_unchecked(self.as_slice())
14694 }
14695}
14696impl molecule::prelude::Entity for VerifiableHeaderVec {
14697 type Builder = VerifiableHeaderVecBuilder;
14698 const NAME: &'static str = "VerifiableHeaderVec";
14699 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
14700 VerifiableHeaderVec(data)
14701 }
14702 fn as_bytes(&self) -> molecule::bytes::Bytes {
14703 self.0.clone()
14704 }
14705 fn as_slice(&self) -> &[u8] {
14706 &self.0[..]
14707 }
14708 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14709 VerifiableHeaderVecReader::from_slice(slice).map(|reader| reader.to_entity())
14710 }
14711 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
14712 VerifiableHeaderVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
14713 }
14714 fn new_builder() -> Self::Builder {
14715 ::core::default::Default::default()
14716 }
14717 fn as_builder(self) -> Self::Builder {
14718 Self::new_builder().extend(self.into_iter())
14719 }
14720}
14721#[derive(Clone, Copy)]
14722pub struct VerifiableHeaderVecReader<'r>(&'r [u8]);
14723impl<'r> ::core::fmt::LowerHex for VerifiableHeaderVecReader<'r> {
14724 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14725 use molecule::hex_string;
14726 if f.alternate() {
14727 write!(f, "0x")?;
14728 }
14729 write!(f, "{}", hex_string(self.as_slice()))
14730 }
14731}
14732impl<'r> ::core::fmt::Debug for VerifiableHeaderVecReader<'r> {
14733 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14734 write!(f, "{}({:#x})", Self::NAME, self)
14735 }
14736}
14737impl<'r> ::core::fmt::Display for VerifiableHeaderVecReader<'r> {
14738 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14739 write!(f, "{} [", Self::NAME)?;
14740 for i in 0..self.len() {
14741 if i == 0 {
14742 write!(f, "{}", self.get_unchecked(i))?;
14743 } else {
14744 write!(f, ", {}", self.get_unchecked(i))?;
14745 }
14746 }
14747 write!(f, "]")
14748 }
14749}
14750impl<'r> VerifiableHeaderVecReader<'r> {
14751 pub fn total_size(&self) -> usize {
14752 molecule::unpack_number(self.as_slice()) as usize
14753 }
14754 pub fn item_count(&self) -> usize {
14755 if self.total_size() == molecule::NUMBER_SIZE {
14756 0
14757 } else {
14758 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
14759 }
14760 }
14761 pub fn len(&self) -> usize {
14762 self.item_count()
14763 }
14764 pub fn is_empty(&self) -> bool {
14765 self.len() == 0
14766 }
14767 pub fn get(&self, idx: usize) -> Option<VerifiableHeaderReader<'r>> {
14768 if idx >= self.len() {
14769 None
14770 } else {
14771 Some(self.get_unchecked(idx))
14772 }
14773 }
14774 pub fn get_unchecked(&self, idx: usize) -> VerifiableHeaderReader<'r> {
14775 let slice = self.as_slice();
14776 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
14777 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
14778 if idx == self.len() - 1 {
14779 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..])
14780 } else {
14781 let end_idx = start_idx + molecule::NUMBER_SIZE;
14782 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
14783 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
14784 }
14785 }
14786}
14787impl<'r> molecule::prelude::Reader<'r> for VerifiableHeaderVecReader<'r> {
14788 type Entity = VerifiableHeaderVec;
14789 const NAME: &'static str = "VerifiableHeaderVecReader";
14790 fn to_entity(&self) -> Self::Entity {
14791 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
14792 }
14793 fn new_unchecked(slice: &'r [u8]) -> Self {
14794 VerifiableHeaderVecReader(slice)
14795 }
14796 fn as_slice(&self) -> &'r [u8] {
14797 self.0
14798 }
14799 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
14800 use molecule::verification_error as ve;
14801 let slice_len = slice.len();
14802 if slice_len < molecule::NUMBER_SIZE {
14803 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
14804 }
14805 let total_size = molecule::unpack_number(slice) as usize;
14806 if slice_len != total_size {
14807 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
14808 }
14809 if slice_len == molecule::NUMBER_SIZE {
14810 return Ok(());
14811 }
14812 if slice_len < molecule::NUMBER_SIZE * 2 {
14813 return ve!(
14814 Self,
14815 TotalSizeNotMatch,
14816 molecule::NUMBER_SIZE * 2,
14817 slice_len
14818 );
14819 }
14820 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
14821 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
14822 return ve!(Self, OffsetsNotMatch);
14823 }
14824 if slice_len < offset_first {
14825 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
14826 }
14827 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
14828 .chunks_exact(molecule::NUMBER_SIZE)
14829 .map(|x| molecule::unpack_number(x) as usize)
14830 .collect();
14831 offsets.push(total_size);
14832 if offsets.windows(2).any(|i| i[0] > i[1]) {
14833 return ve!(Self, OffsetsNotMatch);
14834 }
14835 for pair in offsets.windows(2) {
14836 let start = pair[0];
14837 let end = pair[1];
14838 VerifiableHeaderReader::verify(&slice[start..end], compatible)?;
14839 }
14840 Ok(())
14841 }
14842}
14843#[derive(Debug, Default)]
14844pub struct VerifiableHeaderVecBuilder(pub(crate) Vec<VerifiableHeader>);
14845impl VerifiableHeaderVecBuilder {
14846 pub fn set(mut self, v: Vec<VerifiableHeader>) -> Self {
14847 self.0 = v;
14848 self
14849 }
14850 pub fn push(mut self, v: VerifiableHeader) -> Self {
14851 self.0.push(v);
14852 self
14853 }
14854 pub fn extend<T: ::core::iter::IntoIterator<Item = VerifiableHeader>>(
14855 mut self,
14856 iter: T,
14857 ) -> Self {
14858 for elem in iter {
14859 self.0.push(elem);
14860 }
14861 self
14862 }
14863 pub fn replace(&mut self, index: usize, v: VerifiableHeader) -> Option<VerifiableHeader> {
14864 self.0
14865 .get_mut(index)
14866 .map(|item| ::core::mem::replace(item, v))
14867 }
14868}
14869impl molecule::prelude::Builder for VerifiableHeaderVecBuilder {
14870 type Entity = VerifiableHeaderVec;
14871 const NAME: &'static str = "VerifiableHeaderVecBuilder";
14872 fn expected_length(&self) -> usize {
14873 molecule::NUMBER_SIZE * (self.0.len() + 1)
14874 + self
14875 .0
14876 .iter()
14877 .map(|inner| inner.as_slice().len())
14878 .sum::<usize>()
14879 }
14880 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
14881 let item_count = self.0.len();
14882 if item_count == 0 {
14883 writer.write_all(&molecule::pack_number(
14884 molecule::NUMBER_SIZE as molecule::Number,
14885 ))?;
14886 } else {
14887 let (total_size, offsets) = self.0.iter().fold(
14888 (
14889 molecule::NUMBER_SIZE * (item_count + 1),
14890 Vec::with_capacity(item_count),
14891 ),
14892 |(start, mut offsets), inner| {
14893 offsets.push(start);
14894 (start + inner.as_slice().len(), offsets)
14895 },
14896 );
14897 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
14898 for offset in offsets.into_iter() {
14899 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
14900 }
14901 for inner in self.0.iter() {
14902 writer.write_all(inner.as_slice())?;
14903 }
14904 }
14905 Ok(())
14906 }
14907 fn build(&self) -> Self::Entity {
14908 let mut inner = Vec::with_capacity(self.expected_length());
14909 self.write(&mut inner)
14910 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
14911 VerifiableHeaderVec::new_unchecked(inner.into())
14912 }
14913}
14914pub struct VerifiableHeaderVecIterator(VerifiableHeaderVec, usize, usize);
14915impl ::core::iter::Iterator for VerifiableHeaderVecIterator {
14916 type Item = VerifiableHeader;
14917 fn next(&mut self) -> Option<Self::Item> {
14918 if self.1 >= self.2 {
14919 None
14920 } else {
14921 let ret = self.0.get_unchecked(self.1);
14922 self.1 += 1;
14923 Some(ret)
14924 }
14925 }
14926}
14927impl ::core::iter::ExactSizeIterator for VerifiableHeaderVecIterator {
14928 fn len(&self) -> usize {
14929 self.2 - self.1
14930 }
14931}
14932impl ::core::iter::IntoIterator for VerifiableHeaderVec {
14933 type Item = VerifiableHeader;
14934 type IntoIter = VerifiableHeaderVecIterator;
14935 fn into_iter(self) -> Self::IntoIter {
14936 let len = self.len();
14937 VerifiableHeaderVecIterator(self, 0, len)
14938 }
14939}
14940impl<'r> VerifiableHeaderVecReader<'r> {
14941 pub fn iter<'t>(&'t self) -> VerifiableHeaderVecReaderIterator<'t, 'r> {
14942 VerifiableHeaderVecReaderIterator(&self, 0, self.len())
14943 }
14944}
14945pub struct VerifiableHeaderVecReaderIterator<'t, 'r>(
14946 &'t VerifiableHeaderVecReader<'r>,
14947 usize,
14948 usize,
14949);
14950impl<'t: 'r, 'r> ::core::iter::Iterator for VerifiableHeaderVecReaderIterator<'t, 'r> {
14951 type Item = VerifiableHeaderReader<'t>;
14952 fn next(&mut self) -> Option<Self::Item> {
14953 if self.1 >= self.2 {
14954 None
14955 } else {
14956 let ret = self.0.get_unchecked(self.1);
14957 self.1 += 1;
14958 Some(ret)
14959 }
14960 }
14961}
14962impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for VerifiableHeaderVecReaderIterator<'t, 'r> {
14963 fn len(&self) -> usize {
14964 self.2 - self.1
14965 }
14966}
14967#[derive(Clone)]
14968pub struct FilteredBlockVec(molecule::bytes::Bytes);
14969impl ::core::fmt::LowerHex for FilteredBlockVec {
14970 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14971 use molecule::hex_string;
14972 if f.alternate() {
14973 write!(f, "0x")?;
14974 }
14975 write!(f, "{}", hex_string(self.as_slice()))
14976 }
14977}
14978impl ::core::fmt::Debug for FilteredBlockVec {
14979 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14980 write!(f, "{}({:#x})", Self::NAME, self)
14981 }
14982}
14983impl ::core::fmt::Display for FilteredBlockVec {
14984 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
14985 write!(f, "{} [", Self::NAME)?;
14986 for i in 0..self.len() {
14987 if i == 0 {
14988 write!(f, "{}", self.get_unchecked(i))?;
14989 } else {
14990 write!(f, ", {}", self.get_unchecked(i))?;
14991 }
14992 }
14993 write!(f, "]")
14994 }
14995}
14996impl ::core::default::Default for FilteredBlockVec {
14997 fn default() -> Self {
14998 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
14999 FilteredBlockVec::new_unchecked(v)
15000 }
15001}
15002impl FilteredBlockVec {
15003 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
15004 pub fn total_size(&self) -> usize {
15005 molecule::unpack_number(self.as_slice()) as usize
15006 }
15007 pub fn item_count(&self) -> usize {
15008 if self.total_size() == molecule::NUMBER_SIZE {
15009 0
15010 } else {
15011 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
15012 }
15013 }
15014 pub fn len(&self) -> usize {
15015 self.item_count()
15016 }
15017 pub fn is_empty(&self) -> bool {
15018 self.len() == 0
15019 }
15020 pub fn get(&self, idx: usize) -> Option<FilteredBlock> {
15021 if idx >= self.len() {
15022 None
15023 } else {
15024 Some(self.get_unchecked(idx))
15025 }
15026 }
15027 pub fn get_unchecked(&self, idx: usize) -> FilteredBlock {
15028 let slice = self.as_slice();
15029 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
15030 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
15031 if idx == self.len() - 1 {
15032 FilteredBlock::new_unchecked(self.0.slice(start..))
15033 } else {
15034 let end_idx = start_idx + molecule::NUMBER_SIZE;
15035 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
15036 FilteredBlock::new_unchecked(self.0.slice(start..end))
15037 }
15038 }
15039 pub fn as_reader<'r>(&'r self) -> FilteredBlockVecReader<'r> {
15040 FilteredBlockVecReader::new_unchecked(self.as_slice())
15041 }
15042}
15043impl molecule::prelude::Entity for FilteredBlockVec {
15044 type Builder = FilteredBlockVecBuilder;
15045 const NAME: &'static str = "FilteredBlockVec";
15046 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
15047 FilteredBlockVec(data)
15048 }
15049 fn as_bytes(&self) -> molecule::bytes::Bytes {
15050 self.0.clone()
15051 }
15052 fn as_slice(&self) -> &[u8] {
15053 &self.0[..]
15054 }
15055 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15056 FilteredBlockVecReader::from_slice(slice).map(|reader| reader.to_entity())
15057 }
15058 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15059 FilteredBlockVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
15060 }
15061 fn new_builder() -> Self::Builder {
15062 ::core::default::Default::default()
15063 }
15064 fn as_builder(self) -> Self::Builder {
15065 Self::new_builder().extend(self.into_iter())
15066 }
15067}
15068#[derive(Clone, Copy)]
15069pub struct FilteredBlockVecReader<'r>(&'r [u8]);
15070impl<'r> ::core::fmt::LowerHex for FilteredBlockVecReader<'r> {
15071 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15072 use molecule::hex_string;
15073 if f.alternate() {
15074 write!(f, "0x")?;
15075 }
15076 write!(f, "{}", hex_string(self.as_slice()))
15077 }
15078}
15079impl<'r> ::core::fmt::Debug for FilteredBlockVecReader<'r> {
15080 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15081 write!(f, "{}({:#x})", Self::NAME, self)
15082 }
15083}
15084impl<'r> ::core::fmt::Display for FilteredBlockVecReader<'r> {
15085 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15086 write!(f, "{} [", Self::NAME)?;
15087 for i in 0..self.len() {
15088 if i == 0 {
15089 write!(f, "{}", self.get_unchecked(i))?;
15090 } else {
15091 write!(f, ", {}", self.get_unchecked(i))?;
15092 }
15093 }
15094 write!(f, "]")
15095 }
15096}
15097impl<'r> FilteredBlockVecReader<'r> {
15098 pub fn total_size(&self) -> usize {
15099 molecule::unpack_number(self.as_slice()) as usize
15100 }
15101 pub fn item_count(&self) -> usize {
15102 if self.total_size() == molecule::NUMBER_SIZE {
15103 0
15104 } else {
15105 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
15106 }
15107 }
15108 pub fn len(&self) -> usize {
15109 self.item_count()
15110 }
15111 pub fn is_empty(&self) -> bool {
15112 self.len() == 0
15113 }
15114 pub fn get(&self, idx: usize) -> Option<FilteredBlockReader<'r>> {
15115 if idx >= self.len() {
15116 None
15117 } else {
15118 Some(self.get_unchecked(idx))
15119 }
15120 }
15121 pub fn get_unchecked(&self, idx: usize) -> FilteredBlockReader<'r> {
15122 let slice = self.as_slice();
15123 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
15124 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
15125 if idx == self.len() - 1 {
15126 FilteredBlockReader::new_unchecked(&self.as_slice()[start..])
15127 } else {
15128 let end_idx = start_idx + molecule::NUMBER_SIZE;
15129 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
15130 FilteredBlockReader::new_unchecked(&self.as_slice()[start..end])
15131 }
15132 }
15133}
15134impl<'r> molecule::prelude::Reader<'r> for FilteredBlockVecReader<'r> {
15135 type Entity = FilteredBlockVec;
15136 const NAME: &'static str = "FilteredBlockVecReader";
15137 fn to_entity(&self) -> Self::Entity {
15138 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
15139 }
15140 fn new_unchecked(slice: &'r [u8]) -> Self {
15141 FilteredBlockVecReader(slice)
15142 }
15143 fn as_slice(&self) -> &'r [u8] {
15144 self.0
15145 }
15146 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
15147 use molecule::verification_error as ve;
15148 let slice_len = slice.len();
15149 if slice_len < molecule::NUMBER_SIZE {
15150 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
15151 }
15152 let total_size = molecule::unpack_number(slice) as usize;
15153 if slice_len != total_size {
15154 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
15155 }
15156 if slice_len == molecule::NUMBER_SIZE {
15157 return Ok(());
15158 }
15159 if slice_len < molecule::NUMBER_SIZE * 2 {
15160 return ve!(
15161 Self,
15162 TotalSizeNotMatch,
15163 molecule::NUMBER_SIZE * 2,
15164 slice_len
15165 );
15166 }
15167 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
15168 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
15169 return ve!(Self, OffsetsNotMatch);
15170 }
15171 if slice_len < offset_first {
15172 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
15173 }
15174 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
15175 .chunks_exact(molecule::NUMBER_SIZE)
15176 .map(|x| molecule::unpack_number(x) as usize)
15177 .collect();
15178 offsets.push(total_size);
15179 if offsets.windows(2).any(|i| i[0] > i[1]) {
15180 return ve!(Self, OffsetsNotMatch);
15181 }
15182 for pair in offsets.windows(2) {
15183 let start = pair[0];
15184 let end = pair[1];
15185 FilteredBlockReader::verify(&slice[start..end], compatible)?;
15186 }
15187 Ok(())
15188 }
15189}
15190#[derive(Debug, Default)]
15191pub struct FilteredBlockVecBuilder(pub(crate) Vec<FilteredBlock>);
15192impl FilteredBlockVecBuilder {
15193 pub fn set(mut self, v: Vec<FilteredBlock>) -> Self {
15194 self.0 = v;
15195 self
15196 }
15197 pub fn push(mut self, v: FilteredBlock) -> Self {
15198 self.0.push(v);
15199 self
15200 }
15201 pub fn extend<T: ::core::iter::IntoIterator<Item = FilteredBlock>>(mut self, iter: T) -> Self {
15202 for elem in iter {
15203 self.0.push(elem);
15204 }
15205 self
15206 }
15207 pub fn replace(&mut self, index: usize, v: FilteredBlock) -> Option<FilteredBlock> {
15208 self.0
15209 .get_mut(index)
15210 .map(|item| ::core::mem::replace(item, v))
15211 }
15212}
15213impl molecule::prelude::Builder for FilteredBlockVecBuilder {
15214 type Entity = FilteredBlockVec;
15215 const NAME: &'static str = "FilteredBlockVecBuilder";
15216 fn expected_length(&self) -> usize {
15217 molecule::NUMBER_SIZE * (self.0.len() + 1)
15218 + self
15219 .0
15220 .iter()
15221 .map(|inner| inner.as_slice().len())
15222 .sum::<usize>()
15223 }
15224 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
15225 let item_count = self.0.len();
15226 if item_count == 0 {
15227 writer.write_all(&molecule::pack_number(
15228 molecule::NUMBER_SIZE as molecule::Number,
15229 ))?;
15230 } else {
15231 let (total_size, offsets) = self.0.iter().fold(
15232 (
15233 molecule::NUMBER_SIZE * (item_count + 1),
15234 Vec::with_capacity(item_count),
15235 ),
15236 |(start, mut offsets), inner| {
15237 offsets.push(start);
15238 (start + inner.as_slice().len(), offsets)
15239 },
15240 );
15241 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
15242 for offset in offsets.into_iter() {
15243 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
15244 }
15245 for inner in self.0.iter() {
15246 writer.write_all(inner.as_slice())?;
15247 }
15248 }
15249 Ok(())
15250 }
15251 fn build(&self) -> Self::Entity {
15252 let mut inner = Vec::with_capacity(self.expected_length());
15253 self.write(&mut inner)
15254 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
15255 FilteredBlockVec::new_unchecked(inner.into())
15256 }
15257}
15258pub struct FilteredBlockVecIterator(FilteredBlockVec, usize, usize);
15259impl ::core::iter::Iterator for FilteredBlockVecIterator {
15260 type Item = FilteredBlock;
15261 fn next(&mut self) -> Option<Self::Item> {
15262 if self.1 >= self.2 {
15263 None
15264 } else {
15265 let ret = self.0.get_unchecked(self.1);
15266 self.1 += 1;
15267 Some(ret)
15268 }
15269 }
15270}
15271impl ::core::iter::ExactSizeIterator for FilteredBlockVecIterator {
15272 fn len(&self) -> usize {
15273 self.2 - self.1
15274 }
15275}
15276impl ::core::iter::IntoIterator for FilteredBlockVec {
15277 type Item = FilteredBlock;
15278 type IntoIter = FilteredBlockVecIterator;
15279 fn into_iter(self) -> Self::IntoIter {
15280 let len = self.len();
15281 FilteredBlockVecIterator(self, 0, len)
15282 }
15283}
15284impl<'r> FilteredBlockVecReader<'r> {
15285 pub fn iter<'t>(&'t self) -> FilteredBlockVecReaderIterator<'t, 'r> {
15286 FilteredBlockVecReaderIterator(&self, 0, self.len())
15287 }
15288}
15289pub struct FilteredBlockVecReaderIterator<'t, 'r>(&'t FilteredBlockVecReader<'r>, usize, usize);
15290impl<'t: 'r, 'r> ::core::iter::Iterator for FilteredBlockVecReaderIterator<'t, 'r> {
15291 type Item = FilteredBlockReader<'t>;
15292 fn next(&mut self) -> Option<Self::Item> {
15293 if self.1 >= self.2 {
15294 None
15295 } else {
15296 let ret = self.0.get_unchecked(self.1);
15297 self.1 += 1;
15298 Some(ret)
15299 }
15300 }
15301}
15302impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for FilteredBlockVecReaderIterator<'t, 'r> {
15303 fn len(&self) -> usize {
15304 self.2 - self.1
15305 }
15306}
15307#[derive(Clone)]
15308pub struct LightClientMessage(molecule::bytes::Bytes);
15309impl ::core::fmt::LowerHex for LightClientMessage {
15310 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15311 use molecule::hex_string;
15312 if f.alternate() {
15313 write!(f, "0x")?;
15314 }
15315 write!(f, "{}", hex_string(self.as_slice()))
15316 }
15317}
15318impl ::core::fmt::Debug for LightClientMessage {
15319 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15320 write!(f, "{}({:#x})", Self::NAME, self)
15321 }
15322}
15323impl ::core::fmt::Display for LightClientMessage {
15324 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15325 write!(f, "{}(", Self::NAME)?;
15326 self.to_enum().display_inner(f)?;
15327 write!(f, ")")
15328 }
15329}
15330impl ::core::default::Default for LightClientMessage {
15331 fn default() -> Self {
15332 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
15333 LightClientMessage::new_unchecked(v)
15334 }
15335}
15336impl LightClientMessage {
15337 const DEFAULT_VALUE: [u8; 13] = [0, 0, 0, 0, 9, 0, 0, 0, 8, 0, 0, 0, 0];
15338 pub const ITEMS_COUNT: usize = 8;
15339 pub fn item_id(&self) -> molecule::Number {
15340 molecule::unpack_number(self.as_slice())
15341 }
15342 pub fn to_enum(&self) -> LightClientMessageUnion {
15343 let inner = self.0.slice(molecule::NUMBER_SIZE..);
15344 match self.item_id() {
15345 0 => GetLastState::new_unchecked(inner).into(),
15346 1 => SendLastState::new_unchecked(inner).into(),
15347 2 => GetLastStateProof::new_unchecked(inner).into(),
15348 3 => SendLastStateProof::new_unchecked(inner).into(),
15349 4 => GetBlocksProof::new_unchecked(inner).into(),
15350 5 => SendBlocksProof::new_unchecked(inner).into(),
15351 6 => GetTransactionsProof::new_unchecked(inner).into(),
15352 7 => SendTransactionsProof::new_unchecked(inner).into(),
15353 _ => panic!("{}: invalid data", Self::NAME),
15354 }
15355 }
15356 pub fn as_reader<'r>(&'r self) -> LightClientMessageReader<'r> {
15357 LightClientMessageReader::new_unchecked(self.as_slice())
15358 }
15359}
15360impl molecule::prelude::Entity for LightClientMessage {
15361 type Builder = LightClientMessageBuilder;
15362 const NAME: &'static str = "LightClientMessage";
15363 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
15364 LightClientMessage(data)
15365 }
15366 fn as_bytes(&self) -> molecule::bytes::Bytes {
15367 self.0.clone()
15368 }
15369 fn as_slice(&self) -> &[u8] {
15370 &self.0[..]
15371 }
15372 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15373 LightClientMessageReader::from_slice(slice).map(|reader| reader.to_entity())
15374 }
15375 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15376 LightClientMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
15377 }
15378 fn new_builder() -> Self::Builder {
15379 ::core::default::Default::default()
15380 }
15381 fn as_builder(self) -> Self::Builder {
15382 Self::new_builder().set(self.to_enum())
15383 }
15384}
15385#[derive(Clone, Copy)]
15386pub struct LightClientMessageReader<'r>(&'r [u8]);
15387impl<'r> ::core::fmt::LowerHex for LightClientMessageReader<'r> {
15388 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15389 use molecule::hex_string;
15390 if f.alternate() {
15391 write!(f, "0x")?;
15392 }
15393 write!(f, "{}", hex_string(self.as_slice()))
15394 }
15395}
15396impl<'r> ::core::fmt::Debug for LightClientMessageReader<'r> {
15397 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15398 write!(f, "{}({:#x})", Self::NAME, self)
15399 }
15400}
15401impl<'r> ::core::fmt::Display for LightClientMessageReader<'r> {
15402 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15403 write!(f, "{}(", Self::NAME)?;
15404 self.to_enum().display_inner(f)?;
15405 write!(f, ")")
15406 }
15407}
15408impl<'r> LightClientMessageReader<'r> {
15409 pub const ITEMS_COUNT: usize = 8;
15410 pub fn item_id(&self) -> molecule::Number {
15411 molecule::unpack_number(self.as_slice())
15412 }
15413 pub fn to_enum(&self) -> LightClientMessageUnionReader<'r> {
15414 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
15415 match self.item_id() {
15416 0 => GetLastStateReader::new_unchecked(inner).into(),
15417 1 => SendLastStateReader::new_unchecked(inner).into(),
15418 2 => GetLastStateProofReader::new_unchecked(inner).into(),
15419 3 => SendLastStateProofReader::new_unchecked(inner).into(),
15420 4 => GetBlocksProofReader::new_unchecked(inner).into(),
15421 5 => SendBlocksProofReader::new_unchecked(inner).into(),
15422 6 => GetTransactionsProofReader::new_unchecked(inner).into(),
15423 7 => SendTransactionsProofReader::new_unchecked(inner).into(),
15424 _ => panic!("{}: invalid data", Self::NAME),
15425 }
15426 }
15427}
15428impl<'r> molecule::prelude::Reader<'r> for LightClientMessageReader<'r> {
15429 type Entity = LightClientMessage;
15430 const NAME: &'static str = "LightClientMessageReader";
15431 fn to_entity(&self) -> Self::Entity {
15432 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
15433 }
15434 fn new_unchecked(slice: &'r [u8]) -> Self {
15435 LightClientMessageReader(slice)
15436 }
15437 fn as_slice(&self) -> &'r [u8] {
15438 self.0
15439 }
15440 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
15441 use molecule::verification_error as ve;
15442 let slice_len = slice.len();
15443 if slice_len < molecule::NUMBER_SIZE {
15444 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
15445 }
15446 let item_id = molecule::unpack_number(slice);
15447 let inner_slice = &slice[molecule::NUMBER_SIZE..];
15448 match item_id {
15449 0 => GetLastStateReader::verify(inner_slice, compatible),
15450 1 => SendLastStateReader::verify(inner_slice, compatible),
15451 2 => GetLastStateProofReader::verify(inner_slice, compatible),
15452 3 => SendLastStateProofReader::verify(inner_slice, compatible),
15453 4 => GetBlocksProofReader::verify(inner_slice, compatible),
15454 5 => SendBlocksProofReader::verify(inner_slice, compatible),
15455 6 => GetTransactionsProofReader::verify(inner_slice, compatible),
15456 7 => SendTransactionsProofReader::verify(inner_slice, compatible),
15457 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
15458 }?;
15459 Ok(())
15460 }
15461}
15462#[derive(Debug, Default)]
15463pub struct LightClientMessageBuilder(pub(crate) LightClientMessageUnion);
15464impl LightClientMessageBuilder {
15465 pub const ITEMS_COUNT: usize = 8;
15466 pub fn set<I>(mut self, v: I) -> Self
15467 where
15468 I: ::core::convert::Into<LightClientMessageUnion>,
15469 {
15470 self.0 = v.into();
15471 self
15472 }
15473}
15474impl molecule::prelude::Builder for LightClientMessageBuilder {
15475 type Entity = LightClientMessage;
15476 const NAME: &'static str = "LightClientMessageBuilder";
15477 fn expected_length(&self) -> usize {
15478 molecule::NUMBER_SIZE + self.0.as_slice().len()
15479 }
15480 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
15481 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
15482 writer.write_all(self.0.as_slice())
15483 }
15484 fn build(&self) -> Self::Entity {
15485 let mut inner = Vec::with_capacity(self.expected_length());
15486 self.write(&mut inner)
15487 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
15488 LightClientMessage::new_unchecked(inner.into())
15489 }
15490}
15491#[derive(Debug, Clone)]
15492pub enum LightClientMessageUnion {
15493 GetLastState(GetLastState),
15494 SendLastState(SendLastState),
15495 GetLastStateProof(GetLastStateProof),
15496 SendLastStateProof(SendLastStateProof),
15497 GetBlocksProof(GetBlocksProof),
15498 SendBlocksProof(SendBlocksProof),
15499 GetTransactionsProof(GetTransactionsProof),
15500 SendTransactionsProof(SendTransactionsProof),
15501}
15502#[derive(Debug, Clone, Copy)]
15503pub enum LightClientMessageUnionReader<'r> {
15504 GetLastState(GetLastStateReader<'r>),
15505 SendLastState(SendLastStateReader<'r>),
15506 GetLastStateProof(GetLastStateProofReader<'r>),
15507 SendLastStateProof(SendLastStateProofReader<'r>),
15508 GetBlocksProof(GetBlocksProofReader<'r>),
15509 SendBlocksProof(SendBlocksProofReader<'r>),
15510 GetTransactionsProof(GetTransactionsProofReader<'r>),
15511 SendTransactionsProof(SendTransactionsProofReader<'r>),
15512}
15513impl ::core::default::Default for LightClientMessageUnion {
15514 fn default() -> Self {
15515 LightClientMessageUnion::GetLastState(::core::default::Default::default())
15516 }
15517}
15518impl ::core::fmt::Display for LightClientMessageUnion {
15519 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15520 match self {
15521 LightClientMessageUnion::GetLastState(ref item) => {
15522 write!(f, "{}::{}({})", Self::NAME, GetLastState::NAME, item)
15523 }
15524 LightClientMessageUnion::SendLastState(ref item) => {
15525 write!(f, "{}::{}({})", Self::NAME, SendLastState::NAME, item)
15526 }
15527 LightClientMessageUnion::GetLastStateProof(ref item) => {
15528 write!(f, "{}::{}({})", Self::NAME, GetLastStateProof::NAME, item)
15529 }
15530 LightClientMessageUnion::SendLastStateProof(ref item) => {
15531 write!(f, "{}::{}({})", Self::NAME, SendLastStateProof::NAME, item)
15532 }
15533 LightClientMessageUnion::GetBlocksProof(ref item) => {
15534 write!(f, "{}::{}({})", Self::NAME, GetBlocksProof::NAME, item)
15535 }
15536 LightClientMessageUnion::SendBlocksProof(ref item) => {
15537 write!(f, "{}::{}({})", Self::NAME, SendBlocksProof::NAME, item)
15538 }
15539 LightClientMessageUnion::GetTransactionsProof(ref item) => {
15540 write!(
15541 f,
15542 "{}::{}({})",
15543 Self::NAME,
15544 GetTransactionsProof::NAME,
15545 item
15546 )
15547 }
15548 LightClientMessageUnion::SendTransactionsProof(ref item) => {
15549 write!(
15550 f,
15551 "{}::{}({})",
15552 Self::NAME,
15553 SendTransactionsProof::NAME,
15554 item
15555 )
15556 }
15557 }
15558 }
15559}
15560impl<'r> ::core::fmt::Display for LightClientMessageUnionReader<'r> {
15561 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15562 match self {
15563 LightClientMessageUnionReader::GetLastState(ref item) => {
15564 write!(f, "{}::{}({})", Self::NAME, GetLastState::NAME, item)
15565 }
15566 LightClientMessageUnionReader::SendLastState(ref item) => {
15567 write!(f, "{}::{}({})", Self::NAME, SendLastState::NAME, item)
15568 }
15569 LightClientMessageUnionReader::GetLastStateProof(ref item) => {
15570 write!(f, "{}::{}({})", Self::NAME, GetLastStateProof::NAME, item)
15571 }
15572 LightClientMessageUnionReader::SendLastStateProof(ref item) => {
15573 write!(f, "{}::{}({})", Self::NAME, SendLastStateProof::NAME, item)
15574 }
15575 LightClientMessageUnionReader::GetBlocksProof(ref item) => {
15576 write!(f, "{}::{}({})", Self::NAME, GetBlocksProof::NAME, item)
15577 }
15578 LightClientMessageUnionReader::SendBlocksProof(ref item) => {
15579 write!(f, "{}::{}({})", Self::NAME, SendBlocksProof::NAME, item)
15580 }
15581 LightClientMessageUnionReader::GetTransactionsProof(ref item) => {
15582 write!(
15583 f,
15584 "{}::{}({})",
15585 Self::NAME,
15586 GetTransactionsProof::NAME,
15587 item
15588 )
15589 }
15590 LightClientMessageUnionReader::SendTransactionsProof(ref item) => {
15591 write!(
15592 f,
15593 "{}::{}({})",
15594 Self::NAME,
15595 SendTransactionsProof::NAME,
15596 item
15597 )
15598 }
15599 }
15600 }
15601}
15602impl LightClientMessageUnion {
15603 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15604 match self {
15605 LightClientMessageUnion::GetLastState(ref item) => write!(f, "{}", item),
15606 LightClientMessageUnion::SendLastState(ref item) => write!(f, "{}", item),
15607 LightClientMessageUnion::GetLastStateProof(ref item) => write!(f, "{}", item),
15608 LightClientMessageUnion::SendLastStateProof(ref item) => write!(f, "{}", item),
15609 LightClientMessageUnion::GetBlocksProof(ref item) => write!(f, "{}", item),
15610 LightClientMessageUnion::SendBlocksProof(ref item) => write!(f, "{}", item),
15611 LightClientMessageUnion::GetTransactionsProof(ref item) => write!(f, "{}", item),
15612 LightClientMessageUnion::SendTransactionsProof(ref item) => write!(f, "{}", item),
15613 }
15614 }
15615}
15616impl<'r> LightClientMessageUnionReader<'r> {
15617 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15618 match self {
15619 LightClientMessageUnionReader::GetLastState(ref item) => write!(f, "{}", item),
15620 LightClientMessageUnionReader::SendLastState(ref item) => write!(f, "{}", item),
15621 LightClientMessageUnionReader::GetLastStateProof(ref item) => write!(f, "{}", item),
15622 LightClientMessageUnionReader::SendLastStateProof(ref item) => write!(f, "{}", item),
15623 LightClientMessageUnionReader::GetBlocksProof(ref item) => write!(f, "{}", item),
15624 LightClientMessageUnionReader::SendBlocksProof(ref item) => write!(f, "{}", item),
15625 LightClientMessageUnionReader::GetTransactionsProof(ref item) => write!(f, "{}", item),
15626 LightClientMessageUnionReader::SendTransactionsProof(ref item) => write!(f, "{}", item),
15627 }
15628 }
15629}
15630impl ::core::convert::From<GetLastState> for LightClientMessageUnion {
15631 fn from(item: GetLastState) -> Self {
15632 LightClientMessageUnion::GetLastState(item)
15633 }
15634}
15635impl ::core::convert::From<SendLastState> for LightClientMessageUnion {
15636 fn from(item: SendLastState) -> Self {
15637 LightClientMessageUnion::SendLastState(item)
15638 }
15639}
15640impl ::core::convert::From<GetLastStateProof> for LightClientMessageUnion {
15641 fn from(item: GetLastStateProof) -> Self {
15642 LightClientMessageUnion::GetLastStateProof(item)
15643 }
15644}
15645impl ::core::convert::From<SendLastStateProof> for LightClientMessageUnion {
15646 fn from(item: SendLastStateProof) -> Self {
15647 LightClientMessageUnion::SendLastStateProof(item)
15648 }
15649}
15650impl ::core::convert::From<GetBlocksProof> for LightClientMessageUnion {
15651 fn from(item: GetBlocksProof) -> Self {
15652 LightClientMessageUnion::GetBlocksProof(item)
15653 }
15654}
15655impl ::core::convert::From<SendBlocksProof> for LightClientMessageUnion {
15656 fn from(item: SendBlocksProof) -> Self {
15657 LightClientMessageUnion::SendBlocksProof(item)
15658 }
15659}
15660impl ::core::convert::From<GetTransactionsProof> for LightClientMessageUnion {
15661 fn from(item: GetTransactionsProof) -> Self {
15662 LightClientMessageUnion::GetTransactionsProof(item)
15663 }
15664}
15665impl ::core::convert::From<SendTransactionsProof> for LightClientMessageUnion {
15666 fn from(item: SendTransactionsProof) -> Self {
15667 LightClientMessageUnion::SendTransactionsProof(item)
15668 }
15669}
15670impl<'r> ::core::convert::From<GetLastStateReader<'r>> for LightClientMessageUnionReader<'r> {
15671 fn from(item: GetLastStateReader<'r>) -> Self {
15672 LightClientMessageUnionReader::GetLastState(item)
15673 }
15674}
15675impl<'r> ::core::convert::From<SendLastStateReader<'r>> for LightClientMessageUnionReader<'r> {
15676 fn from(item: SendLastStateReader<'r>) -> Self {
15677 LightClientMessageUnionReader::SendLastState(item)
15678 }
15679}
15680impl<'r> ::core::convert::From<GetLastStateProofReader<'r>> for LightClientMessageUnionReader<'r> {
15681 fn from(item: GetLastStateProofReader<'r>) -> Self {
15682 LightClientMessageUnionReader::GetLastStateProof(item)
15683 }
15684}
15685impl<'r> ::core::convert::From<SendLastStateProofReader<'r>> for LightClientMessageUnionReader<'r> {
15686 fn from(item: SendLastStateProofReader<'r>) -> Self {
15687 LightClientMessageUnionReader::SendLastStateProof(item)
15688 }
15689}
15690impl<'r> ::core::convert::From<GetBlocksProofReader<'r>> for LightClientMessageUnionReader<'r> {
15691 fn from(item: GetBlocksProofReader<'r>) -> Self {
15692 LightClientMessageUnionReader::GetBlocksProof(item)
15693 }
15694}
15695impl<'r> ::core::convert::From<SendBlocksProofReader<'r>> for LightClientMessageUnionReader<'r> {
15696 fn from(item: SendBlocksProofReader<'r>) -> Self {
15697 LightClientMessageUnionReader::SendBlocksProof(item)
15698 }
15699}
15700impl<'r> ::core::convert::From<GetTransactionsProofReader<'r>>
15701 for LightClientMessageUnionReader<'r>
15702{
15703 fn from(item: GetTransactionsProofReader<'r>) -> Self {
15704 LightClientMessageUnionReader::GetTransactionsProof(item)
15705 }
15706}
15707impl<'r> ::core::convert::From<SendTransactionsProofReader<'r>>
15708 for LightClientMessageUnionReader<'r>
15709{
15710 fn from(item: SendTransactionsProofReader<'r>) -> Self {
15711 LightClientMessageUnionReader::SendTransactionsProof(item)
15712 }
15713}
15714impl LightClientMessageUnion {
15715 pub const NAME: &'static str = "LightClientMessageUnion";
15716 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
15717 match self {
15718 LightClientMessageUnion::GetLastState(item) => item.as_bytes(),
15719 LightClientMessageUnion::SendLastState(item) => item.as_bytes(),
15720 LightClientMessageUnion::GetLastStateProof(item) => item.as_bytes(),
15721 LightClientMessageUnion::SendLastStateProof(item) => item.as_bytes(),
15722 LightClientMessageUnion::GetBlocksProof(item) => item.as_bytes(),
15723 LightClientMessageUnion::SendBlocksProof(item) => item.as_bytes(),
15724 LightClientMessageUnion::GetTransactionsProof(item) => item.as_bytes(),
15725 LightClientMessageUnion::SendTransactionsProof(item) => item.as_bytes(),
15726 }
15727 }
15728 pub fn as_slice(&self) -> &[u8] {
15729 match self {
15730 LightClientMessageUnion::GetLastState(item) => item.as_slice(),
15731 LightClientMessageUnion::SendLastState(item) => item.as_slice(),
15732 LightClientMessageUnion::GetLastStateProof(item) => item.as_slice(),
15733 LightClientMessageUnion::SendLastStateProof(item) => item.as_slice(),
15734 LightClientMessageUnion::GetBlocksProof(item) => item.as_slice(),
15735 LightClientMessageUnion::SendBlocksProof(item) => item.as_slice(),
15736 LightClientMessageUnion::GetTransactionsProof(item) => item.as_slice(),
15737 LightClientMessageUnion::SendTransactionsProof(item) => item.as_slice(),
15738 }
15739 }
15740 pub fn item_id(&self) -> molecule::Number {
15741 match self {
15742 LightClientMessageUnion::GetLastState(_) => 0,
15743 LightClientMessageUnion::SendLastState(_) => 1,
15744 LightClientMessageUnion::GetLastStateProof(_) => 2,
15745 LightClientMessageUnion::SendLastStateProof(_) => 3,
15746 LightClientMessageUnion::GetBlocksProof(_) => 4,
15747 LightClientMessageUnion::SendBlocksProof(_) => 5,
15748 LightClientMessageUnion::GetTransactionsProof(_) => 6,
15749 LightClientMessageUnion::SendTransactionsProof(_) => 7,
15750 }
15751 }
15752 pub fn item_name(&self) -> &str {
15753 match self {
15754 LightClientMessageUnion::GetLastState(_) => "GetLastState",
15755 LightClientMessageUnion::SendLastState(_) => "SendLastState",
15756 LightClientMessageUnion::GetLastStateProof(_) => "GetLastStateProof",
15757 LightClientMessageUnion::SendLastStateProof(_) => "SendLastStateProof",
15758 LightClientMessageUnion::GetBlocksProof(_) => "GetBlocksProof",
15759 LightClientMessageUnion::SendBlocksProof(_) => "SendBlocksProof",
15760 LightClientMessageUnion::GetTransactionsProof(_) => "GetTransactionsProof",
15761 LightClientMessageUnion::SendTransactionsProof(_) => "SendTransactionsProof",
15762 }
15763 }
15764 pub fn as_reader<'r>(&'r self) -> LightClientMessageUnionReader<'r> {
15765 match self {
15766 LightClientMessageUnion::GetLastState(item) => item.as_reader().into(),
15767 LightClientMessageUnion::SendLastState(item) => item.as_reader().into(),
15768 LightClientMessageUnion::GetLastStateProof(item) => item.as_reader().into(),
15769 LightClientMessageUnion::SendLastStateProof(item) => item.as_reader().into(),
15770 LightClientMessageUnion::GetBlocksProof(item) => item.as_reader().into(),
15771 LightClientMessageUnion::SendBlocksProof(item) => item.as_reader().into(),
15772 LightClientMessageUnion::GetTransactionsProof(item) => item.as_reader().into(),
15773 LightClientMessageUnion::SendTransactionsProof(item) => item.as_reader().into(),
15774 }
15775 }
15776}
15777impl<'r> LightClientMessageUnionReader<'r> {
15778 pub const NAME: &'r str = "LightClientMessageUnionReader";
15779 pub fn as_slice(&self) -> &'r [u8] {
15780 match self {
15781 LightClientMessageUnionReader::GetLastState(item) => item.as_slice(),
15782 LightClientMessageUnionReader::SendLastState(item) => item.as_slice(),
15783 LightClientMessageUnionReader::GetLastStateProof(item) => item.as_slice(),
15784 LightClientMessageUnionReader::SendLastStateProof(item) => item.as_slice(),
15785 LightClientMessageUnionReader::GetBlocksProof(item) => item.as_slice(),
15786 LightClientMessageUnionReader::SendBlocksProof(item) => item.as_slice(),
15787 LightClientMessageUnionReader::GetTransactionsProof(item) => item.as_slice(),
15788 LightClientMessageUnionReader::SendTransactionsProof(item) => item.as_slice(),
15789 }
15790 }
15791 pub fn item_id(&self) -> molecule::Number {
15792 match self {
15793 LightClientMessageUnionReader::GetLastState(_) => 0,
15794 LightClientMessageUnionReader::SendLastState(_) => 1,
15795 LightClientMessageUnionReader::GetLastStateProof(_) => 2,
15796 LightClientMessageUnionReader::SendLastStateProof(_) => 3,
15797 LightClientMessageUnionReader::GetBlocksProof(_) => 4,
15798 LightClientMessageUnionReader::SendBlocksProof(_) => 5,
15799 LightClientMessageUnionReader::GetTransactionsProof(_) => 6,
15800 LightClientMessageUnionReader::SendTransactionsProof(_) => 7,
15801 }
15802 }
15803 pub fn item_name(&self) -> &str {
15804 match self {
15805 LightClientMessageUnionReader::GetLastState(_) => "GetLastState",
15806 LightClientMessageUnionReader::SendLastState(_) => "SendLastState",
15807 LightClientMessageUnionReader::GetLastStateProof(_) => "GetLastStateProof",
15808 LightClientMessageUnionReader::SendLastStateProof(_) => "SendLastStateProof",
15809 LightClientMessageUnionReader::GetBlocksProof(_) => "GetBlocksProof",
15810 LightClientMessageUnionReader::SendBlocksProof(_) => "SendBlocksProof",
15811 LightClientMessageUnionReader::GetTransactionsProof(_) => "GetTransactionsProof",
15812 LightClientMessageUnionReader::SendTransactionsProof(_) => "SendTransactionsProof",
15813 }
15814 }
15815}
15816#[derive(Clone)]
15817pub struct GetLastState(molecule::bytes::Bytes);
15818impl ::core::fmt::LowerHex for GetLastState {
15819 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15820 use molecule::hex_string;
15821 if f.alternate() {
15822 write!(f, "0x")?;
15823 }
15824 write!(f, "{}", hex_string(self.as_slice()))
15825 }
15826}
15827impl ::core::fmt::Debug for GetLastState {
15828 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15829 write!(f, "{}({:#x})", Self::NAME, self)
15830 }
15831}
15832impl ::core::fmt::Display for GetLastState {
15833 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15834 write!(f, "{} {{ ", Self::NAME)?;
15835 write!(f, "{}: {}", "subscribe", self.subscribe())?;
15836 let extra_count = self.count_extra_fields();
15837 if extra_count != 0 {
15838 write!(f, ", .. ({} fields)", extra_count)?;
15839 }
15840 write!(f, " }}")
15841 }
15842}
15843impl ::core::default::Default for GetLastState {
15844 fn default() -> Self {
15845 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
15846 GetLastState::new_unchecked(v)
15847 }
15848}
15849impl GetLastState {
15850 const DEFAULT_VALUE: [u8; 9] = [9, 0, 0, 0, 8, 0, 0, 0, 0];
15851 pub const FIELD_COUNT: usize = 1;
15852 pub fn total_size(&self) -> usize {
15853 molecule::unpack_number(self.as_slice()) as usize
15854 }
15855 pub fn field_count(&self) -> usize {
15856 if self.total_size() == molecule::NUMBER_SIZE {
15857 0
15858 } else {
15859 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
15860 }
15861 }
15862 pub fn count_extra_fields(&self) -> usize {
15863 self.field_count() - Self::FIELD_COUNT
15864 }
15865 pub fn has_extra_fields(&self) -> bool {
15866 Self::FIELD_COUNT != self.field_count()
15867 }
15868 pub fn subscribe(&self) -> Bool {
15869 let slice = self.as_slice();
15870 let start = molecule::unpack_number(&slice[4..]) as usize;
15871 if self.has_extra_fields() {
15872 let end = molecule::unpack_number(&slice[8..]) as usize;
15873 Bool::new_unchecked(self.0.slice(start..end))
15874 } else {
15875 Bool::new_unchecked(self.0.slice(start..))
15876 }
15877 }
15878 pub fn as_reader<'r>(&'r self) -> GetLastStateReader<'r> {
15879 GetLastStateReader::new_unchecked(self.as_slice())
15880 }
15881}
15882impl molecule::prelude::Entity for GetLastState {
15883 type Builder = GetLastStateBuilder;
15884 const NAME: &'static str = "GetLastState";
15885 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
15886 GetLastState(data)
15887 }
15888 fn as_bytes(&self) -> molecule::bytes::Bytes {
15889 self.0.clone()
15890 }
15891 fn as_slice(&self) -> &[u8] {
15892 &self.0[..]
15893 }
15894 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15895 GetLastStateReader::from_slice(slice).map(|reader| reader.to_entity())
15896 }
15897 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
15898 GetLastStateReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
15899 }
15900 fn new_builder() -> Self::Builder {
15901 ::core::default::Default::default()
15902 }
15903 fn as_builder(self) -> Self::Builder {
15904 Self::new_builder().subscribe(self.subscribe())
15905 }
15906}
15907#[derive(Clone, Copy)]
15908pub struct GetLastStateReader<'r>(&'r [u8]);
15909impl<'r> ::core::fmt::LowerHex for GetLastStateReader<'r> {
15910 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15911 use molecule::hex_string;
15912 if f.alternate() {
15913 write!(f, "0x")?;
15914 }
15915 write!(f, "{}", hex_string(self.as_slice()))
15916 }
15917}
15918impl<'r> ::core::fmt::Debug for GetLastStateReader<'r> {
15919 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15920 write!(f, "{}({:#x})", Self::NAME, self)
15921 }
15922}
15923impl<'r> ::core::fmt::Display for GetLastStateReader<'r> {
15924 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
15925 write!(f, "{} {{ ", Self::NAME)?;
15926 write!(f, "{}: {}", "subscribe", self.subscribe())?;
15927 let extra_count = self.count_extra_fields();
15928 if extra_count != 0 {
15929 write!(f, ", .. ({} fields)", extra_count)?;
15930 }
15931 write!(f, " }}")
15932 }
15933}
15934impl<'r> GetLastStateReader<'r> {
15935 pub const FIELD_COUNT: usize = 1;
15936 pub fn total_size(&self) -> usize {
15937 molecule::unpack_number(self.as_slice()) as usize
15938 }
15939 pub fn field_count(&self) -> usize {
15940 if self.total_size() == molecule::NUMBER_SIZE {
15941 0
15942 } else {
15943 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
15944 }
15945 }
15946 pub fn count_extra_fields(&self) -> usize {
15947 self.field_count() - Self::FIELD_COUNT
15948 }
15949 pub fn has_extra_fields(&self) -> bool {
15950 Self::FIELD_COUNT != self.field_count()
15951 }
15952 pub fn subscribe(&self) -> BoolReader<'r> {
15953 let slice = self.as_slice();
15954 let start = molecule::unpack_number(&slice[4..]) as usize;
15955 if self.has_extra_fields() {
15956 let end = molecule::unpack_number(&slice[8..]) as usize;
15957 BoolReader::new_unchecked(&self.as_slice()[start..end])
15958 } else {
15959 BoolReader::new_unchecked(&self.as_slice()[start..])
15960 }
15961 }
15962}
15963impl<'r> molecule::prelude::Reader<'r> for GetLastStateReader<'r> {
15964 type Entity = GetLastState;
15965 const NAME: &'static str = "GetLastStateReader";
15966 fn to_entity(&self) -> Self::Entity {
15967 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
15968 }
15969 fn new_unchecked(slice: &'r [u8]) -> Self {
15970 GetLastStateReader(slice)
15971 }
15972 fn as_slice(&self) -> &'r [u8] {
15973 self.0
15974 }
15975 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
15976 use molecule::verification_error as ve;
15977 let slice_len = slice.len();
15978 if slice_len < molecule::NUMBER_SIZE {
15979 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
15980 }
15981 let total_size = molecule::unpack_number(slice) as usize;
15982 if slice_len != total_size {
15983 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
15984 }
15985 if slice_len < molecule::NUMBER_SIZE * 2 {
15986 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
15987 }
15988 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
15989 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
15990 return ve!(Self, OffsetsNotMatch);
15991 }
15992 if slice_len < offset_first {
15993 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
15994 }
15995 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
15996 if field_count < Self::FIELD_COUNT {
15997 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
15998 } else if !compatible && field_count > Self::FIELD_COUNT {
15999 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
16000 };
16001 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
16002 .chunks_exact(molecule::NUMBER_SIZE)
16003 .map(|x| molecule::unpack_number(x) as usize)
16004 .collect();
16005 offsets.push(total_size);
16006 if offsets.windows(2).any(|i| i[0] > i[1]) {
16007 return ve!(Self, OffsetsNotMatch);
16008 }
16009 BoolReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
16010 Ok(())
16011 }
16012}
16013#[derive(Debug, Default)]
16014pub struct GetLastStateBuilder {
16015 pub(crate) subscribe: Bool,
16016}
16017impl GetLastStateBuilder {
16018 pub const FIELD_COUNT: usize = 1;
16019 pub fn subscribe(mut self, v: Bool) -> Self {
16020 self.subscribe = v;
16021 self
16022 }
16023}
16024impl molecule::prelude::Builder for GetLastStateBuilder {
16025 type Entity = GetLastState;
16026 const NAME: &'static str = "GetLastStateBuilder";
16027 fn expected_length(&self) -> usize {
16028 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.subscribe.as_slice().len()
16029 }
16030 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
16031 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
16032 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
16033 offsets.push(total_size);
16034 total_size += self.subscribe.as_slice().len();
16035 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
16036 for offset in offsets.into_iter() {
16037 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
16038 }
16039 writer.write_all(self.subscribe.as_slice())?;
16040 Ok(())
16041 }
16042 fn build(&self) -> Self::Entity {
16043 let mut inner = Vec::with_capacity(self.expected_length());
16044 self.write(&mut inner)
16045 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
16046 GetLastState::new_unchecked(inner.into())
16047 }
16048}
16049#[derive(Clone)]
16050pub struct SendLastState(molecule::bytes::Bytes);
16051impl ::core::fmt::LowerHex for SendLastState {
16052 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16053 use molecule::hex_string;
16054 if f.alternate() {
16055 write!(f, "0x")?;
16056 }
16057 write!(f, "{}", hex_string(self.as_slice()))
16058 }
16059}
16060impl ::core::fmt::Debug for SendLastState {
16061 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16062 write!(f, "{}({:#x})", Self::NAME, self)
16063 }
16064}
16065impl ::core::fmt::Display for SendLastState {
16066 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16067 write!(f, "{} {{ ", Self::NAME)?;
16068 write!(f, "{}: {}", "last_header", self.last_header())?;
16069 let extra_count = self.count_extra_fields();
16070 if extra_count != 0 {
16071 write!(f, ", .. ({} fields)", extra_count)?;
16072 }
16073 write!(f, " }}")
16074 }
16075}
16076impl ::core::default::Default for SendLastState {
16077 fn default() -> Self {
16078 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
16079 SendLastState::new_unchecked(v)
16080 }
16081}
16082impl SendLastState {
16083 const DEFAULT_VALUE: [u8; 388] = [
16084 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,
16085 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16086 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16087 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16088 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16090 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16091 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16092 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16093 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16094 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16095 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16096 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16097 ];
16098 pub const FIELD_COUNT: usize = 1;
16099 pub fn total_size(&self) -> usize {
16100 molecule::unpack_number(self.as_slice()) as usize
16101 }
16102 pub fn field_count(&self) -> usize {
16103 if self.total_size() == molecule::NUMBER_SIZE {
16104 0
16105 } else {
16106 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
16107 }
16108 }
16109 pub fn count_extra_fields(&self) -> usize {
16110 self.field_count() - Self::FIELD_COUNT
16111 }
16112 pub fn has_extra_fields(&self) -> bool {
16113 Self::FIELD_COUNT != self.field_count()
16114 }
16115 pub fn last_header(&self) -> VerifiableHeader {
16116 let slice = self.as_slice();
16117 let start = molecule::unpack_number(&slice[4..]) as usize;
16118 if self.has_extra_fields() {
16119 let end = molecule::unpack_number(&slice[8..]) as usize;
16120 VerifiableHeader::new_unchecked(self.0.slice(start..end))
16121 } else {
16122 VerifiableHeader::new_unchecked(self.0.slice(start..))
16123 }
16124 }
16125 pub fn as_reader<'r>(&'r self) -> SendLastStateReader<'r> {
16126 SendLastStateReader::new_unchecked(self.as_slice())
16127 }
16128}
16129impl molecule::prelude::Entity for SendLastState {
16130 type Builder = SendLastStateBuilder;
16131 const NAME: &'static str = "SendLastState";
16132 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
16133 SendLastState(data)
16134 }
16135 fn as_bytes(&self) -> molecule::bytes::Bytes {
16136 self.0.clone()
16137 }
16138 fn as_slice(&self) -> &[u8] {
16139 &self.0[..]
16140 }
16141 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
16142 SendLastStateReader::from_slice(slice).map(|reader| reader.to_entity())
16143 }
16144 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
16145 SendLastStateReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
16146 }
16147 fn new_builder() -> Self::Builder {
16148 ::core::default::Default::default()
16149 }
16150 fn as_builder(self) -> Self::Builder {
16151 Self::new_builder().last_header(self.last_header())
16152 }
16153}
16154#[derive(Clone, Copy)]
16155pub struct SendLastStateReader<'r>(&'r [u8]);
16156impl<'r> ::core::fmt::LowerHex for SendLastStateReader<'r> {
16157 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16158 use molecule::hex_string;
16159 if f.alternate() {
16160 write!(f, "0x")?;
16161 }
16162 write!(f, "{}", hex_string(self.as_slice()))
16163 }
16164}
16165impl<'r> ::core::fmt::Debug for SendLastStateReader<'r> {
16166 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16167 write!(f, "{}({:#x})", Self::NAME, self)
16168 }
16169}
16170impl<'r> ::core::fmt::Display for SendLastStateReader<'r> {
16171 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16172 write!(f, "{} {{ ", Self::NAME)?;
16173 write!(f, "{}: {}", "last_header", self.last_header())?;
16174 let extra_count = self.count_extra_fields();
16175 if extra_count != 0 {
16176 write!(f, ", .. ({} fields)", extra_count)?;
16177 }
16178 write!(f, " }}")
16179 }
16180}
16181impl<'r> SendLastStateReader<'r> {
16182 pub const FIELD_COUNT: usize = 1;
16183 pub fn total_size(&self) -> usize {
16184 molecule::unpack_number(self.as_slice()) as usize
16185 }
16186 pub fn field_count(&self) -> usize {
16187 if self.total_size() == molecule::NUMBER_SIZE {
16188 0
16189 } else {
16190 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
16191 }
16192 }
16193 pub fn count_extra_fields(&self) -> usize {
16194 self.field_count() - Self::FIELD_COUNT
16195 }
16196 pub fn has_extra_fields(&self) -> bool {
16197 Self::FIELD_COUNT != self.field_count()
16198 }
16199 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
16200 let slice = self.as_slice();
16201 let start = molecule::unpack_number(&slice[4..]) as usize;
16202 if self.has_extra_fields() {
16203 let end = molecule::unpack_number(&slice[8..]) as usize;
16204 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
16205 } else {
16206 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..])
16207 }
16208 }
16209}
16210impl<'r> molecule::prelude::Reader<'r> for SendLastStateReader<'r> {
16211 type Entity = SendLastState;
16212 const NAME: &'static str = "SendLastStateReader";
16213 fn to_entity(&self) -> Self::Entity {
16214 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
16215 }
16216 fn new_unchecked(slice: &'r [u8]) -> Self {
16217 SendLastStateReader(slice)
16218 }
16219 fn as_slice(&self) -> &'r [u8] {
16220 self.0
16221 }
16222 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
16223 use molecule::verification_error as ve;
16224 let slice_len = slice.len();
16225 if slice_len < molecule::NUMBER_SIZE {
16226 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
16227 }
16228 let total_size = molecule::unpack_number(slice) as usize;
16229 if slice_len != total_size {
16230 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
16231 }
16232 if slice_len < molecule::NUMBER_SIZE * 2 {
16233 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
16234 }
16235 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
16236 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
16237 return ve!(Self, OffsetsNotMatch);
16238 }
16239 if slice_len < offset_first {
16240 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
16241 }
16242 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
16243 if field_count < Self::FIELD_COUNT {
16244 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
16245 } else if !compatible && field_count > Self::FIELD_COUNT {
16246 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
16247 };
16248 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
16249 .chunks_exact(molecule::NUMBER_SIZE)
16250 .map(|x| molecule::unpack_number(x) as usize)
16251 .collect();
16252 offsets.push(total_size);
16253 if offsets.windows(2).any(|i| i[0] > i[1]) {
16254 return ve!(Self, OffsetsNotMatch);
16255 }
16256 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
16257 Ok(())
16258 }
16259}
16260#[derive(Debug, Default)]
16261pub struct SendLastStateBuilder {
16262 pub(crate) last_header: VerifiableHeader,
16263}
16264impl SendLastStateBuilder {
16265 pub const FIELD_COUNT: usize = 1;
16266 pub fn last_header(mut self, v: VerifiableHeader) -> Self {
16267 self.last_header = v;
16268 self
16269 }
16270}
16271impl molecule::prelude::Builder for SendLastStateBuilder {
16272 type Entity = SendLastState;
16273 const NAME: &'static str = "SendLastStateBuilder";
16274 fn expected_length(&self) -> usize {
16275 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.last_header.as_slice().len()
16276 }
16277 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
16278 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
16279 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
16280 offsets.push(total_size);
16281 total_size += self.last_header.as_slice().len();
16282 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
16283 for offset in offsets.into_iter() {
16284 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
16285 }
16286 writer.write_all(self.last_header.as_slice())?;
16287 Ok(())
16288 }
16289 fn build(&self) -> Self::Entity {
16290 let mut inner = Vec::with_capacity(self.expected_length());
16291 self.write(&mut inner)
16292 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
16293 SendLastState::new_unchecked(inner.into())
16294 }
16295}
16296#[derive(Clone)]
16297pub struct GetLastStateProof(molecule::bytes::Bytes);
16298impl ::core::fmt::LowerHex for GetLastStateProof {
16299 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16300 use molecule::hex_string;
16301 if f.alternate() {
16302 write!(f, "0x")?;
16303 }
16304 write!(f, "{}", hex_string(self.as_slice()))
16305 }
16306}
16307impl ::core::fmt::Debug for GetLastStateProof {
16308 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16309 write!(f, "{}({:#x})", Self::NAME, self)
16310 }
16311}
16312impl ::core::fmt::Display for GetLastStateProof {
16313 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16314 write!(f, "{} {{ ", Self::NAME)?;
16315 write!(f, "{}: {}", "last_hash", self.last_hash())?;
16316 write!(f, ", {}: {}", "start_hash", self.start_hash())?;
16317 write!(f, ", {}: {}", "start_number", self.start_number())?;
16318 write!(f, ", {}: {}", "last_n_blocks", self.last_n_blocks())?;
16319 write!(
16320 f,
16321 ", {}: {}",
16322 "difficulty_boundary",
16323 self.difficulty_boundary()
16324 )?;
16325 write!(f, ", {}: {}", "difficulties", self.difficulties())?;
16326 let extra_count = self.count_extra_fields();
16327 if extra_count != 0 {
16328 write!(f, ", .. ({} fields)", extra_count)?;
16329 }
16330 write!(f, " }}")
16331 }
16332}
16333impl ::core::default::Default for GetLastStateProof {
16334 fn default() -> Self {
16335 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
16336 GetLastStateProof::new_unchecked(v)
16337 }
16338}
16339impl GetLastStateProof {
16340 const DEFAULT_VALUE: [u8; 144] = [
16341 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,
16342 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16343 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16344 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16345 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16346 ];
16347 pub const FIELD_COUNT: usize = 6;
16348 pub fn total_size(&self) -> usize {
16349 molecule::unpack_number(self.as_slice()) as usize
16350 }
16351 pub fn field_count(&self) -> usize {
16352 if self.total_size() == molecule::NUMBER_SIZE {
16353 0
16354 } else {
16355 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
16356 }
16357 }
16358 pub fn count_extra_fields(&self) -> usize {
16359 self.field_count() - Self::FIELD_COUNT
16360 }
16361 pub fn has_extra_fields(&self) -> bool {
16362 Self::FIELD_COUNT != self.field_count()
16363 }
16364 pub fn last_hash(&self) -> Byte32 {
16365 let slice = self.as_slice();
16366 let start = molecule::unpack_number(&slice[4..]) as usize;
16367 let end = molecule::unpack_number(&slice[8..]) as usize;
16368 Byte32::new_unchecked(self.0.slice(start..end))
16369 }
16370 pub fn start_hash(&self) -> Byte32 {
16371 let slice = self.as_slice();
16372 let start = molecule::unpack_number(&slice[8..]) as usize;
16373 let end = molecule::unpack_number(&slice[12..]) as usize;
16374 Byte32::new_unchecked(self.0.slice(start..end))
16375 }
16376 pub fn start_number(&self) -> Uint64 {
16377 let slice = self.as_slice();
16378 let start = molecule::unpack_number(&slice[12..]) as usize;
16379 let end = molecule::unpack_number(&slice[16..]) as usize;
16380 Uint64::new_unchecked(self.0.slice(start..end))
16381 }
16382 pub fn last_n_blocks(&self) -> Uint64 {
16383 let slice = self.as_slice();
16384 let start = molecule::unpack_number(&slice[16..]) as usize;
16385 let end = molecule::unpack_number(&slice[20..]) as usize;
16386 Uint64::new_unchecked(self.0.slice(start..end))
16387 }
16388 pub fn difficulty_boundary(&self) -> Uint256 {
16389 let slice = self.as_slice();
16390 let start = molecule::unpack_number(&slice[20..]) as usize;
16391 let end = molecule::unpack_number(&slice[24..]) as usize;
16392 Uint256::new_unchecked(self.0.slice(start..end))
16393 }
16394 pub fn difficulties(&self) -> Uint256Vec {
16395 let slice = self.as_slice();
16396 let start = molecule::unpack_number(&slice[24..]) as usize;
16397 if self.has_extra_fields() {
16398 let end = molecule::unpack_number(&slice[28..]) as usize;
16399 Uint256Vec::new_unchecked(self.0.slice(start..end))
16400 } else {
16401 Uint256Vec::new_unchecked(self.0.slice(start..))
16402 }
16403 }
16404 pub fn as_reader<'r>(&'r self) -> GetLastStateProofReader<'r> {
16405 GetLastStateProofReader::new_unchecked(self.as_slice())
16406 }
16407}
16408impl molecule::prelude::Entity for GetLastStateProof {
16409 type Builder = GetLastStateProofBuilder;
16410 const NAME: &'static str = "GetLastStateProof";
16411 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
16412 GetLastStateProof(data)
16413 }
16414 fn as_bytes(&self) -> molecule::bytes::Bytes {
16415 self.0.clone()
16416 }
16417 fn as_slice(&self) -> &[u8] {
16418 &self.0[..]
16419 }
16420 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
16421 GetLastStateProofReader::from_slice(slice).map(|reader| reader.to_entity())
16422 }
16423 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
16424 GetLastStateProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
16425 }
16426 fn new_builder() -> Self::Builder {
16427 ::core::default::Default::default()
16428 }
16429 fn as_builder(self) -> Self::Builder {
16430 Self::new_builder()
16431 .last_hash(self.last_hash())
16432 .start_hash(self.start_hash())
16433 .start_number(self.start_number())
16434 .last_n_blocks(self.last_n_blocks())
16435 .difficulty_boundary(self.difficulty_boundary())
16436 .difficulties(self.difficulties())
16437 }
16438}
16439#[derive(Clone, Copy)]
16440pub struct GetLastStateProofReader<'r>(&'r [u8]);
16441impl<'r> ::core::fmt::LowerHex for GetLastStateProofReader<'r> {
16442 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16443 use molecule::hex_string;
16444 if f.alternate() {
16445 write!(f, "0x")?;
16446 }
16447 write!(f, "{}", hex_string(self.as_slice()))
16448 }
16449}
16450impl<'r> ::core::fmt::Debug for GetLastStateProofReader<'r> {
16451 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16452 write!(f, "{}({:#x})", Self::NAME, self)
16453 }
16454}
16455impl<'r> ::core::fmt::Display for GetLastStateProofReader<'r> {
16456 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16457 write!(f, "{} {{ ", Self::NAME)?;
16458 write!(f, "{}: {}", "last_hash", self.last_hash())?;
16459 write!(f, ", {}: {}", "start_hash", self.start_hash())?;
16460 write!(f, ", {}: {}", "start_number", self.start_number())?;
16461 write!(f, ", {}: {}", "last_n_blocks", self.last_n_blocks())?;
16462 write!(
16463 f,
16464 ", {}: {}",
16465 "difficulty_boundary",
16466 self.difficulty_boundary()
16467 )?;
16468 write!(f, ", {}: {}", "difficulties", self.difficulties())?;
16469 let extra_count = self.count_extra_fields();
16470 if extra_count != 0 {
16471 write!(f, ", .. ({} fields)", extra_count)?;
16472 }
16473 write!(f, " }}")
16474 }
16475}
16476impl<'r> GetLastStateProofReader<'r> {
16477 pub const FIELD_COUNT: usize = 6;
16478 pub fn total_size(&self) -> usize {
16479 molecule::unpack_number(self.as_slice()) as usize
16480 }
16481 pub fn field_count(&self) -> usize {
16482 if self.total_size() == molecule::NUMBER_SIZE {
16483 0
16484 } else {
16485 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
16486 }
16487 }
16488 pub fn count_extra_fields(&self) -> usize {
16489 self.field_count() - Self::FIELD_COUNT
16490 }
16491 pub fn has_extra_fields(&self) -> bool {
16492 Self::FIELD_COUNT != self.field_count()
16493 }
16494 pub fn last_hash(&self) -> Byte32Reader<'r> {
16495 let slice = self.as_slice();
16496 let start = molecule::unpack_number(&slice[4..]) as usize;
16497 let end = molecule::unpack_number(&slice[8..]) as usize;
16498 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
16499 }
16500 pub fn start_hash(&self) -> Byte32Reader<'r> {
16501 let slice = self.as_slice();
16502 let start = molecule::unpack_number(&slice[8..]) as usize;
16503 let end = molecule::unpack_number(&slice[12..]) as usize;
16504 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
16505 }
16506 pub fn start_number(&self) -> Uint64Reader<'r> {
16507 let slice = self.as_slice();
16508 let start = molecule::unpack_number(&slice[12..]) as usize;
16509 let end = molecule::unpack_number(&slice[16..]) as usize;
16510 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
16511 }
16512 pub fn last_n_blocks(&self) -> Uint64Reader<'r> {
16513 let slice = self.as_slice();
16514 let start = molecule::unpack_number(&slice[16..]) as usize;
16515 let end = molecule::unpack_number(&slice[20..]) as usize;
16516 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
16517 }
16518 pub fn difficulty_boundary(&self) -> Uint256Reader<'r> {
16519 let slice = self.as_slice();
16520 let start = molecule::unpack_number(&slice[20..]) as usize;
16521 let end = molecule::unpack_number(&slice[24..]) as usize;
16522 Uint256Reader::new_unchecked(&self.as_slice()[start..end])
16523 }
16524 pub fn difficulties(&self) -> Uint256VecReader<'r> {
16525 let slice = self.as_slice();
16526 let start = molecule::unpack_number(&slice[24..]) as usize;
16527 if self.has_extra_fields() {
16528 let end = molecule::unpack_number(&slice[28..]) as usize;
16529 Uint256VecReader::new_unchecked(&self.as_slice()[start..end])
16530 } else {
16531 Uint256VecReader::new_unchecked(&self.as_slice()[start..])
16532 }
16533 }
16534}
16535impl<'r> molecule::prelude::Reader<'r> for GetLastStateProofReader<'r> {
16536 type Entity = GetLastStateProof;
16537 const NAME: &'static str = "GetLastStateProofReader";
16538 fn to_entity(&self) -> Self::Entity {
16539 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
16540 }
16541 fn new_unchecked(slice: &'r [u8]) -> Self {
16542 GetLastStateProofReader(slice)
16543 }
16544 fn as_slice(&self) -> &'r [u8] {
16545 self.0
16546 }
16547 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
16548 use molecule::verification_error as ve;
16549 let slice_len = slice.len();
16550 if slice_len < molecule::NUMBER_SIZE {
16551 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
16552 }
16553 let total_size = molecule::unpack_number(slice) as usize;
16554 if slice_len != total_size {
16555 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
16556 }
16557 if slice_len < molecule::NUMBER_SIZE * 2 {
16558 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
16559 }
16560 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
16561 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
16562 return ve!(Self, OffsetsNotMatch);
16563 }
16564 if slice_len < offset_first {
16565 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
16566 }
16567 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
16568 if field_count < Self::FIELD_COUNT {
16569 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
16570 } else if !compatible && field_count > Self::FIELD_COUNT {
16571 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
16572 };
16573 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
16574 .chunks_exact(molecule::NUMBER_SIZE)
16575 .map(|x| molecule::unpack_number(x) as usize)
16576 .collect();
16577 offsets.push(total_size);
16578 if offsets.windows(2).any(|i| i[0] > i[1]) {
16579 return ve!(Self, OffsetsNotMatch);
16580 }
16581 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
16582 Byte32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
16583 Uint64Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
16584 Uint64Reader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
16585 Uint256Reader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
16586 Uint256VecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
16587 Ok(())
16588 }
16589}
16590#[derive(Debug, Default)]
16591pub struct GetLastStateProofBuilder {
16592 pub(crate) last_hash: Byte32,
16593 pub(crate) start_hash: Byte32,
16594 pub(crate) start_number: Uint64,
16595 pub(crate) last_n_blocks: Uint64,
16596 pub(crate) difficulty_boundary: Uint256,
16597 pub(crate) difficulties: Uint256Vec,
16598}
16599impl GetLastStateProofBuilder {
16600 pub const FIELD_COUNT: usize = 6;
16601 pub fn last_hash(mut self, v: Byte32) -> Self {
16602 self.last_hash = v;
16603 self
16604 }
16605 pub fn start_hash(mut self, v: Byte32) -> Self {
16606 self.start_hash = v;
16607 self
16608 }
16609 pub fn start_number(mut self, v: Uint64) -> Self {
16610 self.start_number = v;
16611 self
16612 }
16613 pub fn last_n_blocks(mut self, v: Uint64) -> Self {
16614 self.last_n_blocks = v;
16615 self
16616 }
16617 pub fn difficulty_boundary(mut self, v: Uint256) -> Self {
16618 self.difficulty_boundary = v;
16619 self
16620 }
16621 pub fn difficulties(mut self, v: Uint256Vec) -> Self {
16622 self.difficulties = v;
16623 self
16624 }
16625}
16626impl molecule::prelude::Builder for GetLastStateProofBuilder {
16627 type Entity = GetLastStateProof;
16628 const NAME: &'static str = "GetLastStateProofBuilder";
16629 fn expected_length(&self) -> usize {
16630 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
16631 + self.last_hash.as_slice().len()
16632 + self.start_hash.as_slice().len()
16633 + self.start_number.as_slice().len()
16634 + self.last_n_blocks.as_slice().len()
16635 + self.difficulty_boundary.as_slice().len()
16636 + self.difficulties.as_slice().len()
16637 }
16638 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
16639 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
16640 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
16641 offsets.push(total_size);
16642 total_size += self.last_hash.as_slice().len();
16643 offsets.push(total_size);
16644 total_size += self.start_hash.as_slice().len();
16645 offsets.push(total_size);
16646 total_size += self.start_number.as_slice().len();
16647 offsets.push(total_size);
16648 total_size += self.last_n_blocks.as_slice().len();
16649 offsets.push(total_size);
16650 total_size += self.difficulty_boundary.as_slice().len();
16651 offsets.push(total_size);
16652 total_size += self.difficulties.as_slice().len();
16653 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
16654 for offset in offsets.into_iter() {
16655 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
16656 }
16657 writer.write_all(self.last_hash.as_slice())?;
16658 writer.write_all(self.start_hash.as_slice())?;
16659 writer.write_all(self.start_number.as_slice())?;
16660 writer.write_all(self.last_n_blocks.as_slice())?;
16661 writer.write_all(self.difficulty_boundary.as_slice())?;
16662 writer.write_all(self.difficulties.as_slice())?;
16663 Ok(())
16664 }
16665 fn build(&self) -> Self::Entity {
16666 let mut inner = Vec::with_capacity(self.expected_length());
16667 self.write(&mut inner)
16668 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
16669 GetLastStateProof::new_unchecked(inner.into())
16670 }
16671}
16672#[derive(Clone)]
16673pub struct SendLastStateProof(molecule::bytes::Bytes);
16674impl ::core::fmt::LowerHex for SendLastStateProof {
16675 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16676 use molecule::hex_string;
16677 if f.alternate() {
16678 write!(f, "0x")?;
16679 }
16680 write!(f, "{}", hex_string(self.as_slice()))
16681 }
16682}
16683impl ::core::fmt::Debug for SendLastStateProof {
16684 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16685 write!(f, "{}({:#x})", Self::NAME, self)
16686 }
16687}
16688impl ::core::fmt::Display for SendLastStateProof {
16689 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16690 write!(f, "{} {{ ", Self::NAME)?;
16691 write!(f, "{}: {}", "last_header", self.last_header())?;
16692 write!(f, ", {}: {}", "proof", self.proof())?;
16693 write!(f, ", {}: {}", "headers", self.headers())?;
16694 let extra_count = self.count_extra_fields();
16695 if extra_count != 0 {
16696 write!(f, ", .. ({} fields)", extra_count)?;
16697 }
16698 write!(f, " }}")
16699 }
16700}
16701impl ::core::default::Default for SendLastStateProof {
16702 fn default() -> Self {
16703 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
16704 SendLastStateProof::new_unchecked(v)
16705 }
16706}
16707impl SendLastStateProof {
16708 const DEFAULT_VALUE: [u8; 404] = [
16709 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,
16710 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,
16711 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16712 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16713 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16714 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16715 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16716 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16717 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16718 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16719 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16720 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16721 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16722 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
16723 ];
16724 pub const FIELD_COUNT: usize = 3;
16725 pub fn total_size(&self) -> usize {
16726 molecule::unpack_number(self.as_slice()) as usize
16727 }
16728 pub fn field_count(&self) -> usize {
16729 if self.total_size() == molecule::NUMBER_SIZE {
16730 0
16731 } else {
16732 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
16733 }
16734 }
16735 pub fn count_extra_fields(&self) -> usize {
16736 self.field_count() - Self::FIELD_COUNT
16737 }
16738 pub fn has_extra_fields(&self) -> bool {
16739 Self::FIELD_COUNT != self.field_count()
16740 }
16741 pub fn last_header(&self) -> VerifiableHeader {
16742 let slice = self.as_slice();
16743 let start = molecule::unpack_number(&slice[4..]) as usize;
16744 let end = molecule::unpack_number(&slice[8..]) as usize;
16745 VerifiableHeader::new_unchecked(self.0.slice(start..end))
16746 }
16747 pub fn proof(&self) -> HeaderDigestVec {
16748 let slice = self.as_slice();
16749 let start = molecule::unpack_number(&slice[8..]) as usize;
16750 let end = molecule::unpack_number(&slice[12..]) as usize;
16751 HeaderDigestVec::new_unchecked(self.0.slice(start..end))
16752 }
16753 pub fn headers(&self) -> VerifiableHeaderVec {
16754 let slice = self.as_slice();
16755 let start = molecule::unpack_number(&slice[12..]) as usize;
16756 if self.has_extra_fields() {
16757 let end = molecule::unpack_number(&slice[16..]) as usize;
16758 VerifiableHeaderVec::new_unchecked(self.0.slice(start..end))
16759 } else {
16760 VerifiableHeaderVec::new_unchecked(self.0.slice(start..))
16761 }
16762 }
16763 pub fn as_reader<'r>(&'r self) -> SendLastStateProofReader<'r> {
16764 SendLastStateProofReader::new_unchecked(self.as_slice())
16765 }
16766}
16767impl molecule::prelude::Entity for SendLastStateProof {
16768 type Builder = SendLastStateProofBuilder;
16769 const NAME: &'static str = "SendLastStateProof";
16770 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
16771 SendLastStateProof(data)
16772 }
16773 fn as_bytes(&self) -> molecule::bytes::Bytes {
16774 self.0.clone()
16775 }
16776 fn as_slice(&self) -> &[u8] {
16777 &self.0[..]
16778 }
16779 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
16780 SendLastStateProofReader::from_slice(slice).map(|reader| reader.to_entity())
16781 }
16782 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
16783 SendLastStateProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
16784 }
16785 fn new_builder() -> Self::Builder {
16786 ::core::default::Default::default()
16787 }
16788 fn as_builder(self) -> Self::Builder {
16789 Self::new_builder()
16790 .last_header(self.last_header())
16791 .proof(self.proof())
16792 .headers(self.headers())
16793 }
16794}
16795#[derive(Clone, Copy)]
16796pub struct SendLastStateProofReader<'r>(&'r [u8]);
16797impl<'r> ::core::fmt::LowerHex for SendLastStateProofReader<'r> {
16798 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16799 use molecule::hex_string;
16800 if f.alternate() {
16801 write!(f, "0x")?;
16802 }
16803 write!(f, "{}", hex_string(self.as_slice()))
16804 }
16805}
16806impl<'r> ::core::fmt::Debug for SendLastStateProofReader<'r> {
16807 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16808 write!(f, "{}({:#x})", Self::NAME, self)
16809 }
16810}
16811impl<'r> ::core::fmt::Display for SendLastStateProofReader<'r> {
16812 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16813 write!(f, "{} {{ ", Self::NAME)?;
16814 write!(f, "{}: {}", "last_header", self.last_header())?;
16815 write!(f, ", {}: {}", "proof", self.proof())?;
16816 write!(f, ", {}: {}", "headers", self.headers())?;
16817 let extra_count = self.count_extra_fields();
16818 if extra_count != 0 {
16819 write!(f, ", .. ({} fields)", extra_count)?;
16820 }
16821 write!(f, " }}")
16822 }
16823}
16824impl<'r> SendLastStateProofReader<'r> {
16825 pub const FIELD_COUNT: usize = 3;
16826 pub fn total_size(&self) -> usize {
16827 molecule::unpack_number(self.as_slice()) as usize
16828 }
16829 pub fn field_count(&self) -> usize {
16830 if self.total_size() == molecule::NUMBER_SIZE {
16831 0
16832 } else {
16833 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
16834 }
16835 }
16836 pub fn count_extra_fields(&self) -> usize {
16837 self.field_count() - Self::FIELD_COUNT
16838 }
16839 pub fn has_extra_fields(&self) -> bool {
16840 Self::FIELD_COUNT != self.field_count()
16841 }
16842 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
16843 let slice = self.as_slice();
16844 let start = molecule::unpack_number(&slice[4..]) as usize;
16845 let end = molecule::unpack_number(&slice[8..]) as usize;
16846 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
16847 }
16848 pub fn proof(&self) -> HeaderDigestVecReader<'r> {
16849 let slice = self.as_slice();
16850 let start = molecule::unpack_number(&slice[8..]) as usize;
16851 let end = molecule::unpack_number(&slice[12..]) as usize;
16852 HeaderDigestVecReader::new_unchecked(&self.as_slice()[start..end])
16853 }
16854 pub fn headers(&self) -> VerifiableHeaderVecReader<'r> {
16855 let slice = self.as_slice();
16856 let start = molecule::unpack_number(&slice[12..]) as usize;
16857 if self.has_extra_fields() {
16858 let end = molecule::unpack_number(&slice[16..]) as usize;
16859 VerifiableHeaderVecReader::new_unchecked(&self.as_slice()[start..end])
16860 } else {
16861 VerifiableHeaderVecReader::new_unchecked(&self.as_slice()[start..])
16862 }
16863 }
16864}
16865impl<'r> molecule::prelude::Reader<'r> for SendLastStateProofReader<'r> {
16866 type Entity = SendLastStateProof;
16867 const NAME: &'static str = "SendLastStateProofReader";
16868 fn to_entity(&self) -> Self::Entity {
16869 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
16870 }
16871 fn new_unchecked(slice: &'r [u8]) -> Self {
16872 SendLastStateProofReader(slice)
16873 }
16874 fn as_slice(&self) -> &'r [u8] {
16875 self.0
16876 }
16877 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
16878 use molecule::verification_error as ve;
16879 let slice_len = slice.len();
16880 if slice_len < molecule::NUMBER_SIZE {
16881 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
16882 }
16883 let total_size = molecule::unpack_number(slice) as usize;
16884 if slice_len != total_size {
16885 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
16886 }
16887 if slice_len < molecule::NUMBER_SIZE * 2 {
16888 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
16889 }
16890 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
16891 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
16892 return ve!(Self, OffsetsNotMatch);
16893 }
16894 if slice_len < offset_first {
16895 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
16896 }
16897 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
16898 if field_count < Self::FIELD_COUNT {
16899 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
16900 } else if !compatible && field_count > Self::FIELD_COUNT {
16901 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
16902 };
16903 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
16904 .chunks_exact(molecule::NUMBER_SIZE)
16905 .map(|x| molecule::unpack_number(x) as usize)
16906 .collect();
16907 offsets.push(total_size);
16908 if offsets.windows(2).any(|i| i[0] > i[1]) {
16909 return ve!(Self, OffsetsNotMatch);
16910 }
16911 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
16912 HeaderDigestVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
16913 VerifiableHeaderVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
16914 Ok(())
16915 }
16916}
16917#[derive(Debug, Default)]
16918pub struct SendLastStateProofBuilder {
16919 pub(crate) last_header: VerifiableHeader,
16920 pub(crate) proof: HeaderDigestVec,
16921 pub(crate) headers: VerifiableHeaderVec,
16922}
16923impl SendLastStateProofBuilder {
16924 pub const FIELD_COUNT: usize = 3;
16925 pub fn last_header(mut self, v: VerifiableHeader) -> Self {
16926 self.last_header = v;
16927 self
16928 }
16929 pub fn proof(mut self, v: HeaderDigestVec) -> Self {
16930 self.proof = v;
16931 self
16932 }
16933 pub fn headers(mut self, v: VerifiableHeaderVec) -> Self {
16934 self.headers = v;
16935 self
16936 }
16937}
16938impl molecule::prelude::Builder for SendLastStateProofBuilder {
16939 type Entity = SendLastStateProof;
16940 const NAME: &'static str = "SendLastStateProofBuilder";
16941 fn expected_length(&self) -> usize {
16942 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
16943 + self.last_header.as_slice().len()
16944 + self.proof.as_slice().len()
16945 + self.headers.as_slice().len()
16946 }
16947 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
16948 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
16949 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
16950 offsets.push(total_size);
16951 total_size += self.last_header.as_slice().len();
16952 offsets.push(total_size);
16953 total_size += self.proof.as_slice().len();
16954 offsets.push(total_size);
16955 total_size += self.headers.as_slice().len();
16956 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
16957 for offset in offsets.into_iter() {
16958 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
16959 }
16960 writer.write_all(self.last_header.as_slice())?;
16961 writer.write_all(self.proof.as_slice())?;
16962 writer.write_all(self.headers.as_slice())?;
16963 Ok(())
16964 }
16965 fn build(&self) -> Self::Entity {
16966 let mut inner = Vec::with_capacity(self.expected_length());
16967 self.write(&mut inner)
16968 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
16969 SendLastStateProof::new_unchecked(inner.into())
16970 }
16971}
16972#[derive(Clone)]
16973pub struct GetBlocksProof(molecule::bytes::Bytes);
16974impl ::core::fmt::LowerHex for GetBlocksProof {
16975 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16976 use molecule::hex_string;
16977 if f.alternate() {
16978 write!(f, "0x")?;
16979 }
16980 write!(f, "{}", hex_string(self.as_slice()))
16981 }
16982}
16983impl ::core::fmt::Debug for GetBlocksProof {
16984 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16985 write!(f, "{}({:#x})", Self::NAME, self)
16986 }
16987}
16988impl ::core::fmt::Display for GetBlocksProof {
16989 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
16990 write!(f, "{} {{ ", Self::NAME)?;
16991 write!(f, "{}: {}", "last_hash", self.last_hash())?;
16992 write!(f, ", {}: {}", "block_hashes", self.block_hashes())?;
16993 let extra_count = self.count_extra_fields();
16994 if extra_count != 0 {
16995 write!(f, ", .. ({} fields)", extra_count)?;
16996 }
16997 write!(f, " }}")
16998 }
16999}
17000impl ::core::default::Default for GetBlocksProof {
17001 fn default() -> Self {
17002 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
17003 GetBlocksProof::new_unchecked(v)
17004 }
17005}
17006impl GetBlocksProof {
17007 const DEFAULT_VALUE: [u8; 48] = [
17008 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,
17009 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17010 ];
17011 pub const FIELD_COUNT: usize = 2;
17012 pub fn total_size(&self) -> usize {
17013 molecule::unpack_number(self.as_slice()) as usize
17014 }
17015 pub fn field_count(&self) -> usize {
17016 if self.total_size() == molecule::NUMBER_SIZE {
17017 0
17018 } else {
17019 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17020 }
17021 }
17022 pub fn count_extra_fields(&self) -> usize {
17023 self.field_count() - Self::FIELD_COUNT
17024 }
17025 pub fn has_extra_fields(&self) -> bool {
17026 Self::FIELD_COUNT != self.field_count()
17027 }
17028 pub fn last_hash(&self) -> Byte32 {
17029 let slice = self.as_slice();
17030 let start = molecule::unpack_number(&slice[4..]) as usize;
17031 let end = molecule::unpack_number(&slice[8..]) as usize;
17032 Byte32::new_unchecked(self.0.slice(start..end))
17033 }
17034 pub fn block_hashes(&self) -> Byte32Vec {
17035 let slice = self.as_slice();
17036 let start = molecule::unpack_number(&slice[8..]) as usize;
17037 if self.has_extra_fields() {
17038 let end = molecule::unpack_number(&slice[12..]) as usize;
17039 Byte32Vec::new_unchecked(self.0.slice(start..end))
17040 } else {
17041 Byte32Vec::new_unchecked(self.0.slice(start..))
17042 }
17043 }
17044 pub fn as_reader<'r>(&'r self) -> GetBlocksProofReader<'r> {
17045 GetBlocksProofReader::new_unchecked(self.as_slice())
17046 }
17047}
17048impl molecule::prelude::Entity for GetBlocksProof {
17049 type Builder = GetBlocksProofBuilder;
17050 const NAME: &'static str = "GetBlocksProof";
17051 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
17052 GetBlocksProof(data)
17053 }
17054 fn as_bytes(&self) -> molecule::bytes::Bytes {
17055 self.0.clone()
17056 }
17057 fn as_slice(&self) -> &[u8] {
17058 &self.0[..]
17059 }
17060 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17061 GetBlocksProofReader::from_slice(slice).map(|reader| reader.to_entity())
17062 }
17063 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17064 GetBlocksProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
17065 }
17066 fn new_builder() -> Self::Builder {
17067 ::core::default::Default::default()
17068 }
17069 fn as_builder(self) -> Self::Builder {
17070 Self::new_builder()
17071 .last_hash(self.last_hash())
17072 .block_hashes(self.block_hashes())
17073 }
17074}
17075#[derive(Clone, Copy)]
17076pub struct GetBlocksProofReader<'r>(&'r [u8]);
17077impl<'r> ::core::fmt::LowerHex for GetBlocksProofReader<'r> {
17078 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17079 use molecule::hex_string;
17080 if f.alternate() {
17081 write!(f, "0x")?;
17082 }
17083 write!(f, "{}", hex_string(self.as_slice()))
17084 }
17085}
17086impl<'r> ::core::fmt::Debug for GetBlocksProofReader<'r> {
17087 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17088 write!(f, "{}({:#x})", Self::NAME, self)
17089 }
17090}
17091impl<'r> ::core::fmt::Display for GetBlocksProofReader<'r> {
17092 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17093 write!(f, "{} {{ ", Self::NAME)?;
17094 write!(f, "{}: {}", "last_hash", self.last_hash())?;
17095 write!(f, ", {}: {}", "block_hashes", self.block_hashes())?;
17096 let extra_count = self.count_extra_fields();
17097 if extra_count != 0 {
17098 write!(f, ", .. ({} fields)", extra_count)?;
17099 }
17100 write!(f, " }}")
17101 }
17102}
17103impl<'r> GetBlocksProofReader<'r> {
17104 pub const FIELD_COUNT: usize = 2;
17105 pub fn total_size(&self) -> usize {
17106 molecule::unpack_number(self.as_slice()) as usize
17107 }
17108 pub fn field_count(&self) -> usize {
17109 if self.total_size() == molecule::NUMBER_SIZE {
17110 0
17111 } else {
17112 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17113 }
17114 }
17115 pub fn count_extra_fields(&self) -> usize {
17116 self.field_count() - Self::FIELD_COUNT
17117 }
17118 pub fn has_extra_fields(&self) -> bool {
17119 Self::FIELD_COUNT != self.field_count()
17120 }
17121 pub fn last_hash(&self) -> Byte32Reader<'r> {
17122 let slice = self.as_slice();
17123 let start = molecule::unpack_number(&slice[4..]) as usize;
17124 let end = molecule::unpack_number(&slice[8..]) as usize;
17125 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
17126 }
17127 pub fn block_hashes(&self) -> Byte32VecReader<'r> {
17128 let slice = self.as_slice();
17129 let start = molecule::unpack_number(&slice[8..]) as usize;
17130 if self.has_extra_fields() {
17131 let end = molecule::unpack_number(&slice[12..]) as usize;
17132 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
17133 } else {
17134 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
17135 }
17136 }
17137}
17138impl<'r> molecule::prelude::Reader<'r> for GetBlocksProofReader<'r> {
17139 type Entity = GetBlocksProof;
17140 const NAME: &'static str = "GetBlocksProofReader";
17141 fn to_entity(&self) -> Self::Entity {
17142 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
17143 }
17144 fn new_unchecked(slice: &'r [u8]) -> Self {
17145 GetBlocksProofReader(slice)
17146 }
17147 fn as_slice(&self) -> &'r [u8] {
17148 self.0
17149 }
17150 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
17151 use molecule::verification_error as ve;
17152 let slice_len = slice.len();
17153 if slice_len < molecule::NUMBER_SIZE {
17154 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
17155 }
17156 let total_size = molecule::unpack_number(slice) as usize;
17157 if slice_len != total_size {
17158 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
17159 }
17160 if slice_len < molecule::NUMBER_SIZE * 2 {
17161 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
17162 }
17163 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
17164 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
17165 return ve!(Self, OffsetsNotMatch);
17166 }
17167 if slice_len < offset_first {
17168 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
17169 }
17170 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
17171 if field_count < Self::FIELD_COUNT {
17172 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17173 } else if !compatible && field_count > Self::FIELD_COUNT {
17174 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17175 };
17176 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
17177 .chunks_exact(molecule::NUMBER_SIZE)
17178 .map(|x| molecule::unpack_number(x) as usize)
17179 .collect();
17180 offsets.push(total_size);
17181 if offsets.windows(2).any(|i| i[0] > i[1]) {
17182 return ve!(Self, OffsetsNotMatch);
17183 }
17184 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
17185 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
17186 Ok(())
17187 }
17188}
17189#[derive(Debug, Default)]
17190pub struct GetBlocksProofBuilder {
17191 pub(crate) last_hash: Byte32,
17192 pub(crate) block_hashes: Byte32Vec,
17193}
17194impl GetBlocksProofBuilder {
17195 pub const FIELD_COUNT: usize = 2;
17196 pub fn last_hash(mut self, v: Byte32) -> Self {
17197 self.last_hash = v;
17198 self
17199 }
17200 pub fn block_hashes(mut self, v: Byte32Vec) -> Self {
17201 self.block_hashes = v;
17202 self
17203 }
17204}
17205impl molecule::prelude::Builder for GetBlocksProofBuilder {
17206 type Entity = GetBlocksProof;
17207 const NAME: &'static str = "GetBlocksProofBuilder";
17208 fn expected_length(&self) -> usize {
17209 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
17210 + self.last_hash.as_slice().len()
17211 + self.block_hashes.as_slice().len()
17212 }
17213 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
17214 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
17215 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
17216 offsets.push(total_size);
17217 total_size += self.last_hash.as_slice().len();
17218 offsets.push(total_size);
17219 total_size += self.block_hashes.as_slice().len();
17220 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
17221 for offset in offsets.into_iter() {
17222 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
17223 }
17224 writer.write_all(self.last_hash.as_slice())?;
17225 writer.write_all(self.block_hashes.as_slice())?;
17226 Ok(())
17227 }
17228 fn build(&self) -> Self::Entity {
17229 let mut inner = Vec::with_capacity(self.expected_length());
17230 self.write(&mut inner)
17231 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
17232 GetBlocksProof::new_unchecked(inner.into())
17233 }
17234}
17235#[derive(Clone)]
17236pub struct SendBlocksProof(molecule::bytes::Bytes);
17237impl ::core::fmt::LowerHex for SendBlocksProof {
17238 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17239 use molecule::hex_string;
17240 if f.alternate() {
17241 write!(f, "0x")?;
17242 }
17243 write!(f, "{}", hex_string(self.as_slice()))
17244 }
17245}
17246impl ::core::fmt::Debug for SendBlocksProof {
17247 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17248 write!(f, "{}({:#x})", Self::NAME, self)
17249 }
17250}
17251impl ::core::fmt::Display for SendBlocksProof {
17252 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17253 write!(f, "{} {{ ", Self::NAME)?;
17254 write!(f, "{}: {}", "last_header", self.last_header())?;
17255 write!(f, ", {}: {}", "proof", self.proof())?;
17256 write!(f, ", {}: {}", "headers", self.headers())?;
17257 write!(
17258 f,
17259 ", {}: {}",
17260 "missing_block_hashes",
17261 self.missing_block_hashes()
17262 )?;
17263 let extra_count = self.count_extra_fields();
17264 if extra_count != 0 {
17265 write!(f, ", .. ({} fields)", extra_count)?;
17266 }
17267 write!(f, " }}")
17268 }
17269}
17270impl ::core::default::Default for SendBlocksProof {
17271 fn default() -> Self {
17272 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
17273 SendBlocksProof::new_unchecked(v)
17274 }
17275}
17276impl SendBlocksProof {
17277 const DEFAULT_VALUE: [u8; 412] = [
17278 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,
17279 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,
17280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17284 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17285 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17287 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17288 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17291 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17292 ];
17293 pub const FIELD_COUNT: usize = 4;
17294 pub fn total_size(&self) -> usize {
17295 molecule::unpack_number(self.as_slice()) as usize
17296 }
17297 pub fn field_count(&self) -> usize {
17298 if self.total_size() == molecule::NUMBER_SIZE {
17299 0
17300 } else {
17301 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17302 }
17303 }
17304 pub fn count_extra_fields(&self) -> usize {
17305 self.field_count() - Self::FIELD_COUNT
17306 }
17307 pub fn has_extra_fields(&self) -> bool {
17308 Self::FIELD_COUNT != self.field_count()
17309 }
17310 pub fn last_header(&self) -> VerifiableHeader {
17311 let slice = self.as_slice();
17312 let start = molecule::unpack_number(&slice[4..]) as usize;
17313 let end = molecule::unpack_number(&slice[8..]) as usize;
17314 VerifiableHeader::new_unchecked(self.0.slice(start..end))
17315 }
17316 pub fn proof(&self) -> HeaderDigestVec {
17317 let slice = self.as_slice();
17318 let start = molecule::unpack_number(&slice[8..]) as usize;
17319 let end = molecule::unpack_number(&slice[12..]) as usize;
17320 HeaderDigestVec::new_unchecked(self.0.slice(start..end))
17321 }
17322 pub fn headers(&self) -> HeaderVec {
17323 let slice = self.as_slice();
17324 let start = molecule::unpack_number(&slice[12..]) as usize;
17325 let end = molecule::unpack_number(&slice[16..]) as usize;
17326 HeaderVec::new_unchecked(self.0.slice(start..end))
17327 }
17328 pub fn missing_block_hashes(&self) -> Byte32Vec {
17329 let slice = self.as_slice();
17330 let start = molecule::unpack_number(&slice[16..]) as usize;
17331 if self.has_extra_fields() {
17332 let end = molecule::unpack_number(&slice[20..]) as usize;
17333 Byte32Vec::new_unchecked(self.0.slice(start..end))
17334 } else {
17335 Byte32Vec::new_unchecked(self.0.slice(start..))
17336 }
17337 }
17338 pub fn as_reader<'r>(&'r self) -> SendBlocksProofReader<'r> {
17339 SendBlocksProofReader::new_unchecked(self.as_slice())
17340 }
17341}
17342impl molecule::prelude::Entity for SendBlocksProof {
17343 type Builder = SendBlocksProofBuilder;
17344 const NAME: &'static str = "SendBlocksProof";
17345 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
17346 SendBlocksProof(data)
17347 }
17348 fn as_bytes(&self) -> molecule::bytes::Bytes {
17349 self.0.clone()
17350 }
17351 fn as_slice(&self) -> &[u8] {
17352 &self.0[..]
17353 }
17354 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17355 SendBlocksProofReader::from_slice(slice).map(|reader| reader.to_entity())
17356 }
17357 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17358 SendBlocksProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
17359 }
17360 fn new_builder() -> Self::Builder {
17361 ::core::default::Default::default()
17362 }
17363 fn as_builder(self) -> Self::Builder {
17364 Self::new_builder()
17365 .last_header(self.last_header())
17366 .proof(self.proof())
17367 .headers(self.headers())
17368 .missing_block_hashes(self.missing_block_hashes())
17369 }
17370}
17371#[derive(Clone, Copy)]
17372pub struct SendBlocksProofReader<'r>(&'r [u8]);
17373impl<'r> ::core::fmt::LowerHex for SendBlocksProofReader<'r> {
17374 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17375 use molecule::hex_string;
17376 if f.alternate() {
17377 write!(f, "0x")?;
17378 }
17379 write!(f, "{}", hex_string(self.as_slice()))
17380 }
17381}
17382impl<'r> ::core::fmt::Debug for SendBlocksProofReader<'r> {
17383 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17384 write!(f, "{}({:#x})", Self::NAME, self)
17385 }
17386}
17387impl<'r> ::core::fmt::Display for SendBlocksProofReader<'r> {
17388 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17389 write!(f, "{} {{ ", Self::NAME)?;
17390 write!(f, "{}: {}", "last_header", self.last_header())?;
17391 write!(f, ", {}: {}", "proof", self.proof())?;
17392 write!(f, ", {}: {}", "headers", self.headers())?;
17393 write!(
17394 f,
17395 ", {}: {}",
17396 "missing_block_hashes",
17397 self.missing_block_hashes()
17398 )?;
17399 let extra_count = self.count_extra_fields();
17400 if extra_count != 0 {
17401 write!(f, ", .. ({} fields)", extra_count)?;
17402 }
17403 write!(f, " }}")
17404 }
17405}
17406impl<'r> SendBlocksProofReader<'r> {
17407 pub const FIELD_COUNT: usize = 4;
17408 pub fn total_size(&self) -> usize {
17409 molecule::unpack_number(self.as_slice()) as usize
17410 }
17411 pub fn field_count(&self) -> usize {
17412 if self.total_size() == molecule::NUMBER_SIZE {
17413 0
17414 } else {
17415 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17416 }
17417 }
17418 pub fn count_extra_fields(&self) -> usize {
17419 self.field_count() - Self::FIELD_COUNT
17420 }
17421 pub fn has_extra_fields(&self) -> bool {
17422 Self::FIELD_COUNT != self.field_count()
17423 }
17424 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
17425 let slice = self.as_slice();
17426 let start = molecule::unpack_number(&slice[4..]) as usize;
17427 let end = molecule::unpack_number(&slice[8..]) as usize;
17428 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
17429 }
17430 pub fn proof(&self) -> HeaderDigestVecReader<'r> {
17431 let slice = self.as_slice();
17432 let start = molecule::unpack_number(&slice[8..]) as usize;
17433 let end = molecule::unpack_number(&slice[12..]) as usize;
17434 HeaderDigestVecReader::new_unchecked(&self.as_slice()[start..end])
17435 }
17436 pub fn headers(&self) -> HeaderVecReader<'r> {
17437 let slice = self.as_slice();
17438 let start = molecule::unpack_number(&slice[12..]) as usize;
17439 let end = molecule::unpack_number(&slice[16..]) as usize;
17440 HeaderVecReader::new_unchecked(&self.as_slice()[start..end])
17441 }
17442 pub fn missing_block_hashes(&self) -> Byte32VecReader<'r> {
17443 let slice = self.as_slice();
17444 let start = molecule::unpack_number(&slice[16..]) as usize;
17445 if self.has_extra_fields() {
17446 let end = molecule::unpack_number(&slice[20..]) as usize;
17447 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
17448 } else {
17449 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
17450 }
17451 }
17452}
17453impl<'r> molecule::prelude::Reader<'r> for SendBlocksProofReader<'r> {
17454 type Entity = SendBlocksProof;
17455 const NAME: &'static str = "SendBlocksProofReader";
17456 fn to_entity(&self) -> Self::Entity {
17457 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
17458 }
17459 fn new_unchecked(slice: &'r [u8]) -> Self {
17460 SendBlocksProofReader(slice)
17461 }
17462 fn as_slice(&self) -> &'r [u8] {
17463 self.0
17464 }
17465 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
17466 use molecule::verification_error as ve;
17467 let slice_len = slice.len();
17468 if slice_len < molecule::NUMBER_SIZE {
17469 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
17470 }
17471 let total_size = molecule::unpack_number(slice) as usize;
17472 if slice_len != total_size {
17473 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
17474 }
17475 if slice_len < molecule::NUMBER_SIZE * 2 {
17476 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
17477 }
17478 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
17479 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
17480 return ve!(Self, OffsetsNotMatch);
17481 }
17482 if slice_len < offset_first {
17483 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
17484 }
17485 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
17486 if field_count < Self::FIELD_COUNT {
17487 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17488 } else if !compatible && field_count > Self::FIELD_COUNT {
17489 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17490 };
17491 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
17492 .chunks_exact(molecule::NUMBER_SIZE)
17493 .map(|x| molecule::unpack_number(x) as usize)
17494 .collect();
17495 offsets.push(total_size);
17496 if offsets.windows(2).any(|i| i[0] > i[1]) {
17497 return ve!(Self, OffsetsNotMatch);
17498 }
17499 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
17500 HeaderDigestVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
17501 HeaderVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
17502 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
17503 Ok(())
17504 }
17505}
17506#[derive(Debug, Default)]
17507pub struct SendBlocksProofBuilder {
17508 pub(crate) last_header: VerifiableHeader,
17509 pub(crate) proof: HeaderDigestVec,
17510 pub(crate) headers: HeaderVec,
17511 pub(crate) missing_block_hashes: Byte32Vec,
17512}
17513impl SendBlocksProofBuilder {
17514 pub const FIELD_COUNT: usize = 4;
17515 pub fn last_header(mut self, v: VerifiableHeader) -> Self {
17516 self.last_header = v;
17517 self
17518 }
17519 pub fn proof(mut self, v: HeaderDigestVec) -> Self {
17520 self.proof = v;
17521 self
17522 }
17523 pub fn headers(mut self, v: HeaderVec) -> Self {
17524 self.headers = v;
17525 self
17526 }
17527 pub fn missing_block_hashes(mut self, v: Byte32Vec) -> Self {
17528 self.missing_block_hashes = v;
17529 self
17530 }
17531}
17532impl molecule::prelude::Builder for SendBlocksProofBuilder {
17533 type Entity = SendBlocksProof;
17534 const NAME: &'static str = "SendBlocksProofBuilder";
17535 fn expected_length(&self) -> usize {
17536 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
17537 + self.last_header.as_slice().len()
17538 + self.proof.as_slice().len()
17539 + self.headers.as_slice().len()
17540 + self.missing_block_hashes.as_slice().len()
17541 }
17542 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
17543 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
17544 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
17545 offsets.push(total_size);
17546 total_size += self.last_header.as_slice().len();
17547 offsets.push(total_size);
17548 total_size += self.proof.as_slice().len();
17549 offsets.push(total_size);
17550 total_size += self.headers.as_slice().len();
17551 offsets.push(total_size);
17552 total_size += self.missing_block_hashes.as_slice().len();
17553 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
17554 for offset in offsets.into_iter() {
17555 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
17556 }
17557 writer.write_all(self.last_header.as_slice())?;
17558 writer.write_all(self.proof.as_slice())?;
17559 writer.write_all(self.headers.as_slice())?;
17560 writer.write_all(self.missing_block_hashes.as_slice())?;
17561 Ok(())
17562 }
17563 fn build(&self) -> Self::Entity {
17564 let mut inner = Vec::with_capacity(self.expected_length());
17565 self.write(&mut inner)
17566 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
17567 SendBlocksProof::new_unchecked(inner.into())
17568 }
17569}
17570#[derive(Clone)]
17571pub struct SendBlocksProofV1(molecule::bytes::Bytes);
17572impl ::core::fmt::LowerHex for SendBlocksProofV1 {
17573 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17574 use molecule::hex_string;
17575 if f.alternate() {
17576 write!(f, "0x")?;
17577 }
17578 write!(f, "{}", hex_string(self.as_slice()))
17579 }
17580}
17581impl ::core::fmt::Debug for SendBlocksProofV1 {
17582 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17583 write!(f, "{}({:#x})", Self::NAME, self)
17584 }
17585}
17586impl ::core::fmt::Display for SendBlocksProofV1 {
17587 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17588 write!(f, "{} {{ ", Self::NAME)?;
17589 write!(f, "{}: {}", "last_header", self.last_header())?;
17590 write!(f, ", {}: {}", "proof", self.proof())?;
17591 write!(f, ", {}: {}", "headers", self.headers())?;
17592 write!(
17593 f,
17594 ", {}: {}",
17595 "missing_block_hashes",
17596 self.missing_block_hashes()
17597 )?;
17598 write!(
17599 f,
17600 ", {}: {}",
17601 "blocks_uncles_hash",
17602 self.blocks_uncles_hash()
17603 )?;
17604 write!(f, ", {}: {}", "blocks_extension", self.blocks_extension())?;
17605 let extra_count = self.count_extra_fields();
17606 if extra_count != 0 {
17607 write!(f, ", .. ({} fields)", extra_count)?;
17608 }
17609 write!(f, " }}")
17610 }
17611}
17612impl ::core::default::Default for SendBlocksProofV1 {
17613 fn default() -> Self {
17614 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
17615 SendBlocksProofV1::new_unchecked(v)
17616 }
17617}
17618impl SendBlocksProofV1 {
17619 const DEFAULT_VALUE: [u8; 428] = [
17620 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,
17621 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,
17622 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17623 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17624 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17625 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17626 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17627 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17628 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17629 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17630 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17631 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17632 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17633 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17634 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
17635 ];
17636 pub const FIELD_COUNT: usize = 6;
17637 pub fn total_size(&self) -> usize {
17638 molecule::unpack_number(self.as_slice()) as usize
17639 }
17640 pub fn field_count(&self) -> usize {
17641 if self.total_size() == molecule::NUMBER_SIZE {
17642 0
17643 } else {
17644 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17645 }
17646 }
17647 pub fn count_extra_fields(&self) -> usize {
17648 self.field_count() - Self::FIELD_COUNT
17649 }
17650 pub fn has_extra_fields(&self) -> bool {
17651 Self::FIELD_COUNT != self.field_count()
17652 }
17653 pub fn last_header(&self) -> VerifiableHeader {
17654 let slice = self.as_slice();
17655 let start = molecule::unpack_number(&slice[4..]) as usize;
17656 let end = molecule::unpack_number(&slice[8..]) as usize;
17657 VerifiableHeader::new_unchecked(self.0.slice(start..end))
17658 }
17659 pub fn proof(&self) -> HeaderDigestVec {
17660 let slice = self.as_slice();
17661 let start = molecule::unpack_number(&slice[8..]) as usize;
17662 let end = molecule::unpack_number(&slice[12..]) as usize;
17663 HeaderDigestVec::new_unchecked(self.0.slice(start..end))
17664 }
17665 pub fn headers(&self) -> HeaderVec {
17666 let slice = self.as_slice();
17667 let start = molecule::unpack_number(&slice[12..]) as usize;
17668 let end = molecule::unpack_number(&slice[16..]) as usize;
17669 HeaderVec::new_unchecked(self.0.slice(start..end))
17670 }
17671 pub fn missing_block_hashes(&self) -> Byte32Vec {
17672 let slice = self.as_slice();
17673 let start = molecule::unpack_number(&slice[16..]) as usize;
17674 let end = molecule::unpack_number(&slice[20..]) as usize;
17675 Byte32Vec::new_unchecked(self.0.slice(start..end))
17676 }
17677 pub fn blocks_uncles_hash(&self) -> Byte32Vec {
17678 let slice = self.as_slice();
17679 let start = molecule::unpack_number(&slice[20..]) as usize;
17680 let end = molecule::unpack_number(&slice[24..]) as usize;
17681 Byte32Vec::new_unchecked(self.0.slice(start..end))
17682 }
17683 pub fn blocks_extension(&self) -> BytesOptVec {
17684 let slice = self.as_slice();
17685 let start = molecule::unpack_number(&slice[24..]) as usize;
17686 if self.has_extra_fields() {
17687 let end = molecule::unpack_number(&slice[28..]) as usize;
17688 BytesOptVec::new_unchecked(self.0.slice(start..end))
17689 } else {
17690 BytesOptVec::new_unchecked(self.0.slice(start..))
17691 }
17692 }
17693 pub fn as_reader<'r>(&'r self) -> SendBlocksProofV1Reader<'r> {
17694 SendBlocksProofV1Reader::new_unchecked(self.as_slice())
17695 }
17696}
17697impl molecule::prelude::Entity for SendBlocksProofV1 {
17698 type Builder = SendBlocksProofV1Builder;
17699 const NAME: &'static str = "SendBlocksProofV1";
17700 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
17701 SendBlocksProofV1(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 SendBlocksProofV1Reader::from_slice(slice).map(|reader| reader.to_entity())
17711 }
17712 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
17713 SendBlocksProofV1Reader::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 .missing_block_hashes(self.missing_block_hashes())
17724 .blocks_uncles_hash(self.blocks_uncles_hash())
17725 .blocks_extension(self.blocks_extension())
17726 }
17727}
17728#[derive(Clone, Copy)]
17729pub struct SendBlocksProofV1Reader<'r>(&'r [u8]);
17730impl<'r> ::core::fmt::LowerHex for SendBlocksProofV1Reader<'r> {
17731 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17732 use molecule::hex_string;
17733 if f.alternate() {
17734 write!(f, "0x")?;
17735 }
17736 write!(f, "{}", hex_string(self.as_slice()))
17737 }
17738}
17739impl<'r> ::core::fmt::Debug for SendBlocksProofV1Reader<'r> {
17740 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17741 write!(f, "{}({:#x})", Self::NAME, self)
17742 }
17743}
17744impl<'r> ::core::fmt::Display for SendBlocksProofV1Reader<'r> {
17745 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17746 write!(f, "{} {{ ", Self::NAME)?;
17747 write!(f, "{}: {}", "last_header", self.last_header())?;
17748 write!(f, ", {}: {}", "proof", self.proof())?;
17749 write!(f, ", {}: {}", "headers", self.headers())?;
17750 write!(
17751 f,
17752 ", {}: {}",
17753 "missing_block_hashes",
17754 self.missing_block_hashes()
17755 )?;
17756 write!(
17757 f,
17758 ", {}: {}",
17759 "blocks_uncles_hash",
17760 self.blocks_uncles_hash()
17761 )?;
17762 write!(f, ", {}: {}", "blocks_extension", self.blocks_extension())?;
17763 let extra_count = self.count_extra_fields();
17764 if extra_count != 0 {
17765 write!(f, ", .. ({} fields)", extra_count)?;
17766 }
17767 write!(f, " }}")
17768 }
17769}
17770impl<'r> SendBlocksProofV1Reader<'r> {
17771 pub const FIELD_COUNT: usize = 6;
17772 pub fn total_size(&self) -> usize {
17773 molecule::unpack_number(self.as_slice()) as usize
17774 }
17775 pub fn field_count(&self) -> usize {
17776 if self.total_size() == molecule::NUMBER_SIZE {
17777 0
17778 } else {
17779 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
17780 }
17781 }
17782 pub fn count_extra_fields(&self) -> usize {
17783 self.field_count() - Self::FIELD_COUNT
17784 }
17785 pub fn has_extra_fields(&self) -> bool {
17786 Self::FIELD_COUNT != self.field_count()
17787 }
17788 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
17789 let slice = self.as_slice();
17790 let start = molecule::unpack_number(&slice[4..]) as usize;
17791 let end = molecule::unpack_number(&slice[8..]) as usize;
17792 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
17793 }
17794 pub fn proof(&self) -> HeaderDigestVecReader<'r> {
17795 let slice = self.as_slice();
17796 let start = molecule::unpack_number(&slice[8..]) as usize;
17797 let end = molecule::unpack_number(&slice[12..]) as usize;
17798 HeaderDigestVecReader::new_unchecked(&self.as_slice()[start..end])
17799 }
17800 pub fn headers(&self) -> HeaderVecReader<'r> {
17801 let slice = self.as_slice();
17802 let start = molecule::unpack_number(&slice[12..]) as usize;
17803 let end = molecule::unpack_number(&slice[16..]) as usize;
17804 HeaderVecReader::new_unchecked(&self.as_slice()[start..end])
17805 }
17806 pub fn missing_block_hashes(&self) -> Byte32VecReader<'r> {
17807 let slice = self.as_slice();
17808 let start = molecule::unpack_number(&slice[16..]) as usize;
17809 let end = molecule::unpack_number(&slice[20..]) as usize;
17810 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
17811 }
17812 pub fn blocks_uncles_hash(&self) -> Byte32VecReader<'r> {
17813 let slice = self.as_slice();
17814 let start = molecule::unpack_number(&slice[20..]) as usize;
17815 let end = molecule::unpack_number(&slice[24..]) as usize;
17816 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
17817 }
17818 pub fn blocks_extension(&self) -> BytesOptVecReader<'r> {
17819 let slice = self.as_slice();
17820 let start = molecule::unpack_number(&slice[24..]) as usize;
17821 if self.has_extra_fields() {
17822 let end = molecule::unpack_number(&slice[28..]) as usize;
17823 BytesOptVecReader::new_unchecked(&self.as_slice()[start..end])
17824 } else {
17825 BytesOptVecReader::new_unchecked(&self.as_slice()[start..])
17826 }
17827 }
17828}
17829impl<'r> molecule::prelude::Reader<'r> for SendBlocksProofV1Reader<'r> {
17830 type Entity = SendBlocksProofV1;
17831 const NAME: &'static str = "SendBlocksProofV1Reader";
17832 fn to_entity(&self) -> Self::Entity {
17833 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
17834 }
17835 fn new_unchecked(slice: &'r [u8]) -> Self {
17836 SendBlocksProofV1Reader(slice)
17837 }
17838 fn as_slice(&self) -> &'r [u8] {
17839 self.0
17840 }
17841 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
17842 use molecule::verification_error as ve;
17843 let slice_len = slice.len();
17844 if slice_len < molecule::NUMBER_SIZE {
17845 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
17846 }
17847 let total_size = molecule::unpack_number(slice) as usize;
17848 if slice_len != total_size {
17849 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
17850 }
17851 if slice_len < molecule::NUMBER_SIZE * 2 {
17852 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
17853 }
17854 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
17855 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
17856 return ve!(Self, OffsetsNotMatch);
17857 }
17858 if slice_len < offset_first {
17859 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
17860 }
17861 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
17862 if field_count < Self::FIELD_COUNT {
17863 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17864 } else if !compatible && field_count > Self::FIELD_COUNT {
17865 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
17866 };
17867 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
17868 .chunks_exact(molecule::NUMBER_SIZE)
17869 .map(|x| molecule::unpack_number(x) as usize)
17870 .collect();
17871 offsets.push(total_size);
17872 if offsets.windows(2).any(|i| i[0] > i[1]) {
17873 return ve!(Self, OffsetsNotMatch);
17874 }
17875 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
17876 HeaderDigestVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
17877 HeaderVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
17878 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
17879 Byte32VecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
17880 BytesOptVecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
17881 Ok(())
17882 }
17883}
17884#[derive(Debug, Default)]
17885pub struct SendBlocksProofV1Builder {
17886 pub(crate) last_header: VerifiableHeader,
17887 pub(crate) proof: HeaderDigestVec,
17888 pub(crate) headers: HeaderVec,
17889 pub(crate) missing_block_hashes: Byte32Vec,
17890 pub(crate) blocks_uncles_hash: Byte32Vec,
17891 pub(crate) blocks_extension: BytesOptVec,
17892}
17893impl SendBlocksProofV1Builder {
17894 pub const FIELD_COUNT: usize = 6;
17895 pub fn last_header(mut self, v: VerifiableHeader) -> Self {
17896 self.last_header = v;
17897 self
17898 }
17899 pub fn proof(mut self, v: HeaderDigestVec) -> Self {
17900 self.proof = v;
17901 self
17902 }
17903 pub fn headers(mut self, v: HeaderVec) -> Self {
17904 self.headers = v;
17905 self
17906 }
17907 pub fn missing_block_hashes(mut self, v: Byte32Vec) -> Self {
17908 self.missing_block_hashes = v;
17909 self
17910 }
17911 pub fn blocks_uncles_hash(mut self, v: Byte32Vec) -> Self {
17912 self.blocks_uncles_hash = v;
17913 self
17914 }
17915 pub fn blocks_extension(mut self, v: BytesOptVec) -> Self {
17916 self.blocks_extension = v;
17917 self
17918 }
17919}
17920impl molecule::prelude::Builder for SendBlocksProofV1Builder {
17921 type Entity = SendBlocksProofV1;
17922 const NAME: &'static str = "SendBlocksProofV1Builder";
17923 fn expected_length(&self) -> usize {
17924 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
17925 + self.last_header.as_slice().len()
17926 + self.proof.as_slice().len()
17927 + self.headers.as_slice().len()
17928 + self.missing_block_hashes.as_slice().len()
17929 + self.blocks_uncles_hash.as_slice().len()
17930 + self.blocks_extension.as_slice().len()
17931 }
17932 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
17933 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
17934 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
17935 offsets.push(total_size);
17936 total_size += self.last_header.as_slice().len();
17937 offsets.push(total_size);
17938 total_size += self.proof.as_slice().len();
17939 offsets.push(total_size);
17940 total_size += self.headers.as_slice().len();
17941 offsets.push(total_size);
17942 total_size += self.missing_block_hashes.as_slice().len();
17943 offsets.push(total_size);
17944 total_size += self.blocks_uncles_hash.as_slice().len();
17945 offsets.push(total_size);
17946 total_size += self.blocks_extension.as_slice().len();
17947 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
17948 for offset in offsets.into_iter() {
17949 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
17950 }
17951 writer.write_all(self.last_header.as_slice())?;
17952 writer.write_all(self.proof.as_slice())?;
17953 writer.write_all(self.headers.as_slice())?;
17954 writer.write_all(self.missing_block_hashes.as_slice())?;
17955 writer.write_all(self.blocks_uncles_hash.as_slice())?;
17956 writer.write_all(self.blocks_extension.as_slice())?;
17957 Ok(())
17958 }
17959 fn build(&self) -> Self::Entity {
17960 let mut inner = Vec::with_capacity(self.expected_length());
17961 self.write(&mut inner)
17962 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
17963 SendBlocksProofV1::new_unchecked(inner.into())
17964 }
17965}
17966#[derive(Clone)]
17967pub struct GetTransactionsProof(molecule::bytes::Bytes);
17968impl ::core::fmt::LowerHex for GetTransactionsProof {
17969 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17970 use molecule::hex_string;
17971 if f.alternate() {
17972 write!(f, "0x")?;
17973 }
17974 write!(f, "{}", hex_string(self.as_slice()))
17975 }
17976}
17977impl ::core::fmt::Debug for GetTransactionsProof {
17978 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17979 write!(f, "{}({:#x})", Self::NAME, self)
17980 }
17981}
17982impl ::core::fmt::Display for GetTransactionsProof {
17983 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17984 write!(f, "{} {{ ", Self::NAME)?;
17985 write!(f, "{}: {}", "last_hash", self.last_hash())?;
17986 write!(f, ", {}: {}", "tx_hashes", self.tx_hashes())?;
17987 let extra_count = self.count_extra_fields();
17988 if extra_count != 0 {
17989 write!(f, ", .. ({} fields)", extra_count)?;
17990 }
17991 write!(f, " }}")
17992 }
17993}
17994impl ::core::default::Default for GetTransactionsProof {
17995 fn default() -> Self {
17996 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
17997 GetTransactionsProof::new_unchecked(v)
17998 }
17999}
18000impl GetTransactionsProof {
18001 const DEFAULT_VALUE: [u8; 48] = [
18002 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,
18003 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18004 ];
18005 pub const FIELD_COUNT: usize = 2;
18006 pub fn total_size(&self) -> usize {
18007 molecule::unpack_number(self.as_slice()) as usize
18008 }
18009 pub fn field_count(&self) -> usize {
18010 if self.total_size() == molecule::NUMBER_SIZE {
18011 0
18012 } else {
18013 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18014 }
18015 }
18016 pub fn count_extra_fields(&self) -> usize {
18017 self.field_count() - Self::FIELD_COUNT
18018 }
18019 pub fn has_extra_fields(&self) -> bool {
18020 Self::FIELD_COUNT != self.field_count()
18021 }
18022 pub fn last_hash(&self) -> Byte32 {
18023 let slice = self.as_slice();
18024 let start = molecule::unpack_number(&slice[4..]) as usize;
18025 let end = molecule::unpack_number(&slice[8..]) as usize;
18026 Byte32::new_unchecked(self.0.slice(start..end))
18027 }
18028 pub fn tx_hashes(&self) -> Byte32Vec {
18029 let slice = self.as_slice();
18030 let start = molecule::unpack_number(&slice[8..]) as usize;
18031 if self.has_extra_fields() {
18032 let end = molecule::unpack_number(&slice[12..]) as usize;
18033 Byte32Vec::new_unchecked(self.0.slice(start..end))
18034 } else {
18035 Byte32Vec::new_unchecked(self.0.slice(start..))
18036 }
18037 }
18038 pub fn as_reader<'r>(&'r self) -> GetTransactionsProofReader<'r> {
18039 GetTransactionsProofReader::new_unchecked(self.as_slice())
18040 }
18041}
18042impl molecule::prelude::Entity for GetTransactionsProof {
18043 type Builder = GetTransactionsProofBuilder;
18044 const NAME: &'static str = "GetTransactionsProof";
18045 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
18046 GetTransactionsProof(data)
18047 }
18048 fn as_bytes(&self) -> molecule::bytes::Bytes {
18049 self.0.clone()
18050 }
18051 fn as_slice(&self) -> &[u8] {
18052 &self.0[..]
18053 }
18054 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18055 GetTransactionsProofReader::from_slice(slice).map(|reader| reader.to_entity())
18056 }
18057 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18058 GetTransactionsProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
18059 }
18060 fn new_builder() -> Self::Builder {
18061 ::core::default::Default::default()
18062 }
18063 fn as_builder(self) -> Self::Builder {
18064 Self::new_builder()
18065 .last_hash(self.last_hash())
18066 .tx_hashes(self.tx_hashes())
18067 }
18068}
18069#[derive(Clone, Copy)]
18070pub struct GetTransactionsProofReader<'r>(&'r [u8]);
18071impl<'r> ::core::fmt::LowerHex for GetTransactionsProofReader<'r> {
18072 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18073 use molecule::hex_string;
18074 if f.alternate() {
18075 write!(f, "0x")?;
18076 }
18077 write!(f, "{}", hex_string(self.as_slice()))
18078 }
18079}
18080impl<'r> ::core::fmt::Debug for GetTransactionsProofReader<'r> {
18081 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18082 write!(f, "{}({:#x})", Self::NAME, self)
18083 }
18084}
18085impl<'r> ::core::fmt::Display for GetTransactionsProofReader<'r> {
18086 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18087 write!(f, "{} {{ ", Self::NAME)?;
18088 write!(f, "{}: {}", "last_hash", self.last_hash())?;
18089 write!(f, ", {}: {}", "tx_hashes", self.tx_hashes())?;
18090 let extra_count = self.count_extra_fields();
18091 if extra_count != 0 {
18092 write!(f, ", .. ({} fields)", extra_count)?;
18093 }
18094 write!(f, " }}")
18095 }
18096}
18097impl<'r> GetTransactionsProofReader<'r> {
18098 pub const FIELD_COUNT: usize = 2;
18099 pub fn total_size(&self) -> usize {
18100 molecule::unpack_number(self.as_slice()) as usize
18101 }
18102 pub fn field_count(&self) -> usize {
18103 if self.total_size() == molecule::NUMBER_SIZE {
18104 0
18105 } else {
18106 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18107 }
18108 }
18109 pub fn count_extra_fields(&self) -> usize {
18110 self.field_count() - Self::FIELD_COUNT
18111 }
18112 pub fn has_extra_fields(&self) -> bool {
18113 Self::FIELD_COUNT != self.field_count()
18114 }
18115 pub fn last_hash(&self) -> Byte32Reader<'r> {
18116 let slice = self.as_slice();
18117 let start = molecule::unpack_number(&slice[4..]) as usize;
18118 let end = molecule::unpack_number(&slice[8..]) as usize;
18119 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
18120 }
18121 pub fn tx_hashes(&self) -> Byte32VecReader<'r> {
18122 let slice = self.as_slice();
18123 let start = molecule::unpack_number(&slice[8..]) as usize;
18124 if self.has_extra_fields() {
18125 let end = molecule::unpack_number(&slice[12..]) as usize;
18126 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
18127 } else {
18128 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
18129 }
18130 }
18131}
18132impl<'r> molecule::prelude::Reader<'r> for GetTransactionsProofReader<'r> {
18133 type Entity = GetTransactionsProof;
18134 const NAME: &'static str = "GetTransactionsProofReader";
18135 fn to_entity(&self) -> Self::Entity {
18136 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
18137 }
18138 fn new_unchecked(slice: &'r [u8]) -> Self {
18139 GetTransactionsProofReader(slice)
18140 }
18141 fn as_slice(&self) -> &'r [u8] {
18142 self.0
18143 }
18144 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
18145 use molecule::verification_error as ve;
18146 let slice_len = slice.len();
18147 if slice_len < molecule::NUMBER_SIZE {
18148 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
18149 }
18150 let total_size = molecule::unpack_number(slice) as usize;
18151 if slice_len != total_size {
18152 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
18153 }
18154 if slice_len < molecule::NUMBER_SIZE * 2 {
18155 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
18156 }
18157 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
18158 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
18159 return ve!(Self, OffsetsNotMatch);
18160 }
18161 if slice_len < offset_first {
18162 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
18163 }
18164 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
18165 if field_count < Self::FIELD_COUNT {
18166 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18167 } else if !compatible && field_count > Self::FIELD_COUNT {
18168 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18169 };
18170 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
18171 .chunks_exact(molecule::NUMBER_SIZE)
18172 .map(|x| molecule::unpack_number(x) as usize)
18173 .collect();
18174 offsets.push(total_size);
18175 if offsets.windows(2).any(|i| i[0] > i[1]) {
18176 return ve!(Self, OffsetsNotMatch);
18177 }
18178 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
18179 Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
18180 Ok(())
18181 }
18182}
18183#[derive(Debug, Default)]
18184pub struct GetTransactionsProofBuilder {
18185 pub(crate) last_hash: Byte32,
18186 pub(crate) tx_hashes: Byte32Vec,
18187}
18188impl GetTransactionsProofBuilder {
18189 pub const FIELD_COUNT: usize = 2;
18190 pub fn last_hash(mut self, v: Byte32) -> Self {
18191 self.last_hash = v;
18192 self
18193 }
18194 pub fn tx_hashes(mut self, v: Byte32Vec) -> Self {
18195 self.tx_hashes = v;
18196 self
18197 }
18198}
18199impl molecule::prelude::Builder for GetTransactionsProofBuilder {
18200 type Entity = GetTransactionsProof;
18201 const NAME: &'static str = "GetTransactionsProofBuilder";
18202 fn expected_length(&self) -> usize {
18203 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
18204 + self.last_hash.as_slice().len()
18205 + self.tx_hashes.as_slice().len()
18206 }
18207 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
18208 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
18209 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
18210 offsets.push(total_size);
18211 total_size += self.last_hash.as_slice().len();
18212 offsets.push(total_size);
18213 total_size += self.tx_hashes.as_slice().len();
18214 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
18215 for offset in offsets.into_iter() {
18216 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
18217 }
18218 writer.write_all(self.last_hash.as_slice())?;
18219 writer.write_all(self.tx_hashes.as_slice())?;
18220 Ok(())
18221 }
18222 fn build(&self) -> Self::Entity {
18223 let mut inner = Vec::with_capacity(self.expected_length());
18224 self.write(&mut inner)
18225 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
18226 GetTransactionsProof::new_unchecked(inner.into())
18227 }
18228}
18229#[derive(Clone)]
18230pub struct SendTransactionsProof(molecule::bytes::Bytes);
18231impl ::core::fmt::LowerHex for SendTransactionsProof {
18232 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18233 use molecule::hex_string;
18234 if f.alternate() {
18235 write!(f, "0x")?;
18236 }
18237 write!(f, "{}", hex_string(self.as_slice()))
18238 }
18239}
18240impl ::core::fmt::Debug for SendTransactionsProof {
18241 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18242 write!(f, "{}({:#x})", Self::NAME, self)
18243 }
18244}
18245impl ::core::fmt::Display for SendTransactionsProof {
18246 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18247 write!(f, "{} {{ ", Self::NAME)?;
18248 write!(f, "{}: {}", "last_header", self.last_header())?;
18249 write!(f, ", {}: {}", "proof", self.proof())?;
18250 write!(f, ", {}: {}", "filtered_blocks", self.filtered_blocks())?;
18251 write!(f, ", {}: {}", "missing_tx_hashes", self.missing_tx_hashes())?;
18252 let extra_count = self.count_extra_fields();
18253 if extra_count != 0 {
18254 write!(f, ", .. ({} fields)", extra_count)?;
18255 }
18256 write!(f, " }}")
18257 }
18258}
18259impl ::core::default::Default for SendTransactionsProof {
18260 fn default() -> Self {
18261 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
18262 SendTransactionsProof::new_unchecked(v)
18263 }
18264}
18265impl SendTransactionsProof {
18266 const DEFAULT_VALUE: [u8; 412] = [
18267 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,
18268 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,
18269 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18270 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18271 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18272 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18273 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18274 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18275 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18276 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18277 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18278 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18280 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,
18281 ];
18282 pub const FIELD_COUNT: usize = 4;
18283 pub fn total_size(&self) -> usize {
18284 molecule::unpack_number(self.as_slice()) as usize
18285 }
18286 pub fn field_count(&self) -> usize {
18287 if self.total_size() == molecule::NUMBER_SIZE {
18288 0
18289 } else {
18290 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18291 }
18292 }
18293 pub fn count_extra_fields(&self) -> usize {
18294 self.field_count() - Self::FIELD_COUNT
18295 }
18296 pub fn has_extra_fields(&self) -> bool {
18297 Self::FIELD_COUNT != self.field_count()
18298 }
18299 pub fn last_header(&self) -> VerifiableHeader {
18300 let slice = self.as_slice();
18301 let start = molecule::unpack_number(&slice[4..]) as usize;
18302 let end = molecule::unpack_number(&slice[8..]) as usize;
18303 VerifiableHeader::new_unchecked(self.0.slice(start..end))
18304 }
18305 pub fn proof(&self) -> HeaderDigestVec {
18306 let slice = self.as_slice();
18307 let start = molecule::unpack_number(&slice[8..]) as usize;
18308 let end = molecule::unpack_number(&slice[12..]) as usize;
18309 HeaderDigestVec::new_unchecked(self.0.slice(start..end))
18310 }
18311 pub fn filtered_blocks(&self) -> FilteredBlockVec {
18312 let slice = self.as_slice();
18313 let start = molecule::unpack_number(&slice[12..]) as usize;
18314 let end = molecule::unpack_number(&slice[16..]) as usize;
18315 FilteredBlockVec::new_unchecked(self.0.slice(start..end))
18316 }
18317 pub fn missing_tx_hashes(&self) -> Byte32Vec {
18318 let slice = self.as_slice();
18319 let start = molecule::unpack_number(&slice[16..]) as usize;
18320 if self.has_extra_fields() {
18321 let end = molecule::unpack_number(&slice[20..]) as usize;
18322 Byte32Vec::new_unchecked(self.0.slice(start..end))
18323 } else {
18324 Byte32Vec::new_unchecked(self.0.slice(start..))
18325 }
18326 }
18327 pub fn as_reader<'r>(&'r self) -> SendTransactionsProofReader<'r> {
18328 SendTransactionsProofReader::new_unchecked(self.as_slice())
18329 }
18330}
18331impl molecule::prelude::Entity for SendTransactionsProof {
18332 type Builder = SendTransactionsProofBuilder;
18333 const NAME: &'static str = "SendTransactionsProof";
18334 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
18335 SendTransactionsProof(data)
18336 }
18337 fn as_bytes(&self) -> molecule::bytes::Bytes {
18338 self.0.clone()
18339 }
18340 fn as_slice(&self) -> &[u8] {
18341 &self.0[..]
18342 }
18343 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18344 SendTransactionsProofReader::from_slice(slice).map(|reader| reader.to_entity())
18345 }
18346 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18347 SendTransactionsProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
18348 }
18349 fn new_builder() -> Self::Builder {
18350 ::core::default::Default::default()
18351 }
18352 fn as_builder(self) -> Self::Builder {
18353 Self::new_builder()
18354 .last_header(self.last_header())
18355 .proof(self.proof())
18356 .filtered_blocks(self.filtered_blocks())
18357 .missing_tx_hashes(self.missing_tx_hashes())
18358 }
18359}
18360#[derive(Clone, Copy)]
18361pub struct SendTransactionsProofReader<'r>(&'r [u8]);
18362impl<'r> ::core::fmt::LowerHex for SendTransactionsProofReader<'r> {
18363 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18364 use molecule::hex_string;
18365 if f.alternate() {
18366 write!(f, "0x")?;
18367 }
18368 write!(f, "{}", hex_string(self.as_slice()))
18369 }
18370}
18371impl<'r> ::core::fmt::Debug for SendTransactionsProofReader<'r> {
18372 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18373 write!(f, "{}({:#x})", Self::NAME, self)
18374 }
18375}
18376impl<'r> ::core::fmt::Display for SendTransactionsProofReader<'r> {
18377 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18378 write!(f, "{} {{ ", Self::NAME)?;
18379 write!(f, "{}: {}", "last_header", self.last_header())?;
18380 write!(f, ", {}: {}", "proof", self.proof())?;
18381 write!(f, ", {}: {}", "filtered_blocks", self.filtered_blocks())?;
18382 write!(f, ", {}: {}", "missing_tx_hashes", self.missing_tx_hashes())?;
18383 let extra_count = self.count_extra_fields();
18384 if extra_count != 0 {
18385 write!(f, ", .. ({} fields)", extra_count)?;
18386 }
18387 write!(f, " }}")
18388 }
18389}
18390impl<'r> SendTransactionsProofReader<'r> {
18391 pub const FIELD_COUNT: usize = 4;
18392 pub fn total_size(&self) -> usize {
18393 molecule::unpack_number(self.as_slice()) as usize
18394 }
18395 pub fn field_count(&self) -> usize {
18396 if self.total_size() == molecule::NUMBER_SIZE {
18397 0
18398 } else {
18399 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18400 }
18401 }
18402 pub fn count_extra_fields(&self) -> usize {
18403 self.field_count() - Self::FIELD_COUNT
18404 }
18405 pub fn has_extra_fields(&self) -> bool {
18406 Self::FIELD_COUNT != self.field_count()
18407 }
18408 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
18409 let slice = self.as_slice();
18410 let start = molecule::unpack_number(&slice[4..]) as usize;
18411 let end = molecule::unpack_number(&slice[8..]) as usize;
18412 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
18413 }
18414 pub fn proof(&self) -> HeaderDigestVecReader<'r> {
18415 let slice = self.as_slice();
18416 let start = molecule::unpack_number(&slice[8..]) as usize;
18417 let end = molecule::unpack_number(&slice[12..]) as usize;
18418 HeaderDigestVecReader::new_unchecked(&self.as_slice()[start..end])
18419 }
18420 pub fn filtered_blocks(&self) -> FilteredBlockVecReader<'r> {
18421 let slice = self.as_slice();
18422 let start = molecule::unpack_number(&slice[12..]) as usize;
18423 let end = molecule::unpack_number(&slice[16..]) as usize;
18424 FilteredBlockVecReader::new_unchecked(&self.as_slice()[start..end])
18425 }
18426 pub fn missing_tx_hashes(&self) -> Byte32VecReader<'r> {
18427 let slice = self.as_slice();
18428 let start = molecule::unpack_number(&slice[16..]) as usize;
18429 if self.has_extra_fields() {
18430 let end = molecule::unpack_number(&slice[20..]) as usize;
18431 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
18432 } else {
18433 Byte32VecReader::new_unchecked(&self.as_slice()[start..])
18434 }
18435 }
18436}
18437impl<'r> molecule::prelude::Reader<'r> for SendTransactionsProofReader<'r> {
18438 type Entity = SendTransactionsProof;
18439 const NAME: &'static str = "SendTransactionsProofReader";
18440 fn to_entity(&self) -> Self::Entity {
18441 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
18442 }
18443 fn new_unchecked(slice: &'r [u8]) -> Self {
18444 SendTransactionsProofReader(slice)
18445 }
18446 fn as_slice(&self) -> &'r [u8] {
18447 self.0
18448 }
18449 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
18450 use molecule::verification_error as ve;
18451 let slice_len = slice.len();
18452 if slice_len < molecule::NUMBER_SIZE {
18453 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
18454 }
18455 let total_size = molecule::unpack_number(slice) as usize;
18456 if slice_len != total_size {
18457 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
18458 }
18459 if slice_len < molecule::NUMBER_SIZE * 2 {
18460 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
18461 }
18462 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
18463 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
18464 return ve!(Self, OffsetsNotMatch);
18465 }
18466 if slice_len < offset_first {
18467 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
18468 }
18469 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
18470 if field_count < Self::FIELD_COUNT {
18471 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18472 } else if !compatible && field_count > Self::FIELD_COUNT {
18473 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18474 };
18475 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
18476 .chunks_exact(molecule::NUMBER_SIZE)
18477 .map(|x| molecule::unpack_number(x) as usize)
18478 .collect();
18479 offsets.push(total_size);
18480 if offsets.windows(2).any(|i| i[0] > i[1]) {
18481 return ve!(Self, OffsetsNotMatch);
18482 }
18483 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
18484 HeaderDigestVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
18485 FilteredBlockVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
18486 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
18487 Ok(())
18488 }
18489}
18490#[derive(Debug, Default)]
18491pub struct SendTransactionsProofBuilder {
18492 pub(crate) last_header: VerifiableHeader,
18493 pub(crate) proof: HeaderDigestVec,
18494 pub(crate) filtered_blocks: FilteredBlockVec,
18495 pub(crate) missing_tx_hashes: Byte32Vec,
18496}
18497impl SendTransactionsProofBuilder {
18498 pub const FIELD_COUNT: usize = 4;
18499 pub fn last_header(mut self, v: VerifiableHeader) -> Self {
18500 self.last_header = v;
18501 self
18502 }
18503 pub fn proof(mut self, v: HeaderDigestVec) -> Self {
18504 self.proof = v;
18505 self
18506 }
18507 pub fn filtered_blocks(mut self, v: FilteredBlockVec) -> Self {
18508 self.filtered_blocks = v;
18509 self
18510 }
18511 pub fn missing_tx_hashes(mut self, v: Byte32Vec) -> Self {
18512 self.missing_tx_hashes = v;
18513 self
18514 }
18515}
18516impl molecule::prelude::Builder for SendTransactionsProofBuilder {
18517 type Entity = SendTransactionsProof;
18518 const NAME: &'static str = "SendTransactionsProofBuilder";
18519 fn expected_length(&self) -> usize {
18520 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
18521 + self.last_header.as_slice().len()
18522 + self.proof.as_slice().len()
18523 + self.filtered_blocks.as_slice().len()
18524 + self.missing_tx_hashes.as_slice().len()
18525 }
18526 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
18527 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
18528 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
18529 offsets.push(total_size);
18530 total_size += self.last_header.as_slice().len();
18531 offsets.push(total_size);
18532 total_size += self.proof.as_slice().len();
18533 offsets.push(total_size);
18534 total_size += self.filtered_blocks.as_slice().len();
18535 offsets.push(total_size);
18536 total_size += self.missing_tx_hashes.as_slice().len();
18537 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
18538 for offset in offsets.into_iter() {
18539 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
18540 }
18541 writer.write_all(self.last_header.as_slice())?;
18542 writer.write_all(self.proof.as_slice())?;
18543 writer.write_all(self.filtered_blocks.as_slice())?;
18544 writer.write_all(self.missing_tx_hashes.as_slice())?;
18545 Ok(())
18546 }
18547 fn build(&self) -> Self::Entity {
18548 let mut inner = Vec::with_capacity(self.expected_length());
18549 self.write(&mut inner)
18550 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
18551 SendTransactionsProof::new_unchecked(inner.into())
18552 }
18553}
18554#[derive(Clone)]
18555pub struct SendTransactionsProofV1(molecule::bytes::Bytes);
18556impl ::core::fmt::LowerHex for SendTransactionsProofV1 {
18557 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18558 use molecule::hex_string;
18559 if f.alternate() {
18560 write!(f, "0x")?;
18561 }
18562 write!(f, "{}", hex_string(self.as_slice()))
18563 }
18564}
18565impl ::core::fmt::Debug for SendTransactionsProofV1 {
18566 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18567 write!(f, "{}({:#x})", Self::NAME, self)
18568 }
18569}
18570impl ::core::fmt::Display for SendTransactionsProofV1 {
18571 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18572 write!(f, "{} {{ ", Self::NAME)?;
18573 write!(f, "{}: {}", "last_header", self.last_header())?;
18574 write!(f, ", {}: {}", "proof", self.proof())?;
18575 write!(f, ", {}: {}", "filtered_blocks", self.filtered_blocks())?;
18576 write!(f, ", {}: {}", "missing_tx_hashes", self.missing_tx_hashes())?;
18577 write!(
18578 f,
18579 ", {}: {}",
18580 "blocks_uncles_hash",
18581 self.blocks_uncles_hash()
18582 )?;
18583 write!(f, ", {}: {}", "blocks_extension", self.blocks_extension())?;
18584 let extra_count = self.count_extra_fields();
18585 if extra_count != 0 {
18586 write!(f, ", .. ({} fields)", extra_count)?;
18587 }
18588 write!(f, " }}")
18589 }
18590}
18591impl ::core::default::Default for SendTransactionsProofV1 {
18592 fn default() -> Self {
18593 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
18594 SendTransactionsProofV1::new_unchecked(v)
18595 }
18596}
18597impl SendTransactionsProofV1 {
18598 const DEFAULT_VALUE: [u8; 428] = [
18599 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,
18600 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,
18601 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18602 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18603 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18604 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18605 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18606 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18607 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18608 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18609 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18610 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18611 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18612 0, 0, 0, 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,
18613 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
18614 ];
18615 pub const FIELD_COUNT: usize = 6;
18616 pub fn total_size(&self) -> usize {
18617 molecule::unpack_number(self.as_slice()) as usize
18618 }
18619 pub fn field_count(&self) -> usize {
18620 if self.total_size() == molecule::NUMBER_SIZE {
18621 0
18622 } else {
18623 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18624 }
18625 }
18626 pub fn count_extra_fields(&self) -> usize {
18627 self.field_count() - Self::FIELD_COUNT
18628 }
18629 pub fn has_extra_fields(&self) -> bool {
18630 Self::FIELD_COUNT != self.field_count()
18631 }
18632 pub fn last_header(&self) -> VerifiableHeader {
18633 let slice = self.as_slice();
18634 let start = molecule::unpack_number(&slice[4..]) as usize;
18635 let end = molecule::unpack_number(&slice[8..]) as usize;
18636 VerifiableHeader::new_unchecked(self.0.slice(start..end))
18637 }
18638 pub fn proof(&self) -> HeaderDigestVec {
18639 let slice = self.as_slice();
18640 let start = molecule::unpack_number(&slice[8..]) as usize;
18641 let end = molecule::unpack_number(&slice[12..]) as usize;
18642 HeaderDigestVec::new_unchecked(self.0.slice(start..end))
18643 }
18644 pub fn filtered_blocks(&self) -> FilteredBlockVec {
18645 let slice = self.as_slice();
18646 let start = molecule::unpack_number(&slice[12..]) as usize;
18647 let end = molecule::unpack_number(&slice[16..]) as usize;
18648 FilteredBlockVec::new_unchecked(self.0.slice(start..end))
18649 }
18650 pub fn missing_tx_hashes(&self) -> Byte32Vec {
18651 let slice = self.as_slice();
18652 let start = molecule::unpack_number(&slice[16..]) as usize;
18653 let end = molecule::unpack_number(&slice[20..]) as usize;
18654 Byte32Vec::new_unchecked(self.0.slice(start..end))
18655 }
18656 pub fn blocks_uncles_hash(&self) -> Byte32Vec {
18657 let slice = self.as_slice();
18658 let start = molecule::unpack_number(&slice[20..]) as usize;
18659 let end = molecule::unpack_number(&slice[24..]) as usize;
18660 Byte32Vec::new_unchecked(self.0.slice(start..end))
18661 }
18662 pub fn blocks_extension(&self) -> BytesOptVec {
18663 let slice = self.as_slice();
18664 let start = molecule::unpack_number(&slice[24..]) as usize;
18665 if self.has_extra_fields() {
18666 let end = molecule::unpack_number(&slice[28..]) as usize;
18667 BytesOptVec::new_unchecked(self.0.slice(start..end))
18668 } else {
18669 BytesOptVec::new_unchecked(self.0.slice(start..))
18670 }
18671 }
18672 pub fn as_reader<'r>(&'r self) -> SendTransactionsProofV1Reader<'r> {
18673 SendTransactionsProofV1Reader::new_unchecked(self.as_slice())
18674 }
18675}
18676impl molecule::prelude::Entity for SendTransactionsProofV1 {
18677 type Builder = SendTransactionsProofV1Builder;
18678 const NAME: &'static str = "SendTransactionsProofV1";
18679 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
18680 SendTransactionsProofV1(data)
18681 }
18682 fn as_bytes(&self) -> molecule::bytes::Bytes {
18683 self.0.clone()
18684 }
18685 fn as_slice(&self) -> &[u8] {
18686 &self.0[..]
18687 }
18688 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18689 SendTransactionsProofV1Reader::from_slice(slice).map(|reader| reader.to_entity())
18690 }
18691 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
18692 SendTransactionsProofV1Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
18693 }
18694 fn new_builder() -> Self::Builder {
18695 ::core::default::Default::default()
18696 }
18697 fn as_builder(self) -> Self::Builder {
18698 Self::new_builder()
18699 .last_header(self.last_header())
18700 .proof(self.proof())
18701 .filtered_blocks(self.filtered_blocks())
18702 .missing_tx_hashes(self.missing_tx_hashes())
18703 .blocks_uncles_hash(self.blocks_uncles_hash())
18704 .blocks_extension(self.blocks_extension())
18705 }
18706}
18707#[derive(Clone, Copy)]
18708pub struct SendTransactionsProofV1Reader<'r>(&'r [u8]);
18709impl<'r> ::core::fmt::LowerHex for SendTransactionsProofV1Reader<'r> {
18710 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18711 use molecule::hex_string;
18712 if f.alternate() {
18713 write!(f, "0x")?;
18714 }
18715 write!(f, "{}", hex_string(self.as_slice()))
18716 }
18717}
18718impl<'r> ::core::fmt::Debug for SendTransactionsProofV1Reader<'r> {
18719 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18720 write!(f, "{}({:#x})", Self::NAME, self)
18721 }
18722}
18723impl<'r> ::core::fmt::Display for SendTransactionsProofV1Reader<'r> {
18724 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18725 write!(f, "{} {{ ", Self::NAME)?;
18726 write!(f, "{}: {}", "last_header", self.last_header())?;
18727 write!(f, ", {}: {}", "proof", self.proof())?;
18728 write!(f, ", {}: {}", "filtered_blocks", self.filtered_blocks())?;
18729 write!(f, ", {}: {}", "missing_tx_hashes", self.missing_tx_hashes())?;
18730 write!(
18731 f,
18732 ", {}: {}",
18733 "blocks_uncles_hash",
18734 self.blocks_uncles_hash()
18735 )?;
18736 write!(f, ", {}: {}", "blocks_extension", self.blocks_extension())?;
18737 let extra_count = self.count_extra_fields();
18738 if extra_count != 0 {
18739 write!(f, ", .. ({} fields)", extra_count)?;
18740 }
18741 write!(f, " }}")
18742 }
18743}
18744impl<'r> SendTransactionsProofV1Reader<'r> {
18745 pub const FIELD_COUNT: usize = 6;
18746 pub fn total_size(&self) -> usize {
18747 molecule::unpack_number(self.as_slice()) as usize
18748 }
18749 pub fn field_count(&self) -> usize {
18750 if self.total_size() == molecule::NUMBER_SIZE {
18751 0
18752 } else {
18753 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18754 }
18755 }
18756 pub fn count_extra_fields(&self) -> usize {
18757 self.field_count() - Self::FIELD_COUNT
18758 }
18759 pub fn has_extra_fields(&self) -> bool {
18760 Self::FIELD_COUNT != self.field_count()
18761 }
18762 pub fn last_header(&self) -> VerifiableHeaderReader<'r> {
18763 let slice = self.as_slice();
18764 let start = molecule::unpack_number(&slice[4..]) as usize;
18765 let end = molecule::unpack_number(&slice[8..]) as usize;
18766 VerifiableHeaderReader::new_unchecked(&self.as_slice()[start..end])
18767 }
18768 pub fn proof(&self) -> HeaderDigestVecReader<'r> {
18769 let slice = self.as_slice();
18770 let start = molecule::unpack_number(&slice[8..]) as usize;
18771 let end = molecule::unpack_number(&slice[12..]) as usize;
18772 HeaderDigestVecReader::new_unchecked(&self.as_slice()[start..end])
18773 }
18774 pub fn filtered_blocks(&self) -> FilteredBlockVecReader<'r> {
18775 let slice = self.as_slice();
18776 let start = molecule::unpack_number(&slice[12..]) as usize;
18777 let end = molecule::unpack_number(&slice[16..]) as usize;
18778 FilteredBlockVecReader::new_unchecked(&self.as_slice()[start..end])
18779 }
18780 pub fn missing_tx_hashes(&self) -> Byte32VecReader<'r> {
18781 let slice = self.as_slice();
18782 let start = molecule::unpack_number(&slice[16..]) as usize;
18783 let end = molecule::unpack_number(&slice[20..]) as usize;
18784 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
18785 }
18786 pub fn blocks_uncles_hash(&self) -> Byte32VecReader<'r> {
18787 let slice = self.as_slice();
18788 let start = molecule::unpack_number(&slice[20..]) as usize;
18789 let end = molecule::unpack_number(&slice[24..]) as usize;
18790 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
18791 }
18792 pub fn blocks_extension(&self) -> BytesOptVecReader<'r> {
18793 let slice = self.as_slice();
18794 let start = molecule::unpack_number(&slice[24..]) as usize;
18795 if self.has_extra_fields() {
18796 let end = molecule::unpack_number(&slice[28..]) as usize;
18797 BytesOptVecReader::new_unchecked(&self.as_slice()[start..end])
18798 } else {
18799 BytesOptVecReader::new_unchecked(&self.as_slice()[start..])
18800 }
18801 }
18802}
18803impl<'r> molecule::prelude::Reader<'r> for SendTransactionsProofV1Reader<'r> {
18804 type Entity = SendTransactionsProofV1;
18805 const NAME: &'static str = "SendTransactionsProofV1Reader";
18806 fn to_entity(&self) -> Self::Entity {
18807 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
18808 }
18809 fn new_unchecked(slice: &'r [u8]) -> Self {
18810 SendTransactionsProofV1Reader(slice)
18811 }
18812 fn as_slice(&self) -> &'r [u8] {
18813 self.0
18814 }
18815 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
18816 use molecule::verification_error as ve;
18817 let slice_len = slice.len();
18818 if slice_len < molecule::NUMBER_SIZE {
18819 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
18820 }
18821 let total_size = molecule::unpack_number(slice) as usize;
18822 if slice_len != total_size {
18823 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
18824 }
18825 if slice_len < molecule::NUMBER_SIZE * 2 {
18826 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
18827 }
18828 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
18829 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
18830 return ve!(Self, OffsetsNotMatch);
18831 }
18832 if slice_len < offset_first {
18833 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
18834 }
18835 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
18836 if field_count < Self::FIELD_COUNT {
18837 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18838 } else if !compatible && field_count > Self::FIELD_COUNT {
18839 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
18840 };
18841 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
18842 .chunks_exact(molecule::NUMBER_SIZE)
18843 .map(|x| molecule::unpack_number(x) as usize)
18844 .collect();
18845 offsets.push(total_size);
18846 if offsets.windows(2).any(|i| i[0] > i[1]) {
18847 return ve!(Self, OffsetsNotMatch);
18848 }
18849 VerifiableHeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
18850 HeaderDigestVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
18851 FilteredBlockVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
18852 Byte32VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
18853 Byte32VecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
18854 BytesOptVecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
18855 Ok(())
18856 }
18857}
18858#[derive(Debug, Default)]
18859pub struct SendTransactionsProofV1Builder {
18860 pub(crate) last_header: VerifiableHeader,
18861 pub(crate) proof: HeaderDigestVec,
18862 pub(crate) filtered_blocks: FilteredBlockVec,
18863 pub(crate) missing_tx_hashes: Byte32Vec,
18864 pub(crate) blocks_uncles_hash: Byte32Vec,
18865 pub(crate) blocks_extension: BytesOptVec,
18866}
18867impl SendTransactionsProofV1Builder {
18868 pub const FIELD_COUNT: usize = 6;
18869 pub fn last_header(mut self, v: VerifiableHeader) -> Self {
18870 self.last_header = v;
18871 self
18872 }
18873 pub fn proof(mut self, v: HeaderDigestVec) -> Self {
18874 self.proof = v;
18875 self
18876 }
18877 pub fn filtered_blocks(mut self, v: FilteredBlockVec) -> Self {
18878 self.filtered_blocks = v;
18879 self
18880 }
18881 pub fn missing_tx_hashes(mut self, v: Byte32Vec) -> Self {
18882 self.missing_tx_hashes = v;
18883 self
18884 }
18885 pub fn blocks_uncles_hash(mut self, v: Byte32Vec) -> Self {
18886 self.blocks_uncles_hash = v;
18887 self
18888 }
18889 pub fn blocks_extension(mut self, v: BytesOptVec) -> Self {
18890 self.blocks_extension = v;
18891 self
18892 }
18893}
18894impl molecule::prelude::Builder for SendTransactionsProofV1Builder {
18895 type Entity = SendTransactionsProofV1;
18896 const NAME: &'static str = "SendTransactionsProofV1Builder";
18897 fn expected_length(&self) -> usize {
18898 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
18899 + self.last_header.as_slice().len()
18900 + self.proof.as_slice().len()
18901 + self.filtered_blocks.as_slice().len()
18902 + self.missing_tx_hashes.as_slice().len()
18903 + self.blocks_uncles_hash.as_slice().len()
18904 + self.blocks_extension.as_slice().len()
18905 }
18906 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
18907 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
18908 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
18909 offsets.push(total_size);
18910 total_size += self.last_header.as_slice().len();
18911 offsets.push(total_size);
18912 total_size += self.proof.as_slice().len();
18913 offsets.push(total_size);
18914 total_size += self.filtered_blocks.as_slice().len();
18915 offsets.push(total_size);
18916 total_size += self.missing_tx_hashes.as_slice().len();
18917 offsets.push(total_size);
18918 total_size += self.blocks_uncles_hash.as_slice().len();
18919 offsets.push(total_size);
18920 total_size += self.blocks_extension.as_slice().len();
18921 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
18922 for offset in offsets.into_iter() {
18923 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
18924 }
18925 writer.write_all(self.last_header.as_slice())?;
18926 writer.write_all(self.proof.as_slice())?;
18927 writer.write_all(self.filtered_blocks.as_slice())?;
18928 writer.write_all(self.missing_tx_hashes.as_slice())?;
18929 writer.write_all(self.blocks_uncles_hash.as_slice())?;
18930 writer.write_all(self.blocks_extension.as_slice())?;
18931 Ok(())
18932 }
18933 fn build(&self) -> Self::Entity {
18934 let mut inner = Vec::with_capacity(self.expected_length());
18935 self.write(&mut inner)
18936 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
18937 SendTransactionsProofV1::new_unchecked(inner.into())
18938 }
18939}
18940#[derive(Clone)]
18941pub struct Time(molecule::bytes::Bytes);
18942impl ::core::fmt::LowerHex for Time {
18943 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18944 use molecule::hex_string;
18945 if f.alternate() {
18946 write!(f, "0x")?;
18947 }
18948 write!(f, "{}", hex_string(self.as_slice()))
18949 }
18950}
18951impl ::core::fmt::Debug for Time {
18952 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18953 write!(f, "{}({:#x})", Self::NAME, self)
18954 }
18955}
18956impl ::core::fmt::Display for Time {
18957 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18958 write!(f, "{} {{ ", Self::NAME)?;
18959 write!(f, "{}: {}", "timestamp", self.timestamp())?;
18960 let extra_count = self.count_extra_fields();
18961 if extra_count != 0 {
18962 write!(f, ", .. ({} fields)", extra_count)?;
18963 }
18964 write!(f, " }}")
18965 }
18966}
18967impl ::core::default::Default for Time {
18968 fn default() -> Self {
18969 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
18970 Time::new_unchecked(v)
18971 }
18972}
18973impl Time {
18974 const DEFAULT_VALUE: [u8; 16] = [16, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
18975 pub const FIELD_COUNT: usize = 1;
18976 pub fn total_size(&self) -> usize {
18977 molecule::unpack_number(self.as_slice()) as usize
18978 }
18979 pub fn field_count(&self) -> usize {
18980 if self.total_size() == molecule::NUMBER_SIZE {
18981 0
18982 } else {
18983 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
18984 }
18985 }
18986 pub fn count_extra_fields(&self) -> usize {
18987 self.field_count() - Self::FIELD_COUNT
18988 }
18989 pub fn has_extra_fields(&self) -> bool {
18990 Self::FIELD_COUNT != self.field_count()
18991 }
18992 pub fn timestamp(&self) -> Uint64 {
18993 let slice = self.as_slice();
18994 let start = molecule::unpack_number(&slice[4..]) as usize;
18995 if self.has_extra_fields() {
18996 let end = molecule::unpack_number(&slice[8..]) as usize;
18997 Uint64::new_unchecked(self.0.slice(start..end))
18998 } else {
18999 Uint64::new_unchecked(self.0.slice(start..))
19000 }
19001 }
19002 pub fn as_reader<'r>(&'r self) -> TimeReader<'r> {
19003 TimeReader::new_unchecked(self.as_slice())
19004 }
19005}
19006impl molecule::prelude::Entity for Time {
19007 type Builder = TimeBuilder;
19008 const NAME: &'static str = "Time";
19009 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
19010 Time(data)
19011 }
19012 fn as_bytes(&self) -> molecule::bytes::Bytes {
19013 self.0.clone()
19014 }
19015 fn as_slice(&self) -> &[u8] {
19016 &self.0[..]
19017 }
19018 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19019 TimeReader::from_slice(slice).map(|reader| reader.to_entity())
19020 }
19021 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19022 TimeReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
19023 }
19024 fn new_builder() -> Self::Builder {
19025 ::core::default::Default::default()
19026 }
19027 fn as_builder(self) -> Self::Builder {
19028 Self::new_builder().timestamp(self.timestamp())
19029 }
19030}
19031#[derive(Clone, Copy)]
19032pub struct TimeReader<'r>(&'r [u8]);
19033impl<'r> ::core::fmt::LowerHex for TimeReader<'r> {
19034 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19035 use molecule::hex_string;
19036 if f.alternate() {
19037 write!(f, "0x")?;
19038 }
19039 write!(f, "{}", hex_string(self.as_slice()))
19040 }
19041}
19042impl<'r> ::core::fmt::Debug for TimeReader<'r> {
19043 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19044 write!(f, "{}({:#x})", Self::NAME, self)
19045 }
19046}
19047impl<'r> ::core::fmt::Display for TimeReader<'r> {
19048 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19049 write!(f, "{} {{ ", Self::NAME)?;
19050 write!(f, "{}: {}", "timestamp", self.timestamp())?;
19051 let extra_count = self.count_extra_fields();
19052 if extra_count != 0 {
19053 write!(f, ", .. ({} fields)", extra_count)?;
19054 }
19055 write!(f, " }}")
19056 }
19057}
19058impl<'r> TimeReader<'r> {
19059 pub const FIELD_COUNT: usize = 1;
19060 pub fn total_size(&self) -> usize {
19061 molecule::unpack_number(self.as_slice()) as usize
19062 }
19063 pub fn field_count(&self) -> usize {
19064 if self.total_size() == molecule::NUMBER_SIZE {
19065 0
19066 } else {
19067 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19068 }
19069 }
19070 pub fn count_extra_fields(&self) -> usize {
19071 self.field_count() - Self::FIELD_COUNT
19072 }
19073 pub fn has_extra_fields(&self) -> bool {
19074 Self::FIELD_COUNT != self.field_count()
19075 }
19076 pub fn timestamp(&self) -> Uint64Reader<'r> {
19077 let slice = self.as_slice();
19078 let start = molecule::unpack_number(&slice[4..]) as usize;
19079 if self.has_extra_fields() {
19080 let end = molecule::unpack_number(&slice[8..]) as usize;
19081 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
19082 } else {
19083 Uint64Reader::new_unchecked(&self.as_slice()[start..])
19084 }
19085 }
19086}
19087impl<'r> molecule::prelude::Reader<'r> for TimeReader<'r> {
19088 type Entity = Time;
19089 const NAME: &'static str = "TimeReader";
19090 fn to_entity(&self) -> Self::Entity {
19091 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
19092 }
19093 fn new_unchecked(slice: &'r [u8]) -> Self {
19094 TimeReader(slice)
19095 }
19096 fn as_slice(&self) -> &'r [u8] {
19097 self.0
19098 }
19099 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
19100 use molecule::verification_error as ve;
19101 let slice_len = slice.len();
19102 if slice_len < molecule::NUMBER_SIZE {
19103 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
19104 }
19105 let total_size = molecule::unpack_number(slice) as usize;
19106 if slice_len != total_size {
19107 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
19108 }
19109 if slice_len < molecule::NUMBER_SIZE * 2 {
19110 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
19111 }
19112 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
19113 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
19114 return ve!(Self, OffsetsNotMatch);
19115 }
19116 if slice_len < offset_first {
19117 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
19118 }
19119 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
19120 if field_count < Self::FIELD_COUNT {
19121 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19122 } else if !compatible && field_count > Self::FIELD_COUNT {
19123 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19124 };
19125 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
19126 .chunks_exact(molecule::NUMBER_SIZE)
19127 .map(|x| molecule::unpack_number(x) as usize)
19128 .collect();
19129 offsets.push(total_size);
19130 if offsets.windows(2).any(|i| i[0] > i[1]) {
19131 return ve!(Self, OffsetsNotMatch);
19132 }
19133 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
19134 Ok(())
19135 }
19136}
19137#[derive(Debug, Default)]
19138pub struct TimeBuilder {
19139 pub(crate) timestamp: Uint64,
19140}
19141impl TimeBuilder {
19142 pub const FIELD_COUNT: usize = 1;
19143 pub fn timestamp(mut self, v: Uint64) -> Self {
19144 self.timestamp = v;
19145 self
19146 }
19147}
19148impl molecule::prelude::Builder for TimeBuilder {
19149 type Entity = Time;
19150 const NAME: &'static str = "TimeBuilder";
19151 fn expected_length(&self) -> usize {
19152 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.timestamp.as_slice().len()
19153 }
19154 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
19155 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
19156 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
19157 offsets.push(total_size);
19158 total_size += self.timestamp.as_slice().len();
19159 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
19160 for offset in offsets.into_iter() {
19161 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
19162 }
19163 writer.write_all(self.timestamp.as_slice())?;
19164 Ok(())
19165 }
19166 fn build(&self) -> Self::Entity {
19167 let mut inner = Vec::with_capacity(self.expected_length());
19168 self.write(&mut inner)
19169 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
19170 Time::new_unchecked(inner.into())
19171 }
19172}
19173#[derive(Clone)]
19174pub struct RawAlert(molecule::bytes::Bytes);
19175impl ::core::fmt::LowerHex for RawAlert {
19176 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19177 use molecule::hex_string;
19178 if f.alternate() {
19179 write!(f, "0x")?;
19180 }
19181 write!(f, "{}", hex_string(self.as_slice()))
19182 }
19183}
19184impl ::core::fmt::Debug for RawAlert {
19185 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19186 write!(f, "{}({:#x})", Self::NAME, self)
19187 }
19188}
19189impl ::core::fmt::Display for RawAlert {
19190 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19191 write!(f, "{} {{ ", Self::NAME)?;
19192 write!(f, "{}: {}", "notice_until", self.notice_until())?;
19193 write!(f, ", {}: {}", "id", self.id())?;
19194 write!(f, ", {}: {}", "cancel", self.cancel())?;
19195 write!(f, ", {}: {}", "priority", self.priority())?;
19196 write!(f, ", {}: {}", "message", self.message())?;
19197 write!(f, ", {}: {}", "min_version", self.min_version())?;
19198 write!(f, ", {}: {}", "max_version", self.max_version())?;
19199 let extra_count = self.count_extra_fields();
19200 if extra_count != 0 {
19201 write!(f, ", .. ({} fields)", extra_count)?;
19202 }
19203 write!(f, " }}")
19204 }
19205}
19206impl ::core::default::Default for RawAlert {
19207 fn default() -> Self {
19208 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
19209 RawAlert::new_unchecked(v)
19210 }
19211}
19212impl RawAlert {
19213 const DEFAULT_VALUE: [u8; 56] = [
19214 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,
19215 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,
19216 ];
19217 pub const FIELD_COUNT: usize = 7;
19218 pub fn total_size(&self) -> usize {
19219 molecule::unpack_number(self.as_slice()) as usize
19220 }
19221 pub fn field_count(&self) -> usize {
19222 if self.total_size() == molecule::NUMBER_SIZE {
19223 0
19224 } else {
19225 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19226 }
19227 }
19228 pub fn count_extra_fields(&self) -> usize {
19229 self.field_count() - Self::FIELD_COUNT
19230 }
19231 pub fn has_extra_fields(&self) -> bool {
19232 Self::FIELD_COUNT != self.field_count()
19233 }
19234 pub fn notice_until(&self) -> Uint64 {
19235 let slice = self.as_slice();
19236 let start = molecule::unpack_number(&slice[4..]) as usize;
19237 let end = molecule::unpack_number(&slice[8..]) as usize;
19238 Uint64::new_unchecked(self.0.slice(start..end))
19239 }
19240 pub fn id(&self) -> Uint32 {
19241 let slice = self.as_slice();
19242 let start = molecule::unpack_number(&slice[8..]) as usize;
19243 let end = molecule::unpack_number(&slice[12..]) as usize;
19244 Uint32::new_unchecked(self.0.slice(start..end))
19245 }
19246 pub fn cancel(&self) -> Uint32 {
19247 let slice = self.as_slice();
19248 let start = molecule::unpack_number(&slice[12..]) as usize;
19249 let end = molecule::unpack_number(&slice[16..]) as usize;
19250 Uint32::new_unchecked(self.0.slice(start..end))
19251 }
19252 pub fn priority(&self) -> Uint32 {
19253 let slice = self.as_slice();
19254 let start = molecule::unpack_number(&slice[16..]) as usize;
19255 let end = molecule::unpack_number(&slice[20..]) as usize;
19256 Uint32::new_unchecked(self.0.slice(start..end))
19257 }
19258 pub fn message(&self) -> Bytes {
19259 let slice = self.as_slice();
19260 let start = molecule::unpack_number(&slice[20..]) as usize;
19261 let end = molecule::unpack_number(&slice[24..]) as usize;
19262 Bytes::new_unchecked(self.0.slice(start..end))
19263 }
19264 pub fn min_version(&self) -> BytesOpt {
19265 let slice = self.as_slice();
19266 let start = molecule::unpack_number(&slice[24..]) as usize;
19267 let end = molecule::unpack_number(&slice[28..]) as usize;
19268 BytesOpt::new_unchecked(self.0.slice(start..end))
19269 }
19270 pub fn max_version(&self) -> BytesOpt {
19271 let slice = self.as_slice();
19272 let start = molecule::unpack_number(&slice[28..]) as usize;
19273 if self.has_extra_fields() {
19274 let end = molecule::unpack_number(&slice[32..]) as usize;
19275 BytesOpt::new_unchecked(self.0.slice(start..end))
19276 } else {
19277 BytesOpt::new_unchecked(self.0.slice(start..))
19278 }
19279 }
19280 pub fn as_reader<'r>(&'r self) -> RawAlertReader<'r> {
19281 RawAlertReader::new_unchecked(self.as_slice())
19282 }
19283}
19284impl molecule::prelude::Entity for RawAlert {
19285 type Builder = RawAlertBuilder;
19286 const NAME: &'static str = "RawAlert";
19287 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
19288 RawAlert(data)
19289 }
19290 fn as_bytes(&self) -> molecule::bytes::Bytes {
19291 self.0.clone()
19292 }
19293 fn as_slice(&self) -> &[u8] {
19294 &self.0[..]
19295 }
19296 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19297 RawAlertReader::from_slice(slice).map(|reader| reader.to_entity())
19298 }
19299 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19300 RawAlertReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
19301 }
19302 fn new_builder() -> Self::Builder {
19303 ::core::default::Default::default()
19304 }
19305 fn as_builder(self) -> Self::Builder {
19306 Self::new_builder()
19307 .notice_until(self.notice_until())
19308 .id(self.id())
19309 .cancel(self.cancel())
19310 .priority(self.priority())
19311 .message(self.message())
19312 .min_version(self.min_version())
19313 .max_version(self.max_version())
19314 }
19315}
19316#[derive(Clone, Copy)]
19317pub struct RawAlertReader<'r>(&'r [u8]);
19318impl<'r> ::core::fmt::LowerHex for RawAlertReader<'r> {
19319 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19320 use molecule::hex_string;
19321 if f.alternate() {
19322 write!(f, "0x")?;
19323 }
19324 write!(f, "{}", hex_string(self.as_slice()))
19325 }
19326}
19327impl<'r> ::core::fmt::Debug for RawAlertReader<'r> {
19328 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19329 write!(f, "{}({:#x})", Self::NAME, self)
19330 }
19331}
19332impl<'r> ::core::fmt::Display for RawAlertReader<'r> {
19333 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19334 write!(f, "{} {{ ", Self::NAME)?;
19335 write!(f, "{}: {}", "notice_until", self.notice_until())?;
19336 write!(f, ", {}: {}", "id", self.id())?;
19337 write!(f, ", {}: {}", "cancel", self.cancel())?;
19338 write!(f, ", {}: {}", "priority", self.priority())?;
19339 write!(f, ", {}: {}", "message", self.message())?;
19340 write!(f, ", {}: {}", "min_version", self.min_version())?;
19341 write!(f, ", {}: {}", "max_version", self.max_version())?;
19342 let extra_count = self.count_extra_fields();
19343 if extra_count != 0 {
19344 write!(f, ", .. ({} fields)", extra_count)?;
19345 }
19346 write!(f, " }}")
19347 }
19348}
19349impl<'r> RawAlertReader<'r> {
19350 pub const FIELD_COUNT: usize = 7;
19351 pub fn total_size(&self) -> usize {
19352 molecule::unpack_number(self.as_slice()) as usize
19353 }
19354 pub fn field_count(&self) -> usize {
19355 if self.total_size() == molecule::NUMBER_SIZE {
19356 0
19357 } else {
19358 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19359 }
19360 }
19361 pub fn count_extra_fields(&self) -> usize {
19362 self.field_count() - Self::FIELD_COUNT
19363 }
19364 pub fn has_extra_fields(&self) -> bool {
19365 Self::FIELD_COUNT != self.field_count()
19366 }
19367 pub fn notice_until(&self) -> Uint64Reader<'r> {
19368 let slice = self.as_slice();
19369 let start = molecule::unpack_number(&slice[4..]) as usize;
19370 let end = molecule::unpack_number(&slice[8..]) as usize;
19371 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
19372 }
19373 pub fn id(&self) -> Uint32Reader<'r> {
19374 let slice = self.as_slice();
19375 let start = molecule::unpack_number(&slice[8..]) as usize;
19376 let end = molecule::unpack_number(&slice[12..]) as usize;
19377 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
19378 }
19379 pub fn cancel(&self) -> Uint32Reader<'r> {
19380 let slice = self.as_slice();
19381 let start = molecule::unpack_number(&slice[12..]) as usize;
19382 let end = molecule::unpack_number(&slice[16..]) as usize;
19383 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
19384 }
19385 pub fn priority(&self) -> Uint32Reader<'r> {
19386 let slice = self.as_slice();
19387 let start = molecule::unpack_number(&slice[16..]) as usize;
19388 let end = molecule::unpack_number(&slice[20..]) as usize;
19389 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
19390 }
19391 pub fn message(&self) -> BytesReader<'r> {
19392 let slice = self.as_slice();
19393 let start = molecule::unpack_number(&slice[20..]) as usize;
19394 let end = molecule::unpack_number(&slice[24..]) as usize;
19395 BytesReader::new_unchecked(&self.as_slice()[start..end])
19396 }
19397 pub fn min_version(&self) -> BytesOptReader<'r> {
19398 let slice = self.as_slice();
19399 let start = molecule::unpack_number(&slice[24..]) as usize;
19400 let end = molecule::unpack_number(&slice[28..]) as usize;
19401 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
19402 }
19403 pub fn max_version(&self) -> BytesOptReader<'r> {
19404 let slice = self.as_slice();
19405 let start = molecule::unpack_number(&slice[28..]) as usize;
19406 if self.has_extra_fields() {
19407 let end = molecule::unpack_number(&slice[32..]) as usize;
19408 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
19409 } else {
19410 BytesOptReader::new_unchecked(&self.as_slice()[start..])
19411 }
19412 }
19413}
19414impl<'r> molecule::prelude::Reader<'r> for RawAlertReader<'r> {
19415 type Entity = RawAlert;
19416 const NAME: &'static str = "RawAlertReader";
19417 fn to_entity(&self) -> Self::Entity {
19418 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
19419 }
19420 fn new_unchecked(slice: &'r [u8]) -> Self {
19421 RawAlertReader(slice)
19422 }
19423 fn as_slice(&self) -> &'r [u8] {
19424 self.0
19425 }
19426 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
19427 use molecule::verification_error as ve;
19428 let slice_len = slice.len();
19429 if slice_len < molecule::NUMBER_SIZE {
19430 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
19431 }
19432 let total_size = molecule::unpack_number(slice) as usize;
19433 if slice_len != total_size {
19434 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
19435 }
19436 if slice_len < molecule::NUMBER_SIZE * 2 {
19437 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
19438 }
19439 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
19440 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
19441 return ve!(Self, OffsetsNotMatch);
19442 }
19443 if slice_len < offset_first {
19444 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
19445 }
19446 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
19447 if field_count < Self::FIELD_COUNT {
19448 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19449 } else if !compatible && field_count > Self::FIELD_COUNT {
19450 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19451 };
19452 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
19453 .chunks_exact(molecule::NUMBER_SIZE)
19454 .map(|x| molecule::unpack_number(x) as usize)
19455 .collect();
19456 offsets.push(total_size);
19457 if offsets.windows(2).any(|i| i[0] > i[1]) {
19458 return ve!(Self, OffsetsNotMatch);
19459 }
19460 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
19461 Uint32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
19462 Uint32Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
19463 Uint32Reader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
19464 BytesReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
19465 BytesOptReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
19466 BytesOptReader::verify(&slice[offsets[6]..offsets[7]], compatible)?;
19467 Ok(())
19468 }
19469}
19470#[derive(Debug, Default)]
19471pub struct RawAlertBuilder {
19472 pub(crate) notice_until: Uint64,
19473 pub(crate) id: Uint32,
19474 pub(crate) cancel: Uint32,
19475 pub(crate) priority: Uint32,
19476 pub(crate) message: Bytes,
19477 pub(crate) min_version: BytesOpt,
19478 pub(crate) max_version: BytesOpt,
19479}
19480impl RawAlertBuilder {
19481 pub const FIELD_COUNT: usize = 7;
19482 pub fn notice_until(mut self, v: Uint64) -> Self {
19483 self.notice_until = v;
19484 self
19485 }
19486 pub fn id(mut self, v: Uint32) -> Self {
19487 self.id = v;
19488 self
19489 }
19490 pub fn cancel(mut self, v: Uint32) -> Self {
19491 self.cancel = v;
19492 self
19493 }
19494 pub fn priority(mut self, v: Uint32) -> Self {
19495 self.priority = v;
19496 self
19497 }
19498 pub fn message(mut self, v: Bytes) -> Self {
19499 self.message = v;
19500 self
19501 }
19502 pub fn min_version(mut self, v: BytesOpt) -> Self {
19503 self.min_version = v;
19504 self
19505 }
19506 pub fn max_version(mut self, v: BytesOpt) -> Self {
19507 self.max_version = v;
19508 self
19509 }
19510}
19511impl molecule::prelude::Builder for RawAlertBuilder {
19512 type Entity = RawAlert;
19513 const NAME: &'static str = "RawAlertBuilder";
19514 fn expected_length(&self) -> usize {
19515 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
19516 + self.notice_until.as_slice().len()
19517 + self.id.as_slice().len()
19518 + self.cancel.as_slice().len()
19519 + self.priority.as_slice().len()
19520 + self.message.as_slice().len()
19521 + self.min_version.as_slice().len()
19522 + self.max_version.as_slice().len()
19523 }
19524 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
19525 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
19526 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
19527 offsets.push(total_size);
19528 total_size += self.notice_until.as_slice().len();
19529 offsets.push(total_size);
19530 total_size += self.id.as_slice().len();
19531 offsets.push(total_size);
19532 total_size += self.cancel.as_slice().len();
19533 offsets.push(total_size);
19534 total_size += self.priority.as_slice().len();
19535 offsets.push(total_size);
19536 total_size += self.message.as_slice().len();
19537 offsets.push(total_size);
19538 total_size += self.min_version.as_slice().len();
19539 offsets.push(total_size);
19540 total_size += self.max_version.as_slice().len();
19541 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
19542 for offset in offsets.into_iter() {
19543 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
19544 }
19545 writer.write_all(self.notice_until.as_slice())?;
19546 writer.write_all(self.id.as_slice())?;
19547 writer.write_all(self.cancel.as_slice())?;
19548 writer.write_all(self.priority.as_slice())?;
19549 writer.write_all(self.message.as_slice())?;
19550 writer.write_all(self.min_version.as_slice())?;
19551 writer.write_all(self.max_version.as_slice())?;
19552 Ok(())
19553 }
19554 fn build(&self) -> Self::Entity {
19555 let mut inner = Vec::with_capacity(self.expected_length());
19556 self.write(&mut inner)
19557 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
19558 RawAlert::new_unchecked(inner.into())
19559 }
19560}
19561#[derive(Clone)]
19562pub struct Alert(molecule::bytes::Bytes);
19563impl ::core::fmt::LowerHex for Alert {
19564 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19565 use molecule::hex_string;
19566 if f.alternate() {
19567 write!(f, "0x")?;
19568 }
19569 write!(f, "{}", hex_string(self.as_slice()))
19570 }
19571}
19572impl ::core::fmt::Debug for Alert {
19573 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19574 write!(f, "{}({:#x})", Self::NAME, self)
19575 }
19576}
19577impl ::core::fmt::Display for Alert {
19578 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19579 write!(f, "{} {{ ", Self::NAME)?;
19580 write!(f, "{}: {}", "raw", self.raw())?;
19581 write!(f, ", {}: {}", "signatures", self.signatures())?;
19582 let extra_count = self.count_extra_fields();
19583 if extra_count != 0 {
19584 write!(f, ", .. ({} fields)", extra_count)?;
19585 }
19586 write!(f, " }}")
19587 }
19588}
19589impl ::core::default::Default for Alert {
19590 fn default() -> Self {
19591 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
19592 Alert::new_unchecked(v)
19593 }
19594}
19595impl Alert {
19596 const DEFAULT_VALUE: [u8; 72] = [
19597 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,
19598 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,
19599 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
19600 ];
19601 pub const FIELD_COUNT: usize = 2;
19602 pub fn total_size(&self) -> usize {
19603 molecule::unpack_number(self.as_slice()) as usize
19604 }
19605 pub fn field_count(&self) -> usize {
19606 if self.total_size() == molecule::NUMBER_SIZE {
19607 0
19608 } else {
19609 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19610 }
19611 }
19612 pub fn count_extra_fields(&self) -> usize {
19613 self.field_count() - Self::FIELD_COUNT
19614 }
19615 pub fn has_extra_fields(&self) -> bool {
19616 Self::FIELD_COUNT != self.field_count()
19617 }
19618 pub fn raw(&self) -> RawAlert {
19619 let slice = self.as_slice();
19620 let start = molecule::unpack_number(&slice[4..]) as usize;
19621 let end = molecule::unpack_number(&slice[8..]) as usize;
19622 RawAlert::new_unchecked(self.0.slice(start..end))
19623 }
19624 pub fn signatures(&self) -> BytesVec {
19625 let slice = self.as_slice();
19626 let start = molecule::unpack_number(&slice[8..]) as usize;
19627 if self.has_extra_fields() {
19628 let end = molecule::unpack_number(&slice[12..]) as usize;
19629 BytesVec::new_unchecked(self.0.slice(start..end))
19630 } else {
19631 BytesVec::new_unchecked(self.0.slice(start..))
19632 }
19633 }
19634 pub fn as_reader<'r>(&'r self) -> AlertReader<'r> {
19635 AlertReader::new_unchecked(self.as_slice())
19636 }
19637}
19638impl molecule::prelude::Entity for Alert {
19639 type Builder = AlertBuilder;
19640 const NAME: &'static str = "Alert";
19641 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
19642 Alert(data)
19643 }
19644 fn as_bytes(&self) -> molecule::bytes::Bytes {
19645 self.0.clone()
19646 }
19647 fn as_slice(&self) -> &[u8] {
19648 &self.0[..]
19649 }
19650 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19651 AlertReader::from_slice(slice).map(|reader| reader.to_entity())
19652 }
19653 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19654 AlertReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
19655 }
19656 fn new_builder() -> Self::Builder {
19657 ::core::default::Default::default()
19658 }
19659 fn as_builder(self) -> Self::Builder {
19660 Self::new_builder()
19661 .raw(self.raw())
19662 .signatures(self.signatures())
19663 }
19664}
19665#[derive(Clone, Copy)]
19666pub struct AlertReader<'r>(&'r [u8]);
19667impl<'r> ::core::fmt::LowerHex for AlertReader<'r> {
19668 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19669 use molecule::hex_string;
19670 if f.alternate() {
19671 write!(f, "0x")?;
19672 }
19673 write!(f, "{}", hex_string(self.as_slice()))
19674 }
19675}
19676impl<'r> ::core::fmt::Debug for AlertReader<'r> {
19677 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19678 write!(f, "{}({:#x})", Self::NAME, self)
19679 }
19680}
19681impl<'r> ::core::fmt::Display for AlertReader<'r> {
19682 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19683 write!(f, "{} {{ ", Self::NAME)?;
19684 write!(f, "{}: {}", "raw", self.raw())?;
19685 write!(f, ", {}: {}", "signatures", self.signatures())?;
19686 let extra_count = self.count_extra_fields();
19687 if extra_count != 0 {
19688 write!(f, ", .. ({} fields)", extra_count)?;
19689 }
19690 write!(f, " }}")
19691 }
19692}
19693impl<'r> AlertReader<'r> {
19694 pub const FIELD_COUNT: usize = 2;
19695 pub fn total_size(&self) -> usize {
19696 molecule::unpack_number(self.as_slice()) as usize
19697 }
19698 pub fn field_count(&self) -> usize {
19699 if self.total_size() == molecule::NUMBER_SIZE {
19700 0
19701 } else {
19702 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19703 }
19704 }
19705 pub fn count_extra_fields(&self) -> usize {
19706 self.field_count() - Self::FIELD_COUNT
19707 }
19708 pub fn has_extra_fields(&self) -> bool {
19709 Self::FIELD_COUNT != self.field_count()
19710 }
19711 pub fn raw(&self) -> RawAlertReader<'r> {
19712 let slice = self.as_slice();
19713 let start = molecule::unpack_number(&slice[4..]) as usize;
19714 let end = molecule::unpack_number(&slice[8..]) as usize;
19715 RawAlertReader::new_unchecked(&self.as_slice()[start..end])
19716 }
19717 pub fn signatures(&self) -> BytesVecReader<'r> {
19718 let slice = self.as_slice();
19719 let start = molecule::unpack_number(&slice[8..]) as usize;
19720 if self.has_extra_fields() {
19721 let end = molecule::unpack_number(&slice[12..]) as usize;
19722 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
19723 } else {
19724 BytesVecReader::new_unchecked(&self.as_slice()[start..])
19725 }
19726 }
19727}
19728impl<'r> molecule::prelude::Reader<'r> for AlertReader<'r> {
19729 type Entity = Alert;
19730 const NAME: &'static str = "AlertReader";
19731 fn to_entity(&self) -> Self::Entity {
19732 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
19733 }
19734 fn new_unchecked(slice: &'r [u8]) -> Self {
19735 AlertReader(slice)
19736 }
19737 fn as_slice(&self) -> &'r [u8] {
19738 self.0
19739 }
19740 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
19741 use molecule::verification_error as ve;
19742 let slice_len = slice.len();
19743 if slice_len < molecule::NUMBER_SIZE {
19744 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
19745 }
19746 let total_size = molecule::unpack_number(slice) as usize;
19747 if slice_len != total_size {
19748 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
19749 }
19750 if slice_len < molecule::NUMBER_SIZE * 2 {
19751 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
19752 }
19753 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
19754 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
19755 return ve!(Self, OffsetsNotMatch);
19756 }
19757 if slice_len < offset_first {
19758 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
19759 }
19760 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
19761 if field_count < Self::FIELD_COUNT {
19762 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19763 } else if !compatible && field_count > Self::FIELD_COUNT {
19764 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
19765 };
19766 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
19767 .chunks_exact(molecule::NUMBER_SIZE)
19768 .map(|x| molecule::unpack_number(x) as usize)
19769 .collect();
19770 offsets.push(total_size);
19771 if offsets.windows(2).any(|i| i[0] > i[1]) {
19772 return ve!(Self, OffsetsNotMatch);
19773 }
19774 RawAlertReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
19775 BytesVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
19776 Ok(())
19777 }
19778}
19779#[derive(Debug, Default)]
19780pub struct AlertBuilder {
19781 pub(crate) raw: RawAlert,
19782 pub(crate) signatures: BytesVec,
19783}
19784impl AlertBuilder {
19785 pub const FIELD_COUNT: usize = 2;
19786 pub fn raw(mut self, v: RawAlert) -> Self {
19787 self.raw = v;
19788 self
19789 }
19790 pub fn signatures(mut self, v: BytesVec) -> Self {
19791 self.signatures = v;
19792 self
19793 }
19794}
19795impl molecule::prelude::Builder for AlertBuilder {
19796 type Entity = Alert;
19797 const NAME: &'static str = "AlertBuilder";
19798 fn expected_length(&self) -> usize {
19799 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
19800 + self.raw.as_slice().len()
19801 + self.signatures.as_slice().len()
19802 }
19803 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
19804 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
19805 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
19806 offsets.push(total_size);
19807 total_size += self.raw.as_slice().len();
19808 offsets.push(total_size);
19809 total_size += self.signatures.as_slice().len();
19810 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
19811 for offset in offsets.into_iter() {
19812 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
19813 }
19814 writer.write_all(self.raw.as_slice())?;
19815 writer.write_all(self.signatures.as_slice())?;
19816 Ok(())
19817 }
19818 fn build(&self) -> Self::Entity {
19819 let mut inner = Vec::with_capacity(self.expected_length());
19820 self.write(&mut inner)
19821 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
19822 Alert::new_unchecked(inner.into())
19823 }
19824}
19825#[derive(Clone)]
19826pub struct Identify(molecule::bytes::Bytes);
19827impl ::core::fmt::LowerHex for Identify {
19828 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19829 use molecule::hex_string;
19830 if f.alternate() {
19831 write!(f, "0x")?;
19832 }
19833 write!(f, "{}", hex_string(self.as_slice()))
19834 }
19835}
19836impl ::core::fmt::Debug for Identify {
19837 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19838 write!(f, "{}({:#x})", Self::NAME, self)
19839 }
19840}
19841impl ::core::fmt::Display for Identify {
19842 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19843 write!(f, "{} {{ ", Self::NAME)?;
19844 write!(f, "{}: {}", "flag", self.flag())?;
19845 write!(f, ", {}: {}", "name", self.name())?;
19846 write!(f, ", {}: {}", "client_version", self.client_version())?;
19847 let extra_count = self.count_extra_fields();
19848 if extra_count != 0 {
19849 write!(f, ", .. ({} fields)", extra_count)?;
19850 }
19851 write!(f, " }}")
19852 }
19853}
19854impl ::core::default::Default for Identify {
19855 fn default() -> Self {
19856 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
19857 Identify::new_unchecked(v)
19858 }
19859}
19860impl Identify {
19861 const DEFAULT_VALUE: [u8; 32] = [
19862 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,
19863 0, 0, 0,
19864 ];
19865 pub const FIELD_COUNT: usize = 3;
19866 pub fn total_size(&self) -> usize {
19867 molecule::unpack_number(self.as_slice()) as usize
19868 }
19869 pub fn field_count(&self) -> usize {
19870 if self.total_size() == molecule::NUMBER_SIZE {
19871 0
19872 } else {
19873 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19874 }
19875 }
19876 pub fn count_extra_fields(&self) -> usize {
19877 self.field_count() - Self::FIELD_COUNT
19878 }
19879 pub fn has_extra_fields(&self) -> bool {
19880 Self::FIELD_COUNT != self.field_count()
19881 }
19882 pub fn flag(&self) -> Uint64 {
19883 let slice = self.as_slice();
19884 let start = molecule::unpack_number(&slice[4..]) as usize;
19885 let end = molecule::unpack_number(&slice[8..]) as usize;
19886 Uint64::new_unchecked(self.0.slice(start..end))
19887 }
19888 pub fn name(&self) -> Bytes {
19889 let slice = self.as_slice();
19890 let start = molecule::unpack_number(&slice[8..]) as usize;
19891 let end = molecule::unpack_number(&slice[12..]) as usize;
19892 Bytes::new_unchecked(self.0.slice(start..end))
19893 }
19894 pub fn client_version(&self) -> Bytes {
19895 let slice = self.as_slice();
19896 let start = molecule::unpack_number(&slice[12..]) as usize;
19897 if self.has_extra_fields() {
19898 let end = molecule::unpack_number(&slice[16..]) as usize;
19899 Bytes::new_unchecked(self.0.slice(start..end))
19900 } else {
19901 Bytes::new_unchecked(self.0.slice(start..))
19902 }
19903 }
19904 pub fn as_reader<'r>(&'r self) -> IdentifyReader<'r> {
19905 IdentifyReader::new_unchecked(self.as_slice())
19906 }
19907}
19908impl molecule::prelude::Entity for Identify {
19909 type Builder = IdentifyBuilder;
19910 const NAME: &'static str = "Identify";
19911 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
19912 Identify(data)
19913 }
19914 fn as_bytes(&self) -> molecule::bytes::Bytes {
19915 self.0.clone()
19916 }
19917 fn as_slice(&self) -> &[u8] {
19918 &self.0[..]
19919 }
19920 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19921 IdentifyReader::from_slice(slice).map(|reader| reader.to_entity())
19922 }
19923 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
19924 IdentifyReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
19925 }
19926 fn new_builder() -> Self::Builder {
19927 ::core::default::Default::default()
19928 }
19929 fn as_builder(self) -> Self::Builder {
19930 Self::new_builder()
19931 .flag(self.flag())
19932 .name(self.name())
19933 .client_version(self.client_version())
19934 }
19935}
19936#[derive(Clone, Copy)]
19937pub struct IdentifyReader<'r>(&'r [u8]);
19938impl<'r> ::core::fmt::LowerHex for IdentifyReader<'r> {
19939 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19940 use molecule::hex_string;
19941 if f.alternate() {
19942 write!(f, "0x")?;
19943 }
19944 write!(f, "{}", hex_string(self.as_slice()))
19945 }
19946}
19947impl<'r> ::core::fmt::Debug for IdentifyReader<'r> {
19948 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19949 write!(f, "{}({:#x})", Self::NAME, self)
19950 }
19951}
19952impl<'r> ::core::fmt::Display for IdentifyReader<'r> {
19953 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19954 write!(f, "{} {{ ", Self::NAME)?;
19955 write!(f, "{}: {}", "flag", self.flag())?;
19956 write!(f, ", {}: {}", "name", self.name())?;
19957 write!(f, ", {}: {}", "client_version", self.client_version())?;
19958 let extra_count = self.count_extra_fields();
19959 if extra_count != 0 {
19960 write!(f, ", .. ({} fields)", extra_count)?;
19961 }
19962 write!(f, " }}")
19963 }
19964}
19965impl<'r> IdentifyReader<'r> {
19966 pub const FIELD_COUNT: usize = 3;
19967 pub fn total_size(&self) -> usize {
19968 molecule::unpack_number(self.as_slice()) as usize
19969 }
19970 pub fn field_count(&self) -> usize {
19971 if self.total_size() == molecule::NUMBER_SIZE {
19972 0
19973 } else {
19974 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
19975 }
19976 }
19977 pub fn count_extra_fields(&self) -> usize {
19978 self.field_count() - Self::FIELD_COUNT
19979 }
19980 pub fn has_extra_fields(&self) -> bool {
19981 Self::FIELD_COUNT != self.field_count()
19982 }
19983 pub fn flag(&self) -> Uint64Reader<'r> {
19984 let slice = self.as_slice();
19985 let start = molecule::unpack_number(&slice[4..]) as usize;
19986 let end = molecule::unpack_number(&slice[8..]) as usize;
19987 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
19988 }
19989 pub fn name(&self) -> BytesReader<'r> {
19990 let slice = self.as_slice();
19991 let start = molecule::unpack_number(&slice[8..]) as usize;
19992 let end = molecule::unpack_number(&slice[12..]) as usize;
19993 BytesReader::new_unchecked(&self.as_slice()[start..end])
19994 }
19995 pub fn client_version(&self) -> BytesReader<'r> {
19996 let slice = self.as_slice();
19997 let start = molecule::unpack_number(&slice[12..]) as usize;
19998 if self.has_extra_fields() {
19999 let end = molecule::unpack_number(&slice[16..]) as usize;
20000 BytesReader::new_unchecked(&self.as_slice()[start..end])
20001 } else {
20002 BytesReader::new_unchecked(&self.as_slice()[start..])
20003 }
20004 }
20005}
20006impl<'r> molecule::prelude::Reader<'r> for IdentifyReader<'r> {
20007 type Entity = Identify;
20008 const NAME: &'static str = "IdentifyReader";
20009 fn to_entity(&self) -> Self::Entity {
20010 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
20011 }
20012 fn new_unchecked(slice: &'r [u8]) -> Self {
20013 IdentifyReader(slice)
20014 }
20015 fn as_slice(&self) -> &'r [u8] {
20016 self.0
20017 }
20018 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
20019 use molecule::verification_error as ve;
20020 let slice_len = slice.len();
20021 if slice_len < molecule::NUMBER_SIZE {
20022 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
20023 }
20024 let total_size = molecule::unpack_number(slice) as usize;
20025 if slice_len != total_size {
20026 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
20027 }
20028 if slice_len < molecule::NUMBER_SIZE * 2 {
20029 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
20030 }
20031 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
20032 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
20033 return ve!(Self, OffsetsNotMatch);
20034 }
20035 if slice_len < offset_first {
20036 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
20037 }
20038 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
20039 if field_count < Self::FIELD_COUNT {
20040 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
20041 } else if !compatible && field_count > Self::FIELD_COUNT {
20042 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
20043 };
20044 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
20045 .chunks_exact(molecule::NUMBER_SIZE)
20046 .map(|x| molecule::unpack_number(x) as usize)
20047 .collect();
20048 offsets.push(total_size);
20049 if offsets.windows(2).any(|i| i[0] > i[1]) {
20050 return ve!(Self, OffsetsNotMatch);
20051 }
20052 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
20053 BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
20054 BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
20055 Ok(())
20056 }
20057}
20058#[derive(Debug, Default)]
20059pub struct IdentifyBuilder {
20060 pub(crate) flag: Uint64,
20061 pub(crate) name: Bytes,
20062 pub(crate) client_version: Bytes,
20063}
20064impl IdentifyBuilder {
20065 pub const FIELD_COUNT: usize = 3;
20066 pub fn flag(mut self, v: Uint64) -> Self {
20067 self.flag = v;
20068 self
20069 }
20070 pub fn name(mut self, v: Bytes) -> Self {
20071 self.name = v;
20072 self
20073 }
20074 pub fn client_version(mut self, v: Bytes) -> Self {
20075 self.client_version = v;
20076 self
20077 }
20078}
20079impl molecule::prelude::Builder for IdentifyBuilder {
20080 type Entity = Identify;
20081 const NAME: &'static str = "IdentifyBuilder";
20082 fn expected_length(&self) -> usize {
20083 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
20084 + self.flag.as_slice().len()
20085 + self.name.as_slice().len()
20086 + self.client_version.as_slice().len()
20087 }
20088 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
20089 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
20090 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
20091 offsets.push(total_size);
20092 total_size += self.flag.as_slice().len();
20093 offsets.push(total_size);
20094 total_size += self.name.as_slice().len();
20095 offsets.push(total_size);
20096 total_size += self.client_version.as_slice().len();
20097 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
20098 for offset in offsets.into_iter() {
20099 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
20100 }
20101 writer.write_all(self.flag.as_slice())?;
20102 writer.write_all(self.name.as_slice())?;
20103 writer.write_all(self.client_version.as_slice())?;
20104 Ok(())
20105 }
20106 fn build(&self) -> Self::Entity {
20107 let mut inner = Vec::with_capacity(self.expected_length());
20108 self.write(&mut inner)
20109 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
20110 Identify::new_unchecked(inner.into())
20111 }
20112}