Skip to content

Commit

Permalink
Merge branch 'bat/fix/masp-rewards-genesis' (#2285)
Browse files Browse the repository at this point in the history
* origin/bat/fix/masp-rewards-genesis:
  [feat]: Added an rpc endpoint to query which tokens can earn MASP rewards while shielded
  [chore]: Add changelog
  [fix]: Masp conversions no longer requires all tokens be present in genesis
  • Loading branch information
Gianmarco Fraccaroli committed Dec 27, 2023
2 parents 20e89b1 + ccf9a86 commit 6fff25c
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 19 deletions.
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 @@ -1472,6 +1477,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 @@ -2413,6 +2413,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
25 changes: 6 additions & 19 deletions core/src/ledger/masp_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,31 +212,18 @@ where
};
use rayon::prelude::ParallelSlice;

use crate::types::address;
use crate::types::storage::{Key, KeySeg};
use crate::types::token::MASP_CONVERT_ANCHOR_KEY;

// 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 @@ -285,6 +285,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

0 comments on commit 6fff25c

Please sign in to comment.