Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move get_validator_info to EpochManagerAdapter #7727

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,22 @@ impl EpochManagerAdapter for KeyValueRuntime {
Err(Error::NotAValidator)
}

fn get_validator_info(
&self,
_epoch_id: ValidatorInfoIdentifier,
) -> Result<EpochValidatorInfo, Error> {
Ok(EpochValidatorInfo {
current_validators: vec![],
next_validators: vec![],
current_fishermen: vec![],
next_fishermen: vec![],
current_proposals: vec![],
prev_epoch_kickout: vec![],
epoch_start_height: 0,
epoch_height: 1,
})
}

fn verify_block_vrf(
&self,
_epoch_id: &EpochId,
Expand Down Expand Up @@ -1274,22 +1290,6 @@ impl RuntimeAdapter for KeyValueRuntime {
Ok(PROTOCOL_VERSION)
}

fn get_validator_info(
&self,
_epoch_id: ValidatorInfoIdentifier,
) -> Result<EpochValidatorInfo, Error> {
Ok(EpochValidatorInfo {
current_validators: vec![],
next_validators: vec![],
current_fishermen: vec![],
next_fishermen: vec![],
current_proposals: vec![],
prev_epoch_kickout: vec![],
epoch_start_height: 0,
epoch_height: 1,
})
}

fn compare_epoch_id(
&self,
epoch_id: &EpochId,
Expand Down
10 changes: 2 additions & 8 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ use near_primitives::transaction::{ExecutionOutcomeWithId, SignedTransaction};
use near_primitives::types::validator_stake::{ValidatorStake, ValidatorStakeIter};
use near_primitives::types::{
AccountId, Balance, BlockHeight, BlockHeightDelta, EpochId, Gas, MerkleHash, NumBlocks,
ShardId, StateChangesForSplitStates, StateRoot, StateRootNode, ValidatorInfoIdentifier,
ShardId, StateChangesForSplitStates, StateRoot, StateRootNode,
};
use near_primitives::version::{
ProtocolVersion, MIN_GAS_PRICE_NEP_92, MIN_GAS_PRICE_NEP_92_FIX, MIN_PROTOCOL_VERSION_NEP_92,
MIN_PROTOCOL_VERSION_NEP_92_FIX,
};
use near_primitives::views::{EpochValidatorInfo, QueryRequest, QueryResponse};
use near_primitives::views::{QueryRequest, QueryResponse};
#[cfg(feature = "protocol_feature_flat_state")]
use near_store::flat_state::ChainAccessForFlatStorage;
use near_store::flat_state::FlatStorageState;
Expand Down Expand Up @@ -561,12 +561,6 @@ pub trait RuntimeAdapter: EpochManagerAdapter + Send + Sync {
request: &QueryRequest,
) -> Result<QueryResponse, near_chain_primitives::error::QueryError>;

/// WARNING: this call may be expensive.
fn get_validator_info(
&self,
epoch_id: ValidatorInfoIdentifier,
) -> Result<EpochValidatorInfo, Error>;

/// Get the part of the state from given state root.
/// `block_hash` is a block whose `prev_state_root` is `state_root`
fn obtain_state_part(
Expand Down
22 changes: 21 additions & 1 deletion chain/epoch-manager/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use near_primitives::{
sharding::{ChunkHash, ShardChunkHeader},
types::{
validator_stake::ValidatorStake, AccountId, ApprovalStake, Balance, BlockHeight,
EpochHeight, EpochId, NumShards, ShardId,
EpochHeight, EpochId, NumShards, ShardId, ValidatorInfoIdentifier,
},
views::EpochValidatorInfo,
};
use near_store::ShardUId;

Expand Down Expand Up @@ -130,6 +131,15 @@ pub trait EpochManagerAdapter: Send + Sync {
account_id: &AccountId,
) -> Result<(ValidatorStake, bool), Error>;

/// WARNING: this call may be expensive.
///
/// This function is intended for diagnostic use in logging & rpc, don't use
/// it for "production" code.
fn get_validator_info(
&self,
epoch_id: ValidatorInfoIdentifier,
) -> Result<EpochValidatorInfo, Error>;

fn verify_block_vrf(
&self,
epoch_id: &EpochId,
Expand Down Expand Up @@ -416,6 +426,16 @@ impl<T: HasEpochMangerHandle + Send + Sync> EpochManagerAdapter for T {
Ok((fisherman, block_info.slashed().contains_key(account_id)))
}

/// WARNING: this function calls EpochManager::get_epoch_info_aggregator_upto_last
/// underneath which can be very expensive.
fn get_validator_info(
&self,
epoch_id: ValidatorInfoIdentifier,
) -> Result<EpochValidatorInfo, Error> {
let epoch_manager = self.read();
epoch_manager.get_validator_info(epoch_id).map_err(|e| e.into())
}

fn verify_block_vrf(
&self,
epoch_id: &EpochId,
Expand Down
23 changes: 8 additions & 15 deletions nearcore/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ use near_primitives::types::validator_stake::ValidatorStakeIter;
use near_primitives::types::{
AccountId, Balance, BlockHeight, CompiledContractCache, EpochHeight, EpochId,
EpochInfoProvider, Gas, MerkleHash, NumShards, ShardId, StateChangeCause,
StateChangesForSplitStates, StateRoot, StateRootNode, ValidatorInfoIdentifier,
StateChangesForSplitStates, StateRoot, StateRootNode,
};
use near_primitives::version::ProtocolVersion;
use near_primitives::views::{
AccessKeyInfoView, CallResult, EpochValidatorInfo, QueryRequest, QueryResponse,
QueryResponseKind, ViewApplyState, ViewStateResult,
AccessKeyInfoView, CallResult, QueryRequest, QueryResponse, QueryResponseKind, ViewApplyState,
ViewStateResult,
};
#[cfg(feature = "protocol_feature_flat_state")]
use near_store::flat_state::ChainAccessForFlatStorage;
Expand Down Expand Up @@ -1280,16 +1280,6 @@ impl RuntimeAdapter for NightshadeRuntime {
}
}

/// WARNING: this function calls EpochManager::get_epoch_info_aggregator_upto_last
/// underneath which can be very expensive.
fn get_validator_info(
&self,
epoch_id: ValidatorInfoIdentifier,
) -> Result<EpochValidatorInfo, Error> {
let epoch_manager = self.epoch_manager.read();
epoch_manager.get_validator_info(epoch_id).map_err(|e| e.into())
}

/// Returns StorageError when storage is inconsistent.
/// This is possible with the used isolation level + running ViewClient in a separate thread
/// `block_hash` is a block whose `prev_state_root` is `state_root`
Expand Down Expand Up @@ -1654,10 +1644,13 @@ mod test {
use near_primitives::block::Tip;
use near_primitives::challenge::SlashedValidator;
use near_primitives::transaction::{Action, DeleteAccountAction, StakeAction, TransferAction};
use near_primitives::types::{BlockHeightDelta, Nonce, ValidatorId, ValidatorKickoutReason};
use near_primitives::types::{
BlockHeightDelta, Nonce, ValidatorId, ValidatorInfoIdentifier, ValidatorKickoutReason,
};
use near_primitives::validator_signer::{InMemoryValidatorSigner, ValidatorSigner};
use near_primitives::views::{
AccountView, CurrentEpochValidatorInfo, NextEpochValidatorInfo, ValidatorKickoutView,
AccountView, CurrentEpochValidatorInfo, EpochValidatorInfo, NextEpochValidatorInfo,
ValidatorKickoutView,
};
use near_store::{NodeStorage, Temperature};

Expand Down