Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testnet refactor #194

Merged
merged 11 commits into from
Mar 31, 2023
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
go.sum
- name: start localnet
run: |
TESTNET_NVAL=6 make clean localnet-start
make clean localnet-start
if: env.GIT_DIFF
- name: test liveness
run: |
Expand Down
18 changes: 8 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ DOCKER := $(shell which docker)
DOCKER_BUF := $(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace bufbuild/buf

#TESTNET PARAMETERS
TESTNET_NVAL := $(if $(TESTNET_NVAL),$(TESTNET_NVAL),4)
TESTNET_CHAINID := $(if $(TESTNET_CHAINID),$(TESTNET_CHAINID),localnet-1)
TESTNET_VOTING_PERIOD := $(if $(TESTNET_VOTING_PERIOD),$(TESTNET_VOTING_PERIOD),86400s)
TESTNET_NVAL := $(if $(TESTNET_NVAL),$(TESTNET_NVAL),7)
TESTNET_CHAINID := $(if $(TESTNET_CHAINID),$(TESTNET_CHAINID),localterra)

ifneq ($(OS),Windows_NT)
UNAME_S = $(shell uname -s)
Expand Down Expand Up @@ -251,7 +250,7 @@ proto-check-breaking:
### Localnet ###
###############################################################################

# Run a 4-node testnet locally
# Run a 7-node testnet locally by default
localnet-start: localnet-stop build-linux
$(if $(shell $(DOCKER) inspect -f '{{ .Id }}' classic-terra/terrad-env 2>/dev/null),$(info found image classic-terra/terrad-env),$(MAKE) -C contrib/localnet terrad-env)
if ! [ -f build/node0/terrad/config/genesis.json ]; then $(DOCKER) run --platform linux/amd64 --rm \
Expand All @@ -262,24 +261,23 @@ localnet-start: localnet-stop build-linux
-v /etc/shadow:/etc/shadow:ro \
classic-terra/terrad-env testnet --chain-id ${TESTNET_CHAINID} --v ${TESTNET_NVAL} -o . --starting-ip-address 192.168.10.2 --keyring-backend=test; \
fi
for i in $$(seq 0 5); do \
echo $$i; \
jq '.app_state.gov.voting_params.voting_period = "${TESTNET_VOTING_PERIOD}"' build/node$$i/terrad/config/genesis.json > build/node$$i/terrad/config/genesis.json.tmp; \
mv build/node$$i/terrad/config/genesis.json.tmp build/node$$i/terrad/config/genesis.json; \
done
docker-compose up -d

localnet-start-upgrade: localnet-upgrade-stop build-linux
$(MAKE) -C contrib/updates build-cosmovisor-linux BUILDDIR=$(BUILDDIR)
$(if $(shell $(DOCKER) inspect -f '{{ .Id }}' classic-terra/terrad-upgrade-env 2>/dev/null),$(info found image classic-terra/terrad-upgrade-env),$(MAKE) -C contrib/localnet terrad-upgrade-env)
bash contrib/updates/prepare_cosmovisor.sh $(BUILDDIR)
bash contrib/updates/prepare_cosmovisor.sh $(BUILDDIR) ${TESTNET_NVAL} ${TESTNET_CHAINID}
docker-compose -f ./contrib/updates/docker-compose.yml up -d

localnet-upgrade-stop:
docker-compose -f ./contrib/updates/docker-compose.yml down
rm -rf build/node*
rm -rf build/gentxs

localnet-stop:
docker-compose down
rm -rf build/node*
rm -rf build/gentxs

.PHONY: localnet-start localnet-stop

Expand Down
94 changes: 51 additions & 43 deletions cmd/terrad/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"os"
"path/filepath"
"time"

"github.com/spf13/cobra"
tmconfig "github.com/tendermint/tendermint/config"
Expand All @@ -32,6 +33,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

core "github.com/classic-terra/core/types"
Expand Down Expand Up @@ -119,8 +121,8 @@ func InitTestnet(
chainID = "chain-" + tmrand.NewRand().Str(6)
}

nodeIDs := make([]string, 6)
valPubKeys := make([]cryptotypes.PubKey, 6)
nodeIDs := make([]string, numValidators)
valPubKeys := make([]cryptotypes.PubKey, numValidators)

_, appConfig := initAppConfig()
terraappConfig := appConfig.(TerraAppConfig)
Expand All @@ -140,9 +142,7 @@ func InitTestnet(
inBuf := bufio.NewReader(cmd.InOrStdin())
// generate private keys, node IDs, and initial transactions

// can have 6 validators at most
// but need to go through all nodes to initialize config and such
for i := 0; i < 6; i++ {
for i := 0; i < numValidators; i++ {
nodeDirName := fmt.Sprintf("%s%d", nodeDirPrefix, i)
nodeDir := filepath.Join(outputDir, nodeDirName, nodeDaemonHome)
gentxsDir := filepath.Join(outputDir, "gentxs")
Expand Down Expand Up @@ -220,43 +220,40 @@ func InitTestnet(
genAccounts = append(genAccounts, authtypes.NewBaseAccount(addr, nil, 0, 0))
valTokens := sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)

// create gentxs only for numValidators
if i < numValidators {
// create the validator for node i
createValMsg, err := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addr),
valPubKeys[i],
sdk.NewCoin(core.MicroLunaDenom, valTokens),
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
stakingtypes.NewCommissionRates(sdk.OneDec(), sdk.OneDec(), sdk.OneDec()),
sdk.OneInt(),
)
if err != nil {
return err
}
// create the validator for node i
createValMsg, err := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addr),
valPubKeys[i],
sdk.NewCoin(core.MicroLunaDenom, valTokens),
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
stakingtypes.NewCommissionRates(sdk.OneDec(), sdk.OneDec(), sdk.OneDec()),
sdk.OneInt(),
)
if err != nil {
return err
}

// create gentx (create validator) and write to file
txBuilder := clientCtx.TxConfig.NewTxBuilder()
if err := txBuilder.SetMsgs(createValMsg); err != nil {
return err
}
txBuilder.SetMemo(memo)
txFactory := tx.Factory{}
txFactory = txFactory.
WithChainID(chainID).
WithMemo(memo).
WithKeybase(kb).
WithTxConfig(clientCtx.TxConfig)
if err := tx.Sign(txFactory, nodeDirName, txBuilder, true); err != nil {
return err
}
txBz, err := clientCtx.TxConfig.TxJSONEncoder()(txBuilder.GetTx())
if err != nil {
return err
}
if err := writeFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBz); err != nil {
return err
}
// create gentx (create validator) and write to file
txBuilder := clientCtx.TxConfig.NewTxBuilder()
if err := txBuilder.SetMsgs(createValMsg); err != nil {
return err
}
txBuilder.SetMemo(memo)
txFactory := tx.Factory{}
txFactory = txFactory.
WithChainID(chainID).
WithMemo(memo).
WithKeybase(kb).
WithTxConfig(clientCtx.TxConfig)
if err := tx.Sign(txFactory, nodeDirName, txBuilder, true); err != nil {
return err
}
txBz, err := clientCtx.TxConfig.TxJSONEncoder()(txBuilder.GetTx())
if err != nil {
return err
}
if err := writeFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBz); err != nil {
return err
}

// write config file app.toml
Expand Down Expand Up @@ -303,8 +300,19 @@ func initGenFiles(
clientCtx.Codec.MustUnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState)

bankGenState.Balances = genBalances
totalSupply := sdk.NewCoins()
for _, balance := range bankGenState.Balances {
totalSupply = totalSupply.Add(balance.Coins...)
}
bankGenState.Supply = totalSupply
appGenState[banktypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&bankGenState)

// set gov in the genesis state
var govGenState govtypes.GenesisState
clientCtx.Codec.MustUnmarshalJSON(appGenState[govtypes.ModuleName], &govGenState)
govGenState.VotingParams.VotingPeriod = time.Second * 30
appGenState[govtypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&govGenState)

appGenStateJSON, err := json.MarshalIndent(appGenState, "", " ")
if err != nil {
return err
Expand All @@ -317,7 +325,7 @@ func initGenFiles(
}

// generate empty genesis files for each validator and save
for i := 0; i < 6; i++ {
for i := 0; i < numValidators; i++ {
if err := genDoc.SaveAs(genFiles[i]); err != nil {
return err
}
Expand All @@ -333,7 +341,7 @@ func collectGenFiles(
var appState json.RawMessage
genTime := tmtime.Now()

for i := 0; i < 6; i++ {
for i := 0; i < numValidators; i++ {
nodeDirName := fmt.Sprintf("%s%d", nodeDirPrefix, i)
nodeDir := filepath.Join(outputDir, nodeDirName, nodeDaemonHome)
gentxsDir := filepath.Join(outputDir, "gentxs")
Expand Down
2 changes: 1 addition & 1 deletion contrib/localnet/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ terrad-env:
docker build --platform linux/amd64 --no-cache --build-arg UID=$(shell id -u) --build-arg GID=$(shell id -g) --tag classic-terra/terrad-env terrad-env

terrad-upgrade-env:
docker build --platform linux/amd64 --build-arg UID=$(shell id -u) --build-arg GID=$(shell id -g) --build-arg entrypoint=cosmovisor-entrypoint.sh --tag classic-terra/terrad-upgrade-env terrad-env
docker build --platform linux/amd64 --no-cache --build-arg UID=$(shell id -u) --build-arg GID=$(shell id -g) --build-arg entrypoint=cosmovisor-entrypoint.sh --tag classic-terra/terrad-upgrade-env terrad-env

.PHONY: all terrad-env terrad-upgrade-env
2 changes: 1 addition & 1 deletion contrib/localnet/deposit-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#
# start network
TESTNET_NVAL=6 TESTNET_VOTING_PERIOD=40s make clean localnet-start
make clean localnet-start

#
# convenience
Expand Down
67 changes: 63 additions & 4 deletions contrib/updates/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ version: '3'
services:
terradnode0:
container_name: terradnode0
platform: linux/amd64
image: "classic-terra/terrad-upgrade-env"
ports:
- "9090:9090"
- "26656-26657:26656-26657"
environment:
- ID=0
- LOG=$${LOG:-terrad.log}
- LOG=terrad.log
- DAEMON_HOME=/terrad/node0/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
Expand All @@ -21,13 +22,14 @@ services:

terradnode1:
container_name: terradnode1
platform: linux/amd64
image: "classic-terra/terrad-upgrade-env"
ports:
- "9091:9090"
- "26659-26660:26656-26657"
environment:
- ID=1
- LOG=$${LOG:-terrad.log}
- LOG=terrad.log
- DAEMON_HOME=/terrad/node1/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
Expand All @@ -39,10 +41,11 @@ services:

terradnode2:
container_name: terradnode2
platform: linux/amd64
image: "classic-terra/terrad-upgrade-env"
environment:
- ID=2
- LOG=$${LOG:-terrad.log}
- LOG=terrad.log
- DAEMON_HOME=/terrad/node2/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
Expand All @@ -57,10 +60,11 @@ services:

terradnode3:
container_name: terradnode3
platform: linux/amd64
image: "classic-terra/terrad-upgrade-env"
environment:
- ID=3
- LOG=$${LOG:-terrad.log}
- LOG=terrad.log
- DAEMON_HOME=/terrad/node3/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
Expand All @@ -72,7 +76,62 @@ services:
networks:
localnet:
ipv4_address: 192.168.10.5
terradnode4:
container_name: terradnode4
platform: linux/amd64
image: "classic-terra/terrad-upgrade-env"
environment:
- ID=4
- LOG=terrad.log
- DAEMON_HOME=/terrad/node4/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
ports:
- "9094:9090"
- "26665-26666:26656-26657"
volumes:
- ../../build:/terrad:Z
networks:
localnet:
ipv4_address: 192.168.10.6
ZaradarBH marked this conversation as resolved.
Show resolved Hide resolved

terradnode5:
container_name: terradnode5
platform: linux/amd64
image: "classic-terra/terrad-upgrade-env"
environment:
- ID=5
- LOG=terrad.log
- DAEMON_HOME=/terrad/node5/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
ports:
- "9095:9090"
- "26667-26668:26656-26657"
volumes:
- ../../build:/terrad:Z
networks:
localnet:
ipv4_address: 192.168.10.7

terradnode6:
container_name: terradnode6
platform: linux/amd64
image: "classic-terra/terrad-upgrade-env"
environment:
- ID=6
- LOG=terrad.log
- DAEMON_HOME=/terrad/node6/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
ports:
- "9096:9090"
- "26669-26670:26656-26657"
volumes:
- ../../build:/terrad:Z
networks:
localnet:
ipv4_address: 192.168.10.8
networks:
localnet:
driver: bridge
Expand Down
12 changes: 8 additions & 4 deletions contrib/updates/prepare_cosmovisor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

# These fields should be fetched automatically in the future
# Need to do more upgrade to see upgrade patterns
OLD_VERSION=v1.0.5
OLD_VERSION=v1.1.0
SOFTWARE_UPGRADE_NAME=$(ls -td -- ./app/upgrades/* | head -n 1 | cut -d'/' -f4)
BUILDDIR=$1
TESTNET_NVAL=$2
TESTNET_CHAINID=$3

# check if BUILDDIR is set
if [ -z "$BUILDDIR" ]; then
Expand All @@ -33,18 +35,19 @@ if [ ! -f "$BUILDDIR/old/terrad" ]; then
docker rm old-temp
fi

# prepare cosmovisor config in four nodes
# prepare cosmovisor config in TESTNET_NVAL nodes
if [ ! -f "$BUILDDIR/node0/terrad/config/genesis.json" ]; then docker run --rm \
--user $(id -u):$(id -g) \
-v $BUILDDIR:/terrad:Z \
-v /etc/group:/etc/group:ro \
-v /etc/passwd:/etc/passwd:ro \
-v /etc/shadow:/etc/shadow:ro \
--entrypoint /terrad/old/terrad \
classic-terra/terrad-upgrade-env testnet --v 4 -o . --starting-ip-address 192.168.10.2 --keyring-backend=test --home /terrad;
--platform linux/amd64 \
classic-terra/terrad-upgrade-env testnet --v $TESTNET_NVAL --chain-id $TESTNET_CHAINID -o . --starting-ip-address 192.168.10.2 --keyring-backend=test --home=temp; \
fi

for i in {0..3}; do
for (( i=0; i<$TESTNET_NVAL; i++ )); do
CURRENT=$BUILDDIR/node$i/terrad

# change gov params voting_period
Expand All @@ -60,6 +63,7 @@ for i in {0..3}; do
-e DAEMON_NAME=terrad \
-e DAEMON_RESTART_AFTER_UPGRADE=true \
--entrypoint /terrad/cosmovisor \
--platform linux/amd64 \
classic-terra/terrad-upgrade-env init /terrad/old/terrad
mkdir -p $CURRENT/cosmovisor/upgrades/$SOFTWARE_UPGRADE_NAME/bin
cp $BUILDDIR/terrad $CURRENT/cosmovisor/upgrades/$SOFTWARE_UPGRADE_NAME/bin
Expand Down
Loading