Skip to content

Commit cd29b47

Browse files
Implement peer package to send messages between peers (#279)
* Initial implementation of class and discoving other peers * Implement peer connection and sending messages between peers * Add react app and use peer package for broadcasting * Maintain stream for each remote peer * Refactor code in peer package * Add serve package for react-app * Add readme for running react app * Add peer package readme * Add logs for events with details * Add a chat CLI using peer package (#280) * Add a flag to instantiate Peer for nodejs * Add a basic chat CLI using peer * Add a signal server arg to chat CLI * Add instructions for chat CLI * Fix typescript and ESM issues after adding peer package (#282) * Fix build issues in util package * Update eslint TS plugins * Scope react app package name * Convert cli package back to CJS and dynamically import ESM * Upgrade ts-node version * Fix tests * Setup a relay node and pubsub based discovery (#284) * Add a script to setup a relay node * Use pubsub based peer discovery * Add peer multiaddr to connection log * Catch relay node dial errors * Increase discovery interval and dial all mutiaddr * Add UI to display self peer ID and multiaddrs * Add UI for displaying live remote connections * Send js objects in peer broadcast messages * Add react-peer package for using peer in react app * Reduce disconnect frequency between peers (#287) * Restrict number of max concurrent dials per peer * Increase hop relay timeout to 1 day * Self review changes * Review changes * Increase pubsub discovery interval and add steps to create a peer id (#290) * Increase pubsub discovery interval * Disable autodial to avoid peer dials without protocol on a reconnect * Add steps to create a peer id and use for relay node * Add back dependency to run signalling server * Avoid bootstrapping and dial to relay node directly Co-authored-by: prathamesh0 <[email protected]> Co-authored-by: prathamesh0 <[email protected]>
1 parent aa4a954 commit cd29b47

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+24880
-10219
lines changed

.npmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
registry=https://git.vdb.to/api/packages/cerc-io/npm/
1+
@cerc-io:registry=https://git.vdb.to/api/packages/cerc-io/npm/

packages/address-watcher/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"@types/express": "^4.17.14",
4949
"@types/mocha": "^8.2.2",
5050
"@types/yargs": "^17.0.0",
51-
"@typescript-eslint/eslint-plugin": "^4.25.0",
52-
"@typescript-eslint/parser": "^4.25.0",
51+
"@typescript-eslint/eslint-plugin": "^5.47.1",
52+
"@typescript-eslint/parser": "^5.47.1",
5353
"chai": "^4.3.4",
5454
"eslint": "^7.27.0",
5555
"eslint-config-semistandard": "^15.0.1",

packages/cache/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"@types/debug": "^4.1.5",
3030
"@types/fs-extra": "^9.0.11",
3131
"@types/level": "^6.0.0",
32-
"@typescript-eslint/eslint-plugin": "^4.25.0",
33-
"@typescript-eslint/parser": "^4.25.0",
32+
"@typescript-eslint/eslint-plugin": "^5.47.1",
33+
"@typescript-eslint/parser": "^5.47.1",
3434
"eslint": "^7.27.0",
3535
"eslint-config-semistandard": "^15.0.1",
3636
"eslint-config-standard": "^16.0.3",

packages/cache/src/cache.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class Cache {
6969
log(`${this._name}: cache hit ${key}`);
7070

7171
return [value, true];
72-
} catch (err) {
72+
} catch (err: any) {
7373
log(`${this._name}: cache miss ${key}`);
7474

7575
if (err.notFound) {

packages/cli/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# cli
2+
3+
## chat
4+
5+
A basic CLI to pass messages between peers using `stdin`/`stdout`
6+
7+
* Install dependencies:
8+
9+
```bash
10+
yarn install
11+
```
12+
13+
* Build the `peer` package:
14+
15+
```
16+
cd packages/peer
17+
yarn build
18+
```
19+
20+
* (Optional) Run a local signalling server:
21+
22+
```bash
23+
# In packages/peer
24+
yarn signal-server
25+
```
26+
27+
* (Optional) Create and export a peer id for the relay node:
28+
29+
```bash
30+
# In packages/peer
31+
yarn create-peer --file [PEER_ID_FILE_PATH]
32+
```
33+
34+
* `file (f)`: file path to export the peer id to (json)
35+
36+
* (Optional) Run a local relay node:
37+
38+
```bash
39+
# In packages/peer
40+
yarn relay-node --signal-server [SIGNAL_SERVER_URL] --peer-id-file [PEER_ID_FILE_PATH]
41+
```
42+
43+
* `signal-server`: multiaddr of a signalling server (default: local signalling server multiaddr)
44+
* `peer-id-file`: file path for peer id to be used (json)
45+
46+
* Start the node:
47+
48+
```bash
49+
# In packages/cli
50+
yarn chat --signal-server [SIGNAL_SERVER_URL] --relay-node [RELAY_NODE_URL]
51+
```
52+
53+
* `signal-server`: multiaddr of a signalling server (default: local signalling server multiaddr)
54+
* `relay-node`: multiaddr of a hop enabled relay node
55+
56+
* The process starts reading from `stdin` and outputs messages from others peers to `stdout`.

packages/cli/package.json

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
"lint": "eslint .",
88
"build": "yarn clean && tsc && yarn copy-assets",
99
"clean": "rm -rf ./dist",
10-
"copy-assets": "copyfiles -u 1 src/**/*.gql dist/"
10+
"copy-assets": "copyfiles -u 1 src/**/*.gql dist/",
11+
"chat": "node dist/chat.js"
1112
},
1213
"dependencies": {
14+
"@cerc-io/peer": "^0.2.18",
1315
"@cerc-io/util": "^0.2.18",
1416
"@ethersproject/providers": "^5.4.4",
1517
"@graphql-tools/utils": "^9.1.1",
16-
"@ipld/dag-cbor": "^6.0.12",
18+
"@ipld/dag-cbor": "^8.0.0",
19+
"@libp2p/interface-peer-id": "^1.1.2",
1720
"apollo-server-express": "^3.11.1",
1821
"debug": "^4.3.1",
1922
"express": "^4.18.2",
@@ -24,13 +27,15 @@
2427
},
2528
"devDependencies": {
2629
"@types/express": "^4.17.14",
27-
"@typescript-eslint/eslint-plugin": "^4.25.0",
28-
"@typescript-eslint/parser": "^4.25.0",
30+
"@types/node": "16.11.7",
31+
"@typescript-eslint/eslint-plugin": "^5.47.1",
32+
"@typescript-eslint/parser": "^5.47.1",
2933
"eslint-config-semistandard": "^15.0.1",
3034
"eslint-config-standard": "^5.0.0",
3135
"eslint-plugin-import": "^2.23.3",
3236
"eslint-plugin-node": "^11.1.0",
3337
"eslint-plugin-promise": "^5.1.0",
34-
"eslint-plugin-standard": "^5.0.0"
38+
"eslint-plugin-standard": "^5.0.0",
39+
"typescript": "^4.9.4"
3540
}
3641
}

packages/cli/src/chat.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Copyright 2022 Vulcanize, Inc.
3+
//
4+
5+
import * as readline from 'readline';
6+
import { hideBin } from 'yargs/helpers';
7+
import yargs from 'yargs';
8+
9+
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183
10+
import { PeerId } from '@libp2p/interface-peer-id';
11+
12+
interface Arguments {
13+
signalServer: string;
14+
relayNode: string;
15+
}
16+
17+
async function main (): Promise<void> {
18+
const argv: Arguments = _getArgv();
19+
if (!argv.signalServer) {
20+
console.log('Using the default signalling server URL');
21+
}
22+
23+
// https://adamcoster.com/blog/commonjs-and-esm-importexport-compatibility-examples#importing-esm-into-commonjs-cjs
24+
const { Peer } = await import('@cerc-io/peer');
25+
const peer = new Peer(true);
26+
await peer.init(argv.signalServer, argv.relayNode);
27+
28+
peer.subscribeMessage((peerId: PeerId, message: string) => {
29+
console.log(`> ${peerId.toString()} > ${message}`);
30+
});
31+
32+
console.log(`Peer ID: ${peer.peerId?.toString()}`);
33+
34+
const rl = readline.createInterface({
35+
input: process.stdin,
36+
output: process.stdout
37+
});
38+
39+
rl.on('line', (input: string) => {
40+
peer.broadcastMessage(input);
41+
});
42+
43+
console.log('Reading input...');
44+
}
45+
46+
function _getArgv (): any {
47+
return yargs(hideBin(process.argv)).parserConfiguration({
48+
'parse-numbers': false
49+
}).options({
50+
signalServer: {
51+
type: 'string',
52+
describe: 'Signalling server URL'
53+
},
54+
relayNode: {
55+
type: 'string',
56+
describe: 'Relay node URL'
57+
}
58+
}).argv;
59+
}
60+
61+
main().catch(err => {
62+
console.log(err);
63+
});

packages/cli/src/export-state.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
GraphWatcherInterface,
2323
Config
2424
} from '@cerc-io/util';
25-
import * as codec from '@ipld/dag-cbor';
2625

2726
import { BaseCmd } from './base';
2827

@@ -164,6 +163,7 @@ export class ExportStateCmd {
164163
}
165164

166165
if (this._argv.exportFile) {
166+
const codec = await import('@ipld/dag-cbor');
167167
const encodedExportData = codec.encode(exportData);
168168

169169
const filePath = path.resolve(this._argv.exportFile);

packages/cli/src/import-state.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
updateEntitiesFromState,
2626
Config
2727
} from '@cerc-io/util';
28-
import * as codec from '@ipld/dag-cbor';
2928

3029
import { BaseCmd } from './base';
3130

@@ -112,6 +111,7 @@ export class ImportStateCmd {
112111
// Import data.
113112
const importFilePath = path.resolve(this._argv.importFile);
114113
const encodedImportData = fs.readFileSync(importFilePath);
114+
const codec = await import('@ipld/dag-cbor');
115115
const importData = codec.decode(Buffer.from(encodedImportData)) as any;
116116

117117
// Fill the snapshot block.

packages/cli/tsconfig.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* Basic Options */
66
// "incremental": true, /* Enable incremental compilation */
77
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
8-
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
8+
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
99
"lib": [ "ES5", "ES6", "ES2020" ], /* Specify library files to be included in the compilation. */
1010
// "allowJs": true, /* Allow javascript files to be compiled. */
1111
// "checkJs": true, /* Report errors in .js files. */
@@ -44,13 +44,11 @@
4444
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
4545

4646
/* Module Resolution Options */
47-
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
47+
"moduleResolution": "Node16", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
4848
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
4949
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
5050
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
51-
"typeRoots": [
52-
"./src/types"
53-
], /* List of folders to include type definitions from. */
51+
"typeRoots": [], /* List of folders to include type definitions from. */
5452
// "types": [], /* Type declaration files to be included in compilation. */
5553
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
5654
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */

packages/codegen/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"@openzeppelin/contracts": "^4.3.2",
4242
"@types/js-yaml": "^4.0.3",
4343
"@types/node": "^16.9.0",
44-
"@typescript-eslint/eslint-plugin": "^4.25.0",
45-
"@typescript-eslint/parser": "^4.25.0",
44+
"@typescript-eslint/eslint-plugin": "^5.47.1",
45+
"@typescript-eslint/parser": "^5.47.1",
4646
"eslint": "^7.27.0",
4747
"eslint-config-semistandard": "^15.0.1",
4848
"eslint-config-standard": "^16.0.3",

packages/codegen/src/templates/indexer-template.handlebars

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import assert from 'assert';
66
import debug from 'debug';
7-
import { DeepPartial, FindConditions, FindManyOptions } from 'typeorm';
7+
import { DeepPartial, FindConditions, FindManyOptions, ObjectLiteral } from 'typeorm';
88
import JSONbig from 'json-bigint';
99
import { ethers } from 'ethers';
1010
{{#if (subgraphPath)}}
@@ -368,7 +368,7 @@ export class Indexer implements IndexerInterface {
368368
}
369369

370370
{{#if (subgraphPath)}}
371-
async getSubgraphEntity<Entity> (
371+
async getSubgraphEntity<Entity extends ObjectLiteral> (
372372
entity: new () => Entity,
373373
id: string,
374374
block: BlockHeight,
@@ -379,7 +379,7 @@ export class Indexer implements IndexerInterface {
379379
return data;
380380
}
381381

382-
async getSubgraphEntities<Entity> (
382+
async getSubgraphEntities<Entity extends ObjectLiteral> (
383383
entity: new () => Entity,
384384
block: BlockHeight,
385385
where: { [key: string]: any } = {},

packages/codegen/src/templates/package-template.handlebars

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@
6161
"devDependencies": {
6262
"@ethersproject/abi": "^5.3.0",
6363
"@types/yargs": "^17.0.0",
64-
"@typescript-eslint/eslint-plugin": "^4.25.0",
65-
"@typescript-eslint/parser": "^4.25.0",
64+
"@typescript-eslint/eslint-plugin": "^5.47.1",
65+
"@typescript-eslint/parser": "^5.47.1",
6666
"eslint": "^7.27.0",
6767
"eslint-config-semistandard": "^15.0.1",
6868
"eslint-config-standard": "^16.0.3",
6969
"eslint-plugin-import": "^2.23.3",
7070
"eslint-plugin-node": "^11.1.0",
7171
"eslint-plugin-promise": "^5.1.0",
7272
"eslint-plugin-standard": "^5.0.0",
73-
"ts-node": "^10.0.0",
73+
"ts-node": "^10.2.1",
7474
"typescript": "^4.3.2",
7575
"copyfiles": "^2.4.1"
7676
}

packages/eden-watcher/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
"devDependencies": {
5757
"@ethersproject/abi": "^5.3.0",
5858
"@types/yargs": "^17.0.0",
59-
"@typescript-eslint/eslint-plugin": "^4.25.0",
60-
"@typescript-eslint/parser": "^4.25.0",
59+
"@typescript-eslint/eslint-plugin": "^5.47.1",
60+
"@typescript-eslint/parser": "^5.47.1",
6161
"copyfiles": "^2.4.1",
6262
"eslint": "^7.27.0",
6363
"eslint-config-semistandard": "^15.0.1",
@@ -66,7 +66,7 @@
6666
"eslint-plugin-node": "^11.1.0",
6767
"eslint-plugin-promise": "^5.1.0",
6868
"eslint-plugin-standard": "^5.0.0",
69-
"ts-node": "^10.0.0",
69+
"ts-node": "^10.2.1",
7070
"typescript": "^4.3.2"
7171
}
7272
}

packages/eden-watcher/src/indexer.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44

55
import assert from 'assert';
6-
import { DeepPartial, FindConditions, FindManyOptions } from 'typeorm';
6+
import { DeepPartial, FindConditions, FindManyOptions, ObjectLiteral } from 'typeorm';
77
import { ethers } from 'ethers';
88
import { SelectionNode } from 'graphql';
99

@@ -281,13 +281,13 @@ export class Indexer implements IndexerInterface {
281281
await this._baseIndexer.removeStates(blockNumber, kind);
282282
}
283283

284-
async getSubgraphEntity<Entity> (entity: new () => Entity, id: string, block: BlockHeight, selections: ReadonlyArray<SelectionNode> = []): Promise<any> {
284+
async getSubgraphEntity<Entity extends ObjectLiteral> (entity: new () => Entity, id: string, block: BlockHeight, selections: ReadonlyArray<SelectionNode> = []): Promise<any> {
285285
const data = await this._graphWatcher.getEntity(entity, id, this._relationsMap, block, selections);
286286

287287
return data;
288288
}
289289

290-
async getSubgraphEntities<Entity> (
290+
async getSubgraphEntities<Entity extends ObjectLiteral> (
291291
entity: new () => Entity,
292292
block: BlockHeight,
293293
where: { [key: string]: any } = {},

packages/erc20-watcher/environments/local.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
host = "127.0.0.1"
33
port = 3001
44
mode = "eth_call"
5-
kind = "lazy"
5+
kind = "active"
66

77
[metrics]
88
host = "127.0.0.1"

packages/erc20-watcher/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
"@nomiclabs/hardhat-waffle": "^2.0.1",
6363
"@openzeppelin/contracts": "^4.3.2",
6464
"@types/json-bigint": "^1.0.0",
65-
"@typescript-eslint/eslint-plugin": "^4.25.0",
66-
"@typescript-eslint/parser": "^4.25.0",
65+
"@typescript-eslint/eslint-plugin": "^5.47.1",
66+
"@typescript-eslint/parser": "^5.47.1",
6767
"eslint": "^7.27.0",
6868
"eslint-config-semistandard": "^15.0.1",
6969
"eslint-config-standard": "^16.0.3",
@@ -72,6 +72,7 @@
7272
"eslint-plugin-promise": "^5.1.0",
7373
"eslint-plugin-standard": "^5.0.0",
7474
"hardhat": "^2.3.0",
75-
"nodemon": "^2.0.7"
75+
"nodemon": "^2.0.7",
76+
"ts-node": "^10.2.1"
7677
}
7778
}

packages/erc20-watcher/src/indexer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ interface EventResult {
5454
to?: string;
5555
owner?: string;
5656
spender?: string;
57-
value?: BigInt;
57+
value?: bigint;
5858
__typename: string;
5959
}
6060
proof?: string;

0 commit comments

Comments
 (0)