Skip to content

Commit 1b57a88

Browse files
add test for multiple peers sharing remotePublicKey (#734)
1 parent 1e795de commit 1b57a88

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

test/replicate.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const b4a = require('b4a')
33
const NoiseSecretStream = require('@hyperswarm/secret-stream')
44
const { create, createStored, replicate, unreplicate, eventFlush, replicateDebugStream } = require('./helpers')
55
const { makeStreamPair } = require('./helpers/networking.js')
6+
const crypto = require('hypercore-crypto')
67
const Hypercore = require('../')
78

89
const DEBUG = false
@@ -1873,6 +1874,66 @@ test('uses hotswaps to avoid long download tail', async t => {
18731874
t.ok(slowPeer.stats.wireData.rx > 0, 'sanity check: still received data from slow peer')
18741875
})
18751876

1877+
test('multiple peers with the same remotePublicKey', async t => {
1878+
const core = await create(t)
1879+
const peer1 = await create(t, core.key)
1880+
const peer2 = await create(t, core.key)
1881+
1882+
const batch = []
1883+
while (batch.length < 100) {
1884+
batch.push(Buffer.allocUnsafe(60000))
1885+
}
1886+
await core.append(batch)
1887+
1888+
const keyPair = crypto.keyPair()
1889+
1890+
const sa1 = core.replicate(true)
1891+
const sa2 = peer1.replicate(false, { keyPair })
1892+
1893+
const sb1 = core.replicate(true)
1894+
const sb2 = peer2.replicate(false, { keyPair })
1895+
1896+
sa1.pipe(sa2).pipe(sa1)
1897+
sb1.pipe(sb2).pipe(sb1)
1898+
1899+
const download1 = peer1.download({ start: 0, end: core.length })
1900+
const download2 = peer2.download({ start: 0, end: core.length })
1901+
1902+
await Promise.all([
1903+
download1.done(),
1904+
download2.done()
1905+
])
1906+
1907+
t.alike(core.peers[0].remotePublicKey, keyPair.publicKey)
1908+
t.alike(core.peers[1].remotePublicKey, keyPair.publicKey)
1909+
1910+
const closing = Promise.all([
1911+
closed(sa1),
1912+
closed(sa2),
1913+
closed(sb1),
1914+
closed(sb2)
1915+
])
1916+
1917+
sa1.destroy()
1918+
sb1.destroy()
1919+
1920+
await closing
1921+
1922+
t.absent(await isSparse(peer1))
1923+
t.absent(await isSparse(peer2))
1924+
1925+
function closed (s) {
1926+
return new Promise(resolve => s.on('close', resolve))
1927+
}
1928+
1929+
async function isSparse (core) {
1930+
for (let i = 0; i < core.length; i++) {
1931+
if (!(await core.get(i))) return true
1932+
}
1933+
return false
1934+
}
1935+
})
1936+
18761937
test('messages exchanged when empty core connects to non-sparse', async function (t) {
18771938
// DEVNOTE: if this test fails, it does not necessarily indicate a bug
18781939
// It might also mean that our replication logic became more efficient

0 commit comments

Comments
 (0)