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

Shielded Inflation rewards #1025

Closed
wants to merge 52 commits into from
Closed

Shielded Inflation rewards #1025

wants to merge 52 commits into from

Conversation

mariari
Copy link
Member

@mariari mariari commented Jan 16, 2023

A followup pr of #714 that adds the inflation rewards for the shielded pool.

The formulas follow from:

https://specs.namada.net/economics/inflation-system.html

@mariari mariari force-pushed the mariari/inflation-rewards branch 2 times, most recently from cdfe93e to ea7bf48 Compare January 16, 2023 10:44
@mariari mariari marked this pull request as ready for review January 17, 2023 06:57
Comment on lines +1325 to +960
self.write(
&token::balance_key(addr, &masp_addr),
total_supply_in_masp.try_to_vec().unwrap(),
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this gets initialized on chain from the genesis config @brentstone @murisi would either of you know?

@mariari
Copy link
Member Author

mariari commented Jan 17, 2023

It seems that currently the e2e tests fail, I have to investigate why still

@mariari
Copy link
Member Author

mariari commented Jan 18, 2023

Currently 19 e2e tests fail:

ifailures:
    e2e::eth_bridge_tests::everything
    e2e::ibc_tests::run_ledger_ibc
    e2e::ledger_tests::double_signing_gets_slashed ; timeout
    e2e::ledger_tests::implicit_account_reveal_pk
    e2e::ledger_tests::invalid_transactions        ; timeout
    e2e::ledger_tests::ledger_many_txs_in_a_block
    e2e::ledger_tests::ledger_txs_and_queries

    ;; Must be updated to the new rewards
    ;; "Needle: NAM: 0.2", in the temp file "NAM: 467977.4\r\n"
    e2e::ledger_tests::masp_incentives

    e2e::ledger_tests::masp_pinned_txs       ; passes on rerun (timeout?)
    e2e::ledger_tests::masp_txs_and_queries  ; passes on rerun (timeout?)
    e2e::ledger_tests::pos_bonds
    e2e::ledger_tests::pos_init_validator
    e2e::ledger_tests::pos_rewards
    e2e::ledger_tests::proposal_offline

    ;; trying to connect: tcp connect error: OS error 111
    ;; Figure this might be due to a timeout somewhere
    e2e::ledger_tests::proposal_submission

    e2e::ledger_tests::run_ledger_load_state_and_reset
    e2e::ledger_tests::test_genesis_validators         ; timeout
    e2e::ledger_tests::test_node_connectivity_and_consensus
    e2e::wallet_tests::wallet_encrypted_key_cmds

Most of them are due to timeout, rerunning two of them locally has made it pass (likely again due to timeout). With two interesting e2e tests failing

the first is masp_incentives which should be updated to the new formula, however the contrast between the expected value and what was inflated seems a bit much. It goes from 0.2 NAM to 467977.4 NAM if I'm reading the tmp files correctly.

The other one is e2e::ledger_tests::proposal_submission which gives a tcp connection error, which may be due to timeouts on epoch, however that is unconfirmed.

Overall, I think the pr should be fit to merge outside of fine tuning masp_incentives, with timing issues handled in further prs

@tzemanovic tzemanovic changed the base branch from brent/pos-inflation-rewards to main January 18, 2023 10:14
@mariari
Copy link
Member Author

mariari commented Jan 30, 2023

pls update wasm

@mariari mariari force-pushed the mariari/inflation-rewards branch 3 times, most recently from 831a49a to 84343fd Compare February 9, 2023 03:49
Copy link
Contributor

@murisi murisi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes! Overall the code looks good (though I've reviewed only your changes, not @brentstone 's). I'm mainly concerned about two and a half things:

  • Is the inflation that is minted exactly equal to what is stored in token::last_inflation(addr)? If it is, then maybe we can make this equivalence clearer in the code... If not, we need to fix the discrepancy lest it subtly affects the PID controller.
  • The conversion tree code was written under the assumption that no rewards are given for shielded NAM (and hence that no compounding takes place). This was my mistake. Since your code can potentially give rewards for locked NAM (meaning that compounding necessarily happens), the conversion tree code needs to be updated.
  • Now that compounding of rewards can take place, we need to add an E2E test for this compounding behavior.

pub const LOCKED_RATIO_TARGET_KEY: &str = "locked_ratio_target";

/// The key for the max reward rate for a given asset
pub const MAX_REWARD_RATE: &str = "max_reward_rate";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could instead be named MAX_REWARD_RATE_KEY lest this variable be mistaken for an actual rate. This doesn't matter too much though since the variable's meaning should be clear from context.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @murisi here

Suggested change
pub const MAX_REWARD_RATE: &str = "max_reward_rate";
pub const MAX_REWARD_RATE_KEY: &str = "max_reward_rate";

@@ -63,7 +167,9 @@ where
// Conversions from the previous to current asset for each address
let mut current_convs = BTreeMap::<Address, AllowedConversion>::new();
// Reward all tokens according to above reward rates
for (addr, reward) in &masp_rewards {
for addr in masp_rewards.keys() {
let reward = calculate_masp_rewards(wl_storage, addr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following code assumes that the reward for locked NAM is 0, i.e. it assumes that compounding doesn't happen. What follows would need to be modified to correctly handle NAM compounding. Something along the lines of deflating NAM_0 rewards, and not handing out NAM_0 rewards for NAM. Probably a test should be added to ensure that compounding happens correctly.

}
}

impl Default for Parameters {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a better default would be to have 0 rewards and 0 locked ratio target? I.e. behave like there is no incentive system by default. But I guess this is debatable...

/// Shielded Pool nominal proportional gain for the given token
kp_gain_nom: Decimal,
/// Locked ratio for the given token
locked_ratio_target_key: Decimal,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you instead mean locked_ratio_target? This variable doesn't seem to be a key...

@@ -72,7 +178,7 @@ where
// reward.0 units of the reward token
// Since floor(a) + floor(b) <= floor(a+b), there will always be
// enough rewards to reimburse users
total_reward += (addr_bal * *reward).0;
total_reward += (addr_bal * reward).0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that the right-hand-side, (addr_bal * reward).0, equals inflation as computed in calculate_masp_rewards. By the implementation of Mul for Amount, (addr_bal * reward).0=(addr_bal / reward.1)*reward.0=(addr_bal / 100)*(inflation * 100 / total_token_in_masp)=(addr_bal / 100)*(inflation * 100 / addr_bal). Algebraically this should equal inflation, but I suspect the first factor alone, (addr_bal / 100), will introduce rounding errors.

Overall it feels like we are recomputing a value that we already knew/computed before, but in a way that is vulnerable to overflows and rounding errors. Maybe we could move the mint_tokens call from this function into calculate_masp_rewards (where the inflation variable is available)? (Whatever we do with rounding errors, the MASP must always have exactly, or slightly more than enough account balance to redeem shielded conversions/withdrawals...)

@mariari mariari force-pushed the mariari/inflation-rewards branch 4 times, most recently from 13d918e to 8bf245e Compare February 22, 2023 11:17
@mariari mariari force-pushed the mariari/inflation-rewards branch from e5d165e to 9ea97ef Compare June 5, 2023 03:26
@mariari mariari force-pushed the mariari/inflation-rewards branch from 9ea97ef to 38bb6bb Compare June 5, 2023 03:31
@mariari
Copy link
Member Author

mariari commented Jun 5, 2023

Last commit before merging v0.16.0. Note that the e2e incetives fails, but everything else works

@mariari mariari force-pushed the mariari/inflation-rewards branch 4 times, most recently from 9bb003e to a46b699 Compare June 12, 2023 06:07
@mariari
Copy link
Member Author

mariari commented Jun 30, 2023

pls update wasms

@mariari mariari force-pushed the mariari/inflation-rewards branch 5 times, most recently from 42998e2 to 31930e4 Compare July 5, 2023 15:45
- _Changes_
  + We offset the denominations of the tokens by their denomination
    relative to the nam token thus a 10^18 token gets offset to 10^6,
    which is the base systems denomination
@mariari mariari force-pushed the mariari/inflation-rewards branch from 31930e4 to 7b3442b Compare July 6, 2023 12:53
@tzemanovic tzemanovic mentioned this pull request Jul 18, 2023
# Portion of a validator's stake that should be slashed on a light
# client attack.
light_client_attack_min_slash_rate = "0.001"
light_client_attack_min_slash_rate = "0.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why has this been changed in this PR? Not sure I follow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants