Skip to content

Commit

Permalink
chore: rename PeerCoreState to PeerNamespaceState (#784)
Browse files Browse the repository at this point in the history
This is just a rename, which is a bit more correct.

Co-authored-by: Gregor MacLennan <[email protected]>
  • Loading branch information
EvanHahn and gmaclennan committed Aug 27, 2024
1 parent 386af02 commit 770a8dc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/sync/core-sync-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import RemoteBitfield, {
* @property {number} wanted blocks we want from this peer
*/
/**
* @typedef {object} PeerCoreState
* @typedef {object} PeerNamespaceState
* @property {number} have blocks the peer has locally
* @property {number} want blocks this peer wants from us
* @property {number} wanted blocks we want from this peer
Expand All @@ -35,7 +35,7 @@ import RemoteBitfield, {
* @typedef {object} DerivedState
* @property {number} coreLength known (sparse) length of the core
* @property {LocalCoreState} localState local state
* @property {{ [peerId in PeerId]: PeerCoreState }} remoteStates map of state of all known peers
* @property {{ [peerId in PeerId]: PeerNamespaceState }} remoteStates map of state of all known peers
*/

/**
Expand Down Expand Up @@ -266,7 +266,7 @@ export class PeerState {
#haves
/** @type {Bitfield} */
#wants = new RemoteBitfield()
/** @type {PeerCoreState['status']} */
/** @type {PeerNamespaceState['status']} */
status = 'disconnected'
#wantAll
constructor({ wantAll = true } = {}) {
Expand Down Expand Up @@ -359,7 +359,7 @@ export function deriveState(coreState) {
const length = coreState.length || 0
/** @type {LocalCoreState} */
const localState = { have: 0, want: 0, wanted: 0 }
/** @type {Record<PeerId, PeerCoreState>} */
/** @type {Record<PeerId, PeerNamespaceState>} */
const remoteStates = {}

/** @type {Map<PeerId, PeerState>} */
Expand Down
22 changes: 11 additions & 11 deletions src/sync/namespace-sync-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ export class NamespaceSyncState {
for (const css of this.#coreStates.values()) {
const coreState = css.getState()
mutatingAddPeerState(state.localState, coreState.localState)
for (const [peerId, peerCoreState] of Object.entries(
for (const [peerId, peerNamespaceState] of Object.entries(
coreState.remoteStates
)) {
if (!(peerId in state.remoteStates)) {
state.remoteStates[peerId] = peerCoreState
state.remoteStates[peerId] = peerNamespaceState
} else {
mutatingAddPeerState(state.remoteStates[peerId], peerCoreState)
mutatingAddPeerState(state.remoteStates[peerId], peerNamespaceState)
}
}
}
Expand Down Expand Up @@ -140,12 +140,12 @@ export class NamespaceSyncState {

/**
* @overload
* @param {import('./core-sync-state.js').PeerCoreState['status']} status
* @returns {import('./core-sync-state.js').PeerCoreState}
* @param {import('./core-sync-state.js').PeerNamespaceState['status']} status
* @returns {import('./core-sync-state.js').PeerNamespaceState}
*/

/**
* @param {import('./core-sync-state.js').PeerCoreState['status']} [status]
* @param {import('./core-sync-state.js').PeerNamespaceState['status']} [status]
*/
export function createState(status) {
if (status) {
Expand All @@ -164,16 +164,16 @@ export function createState(status) {

/**
* @overload
* @param {import('./core-sync-state.js').PeerCoreState} accumulator
* @param {import('./core-sync-state.js').PeerCoreState} currentValue
* @returns {import('./core-sync-state.js').PeerCoreState}
* @param {import('./core-sync-state.js').PeerNamespaceState} accumulator
* @param {import('./core-sync-state.js').PeerNamespaceState} currentValue
* @returns {import('./core-sync-state.js').PeerNamespaceState}
*/

/**
* Adds peer state in `currentValue` to peer state in `accumulator`
*
* @param {import('./core-sync-state.js').PeerCoreState} accumulator
* @param {import('./core-sync-state.js').PeerCoreState} currentValue
* @param {import('./core-sync-state.js').PeerNamespaceState} accumulator
* @param {import('./core-sync-state.js').PeerNamespaceState} currentValue
*/
function mutatingAddPeerState(accumulator, currentValue) {
accumulator.have += currentValue.have
Expand Down
2 changes: 1 addition & 1 deletion src/sync/peer-sync-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class PeerSyncController {
}

/**
* @typedef {{ [namespace in Namespace]?: import("./core-sync-state.js").PeerCoreState }} PeerState
* @typedef {{ [namespace in Namespace]?: import("./core-sync-state.js").PeerNamespaceState }} PeerState
*/

/** @typedef {Record<Namespace, 'unknown' | 'syncing' | 'synced'>} SyncStatus */
Expand Down
13 changes: 7 additions & 6 deletions src/sync/sync-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,13 @@ function getRemoteDevicesSyncState(namespaceSyncState, peerSyncControllers) {
const isBlocked = psc.syncCapability[namespace] === 'blocked'
if (isBlocked) continue

const peerCoreState = namespaceSyncState[namespace].remoteStates[peerId]
if (!peerCoreState) continue
const peerNamespaceState =
namespaceSyncState[namespace].remoteStates[peerId]
if (!peerNamespaceState) continue

/** @type {boolean} */
let isSyncEnabled
switch (peerCoreState.status) {
switch (peerNamespaceState.status) {
case 'disconnected':
case 'connecting':
isSyncEnabled = false
Expand All @@ -455,7 +456,7 @@ function getRemoteDevicesSyncState(namespaceSyncState, peerSyncControllers) {
isSyncEnabled = true
break
default:
throw new ExhaustivenessError(peerCoreState.status)
throw new ExhaustivenessError(peerNamespaceState.status)
}

if (!Object.hasOwn(result, peerId)) {
Expand All @@ -469,8 +470,8 @@ function getRemoteDevicesSyncState(namespaceSyncState, peerSyncControllers) {
? 'initial'
: 'data'
result[peerId][namespaceGroup].isSyncEnabled = isSyncEnabled
result[peerId][namespaceGroup].want += peerCoreState.want
result[peerId][namespaceGroup].wanted += peerCoreState.wanted
result[peerId][namespaceGroup].want += peerNamespaceState.want
result[peerId][namespaceGroup].wanted += peerNamespaceState.wanted
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/sync/core-sync-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ function slowBitCount(n) {

/**
*
* @param {{ have?: number | bigint, prehave?: number, want?: number | bigint, status?: import('../../src/sync/core-sync-state.js').PeerCoreState['status'] }} param0
* @param {{ have?: number | bigint, prehave?: number, want?: number | bigint, status?: import('../../src/sync/core-sync-state.js').PeerNamespaceState['status'] }} param0
*/
function createState({ have, prehave, want, status }) {
const peerState = new PeerState()
Expand Down

0 comments on commit 770a8dc

Please sign in to comment.