Skip to content

Commit

Permalink
Refresh implementation (#155)
Browse files Browse the repository at this point in the history
* Refactor session/service/kademlia

* Move unit tests to test/unit

* Use mocharc

* Add simple mainnet bootnodes test

* Fix slow code path

* Add more informative log message

* Fix e2e test

* Run e2e tests in CI

* Fix linter error

* Fix unit tests

* Fix connectedPeers leak

* Add local e2e test to discover nodes

* Fix TALKREQ/TALKRESP

* Fix unit test

* Add a few small optimizations

* Fix linter errors

* Add comments, remove dead code

Co-authored-by: dapplion <35266934+dapplion@users.noreply.github.com>
  • Loading branch information
wemeetagain and dapplion authored Dec 13, 2021
1 parent 661eb03 commit f85ae2a
Show file tree
Hide file tree
Showing 36 changed files with 2,058 additions and 1,225 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ jobs:
- name: Check Types
run: yarn run check-types
- name: Unit tests
run: yarn test
run: yarn test:unit
- name: E2e tests
run: yarn test:e2e
4 changes: 4 additions & 0 deletions .mocharc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
colors: true
exit: true
require:
- ts-node/register
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"build": "tsc --declaration --outDir lib",
"prepublishOnly": "yarn build",
"lint": "eslint --color --ext .ts src/",
"test": "mocha -r ts-node/register 'test/**/*.test.ts'"
"test": "yarn test:unit && yarn test:e2e",
"test:unit": "mocha 'test/unit/**/*.test.ts'",
"test:e2e": "mocha 'test/unit/**/*.test.ts'"
},
"pre-push": [
"lint"
Expand Down Expand Up @@ -40,6 +42,7 @@
"@types/debug": "^4.1.5",
"@types/eslint": "^6.1.3",
"@types/ip6addr": "^0.2.0",
"@types/lru-cache": "^5.1.1",
"@types/mocha": "^8.0.3",
"@types/node": "^12.0.10",
"@types/varint": "^6.0.0",
Expand All @@ -49,6 +52,7 @@
"eslint": "^6.6.0",
"eslint-plugin-prettier": "^3.1.4",
"karma": "^4.3.0",
"lru-cache": "^6.0.0",
"mocha": "^8.3.0",
"nyc": "^14.1.1",
"prettier": "^2.0.5",
Expand Down
3 changes: 2 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ export type IDiscv5Config = ISessionConfig &
};

export const defaultConfig: IDiscv5Config = {
addrVotesToUpdateEnr: 10,
requestTimeout: 1 * 1000,
requestRetries: 1,
sessionTimeout: 86400 * 1000, // 1 day
sessionEstablishTimeout: 15 * 1000,
addrVotesToUpdateEnr: 10,
sessionCacheCapacity: 2000,
lookupParallelism: 3,
lookupRequestLimit: 3,
lookupNumResults: 16,
Expand Down
14 changes: 10 additions & 4 deletions src/enr/enr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,25 @@ export class ENR extends Map<ENRKey, ENRValue> {
if (decoded.length % 2 !== 0) {
throw new Error("Decoded ENR must have an even number of elements");
}
const [signature, seq, ...kvs] = decoded;

const [signature, seq] = decoded;
if (!signature || Array.isArray(signature)) {
throw new Error("Decoded ENR invalid signature: must be a byte array");
}
if (!seq || Array.isArray(seq)) {
throw new Error("Decoded ENR invalid sequence number: must be a byte array");
}

const obj: Record<ENRKey, ENRValue> = {};
for (let i = 0; i < kvs.length; i += 2) {
obj[kvs[i].toString()] = Buffer.from(kvs[i + 1]);
const signed: Buffer[] = [seq];
for (let i = 2; i < decoded.length; i += 2) {
const k = decoded[i];
const v = decoded[i + 1];
obj[k.toString()] = Buffer.from(v);
signed.push(k, v);
}
const enr = new ENR(obj, toBigIntBE(seq), signature);
if (!enr.verify(RLP.encode([seq, ...kvs]), signature)) {
if (!enr.verify(RLP.encode(signed), signature)) {
throw new Error("Unable to verify enr signature");
}
return enr;
Expand Down
Loading

0 comments on commit f85ae2a

Please sign in to comment.