Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sync should only happen when both sides are enabled #764

Merged
merged 35 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4ef0871
Add failing test
EvanHahn Aug 22, 2024
866e2ce
Fix test (?)
EvanHahn Aug 25, 2024
d2eb629
WIP: messily trying to fix other tests
EvanHahn Aug 25, 2024
713e97c
Tidying
EvanHahn Aug 25, 2024
12683a8
Test cleanup
EvanHahn Aug 25, 2024
cb05512
No .only
EvanHahn Aug 25, 2024
c3193b5
Basic, buggy fuzzer
EvanHahn Aug 25, 2024
8b21536
Organize imports
EvanHahn Aug 25, 2024
fbad430
Fuzzer cleanup
EvanHahn Aug 25, 2024
46a833f
Trade one failure for another
EvanHahn Aug 25, 2024
128dc57
Merge branch 'main' into 2024-08-22-sync-bug-exploration
EvanHahn Aug 26, 2024
bb48eb8
Merge branch 'main' into 2024-08-22-sync-bug-exploration
EvanHahn Aug 26, 2024
389f0b4
Fuzz cleanup
EvanHahn Aug 26, 2024
99063d7
extract & fix unreplicate function
gmaclennan Aug 27, 2024
ead73a4
test adding 10ms wait after sync start/stop in fuzz tests
gmaclennan Aug 27, 2024
e38d99e
Merge branch 'main' into 2024-08-22-sync-bug-exploration
EvanHahn Aug 27, 2024
2ec8936
Ready for Evan to clean up
EvanHahn Aug 27, 2024
7aeb3ad
Merge branch 'main' into 2024-08-22-sync-bug-exploration
EvanHahn Aug 27, 2024
47a9fe3
Remove why-is-node-running commented-out import
EvanHahn Aug 27, 2024
8db6106
sync fuzz: use delay
EvanHahn Aug 27, 2024
afbc1d7
Sync fuzz: parallelize, wait for expectation rather than "finish"
EvanHahn Aug 27, 2024
2dd3813
Sync fuzz test cleanup
EvanHahn Aug 27, 2024
f78bb01
More sync fuzz test cleanup
EvanHahn Aug 27, 2024
f3ad1ee
More sync fuzz test cleanup
EvanHahn Aug 27, 2024
ee49924
Minor: use single quotes for type import
EvanHahn Aug 27, 2024
014cb8b
Revert a type rename
EvanHahn Aug 27, 2024
6c15b21
Merge branch 'main' into 2024-08-22-sync-bug-exploration
EvanHahn Aug 27, 2024
e2d4647
Remove a TODO we've now addressed
EvanHahn Aug 27, 2024
a5ecb06
Merge branch 'main' into 2024-08-22-sync-bug-exploration
EvanHahn Aug 27, 2024
34ec93f
Remove another addressed TODO
EvanHahn Aug 27, 2024
fb4860b
Revert an unused type change
EvanHahn Aug 27, 2024
2926762
Use shared utilities
EvanHahn Aug 27, 2024
c68f3f2
Assert mins and maxes are correct
EvanHahn Aug 27, 2024
47648ae
Wait for ready before unreplicating
EvanHahn Aug 27, 2024
b74fd2e
Fuzz tests should do subtests properly
EvanHahn Aug 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/lib/hypercore-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { assert } from '../utils.js'

/**
* @param {import('hypercore')<'binary', any>} core Core to unreplicate. Must be ready.
* @param {import('protomux')} protomux
*/
export function unreplicate(core, protomux) {
assert(core.discoveryKey, 'Core should have a discovery key')
protomux.unpair({
protocol: 'hypercore/alpha',
id: core.discoveryKey,
})
for (const channel of protomux) {
if (channel.protocol !== 'hypercore/alpha') continue
if (!channel.id.equals(core.discoveryKey)) continue
channel.close()
}
}
21 changes: 14 additions & 7 deletions src/sync/peer-sync-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mapObject from 'map-obj'
import { NAMESPACES, PRESYNC_NAMESPACES } from '../constants.js'
import { Logger } from '../logger.js'
import { ExhaustivenessError, createMap } from '../utils.js'
import { unreplicate } from '../lib/hypercore-helpers.js'
/** @import { CoreRecord } from '../core-manager/index.js' */
/** @import { Role } from '../roles.js' */
/** @import { SyncEnabledState } from './sync-api.js' */
Expand Down Expand Up @@ -255,16 +256,22 @@ export class PeerSyncController {

/**
* @param {import('hypercore')<'binary', any>} core
* @returns {Promise<void>}
*/
#unreplicateCore(core) {
async #unreplicateCore(core) {
if (core === this.#coreManager.creatorCore) return
const peerToUnreplicate = core.peers.find(
(peer) => peer.protomux === this.#protomux
)
if (!peerToUnreplicate) return
this.#log('unreplicating core %k', core.key)
peerToUnreplicate.channel.close()

this.#replicatingCores.delete(core)

const isCoreReady = Boolean(core.discoveryKey)
if (!isCoreReady) {
await core.ready()
const wasReEnabledWhileWaiting = this.#replicatingCores.has(core)
if (wasReEnabledWhileWaiting) return
}

unreplicate(core, this.#protomux)
this.#log('unreplicated core %k', core.key)
}

/**
Expand Down
Loading
Loading