Skip to content

Commit

Permalink
update network simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
nibty committed Jan 25, 2024
1 parent 3f9e07a commit d77145e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 16 deletions.
56 changes: 56 additions & 0 deletions docker/networksimulator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pip3 install -r requirements.txt
> run the simulated network. Replace NUM_OF_NODES with the number of nodes you want to simulate.
```shell
# be in the docker/networksimulator directory
cd docker/networksimulator

# install deps
pip3 install -r requirements.txt

Expand All @@ -26,10 +29,63 @@ python3 generate.py 10 && docker compose up --build

Give it a few minutes for all the nodes to start up.

> Test the RPC endpoint
```shell
curl -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}' http://127.0.0.1:8545
```

Use the fakenet private keys from [../evmcore/apply_fake_genesis.go](../evmcore/apply_fake_genesis.go) to access the validator accounts and their funds

See test.py for an example of how to interact with the network.

## Lachesis Base

This is a hacky way to include a custom Lachesis base inside the docker build context.

> Rsync your lachesis base files into the `lachesis-base` directory inside go-x1. This will be copied into the docker build context.
```shell
rsync -rav --exclude .git ../lachesis-base/ lachesis-base/
```

> Update the go.mod file to point to the local lachesis-base directory
```shell
echo "replace github.com/Fantom-foundation/lachesis-base => ./lachesis-base" >> go.mod
```

> Now build the docker image as usual
```shell
cd docker/networksimulator

# Example: build and run a network of 10 nodes
python3 generate.py 10 && docker compose up --build
```

## Include a Custom SFC contract

The custom SFC contract will be built and included in the genesis block of the simulated network.

Note: The genesis will no longer be compatible with the testnet genesis.

```shell
# clone the sfc contract repo into the parent directory of go-x1
# so both repos are in the same directory
cd ../../..
git clone [email protected]:nibty/x1-sfc.git

cd x1-sfc
# make any changes to the SFC contract here

# run go generate ./... inside the go-x1 directory
# This will build the SFC contract and generate the go bindings.
# This requires Docker, takes a long time, and will not exit with ctrl-c, so be careful.
cd ../go-x1
go generate ./...
```

> Now build the docker image as usual
```shell
cd docker/networksimulator

# Example: build and run a network of 10 nodes
python3 generate.py 10 && docker compose up --build
```
30 changes: 20 additions & 10 deletions docker/networksimulator/generate.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import jinja2
import ipaddress
import sys
import argparse

ip_base = '172.16.239.0'
starting_port = 6050
DEFAULT_IP_BASE = '172.16.239.0'
DEFAULT_NUM_NODES = 3
DEFAULT_STARTING_PORT = 7050
DEFAULT_FLAGS = "--testnet --http --http.port 8545 --http.addr 0.0.0.0 --http.vhosts '*' --http.corsdomain '*' --ws --ws.addr 0.0.0.0 --ws.port 8546 --ws.origins '*' --verbosity 3 "
BOOT_NODE_KEY = "4b20a091f6389ca9ee1492187cc2d775511fa1e0801bf1f787b3a14f961530b1"
BOOT_NODE_PUB_KEY = "d06482f636e5c68586215f9ab9dfda270d38bf468195fc2e767d5d74b5fc7ab4faffc46028aa360b723ce53ded022949a8a6b1c96013d8ec1771f4ed448518b4"

if len(sys.argv) > 1:
num_nodes = int(sys.argv[1])
else:
num_nodes = 3

argparser = argparse.ArgumentParser()
argparser.add_argument(type=int, dest="num_nodes", default=DEFAULT_NUM_NODES)
argparser.add_argument('--ip-base', type=str, default=DEFAULT_IP_BASE)
argparser.add_argument('--flags', type=str, default=DEFAULT_FLAGS)
argparser.add_argument('--starting-port', type=int, default=DEFAULT_STARTING_PORT)
args = argparser.parse_args()

environment = jinja2.Environment()
environment.filters['ip_address'] = ipaddress.ip_address
environment.loader = jinja2.FileSystemLoader('templates')
template = environment.get_template('docker-compose.yaml.j2')

template.stream({
"num_nodes": num_nodes,
"ip_base": ip_base,
"starting_port": starting_port,
"num_nodes": args.num_nodes,
"ip_base": args.ip_base,
"starting_port": args.starting_port,
"flags": args.flags,
"bootnode_key": BOOT_NODE_KEY,
"bootnode_pub_key": BOOT_NODE_PUB_KEY,
}).dump('docker-compose.yaml')
16 changes: 10 additions & 6 deletions docker/networksimulator/templates/docker-compose.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ services:
build:
context: ../..
dockerfile: ./docker/networksimulator/Dockerfile
command: "--fakenet 1/{{ num_nodes }} --testnet --http --http.port 8545 --http.addr 0.0.0.0 --http.vhosts '*' --http.corsdomain '*' --ws --ws.addr 0.0.0.0 --ws.port 8546 --ws.origins '*' --verbosity 3 --port {{ starting_port }} --maxpeers {{ num_nodes }} --nat 'extip:{{ ip_base | ip_address + 1 }}' --nodekeyhex a990fd697515b460482856549dbab85279e31276f9e9027f1c2d85bfb4f5bcb1"
command: "{{ flags }} --fakenet 1/{{ num_nodes }} --maxpeers {{ num_nodes }} --nat 'extip:{{ ip_base | ip_address + 1 }}' --nodekeyhex {{ bootnode_key }}"
ports:
- "{{ starting_port }}:{{ starting_port }}"
- "{{ starting_port }}:{{ starting_port }}/udp"
- "{{ starting_port }}:5050"
- "{{ starting_port }}:5050/udp"
- "{{ starting_port + 1000 }}:6060"
- "8545:8545"
- "8546:8546"
networks:
Expand All @@ -30,10 +31,13 @@ services:
{% for id in range(2, num_nodes + 1) %}
node{{ id }}:
image: x1-network-simulator
command: "--fakenet {{ id }}/{{ num_nodes }} --testnet --http --http.port 8545 --http.addr 0.0.0.0 --http.vhosts '*' --http.corsdomain '*' --verbosity 3 --port {{ starting_port + id - 1 }} --bootnodes 'enode://f1f6ea0bb37434adbee2df628a95b0a46b6b6a13d21d0cfc3714cd5558f904beeaddfb29d090b1c49a8fce52ae1894273b454f3c2bdf113ac3f4f645225043ae@node1:{{ starting_port }}'"
command: "{{ flags }} --fakenet {{ id }}/{{ num_nodes }} --maxpeers {{ num_nodes }} --bootnodes 'enode://{{ bootnode_pub_key }}@{{ ip_base | ip_address + 1 }}:5050'"
ports:
- "{{ starting_port + id - 1 }}:{{ starting_port + id - 1 }}"
- "{{ starting_port + id - 1 }}:{{ starting_port + id - 1 }}/udp"
- "{{ starting_port + id - 1 }}:5050"
- "{{ starting_port + id - 1 }}:5050/udp"
- "{{ starting_port + id + 1001 }}:6060"
- "{{ starting_port + id + 2001 }}:8545"
- "{{ starting_port + id + 3001 }}:8546"
networks:
front-tier:
ipv4_address: {{ ip_base | ip_address + id }}
Expand Down

0 comments on commit d77145e

Please sign in to comment.