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

masp: move to native VP #2051

Merged
merged 8 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .changelog/unreleased/SDK/2051-native-masp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Masp as internal address. Updated `aux_signing_data`
to return no key and 0 threshold if owner is masp.
([\#2051](https://github.com/anoma/namada/pull/2051))
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/2051-native-masp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Moved the masp vp to native.
([\#2051](https://github.com/anoma/namada/pull/2051))
16 changes: 8 additions & 8 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use namada::ledger::pos::PosParams;
use namada::ledger::queries::RPC;
use namada::ledger::storage::ConversionState;
use namada::proof_of_stake::types::{ValidatorState, WeightedValidator};
use namada::types::address::{masp, Address, InternalAddress};
use namada::types::address::{Address, InternalAddress, MASP};
use namada::types::hash::Hash;
use namada::types::ibc::{is_ibc_denom, IbcTokenHash};
use namada::types::io::Io;
Expand Down Expand Up @@ -203,7 +203,7 @@ pub async fn query_transfers<'a>(
);
// Display the transparent changes first
for (account, MaspChange { ref asset, change }) in tfer_delta {
if account != masp() {
if account != MASP {
display!(context.io(), " {}:", account);
let token_alias =
lookup_token_alias(context, asset, &account).await;
Expand All @@ -230,7 +230,7 @@ pub async fn query_transfers<'a>(
display!(context.io(), " {}:", fvk_map[&account]);
for (token_addr, val) in masp_change {
let token_alias =
lookup_token_alias(context, &token_addr, &masp()).await;
lookup_token_alias(context, &token_addr, &MASP).await;
let sign = match val.cmp(&Change::zero()) {
Ordering::Greater => "+",
Ordering::Less => "-",
Expand Down Expand Up @@ -528,7 +528,7 @@ pub async fn query_pinned_balance<'a>(
.format_amount(token_addr, (*value).into())
.await;
let token_alias =
lookup_token_alias(context, token_addr, &masp()).await;
lookup_token_alias(context, token_addr, &MASP).await;
display_line!(
context.io(),
" {}: {}",
Expand Down Expand Up @@ -821,7 +821,7 @@ pub async fn query_shielded_balance<'a>(
// Here the user wants to know the balance for a specific token
(Some(base_token), true) => {
let tokens =
query_tokens(context, Some(&base_token), Some(&masp())).await;
query_tokens(context, Some(&base_token), Some(&MASP)).await;
for (token_alias, token) in tokens {
// Query the multi-asset balance at the given spending key
let viewing_key =
Expand Down Expand Up @@ -939,7 +939,7 @@ pub async fn query_shielded_balance<'a>(
}
for ((fvk, token), token_balance) in balance_map {
// Only assets with the current timestamp count
let alias = lookup_token_alias(context, &token, &masp()).await;
let alias = lookup_token_alias(context, &token, &MASP).await;
display_line!(context.io(), "Shielded Token {}:", alias);
let formatted =
context.format_amount(&token, token_balance.into()).await;
Expand Down Expand Up @@ -1066,7 +1066,7 @@ pub async fn print_decoded_balance<'a>(
display_line!(
context.io(),
"{} : {}",
lookup_token_alias(context, token_addr, &masp()).await,
lookup_token_alias(context, token_addr, &MASP).await,
context.format_amount(token_addr, (*amount).into()).await,
);
}
Expand Down Expand Up @@ -2335,7 +2335,7 @@ pub async fn query_conversions<'a>(
.wallet()
.await
.get_addresses_with_vp_type(AddressVpType::Token);
let masp_addr = masp();
let masp_addr = MASP;
let key_prefix: Key = masp_addr.to_db_key().into();
let state_key = key_prefix
.push(&(token::CONVERSION_KEY_PREFIX.to_owned()))
Expand Down
4 changes: 2 additions & 2 deletions apps/src/lib/config/genesis/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::str::FromStr;
use borsh::{BorshDeserialize, BorshSerialize};
use borsh_ext::BorshSerializeExt;
use namada::ledger::parameters::EpochDuration;
use namada::types::address::{masp, Address, EstablishedAddressGen};
use namada::types::address::{Address, EstablishedAddressGen, MASP};
use namada::types::chain::{ChainId, ChainIdPrefix};
use namada::types::dec::Dec;
use namada::types::hash::Hash;
Expand Down Expand Up @@ -410,7 +410,7 @@ impl Finalized {

pub fn get_user_address(&self, alias: &Alias) -> Option<Address> {
if alias.to_string() == *"masp" {
return Some(masp());
return Some(MASP);
}
let established = self.transactions.established_account.as_ref()?;
let validators = self.transactions.validator_account.as_ref()?;
Expand Down
7 changes: 1 addition & 6 deletions apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ where
.insert(alias, address.clone());
}
}
let key_prefix: Key = address::masp().to_db_key().into();
let key_prefix: Key = address::MASP.to_db_key().into();
// Save the current conversion state
let state_key = key_prefix
.push(&(token::CONVERSION_KEY_PREFIX.to_owned()))
Expand Down Expand Up @@ -355,11 +355,6 @@ where
genesis: &genesis::chain::Finalized,
vp_cache: &mut HashMap<String, Vec<u8>>,
) {
let vp_code = self.lookup_vp("vp_masp", genesis, vp_cache);
let code_hash = CodeHash::sha256(&vp_code);
self.wl_storage
.write_bytes(&Key::validity_predicate(&address::masp()), code_hash)
.unwrap();
if let Some(txs) = genesis.transactions.established_account.as_ref() {
for FinalizedEstablishedAccountTx {
address,
Expand Down
1 change: 1 addition & 0 deletions apps/src/lib/wallet/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ mod dev {
("christel".into(), christel_address()),
("daewon".into(), daewon_address()),
("ester".into(), ester_address()),
("masp".into(), namada::types::address::MASP),
]
.into_iter()
.collect();
Expand Down
142 changes: 139 additions & 3 deletions benches/native_vps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,29 @@ use namada::ledger::native_vp::ethereum_bridge::nut::NonUsableTokens;
use namada::ledger::native_vp::ethereum_bridge::vp::EthBridge;
use namada::ledger::native_vp::ibc::context::PseudoExecutionContext;
use namada::ledger::native_vp::ibc::Ibc;
use namada::ledger::native_vp::masp::MaspVp;
use namada::ledger::native_vp::multitoken::MultitokenVp;
use namada::ledger::native_vp::parameters::ParametersVp;
use namada::ledger::native_vp::{Ctx, NativeVp};
use namada::ledger::pgf::PgfVp;
use namada::ledger::pos::PosVP;
use namada::namada_sdk::masp::verify_shielded_tx;
use namada::namada_sdk::masp_primitives::transaction::Transaction;
use namada::proof_of_stake;
use namada::proof_of_stake::KeySeg;
use namada::proto::{Code, Section, Tx};
use namada::types::address::InternalAddress;
use namada::types::eth_bridge_pool::{GasFee, PendingTransfer};
use namada::types::masp::{TransferSource, TransferTarget};
use namada::types::storage::{Epoch, TxIndex};
use namada::types::transaction::governance::{
InitProposalData, VoteProposalData,
};
use namada_apps::bench_utils::{
generate_foreign_key_tx, BenchShell, TX_BRIDGE_POOL_WASM, TX_IBC_WASM,
TX_INIT_PROPOSAL_WASM, TX_RESIGN_STEWARD, TX_TRANSFER_WASM,
TX_UPDATE_STEWARD_COMMISSION, TX_VOTE_PROPOSAL_WASM,
generate_foreign_key_tx, BenchShell, BenchShieldedCtx,
ALBERT_PAYMENT_ADDRESS, ALBERT_SPENDING_KEY, BERTHA_PAYMENT_ADDRESS,
TX_BRIDGE_POOL_WASM, TX_IBC_WASM, TX_INIT_PROPOSAL_WASM, TX_RESIGN_STEWARD,
TX_TRANSFER_WASM, TX_UPDATE_STEWARD_COMMISSION, TX_VOTE_PROPOSAL_WASM,
};
use namada_apps::wallet::defaults;

Expand Down Expand Up @@ -464,6 +469,135 @@ fn vp_multitoken(c: &mut Criterion) {
}
}

// Generate and run masp transaction to be verified
fn setup_storage_for_masp_verification(
bench_name: &str,
) -> (BenchShieldedCtx, Tx) {
let amount = Amount::native_whole(500);
let mut shielded_ctx = BenchShieldedCtx::default();

let albert_spending_key = shielded_ctx
.wallet
.find_spending_key(ALBERT_SPENDING_KEY, None)
.unwrap()
.to_owned();
let albert_payment_addr = shielded_ctx
.wallet
.find_payment_addr(ALBERT_PAYMENT_ADDRESS)
.unwrap()
.to_owned();
let bertha_payment_addr = shielded_ctx
.wallet
.find_payment_addr(BERTHA_PAYMENT_ADDRESS)
.unwrap()
.to_owned();

// Shield some tokens for Albert
let shield_tx = shielded_ctx.generate_masp_tx(
amount,
TransferSource::Address(defaults::albert_address()),
TransferTarget::PaymentAddress(albert_payment_addr),
);
shielded_ctx.shell.execute_tx(&shield_tx);
shielded_ctx.shell.wl_storage.commit_tx();
shielded_ctx.shell.commit();

let signed_tx = match bench_name {
"shielding" => shielded_ctx.generate_masp_tx(
amount,
TransferSource::Address(defaults::albert_address()),
TransferTarget::PaymentAddress(albert_payment_addr),
),
"unshielding" => shielded_ctx.generate_masp_tx(
amount,
TransferSource::ExtendedSpendingKey(albert_spending_key),
TransferTarget::Address(defaults::albert_address()),
),
"shielded" => shielded_ctx.generate_masp_tx(
amount,
TransferSource::ExtendedSpendingKey(albert_spending_key),
TransferTarget::PaymentAddress(bertha_payment_addr),
),
_ => panic!("Unexpected bench test"),
};
shielded_ctx.shell.execute_tx(&signed_tx);

(shielded_ctx, signed_tx)
}

fn masp(c: &mut Criterion) {
let mut group = c.benchmark_group("vp_masp");

for bench_name in ["shielding", "unshielding", "shielded"] {
group.bench_function(bench_name, |b| {
let (shielded_ctx, signed_tx) =
setup_storage_for_masp_verification(bench_name);
let (verifiers, keys_changed) = shielded_ctx
.shell
.wl_storage
.write_log
.verifiers_and_changed_keys(&BTreeSet::default());

let masp = MaspVp {
ctx: Ctx::new(
&Address::Internal(InternalAddress::Masp),
&shielded_ctx.shell.wl_storage.storage,
&shielded_ctx.shell.wl_storage.write_log,
&signed_tx,
&TxIndex(0),
VpGasMeter::new_from_tx_meter(
&TxGasMeter::new_from_sub_limit(u64::MAX.into()),
),
&keys_changed,
&verifiers,
shielded_ctx.shell.vp_wasm_cache.clone(),
),
};

b.iter(|| {
assert!(
masp.validate_tx(
&signed_tx,
masp.ctx.keys_changed,
masp.ctx.verifiers,
)
.unwrap()
);
})
});
}

group.finish();
}

fn masp_verify_shielded_tx(c: &mut Criterion) {
let mut group = c.benchmark_group("vp_masp_verify_shielded_tx");

for bench_name in ["shielding", "unshielding", "shielded"] {
group.bench_function(bench_name, |b| {
let (_, signed_tx) =
setup_storage_for_masp_verification(bench_name);

let transaction = signed_tx
.sections
.into_iter()
.filter_map(|section| match section {
Section::MaspTx(transaction) => Some(transaction),
_ => None,
})
.collect::<Vec<Transaction>>()
.first()
.unwrap()
.to_owned();
b.iter(|| {
assert!(verify_shielded_tx(&transaction));
})
});
}

group.finish();
}

fn pgf(c: &mut Criterion) {
let mut group = c.benchmark_group("vp_pgf");

Expand Down Expand Up @@ -1146,6 +1280,8 @@ criterion_group!(
governance,
// slash_fund,
ibc,
masp,
masp_verify_shielded_tx,
vp_multitoken,
pgf,
eth_bridge_nut,
Expand Down
Loading
Loading