Skip to content

Commit

Permalink
refactor: use EpochError::NotAValidaotor (#7438)
Browse files Browse the repository at this point in the history
We have `EpochError::NotAValidatora` and chain::Error::NotAValidator.

Surprisingly, the epoch one isn't actually used, and instead epoch
manager uses convention of returning `None`, which is then converted by
RuntimeAdapter to `NotAValidator` manually.

This is inconsistent: we should either remove unused error, or actually
start using it.

This PR chose the second approach, primarily to make sure we don't have
different rules for runtime and epoch_manager.
  • Loading branch information
matklad authored Aug 26, 2022
1 parent 046e1d0 commit a65bc1a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
1 change: 1 addition & 0 deletions chain/chain-primitives/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ impl From<EpochError> for Error {
match error {
EpochError::EpochOutOfBounds(epoch_id) => Error::EpochOutOfBounds(epoch_id),
EpochError::MissingBlock(h) => Error::DBNotFoundErr(h.to_string()),
EpochError::NotAValidator(_account_id, _epoch_id) => Error::NotAValidator,
err => Error::ValidatorError(err.to_string()),
}
}
Expand Down
12 changes: 8 additions & 4 deletions chain/epoch-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,19 +955,23 @@ impl EpochManager {
&self,
epoch_id: &EpochId,
account_id: &AccountId,
) -> Result<Option<ValidatorStake>, EpochError> {
) -> Result<ValidatorStake, EpochError> {
let epoch_info = self.get_epoch_info(epoch_id)?;
Ok(epoch_info.get_validator_by_account(account_id))
epoch_info
.get_validator_by_account(account_id)
.ok_or_else(|| EpochError::NotAValidator(account_id.clone(), epoch_id.clone()))
}

/// Returns fisherman for given account id for given epoch.
pub fn get_fisherman_by_account_id(
&self,
epoch_id: &EpochId,
account_id: &AccountId,
) -> Result<Option<ValidatorStake>, EpochError> {
) -> Result<ValidatorStake, EpochError> {
let epoch_info = self.get_epoch_info(epoch_id)?;
Ok(epoch_info.get_fisherman_by_account(account_id))
epoch_info
.get_fisherman_by_account(account_id)
.ok_or_else(|| EpochError::NotAValidator(account_id.clone(), epoch_id.clone()))
}

pub fn get_epoch_id(&self, block_hash: &CryptoHash) -> Result<EpochId, EpochError> {
Expand Down
38 changes: 12 additions & 26 deletions nearcore/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,17 +907,13 @@ impl RuntimeAdapter for NightshadeRuntime {
shard_id: ShardId,
) -> Result<bool, Error> {
let epoch_manager = self.epoch_manager.read();
if let Ok(chunk_producer) =
epoch_manager.get_chunk_producer_info(epoch_id, height_created, shard_id)
{
let block_info = epoch_manager.get_block_info(last_known_hash)?;
if block_info.slashed().contains_key(chunk_producer.account_id()) {
return Ok(false);
}
Ok(signature.verify(chunk_hash.as_ref(), chunk_producer.public_key()))
} else {
Err(Error::NotAValidator)
let chunk_producer =
epoch_manager.get_chunk_producer_info(epoch_id, height_created, shard_id)?;
let block_info = epoch_manager.get_block_info(last_known_hash)?;
if block_info.slashed().contains_key(chunk_producer.account_id()) {
return Ok(false);
}
Ok(signature.verify(chunk_hash.as_ref(), chunk_producer.public_key()))
}

fn verify_approvals_and_threshold_orphan(
Expand Down Expand Up @@ -1043,14 +1039,9 @@ impl RuntimeAdapter for NightshadeRuntime {
account_id: &AccountId,
) -> Result<(ValidatorStake, bool), Error> {
let epoch_manager = self.epoch_manager.read();
match epoch_manager.get_validator_by_account_id(epoch_id, account_id) {
Ok(Some(validator)) => {
let block_info = epoch_manager.get_block_info(last_known_block_hash)?;
Ok((validator, block_info.slashed().contains_key(account_id)))
}
Ok(None) => Err(Error::NotAValidator),
Err(e) => Err(e.into()),
}
let validator = epoch_manager.get_validator_by_account_id(epoch_id, account_id)?;
let block_info = epoch_manager.get_block_info(last_known_block_hash)?;
Ok((validator, block_info.slashed().contains_key(account_id)))
}

fn get_fisherman_by_account_id(
Expand All @@ -1060,14 +1051,9 @@ impl RuntimeAdapter for NightshadeRuntime {
account_id: &AccountId,
) -> Result<(ValidatorStake, bool), Error> {
let epoch_manager = self.epoch_manager.read();
match epoch_manager.get_fisherman_by_account_id(epoch_id, account_id) {
Ok(Some(fisherman)) => {
let block_info = epoch_manager.get_block_info(last_known_block_hash)?;
Ok((fisherman, block_info.slashed().contains_key(account_id)))
}
Ok(None) => Err(Error::NotAValidator),
Err(e) => Err(e.into()),
}
let fisherman = epoch_manager.get_fisherman_by_account_id(epoch_id, account_id)?;
let block_info = epoch_manager.get_block_info(last_known_block_hash)?;
Ok((fisherman, block_info.slashed().contains_key(account_id)))
}

fn num_shards(&self, epoch_id: &EpochId) -> Result<NumShards, Error> {
Expand Down

0 comments on commit a65bc1a

Please sign in to comment.