-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathlocal.js
633 lines (566 loc) · 26.3 KB
/
local.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
const fs = require('fs')
const readline = require('readline')
const initAudiusLibs = require('../examples/initAudiusLibs')
const dataContractsConfig = require('../src/data-contracts/config.json')
const { distributeTokens } = require('./helpers/distributeTokens')
const { setServiceVersion, addServiceType } = require('./helpers/version')
const {
registerLocalService,
queryLocalServices,
getStakingParameters,
updateServiceDelegateOwnerWallet
} = require('./helpers/spRegistration')
const { deregisterLocalService } = require('./helpers/spRegistration')
const { getClaimInfo, fundNewClaim } = require('./helpers/claim')
const { getEthContractAccounts } = require('./helpers/utils')
// Directories within the audius-protocol repository used for development
const serviceDirectoryList = ['discovery-provider', 'creator-node']
const discProvEndpoint1 = 'http://dn1_web-server_1:5000'
const discProvEndpoint2 = 'http://dn2_web-server_1:5001'
const creatorNodeEndpoint1 = 'http://cn1_creator-node_1:4000'
const creatorNodeEndpoint2 = 'http://cn2_creator-node_1:4001'
const creatorNodeEndpoint3 = 'http://cn3_creator-node_1:4002'
const creatorNodeEndpoint4 = 'http://cn4_creator-node_1:4003'
const amountOfAuds = 2000000
const contentNodeType = 'content-node'
const contentNodeTypeMin = 200000
const contentNodeTypeMax = 10000000
const discoveryNodeType = 'discovery-node'
const discoveryNodeTypeMin = 200000
const discoveryNodeTypeMax = 7000000
const DISCOVERY_WALLET_OFFSET = 8
// try to dynamically get versions from .version.json
const serviceVersions = {}
const serviceTypesList = []
try {
serviceDirectoryList.forEach((type) => {
const typeInfo = require(`../../${type}/.version.json`)
const version = typeInfo.version
const serviceType = typeInfo.service
serviceVersions[serviceType] = version
serviceTypesList.push(serviceType)
})
} catch (e) {
throw new Error("Couldn't get the service versions")
}
const throwArgError = () => {
throw new Error(`missing argument - format: node local.js [
distribute,
fundclaim,
getclaim,
stakeinfo,
setversion,
register-sps,
deregister-sps,
query-sps,
init-all
]`)
}
const args = process.argv
if (args.length < 3) {
throwArgError()
}
const getEnvConfigPathsForContentNode = async ({ workspace, serviceCount }) => {
const { envPath, writePath, templatePath } = getEnvConfigPathsForService({ workspace, serviceCount })
fs.writeFileSync(writePath, '')
return { envPath, templatePath, writePath }
}
const getEnvConfigPathsForDiscoveryNode = async ({ workspace, serviceCount }) => {
const { envPath, writePath, templatePath } = getEnvConfigPathsForService({ workspace, serviceCount })
fs.copyFileSync(envPath, writePath)
return { templatePath, writePath }
}
const getEnvConfigPathsForService = ({ workspace, serviceCount }) => {
const tmpDir = `${workspace}/tmp`
const writePath = `${tmpDir}/shellEnv${serviceCount}.sh`
const dirExists = fs.existsSync(tmpDir)
if (!dirExists) {
fs.mkdirSync(tmpDir)
} else {
const cleanupNeeded = fs.existsSync(writePath)
if (cleanupNeeded) {
fs.rmSync(writePath, { force: true })
}
}
const templatePath = `${workspace}/commonEnv.sh`
const envPath = `${workspace}/shellEnv${serviceCount}.sh`
return { envPath, writePath, templatePath }
}
const run = async () => {
try {
const audiusLibs = await initAudiusLibs(true)
// A separate libs instance
let userReplicaBootstrapAddressLibs
const ethWeb3 = audiusLibs.ethWeb3Manager.getWeb3()
const ethAccounts = await ethWeb3.eth.getAccounts()
switch (args[2]) {
case 'init':
console.log('initialized libs')
break
case 'distribute':
await distributeTokens(audiusLibs, amountOfAuds)
break
case 'fundclaim':
await fundNewClaim(audiusLibs, amountOfAuds)
break
case 'getclaim':
await getClaimInfo(audiusLibs)
break
case 'stakeinfo':
await getStakingParameters(audiusLibs)
break
case 'setversion':
await _initAllVersions(audiusLibs)
break
case 'configure-discprov-wallet': {
const serviceCount = args[3]
if (serviceCount === undefined) throw new Error('configure-discprov-wallet requires a service # as the second arg')
const workspace = '../discovery-provider/compose/env'
const { templatePath, writePath } = await getEnvConfigPathsForDiscoveryNode({ workspace, serviceCount })
await _configureDiscProv(ethAccounts, parseInt(serviceCount), templatePath, writePath)
break
}
case 'register-discovery-node':
const serviceCount = args[3]
if (serviceCount === undefined) throw new Error('register-discovery-node requires a service # as the second arg')
await _registerDiscProv(ethAccounts, parseInt(serviceCount))
break
case 'register-cnode': {
const serviceCount = args[3]
if (serviceCount === undefined) throw new Error('register-cnode requires a service # as the second arg')
await _registerCnode(ethAccounts, parseInt(serviceCount))
break
}
case 'deregister-cnode': {
const serviceCount = args[3]
if (serviceCount === undefined) throw new Error('deregister-cnode requires a service # as the second arg')
await _deregisterCnode(ethAccounts, parseInt(serviceCount))
break
}
case 'print-accounts': {
const numAccounts = args[3] || 20
await _printEthContractAccounts(ethAccounts, numAccounts)
break
}
case 'update-cnode-delegate-wallet': {
const serviceCount = args[3]
if (serviceCount === undefined) throw new Error('register-cnode requires a service # as the second arg')
await _updateCNodeDelegateOwnerWallet(ethAccounts, parseInt(serviceCount))
break
}
case 'deregister-sps':
await _deregisterAllSPs(audiusLibs, ethAccounts)
break
case 'query-sps':
await queryLocalServices(audiusLibs, serviceTypesList)
break
case 'query-sps-ursm':
await queryLocalServices(audiusLibs, serviceTypesList, true)
break
case 'update-cnode-config': {
// Update arbitrary cnode
const serviceCount = args[3]
if (serviceCount === undefined) throw new Error('update-delegate-wallet requires a service # as the second arg')
const workspace = '../creator-node/compose/env'
const { envPath, templatePath, writePath } = await getEnvConfigPathsForContentNode({ workspace, serviceCount })
// Local dev, delegate and owner wallet are equal
const ownerWallet = ethAccounts[parseInt(serviceCount)]
const delegateWallet = ownerWallet
const endpoint = makeCreatorNodeEndpoint(serviceCount)
await _updateCreatorNodeConfig({ ownerWallet, templatePath, writePath, endpoint, isShell: true, delegateWallet, envPath })
break
}
case 'init-all':
await _initializeLocalEnvironment(audiusLibs, ethAccounts)
break
case 'update-userreplicasetmanager-init-config':
await _updateUserReplicaSetManagerBootstrapConfig(ethAccounts)
break
case 'update-user-replica-set':
console.log('Usage: node local.js update-user-replica-set userId=1 primary=2 secondaries=3,1')
const userIdStr = args[3]
const primaryReplicaIdStr = args[4]
const secondaryReplicaIdStr = args[5]
const userId = parseInt(userIdStr.split('=')[1])
const primaryReplicaId = parseInt(primaryReplicaIdStr.split('=')[1])
let secondaryReplicaIds = (secondaryReplicaIdStr.split('=')[1])
secondaryReplicaIds = secondaryReplicaIds.split(',').map(x => parseInt(x))
console.log(`Received userId: ${userId}`)
console.log(`Received primaryReplicaId: ${primaryReplicaId}`)
console.log(`Received secondaryReplicaIds: ${secondaryReplicaIds}`)
await updateUserReplicaSet(audiusLibs, userId, primaryReplicaId, secondaryReplicaIds)
break
case 'query-user-replica-set':
console.log('Usage: node local.js query-user-replica-set userId=1')
userReplicaBootstrapAddressLibs = await getUrsmLibs(audiusLibs, 9)
const userReplicaSet = await userReplicaBootstrapAddressLibs.contracts.UserReplicaSetManagerClient.getUserReplicaSet(
parseInt(
args[3].split('=')[1]
)
)
console.log(userReplicaSet)
break
case 'query-ursm-content-node-wallet':
console.log('Usage: node local.js query-ursm-content-node-wallet spId=1')
userReplicaBootstrapAddressLibs = await getUrsmLibs(audiusLibs, 9)
const contentNodeWallet = await userReplicaBootstrapAddressLibs.contracts.UserReplicaSetManagerClient.getContentNodeWallets(
parseInt(
args[3].split('=')[1]
)
)
console.log(contentNodeWallet)
break
case 'add-l2-content-node':
console.log('Usage: node local.js add-l2-content-node spId=4 delegateWallet=0x95b6A2Be3423dF7D5774...')
const spIdStr = args[3]
const spID = parseInt(spIdStr.split('=')[1])
const delegateWalletStr = args[4]
const delegateWallet = delegateWalletStr.split('=')[1]
const ownerWallet = delegateWallet
console.log(`Configuring L2 ${spID} with wallet: ${delegateWallet}`)
// Initialize from a different acct than proxy admin
const queryLibs = await getUrsmLibs(audiusLibs, 9)
await addL2ContentNode(
queryLibs,
ethAccounts,
spID,
delegateWallet,
ownerWallet)
break
case 'register-trusted-notifier':
// hardcoded in trusted-notifier .env.dev
const wallet = '0xe82a5a2948d2b5e71232f640777346869817fbae'
const endpoint = 'http://trusted_notifier_service:8000'
const email = 'email@trusted_notifier_service:8000'
await audiusLibs.ethContracts.TrustedNotifierManagerClient.registerNotifier(
wallet,
endpoint,
email
)
break
case 'query-trusted-notifier':
const id = args[3]
if (!id) throw new Error('Please pass in a valid ID')
const resp = await audiusLibs.ethContracts.TrustedNotifierManagerClient.getNotifierForID(id)
console.log(resp)
break
default:
throwArgError()
}
process.exit(0)
} catch (e) {
throw e
}
}
run()
/*
Helper function to bootstrap additional local content nodes onto the L2 local network
Assumes 3 content nodes are already up prior to execution
*/
const addL2ContentNode = async (
audiusLibs,
ethAccounts,
newCnodeId,
newCnodeDelegateWallet,
newCnodeOwnerWallet
) => {
const incomingWallets = [newCnodeDelegateWallet, newCnodeOwnerWallet]
const existingWalletInfo = await audiusLibs.contracts.UserReplicaSetManagerClient.getContentNodeWallets(newCnodeId)
const existingWalletToCnodeIdL2 = existingWalletInfo.delegateOwnerWallet
const spIdToWalletPresent = (existingWalletToCnodeIdL2 === newCnodeDelegateWallet)
if (spIdToWalletPresent) {
console.log(`No update required! Found ${existingWalletToCnodeIdL2} for spId=${newCnodeId}, expected ${newCnodeDelegateWallet}`)
// return
}
console.log(`addL2ContentNode: ${newCnodeId}, ${newCnodeDelegateWallet}`)
const ganacheEthAccounts = await getEthContractAccounts()
// cn1, cn2, cn3 are all initialized to the 1,2,3 indexes into local ethAccounts array
// NOTE: Proposer Accounts must match the deployed bootstrap addresses on UserReplicaSetManager
// Changing any of the below indices will affect the wallets performing this update and operations will fail
const proposer1WalletIndex = 1
const proposer2WalletIndex = 2
const proposer3WalletIndex = 3
// Local service provider IDs assigned by eth-contracts
// These values just HAPPEN to coincide with the indices above but are set explicitly to avoid confusion
const proposer1SpId = 1
const proposer2SpId = 2
const proposer3SpId = 3
// Retrieve the wallet associated with each index
const proposer1Wallet = ethAccounts[parseInt(proposer1WalletIndex)]
const proposer1PKey = ganacheEthAccounts.private_keys[proposer1Wallet.toLowerCase()]
const proposer2Wallet = ethAccounts[parseInt(proposer2WalletIndex)]
const proposer2PKey = ganacheEthAccounts.private_keys[proposer2Wallet.toLowerCase()]
const proposer3Wallet = ethAccounts[parseInt(proposer3WalletIndex)]
const proposer3PKey = ganacheEthAccounts.private_keys[proposer3Wallet.toLowerCase()]
console.log(`proposer1Wallet: ${proposer1Wallet}`)
console.log(`proposer1WalletPkey: ${proposer1PKey}`)
console.log(`proposer2Wallet: ${proposer2Wallet}`)
console.log(`proposer2WalletPkey: ${proposer2PKey}`)
console.log(`proposer3Wallet: ${proposer3Wallet}`)
console.log(`proposer3WalletPkey: ${proposer3PKey}`)
// console.dir(ganacheEthAccounts)
// Initialize libs with each incoming proposer
const proposer1Libs = await initAudiusLibs(
false,
proposer1Wallet,
proposer1Wallet,
proposer1PKey
)
const proposer1EthAddress = proposer1Libs.ethWeb3Manager.getWalletAddress()
console.log(`Initialized proposer1 libs, proposer1EthAddress: ${proposer1EthAddress}`)
const proposer1SignatureInfo = await proposer1Libs.contracts.UserReplicaSetManagerClient.getProposeAddOrUpdateContentNodeRequestData(
newCnodeId,
newCnodeDelegateWallet,
newCnodeOwnerWallet,
proposer1SpId
)
console.dir(proposer1SignatureInfo, { depth: 5 })
const proposer2Libs = await initAudiusLibs(
false,
proposer2Wallet,
proposer2Wallet,
proposer2PKey
)
const proposer2EthAddress = proposer2Libs.ethWeb3Manager.getWalletAddress()
console.log(`Initialized proposer2 libs, proposer2EthAddress: ${proposer2EthAddress}`)
const proposer2SignatureInfo = await proposer2Libs.contracts.UserReplicaSetManagerClient.getProposeAddOrUpdateContentNodeRequestData(
newCnodeId,
newCnodeDelegateWallet,
newCnodeOwnerWallet,
proposer2SpId
)
console.dir(proposer2SignatureInfo, { depth: 5 })
const proposer3Libs = await initAudiusLibs(
false,
proposer3Wallet,
proposer3Wallet,
proposer3PKey
)
const proposer3EthAddress = proposer3Libs.ethWeb3Manager.getWalletAddress()
console.log(`Initialized proposer3 libs, proposer3EthAddress: ${proposer3EthAddress}`)
const proposer3SignatureInfo = await proposer3Libs.contracts.UserReplicaSetManagerClient.getProposeAddOrUpdateContentNodeRequestData(
newCnodeId,
newCnodeDelegateWallet,
newCnodeOwnerWallet,
proposer3SpId
)
console.dir(proposer3SignatureInfo, { depth: 5 })
// Generate arguments for proposal
const proposerSpIds = [proposer1SpId, proposer2SpId, proposer3SpId]
const proposerNonces = [
proposer1SignatureInfo.nonce,
proposer2SignatureInfo.nonce,
proposer3SignatureInfo.nonce
]
await audiusLibs.contracts.UserReplicaSetManagerClient.addOrUpdateContentNode(
newCnodeId,
incomingWallets,
proposerSpIds,
proposerNonces,
proposer1SignatureInfo.sig,
proposer2SignatureInfo.sig,
proposer3SignatureInfo.sig
)
}
// In order to issue operations we need a libs account initialized from a different address than
// the 0th account on local data-contracts
// This function explicitly queries the 20th account from data-contracts ganache
// Returns libs instance logged in as said account
const getUrsmLibs = async (defaultAudiusLibs, acctIndex = 20) => {
const dataWeb3 = defaultAudiusLibs.web3Manager.getWeb3()
const dataWeb3Accounts = await dataWeb3.eth.getAccounts()
const localQueryAccount = dataWeb3Accounts[acctIndex]
const ursmLibs = await initAudiusLibs(true, localQueryAccount)
return ursmLibs
}
// Update a user's replica set on chain
// Using the bootstrap address configured for local development (accounts[9])
const updateUserReplicaSet = async (
defaultAudiusLibs,
userId,
primaryId,
secondaryIds
) => {
// UserReplicaBootstrapLibs, logged in as the known bootstrap address
const userReplicaBootstrapAddressLibs = await getUrsmLibs(defaultAudiusLibs, 9)
const sp1Id = primaryId
const sp1ContentNodeWallets = await userReplicaBootstrapAddressLibs.contracts.UserReplicaSetManagerClient.getContentNodeWallets(sp1Id)
const sp1DelWal = sp1ContentNodeWallets.delegateOwnerWallet
console.log(`spId <-> delegateWallet from UserReplicaSetManager: ${sp1Id} - ${sp1DelWal}`)
const sp2Id = secondaryIds[0]
const sp2ContentNodeWallets = await userReplicaBootstrapAddressLibs.contracts.UserReplicaSetManagerClient.getContentNodeWallets(sp2Id)
const sp2DelWal = sp2ContentNodeWallets.delegateOwnerWallet
console.log(`spId <-> delegateWallet from UserReplicaSetManager: ${sp2Id} - ${sp2DelWal}`)
const sp3Id = secondaryIds[1]
const sp3ContentNodeWallets = await userReplicaBootstrapAddressLibs.contracts.UserReplicaSetManagerClient.getContentNodeWallets(sp3Id)
const sp3DelWal = sp3ContentNodeWallets.delegateOwnerWallet
console.log(`spId <-> delegateWallet from UserReplicaSetManager: ${sp3Id} - ${sp3DelWal}`)
const user1ReplicaSet = await userReplicaBootstrapAddressLibs.contracts.UserReplicaSetManagerClient.getUserReplicaSet(userId)
console.log(`User ${userId} replica set prior to update: ${JSON.stringify(user1ReplicaSet)}`)
console.log(`User ${userId} replica set updating to primary=${primaryId}, secondaries=${secondaryIds}`)
// Uncomment to perform update operation
const tx1 = await userReplicaBootstrapAddressLibs.contracts.UserReplicaSetManagerClient.updateReplicaSet(
userId,
primaryId,
secondaryIds,
user1ReplicaSet.primary,
user1ReplicaSet.secondaries
)
console.dir(tx1, { depth: 5 })
const user1ReplicaSetAfterUpdate = await userReplicaBootstrapAddressLibs.contracts.UserReplicaSetManagerClient.getUserReplicaSet(userId)
console.log(`User ${userId} replica set after to update: ${JSON.stringify(user1ReplicaSetAfterUpdate)}`)
}
const _initializeLocalEnvironment = async (audiusLibs, ethAccounts) => {
await distributeTokens(audiusLibs, amountOfAuds)
await _initEthContractTypes(audiusLibs)
await _initAllVersions(audiusLibs)
await queryLocalServices(audiusLibs, serviceTypesList)
}
const makeDiscoveryProviderEndpoint = (serviceNumber) => `http://dn${serviceNumber}_web-server_1:${5000 + parseInt(serviceNumber) - 1}`
const _registerDiscProv = async (ethAccounts, serviceNumber) => {
const audiusLibs = await initAudiusLibs(true, null, ethAccounts[DISCOVERY_WALLET_OFFSET + serviceNumber])
const endpoint = makeDiscoveryProviderEndpoint(serviceNumber)
await registerLocalService(audiusLibs, discoveryNodeType, endpoint, amountOfAuds)
}
const makeCreatorNodeEndpoint = (serviceNumber) => `http://cn${serviceNumber}_creator-node_1:${4000 + parseInt(serviceNumber) - 1}`
// Templated cnode to allow for dynamic number of services
const _registerCnode = async (ethAccounts, serviceNumber) => {
const audiusLibs = await initAudiusLibs(true, null, ethAccounts[serviceNumber])
const endpoint = makeCreatorNodeEndpoint(serviceNumber)
await registerLocalService(audiusLibs, contentNodeType, endpoint, amountOfAuds)
}
const _deregisterCnode = async (ethAccounts, serviceNumber) => {
const audiusLibs = await initAudiusLibs(true, null, ethAccounts[serviceNumber])
const endpoint = makeCreatorNodeEndpoint(serviceNumber)
await deregisterLocalService(audiusLibs, contentNodeType, endpoint)
}
const _printEthContractAccounts = async (ethAccounts, numAccounts = 20) => {
const ganacheEthAccounts = await getEthContractAccounts()
const accts = []
for (let i = 0; i < numAccounts; i += 1) {
const addr = ethAccounts[i]
const privateKey = ganacheEthAccounts.private_keys[addr.toLowerCase()]
accts.push({
address: ethAccounts[i],
privateKey
})
}
console.log(JSON.stringify(accts))
}
// NOTE - newly selected wallet is the ethAccount with index 10 + current service number
const _updateCNodeDelegateOwnerWallet = async (ethAccounts, serviceNumber) => {
const audiusLibs = await initAudiusLibs(true, null, ethAccounts[serviceNumber])
const endpoint = makeCreatorNodeEndpoint(serviceNumber)
await updateServiceDelegateOwnerWallet(audiusLibs, contentNodeType, endpoint, ethAccounts[serviceNumber + 10])
}
const _updateCreatorNodeConfig = async ({ ownerWallet, templatePath, writePath, endpoint = null, isShell = false, delegateWallet, envPath = null }) => {
delegateWallet = (delegateWallet || ownerWallet).toLowerCase()
ownerWallet = ownerWallet.toLowerCase()
const ganacheEthAccounts = await getEthContractAccounts()
// PKey is now recovered
const ownerWalletPrivKey = ganacheEthAccounts.private_keys[`${ownerWallet}`]
const delegateWalletPrivKey = ganacheEthAccounts.private_keys[`${delegateWallet}`]
await _updateCreatorNodeConfigFile({ templatePath, writePath, ownerWallet, ownerWalletPrivKey, delegateWallet, delegateWalletPrivKey, endpoint, isShell, envPath })
}
const _deregisterAllSPs = async (audiusLibs, ethAccounts) => {
const audiusLibs1 = audiusLibs
await deregisterLocalService(audiusLibs1, discoveryNodeType, discProvEndpoint1)
const audiusLibs2 = await initAudiusLibs(true, null, ethAccounts[3])
await deregisterLocalService(audiusLibs2, discoveryNodeType, discProvEndpoint2)
const audiusLibs3 = await initAudiusLibs(true, null, ethAccounts[1])
await deregisterLocalService(audiusLibs3, contentNodeType, creatorNodeEndpoint1)
const audiusLibs4 = await initAudiusLibs(true, null, ethAccounts[2])
await deregisterLocalService(audiusLibs4, contentNodeType, creatorNodeEndpoint2)
const audiusLibs5 = await initAudiusLibs(true, null, ethAccounts[4])
await deregisterLocalService(audiusLibs5, contentNodeType, creatorNodeEndpoint3)
const audiusLibs6 = await initAudiusLibs(true, null, ethAccounts[5])
await deregisterLocalService(audiusLibs6, contentNodeType, creatorNodeEndpoint4)
}
const _initAllVersions = async (audiusLibs) => {
for (const serviceType of serviceTypesList) {
await setServiceVersion(audiusLibs, serviceType, serviceVersions[serviceType])
}
}
const _initEthContractTypes = async (libs) => {
console.log(`Registering additional service type ${contentNodeType} - Min=${contentNodeTypeMin}, Max=${contentNodeTypeMax}`)
// Add content-node serviceType
await addServiceType(libs, contentNodeType, contentNodeTypeMin, contentNodeTypeMax)
console.log(`Registering additional service type ${contentNodeType} - Min=${contentNodeTypeMin}, Max=${contentNodeTypeMax}`)
// Add discovery-node serviceType
await addServiceType(libs, discoveryNodeType, discoveryNodeTypeMin, discoveryNodeTypeMax)
}
const writeEnvConfigFromTemplate = async ({ templatePath, writePath, replaceMap, envPath }) => {
let template = fs.readFileSync(templatePath, 'utf8')
const progressReport = []
Object.entries(replaceMap).forEach(([toReplace, replacement]) => {
template = template.replace(`${toReplace}`, replacement)
progressReport.push(`${toReplace}: ${replacement}`)
})
fs.appendFileSync(writePath, template)
if (envPath) {
fs.appendFileSync(writePath, fs.readFileSync(envPath, 'utf8'))
}
console.log(`Updated ${writePath}:\n${progressReport.join('\n')}`)
}
const _configureDiscProv = async (ethAccounts, serviceNumber, templatePath, writePath) => {
const ganacheEthAccounts = await getEthContractAccounts()
const discProvAccountPubkey = ethAccounts[DISCOVERY_WALLET_OFFSET + serviceNumber].toLowerCase()
const delegateWalletPrivKey = ganacheEthAccounts.private_keys[`${discProvAccountPubkey}`]
const replaceMap = {
AUDIUS_DELEGATE_OWNER_WALLET: discProvAccountPubkey,
AUDIUS_DELEGATE_PRIVATE_KEY: delegateWalletPrivKey
}
writeEnvConfigFromTemplate({ templatePath, writePath, replaceMap })
}
// Write an update to shell env file for creator nodes or docker env file
const _updateCreatorNodeConfigFile = async ({ templatePath, writePath, ownerWallet, ownerWalletPkey, delegateWallet, delegateWalletPrivKey, endpoint, isShell, envPath }) => {
const replaceMap = {
DELEGATE_OWNER_WALLET: delegateWallet,
DELEGATE_PRIVATE_KEY: delegateWalletPrivKey,
CREATOR_NODE_ENDPOINT: endpoint,
SP_OWNER_WALLET: ownerWallet
}
writeEnvConfigFromTemplate({ templatePath, writePath, replaceMap, envPath })
}
const _updateUserReplicaSetManagerBootstrapConfig = async (ethAccounts) => {
const dataContractConfigPath = '../contracts/contract-config.js'
const fileStream = fs.createReadStream(dataContractConfigPath)
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
})
const bootstrapSPIds = [1, 2, 3]
const bootstrapSPDelegateWallets = bootstrapSPIds.map((id) => {
return ethAccounts[id]
})
const bootstrapSPOwnerWallets = bootstrapSPDelegateWallets
const bootstrapSPIdsString = ` bootstrapSPIds: [${bootstrapSPIds}],`
const bootstrapSPDelegateWalletsString = ` bootstrapSPDelegateWallets: ['${bootstrapSPDelegateWallets[0]}', '${bootstrapSPDelegateWallets[1]}', '${bootstrapSPDelegateWallets[2]}'],`
const bootstrapSPOwnerWalletString = ` bootstrapSPOwnerWallets: ['${bootstrapSPOwnerWallets[0]}', '${bootstrapSPOwnerWallets[1]}', '${bootstrapSPDelegateWallets[2]}'],`
const bootstrapRegistryAddressString = ` registryAddress: '${dataContractsConfig.registryAddress}'`
console.log('Initializing UserReplicaSetManager configuration from known delegateWallets within system...')
console.log(`Bootstrapping with ${bootstrapSPIds}, ${bootstrapSPDelegateWallets}, ${bootstrapSPOwnerWalletString}`)
let traversingDevelopmentConfigBlock = false
const output = []
for await (const line of rl) {
if (line.includes('development')) {
traversingDevelopmentConfigBlock = true
output.push(line)
} else if (line.includes('test_local')) {
traversingDevelopmentConfigBlock = false
output.push(line)
} else if (traversingDevelopmentConfigBlock && line.includes('bootstrapSPIds')) {
output.push(bootstrapSPIdsString)
} else if (traversingDevelopmentConfigBlock && line.includes('bootstrapSPDelegateWallets')) {
output.push(bootstrapSPDelegateWalletsString)
} else if (traversingDevelopmentConfigBlock && line.includes('bootstrapSPOwnerWallets')) {
output.push(bootstrapSPOwnerWalletString)
} else if (traversingDevelopmentConfigBlock && line.includes('registryAddress')) {
output.push(bootstrapRegistryAddressString)
} else {
output.push(line)
}
}
fs.writeFileSync(dataContractConfigPath, output.join('\n'))
console.log(`Updated ${dataContractConfigPath} with \nbootstrapSPIds=${bootstrapSPIds}\nbootstrapSPDelegateWallets=${bootstrapSPDelegateWallets}\nbootstrapSPOwnerWallets:${bootstrapSPOwnerWallets}`)
}