Skip to content

Commit 9119ec6

Browse files
committed
test: fix create-core helper silently skipping bitfield exchange on hypercore 11
peer.core is hypercore 11's internal Core object, which has no .length property, so bitfield.want(contig, undefined) yielded nothing and sendWants was a silent no-op. Same fix as cdf3eb6: use the session core's length. Adds a regression test that shares a sparse pre-existing bitfield without downloading (a download would fetch the bitfield natively and mask a broken helper).
1 parent d563236 commit 9119ec6

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

test/core-manager.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,55 @@ test('eagerly updates remote bitfields', async (t) => {
149149
}
150150
})
151151

152+
test('createCore helper eagerly shares sparse bitfields', async (t) => {
153+
// Tests using the createCore helper rely on it mimicking CoreManager's
154+
// eager bitfield exchange: a peer's non-contiguous bitfield must reach the
155+
// other side even when nothing is downloaded. Native hypercore replication
156+
// only shares the contiguous length on connect (bitfields are otherwise
157+
// only sent in response to wants from a download), so we must not download
158+
// here — a download would fetch the bitfield anyway and mask a broken
159+
// helper.
160+
const writer = await createCore(t)
161+
await writer.append(['a', 'b', 'c', 'd', 'e'])
162+
await writer.clear(2, 3)
163+
164+
const reader = await createCore(t, writer.key)
165+
replicateCores(writer, reader)
166+
await reader.update({ wait: true })
167+
// Need to wait for now, since no event for when a remote bitfield is updated
168+
await delay(200)
169+
170+
assert(writer.core)
171+
assert.equal(reader.peers.length, 1)
172+
assert(
173+
bitfieldEquals(
174+
reader.peers[0].remoteBitfield,
175+
writer.core.bitfield,
176+
writer.length
177+
),
178+
'reader learns writer sparse bitfield without downloading'
179+
)
180+
181+
// A core whose own data is sparse (from a partial download) must also share
182+
// its bitfield with a newly connected peer.
183+
await reader.download({ blocks: [0, 3] }).done()
184+
const observer = await createCore(t, writer.key)
185+
replicateCores(reader, observer)
186+
await observer.update({ wait: true })
187+
await delay(200)
188+
189+
assert(reader.core)
190+
assert.equal(observer.peers.length, 1)
191+
assert(
192+
bitfieldEquals(
193+
observer.peers[0].remoteBitfield,
194+
reader.core.bitfield,
195+
reader.length
196+
),
197+
'newly connected peer learns sparse bitfield without downloading'
198+
)
199+
})
200+
152201
test('multiplexing waits for cores to be added', async (t) => {
153202
// Mapeo code expects replication to work when cores are not added to the
154203
// replication stream at the same time. This is not explicitly tested in

test/helpers/create-core.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ export async function createCore(t, key) {
1818

1919
// Send over entire local bitfield
2020
core.on('peer-add', (peer) => {
21-
sendWants(peer)
21+
sendWants(core, peer)
2222
})
2323

2424
core.once('append', () => {
2525
for (const peer of core.peers) {
26-
sendWants(peer)
26+
sendWants(core, peer)
2727
}
2828
})
2929

@@ -33,17 +33,18 @@ export async function createCore(t, key) {
3333
}
3434

3535
/**
36+
* @param {Hypercore} core
3637
* @param {import('../../src/types.js').HypercorePeer} peer
3738
*/
38-
function sendWants(peer) {
39+
function sendWants(core, peer) {
3940
// How much of the contiguousLength do we have locally?
4041
const contig = /** @type {number} */ (
4142
// @ts-ignore
4243
Math.min(peer.core.state.length, peer.core.header.hints.contiguousLength)
4344
)
4445

4546
// @ts-ignore
46-
for (const msg of peer.core.bitfield.want(contig, peer.core.length)) {
47+
for (const msg of peer.core.bitfield.want(contig, core.length)) {
4748
// @ts-ignore
4849
peer.wireBitfield.send(msg)
4950
}

0 commit comments

Comments
 (0)