From 9db05c71407b7c8d805e4f74be930ba0a67e7933 Mon Sep 17 00:00:00 2001 From: conache Date: Thu, 8 Aug 2024 14:12:31 +0300 Subject: [PATCH] Add init scripts --- .gitignore | 13 ++++---- ...fchain-node.example => config.example.json | 7 ++-- docker-compose.yml | 8 ++--- init.offchain-node | 29 ++++++++++++++++ offchain-node-data/env_file | 4 +++ offchain-node-data/scripts/init.sh | 33 +++++++++++++++++++ 6 files changed, 79 insertions(+), 15 deletions(-) rename .env.offchain-node.example => config.example.json (76%) create mode 100755 init.offchain-node create mode 100644 offchain-node-data/env_file create mode 100644 offchain-node-data/scripts/init.sh diff --git a/.gitignore b/.gitignore index 4a971f2..c1d351e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,11 +2,10 @@ __pycache__ *.pyc logs/* -.env -inference-data -worker-data -head-data -offchain-node-data -.env.* -!.env.*.example +.allorad +.cache +inference-data + +config.json +env diff --git a/.env.offchain-node.example b/config.example.json similarity index 76% rename from .env.offchain-node.example rename to config.example.json index 1f0a878..e22ab8b 100644 --- a/.env.offchain-node.example +++ b/config.example.json @@ -1,8 +1,7 @@ -ALLORA_OFFCHAIN_NODE_CONFIG_JSON='{ +{ "wallet": { "addressKeyName": "test", "addressRestoreMnemonic": "", - "addressAccountPassphrase": "", "alloraHomeDir": "", "gas": "1000000", "gasAdjustment": 1.0, @@ -17,9 +16,9 @@ ALLORA_OFFCHAIN_NODE_CONFIG_JSON='{ "inferenceEntrypointName": "api-worker-reputer", "loopSeconds": 5, "parameters": { - "InferenceEndpoint": "http://inference:8000/inference/{Token}", + "InferenceEndpoint": "http://source:8000/inference/{Token}", "Token": "ETH" } } ] -}' \ No newline at end of file +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 2e6f837..b85357a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,7 @@ services: dockerfile: Dockerfile_inference command: python -u /app/app.py ports: - - "8000:8000" + - "8002:8000" healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8000/inference/ETH"] interval: 10s @@ -34,7 +34,7 @@ services: condition: service_healthy node: - container_name: offchain_node_test + container_name: offchain_node image: allora-offchain-node:latest volumes: - ./offchain-node-data:/data @@ -42,7 +42,7 @@ services: inference: condition: service_healthy env_file: - - ./env.offchain-node + - ./offchain-node-data/env_file networks: eth-model-local: @@ -53,4 +53,4 @@ networks: volumes: inference-data: - worker-data: + offchain-node-data: diff --git a/init.offchain-node b/init.offchain-node new file mode 100755 index 0000000..023c379 --- /dev/null +++ b/init.offchain-node @@ -0,0 +1,29 @@ +#!/bin/bash + +set -e + +if [ ! -f config.json ]; then + echo "Error: config.json file not found, please provide one" + exit 1 +fi + +nodeName=$(jq -r '.wallet.addressKeyName' config.json) +if [ -z "$nodeName" ]; then + echo "No name was provided for the node, please provide value for wallet.addressKeyName in the config.json" + exit 1 +fi + +if [ ! -f ./offchain-node-data/env_file ]; then + echo "ENV_LOADED=false" > ./offchain-node-data/env_file +fi + +ENV_LOADED=$(grep '^ENV_LOADED=' ./offchain-node-data/env_file | cut -d '=' -f 2) +if [ "$ENV_LOADED" = "false" ]; then + json_content=$(cat ./config.json) + stringified_json=$(echo "$json_content" | jq -c .) + + docker run -it --entrypoint=bash -v $(pwd)/offchain-node-data:/data -e NAME="${nodeName}" -e ALLORA_OFFCHAIN_NODE_CONFIG_JSON="${stringified_json}" alloranetwork/allora-chain:latest -c "bash /data/scripts/init.sh" + echo "config.json saved to ./offchain-node-data/env_file" +else + echo "config.json is already loaded, skipping the operation. You can set ENV_LOADED variable to false in ./offchain-node-data/env_file to reload the config.json" +fi \ No newline at end of file diff --git a/offchain-node-data/env_file b/offchain-node-data/env_file new file mode 100644 index 0000000..5285859 --- /dev/null +++ b/offchain-node-data/env_file @@ -0,0 +1,4 @@ +ALLORA_OFFCHAIN_NODE_CONFIG_JSON='{"wallet":{"addressKeyName":"basic-coin-prediction-offchain-node","addressRestoreMnemonic":"rich note fetch lava bless snake delay theme era anger ritual sea pluck neck hazard dish talk ranch trophy clap fancy human divide gun","addressAccountPassphrase":"secret","alloraHomeDir":"","gas":"1000000","gasAdjustment":1,"nodeRpc":"https://allora-rpc.devnet.behindthecurtain.xyz","maxRetries":1,"minDelay":1,"maxDelay":2,"submitTx":false},"worker":[{"topicId":1,"inferenceEntrypointName":"api-worker-reputer","loopSeconds":5,"parameters":{"InferenceEndpoint":"http://inference:8000/inference/{Token}","Token":"ETH"}}]}' +ALLORA_OFFCHAIN_ACCOUNT_ADDRESS=allo14wkkdeg93mdc0sd770z9p4mpjz7w9mysz328um +NAME=basic-coin-prediction-offchain-node +ENV_LOADED=true diff --git a/offchain-node-data/scripts/init.sh b/offchain-node-data/scripts/init.sh new file mode 100644 index 0000000..0e6af84 --- /dev/null +++ b/offchain-node-data/scripts/init.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +set -e + +if allorad keys --home=/data/.allorad --keyring-backend test show $NAME > /dev/null 2>&1 ; then + echo "allora account: $NAME already imported" +else + echo "creating allora account: $NAME" + output=$(allorad keys add $NAME --home=/data/.allorad --keyring-backend test 2>&1) + address=$(echo "$output" | grep 'address:' | sed 's/.*address: //') + mnemonic=$(echo "$output" | tail -n 1) + + # Parse and update the JSON string + updated_json=$(echo "$ALLORA_OFFCHAIN_NODE_CONFIG_JSON" | jq --arg name "$NAME" --arg mnemonic "$mnemonic" ' + .wallet.addressKeyName = $name | + .wallet.addressRestoreMnemonic = $mnemonic + ') + + stringified_json=$(echo "$updated_json" | jq -c .) + + echo "ALLORA_OFFCHAIN_NODE_CONFIG_JSON='$stringified_json'" > /data/env_file + echo ALLORA_OFFCHAIN_ACCOUNT_ADDRESS=$address >> /data/env_file + echo "NAME=$NAME" >> /data/env_file + + echo "Updated ALLORA_OFFCHAIN_NODE_CONFIG_JSON saved to /data/env_file" +fi + + +if grep -q "ENV_LOADED=false" /data/env_file; then + sed -i 's/ENV_LOADED=false/ENV_LOADED=true/' /data/env_file +else + echo "ENV_LOADED=true" >> /data/env_file +fi