From eeb88b9430dd73baab252696c3689f9de1496c02 Mon Sep 17 00:00:00 2001 From: gusto Date: Mon, 30 Oct 2023 15:51:40 +0100 Subject: [PATCH] Docker compose consensus layer config (#482) * Same consensus node list for all nodes * Extract duplicate code and add debug info * Update initial net peer list script * Remove unused envvar --- compose.yml | 14 +++++++----- testnet/scripts/consensus_node_list.sh | 22 +++++++++++++++++++ testnet/scripts/node_key_from_id.sh | 17 ++++++++++++++ testnet/{ => scripts}/register_node.sh | 5 ++--- testnet/scripts/run_bootstrap_node.sh | 17 ++++++++++++++ .../run_nomos_node.sh} | 10 +++++---- 6 files changed, 72 insertions(+), 13 deletions(-) create mode 100755 testnet/scripts/consensus_node_list.sh create mode 100755 testnet/scripts/node_key_from_id.sh rename testnet/{ => scripts}/register_node.sh (88%) create mode 100755 testnet/scripts/run_bootstrap_node.sh rename testnet/{configure_node.sh => scripts/run_nomos_node.sh} (70%) diff --git a/compose.yml b/compose.yml index b3348898..509dd62d 100644 --- a/compose.yml +++ b/compose.yml @@ -10,9 +10,11 @@ services: volumes: - ./testnet:/etc/nomos environment: - - OVERLAY_SUPER_MAJORITY_THRESHOLD=${DOCKER_COMPOSE_SUPER_MAJORITY_THRESHOLD:-1} - - NET_NODE_KEY=${DOCKER_COMPOSE_BOOSTRAP_NET_NODE_KEY:-1000000000000000000000000000000000000000000000000000000000000000} - command: /etc/nomos/bootstrap_config.yaml + - BOOTSTRAP_NODE_KEY=${DOCKER_COMPOSE_BOOSTRAP_NET_NODE_KEY:-1000000000000000000000000000000000000000000000000000000000000000} + - LIBP2P_NODE_MASK=${DOCKER_COMPOSE_LIBP2P_NODE_KEY_MASK:-2000000000000000000000000000000000000000000000000000000000000000} + - LIBP2P_REPLICAS=${DOCKER_COMPOSE_LIBP2P_REPLICAS:-1} + - OVERLAY_NODES=${DOCKER_COMPOSE_OVERLAY_NODES:-1000000000000000000000000000000000000000000000000000000000000000} + entrypoint: /etc/nomos/scripts/run_bootstrap_node.sh libp2p-node: build: @@ -29,14 +31,14 @@ services: - mix-node-1 - mix-node-2 environment: - - OVERLAY_SUPER_MAJORITY_THRESHOLD=${DOCKER_COMPOSE_SUPER_MAJORITY_THRESHOLD:-1} - LIBP2P_REPLICAS=${DOCKER_COMPOSE_LIBP2P_REPLICAS:-1} - ETCDCTL_ENDPOINTS=${DOCKER_COMPOSE_ETCDCTL_ENDPOINTS:-etcd:2379} - ETCDCTL_API=${DOCKER_COMPOSE_ETCDCTL_API:-3} - - NODE_MASK=${DOCKER_COMPOSE_LIBP2P_NODE_KEY_MASK:-2000000000000000000000000000000000000000000000000000000000000000} + - LIBP2P_NODE_MASK=${DOCKER_COMPOSE_LIBP2P_NODE_KEY_MASK:-2000000000000000000000000000000000000000000000000000000000000000} - OVERLAY_NODES=${DOCKER_COMPOSE_OVERLAY_NODES:-1000000000000000000000000000000000000000000000000000000000000000} + - OVERLAY_SUPER_MAJORITY_THRESHOLD=${DOCKER_COMPOSE_SUPER_MAJORITY_THRESHOLD:-1} - NET_INITIAL_PEERS=${DOCKER_COMPOSE_NET_INITIAL_PEERS:-/dns/bootstrap/tcp/3000} - entrypoint: /etc/nomos/configure_node.sh + entrypoint: /etc/nomos/scripts/run_nomos_node.sh mix-node-0: build: diff --git a/testnet/scripts/consensus_node_list.sh b/testnet/scripts/consensus_node_list.sh new file mode 100755 index 00000000..c7c20572 --- /dev/null +++ b/testnet/scripts/consensus_node_list.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +set -e + +# This node id will be used to generate consensus node list. +tmp_node_id=0 +# OVERLAY_NODES might be set in compose.yml. +tmp_overlay_nodes=$OVERLAY_NODES + +# All spawned nodes should be added to consensus configuration. +for i in $(seq 1 $LIBP2P_REPLICAS); do + tmp_node_id=$((tmp_node_id + 1)) + node_key=$(/etc/nomos/scripts/node_key_from_id.sh "$LIBP2P_NODE_MASK" "$tmp_node_id") + + if [ -z "$tmp_overlay_nodes" ]; then + tmp_overlay_nodes=$node_key + else + tmp_overlay_nodes="${tmp_overlay_nodes},${node_key}" + fi +done + +echo "${tmp_overlay_nodes}" diff --git a/testnet/scripts/node_key_from_id.sh b/testnet/scripts/node_key_from_id.sh new file mode 100755 index 00000000..d87d8736 --- /dev/null +++ b/testnet/scripts/node_key_from_id.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +set -e + +if [ -z "$1" ] || [ -z "$2" ]; then + echo "Usage: $0 " + exit 1 +fi + +libp2p_node_mask=$1 +node_id=$2 + +node_key_from_id() { + echo "${libp2p_node_mask}" | sed "s/.\{${#node_id}\}$/${node_id}/" +} + +node_key_from_id diff --git a/testnet/register_node.sh b/testnet/scripts/register_node.sh similarity index 88% rename from testnet/register_node.sh rename to testnet/scripts/register_node.sh index a2ec94ec..667cc734 100755 --- a/testnet/register_node.sh +++ b/testnet/scripts/register_node.sh @@ -1,9 +1,8 @@ #!/bin/sh -# NODE_MASK is set via compose.yml file. - +# LIBP2P_NODE_MASK is set via compose.yml file. node_key_from_id() { - echo "${NODE_MASK}" | sed "s/.\{${#NODE_ID}\}$/${NODE_ID}/" + echo "${LIBP2P_NODE_MASK}" | sed "s/.\{${#NODE_ID}\}$/${NODE_ID}/" } END=$LIBP2P_REPLICAS diff --git a/testnet/scripts/run_bootstrap_node.sh b/testnet/scripts/run_bootstrap_node.sh new file mode 100755 index 00000000..253da58d --- /dev/null +++ b/testnet/scripts/run_bootstrap_node.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +set -e + +CONSENSUS_PRIV_KEY=$BOOTSTRAP_NODE_KEY +NET_NODE_KEY=$BOOTSTRAP_NODE_KEY +OVERLAY_NODES=$(/etc/nomos/scripts/consensus_node_list.sh) + +export CONSENSUS_PRIV_KEY \ + OVERLAY_NODES \ + NET_NODE_KEY + +echo "I am a container ${HOSTNAME} node ${NET_NODE_KEY}" +echo "CONSENSUS_PRIV_KEY: ${CONSENSUS_PRIV_KEY}" +echo "OVERLAY_NODES: ${OVERLAY_NODES}" + +exec /usr/bin/nomos-node /etc/nomos/bootstrap_config.yaml diff --git a/testnet/configure_node.sh b/testnet/scripts/run_nomos_node.sh similarity index 70% rename from testnet/configure_node.sh rename to testnet/scripts/run_nomos_node.sh index 4740ce11..4c33a934 100755 --- a/testnet/configure_node.sh +++ b/testnet/scripts/run_nomos_node.sh @@ -3,8 +3,9 @@ set -e # Set env variables for nomos-node. -NET_NODE_KEY=$(./etc/nomos/register_node.sh) +NET_NODE_KEY=$(/etc/nomos/scripts/register_node.sh) CONSENSUS_PRIV_KEY=$NET_NODE_KEY +OVERLAY_NODES=$(/etc/nomos/scripts/consensus_node_list.sh) node_ids=$(etcdctl get "/node/" --prefix --keys-only) for node_id in $node_ids; do @@ -12,11 +13,9 @@ for node_id in $node_ids; do node_ip=$(etcdctl get "/config${node_id}/ip" --print-value-only) node_multiaddr="/ip4/${node_ip}/tcp/3000" - if [ -z "$OVERLAY_NODES" ]; then - OVERLAY_NODES=$node_key + if [ -z "$NET_INITIAL_PEERS" ]; then NET_INITIAL_PEERS=$node_multiaddr else - OVERLAY_NODES="${OVERLAY_NODES},${node_key}" NET_INITIAL_PEERS="${NET_INITIAL_PEERS},${node_multiaddr}" fi done @@ -27,5 +26,8 @@ export CONSENSUS_PRIV_KEY \ NET_INITIAL_PEERS echo "I am a container ${HOSTNAME} node ${NET_NODE_KEY}" +echo "CONSENSUS_PRIV_KEY: ${CONSENSUS_PRIV_KEY}" +echo "OVERLAY_NODES: ${OVERLAY_NODES}" +echo "NET_INITIAL_PEERS: ${NET_INITIAL_PEERS}" exec /usr/bin/nomos-node /etc/nomos/libp2p_config.yaml