OverviewFilesCommitsIssuesDocsReleasesPeers

hyperswarm-peer / integration / layer2_testnet.js

32 lines · javascript ref:
// DHT Testnet — creates 3 local HyperDHT bootstrap nodes.
// Used by Layer 2, 3, 5 tests.
//
// Writes bootstrap address to /tmp/layer_bootstrap.txt
// Waits for SIGTERM to cleanup.

import DHT from 'hyperdht'
import createTestnet from 'hyperdht/testnet.js'
import fs from 'fs'

const BOOTSTRAP_FILE = process.env.BOOTSTRAP_FILE || '/tmp/layer_bootstrap.txt'

console.log('[testnet] creating 3 bootstrap nodes...')
const testnet = await createTestnet(3)
const bootstrap = testnet.bootstrap

// Write bootstrap addresses as comma-separated host:port list
const addresses = bootstrap.map(n => `${n.host}:${n.port}`)
fs.writeFileSync(BOOTSTRAP_FILE, addresses.join(','))
console.log('[testnet] bootstrap:', addresses.join(', '))
console.log('[testnet] ready (PID ' + process.pid + ')')

const shutdown = async () => {
  console.log('[testnet] shutting down...')
  await testnet.destroy()
  try { fs.unlinkSync(BOOTSTRAP_FILE) } catch {}
  process.exit(0)
}

process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)