简介 simd testnet 子命令可以帮助你轻松初始化并启动一个用于测试的模拟测试网络。
除了运行节点的相关命令外,simd 二进制文件还包含一个 testnet 命令,使你能够在进程内启动一个模拟测试网络,或者为在独立进程中运行的模拟测试网络初始化所需文件。

初始化文件

首先,我们来看一下 init-files 子命令。 这与初始化单节点时使用的 init 命令类似,但这里初始化的是多个节点,会为每个节点生成创世交易,并将这些交易收集起来。 init-files 子命令会初始化在独立进程中运行测试网络所需的文件(例如使用 Docker 容器)。运行此命令并不是执行 start 子命令的前置条件(见下文)。 要为测试网络初始化文件,请运行以下命令:
simd testnet init-files
你应该会在终端中看到如下输出:
Successfully initialized 4 node directories
默认输出目录是相对路径 .testnets 目录。下面来看一下 .testnets 目录中创建的文件。

gentxs

gentxs 目录包含每个验证者节点的一笔创世交易。每个文件中都包含一个 JSON 编码的创世交易,用于在创世时注册对应的验证者节点。在初始化过程中,这些创世交易会被添加到每个节点目录中的 genesis.json 文件里。

nodes

系统会为每个验证者节点创建一个节点目录。每个节点目录中都包含一个 simd 目录。simd 目录是每个节点的主目录,其中包含该节点的配置文件和数据文件(也就是运行单节点时默认 ~/.simapp 目录中包含的同类文件)。

启动测试网络

现在,我们来看一下 start 子命令。 start 子命令会同时初始化并启动一个进程内测试网络。这是为了测试目的快速启动本地测试网络的最快方式。 你可以运行以下命令来启动本地测试网络:
simd testnet start
你应该会看到类似如下内容:
acquiring test network lock
preparing test network with chain-id "chain-mtoD9v"

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++       THIS MNEMONIC IS FOR TESTING PURPOSES ONLY        ++
++                DO NOT USE IN PRODUCTION                 ++
++                                                         ++
++  sustain know debris minute gate hybrid stereo custom   ++
++  divorce cross spoon machine latin vibrant term oblige  ++
++   moment beauty laundry repeat grab game bronze truly   ++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

starting test network...
started test network
press the Enter Key to terminate
第一个验证者节点现在已经在进程内运行,这意味着当你关闭终端窗口或按下 Enter 键后,测试网络就会终止。在输出中,会提供第一个验证者节点的助记词,供测试使用。该验证者节点使用的默认地址与初始化和启动单节点时相同(无需提供 --node 标志)。 检查第一个验证者节点的状态:
simd status
使用提供的助记词导入密钥:
simd keys add test --recover --keyring-backend test
查询该账户地址的余额:
simd q bank balances [address]
使用这个测试账户对测试网络进行手动测试。

测试网络选项

你可以通过标志自定义测试网络的配置。如需查看所有标志选项,请在每个命令后附加 --help 标志。
Synopsis The simd testnet subcommand makes it easy to initialize and start a simulated test network for testing purposes.
In addition to the commands for running a node, the simd binary also includes a testnet command that allows you to start a simulated test network in-process or to initialize files for a simulated test network that runs in a separate process.

Initialize Files

First, let’s take a look at the init-files subcommand. This is similar to the init command when initializing a single node, but in this case we are initializing multiple nodes, generating the genesis transactions for each node, and then collecting those transactions. The init-files subcommand initializes the necessary files to run a test network in a separate process (i.e. using a Docker container). Running this command is not a prerequisite for the start subcommand (see below). In order to initialize the files for a test network, run the following command:
simd testnet init-files
You should see the following output in your terminal:
Successfully initialized 4 node directories
The default output directory is a relative .testnets directory. Let’s take a look at the files created within the .testnets directory.

gentxs

The gentxs directory includes a genesis transaction for each validator node. Each file includes a JSON encoded genesis transaction used to register a validator node at the time of genesis. The genesis transactions are added to the genesis.json file within each node directory during the initialization process.

nodes

A node directory is created for each validator node. Within each node directory is a simd directory. The simd directory is the home directory for each node, which includes the configuration and data files for that node (i.e. the same files included in the default ~/.simapp directory when running a single node).

Start Testnet

Now, let’s take a look at the start subcommand. The start subcommand both initializes and starts an in-process test network. This is the fastest way to spin up a local test network for testing purposes. You can start the local test network by running the following command:
simd testnet start
You should see something similar to the following:
acquiring test network lock
preparing test network with chain-id "chain-mtoD9v"

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++       THIS MNEMONIC IS FOR TESTING PURPOSES ONLY        ++
++                DO NOT USE IN PRODUCTION                 ++
++                                                         ++
++  sustain know debris minute gate hybrid stereo custom   ++
++  divorce cross spoon machine latin vibrant term oblige  ++
++   moment beauty laundry repeat grab game bronze truly   ++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

starting test network...
started test network
press the Enter Key to terminate
The first validator node is now running in-process, which means the test network will terminate once you either close the terminal window or you press the Enter key. In the output, the mnemonic phrase for the first validator node is provided for testing purposes. The validator node is using the same default addresses being used when initializing and starting a single node (no need to provide a --node flag). Check the status of the first validator node:
simd status
Import the key from the provided mnemonic:
simd keys add test --recover --keyring-backend test
Check the balance of the account address:
simd q bank balances [address]
Use this test account to manually test against the test network.

Testnet Options

You can customize the configuration of the test network with flags. In order to see all flag options, append the --help flag to each command.