diff --git a/.gitignore b/.gitignore index 45d9d21a..2d92b91c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,7 @@ dist/ scripts/testnet/config.sh scripts/testnet/nodes scripts/testnet/artifacts + +# Ignore local env files +seda.env +.env \ No newline at end of file diff --git a/dockerfiles/Dockerfile.node-static b/dockerfiles/Dockerfile.node-static index 6ac1502f..74fbcaf3 100644 --- a/dockerfiles/Dockerfile.node-static +++ b/dockerfiles/Dockerfile.node-static @@ -1,6 +1,5 @@ # syntax=docker/dockerfile:1 ARG GO_VERSION="1.20" -ARG RUNNER_IMAGE="gcr.io/distroless/static-debian11" # -------------------------------------------------------- # Builder @@ -26,7 +25,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build \ # Cosmwasm - Download correct libwasmvm version RUN ARCH=$(uname -m) && WASMVM_VERSION=$(go list -m github.com/CosmWasm/wasmvm | sed 's/.* //') && \ wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm_muslc.$ARCH.a \ - -O /lib/libwasmvm_muslc.a && \ + -O /lib/libwasmvm_muslc.a && \ # verify checksum wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/checksums.txt -O /tmp/checksums.txt && \ sha256sum /lib/libwasmvm_muslc.a | grep $(cat /tmp/checksums.txt | grep libwasmvm_muslc.$ARCH | cut -d ' ' -f 1) @@ -38,30 +37,28 @@ COPY . . RUN --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/root/go/pkg/mod \ GOWORK=off go build -v \ - -mod=readonly \ - -tags "netgo,ledger,muslc" \ - -ldflags \ - "-X github.com/cosmos/cosmos-sdk/version.Name="seda-chain" \ - -X github.com/cosmos/cosmos-sdk/version.AppName="seda-chaind" \ - -X github.com/cosmos/cosmos-sdk/version.Version=${GIT_VERSION} \ - -X github.com/cosmos/cosmos-sdk/version.Commit=${GIT_COMMIT} \ - -X github.com/cosmos/cosmos-sdk/version.BuildTags=netgo,ledger,muslc \ - -w -s -linkmode=external -extldflags '-Wl,-z,muldefs -static'" \ - -trimpath \ - -o /seda-chain/build/seda-chaind \ - cmd/seda-chaind/main.go -# --------- To include chmod and shell for static image -FROM busybox:1.35.0-uclibc as busybox + -mod=readonly \ + -tags "netgo,ledger,muslc" \ + -ldflags \ + "-X github.com/cosmos/cosmos-sdk/version.Name="seda-chain" \ + -X github.com/cosmos/cosmos-sdk/version.AppName="seda-chaind" \ + -X github.com/cosmos/cosmos-sdk/version.Version=${GIT_VERSION} \ + -X github.com/cosmos/cosmos-sdk/version.Commit=${GIT_COMMIT} \ + -X github.com/cosmos/cosmos-sdk/version.BuildTags=netgo,ledger,muslc \ + -w -s -linkmode=external -extldflags '-Wl,-z,muldefs -static'" \ + -trimpath \ + -o /seda-chain/build/seda-chaind \ + cmd/seda-chaind/main.go # -------------------------------------------------------- # Runner # -------------------------------------------------------- -FROM ${RUNNER_IMAGE} +FROM alpine:latest -COPY --from=busybox ["/bin/sh", "/bin/chmod", "/bin/chown", "/bin/mkdir", "/bin/"] -RUN mkdir -p /seda-chain/.seda-chain && chown nonroot:nonroot -R /seda-chain/.seda-chain +RUN mkdir -p /seda-chain/.seda-chain -USER nonroot -COPY --from=builder --chown=nonroot:nonroot /seda-chain/build/seda-chaind /bin/seda-chaind +COPY --from=builder /seda-chain/build/seda-chaind /bin/seda-chaind + +RUN apk add --no-cache bash expect jq ENV HOME /seda-chain WORKDIR $HOME @@ -71,6 +68,6 @@ EXPOSE 26657 EXPOSE 1317 EXPOSE 9090 -COPY --chown=nonroot:nonroot scripts/validator_setup/validator_setup.sh . -RUN chmod +x validator_setup.sh -ENTRYPOINT ["sh", "validator_setup.sh"] +COPY scripts/node_setup/* . +RUN find . -type f -name "*.sh" -exec chmod +x {} \; +ENTRYPOINT ["bash", "setup_run_node.sh"] diff --git a/scripts/node_setup/become_validator.sh b/scripts/node_setup/become_validator.sh new file mode 100644 index 00000000..244aa5cf --- /dev/null +++ b/scripts/node_setup/become_validator.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Tell script to exit immediately if any cmd fails +set -e + +BIN="seda-chaind" +KEY_NAME="${KEY_NAME:-default_key}" +STAKE_AMOUNT="$1aseda" + +source common.sh + +validator_pub_key=$($BIN tendermint show-validator --home=$HOME/.seda-chain) + +chain_id=$(cat $HOME/.seda-chain/config/genesis.json | jq .chain_id | tr -d '"') +auth_seda_chaind_command tx staking create-validator --amount=$STAKE_AMOUNT --pubkey=$validator_pub_key --moniker=$MONIKER --commission-rate=0.10 --commission-max-rate=0.20 --commission-max-change-rate=0.01 --gas=auto --gas-adjustment=1.2 --gas-prices=0.0025aseda --from=$KEY_NAME --min-self-delegation=1 --yes --home=$HOME/.seda-chain --chain-id=$chain_id + diff --git a/scripts/node_setup/check_validator.sh b/scripts/node_setup/check_validator.sh new file mode 100644 index 00000000..c4688d91 --- /dev/null +++ b/scripts/node_setup/check_validator.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +# Tell script to exit immediately if any cmd fails +set -e + +BIN="seda-chaind" +KEY_NAME="${KEY_NAME:-default_key}" + +source common.sh + +validator_address=$(auth_seda_chaind_command keys show $KEY_NAME --bech val | grep "address:" | awk '{print $3}') +output=$(seda-chaind query staking validator $validator_address 2>&1) +echo "$output" | grep "error:" || echo "$output" + diff --git a/scripts/node_setup/common.sh b/scripts/node_setup/common.sh new file mode 100644 index 00000000..5c0bab1e --- /dev/null +++ b/scripts/node_setup/common.sh @@ -0,0 +1,18 @@ + +function auth_seda_chaind_command { + local args=("$@") + output=$(expect -c " + spawn $BIN ${args[*]} + expect { + \"Enter keyring passphrase (attempt 1/3):\" { + send \"$KEYRING_PASSWORD\r\" + } + timeout { + send_user \"Timed out waiting for enter passphrase prompt\r\" + exit 1 + } + } + expect eof +") + echo "$output" +} \ No newline at end of file diff --git a/scripts/node_setup/setup_run_node.sh b/scripts/node_setup/setup_run_node.sh new file mode 100644 index 00000000..b1441b4e --- /dev/null +++ b/scripts/node_setup/setup_run_node.sh @@ -0,0 +1,133 @@ +#!/usr/bin/env bash +# Tell script to exit immediately if any cmd fails +set -e + +# Basic Setup Configuration +# -------------------------- +# This script is used to setup a node for the seda-chain network. + + +BIN=seda-chaind +KEY_NAME="${KEY_NAME:-default_key}" + +# Check if a env variable exists +# returns true if it doesn't exist false otherwise +function check_env_var { + local var_name="$1" + if [[ -z "${!var_name}" ]]; then + echo "Error: no key $var_name provided" + return 1 + fi + return 0 +} + +# Define an error boolean +# Then check if required env variables are set +error=0 +check_env_var "MONIKER" || error=1 +check_env_var "NODE_ADDRESS" || error=1 +check_env_var "NETWORK" || error=1 + +# TODO +# check_env_var "VALIDATOR" || error=1 +# if [[ "$VALIDATOR" == "true" ]]; then + # echo "Error: MNEMONIC is required for validator nodes since it requires funds" + # check_env_var "MNEMONIC" || error=1 +# fi + +# If any one of them was missing then exit +if (( error )); then + exit 1 +fi + +# Set the keyring backend to file +$BIN config keyring-backend file + +function create_or_import_key { + local key_name="$1" + local mnemonic="$2" + local recover_flag="$3" + + output=$(expect -c " + set key_name \"$key_name\" + set recover_flag \"$recover_flag\" + set mnemonic \"$mnemonic\" + spawn seda-chaind keys add \$key_name \$recover_flag + expect { + \"> Enter your bip39 mnemonic\" { + send \"$mnemonic\r\" + } + timeout { + send_user \"Timed out waiting for enter mnemonic prompt\r\" + exit 1 + } + } + expect { + \"Enter keyring passphrase (attempt 1/3):\" { + send \"$KEYRING_PASSWORD\r\" + expect { + \"Re-enter keyring passphrase:\" { + send \"$KEYRING_PASSWORD\r\" + } + timeout { + # We're done if we timeout after sending the passphrase + } + } + } + timeout { + send_user \"Timed out waiting for enter passphrase prompt\r\" + exit 1 + } + } + expect eof +") + + + echo "$output" +} + +# If the MNEMONIC is provided we import it +if [[ ! -z "${MNEMONIC}" ]]; then + echo "Importing provided MNEMONIC..." + create_or_import_key "$KEY_NAME" "$MNEMONIC" "--recover" +# If the MNEMONIC is not provided we generate one +else + echo "Error no key MNEMONIC provided generating one..." + echo "NOTE: This is done in the file backend..." + echo "We recommend storing this somewhere secure..." + output=$(create_or_import_key "$KEY_NAME" "" "") + # We greb the mnemonic from the output. + mnemonic=$(echo "$output" | awk '/Important/,0' | tail -n 1) + # We check if the `env` file had a MNEMONIC variable in general... + awk -v mnemonic="$mnemonic" 'BEGIN{OFS=FS="="} $1=="MNEMONIC"{$2=mnemonic}1' .env > .env.tmp && cat .env.tmp > .env && rm .env.tmp + # Lastly set the env variable for this session. + export MNEMONIC="$mnemonic" +fi + + +# Initialize NODE config +echo "Initializing Node ..." + +# # Give docker image permission to write to the seda-chain config directory +# chmod -R a+w /seda-chain/.seda-chain + +# Check if configuration directory seda-chain config directory exist if it does not +# exist initialize the node with the given MNEMONIC, MONIKER and NETWORK +if ! [ -f /seda-chain/.seda-chain/config/genesis.json ]; then + echo "Setting Up seda configuration" + echo $MNEMONIC | $BIN init join ${MONIKER} --network ${NETWORK} --recover + else + echo "seda configuration already exists" +fi + +echo "Node Initialized !" + + +# It creates a Client to the seda-chain network +echo "Connecting to Network `$NETWORK`..." +$BIN config node $NODE_ADDRESS +echo "Connected to network !" + +# Run node +echo "Running Node ..." +$BIN start diff --git a/scripts/validator_setup/validator_setup.sh b/scripts/validator_setup/validator_setup.sh deleted file mode 100644 index 03690103..00000000 --- a/scripts/validator_setup/validator_setup.sh +++ /dev/null @@ -1,73 +0,0 @@ -#!/bin/bash -set -e - -# Basic Setup Configurations -# -------------------------- -# This script is used to setup a validator node for the seda-chain network -# It takes the following parameters insde a env file: -# 1. MNEMONIC: The mnemonic of the validator account -# 2. KEYRING_PASSWORD: The password to encrypt the keyring -# 3. NETWORK_ID: The network id of the seda-chain network -# 4. MONIKER: The moniker of the validator node -# 5. NODE_ADDRESS: The address of the seda-chain network -# IE: -# MONIKER="somemonicer" - #MNEMONIC="a mnemonic" - #KEYRING_PASSWORD="somepassword" - #NETWORK_ID="devnet" - #NODE_ADDRESS="http://35.177.180.184:26657" - -BIN=seda-chaind # chain binary executable on your machine -KEY_NAME="${KEY_NAME:-default_key}" - -#if NODE_ADDRESS is not provided exit with error -if [[ -z "${NODE_ADDRESS}" ]]; then - echo "Error no key NODE_ADDRESS provided" - exit 1 -fi - -if [[ -z "${KEYRING_PASSWORD}" ]]; then - echo "Error no key KEYRING_PASSWORD provided" - exit 1 -fi - -if [[ -z "${NETWORK_ID}" ]]; then - echo "Error no key NETWORK_ID provided" - exit 1 -fi - -if [[ -z "${MONIKER}" ]]; then - echo "Error no key password provided" - exit 1 -fi - -if [[ -z "${MNEMONIC}" ]]; then - echo "Error no key MNEMONIC provided" - exit 1 -fi - -# Set the keyring-backend to file -# Initialize NODE config -echo "Initializing Node ..." - -# Check if configuration directory seda-chain config directory exist if it does not -# exist initialize the node with the given MNEMONIC, MONIKER and NETWORK_ID -if ! [ -f /seda-chain/.seda-chain/config/genesis.json ]; then - echo "Setting Up seda configuration" - $BIN config keyring-backend file # use file backend - echo $MNEMONIC | $BIN init join ${MONIKER} --network ${NETWORK_ID} --recover - else - echo "seda configuration already exists" -fi - -echo "Node Initialized !" - - -# It creates a Client to the seda-chain network -echo "Connecting to Network ..." -echo $NODE_ADDRESS | $BIN config node $NODE_ADDRESS -echo "Connected to network !" - -# Run node -echo "Running Node ..." -$BIN start \ No newline at end of file