Skip to content

Commit

Permalink
Add ctor from flattened block and entries iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Dec 19, 2023
1 parent f52ba01 commit 0111162
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion ledger-tool/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use {
clock::{Slot, UnixTimestamp},
native_token::lamports_to_sol,
},
solana_transaction_status::{EncodedTransactionWithStatusMeta, EntrySummary, Rewards},
solana_transaction_status::{
EncodedConfirmedBlock, EncodedTransactionWithStatusMeta, EntrySummary, Rewards,
},
std::fmt::{self, Display, Formatter, Result},
};

Expand Down Expand Up @@ -264,3 +266,40 @@ pub struct EncodedConfirmedBlockWithEntries {
pub block_time: Option<UnixTimestamp>,
pub block_height: Option<u64>,
}

impl EncodedConfirmedBlockWithEntries {
pub fn try_from(
block: EncodedConfirmedBlock,
entries_iterator: impl Iterator<Item = EntrySummary>,
) -> std::result::Result<Self, String> {
let mut entries = vec![];
for (i, entry) in entries_iterator.enumerate() {
let ending_transaction_index = entry
.starting_transaction_index
.saturating_add(entry.num_transactions as usize);
let transactions = block
.transactions
.get(entry.starting_transaction_index..ending_transaction_index)
.ok_or(format!(
"Mismatched entry data and transactions: entry {:?}",
i
))?;
entries.push(CliPopulatedEntry {
num_hashes: entry.num_hashes,
hash: entry.hash.to_string(),
num_transactions: entry.num_transactions,
starting_transaction_index: entry.starting_transaction_index,
transactions: transactions.to_vec(),
});
}
Ok(Self {
previous_blockhash: block.previous_blockhash,
blockhash: block.blockhash,
parent_slot: block.parent_slot,
entries: entries,
rewards: block.rewards,
block_time: block.block_time,
block_height: block.block_height,
})
}
}

0 comments on commit 0111162

Please sign in to comment.