Skip to content

Commit

Permalink
staking: simplify deposit api
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanor committed Feb 6, 2025
1 parent c1125ba commit 3a44f29
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use anyhow::bail;
use async_trait::async_trait;
use cnidarium::StateWrite;
use penumbra_sdk_asset::Value;
use penumbra_sdk_dex::component::PositionManager as _;
use penumbra_sdk_dex::lp::position;
use penumbra_sdk_keys::Address;
use penumbra_sdk_num::Amount;
use penumbra_sdk_sct::component::clock::EpochRead as _;
use penumbra_sdk_sct::CommitmentSource;
use penumbra_sdk_shielded_pool::component::NoteManager as _;
use penumbra_sdk_stake::component::ValidatorPoolDeposit;
use penumbra_sdk_stake::IdentityKey;

use penumbra_sdk_txhash::TransactionId;

#[async_trait]
Expand All @@ -22,21 +23,20 @@ pub trait Bank: StateWrite + Sized {
/// Move a fraction of our issuance budget towards an address, by minting a note.
async fn reward_to_voter(
&mut self,
unbonded_reward: Value,
unbonded_reward: Amount,
validator: IdentityKey,
voter: &Address,
tx_hash: TransactionId,
) -> anyhow::Result<()> {
if unbonded_reward.amount == Amount::zero() {
if unbonded_reward == Amount::zero() {
return Ok(());
}
let epoch = self
.get_current_epoch()
.await
.expect("should be able to read current epoch");

use penumbra_sdk_stake::component::ValidatorPoolDeposit;
let Some((_, bonded_reward)) = self
let Some(bonded_reward) = self
.deposit_to_validator_pool(&validator, unbonded_reward)
.await
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,7 @@ pub async fn distribute_rewards(mut state: impl StateWrite + Sized) -> anyhow::R
// It is the responsibility of the `Bank::reward_to_voter` implementation to
// decide the modalities of how and in what form the reward is minted.
state
.reward_to_voter(
Value {
asset_id: *STAKING_TOKEN_ASSET_ID,
amount: unbonded_reward_amount,
},
identity_key,
&voter,
tx,
)
.reward_to_voter(unbonded_reward_amount, identity_key, &voter, tx)
.await?;
current_budget = current_budget
.checked_sub(&unbonded_reward_amount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use async_trait::async_trait;
use cnidarium::{StateRead, StateWrite};
use futures::{Future, FutureExt};
use penumbra_sdk_asset::Value;
use penumbra_sdk_asset::STAKING_TOKEN_ASSET_ID;
use penumbra_sdk_num::Amount;
use penumbra_sdk_proto::{
state::future::DomainFuture, DomainType, StateReadProto, StateWriteProto,
Expand Down Expand Up @@ -238,32 +237,25 @@ impl<T: StateRead + ?Sized> ValidatorDataRead for T {}

#[async_trait]
pub trait ValidatorPoolDeposit: StateWrite {
/// Checked increase of the validator pool size by the given amount.
/// Checked increase of the validator pool size by the given amount
/// of staking tokens (unbonded).
///
/// On success, this method returns a tuple consisting of:
/// - the new delegation pool size (measured in staking tokens)
/// - the bonded value of the deposit (measured in delegation tokens)
/// On success, this method returns the bonded value
/// of the deposit, i.e, measured in delegation tokens.
///
/// Returns `None` if the update failed.
async fn deposit_to_validator_pool(
&mut self,
validator_ik: &IdentityKey,
deposit: Value,
) -> Option<(Amount, Value)> {
unbonded_deposit: Amount,
) -> Option<Value> {
let state_path = state_key::validators::pool::balance::by_id(validator_ik);
let old_supply = self
.get(&state_path)
.await
.expect("no deserialization error expected")
.unwrap_or(Amount::zero());

let unbonded_deposit = if deposit.asset_id == *STAKING_TOKEN_ASSET_ID {
deposit.amount
} else {
tracing::warn!("depositing non-staking tokens into validator pool");
return None;
};

tracing::debug!(validator_identity = %validator_ik, ?unbonded_deposit, ?old_supply, "depositing into validator pool");

// Simulate increasing the validator pool size, backing off on any error.
Expand Down Expand Up @@ -298,7 +290,7 @@ pub trait ValidatorPoolDeposit: StateWrite {
// Finally, perform the necessary state update:
self.put(state_path, new_supply);

Some((new_supply, bonded_deposit))
Some(bonded_deposit)
}
}

Expand Down

0 comments on commit 3a44f29

Please sign in to comment.