Skip to content

Commit

Permalink
Status messages are being passed around
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Dec 15, 2017
1 parent 0e55258 commit 1904fda
Show file tree
Hide file tree
Showing 36 changed files with 640 additions and 179 deletions.
2 changes: 2 additions & 0 deletions flow-typed/peer-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare module 'peer-book' {
constructor (): PeerBook;

put (peerInfo: PeerInfo): void;
getAll (): { [string]: PeerInfo };
getAllArray (): Array<PeerInfo>;
}

declare module.exports: typeof PeerBook;
Expand Down
1 change: 1 addition & 0 deletions packages/client-chains/src/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import type { ChainConfigType } from './types';

const path = require('path');

const assert = require('@polkadot/util/assert');
const ExtError = require('@polkadot/util/ext/error');
const isString = require('@polkadot/util/is/string');
Expand Down
1 change: 1 addition & 0 deletions packages/client-chains/src/load.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ISC, Copyright 2017 Jaco Greeff

const path = require('path');

const { loadChain } = require('./index');

describe('loadChain', () => {
Expand Down
26 changes: 0 additions & 26 deletions packages/client-p2p/example/blocklog.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/client-p2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"libp2p-tcp": "^0.11.1",
"pull-pushable": "^2.1.1",
"pull-stream": "^3.6.1",
"pump": "^2.0.0"
"pump": "^2.0.0",
"rlp": "^2.0.0"
}
}
8 changes: 4 additions & 4 deletions packages/client-p2p/src/create/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

import type { ChainConfigType$Nodes } from '@polkadot/client-chains/types';

const arrayFilter = require('@polkadot/util/array/filter');
const assert = require('@polkadot/util/assert');
const isInstanceOf = require('@polkadot/util/is/instanceOf');
const KadDHT = require('libp2p-kad-dht');
const Multicast = require('libp2p-mdns');
const Multiplex = require('libp2p-multiplex');
const Railing = require('libp2p-railing');
const TCP = require('libp2p-tcp');
const PeerInfo = require('peer-info');

// TODO: enable as required
// const Secio = require('libp2p-secio');
// const Spdy = require('libp2p-spdy');
// const WS = require('libp2p-websockets');

const arrayFilter = require('@polkadot/util/array/filter');
const assert = require('@polkadot/util/assert');
const isInstanceOf = require('@polkadot/util/is/instanceOf');

const DEFAULT_CRYPTO = []; // [Secio]
const DEFAULT_DHT = KadDHT;
const DEFAULT_MUXER = [
Expand Down
5 changes: 3 additions & 2 deletions packages/client-p2p/src/create/config.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// ISC, Copyright 2017 Jaco Greeff

const isInstanceOf = require('@polkadot/util/is/instanceOf');
const promisify = require('@polkadot/util/promisify');
const Railing = require('libp2p-railing');
const PeerInfo = require('peer-info');

const isInstanceOf = require('@polkadot/util/is/instanceOf');
const promisify = require('@polkadot/util/promisify');

const createConfig = require('./config');

describe('createConfig', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/client-p2p/src/create/listener.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

const PeerInfo = require('peer-info');

const assert = require('@polkadot/util/assert');
const isIp = require('@polkadot/util/is/ip');
const isNumber = require('@polkadot/util/is/number');
const PeerInfo = require('peer-info');

const createPeerInfo = require('./peerInfo');
const defaults = require('../defaults');
Expand Down
3 changes: 2 additions & 1 deletion packages/client-p2p/src/create/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import type { ChainConfigType } from '@polkadot/client-chains/types';
import type { P2pConfigType } from '../types';

const Libp2p = require('libp2p');

const assert = require('@polkadot/util/assert');
const isObject = require('@polkadot/util/is/object');
const Libp2p = require('libp2p');

const createConfig = require('./config');
const createListener = require('./listener');
Expand Down
6 changes: 4 additions & 2 deletions packages/client-p2p/src/create/node.spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// ISC, Copyright 2017 Jaco Greeff

const isInstanceOf = require('@polkadot/util/is/instanceOf');
const LibP2P = require('libp2p');
const PeerId = require('peer-id');
const PeerInfo = require('peer-info');

const isInstanceOf = require('@polkadot/util/is/instanceOf');

const createNode = require('./node');

describe('createNode', () => {
let origPeerInfoCreate;
let count = 0;

beforeEach(() => {
origPeerInfoCreate = PeerInfo.create;
PeerInfo.create = (callback) => {
origPeerInfoCreate(new PeerId(Buffer.from([])), callback);
origPeerInfoCreate(new PeerId(Buffer.from([count++])), callback);
};
});

Expand Down
5 changes: 3 additions & 2 deletions packages/client-p2p/src/create/peerBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

import type { ChainConfigType$Nodes } from '@polkadot/client-chains/types';

const assert = require('@polkadot/util/assert');
const PeerBook = require('peer-book');

const assert = require('@polkadot/util/assert');

const createPeerInfo = require('./peerInfo');

module.exports = async function createPeerBook (peers: ChainConfigType$Nodes = []): Promise<PeerBook> {
assert(Array.isArray(peers), 'Expected an array of peers');
assert(Array.isArray(peers), 'Expected an array of peer addresses');

const peerBook = new PeerBook();
const peerInfos = await Promise.all(
Expand Down
51 changes: 51 additions & 0 deletions packages/client-p2p/src/create/peerBook.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ISC, Copyright 2017 Jaco Greeff

const PeerBook = require('peer-book');
const PeerId = require('peer-id');
const PeerInfo = require('peer-info');

const isInstanceOf = require('@polkadot/util/is/instanceOf');

const createPeerBook = require('./peerBook');

describe('createPeerBook', () => {
let origPeerInfoCreate;
let count = 0;

beforeEach(() => {
origPeerInfoCreate = PeerInfo.create;
PeerInfo.create = (callback) => {
origPeerInfoCreate(new PeerId(Buffer.from([count++])), callback);
};
});

afterEach(() => {
PeerInfo.create = origPeerInfoCreate;
});

it('expects an array of peers', () => {
return createPeerBook('notAddrArray').catch((error) => {
expect(error.message).toMatch(/array of peer addresses/);
});
});

it('returns a PeerBook instance', async () => {
const peerBook = await createPeerBook();

expect(
isInstanceOf(peerBook, PeerBook)
).toEqual(true);
});

it('has the provided peers added', async () => {
const addresses = [
'/ip4/127.0.0.1/tcp/8888',
'/ip4/127.0.0.1/tcp/8899'
];
const peerBook = await createPeerBook(addresses);

expect(
peerBook.getAllArray()
).toHaveLength(2);
});
});
6 changes: 4 additions & 2 deletions packages/client-p2p/src/create/peerInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

import type { ChainConfigType$Nodes } from '@polkadot/client-chains/types';

const PeerInfo = require('peer-info');

const assert = require('@polkadot/util/assert');
const promisify = require('@polkadot/util/promisify');
const PeerInfo = require('peer-info');

module.exports = async function createPeerInfo (addresses: ChainConfigType$Nodes): Promise<PeerInfo> {
assert(Array.isArray(addresses), 'Expected an array of addresses');
assert(Array.isArray(addresses), 'Expected an array of network addresses');
assert(addresses.length, 'Expected at least one network address');

const peerInfo: PeerInfo = await promisify(null, PeerInfo.create);

Expand Down
59 changes: 59 additions & 0 deletions packages/client-p2p/src/create/peerInfo.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// ISC, Copyright 2017 Jaco Greeff

const PeerId = require('peer-id');
const PeerInfo = require('peer-info');

const isInstanceOf = require('@polkadot/util/is/instanceOf');

const createPeerInfo = require('./peerInfo');

describe('createPeerInfo', () => {
const addresses = [
'/ip4/127.0.0.1/tcp/8888',
'/ip4/127.0.0.1/tcp/8899'
];
let origPeerInfoCreate;
let count = 0;

beforeEach(() => {
origPeerInfoCreate = PeerInfo.create;
PeerInfo.create = (callback) => {
origPeerInfoCreate(new PeerId(Buffer.from([count++])), callback);
};
});

afterEach(() => {
PeerInfo.create = origPeerInfoCreate;
});

it('expects address Array', () => {
return createPeerInfo('notAddrArray').catch((error) => {
expect(error.message).toMatch(/array of network addresses/);
});
});

it('expects address values', () => {
return createPeerInfo([]).catch((error) => {
expect(error.message).toMatch(/one network address/);
});
});

it('returns a valid PeerInfo instance', async () => {
const peerInfo = await createPeerInfo(addresses);

expect(
isInstanceOf(peerInfo, PeerInfo)
).toEqual(true);
});

it('adds the provided addresses', async () => {
const peerInfo = await createPeerInfo(addresses);

expect(
peerInfo.multiaddrs.has(addresses[0])
).toEqual(true);
expect(
peerInfo.multiaddrs.has(addresses[1])
).toEqual(true);
});
});
40 changes: 40 additions & 0 deletions packages/client-p2p/src/message/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

const assert = require('@polkadot/util/assert');
const ExtError = require('@polkadot/util/ext/error');
const isNumber = require('@polkadot/util/is/number');
const numberToBuffer = require('@polkadot/util/number/toBuffer');

module.exports = class BaseMessage {
id: number;

constructor (id: number) {
assert(isNumber(id), 'Expected numeric id');

this.id = id;
}

decodeRlp (id: number, raw: Array<any>): void {
assert(isNumber(id), 'Expected numeric id');
assert(id === this.id, 'Expected message id to match');
assert(Array.isArray(raw), 'Expected raw message body');

this._rawDecodeRlp(raw);
}

encodeRlp (): [Buffer, Array<any>] {
return [
numberToBuffer(this.id),
this._rawEncodeRlp()
];
}

_rawDecodeRlp (raw: Array<any>): void {
throw new ExtError('Expected _rawDecodeRlp() to be implemented');
}

_rawEncodeRlp (): Array<any> {
throw new ExtError('Expected _rawEncodeRlp() to be implemented');
}
};
61 changes: 61 additions & 0 deletions packages/client-p2p/src/message/base.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// ISC, Copyright 2017 Jaco Greeff

const BaseMessage = require('./base');

describe('BaseMessage', () => {
let base;

beforeEach(() => {
base = new BaseMessage(123);
});

it('expects a numeric id', () => {
expect(
() => new BaseMessage()
).toThrow(/ numeric id/);
});

describe('decodeRlp', () => {
it('expects a numeric message id', () => {
expect(
() => base.decodeRlp()
).toThrow(/numeric id/);
});

it('expects id to match constructor id', () => {
expect(
() => base.decodeRlp(1)
).toThrow(/id to match/);
});

it('expects raw body in message', () => {
expect(
() => base.decodeRlp(123)
).toThrow(/raw message body/);
});

it('calls into _rawDecode, failing (base)', () => {
expect(
() => base.decodeRlp(123, [])
).toThrow(/Expected _rawDecodeRlp/);
});
});

describe('encodeRlp', () => {
it('calls into _rawEncode, failing (base)', () => {
expect(
() => base.encodeRlp()
).toThrow(/Expected _rawEncodeRlp/);
});

it('returns message containing id and raw (implemented)', () => {
const raw = ['something', ['else']];

base._rawEncodeRlp = () => raw;

expect(
base.encodeRlp()
).toEqual([Buffer.from([123]), raw]);
});
});
});

0 comments on commit 1904fda

Please sign in to comment.