Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # script functions
- #
- # set values for NETWORK_NAME, EXPECTED_POW, CORS_ORIGIN, PEERS, WALLET_NAME
- init_env() {
- # verbose commands
- # set -x
- export TEZOS_CLIENT_UNSAFE_DISABLE_DISCLAIMER="Y"
- CORS_ORIGIN="*"
- EXPECTED_POW="1"
- if [ -z RPC_PORT ] || [ -z NET_PORT ]; then
- RPC_PORT="8732"
- NET_PORT="9732"
- fi
- # network name
- if [ -z NETWORK_NAME ]; then
- NETWORK_NAME="TEZOS_PRIVATENET"
- fi
- # expected proof of work for generated identities
- if [ -z EXPECTED_POW ]; then
- EXPECTED_POW=26
- fi
- # enable origin for CORS
- if [ -z CORS_ORIGIN ]; then
- CORS_ORIGIN=""
- else
- CORS_ORIGIN="\
- --cors-header='content-type' \
- --cors-origin='$CORS_ORIGIN' \
- "
- fi
- # set manual peers
- if [ -z PEERS ]; then
- PEERS=""
- else
- PEERS_OPTS="\
- --no-bootstrap-peers \
- --connections $((${#PEERS[@]} + 1)) \
- "
- for peer in ${PEERS[@]}; do
- PEERS_OPTS+="--peer $peer "
- done
- fi
- # credited wallet
- if [ -z WALLET_NAME ]; then
- WALLET_NAME="test"
- fi
- }
- # generate node id with $EXPECTED_POW
- generate_identity() {
- ./tezos-node identity &> /dev/null
- if [ $? -ne 0 ]; then
- echo "generating node id..."
- ./tezos-node identity generate $EXPECTED_POW
- fi
- }
- # insert $ACTIVATOR_PK key in protocol
- # $1: protocol folder name
- protocol_activator_key() {
- echo "inserting activator public key in protocol..."
- sed -i "/Signature.Public_key.of_b58check_exn/{n;s/\".*\"/\"$ACTIVATOR_PK\"/;}" \
- src/$1/lib_protocol/data.ml
- # cat src/$1/lib_protocol/data.ml
- GENESIS_HASH=`./tezos-protocol-compiler -hash-only src/$1/lib_protocol/`
- echo "genesis hash: $GENESIS_HASH"
- echo "inserting genesis protocol hash in lib_protocol..."
- sed -i "s/\"hash\": \".*\"/\"hash\": \"$GENESIS_HASH\"/" \
- src/$1/lib_protocol/TEZOS_PROTOCOL
- # cat src/$1/lib_protocol/TEZOS_PROTOCOL
- echo "inserting genesis protocol hash in lib_client protocol..."
- sed -i "/Protocol_hash.of_b58check_exn/{n;s/\".*\"/\"$GENESIS_HASH\"/;}" \
- src/$1/lib_client/client_proto_main.ml
- # cat src/$1/lib_client/client_proto_main.ml
- echo "inserting genesis protocol hash in genesis block..."
- sed -i "/Protocol_hash.of_b58check_exn/{n;s/\".*\"/\"$GENESIS_HASH\"/;}" \
- src/bin_node/genesis_chain.ml
- # cat src/bin_node/genesis_chain.ml
- }
- # injects a protocol for other nodes to access
- # $1: protocol folder name
- inject_protocol() {
- echo "injecting $1..."
- ./tezos-admin-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- inject protocol src/$1/lib_protocol/
- }
- # deletes the blockchain and context. Leaves wallets untouched.
- clear_chain() {
- echo "clearing chain info..."
- rm -rf /home/tezos/.tezos-node/{store,context}
- }
- # change the shell's network name
- # $1: network name
- network_name() {
- echo "changing network name in shell to $1..."
- sed -i "s/name = \".*\" \;/name = \"$1\" \;/" \
- src/lib_shell/distributed_db_message.ml
- }
- # set protocol parameters to be bootstrapped
- # expects $WALLET_PK and $ACTIVATOR_PK
- set_protocol_parameters() {
- PROTOCOL_PARAMETERS=`jq -n \
- --argjson tbb "[\"3\", \"0\"]" \
- --argjson tbbs 0 \
- --argjson bprs 4 \
- --argjson bpc 8 \
- --argjson bpvp 64 \
- --argjson pc 2 \
- --argjson powt "\"-1\"" \
- --argjson ba "[ \
- [\"$WALLET_PK\", \"4000000000000\"], \
- [\"$ACTIVATOR_PK\", \"1\"] \
- ]" \
- '{ \
- time_between_blocks: $tbb, \
- blocks_per_roll_snapshot: $bprs, \
- blocks_per_cycle: $bpc, \
- blocks_per_voting_period: $bpvp, \
- preserved_cycles: $pc, \
- proof_of_work_threshold: $powt, \
- bootstrap_accounts: $ba \
- }'
- `
- echo "rewriting protocol parameters..."
- echo $PROTOCOL_PARAMETERS > scripts/protocol_parameters.json
- }
- # start a node in background and wait for it to be ready
- # expects $NET_ADDR, $EXPECTED_POW, $PEER_OPTS
- start_node() {
- echo "starting node..."
- echo "ports: $RPC_PORT/$NET_PORT"
- # --net-addr $NET_ADDR:$NET_PORT \
- # $PEERS_OPTS \
- ./tezos-node run \
- --rpc-addr $NET_ADDR:$RPC_PORT \
- --expected-pow $EXPECTED_POW \
- --no-bootstrap-peers \
- --connections 1 \
- &
- PID=$!
- echo "started node with pid $PID"
- wait_for_node
- }
- # waits for node to be ready
- wait_for_node() {
- echo "waiting for node to be ready..."
- RET=1
- while [ $RET -ne 0 ]; do
- ./tezos-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- rpc get /chains/main/blocks/head/metadata \
- &> /dev/null
- RET=$?
- done
- echo "node started."
- }
- # generate a wallet and store edpk in variable
- # $1: wallet name
- # $2: variable name to store edpk
- gen_keys() {
- ./tezos-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- list known addresses \
- | grep $1 \
- > /dev/null
- if [ $? -ne 0 ]; then
- echo "generating wallet $1..."
- ./tezos-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- gen keys $1
- fi
- TMP_WALLET=`\
- ./tezos-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- show address $1 \
- --show-secret \
- `
- # show wallet on console
- echo "=================================================="
- echo "$TMP_WALLET"
- echo "=================================================="
- # get wallet public key in $2
- eval $2=`echo "$TMP_WALLET" | grep Public | cut -d ' ' -f 3`
- # get wallet secret key
- KEY=`echo "$TMP_WALLET" | grep Secret | cut -d ' ' -f 3`
- if [ "$1" == "$WALLET_NAME" ] && [ "$MASTER" == "true" ]; then
- echo "posting wallet key..."
- curl -s \
- -H "Content-Type: application/json" \
- -X POST "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/privkey" \
- -d "{ \"key\": \"$KEY\" }"
- fi
- }
- # spin a node to activate the protocol and bootstrap chain after genesis node
- # $1: protocol hash
- activate_protocol() {
- echo "activating custom protocol..."
- ./tezos-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- -block genesis \
- activate protocol $1 \
- with fitness 1 \
- and key activator \
- and parameters ./scripts/protocol_parameters.json
- wait_protocol $1
- }
- # wait for protocol to be running
- # $1: protocol hash
- wait_protocol() {
- PROTO=""
- while [[ $PROTO != $1 ]]; do
- PROTO=`\
- ./tezos-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- rpc get /chains/main/blocks/head/metadata \
- | jq .next_protocol \
- | cut -d '"' -f 2 \
- `
- done
- }
- # kill the node which has pid $PID
- kill_node() {
- echo "kill node pid $PID"
- kill $PID
- wait $PID
- }
- # recompile tezos
- recompile() {
- echo "setting opam env..."
- eval $(opam env)
- echo "recompiling protocol & shell..."
- make
- }
- # compiles the originated smart contract
- originate_smart_contract() {
- echo "compiling smart contract..."
- /tezos/liquidity/liquidity /tezos/node/smart-contract.reliq
- STORAGE=`cat /tezos/node/smart-contract.init.tz`
- /tezos/liquidity/liquidity \
- --convert /tezos/node/smart-contract.reliq \
- > /tezos/node/smart-contract.liq
- # --force in case it's the second time we do it
- ./tezos-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- originate contract tzcontract \
- for $WALLET_NAME \
- transferring 1.0 from $WALLET_NAME \
- running /tezos/node/smart-contract.tz \
- --init "$STORAGE" \
- --burn-cap 100.0 \
- --force \
- > origination.log
- OPHASH=`cat origination.log | grep "Operation hash" | cut -d "'" -f 2`
- wait_for_op $OPHASH
- }
- # waits for an operation to be included
- # $1: op hash
- wait_for_op() {
- echo "waiting for op $1..."
- ./tezos-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- wait for $1 to be included \
- --confirmations 1
- }
- # originate a delegated account to enable baking
- delegate_funds() {
- echo "delegating funds..."
- # --force in case it's the second time we do it
- ./tezos-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- originate account delegated_account for $WALLET_NAME \
- transferring 1000 from $WALLET_NAME \
- --delegatable \
- --burn-cap 0.257 \
- --force \
- > delegation.log
- DELEGATED=$?
- echo "delegation return code: $DELEGATED"
- cat delegation.log
- # TODO use DELEGATED to know if we can continue
- OPHASH=`cat delegation.log | grep "Operation hash" | cut -d "'" -f 2`
- wait_for_op $OPHASH
- }
- # start baker, endorser, accuser, then delegate funds and start baking
- # if node is master, also deploys smart-contract and notices server
- start_services() {
- wait_for_node
- echo "enable CORS..."
- ./tezos-node config --cors-origin="*"
- echo "starting baker..."
- ./tezos-baker-$PROTOCOL_SLUG \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- run with local node ~/.tezos-node/ \
- &
- BAKER_PID=$!
- echo "baker's PID: $BAKER_PID"
- echo "start baking... on $NET_ADDR:$RPC_PORT for $WALLET_NAME"
- ./tezos-client \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- bake for $WALLET_NAME
- echo "starting endorser..."
- ./tezos-endorser-$PROTOCOL_SLUG \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- run \
- &
- ENDORSER_PID=$!
- echo "endorser's PID: $ENDORSER_PID"
- echo "starting accuser..."
- ./tezos-accuser-$PROTOCOL_SLUG \
- --addr $NET_ADDR \
- --port $RPC_PORT \
- run \
- &
- ACCUSER_PID=$!
- echo "accuser's PID: $ACCUSER_PID"
- delegate_funds
- if [ "$MASTER" == "true" ]; then
- echo "master node"
- originate_smart_contract
- # tell the server that node started
- curl -s \
- -H "Content-Type: application/json" \
- -X POST "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/nodeready"
- fi
- }
- #
- # init script
- #
- echo "
- ████████╗███████╗███████╗ ██████╗ ███████╗
- ╚══██╔══╝██╔════╝╚══███╔╝██╔═══██╗██╔════╝
- ██║ █████╗ ███╔╝ ██║ ██║███████╗
- ██║ ██╔══╝ ███╔╝ ██║ ██║╚════██║
- ██║ ███████╗███████╗╚██████╔╝███████║
- ╚═╝ ╚══════╝╚══════╝ ╚═════╝ ╚══════╝"
- init_env
- # checking values
- # echo "network name: $NETWORK_NAME"
- # echo "expected POW: $EXPECTED_POW"
- # echo "net address: $NET_ADDR"
- # echo "CORS origin: $CORS_ORIGIN"
- # echo "peers: $PEERS_OPTS"
- # echo "wallet name: $WALLET_NAME"
- # TODO Fitness too low Error sometimes happens
- # TODO change protocol to correspond to current state of repo
- # PROTOCOL_SLUG="003-PsddFKi3"
- # PROTOCOL_HASH="PsddFKi32cMJ2qPjf43Qv5GDWLDPZb3T3bF6fLKiF5HtvHNU7aP"
- PROTOCOL_SLUG="004-Pt24m4xi"
- PROTOCOL_HASH="Pt24m4xiPbLDhVgVfABUjirbmda3yohdN82Sp9FeuAXJ4eV9otd"
- echo "setting active protocol version..."
- echo $PROTOCOL_SLUG > active_protocol_versions
- network_name $NETWORK_NAME
- generate_identity
- if [ "$MASTER" == "true" ]; then
- start_node
- gen_keys $WALLET_NAME "WALLET_PK"
- gen_keys "activator" "ACTIVATOR_PK"
- kill_node
- protocol_activator_key "proto_genesis"
- recompile
- clear_chain
- set_protocol_parameters
- # echo "protocol parameters: $PROTOCOL_PARAMETERS"
- start_node
- echo "posting activator key..."
- curl -s \
- -H "Content-Type: application/json" \
- -X POST "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/activator-key" \
- -d "{ \"key\": \"$ACTIVATOR_PK\" }"
- activate_protocol $PROTOCOL_HASH
- kill_node
- # TODO remove from here when both can bake
- start_services &
- else
- echo "waiting for server..."
- RET=1
- while [ $RET -ne 0 ]; do
- curl -s \
- -H "Content-Type: application/json" \
- -X GET "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/"
- RET=$?
- sleep 1
- done
- echo "waiting for activator key..."
- # curl \
- # -H "Content-Type: application/json" \
- # -X GET "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/activator-key" \
- # --connect-timeout 900 \
- # --max-time 900 \
- # --no-keepalive \
- # > activator_key
- wget "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/activator-key" \
- -O activator_key
- ACTIVATOR_PK=`cat activator_key`
- echo "received activator key: $ACTIVATOR_PK"
- # TODO check if activator key is empty or valid
- protocol_activator_key "proto_genesis"
- # set_protocol_parameters
- start_node
- gen_keys $WALLET_NAME "WALLET_PK"
- kill_node
- recompile
- clear_chain
- fi
- # start_services &
- # relaunch node with PID 1 to catch container signals
- echo "starting real node..."
- # -v \
- ./tezos-node run \
- --rpc-addr $NET_ADDR:$RPC_PORT \
- --net-addr $NET_ADDR:$NET_PORT \
- --expected-pow $EXPECTED_POW \
- --cors-origin="*" \
- $PEERS_OPTS \
- $BOOTSTRAP_THRESHOLD \
- &
- PID=$!
- echo "node's PID: $PID"
- kill_service() {
- echo "killing $1 every $2 seconds"
- ps -p $1
- while kill $1; do
- echo "sent kill"
- ps -p $1
- sleep $2
- done
- }
- while true; do
- RES=`curl -s \
- -H "Content-Type: application/json" \
- -X GET "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/"`
- echo "got $RES"
- if [ "$RES" != "OK" ]; then
- echo "restarting node..."
- kill_service $PID 10
- # kill_service $BAKER_PID 2
- # kill_service $ENDORSER_PID 2
- # kill_service $ACCUSER_PID 2
- echo "posting node reset..."
- curl -s \
- -H "Content-Type: application/json" \
- -X POST "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/node-reset" \
- -d "{ }"
- clear_chain
- ./tezos-node run \
- --rpc-addr $NET_ADDR:$RPC_PORT \
- --net-addr $NET_ADDR:$NET_PORT \
- --expected-pow $EXPECTED_POW \
- --cors-origin="*" \
- $PEERS_OPTS \
- $BOOTSTRAP_THRESHOLD \
- &
- PID=$!
- echo "node's PID: $PID"
- wait_for_node
- activate_protocol $PROTOCOL_HASH
- kill_service $PID 10
- start_services &
- ./tezos-node run \
- --rpc-addr $NET_ADDR:$RPC_PORT \
- --net-addr $NET_ADDR:$NET_PORT \
- --expected-pow $EXPECTED_POW \
- --cors-origin="*" \
- $PEERS_OPTS \
- $BOOTSTRAP_THRESHOLD \
- &
- PID=$!
- echo "node's PID: $PID"
- fi
- sleep 1
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement