Skip to content

Commit

Permalink
tests: create CLI tool to help with mainnet export testing (#3082)
Browse files Browse the repository at this point in the history
* replace validators

* added unsafe-set-local-validator testnet command

* fixed blockstate and lastcommit

* update comments and descriptions

* unsafe-set-local-validator changed to unsafe-start-local-validator

* delete block from store if app and cons state are not at that version

* add docs on testnet extensions

* fix typo; add extra docs

---------

Co-authored-by: MSalopek <[email protected]>
  • Loading branch information
stana-miric and MSalopek authored May 14, 2024
1 parent c9a3831 commit 14e5927
Show file tree
Hide file tree
Showing 4 changed files with 510 additions and 14 deletions.
68 changes: 68 additions & 0 deletions cmd/gaiad/cmd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Testnet command extensions

`gaiad` extends testnet commands with `unsafe-start-local-validator` command that should be used only for testing.

The command makes changes to the local mainnet node to make it suitable for local testing. The changes include modification of consensus and application states by removing old validator data and injecting the new one, and funding the addresses to be used in testing without affecting existing addresses.

The command is added as a sub-command of the `gaiad testnet` command.

## Building a local testnet binary

The gaia binary will cointain the testnet extensions only if the `unsafe_start_local_validator` build tags is used.

```shell
make build BUILD_TAGS="-tag unsafe_start_local_validator
```
## CLI usage
Example of running the command:
```shell
./gaiad testnet unsafe-start-local-validator
--validator-operator="cosmosvaloper17fjdcqy7g80pn0seexcch5pg0dtvs45p57t97r"
--validator-pubkey="SLpHEfzQHuuNO9J1BB/hXyiH6c1NmpoIVQ2pMWmyctE="
--validator-privkey="AiayvI2px5CZVl/uOGmacfFjcIBoyk3Oa2JPBO6zEcdIukcR/NAe64070nUEH+FfKIfpzU2amghVDakxabJy0Q=="
--accounts-to-fund="cosmos1ju6tlfclulxumtt2kglvnxduj5d93a64r5czge,cosmos1r5v5srda7xfth3hn2s26txvrcrntldjumt8mhl"
[other gaiad start flags]
```
## Local setup
```shell
gaiad init localnet-export
gaiad keys add test-key --keyring-backend test --keyring-dir ~/.gaia
# get --validator-operator
export VAL_ACC_ADDR=$(gaiad keys show test-key --home ~/.gaia --keyring-backend test --output json | jq .address -r)
gaiad keys parse $(gaiad keys parse $VAL_ACC_ADDR --output=json | jq .bytes -r) --output json | jq .
{
"formats": [
"cosmos1738fdeqcepf9mrpdwyrl9zvhlmf2jk4t2x3jwd",
"cosmospub1738fdeqcepf9mrpdwyrl9zvhlmf2jk4t4qaphg",
"cosmosvaloper1738fdeqcepf9mrpdwyrl9zvhlmf2jk4t0j98z7", # --> take this one
"cosmosvaloperpub1738fdeqcepf9mrpdwyrl9zvhlmf2jk4ta3uyqm",
"cosmosvalcons1738fdeqcepf9mrpdwyrl9zvhlmf2jk4tmpkmwl",
"cosmosvalconspub1738fdeqcepf9mrpdwyrl9zvhlmf2jk4tv48v22"
]
}
# --validator-pubkey and --validator-privkey are in`$HOME/.gaia/config/priv_validator.json
cat $HOME/.gaia/config/priv_validator.json
{
"address": "067CC9545EC0CD744C44D611E3E8857D69E9CAD4",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "0zorKvPxmVUSBRl49julqrBu69mu6U6+V4GxC8fvxcM=" # --validator-pubkey
},
"priv_key": {
"type": "tendermint/PrivKeyEd25519",
"value": "rfZBWExZtNzrLx+cy8lMPXQFowXO7AZc5FXBeOyvSdDTOisq8/GZVRIFGXj2O6WqsG7r2a7pTr5XgbELx+/Fww==" # --validator-privkey
}
}
```
## Example usecase
1. download a mainnet node snapshot
2. replace all validator key files (keyring data, `priv_validator_key.json`, values in `priv_validator_state.json` are reset to 0...)
3. run `gaiad testnet unsafe-start-local-validator` -> switches the validator set and starts the node
2 changes: 1 addition & 1 deletion cmd/gaiad/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
rootCmd.AddCommand(
genutilcli.InitCmd(gaia.ModuleBasics, gaia.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
NewTestnetCmd(gaia.ModuleBasics, banktypes.GenesisBalancesIterator{}),
NewTestnetCmd(gaia.ModuleBasics, banktypes.GenesisBalancesIterator{}, ac),
addDebugCommands(debug.Cmd()),
config.Cmd(),
pruning.PruningCmd(ac.newApp),
Expand Down
36 changes: 23 additions & 13 deletions cmd/gaiad/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ import (
)

var (
flagNodeDirPrefix = "node-dir-prefix"
flagNumValidators = "v"
flagOutputDir = "output-dir"
flagNodeDaemonHome = "node-daemon-home"
flagStartingIPAddress = "starting-ip-address"
flagEnableLogging = "enable-logging"
flagGRPCAddress = "grpc.address"
flagRPCAddress = "rpc.address"
flagAPIAddress = "api.address"
flagPrintMnemonic = "print-mnemonic"
flagNodeDirPrefix = "node-dir-prefix"
flagNumValidators = "v"
flagOutputDir = "output-dir"
flagNodeDaemonHome = "node-daemon-home"
flagStartingIPAddress = "starting-ip-address"
flagEnableLogging = "enable-logging"
flagGRPCAddress = "grpc.address"
flagRPCAddress = "rpc.address"
flagAPIAddress = "api.address"
flagPrintMnemonic = "print-mnemonic"
unsafeStartValidatorFn UnsafeStartValidatorCmdCreator
)

type UnsafeStartValidatorCmdCreator func(ac appCreator) *cobra.Command

type initArgs struct {
algo string
chainID string
Expand Down Expand Up @@ -93,9 +96,11 @@ func addTestnetFlagsToCmd(cmd *cobra.Command) {
})
}

// NewTestnetCmd creates a root testnet command with subcommands to run an in-process testnet or initialize
// validator configuration files for running a multi-validator testnet in a separate process
func NewTestnetCmd(mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator) *cobra.Command {
// NewTestnetCmd creates a root testnet command with subcommands to:
// 1. run an in-process testnet or
// 2. initialize validator configuration files for running a multi-validator testnet in a separate process or
// 3. update application and consensus state with the local validator info
func NewTestnetCmd(mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator, appCreator appCreator) *cobra.Command {
testnetCmd := &cobra.Command{
Use: "testnet",
Short: "subcommands for starting or configuring local testnets",
Expand All @@ -106,6 +111,11 @@ func NewTestnetCmd(mbm module.BasicManager, genBalIterator banktypes.GenesisBala

testnetCmd.AddCommand(testnetStartCmd())
testnetCmd.AddCommand(testnetInitFilesCmd(mbm, genBalIterator))
// if the binary is built with the unsafe_start_local_validator tag, unsafeStartValidatorFn will be set
// and the subcommand will be added
if unsafeStartValidatorFn != nil {
testnetCmd.AddCommand(unsafeStartValidatorFn(appCreator))
}

return testnetCmd
}
Expand Down
Loading

0 comments on commit 14e5927

Please sign in to comment.