Skip to content

Commit

Permalink
feat: runtime-tester can use a real store (#4767)
Browse files Browse the repository at this point in the history
Test Plan
---------

Add a smoke test, manually verify that it works with a real database.
  • Loading branch information
matklad authored Sep 2, 2021
1 parent 1e5da20 commit e5315e5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test-utils/runtime-tester/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ libfuzzer-sys = { version = "0.4"}
log = "0.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tempfile = "3"

near-chain = { path = "../../chain/chain" }
near-chain-configs = { path = "../../core/chain-configs" }
Expand Down
2 changes: 1 addition & 1 deletion test-utils/runtime-tester/src/fuzzing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Arbitrary<'_> for Scenario {
while blocks.len() < MAX_BLOCKS && u.len() > BlockConfig::size_hint(0).0 {
blocks.push(BlockConfig::arbitrary(u, &mut scope)?);
}
Ok(Scenario { network_config, blocks })
Ok(Scenario { network_config, blocks, use_in_memory_store: true })
}

fn size_hint(_depth: usize) -> (usize, Option<usize>) {
Expand Down
42 changes: 42 additions & 0 deletions test-utils/runtime-tester/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,46 @@
//! fuzzing provides Arbitrary trait for Scenario, thus enabling creating random scenarios.
pub mod fuzzing;
pub mod run_test;

pub use crate::run_test::{BlockConfig, NetworkConfig, Scenario, TransactionConfig};

#[test]
// Use this test as a base for creating reproducers.
fn scenario_smoke_test() {
use near_crypto::{InMemorySigner, KeyType};
use near_primitives::transaction::{Action, TransferAction};
use near_primitives::types::AccountId;

let num_accounts = 5;

let seeds: Vec<String> = (0..num_accounts).map(|i| format!("test{}", i)).collect();
let accounts: Vec<AccountId> = seeds.iter().map(|id| id.parse().unwrap()).collect();

let mut scenario = Scenario {
network_config: NetworkConfig { seeds: seeds },
blocks: Vec::new(),
use_in_memory_store: true,
};

for h in 1..5 {
let mut block = BlockConfig::at_height(h);
let transaction = {
let signer_id = accounts[h as usize].clone();
let receiver_id = accounts[(h - 1) as usize].clone();
let signer =
InMemorySigner::from_seed(signer_id.clone(), KeyType::ED25519, signer_id.as_ref());

TransactionConfig {
nonce: h,
signer_id,
receiver_id,
signer,
actions: vec![Action::Transfer(TransferAction { deposit: 10 })],
}
};
block.transactions.push(transaction);
scenario.blocks.push(block)
}

scenario.run().unwrap();
}
15 changes: 14 additions & 1 deletion test-utils/runtime-tester/src/run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use near_client_primitives::types::Error;
use near_crypto::InMemorySigner;
use near_primitives::transaction::{Action, SignedTransaction};
use near_primitives::types::{AccountId, BlockHeight, Nonce};
use near_store::create_store;
use near_store::test_utils::create_test_store;
use nearcore::{config::GenesisExt, NightshadeRuntime};

Expand All @@ -26,13 +27,24 @@ impl Scenario {
1,
);

let tempdir;
let store = if self.use_in_memory_store {
create_test_store()
} else {
tempdir = tempfile::tempdir().map_err(|err| {
Error::Other(format!("failed to create temporary directory: {}", err))
})?;
let path = tempdir.path().to_str().unwrap();
create_store(path)
};

let mut env = TestEnv::new_with_runtime(
ChainGenesis::from(&genesis),
1,
1,
vec![Arc::new(NightshadeRuntime::new(
Path::new("."),
create_test_store(),
store,
&genesis,
vec![],
vec![],
Expand Down Expand Up @@ -72,6 +84,7 @@ impl Scenario {
pub struct Scenario {
pub network_config: NetworkConfig,
pub blocks: Vec<BlockConfig>,
pub use_in_memory_store: bool,
}

#[derive(Serialize, Deserialize)]
Expand Down

0 comments on commit e5315e5

Please sign in to comment.