这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 88 additions & 39 deletions aptos-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ impl AptosNodeArgs {
.map(StdRng::from_seed)
.unwrap_or_else(StdRng::from_entropy);
setup_test_environment_and_start_node(
self.config,
self.test_config_override,
&self.config,
&self.test_config_override,
None,
self.test_dir,
self.random_ports,
Expand Down Expand Up @@ -228,68 +228,66 @@ pub fn start(
Ok(())
}

/// Creates a simple test environment and starts the node
pub fn setup_test_environment_and_start_node<R>(
config_path: Option<PathBuf>,
test_config_override_path: Option<PathBuf>,
config: Option<NodeConfig>,
test_dir: Option<PathBuf>,
/// Load a config based on a variety of different ways to provide config options. For
/// more information about each argument and its precedence, see
/// `setup_test_environment_and_start_node`.
pub fn load_node_config<R>(
config_path: &Option<PathBuf>,
test_config_override_path: &Option<PathBuf>,
test_dir: &Path,
random_ports: bool,
enable_lazy_mode: bool,
framework: &ReleaseBundle,
rng: R,
) -> anyhow::Result<()>
) -> anyhow::Result<NodeConfig>
where
R: rand::RngCore + rand::CryptoRng,
{
// If there wasn't a test directory specified, create a temporary one
let test_dir =
test_dir.unwrap_or_else(|| aptos_temppath::TempPath::new().as_ref().to_path_buf());

// Create the directories for the node
fs::DirBuilder::new().recursive(true).create(&test_dir)?;
let test_dir = test_dir.canonicalize()?;

// The validator builder puts the first node in the 0 directory
let validator_config_path = test_dir.join("0").join("node.yaml");
let aptos_root_key_path = test_dir.join("mint.key");

// If there's already a config, use it. Otherwise create a test one.
let config = if let Some(config) = config {
config
} else if validator_config_path.exists() {
let config = if validator_config_path.exists() {
NodeConfig::load_from_path(&validator_config_path)
.map_err(|error| anyhow!("Unable to load config: {:?}", error))?
} else {
// Create a test only config for a single validator node.
create_single_node_test_config(
&config_path,
&test_config_override_path,
&test_dir,
let config = create_single_node_test_config(
config_path,
test_config_override_path,
test_dir,
random_ports,
enable_lazy_mode,
framework,
rng,
)?
)?;
if let Some(ref test_config_override_path) = test_config_override_path {
println!(
"\tMerged default config with override from path: {:?}",
test_config_override_path
);
}
if let Some(ref config_path) = config_path {
println!("\tUsed user-provided config from path: {:?}", config_path);
}
config
};

Ok(config)
}

/// Print details about a node config configured for a test environment and start it.
pub fn start_test_environment_node(
config: NodeConfig,
test_dir: PathBuf,
enable_lazy_mode: bool,
) -> anyhow::Result<()> {
let aptos_root_key_path = test_dir.join("mint.key");

// Prepare log file since we cannot automatically route logs to stderr
let log_file = test_dir.join("validator.log");

// Print out useful information about the environment and the node
println!("Completed generating configuration:");
if test_config_override_path.is_some() {
println!(
"\tMerged default config with override from path: {:?}",
test_config_override_path.unwrap()
);
}
if config_path.is_some() {
println!(
"\tUsed user-provided config from path: {:?}",
config_path.unwrap()
);
}
println!("\tLog file: {:?}", log_file);
println!("\tTest dir: {:?}", test_dir);
println!("\tAptos root key path: {:?}", aptos_root_key_path);
Expand Down Expand Up @@ -318,6 +316,57 @@ where
start(config, Some(log_file), false)
}

/// Creates a simple test environment and starts the node.
///
/// You will notice many args referring to configs. Let's explain them:
/// - `test_config_override_path` is the path to a config file that will be used as
/// a template when building the final config. If not provided, a default template
/// will be used. Many overrides are applied on top of this base config.
/// - `config_path` is similar to `test_config_override_path`, but many of the
/// overrides that are applied when using `test_config_override_path` are not
/// applied when using `config_path`. Read the code for more info.
/// - `config` is a complete NodeConfig. No overrides are applied on top of this if
/// it is provided. If both `config` and `test_dir` are provided, `config` takes
/// precedence.
/// - `test_dir` is a directory that contains a config file. Much like `config`, the
/// config read from this file is used without any overrides.
Comment on lines +321 to +332
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying wrap my head around this...

Not sure if I read the code right, but If both config_path and test_dir are provided, the config from test_dir is ignored? Should we use the one from test_dir if provided?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the other way around, if test_dir is provided then config_path is ignored. This is a pretty awful function, I'm just trying to document it, an enum would be better here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! Yeah probably a good ops week item in Q4. Lets get this fix and do a release soon to unblock 😃

nit: Intuitively, I feel like if test_dir is provided, I would assume test_dir is used and overrides everything else, including config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I went back and forth on that. In reality this is just not good code and Rust has enums to solve this problem.

pub fn setup_test_environment_and_start_node<R>(
config_path: &Option<PathBuf>,
test_config_override_path: &Option<PathBuf>,
config: Option<NodeConfig>,
test_dir: Option<PathBuf>,
random_ports: bool,
enable_lazy_mode: bool,
framework: &ReleaseBundle,
rng: R,
) -> anyhow::Result<()>
where
R: rand::RngCore + rand::CryptoRng,
{
// If there wasn't a test directory specified, create a temporary one
let test_dir =
test_dir.unwrap_or_else(|| aptos_temppath::TempPath::new().as_ref().to_path_buf());

// Create the directories for the node
fs::DirBuilder::new().recursive(true).create(&test_dir)?;
let test_dir = test_dir.canonicalize()?;

let config = match config {
Some(config) => config,
None => load_node_config(
config_path,
test_config_override_path,
&test_dir,
random_ports,
enable_lazy_mode,
framework,
rng,
)?,
};

start_test_environment_node(config, test_dir, enable_lazy_mode)
}

/// Creates a single node test config, with a few config tweaks to reduce
/// the overhead of running the node on a local machine. It writes necessary
/// configuration artifacts (e.g. the mint key) to disk.
Expand Down
2 changes: 1 addition & 1 deletion crates/aptos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ All notable changes to the Aptos CLI will be captured in this file. This project

### Updated
- The `--with-faucet` flag has been removed from `aptos node run-local-testnet`, we now run a faucet by default. To disable the faucet use the `--no-faucet` flag.
- When using `aptos node run-local-testnet` we now expose a transaction stream. Learn more about the transaction stream service here: https://aptos.dev/indexer/txn-stream/. Opt out of this with `--no-txn-stream`.
- **Breaking change**: When using `aptos node run-local-testnet` we now expose a transaction stream. Learn more about the transaction stream service here: https://aptos.dev/indexer/txn-stream/. Opt out of this with `--no-txn-stream`. This is marked as a breaking change since the CLI now uses a port (50051 by default) that it didn't used to. If you need this port, you can tell the CLI to use a different port with `--txn-stream-port`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New port is being used now. Does this break our CLI e2e? (Can't remember if we hard coded the port in e2e)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an additional new port, all the existing stuff is on the same port.


## [2.1.0] - 2023/08/24
### Updated
Expand Down
25 changes: 7 additions & 18 deletions crates/aptos/src/node/local_testnet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use aptos_config::config::{NodeConfig, DEFAULT_GRPC_STREAM_PORT};
use aptos_faucet_core::server::{FunderKeyEnum, RunConfig as FaucetConfig};
use aptos_indexer_grpc_server_framework::setup_logging;
use aptos_logger::debug;
use aptos_node::create_single_node_test_config;
use aptos_node::{load_node_config, start_test_environment_node};
use async_trait::async_trait;
use clap::Parser;
use futures::{Future, FutureExt};
Expand Down Expand Up @@ -150,7 +150,10 @@ impl RunLocalTestnet {
.map(StdRng::from_seed)
.unwrap_or_else(StdRng::from_entropy);

let mut node_config = create_single_node_test_config(
// If there is a config on disk, this function will use that. If not, it will
// create a new one, taking the config_path and test_config_override arguments
// into account.
let mut node_config = load_node_config(
&self.config_path,
&self.test_config_override,
&test_dir,
Expand All @@ -159,7 +162,7 @@ impl RunLocalTestnet {
aptos_cached_packages::head_release_bundle(),
rng,
)
.context("Failed to create config for node")?;
.context("Failed to load / create config for node")?;

eprintln!();

Expand Down Expand Up @@ -210,22 +213,8 @@ impl RunLocalTestnet {
test_dir: PathBuf,
config: NodeConfig,
) -> CliTypedResult<impl Future<Output = ()>> {
let rng = self
.seed
.map(StdRng::from_seed)
.unwrap_or_else(StdRng::from_entropy);

let node_thread_handle = thread::spawn(move || {
let result = aptos_node::setup_test_environment_and_start_node(
None,
None,
Some(config),
Some(test_dir),
false,
false,
aptos_cached_packages::head_release_bundle(),
rng,
);
let result = start_test_environment_node(config, test_dir, false);
eprintln!("Node stopped unexpectedly {:#?}", result);
});

Expand Down
2 changes: 2 additions & 0 deletions crates/aptos/src/node/local_testnet/ready_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use std::net::{Ipv4Addr, SocketAddrV4};

#[derive(Debug, Clone, Parser)]
pub struct ReadyServerConfig {
/// The port to run the ready server. This exposes an endpoint at `/` that you can
/// use to check if the entire local testnet is ready.
#[clap(long, default_value_t = 8090)]
pub ready_server_listen_port: u16,
}
Expand Down