Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Use peerInfoSchema for RPC communication that allows custom
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Jul 6, 2020
1 parent 3c7eb1a commit 71ab461
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 53 deletions.
30 changes: 23 additions & 7 deletions elements/lisk-p2p/src/p2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ import {
} from './schema';

const createRPCSchemas = (customRPCSchemas: RPCSchemas): RPCSchemas => ({
peerInfo: mergeCustomSchema(peerInfoSchema, customRPCSchemas.peerInfo),
nodeInfo: mergeCustomSchema(nodeInfoSchema, customRPCSchemas.nodeInfo),
peerInfo: mergeCustomSchema(
peerInfoSchema,
customRPCSchemas.peerInfo ?? peerInfoSchema,
),
nodeInfo: mergeCustomSchema(
nodeInfoSchema,
customRPCSchemas.nodeInfo ?? nodeInfoSchema,
),
});

const createPeerPoolConfig = (
Expand Down Expand Up @@ -833,11 +839,21 @@ export class P2P extends EventEmitter {
.filter(
peer => !(peer.internalState && !peer.internalState.advertiseAddress),
)
.map(peer => ({
ipAddress: peer.ipAddress,
port: peer.port,
...peer.sharedState,
}));
.map(peer => {
// If custom fields are available then share them
if (peer.sharedState?.options) {
return {
options: peer.sharedState.options,
ipAddress: peer.ipAddress,
port: peer.port,
};
}

return {
ipAddress: peer.ipAddress,
port: peer.port,
};
});

const encodedPeersList = sanitizedPeerInfoList.map(peer =>
codec.encode(this._rpcSchemas.peerInfo, peer).toString('base64'),
Expand Down
31 changes: 1 addition & 30 deletions elements/lisk-p2p/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,8 @@ export const nodeInfoSchema = {
required: ['networkId', 'networkVersion', 'nonce'],
};

export const protocolPeerInfoSchema = {
$id: '/protocolPeerInfo',
type: 'object',
properties: {
ipAddress: {
dataType: 'string',
fieldNumber: 1,
},
port: {
dataType: 'uint32',
fieldNumber: 2,
},
},
required: ['ipAddress', 'port'],
};

export const peerInfoSchema = {
$id: '/peerInfo',
$id: '/protocolPeerInfo',
type: 'object',
properties: {
ipAddress: {
Expand All @@ -66,26 +50,13 @@ export const peerInfoSchema = {
dataType: 'uint32',
fieldNumber: 2,
},
networkId: {
dataType: 'string',
fieldNumber: 3,
},
networkVersion: {
dataType: 'string',
fieldNumber: 4,
},
nonce: {
dataType: 'string',
fieldNumber: 5,
},
},
required: ['ipAddress', 'port'],
};

export const defaultRPCSchemas = {
peerInfo: peerInfoSchema,
nodeInfo: nodeInfoSchema,
protocolPeerInfo: protocolPeerInfoSchema,
};

export const mergeCustomSchema = (
Expand Down
4 changes: 2 additions & 2 deletions elements/lisk-p2p/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ export interface IncomingPeerConnection {
}

export interface RPCSchemas {
peerInfo: Schema;
nodeInfo: Schema;
readonly peerInfo: Schema;
readonly nodeInfo: Schema;
}

export interface P2PConfig {
Expand Down
5 changes: 3 additions & 2 deletions elements/lisk-p2p/test/unit/p2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import { P2P } from '../../src/p2p';
import { constructPeerId } from '../../src/utils';
import { wait } from '../utils/helpers';
import { customPeerInfoSchema, customNodeInfoSchema } from '../utils/schema';
import { customNodeInfoSchema } from '../utils/schema';
import { peerInfoSchema } from '../../src/schema';

describe('p2p', () => {
let p2pNode: P2P;
Expand Down Expand Up @@ -91,8 +92,8 @@ describe('p2p', () => {
let firstNode: P2P;
// console.log((mergeCustomSchema(peerInfoSchema, customPeerInfoSchema).properties as any).options)
const customRPCSchemas = {
peerInfo: customPeerInfoSchema,
nodeInfo: customNodeInfoSchema,
peerInfo: peerInfoSchema,
};

beforeEach(async () => {
Expand Down
14 changes: 2 additions & 12 deletions elements/lisk-p2p/test/unit/peer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,30 +412,21 @@ describe('peer/base', () => {
ipAddress: '1.1.1.1',
sourceAddress: '12.12.12.12',
port: 1111,
sharedState: {
networkId: '',
nonce: '',
networkVersion: '',
},
sharedState: {},
},
{
peerId: constructPeerId('2.2.2.2', 2222),
ipAddress: '2.2.2.2',
sourceAddress: '12.12.12.12',
port: 2222,
sharedState: {
networkId: '',
nonce: '',
networkVersion: '',
},
sharedState: {},
},
];
codec.addSchema(peerInfoSchema);

const encodedPeers = peers.map(peer =>
codec
.encode(peerInfoSchema, {
...peer.sharedState,
ipAddress: peer.ipAddress,
port: peer.port,
})
Expand Down Expand Up @@ -465,7 +456,6 @@ describe('peer/base', () => {
const encodedMalformedPeersList = malformedPeerList.map(peer =>
codec
.encode(peerInfoSchema, {
...peer.sharedState,
ipAddress: peer.ipAddress,
port: peer.port,
})
Expand Down

0 comments on commit 71ab461

Please sign in to comment.