diff --git a/build.sh b/build.sh index e252d09..dd2ba54 100755 --- a/build.sh +++ b/build.sh @@ -45,48 +45,68 @@ fi source .env export DOCKER_NETWORK_NAME="snapshotter-lite-v2-${SLOT_ID}" -SUBNET_SECOND_OCTET=$((16 + (SLOT_ID / 256) % 240)) -SUBNET_THIRD_OCTET=$((SLOT_ID % 256)) -# Always use 0 for the fourth octet to ensure a valid subnet -export DOCKER_NETWORK_SUBNET="172.${SUBNET_SECOND_OCTET}.${SUBNET_THIRD_OCTET}.0/24" +# Use 172.18.0.0/16 as the base, which is within Docker's default pool +if [ -z "$SUBNET_THIRD_OCTET" ]; then + SUBNET_THIRD_OCTET=$((SLOT_ID % 256)) + echo "SUBNET_THIRD_OCTET not found in .env, setting to default value ${SUBNET_THIRD_OCTET}" +fi +export DOCKER_NETWORK_SUBNET="172.18.${SUBNET_THIRD_OCTET}.0/24" echo "Selected DOCKER_NETWORK_NAME: ${DOCKER_NETWORK_NAME}" echo "Selected DOCKER_NETWORK_SUBNET: ${DOCKER_NETWORK_SUBNET}" -# Test function for subnet calculation -test_subnet_calculation() { - local test_slot_id=$1 - local expected_second_octet=$2 - local expected_third_octet=$3 +# Check if the first argument is "test" +if [ "$1" = "test" ]; then + echo "Running subnet calculation tests..." + + # Test function for subnet calculation + test_subnet_calculation() { + local test_slot_id=$1 + local expected_third_octet=$2 + + SLOT_ID=$test_slot_id + SUBNET_THIRD_OCTET=$((SLOT_ID % 256)) + SUBNET="172.18.${SUBNET_THIRD_OCTET}.0/24" + + if [ $SUBNET_THIRD_OCTET -eq $expected_third_octet ]; then + echo "Test passed for SLOT_ID $test_slot_id: $SUBNET" + else + echo "Test failed for SLOT_ID $test_slot_id: Expected 172.18.$expected_third_octet.0/24, got $SUBNET" + fi + } - SLOT_ID=$test_slot_id - SUBNET_SECOND_OCTET=$((16 + (SLOT_ID / 256) % 240)) - SUBNET_THIRD_OCTET=$((SLOT_ID % 256)) - SUBNET="172.${SUBNET_SECOND_OCTET}.${SUBNET_THIRD_OCTET}.0/24" + # Run test cases + test_subnet_calculation 0 0 + test_subnet_calculation 1 1 + test_subnet_calculation 99 99 + test_subnet_calculation 100 100 + test_subnet_calculation 255 255 + test_subnet_calculation 256 0 + + echo "Subnet calculation tests completed." + exit 0 +fi + +# check if ufw command exists +if [ -x "$(command -v ufw)" ]; then + # delete old blanket allow rule + ufw delete allow $LOCAL_COLLECTOR_PORT >> /dev/null + ufw allow from $DOCKER_NETWORK_SUBNET to any port $LOCAL_COLLECTOR_PORT + if [ $? -eq 0 ]; then + echo "ufw allow rule added for local collector port ${LOCAL_COLLECTOR_PORT} to allow connections from ${DOCKER_NETWORK_SUBNET}.\n" + else + echo "ufw firewall allow rule could not added for local collector port ${LOCAL_COLLECTOR_PORT} \ +Please attempt to add it manually with the following command with sudo privileges: \ +sudo ufw allow from $DOCKER_NETWORK_SUBNET to any port $LOCAL_COLLECTOR_PORT \ +Then run ./build.sh again." + # exit script if ufw rule not added + exit 1 + fi +else + echo "ufw command not found, skipping firewall rule addition for local collector port ${LOCAL_COLLECTOR_PORT}. \ +If you are on a Linux VPS, please ensure that the port is open for connections from ${DOCKER_NETWORK_SUBNET} manually to ${LOCAL_COLLECTOR_PORT}." +fi - if [ $SUBNET_SECOND_OCTET -eq $expected_second_octet ] && - [ $SUBNET_THIRD_OCTET -eq $expected_third_octet ]; then - echo "Test passed for SLOT_ID $test_slot_id: $SUBNET" - else - echo "Test failed for SLOT_ID $test_slot_id: Expected 172.$expected_second_octet.$expected_third_octet.0/24, got $SUBNET" - fi -} - -# Run tests -echo "Running subnet calculation tests..." -test_subnet_calculation 1 16 0 -test_subnet_calculation 255 16 0 -test_subnet_calculation 256 16 1 -test_subnet_calculation 1000 16 3 -test_subnet_calculation 10000 16 39 -test_subnet_calculation 65535 16 255 -test_subnet_calculation 65536 17 0 -test_subnet_calculation 100000 17 134 -test_subnet_calculation 1048575 31 255 -test_subnet_calculation 1048576 16 0 - -# Add this line to run tests before the main script logic -[ "$1" = "--test" ] && exit 0 if [ -z "$OVERRIDE_DEFAULTS" ]; then echo "setting default values..."; @@ -114,14 +134,6 @@ if [ -z "$SIGNER_ACCOUNT_PRIVATE_KEY" ]; then exit 1; fi -if [ -z "$DOCKER_NETWORK_SUBNET" ]; then - echo "DOCKER_NETWORK_SUBNET not found, please set this in your .env!"; - exit 1; -fi - -echo "DOCKER NETWORK SUBNET: ${DOCKER_NETWORK_SUBNET}" -echo "DOCKER NETWORK NAME: ${DOCKER_NETWORK_NAME}" - echo "Found SOURCE RPC URL ${SOURCE_RPC_URL}" echo "Found SIGNER ACCOUNT ADDRESS ${SIGNER_ACCOUNT_ADDRESS}"; @@ -169,26 +181,6 @@ else echo "Found LOCAL_COLLECTOR_PORT ${LOCAL_COLLECTOR_PORT}"; fi -# check if ufw command exists -if [ -x "$(command -v ufw)" ]; then - # delete old blanket allow rule - ufw delete allow $LOCAL_COLLECTOR_PORT >> /dev/null - ufw allow from $DOCKER_NETWORK_SUBNET to any port $LOCAL_COLLECTOR_PORT - if [ $? -eq 0 ]; then - echo "ufw allow rule added for local collector port ${LOCAL_COLLECTOR_PORT} to allow connections from ${DOCKER_NETWORK_SUBNET}.\n" - else - echo "ufw firewall allow rule could not added for local collector port ${LOCAL_COLLECTOR_PORT} \ -Please attempt to add it manually with the following command with sudo privileges: \ -sudo ufw allow from $DOCKER_NETWORK_SUBNET to any port $LOCAL_COLLECTOR_PORT \ -Then run ./build.sh again." - # exit script if ufw rule not added - exit 1 - fi -else - echo "ufw command not found, skipping firewall rule addition for local collector port ${LOCAL_COLLECTOR_PORT}. \ -If you are on a Linux VPS, please ensure that the port is open for connections from ${DOCKER_NETWORK_SUBNET} manually to ${LOCAL_COLLECTOR_PORT}." -fi - #fetch current git branch name GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD) @@ -237,3 +229,4 @@ else docker-compose -f docker-compose.yaml $COLLECTOR_PROFILE_STRING up -V --abort-on-container-exit fi fi + diff --git a/snapshotter/tests/test_docker_subnets.py b/snapshotter/tests/test_docker_subnets.py deleted file mode 100644 index f3ee11b..0000000 --- a/snapshotter/tests/test_docker_subnets.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python3 - -import time - -def calculate_subnet(slot_id): - second_octet = 16 + (slot_id // 256) % 240 - third_octet = slot_id % 256 - return f"172.{second_octet}.{third_octet}.0/24" - -def test_unique_subnets(): - print("Testing unique subnet assignments for slot IDs 1 to 10000...") - - used_subnets = set() - collisions = 0 - - for slot_id in range(1, 10001): - subnet = calculate_subnet(slot_id) - - if subnet in used_subnets: - print(f"Collision detected: Slot ID {slot_id} maps to existing subnet {subnet}") - collisions += 1 - else: - used_subnets.add(subnet) - - if collisions == 0: - print("Test passed: All 10000 slot IDs have unique subnet assignments.") - else: - print(f"Test failed: {collisions} collisions detected.") - -if __name__ == "__main__": - start_time = time.time() - test_unique_subnets() - end_time = time.time() - print(f"Execution time: {end_time - start_time:.4f} seconds") \ No newline at end of file