Skip to content

Commit

Permalink
Masp tx extraction refactor + reduced type verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Sep 18, 2024
1 parent 6c813df commit 561f604
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
6 changes: 4 additions & 2 deletions crates/node/src/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use namada_sdk::args::ShieldedSync;
use namada_sdk::chain::testing::get_dummy_header;
use namada_sdk::chain::{BlockHeight, ChainId, Epoch};
use namada_sdk::events::extend::{
ComposeEvent, MaspTxBatchRefs, MaspTxBlockIndex, MaspTxRefs,
ComposeEvent, MaspTxBatchRefs, MaspTxBlockIndex, MaspTxRef, MaspTxRefs,
};
use namada_sdk::events::Event;
use namada_sdk::gas::TxGasMeter;
Expand Down Expand Up @@ -1044,7 +1044,9 @@ impl Client for BenchShell {
if let Section::MaspTx(transaction) =
section
{
Some(namada_sdk::events::extend::MaspTxRef::MaspSection(transaction.txid().into()))
Some(MaspTxRef::MaspSection(
transaction.txid().into(),
))
} else {
None
}
Expand Down
78 changes: 38 additions & 40 deletions crates/sdk/src/masp/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::Arc;
use borsh::BorshDeserialize;
use masp_primitives::merkle_tree::{CommitmentTree, IncrementalWitness};
use masp_primitives::sapling::Node;
use masp_primitives::transaction::Transaction as MaspTx;
use namada_core::chain::BlockHeight;
use namada_core::collections::HashMap;
use namada_core::storage::TxIndex;
Expand Down Expand Up @@ -110,25 +111,7 @@ impl<C: Client + Send + Sync> MaspClient for LedgerMaspClient<C> {
let extracted_masp_txs = extract_masp_tx(&tx, &masp_refs)
.map_err(|e| Error::Other(e.to_string()))?;

// Note that the index of the extracted MASP transaction does
// not necessarely match the index of the inner tx in the batch,
// we are only interested in giving a sequential ordering to the
// data
for (batch_index, transaction) in
extracted_masp_txs.into_iter().enumerate()
{
txs.push((
IndexedTx {
height: height.into(),
index: idx,
batch_index: Some(
u32::try_from(batch_index)
.map_err(|e| Error::Other(e.to_string()))?,
),
},
transaction,
));
}
index_txs(&mut txs, extracted_masp_txs, height.into(), idx)?;
}
}

Expand Down Expand Up @@ -486,8 +469,6 @@ impl MaspClient for IndexerMaspClient {
let mut extracted_masp_txs = Vec::with_capacity(batch.len());

for TransactionSlot { bytes } in batch {
type MaspTx = masp_primitives::transaction::Transaction;

extracted_masp_txs.push(
MaspTx::try_from_slice(&bytes).map_err(|err| {
Error::Other(format!(
Expand All @@ -499,25 +480,12 @@ impl MaspClient for IndexerMaspClient {
);
}

// Note that the index of the extracted MASP transaction does
// not necessarely match the index of the inner tx in the batch,
// we are only interested in giving a sequential ordering to the
// data
for (batch_index, transaction) in
extracted_masp_txs.into_iter().enumerate()
{
txs.push((
IndexedTx {
height: BlockHeight(block_height),
index: TxIndex(block_index),
batch_index: Some(
u32::try_from(batch_index)
.map_err(|e| Error::Other(e.to_string()))?,
),
},
transaction,
));
}
index_txs(
&mut txs,
extracted_masp_txs,
block_height.into(),
block_index.into(),
)?;
}
}

Expand Down Expand Up @@ -708,6 +676,36 @@ impl MaspClient for IndexerMaspClient {
)
}
}

#[allow(clippy::result_large_err)]
fn index_txs(
txs: &mut Vec<(IndexedTx, MaspTx)>,
extracted_masp_txs: impl IntoIterator<Item = MaspTx>,
height: BlockHeight,
index: TxIndex,
) -> Result<(), Error> {
// Note that the index of the extracted MASP transaction does
// not necessarely match the index of the inner tx in the batch,
// we are only interested in giving a sequential ordering to the
// data
for (batch_index, transaction) in extracted_masp_txs.into_iter().enumerate()
{
txs.push((
IndexedTx {
height,
index,
batch_index: Some(
u32::try_from(batch_index)
.map_err(|e| Error::Other(e.to_string()))?,
),
},
transaction,
));
}

Ok(())
}

#[derive(Copy, Clone)]
#[allow(clippy::enum_variant_names)]
enum BlockIndex {
Expand Down

0 comments on commit 561f604

Please sign in to comment.