TAO/client/src/powWorker.ts

81 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-10-29 03:09:46 +00:00
import { type UnsignedEvent, type Event, getEventHash } from 'nostr-tools'
// eslint-disable-next-line no-restricted-globals
const ctx: Worker = self as any;
// Respond to message from parent thread
ctx.addEventListener('message', (event) => {
console.log("Received message in worker:", event.data);
2023-11-06 04:25:52 +00:00
const { unsigned, difficulty, nonceStart, nonceStep } = event.data;
2023-10-29 03:09:46 +00:00
2023-11-06 04:25:52 +00:00
const result = minePow(unsigned, difficulty, nonceStart, nonceStep);
2023-10-29 03:09:46 +00:00
console.log("Mining result:", result);
// Post the mined event back to the main thread
ctx.postMessage(result);
});
ctx.onerror = function(e) {
console.error("Worker error:", e);
};
/** Get POW difficulty from a Nostr hex ID. */
export function getPow(hex: string): number {
let count = 0
for (let i = 0; i < hex.length; i++) {
const nibble = parseInt(hex[i], 16)
if (nibble === 0) {
count += 4
} else {
count += Math.clz32(nibble) - 28
break
}
}
return count
}
/**
* Mine an event with the desired POW. This function mutates the event.
* Note that this operation is synchronous and should be run in a worker context to avoid blocking the main thread.
*
* Adapted from Snort: https://git.v0l.io/Kieran/snort/src/commit/4df6c19248184218c4c03728d61e94dae5f2d90c/packages/system/src/pow-util.ts#L14-L36
*/
2024-01-05 06:16:55 +00:00
export function minePow<K extends number>(unsigned: UnsignedEvent, difficulty: number, nonceStart: number, nonceStep: number): { found: boolean, event?: Omit<Event, 'sig'> } {
2023-11-06 04:25:52 +00:00
let nonce = nonceStart;
let bestPoW = 0;
2024-01-05 06:16:55 +00:00
const event = unsigned as Omit<Event, 'sig'>
2023-11-06 04:25:52 +00:00
const tag = ['nonce', nonce.toString(), difficulty.toString()]
2023-10-29 03:09:46 +00:00
2023-11-06 04:25:52 +00:00
event.tags.push(tag);
2023-10-29 03:09:46 +00:00
2023-11-06 04:25:52 +00:00
// We use a while loop that might run indefinitely until a solution is found.
// Consider adding a breaking condition if you want to limit the number of nonces each worker checks.
2023-10-29 03:09:46 +00:00
while (true) {
2023-11-06 04:25:52 +00:00
tag[1] = (nonce).toString();
2023-10-29 03:09:46 +00:00
2023-11-06 04:25:52 +00:00
event.id = getEventHash(event);
let leadingZeroes = getPow(event.id);
if (leadingZeroes > bestPoW) {
bestPoW = leadingZeroes;
}
2023-10-29 03:09:46 +00:00
if (leadingZeroes >= difficulty) {
2023-11-06 04:25:52 +00:00
return { found: true, event: event };
}
2023-10-29 03:09:46 +00:00
2023-11-06 04:25:52 +00:00
nonce += nonceStep;
2023-10-29 03:09:46 +00:00
2023-11-06 04:25:52 +00:00
if (nonce % (nonceStep * 10000) === 0) {
ctx.postMessage({ status: 'progress', currentNonce: nonce, bestPoW});
2023-10-29 03:09:46 +00:00
}
}
2023-11-06 04:25:52 +00:00
return { found: false };
2023-10-29 03:09:46 +00:00
}
export default ctx;