Skip to content

Commit

Permalink
Expanding and fixing slashes query
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Jul 6, 2023
1 parent 8370707 commit 03adf59
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 14 deletions.
85 changes: 78 additions & 7 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,18 +1748,55 @@ pub async fn query_slashes<C: namada::ledger::queries::Client + Sync>(
RPC.vp().pos().validator_slashes(client, &validator).await,
);
if !slashes.is_empty() {
println!("Processed slashes:");
let stdout = io::stdout();
let mut w = stdout.lock();
for slash in slashes {
writeln!(
w,
"Slash epoch {}, type {}, rate {}",
slash.epoch, slash.r#type, slash.rate
"Infraction epoch {}, block height {}, type {}, rate \
{}",
slash.epoch,
slash.block_height,
slash.r#type,
slash.rate
)
.unwrap();
}
} else {
println!("No slashes found for {}", validator.encode())
println!(
"No processed slashes found for {}",
validator.encode()
)
}
// Find enqueued slashes to be processed in the future for the given
// validator
let enqueued_slashes: HashMap<Address, HashMap<Epoch, Vec<Slash>>> =
unwrap_client_response::<
C,
HashMap<Address, HashMap<Epoch, Vec<Slash>>>,
>(RPC.vp().pos().enqueued_slashes(client).await);
let enqueued_slashes = enqueued_slashes.get(&validator).cloned();
if let Some(enqueued) = enqueued_slashes {
println!("\nEnqueued slashes for future processing");
for (epoch, slashes) in enqueued {
println!("To be processed in epoch {}", epoch);
for slash in slashes {
let stdout = io::stdout();
let mut w = stdout.lock();
writeln!(
w,
"Infraction epoch {}, block height {}, type {}",
slash.epoch,
slash.block_height,
slash.r#type,
slash.rate
)
.unwrap();
}
}
} else {
println!("No enqueued slashes found for {}", validator.encode())
}
}
None => {
Expand All @@ -1771,23 +1808,57 @@ pub async fn query_slashes<C: namada::ledger::queries::Client + Sync>(
if !all_slashes.is_empty() {
let stdout = io::stdout();
let mut w = stdout.lock();
println!("Processed slashes:");
for (validator, slashes) in all_slashes.into_iter() {
for slash in slashes {
writeln!(
w,
"Slash epoch {}, block height {}, rate {}, type \
{}, validator {}",
"Infraction epoch {}, block height {}, rate {}, \
type {}, validator {}",
slash.epoch,
slash.block_height,
slash.r#type.get_slash_rate(&params),
slash.rate,
slash.r#type,
validator,
)
.unwrap();
}
}
} else {
println!("No slashes found")
println!("No processed slashes found")
}

// Find enqueued slashes to be processed in the future for the given
// validator
let enqueued_slashes: HashMap<Address, HashMap<Epoch, Vec<Slash>>> =
unwrap_client_response::<
C,
HashMap<Address, HashMap<Epoch, Vec<Slash>>>,
>(RPC.vp().pos().enqueued_slashes(client).await);
if !enqueued_slashes.is_empty() {
println!("\nEnqueued slashes for future processing");
for (validator, slashes_by_epoch) in enqueued_slashes {
for (epoch, slashes) in slashes_by_epoch {
println!("\nTo be processed in epoch {}", epoch);
for slash in slashes {
let stdout = io::stdout();
let mut w = stdout.lock();
writeln!(
w,
"Infraction epoch {}, block height {}, type \
{}, validator {}",
slash.epoch,
slash.block_height,
slash.r#type,
slash.rate,
validator
)
.unwrap();
}
}
}
} else {
println!("\nNo enqueued slashes found for future processing")
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,36 @@ where
}
}

pub fn find_all_enqueued_slashes<S>(
storage: &S,
) -> storage_api::Result<HashMap<Address, BTreeMap<Epoch, Vec<Slash>>>>
where
S: StorageRead,
{
let mut enqueued = HashMap::<Address, BTreeMap<Epoch, Vec<Slash>>>::new();
for res in enqueued_slashes_handle().get_data_handler().iter(storage)? {
let (
NestedSubKey::Data {
key: processing_epoch,
nested_sub_key:
NestedSubKey::Data {
key: address,
nested_sub_key: _,
},
},
slash,
) = res?;

let slashes = enqueued
.entry(address)
.or_default()
.entry(processing_epoch)
.or_default();
slashes.push(slash);
}
Ok(enqueued)
}

/// Find all slashes and the associated validators in the PoS system
pub fn find_all_slashes<S>(
storage: &S,
Expand Down
29 changes: 22 additions & 7 deletions shared/src/ledger/queries/vp/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use namada_proof_of_stake::types::{
};
use namada_proof_of_stake::{
self, below_capacity_validator_set_handle, bond_amount, bond_handle,
consensus_validator_set_handle, find_all_slashes,
find_delegation_validators, find_delegations, read_all_validator_addresses,
read_pos_params, read_total_stake,
read_validator_max_commission_rate_change, read_validator_stake,
unbond_handle, validator_commission_rate_handle, validator_slashes_handle,
validator_state_handle,
consensus_validator_set_handle, enqueued_slashes_handle,
find_all_enqueued_slashes, find_all_slashes, find_delegation_validators,
find_delegations, read_all_validator_addresses, read_pos_params,
read_total_stake, read_validator_max_commission_rate_change,
read_validator_stake, unbond_handle, validator_commission_rate_handle,
validator_slashes_handle, validator_state_handle,
};

use crate::ledger::queries::types::RequestCtx;
Expand Down Expand Up @@ -86,6 +86,9 @@ router! {POS,
( "bonds_and_unbonds" / [source: opt Address] / [validator: opt Address] )
-> BondsAndUnbondsDetails = bonds_and_unbonds,

( "enqueued_slashes" )
-> HashMap<Address, BTreeMap<Epoch, Vec<Slash>>> = enqueued_slashes,

( "all_slashes" ) -> HashMap<Address, Vec<Slash>> = slashes,

( "is_delegator" / [addr: Address ] / [epoch: opt Epoch] ) -> bool = is_delegator,
Expand Down Expand Up @@ -520,7 +523,19 @@ where
find_all_slashes(ctx.wl_storage)
}

/// All slashes
/// Enqueued slashes
fn enqueued_slashes<D, H>(
ctx: RequestCtx<'_, D, H>,
) -> storage_api::Result<HashMap<Address, BTreeMap<Epoch, Vec<Slash>>>>
where
D: 'static + DB + for<'iter> DBIter<'iter> + Sync,
H: 'static + StorageHasher + Sync,
{
let current_epoch = ctx.wl_storage.storage.last_epoch;
find_all_enqueued_slashes(storage)?
}

/// Native validator address by looking up the Tendermint address
fn validator_by_tm_addr<D, H>(
ctx: RequestCtx<'_, D, H>,
tm_addr: String,
Expand Down

0 comments on commit 03adf59

Please sign in to comment.