bee_inx/
metadata.rs

1// Copyright 2022 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use bee_block as bee;
5use inx::proto;
6
7use crate::maybe_missing;
8
9#[allow(missing_docs)]
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub enum LedgerInclusionState {
12    NoTransaction,
13    Included,
14    Conflicting,
15}
16
17/// The metadata for a block with a given [`BlockId`](bee::BlockId).
18#[derive(Clone, Debug, PartialEq, Eq)]
19pub struct BlockMetadata {
20    /// The id of the block.
21    pub block_id: bee::BlockId,
22    /// The parents of the block.
23    pub parents: Box<[bee::BlockId]>,
24    /// Status of the solidification process.
25    pub is_solid: bool,
26    /// Indicates that the block should be promoted.
27    pub should_promote: bool,
28    /// Indicates that the block should be reattached.
29    pub should_reattach: bool,
30    /// The milestone that referenced the block.
31    pub referenced_by_milestone_index: u32,
32    /// The corresponding milestone index.
33    pub milestone_index: u32,
34    /// Indicates if a block is part of the ledger state or not.
35    pub ledger_inclusion_state: LedgerInclusionState,
36    /// Indicates if a conflict occurred, and if so holds information about the reason for the conflict.
37    pub conflict_reason: bee::semantic::ConflictReason,
38    /// The whiteflag index of this block inside the milestone.
39    pub white_flag_index: u32,
40}
41
42impl TryFrom<proto::BlockMetadata> for BlockMetadata {
43    type Error = bee::InxError;
44
45    fn try_from(value: proto::BlockMetadata) -> Result<Self, Self::Error> {
46        let ledger_inclusion_state = value.ledger_inclusion_state().into();
47        let conflict_reason = value.conflict_reason().into();
48
49        let parents = value
50            .parents
51            .into_iter()
52            .map(TryInto::try_into)
53            .collect::<Result<Vec<_>, _>>()?;
54
55        Ok(BlockMetadata {
56            block_id: maybe_missing!(value.block_id).try_into()?,
57            parents: parents.into_boxed_slice(),
58            is_solid: value.solid,
59            should_promote: value.should_promote,
60            should_reattach: value.should_reattach,
61            referenced_by_milestone_index: value.referenced_by_milestone_index,
62            milestone_index: value.milestone_index,
63            ledger_inclusion_state,
64            conflict_reason,
65            white_flag_index: value.white_flag_index,
66        })
67    }
68}
69
70impl From<proto::block_metadata::LedgerInclusionState> for LedgerInclusionState {
71    fn from(value: proto::block_metadata::LedgerInclusionState) -> Self {
72        use proto::block_metadata::LedgerInclusionState::*;
73        match value {
74            NoTransaction => LedgerInclusionState::NoTransaction,
75            Included => LedgerInclusionState::Included,
76            Conflicting => LedgerInclusionState::Conflicting,
77        }
78    }
79}
80
81impl From<LedgerInclusionState> for proto::block_metadata::LedgerInclusionState {
82    fn from(value: LedgerInclusionState) -> Self {
83        match value {
84            LedgerInclusionState::NoTransaction => Self::NoTransaction,
85            LedgerInclusionState::Included => Self::Included,
86            LedgerInclusionState::Conflicting => Self::Conflicting,
87        }
88    }
89}
90
91impl From<BlockMetadata> for proto::BlockMetadata {
92    fn from(value: BlockMetadata) -> Self {
93        Self {
94            block_id: Some(value.block_id.into()),
95            parents: value.parents.into_vec().into_iter().map(Into::into).collect(),
96            solid: value.is_solid,
97            should_promote: value.should_promote,
98            should_reattach: value.should_reattach,
99            referenced_by_milestone_index: value.referenced_by_milestone_index,
100            milestone_index: value.milestone_index,
101            ledger_inclusion_state: proto::block_metadata::LedgerInclusionState::from(value.ledger_inclusion_state)
102                .into(),
103            conflict_reason: proto::block_metadata::ConflictReason::from(value.conflict_reason).into(),
104            white_flag_index: value.white_flag_index,
105        }
106    }
107}