Skip to content

Commit

Permalink
initial refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pugachAG committed Mar 16, 2024
1 parent 6de3a9d commit 79ca070
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 90 deletions.
82 changes: 3 additions & 79 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ pub fn process_block_sync(
pub fn setup(
clock: Clock,
) -> (Chain, Arc<MockEpochManager>, Arc<KeyValueRuntime>, Arc<InMemoryValidatorSigner>) {
setup_with_tx_validity_period(clock, 100)
setup_with_tx_validity_period(clock, 100, 1000)
}

fn setup_with_tx_validity_period(
pub fn setup_with_tx_validity_period(
clock: Clock,
tx_validity_period: NumBlocks,
epoch_length: u64,
) -> (Chain, Arc<MockEpochManager>, Arc<KeyValueRuntime>, Arc<InMemoryValidatorSigner>) {
let store = create_test_store();
let epoch_length = 1000;
let epoch_manager = MockEpochManager::new(store.clone(), epoch_length);
let shard_tracker = ShardTracker::new_empty(epoch_manager.clone());
let runtime = KeyValueRuntime::new(store, epoch_manager.as_ref());
Expand Down Expand Up @@ -165,82 +165,6 @@ fn setup_with_tx_validity_period(
(chain, epoch_manager, runtime, signer)
}

pub fn setup_with_validators(
clock: Clock,
vs: ValidatorSchedule,
epoch_length: u64,
tx_validity_period: NumBlocks,
) -> (Chain, Arc<MockEpochManager>, Arc<KeyValueRuntime>, Vec<Arc<InMemoryValidatorSigner>>) {
let store = create_test_store();
let signers =
vs.all_block_producers().map(|x| Arc::new(create_test_signer(x.as_str()))).collect();
let epoch_manager = MockEpochManager::new_with_validators(store.clone(), vs, epoch_length);
let shard_tracker = ShardTracker::new_empty(epoch_manager.clone());
let runtime = KeyValueRuntime::new(store, epoch_manager.as_ref());
let chain = Chain::new(
clock.clone(),
epoch_manager.clone(),
shard_tracker,
runtime.clone(),
&ChainGenesis {
time: clock.now_utc(),
height: 0,
gas_limit: 1_000_000,
min_gas_price: 100,
max_gas_price: 1_000_000_000,
total_supply: 1_000_000_000,
gas_price_adjustment_rate: Ratio::from_integer(0),
transaction_validity_period: tx_validity_period,
epoch_length,
protocol_version: PROTOCOL_VERSION,
},
DoomslugThresholdMode::NoApprovals,
ChainConfig::test(),
None,
Arc::new(RayonAsyncComputationSpawner),
)
.unwrap();
(chain, epoch_manager, runtime, signers)
}

pub fn setup_with_validators_and_start_time(
clock: Clock,
vs: ValidatorSchedule,
epoch_length: u64,
tx_validity_period: NumBlocks,
) -> (Chain, Arc<MockEpochManager>, Arc<KeyValueRuntime>, Vec<Arc<InMemoryValidatorSigner>>) {
let store = create_test_store();
let signers =
vs.all_block_producers().map(|x| Arc::new(create_test_signer(x.as_str()))).collect();
let epoch_manager = MockEpochManager::new_with_validators(store.clone(), vs, epoch_length);
let shard_tracker = ShardTracker::new_empty(epoch_manager.clone());
let runtime = KeyValueRuntime::new(store, epoch_manager.as_ref());
let chain = Chain::new(
clock.clone(),
epoch_manager.clone(),
shard_tracker,
runtime.clone(),
&ChainGenesis {
time: clock.now_utc(),
height: 0,
gas_limit: 1_000_000,
min_gas_price: 100,
max_gas_price: 1_000_000_000,
total_supply: 1_000_000_000,
gas_price_adjustment_rate: Ratio::from_integer(0),
transaction_validity_period: tx_validity_period,
epoch_length,
protocol_version: PROTOCOL_VERSION,
},
DoomslugThresholdMode::NoApprovals,
ChainConfig::test(),
None,
Arc::new(RayonAsyncComputationSpawner),
)
.unwrap();
(chain, epoch_manager, runtime, signers)
}

pub fn format_hash(hash: CryptoHash) -> String {
let mut hash = hash.to_string();
hash.truncate(6);
Expand Down
16 changes: 5 additions & 11 deletions chain/client/src/sync/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,7 @@ fn get_locator_ordinals(lowest_ordinal: u64, highest_ordinal: u64) -> Vec<u64> {
mod test {
use near_async::messaging::IntoMultiSender;
use near_async::time::{Clock, Duration, FakeClock, Utc};
use near_chain::test_utils::{
process_block_sync, setup, setup_with_validators_and_start_time, ValidatorSchedule,
};
use near_chain::test_utils::{process_block_sync, setup, setup_with_tx_validity_period};
use near_chain::types::Tip;
use near_chain::{BlockProcessingArtifact, Provenance};
use near_client_primitives::types::SyncStatus;
Expand Down Expand Up @@ -744,14 +742,10 @@ mod test {
1_000_000_000,
);

let vs = ValidatorSchedule::new()
.block_producers_per_epoch(vec![vec!["test0".parse().unwrap()]]);
let clock = FakeClock::new(Utc::UNIX_EPOCH);
// Don't bother with epoch switches. It's not relevant.
let (mut chain, _, _, _) =
setup_with_validators_and_start_time(clock.clock(), vs.clone(), 10000, 100);
let (mut chain2, _, _, signers2) =
setup_with_validators_and_start_time(clock.clock(), vs, 10000, 100);
let (mut chain, _, _, _) = setup_with_tx_validity_period(clock.clock(), 100, 10000);
let (mut chain2, _, _, signer2) = setup_with_tx_validity_period(clock.clock(), 100, 10000);
// Set up the second chain with 2000+ blocks.
let mut block_merkle_tree = PartialMerkleTree::default();
block_merkle_tree.insert(*chain.genesis().hash()); // for genesis block
Expand All @@ -777,7 +771,7 @@ mod test {
epoch_id,
next_epoch_id,
None,
signers2
[&signer2]
.iter()
.map(|signer| {
Some(Box::new(
Expand All @@ -797,7 +791,7 @@ mod test {
Some(0),
vec![],
vec![],
&*signers2[0],
signer2.as_ref(),
*last_block.header().next_bp_hash(),
block_merkle_tree.root(),
clock.now_utc(),
Expand Down

0 comments on commit 79ca070

Please sign in to comment.