Repository navigation
fix(wot): rebuild every worker's trust graph when a contact list changes it #1348
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| 'nostream': patch | ||
| --- | ||
|
|
||
| fix(wot): rebuild the trust graph of every worker when a contact list changes it | ||
|
|
||
| Each worker keeps its own Web of Trust graph, and only the worker that handled a contact list rebuilt it. The other workers kept granting a proof-of-work discount for a relationship the author had just removed, until their next rebuild, which could be a whole `wot.refreshIntervalHours` away. A worker that applies a contact list now publishes a token in the cache, and every worker compares it with the one its graph was built under and rebuilds when it differs, and asks again if that rebuild fails, spaced out by `wot.minRebuildIntervalSeconds` like a rebuild for a contact list it handled itself. A lookup reads the token at most once a second. If the cache cannot be read or written, the worker keeps answering from its own graph. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import { randomBytes } from 'node:crypto' | ||
|
|
||
| import { ICacheAdapter } from '../@types/adapters' | ||
| import { Pubkey, Tag } from '../@types/base' | ||
| import { IEventRepository } from '../@types/repositories' | ||
|
|
@@ -17,6 +19,12 @@ export const DEFAULT_WOT_MAX_EDGES = 2_000_000 | |
| export const DEFAULT_WOT_MIN_REBUILD_INTERVAL_SECONDS = 60 | ||
| export const WOT_FOLLOW_LIST_BATCH_SIZE = 100 | ||
|
|
||
| // Every worker holds its own graph. A worker that applies a contact list publishes a new token here, and the others | ||
| // compare it with the one their graph was built under, so they rebuild too instead of keeping the old relationships. | ||
| export const WOT_INVALIDATION_KEY = 'wot:graph:invalidation' | ||
| // How long a worker trusts the token it read, so a lookup costs a cache read at most once per interval. | ||
| export const WOT_INVALIDATION_CHECK_INTERVAL_MS = 1000 | ||
|
|
||
| // The longest delay a Node.js timer accepts; a longer wait is re-armed. | ||
| const MAX_TIMER_DELAY_MS = 2 ** 31 - 1 | ||
|
|
||
|
|
@@ -78,6 +86,13 @@ export class WotGraphService implements IWotGraphService { | |
|
|
||
| private lastRebuildEndedAt: number | undefined | ||
|
|
||
| // The invalidation token the current graph was built under, and the last one a rebuild was requested for. | ||
| private builtInvalidation: string | undefined | ||
|
|
||
| private requestedInvalidation: string | undefined | ||
|
|
||
| private invalidationCheckedAt: number | undefined | ||
|
|
||
| public constructor( | ||
| private readonly cache: ICacheAdapter, | ||
| private readonly eventRepository: IEventRepository, | ||
|
|
@@ -111,6 +126,7 @@ export class WotGraphService implements IWotGraphService { | |
| return 0 | ||
| } | ||
|
|
||
| await this.checkInvalidation() | ||
| await this.ensureBuilt() | ||
|
|
||
| // Fail closed: if the settings changed and the rebuild for them did not | ||
|
|
@@ -137,15 +153,59 @@ export class WotGraphService implements IWotGraphService { | |
| await this.cache.addToSet(followSetKey(pubkey), follows) | ||
| } | ||
|
|
||
| // A pubkey outside the current trust graph publishing a new follow list | ||
| // can't change anyone's distance from the seed, so only rebuild when the | ||
| // change could actually matter. The event path never waits for the | ||
| // The other workers are told of every contact list: this worker's graph cannot say whether theirs holds the | ||
| // pubkey, because it may not be built yet, or may not have caught up with a change the others already applied. | ||
| // Each worker rebuilds at most once per wot.minRebuildIntervalSeconds however many tokens arrive. | ||
| await this.publishInvalidation() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not suppress an invalidation without scheduling a rebuild. Suppose this worker holds This publication sets Do not set
|
||
|
|
||
| // A pubkey outside this worker's trust graph publishing a new follow list can't change anyone's distance from | ||
| // the seed here, so only rebuild when the change could actually matter. The event path never waits for the | ||
| // rebuild, and requestRebuild() spaces these rebuilds out. | ||
| if (pubkey === wot.seedPubkey || this.distances.has(pubkey)) { | ||
| this.requestRebuild() | ||
| } | ||
| } | ||
|
|
||
| // Tells the other workers that a contact list changed. The follow lists are written before this, so a worker that | ||
| // sees the token and rebuilds reads the change. | ||
| private async publishInvalidation(): Promise<void> { | ||
| const token = `${Date.now()}-${randomBytes(8).toString('hex')}` | ||
| try { | ||
| await this.cache.setKey(WOT_INVALIDATION_KEY, token) | ||
| this.requestedInvalidation = token | ||
| } catch (error) { | ||
| logger('could not publish the wot graph invalidation: %o', error) | ||
| } | ||
| } | ||
|
|
||
| private async readInvalidation(): Promise<string> { | ||
| try { | ||
| return (await this.cache.getKey(WOT_INVALIDATION_KEY)) || '' | ||
| } catch (error) { | ||
| logger('could not read the wot graph invalidation: %o', error) | ||
| return this.builtInvalidation ?? '' | ||
| } | ||
| } | ||
|
|
||
| // A token other than the one this graph was built under means another worker applied a contact list since. The | ||
| // rebuild is requested like one for a contact list this worker handled, so it is spaced out the same way. | ||
| private async checkInvalidation(): Promise<void> { | ||
| const now = performance.now() | ||
| if ( | ||
| this.invalidationCheckedAt !== undefined && | ||
| now - this.invalidationCheckedAt < WOT_INVALIDATION_CHECK_INTERVAL_MS | ||
| ) { | ||
| return | ||
| } | ||
| this.invalidationCheckedAt = now | ||
|
|
||
| const token = await this.readInvalidation() | ||
| if (this.ready && token !== this.builtInvalidation && token !== this.requestedInvalidation) { | ||
| this.requestedInvalidation = token | ||
| this.requestRebuild() | ||
| } | ||
| } | ||
|
|
||
| private async ensureBuilt(): Promise<void> { | ||
| // scheduleRebuild() hands back a rebuild that may already be in flight for | ||
| // the previous settings, so check the key again once it finishes; a | ||
|
|
@@ -181,6 +241,9 @@ export class WotGraphService implements IWotGraphService { | |
| this.building = this.rebuild() | ||
| .catch((error) => { | ||
| logger.error('wot graph rebuild failed: %o', error) | ||
| // The graph was not replaced, so it is still older than the change that asked for this rebuild. Forgetting the | ||
| // request lets the next token check ask again, spaced out like any other. | ||
| this.requestedInvalidation = undefined | ||
| }) | ||
| .finally(() => { | ||
| this.building = undefined | ||
|
|
@@ -249,6 +312,10 @@ export class WotGraphService implements IWotGraphService { | |
| return | ||
| } | ||
|
|
||
| // Read before any follow list, so a change published after this is seen as newer than the graph. It becomes the | ||
| // graph's token only when the graph replaces the old one. | ||
| const startedUnder = await this.readInvalidation() | ||
|
|
||
| const maxDepth = wot.maxDepth ?? 2 | ||
| const minimumFollowers = wot.minimumFollowers ?? 1 | ||
| const limits = rebuildLimits(wot) | ||
|
|
@@ -333,6 +400,7 @@ export class WotGraphService implements IWotGraphService { | |
|
|
||
| this.distances = distances | ||
| this.builtKey = key | ||
| this.builtInvalidation = startedUnder | ||
| this.ready = true | ||
| if (stoppedAt) { | ||
| logger.warn( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppose worker B publishes a removed relationship, then worker A handles a contact list from an account outside A's graph before A's next token check. This publication sets A's
requestedInvalidationto the new shared token, but the membership check skipsrequestRebuild(). Later checks ignore that token, so A keeps granting the removed account's proof-of-work discount until another rebuild trigger arrives.Only mark a token as requested when a rebuild is actually queued, and ensure publishing an unrelated change cannot hide earlier changes.
How this was verified: Every accepted contact list reaches
updateFollowList(), publishing setsrequestedInvalidationwithout necessarily requesting a rebuild, and event admission uses the unchanged distances to reduce required proof of work.