Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(libp2p): direct connection through relay protocol (DCUtR) #1928

Merged
merged 17 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/interface-internal/src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import type { PeerId } from '@libp2p/interface/peer-id'
import type { PeerMap } from '@libp2p/peer-collections'
import type { Multiaddr } from '@multiformats/multiaddr'

export interface OpenConnectionOptions extends AbortOptions {
/**
* Connection requests with a higher priority will be executed before those
* with a lower priority. (default: 50)
*/
priority?: number

/**
* When opening a connection to a remote peer, if a connection already exists
* it will be returned instead of creating a new connection. Pass true here
* to override that and dial a new connection anyway. (default: false)
*/
force?: boolean
}

export interface ConnectionManager {
/**
* Return connections, optionally filtering by a PeerId
Expand Down Expand Up @@ -37,7 +52,7 @@ export interface ConnectionManager {
* const connection = await libp2p.connectionManager.openConnection(peerId)
* ```
*/
openConnection: (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions) => Promise<Connection>
openConnection: (peer: PeerId | Multiaddr | Multiaddr[], options?: OpenConnectionOptions) => Promise<Connection>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could probably do this as a separate PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's needed to pass the force option to openConnection when we have an existing transient connection.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, we could make this change in a smaller PR and get the code out prior to the whole DCUtR. it's beneficial in it's own right.


/**
* Close our connections to a peer
Expand Down
8 changes: 7 additions & 1 deletion packages/libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
"types": "./dist/src/circuit-relay/index.d.ts",
"import": "./dist/src/circuit-relay/index.js"
},
"./dcutr": {
"types": "./dist/src/dcutr/index.d.ts",
"import": "./dist/src/dcutr/index.js"
},
"./fetch": {
"types": "./dist/src/fetch/index.d.ts",
"import": "./dist/src/fetch/index.js"
Expand Down Expand Up @@ -100,7 +104,8 @@
"build": "aegir build",
"generate": "run-s generate:proto:*",
"generate:proto:autonat": "protons ./src/autonat/pb/index.proto",
"generate:proto:circuit": "protons ./src/circuit/pb/index.proto",
"generate:proto:circuit-relay": "protons ./src/circuit-relay/pb/index.proto",
"generate:proto:dcutr": "protons ./src/dcutr/pb/message.proto",
"generate:proto:fetch": "protons ./src/fetch/pb/proto.proto",
"generate:proto:identify": "protons ./src/identify/pb/message.proto",
"generate:proto:plaintext": "protons ./src/insecure/pb/proto.proto",
Expand Down Expand Up @@ -129,6 +134,7 @@
"@libp2p/utils": "^4.0.1",
"@multiformats/mafmt": "^12.1.2",
"@multiformats/multiaddr": "^12.1.3",
"@multiformats/multiaddr-matcher": "^1.0.0",
"abortable-iterator": "^5.0.1",
"any-signal": "^4.1.1",
"datastore-core": "^9.0.1",
Expand Down
1 change: 0 additions & 1 deletion packages/libp2p/src/connection-manager/auto-dial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ export class AutoDial implements Startable {

log('connecting to a peerStore stored peer %p', peer.id)
await this.connectionManager.openConnection(peer.id, {
// @ts-expect-error needs adding to the ConnectionManager interface
priority: this.autoDialPriority
})
}, {
Expand Down
8 changes: 2 additions & 6 deletions packages/libp2p/src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { Metrics } from '@libp2p/interface/metrics'
import type { PeerId } from '@libp2p/interface/peer-id'
import type { Peer, PeerStore } from '@libp2p/interface/peer-store'
import type { Startable } from '@libp2p/interface/startable'
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
import type { ConnectionManager, OpenConnectionOptions } from '@libp2p/interface-internal/connection-manager'
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'

const log = logger('libp2p:connection-manager')
Expand Down Expand Up @@ -154,10 +154,6 @@ export interface DefaultConnectionManagerComponents {
events: EventEmitter<Libp2pEvents>
}

export interface OpenConnectionOptions extends AbortOptions {
priority?: number
}

/**
* Responsible for managing known connections.
*/
Expand Down Expand Up @@ -492,7 +488,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {

const { peerId } = getPeerAddress(peerIdOrMultiaddr)

if (peerId != null) {
if (peerId != null && options.force !== true) {
log('dial %p', peerId)
const existingConnections = this.getConnections(peerId)

Expand Down
Loading
Loading