Skip to content

Commit

Permalink
refactor: move validator accessor to EpochManagerHandle (#7655)
Browse files Browse the repository at this point in the history
Part of #6910, depends on #7652

Must be reviewed per commit.
  • Loading branch information
matklad authored and nikurt committed Nov 9, 2022
1 parent 97007a3 commit 8753f18
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 206 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

177 changes: 89 additions & 88 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,95 @@ impl EpochManagerAdapter for KeyValueRuntime {
fn epoch_exists(&self, epoch_id: &EpochId) -> bool {
self.hash_to_valset.write().unwrap().contains_key(epoch_id)
}

fn get_epoch_block_producers_ordered(
&self,
epoch_id: &EpochId,
_last_known_block_hash: &CryptoHash,
) -> Result<Vec<(ValidatorStake, bool)>, Error> {
let validators = self.get_block_producers(self.get_valset_for_epoch(epoch_id)?);
Ok(validators.iter().map(|x| (x.clone(), false)).collect())
}

fn get_epoch_block_approvers_ordered(
&self,
parent_hash: &CryptoHash,
) -> Result<Vec<(ApprovalStake, bool)>, Error> {
let (_cur_epoch, cur_valset, next_epoch) = self.get_epoch_and_valset(*parent_hash)?;
let mut validators = self
.get_block_producers(cur_valset)
.iter()
.map(|x| x.get_approval_stake(false))
.collect::<Vec<_>>();
if *self.hash_to_next_epoch_approvals_req.write().unwrap().get(parent_hash).unwrap() {
let validators_copy = validators.clone();
validators.extend(
self.get_block_producers(self.get_valset_for_epoch(&next_epoch)?)
.iter()
.filter(|x| {
!validators_copy.iter().any(|entry| &entry.account_id == x.account_id())
})
.map(|x| x.get_approval_stake(true)),
);
}
let validators = validators.into_iter().map(|stake| (stake, false)).collect::<Vec<_>>();
Ok(validators)
}

fn get_epoch_chunk_producers(&self, _epoch_id: &EpochId) -> Result<Vec<ValidatorStake>, Error> {
tracing::warn!("not implemented, returning a dummy value");
Ok(vec![])
}

fn get_block_producer(
&self,
epoch_id: &EpochId,
height: BlockHeight,
) -> Result<AccountId, Error> {
let validators = self.get_block_producers(self.get_valset_for_epoch(epoch_id)?);
Ok(validators[(height as usize) % validators.len()].account_id().clone())
}

fn get_chunk_producer(
&self,
epoch_id: &EpochId,
height: BlockHeight,
shard_id: ShardId,
) -> Result<AccountId, Error> {
let valset = self.get_valset_for_epoch(epoch_id)?;
let chunk_producers = self.get_chunk_producers(valset, shard_id);
let index = (shard_id + height + 1) as usize % chunk_producers.len();
Ok(chunk_producers[index].account_id().clone())
}

fn get_validator_by_account_id(
&self,
epoch_id: &EpochId,
_last_known_block_hash: &CryptoHash,
account_id: &AccountId,
) -> Result<(ValidatorStake, bool), Error> {
let validators = &self.validators_by_valset[self.get_valset_for_epoch(epoch_id)?];
for validator_stake in validators.block_producers.iter() {
if validator_stake.account_id() == account_id {
return Ok((validator_stake.clone(), false));
}
}
for validator_stake in validators.chunk_producers.iter().flatten() {
if validator_stake.account_id() == account_id {
return Ok((validator_stake.clone(), false));
}
}
Err(Error::NotAValidator)
}

fn get_fisherman_by_account_id(
&self,
_epoch_id: &EpochId,
_last_known_block_hash: &CryptoHash,
_account_id: &AccountId,
) -> Result<(ValidatorStake, bool), Error> {
Err(Error::NotAValidator)
}
}

impl RuntimeAdapter for KeyValueRuntime {
Expand Down Expand Up @@ -543,65 +632,6 @@ impl RuntimeAdapter for KeyValueRuntime {
}
}

fn get_epoch_block_producers_ordered(
&self,
epoch_id: &EpochId,
_last_known_block_hash: &CryptoHash,
) -> Result<Vec<(ValidatorStake, bool)>, Error> {
let validators = self.get_block_producers(self.get_valset_for_epoch(epoch_id)?);
Ok(validators.iter().map(|x| (x.clone(), false)).collect())
}

fn get_epoch_block_approvers_ordered(
&self,
parent_hash: &CryptoHash,
) -> Result<Vec<(ApprovalStake, bool)>, Error> {
let (_cur_epoch, cur_valset, next_epoch) = self.get_epoch_and_valset(*parent_hash)?;
let mut validators = self
.get_block_producers(cur_valset)
.iter()
.map(|x| x.get_approval_stake(false))
.collect::<Vec<_>>();
if *self.hash_to_next_epoch_approvals_req.write().unwrap().get(parent_hash).unwrap() {
let validators_copy = validators.clone();
validators.extend(
self.get_block_producers(self.get_valset_for_epoch(&next_epoch)?)
.iter()
.filter(|x| {
!validators_copy.iter().any(|entry| &entry.account_id == x.account_id())
})
.map(|x| x.get_approval_stake(true)),
);
}
let validators = validators.into_iter().map(|stake| (stake, false)).collect::<Vec<_>>();
Ok(validators)
}
fn get_epoch_chunk_producers(&self, _epoch_id: &EpochId) -> Result<Vec<ValidatorStake>, Error> {
tracing::warn!("not implemented, returning a dummy value");
Ok(vec![])
}

fn get_block_producer(
&self,
epoch_id: &EpochId,
height: BlockHeight,
) -> Result<AccountId, Error> {
let validators = self.get_block_producers(self.get_valset_for_epoch(epoch_id)?);
Ok(validators[(height as usize) % validators.len()].account_id().clone())
}

fn get_chunk_producer(
&self,
epoch_id: &EpochId,
height: BlockHeight,
shard_id: ShardId,
) -> Result<AccountId, Error> {
let valset = self.get_valset_for_epoch(epoch_id)?;
let chunk_producers = self.get_chunk_producers(valset, shard_id);
let index = (shard_id + height + 1) as usize % chunk_producers.len();
Ok(chunk_producers[index].account_id().clone())
}

fn num_shards(&self, _epoch_id: &EpochId) -> Result<ShardId, Error> {
Ok(self.num_shards)
}
Expand Down Expand Up @@ -1254,35 +1284,6 @@ impl RuntimeAdapter for KeyValueRuntime {
Ok(true)
}

fn get_validator_by_account_id(
&self,
epoch_id: &EpochId,
_last_known_block_hash: &CryptoHash,
account_id: &AccountId,
) -> Result<(ValidatorStake, bool), Error> {
let validators = &self.validators_by_valset[self.get_valset_for_epoch(epoch_id)?];
for validator_stake in validators.block_producers.iter() {
if validator_stake.account_id() == account_id {
return Ok((validator_stake.clone(), false));
}
}
for validator_stake in validators.chunk_producers.iter().flatten() {
if validator_stake.account_id() == account_id {
return Ok((validator_stake.clone(), false));
}
}
Err(Error::NotAValidator)
}

fn get_fisherman_by_account_id(
&self,
_epoch_id: &EpochId,
_last_known_block_hash: &CryptoHash,
_account_id: &AccountId,
) -> Result<(ValidatorStake, bool), Error> {
Err(Error::NotAValidator)
}

fn get_protocol_config(&self, _epoch_id: &EpochId) -> Result<ProtocolConfig, Error> {
unreachable!("get_protocol_config should not be called in KeyValueRuntime");
}
Expand Down
53 changes: 4 additions & 49 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::Arc;

use borsh::{BorshDeserialize, BorshSerialize};
use chrono::DateTime;
use near_epoch_manager::EpochManagerAdapter;
use near_primitives::sandbox::state_patch::SandboxStatePatch;
use near_primitives::time::Utc;
use num_rational::Rational32;
Expand All @@ -30,19 +29,20 @@ use near_primitives::state_part::PartId;
use near_primitives::transaction::{ExecutionOutcomeWithId, SignedTransaction};
use near_primitives::types::validator_stake::{ValidatorStake, ValidatorStakeIter};
use near_primitives::types::{
AccountId, ApprovalStake, Balance, BlockHeight, BlockHeightDelta, EpochHeight, EpochId, Gas,
MerkleHash, NumBlocks, ShardId, StateChangesForSplitStates, StateRoot, StateRootNode,
AccountId, Balance, BlockHeight, BlockHeightDelta, EpochHeight, EpochId, Gas, MerkleHash,
NumBlocks, ShardId, StateChangesForSplitStates, StateRoot, StateRootNode,
ValidatorInfoIdentifier,
};
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_store::flat_state::FlatStorageState;
use near_store::{PartialStorage, ShardTries, Store, StoreUpdate, Trie, WrappedTrieChanges};

pub use near_epoch_manager::EpochManagerAdapter;
pub use near_primitives::block::{Block, BlockHeader, Tip};
use near_store::flat_state::FlatStorageState;

#[derive(Eq, PartialEq, Debug, Clone)]
pub enum BlockStatus {
Expand Down Expand Up @@ -419,51 +419,6 @@ pub trait RuntimeAdapter: EpochManagerAdapter + Send + Sync {
approvals: &[Option<Signature>],
) -> Result<(), Error>;

/// Epoch block producers ordered by their order in the proposals.
/// Returns error if height is outside of known boundaries.
fn get_epoch_block_producers_ordered(
&self,
epoch_id: &EpochId,
last_known_block_hash: &CryptoHash,
) -> Result<Vec<(ValidatorStake, bool)>, Error>;

fn get_epoch_block_approvers_ordered(
&self,
parent_hash: &CryptoHash,
) -> Result<Vec<(ApprovalStake, bool)>, Error>;

/// Returns all the chunk producers for a given epoch.
fn get_epoch_chunk_producers(&self, epoch_id: &EpochId) -> Result<Vec<ValidatorStake>, Error>;

/// Block producers for given height for the main block. Return error if outside of known boundaries.
fn get_block_producer(
&self,
epoch_id: &EpochId,
height: BlockHeight,
) -> Result<AccountId, Error>;

/// Chunk producer for given height for given shard. Return error if outside of known boundaries.
fn get_chunk_producer(
&self,
epoch_id: &EpochId,
height: BlockHeight,
shard_id: ShardId,
) -> Result<AccountId, Error>;

fn get_validator_by_account_id(
&self,
epoch_id: &EpochId,
last_known_block_hash: &CryptoHash,
account_id: &AccountId,
) -> Result<(ValidatorStake, bool), Error>;

fn get_fisherman_by_account_id(
&self,
epoch_id: &EpochId,
last_known_block_hash: &CryptoHash,
account_id: &AccountId,
) -> Result<(ValidatorStake, bool), Error>;

/// Get current number of shards.
fn num_shards(&self, epoch_id: &EpochId) -> Result<ShardId, Error>;

Expand Down
1 change: 1 addition & 0 deletions chain/chunks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2283,6 +2283,7 @@ impl ShardsManager {
mod test {

use assert_matches::assert_matches;
use near_chain::types::EpochManagerAdapter;
use std::sync::Arc;
use std::time::Duration;

Expand Down
2 changes: 1 addition & 1 deletion chain/chunks/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use near_primitives::time::Clock;

use near_chain::test_utils::{KeyValueRuntime, ValidatorSchedule};
use near_chain::types::{RuntimeAdapter, Tip};
use near_chain::types::{EpochManagerAdapter, RuntimeAdapter, Tip};
use near_chain::{Chain, ChainStore};
use near_crypto::KeyType;
use near_network::test_utils::MockPeerManagerAdapter;
Expand Down
1 change: 1 addition & 0 deletions chain/epoch-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ near-crypto = { path = "../../core/crypto" }
near-primitives = { path = "../../core/primitives" }
near-store = { path = "../../core/store" }
near-chain-configs = { path = "../../core/chain-configs" }
near-chain-primitives = { path = "../chain-primitives" }
near-cache = { path = "../../utils/near-cache" }

[features]
Expand Down
Loading

0 comments on commit 8753f18

Please sign in to comment.