Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): shorten try_from_block_info_and_data() #1371

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions crates/astria-core/src/sequencerblock/v1alpha1/block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::HashMap;
use std::{
collections::HashMap,
vec::IntoIter,
};

use bytes::Bytes;
use indexmap::IndexMap;
Expand Down Expand Up @@ -703,7 +706,6 @@ impl SequencerBlock {
/// # Panics
///
/// - if a rollup data merkle proof cannot be constructed.
#[allow(clippy::too_many_lines)] // Temporary fix, should refactor: TODO(https://github.com/astriaorg/astria/issues/1357)
pub fn try_from_block_info_and_data(
block_hash: [u8; 32],
chain_id: tendermint::chain::Id,
Expand All @@ -719,21 +721,8 @@ impl SequencerBlock {
let data_hash = tree.root();

let mut data_list = data.into_iter();
let rollup_transactions_root: [u8; 32] = data_list
.next()
.ok_or(SequencerBlockError::no_rollup_transactions_root())?
.as_ref()
.try_into()
.map_err(|_| {
SequencerBlockError::incorrect_rollup_transactions_root_length(data_list.len())
})?;

let rollup_ids_root: [u8; 32] = data_list
.next()
.ok_or(SequencerBlockError::no_rollup_ids_root())?
.as_ref()
.try_into()
.map_err(|_| SequencerBlockError::incorrect_rollup_ids_root_length(data_list.len()))?;
let (rollup_transactions_root, rollup_ids_root) =
rollup_transactions_and_ids_root_from_data(&mut data_list)?;

let mut rollup_datas = IndexMap::new();
for elem in data_list {
Expand Down Expand Up @@ -923,6 +912,26 @@ impl SequencerBlock {
}
}

fn rollup_transactions_and_ids_root_from_data(
data_list: &mut IntoIter<Bytes>,
) -> Result<([u8; 32], [u8; 32]), SequencerBlockError> {
let rollup_transactions_root: [u8; 32] = data_list
.next()
.ok_or(SequencerBlockError::no_rollup_transactions_root())?
.as_ref()
.try_into()
.map_err(|_| {
SequencerBlockError::incorrect_rollup_transactions_root_length(data_list.len())
})?;
let rollup_ids_root: [u8; 32] = data_list
.next()
.ok_or(SequencerBlockError::no_rollup_ids_root())?
.as_ref()
.try_into()
.map_err(|_| SequencerBlockError::incorrect_rollup_ids_root_length(data_list.len()))?;
Ok((rollup_transactions_root, rollup_ids_root))
}

/// Constructs a `[merkle::Tree]` from an iterator yielding byte slices.
///
/// This hashes each item before pushing it into the Merkle Tree, which
Expand Down
Loading