Skip to content

Commit bed89dc

Browse files
committed
client: added peer related types
1 parent 847d0ef commit bed89dc

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

packages/client/lib/net/peer/libp2ppeer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { parseMultiaddrs } from '../../util'
55
import { Libp2pSender } from '../protocol/libp2psender'
66
import { Peer, PeerOptions } from './peer'
77
import { Libp2pNode } from './libp2pnode'
8+
import { Protocol } from '../protocol'
9+
import { Libp2pServer } from '../server'
810

911
export interface Libp2pPeerOptions extends Omit<PeerOptions, 'address' | 'transport'> {
1012
/* Multiaddrs to listen on (can be a comma separated string or list) */
@@ -73,7 +75,7 @@ export class Libp2pPeer extends Peer {
7375
* @private
7476
* @return {Promise}
7577
*/
76-
async accept(protocol: any, connection: any, server: any): Promise<void> {
78+
async accept(protocol: Protocol, connection: any, server: Libp2pServer): Promise<void> {
7779
await this.bindProtocol(protocol, new Libp2pSender(connection))
7880
this.inbound = true
7981
this.server = server
@@ -87,7 +89,7 @@ export class Libp2pPeer extends Peer {
8789
* @param {Server} [server] optional server that initiated connection
8890
* @return {Promise}
8991
*/
90-
async bindProtocols(node: any, peerInfo: any, server: any = null): Promise<void> {
92+
async bindProtocols(node: Libp2pNode, peerInfo: any, server?: Libp2pServer): Promise<void> {
9193
await Promise.all(
9294
this.protocols.map(async (p: any) => {
9395
await p.open()

packages/client/lib/net/peer/rlpxpeer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@ethereumjs/devp2p'
99
import { Protocol, RlpxSender } from '../protocol'
1010
import { Peer, PeerOptions } from './peer'
11+
import { RlpxServer } from '../server'
1112

1213
const devp2pCapabilities: any = {
1314
eth63: Devp2pETH.eth63,
@@ -140,7 +141,7 @@ export class RlpxPeer extends Peer {
140141
* @private
141142
* @return {Promise}
142143
*/
143-
async accept(rlpxPeer: Devp2pRlpxPeer, server: any): Promise<void> {
144+
async accept(rlpxPeer: Devp2pRlpxPeer, server: RlpxServer): Promise<void> {
144145
if (this.connected) {
145146
return
146147
}

packages/client/lib/net/server/libp2pserver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class Libp2pServer extends Server {
8585
return
8686
}
8787
const peer = this.createPeer(peerInfo)
88-
await peer.bindProtocols(this.node, peerInfo, this)
88+
await peer.bindProtocols(this.node as Libp2pNode, peerInfo, this)
8989
this.config.logger.debug(`Peer discovered: ${peer}`)
9090
this.emit('connected', peer)
9191
} catch (e) {

packages/client/test/net/peer/libp2ppeer.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ tape('[Libp2pPeer]', async (t) => {
5959

6060
t.test('should connect to peer', async (t) => {
6161
const config = new Config({ loglevel: 'error' })
62-
const peer = new Libp2pPeer({ config })
62+
const peer: any = new Libp2pPeer({ config })
6363
peer.bindProtocols = td.func<typeof peer['bindProtocol']>()
64-
td.when(peer.bindProtocols(td.matchers.anything(), peerInfo)).thenResolve()
64+
td.when(peer.bindProtocols(td.matchers.anything(), peerInfo)).thenResolve(null)
6565
peer.on('connected', () => {
6666
t.pass('connected')
6767
t.end()
@@ -71,9 +71,9 @@ tape('[Libp2pPeer]', async (t) => {
7171

7272
t.test('should accept peer connection', async (t) => {
7373
const config = new Config({ loglevel: 'error' })
74-
const peer = new Libp2pPeer({ config })
74+
const peer: any = new Libp2pPeer({ config })
7575
peer.bindProtocol = td.func<typeof peer['bindProtocol']>()
76-
td.when(peer.bindProtocol('proto' as any, 'conn' as any)).thenResolve()
76+
td.when(peer.bindProtocol('proto' as any, 'conn' as any)).thenResolve(null)
7777
await peer.accept('proto', 'conn', 'server')
7878
t.equals(peer.server, 'server', 'server set')
7979
t.ok(peer.inbound, 'inbound set to true')
@@ -84,12 +84,12 @@ tape('[Libp2pPeer]', async (t) => {
8484
const config = new Config({ loglevel: 'error' })
8585
const protocol = { name: 'proto', versions: [1], open: () => {} } as Protocol
8686
const badProto = { name: 'bad', versions: [1], open: () => {} } as Protocol
87-
const peer = new Libp2pPeer({ config, protocols: [protocol, badProto] })
87+
const peer: any = new Libp2pPeer({ config, protocols: [protocol, badProto] })
8888
const node = new Libp2pNode() as any
8989
peer.bindProtocol = td.func<typeof peer['bindProtocol']>()
9090
protocol.open = td.func<Protocol['open']>()
9191
badProto.open = td.func<Protocol['open']>()
92-
td.when(peer.bindProtocol(protocol, td.matchers.isA(Libp2pSender))).thenResolve()
92+
td.when(peer.bindProtocol(protocol, td.matchers.isA(Libp2pSender))).thenResolve(null)
9393
td.when(protocol.open()).thenResolve()
9494
td.when(node.asyncDialProtocol(peerInfo, '/proto/1')).thenResolve(null)
9595
td.when(node.asyncDialProtocol(peerInfo, '/bad/1')).thenReject(new Error('bad'))

packages/client/test/net/peer/rlpxpeer.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ tape('[RlpxPeer]', async (t) => {
9898

9999
t.test('should accept peer connection', async (t) => {
100100
const config = new Config({ transports: [], loglevel: 'error' })
101-
const peer = new RlpxPeer({ config, id: 'abcdef0123', host: '10.0.0.1', port: 1234 })
101+
const peer: any = new RlpxPeer({ config, id: 'abcdef0123', host: '10.0.0.1', port: 1234 })
102102
peer.bindProtocols = td.func<typeof peer['bindProtocols']>()
103-
td.when(peer.bindProtocols('rlpxpeer' as any)).thenResolve()
103+
td.when(peer.bindProtocols('rlpxpeer' as any)).thenResolve(null)
104104
await peer.accept('rlpxpeer' as any, 'server')
105105
t.equals(peer.server, 'server', 'server set')
106106
t.end()

0 commit comments

Comments
 (0)