Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 14.23 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # script functions
  5. #
  6.  
  7. # set values for NETWORK_NAME, EXPECTED_POW, CORS_ORIGIN, PEERS, WALLET_NAME
  8. init_env() {
  9.   # verbose commands
  10.   # set -x
  11.  
  12.   export TEZOS_CLIENT_UNSAFE_DISABLE_DISCLAIMER="Y"
  13.   CORS_ORIGIN="*"
  14.   EXPECTED_POW="1"
  15.  
  16.   if [ -z RPC_PORT ] || [ -z NET_PORT ]; then
  17.     RPC_PORT="8732"
  18.     NET_PORT="9732"
  19.   fi
  20.  
  21.   # network name
  22.   if [ -z NETWORK_NAME ]; then
  23.     NETWORK_NAME="TEZOS_PRIVATENET"
  24.   fi
  25.  
  26.   # expected proof of work for generated identities
  27.   if [ -z EXPECTED_POW ]; then
  28.     EXPECTED_POW=26
  29.   fi
  30.  
  31.   # enable origin for CORS
  32.   if [ -z CORS_ORIGIN ]; then
  33.     CORS_ORIGIN=""
  34.   else
  35.     CORS_ORIGIN="\
  36.    --cors-header='content-type' \
  37.    --cors-origin='$CORS_ORIGIN' \
  38.    "
  39.   fi
  40.  
  41.   # set manual peers
  42.   if [ -z PEERS ]; then
  43.     PEERS=""
  44.   else
  45.     PEERS_OPTS="\
  46.    --no-bootstrap-peers \
  47.    --connections $((${#PEERS[@]} + 1)) \
  48.    "
  49.     for peer in ${PEERS[@]}; do
  50.       PEERS_OPTS+="--peer $peer "
  51.     done
  52.   fi
  53.  
  54.   # credited wallet
  55.   if [ -z WALLET_NAME ]; then
  56.     WALLET_NAME="test"
  57.   fi
  58. }
  59.  
  60. # generate node id with $EXPECTED_POW
  61. generate_identity() {
  62.   ./tezos-node identity &> /dev/null
  63.   if [ $? -ne 0 ]; then
  64.     echo "generating node id..."
  65.     ./tezos-node identity generate $EXPECTED_POW
  66.   fi
  67. }
  68.  
  69. # insert $ACTIVATOR_PK key in protocol
  70. # $1: protocol folder name
  71. protocol_activator_key() {
  72.   echo "inserting activator public key in protocol..."
  73.   sed -i "/Signature.Public_key.of_b58check_exn/{n;s/\".*\"/\"$ACTIVATOR_PK\"/;}" \
  74.     src/$1/lib_protocol/data.ml
  75.   # cat src/$1/lib_protocol/data.ml
  76.  
  77.   GENESIS_HASH=`./tezos-protocol-compiler -hash-only src/$1/lib_protocol/`
  78.   echo "genesis hash: $GENESIS_HASH"
  79.  
  80.   echo "inserting genesis protocol hash in lib_protocol..."
  81.   sed -i "s/\"hash\": \".*\"/\"hash\": \"$GENESIS_HASH\"/" \
  82.     src/$1/lib_protocol/TEZOS_PROTOCOL
  83.   # cat src/$1/lib_protocol/TEZOS_PROTOCOL
  84.  
  85.   echo "inserting genesis protocol hash in lib_client protocol..."
  86.   sed -i "/Protocol_hash.of_b58check_exn/{n;s/\".*\"/\"$GENESIS_HASH\"/;}" \
  87.     src/$1/lib_client/client_proto_main.ml
  88.   # cat src/$1/lib_client/client_proto_main.ml
  89.  
  90.   echo "inserting genesis protocol hash in genesis block..."
  91.   sed -i "/Protocol_hash.of_b58check_exn/{n;s/\".*\"/\"$GENESIS_HASH\"/;}" \
  92.     src/bin_node/genesis_chain.ml
  93.   # cat src/bin_node/genesis_chain.ml
  94. }
  95.  
  96. # injects a protocol for other nodes to access
  97. # $1: protocol folder name
  98. inject_protocol() {
  99.   echo "injecting $1..."
  100.   ./tezos-admin-client \
  101.     --addr $NET_ADDR \
  102.     --port $RPC_PORT \
  103.     inject protocol src/$1/lib_protocol/
  104. }
  105.  
  106. # deletes the blockchain and context. Leaves wallets untouched.
  107. clear_chain() {
  108.   echo "clearing chain info..."
  109.   rm -rf /home/tezos/.tezos-node/{store,context}
  110. }
  111.  
  112. # change the shell's network name
  113. # $1: network name
  114. network_name() {
  115.   echo "changing network name in shell to $1..."
  116.   sed -i "s/name = \".*\" \;/name = \"$1\" \;/" \
  117.     src/lib_shell/distributed_db_message.ml
  118. }
  119.  
  120. # set protocol parameters to be bootstrapped
  121. # expects $WALLET_PK and $ACTIVATOR_PK
  122. set_protocol_parameters() {
  123.   PROTOCOL_PARAMETERS=`jq -n \
  124.     --argjson tbb "[\"3\", \"0\"]" \
  125.     --argjson tbbs 0 \
  126.     --argjson bprs 4 \
  127.     --argjson bpc 8 \
  128.     --argjson bpvp 64 \
  129.     --argjson pc 2 \
  130.     --argjson powt "\"-1\"" \
  131.     --argjson ba "[ \
  132.      [\"$WALLET_PK\", \"4000000000000\"], \
  133.      [\"$ACTIVATOR_PK\", \"1\"] \
  134.    ]" \
  135.     '{ \
  136.      time_between_blocks: $tbb, \
  137.      blocks_per_roll_snapshot: $bprs, \
  138.      blocks_per_cycle: $bpc, \
  139.      blocks_per_voting_period: $bpvp, \
  140.      preserved_cycles: $pc, \
  141.      proof_of_work_threshold: $powt, \
  142.      bootstrap_accounts: $ba \
  143.    }'
  144.   `
  145.   echo "rewriting protocol parameters..."
  146.   echo $PROTOCOL_PARAMETERS > scripts/protocol_parameters.json
  147. }
  148.  
  149. # start a node in background and wait for it to be ready
  150. # expects $NET_ADDR, $EXPECTED_POW, $PEER_OPTS
  151. start_node() {
  152.   echo "starting node..."
  153.   echo "ports: $RPC_PORT/$NET_PORT"
  154.   # --net-addr $NET_ADDR:$NET_PORT \
  155.   # $PEERS_OPTS \
  156.   ./tezos-node run \
  157.     --rpc-addr $NET_ADDR:$RPC_PORT \
  158.     --expected-pow $EXPECTED_POW \
  159.     --no-bootstrap-peers \
  160.     --connections 1 \
  161.     &
  162.   PID=$!
  163.   echo "started node with pid $PID"
  164.  
  165.   wait_for_node
  166. }
  167.  
  168. # waits for node to be ready
  169. wait_for_node() {
  170.   echo "waiting for node to be ready..."
  171.   RET=1
  172.   while [ $RET -ne 0 ]; do
  173.     ./tezos-client \
  174.       --addr $NET_ADDR \
  175.       --port $RPC_PORT \
  176.       rpc get /chains/main/blocks/head/metadata \
  177.       &> /dev/null
  178.     RET=$?
  179.   done
  180.   echo "node started."
  181. }
  182.  
  183. # generate a wallet and store edpk in variable
  184. # $1: wallet name
  185. # $2: variable name to store edpk
  186. gen_keys() {
  187.  
  188.   ./tezos-client \
  189.     --addr $NET_ADDR \
  190.     --port $RPC_PORT \
  191.     list known addresses \
  192.     | grep $1 \
  193.     > /dev/null
  194.  
  195.   if [ $? -ne 0 ]; then
  196.     echo "generating wallet $1..."
  197.     ./tezos-client \
  198.       --addr $NET_ADDR \
  199.       --port $RPC_PORT \
  200.       gen keys $1
  201.   fi
  202.  
  203.   TMP_WALLET=`\
  204.     ./tezos-client \
  205.       --addr $NET_ADDR \
  206.       --port $RPC_PORT \
  207.       show address $1 \
  208.       --show-secret \
  209.   `
  210.  
  211.   # show wallet on console
  212.   echo "=================================================="
  213.   echo "$TMP_WALLET"
  214.   echo "=================================================="
  215.  
  216.   # get wallet public key in $2
  217.   eval $2=`echo "$TMP_WALLET" | grep Public | cut -d ' ' -f 3`
  218.   # get wallet secret key
  219.   KEY=`echo "$TMP_WALLET" | grep Secret | cut -d ' ' -f 3`
  220.  
  221.   if [ "$1" == "$WALLET_NAME" ] && [ "$MASTER" == "true" ]; then
  222.     echo "posting wallet key..."
  223.     curl -s \
  224.       -H "Content-Type: application/json" \
  225.       -X POST "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/privkey" \
  226.       -d "{ \"key\": \"$KEY\" }"
  227.   fi
  228. }
  229.  
  230. # spin a node to activate the protocol and bootstrap chain after genesis node
  231. # $1: protocol hash
  232. activate_protocol() {
  233.   echo "activating custom protocol..."
  234.   ./tezos-client \
  235.     --addr $NET_ADDR \
  236.     --port $RPC_PORT \
  237.     -block genesis \
  238.     activate protocol $1 \
  239.     with fitness 1 \
  240.     and key activator \
  241.     and parameters ./scripts/protocol_parameters.json
  242.   wait_protocol $1
  243. }
  244.  
  245. # wait for protocol to be running
  246. # $1: protocol hash
  247. wait_protocol() {
  248.   PROTO=""
  249.   while [[ $PROTO != $1 ]]; do
  250.     PROTO=`\
  251.       ./tezos-client \
  252.         --addr $NET_ADDR \
  253.         --port $RPC_PORT \
  254.         rpc get /chains/main/blocks/head/metadata \
  255.         | jq .next_protocol \
  256.         | cut -d '"' -f 2 \
  257.     `
  258.   done
  259. }
  260.  
  261. # kill the node which has pid $PID
  262. kill_node() {
  263.   echo "kill node pid $PID"
  264.   kill $PID
  265.   wait $PID
  266. }
  267.  
  268. # recompile tezos
  269. recompile() {
  270.   echo "setting opam env..."
  271.   eval $(opam env)
  272.   echo "recompiling protocol & shell..."
  273.   make
  274. }
  275.  
  276. # compiles the originated smart contract
  277. originate_smart_contract() {
  278.   echo "compiling smart contract..."
  279.  
  280.   /tezos/liquidity/liquidity /tezos/node/smart-contract.reliq
  281.   STORAGE=`cat /tezos/node/smart-contract.init.tz`
  282.   /tezos/liquidity/liquidity \
  283.     --convert /tezos/node/smart-contract.reliq \
  284.     > /tezos/node/smart-contract.liq
  285.  
  286.   # --force in case it's the second time we do it
  287.   ./tezos-client \
  288.     --addr $NET_ADDR \
  289.     --port $RPC_PORT \
  290.     originate contract tzcontract \
  291.     for $WALLET_NAME \
  292.     transferring 1.0 from $WALLET_NAME \
  293.     running /tezos/node/smart-contract.tz \
  294.     --init "$STORAGE" \
  295.     --burn-cap 100.0 \
  296.     --force \
  297.     > origination.log
  298.  
  299.   OPHASH=`cat origination.log | grep "Operation hash" | cut -d "'" -f 2`
  300.   wait_for_op $OPHASH
  301. }
  302.  
  303. # waits for an operation to be included
  304. # $1: op hash
  305. wait_for_op() {
  306.   echo "waiting for op $1..."
  307.   ./tezos-client \
  308.     --addr $NET_ADDR \
  309.     --port $RPC_PORT \
  310.     wait for $1 to be included \
  311.     --confirmations 1
  312. }
  313.  
  314. # originate a delegated account to enable baking
  315. delegate_funds() {
  316.   echo "delegating funds..."
  317.   # --force in case it's the second time we do it
  318.   ./tezos-client \
  319.     --addr $NET_ADDR \
  320.     --port $RPC_PORT \
  321.     originate account delegated_account for $WALLET_NAME \
  322.     transferring 1000 from $WALLET_NAME \
  323.     --delegatable \
  324.     --burn-cap 0.257 \
  325.     --force \
  326.     > delegation.log
  327.   DELEGATED=$?
  328.   echo "delegation return code: $DELEGATED"
  329.   cat delegation.log
  330.   # TODO use DELEGATED to know if we can continue
  331.   OPHASH=`cat delegation.log | grep "Operation hash" | cut -d "'" -f 2`
  332.   wait_for_op $OPHASH
  333. }
  334.  
  335. # start baker, endorser, accuser, then delegate funds and start baking
  336. # if node is master, also deploys smart-contract and notices server
  337. start_services() {
  338.  
  339.   wait_for_node
  340.  
  341.   echo "enable CORS..."
  342.   ./tezos-node config --cors-origin="*"
  343.  
  344.   echo "starting baker..."
  345.   ./tezos-baker-$PROTOCOL_SLUG \
  346.     --addr $NET_ADDR \
  347.     --port $RPC_PORT \
  348.     run with local node ~/.tezos-node/ \
  349.     &
  350.   BAKER_PID=$!
  351.   echo "baker's PID: $BAKER_PID"
  352.  
  353.   echo "start baking... on $NET_ADDR:$RPC_PORT for $WALLET_NAME"
  354.   ./tezos-client \
  355.     --addr $NET_ADDR \
  356.     --port $RPC_PORT \
  357.     bake for $WALLET_NAME
  358.  
  359.   echo "starting endorser..."
  360.   ./tezos-endorser-$PROTOCOL_SLUG \
  361.     --addr $NET_ADDR \
  362.     --port $RPC_PORT \
  363.     run \
  364.     &
  365.   ENDORSER_PID=$!
  366.   echo "endorser's PID: $ENDORSER_PID"
  367.  
  368.   echo "starting accuser..."
  369.   ./tezos-accuser-$PROTOCOL_SLUG \
  370.     --addr $NET_ADDR \
  371.     --port $RPC_PORT \
  372.     run \
  373.     &
  374.   ACCUSER_PID=$!
  375.   echo "accuser's PID: $ACCUSER_PID"
  376.  
  377.   delegate_funds
  378.  
  379.   if [ "$MASTER" == "true" ]; then
  380.     echo "master node"
  381.     originate_smart_contract
  382.     # tell the server that node started
  383.     curl -s \
  384.       -H "Content-Type: application/json" \
  385.       -X POST "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/nodeready"
  386.   fi
  387. }
  388.  
  389. #
  390. # init script
  391. #
  392.  
  393. echo "
  394. ████████╗███████╗███████╗ ██████╗ ███████╗
  395. ╚══██╔══╝██╔════╝╚══███╔╝██╔═══██╗██╔════╝
  396.   ██║   █████╗    ███╔╝ ██║   ██║███████╗
  397.   ██║   ██╔══╝   ███╔╝  ██║   ██║╚════██║
  398.   ██║   ███████╗███████╗╚██████╔╝███████║
  399.   ╚═╝   ╚══════╝╚══════╝ ╚═════╝ ╚══════╝"
  400.  
  401. init_env
  402.  
  403. # checking values
  404. # echo "network name: $NETWORK_NAME"
  405. # echo "expected POW: $EXPECTED_POW"
  406. # echo "net address: $NET_ADDR"
  407. # echo "CORS origin: $CORS_ORIGIN"
  408. # echo "peers: $PEERS_OPTS"
  409. # echo "wallet name: $WALLET_NAME"
  410.  
  411. # TODO Fitness too low Error sometimes happens
  412. # TODO change protocol to correspond to current state of repo
  413. # PROTOCOL_SLUG="003-PsddFKi3"
  414. # PROTOCOL_HASH="PsddFKi32cMJ2qPjf43Qv5GDWLDPZb3T3bF6fLKiF5HtvHNU7aP"
  415. PROTOCOL_SLUG="004-Pt24m4xi"
  416. PROTOCOL_HASH="Pt24m4xiPbLDhVgVfABUjirbmda3yohdN82Sp9FeuAXJ4eV9otd"
  417.  
  418. echo "setting active protocol version..."
  419. echo $PROTOCOL_SLUG > active_protocol_versions
  420.  
  421. network_name $NETWORK_NAME
  422.  
  423. generate_identity
  424.  
  425. if [ "$MASTER" == "true" ]; then
  426.   start_node
  427.   gen_keys $WALLET_NAME "WALLET_PK"
  428.   gen_keys "activator" "ACTIVATOR_PK"
  429.   kill_node
  430.  
  431.   protocol_activator_key "proto_genesis"
  432.   recompile
  433.   clear_chain
  434.   set_protocol_parameters
  435.   # echo "protocol parameters: $PROTOCOL_PARAMETERS"
  436.   start_node
  437.  
  438.   echo "posting activator key..."
  439.   curl -s \
  440.     -H "Content-Type: application/json" \
  441.     -X POST "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/activator-key" \
  442.     -d "{ \"key\": \"$ACTIVATOR_PK\" }"
  443.  
  444.   activate_protocol $PROTOCOL_HASH
  445.   kill_node
  446.   # TODO remove from here when both can bake
  447.   start_services &
  448. else
  449.  
  450.   echo "waiting for server..."
  451.   RET=1
  452.   while [ $RET -ne 0 ]; do
  453.     curl -s \
  454.       -H "Content-Type: application/json" \
  455.       -X GET "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/"
  456.     RET=$?
  457.     sleep 1
  458.   done
  459.  
  460.   echo "waiting for activator key..."
  461.   # curl \
  462.   #   -H "Content-Type: application/json" \
  463.   #   -X GET "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/activator-key" \
  464.   #   --connect-timeout 900 \
  465.   #   --max-time 900 \
  466.   #   --no-keepalive \
  467.   #   > activator_key
  468.  
  469.   wget "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/activator-key" \
  470.     -O activator_key
  471.  
  472.   ACTIVATOR_PK=`cat activator_key`
  473.   echo "received activator key: $ACTIVATOR_PK"
  474.   # TODO check if activator key is empty or valid
  475.   protocol_activator_key "proto_genesis"
  476.   # set_protocol_parameters
  477.   start_node
  478.  
  479.   gen_keys $WALLET_NAME "WALLET_PK"
  480.   kill_node
  481.  
  482.   recompile
  483.   clear_chain
  484. fi
  485.  
  486. # start_services &
  487.  
  488. # relaunch node with PID 1 to catch container signals
  489. echo "starting real node..."
  490.   # -v \
  491. ./tezos-node run \
  492.   --rpc-addr $NET_ADDR:$RPC_PORT \
  493.   --net-addr $NET_ADDR:$NET_PORT \
  494.   --expected-pow $EXPECTED_POW \
  495.   --cors-origin="*" \
  496.   $PEERS_OPTS \
  497.   $BOOTSTRAP_THRESHOLD \
  498.   &
  499. PID=$!
  500. echo "node's PID: $PID"
  501.  
  502. kill_service() {
  503.   echo "killing $1 every $2 seconds"
  504.   ps -p $1
  505.   while kill $1; do
  506.     echo "sent kill"
  507.     ps -p $1
  508.     sleep $2
  509.   done
  510. }
  511.  
  512. while true; do
  513.   RES=`curl -s \
  514.     -H "Content-Type: application/json" \
  515.     -X GET "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/"`
  516.   echo "got $RES"
  517.   if [ "$RES" != "OK" ]; then
  518.  
  519.     echo "restarting node..."
  520.     kill_service $PID 10
  521.     # kill_service $BAKER_PID 2
  522.     # kill_service $ENDORSER_PID 2
  523.     # kill_service $ACCUSER_PID 2
  524.  
  525.     echo "posting node reset..."
  526.     curl -s \
  527.       -H "Content-Type: application/json" \
  528.       -X POST "$SERVER_PROTOCOL://$SERVER_URL:$SERVER_PORT/node-reset" \
  529.       -d "{ }"
  530.  
  531.     clear_chain
  532.  
  533.     ./tezos-node run \
  534.       --rpc-addr $NET_ADDR:$RPC_PORT \
  535.       --net-addr $NET_ADDR:$NET_PORT \
  536.       --expected-pow $EXPECTED_POW \
  537.       --cors-origin="*" \
  538.       $PEERS_OPTS \
  539.       $BOOTSTRAP_THRESHOLD \
  540.       &
  541.     PID=$!
  542.     echo "node's PID: $PID"
  543.     wait_for_node
  544.  
  545.     activate_protocol $PROTOCOL_HASH
  546.     kill_service $PID 10
  547.  
  548.     start_services &
  549.  
  550.     ./tezos-node run \
  551.       --rpc-addr $NET_ADDR:$RPC_PORT \
  552.       --net-addr $NET_ADDR:$NET_PORT \
  553.       --expected-pow $EXPECTED_POW \
  554.       --cors-origin="*" \
  555.       $PEERS_OPTS \
  556.       $BOOTSTRAP_THRESHOLD \
  557.       &
  558.     PID=$!
  559.     echo "node's PID: $PID"
  560.   fi
  561.   sleep 1
  562. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement