forked from Qortal/qortal
c069c39ce1
When using POST /arbitrary/{service}/{name}... it will now automatically decide which method to use (PUT/PATCH) based on a few factors: - If there are already 10 or more layers, use PUT to reset back to a single layer - If the next layer's patch is more than 20% of the total resource file size, use PUT - If the next layer modifies more than 50% of the total file count, use PUT - Otherwise, use PATCH The PUT method causes a new base layer to be created and all previous update history for that resource becomes obsolete. The PATCH method adds a small delta layer on top of the existing layer(s). The idea is to wipe the slate clean with a new base layer once the patches start to get demanding for the network to apply. Nodes which view the content will ultimately have build timeouts to prevent someone from deploying a resource with hundreds of complex layers for example, so this approach is there to maximize the chances of the resource being buildable. The constants above (10 layers, 20% total size, 50% file count) will most likely need tweaking once we have some real world data.
105 lines
3.1 KiB
Bash
Executable File
105 lines
3.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# Qortal defaults
|
|
host="localhost"
|
|
port=12393
|
|
|
|
if [ -z "$*" ]; then
|
|
echo "Usage:"
|
|
echo
|
|
echo "Host/update data:"
|
|
echo "qdata [POST/PUT/PATCH] [service] [name] [dirpath] <identifier>"
|
|
echo
|
|
echo "Fetch data:"
|
|
echo "qdata GET [service] [name] <identifier-or-default> <filepath-or-default> <rebuild>"
|
|
echo
|
|
echo "Notes:"
|
|
echo "- When requesting a resource, please use 'default' to indicate a file with no identifier."
|
|
echo "- The same applies when specifying the relative path to a file within the data structure; use 'default'"
|
|
echo " to indicate a single file resource."
|
|
echo
|
|
exit
|
|
fi
|
|
|
|
method=$1
|
|
service=$2
|
|
name=$3
|
|
|
|
if [ -z "${method}" ]; then
|
|
echo "Error: missing method"; exit
|
|
fi
|
|
if [ -z "${service}" ]; then
|
|
echo "Error: missing service"; exit
|
|
fi
|
|
if [ -z "${name}" ]; then
|
|
echo "Error: missing name"; exit
|
|
fi
|
|
|
|
|
|
if [[ "${method}" == "POST" || "${method}" == "PUT" || "${method}" == "PATCH" ]]; then
|
|
directory=$4
|
|
identifier=$5
|
|
|
|
if [ -z "${directory}" ]; then
|
|
echo "Error: missing directory"; exit
|
|
fi
|
|
if [ -z "${QORTAL_PRIVKEY}" ]; then
|
|
echo "Error: missing private key. Set it by running: export QORTAL_PRIVKEY=privkeyhere"; exit
|
|
fi
|
|
|
|
echo "Creating transaction - this can take a while..."
|
|
tx_data=$(curl --silent --insecure -X ${method} "http://${host}:${port}/arbitrary/${service}/${name}/${identifier}" -d "${directory}")
|
|
if [[ "${tx_data}" == *"error"* || "${tx_data}" == *"ERROR"* ]]; then
|
|
echo "${tx_data}"; exit
|
|
fi
|
|
|
|
echo "Signing..."
|
|
signed_tx_data=$(curl --silent --insecure -X POST "http://${host}:${port}/transactions/sign" -H "Content-Type: application/json" -d "{\"privateKey\":\"${QORTAL_PRIVKEY}\",\"transactionBytes\":\"${tx_data}\"}")
|
|
if [[ "${signed_tx_data}" == *"error"* || "${signed_tx_data}" == *"ERROR"* ]]; then
|
|
echo "${signed_tx_data}"; exit
|
|
fi
|
|
|
|
echo "Broadcasting..."
|
|
success=$(curl --silent --insecure -X POST "http://${host}:${port}/transactions/process" -H "Content-Type: text/plain" -d "${signed_tx_data}")
|
|
if [[ "${success}" == "true" ]]; then
|
|
echo "Transaction broadcast successfully"
|
|
else
|
|
echo "Error when broadcasting transaction. Please try again."
|
|
echo "Response: ${success}"
|
|
fi
|
|
|
|
elif [[ "${method}" == "GET" ]]; then
|
|
identifier=$4
|
|
filepath=$5
|
|
rebuild=$6
|
|
|
|
if [ -z "${rebuild}" ]; then
|
|
rebuild="false"
|
|
fi
|
|
|
|
# Handle default
|
|
if [[ "${identifier}" == "default" ]]; then
|
|
identifier=""
|
|
fi
|
|
if [[ "${filepath}" == "default" ]]; then
|
|
filepath=""
|
|
fi
|
|
|
|
# We use a different API depending on whether or not an identifier is supplied
|
|
if [ -n "${identifier}" ]; then
|
|
response=$(curl --silent --insecure -X GET "http://${host}:${port}/arbitrary/${service}/${name}/${identifier}?rebuild=${rebuild}&filepath=${filepath}")
|
|
else
|
|
response=$(curl --silent --insecure -X GET "http://${host}:${port}/arbitrary/${service}/${name}?rebuild=${rebuild}&filepath=${filepath}")
|
|
fi
|
|
|
|
if [ -z "${response}" ]; then
|
|
echo "Empty response from ${host}:${port}"
|
|
fi
|
|
if [[ "${response}" == *"error"* || "${response}" == *"ERROR"* ]]; then
|
|
echo "${response}"; exit
|
|
fi
|
|
|
|
echo "${response}"
|
|
|
|
fi
|