From b725905b397963c6662f9d0cd34bc15b0774ed04 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 10 Jun 2022 11:32:20 +0300 Subject: [PATCH] [#185] bin: Print human-readable errors Signed-off-by: Evgenii Stratonikov --- bin/addCert.sh | 2 +- bin/config.sh | 23 +++++++++++++---------- bin/deposit.sh | 23 +++++++++++++++-------- bin/helper.sh | 6 ++++++ bin/resolve.sh | 15 ++++++++++++--- bin/tick.sh | 31 ++++++++++++++++++------------- 6 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 bin/helper.sh diff --git a/bin/addCert.sh b/bin/addCert.sh index d4bb142..49ff3d0 100755 --- a/bin/addCert.sh +++ b/bin/addCert.sh @@ -3,4 +3,4 @@ # Source env settings . .env -ln -sf $(pwd)/services/storage/s04tls.crt ${CA_CERTS_TRUSTED_STORE}/s04.${LOCAL_DOMAIN}.tls.crt +ln -sf "$(pwd)/services/storage/s04tls.crt" "${CA_CERTS_TRUSTED_STORE}/s04.${LOCAL_DOMAIN}.tls.crt" diff --git a/bin/config.sh b/bin/config.sh index d4fd1fa..0f88192 100755 --- a/bin/config.sh +++ b/bin/config.sh @@ -3,6 +3,7 @@ # Source env settings . .env . services/ir/.ir.env +source bin/helper.sh # NeoGo binary path. NEOGO="${NEOGO:-docker exec -it main_chain neo-go}" @@ -12,7 +13,7 @@ WALLET="${WALLET:-services/chain/node-wallet.json}" WALLET_IMG="${WALLET_IMG:-wallets/node-wallet.json}" # Wallet password that would be entered automatically; '-' means no password PASSWD="one" -NETMAP_ADDR=`bin/resolve.sh netmap.neofs` +NETMAP_ADDR=$(bin/resolve.sh netmap.neofs) # NeoFS configuration record: variable type [string|int|etc], # key is a string and value is a constant of given type @@ -26,21 +27,23 @@ VALUE="${3}" # Internal variables if [[ -z "${NEOFS_NOTARY_DISABLED}" ]]; then - ADDR=`cat ${WALLET} | jq -r .accounts[2].address` + ADDR=$(jq -r .accounts[2].address < "${WALLET}" || die "Cannot get address from ${WALLET}") else - ADDR=`cat ${WALLET} | jq -r .accounts[0].address` + ADDR=$(jq -r .accounts[0].address < "${WALLET}" || die "Cannot get address from ${WALLET}") fi # Change config value in side chain echo "Changing ${KEY} configration value to ${VALUE}" + +# shellcheck disable=SC2086 ./bin/passwd.exp ${PASSWD} ${NEOGO} contract invokefunction \ --w ${WALLET_IMG} \ --a ${ADDR} \ --r http://morph-chain.${LOCAL_DOMAIN}:30333 \ -${NETMAP_ADDR} \ -setConfig bytes:beefcafe \ -string:${KEY} \ -${TYPE}:${VALUE} -- ${ADDR} || exit 1 + -w ${WALLET_IMG} \ + -a ${ADDR} \ + -r http://morph-chain.${LOCAL_DOMAIN}:30333 \ + ${NETMAP_ADDR} \ + setConfig bytes:beefcafe \ + string:${KEY} \ + ${TYPE}:${VALUE} -- ${ADDR} || exit 1 # Update epoch to apply new configuration value ./bin/tick.sh diff --git a/bin/deposit.sh b/bin/deposit.sh index 6032354..95fbc3e 100755 --- a/bin/deposit.sh +++ b/bin/deposit.sh @@ -3,6 +3,7 @@ # Source env settings . .env . services/ir/.ir.env +source bin/helper.sh # NeoGo binary path. NEOGO="${NEOGO:-docker exec -it main_chain neo-go}" @@ -14,14 +15,20 @@ PASSWD="-" DEPOSIT="${1:-50}" # Internal variables -ADDR=`cat ${WALLET} | jq -r .accounts[0].address` -CONTRACT_ADDR=`${NEOGO} util convert ${NEOFS_IR_CONTRACTS_NEOFS} | grep 'LE ScriptHash to Address' | awk '{print $5}' | grep -oP [A-z0-9]+` +ADDR=$(jq -r .accounts[0].address < "${WALLET}" \ + || die "Cannot get address from wallet: ${WALLET}") +CONTRACT_ADDR=$(${NEOGO} util convert "${NEOFS_IR_CONTRACTS_NEOFS}" \ + | grep 'LE ScriptHash to Address' \ + | awk '{print $5}' \ + | grep -oP "[A-z0-9]+" \ + || die "Cannot parse contract address: ${NEOFS_IR_CONTRACTS_NEOFS}") # Make deposit +# shellcheck disable=SC2086 ./bin/passwd.exp ${PASSWD} ${NEOGO} wallet nep17 transfer \ --w ${WALLET} \ --r http://main-chain.${LOCAL_DOMAIN}:30333 \ ---from ${ADDR} \ ---to ${CONTRACT_ADDR} \ ---token GAS \ ---amount ${DEPOSIT} + -w ${WALLET} \ + -r http://main-chain.${LOCAL_DOMAIN}:30333 \ + --from ${ADDR} \ + --to ${CONTRACT_ADDR} \ + --token GAS \ + --amount ${DEPOSIT} diff --git a/bin/helper.sh b/bin/helper.sh new file mode 100644 index 0000000..0641312 --- /dev/null +++ b/bin/helper.sh @@ -0,0 +1,6 @@ +set -eo pipefail + +die() { + echo "$(caller 0):" "$*" >&2 + exit 1 +} diff --git a/bin/resolve.sh b/bin/resolve.sh index 04b8e52..009aa54 100755 --- a/bin/resolve.sh +++ b/bin/resolve.sh @@ -2,12 +2,21 @@ # Source env settings . .env +source bin/helper.sh # NeoGo binary path. NEOGO="${NEOGO:-docker exec -it morph_chain neo-go}" # NNS contract script hash -NNS_ADDR=`curl -s --data '{ "id": 1, "jsonrpc": "2.0", "method": "getcontractstate", "params": [1] }' http://morph-chain.${LOCAL_DOMAIN}:30333/ | jq -r '.result.hash'` +output=$(curl -s --data '{ "id": 1, "jsonrpc": "2.0", "method": "getcontractstate", "params": [1] }' \ + "http://morph-chain.${LOCAL_DOMAIN}:30333/") \ + || die "Cannot fetch NNS contract state" + +NNS_ADDR=$(jq -r '.result.hash' <<< "$output") \ + || die "Cannot parse NNS contract hash: $NNS_ADDR" ${NEOGO} contract testinvokefunction \ --r http://morph-chain.${LOCAL_DOMAIN}:30333 \ -${NNS_ADDR} resolve string:${1} int:16 | jq -r '.stack[0].value | if type=="array" then .[0].value else . end' | base64 -d + -r "http://morph-chain.${LOCAL_DOMAIN}:30333" \ + "${NNS_ADDR}" resolve string:"${1}" int:16 \ + | jq -r '.stack[0].value | if type=="array" then .[0].value else . end' \ + | base64 -d \ + || die "Cannot invoke 'NNS.resolve' $output" diff --git a/bin/tick.sh b/bin/tick.sh index 5fb7b4a..1ef6352 100755 --- a/bin/tick.sh +++ b/bin/tick.sh @@ -3,6 +3,7 @@ # Source env settings . .env . services/ir/.ir.env +source bin/helper.sh # NeoGo binary path. NEOGO="${NEOGO:-docker exec -it main_chain neo-go}" @@ -16,29 +17,33 @@ PASSWD="one" # Internal variables if [[ -z "${NEOFS_NOTARY_DISABLED}" ]]; then - ADDR=`cat ${WALLET} | jq -r .accounts[2].address` + ADDR=$(jq -r .accounts[2].address < "${WALLET}" || die "Cannot get address from ${WALLET}") else - ADDR=`cat ${WALLET} | jq -r .accounts[0].address` + ADDR=$(jq -r .accounts[0].address < "${WALLET}" || die "Cannot get address from ${WALLET}") fi # Grep Morph block time SIDECHAIN_PROTO="${SIDECHAIN_PROTO:-services/morph_chain/protocol.privnet.yml}" -BLOCK_DURATION=`grep SecondsPerBlock < $SIDECHAIN_PROTO | awk '{print $2}'` -NETMAP_ADDR=`bin/resolve.sh netmap.neofs` +BLOCK_DURATION=$(grep SecondsPerBlock < "$SIDECHAIN_PROTO" | awk '{print $2}') \ + || die "Cannot fetch block duration" +NETMAP_ADDR=$(bin/resolve.sh netmap.neofs) || die "Cannot resolve netmap.neofs" # Fetch current epoch value -EPOCH=`${NEOGO} contract testinvokefunction -r \ -http://morph-chain.${LOCAL_DOMAIN}:30333 \ -${NETMAP_ADDR} \ -epoch | grep 'value' | awk -F'"' '{ print $4 }'` +EPOCH=$(${NEOGO} contract testinvokefunction \ + -r "http://morph-chain.${LOCAL_DOMAIN}:30333" "${NETMAP_ADDR}" epoch \ + | grep 'value' | awk -F'"' '{ print $4 }') \ + || die "Cannot fetch epoch from netmap contract" echo "Updating NeoFS epoch to $((EPOCH+1))" + +# shellcheck disable=SC2086 ./bin/passwd.exp ${PASSWD} ${NEOGO} contract invokefunction \ --w ${WALLET_IMG} \ --a ${ADDR} \ --r http://morph-chain.${LOCAL_DOMAIN}:30333 \ -${NETMAP_ADDR} \ -newEpoch int:$((EPOCH+1)) -- ${ADDR}:Global + -w ${WALLET_IMG} \ + -a ${ADDR} \ + -r http://morph-chain.${LOCAL_DOMAIN}:30333 \ + ${NETMAP_ADDR} \ + newEpoch int:$((EPOCH+1)) -- ${ADDR}:Global # Wait one Morph block to ensure the transaction broadcasted +# shellcheck disable=SC2086 sleep $BLOCK_DURATION