Put dropped router values into top relay selections

This commit is contained in:
Jon Staab
2024-04-04 10:22:24 -07:00
parent 6220cd152c
commit 7369074741
3 changed files with 25 additions and 19 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
import type {Event} from 'nostr-tools'
import {matchFilter as nostrToolsMatchFilter} from 'nostr-tools'
import {prop, hash, identity, groupBy, randomId, uniq} from '@coracle.social/lib'
import {prop, avg, hash, identity, groupBy, randomId, uniq} from '@coracle.social/lib'
import type {Rumor} from './Events'
import {decodeAddress, addressFromEvent, encodeAddress} from './Address'
import {isReplaceableKind} from './Kinds'
@@ -168,5 +168,5 @@ export const getFilterGenerality = (filter: Filter) => {
return 1
}
export const guessFilterDelta = (filters: Filter[], max = 60 * 60 * 24) =>
Math.round(max * Math.max(0.005, 1 - filters.map(getFilterGenerality).reduce((a, b) => a + b) / filters.length))
export const guessFilterDelta = (filters: Filter[], max = 60 * 60 * 24 * 7) =>
Math.round(max * Math.max(0.005, 1 - avg(filters.map(getFilterGenerality))))
+11 -11
View File
@@ -1,5 +1,5 @@
import type {EventTemplate} from 'nostr-tools'
import {first, identity, sortBy, uniq, shuffle, pushToMapKey} from '@coracle.social/lib'
import {first, splitAt, identity, sortBy, uniq, shuffle, pushToMapKey} from '@coracle.social/lib'
import {Tags, Tag} from '@coracle.social/util'
import type {Rumor} from './Events'
import {getAddress, isReplaceable} from './Events'
@@ -44,12 +44,6 @@ export class Router {
// Utilities derived from options
getTagSelections = (tags: Tags) =>
tags
.filter(t => isShareableRelayUrl(t.nth(2)))
.mapTo(t => this.selection(t.nth(1), [t.nth(2)]))
.valueOf()
getPubkeySelection = (pubkey: string, mode?: RelayMode) =>
this.selection(pubkey, this.options.getPubkeyRelays(pubkey, mode))
@@ -288,7 +282,9 @@ export class RouterScenario {
allValues.add(value)
for (const relay of relays) {
pushToMapKey(valuesByRelay, relay, value)
if (isShareableRelayUrl(relay)) {
pushToMapKey(valuesByRelay, relay, value)
}
}
}
@@ -296,7 +292,7 @@ export class RouterScenario {
// are wee're less tolerant of failure. Add more redundancy to fill our relay limit.
const limit = this.getLimit()
const redundancy = this.getRedundancy()
const adjustedRedundancy = redundancy * (limit / (allValues.size * redundancy))
const adjustedRedundancy = Math.max(redundancy, redundancy * (limit / (allValues.size * redundancy)))
const seen = new Map<string, number>()
const result: ValuesByRelay = new Map()
@@ -330,9 +326,13 @@ export class RouterScenario {
}
}
const selections = this.router.relaySelectionsFromMap(result)
const [keep, discard] = splitAt(limit, this.router.relaySelectionsFromMap(result))
return limit ? selections.slice(0, limit) : selections
for (const target of keep.slice(0, redundancy)) {
target.values = uniq(discard.concat(target).flatMap((selection: RelayValues) => selection.values))
}
return keep
}
getUrls = () => this.getSelections().map((selection: RelayValues) => selection.relay)