Skip to content

Commit

Permalink
cleanup after rebase (updated #677)
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Nov 14, 2022
1 parent 2b23ad2 commit 8cc15c0
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 201 deletions.
15 changes: 2 additions & 13 deletions core/src/ledger/storage/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ics23::{CommitmentProof, ExistenceProof, NonExistenceProof};
use thiserror::Error;

use super::traits::{StorageHasher, SubTreeRead, SubTreeWrite};
use crate::bytes::ByteBuf;
use crate::ledger::storage::ics23_specs::{self, ibc_leaf_spec};
use crate::ledger::storage::types;
use crate::types::address::{Address, InternalAddress};
Expand Down Expand Up @@ -873,16 +874,4 @@ mod test {
);
assert!(basetree_verification_res);
}
}

/// Type of membership proof from a merkle tree
pub enum MembershipProof {
/// ICS23 compliant membership proof
ICS23(CommitmentProof),
}

impl From<CommitmentProof> for MembershipProof {
fn from(proof: CommitmentProof) -> Self {
Self::ICS23(proof)
}
}
}
1 change: 0 additions & 1 deletion core/src/types/transaction/governance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use borsh::{BorshDeserialize, BorshSerialize};
use namada_core::types::governance::{Proposal, ProposalError};
use serde::{Deserialize, Serialize};

use crate::types::address::Address;
Expand Down
16 changes: 9 additions & 7 deletions proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ use parameters::PosParams;
use rust_decimal::Decimal;
use thiserror::Error;
use types::{
ActiveValidator, Bonds, CommissionRate, Epoch, GenesisValidator, GenesisValidator_NEW,
ActiveValidator, Bonds, CommissionRates, GenesisValidator,
Slash, SlashType, Slashes, TotalDeltas, Unbond, Unbonds,
ValidatorConsensusKeys, ValidatorConsensusKeys_NEW, ValidatorSet,
ValidatorSetUpdate, ValidatorSets, ValidatorState, ValidatorStates,
ValidatorDeltas, ValidatorStates_NEW,
ValidatorDeltas_NEW, ValidatorSets_NEW, BondId_NEW
ValidatorDeltas_NEW, ValidatorSets_NEW
};

use crate::btree_set::BTreeSetShims;
Expand Down Expand Up @@ -1703,7 +1703,7 @@ pub fn validator_state_handle(
pub fn validator_deltas_handle(
validator: &Address
) -> ValidatorDeltas_NEW {
let key = storage::validator_total_deltas_key(&validator);
let key = storage::validator_deltas_key(&validator);
crate::epoched_new::EpochedDelta::open(key)
}

Expand All @@ -1712,7 +1712,7 @@ pub fn bond_handle(
source: &Address,
validator: &Address
) -> LazyMap<Epoch, token::Amount> {
let bond_id = BondId_NEW {source: source.clone(), validator: validator.clone()};
let bond_id = BondId {source: source.clone(), validator: validator.clone()};
let key = storage::bond_key(&bond_id);
LazyMap::open(key)
}
Expand All @@ -1721,7 +1721,7 @@ pub fn bond_handle(
pub fn init_genesis_NEW<S>(
storage: &mut S,
params: &PosParams,
validators: impl Iterator<Item = GenesisValidator_NEW> + Clone,
validators: impl Iterator<Item = GenesisValidator> + Clone,
current_epoch: namada_core::types::storage::Epoch,
) -> storage_api::Result<()>
where
Expand All @@ -1732,6 +1732,8 @@ where
address,
tokens,
consensus_key,
commission_rate,
max_commission_rate_change
} in validators
{
validator_consensus_key_handle(&address).init_at_genesis(
Expand Down Expand Up @@ -1761,7 +1763,7 @@ pub fn read_validator_consensus_key<S>(
params: &PosParams,
validator: &Address,
epoch: namada_core::types::storage::Epoch,
) -> storage_api::Result<Option<key::common::PublicKey>>
) -> storage_api::Result<Option<common::PublicKey>>
where
S: for<'iter> StorageRead<'iter>,
{
Expand All @@ -1774,7 +1776,7 @@ pub fn write_validator_consensus_key<S>(
storage: &mut S,
params: &PosParams,
validator: &Address,
consensus_key: key::common::PublicKey,
consensus_key: common::PublicKey,
current_epoch: namada_core::types::storage::Epoch,
) -> storage_api::Result<()>
where
Expand Down
6 changes: 2 additions & 4 deletions proof_of_stake/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use namada_core::ledger::storage::types::{decode, encode};
use namada_core::ledger::storage::{self, Storage, StorageHasher};

use namada_core::types::address::{self, Address};
use namada_core::types::address::Address;
use namada_core::types::storage::{DbKeySeg, Key, KeySeg};
use namada_core::types::{key, token};
use rust_decimal::Decimal;
Expand All @@ -18,7 +18,7 @@ const VALIDATOR_STORAGE_PREFIX: &str = "validator_NEW";
const VALIDATOR_ADDRESS_RAW_HASH: &str = "address_raw_hash_NEW";
const VALIDATOR_CONSENSUS_KEY_STORAGE_KEY: &str = "consensus_key_NEW";
const VALIDATOR_STATE_STORAGE_KEY: &str = "state_NEW";
const VALIDATOR_ELTAS_STORAGE_KEY: &str = "validator_deltas_NEW";
const VALIDATOR_DELTAS_STORAGE_KEY: &str = "validator_deltas_NEW";
const VALIDATOR_COMMISSION_RATE_STORAGE_KEY: &str = "commission_rate_NEW";
const VALIDATOR_MAX_COMMISSION_CHANGE_STORAGE_KEY: &str =
"max_commission_rate_change_NEW";
Expand All @@ -28,8 +28,6 @@ const UNBOND_STORAGE_KEY: &str = "unbond_NEW";
const VALIDATOR_SET_STORAGE_KEY: &str = "validator_set_NEW";
const TOTAL_DELTAS_STORAGE_KEY: &str = "total_deltas_NEW";

const ADDRESS: Address = address::POS;

/// Is the given key a PoS storage key?
pub fn is_pos_key(key: &Key) -> bool {
match &key.segments.get(0) {
Expand Down
26 changes: 1 addition & 25 deletions proof_of_stake/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub type ValidatorStates_NEW = crate::epoched_new::Epoched<

/// Epoched validator sets.
pub type ValidatorSets_NEW = crate::epoched_new::Epoched<
ValidatorSet_NEW,
ValidatorSet,
crate::epoched_new::OffsetPipelineLen,
0
>;
Expand Down Expand Up @@ -100,8 +100,6 @@ pub struct GenesisValidator {
/// Maximum change in commission rate permitted per epoch
pub max_commission_rate_change: Decimal,
}
pub type GenesisValidator_NEW =
GenesisValidator<Address, token::Amount, key::common::PublicKey>;

/// An update of the active and inactive validator set.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -141,26 +139,6 @@ pub struct BondId {
pub validator: Address,
}

/// ID of a bond and/or an unbond.
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
BorshDeserialize,
BorshSerialize,
BorshSchema,
)]
pub struct BondId_NEW {
/// (Un)bond's source address is the owner of the bonded tokens.
pub source: Address,
/// (Un)bond's validator address.
pub validator: Address,
}

/// Validator's address with its voting power.
#[derive(
Debug,
Expand Down Expand Up @@ -213,8 +191,6 @@ pub struct ValidatorSet {
pub inactive: BTreeSet<WeightedValidator>,
}

pub type ValidatorSet_NEW = ValidatorSet<Address>;

/// Validator's state.
#[derive(
Debug,
Expand Down
158 changes: 12 additions & 146 deletions shared/src/ledger/pos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ use crate::ledger::storage::{self as ledger_storage, Storage, StorageHasher};
use crate::types::address::{Address, InternalAddress};
use crate::types::storage::Epoch;

pub use namada_core::types::key::common;
pub use namada_core::types::token;
pub use namada_core::ledger::storage_api;


/// Address of the PoS account implemented as a native VP
pub const ADDRESS: Address = Address::Internal(InternalAddress::PoS);
Expand Down Expand Up @@ -69,161 +73,23 @@ pub fn init_genesis_storage_NEW<DB, H>(

/// Alias for a PoS type with the same name with concrete type parameters
pub type ValidatorConsensusKeys =
namada_proof_of_stake::types::ValidatorConsensusKeys<
key::common::PublicKey,
>;
namada_proof_of_stake::types::ValidatorConsensusKeys;

/// Alias for a PoS type with the same name with concrete type parameters
pub type ValidatorTotalDeltas =
namada_proof_of_stake::types::ValidatorTotalDeltas<token::Change>;
pub type ValidatorDeltas =
namada_proof_of_stake::types::ValidatorDeltas;

/// Alias for a PoS type with the same name with concrete type parameters
pub type Bonds = namada_proof_of_stake::types::Bonds<token::Amount>;
pub type Bonds = namada_proof_of_stake::types::Bonds;

/// Alias for a PoS type with the same name with concrete type parameters
pub type Unbonds = namada_proof_of_stake::types::Unbonds<token::Amount>;
pub type Unbonds = namada_proof_of_stake::types::Unbonds;

/// Alias for a PoS type with the same name with concrete type parameters
pub type ValidatorSets = namada_proof_of_stake::types::ValidatorSets<Address>;
pub type ValidatorSets = namada_proof_of_stake::types::ValidatorSets;

/// Alias for a PoS type with the same name with concrete type parameters
pub type BondId = namada_proof_of_stake::types::BondId<Address>;
pub type BondId = namada_proof_of_stake::types::BondId;

/// Alias for a PoS type with the same name with concrete type parameters
pub type GenesisValidator = namada_proof_of_stake::types::GenesisValidator<
Address,
token::Amount,
key::common::PublicKey,
>;

#[macro_use]
mod macros {
/// Implement `PosReadOnly` for a type that implements
/// [`trait@crate::ledger::storage_api::StorageRead`].
///
/// Excuse the horrible syntax - we haven't found a better way to use this
/// for native_vp `CtxPreStorageRead`/`CtxPostStorageRead`, which have
/// generics and explicit lifetimes.
///
/// # Examples
///
/// ```ignore
/// impl_pos_read_only! { impl PosReadOnly for X }
/// ```
#[macro_export]
macro_rules! impl_pos_read_only {
(
// Type error type has to be declared before the impl.
// This error type must `impl From<storage_api::Error> for $error`.
type $error:tt = $err_ty:ty ;
// Matches anything, so that we can use lifetimes and generic types.
// This expects `impl(<.*>)? PoSReadOnly for $ty(<.*>)?`.
$( $any:tt )* )
=> {
$( $any )*
{
type Address = $crate::types::address::Address;
type $error = $err_ty;
type PublicKey = $crate::types::key::common::PublicKey;
type TokenAmount = $crate::types::token::Amount;
type TokenChange = $crate::types::token::Change;

const POS_ADDRESS: Self::Address = $crate::ledger::pos::ADDRESS;

fn staking_token_address() -> Self::Address {
$crate::ledger::pos::staking_token_address()
}

fn read_pos_params(&self) -> std::result::Result<PosParams, Self::Error> {
let value = $crate::ledger::storage_api::StorageRead::read_bytes(self, &params_key())?.unwrap();
Ok($crate::ledger::storage::types::decode(value).unwrap())
}

fn read_validator_consensus_key(
&self,
key: &Self::Address,
) -> std::result::Result<Option<ValidatorConsensusKeys>, Self::Error> {
let value =
$crate::ledger::storage_api::StorageRead::read_bytes(self, &validator_consensus_key_key(key))?;
Ok(value.map(|value| $crate::ledger::storage::types::decode(value).unwrap()))
}

fn read_validator_state(
&self,
key: &Self::Address,
) -> std::result::Result<Option<ValidatorStates>, Self::Error> {
let value = $crate::ledger::storage_api::StorageRead::read_bytes(self, &validator_state_key(key))?;
Ok(value.map(|value| $crate::ledger::storage::types::decode(value).unwrap()))
}

fn read_validator_total_deltas(
&self,
key: &Self::Address,
) -> std::result::Result<Option<ValidatorTotalDeltas>, Self::Error> {
let value =
$crate::ledger::storage_api::StorageRead::read_bytes(self, &validator_total_deltas_key(key))?;
Ok(value.map(|value| $crate::ledger::storage::types::decode(value).unwrap()))
}

fn read_validator_voting_power(
&self,
key: &Self::Address,
) -> std::result::Result<Option<ValidatorVotingPowers>, Self::Error> {
let value =
$crate::ledger::storage_api::StorageRead::read_bytes(self, &validator_voting_power_key(key))?;
Ok(value.map(|value| $crate::ledger::storage::types::decode(value).unwrap()))
}

fn read_validator_slashes(
&self,
key: &Self::Address,
) -> std::result::Result<Vec<types::Slash>, Self::Error> {
let value = $crate::ledger::storage_api::StorageRead::read_bytes(self, &validator_slashes_key(key))?;
Ok(value
.map(|value| $crate::ledger::storage::types::decode(value).unwrap())
.unwrap_or_default())
}

fn read_bond(
&self,
key: &BondId,
) -> std::result::Result<Option<Bonds>, Self::Error> {
let value = $crate::ledger::storage_api::StorageRead::read_bytes(self, &bond_key(key))?;
Ok(value.map(|value| $crate::ledger::storage::types::decode(value).unwrap()))
}

fn read_unbond(
&self,
key: &BondId,
) -> std::result::Result<Option<Unbonds>, Self::Error> {
let value = $crate::ledger::storage_api::StorageRead::read_bytes(self, &unbond_key(key))?;
Ok(value.map(|value| $crate::ledger::storage::types::decode(value).unwrap()))
}

fn read_validator_set(
&self,
) -> std::result::Result<ValidatorSets, Self::Error> {
let value =
$crate::ledger::storage_api::StorageRead::read_bytes(self, &validator_set_key())?.unwrap();
Ok($crate::ledger::storage::types::decode(value).unwrap())
}

fn read_total_voting_power(
&self,
) -> std::result::Result<TotalVotingPowers, Self::Error> {
let value =
$crate::ledger::storage_api::StorageRead::read_bytes(self, &total_voting_power_key())?.unwrap();
Ok($crate::ledger::storage::types::decode(value).unwrap())
}
}
}
}
}

impl_pos_read_only! {
type Error = storage_api::Error;
impl<DB, H> PosReadOnly for Storage<DB, H>
where
DB: ledger_storage::DB + for<'iter> ledger_storage::DBIter<'iter> +'static,
H: StorageHasher +'static,
}
pub type GenesisValidator = namada_proof_of_stake::types::GenesisValidator;
3 changes: 1 addition & 2 deletions shared/src/types/key/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! Cryptographic keys

pub use namada_core::types::key::*;
pub mod dkg_session_keys;
pub use namada_core::types::key::*;
1 change: 0 additions & 1 deletion shared/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

pub mod ibc;
pub mod key;
pub mod transaction;

pub use namada_core::types::{
address, chain, governance, hash, internal, storage, time, token,
Expand Down
2 changes: 1 addition & 1 deletion tests/src/native_vp/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@

use namada::ledger::pos::namada_proof_of_stake::PosBase;
use namada::proof_of_stake::storage::GenesisValidator;
use namada::proof_of_stake::PosParams;
use namada::proof_of_stake::parameters::PosParams;
use namada::types::storage::Epoch;

use crate::tx::tx_host_env;
Expand Down
2 changes: 1 addition & 1 deletion tx_prelude/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl StorageWrite for Ctx {
}

impl TxEnv<'_> for Ctx {
type IbcEvent = namada::types::ibc::IbcEvent;
type IbcEvent = namada_core::types::ibc::IbcEvent;

fn get_block_time(&self) -> Result<time::Rfc3339String, Error> {
let read_result = unsafe { anoma_tx_get_block_time() };
Expand Down

0 comments on commit 8cc15c0

Please sign in to comment.