Skip to content

Commit

Permalink
Move spawn_zebrad_for_rpc_without_initial_peers
Browse files Browse the repository at this point in the history
Place it in the `launch` sub-module.
  • Loading branch information
jvff committed Apr 26, 2022
1 parent ed0e1c8 commit 25f0ac4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 66 deletions.
10 changes: 6 additions & 4 deletions zebrad/tests/acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ mod common;
use common::{
check::{is_zebrad_version, EphemeralCheck, EphemeralConfig},
config::{default_test_config, persistent_test_config, testdir},
launch::{ZebradTestDirExt, BETWEEN_NODES_DELAY, LAUNCH_DELAY, LIGHTWALLETD_DELAY},
launch::{
spawn_zebrad_for_rpc_without_initial_peers, ZebradTestDirExt, BETWEEN_NODES_DELAY,
LAUNCH_DELAY, LIGHTWALLETD_DELAY,
},
lightwalletd::{
random_known_rpc_port_config,
send_transaction_test::spawn_zebrad_for_rpc_without_initial_peers,
zebra_skip_lightwalletd_tests, LightWalletdTestDirExt, LIGHTWALLETD_TEST_TIMEOUT,
random_known_rpc_port_config, zebra_skip_lightwalletd_tests, LightWalletdTestDirExt,
LIGHTWALLETD_TEST_TIMEOUT,
},
sync::{
create_cached_database_height, sync_until, MempoolBehavior, LARGE_CHECKPOINT_TEST_HEIGHT,
Expand Down
54 changes: 51 additions & 3 deletions zebrad/tests/common/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@
//! This file is only for test library code.

use std::{
collections::HashSet,
env,
net::SocketAddr,
path::{Path, PathBuf},
time::Duration,
};

use color_eyre::eyre::Result;

use zebrad::config::ZebradConfig;

use zebra_chain::parameters::Network;
use zebra_test::{
command::{Arguments, TestDirExt},
args,
command::{Arguments, TestDirExt, NO_MATCHES_REGEX_ITER},
prelude::*,
};
use zebrad::config::ZebradConfig;

use crate::{
common::lightwalletd::random_known_rpc_port_config, PROCESS_FAILURE_MESSAGES,
ZEBRA_FAILURE_MESSAGES,
};

/// After we launch `zebrad`, wait this long for the command to start up,
/// take the actions expected by the tests, and log the expected logs.
Expand Down Expand Up @@ -173,6 +181,46 @@ where
}
}

/// Spawns a zebrad instance to interact with lightwalletd, but without an internet connection.
///
/// This prevents it from downloading blocks. Instead, the `zebra_directory` parameter allows
/// providing an initial state to the zebrad instance.
pub fn spawn_zebrad_for_rpc_without_initial_peers<P: ZebradTestDirExt>(
network: Network,
zebra_directory: P,
timeout: Duration,
) -> Result<(TestChild<P>, SocketAddr)> {
let mut config = random_known_rpc_port_config()
.expect("Failed to create a config file with a known RPC listener port");

config.state.ephemeral = false;
config.network.initial_mainnet_peers = HashSet::new();
config.network.initial_testnet_peers = HashSet::new();
config.network.network = network;
config.mempool.debug_enable_at_height = Some(0);

let mut zebrad = zebra_directory
.with_config(&mut config)?
.spawn_child(args!["start"])?
.bypass_test_capture(true)
.with_timeout(timeout)
.with_failure_regex_iter(
// TODO: replace with a function that returns the full list and correct return type
ZEBRA_FAILURE_MESSAGES
.iter()
.chain(PROCESS_FAILURE_MESSAGES)
.cloned(),
NO_MATCHES_REGEX_ITER.iter().cloned(),
);

let rpc_address = config.rpc.listen_addr.unwrap();

zebrad.expect_stdout_line_matches("activating mempool")?;
zebrad.expect_stdout_line_matches(&format!("Opened RPC endpoint at {}", rpc_address))?;

Ok((zebrad, rpc_address))
}

/// Panics if `$pred` is false, with an error report containing:
/// * context from `$source`, and
/// * an optional wrapper error, using `$fmt_arg`+ as a format string and
Expand Down
71 changes: 12 additions & 59 deletions zebrad/tests/common/lightwalletd/send_transaction_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@
//! already been seen in a block.

use std::{
collections::HashSet,
env,
net::SocketAddr,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};

use color_eyre::eyre::{eyre, Result};
Expand All @@ -30,23 +27,19 @@ use zebra_chain::{
transaction::Transaction,
};
use zebra_state::HashOrHeight;
use zebra_test::{args, command::NO_MATCHES_REGEX_ITER, prelude::*};

use crate::{
common::{
cached_state::{
copy_state_directory, load_tip_height_from_state_directory,
start_state_service_with_cache_dir, ZEBRA_CACHED_STATE_DIR_VAR,
},
launch::ZebradTestDirExt,
lightwalletd::{
self, random_known_rpc_port_config,
rpc::{connect_to_lightwalletd, spawn_lightwalletd_with_rpc_server},
zebra_skip_lightwalletd_tests, LIGHTWALLETD_TEST_TIMEOUT,
},
sync::perform_full_sync_starting_from,

use crate::common::{
cached_state::{
copy_state_directory, load_tip_height_from_state_directory,
start_state_service_with_cache_dir, ZEBRA_CACHED_STATE_DIR_VAR,
},
launch::spawn_zebrad_for_rpc_without_initial_peers,
lightwalletd::{
self,
rpc::{connect_to_lightwalletd, spawn_lightwalletd_with_rpc_server},
zebra_skip_lightwalletd_tests, LIGHTWALLETD_TEST_TIMEOUT,
},
PROCESS_FAILURE_MESSAGES, ZEBRA_FAILURE_MESSAGES,
sync::perform_full_sync_starting_from,
};

/// The test entry point.
Expand Down Expand Up @@ -218,46 +211,6 @@ where
Ok(block.transactions.to_vec())
}

/// Spawns a zebrad instance to interact with lightwalletd, but without an internet connection.
///
/// This prevents it from downloading blocks. Instead, the `zebra_directory` parameter allows
/// providing an initial state to the zebrad instance.
pub fn spawn_zebrad_for_rpc_without_initial_peers<P: ZebradTestDirExt>(
network: Network,
zebra_directory: P,
timeout: Duration,
) -> Result<(TestChild<P>, SocketAddr)> {
let mut config = random_known_rpc_port_config()
.expect("Failed to create a config file with a known RPC listener port");

config.state.ephemeral = false;
config.network.initial_mainnet_peers = HashSet::new();
config.network.initial_testnet_peers = HashSet::new();
config.network.network = network;
config.mempool.debug_enable_at_height = Some(0);

let mut zebrad = zebra_directory
.with_config(&mut config)?
.spawn_child(args!["start"])?
.bypass_test_capture(true)
.with_timeout(timeout)
.with_failure_regex_iter(
// TODO: replace with a function that returns the full list and correct return type
ZEBRA_FAILURE_MESSAGES
.iter()
.chain(PROCESS_FAILURE_MESSAGES)
.cloned(),
NO_MATCHES_REGEX_ITER.iter().cloned(),
);

let rpc_address = config.rpc.listen_addr.unwrap();

zebrad.expect_stdout_line_matches("activating mempool")?;
zebrad.expect_stdout_line_matches(&format!("Opened RPC endpoint at {}", rpc_address))?;

Ok((zebrad, rpc_address))
}

/// Prepare a request to send to lightwalletd that contains a transaction to be sent.
fn prepare_send_transaction_request(
transaction: Arc<Transaction>,
Expand Down

0 comments on commit 25f0ac4

Please sign in to comment.