Skip to content

Commit

Permalink
WIP fetches masp txs from blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Jan 4, 2024
1 parent c2acfec commit 33cab6a
Show file tree
Hide file tree
Showing 19 changed files with 514 additions and 106 deletions.
8 changes: 6 additions & 2 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ use namada::types::ibc::{is_ibc_denom, IbcTokenHash};
use namada::types::io::Io;
use namada::types::key::*;
use namada::types::masp::{BalanceOwner, ExtendedViewingKey, PaymentAddress};
use namada::types::storage::{BlockHeight, BlockResults, Epoch, Key, KeySeg};
use namada::types::storage::{
BlockHeight, BlockResults, Epoch, IndexedTx, Key, KeySeg,
};
use namada::types::token::{Change, MaspDenom};
use namada::types::{storage, token};
use namada_sdk::error::{is_pinned_error, Error, PinnedBalanceError};
Expand Down Expand Up @@ -145,7 +147,9 @@ pub async fn query_transfers(
.map(|fvk| (ExtendedFullViewingKey::from(*fvk).fvk.vk, fvk))
.collect();
// Now display historical shielded and transparent transactions
for ((height, idx), (epoch, tfer_delta, tx_delta)) in transfers {
for (IndexedTx { height, index: idx }, (epoch, tfer_delta, tx_delta)) in
transfers
{
// Check if this transfer pertains to the supplied owner
let mut relevant = match &query_owner {
Either::Left(BalanceOwner::FullViewingKey(fvk)) => tx_delta
Expand Down
10 changes: 10 additions & 0 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,16 @@ where
let mut event = Event::from(ibc_event);
// Add the height for IBC event query
event["height"] = height.to_string();
if tx_event
.attributes
.contains_key("is_valid_masp_tx")
{
// Add the tx index for masp txs clients
// queries
// FIXME: review this
event["is_valid_masp_tx"] =
tx_index.to_string();
}
event
})
// eth bridge events
Expand Down
1 change: 1 addition & 0 deletions apps/src/lib/node/ledger/shell/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ where
&mut shell.vp_wasm_cache,
&mut shell.tx_wasm_cache,
None,
&mut false,
);
shell
.wl_storage
Expand Down
6 changes: 4 additions & 2 deletions apps/src/lib/node/ledger/shell/testing/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,10 +761,12 @@ impl<'a> Client for &'a MockNode {
height: H,
) -> Result<tendermint_rpc::endpoint::block_results::Response, RpcError>
where
H: Into<namada::tendermint::block::Height> + Send,
H: TryInto<namada::tendermint::block::Height> + Send,
{
self.drive_mock_services_bg().await;
let height = height.into();
let height = height.try_into().map_err(|_| {
RpcError::parse("Could not parse block height".to_string())
})?;
let encoded_event = EncodedEvent(height.value());
let locked = self.shell.lock().unwrap();
let events: Vec<_> = locked
Expand Down
2 changes: 2 additions & 0 deletions core/src/ledger/ibc/context/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ pub trait IbcStorageContext: StorageRead + StorageWrite {
) -> Result<(), Error>;

/// Handle masp tx
// FIXME: try again to remove tx_index from some places
fn handle_masp_tx(
&mut self,
shielded: &masp_primitives::transaction::Transaction,
pin_key: Option<&str>,
) -> Result<(), Error>;

/// Mint token
Expand Down
6 changes: 5 additions & 1 deletion core/src/ledger/ibc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ where
pub fn execute(&mut self, tx_data: &[u8]) -> Result<(), Error> {
let message = decode_message(tx_data)?;
match &message {
// FIXME: look here for MASP on IBC
IbcMessage::Transfer(msg) => {
let mut token_transfer_ctx =
TokenTransferContext::new(self.ctx.inner.clone());
Expand Down Expand Up @@ -282,7 +283,10 @@ where
self.ctx
.inner
.borrow_mut()
.handle_masp_tx(&shielded_transfer.masp_tx)
.handle_masp_tx(
&shielded_transfer.masp_tx,
shielded_transfer.transfer.key.as_deref(),
)
.map_err(|_| {
Error::MaspTx("Writing MASP components failed".to_string())
})?;
Expand Down
21 changes: 20 additions & 1 deletion core/src/ledger/masp_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use masp_primitives::transaction::Transaction;

use super::storage_api::{StorageRead, StorageWrite};
use crate::ledger::storage_api::{Error, Result};
use crate::types::token::{masp_commitment_tree_key, masp_nullifier_key};
use crate::types::address::MASP;
use crate::types::storage::{IndexedTx, Key, KeySeg};
use crate::types::token::{
masp_commitment_tree_key, masp_nullifier_key, PIN_KEY_PREFIX,
};

// Writes the nullifiers of the provided masp transaction to storage
fn reveal_nullifiers(
Expand Down Expand Up @@ -59,12 +63,27 @@ pub fn update_note_commitment_tree(
pub fn handle_masp_tx(
ctx: &mut (impl StorageRead + StorageWrite),
shielded: &Transaction,
pin_key: Option<&str>,
) -> Result<()> {
// TODO: temporarily disabled because of the node aggregation issue in WASM.
// Using the host env tx_update_masp_note_commitment_tree or directly the
// update_note_commitment_tree function as a workaround instead
// update_note_commitment_tree(ctx, shielded)?;
reveal_nullifiers(ctx, shielded)?;

// If storage key has been supplied, then pin this transaction to it
if let Some(key) = pin_key {
let pin_key = Key::from(MASP.to_db_key())
.push(&(PIN_KEY_PREFIX.to_owned() + key))
.expect("Cannot obtain a storage key");
ctx.write(
&pin_key,
IndexedTx {
height: ctx.get_block_height()?,
index: ctx.get_tx_index()?,
},
)?;
}

Ok(())
}
1 change: 1 addition & 0 deletions core/src/ledger/vp_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ where

/// Get the shielded action including the transfer and the masp tx
fn get_shielded_action(
// FIXME: I need this
&self,
tx_data: &Tx,
) -> Result<(Transfer, Transaction), storage_api::Error> {
Expand Down
2 changes: 2 additions & 0 deletions core/src/types/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ pub fn get_shielded_transfer(
return Ok(None);
}

// FIXME: I should place the is_masp_tx attribute directly on the ibc event
// not in finalize block FIXME: maybe it's not possible
event
.attributes
.get("memo")
Expand Down
21 changes: 21 additions & 0 deletions core/src/types/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,27 @@ impl<E> GetEventNonce for InnerEthEventsQueue<E> {
}
}

/// Represents the pointers of an indexed tx, which are the block height and the
/// index inside that block
#[derive(
Default,
Debug,
Copy,
Clone,
BorshSerialize,
BorshDeserialize,
Eq,
PartialEq,
Ord,
PartialOrd,
)]
pub struct IndexedTx {
/// The block height of the indexed tx
pub height: BlockHeight,
/// The index in the block of the tx
pub index: TxIndex,
}

#[cfg(test)]
/// Tests and strategies for storage
pub mod tests {
Expand Down
14 changes: 13 additions & 1 deletion core/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,8 @@ pub const DENOM_STORAGE_KEY: &str = "denomination";
pub const MINTER_STORAGE_KEY: &str = "minter";
/// Key segment for minted balance
pub const MINTED_STORAGE_KEY: &str = "minted";
/// Key segment prefix for pinned shielded transactions
pub const PIN_KEY_PREFIX: &str = "pin-";
/// Key segment prefix for the nullifiers
pub const MASP_NULLIFIERS_KEY: &str = "nullifiers";
/// Key segment prefix for the note commitment merkle tree
Expand Down Expand Up @@ -1217,7 +1219,10 @@ pub fn is_masp_key(key: &Key) -> bool {
pub fn is_masp_allowed_key(key: &Key) -> bool {
match &key.segments[..] {
[DbKeySeg::AddressSeg(addr), DbKeySeg::StringSeg(key)]
if *addr == MASP && key == MASP_NOTE_COMMITMENT_TREE_KEY =>
if *addr == MASP
//FIXME: place the check back in the masp vp if needed
&& (key.starts_with(PIN_KEY_PREFIX)
|| key == MASP_NOTE_COMMITMENT_TREE_KEY) =>
{
true
}
Expand Down Expand Up @@ -1258,6 +1263,13 @@ pub fn masp_last_inflation_key(token_address: &Address) -> Key {
)
}

/// Get a key for a masp pin
pub fn masp_pin_tx_key(key: &str) -> Key {
Key::from(MASP.to_db_key())
.push(&(PIN_KEY_PREFIX.to_owned() + key))
.expect("Cannot obtain a storage key")
}

/// Get a key for a masp nullifier
pub fn masp_nullifier_key(nullifier: &Nullifier) -> Key {
Key::from(MASP.to_db_key())
Expand Down
Loading

0 comments on commit 33cab6a

Please sign in to comment.