Skip to content

Commit

Permalink
problem: can't debug bloom filter issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gsovereignty committed Apr 21, 2024
1 parent 09ed37b commit 737d702
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/lib/snort_workers/master_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NostrSystem, RequestBuilder, type QueryLike } from '@snort/system';
import { derived, writable } from 'svelte/store';
import { Command, FrontendData, WorkerData } from './types';
import { execTime, followsFromKind3, getNostrEvent, tagSplits, updateRepliesInPlace } from './utils';
import { BloomFilter } from 'bloomfilter';

let workerData = new WorkerData();
let workerDataStore = writable(workerData);
Expand Down Expand Up @@ -33,7 +34,7 @@ workerDataStore.subscribe((data) => {
for (let r of data.roots) {
if (!data.ourBloom.test(r)) {
let re = data.events.get(r);
if (!r) {
if (!re) {
throw new Error('missing event, this should not happen, bug!');
}
roots.push(re!);
Expand All @@ -57,6 +58,8 @@ workerDataStore.subscribe((data) => {
});
fed.replies = data.replies;
fed.events = data.events;
fed._bloomString = JSON.stringify([].slice.call(data.ourBloom.buckets))
fed.ourBloom = data.ourBloom;//new BloomFilter(JSON.parse(fed._bloomString), 32)
postMessage(fed);
end()
});
Expand Down Expand Up @@ -125,13 +128,17 @@ async function start(pubkey?: string, pubkeys?: string[]) {
let end = execTime("125 async start")
const rb = new RequestBuilder('fetch-initial-data');
let _pukeys: string[] = [];
let kinds = [3, 10002]
if (pubkeys) {
_pukeys = pubkeys;
}
if (pubkey) {
_pukeys.push(pubkey);
}
rb.withFilter().authors(_pukeys).kinds([3, 10002]);
if (pubkey && !pubkeys) {
kinds = [3, 10002, 1, 7]
}
rb.withFilter().authors(_pukeys).kinds(kinds);
rb.withOptions({ leaveOpen: false });

const _q = sys.Query(rb);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/snort_workers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class WorkerData {
constructor(pubkey?: string) {
this.bloomSize = 0;
this.ourBloom = new BloomFilter(
32 * 256, // bits to allocate.
16 // number of hashes
64 * 256, // bits to allocate.
32 // number of hashes
);
this.missingEvents = new Set();
this.latestReplaceable = new Map();
Expand All @@ -69,9 +69,9 @@ export class FrontendData {
replies: Map<string, Set<string>>;
baseFollows: Set<string>;
events: Map<string, NostrEvent>;
//ourBloom: BloomFilter;
ourBloom: BloomFilter | undefined;
_bloomString: string | undefined;
constructor() {
//this.ourBloom = new BloomFilter()
this.roots = [];
this.replies = new Map();
this.baseFollows = new Set();
Expand Down
7 changes: 6 additions & 1 deletion src/lib/snort_workers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { TaggedNostrEvent } from '@snort/system';
import type { Event } from 'nostr-tools';
import { FrontendData, WorkerData } from './types';

let debug = false

export let execTime = (name: string): (() => void) => {
let start = performance.now();
let ended = false;
Expand All @@ -14,7 +16,10 @@ export let execTime = (name: string): (() => void) => {
return () => {
let end = performance.now();
ended = true;
console.log(name, end - start, 'ms');
if (debug) {
console.log(name, end - start, 'ms');
}

};
};

Expand Down
21 changes: 19 additions & 2 deletions src/lib/views/messages/Messages.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import { updateRepliesInPlace } from '@/snort_workers/utils';
import Login from '@/ndk/Login.svelte';
import { currentUser } from '@/ndk/ndk';
import Input from '@/components/ui/input/input.svelte';
import { BloomFilter } from 'bloomfilter';
let localEvents = writable(new Map<string, NostrEvent>());
Expand Down Expand Up @@ -149,6 +151,8 @@
}
return inSet.has(id);
}
let eventID:string;
</script>
<div class=" hidden">{$shortListLength}</div>
<ChatLayout hideFaucet={$threadParentID != 'root'}>
Expand Down Expand Up @@ -198,8 +202,21 @@
onClick={() => {
console.log($threadParentID, $FrontendDataStore.replies.get($threadParentID));
}}>Print root event data</Button
>
{$currentUser?.pubkey}
><br />
LOGGED IN AS: {$currentUser?.pubkey} <br />
<Input bind:value={eventID} class="w-64" /><Button onClick={()=>{
//let bloom = new BloomFilter(JSON.parse($FrontendDataStore._bloomString), 32)
console.log($FrontendDataStore.ourBloom)
if ($FrontendDataStore.ourBloom) {
console.log(210)
console.log(eventID)
$FrontendDataStore.ourBloom.add("t")
let result = $FrontendDataStore.ourBloom.test(eventID)
console.log(213)
console.log(214, result)
}
}}>Check if event is in bloom filter</Button><br />
{$FrontendDataStore.ourBloom?.buckets.byteLength}
</div>
</div>
</ChatLayout>

0 comments on commit 737d702

Please sign in to comment.