miden_objects/conversion/
block.rs1use 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
19impl From<BlockNumber> for proto::blockchain::BlockNumber {
23 fn from(value: BlockNumber) -> Self {
24 Self { block_num: value.as_u32() }
25 }
26}
27
28impl From<&PartialBlockchain> for proto::blockchain::PartialBlockchain {
32 fn from(value: &PartialBlockchain) -> Self {
33 let mmr = value.mmr();
34 let tracked_leaves = mmr
35 .leaves()
36 .map(|(position, leaf)| {
37 let proof = mmr
38 .open(position)
39 .expect("tracked MMR position must be in bounds")
40 .expect("tracked MMR leaf must have an opening");
41 proto::blockchain::TrackedMmrLeaf {
42 position: position as u64,
43 leaf: Some(leaf.into()),
44 path: proof.merkle_path().nodes().iter().map(Into::into).collect(),
45 }
46 })
47 .collect();
48 Self {
49 forest: mmr.forest().num_leaves() as u64,
50 peaks: mmr.peaks().peaks().iter().map(Into::into).collect(),
51 tracked_leaves,
52 block_headers: value.block_headers().map(Into::into).collect(),
53 }
54 }
55}
56
57impl From<&BlockHeader> for proto::blockchain::BlockHeader {
61 fn from(header: &BlockHeader) -> Self {
62 Self {
63 version: proto::blockchain::BlockVersion::V1 as i32,
64 timestamp: header.timestamp(),
65 block_num: Some(header.block_num().into()),
66 prev_block_commitment: Some(header.prev_block_commitment().into()),
67 chain_commitment: Some(header.chain_commitment().into()),
68 account_root: Some(header.account_root().into()),
69 nullifier_root: Some(header.nullifier_root().into()),
70 note_root: Some(header.note_root().into()),
71 tx_commitment: Some(header.tx_commitment().into()),
72 validator_config: Some(header.validator_config().into()),
73 fee_parameters: Some(header.fee_parameters().into()),
74 protocol_config_commitment: Some(header.protocol_config_commitment().into()),
75 next_protocol_config: header.next_protocol_config().map(Into::into),
76 }
77 }
78}
79
80impl From<BlockHeader> for proto::blockchain::BlockHeader {
81 fn from(header: BlockHeader) -> Self {
82 (&header).into()
83 }
84}
85
86impl From<&BlockBody> for proto::blockchain::BlockBody {
90 fn from(body: &BlockBody) -> Self {
91 Self {
92 updated_accounts: body.updated_accounts().iter().map(Into::into).collect(),
93 output_note_batches: body.output_note_batches().iter().map(Into::into).collect(),
94 created_nullifiers: body
95 .created_nullifiers()
96 .iter()
97 .map(|nullifier| nullifier.as_word().into())
98 .collect(),
99 transactions: body.transactions().as_slice().iter().map(Into::into).collect(),
100 }
101 }
102}
103
104impl From<BlockBody> for proto::blockchain::BlockBody {
105 fn from(body: BlockBody) -> Self {
106 (&body).into()
107 }
108}
109
110impl From<&BlockAccountUpdate> for proto::blockchain::BlockAccountUpdate {
114 fn from(update: &BlockAccountUpdate) -> Self {
115 Self {
116 account_id: Some(update.account_id().into()),
117 final_state_commitment: Some(update.final_state_commitment().into()),
118 details: Some(update.details().into()),
119 }
120 }
121}
122
123impl From<&(usize, OutputNote)> for proto::blockchain::IndexedOutputNote {
124 fn from((index, note): &(usize, OutputNote)) -> Self {
125 Self {
126 note_index_in_batch: u32::try_from(*index)
127 .expect("valid output note indices fit into u32"),
128 note: Some(note.into()),
129 }
130 }
131}
132
133impl From<&OutputNoteBatch> for proto::blockchain::OutputNoteBatch {
134 fn from(batch: &OutputNoteBatch) -> Self {
135 Self {
136 notes: batch.iter().map(Into::into).collect(),
137 }
138 }
139}
140
141impl From<&SignedBlock> for proto::blockchain::SignedBlock {
145 fn from(block: &SignedBlock) -> Self {
146 Self {
147 header: Some(block.header().into()),
148 body: Some(block.body().into()),
149 signatures: block.signatures().as_signatures().iter().map(Into::into).collect(),
150 }
151 }
152}
153
154impl From<SignedBlock> for proto::blockchain::SignedBlock {
155 fn from(block: SignedBlock) -> Self {
156 (&block).into()
157 }
158}
159
160impl From<&ValidatorConfig> for proto::blockchain::ValidatorConfig {
164 fn from(value: &ValidatorConfig) -> Self {
165 Self {
166 keys: value.keys().iter().map(Into::into).collect(),
167 quorum: u32::from(value.quorum()),
168 }
169 }
170}
171
172impl From<ValidatorConfig> for proto::blockchain::ValidatorConfig {
173 fn from(value: ValidatorConfig) -> Self {
174 (&value).into()
175 }
176}
177
178impl From<&NextProtocolConfig> for proto::blockchain::NextProtocolConfig {
179 fn from(value: &NextProtocolConfig) -> Self {
180 Self {
181 effective_from: Some(value.effective_from().into()),
182 protocol_config: Some(value.protocol_config().into()),
183 }
184 }
185}
186
187impl From<NextProtocolConfig> for proto::blockchain::NextProtocolConfig {
188 fn from(value: NextProtocolConfig) -> Self {
189 (&value).into()
190 }
191}
192
193impl From<&FeeParameters> for proto::blockchain::FeeParameters {
194 fn from(value: &FeeParameters) -> Self {
195 Self {
196 verification_base_fee: value.verification_base_fee(),
197 }
198 }
199}
200
201impl From<FeeParameters> for proto::blockchain::FeeParameters {
202 fn from(value: FeeParameters) -> Self {
203 (&value).into()
204 }
205}