Skip to content

Commit

Permalink
refactor: simplify get_epoch_sync_data_hash
Browse files Browse the repository at this point in the history
Can be implemented in the trait itself, its just a convenience fn at
this point.
  • Loading branch information
matklad committed Sep 29, 2022
1 parent 1796410 commit 1652e28
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 35 deletions.
9 changes: 0 additions & 9 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,15 +1256,6 @@ impl RuntimeAdapter for KeyValueRuntime {
Ok(Default::default())
}

fn get_epoch_sync_data_hash(
&self,
_prev_epoch_last_block_hash: &CryptoHash,
_epoch_id: &EpochId,
_next_epoch_id: &EpochId,
) -> Result<CryptoHash, Error> {
Ok(CryptoHash::default())
}

fn get_epoch_protocol_version(&self, _epoch_id: &EpochId) -> Result<ProtocolVersion, Error> {
Ok(PROTOCOL_VERSION)
}
Expand Down
20 changes: 18 additions & 2 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use near_primitives::checked_feature;
use near_primitives::epoch_manager::block_info::BlockInfo;
use near_primitives::epoch_manager::epoch_info::EpochInfo;
use near_primitives::errors::{EpochError, InvalidTxError};
use near_primitives::hash::CryptoHash;
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::merkle::{merklize, MerklePath};
use near_primitives::receipt::Receipt;
use near_primitives::shard_layout::{ShardLayout, ShardUId};
Expand Down Expand Up @@ -409,7 +409,23 @@ pub trait RuntimeAdapter: EpochManagerAdapter + Send + Sync {
prev_epoch_last_block_hash: &CryptoHash,
epoch_id: &EpochId,
next_epoch_id: &EpochId,
) -> Result<CryptoHash, Error>;
) -> Result<CryptoHash, Error> {
let (
prev_epoch_first_block_info,
prev_epoch_prev_last_block_info,
prev_epoch_last_block_info,
prev_epoch_info,
cur_epoch_info,
next_epoch_info,
) = self.get_epoch_sync_data(prev_epoch_last_block_hash, epoch_id, next_epoch_id)?;
let mut data = prev_epoch_first_block_info.try_to_vec().unwrap();
data.extend(prev_epoch_prev_last_block_info.try_to_vec().unwrap());
data.extend(prev_epoch_last_block_info.try_to_vec().unwrap());
data.extend(prev_epoch_info.try_to_vec().unwrap());
data.extend(cur_epoch_info.try_to_vec().unwrap());
data.extend(next_epoch_info.try_to_vec().unwrap());
Ok(hash(data.as_slice()))
}

/// Epoch active protocol version.
fn get_epoch_protocol_version(&self, epoch_id: &EpochId) -> Result<ProtocolVersion, Error>;
Expand Down
24 changes: 0 additions & 24 deletions nearcore/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,30 +927,6 @@ impl RuntimeAdapter for NightshadeRuntime {
Ok(epoch_manager.get_epoch_info(epoch_id)?.minted_amount())
}

// TODO #3488 this likely to be updated
fn get_epoch_sync_data_hash(
&self,
prev_epoch_last_block_hash: &CryptoHash,
epoch_id: &EpochId,
next_epoch_id: &EpochId,
) -> Result<CryptoHash, Error> {
let (
prev_epoch_first_block_info,
prev_epoch_prev_last_block_info,
prev_epoch_last_block_info,
prev_epoch_info,
cur_epoch_info,
next_epoch_info,
) = self.get_epoch_sync_data(prev_epoch_last_block_hash, epoch_id, next_epoch_id)?;
let mut data = prev_epoch_first_block_info.try_to_vec().unwrap();
data.extend(prev_epoch_prev_last_block_info.try_to_vec().unwrap());
data.extend(prev_epoch_last_block_info.try_to_vec().unwrap());
data.extend(prev_epoch_info.try_to_vec().unwrap());
data.extend(cur_epoch_info.try_to_vec().unwrap());
data.extend(next_epoch_info.try_to_vec().unwrap());
Ok(hash(data.as_slice()))
}

// TODO #3488 this likely to be updated
fn get_epoch_sync_data(
&self,
Expand Down

0 comments on commit 1652e28

Please sign in to comment.