Skip to content

Commit 83cd4b7

Browse files
committed
chore: run with mysql
1 parent a2d5f83 commit 83cd4b7

File tree

20 files changed

+520
-224
lines changed

20 files changed

+520
-224
lines changed

.aegir.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ const WebSockets = require('libp2p-websockets')
88
const Muxer = require('libp2p-mplex')
99
const { NOISE: Crypto } = require('libp2p-noise')
1010

11+
const { isNode } = require('ipfs-utils/src/env')
12+
const delay = require('delay')
13+
const execa = require('execa')
14+
const pWaitFor = require('p-wait-for')
15+
const isCI = require('is-ci')
16+
1117
let libp2p
18+
let containerId
1219

1320
const before = async () => {
1421
// Use the last peer
@@ -36,10 +43,40 @@ const before = async () => {
3643
})
3744

3845
await libp2p.start()
46+
47+
// CI runs datastore service
48+
if (isCI || !isNode) {
49+
return
50+
}
51+
52+
const procResult = execa.commandSync('docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=test-secret-pw -e MYSQL_DATABASE=libp2p_rendezvous_db -d mysql:8 --default-authentication-plugin=mysql_native_password', {
53+
all: true
54+
})
55+
containerId = procResult.stdout
56+
57+
console.log(`wait for docker container ${containerId} to be ready`)
58+
59+
await pWaitFor(() => {
60+
const procCheck = execa.commandSync(`docker logs ${containerId}`)
61+
const logs = procCheck.stdout + procCheck.stderr // Docker/MySQL sends to the stderr the ready for connections...
62+
63+
return logs.includes('ready for connections')
64+
}, {
65+
interval: 5000
66+
})
67+
// Some more time waiting
68+
await delay(10e3)
3969
}
4070

4171
const after = async () => {
4272
await libp2p.stop()
73+
74+
if (isCI || !isNode) {
75+
return
76+
}
77+
78+
console.log('docker container is stopping')
79+
execa.commandSync(`docker stop ${containerId}`)
4380
}
4481

4582
module.exports = {

.github/workflows/main.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: ci
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
branches:
8+
- '**'
9+
10+
jobs:
11+
check:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- run: yarn
16+
- run: yarn lint
17+
- uses: gozala/[email protected]
18+
- run: yarn build
19+
- run: yarn aegir dep-check
20+
- uses: ipfs/aegir/actions/bundle-size@master
21+
name: size
22+
with:
23+
github_token: ${{ secrets.GITHUB_TOKEN }}
24+
test-node:
25+
needs: check
26+
runs-on: ${{ matrix.os }}
27+
services:
28+
mysql:
29+
image: mysql:5.7
30+
env:
31+
MYSQL_ROOT_PASSWORD: test-secret-pw
32+
MYSQL_DATABASE: libp2p_rendezvous_db
33+
ports:
34+
- 3306:3306
35+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
36+
strategy:
37+
matrix:
38+
os: [ubuntu-latest]
39+
node: [12, 14]
40+
fail-fast: true
41+
steps:
42+
- uses: actions/checkout@v2
43+
- uses: actions/setup-node@v1
44+
with:
45+
node-version: ${{ matrix.node }}
46+
- run: yarn
47+
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
48+
- uses: codecov/codecov-action@v1
49+
test-chrome:
50+
needs: check
51+
runs-on: ubuntu-latest
52+
steps:
53+
- uses: actions/checkout@v2
54+
- run: yarn
55+
- run: npx aegir test -t browser -t webworker --bail
56+
test-firefox:
57+
needs: check
58+
runs-on: ubuntu-latest
59+
steps:
60+
- uses: actions/checkout@v2
61+
- run: yarn
62+
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless

.travis.yml

Lines changed: 0 additions & 42 deletions
This file was deleted.

README.md

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
55
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
66
[![](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
7+
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-rendezvous.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-rendezvous)
8+
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-rendezvous/ci?label=ci&style=flat-square)](https://github.com/libp2p/js-libp2p-rendezvous/actions?query=branch%3Amaster+workflow%3Aci+)
79

8-
> Javascript implementation of the rendezvous protocol for libp2p
10+
> Javascript implementation of the rendezvous server protocol for libp2p
911
1012
## Lead Maintainer <!-- omit in toc -->
1113

@@ -23,7 +25,7 @@
2325

2426
## Overview
2527

26-
Libp2p rendezvous is a lightweight mechanism for generalized peer discovery. It can be used for bootstrap purposes, real time peer discovery, application specific routing, and so on. Any node implementing the rendezvous protocol can act as a rendezvous point, allowing the discovery of relevant peers in a decentralized fashion.
28+
Libp2p rendezvous is a lightweight mechanism for generalized peer discovery. It can be used for bootstrap purposes, real time peer discovery, application specific routing, and so on. This module is the implementation of the rendezvous server protocol for libp2p.
2729

2830
See the [SPEC](https://github.com/libp2p/specs/tree/master/rendezvous) for more details.
2931

@@ -35,35 +37,58 @@ See the [SPEC](https://github.com/libp2p/specs/tree/master/rendezvous) for more
3537
> npm install --global libp2p-rendezvous
3638
```
3739

38-
Now you can use the cli command `libp2p-rendezvous-server` to spawn a libp2p rendezvous server.
40+
Now you can use the cli command `libp2p-rendezvous-server` to spawn a libp2p rendezvous server. Bear in mind that a MySQL database is required to run the rendezvous server.
41+
42+
### Testing
43+
44+
For running the tests in this module, you will need to have Docker installed. A docker container is used to run a MySQL database for testing purposes.
3945

4046
### CLI
4147

42-
After installing the rendezvous server, you can use its binary. It accepts several arguments: `--peerId`, `--listenMultiaddrs`, `--announceMultiaddrs`, `--metricsPort` and `--disableMetrics`
48+
After installing the rendezvous server, you can use its binary. It accepts several arguments: `--datastoreHost`, `--datastoreUser`, `--datastorePassword`, `--datastoreDatabase`, `--enableMemoryDatabase`, `--peerId`, `--listenMultiaddrs`, `--announceMultiaddrs`, `--metricsPort` and `--disableMetrics`
4349

4450
```sh
45-
libp2p-rendezvous-server [--peerId <jsonFilePath>] [--listenMultiaddrs <ma> ... <ma>] [--announceMultiaddrs <ma> ... <ma>] [--metricsPort <port>] [--disableMetrics]
51+
libp2p-rendezvous-server [--datastoreHost <hostname>] [--datastoreUser <username>] [datastorePassword <password>] [datastoreDatabase <name>] [--enableMemoryDatabase] [--peerId <jsonFilePath>] [--listenMultiaddrs <ma> ... <ma>] [--announceMultiaddrs <ma> ... <ma>] [--metricsPort <port>] [--disableMetrics]
4652
```
4753

48-
For further customization (e.g. swapping the muxer, using other transports) it is recommended to create a server via the API.
54+
For further customization (e.g. swapping the muxer, using other transports, use other database) it is recommended to create a server via the API.
55+
56+
#### Datastore
57+
58+
A rendezvous server needs to leverage a MySQL database as a datastore for the registrations. This needs to be configured in order to run a rendezvous server. You can rely on docker to run a MySQL database using a command like:
59+
60+
```sh
61+
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=your-secret-pw -e MYSQL_DATABASE=libp2p_rendezvous_db -d mysql:8 --default-authentication-plugin=mysql_native_password
62+
```
63+
64+
Once a MySQL database is running, you can run the rendezvous server by providing the datastore configuration options as follows:
65+
66+
```sh
67+
libp2p-rendezvous-server --datastoreHost 'localhost' --datastoreUser 'root' --datastorePassword 'your-secret-pw' --datastoreDatabase 'libp2p_rendezvous_db'
68+
```
69+
70+
⚠️ For testing purposes you can skip using MySQL and use a memory datastore. This must not be used in production! For this you just need to provide the `--enableMemoryDatabase` option.
4971

5072
#### PeerId
5173

52-
You can create a [PeerId](https://github.com/libp2p/js-peer-id) via its [CLI](https://github.com/libp2p/js-peer-id#cli).
74+
You can create a [PeerId](https://github.com/libp2p/js-peer-id) via its [CLI](https://github.com/libp2p/js-peer-id#cli) and use it in the rendezvous server.
75+
76+
Once you have a generated PeerId json file, you can start the rendezvous with that PeerId by specifying its path via the `--peerId` flag:
5377

5478
```sh
55-
libp2p-rendezvous-server --peerId id.json
79+
peer-id --type=ed25519 > id.json
80+
libp2p-rendezvous-server --peerId id.json --datastoreHost 'localhost' --datastoreUser 'root' --datastorePassword 'your-secret-pw' --datastoreDatabase 'libp2p_rendezvous_db'
5681
```
5782

5883
#### Multiaddrs
5984

6085
You can specify the libp2p rendezvous server listen and announce multiaddrs. This server is configured with [libp2p-tcp](https://github.com/libp2p/js-libp2p-tcp) and [libp2p-websockets](https://github.com/libp2p/js-libp2p-websockets) and addresses with this transports should be used. It can always be modified via the API.
6186

6287
```sh
63-
libp2p-rendezvous-server --peerId id.json --listenMultiaddrs '/ip4/127.0.0.1/tcp/15002/ws' '/ip4/127.0.0.1/tcp/8000' --announceMultiaddrs '/dns4/test.io/tcp/443/wss/p2p/12D3KooWAuEpJKhCAfNcHycKcZCv9Qy69utLAJ3MobjKpsoKbrGA' '/dns6/test.io/tcp/443/wss/p2p/12D3KooWAuEpJKhCAfNcHycKcZCv9Qy69utLAJ3MobjKpsoKbrGA'
88+
libp2p-rendezvous-server --peerId id.json --listenMultiaddrs '/ip4/127.0.0.1/tcp/15002/ws' '/ip4/127.0.0.1/tcp/8000' --announceMultiaddrs '/dns4/test.io/tcp/443/wss/p2p/12D3KooWAuEpJKhCAfNcHycKcZCv9Qy69utLAJ3MobjKpsoKbrGA' '/dns6/test.io/tcp/443/wss/p2p/12D3KooWAuEpJKhCAfNcHycKcZCv9Qy69utLAJ3MobjKpsoKbrGA' --datastoreHost 'localhost' --datastoreUser 'root' --datastorePassword 'your-secret-pw' --datastoreDatabase 'libp2p_rendezvous_db'
6489
```
6590

66-
By default it listens on `/ip4/127.0.0.1/tcp/15002/ws` and has no announce multiaddrs specified.
91+
By default it listens on `/ip4/127.0.0.1/tcp/8000` and `/ip4/127.0.0.1/tcp/15003/ws`. It has no announce multiaddrs specified.
6792

6893
#### Metrics
6994

@@ -81,8 +106,30 @@ libp2p-rendezvous-server --disableMetrics
81106

82107
### Docker Setup
83108

109+
```yml
110+
version: '3.1'
111+
services:
112+
db:
113+
image: mysql
114+
volumes:
115+
- mysql-db:/var/lib/mysql
116+
command: --default-authentication-plugin=mysql_native_password
117+
restart: always
118+
environment:
119+
MYSQL_ROOT_PASSWORD: your-secret-pw
120+
MYSQL_DATABASE: libp2p_rendezvous_db
121+
ports:
122+
- "3306:3306"
123+
volumes:
124+
mysql-db:
125+
```
126+
127+
## Library
128+
84129
TODO
85130
131+
Datastores
132+
86133
## Contribute
87134
88135
Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-libp2p-rendezvous/issues)!

mysql/Dockerfile

Lines changed: 0 additions & 10 deletions
This file was deleted.

mysql/docker-compose.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ services:
77
command: --default-authentication-plugin=mysql_native_password
88
restart: always
99
environment:
10-
# MYSQL_ROOT_PASSWORD: my-secret-pw
11-
# MYSQL_USER: libp2p
12-
# MYSQL_PASSWORD: my-secret-pw
13-
# MYSQL_DATABASE: libp2p_rendezvous_db
14-
MYSQL_DATABASE: ${DATABASE}
15-
MYSQL_ROOT_PASSWORD: ${ROOT_PASSWORD}
16-
MYSQL_USER: ${USER}
17-
MYSQL_PASSWORD: ${PASSWORD}
10+
MYSQL_ROOT_PASSWORD: my-secret-pw
11+
MYSQL_USER: libp2p
12+
MYSQL_PASSWORD: my-secret-pw
13+
MYSQL_DATABASE: libp2p_rendezvous_db
14+
# MYSQL_DATABASE: ${DATABASE}
15+
# MYSQL_ROOT_PASSWORD: ${ROOT_PASSWORD}
16+
# MYSQL_USER: ${USER}
17+
# MYSQL_PASSWORD: ${PASSWORD}
1818
ports:
1919
- "3306:3306"
2020
volumes:

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"node": ">=12.0.0",
3131
"npm": ">=6.0.0"
3232
},
33+
"browser": {
34+
"mysql": false
35+
},
3336
"scripts": {
3437
"lint": "aegir lint",
3538
"build": "aegir build",
@@ -48,7 +51,7 @@
4851
"it-buffer": "^0.1.2",
4952
"it-length-prefixed": "^3.1.0",
5053
"it-pipe": "^1.1.0",
51-
"libp2p": "libp2p/js-libp2p#chore/add-typedfs-with-post-install",
54+
"libp2p": "^0.30.0",
5255
"libp2p-mplex": "^0.10.0",
5356
"libp2p-noise": "^2.0.1",
5457
"libp2p-tcp": "^0.15.1",
@@ -59,14 +62,18 @@
5962
"mysql": "^2.18.1",
6063
"peer-id": "^0.14.1",
6164
"protons": "^2.0.0",
62-
"streaming-iterables": "^5.0.2"
65+
"streaming-iterables": "^5.0.2",
66+
"uint8arrays": "^2.0.5"
6367
},
6468
"devDependencies": {
6569
"aegir": "^29.2.2",
6670
"chai": "^4.2.0",
6771
"chai-as-promised": "^7.1.1",
6872
"delay": "^4.4.0",
6973
"dirty-chai": "^2.0.1",
74+
"execa": "^5.0.0",
75+
"ipfs-utils": "^5.0.1",
76+
"is-ci": "^2.0.0",
7077
"p-defer": "^3.0.0",
7178
"p-times": "^3.0.0",
7279
"p-wait-for": "^3.1.0",

sql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
USE libp2p_rendezvous_db
2+
3+
CREATE TABLE IF NOT EXISTS registration (
4+
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
5+
namespace varchar(255) NOT NULL,
6+
peer_id varchar(255) NOT NULL,
7+
PRIMARY KEY (id),
8+
INDEX (namespace, peer_id)
9+
);
10+
11+
CREATE TABLE IF NOT EXISTS cookie (
12+
id varchar(21),
13+
namespace varchar(255),
14+
reg_id INT UNSIGNED,
15+
peer_id varchar(255) NOT NULL,
16+
created_at datetime DEFAULT CURRENT_TIMESTAMP,
17+
PRIMARY KEY (id, namespace, reg_id),
18+
INDEX (created_at)
19+
);
20+
21+
INSERT INTO registration (namespace, peer_id) VALUES ('test-ns', 'QmW8rAgaaA6sRydK1k6vonShQME47aDxaFidbtMevWs73t');
22+
23+
SELECT * FROM registration
24+
25+
SELECT * FROM cookie
26+
27+
INSERT INTO registration (namespace, peer_id) VALUES ('test-ns', 'QmZqCdSzgpsmB3Qweb9s4fojAoqELWzqku21UVrqtVSKi4');

0 commit comments

Comments
 (0)