Skip to content

Commit

Permalink
refactor: move epoch_exists to EpochManagerAdapter
Browse files Browse the repository at this point in the history
Just to show how it works! Expect this repeated a bunch of times soon!
  • Loading branch information
matklad committed Sep 20, 2022
1 parent 1bcd0c2 commit 34bdf28
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
10 changes: 5 additions & 5 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,11 @@ impl KeyValueRuntime {
}
}

impl EpochManagerAdapter for KeyValueRuntime {}
impl EpochManagerAdapter for KeyValueRuntime {
fn epoch_exists(&self, epoch_id: &EpochId) -> bool {
self.hash_to_valset.write().unwrap().contains_key(epoch_id)
}
}

impl RuntimeAdapter for KeyValueRuntime {
fn genesis_state(&self) -> (Store, Vec<StateRoot>) {
Expand Down Expand Up @@ -1159,10 +1163,6 @@ impl RuntimeAdapter for KeyValueRuntime {
}
}

fn epoch_exists(&self, epoch_id: &EpochId) -> bool {
self.hash_to_valset.write().unwrap().contains_key(epoch_id)
}

fn get_epoch_minted_amount(&self, _epoch_id: &EpochId) -> Result<Balance, Error> {
Ok(0)
}
Expand Down
3 changes: 0 additions & 3 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,6 @@ pub trait RuntimeAdapter: EpochManagerAdapter + Send + Sync {
/// Get the block height for which garbage collection should not go over
fn get_gc_stop_height(&self, block_hash: &CryptoHash) -> BlockHeight;

/// Check if epoch exists.
fn epoch_exists(&self, epoch_id: &EpochId) -> bool;

/// Amount of tokens minted in given epoch.
fn get_epoch_minted_amount(&self, epoch_id: &EpochId) -> Result<Balance, Error>;

Expand Down
14 changes: 12 additions & 2 deletions chain/epoch-manager/src/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use near_primitives::types::EpochId;

use crate::{EpochManager, EpochManagerHandle};
use std::sync::{RwLockReadGuard, RwLockWriteGuard};

Expand All @@ -8,7 +10,10 @@ use std::sync::{RwLockReadGuard, RwLockWriteGuard};
/// we move it to a new trait. The end goal is for the code to use the concrete
/// epoch manager type directly. Though, we might want to still keep this trait
/// in, to allow for easy overriding of epoch manager in tests.
pub trait EpochManagerAdapter: Send + Sync {}
pub trait EpochManagerAdapter: Send + Sync {
/// Check if epoch exists.
fn epoch_exists(&self, epoch_id: &EpochId) -> bool;
}

/// A technical plumbing trait to conveniently implement [`EpochManagerAdapter`]
/// for `NightshadeRuntime` without too much copy-paste.
Expand All @@ -30,4 +35,9 @@ impl HasEpochMangerHandle for EpochManagerHandle {
}
}

impl<T: HasEpochMangerHandle + Send + Sync> EpochManagerAdapter for T {}
impl<T: HasEpochMangerHandle + Send + Sync> EpochManagerAdapter for T {
fn epoch_exists(&self, epoch_id: &EpochId) -> bool {
let epoch_manager = self.read();
epoch_manager.get_epoch_info(epoch_id).is_ok()
}
}
5 changes: 0 additions & 5 deletions nearcore/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,11 +1231,6 @@ impl RuntimeAdapter for NightshadeRuntime {
.unwrap_or(self.genesis_config.genesis_height)
}

fn epoch_exists(&self, epoch_id: &EpochId) -> bool {
let epoch_manager = self.epoch_manager.read();
epoch_manager.get_epoch_info(epoch_id).is_ok()
}

fn get_epoch_minted_amount(&self, epoch_id: &EpochId) -> Result<Balance, Error> {
let epoch_manager = self.epoch_manager.read();
Ok(epoch_manager.get_epoch_info(epoch_id)?.minted_amount())
Expand Down

0 comments on commit 34bdf28

Please sign in to comment.