2018-12-07 10:00:35 -08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2019-05-03 13:32:59 -07:00
|
|
|
# Start a fullnode
|
2018-12-07 10:00:35 -08:00
|
|
|
#
|
|
|
|
here=$(dirname "$0")
|
|
|
|
# shellcheck source=multinode-demo/common.sh
|
|
|
|
source "$here"/common.sh
|
|
|
|
|
|
|
|
# shellcheck source=scripts/oom-score-adj.sh
|
|
|
|
source "$here"/../scripts/oom-score-adj.sh
|
|
|
|
|
2019-05-03 13:32:59 -07:00
|
|
|
fullnode_usage() {
|
|
|
|
if [[ -n $1 ]]; then
|
|
|
|
echo "$*"
|
|
|
|
echo
|
|
|
|
fi
|
|
|
|
cat <<EOF
|
2019-05-03 20:49:24 -07:00
|
|
|
usage: $0 [--blockstream PATH] [--init-complete-file FILE] [--label LABEL] [--stake LAMPORTS] [--no-voting] [--rpc-port port] [rsync network path to bootstrap leader configuration] [cluster entry point]
|
2019-05-03 13:32:59 -07:00
|
|
|
|
|
|
|
Start a full node
|
|
|
|
|
|
|
|
--blockstream PATH - open blockstream at this unix domain socket location
|
|
|
|
--init-complete-file FILE - create this file, if it doesn't already exist, once node initialization is complete
|
|
|
|
--label LABEL - Append the given label to the fullnode configuration files, useful when running
|
|
|
|
multiple fullnodes from the same filesystem location
|
|
|
|
--stake LAMPORTS - Number of lamports to stake
|
|
|
|
--no-voting - start node without vote signer
|
|
|
|
--rpc-port port - custom RPC port for this node
|
|
|
|
|
|
|
|
EOF
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2018-12-07 10:00:35 -08:00
|
|
|
find_leader() {
|
|
|
|
declare leader leader_address
|
|
|
|
declare shift=0
|
|
|
|
|
2019-03-02 17:08:46 -08:00
|
|
|
if [[ -z $1 ]]; then
|
2019-03-21 23:18:19 -07:00
|
|
|
leader=$PWD # Default to local tree for rsync
|
2019-03-02 17:08:46 -08:00
|
|
|
leader_address=127.0.0.1:8001 # Default to local leader
|
|
|
|
elif [[ -z $2 ]]; then
|
|
|
|
leader=$1
|
2019-04-13 19:34:27 -07:00
|
|
|
leader_address=$leader:8001
|
2019-03-02 17:08:46 -08:00
|
|
|
shift=1
|
2018-12-07 10:00:35 -08:00
|
|
|
else
|
2019-03-02 17:08:46 -08:00
|
|
|
leader=$1
|
|
|
|
leader_address=$2
|
|
|
|
shift=2
|
2018-12-07 10:00:35 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$leader" "$leader_address" "$shift"
|
|
|
|
}
|
|
|
|
|
|
|
|
rsync_url() { # adds the 'rsync://` prefix to URLs that need it
|
|
|
|
declare url="$1"
|
|
|
|
|
|
|
|
if [[ $url =~ ^.*:.*$ ]]; then
|
|
|
|
# assume remote-shell transport when colon is present, use $url unmodified
|
|
|
|
echo "$url"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -d $url ]]; then
|
|
|
|
# assume local directory if $url is a valid directory, use $url unmodified
|
|
|
|
echo "$url"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Default to rsync:// URL
|
|
|
|
echo "rsync://$url"
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:03:58 -07:00
|
|
|
airdrop() {
|
|
|
|
declare keypair_file=$1
|
2019-05-06 07:38:26 -07:00
|
|
|
declare entrypoint_ip=$2
|
2019-04-17 18:03:58 -07:00
|
|
|
declare amount=$3
|
2019-03-08 18:29:08 -08:00
|
|
|
|
2019-04-17 18:03:58 -07:00
|
|
|
declare address
|
|
|
|
address=$($solana_wallet --keypair "$keypair_file" address)
|
|
|
|
|
|
|
|
# TODO: Until https://github.com/solana-labs/solana/issues/2355 is resolved
|
|
|
|
# a fullnode needs N lamports as its vote account gets re-created on every
|
|
|
|
# node restart, costing it lamports
|
|
|
|
declare retries=5
|
|
|
|
|
2019-05-06 07:38:26 -07:00
|
|
|
while ! $solana_wallet --keypair "$keypair_file" --url "http://$entrypoint_ip:8899" airdrop "$amount"; do
|
2019-04-17 18:03:58 -07:00
|
|
|
|
|
|
|
# TODO: Consider moving this retry logic into `solana-wallet airdrop`
|
|
|
|
# itself, currently it does not retry on "Connection refused" errors.
|
|
|
|
((retries--))
|
|
|
|
if [[ $retries -le 0 ]]; then
|
|
|
|
echo "Airdrop to $address failed."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
echo "Airdrop to $address failed. Remaining retries: $retries"
|
|
|
|
sleep 1
|
|
|
|
done
|
2019-03-21 23:18:19 -07:00
|
|
|
|
2019-04-17 18:03:58 -07:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_vote_account() {
|
2019-05-06 07:38:26 -07:00
|
|
|
declare entrypoint_ip=$1
|
2019-04-17 18:03:58 -07:00
|
|
|
declare node_id_path=$2
|
|
|
|
declare vote_id_path=$3
|
|
|
|
declare stake=$4
|
|
|
|
|
|
|
|
declare node_id
|
|
|
|
node_id=$($solana_wallet --keypair "$node_id_path" address)
|
|
|
|
|
|
|
|
declare vote_id
|
|
|
|
vote_id=$($solana_wallet --keypair "$vote_id_path" address)
|
|
|
|
|
|
|
|
if [[ -f "$vote_id_path".configured ]]; then
|
|
|
|
echo "Vote account has already been configured"
|
|
|
|
else
|
2019-05-06 07:38:26 -07:00
|
|
|
airdrop "$node_id_path" "$entrypoint_ip" "$stake" || return $?
|
2019-04-17 18:03:58 -07:00
|
|
|
|
|
|
|
# Fund the vote account from the node, with the node as the node_id
|
2019-05-06 07:38:26 -07:00
|
|
|
$solana_wallet --keypair "$node_id_path" --url "http://$entrypoint_ip:8899" \
|
2019-04-17 18:03:58 -07:00
|
|
|
create-vote-account "$vote_id" "$node_id" $((stake - 1)) || return $?
|
|
|
|
|
|
|
|
touch "$vote_id_path".configured
|
|
|
|
fi
|
|
|
|
|
2019-05-06 07:38:26 -07:00
|
|
|
$solana_wallet --keypair "$node_id_path" --url "http://$entrypoint_ip:8899" \
|
|
|
|
show-vote-account "$vote_id"
|
2019-04-17 18:03:58 -07:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
ledger_not_setup() {
|
|
|
|
echo "Error: $*"
|
|
|
|
echo
|
|
|
|
echo "Please run: ${here}/setup.sh"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2019-05-03 13:32:59 -07:00
|
|
|
default_fullnode_arg() {
|
|
|
|
declare name=$1
|
|
|
|
declare value=$2
|
|
|
|
|
|
|
|
for arg in "${extra_fullnode_args[@]}"; do
|
|
|
|
if [[ $arg = "$name" ]]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -n $value ]]; then
|
|
|
|
extra_fullnode_args+=("$name" "$value")
|
|
|
|
else
|
|
|
|
extra_fullnode_args+=("$name")
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
extra_fullnode_args=()
|
|
|
|
bootstrap_leader=false
|
2019-05-07 20:28:41 -07:00
|
|
|
stake=42 # number of lamports to assign as stake
|
2019-05-03 13:32:59 -07:00
|
|
|
poll_for_new_genesis_block=0
|
|
|
|
label=
|
|
|
|
|
2019-05-03 20:49:24 -07:00
|
|
|
positional_args=()
|
|
|
|
while [[ -n $1 ]]; do
|
|
|
|
if [[ ${1:0:1} = - ]]; then
|
|
|
|
if [[ $1 = --label ]]; then
|
|
|
|
label="-$2"
|
|
|
|
shift 2
|
|
|
|
elif [[ $1 = --bootstrap-leader ]]; then
|
|
|
|
bootstrap_leader=true
|
|
|
|
shift
|
|
|
|
elif [[ $1 = --poll-for-new-genesis-block ]]; then
|
|
|
|
poll_for_new_genesis_block=1
|
|
|
|
shift
|
|
|
|
elif [[ $1 = --blockstream ]]; then
|
|
|
|
stake=0
|
|
|
|
extra_fullnode_args+=("$1" "$2")
|
|
|
|
shift 2
|
|
|
|
elif [[ $1 = --enable-rpc-exit ]]; then
|
|
|
|
extra_fullnode_args+=("$1")
|
|
|
|
shift
|
|
|
|
elif [[ $1 = --init-complete-file ]]; then
|
|
|
|
extra_fullnode_args+=("$1" "$2")
|
|
|
|
shift 2
|
|
|
|
elif [[ $1 = --stake ]]; then
|
|
|
|
stake="$2"
|
|
|
|
shift 2
|
|
|
|
elif [[ $1 = --no-voting ]]; then
|
|
|
|
extra_fullnode_args+=("$1")
|
|
|
|
shift
|
|
|
|
elif [[ $1 = --no-sigverify ]]; then
|
|
|
|
extra_fullnode_args+=("$1")
|
|
|
|
shift
|
|
|
|
elif [[ $1 = --rpc-port ]]; then
|
|
|
|
extra_fullnode_args+=("$1" "$2")
|
|
|
|
shift 2
|
|
|
|
elif [[ $1 = --dynamic-port-range ]]; then
|
|
|
|
extra_fullnode_args+=("$1" "$2")
|
|
|
|
shift 2
|
|
|
|
elif [[ $1 = --gossip-port ]]; then
|
|
|
|
extra_fullnode_args+=("$1" "$2")
|
|
|
|
shift 2
|
|
|
|
elif [[ $1 = -h ]]; then
|
|
|
|
fullnode_usage "$@"
|
|
|
|
else
|
|
|
|
echo "Unknown argument: $1"
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-05-03 13:32:59 -07:00
|
|
|
else
|
2019-05-03 20:49:24 -07:00
|
|
|
positional_args+=("$1")
|
|
|
|
shift
|
2019-05-03 13:32:59 -07:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
if $bootstrap_leader; then
|
2019-05-03 20:49:24 -07:00
|
|
|
if [[ ${#positional_args[@]} -ne 0 ]]; then
|
|
|
|
fullnode_usage "Unknown argument: ${positional_args[0]}"
|
2019-05-03 13:32:59 -07:00
|
|
|
fi
|
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
[[ -f "$SOLANA_CONFIG_DIR"/bootstrap-leader-id.json ]] ||
|
|
|
|
ledger_not_setup "$SOLANA_CONFIG_DIR/bootstrap-leader-id.json not found"
|
|
|
|
|
|
|
|
$solana_ledger_tool --ledger "$SOLANA_CONFIG_DIR"/bootstrap-leader-ledger verify
|
|
|
|
|
|
|
|
fullnode_id_path="$SOLANA_CONFIG_DIR"/bootstrap-leader-id.json
|
|
|
|
fullnode_vote_id_path="$SOLANA_CONFIG_DIR"/bootstrap-leader-vote-id.json
|
|
|
|
ledger_config_dir="$SOLANA_CONFIG_DIR"/bootstrap-leader-ledger
|
|
|
|
accounts_config_dir="$SOLANA_CONFIG_DIR"/bootstrap-leader-accounts
|
|
|
|
|
|
|
|
default_fullnode_arg --rpc-port 8899
|
|
|
|
default_fullnode_arg --rpc-drone-address 127.0.0.1:9900
|
|
|
|
default_fullnode_arg --gossip-port 8001
|
|
|
|
else
|
2019-05-03 20:49:24 -07:00
|
|
|
|
|
|
|
if [[ ${#positional_args[@]} -gt 2 ]]; then
|
2019-05-03 13:32:59 -07:00
|
|
|
fullnode_usage "$@"
|
|
|
|
fi
|
|
|
|
|
2019-05-03 20:49:24 -07:00
|
|
|
read -r leader leader_address shift < <(find_leader "${positional_args[@]}")
|
2019-05-03 12:33:48 -07:00
|
|
|
shift "$shift"
|
|
|
|
|
|
|
|
fullnode_id_path=$SOLANA_CONFIG_DIR/fullnode-id$label.json
|
|
|
|
fullnode_vote_id_path=$SOLANA_CONFIG_DIR/fullnode-vote-id$label.json
|
|
|
|
ledger_config_dir=$SOLANA_CONFIG_DIR/fullnode-ledger$label
|
|
|
|
accounts_config_dir=$SOLANA_CONFIG_DIR/fullnode-accounts$label
|
|
|
|
|
|
|
|
mkdir -p "$SOLANA_CONFIG_DIR"
|
|
|
|
[[ -r "$fullnode_id_path" ]] || $solana_keygen -o "$fullnode_id_path"
|
|
|
|
[[ -r "$fullnode_vote_id_path" ]] || $solana_keygen -o "$fullnode_vote_id_path"
|
|
|
|
|
2019-05-03 15:00:19 -07:00
|
|
|
default_fullnode_arg --entrypoint "$leader_address"
|
2019-05-03 12:33:48 -07:00
|
|
|
default_fullnode_arg --rpc-drone-address "${leader_address%:*}:9900"
|
|
|
|
fi
|
|
|
|
|
|
|
|
fullnode_id=$($solana_keygen pubkey "$fullnode_id_path")
|
|
|
|
fullnode_vote_id=$($solana_keygen pubkey "$fullnode_vote_id_path")
|
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
======================[ Fullnode configuration ]======================
|
|
|
|
node id: $fullnode_id
|
|
|
|
vote id: $fullnode_vote_id
|
|
|
|
ledger: $ledger_config_dir
|
|
|
|
accounts: $accounts_config_dir
|
|
|
|
======================================================================
|
|
|
|
EOF
|
2019-05-03 20:49:24 -07:00
|
|
|
|
|
|
|
if [[ -z $CI ]]; then # Skip in CI
|
|
|
|
# shellcheck source=scripts/tune-system.sh
|
|
|
|
source "$here"/../scripts/tune-system.sh
|
|
|
|
fi
|
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
default_fullnode_arg --identity "$fullnode_id_path"
|
|
|
|
default_fullnode_arg --voting-keypair "$fullnode_vote_id_path"
|
|
|
|
default_fullnode_arg --vote-account "$fullnode_vote_id"
|
|
|
|
default_fullnode_arg --ledger "$ledger_config_dir"
|
|
|
|
default_fullnode_arg --accounts "$accounts_config_dir"
|
|
|
|
|
|
|
|
if [[ -n $SOLANA_CUDA ]]; then
|
|
|
|
program=$solana_fullnode_cuda
|
|
|
|
else
|
|
|
|
program=$solana_fullnode
|
|
|
|
fi
|
|
|
|
|
2019-04-17 18:03:58 -07:00
|
|
|
set -e
|
2019-03-21 23:18:19 -07:00
|
|
|
secs_to_next_genesis_poll=0
|
|
|
|
PS4="$(basename "$0"): "
|
|
|
|
while true; do
|
|
|
|
if [[ ! -d "$SOLANA_RSYNC_CONFIG_DIR"/ledger ]]; then
|
2019-05-03 12:33:48 -07:00
|
|
|
if $bootstrap_leader; then
|
|
|
|
ledger_not_setup "$SOLANA_RSYNC_CONFIG_DIR/ledger does not exist"
|
|
|
|
fi
|
|
|
|
rsync_leader_url=$(rsync_url "$leader")
|
2019-03-21 23:18:19 -07:00
|
|
|
$rsync -vPr "$rsync_leader_url"/config/ledger "$SOLANA_RSYNC_CONFIG_DIR"
|
|
|
|
fi
|
2019-03-21 15:27:09 -07:00
|
|
|
|
2019-03-21 23:18:19 -07:00
|
|
|
if [[ ! -d "$ledger_config_dir" ]]; then
|
|
|
|
cp -a "$SOLANA_RSYNC_CONFIG_DIR"/ledger/ "$ledger_config_dir"
|
|
|
|
$solana_ledger_tool --ledger "$ledger_config_dir" verify
|
|
|
|
fi
|
2018-12-07 10:00:35 -08:00
|
|
|
|
2019-04-26 07:30:52 -07:00
|
|
|
trap '[[ -n $pid ]] && kill "$pid" >/dev/null 2>&1 && wait "$pid"' INT TERM ERR
|
2019-04-16 13:03:01 -07:00
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
if ! $bootstrap_leader && ((stake)); then
|
2019-04-16 13:03:01 -07:00
|
|
|
setup_vote_account "${leader_address%:*}" "$fullnode_id_path" "$fullnode_vote_id_path" "$stake"
|
|
|
|
fi
|
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
echo "$PS4$program ${extra_fullnode_args[*]}"
|
2019-05-03 11:01:35 -07:00
|
|
|
$program "${extra_fullnode_args[@]}" > >($fullnode_logger) 2>&1 &
|
2019-03-21 23:18:19 -07:00
|
|
|
pid=$!
|
|
|
|
oom_score_adj "$pid" 1000
|
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
if $bootstrap_leader; then
|
|
|
|
wait "$pid"
|
2019-03-21 23:18:19 -07:00
|
|
|
sleep 1
|
2019-05-03 12:33:48 -07:00
|
|
|
else
|
|
|
|
while true; do
|
|
|
|
if ! kill -0 "$pid"; then
|
|
|
|
wait "$pid"
|
|
|
|
exit 0
|
|
|
|
fi
|
2019-03-21 23:18:19 -07:00
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
sleep 1
|
2019-04-26 07:30:52 -07:00
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
((poll_for_new_genesis_block)) || continue
|
|
|
|
((secs_to_next_genesis_poll--)) && continue
|
2019-04-26 07:30:52 -07:00
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
$rsync -r "$rsync_leader_url"/config/ledger "$SOLANA_RSYNC_CONFIG_DIR" || true
|
|
|
|
diff -q "$SOLANA_RSYNC_CONFIG_DIR"/ledger/genesis.json "$ledger_config_dir"/genesis.json >/dev/null 2>&1 || break
|
|
|
|
secs_to_next_genesis_poll=60
|
2019-04-26 07:30:52 -07:00
|
|
|
|
2019-05-03 12:33:48 -07:00
|
|
|
done
|
|
|
|
|
|
|
|
echo "############## New genesis detected, restarting fullnode ##############"
|
|
|
|
kill "$pid" || true
|
|
|
|
wait "$pid" || true
|
|
|
|
rm -rf "$ledger_config_dir" "$accounts_config_dir" "$fullnode_vote_id_path".configured
|
|
|
|
sleep 60 # give the network time to come back up
|
|
|
|
fi
|
2019-04-26 07:30:52 -07:00
|
|
|
|
2019-03-21 23:18:19 -07:00
|
|
|
done
|