Skip to content

Commit

Permalink
staking: propagate api changes throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanor committed Mar 14, 2024
1 parent 1c85370 commit b483e30
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
44 changes: 42 additions & 2 deletions crates/bin/pd/src/testnet/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,46 @@ impl TestnetConfig {
}
}

/// Create a new testnet definition, including genesis and at least one
/// validator config. Write all configs to the target testnet dir,
/// defaulting to `~/.penumbra/testnet_data`, as usual.
#[allow(clippy::too_many_arguments)]
pub fn testnet_generate(
testnet_dir: PathBuf,
chain_id: &str,
active_validator_limit: Option<u64>,
tendermint_timeout_commit: Option<tendermint::Timeout>,
epoch_duration: Option<u64>,
unbonding_epochs: Option<u64>,
peer_address_template: Option<String>,
external_addresses: Vec<TendermintAddress>,
validators_input_file: Option<PathBuf>,
allocations_input_file: Option<PathBuf>,
proposal_voting_blocks: Option<u64>,
) -> anyhow::Result<()> {
tracing::info!(?chain_id, "Generating network config");
let t = TestnetConfig::generate(
chain_id,
Some(testnet_dir),
peer_address_template,
Some(external_addresses),
allocations_input_file,
validators_input_file,
tendermint_timeout_commit,
active_validator_limit,
epoch_duration,
unbonding_epochs,
proposal_voting_blocks,
)?;
tracing::info!(
n_validators = t.validators.len(),
chain_id = %t.genesis.chain_id,
"Writing config files for network"
);
t.write_configs()?;
Ok(())
}

/// Represents initial allocations to the testnet.
#[derive(Debug, Deserialize)]
pub struct TestnetAllocation {
Expand Down Expand Up @@ -500,8 +540,8 @@ impl TryFrom<&TestnetValidator> for Validator {
// Currently there's no way to set validator keys beyond
// manually editing the genesis.json. Otherwise they
// will be randomly generated keys.
identity_key: tv.keys.validator_id_vk,
governance_key: GovernanceKey(tv.keys.validator_id_vk.0),
identity_key: IdentityKey(tv.keys.validator_id_vk),
governance_key: GovernanceKey(tv.keys.validator_id_vk),
consensus_key: tv.keys.validator_cons_pk,
name: tv.name.clone(),
website: tv.website.clone(),
Expand Down
16 changes: 10 additions & 6 deletions crates/core/component/stake/src/component/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::params::StakeParameters;
use crate::rate::BaseRateData;
use crate::validator::{self, Validator};
use crate::{
state_key, CurrentConsensusKeys, Delegate, DelegationChanges, DelegationToken, FundingStreams,
IdentityKey, Penalty, Undelegate,
state_key, CurrentConsensusKeys, Delegate, DelegationChanges, FundingStreams, IdentityKey,
Penalty, Undelegate,
};
use anyhow::Context;
use anyhow::{anyhow, Result};
Expand All @@ -14,7 +14,6 @@ use futures::{StreamExt, TryStreamExt};
use penumbra_num::Amount;
use penumbra_proto::{StateReadProto, StateWriteProto};
use penumbra_sct::component::clock::EpochRead;
use penumbra_shielded_pool::component::SupplyRead;
use sha2::{Digest, Sha256};
use std::pin::Pin;
use std::str::FromStr;
Expand Down Expand Up @@ -418,9 +417,14 @@ pub(crate) trait InternalStakingData: StateRead {
}

let delegation_token_supply = self
.token_supply(&DelegationToken::from(validator_identity).id())
.await?
.expect("delegation token should be known");
.get_validator_pool_size(&validator_identity)
.await
.ok_or_else(|| {
anyhow::anyhow!(
"validator delegation pool not found for {}",
validator_identity
)
})?;

let validator_rate = self
.get_validator_rate(&validator_identity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use async_trait::async_trait;
use futures::StreamExt as _;
use penumbra_num::Amount;
use penumbra_sct::component::clock::{EpochManager, EpochRead};
use penumbra_shielded_pool::component::{SupplyRead as _, SupplyWrite};
use penumbra_shielded_pool::component::SupplyWrite;
use sha2::{Digest as _, Sha256};
use tendermint::abci::types::{CommitInfo, Misbehavior};
use tokio::task::JoinSet;
Expand All @@ -25,6 +25,7 @@ use cnidarium::StateWrite;
use penumbra_proto::StateWriteProto;
use tracing::{instrument, Instrument};

use crate::component::validator_handler::validator_store::ValidatorPoolTracker;
use crate::{
component::validator_handler::ValidatorDataRead,
component::StateReadExt as _,
Expand Down Expand Up @@ -393,6 +394,7 @@ pub trait ValidatorManager: StateWrite {
// All genesis validators start in the "Bonded" bonding state:
validator::BondingState::Bonded,
power,
total_delegation_tokens,
)
.await?;

Expand Down Expand Up @@ -421,7 +423,8 @@ pub trait ValidatorManager: StateWrite {
rate_data,
validator::State::Defined,
validator::BondingState::Unbonded,
0u128.into(),
Amount::zero(),
Amount::zero(),
)
.await
}
Expand All @@ -442,6 +445,7 @@ pub trait ValidatorManager: StateWrite {
initial_state: validator::State,
initial_bonding_state: validator::BondingState,
initial_voting_power: Amount,
initial_delegation_pool_size: Amount,
) -> Result<()> {
tracing::debug!("adding validator");
if !matches!(initial_state, State::Defined | State::Active) {
Expand Down Expand Up @@ -473,6 +477,7 @@ pub trait ValidatorManager: StateWrite {
self.set_initial_validator_state(&validator_identity, initial_state)?;
self.set_validator_power(&validator_identity, initial_voting_power)?;
self.set_validator_bonding_state(&validator_identity, initial_bonding_state);
self.set_validator_pool_size(&validator_identity, initial_delegation_pool_size);

// Finally, update metrics for the new validator.
match initial_state {
Expand Down Expand Up @@ -517,9 +522,9 @@ pub trait ValidatorManager: StateWrite {
.await?
.ok_or_else(|| anyhow::anyhow!("updated validator not found in JMT"))?;
let delegation_token_supply = self
.token_supply(&DelegationToken::from(id).id())
.await?
.ok_or_else(|| anyhow::anyhow!("updated validator not found in JMT"))?;
.get_validator_pool_size(id)
.await
.unwrap_or_else(Amount::zero);
let unbonded_amount =
current_validator_rate.unbonded_amount(delegation_token_supply);

Expand All @@ -538,9 +543,9 @@ pub trait ValidatorManager: StateWrite {
.await?
.ok_or_else(|| anyhow::anyhow!("updated validator not found in JMT"))?;
let delegation_pool_size = self
.token_supply(&DelegationToken::from(id).id())
.await?
.ok_or_else(|| anyhow::anyhow!("updated validator not found in JMT"))?;
.get_validator_pool_size(id)
.await
.unwrap_or_else(Amount::zero);

let unbonded_pool_size = validator_rate_data.unbonded_amount(delegation_pool_size);

Expand Down

0 comments on commit b483e30

Please sign in to comment.