Skip to main content

miden_objects/conversion/
block.rs

1use miden_protocol::block::{
2    BlockAccountUpdate,
3    BlockBody,
4    BlockHeader,
5    BlockNumber,
6    FeeParameters,
7    OutputNoteBatch,
8    SignedBlock,
9    ValidatorConfig,
10};
11use miden_protocol::protocol_config::NextProtocolConfig;
12use miden_protocol::transaction::{OutputNote, PartialBlockchain};
13
14use crate::proto;
15
16#[cfg(test)]
17mod tests;
18
19// BLOCK NUMBER
20// ================================================================================================
21
22impl From<BlockNumber> for proto::blockchain::BlockNumber {
23    fn from(value: BlockNumber) -> Self {
24        Self { block_num: value.as_u32() }
25    }
26}
27
28// PARTIAL BLOCKCHAIN
29// ================================================================================================
30
31impl From<&PartialBlockchain> for proto::blockchain::PartialBlockchain {
32    fn from(value: &PartialBlockchain) -> Self {
33        Self {
34            mmr: Some(value.mmr().into()),
35            block_headers: value.block_headers().map(Into::into).collect(),
36        }
37    }
38}
39
40// BLOCK HEADER
41// ================================================================================================
42
43impl From<&BlockHeader> for proto::blockchain::BlockHeader {
44    fn from(header: &BlockHeader) -> Self {
45        Self {
46            version: proto::blockchain::BlockVersion::V1 as i32,
47            timestamp: header.timestamp(),
48            block_num: Some(header.block_num().into()),
49            prev_block_commitment: Some(header.prev_block_commitment().into()),
50            chain_commitment: Some(header.chain_commitment().into()),
51            account_root: Some(header.account_root().into()),
52            nullifier_root: Some(header.nullifier_root().into()),
53            note_root: Some(header.note_root().into()),
54            tx_commitment: Some(header.tx_commitment().into()),
55            validator_config: Some(header.validator_config().into()),
56            fee_parameters: Some(header.fee_parameters().into()),
57            protocol_config_commitment: Some(header.protocol_config_commitment().into()),
58            next_protocol_config: header.next_protocol_config().map(Into::into),
59        }
60    }
61}
62
63impl From<BlockHeader> for proto::blockchain::BlockHeader {
64    fn from(header: BlockHeader) -> Self {
65        (&header).into()
66    }
67}
68
69// BLOCK BODY
70// ================================================================================================
71
72impl From<&BlockBody> for proto::blockchain::BlockBody {
73    fn from(body: &BlockBody) -> Self {
74        Self {
75            updated_accounts: body.updated_accounts().iter().map(Into::into).collect(),
76            output_note_batches: body.output_note_batches().iter().map(Into::into).collect(),
77            created_nullifiers: body
78                .created_nullifiers()
79                .iter()
80                .map(|nullifier| nullifier.as_word().into())
81                .collect(),
82            transactions: body.transactions().as_slice().iter().map(Into::into).collect(),
83        }
84    }
85}
86
87impl From<BlockBody> for proto::blockchain::BlockBody {
88    fn from(body: BlockBody) -> Self {
89        (&body).into()
90    }
91}
92
93// BLOCK BODY COMPONENTS
94// ================================================================================================
95
96impl From<&BlockAccountUpdate> for proto::blockchain::BlockAccountUpdate {
97    fn from(update: &BlockAccountUpdate) -> Self {
98        Self {
99            account_id: Some(update.account_id().into()),
100            final_state_commitment: Some(update.final_state_commitment().into()),
101            details: Some(update.details().into()),
102        }
103    }
104}
105
106impl From<&(usize, OutputNote)> for proto::blockchain::IndexedOutputNote {
107    fn from((index, note): &(usize, OutputNote)) -> Self {
108        Self {
109            note_index_in_batch: u32::try_from(*index)
110                .expect("valid output note indices fit into u32"),
111            note: Some(note.into()),
112        }
113    }
114}
115
116impl From<&OutputNoteBatch> for proto::blockchain::OutputNoteBatch {
117    fn from(batch: &OutputNoteBatch) -> Self {
118        Self {
119            notes: batch.iter().map(Into::into).collect(),
120        }
121    }
122}
123
124// SIGNED BLOCK
125// ================================================================================================
126
127impl From<&SignedBlock> for proto::blockchain::SignedBlock {
128    fn from(block: &SignedBlock) -> Self {
129        Self {
130            header: Some(block.header().into()),
131            body: Some(block.body().into()),
132            signatures: block.signatures().as_signatures().iter().map(Into::into).collect(),
133        }
134    }
135}
136
137impl From<SignedBlock> for proto::blockchain::SignedBlock {
138    fn from(block: SignedBlock) -> Self {
139        (&block).into()
140    }
141}
142
143// VALIDATOR AND PROTOCOL CONFIGURATION
144// ================================================================================================
145
146impl From<&ValidatorConfig> for proto::blockchain::ValidatorConfig {
147    fn from(value: &ValidatorConfig) -> Self {
148        Self {
149            keys: value.keys().iter().map(Into::into).collect(),
150            quorum: u32::from(value.quorum()),
151        }
152    }
153}
154
155impl From<ValidatorConfig> for proto::blockchain::ValidatorConfig {
156    fn from(value: ValidatorConfig) -> Self {
157        (&value).into()
158    }
159}
160
161impl From<&NextProtocolConfig> for proto::blockchain::NextProtocolConfig {
162    fn from(value: &NextProtocolConfig) -> Self {
163        Self {
164            effective_from: Some(value.effective_from().into()),
165            protocol_config: Some(value.protocol_config().into()),
166        }
167    }
168}
169
170impl From<NextProtocolConfig> for proto::blockchain::NextProtocolConfig {
171    fn from(value: NextProtocolConfig) -> Self {
172        (&value).into()
173    }
174}
175
176impl From<&FeeParameters> for proto::blockchain::FeeParameters {
177    fn from(value: &FeeParameters) -> Self {
178        Self {
179            verification_base_fee: value.verification_base_fee(),
180        }
181    }
182}
183
184impl From<FeeParameters> for proto::blockchain::FeeParameters {
185    fn from(value: FeeParameters) -> Self {
186        (&value).into()
187    }
188}