-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathutils.ts
More file actions
192 lines (149 loc) · 6.01 KB
/
utils.ts
File metadata and controls
192 lines (149 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { peerIdFromString } from '@libp2p/peer-id'
import { CODE_P2P } from '@multiformats/multiaddr'
import { Circuit } from '@multiformats/multiaddr-matcher'
import pWaitFor from 'p-wait-for'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { RELAY_V2_HOP_CODEC } from '../../../packages/transport-circuit-relay-v2/src/constants.js'
import type { Libp2p, AbortOptions, ContentRouting, PeerId, Provider } from '@libp2p/interface'
import type { AddressManager } from '@libp2p/interface-internal'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { CID, Version } from 'multiformats'
import type { Options as PWaitForOptions } from 'p-wait-for'
export async function usingAsRelay (node: Libp2p, relay: Libp2p, opts?: PWaitForOptions<boolean>): Promise<void> {
// Wait for peer to be used as a relay
await pWaitFor(() => {
const relayAddrs = node.getMultiaddrs().filter(addr => Circuit.exactMatch(addr))
if (relayAddrs.length > 0) {
const search = `${relay.peerId.toString()}/p2p-circuit`
if (relayAddrs.find(addr => addr.toString().includes(search)) != null) {
return true
}
throw new Error('node had relay addresses that did not include the expected relay server')
}
return false
}, opts)
}
export async function usingAsRelayCount (node: Libp2p, relays: Libp2p[], count: number): Promise<void> {
// Wait for peer to be used as a relay
await pWaitFor(async () => {
let relayCount = 0
for (const relay of relays) {
for (const addr of node.getMultiaddrs()) {
const search = `${relay.peerId.toString()}/p2p-circuit`
if (addr.toString().includes(search)) {
relayCount++
}
}
}
return relayCount === count
})
}
export async function notUsingAsRelay (node: Libp2p, relay: Libp2p, opts?: PWaitForOptions<boolean>): Promise<void> {
// Wait for peer to be used as a relay
await pWaitFor(() => {
const search = `${relay.peerId.toString()}/p2p-circuit`
const relayAddrs = node.getMultiaddrs().filter(addr => addr.toString().includes(search))
return relayAddrs.length === 0
}, opts)
}
export async function hasRelay (node: Libp2p, opts?: PWaitForOptions<PeerId>): Promise<PeerId> {
let relayPeerId: PeerId | undefined
// Wait for peer to be used as a relay
await pWaitFor(() => {
const relayAddrs = node.getMultiaddrs().filter(addr => Circuit.exactMatch(addr))
if (relayAddrs.length === 0) {
return false
}
if (relayAddrs.length !== 1) {
throw new Error(`node listening on too many relays - ${relayAddrs.length}`)
}
const relayPeerIdString = relayAddrs[0].getComponents().find(c => c.code === CODE_P2P)?.value
if (relayPeerIdString == null) {
throw new Error('node had circuit relay address but address had no peer id')
}
relayPeerId = peerIdFromString(relayPeerIdString)
if (relayPeerId.equals(node.peerId)) {
throw new Error('node was listening on itself as a relay')
}
return true
}, opts)
if (relayPeerId == null) {
throw new Error('could not find relay peer id')
}
return relayPeerId
}
export async function doesNotHaveRelay (node: Libp2p, opts?: PWaitForOptions<boolean>): Promise<void> {
// Wait for peer to be used as a relay
await pWaitFor(() => {
const relayAddrs = node.getMultiaddrs().filter(addr => Circuit.exactMatch(addr))
return relayAddrs.length === 0
}, opts)
}
export async function discoveredRelayConfig (node: Libp2p, relay: Libp2p, opts?: PWaitForOptions<boolean>): Promise<void> {
await pWaitFor(async () => {
try {
const peerData = await node.peerStore.get(relay.peerId)
return peerData.protocols.includes(RELAY_V2_HOP_CODEC)
} catch {
return false
}
}, opts)
}
export function getRelayAddress (node: Libp2p): Multiaddr {
const relayAddrs = node.getMultiaddrs().filter(addr => Circuit.exactMatch(addr))
if (relayAddrs.length === 0) {
throw new Error('could not find relay address')
}
if (relayAddrs.length > 1) {
throw new Error('had too many relay addresses')
}
return relayAddrs[0]
}
export interface MockContentRoutingComponents {
peerId: PeerId
addressManager: AddressManager
}
export class MockContentRouting implements ContentRouting {
static providers = new Map<string, Provider[]>()
static data = new Map<string, Uint8Array>()
static reset (): void {
MockContentRouting.providers.clear()
MockContentRouting.data.clear()
}
private readonly peerId: PeerId
private readonly addressManager: AddressManager
constructor (components: MockContentRoutingComponents) {
this.peerId = components.peerId
this.addressManager = components.addressManager
}
async provide (cid: CID, options?: AbortOptions): Promise<void> {
let providers = MockContentRouting.providers.get(cid.toString()) ?? []
providers = providers.filter(peerInfo => !peerInfo.id.equals(this.peerId))
providers.push({
id: this.peerId,
multiaddrs: this.addressManager.getAddresses(),
routing: 'mock-content-routing'
})
MockContentRouting.providers.set(cid.toString(), providers)
}
async cancelReprovide (): Promise<void> {
}
async * findProviders (cid: CID<unknown, number, number, Version>, options?: AbortOptions | undefined): AsyncGenerator<Provider, void, undefined> {
yield * MockContentRouting.providers.get(cid.toString()) ?? []
}
async put (key: Uint8Array, value: Uint8Array, options?: AbortOptions): Promise<void> {
MockContentRouting.data.set(uint8ArrayToString(key, 'base58btc'), value)
}
async get (key: Uint8Array, options?: AbortOptions): Promise<Uint8Array> {
const value = MockContentRouting.data.get(uint8ArrayToString(key, 'base58btc'))
if (value != null) {
return Promise.resolve(value)
}
return Promise.reject(new Error('Not found'))
}
}
export function mockContentRouting (): (components: MockContentRoutingComponents) => ContentRouting {
return (components: MockContentRoutingComponents) => {
return new MockContentRouting(components)
}
}