Skip to content

Commit

Permalink
make block time configurable to sim contract tests that depend on env…
Browse files Browse the repository at this point in the history
…::block_time (#378)

* make block time configurable to sim contract tests that depend on env::block_time

* Update near-sdk-sim/src/runtime.rs

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>

* use block_prod_time name to make it matches nearcore

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
Co-authored-by: Evgeny Kuzyakov <ek@nearprotocol.com>
  • Loading branch information
3 people authored Apr 30, 2021
1 parent 1951284 commit 9af51b8
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions near-sdk-sim/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ use near_primitives::types::{
use near_primitives::version::PROTOCOL_VERSION;
use near_primitives::views::ViewApplyState;
use near_runtime::{state_viewer::TrieViewer, ApplyState, Runtime};
use near_sdk::Duration;
use near_store::{
get_access_key, get_account, set_account, test_utils::create_test_store, ShardTries, Store,
};

const DEFAULT_EPOCH_LENGTH: u64 = 3;
const DEFAULT_BLOCK_PROD_TIME: Duration = 1_000_000_000;

pub fn init_runtime(
genesis_config: Option<GenesisConfig>,
Expand All @@ -46,6 +48,7 @@ pub struct GenesisConfig {
pub gas_limit: Gas,
pub genesis_height: u64,
pub epoch_length: u64,
pub block_prod_time: Duration,
pub runtime_config: RuntimeConfig,
pub state_records: Vec<StateRecord>,
pub validators: Vec<AccountInfo>,
Expand All @@ -60,6 +63,7 @@ impl Default for GenesisConfig {
gas_limit: runtime_config.wasm_config.limit_config.max_total_prepaid_gas,
genesis_height: 0,
epoch_length: DEFAULT_EPOCH_LENGTH,
block_prod_time: DEFAULT_BLOCK_PROD_TIME,
runtime_config,
state_records: vec![],
validators: vec![],
Expand Down Expand Up @@ -122,11 +126,16 @@ impl Block {
}
}

pub fn produce(&self, new_state_root: CryptoHash, epoch_length: u64) -> Block {
fn produce(
&self,
new_state_root: CryptoHash,
epoch_length: u64,
block_prod_time: Duration,
) -> Block {
Self {
gas_price: self.gas_price,
gas_limit: self.gas_limit,
block_timestamp: self.block_timestamp + 1_000_000_000,
block_timestamp: self.block_timestamp + block_prod_time,
prev_block: Some(Arc::new(self.clone())),
state_root: new_state_root,
block_height: self.block_height + 1,
Expand Down Expand Up @@ -286,7 +295,11 @@ impl RuntimeStandalone {
let (update, _) =
self.tries.apply_all(&apply_result.trie_changes, 0).expect("Unexpected Storage error");
update.commit().expect("Unexpected io error");
self.cur_block = self.cur_block.produce(apply_result.state_root, self.genesis.epoch_length);
self.cur_block = self.cur_block.produce(
apply_result.state_root,
self.genesis.epoch_length,
self.genesis.block_prod_time,
);

Ok(())
}
Expand Down

0 comments on commit 9af51b8

Please sign in to comment.