Skip to content

Commit 3662f6b

Browse files
committed
Fix config so client starts correctly and connects to network
1 parent 49583d2 commit 3662f6b

File tree

1 file changed

+58
-51
lines changed

1 file changed

+58
-51
lines changed

packages/portalnetwork/examples/portalClient.ts

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import { SignableENR, ENR } from '@chainsafe/enr'
22
import { keys } from '@libp2p/crypto'
33
import { multiaddr } from '@multiformats/multiaddr'
4-
import {
5-
PortalNetwork,
6-
NetworkId,
7-
TransportLayer,
8-
BaseNetwork,
9-
HistoryNetwork,
10-
StateNetwork,
11-
} from '../../portalnetwork/src'
4+
import { PortalNetwork, NetworkId, TransportLayer, BaseNetwork } from '../../portalnetwork/src'
125
import repl from 'repl'
136
import { hexToBytes } from '@ethereumjs/util'
147
import debug, { Debugger } from 'debug'
8+
import { DEFAULT_BOOTNODES } from '../src/util/bootnodes'
159

1610
class PortalNetworkRepl {
1711
private node?: PortalNetwork
@@ -27,7 +21,7 @@ class PortalNetworkRepl {
2721

2822
this.logger = debug(this.enr.nodeId.slice(0, 5)).extend('Portal')
2923

30-
const nodeAddr = multiaddr(`/ip4/127.0.0.1/udp/${port}`)
24+
const nodeAddr = multiaddr(`/ip4/0.0.0.0/udp/${port}`)
3125
this.enr.setLocationMultiaddr(nodeAddr)
3226

3327
this.node = await PortalNetwork.create({
@@ -41,19 +35,11 @@ class PortalNetworkRepl {
4135
bindAddrs: { ip4: nodeAddr },
4236
privateKey,
4337
},
38+
bootnodes: DEFAULT_BOOTNODES.mainnet,
4439
})
4540

46-
this.historyNetwork = new HistoryNetwork({
47-
client: this.node,
48-
networkId: NetworkId.HistoryNetwork
49-
})
50-
this.stateNetwork = new StateNetwork({
51-
client: this.node,
52-
networkId: NetworkId.StateNetwork
53-
})
54-
55-
this.node.networks[NetworkId.HistoryNetwork] = this.historyNetwork
56-
this.node.networks[NetworkId.StateNetwork] = this.stateNetwork
41+
this.historyNetwork = this.node.network()['0x500b']!
42+
this.stateNetwork = this.node.network()['0x500c']!
5743

5844
await this.node.start()
5945

@@ -74,26 +60,49 @@ class PortalNetworkRepl {
7460
private setupEventListeners(): void {
7561
if (!this.node) return
7662

77-
this.node.on('SendTalkReq', (nodeId, requestId, payload) =>
78-
this.logger('Sent talk request', { nodeId, requestId, payload }))
79-
80-
this.node.on('SendTalkResp', (nodeId, requestId, payload) =>
81-
this.logger('Received talk response', { nodeId, requestId, payload }))
63+
this.node.on('SendTalkReq', (nodeId, requestId, payload) =>
64+
this.logger('Sent talk request', { nodeId, requestId, payload }),
65+
)
66+
67+
this.node.on('SendTalkResp', (nodeId, requestId, payload) =>
68+
this.logger('Received talk response', { nodeId, requestId, payload }),
69+
)
8270
}
8371

8472
private startRepl(): void {
8573
const replServer = repl.start('portal> ')
8674

75+
replServer.defineCommand('debug', {
76+
help: 'Set debug log topics (e.g. *Portal*,*uTP*)',
77+
async action(topics: string) {
78+
const context = this.context as any
79+
const portalRepl: PortalNetworkRepl = context.portalRepl
80+
portalRepl.node?.enableLog(topics)
81+
this.displayPrompt()
82+
},
83+
})
84+
85+
replServer.defineCommand('bootstrap', {
86+
help: 'Bootstrap the network',
87+
async action() {
88+
const context = this.context as any
89+
const portalRepl: PortalNetworkRepl = context.portalRepl
90+
await portalRepl.node?.bootstrap()
91+
this.displayPrompt()
92+
},
93+
})
94+
8795
replServer.defineCommand('ping', {
8896
help: 'Send ping to network (history/state)',
8997
async action(network: string) {
9098
const context = this.context as any
9199
const portalRepl: PortalNetworkRepl = context.portalRepl
92100

93101
try {
94-
const networkObj = network.toLowerCase() === 'history'
95-
? portalRepl.historyNetwork
96-
: portalRepl.stateNetwork
102+
const networkObj =
103+
network.toLowerCase() === 'history'
104+
? portalRepl.historyNetwork
105+
: portalRepl.stateNetwork
97106

98107
if (!networkObj) {
99108
portalRepl.logger(`${network} Network not initialized`)
@@ -107,7 +116,7 @@ class PortalNetworkRepl {
107116
portalRepl.logger(`Ping to ${network} network failed`, error)
108117
}
109118
this.displayPrompt()
110-
}
119+
},
111120
})
112121

113122
replServer.defineCommand('findnodes', {
@@ -117,7 +126,7 @@ class PortalNetworkRepl {
117126
const portalRepl: PortalNetworkRepl = context.portalRepl
118127

119128
const [network, enr, ...distancesStr] = args.split(' ')
120-
const distances = distancesStr.map(d => parseInt(d, 10))
129+
const distances = distancesStr.map((d) => parseInt(d, 10))
121130

122131
try {
123132
switch (network.toLowerCase()) {
@@ -144,14 +153,13 @@ class PortalNetworkRepl {
144153
console.log('Find nodes failed:', error)
145154
}
146155
this.displayPrompt()
147-
}
156+
},
148157
})
149158

150159
replServer.defineCommand('findcontent', {
151160
help: 'Find content in network (history/state) with ENR and content key. Usage: .findcontent <network> <enr> <contentKey>',
152161
async action(args: string) {
153162
try {
154-
155163
const context = this.context as any
156164
const portalRepl: PortalNetworkRepl = context.portalRepl
157165

@@ -163,9 +171,10 @@ class PortalNetworkRepl {
163171

164172
const [network, enr, contentKey] = parts
165173

166-
const networkObj = network.toLowerCase() === 'history'
167-
? portalRepl.historyNetwork
168-
: portalRepl.stateNetwork
174+
const networkObj =
175+
network.toLowerCase() === 'history'
176+
? portalRepl.historyNetwork
177+
: portalRepl.stateNetwork
169178

170179
if (!networkObj) {
171180
portalRepl.logger(`${network} Network not initialized`)
@@ -199,30 +208,32 @@ class PortalNetworkRepl {
199208
console.log('Find content operation failed', error)
200209
}
201210
this.displayPrompt()
202-
}
211+
},
203212
})
204213

205214
replServer.defineCommand('offer', {
206215
help: 'Offer content to a specific network. Usage: .offer <network> <enr> <contentKey> <contentValue>',
207216
async action(args: string) {
208217
try {
209-
210218
const context = this.context as any
211219
const portalRepl: PortalNetworkRepl = context.portalRepl
212220

213221
const parts = args.trim().split(/\s+/)
214222

215223
if (parts.length < 4) {
216-
portalRepl.logger('Invalid arguments. Usage: .offer <network> <enr> <contentKey> <contentValue>')
224+
portalRepl.logger(
225+
'Invalid arguments. Usage: .offer <network> <enr> <contentKey> <contentValue>',
226+
)
217227
return this.displayPrompt()
218228
}
219229

220230
const [network, enr, contentKey, ...contentValueParts] = parts
221231
const contentValue = contentValueParts.join(' ')
222232

223-
const networkObj = network.toLowerCase() === 'history'
224-
? portalRepl.historyNetwork
225-
: portalRepl.stateNetwork
233+
const networkObj =
234+
network.toLowerCase() === 'history'
235+
? portalRepl.historyNetwork
236+
: portalRepl.stateNetwork
226237

227238
if (!networkObj) {
228239
portalRepl.logger(`${network} Network not initialized`)
@@ -253,14 +264,10 @@ class PortalNetworkRepl {
253264
}
254265

255266
portalRepl.logger('Sending content offer: network=%s, key=%o', network, contentKeyBytes)
256-
267+
257268
let offerResult
258269
try {
259-
offerResult = await networkObj.sendOffer(
260-
parsedEnr,
261-
contentKeyBytes,
262-
contentValueBytes,
263-
)
270+
offerResult = await networkObj.sendOffer(parsedEnr, contentKeyBytes, contentValueBytes)
264271

265272
portalRepl.logger('Content offer result: %O', offerResult)
266273
} catch (offerError) {
@@ -269,9 +276,9 @@ class PortalNetworkRepl {
269276
} catch (error) {
270277
console.log('Offer operation failed: %O', error)
271278
}
272-
279+
273280
this.displayPrompt()
274-
}
281+
},
275282
})
276283

277284
replServer.defineCommand('status', {
@@ -285,7 +292,7 @@ class PortalNetworkRepl {
285292
portalRepl.logger('Node initialized:', !!portalRepl.node)
286293

287294
this.displayPrompt()
288-
}
295+
},
289296
})
290297

291298
replServer.context.portalRepl = this
@@ -308,7 +315,7 @@ async function main() {
308315
}
309316
}
310317

311-
main().catch(err => {
318+
main().catch((err) => {
312319
console.log('Unhandled error in main', err)
313320
process.exit(1)
314321
})

0 commit comments

Comments
 (0)