Skip to content

Commit ce20a15

Browse files
committed
Increased the precision of MASP rewards.
1 parent a9964d6 commit ce20a15

35 files changed

Lines changed: 149 additions & 262 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ libc = "0.2.97"
9090
libloading = "0.7.2"
9191
libsecp256k1 = {git = "https://github.com/heliaxdev/libsecp256k1", rev = "bbb3bd44a49db361f21d9db80f9a087c194c0ae9", default-features = false, features = ["std", "static-context"]}
9292
# branch = "murisi/namada-integration"
93-
masp_primitives = { git = "https://github.com/anoma/masp", rev = "50acc5028fbcd52a05970fe7991c7850ab04358e" }
94-
masp_proofs = { git = "https://github.com/anoma/masp", rev = "50acc5028fbcd52a05970fe7991c7850ab04358e", default-features = false, features = ["local-prover"] }
93+
masp_primitives = { git = "https://github.com/anoma/masp", rev = "1345b463e8fa3b3a6fa13e4a43fb1c410690ad62" }
94+
masp_proofs = { git = "https://github.com/anoma/masp", rev = "1345b463e8fa3b3a6fa13e4a43fb1c410690ad62", default-features = false, features = ["local-prover"] }
9595
num256 = "0.3.5"
9696
num_cpus = "1.13.0"
9797
num-derive = "0.3.3"

apps/src/lib/client/rpc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ pub async fn query_conversions<
21262126
// Track whether any non-sentinel conversions are found
21272127
let mut conversions_found = false;
21282128
for ((addr, _), epoch, conv, _) in conv_state.assets.values() {
2129-
let amt: masp_primitives::transaction::components::I32Sum =
2129+
let amt: masp_primitives::transaction::components::I128Sum =
21302130
conv.clone().into();
21312131
// If the user has specified any targets, then meet them
21322132
// If we have a sentinel conversion, then skip printing
@@ -2181,7 +2181,7 @@ pub async fn query_conversion<C: namada::ledger::queries::Client + Sync>(
21812181
Address,
21822182
MaspDenom,
21832183
Epoch,
2184-
masp_primitives::transaction::components::I32Sum,
2184+
masp_primitives::transaction::components::I128Sum,
21852185
MerklePath<Node>,
21862186
)> {
21872187
namada::sdk::rpc::query_conversion(client, asset_type).await

apps/src/lib/config/genesis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,8 @@ pub fn genesis(num_validators: u64) -> Genesis {
10121012
implicit_vp_code_path: vp_implicit_path.into(),
10131013
implicit_vp_sha256: Default::default(),
10141014
max_signatures_per_transaction: 15,
1015-
epochs_per_year: 525_600, /* seconds in yr (60*60*24*365) div seconds
1016-
* per epoch (60 = min_duration) */
1015+
epochs_per_year: 365, /* seconds in yr (60*60*24*365) div seconds
1016+
* per epoch (60 = min_duration) */
10171017
pos_gain_p: Dec::new(1, 1).expect("This can't fail"),
10181018
pos_gain_d: Dec::new(1, 1).expect("This can't fail"),
10191019
staked_ratio: Dec::zero(),

apps/src/lib/node/ledger/shell/finalize_block.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,8 @@ where
618618
/// with respect to the previous epoch.
619619
fn apply_inflation(&mut self, current_epoch: Epoch) -> Result<()> {
620620
let last_epoch = current_epoch.prev();
621-
// Get input values needed for the PD controller for PoS and MASP.
621+
// Get input values needed for the PD controller for PoS.
622622
// Run the PD controllers to calculate new rates.
623-
//
624-
// MASP is included below just for some completeness.
625623

626624
let params = read_pos_params(&self.wl_storage)?;
627625

@@ -653,15 +651,6 @@ where
653651
let pos_locked_ratio_target = params.target_staked_ratio;
654652
let pos_max_inflation_rate = params.max_inflation_rate;
655653

656-
// TODO: properly fetch these values (arbitrary for now)
657-
let masp_locked_supply: Amount = Amount::default();
658-
let masp_locked_ratio_target = Dec::new(5, 1).expect("Cannot fail");
659-
let masp_locked_ratio_last = Dec::new(5, 1).expect("Cannot fail");
660-
let masp_max_inflation_rate = Dec::new(2, 1).expect("Cannot fail");
661-
let masp_last_inflation_rate = Dec::new(12, 2).expect("Cannot fail");
662-
let masp_p_gain = Dec::new(1, 1).expect("Cannot fail");
663-
let masp_d_gain = Dec::new(1, 1).expect("Cannot fail");
664-
665654
// Run rewards PD controller
666655
let pos_controller = inflation::RewardsController {
667656
locked_tokens: pos_locked_supply,
@@ -675,27 +664,12 @@ where
675664
d_gain_nom: pos_d_gain_nom,
676665
epochs_per_year,
677666
};
678-
let _masp_controller = inflation::RewardsController {
679-
locked_tokens: masp_locked_supply,
680-
total_tokens,
681-
total_native_tokens: total_tokens,
682-
locked_ratio_target: masp_locked_ratio_target,
683-
locked_ratio_last: masp_locked_ratio_last,
684-
max_reward_rate: masp_max_inflation_rate,
685-
last_inflation_amount: token::Amount::from(
686-
masp_last_inflation_rate,
687-
),
688-
p_gain_nom: masp_p_gain,
689-
d_gain_nom: masp_d_gain,
690-
epochs_per_year,
691-
};
692667

693668
// Run the rewards controllers
694669
let inflation::ValsToUpdate {
695670
locked_ratio,
696671
inflation,
697672
} = pos_controller.run();
698-
// let new_masp_vals = _masp_controller.run();
699673

700674
// Get the number of blocks in the last epoch
701675
let first_block_of_last_epoch = self

apps/src/lib/node/ledger/shell/init_chain.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,14 @@ where
338338
// Init token parameters and last inflation and caching rates
339339
parameters.init_storage(&address, &mut self.wl_storage);
340340
self.wl_storage
341-
.write(&token::masp_last_inflation(&address), last_inflation)
341+
.write(
342+
&token::masp_last_inflation_key(&address),
343+
last_inflation,
344+
)
342345
.unwrap();
343346
self.wl_storage
344347
.write(
345-
&token::masp_last_locked_ratio(&address),
348+
&token::masp_last_locked_ratio_key(&address),
346349
last_locked_ratio,
347350
)
348351
.unwrap();

core/src/ledger/storage/masp_conversions.rs

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,19 @@ use masp_primitives::sapling::Node;
1010

1111
use crate::ledger::inflation::{RewardsController, ValsToUpdate};
1212
use crate::ledger::parameters;
13+
use crate::ledger::storage_api::token::read_denom;
1314
use crate::ledger::storage_api::{StorageRead, StorageWrite};
1415
use crate::types::address::Address;
1516
use crate::types::dec::Dec;
1617
use crate::types::storage::Epoch;
1718
use crate::types::token::MaspDenom;
1819
use crate::types::{address, token};
1920

20-
/// Inflation is implicitly denominated by this value. The lower this figure,
21-
/// the less precise inflation computations are. The higher this figure, the
22-
/// larger the fixed-width types that are required to carry out inflation
23-
/// computations. This value should be fixed constant for each asset type - here
24-
/// we have simplified it and made it constant across asset types.
25-
const PRECISION: u64 = 100;
26-
2721
/// A representation of the conversion state
2822
#[derive(Debug, Default, BorshSerialize, BorshDeserialize)]
2923
pub struct ConversionState {
3024
/// The last amount of the native token distributed
31-
pub normed_inflation: Option<u32>,
25+
pub normed_inflation: Option<u128>,
3226
/// The tree currently containing all the conversions
3327
pub tree: FrozenCommitmentTree<Node>,
3428
/// Map assets to their latest conversion and position in Merkle tree
@@ -44,11 +38,20 @@ pub struct ConversionState {
4438
pub fn calculate_masp_rewards<D, H>(
4539
wl_storage: &mut super::WlStorage<D, H>,
4640
addr: &Address,
47-
) -> crate::ledger::storage_api::Result<(u32, u32)>
41+
) -> crate::ledger::storage_api::Result<(u128, u128)>
4842
where
4943
D: 'static + super::DB + for<'iter> super::DBIter<'iter>,
5044
H: 'static + super::StorageHasher,
5145
{
46+
let denomination = read_denom(wl_storage, addr).unwrap().unwrap();
47+
// Inflation is implicitly denominated by this value. The lower this
48+
// figure, the less precise inflation computations are. This is especially
49+
// problematic when inflation is coming from a token with much higher
50+
// denomination than the native token. The higher this figure, the higher
51+
// the threshold of holdings required in order to receive non-zero rewards.
52+
// This value should be fixed constant for each asset type.
53+
let precision = 10u128.pow(std::cmp::max(u32::from(denomination.0), 3) - 3);
54+
5255
let masp_addr = address::masp();
5356
// Query the storage for information
5457

@@ -69,38 +72,33 @@ where
6972

7073
let epochs_per_year: u64 = wl_storage
7174
.read(&parameters::storage::get_epochs_per_year_key())?
72-
.expect("");
75+
.expect("epochs per year should properly decode");
7376

7477
//// Values from the last epoch
7578
let last_inflation: token::Amount = wl_storage
76-
.read(&token::masp_last_inflation(addr))
77-
.expect("failure to read last inflation")
78-
.expect("");
79+
.read(&token::masp_last_inflation_key(addr))?
80+
.expect("failure to read last inflation");
7981

8082
let last_locked_ratio: Dec = wl_storage
81-
.read(&token::masp_last_locked_ratio(addr))
82-
.expect("failure to read last inflation")
83-
.expect("");
83+
.read(&token::masp_last_locked_ratio_key(addr))?
84+
.expect("failure to read last inflation");
8485

8586
//// Parameters for each token
8687
let max_reward_rate: Dec = wl_storage
87-
.read(&token::masp_max_reward_rate(addr))
88-
.expect("max reward should properly decode")
89-
.expect("");
88+
.read(&token::masp_max_reward_rate_key(addr))?
89+
.expect("max reward should properly decode");
9090

9191
let kp_gain_nom: Dec = wl_storage
92-
.read(&token::masp_kp_gain(addr))
93-
.expect("kp_gain_nom reward should properly decode")
94-
.expect("");
92+
.read(&token::masp_kp_gain_key(addr))?
93+
.expect("kp_gain_nom reward should properly decode");
9594

9695
let kd_gain_nom: Dec = wl_storage
97-
.read(&token::masp_kd_gain(addr))
98-
.expect("kd_gain_nom reward should properly decode")
99-
.expect("");
96+
.read(&token::masp_kd_gain_key(addr))?
97+
.expect("kd_gain_nom reward should properly decode");
10098

10199
let locked_target_ratio: Dec = wl_storage
102-
.read(&token::masp_locked_ratio_target(addr))?
103-
.expect("");
100+
.read(&token::masp_locked_ratio_target_key(addr))?
101+
.expect("locked ratio target should properly decode");
104102

105103
// Creating the PD controller for handing out tokens
106104
let controller = RewardsController {
@@ -126,10 +124,11 @@ where
126124
// Since we must put the notes in a compatible format with the
127125
// note format, we must make the inflation amount discrete.
128126
let noterized_inflation = if total_token_in_masp.is_zero() {
129-
0u32
127+
0u128
130128
} else {
131129
crate::types::uint::Uint::try_into(
132-
(inflation.raw_amount() * PRECISION)
130+
(inflation.raw_amount()
131+
* crate::types::uint::Uint::from(precision))
133132
/ total_token_in_masp.raw_amount(),
134133
)
135134
.unwrap()
@@ -163,24 +162,21 @@ where
163162
// otherwise we will have an inaccurate view of inflation
164163
wl_storage
165164
.write(
166-
&token::masp_last_inflation(addr),
167-
(total_token_in_masp / PRECISION) * u64::from(noterized_inflation),
165+
&token::masp_last_inflation_key(addr),
166+
token::Amount::from_uint(
167+
(total_token_in_masp.raw_amount() / precision)
168+
* crate::types::uint::Uint::from(noterized_inflation),
169+
0,
170+
)
171+
.unwrap(),
168172
)
169173
.expect("unable to encode new inflation rate (Decimal)");
170174

171175
wl_storage
172-
.write(&token::masp_last_locked_ratio(addr), locked_ratio)
176+
.write(&token::masp_last_locked_ratio_key(addr), locked_ratio)
173177
.expect("unable to encode new locked ratio (Decimal)");
174178

175-
// to make it conform with the expected output, we need to
176-
// move it to a ratio of x/100 to match the masp_rewards
177-
// function This may be unneeded, as we could describe it as a
178-
// ratio of x/1
179-
180-
Ok((
181-
noterized_inflation,
182-
PRECISION.try_into().expect("inflation precision too large"),
183-
))
179+
Ok((noterized_inflation, precision))
184180
}
185181

186182
// This is only enabled when "wasm-runtime" is on, because we're using rayon
@@ -196,7 +192,7 @@ where
196192
use std::cmp::Ordering;
197193

198194
use masp_primitives::ff::PrimeField;
199-
use masp_primitives::transaction::components::I32Sum as MaspAmount;
195+
use masp_primitives::transaction::components::I128Sum as MaspAmount;
200196
use rayon::iter::{
201197
IndexedParallelIterator, IntoParallelIterator, ParallelIterator,
202198
};
@@ -288,12 +284,12 @@ where
288284
(addr.clone(), denom),
289285
(MaspAmount::from_pair(
290286
old_asset,
291-
-(*normed_inflation as i32),
287+
-(*normed_inflation as i128),
292288
)
293289
.unwrap()
294290
+ MaspAmount::from_pair(
295291
new_asset,
296-
new_normed_inflation as i32,
292+
new_normed_inflation as i128,
297293
)
298294
.unwrap())
299295
.into(),
@@ -320,13 +316,13 @@ where
320316
// intermediate tokens cancel/ telescope out
321317
current_convs.insert(
322318
(addr.clone(), denom),
323-
(MaspAmount::from_pair(old_asset, -(reward.1 as i32))
319+
(MaspAmount::from_pair(old_asset, -(reward.1 as i128))
324320
.unwrap()
325-
+ MaspAmount::from_pair(new_asset, reward.1 as i32)
321+
+ MaspAmount::from_pair(new_asset, reward.1 as i128)
326322
.unwrap()
327323
+ MaspAmount::from_pair(
328324
reward_assets[denom as usize],
329-
real_reward as i32,
325+
real_reward as i128,
330326
)
331327
.unwrap())
332328
.into(),

core/src/types/address.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -681,23 +681,6 @@ pub fn tokens() -> HashMap<Address, (&'static str, Denomination)> {
681681
.collect()
682682
}
683683

684-
/// Temporary helper for testing, a hash map of tokens addresses with their
685-
/// MASP XAN incentive schedules. If the reward is (a, b) then a rewarded tokens
686-
/// are dispensed for every b possessed tokens.
687-
pub fn masp_rewards() -> HashMap<Address, (u32, u32)> {
688-
vec![
689-
(nam(), (0, 100)),
690-
(btc(), (1, 100)),
691-
(eth(), (2, 100)),
692-
(dot(), (3, 100)),
693-
(schnitzel(), (4, 100)),
694-
(apfel(), (5, 100)),
695-
(kartoffel(), (6, 100)),
696-
]
697-
.into_iter()
698-
.collect()
699-
}
700-
701684
#[cfg(test)]
702685
pub mod tests {
703686
use proptest::prelude::*;

0 commit comments

Comments
 (0)