Skip to content

Commit

Permalink
tests: deduplicate some RPC test code
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed May 3, 2022
1 parent 280d756 commit 6c772fe
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions rpc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ fn new_ledger_state<N: Network, S: Storage, P: AsRef<Path>>(path: Option<P>) ->
}
}

/// Returns a single test block.
fn test_block() -> Block<CurrentNetwork> {
let mut test_block_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_block_path.push("..");
test_block_path.push("storage");
test_block_path.push("benches");
// note: the `blocks_1` file was generated on a testnet2 storage using `LedgerState::dump_blocks`.
test_block_path.push("blocks_1");

let test_blocks = fs::read(test_block_path).unwrap_or_else(|_| panic!("Missing the test blocks file"));
let mut blocks: Vec<Block<CurrentNetwork>> = bincode::deserialize(&test_blocks).expect("Failed to deserialize a block dump");
blocks.pop().unwrap()
}

async fn new_rpc_context<N: Network, E: Environment, S: Storage, P: AsRef<Path>>(path: P) -> RpcContext<N, E> {
let username = "root".to_string();
let password = "pass".to_string();
Expand Down Expand Up @@ -260,20 +274,11 @@ async fn test_get_blocks() {
// Initialize an empty ledger.
let ledger_state = LedgerState::open_writer::<RocksDB, _>(directory.clone()).expect("Failed to initialize ledger");

// Read the test blocks; note: they don't include the genesis block, as it's always available when creating a ledger.
// note: the `blocks_1` file was generated on a testnet2 storage using `LedgerState::dump_blocks`.
let mut test_block_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_block_path.push("..");
test_block_path.push("storage");
test_block_path.push("benches");
test_block_path.push("blocks_1");

let test_blocks = fs::read(test_block_path).unwrap_or_else(|_| panic!("Missing the test blocks file"));

let blocks: Vec<Block<CurrentNetwork>> = bincode::deserialize(&test_blocks).expect("Failed to deserialize a block dump");
// Read a single test block.
let test_block = test_block();

// Load a test block into the ledger.
ledger_state.add_next_block(&blocks[0]).expect("Failed to add a test block");
ledger_state.add_next_block(&test_block).expect("Failed to add a test block");

// Drop the handle to ledger_state. Note this does not remove the blocks in the temporary directory.
drop(ledger_state);
Expand All @@ -288,7 +293,7 @@ async fn test_get_blocks() {
let response: Vec<Block<CurrentNetwork>> = rpc_client.request("getblocks", params).await.expect("Invalid response");

// Check the blocks.
assert_eq!(response, vec![CurrentNetwork::genesis_block().clone(), blocks[0].clone()]);
assert_eq!(response, vec![CurrentNetwork::genesis_block().clone(), test_block]);
}

#[tokio::test]
Expand Down Expand Up @@ -327,20 +332,11 @@ async fn test_get_block_hashes() {
// Initialize an empty ledger.
let ledger_state = LedgerState::open_writer::<RocksDB, _>(directory.clone()).expect("Failed to initialize ledger");

// Read the test blocks; note: they don't include the genesis block, as it's always available when creating a ledger.
// note: the `blocks_1` file was generated on a testnet2 storage using `LedgerState::dump_blocks`.
let mut test_block_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_block_path.push("..");
test_block_path.push("storage");
test_block_path.push("benches");
test_block_path.push("blocks_1");

let test_blocks = fs::read(test_block_path).unwrap_or_else(|_| panic!("Missing the test blocks file"));

let blocks: Vec<Block<CurrentNetwork>> = bincode::deserialize(&test_blocks).expect("Failed to deserialize a block dump");
// Read a single test block.
let test_block = test_block();

// Load a test block into the ledger.
ledger_state.add_next_block(&blocks[0]).expect("Failed to add a test block");
ledger_state.add_next_block(&test_block).expect("Failed to add a test block");

// Drop the handle to ledger_state. Note this does not remove the blocks in the temporary directory.
drop(ledger_state);
Expand All @@ -356,7 +352,7 @@ async fn test_get_block_hashes() {
rpc_client.request("getblockhashes", params).await.expect("Invalid response");

// Check the block hashes.
assert_eq!(response, vec![CurrentNetwork::genesis_block().hash(), blocks[0].hash()]);
assert_eq!(response, vec![CurrentNetwork::genesis_block().hash(), test_block.hash()]);
}

#[tokio::test]
Expand Down

0 comments on commit 6c772fe

Please sign in to comment.