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 conversions doesn't require all tokens be in genesis #2285

Merged
merged 3 commits into from
Dec 29, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- Previously, a hardcoded set of tokens were expected to be used in Masp conversions.
If these tokens did not have configs in genesis, this would lead to a panic after the first
epoch change. This PR fixes this to use the tokens found in genesis belonging to the MASP
rewards whitelist instead of hardcoding the tokens.
([\#2285](https://github.com/anoma/namada/pull/2285))
27 changes: 27 additions & 0 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ pub mod cmds {
.subcommand(QueryAccount::def().display_order(5))
.subcommand(QueryTransfers::def().display_order(5))
.subcommand(QueryConversions::def().display_order(5))
.subcommand(QueryMaspRewardTokens::def().display_order(5))
.subcommand(QueryBlock::def().display_order(5))
.subcommand(QueryBalance::def().display_order(5))
.subcommand(QueryBonds::def().display_order(5))
Expand Down Expand Up @@ -313,6 +314,8 @@ pub mod cmds {
let query_transfers = Self::parse_with_ctx(matches, QueryTransfers);
let query_conversions =
Self::parse_with_ctx(matches, QueryConversions);
let query_masp_reward_tokens =
Self::parse_with_ctx(matches, QueryMaspRewardTokens);
let query_block = Self::parse_with_ctx(matches, QueryBlock);
let query_balance = Self::parse_with_ctx(matches, QueryBalance);
let query_bonds = Self::parse_with_ctx(matches, QueryBonds);
Expand Down Expand Up @@ -370,6 +373,7 @@ pub mod cmds {
.or(query_epoch)
.or(query_transfers)
.or(query_conversions)
.or(query_masp_reward_tokens)
.or(query_block)
.or(query_balance)
.or(query_bonds)
Expand Down Expand Up @@ -456,6 +460,7 @@ pub mod cmds {
QueryAccount(QueryAccount),
QueryTransfers(QueryTransfers),
QueryConversions(QueryConversions),
QueryMaspRewardTokens(QueryMaspRewardTokens),
QueryBlock(QueryBlock),
QueryBalance(QueryBalance),
QueryBonds(QueryBonds),
Expand Down Expand Up @@ -1670,6 +1675,28 @@ pub mod cmds {
}
}

#[derive(Clone, Debug)]
pub struct QueryMaspRewardTokens(pub args::Query<args::CliTypes>);

impl SubCmd for QueryMaspRewardTokens {
const CMD: &'static str = "masp-reward-tokens";

fn parse(matches: &ArgMatches) -> Option<Self> {
matches.subcommand_matches(Self::CMD).map(|matches| {
QueryMaspRewardTokens(args::Query::parse(matches))
})
}

fn def() -> App {
App::new(Self::CMD)
.about(
"Query the tokens which can earn MASP rewards while \
shielded.",
)
.add_args::<args::Query<args::CliTypes>>()
}
}

#[derive(Clone, Debug)]
pub struct QueryBlock(pub args::Query<args::CliTypes>);

Expand Down
10 changes: 10 additions & 0 deletions apps/src/lib/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ impl CliApi {
let namada = ctx.to_sdk(client, io);
rpc::query_conversions(&namada, args).await;
}
Sub::QueryMaspRewardTokens(QueryMaspRewardTokens(
mut args,
)) => {
let client = client.unwrap_or_else(|| {
C::from_tendermint_address(&mut args.ledger_address)
});
client.wait_until_node_is_synced(&io).await?;
let namada = ctx.to_sdk(client, io);
rpc::query_masp_reward_tokens(&namada).await;
}
Sub::QueryBlock(QueryBlock(mut args)) => {
let client = client.unwrap_or_else(|| {
C::from_tendermint_address(&mut args.ledger_address)
Expand Down
11 changes: 11 additions & 0 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2428,6 +2428,17 @@ pub async fn query_conversion<C: namada::ledger::queries::Client + Sync>(
namada_sdk::rpc::query_conversion(client, asset_type).await
}

/// Query to read the tokens that earn masp rewards.
pub async fn query_masp_reward_tokens(context: &impl Namada) {
let tokens = namada_sdk::rpc::query_masp_reward_tokens(context.client())
.await
.expect("The tokens that may earn MASP rewards should be defined");
display_line!(context.io(), "The following tokens may ear MASP rewards:");
for (alias, address) in tokens {
display_line!(context.io(), "{}: {}", alias, address);
}
}

/// Query a wasm code hash
pub async fn query_wasm_code_hash(
context: &impl Namada,
Expand Down
26 changes: 6 additions & 20 deletions core/src/ledger/masp_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,29 +214,15 @@ where
};
use rayon::prelude::ParallelSlice;

use crate::types::address;

// The derived conversions will be placed in MASP address space
let masp_addr = MASP;

let tokens = address::tokens();
let mut masp_reward_keys: Vec<_> = tokens
.into_keys()
.map(|k| {
wl_storage
.storage
.conversion_state
.tokens
.get(k)
.unwrap_or_else(|| {
panic!(
"Could not find token alias {} in MASP conversion \
state.",
k
)
})
.clone()
})
let mut masp_reward_keys: Vec<_> = wl_storage
.storage
.conversion_state
.tokens
.values()
.cloned()
.collect();
// Put the native rewards first because other inflation computations depend
// on it
Expand Down
15 changes: 15 additions & 0 deletions sdk/src/queries/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ router! {SHELL,
// Conversion state access - read conversion
( "conversions" ) -> BTreeMap<AssetType, ConversionWithoutPath> = read_conversions,


// Conversion state access - read conversion
( "masp_reward_tokens" ) -> BTreeMap<String, Address> = masp_reward_tokens,

// Block results access - read bit-vec
( "results" ) -> Vec<BlockResults> = read_results,

Expand Down Expand Up @@ -210,6 +214,17 @@ where
}
}

/// Query to read the tokens that earn masp rewards.
fn masp_reward_tokens<D, H, V, T>(
ctx: RequestCtx<'_, D, H, V, T>,
) -> storage_api::Result<BTreeMap<String, Address>>
where
D: 'static + DB + for<'iter> DBIter<'iter> + Sync,
H: 'static + StorageHasher + Sync,
{
Ok(ctx.wl_storage.storage.conversion_state.tokens.clone())
}

fn epoch<D, H, V, T>(
ctx: RequestCtx<'_, D, H, V, T>,
) -> storage_api::Result<Epoch>
Expand Down
7 changes: 7 additions & 0 deletions sdk/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ pub async fn query_conversions<C: crate::queries::Client + Sync>(
convert_response::<C, _>(RPC.shell().read_conversions(client).await)
}

/// Query to read the tokens that earn masp rewards.
pub async fn query_masp_reward_tokens<C: crate::queries::Client + Sync>(
client: &C,
) -> Result<BTreeMap<String, Address>, Error> {
convert_response::<C, _>(RPC.shell().masp_reward_tokens(client).await)
}

/// Query a wasm code hash
pub async fn query_wasm_code_hash(
context: &impl Namada,
Expand Down
Loading