wakurtosis/main.star

141 lines
4.0 KiB
Plaintext
Raw Normal View History

2022-12-01 16:38:36 +00:00
IMAGE_NAME = "wakunode"
2022-12-06 10:43:00 +00:00
WAKU_RPC_PORT_ID = "rpc"
2022-12-01 16:38:36 +00:00
TCP_PORT = 8545
2022-12-12 12:04:22 +00:00
POST_RELAY_MESSAGE = "post_waku_v2_relay_v1_message"
2022-12-01 16:38:36 +00:00
GET_WAKU_INFO_METHOD = "get_waku_v2_debug_v1_info"
CONNECT_TO_PEER_METHOD = "post_waku_v2_admin_v1_peers"
2022-12-06 10:43:00 +00:00
def create_waku_id(other_node_info):
ip = other_node_info["service"].ip_address
port = other_node_info["service"].ports["rpc"].number
node_id = other_node_info["id"]
return '["/ip4/' + str(ip) + '/tcp/' + str(port) + '/p2p/' + node_id + '"]'
2022-12-06 10:43:00 +00:00
def connect_wakunode_to_peer(service_id, port_id, other_node_info):
method = CONNECT_TO_PEER_METHOD
params = create_waku_id(other_node_info)
response = send_json_rpc(service_id, port_id, method, params)
print(response)
2022-12-12 12:04:22 +00:00
def send_waku_message(service_id, topic):
topic = topic
waku_message = '{"payload": "0x1a2b3c4d5e6f", "timestamp": 1626813243}'
params = '"' + topic + '"' + ", " + waku_message
2022-12-12 12:04:22 +00:00
response = send_json_rpc(service_id, WAKU_RPC_PORT_ID, POST_RELAY_MESSAGE, params)
print(response)
2022-12-06 10:43:00 +00:00
def send_json_rpc(service_id, port_id, method, params):
2022-12-01 16:38:36 +00:00
recipe = struct(
service_id=service_id,
port_id=port_id,
endpoint="",
method="POST",
content_type="application/json",
2022-12-06 10:43:00 +00:00
body='{ "jsonrpc": "2.0", "method": "' + method + '", "params": [' + params + '], "id": 1}'
2022-12-01 16:38:36 +00:00
)
response = get_value(recipe=recipe)
2022-12-06 10:43:00 +00:00
return response
def get_wakunode_id(service_id, port_id):
response = send_json_rpc(service_id, port_id, GET_WAKU_INFO_METHOD, "")
2022-12-12 12:04:22 +00:00
result = extract(response.body, '.result.listenAddresses | .[0] | split("/") | .[-1]')
print(result)
2022-12-06 10:43:00 +00:00
2022-12-12 12:04:22 +00:00
return result
2022-12-06 10:43:00 +00:00
2022-12-01 16:38:36 +00:00
def instantiate_waku_nodes(waku_topology):
2022-12-01 16:38:36 +00:00
services = {}
# Get up all waku nodes
for wakunode_name in waku_topology.keys():
CONFIG_LOCATION = "/tmp"
2022-12-12 12:04:22 +00:00
artifact_id = upload_files(
src="github.com/logos-co/wakurtosis/kurtosis-module/starlark/config_files/" + wakunode_name + ".toml"
)
2022-12-01 16:38:36 +00:00
waku_service = add_service(
service_id=wakunode_name,
config=struct(
image=IMAGE_NAME,
2022-12-06 10:43:00 +00:00
ports={WAKU_RPC_PORT_ID: struct(number=TCP_PORT, protocol="TCP")},
2022-12-12 12:04:22 +00:00
files={
artifact_id: CONFIG_LOCATION
},
2022-12-01 16:38:36 +00:00
entrypoint=[
"/usr/bin/wakunode", "--rpc-address=0.0.0.0"
],
cmd=[
"--topics='" + decoded[wakunode_name]["topics"] + "'",
"--config-file=" + CONFIG_LOCATION + "/" + wakunode_name + ".toml"
2022-12-01 16:38:36 +00:00
]
)
)
2022-12-06 10:43:00 +00:00
waku_info = {}
id = get_wakunode_id(wakunode_name, WAKU_RPC_PORT_ID)
waku_info["id"] = id
waku_info["service"] = waku_service
services[wakunode_name] = waku_info
return services
def interconnect_waku_nodes(topology_information, services):
2022-12-01 16:38:36 +00:00
# Interconnect them
for wakunode_name in topology_information.keys():
peers = topology_information[wakunode_name]["static_nodes"]
# todo: change to do only one rpc call
for peer in peers:
connect_wakunode_to_peer(wakunode_name, WAKU_RPC_PORT_ID, services[peer])
def send_test_messages(topology_information):
for wakunode_name in topology_information.keys():
# send message in topic
send_waku_message(wakunode_name, "test")
def run(args):
contents = read_file(src="github.com/logos-co/wakurtosis/kurtosis-module/starlark/waku_test_topology.json")
# decoded = json.decode(contents)
decoded = {
"waku_0": {
"ports_shift": 0,
"topics": "test",
"static_nodes": [
"waku_1",
]
},
"waku_1": {
"ports_shift": 1,
"topics": "test",
"static_nodes": [
"waku_0"
]
}
}
services = instantiate_waku_nodes(decoded)
2022-12-12 12:04:22 +00:00
interconnect_waku_nodes(decoded, services)
2022-12-01 16:38:36 +00:00
send_test_messages(decoded)