-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Status messages are being passed around
- Loading branch information
Showing
36 changed files
with
640 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.