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

Commit

Permalink
fix: update package config and add connection gater interface (#178)
Browse files Browse the repository at this point in the history
* Updates interfaces in line with implementation
* Aligns monorepo dep versions
* Configures project references
  • Loading branch information
achingbrain committed Feb 27, 2022
1 parent 85f09bf commit c6079a6
Show file tree
Hide file tree
Showing 28 changed files with 186 additions and 71 deletions.
8 changes: 4 additions & 4 deletions packages/libp2p-connection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@
"test:electron-main": "npm run test -- -t electron-main"
},
"dependencies": {
"@libp2p/interfaces": "^1.0.0",
"@multiformats/multiaddr": "^10.1.1",
"@libp2p/interfaces": "^1.3.6",
"@multiformats/multiaddr": "^10.1.5",
"err-code": "^3.0.1"
},
"devDependencies": {
"@libp2p/interface-compliance-tests": "^1.0.0",
"@libp2p/peer-id-factory": "^1.0.0",
"@libp2p/interface-compliance-tests": "^1.1.2",
"@libp2p/peer-id-factory": "^1.0.5",
"aegir": "^36.1.3",
"it-pair": "^2.0.2"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/libp2p-connection/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
],
"references": [
{
"path": "../libp2p-interfaces"
"path": "../libp2p-interface-compliance-tests"
},
{
"path": "../libp2p-peer-id-factory"
"path": "../libp2p-interfaces"
},
{
"path": "../libp2p-interface-compliance-tests"
"path": "../libp2p-peer-id-factory"
}
]
}
14 changes: 7 additions & 7 deletions packages/libp2p-interface-compliance-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@
"test:electron-main": "npm run test -- -t electron-main"
},
"dependencies": {
"@libp2p/crypto": "^0.22.2",
"@libp2p/interfaces": "^1.0.0",
"@libp2p/crypto": "^0.22.7",
"@libp2p/interfaces": "^1.3.6",
"@libp2p/logger": "^1.0.3",
"@libp2p/multistream-select": "^1.0.0",
"@libp2p/peer-id": "^1.0.0",
"@libp2p/peer-id": "^1.0.4",
"@libp2p/peer-id-factory": "^1.0.0",
"@libp2p/pubsub": "^1.1.0",
"@multiformats/multiaddr": "^10.1.1",
"abortable-iterator": "^4.0.0",
"@multiformats/multiaddr": "^10.1.5",
"abortable-iterator": "^4.0.2",
"aegir": "^36.1.3",
"any-signal": "^3.0.0",
"delay": "^5.0.0",
Expand All @@ -217,10 +217,10 @@
"it-map": "^1.0.6",
"it-ndjson": "^0.1.1",
"it-pair": "^2.0.0",
"it-pipe": "^2.0.2",
"it-pipe": "^2.0.3",
"it-pushable": "^2.0.1",
"it-stream-types": "^1.0.4",
"multiformats": "^9.4.10",
"multiformats": "^9.6.3",
"p-defer": "^4.0.0",
"p-event": "^5.0.1",
"p-limit": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
throw new Error('Method not implemented.')
}

addEventListener<U extends 'peer:connect'>(type: U, callback: ((evt: ConnectionManagerEvents[U]) => void) | { handleEvent: (evt: ConnectionManagerEvents[U]) => void } | null, options?: boolean | AddEventListenerOptions): void {
addEventListener<U extends keyof ConnectionManagerEvents>(type: U, callback: ((evt: ConnectionManagerEvents[U]) => void) | { handleEvent: (evt: ConnectionManagerEvents[U]) => void } | null, options?: boolean | AddEventListenerOptions): void {
throw new Error('Method not implemented.')
}

removeEventListener<U extends 'peer:connect'>(type: U, callback: (((evt: ConnectionManagerEvents[U]) => void) | { handleEvent: (evt: ConnectionManagerEvents[U]) => void } | null) | undefined, options?: boolean | EventListenerOptions): void {
removeEventListener<U extends keyof ConnectionManagerEvents>(type: U, callback: (((evt: ConnectionManagerEvents[U]) => void) | { handleEvent: (evt: ConnectionManagerEvents[U]) => void } | null) | undefined, options?: boolean | EventListenerOptions): void {
throw new Error('Method not implemented.')
}

Expand Down
7 changes: 5 additions & 2 deletions packages/libp2p-interface-compliance-tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
"path": "../libp2p-interfaces"
},
{
"path": "../libp2p-pubsub"
"path": "../libp2p-logger"
},
{
"path": "../libp2p-topology"
"path": "../libp2p-multistream-select"
},
{
"path": "../libp2p-peer-id"
},
{
"path": "../libp2p-peer-id-factory"
},
{
"path": "../libp2p-pubsub"
}
]
}
4 changes: 2 additions & 2 deletions packages/libp2p-interfaces/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@
"build": "tsc"
},
"dependencies": {
"@multiformats/multiaddr": "^10.1.1",
"multiformats": "^9.4.10"
"@multiformats/multiaddr": "^10.1.5",
"multiformats": "^9.6.3"
},
"devDependencies": {
"aegir": "^36.1.3"
Expand Down
96 changes: 96 additions & 0 deletions packages/libp2p-interfaces/src/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Multiaddr } from '@multiformats/multiaddr'
import type { PeerId } from '../peer-id'
import type * as Status from './status.js'
import type { Duplex } from 'it-stream-types'
import type { MultiaddrConnection } from '../transport'

export interface Timeline {
open: number
Expand Down Expand Up @@ -62,3 +63,98 @@ export interface Connection {
removeStream: (id: string) => void
close: () => Promise<void>
}

export interface ConnectionGater {
/**
* denyDialMultiaddr tests whether we're permitted to Dial the
* specified peer.
*
* This is called by the dialer.connectToPeer implementation before
* dialling a peer.
*
* Return true to prevent dialing the passed peer.
*/
denyDialPeer: (peerId: PeerId) => Promise<boolean>

/**
* denyDialMultiaddr tests whether we're permitted to dial the specified
* multiaddr for the given peer.
*
* This is called by the dialer.connectToPeer implementation after it has
* resolved the peer's addrs, and prior to dialling each.
*
* Return true to prevent dialing the passed peer on the passed multiaddr.
*/
denyDialMultiaddr: (peerId: PeerId, multiaddr: Multiaddr) => Promise<boolean>

/**
* denyInboundConnection tests whether an incipient inbound connection is allowed.
*
* This is called by the upgrader, or by the transport directly (e.g. QUIC,
* Bluetooth), straight after it has accepted a connection from its socket.
*
* Return true to deny the incoming passed connection.
*/
denyInboundConnection: (maConn: MultiaddrConnection) => Promise<boolean>

/**
* denyOutboundConnection tests whether an incipient outbound connection is allowed.
*
* This is called by the upgrader, or by the transport directly (e.g. QUIC,
* Bluetooth), straight after it has created a connection with its socket.
*
* Return true to deny the incoming passed connection.
*/
denyOutboundConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>

/**
* denyInboundEncryptedConnection tests whether a given connection, now encrypted,
* is allowed.
*
* This is called by the upgrader, after it has performed the security
* handshake, and before it negotiates the muxer, or by the directly by the
* transport, at the exact same checkpoint.
*
* Return true to deny the passed secured connection.
*/
denyInboundEncryptedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>

/**
* denyOutboundEncryptedConnection tests whether a given connection, now encrypted,
* is allowed.
*
* This is called by the upgrader, after it has performed the security
* handshake, and before it negotiates the muxer, or by the directly by the
* transport, at the exact same checkpoint.
*
* Return true to deny the passed secured connection.
*/
denyOutboundEncryptedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>

/**
* denyInboundUpgradedConnection tests whether a fully capable connection is allowed.
*
* This is called after encryption has been negotiated and the connection has been
* multiplexed, if a multiplexer is configured.
*
* Return true to deny the passed upgraded connection.
*/
denyInboundUpgradedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>

/**
* denyOutboundUpgradedConnection tests whether a fully capable connection is allowed.
*
* This is called after encryption has been negotiated and the connection has been
* multiplexed, if a multiplexer is configured.
*
* Return true to deny the passed upgraded connection.
*/
denyOutboundUpgradedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>

/**
* Used by the address book to filter passed addresses.
*
* Return true to allow storing the passed multiaddr for the passed peer.
*/
filterMultiaddrForPeer: (peer: PeerId, multiaddr: Multiaddr) => Promise<boolean>
}
5 changes: 2 additions & 3 deletions packages/libp2p-interfaces/src/content-routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { PeerId } from '../peer-id'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { CID } from 'multiformats/cid'
import type { AbortOptions } from '../index'
import type { PeerData } from '../peer-data'

export interface ContentRoutingFactory {
new (options?: any): ContentRouting
}

export interface ContentRouting {
provide: (cid: CID, options: AbortOptions) => Promise<void>
findProviders: (cid: CID, options: AbortOptions) => AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>
findProviders: (cid: CID, options: AbortOptions) => AsyncIterable<PeerData>
}

export default ContentRouting
2 changes: 2 additions & 0 deletions packages/libp2p-interfaces/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface Startable {
export interface Dialer {
dial: (peer: PeerId, options?: { signal?: AbortSignal }) => Promise<Connection>
dialProtocol: (peer: PeerId, protocol: string, options?: { signal?: AbortSignal }) => Promise<ProtocolStream>
getTokens: (count: number) => number[]
releaseToken: (token: number) => void
}

export interface Addressable {
Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p-interfaces/src/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export interface ComponentMetricsTracker {
/**
* Returns tracked metrics key by system, component, metric, value
*/
getComponentMetrics: () => Map<string, Map<string, Map<string, string>>>
getComponentMetrics: () => Map<string, Map<string, Map<string, number>>>

/**
* Update the stored metric value for the given system and component
Expand Down
6 changes: 3 additions & 3 deletions packages/libp2p-interfaces/src/peer-routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { PeerId } from '../peer-id'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { PeerData } from '../peer-data'

export interface PeerRoutingFactory {
new (options?: any): PeerRouting
}

export interface PeerRouting {
findPeer: (peerId: PeerId, options?: Object) => Promise<{ id: PeerId, multiaddrs: Multiaddr[] }>
getClosestPeers: (key: Uint8Array, options?: Object) => AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>
findPeer: (peerId: PeerId, options?: Object) => Promise<PeerData>
getClosestPeers: (key: Uint8Array, options?: Object) => AsyncIterable<PeerData>
}

export default PeerRouting
6 changes: 5 additions & 1 deletion packages/libp2p-interfaces/src/peer-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface Address {
isCertified: boolean
}

export interface AddressSorter {
(ms: Address[]): Address[]
}

export interface Peer {
/**
* Peer's peer-id instance
Expand Down Expand Up @@ -123,7 +127,7 @@ export interface AddressBook {
* Get the known multiaddrs for a given peer. All returned multiaddrs
* will include the encapsulated `PeerId` of the peer.
*/
getMultiaddrsForPeer: (peerId: PeerId, addressSorter?: (ms: Address[]) => Address[]) => Promise<Multiaddr[]>
getMultiaddrsForPeer: (peerId: PeerId, addressSorter?: AddressSorter) => Promise<Multiaddr[]>
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/libp2p-interfaces/src/registrar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface IncomingStreamData {

export interface ConnectionManagerEvents {
'peer:connect': CustomEvent<Connection>
'peer:disconnect': CustomEvent<Connection>
}

export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p-multistream-select/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"aegir": "^36.1.1",
"aegir": "^36.1.3",
"iso-random-stream": "^2.0.2",
"it-all": "^1.0.6",
"it-map": "^1.0.6",
Expand Down
10 changes: 5 additions & 5 deletions packages/libp2p-peer-id-factory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@
"build:proto:types": "pbts -o src/proto.d.ts src/proto.js"
},
"dependencies": {
"@libp2p/crypto": "^0.22.2",
"@libp2p/interfaces": "^1.0.0",
"@libp2p/peer-id": "^1.0.0",
"multiformats": "^9.4.5",
"@libp2p/crypto": "^0.22.7",
"@libp2p/interfaces": "^1.3.6",
"@libp2p/peer-id": "^1.0.4",
"multiformats": "^9.6.3",
"protobufjs": "^6.10.2",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"aegir": "^36.1.3",
"util": "^0.12.3"
"util": "^0.12.4"
}
}
4 changes: 2 additions & 2 deletions packages/libp2p-peer-id-factory/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
],
"references": [
{
"path": "../libp2p-peer-id"
"path": "../libp2p-interfaces"
},
{
"path": "../libp2p-interfaces"
"path": "../libp2p-peer-id"
}
]
}
4 changes: 2 additions & 2 deletions packages/libp2p-peer-id/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@
"test:electron-main": "npm run test -- -t electron-main"
},
"dependencies": {
"@libp2p/interfaces": "^1.0.0",
"@libp2p/interfaces": "^1.3.6",
"err-code": "^3.0.1",
"multiformats": "^9.4.5",
"multiformats": "^9.6.3",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p-peer-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"test:electron-main": "npm run test -- -t electron-main"
},
"dependencies": {
"@libp2p/interfaces": "^1.0.0"
"@libp2p/interfaces": "^1.3.6"
},
"devDependencies": {
"@libp2p/peer-id": "^1.1.3",
Expand Down
8 changes: 4 additions & 4 deletions packages/libp2p-peer-record/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@
},
"dependencies": {
"@libp2p/crypto": "^0.22.7",
"@libp2p/interfaces": "^1.2.0",
"@libp2p/logger": "^1.0.1",
"@libp2p/interfaces": "^1.3.6",
"@libp2p/logger": "^1.0.3",
"@libp2p/peer-id": "^1.0.4",
"@libp2p/utils": "^1.0.5",
"@multiformats/multiaddr": "^10.1.5",
Expand All @@ -163,8 +163,8 @@
"varint": "^6.0.0"
},
"devDependencies": {
"@libp2p/interface-compliance-tests": "^1.0.8",
"@libp2p/peer-id-factory": "^1.0.3",
"@libp2p/interface-compliance-tests": "^1.1.2",
"@libp2p/peer-id-factory": "^1.0.5",
"@types/varint": "^6.0.0",
"aegir": "^36.1.3",
"sinon": "^13.0.1"
Expand Down
Loading

0 comments on commit c6079a6

Please sign in to comment.