Skip to content

Commit

Permalink
Merge branch 'tiago/main/block-space-allocator' (updated #976)
Browse files Browse the repository at this point in the history
* tiago/main/block-space-allocator:
  Improve ErrorCodes::is_recoverable()
  Rename active to consensus validators in PosQueries
  Read token balances with storage_api::token::read_balance()
  • Loading branch information
tzemanovic committed Apr 7, 2023
2 parents 5814cdc + 9d75163 commit 56c02ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
12 changes: 9 additions & 3 deletions apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,21 @@ pub enum ErrorCodes {
ExtraTxs = 5,
Undecryptable = 6,
AllocationError = 7,
ReplayTx = 8, /* NOTE: keep these values in sync with
* [`ErrorCodes::is_recoverable`] */
ReplayTx = 8,
}

impl ErrorCodes {
/// Checks if the given [`ErrorCodes`] value is a protocol level error,
/// that can be recovered from at the finalize block stage.
pub const fn is_recoverable(&self) -> bool {
(*self as u32) == 0
use ErrorCodes::*;
// NOTE: pattern match on all `ErrorCodes` variants, in order
// to catch potential bugs when adding new codes
match self {
Ok | InvalidTx | InvalidSig | WasmRuntimeError => true,
InvalidOrder | ExtraTxs | Undecryptable | AllocationError
| ReplayTx => false,
}
}
}

Expand Down
53 changes: 23 additions & 30 deletions proof_of_stake/src/pos_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ use crate::{consensus_validator_set_handle, PosParams};
/// Errors returned by [`PosQueries`] operations.
#[derive(Error, Debug)]
pub enum Error {
/// The given address is not among the set of active validators for
/// The given address is not among the set of consensus validators for
/// the corresponding epoch.
#[error(
"The address '{0:?}' is not among the active validator set for epoch \
{1}"
"The address '{0:?}' is not among the consensus validator set for \
epoch {1}"
)]
NotValidatorAddress(Address, Epoch),
/// The given public key does not correspond to any active validator's
/// The given public key does not correspond to any consensus validator's
/// key at the provided epoch.
#[error(
"The public key '{0}' is not among the active validator set for epoch \
{1}"
"The public key '{0}' is not among the consensus validator set for \
epoch {1}"
)]
NotValidatorKey(String, Epoch),
/// The given public key hash does not correspond to any active validator's
/// key at the provided epoch.
/// The given public key hash does not correspond to any consensus
/// validator's key at the provided epoch.
#[error(
"The public key hash '{0}' is not among the active validator set for \
epoch {1}"
"The public key hash '{0}' is not among the consensus validator set \
for epoch {1}"
)]
NotValidatorKeyHash(String, Epoch),
/// An invalid Tendermint validator address was detected.
Expand All @@ -50,7 +50,7 @@ pub enum Error {
pub type Result<T> = ::std::result::Result<T, Error>;

/// Methods used to query blockchain proof-of-stake related state,
/// such as the currently active set of validators.
/// such as the current set of consensus validators.
pub trait PosQueries {
/// The underlying storage type.
type Storage;
Expand Down Expand Up @@ -103,16 +103,16 @@ where
self.wl_storage
}

/// Get the set of active validators for a given epoch (defaulting to the
/// Get the set of consensus validators for a given epoch (defaulting to the
/// epoch of the current yet-to-be-committed block).
#[inline]
pub fn get_active_validators(
pub fn get_consensus_validators(
self,
epoch: Option<Epoch>,
) -> ActiveValidators<'db, D, H> {
) -> ConsensusValidators<'db, D, H> {
let epoch = epoch
.unwrap_or_else(|| self.wl_storage.storage.get_current_epoch().0);
ActiveValidators {
ConsensusValidators {
wl_storage: self.wl_storage,
validator_set: consensus_validator_set_handle().at(&epoch),
}
Expand All @@ -121,7 +121,7 @@ where
/// Lookup the total voting power for an epoch (defaulting to the
/// epoch of the current yet-to-be-committed block).
pub fn get_total_voting_power(self, epoch: Option<Epoch>) -> token::Amount {
self.get_active_validators(epoch)
self.get_consensus_validators(epoch)
.iter()
.map(|validator| u64::from(validator.bonded_stake))
.sum::<u64>()
Expand All @@ -135,15 +135,8 @@ where
token: &Address,
owner: &Address,
) -> token::Amount {
let balance = storage_api::StorageRead::read(
self.wl_storage,
&token::balance_key(token, owner),
);
// Storage read must not fail, but there might be no value, in which
// case default (0) is returned
balance
storage_api::token::read_balance(self.wl_storage, token, owner)
.expect("Storage read in the protocol must not fail")
.unwrap_or_default()
}

/// Return evidence parameters.
Expand Down Expand Up @@ -179,7 +172,7 @@ where
) -> Result<(token::Amount, key::common::PublicKey)> {
let epoch = epoch
.unwrap_or_else(|| self.wl_storage.storage.get_current_epoch().0);
self.get_active_validators(Some(epoch))
self.get_consensus_validators(Some(epoch))
.iter()
.find(|validator| address == &validator.address)
.map(|validator| {
Expand Down Expand Up @@ -279,9 +272,9 @@ where
}
}

/// A handle to the set of active validators in Namada,
/// A handle to the set of consensus validators in Namada,
/// at some given epoch.
pub struct ActiveValidators<'db, D, H>
pub struct ConsensusValidators<'db, D, H>
where
D: storage::DB + for<'iter> storage::DBIter<'iter>,
H: storage::StorageHasher,
Expand All @@ -290,19 +283,19 @@ where
validator_set: ConsensusValidatorSet,
}

impl<'db, D, H> ActiveValidators<'db, D, H>
impl<'db, D, H> ConsensusValidators<'db, D, H>
where
D: 'static + storage::DB + for<'iter> storage::DBIter<'iter>,
H: 'static + storage::StorageHasher,
{
/// Iterate over the set of active validators in Namada, at some given
/// Iterate over the set of consensus validators in Namada, at some given
/// epoch.
pub fn iter<'this: 'db>(
&'this self,
) -> impl Iterator<Item = WeightedValidator> + 'db {
self.validator_set
.iter(self.wl_storage)
.expect("Must be able to iterate over active validators")
.expect("Must be able to iterate over consensus validators")
.map(|res| {
let (
NestedSubKey::Data {
Expand Down

0 comments on commit 56c02ce

Please sign in to comment.