Skip to content

Commit

Permalink
Testnet refactor (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
nghuyenthevinh2000 committed May 2, 2023
1 parent 04401e3 commit 30e0863
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 82 deletions.
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ WORKDIR /code
COPY ${source} /code/

# Install mimalloc
RUN git clone --depth 1 https://github.com/microsoft/mimalloc; cd mimalloc; mkdir build; cd build; cmake ..; make -j$(nproc); make install
RUN git clone -b v2.0.0 --depth 1 https://github.com/microsoft/mimalloc; cd mimalloc; mkdir build; cd build; cmake ..; make -j$(nproc); make install
ENV MIMALLOC_RESERVE_HUGE_OS_PAGES=4

# Cosmwasm - download correct libwasmvm version and verify checksum
Expand Down
23 changes: 12 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ 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)

#OPERATOR ARGS
NODE_VERSION := $(if $(NODE_VERSION),$(NODE_VERSION),alpine3.17)

ifneq ($(OS),Windows_NT)
UNAME_S = $(shell uname -s)
Expand Down Expand Up @@ -251,7 +253,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 +264,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 All @@ -293,7 +294,7 @@ build-operator-img-core:
docker-compose -f contrib/terra-operator/docker-compose.build.yml build core --no-cache

build-operator-img-node:
@if ! docker image inspect public.ecr.aws/p5q2r9h7/core:alpine3.17 &>/dev/null ; then make build-operator-img-core ; fi
@if ! docker image inspect public.ecr.aws/classic-terra/core:${NODE_VERSION} &>/dev/null ; then make build-operator-img-core ; fi
docker-compose -f contrib/terra-operator/docker-compose.build.yml build node --no-cache

.PHONY: build-operator-img-all build-operator-img-core build-operator-img-node
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
6 changes: 3 additions & 3 deletions contrib/terra-operator/docker-compose.build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3.8'

services:
core:
image: public.ecr.aws/p5q2r9h7/core:alpine3.17
image: public.ecr.aws/classic-terra/core:alpine3.17
build:
context: ../..
dockerfile: Dockerfile
Expand All @@ -11,12 +11,12 @@ services:
labels:
- "description=Contains the terrad binary"
node:
image: public.ecr.aws/p5q2r9h7/node:alpine3.17
image: public.ecr.aws/classic-terra/node:alpine3.17
build:
context: .
dockerfile: Dockerfile
args:
base_image: public.ecr.aws/p5q2r9h7/core
base_image: public.ecr.aws/classic-terra/core
version: alpine3.17
platforms:
- "linux/amd64"
Expand Down
2 changes: 1 addition & 1 deletion contrib/terra-operator/docker-compose.node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3.8'
services:
node:
container_name: node
image: public.ecr.aws/p5q2r9h7/node:alpine3.17
image: public.ecr.aws/classic-terra/node:alpine3.17
platform: linux/amd64
environment:
- CHAINID=test
Expand Down
Loading

0 comments on commit 30e0863

Please sign in to comment.