Skip to content

Commit

Permalink
Reworks masp tx indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed May 23, 2024
1 parent 4c36f6e commit f399f25
Show file tree
Hide file tree
Showing 14 changed files with 437 additions and 438 deletions.
17 changes: 3 additions & 14 deletions crates/core/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,28 +534,17 @@ impl FromStr for MaspValue {
}
}

/// Reference to a masp transaction inside a [`BatchedTx`]
#[derive(Clone, Serialize, Deserialize)]
pub struct MaspTxRef {
// FIXME: actually, are we using the commitment? Probably if I give the
// masp section ref I don't need the commitment anymore right?
/// The inner tx commitment's hash
pub cmt: Hash,
/// The hash of the masp [`Section::MaspTx`]
pub masp_section_ref: Hash,
}

/// The masp transactions' references of a given batch
#[derive(Default, Clone, Serialize, Deserialize)]
pub struct BatchMaspTxRefs(pub Vec<MaspTxRef>);
pub struct MaspTxRefs(pub Vec<Hash>);

impl Display for BatchMaspTxRefs {
impl Display for MaspTxRefs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string(self).unwrap())
}
}

impl FromStr for BatchMaspTxRefs {
impl FromStr for MaspTxRefs {
type Err = serde_json::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down
12 changes: 6 additions & 6 deletions crates/events/src/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

use std::fmt::Display;
use std::marker::PhantomData;
use std::ops::{ControlFlow, DerefMut};
use std::ops::ControlFlow;
use std::str::FromStr;

use namada_core::collections::HashMap;
use namada_core::hash::Hash;
use namada_core::masp::BatchMaspTxRefs;
use namada_core::masp::MaspTxRefs;
use namada_core::storage::{BlockHeight, TxIndex};

use super::*;
Expand Down Expand Up @@ -502,10 +502,10 @@ impl EventAttributeEntry<'static> for MaspTxBlockIndex {
/// Extend an [`Event`] with `masp_tx_batch_refs` data, indicating the specific
/// inner transactions inside the batch that are valid masp txs and the
/// references to the relative masp sections.
pub struct MaspTxBatchRefs(pub BatchMaspTxRefs);
pub struct MaspTxBatchRefs(pub MaspTxRefs);

impl EventAttributeEntry<'static> for MaspTxBatchRefs {
type Value = BatchMaspTxRefs;
type Value = MaspTxRefs;
type ValueOwned = Self::Value;

const KEY: &'static str = "masp_tx_batch_refs";
Expand Down Expand Up @@ -607,8 +607,8 @@ where
}

/// Return a new implementation of [`EventAttributeChecker`].
pub fn attribute_checker<'value, DATA, ATTR>(
) -> Box<dyn EventAttributeChecker<'value, ATTR>>
pub fn attribute_checker<'value, DATA, ATTR>()
-> Box<dyn EventAttributeChecker<'value, ATTR>>
where
DATA: EventAttributeEntry<'value> + 'static,
ATTR: AttributesMap,
Expand Down
4 changes: 1 addition & 3 deletions crates/namada/src/ledger/native_vp/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use namada_proof_of_stake::Epoch;
use namada_sdk::masp::verify_shielded_tx;
use namada_state::{ConversionState, OptionExt, ResultExt, StateRead};
use namada_token::read_denom;
use namada_tx::action::{Action, MaspAction, Read};
use namada_tx::BatchedTxRef;
use namada_vp_env::VpEnv;
use ripemd::Digest as RipemdDigest;
Expand Down Expand Up @@ -364,8 +363,7 @@ where
let shielded_tx = tx_data
.tx
.get_section(&masp_section_ref)
.map(|section| section.masp_tx())
.flatten()
.and_then(|section| section.masp_tx())
.ok_or_else(|| {
native_vp::Error::new_const(
"Missing MASP section in transaction",
Expand Down
14 changes: 6 additions & 8 deletions crates/namada/src/ledger/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ use borsh_ext::BorshSerializeExt;
use eyre::{eyre, WrapErr};
use namada_core::booleans::BoolResultUnitExt;
use namada_core::hash::Hash;
use namada_core::masp::MaspTxRef;
use namada_core::storage::Key;
use namada_events::extend::{
ComposeEvent, Height as HeightAttr, TxHash as TxHashAttr,
};
use namada_events::EventLevel;
use namada_gas::TxGasMeter;
use namada_sdk::tx::TX_TRANSFER_WASM;
use namada_state::{StateRead, StorageWrite};
use namada_state::StorageWrite;
use namada_token::event::{TokenEvent, TokenOperation, UserAccount};
use namada_tx::action::{Action, MaspAction, Read};
use namada_tx::data::protocol::ProtocolTxType;
use namada_tx::data::{
BatchResults, BatchedTxResult, ExtendedTxResult, TxResult, TxType,
Expand Down Expand Up @@ -338,12 +336,12 @@ where
// transaction refs for the events
if let Some(masp_section_ref) =
namada_tx::action::get_masp_section_ref(state)
.map_err(|e| Error::StateError(e))?
.map_err(Error::StateError)?
{
extended_tx_result.masp_tx_refs.0.push(MaspTxRef {
cmt: cmt.get_hash(),
masp_section_ref,
});
extended_tx_result
.masp_tx_refs
.0
.push(masp_section_ref);
}
state.write_log_mut().commit_tx_to_batch();
} else {
Expand Down
32 changes: 12 additions & 20 deletions crates/node/src/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use namada::ledger::native_vp::ibc::get_dummy_header;
use namada::ledger::queries::{
Client, EncodedResponseQuery, RequestCtx, RequestQuery, Router, RPC,
};
use namada::masp::{BatchMaspTxRefs, MaspTxRef};
use namada::masp::MaspTxRefs;
use namada::state::StorageRead;
use namada::tx::data::pos::Bond;
use namada::tx::data::{
Expand Down Expand Up @@ -923,25 +923,17 @@ impl Client for BenchShell {
.with(MaspTxBlockIndex(TxIndex::must_from_usize(
idx,
)))
.with(MaspTxBatchRefs(BatchMaspTxRefs(vec![
MaspTxRef {
cmt: tx
.first_commitments()
.unwrap()
.get_hash(),
masp_section_ref: tx
.sections
.iter()
.find_map(|section| {
if let Section::MaspTx(_) = section
{
Some(section.get_hash())
} else {
None
}
})
.unwrap(),
},
.with(MaspTxBatchRefs(MaspTxRefs(vec![
tx.sections
.iter()
.find_map(|section| {
if let Section::MaspTx(_) = section {
Some(section.get_hash())
} else {
None
}
})
.unwrap(),
])))
.into();
namada::tendermint::abci::Event::from(event)
Expand Down
Loading

0 comments on commit f399f25

Please sign in to comment.