1+ #! /bin/bash
2+ set -eux
3+
4+ BINARY_NAME=$1
5+
6+ # User balance of stake tokens
7+ USER_COINS=" 100000000000stake"
8+ # Amount of stake tokens staked
9+ STAKE=" 100000000stake"
10+ # Node IP address
11+ NODE_IP=" 127.0.0.1"
12+
13+ # Home directory
14+ HOME_DIR=$HOME
15+
16+ # Validator moniker
17+ MONIKERS=(" coordinator" " alice" " bob" )
18+ LEAD_VALIDATOR_MONIKER=" coordinator"
19+
20+ PROV_NODES_ROOT_DIR=${HOME_DIR} /nodes/provider
21+ CONS_NODES_ROOT_DIR=${HOME_DIR} /nodes/consumer
22+
23+ # Base port. Ports assigned after these ports sequentially by nodes.
24+ RPC_LADDR_BASEPORT=29170
25+ P2P_LADDR_BASEPORT=29180
26+ GRPC_LADDR_BASEPORT=29190
27+ NODE_ADDRESS_BASEPORT=29200
28+ PPROF_LADDR_BASEPORT=29210
29+ CLIENT_BASEPORT=29220
30+
31+ # keeps a comma separated list of node addresses for provider and consumer
32+ PROVIDER_NODE_LISTEN_ADDR_STR=" "
33+ CONSUMER_NODE_LISTEN_ADDR_STR=" "
34+
35+ # Strings that keep the homes of provider nodes and homes of consumer nodes
36+ PROV_NODES_HOME_STR=" "
37+ CONS_NODES_HOME_STR=" "
38+
39+ PROVIDER_COMETMOCK_ADDR=tcp://$NODE_IP :22331
40+ CONSUMER_COMETMOCK_ADDR=tcp://$NODE_IP :22332
41+
42+ # Clean start
43+ pkill -f $BINARY_NAME & > /dev/null || true
44+ pkill -f cometmock & > /dev/null || true
45+ sleep 1
46+ rm -rf ${PROV_NODES_ROOT_DIR}
47+ rm -rf ${CONS_NODES_ROOT_DIR}
48+
49+ # Let lead validator create genesis file
50+ LEAD_VALIDATOR_PROV_DIR=${PROV_NODES_ROOT_DIR} /provider-${LEAD_VALIDATOR_MONIKER}
51+ LEAD_VALIDATOR_CONS_DIR=${CONS_NODES_ROOT_DIR} /consumer-${LEAD_VALIDATOR_MONIKER}
52+ LEAD_PROV_KEY=${LEAD_VALIDATOR_MONIKER} -key
53+ LEAD_PROV_LISTEN_ADDR=tcp://${NODE_IP} :${RPC_LADDR_BASEPORT}
54+
55+ for index in " ${! MONIKERS[@]} "
56+ do
57+ MONIKER=${MONIKERS[$index]}
58+ # validator key
59+ PROV_KEY=${MONIKER} -key
60+
61+ # home directory of this validator on provider
62+ PROV_NODE_DIR=${PROV_NODES_ROOT_DIR} /provider-${MONIKER}
63+
64+ # home directory of this validator on consumer
65+ CONS_NODE_DIR=${CONS_NODES_ROOT_DIR} /consumer-${MONIKER}
66+
67+ # Build genesis file and node directory structure
68+ $BINARY_NAME init $MONIKER --chain-id provider --home ${PROV_NODE_DIR}
69+ jq " .app_state.gov.params.voting_period = \" 100000s\" | .app_state.staking.params.unbonding_time = \" 86400s\" | .app_state.slashing.params.signed_blocks_window=\" 1000\" " \
70+ ${PROV_NODE_DIR} /config/genesis.json > \
71+ ${PROV_NODE_DIR} /edited_genesis.json && mv ${PROV_NODE_DIR} /edited_genesis.json ${PROV_NODE_DIR} /config/genesis.json
72+
73+
74+ sleep 1
75+
76+ # Create account keypair
77+ $BINARY_NAME keys add $PROV_KEY --home ${PROV_NODE_DIR} --keyring-backend test --output json > ${PROV_NODE_DIR} /${PROV_KEY} .json 2>&1
78+ sleep 1
79+
80+ # copy genesis in, unless this validator is the lead validator
81+ if [ $MONIKER != $LEAD_VALIDATOR_MONIKER ]; then
82+ cp ${LEAD_VALIDATOR_PROV_DIR} /config/genesis.json ${PROV_NODE_DIR} /config/genesis.json
83+ fi
84+
85+ # Add stake to user
86+ PROV_ACCOUNT_ADDR=$( jq -r ' .address' ${PROV_NODE_DIR} /${PROV_KEY} .json)
87+ $BINARY_NAME genesis add-genesis-account $PROV_ACCOUNT_ADDR $USER_COINS --home ${PROV_NODE_DIR} --keyring-backend test
88+ sleep 1
89+
90+ # copy genesis out, unless this validator is the lead validator
91+ if [ $MONIKER != $LEAD_VALIDATOR_MONIKER ]; then
92+ cp ${PROV_NODE_DIR} /config/genesis.json ${LEAD_VALIDATOR_PROV_DIR} /config/genesis.json
93+ fi
94+
95+ PPROF_LADDR=${NODE_IP} :$(( $PPROF_LADDR_BASEPORT + $index ))
96+ P2P_LADDR_PORT=$(( $P2P_LADDR_BASEPORT + $index ))
97+
98+ # adjust configs of this node
99+ sed -i -r ' s/timeout_commit = "5s"/timeout_commit = "3s"/g' ${PROV_NODE_DIR} /config/config.toml
100+ sed -i -r ' s/timeout_propose = "3s"/timeout_propose = "1s"/g' ${PROV_NODE_DIR} /config/config.toml
101+
102+ # make address book non-strict. necessary for this setup
103+ sed -i -r ' s/addr_book_strict = true/addr_book_strict = false/g' ${PROV_NODE_DIR} /config/config.toml
104+
105+ # avoid port double binding
106+ sed -i -r " s/pprof_laddr = \" localhost:6060\" /pprof_laddr = \" ${PPROF_LADDR} \" /g" ${PROV_NODE_DIR} /config/config.toml
107+
108+ # allow duplicate IP addresses (all nodes are on the same machine)
109+ sed -i -r ' s/allow_duplicate_ip = false/allow_duplicate_ip = true/g' ${PROV_NODE_DIR} /config/config.toml
110+ done
111+
112+ for MONIKER in " ${MONIKERS[@]} "
113+ do
114+ # validator key
115+ PROV_KEY=${MONIKER} -key
116+
117+ # home directory of this validator on provider
118+ PROV_NODE_DIR=${PROV_NODES_ROOT_DIR} /provider-${MONIKER}
119+
120+ # copy genesis in, unless this validator is the lead validator
121+ if [ $MONIKER != $LEAD_VALIDATOR_MONIKER ]; then
122+ cp ${LEAD_VALIDATOR_PROV_DIR} /config/genesis.json* ${PROV_NODE_DIR} /config/genesis.json
123+ fi
124+
125+ # Stake 1/1000 user's coins
126+ $BINARY_NAME genesis gentx $PROV_KEY $STAKE --chain-id provider --home ${PROV_NODE_DIR} --keyring-backend test --moniker $MONIKER
127+ sleep 1
128+
129+ # Copy gentxs to the lead validator for possible future collection.
130+ # Obviously we don't need to copy the first validator's gentx to itself
131+ if [ $MONIKER != $LEAD_VALIDATOR_MONIKER ]; then
132+ cp ${PROV_NODE_DIR} /config/gentx/* ${LEAD_VALIDATOR_PROV_DIR} /config/gentx/
133+ fi
134+ done
135+
136+ # Collect genesis transactions with lead validator
137+ $BINARY_NAME genesis collect-gentxs --home ${LEAD_VALIDATOR_PROV_DIR} --gentx-dir ${LEAD_VALIDATOR_PROV_DIR} /config/gentx/
138+
139+ sleep 1
140+
141+
142+ for index in " ${! MONIKERS[@]} "
143+ do
144+ MONIKER=${MONIKERS[$index]}
145+
146+ PERSISTENT_PEERS=" "
147+
148+ for peer_index in " ${! MONIKERS[@]} "
149+ do
150+ if [ $index == $peer_index ]; then
151+ continue
152+ fi
153+ PEER_MONIKER=${MONIKERS[$peer_index]}
154+
155+ PEER_PROV_NODE_DIR=${PROV_NODES_ROOT_DIR} /provider-${PEER_MONIKER}
156+
157+ PEER_NODE_ID=$( $BINARY_NAME tendermint show-node-id --home ${PEER_PROV_NODE_DIR} )
158+
159+ PEER_P2P_LADDR_PORT=$(( $P2P_LADDR_BASEPORT + $peer_index ))
160+ PERSISTENT_PEERS=" $PERSISTENT_PEERS ,$PEER_NODE_ID @${NODE_IP} :${PEER_P2P_LADDR_PORT} "
161+ done
162+
163+ # remove trailing comma from persistent peers
164+ PERSISTENT_PEERS=${PERSISTENT_PEERS: 1}
165+
166+ # validator key
167+ PROV_KEY=${MONIKER} -key
168+
169+ # home directory of this validator on provider
170+ PROV_NODE_DIR=${PROV_NODES_ROOT_DIR} /provider-${MONIKER}
171+
172+ # home directory of this validator on consumer
173+ CONS_NODE_DIR=${PROV_NODES_ROOT_DIR} /consumer-${MONIKER}
174+
175+ # copy genesis in, unless this validator is already the lead validator and thus it already has its genesis
176+ if [ $MONIKER != $LEAD_VALIDATOR_MONIKER ]; then
177+ cp ${LEAD_VALIDATOR_PROV_DIR} /config/genesis.json ${PROV_NODE_DIR} /config/genesis.json
178+ fi
179+
180+ RPC_LADDR_PORT=$(( $RPC_LADDR_BASEPORT + $index ))
181+ P2P_LADDR_PORT=$(( $P2P_LADDR_BASEPORT + $index ))
182+ GRPC_LADDR_PORT=$(( $GRPC_LADDR_BASEPORT + $index ))
183+ NODE_ADDRESS_PORT=$(( $NODE_ADDRESS_BASEPORT + $index ))
184+
185+ PROVIDER_NODE_LISTEN_ADDR_STR=" ${NODE_IP} :${NODE_ADDRESS_PORT} ,$PROVIDER_NODE_LISTEN_ADDR_STR "
186+ PROV_NODES_HOME_STR=" ${PROV_NODE_DIR} ,$PROV_NODES_HOME_STR "
187+
188+ # Start gaia
189+ $BINARY_NAME start \
190+ --home ${PROV_NODE_DIR} \
191+ --transport=grpc --with-tendermint=false \
192+ --p2p.persistent_peers ${PERSISTENT_PEERS} \
193+ --rpc.laddr tcp://${NODE_IP} :${RPC_LADDR_PORT} \
194+ --grpc.address ${NODE_IP} :${GRPC_LADDR_PORT} \
195+ --address tcp://${NODE_IP} :${NODE_ADDRESS_PORT} \
196+ --p2p.laddr tcp://${NODE_IP} :${P2P_LADDR_PORT} \
197+ --grpc-web.enable=false & > ${PROV_NODE_DIR} /logs &
198+
199+ sleep 5
200+ done
201+
202+ PROVIDER_NODE_LISTEN_ADDR_STR=${PROVIDER_NODE_LISTEN_ADDR_STR:: ${# PROVIDER_NODE_LISTEN_ADDR_STR} -1}
203+ PROV_NODES_HOME_STR=${PROV_NODES_HOME_STR:: ${# PROV_NODES_HOME_STR} -1}
204+
205+ echo " Testnet applications are set up! Run the following command to start CometMock:"
206+ cometmock $PROVIDER_NODE_LISTEN_ADDR_STR ${LEAD_VALIDATOR_PROV_DIR} /config/genesis.json $PROVIDER_COMETMOCK_ADDR $PROV_NODES_HOME_STR grpc
207+
208+ sleep 5
0 commit comments