Skip to content

Commit

Permalink
ledger/pos: implement PosReadOnly using StorageRead trait
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Aug 12, 2022
1 parent 8837b87 commit e8d6014
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 254 deletions.
136 changes: 136 additions & 0 deletions shared/src/ledger/pos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,139 @@ impl From<namada_proof_of_stake::types::Epoch> for Epoch {
Epoch(epoch)
}
}

#[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 = $crate::ledger::native_vp::Error;
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_staking_reward_address(
&self,
key: &Self::Address,
) -> std::result::Result<Option<Self::Address>, Self::Error> {
let value = $crate::ledger::storage_api::StorageRead::read_bytes(
self,
&validator_staking_reward_address_key(key),
)?;
Ok(value.map(|value| $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())
}
}
}
}
}
Loading

0 comments on commit e8d6014

Please sign in to comment.