1#[cfg(all(
6 test,
7 any(
8 not(miri),
9 not(any(
10 feature = "ed25519",
11 feature = "parallel",
12 feature = "scale-codec",
13 feature = "serde"
14 )),
15 all(
16 feature = "ed25519",
17 feature = "parallel",
18 feature = "scale-codec",
19 feature = "serde"
20 ),
21 ),
22))]
23mod tests;
24
25use crate::block::body::owned::{
26 GenericOwnedBlockBody, OwnedBeaconChainBody, OwnedBeaconChainBodyError, OwnedBlockBody,
27 OwnedIntermediateShardBody, OwnedIntermediateShardBodyError, OwnedLeafShardBlockBodyBuilder,
28 OwnedLeafShardBody, OwnedLeafShardBodyError, WritableBodyTransaction,
29};
30use crate::block::body::{BlockBody, IntermediateShardBlockInfo, LeafShardBlockInfo};
31use crate::block::header::owned::{
32 GenericOwnedBlockHeader, OwnedBeaconChainHeader, OwnedBeaconChainHeaderUnsealed,
33 OwnedBlockHeader, OwnedIntermediateShardHeader, OwnedIntermediateShardHeaderUnsealed,
34 OwnedLeafShardHeader, OwnedLeafShardHeaderUnsealed,
35};
36use crate::block::header::{
37 BlockHeader, BlockHeaderBeaconChainInfo, BlockHeaderConsensusInfo, BlockHeaderConsistencyError,
38 BlockHeaderPrefix, BlockHeaderResult, BlockHeaderSeal, OwnedBlockHeaderConsensusParameters,
39};
40use crate::block::{
41 BeaconChainBlock, Block, GenericBlock, IntermediateShardBlock, LeafShardBlock,
42 align_to_and_ensure_zero_padding,
43};
44use crate::hashes::Blake3Hash;
45use crate::pot::PotCheckpoints;
46use crate::segments::{LocalSegmentIndex, SegmentRoot};
47use crate::shard::{RealShardKind, ShardIndex};
48use ab_aligned_buffer::SharedAlignedBuffer;
49use ab_aligned_buffer::alignment::Align16;
50use ab_io_type::alignment::Alignment;
51use core::any::Any;
52use core::fmt;
53use core::iter::TrustedLen;
54use derive_more::From;
55
56pub impl(self) trait GenericOwnedBlock:
61 Clone + fmt::Debug + Send + Sync + Into<OwnedBlock> + Any + 'static
62{
63 const SHARD_KIND: RealShardKind;
65
66 type Header: GenericOwnedBlockHeader;
68 type Body: GenericOwnedBlockBody;
70 type Block<'a>: GenericBlock<'a>
72 where
73 Self: 'a;
74
75 fn split(self) -> (Self::Header, Self::Body);
77
78 fn header(&self) -> &Self::Header;
80
81 fn body(&self) -> &Self::Body;
83
84 fn from_parts(
88 header: Self::Header,
89 body: Self::Body,
90 ) -> Result<Self, (Self::Header, Self::Body)>;
91
92 fn from_buffers(
96 header: SharedAlignedBuffer,
97 body: SharedAlignedBuffer,
98 ) -> Result<Self, (SharedAlignedBuffer, SharedAlignedBuffer)>;
99
100 fn from_buffer(buffer: SharedAlignedBuffer) -> Result<Self, SharedAlignedBuffer>;
107
108 fn block(&self) -> Self::Block<'_>;
110}
111
112fn split_block_buffer<Header, HeaderFromBufferStart>(
120 buffer: &SharedAlignedBuffer,
121 max_header_size: u32,
122 header_from_buffer_start: HeaderFromBufferStart,
123) -> Option<(Header, SharedAlignedBuffer)>
124where
125 Header: GenericOwnedBlockHeader,
126 HeaderFromBufferStart: FnOnce(SharedAlignedBuffer) -> Result<Header, SharedAlignedBuffer>,
127{
128 let header_bytes = buffer
129 .get_slice(..buffer.len().min(max_header_size))
130 .expect("Not longer than the buffer; qed");
131 let header =
132 header_from_buffer_start(SharedAlignedBuffer::from_bytes(header_bytes.as_slice())).ok()?;
133
134 let bytes = buffer.as_slice();
135 let after_header = bytes
136 .get(header.buffer().as_slice().len()..)
137 .expect("Header is a part of the buffer; qed");
138 let body_bytes = align_to_and_ensure_zero_padding(after_header, Alignment::MAX)?;
139 if body_bytes.is_empty() {
140 return None;
141 }
142
143 let body_offset = u32::try_from(bytes.len() - body_bytes.len())
144 .expect("Body is a part of the buffer, whose length is `u32`; qed");
145 let body = buffer
146 .get_slice(body_offset..)
147 .expect("Body is a part of the buffer; qed")
148 .try_into_start_align::<Align16>()
149 .expect("Body follows padding to `Alignment::MAX` in memory; qed");
150
151 Some((header, body))
152}
153
154#[derive(Debug, Clone)]
162pub struct OwnedBeaconChainBlock {
163 pub mut(self) header: OwnedBeaconChainHeader,
165 pub mut(self) body: OwnedBeaconChainBody,
167}
168
169impl GenericOwnedBlock for OwnedBeaconChainBlock {
170 const SHARD_KIND: RealShardKind = RealShardKind::BeaconChain;
171
172 type Header = OwnedBeaconChainHeader;
173 type Body = OwnedBeaconChainBody;
174 type Block<'a> = BeaconChainBlock<'a>;
175
176 #[inline(always)]
177 fn split(self) -> (Self::Header, Self::Body) {
178 (self.header, self.body)
179 }
180
181 #[inline(always)]
182 fn header(&self) -> &Self::Header {
183 &self.header
184 }
185
186 #[inline(always)]
187 fn body(&self) -> &Self::Body {
188 &self.body
189 }
190
191 #[inline(always)]
192 fn from_parts(
193 header: Self::Header,
194 body: Self::Body,
195 ) -> Result<Self, (Self::Header, Self::Body)> {
196 Self::from_parts(header, body)
197 }
198
199 #[inline(always)]
200 fn from_buffers(
201 header: SharedAlignedBuffer,
202 body: SharedAlignedBuffer,
203 ) -> Result<Self, (SharedAlignedBuffer, SharedAlignedBuffer)> {
204 Self::from_buffers(header, body)
205 }
206
207 #[inline(always)]
208 fn from_buffer(buffer: SharedAlignedBuffer) -> Result<Self, SharedAlignedBuffer> {
209 Self::from_buffer(buffer)
210 }
211
212 #[inline(always)]
213 fn block(&self) -> Self::Block<'_> {
214 self.block()
215 }
216}
217
218impl OwnedBeaconChainBlock {
219 pub fn init<'a, OS, ISB>(
221 own_segments: OS,
222 intermediate_shard_blocks: ISB,
223 pot_checkpoints: &[PotCheckpoints],
224 ) -> Result<OwnedBeaconChainBlockBuilder, OwnedBeaconChainBodyError>
225 where
226 OS: TrustedLen<Item = (LocalSegmentIndex, SegmentRoot)>,
227 ISB: TrustedLen<Item = IntermediateShardBlockInfo<'a>> + 'a,
228 {
229 Ok(OwnedBeaconChainBlockBuilder {
230 body: OwnedBeaconChainBody::new(
231 own_segments,
232 intermediate_shard_blocks,
233 pot_checkpoints,
234 )?,
235 })
236 }
237
238 #[inline]
242 pub fn from_parts(
243 header: OwnedBeaconChainHeader,
244 body: OwnedBeaconChainBody,
245 ) -> Result<Self, (OwnedBeaconChainHeader, OwnedBeaconChainBody)> {
246 let block = Self { header, body };
247
248 if !block.block().is_internally_consistent() {
249 return Err((block.header, block.body));
250 }
251
252 Ok(block)
253 }
254
255 #[inline]
259 pub fn from_buffers(
260 header: SharedAlignedBuffer,
261 body: SharedAlignedBuffer,
262 ) -> Result<Self, (SharedAlignedBuffer, SharedAlignedBuffer)> {
263 let header = match OwnedBeaconChainHeader::from_buffer(header) {
264 Ok(header) => header,
265 Err(header) => return Err((header, body)),
266 };
267 let body = match OwnedBeaconChainBody::from_buffer(body) {
268 Ok(body) => body,
269 Err(body) => return Err((header.buffer().clone(), body)),
270 };
271
272 Self::from_parts(header, body)
273 .map_err(|(header, body)| (header.buffer().clone(), body.buffer().clone()))
274 }
275
276 #[inline]
279 pub fn from_buffer(buffer: SharedAlignedBuffer) -> Result<Self, SharedAlignedBuffer> {
280 let Some((header, body)) = split_block_buffer(
281 &buffer,
282 OwnedBeaconChainHeader::MAX_ALLOCATION,
283 OwnedBeaconChainHeader::from_buffer_start,
284 ) else {
285 return Err(buffer);
286 };
287 let Ok(body) = OwnedBeaconChainBody::from_buffer(body) else {
288 return Err(buffer);
289 };
290
291 Self::from_parts(header, body).map_err(|_parts| buffer)
292 }
293
294 #[inline(always)]
297 pub(super) fn from_block(block: BeaconChainBlock<'_>) -> Self {
298 Self {
299 header: block.header.to_owned(),
300 body: block.body.to_owned(),
301 }
302 }
303
304 pub fn block(&self) -> BeaconChainBlock<'_> {
306 BeaconChainBlock {
307 header: self.header.header().clone(),
308 body: *self.body.body(),
309 }
310 }
311}
312
313#[derive(Debug, Clone)]
315pub struct OwnedBeaconChainBlockBuilder {
316 body: OwnedBeaconChainBody,
317}
318
319impl OwnedBeaconChainBlockBuilder {
320 pub fn with_header(
325 self,
326 prefix: &BlockHeaderPrefix,
327 state_root: Blake3Hash,
328 consensus_info: &BlockHeaderConsensusInfo,
329 consensus_parameters: &OwnedBlockHeaderConsensusParameters,
330 ) -> Result<OwnedBeaconChainBlockUnsealed, BlockHeaderConsistencyError> {
331 let body = self.body;
332 let intermediate_shard_blocks_root = body.body().intermediate_shard_blocks().headers_root();
335 let header = OwnedBeaconChainHeader::from_parts(
336 prefix,
337 &BlockHeaderResult {
338 body_root: body
339 .body()
340 .root_with_intermediate_shard_blocks_root(&intermediate_shard_blocks_root),
341 state_root,
342 },
343 consensus_info,
344 &intermediate_shard_blocks_root,
345 consensus_parameters,
346 )?;
347
348 Ok(OwnedBeaconChainBlockUnsealed { body, header })
349 }
350}
351
352#[derive(Debug, Clone)]
354pub struct OwnedBeaconChainBlockUnsealed {
355 body: OwnedBeaconChainBody,
356 header: OwnedBeaconChainHeaderUnsealed,
357}
358
359impl OwnedBeaconChainBlockUnsealed {
360 #[inline(always)]
362 pub fn pre_seal_hash(&self) -> Blake3Hash {
363 self.header.pre_seal_hash()
364 }
365
366 pub fn with_seal(
370 self,
371 seal: BlockHeaderSeal<'_>,
372 ) -> Result<OwnedBeaconChainBlock, BlockHeaderConsistencyError> {
373 let header = self.header.with_seal(seal)?;
374
375 Ok(OwnedBeaconChainBlock {
376 header,
377 body: self.body,
378 })
379 }
380}
381
382#[derive(Debug, Clone)]
390pub struct OwnedIntermediateShardBlock {
391 pub mut(self) header: OwnedIntermediateShardHeader,
393 pub mut(self) body: OwnedIntermediateShardBody,
395}
396
397impl GenericOwnedBlock for OwnedIntermediateShardBlock {
398 const SHARD_KIND: RealShardKind = RealShardKind::IntermediateShard;
399
400 type Header = OwnedIntermediateShardHeader;
401 type Body = OwnedIntermediateShardBody;
402 type Block<'a> = IntermediateShardBlock<'a>;
403
404 #[inline(always)]
405 fn split(self) -> (Self::Header, Self::Body) {
406 (self.header, self.body)
407 }
408
409 #[inline(always)]
410 fn header(&self) -> &Self::Header {
411 &self.header
412 }
413
414 #[inline(always)]
415 fn body(&self) -> &Self::Body {
416 &self.body
417 }
418
419 #[inline(always)]
420 fn from_parts(
421 header: Self::Header,
422 body: Self::Body,
423 ) -> Result<Self, (Self::Header, Self::Body)> {
424 Self::from_parts(header, body)
425 }
426
427 #[inline(always)]
428 fn from_buffers(
429 header: SharedAlignedBuffer,
430 body: SharedAlignedBuffer,
431 ) -> Result<Self, (SharedAlignedBuffer, SharedAlignedBuffer)> {
432 Self::from_buffers(header, body)
433 }
434
435 #[inline(always)]
436 fn from_buffer(buffer: SharedAlignedBuffer) -> Result<Self, SharedAlignedBuffer> {
437 Self::from_buffer(buffer)
438 }
439
440 #[inline(always)]
441 fn block(&self) -> Self::Block<'_> {
442 self.block()
443 }
444}
445
446impl OwnedIntermediateShardBlock {
447 pub fn init<'a, OS, LSB>(
449 own_segments: OS,
450 leaf_shard_blocks: LSB,
451 ) -> Result<OwnedIntermediateShardBlockBuilder, OwnedIntermediateShardBodyError>
452 where
453 OS: TrustedLen<Item = (LocalSegmentIndex, SegmentRoot)>,
454 LSB: TrustedLen<Item = LeafShardBlockInfo<'a>> + 'a,
455 {
456 Ok(OwnedIntermediateShardBlockBuilder {
457 body: OwnedIntermediateShardBody::new(own_segments, leaf_shard_blocks)?,
458 })
459 }
460
461 #[inline]
465 pub fn from_parts(
466 header: OwnedIntermediateShardHeader,
467 body: OwnedIntermediateShardBody,
468 ) -> Result<Self, (OwnedIntermediateShardHeader, OwnedIntermediateShardBody)> {
469 let block = Self { header, body };
470
471 if !block.block().is_internally_consistent() {
472 return Err((block.header, block.body));
473 }
474
475 Ok(block)
476 }
477
478 #[inline]
482 pub fn from_buffers(
483 header: SharedAlignedBuffer,
484 body: SharedAlignedBuffer,
485 ) -> Result<Self, (SharedAlignedBuffer, SharedAlignedBuffer)> {
486 let header = match OwnedIntermediateShardHeader::from_buffer(header) {
487 Ok(header) => header,
488 Err(header) => return Err((header, body)),
489 };
490 let body = match OwnedIntermediateShardBody::from_buffer(body) {
491 Ok(body) => body,
492 Err(body) => return Err((header.buffer().clone(), body)),
493 };
494
495 Self::from_parts(header, body)
496 .map_err(|(header, body)| (header.buffer().clone(), body.buffer().clone()))
497 }
498
499 #[inline]
502 pub fn from_buffer(buffer: SharedAlignedBuffer) -> Result<Self, SharedAlignedBuffer> {
503 let Some((header, body)) = split_block_buffer(
504 &buffer,
505 OwnedIntermediateShardHeader::MAX_ALLOCATION,
506 OwnedIntermediateShardHeader::from_buffer_start,
507 ) else {
508 return Err(buffer);
509 };
510 let Ok(body) = OwnedIntermediateShardBody::from_buffer(body) else {
511 return Err(buffer);
512 };
513
514 Self::from_parts(header, body).map_err(|_parts| buffer)
515 }
516
517 #[inline(always)]
520 pub(super) fn from_block(block: IntermediateShardBlock<'_>) -> Self {
521 Self {
522 header: block.header.to_owned(),
523 body: block.body.to_owned(),
524 }
525 }
526
527 pub fn block(&self) -> IntermediateShardBlock<'_> {
529 IntermediateShardBlock {
530 header: self.header.header().clone(),
531 body: *self.body.body(),
532 }
533 }
534}
535
536#[derive(Debug, Copy, Clone, Eq, PartialEq, thiserror::Error)]
538pub enum OwnedIntermediateShardBlockBuilderError {
539 #[error("Invalid header parts: {error}")]
541 InvalidHeaderParts {
542 #[from]
544 error: BlockHeaderConsistencyError,
545 },
546 #[error("Leaf shard block of shard {leaf_shard_index} is not a child of shard {shard_index}")]
548 LeafShardBlockOfAnotherShard {
549 shard_index: ShardIndex,
551 leaf_shard_index: ShardIndex,
553 },
554}
555
556#[derive(Debug, Clone)]
558pub struct OwnedIntermediateShardBlockBuilder {
559 body: OwnedIntermediateShardBody,
560}
561
562impl OwnedIntermediateShardBlockBuilder {
563 pub fn with_header(
568 self,
569 prefix: &BlockHeaderPrefix,
570 state_root: Blake3Hash,
571 consensus_info: &BlockHeaderConsensusInfo,
572 beacon_chain_info: &BlockHeaderBeaconChainInfo,
573 ) -> Result<OwnedIntermediateShardBlockUnsealed, OwnedIntermediateShardBlockBuilderError> {
574 let body = self.body;
575 let leaf_shard_blocks_root = body.body().leaf_shard_blocks().headers_root();
578 let header = OwnedIntermediateShardHeader::from_parts(
579 prefix,
580 &BlockHeaderResult {
581 body_root: body
582 .body()
583 .root_with_leaf_shard_blocks_root(&leaf_shard_blocks_root),
584 state_root,
585 },
586 consensus_info,
587 beacon_chain_info,
588 &leaf_shard_blocks_root,
589 )?;
590
591 if let Some(leaf_shard_index) =
592 IntermediateShardBlock::find_leaf_shard_block_of_another_shard(
593 body.body().leaf_shard_blocks(),
594 prefix.shard_index,
595 )
596 {
597 return Err(
598 OwnedIntermediateShardBlockBuilderError::LeafShardBlockOfAnotherShard {
599 shard_index: prefix.shard_index,
600 leaf_shard_index,
601 },
602 );
603 }
604
605 Ok(OwnedIntermediateShardBlockUnsealed { body, header })
606 }
607}
608
609#[derive(Debug, Clone)]
611pub struct OwnedIntermediateShardBlockUnsealed {
612 body: OwnedIntermediateShardBody,
613 header: OwnedIntermediateShardHeaderUnsealed,
614}
615
616impl OwnedIntermediateShardBlockUnsealed {
617 #[inline(always)]
619 pub fn pre_seal_hash(&self) -> Blake3Hash {
620 self.header.pre_seal_hash()
621 }
622
623 pub fn with_seal(
627 self,
628 seal: BlockHeaderSeal<'_>,
629 ) -> Result<OwnedIntermediateShardBlock, BlockHeaderConsistencyError> {
630 let header = self.header.with_seal(seal)?;
631
632 Ok(OwnedIntermediateShardBlock {
633 header,
634 body: self.body,
635 })
636 }
637}
638
639#[derive(Debug, Clone)]
647pub struct OwnedLeafShardBlock {
648 pub mut(self) header: OwnedLeafShardHeader,
650 pub mut(self) body: OwnedLeafShardBody,
652}
653
654impl GenericOwnedBlock for OwnedLeafShardBlock {
655 const SHARD_KIND: RealShardKind = RealShardKind::LeafShard;
656
657 type Header = OwnedLeafShardHeader;
658 type Body = OwnedLeafShardBody;
659 type Block<'a> = LeafShardBlock<'a>;
660
661 #[inline(always)]
662 fn split(self) -> (Self::Header, Self::Body) {
663 (self.header, self.body)
664 }
665
666 #[inline(always)]
667 fn header(&self) -> &Self::Header {
668 &self.header
669 }
670
671 #[inline(always)]
672 fn body(&self) -> &Self::Body {
673 &self.body
674 }
675
676 #[inline(always)]
677 fn from_parts(
678 header: Self::Header,
679 body: Self::Body,
680 ) -> Result<Self, (Self::Header, Self::Body)> {
681 Self::from_parts(header, body)
682 }
683
684 #[inline(always)]
685 fn from_buffers(
686 header: SharedAlignedBuffer,
687 body: SharedAlignedBuffer,
688 ) -> Result<Self, (SharedAlignedBuffer, SharedAlignedBuffer)> {
689 Self::from_buffers(header, body)
690 }
691
692 #[inline(always)]
693 fn from_buffer(buffer: SharedAlignedBuffer) -> Result<Self, SharedAlignedBuffer> {
694 Self::from_buffer(buffer)
695 }
696
697 #[inline(always)]
698 fn block(&self) -> Self::Block<'_> {
699 self.block()
700 }
701}
702
703impl OwnedLeafShardBlock {
704 pub fn init<OS>(own_segments: OS) -> Result<OwnedLeafShardBlockBuilder, OwnedLeafShardBodyError>
706 where
707 OS: TrustedLen<Item = (LocalSegmentIndex, SegmentRoot)>,
708 {
709 Ok(OwnedLeafShardBlockBuilder {
710 body_builder: OwnedLeafShardBody::init(own_segments)?,
711 })
712 }
713
714 #[inline]
718 pub fn from_parts(
719 header: OwnedLeafShardHeader,
720 body: OwnedLeafShardBody,
721 ) -> Result<Self, (OwnedLeafShardHeader, OwnedLeafShardBody)> {
722 let block = Self { header, body };
723
724 if !block.block().is_internally_consistent() {
725 return Err((block.header, block.body));
726 }
727
728 Ok(block)
729 }
730
731 #[inline]
735 pub fn from_buffers(
736 header: SharedAlignedBuffer,
737 body: SharedAlignedBuffer,
738 ) -> Result<Self, (SharedAlignedBuffer, SharedAlignedBuffer)> {
739 let header = match OwnedLeafShardHeader::from_buffer(header) {
740 Ok(header) => header,
741 Err(header) => return Err((header, body)),
742 };
743 let body = match OwnedLeafShardBody::from_buffer(body) {
744 Ok(body) => body,
745 Err(body) => return Err((header.buffer().clone(), body)),
746 };
747
748 Self::from_parts(header, body)
749 .map_err(|(header, body)| (header.buffer().clone(), body.buffer().clone()))
750 }
751
752 #[inline]
755 pub fn from_buffer(buffer: SharedAlignedBuffer) -> Result<Self, SharedAlignedBuffer> {
756 let Some((header, body)) = split_block_buffer(
757 &buffer,
758 OwnedLeafShardHeader::MAX_ALLOCATION,
759 OwnedLeafShardHeader::from_buffer_start,
760 ) else {
761 return Err(buffer);
762 };
763 let Ok(body) = OwnedLeafShardBody::from_buffer(body) else {
764 return Err(buffer);
765 };
766
767 Self::from_parts(header, body).map_err(|_parts| buffer)
768 }
769
770 #[inline(always)]
773 pub(super) fn from_block(block: LeafShardBlock<'_>) -> Self {
774 Self {
775 header: block.header.to_owned(),
776 body: block.body.to_owned(),
777 }
778 }
779
780 pub fn block(&self) -> LeafShardBlock<'_> {
782 LeafShardBlock {
783 header: self.header.header().clone(),
784 body: *self.body.body(),
785 }
786 }
787}
788
789#[derive(Debug, Clone)]
791pub struct OwnedLeafShardBlockBuilder {
792 body_builder: OwnedLeafShardBlockBodyBuilder,
793}
794
795impl OwnedLeafShardBlockBuilder {
796 #[inline(always)]
798 pub fn add_transaction<T>(&mut self, transaction: T) -> Result<(), OwnedLeafShardBodyError>
799 where
800 T: WritableBodyTransaction,
801 {
802 self.body_builder.add_transaction(transaction)?;
803
804 Ok(())
805 }
806
807 pub fn with_header(
812 self,
813 prefix: &BlockHeaderPrefix,
814 state_root: Blake3Hash,
815 consensus_info: &BlockHeaderConsensusInfo,
816 beacon_chain_info: &BlockHeaderBeaconChainInfo,
817 ) -> Result<OwnedLeafShardBlockUnsealed, BlockHeaderConsistencyError> {
818 let body = self.body_builder.finish();
819 let header = OwnedLeafShardHeader::from_parts(
820 prefix,
821 &BlockHeaderResult {
822 body_root: body.root(),
823 state_root,
824 },
825 consensus_info,
826 beacon_chain_info,
827 )?;
828 Ok(OwnedLeafShardBlockUnsealed { body, header })
829 }
830}
831
832#[derive(Debug, Clone)]
834pub struct OwnedLeafShardBlockUnsealed {
835 body: OwnedLeafShardBody,
836 header: OwnedLeafShardHeaderUnsealed,
837}
838
839impl OwnedLeafShardBlockUnsealed {
840 #[inline(always)]
842 pub fn pre_seal_hash(&self) -> Blake3Hash {
843 self.header.pre_seal_hash()
844 }
845
846 pub fn with_seal(
850 self,
851 seal: BlockHeaderSeal<'_>,
852 ) -> Result<OwnedLeafShardBlock, BlockHeaderConsistencyError> {
853 let header = self.header.with_seal(seal)?;
854
855 Ok(OwnedLeafShardBlock {
856 header,
857 body: self.body,
858 })
859 }
860}
861
862#[derive(Debug, Clone, From)]
867pub enum OwnedBlock {
868 BeaconChain(OwnedBeaconChainBlock),
870 IntermediateShard(OwnedIntermediateShardBlock),
872 LeafShard(OwnedLeafShardBlock),
874}
875
876impl OwnedBlock {
877 #[inline]
879 pub fn split(self) -> (OwnedBlockHeader, OwnedBlockBody) {
880 match self {
881 Self::BeaconChain(block) => {
882 let (header, body) = block.split();
883 (header.into(), body.into())
884 }
885 Self::IntermediateShard(block) => {
886 let (header, body) = block.split();
887 (header.into(), body.into())
888 }
889 Self::LeafShard(block) => {
890 let (header, body) = block.split();
891 (header.into(), body.into())
892 }
893 }
894 }
895
896 #[inline(always)]
898 pub fn header(&self) -> BlockHeader<'_> {
899 match self {
900 Self::BeaconChain(block) => BlockHeader::BeaconChain(block.header.header().clone()),
901 Self::IntermediateShard(block) => {
902 BlockHeader::IntermediateShard(block.header.header().clone())
903 }
904 Self::LeafShard(block) => BlockHeader::LeafShard(block.header.header().clone()),
905 }
906 }
907
908 #[inline(always)]
910 pub fn body(&self) -> BlockBody<'_> {
911 match self {
912 Self::BeaconChain(block) => BlockBody::BeaconChain(*block.body.body()),
913 Self::IntermediateShard(block) => BlockBody::IntermediateShard(*block.body.body()),
914 Self::LeafShard(block) => BlockBody::LeafShard(*block.body.body()),
915 }
916 }
917
918 #[inline]
924 pub fn from_buffers(
925 header: SharedAlignedBuffer,
926 body: SharedAlignedBuffer,
927 ) -> Result<Self, (SharedAlignedBuffer, SharedAlignedBuffer)> {
928 let Some(shard_kind) = BlockHeader::shard_kind_from_bytes(header.as_slice()) else {
929 return Err((header, body));
930 };
931
932 Ok(match shard_kind {
933 RealShardKind::BeaconChain => {
934 Self::BeaconChain(OwnedBeaconChainBlock::from_buffers(header, body)?)
935 }
936 RealShardKind::IntermediateShard => {
937 Self::IntermediateShard(OwnedIntermediateShardBlock::from_buffers(header, body)?)
938 }
939 RealShardKind::LeafShard => {
940 Self::LeafShard(OwnedLeafShardBlock::from_buffers(header, body)?)
941 }
942 })
943 }
944
945 #[inline]
951 pub fn from_buffer(buffer: SharedAlignedBuffer) -> Result<Self, SharedAlignedBuffer> {
952 let Some(shard_kind) = BlockHeader::shard_kind_from_bytes(buffer.as_slice()) else {
953 return Err(buffer);
954 };
955
956 Ok(match shard_kind {
957 RealShardKind::BeaconChain => {
958 Self::BeaconChain(OwnedBeaconChainBlock::from_buffer(buffer)?)
959 }
960 RealShardKind::IntermediateShard => {
961 Self::IntermediateShard(OwnedIntermediateShardBlock::from_buffer(buffer)?)
962 }
963 RealShardKind::LeafShard => Self::LeafShard(OwnedLeafShardBlock::from_buffer(buffer)?),
964 })
965 }
966
967 #[inline(always)]
969 pub fn block(&self) -> Block<'_> {
970 match self {
971 Self::BeaconChain(block) => Block::BeaconChain(block.block()),
972 Self::IntermediateShard(block) => Block::IntermediateShard(block.block()),
973 Self::LeafShard(block) => Block::LeafShard(block.block()),
974 }
975 }
976}