Skip to content

Commit 7b3091b

Browse files
committed
impl
1 parent 1ce9e63 commit 7b3091b

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

core/chain-configs/src/test_genesis.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::collections::{BTreeMap, HashMap, HashSet};
2-
use std::sync::Arc;
1+
use std::collections::{HashMap, HashSet};
32

43
use near_crypto::PublicKey;
54
use near_primitives::account::{AccessKey, Account, AccountContract};
@@ -392,6 +391,14 @@ impl TestGenesisBuilder {
392391
self
393392
}
394393

394+
/// Creates epoch config builder compatible with the current genesis builder state
395+
pub fn derive_epoch_config_builder(&self) -> TestEpochConfigBuilder {
396+
TestEpochConfigBuilder::new()
397+
.epoch_length(self.epoch_length)
398+
.shard_layout(self.shard_layout.clone())
399+
.validators_spec(self.validators_spec.clone())
400+
}
401+
395402
pub fn build(self) -> Genesis {
396403
if self
397404
.user_accounts
@@ -512,6 +519,16 @@ impl TestGenesisBuilder {
512519
contents: GenesisContents::Records { records: GenesisRecords(records) },
513520
}
514521
}
522+
523+
// Builds genesis along with epoch config store containing single epoch config
524+
// for the genesis protocol version.
525+
pub fn build_with_simple_epoch_config_store(self) -> (Genesis, EpochConfigStore) {
526+
let epoch_config = self.derive_epoch_config_builder().build();
527+
let genesis = self.build();
528+
let epoch_config_store =
529+
EpochConfigStore::test_single_version(genesis.config.protocol_version, epoch_config);
530+
(genesis, epoch_config_store)
531+
}
515532
}
516533

517534
impl ValidatorsSpec {
@@ -671,21 +688,17 @@ pub fn build_genesis_and_epoch_config_store<'a>(
671688
.genesis_time_from_clock(&FakeClock::default().clock())
672689
.protocol_version(protocol_version)
673690
.epoch_length(epoch_length)
674-
.shard_layout(shard_layout.clone())
675-
.validators_spec(validators_spec.clone())
691+
.shard_layout(shard_layout)
692+
.validators_spec(validators_spec)
676693
.add_user_accounts_simple(accounts, 1_000_000 * ONE_NEAR)
677694
.gas_limit_one_petagas();
678-
let epoch_config_builder = TestEpochConfigBuilder::new()
679-
.epoch_length(epoch_length)
680-
.shard_layout(shard_layout)
681-
.validators_spec(validators_spec);
695+
let epoch_config_builder = genesis_builder.derive_epoch_config_builder();
682696
let genesis_builder = customize_genesis_builder(genesis_builder);
683697
let epoch_config_builder = customize_epoch_config_builder(epoch_config_builder);
684698

685699
let genesis = genesis_builder.build();
686700
let epoch_config = epoch_config_builder.build();
687-
let epoch_config_store =
688-
EpochConfigStore::test(BTreeMap::from([(protocol_version, Arc::new(epoch_config))]));
701+
let epoch_config_store = EpochConfigStore::test_single_version(protocol_version, epoch_config);
689702

690703
(genesis, epoch_config_store)
691704
}

core/primitives/src/epoch_manager.rs

+7
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,13 @@ impl EpochConfigStore {
631631
Self { store }
632632
}
633633

634+
pub fn test_single_version(
635+
protocol_version: ProtocolVersion,
636+
epoch_config: EpochConfig,
637+
) -> Self {
638+
Self::test(BTreeMap::from([(protocol_version, Arc::new(epoch_config))]))
639+
}
640+
634641
/// Returns the EpochConfig for the given protocol version.
635642
/// This panics if no config is found for the given version, thus the initialization via `for_chain_id` should
636643
/// only be performed for chains with some configs stored in files.

test-loop-tests/src/builder.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use near_chain_configs::test_genesis::TestGenesisBuilder;
12
use std::collections::{HashMap, HashSet};
23
use std::sync::{Arc, Mutex};
34
use tempfile::TempDir;
@@ -306,6 +307,12 @@ impl TestLoopBuilder {
306307
}
307308
}
308309

310+
// Creates TestLoop-compatible genesis builder
311+
pub(crate) fn new_genesis_builder() -> TestGenesisBuilder {
312+
TestGenesisBuilder::new()
313+
.genesis_time_from_clock(&near_async::time::FakeClock::default().clock())
314+
}
315+
309316
/// Get the clock for the test loop.
310317
pub(crate) fn clock(&self) -> Clock {
311318
self.test_loop.clock()

test-loop-tests/src/tests/global_contracts.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
use std::collections::BTreeMap;
2-
use std::sync::Arc;
3-
41
use assert_matches::assert_matches;
52
use near_async::time::Duration;
6-
use near_chain_configs::test_genesis::{
7-
TestEpochConfigBuilder, TestGenesisBuilder, ValidatorsSpec,
8-
};
3+
use near_chain_configs::test_genesis::ValidatorsSpec;
94
use near_client::Client;
105
use near_client::test_utils::test_loop::ClientQueries;
116
use near_o11y::testonly::init_test_logger;
127
use near_parameters::{ActionCosts, RuntimeConfigStore, RuntimeFeesConfig};
138
use near_primitives::action::{GlobalContractDeployMode, GlobalContractIdentifier};
14-
use near_primitives::epoch_manager::EpochConfigStore;
159
use near_primitives::errors::{
1610
ActionError, ActionErrorKind, FunctionCallError, MethodResolveError, TxExecutionError,
1711
};
@@ -181,24 +175,15 @@ impl GlobalContractsTestEnv {
181175
let validators_spec =
182176
ValidatorsSpec::desired_roles(&block_and_chunk_producers, &chunk_validators_only);
183177

184-
let genesis = TestGenesisBuilder::new()
185-
.genesis_time_from_clock(&near_async::time::FakeClock::default().clock())
186-
.validators_spec(validators_spec.clone())
187-
.shard_layout(shard_layout.clone())
178+
let (genesis, epoch_config_store) = TestLoopBuilder::new_genesis_builder()
179+
.validators_spec(validators_spec)
180+
.shard_layout(shard_layout)
188181
.add_user_accounts_simple(
189182
&[account_shard_0.clone(), account_shard_1.clone(), deploy_account.clone()],
190183
initial_balance,
191184
)
192185
.gas_prices(GAS_PRICE, GAS_PRICE)
193-
.build();
194-
let epoch_config = TestEpochConfigBuilder::new()
195-
.shard_layout(shard_layout)
196-
.validators_spec(validators_spec)
197-
.build();
198-
let epoch_config_store = EpochConfigStore::test(BTreeMap::from([(
199-
genesis.config.protocol_version,
200-
Arc::new(epoch_config),
201-
)]));
186+
.build_with_simple_epoch_config_store();
202187

203188
let clients = block_and_chunk_producers
204189
.iter()

0 commit comments

Comments
 (0)