Skip to content

Commit

Permalink
Merge pull request #2 from jtremback/try-persistent-peers
Browse files Browse the repository at this point in the history
Try persistent peers
  • Loading branch information
jtremback authored Feb 24, 2022
2 parents 358606a + c794ecf commit 697dafe
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 168 deletions.
2 changes: 1 addition & 1 deletion interchain-security
10 changes: 6 additions & 4 deletions start-docker.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#!/bin/bash
# set -eux
# If -e is not set then if the build fails, it will use the old container, resulting in a very confusing debugging situation
# Setting -e makes it error out if the build fails
set -eux

CONTAINER_NAME=$1
INSTANCE_NAME=$2
# Must be in this format "-p 9090:9090 -p 26657:26657 -p 1317:1317 -p 8545:8545"
EXPOSE_PORTS=$3

# Build the Docker container
docker build -t "$CONTAINER_NAME" .

# Remove existing container instance
set +e
docker rm -f "$INSTANCE_NAME"
set -e

# Build the Docker container
docker build -t "$CONTAINER_NAME" .

# Run new test container instance
docker run --name "$INSTANCE_NAME" --cap-add=NET_ADMIN $EXPOSE_PORTS "$CONTAINER_NAME" /bin/bash /testnet-scripts/beacon.sh
85 changes: 0 additions & 85 deletions testnet-scripts/start-chain/setup-validators.sh

This file was deleted.

176 changes: 154 additions & 22 deletions testnet-scripts/start-chain/start-chain.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/bin/bash
set -eux

# the directory of this script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# The gaiad binary
BIN=$1

# Mnemonics with which to start nodes
MNEMONICS=$2
# JSON array of validator information
# [{
# mnemonic: "crackle snap pop ... etc",
# allocation: "10000000000stake,10000000000footoken",
# stake: "5000000000stake",
# number: "0"
# }, ... ]
VALIDATORS=$2

# The chain ID
CHAIN_ID=$3
Expand All @@ -18,32 +21,161 @@ CHAIN_ID=$3
# For example: "7.7.7"
CHAIN_IP_PREFIX=$4

# Default: 26657
RPC_PORT=$5
# A transformation to apply to the genesis file, as a jq string
GENESIS_TRANSFORM=$5

# Default: 9090
GRPC_PORT=$6
# Whether to skip collecting gentxs so that the genesis does not have them
SKIP_GENTX=$6

# A transformation to apply to the genesis file, as a jq string
GENESIS_TRANSFORM=$7
# Whether to copy in validator configs from somewhere else
COPY_KEYS=$7

# How much coin to give each validator on start
# Default: "10000000000stake,10000000000footoken"
ALLOCATION=$8

# Amount for each validator to stake
STAKE_AMOUNT=$9

# Whether to skip collecting gentxs so that the genesis does not have them
SKIP_GENTX=${10}
# CREATE VALIDATORS AND DO GENESIS CEREMONY

# Get number of nodes from length of validators array
NODES=$(echo "$VALIDATORS" | jq '. | length')

# first we start a genesis.json with the first validator
# the first validator will also collect the gentx's once gnerated
FIRST_VAL_ID=$(echo "$VALIDATORS" | jq -r ".[0].number")
echo "$VALIDATORS" | jq -r ".[0].mnemonic" | $BIN init --home /$CHAIN_ID/validator$FIRST_VAL_ID --chain-id=$CHAIN_ID validator$FIRST_VAL_ID --recover > /dev/null

# Apply jq transformations to genesis file
jq "$GENESIS_TRANSFORM" /$CHAIN_ID/validator$FIRST_VAL_ID/config/genesis.json > /$CHAIN_ID/edited-genesis.json
mv /$CHAIN_ID/edited-genesis.json /$CHAIN_ID/genesis.json




# CREATE VALIDATOR HOME FOLDERS ETC

for i in $(seq 0 $(($NODES - 1)));
do
VAL_ID=$(echo "$VALIDATORS" | jq -r ".[$i].number")
# make the folders for this validator
mkdir -p /$CHAIN_ID/validator$VAL_ID/config/

# Generate an application key for each validator
# Sets up an arbitrary number of validators on a single machine by manipulating
# the --home parameter on gaiad
echo "$VALIDATORS" | jq -r ".[$i].mnemonic" | $BIN keys add validator$VAL_ID \
--home /$CHAIN_ID/validator$VAL_ID \
--keyring-backend test \
--recover > /dev/null

# Give validators their initial token allocations
# move the genesis in
mv /$CHAIN_ID/genesis.json /$CHAIN_ID/validator$VAL_ID/config/genesis.json

# give this validator some money
ALLOCATION=$(echo "$VALIDATORS" | jq -r ".[$i].allocation")
$BIN add-genesis-account validator$VAL_ID $ALLOCATION \
--home /$CHAIN_ID/validator$VAL_ID \
--keyring-backend test

# move the genesis back out
mv /$CHAIN_ID/validator$VAL_ID/config/genesis.json /$CHAIN_ID/genesis.json
done



# MAKE GENTXS AND SET UP LOCAL VALIDATOR STATE

for i in $(seq 0 $(($NODES - 1)));
do
VAL_ID=$(echo "$VALIDATORS" | jq -r ".[$i].number")
# Copy in the genesis.json
cp /$CHAIN_ID/genesis.json /$CHAIN_ID/validator$VAL_ID/config/genesis.json

# Make a gentx (this command also sets up validator state on disk even if we are not going to use the gentx for anything)
STAKE_AMOUNT=$(echo "$VALIDATORS" | jq -r ".[$i].stake")
$BIN gentx validator$VAL_ID "$STAKE_AMOUNT" \
--home /$CHAIN_ID/validator$VAL_ID \
--keyring-backend test \
--moniker validator$VAL_ID \
--chain-id=$CHAIN_ID \
--ip $CHAIN_IP_PREFIX.$VAL_ID

# Copy gentxs to the first validator for possible future collection.
# Obviously we don't need to copy the first validator's gentx to itself
if [ $VAL_ID != $FIRST_VAL_ID ]; then
cp /$CHAIN_ID/validator$VAL_ID/config/gentx/* /$CHAIN_ID/validator$FIRST_VAL_ID/config/gentx/
fi

# Copy in keys from another chain. This is used to start a consumer chain
if [ "$COPY_KEYS" != "" ] ; then
cp /$COPY_KEYS/validator$VAL_ID/config/priv_validator_key.json /$CHAIN_ID/validator$VAL_ID/config/
cp /$COPY_KEYS/validator$VAL_ID/config/node_key.json /$CHAIN_ID/validator$VAL_ID/config/
fi
done




# COLLECT GENTXS IF WE ARE STARTING A NEW CHAIN

if [ "$SKIP_GENTX" = "false" ] ; then
# make the final genesis.json
$BIN collect-gentxs --home /$CHAIN_ID/validator$FIRST_VAL_ID

# and copy it to the root
cp /$CHAIN_ID/validator$FIRST_VAL_ID/config/genesis.json /$CHAIN_ID/genesis.json

# put the now final genesis.json into the correct folders
for i in $(seq 1 $(($NODES - 1)));
do
VAL_ID=$(echo "$VALIDATORS" | jq -r ".[$i].number")
cp /$CHAIN_ID/genesis.json /$CHAIN_ID/validator$VAL_ID/config/genesis.json
done
fi




# START VALIDATOR NODES

for i in $(seq 0 $(($NODES - 1)));
do
VAL_ID=$(echo "$VALIDATORS" | jq -r ".[$i].number")
# add this ip for loopback dialing
ip addr add $CHAIN_IP_PREFIX.$VAL_ID/32 dev eth0 || true # allowed to fail

GAIA_HOME="--home /$CHAIN_ID/validator$VAL_ID"
RPC_ADDRESS="--rpc.laddr tcp://$CHAIN_IP_PREFIX.$VAL_ID:26658"
GRPC_ADDRESS="--grpc.address $CHAIN_IP_PREFIX.$VAL_ID:9091"
LISTEN_ADDRESS="--address tcp://$CHAIN_IP_PREFIX.$VAL_ID:26655"
P2P_ADDRESS="--p2p.laddr tcp://$CHAIN_IP_PREFIX.$VAL_ID:26656"
LOG_LEVEL="--log_level info"
ENABLE_WEBGRPC="--grpc-web.enable=false"

PERSISTENT_PEERS=""

for j in $(seq 0 $(($NODES - 1)));
do
if [ $i -ne $j ]; then
PEER_VAL_ID=$(echo "$VALIDATORS" | jq -r ".[$j].number")
NODE_ID=$($BIN tendermint show-node-id --home /$CHAIN_ID/validator$PEER_VAL_ID)
ADDRESS="$NODE_ID@$CHAIN_IP_PREFIX.$PEER_VAL_ID:26656"
# (jq -r '.body.memo' /$CHAIN_ID/validator$j/config/gentx/*) # Getting the address from the gentx should also work
PERSISTENT_PEERS="$PERSISTENT_PEERS,$ADDRESS"
fi
done

# Remove leading comma and concat to flag
PERSISTENT_PEERS="--p2p.persistent_peers ${PERSISTENT_PEERS:1}"

ARGS="$GAIA_HOME $LISTEN_ADDRESS $RPC_ADDRESS $GRPC_ADDRESS $LOG_LEVEL $P2P_ADDRESS $ENABLE_WEBGRPC $PERSISTENT_PEERS"
$BIN $ARGS start &> /$CHAIN_ID/validator$VAL_ID/logs &
done



# generate accounts and do genesis ceremony
/bin/bash "$DIR/setup-validators.sh" "$BIN" "$MNEMONICS" "$CHAIN_ID" "$CHAIN_IP_PREFIX" "$GENESIS_TRANSFORM" "$ALLOCATION" "$STAKE_AMOUNT" "$SKIP_GENTX"
/bin/bash "$DIR/start-validators.sh" "$BIN" "$MNEMONICS" "$CHAIN_ID" "$CHAIN_IP_PREFIX" "$RPC_PORT" "$GRPC_PORT"

# poll for chain start
set +e
until interchain-securityd query block --node "tcp://$CHAIN_IP_PREFIX.0:26658" | grep -q -v '{"block_id":{"hash":"","parts":{"total":0,"hash":""}},"block":null}'; do sleep 0.3 ; done
until interchain-securityd query block --node "tcp://$CHAIN_IP_PREFIX.$FIRST_VAL_ID:26658" | grep -q -v '{"block_id":{"hash":"","parts":{"total":0,"hash":""}},"block":null}'; do sleep 0.3 ; done
set -e

echo "done!!!!!!!!"
Expand Down
45 changes: 0 additions & 45 deletions testnet-scripts/start-chain/start-validators.sh

This file was deleted.

Loading

0 comments on commit 697dafe

Please sign in to comment.