Skip to content

Commit

Permalink
native_vp/PoS: update to use actions from txs
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Mar 21, 2024
1 parent ae9cec0 commit 1e36094
Showing 1 changed file with 201 additions and 43 deletions.
244 changes: 201 additions & 43 deletions crates/namada/src/ledger/pos/vp.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
//! Proof-of-Stake native validity predicate.

use std::collections::BTreeSet;
use std::collections::{BTreeMap, BTreeSet};

pub use namada_proof_of_stake;
pub use namada_proof_of_stake::parameters::PosParams;
// use namada_proof_of_stake::validation::validate;
use namada_proof_of_stake::storage::read_pos_params;
use namada_proof_of_stake::storage_key::is_params_key;
use namada_proof_of_stake::token;
pub use namada_proof_of_stake::types;
use namada_proof_of_stake::types::BondId;
use namada_state::StateRead;
use namada_tx::action::{
Action, Bond, ClaimRewards, PosAction, Read, Redelegation, Unbond, Withdraw,
};
use namada_tx::Tx;
use thiserror::Error;

use crate::address::{self, Address};
use crate::address::Address;
use crate::ledger::native_vp::{self, Ctx, NativeVp};
use crate::storage::{Key, KeySeg};
use crate::storage::Key;
use crate::vm::WasmCacheAccess;

#[allow(missing_docs)]
Expand Down Expand Up @@ -59,7 +64,7 @@ where
&self,
tx_data: &Tx,
keys_changed: &BTreeSet<Key>,
_verifiers: &BTreeSet<Address>,
verifiers: &BTreeSet<Address>,

Check warning on line 67 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L67

Added line #L67 was not covered by tests
) -> Result<bool> {
// use validation::Data;
// use validation::DataUpdate::{self, *};
Expand All @@ -70,49 +75,202 @@ where

tracing::debug!("\nValidating PoS storage changes\n");

for key in keys_changed {
if is_params_key(key) {
let data = if let Some(data) = tx_data.data() {
data
} else {
return Ok(false);
};
if !namada_governance::is_proposal_accepted(
&self.ctx.pre(),
&data,
)
.map_err(Error::NativeVpError)?
{
return Ok(false);
}
let params = read_pos_params(&self.ctx.post())?.owned;
if !params.validate().is_empty() {
return Ok(false);
let data = if let Some(data) = tx_data.data() {
data

Check warning on line 79 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L78-L79

Added lines #L78 - L79 were not covered by tests
} else {
return Ok(false);

Check warning on line 81 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L81

Added line #L81 was not covered by tests
};

// Check if this is a governance proposal first
if namada_governance::is_proposal_accepted(&self.ctx.pre(), &data)
.map_err(Error::NativeVpError)?

Check warning on line 86 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L85-L86

Added lines #L85 - L86 were not covered by tests
{
for key in keys_changed {
if is_params_key(key) {
let params = read_pos_params(&self.ctx.post())?.owned;

Check warning on line 90 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L88-L90

Added lines #L88 - L90 were not covered by tests
// If governance changes PoS params, the params have to be
// valid
if !params.validate().is_empty() {
return Ok(false);
}

Check warning on line 95 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L93-L95

Added lines #L93 - L95 were not covered by tests
}
} else if key.segments.first() == Some(&address::POS.to_db_key()) {
// No VP logic applied to all other PoS keys for now, as PoS txs
// are all whitelisted
tracing::debug!(
"PoS key change {} - No action is taken currently.",
key
);
} else {
// Unknown changes anywhere else are permitted
tracing::debug!("PoS unrecognized key change {}", key);
// Any other change from governance is allowed without further
// checks
}
return Ok(true);
}

Check warning on line 101 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L100-L101

Added lines #L100 - L101 were not covered by tests

// Find the actions applied in the tx
let actions = self.ctx.read_actions()?;

Check warning on line 104 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L104

Added line #L104 was not covered by tests

// There must be at least one action
if actions.is_empty() {
tracing::info!(
"Rejecting tx without any action written to temp storage"

Check warning on line 109 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L107-L109

Added lines #L107 - L109 were not covered by tests
);
return Ok(false);
}

let mut became_validator: BTreeSet<Address> = Default::default();
let mut deactivated: BTreeSet<Address> = Default::default();
let mut reactivated: BTreeSet<Address> = Default::default();
let mut unjailed: BTreeSet<Address> = Default::default();
let mut bonds: BTreeMap<BondId, token::Amount> = Default::default();
let mut unbonds: BTreeMap<BondId, token::Amount> = Default::default();
let mut withdrawals: BTreeSet<BondId> = Default::default();
// 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();
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();

Check warning on line 127 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L111-L127

Added lines #L111 - L127 were not covered by tests

// Accumulate changes from the actions
for action in actions {
match action {
Action::Pos(pos_action) => match pos_action {
PosAction::BecomeValidator(address) => {
if !verifiers.contains(&address) {
tracing::info!(
"Unauthorized PosAction::BecomeValidator"

Check warning on line 136 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L130-L136

Added lines #L130 - L136 were not covered by tests
);
return Ok(false);
}
became_validator.insert(address);

Check warning on line 140 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L138-L140

Added lines #L138 - L140 were not covered by tests
}
PosAction::DeactivateValidator(validator) => {
if !verifiers.contains(&validator) {
tracing::info!(
"Unauthorized PosAction::DeactivateValidator"

Check warning on line 145 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L142-L145

Added lines #L142 - L145 were not covered by tests
);
return Ok(false);
}
deactivated.insert(validator);

Check warning on line 149 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L147-L149

Added lines #L147 - L149 were not covered by tests
}
PosAction::ReactivateValidator(validator) => {
if !verifiers.contains(&validator) {
tracing::info!(
"Unauthorized PosAction::ReactivateValidator"

Check warning on line 154 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L151-L154

Added lines #L151 - L154 were not covered by tests
);
return Ok(false);
}
reactivated.insert(validator);

Check warning on line 158 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L156-L158

Added lines #L156 - L158 were not covered by tests
}
PosAction::Unjail(validator) => {
if !verifiers.contains(&validator) {
tracing::info!("Unauthorized PosAction::Unjail");
return Ok(false);
}
unjailed.insert(validator);

Check warning on line 165 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L160-L165

Added lines #L160 - L165 were not covered by tests
}
PosAction::Bond(Bond {
validator,
amount,
source,
}) => {
let bond_id = BondId {
source: source.unwrap_or_else(|| validator.clone()),
validator,
};
if !verifiers.contains(&bond_id.source) {
tracing::info!("Unauthorized PosAction::Bond");
return Ok(false);
}
bonds.insert(bond_id, amount);

Check warning on line 180 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L168-L180

Added lines #L168 - L180 were not covered by tests
}
PosAction::Unbond(Unbond {
validator,
amount,
source,
}) => {
let bond_id = BondId {
source: source.unwrap_or_else(|| validator.clone()),
validator,
};
if !verifiers.contains(&bond_id.source) {
tracing::info!("Unauthorized PosAction::Unbond");
return Ok(false);
}
unbonds.insert(bond_id, amount);

Check warning on line 195 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L183-L195

Added lines #L183 - L195 were not covered by tests
}
PosAction::Withdraw(Withdraw { validator, source }) => {
let bond_id = BondId {
source: source.unwrap_or_else(|| validator.clone()),
validator,
};
if !verifiers.contains(&bond_id.source) {
tracing::info!("Unauthorized PosAction::Withdraw");
return Ok(false);
}
withdrawals.insert(bond_id);

Check warning on line 206 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L197-L206

Added lines #L197 - L206 were not covered by tests
}
PosAction::Redelegation(Redelegation {
src_validator,
dest_validator,
owner,
amount,
}) => {
if !verifiers.contains(&owner) {
tracing::info!(
"Unauthorized PosAction::Redelegation"

Check warning on line 216 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L209-L216

Added lines #L209 - L216 were not covered by tests
);
return Ok(false);
}
let bond_id = BondId {
source: owner,
validator: src_validator,
};
redelegations.insert(bond_id, (dest_validator, amount));

Check warning on line 224 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L218-L224

Added lines #L218 - L224 were not covered by tests
}
PosAction::ClaimRewards(ClaimRewards {
validator,
source,
}) => {
let bond_id = BondId {
source: source.unwrap_or_else(|| validator.clone()),
validator,
};
if !verifiers.contains(&bond_id.source) {
tracing::info!(
"Unauthorized PosAction::ClaimRewards"

Check warning on line 236 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L227-L236

Added lines #L227 - L236 were not covered by tests
);
return Ok(false);
}
claimed_rewards.insert(bond_id);

Check warning on line 240 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L238-L240

Added lines #L238 - L240 were not covered by tests
}
PosAction::CommissionChange(validator) => {
if !verifiers.contains(&validator) {
tracing::info!(
"Unauthorized PosAction::CommissionChange"

Check warning on line 245 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L242-L245

Added lines #L242 - L245 were not covered by tests
);
return Ok(false);
}
changed_commission.insert(validator);

Check warning on line 249 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L247-L249

Added lines #L247 - L249 were not covered by tests
}
PosAction::MetadataChange(validator) => {
if !verifiers.contains(&validator) {
tracing::info!(
"Unauthorized PosAction::MetadataChange"

Check warning on line 254 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L251-L254

Added lines #L251 - L254 were not covered by tests
);
return Ok(false);
}
changed_metadata.insert(validator);

Check warning on line 258 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L256-L258

Added lines #L256 - L258 were not covered by tests
}
PosAction::ConsensusKeyChange(validator) => {
if !verifiers.contains(&validator) {
tracing::info!(
"Unauthorized PosAction::ConsensusKeyChange"

Check warning on line 263 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L260-L263

Added lines #L260 - L263 were not covered by tests
);
return Ok(false);
}
changed_consensus_key.insert(validator);

Check warning on line 267 in crates/namada/src/ledger/pos/vp.rs

View check run for this annotation

Codecov / codecov/patch

crates/namada/src/ledger/pos/vp.rs#L265-L267

Added lines #L265 - L267 were not covered by tests
}
},
}
}

// let _params = read_pos_params(&self.ctx.pre())?;
// let errors = validate(&params, changes, current_epoch);
// Ok(if errors.is_empty() {
// true
// } else {
// tracing::info!(
// "PoS validation errors:\n - {}",
// errors.iter().format("\n - ")
// );
// false
// })
// TODO: validate changes keys against the accumulated changes
Ok(true)
}
}
Expand Down

0 comments on commit 1e36094

Please sign in to comment.