Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tune past epochs in storage #1944

Merged
merged 26 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4f89775
refactor `Epoched` for past epochs
brentstone Sep 26, 2023
d8a29ef
cleaning: remove unused code + improve docstrings
brentstone Sep 26, 2023
8416f0b
test/eth: init storage with PoS params where needed
tzemanovic Sep 28, 2023
f5d4544
test/PoS/epoched: init PoS params and fix `PastEpochs` type param
tzemanovic Sep 28, 2023
d3f1eb3
changelog: add #1943
brentstone Sep 28, 2023
812b2c2
[ci] wasm checksums update
github-actions[bot] Sep 28, 2023
bfd91f9
WIP want to pass `max_proposal_period` from gov params into PoS
brentstone Sep 26, 2023
053fffb
add PosParams type with added gov param
tzemanovic Sep 27, 2023
bfe4c97
pos/types: configure number of past epochs kept for PoS data
brentstone Sep 28, 2023
eaed189
new impl for purging old validator sets
brentstone Sep 28, 2023
85bf4aa
docstring cleanup
brentstone Sep 28, 2023
6364cc0
Apply suggestions from code review
tzemanovic Sep 28, 2023
1ddd82b
fixup! Apply suggestions from code review
tzemanovic Sep 28, 2023
f1bc264
fix PoS crate standalone build
tzemanovic Sep 28, 2023
f40bd26
replace direct storage read of PosParams with an RPC fn
tzemanovic Sep 28, 2023
41d3838
fixup! replace direct storage read of PosParams with an RPC fn
tzemanovic Sep 28, 2023
620e4e2
core/types/storage:m ore flexible fn checked_sub param
tzemanovic Sep 28, 2023
856130e
PoS: refactor usages of `Epoch::checked_sub`
tzemanovic Sep 28, 2023
20180c4
changelog: add #1944
brentstone Sep 28, 2023
3fa8d38
[ci] wasm checksums update
github-actions[bot] Sep 28, 2023
c65a6d0
fixed `epoched` tests and `test_validator_sets`
brentstone Sep 28, 2023
9971497
fixup! pos/types: configure number of past epochs kept for PoS data
tzemanovic Sep 29, 2023
7c7ee02
keep validator eth keys for max proposal period
brentstone Oct 2, 2023
85770be
clear old enqueued slashes when processing slashes
brentstone Oct 2, 2023
59d8465
keep `total_deltas` for same period of time as `validator_deltas`
brentstone Oct 2, 2023
64e817b
move protocol keys into epoched PoS data
brentstone Oct 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Improve the Epoched data structure's bookkeeping of past
epochs, now parameterizable by PoS and governance params.
([\#1943](https://github.com/anoma/namada/pull/1943))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- New implementation and parameters for purging old epochs for Epoched validator
data in storage. ([\#1944](https://github.com/anoma/namada/pull/1944))
8 changes: 1 addition & 7 deletions apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use namada::core::ledger::governance::cli::offline::{
use namada::core::ledger::governance::cli::onchain::{
DefaultProposal, PgfFundingProposal, PgfStewardProposal, ProposalVote,
};
use namada::ledger::pos;
use namada::proof_of_stake::parameters::PosParams;
use namada::proto::Tx;
use namada::sdk::rpc::{TxBroadcastData, TxResponse};
use namada::sdk::wallet::{Wallet, WalletUtils};
Expand Down Expand Up @@ -550,11 +548,7 @@ where
)
.unwrap();

let key = pos::params_key();
let pos_params =
rpc::query_storage_value::<C, PosParams>(client, &key)
.await
.expect("Pos parameter should be defined.");
let pos_params = rpc::query_pos_parameters(client).await;

display_line!(IO, "");
display_line!(
Expand Down
29 changes: 13 additions & 16 deletions apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use namada::core::ledger::governance::parameters::GovernanceParameters;
use namada::core::ledger::pgf::parameters::PgfParameters;
use namada::ledger::eth_bridge::EthereumBridgeConfig;
use namada::ledger::parameters::EpochDuration;
use namada::ledger::pos::{Dec, GenesisValidator, PosParams};
use namada::ledger::pos::{Dec, GenesisValidator, OwnedPosParams};
use namada::types::address::Address;
use namada::types::chain::ProposalBytes;
use namada::types::key::dkg_session_keys::DkgPublicKey;
Expand All @@ -31,7 +31,7 @@ pub mod genesis_config {
use namada::core::ledger::governance::parameters::GovernanceParameters;
use namada::core::ledger::pgf::parameters::PgfParameters;
use namada::ledger::parameters::EpochDuration;
use namada::ledger::pos::{Dec, GenesisValidator, PosParams};
use namada::ledger::pos::{Dec, GenesisValidator, OwnedPosParams};
use namada::types::address::Address;
use namada::types::chain::ProposalBytes;
use namada::types::key::dkg_session_keys::DkgPublicKey;
Expand Down Expand Up @@ -338,6 +338,12 @@ pub mod genesis_config {
.unwrap()
.to_public_key()
.unwrap(),
protocol_key: config
.protocol_public_key
.as_ref()
.unwrap()
.to_public_key()
.unwrap(),
eth_cold_key: config
.eth_cold_key
.as_ref()
Expand Down Expand Up @@ -372,12 +378,6 @@ pub mod genesis_config {
.unwrap()
.to_public_key()
.unwrap(),
protocol_key: config
.protocol_public_key
.as_ref()
.unwrap()
.to_public_key()
.unwrap(),
dkg_public_key: config
.dkg_public_key
.as_ref()
Expand Down Expand Up @@ -665,7 +665,7 @@ pub mod genesis_config {
validator_stake_threshold,
} = pos_params;

let pos_params = PosParams {
let pos_params = OwnedPosParams {
max_validator_slots,
pipeline_len,
unbonding_len,
Expand Down Expand Up @@ -738,7 +738,7 @@ pub struct Genesis {
pub established_accounts: Vec<EstablishedAccount>,
pub implicit_accounts: Vec<ImplicitAccount>,
pub parameters: Parameters,
pub pos_params: PosParams,
pub pos_params: OwnedPosParams,
pub gov_params: GovernanceParameters,
pub pgf_params: PgfParameters,
// Ethereum bridge config
Expand Down Expand Up @@ -774,9 +774,6 @@ pub struct Validator {
/// this key on a transaction signature.
/// Note that this is distinct from consensus key used in the PoS system.
pub account_key: common::PublicKey,
/// Public key associated with validator account used for signing protocol
/// transactions
pub protocol_key: common::PublicKey,
/// The public DKG session key used during the DKG protocol
pub dkg_public_key: DkgPublicKey,
/// These tokens are not staked and hence do not contribute to the
Expand Down Expand Up @@ -938,14 +935,14 @@ pub fn genesis(num_validators: u64) -> Genesis {
address,
tokens: token::Amount::native_whole(200_000),
consensus_key: consensus_keypair.ref_to(),
protocol_key: protocol_keypair.ref_to(),
commission_rate: Dec::new(5, 2).expect("This can't fail"),
max_commission_rate_change: Dec::new(1, 2)
.expect("This can't fail"),
eth_cold_key: eth_cold_keypair.ref_to(),
eth_hot_key: eth_bridge_keypair.ref_to(),
},
account_key: account_keypair.ref_to(),
protocol_key: protocol_keypair.ref_to(),
dkg_public_key: dkg_keypair.public(),
non_staked_balance: token::Amount::native_whole(100_000),
// TODO replace with https://github.com/anoma/namada/issues/25)
Expand All @@ -971,14 +968,14 @@ pub fn genesis(num_validators: u64) -> Genesis {
address,
tokens: token::Amount::native_whole(200_000),
consensus_key: consensus_keypair.ref_to(),
protocol_key: protocol_keypair.ref_to(),
commission_rate: Dec::new(5, 2).expect("This can't fail"),
max_commission_rate_change: Dec::new(1, 2)
.expect("This can't fail"),
eth_cold_key: eth_cold_keypair.ref_to(),
eth_hot_key: eth_bridge_keypair.ref_to(),
},
account_key: account_keypair.ref_to(),
protocol_key: protocol_keypair.ref_to(),
dkg_public_key: dkg_keypair.public(),
non_staked_balance: token::Amount::native_whole(100_000),
// TODO replace with https://github.com/anoma/namada/issues/25)
Expand Down Expand Up @@ -1109,7 +1106,7 @@ pub fn genesis(num_validators: u64) -> Genesis {
implicit_accounts,
token_accounts,
parameters,
pos_params: PosParams::default(),
pos_params: OwnedPosParams::default(),
gov_params: GovernanceParameters::default(),
pgf_params: PgfParameters::default(),
ethereum_bridge_params: Some(EthereumBridgeConfig {
Expand Down
188 changes: 179 additions & 9 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,14 @@ where
namada_proof_of_stake::read_pos_params(&self.wl_storage)?;
namada_proof_of_stake::copy_validator_sets_and_positions(
&mut self.wl_storage,
&pos_params,
current_epoch,
current_epoch + pos_params.pipeline_len,
)?;
namada_proof_of_stake::store_total_consensus_stake(
&mut self.wl_storage,
current_epoch,
)?;
namada_proof_of_stake::purge_validator_sets_for_old_epoch(
&mut self.wl_storage,
current_epoch,
)?;
sug0 marked this conversation as resolved.
Show resolved Hide resolved
}

// Invariant: Has to be applied before `record_slashes_from_evidence`
Expand Down Expand Up @@ -2653,8 +2650,8 @@ mod test_finalize_block {
num_validators,
});
let mut params = read_pos_params(&shell.wl_storage).unwrap();
params.unbonding_len = 4;
write_pos_params(&mut shell.wl_storage, params.clone())?;
params.owned.unbonding_len = 4;
write_pos_params(&mut shell.wl_storage, &params.owned)?;

let validator_set: Vec<WeightedValidator> =
read_consensus_validator_set_addresses_with_stake(
Expand Down Expand Up @@ -3031,9 +3028,9 @@ mod test_finalize_block {
num_validators,
});
let mut params = read_pos_params(&shell.wl_storage).unwrap();
params.unbonding_len = 4;
params.max_validator_slots = 4;
write_pos_params(&mut shell.wl_storage, params.clone())?;
params.owned.unbonding_len = 4;
params.owned.max_validator_slots = 4;
write_pos_params(&mut shell.wl_storage, &params.owned)?;

// Slash pool balance
let nam_address = shell.wl_storage.storage.native_token.clone();
Expand Down Expand Up @@ -3823,6 +3820,179 @@ mod test_finalize_block {
Ok(())
}

#[test]
fn test_purge_validator_information() -> storage_api::Result<()> {
// Setup the network with pipeline_len = 2, unbonding_len = 4
let num_validators = 4_u64;
let (mut shell, _recv, _, _) = setup_with_cfg(SetupCfg {
last_height: 0,
num_validators,
});
let mut params = read_pos_params(&shell.wl_storage).unwrap();
params.owned.unbonding_len = 4;
// params.owned.max_validator_slots = 3;
// params.owned.validator_stake_threshold = token::Amount::zero();
write_pos_params(&mut shell.wl_storage, &params.owned)?;

let max_proposal_period = params.max_proposal_period;
let default_past_epochs = 2;
let consensus_val_set_len = max_proposal_period + default_past_epochs;

let consensus_val_set =
namada_proof_of_stake::consensus_validator_set_handle();
// let below_cap_val_set =
// namada_proof_of_stake::below_capacity_validator_set_handle();
let validator_positions =
namada_proof_of_stake::validator_set_positions_handle();
let all_validator_addresses =
namada_proof_of_stake::validator_addresses_handle();

let consensus_set: Vec<WeightedValidator> =
read_consensus_validator_set_addresses_with_stake(
&shell.wl_storage,
Epoch::default(),
)
.unwrap()
.into_iter()
.collect();
let val1 = consensus_set[0].clone();
let pkh1 = get_pkh_from_address(
&shell.wl_storage,
&params,
val1.address,
Epoch::default(),
);

// Finalize block 1
next_block_for_inflation(&mut shell, pkh1.clone(), vec![], None);

let votes = get_default_true_votes(&shell.wl_storage, Epoch::default());
assert!(!votes.is_empty());

let check_is_data = |storage: &WlStorage<_, _>,
start: Epoch,
end: Epoch| {
for ep in Epoch::iter_bounds_inclusive(start, end) {
assert!(!consensus_val_set.at(&ep).is_empty(storage).unwrap());
// assert!(!below_cap_val_set.at(&ep).is_empty(storage).
// unwrap());
assert!(
!validator_positions.at(&ep).is_empty(storage).unwrap()
);
assert!(
!all_validator_addresses.at(&ep).is_empty(storage).unwrap()
);
}
};

// Check that there is validator data for epochs 0 - pipeline_len
check_is_data(&shell.wl_storage, Epoch(0), Epoch(params.pipeline_len));

// Advance to epoch `default_past_epochs`
let mut current_epoch = Epoch(0);
for _ in 0..default_past_epochs {
let votes = get_default_true_votes(
&shell.wl_storage,
shell.wl_storage.storage.block.epoch,
);
current_epoch = advance_epoch(&mut shell, &pkh1, &votes, None);
}
assert_eq!(shell.wl_storage.storage.block.epoch.0, default_past_epochs);
assert_eq!(current_epoch.0, default_past_epochs);

check_is_data(
&shell.wl_storage,
Epoch(0),
Epoch(params.pipeline_len + default_past_epochs),
);

// Advance one more epoch, which should purge the data for epoch 0 in
// everything except the consensus validator set
let votes = get_default_true_votes(
&shell.wl_storage,
shell.wl_storage.storage.block.epoch,
);
current_epoch = advance_epoch(&mut shell, &pkh1, &votes, None);
assert_eq!(current_epoch.0, default_past_epochs + 1);

check_is_data(
&shell.wl_storage,
Epoch(1),
Epoch(params.pipeline_len + default_past_epochs + 1),
);
assert!(
!consensus_val_set
.at(&Epoch(0))
.is_empty(&shell.wl_storage)
.unwrap()
);
assert!(
validator_positions
.at(&Epoch(0))
.is_empty(&shell.wl_storage)
.unwrap()
);
assert!(
all_validator_addresses
.at(&Epoch(0))
.is_empty(&shell.wl_storage)
.unwrap()
);

// Advance to the epoch `consensus_val_set_len` + 1
loop {
assert!(
!consensus_val_set
.at(&Epoch(0))
.is_empty(&shell.wl_storage)
.unwrap()
);
let votes = get_default_true_votes(
&shell.wl_storage,
shell.wl_storage.storage.block.epoch,
);
current_epoch = advance_epoch(&mut shell, &pkh1, &votes, None);
if current_epoch.0 == consensus_val_set_len + 1 {
break;
}
}

assert!(
consensus_val_set
.at(&Epoch(0))
.is_empty(&shell.wl_storage)
.unwrap()
);

// Advance one more epoch
let votes = get_default_true_votes(
&shell.wl_storage,
shell.wl_storage.storage.block.epoch,
);
current_epoch = advance_epoch(&mut shell, &pkh1, &votes, None);
for ep in Epoch::default().iter_range(2) {
assert!(
consensus_val_set
.at(&ep)
.is_empty(&shell.wl_storage)
.unwrap()
);
}
for ep in Epoch::iter_bounds_inclusive(
Epoch(2),
current_epoch + params.pipeline_len,
) {
assert!(
!consensus_val_set
.at(&ep)
.is_empty(&shell.wl_storage)
.unwrap()
);
}

Ok(())
}

fn get_default_true_votes<S>(storage: &S, epoch: Epoch) -> Vec<VoteInfo>
where
S: StorageRead,
Expand Down
8 changes: 2 additions & 6 deletions apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hash::Hash;

use namada::ledger::eth_bridge::EthBridgeStatus;
use namada::ledger::parameters::{self, Parameters};
use namada::ledger::pos::{staking_token_address, PosParams};
use namada::ledger::pos::{staking_token_address, OwnedPosParams};
use namada::ledger::storage::traits::StorageHasher;
use namada::ledger::storage::{DBIter, DB};
use namada::ledger::storage_api::token::{
Expand Down Expand Up @@ -394,10 +394,6 @@ where
)
.unwrap();

self.wl_storage
.write(&protocol_pk_key(addr), &validator.protocol_key)
.expect("Unable to set genesis user protocol public key");

self.wl_storage
.write(
&dkg_session_keys::dkg_pk_key(addr),
Expand All @@ -412,7 +408,7 @@ where
&mut self,
staking_token: &Address,
validators: Vec<genesis::Validator>,
pos_params: &PosParams,
pos_params: &OwnedPosParams,
) -> Result<response::InitChain> {
let mut response = response::InitChain::default();
// PoS system depends on epoch being initialized. Write the total
Expand Down
Loading