Skip to content

Commit

Permalink
move protocol keys into epoched PoS data
Browse files Browse the repository at this point in the history
fix tests
  • Loading branch information
brentstone committed Oct 4, 2023
1 parent 59d8465 commit 64e817b
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 53 deletions.
19 changes: 8 additions & 11 deletions apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
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 @@ -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
4 changes: 0 additions & 4 deletions apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
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 Down
16 changes: 2 additions & 14 deletions apps/src/lib/node/ledger/shell/vote_extensions/bridge_pool_vext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ mod test_bp_vote_extensions {
#[cfg(not(feature = "abcipp"))]
use namada::ledger::eth_bridge::EthBridgeQueries;
use namada::ledger::pos::PosQueries;
use namada::ledger::storage_api::StorageWrite;
use namada::proof_of_stake::types::{
Position as ValidatorPosition, WeightedValidator,
};
Expand Down Expand Up @@ -321,24 +320,12 @@ mod test_bp_vote_extensions {
)
.expect("Test failed");

// register Bertha's protocol key
let pk_key = protocol_pk_key(&bertha_address());
shell
.wl_storage
.write_bytes(
&pk_key,
bertha_keypair()
.ref_to()
.try_to_vec()
.expect("Test failed."),
)
.expect("Test failed.");

// change pipeline length to 1
let mut params = shell.wl_storage.pos_queries().get_pos_params();
params.owned.pipeline_len = 1;

let consensus_key = gen_keypair();
let protocol_key = bertha_keypair();
let hot_key = gen_secp256k1_keypair();
let cold_key = gen_secp256k1_keypair();

Expand All @@ -347,6 +334,7 @@ mod test_bp_vote_extensions {
params: &params,
address: &bertha_address(),
consensus_key: &consensus_key.ref_to(),
protocol_key: &protocol_key.ref_to(),
eth_hot_key: &hot_key.ref_to(),
eth_cold_key: &cold_key.ref_to(),
current_epoch: 0.into(),
Expand Down
4 changes: 4 additions & 0 deletions ethereum_bridge/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,15 @@ pub fn init_storage_with_validators(
.map(|(address, tokens)| {
let keys = TestValidatorKeys::generate();
let consensus_key = keys.consensus.ref_to();
let protocol_key = keys.protocol.ref_to();
let eth_cold_key = keys.eth_gov.ref_to();
let eth_hot_key = keys.eth_bridge.ref_to();
all_keys.insert(address.clone(), keys);
GenesisValidator {
address,
tokens,
consensus_key,
protocol_key,
eth_cold_key,
eth_hot_key,
commission_rate: Dec::new(5, 2).unwrap(),
Expand Down Expand Up @@ -270,6 +272,7 @@ pub fn append_validators_to_storage(
let keys = TestValidatorKeys::generate();

let consensus_key = &keys.consensus.ref_to();
let protocol_key = &&keys.protocol.ref_to();
let eth_cold_key = &keys.eth_gov.ref_to();
let eth_hot_key = &keys.eth_bridge.ref_to();

Expand All @@ -278,6 +281,7 @@ pub fn append_validators_to_storage(
params: &params,
address: &validator,
consensus_key,
protocol_key,
eth_cold_key,
eth_hot_key,
current_epoch,
Expand Down
1 change: 1 addition & 0 deletions proof_of_stake/src/epoched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ mod test {
address: established_address_1(),
tokens: token::Amount::native_whole(1_000),
consensus_key: key::testing::keypair_1().to_public(),
protocol_key: key::testing::keypair_2().to_public(),
eth_hot_key: key::testing::keypair_3().to_public(),
eth_cold_key: key::testing::keypair_3().to_public(),
commission_rate: Dec::new(1, 1).expect("Dec creation failed"),
Expand Down
27 changes: 25 additions & 2 deletions proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use namada_core::ledger::storage_api::{
use namada_core::types::address::{Address, InternalAddress};
use namada_core::types::dec::Dec;
use namada_core::types::key::{
common, tm_consensus_key_raw_hash, PublicKeyTmRawHash,
common, protocol_pk_key, tm_consensus_key_raw_hash, PublicKeyTmRawHash,
};
pub use namada_core::types::storage::{Epoch, Key, KeySeg};
use namada_core::types::token;
Expand All @@ -58,7 +58,7 @@ use storage::{
BondsAndUnbondsDetail, BondsAndUnbondsDetails, EpochedSlashes,
ReverseOrdTokenAmount, RewardsAccumulator, SlashedAmount,
TotalConsensusStakes, UnbondDetails, ValidatorAddresses,
ValidatorUnbondRecords,
ValidatorProtocolKeys, ValidatorUnbondRecords,
};
use thiserror::Error;
use types::{
Expand Down Expand Up @@ -261,6 +261,14 @@ pub fn validator_consensus_key_handle(
ValidatorConsensusKeys::open(key)
}

/// Get the storage handle to a PoS validator's protocol key key.
pub fn validator_protocol_key_handle(
validator: &Address,
) -> ValidatorProtocolKeys {
let key = protocol_pk_key(validator);
ValidatorProtocolKeys::open(key)
}

/// Get the storage handle to a PoS validator's eth hot key.
pub fn validator_eth_hot_key_handle(
validator: &Address,
Expand Down Expand Up @@ -414,6 +422,7 @@ where
address,
tokens,
consensus_key,
protocol_key,
eth_cold_key,
eth_hot_key,
commission_rate,
Expand Down Expand Up @@ -453,6 +462,11 @@ where
consensus_key,
current_epoch,
)?;
validator_protocol_key_handle(&address).init_at_genesis(
storage,
protocol_key,
current_epoch,
)?;
validator_eth_hot_key_handle(&address).init_at_genesis(
storage,
eth_hot_key,
Expand Down Expand Up @@ -2159,6 +2173,8 @@ pub struct BecomeValidator<'a, S> {
pub address: &'a Address,
/// The validator's consensus key, used by Tendermint.
pub consensus_key: &'a common::PublicKey,
/// The validator's protocol key.
pub protocol_key: &'a common::PublicKey,
/// The validator's Ethereum bridge cold key.
pub eth_cold_key: &'a common::PublicKey,
/// The validator's Ethereum bridge hot key.
Expand All @@ -2183,6 +2199,7 @@ where
params,
address,
consensus_key,
protocol_key,
eth_cold_key,
eth_hot_key,
current_epoch,
Expand Down Expand Up @@ -2213,6 +2230,12 @@ where
current_epoch,
params.pipeline_len,
)?;
validator_protocol_key_handle(address).set(
storage,
protocol_key.clone(),
current_epoch,
params.pipeline_len,
)?;
validator_eth_hot_key_handle(address).set(
storage,
eth_hot_key.clone(),
Expand Down
37 changes: 17 additions & 20 deletions proof_of_stake/src/pos_queries.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Storage API for querying data about Proof-of-stake related
//! data. This includes validator and epoch related data.
use borsh::{BorshDeserialize, BorshSerialize};
use namada_core::ledger::parameters::storage::get_max_proposal_bytes_key;
use namada_core::ledger::parameters::EpochDuration;
use namada_core::ledger::storage::WlStorage;
Expand Down Expand Up @@ -172,17 +171,17 @@ where
pk: &key::common::PublicKey,
epoch: Option<Epoch>,
) -> Result<WeightedValidator> {
let pk_bytes = pk
.try_to_vec()
.expect("Serializing public key should not fail");
let params = crate::read_pos_params(self.wl_storage)
.expect("Failed to fetch Pos params");
let epoch = epoch
.unwrap_or_else(|| self.wl_storage.storage.get_current_epoch().0);
self.get_consensus_validators(Some(epoch))
.iter()
.find(|validator| {
let pk_key = key::protocol_pk_key(&validator.address);
match self.wl_storage.storage.read(&pk_key) {
Ok((Some(bytes), _)) => bytes == pk_bytes,
let protocol_keys =
crate::validator_protocol_key_handle(&validator.address);
match protocol_keys.get(self.wl_storage, epoch, &params) {
Ok(Some(key)) => key == *pk,
_ => false,
}
})
Expand All @@ -195,26 +194,24 @@ where
address: &Address,
epoch: Option<Epoch>,
) -> Result<(token::Amount, key::common::PublicKey)> {
let params = crate::read_pos_params(self.wl_storage)
.expect("Failed to fetch Pos params");
let epoch = epoch
.unwrap_or_else(|| self.wl_storage.storage.get_current_epoch().0);
self.get_consensus_validators(Some(epoch))
.iter()
.find(|validator| address == &validator.address)
.map(|validator| {
let protocol_pk_key = key::protocol_pk_key(&validator.address);
// TODO: rewrite this, to use `StorageRead::read`
let bytes = self
.wl_storage
.storage
.read(&protocol_pk_key)
.expect("Validator should have public protocol key")
.0
.expect("Validator should have public protocol key");
let protocol_pk: key::common::PublicKey =
BorshDeserialize::deserialize(&mut bytes.as_ref()).expect(
"Protocol public key in storage should be \
deserializable",
let protocol_keys =
crate::validator_protocol_key_handle(&validator.address);
let protocol_pk = protocol_keys
.get(self.wl_storage, epoch, &params)
.unwrap()
.expect(
"Protocol public key should be set in storage after \
genesis.",
);

(validator.bonded_stake, protocol_pk)
})
.ok_or_else(|| Error::NotValidatorAddress(address.clone(), epoch))
Expand Down
Loading

0 comments on commit 64e817b

Please sign in to comment.