diff --git a/Dockerfile.rest-traffic b/Dockerfile.rest-traffic new file mode 100644 index 0000000..f09b1e3 --- /dev/null +++ b/Dockerfile.rest-traffic @@ -0,0 +1,3 @@ +FROM python:3.9.18-alpine3.18 + +RUN pip install requests argparse \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 7ba2a88..8fbb234 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,10 +1,5 @@ version: "3.7" -x-traffic-multiple-nodes: &traffic-multiple-nodes > - ${TRAFFIC_MULTIPLE_NODES:-http://waku-simulator_nwaku_[1..10]:8545} -x-traffic-message-size-kb: &traffic-message-size-kb ${TRAFFIC_MESSAGE_SIZE_KB:-10} -x-traffic-delay-seconds: &traffic-delay-seconds ${TRAFFIC_DELAY_SECONDS:-15} - networks: simulation: driver: bridge @@ -67,16 +62,19 @@ services: - bootstrap rest-traffic: - image: python:3.9.18-alpine3.18 + build: + dockerfile: Dockerfile.rest-traffic command: > python /opt/traffic.py - -mn=${traffic-multiple-nodes} - -s=${traffic-message-size-kb} - -d=${traffic-delay-seconds} + -mn=${TRAFFIC_MULTIPLE_NODES:-http://waku-simulator_nwaku_[1..5]:8645} + -s=${TRAFFIC_MESSAGE_SIZE_KB:-10} + -d=${TRAFFIC_DELAY_SECONDS:-15} volumes: - ./traffic.py:/opt/traffic.py:Z networks: - simulation + depends_on: + - nwaku prometheus: image: prom/prometheus:latest diff --git a/run_nwaku.sh b/run_nwaku.sh index c7d73a5..2a69a73 100755 --- a/run_nwaku.sh +++ b/run_nwaku.sh @@ -2,7 +2,17 @@ IP=$(ip a | grep "inet " | grep -Fv 127.0.0.1 | sed 's/.*inet \([^/]*\).*/\1/') -echo "I am a nwaku node" +echo "My container name is: $HOSTNAME" + +FOURTH_OCTET=${IP##*.} +THIRD_OCTET="${IP%.*}"; THIRD_OCTET="${THIRD_OCTET##*.}" +NODE_INDEX=$((FOURTH_OCTET + 256 * THIRD_OCTET)) + +echo "FOURTH_OCTET $FOURTH_OCTET" +echo "THIRD_OCTET $THIRD_OCTET" +echo "NODE_INDEX $NODE_INDEX" +echo "$IP" + RETRIES=${RETRIES:=10} @@ -23,6 +33,14 @@ exec /usr/bin/wakunode\ --relay=true\ --rpc-admin=true\ --max-connections=250\ + --rpc-address=0.0.0.0\ + --rest=true\ + --rest-admin=true\ + --rest-private=true\ + --rest-address=0.0.0.0\ + #--rln-relay=true\ + #--rln-relay-dynamic=false\ + #--rln-relay-membership-index=${NODE_INDEX}\ --dns-discovery=true\ --discv5-discovery=true\ --discv5-enr-auto-update=True\ diff --git a/traffic.py b/traffic.py index ea01532..834ccea 100644 --- a/traffic.py +++ b/traffic.py @@ -3,37 +3,51 @@ import time import json import os import base64 -import argparse import sys +import urllib.parse +import requests +import argparse def send_waku_msg(node_address, kbytes, pubsub_topic, content_topic): # TODO dirty trick .replace("=", "") base64_payload = (base64.b64encode(os.urandom(kbytes*1000)).decode('ascii')).replace("=", "") - #print(base64_payload) print("size message kBytes", len(base64_payload) *(3/4)/1000, "KBytes") - #pubsub_topic = "/waku/2/default-waku/proto" - #content_topic = "xxx" - data = { - 'jsonrpc': '2.0', - 'method': 'post_waku_v2_relay_v1_message', - 'id': 1, - 'params': [pubsub_topic, {"payload": base64_payload, "contentTopic": content_topic, "ephemeral": False}] + body = { + "payload": base64_payload, + "contentTopic": content_topic, + "version": 1, # You can adjust the version as needed + #"timestamp": int(time.time()) } - print('Waku RPC: %s from %s PubSubTopic: %s, ContentTopic: %s' % (data['method'], node_address, pubsub_topic, content_topic)) + + encoded_pubsub_topic = urllib.parse.quote(pubsub_topic, safe='') + + url = f"{node_address}/relay/v1/messages/{encoded_pubsub_topic}" + headers = {'content-type': 'application/json'} + + print('Waku REST API: %s PubSubTopic: %s, ContentTopic: %s' % (url, pubsub_topic, content_topic)) s_time = time.time() - #print(data) - response = requests.post(node_address, data=json.dumps(data), headers={'content-type': 'application/json'}) - elapsed_ms = (time.time() - s_time) * 1000 - response_obj = response.json() - print('Response from %s: %s [%.4f ms.]' % (node_address, response_obj, elapsed_ms)) - return response_obj + + response = None + + try: + print("Sending request") + response = requests.post(url, json=body, headers=headers) + print("Got response", response) + except Exception as e: + print(f"Error sending request: {e}") + + if(response != None): + elapsed_ms = (time.time() - s_time) * 1000 + print(response.status_code, response.text) + print('Response from %s: status:%s content:%s [%.4f ms.]' % (node_address, \ + response.status_code, response.text, elapsed_ms)) parser = argparse.ArgumentParser(description='') # these flags are mutually exclusive, one or the other, never at once group = parser.add_mutually_exclusive_group(required=True) -group.add_argument('-sn', '--single-node', type=str, help='example: http://waku-simulator_nwaku_1:8545') -group.add_argument('-mn', '--multiple-nodes', type=str, help='example: http://waku-simulator_nwaku_[1..10]:8545') +group.add_argument('-sn', '--single-node', type=str, help='example: http://waku-simulator_nwaku_1:8645') +group.add_argument('-mn', '--multiple-nodes', type=str, help='example: http://waku-simulator_nwaku_[1..10]:8645') # rest of araguments parser.add_argument('-c', '--content-topic', type=str, help='content topic', default="my-ctopic") @@ -45,7 +59,7 @@ args = parser.parse_args() print(args) if args.single_node != None: - print("Injecting traffic to single node RPC:", args.single_node) + print("Injecting traffic to single node REST API:", args.single_node) # this simply converts from http://url_[1..5]:port to # [http://url_1:port @@ -57,17 +71,16 @@ if args.multiple_nodes: start = int(clean_range.split("..")[0]) end = int(clean_range.split("..")[1]) - print("Injecting traffic to multiple nodes RPC") + print("Injecting traffic to multiple nodes REST APIs") for i in range(start, end+1): nodes.append(node_placeholder.replace("{placeholder}", str(i))) -print("Injecting traffic to multiple nodes RPC") for node in nodes: print(node) while True: # calls are blocking - # limited by the time it takes the rpc to reply + # limited by the time it takes the REST API to reply if args.single_node != None: send_waku_msg(args.single_node, args.msg_size_kbytes, args.pubsub_topic, args.content_topic)