Skip to content

Commit

Permalink
PoS: update ClaimRewards tx action
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Feb 27, 2025
1 parent 54d1162 commit 0ac1938
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 34 deletions.
7 changes: 5 additions & 2 deletions crates/proof_of_stake/src/vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ where
// The key is src bond ID and value is pair of (dest_validator, amount)
let mut redelegations: BTreeMap<BondId, (Address, token::Amount)> =
Default::default();
let mut claimed_rewards: BTreeSet<BondId> = Default::default();
// The value is an optional rewards receiver
let mut claimed_rewards: BTreeMap<BondId, Option<Address>> =
Default::default();
let mut changed_commission: BTreeSet<Address> = Default::default();
let mut changed_metadata: BTreeSet<Address> = Default::default();
let mut changed_consensus_key: BTreeSet<Address> = Default::default();
Expand Down Expand Up @@ -241,6 +243,7 @@ where
PosAction::ClaimRewards(ClaimRewards {
validator,
source,
receiver,
}) => {
let bond_id = BondId {
source: source.unwrap_or_else(|| validator.clone()),
Expand All @@ -256,7 +259,7 @@ where
)
.into());
}
claimed_rewards.insert(bond_id);
claimed_rewards.insert(bond_id, receiver);
}
PosAction::CommissionChange(validator) => {
if !verifiers.contains(&validator) {
Expand Down
7 changes: 6 additions & 1 deletion crates/trans_token/src/vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@ where
fn has_bal_inc_protocol_action(action: &Action, owner: Owner<'_>) -> bool {
match action {
Action::Pos(
PosAction::ClaimRewards(ClaimRewards { validator, source })
PosAction::ClaimRewards(ClaimRewards {
validator,
source,
receiver: _,
})
| PosAction::Withdraw(Withdraw { validator, source }),
) => match owner {
Owner::Account(owner) => {
Expand Down Expand Up @@ -1099,6 +1103,7 @@ mod tests {
.push_action(Action::Pos(PosAction::ClaimRewards(ClaimRewards {
validator: established_address_1(),
source: None,
receiver: None,
})))
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion crates/tx/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use namada_core::storage::KeySeg;
use namada_core::{address, storage};

pub use crate::data::pos::{
Bond, ClaimRewardsCompat as ClaimRewards, Redelegation, Unbond, Withdraw,
Bond, ClaimRewards, Redelegation, Unbond, Withdraw,
};

/// Actions applied from txs.
Expand Down
15 changes: 0 additions & 15 deletions crates/tx/src/data/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,6 @@ pub struct ClaimRewards {
pub receiver: Option<Address>,
}

/// Compatibility data definition from previous version (before addition of a
/// receiver field).
///
/// TODO: Temporarily replaces `crate::data::pos::ClaimRewards` that now
/// contains an additional `receiver` field to maintain consensus compatibility.
/// PoS VP reads this action and will need to be updated to use the new field.
#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, PartialEq)]
pub struct ClaimRewardsCompat {
/// Validator address
pub validator: Address,
/// Source address for claiming rewards from a bond. For self-bonds, the
/// validator is also the source
pub source: Option<Address>,
}

/// A redelegation of bonded tokens from one validator to another.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(
Expand Down
1 change: 1 addition & 0 deletions crates/tx_prelude/src/proof_of_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ impl Ctx {
self.push_action(Action::Pos(PosAction::ClaimRewards(ClaimRewards {
validator: validator.clone(),
source: source.cloned(),
receiver: receiver.cloned(),
})))?;

let current_epoch = self.get_block_epoch()?;
Expand Down
14 changes: 3 additions & 11 deletions wasm/tx_claim_rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@ use namada_tx_prelude::*;
#[transaction]
fn apply_tx(ctx: &mut Ctx, tx_data: BatchedTx) -> TxResult {
let data = ctx.get_tx_data(&tx_data)?;
let (validator, source, receiver) = if let Ok(pos::ClaimRewards {
let pos::ClaimRewards {
validator,
source,
receiver,
}) =
pos::ClaimRewards::try_from_slice(&data[..])
{
(validator, source, receiver)
} else {
let pos::ClaimRewardsCompat { validator, source } =
pos::ClaimRewardsCompat::try_from_slice(&data[..])
.wrap_err("Failed to decode ClaimRewards value")?;
(validator, source, None)
};
} = pos::ClaimRewards::try_from_slice(&data[..])
.wrap_err("Failed to decode ClaimRewards value")?;
ctx.claim_reward_tokens(source.as_ref(), &validator, receiver.as_ref())
.wrap_err("Failed to claim rewards")?;

Expand Down
7 changes: 5 additions & 2 deletions wasm/vp_implicit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ fn validate_tx(
source, validator, ..
})
| PosAction::Withdraw(Withdraw { source, validator })
| PosAction::ClaimRewards(ClaimRewards { validator, source }) =>
{
| PosAction::ClaimRewards(ClaimRewards {
validator,
source,
receiver: _,
}) => {
let source = source.unwrap_or(validator);
gadget.verify_signatures_when(
|| source == addr,
Expand Down
7 changes: 5 additions & 2 deletions wasm/vp_user/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ fn validate_tx(
source, validator, ..
})
| PosAction::Withdraw(Withdraw { source, validator })
| PosAction::ClaimRewards(ClaimRewards { validator, source }) =>
{
| PosAction::ClaimRewards(ClaimRewards {
validator,
source,
receiver: _,
}) => {
let source = source.unwrap_or(validator);
gadget.verify_signatures_when(
|| source == addr,
Expand Down

0 comments on commit 0ac1938

Please sign in to comment.