Skip to content

Commit

Permalink
refine demo
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Oct 26, 2024
1 parent 5d168de commit 7a6f6a7
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 55 deletions.
4 changes: 2 additions & 2 deletions demo/scripts/setupTypeScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ fs.writeFileSync(path.join(projectRoot, ".vscode", "extensions.json"), `{
}
`)

//console.log("Converted to TypeScript.")
////console.log("Converted to TypeScript.")

if (fs.existsSync(path.join(projectRoot, "node_modules"))) {
//console.log("\nYou will need to re-run your dependency manager to get started.")
////console.log("\nYou will need to re-run your dependency manager to get started.")
}
18 changes: 12 additions & 6 deletions demo/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
});
}
function startMining() {
async function startMining() {
const currentUser = get(user);
const currentContent = get(contentState);
Expand All @@ -65,7 +65,8 @@
return;
}
miningState.update(m => ({ ...m, mining: true, result: 'Mining started...' }));
resetMiningState();
miningState.update(m => ({ ...m, mining: true }));
notemine = new Notemine({
content: currentContent.content,
Expand Down Expand Up @@ -108,12 +109,15 @@
successSub = notemine.success$.subscribe(async ({ result: minedResult }) => {
// const currentActiveRelays = get(activeRelays);
console.log(`currentActiveRelays: ${$activeRelays}`);
await publishEvent(minedResult.event)
// //console.log(`currentActiveRelays: ${$activeRelays}`);
miningState.update(m => ({
...m,
mining: false,
result: minedResult ? JSON.stringify(minedResult, null, 2) : 'No result received.',
}));
await publishEvent(minedResult.event)
miningState.update(m => ({
...m,
relayStatus: `Published to relays: ${$activeRelays.join(', ')}`
}));
});
Expand All @@ -127,7 +131,8 @@
}));
});
notemine.mine();
await notemine.mine();
console.log('All workers mining.')
}
const resetMiningState = () => {
Expand All @@ -137,7 +142,8 @@
result: '',
relayStatus: '',
hashRate: 0,
overallBestPow: null
overallBestPow: null,
publishSuccessNum: 0
}));
}
Expand Down
62 changes: 41 additions & 21 deletions demo/src/lib/nostr.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
import { SimplePool } from "nostr-tools";
import { get } from 'svelte/store';
import { finalizeEvent, verifyEvent } from "nostr-tools";
import { relaySettings, user, events, activeRelays, usub } from './stores/index';
import { relaySettings, user, events, activeRelays, usub, miningState } from './stores/index';
import { verifyPow } from './utils.js';

let pubs;

export const pool = new SimplePool();

const timeout = (promise, ms) => new Promise((resolve, reject) => {
const timer = setTimeout(() => reject(new Error("Timeout")), ms);
promise
.then(value => {
clearTimeout(timer);
resolve(value);
})
.catch(err => {
clearTimeout(timer);
reject(err);
});
});

export const publishEvent = async (ev) => {
console.log(ev)
//console.log(ev);
const pow = verifyPow(ev);
console.log('Publishing event:', ev);
//console.log('Publishing event:', ev);
try {
const {isAnon, secret} = get(user);
if(isAnon) {
ev = finalizeEvent(ev, secret);
}
else {
ev = await window.nostr.signEvent(ev)
}
let isGood = verifyEvent(ev);
if (!isGood) throw new Error('Event is not valid');
const currentActiveRelays = get(activeRelays);
pubs = pool.publish(currentActiveRelays, ev);
await Promise.allSettled(pubs);
console.log('Event published successfully.');
const { isAnon, secret } = get(user);
if (isAnon) {
ev = finalizeEvent(ev, secret);
} else {
ev = await window.nostr.signEvent(ev);
}
const isGood = verifyEvent(ev);
if (!isGood) throw new Error('Event is not valid');

const currentActiveRelays = get(activeRelays);
const pubs = pool.publish(currentActiveRelays, ev).map(p => timeout(p, 10000));

const results = await Promise.allSettled(pubs);
const successCount = results.filter(result => result.status === 'fulfilled').length;

miningState.update( m => ({...m, publishSuccessNum: successCount}) )

//console.log(`Event published successfully to ${successCount} relays.`);
} catch (error) {
console.error('Error publishing event:', error);
console.error('Error publishing event:', error);
}
};



function setMyRelays(relays) {
console.log(`Setting my relays: ${relays}`);
//console.log(`Setting my relays: ${relays}`);
relaySettings.update(r => ({
...r,
myRelays: Array.from(new Set([...r.myRelays, ...relays]))
Expand All @@ -52,7 +72,7 @@ export function onK0(event){
catch(e){
console.error('Error parsing K0 content:', e)
}
console.log('K0 profile:', profile)
//console.log('K0 profile:', profile)
events.update( e => ({...e, k0: event}) )
}

Expand All @@ -66,14 +86,14 @@ export function onK3(event){
console.warn('K3 content:', event.content)
}

console.log('K3 relays:', relays)
//console.log('K3 relays:', relays)
setMyRelays(relays)
events.update( e => ({...e, k3: event}) )
}

export function onK10002(event){
const relays = event.tags.filter( t => t[0] === 'r' ).map( r => r[1] )
console.log('K10002 relays:', relays)
//console.log('K10002 relays:', relays)
setMyRelays(relays?.length? relays : [])
events.update( e => ({...e, k10002: event}) )
}
Expand Down
3 changes: 2 additions & 1 deletion demo/src/lib/stores/mining-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export const miningState = writable({
relayStatus: '',
hashRate: 0,
overallBestPow: null,
workersBestPow: []
workersBestPow: [],
publishSuccessNum: 0,
});
2 changes: 1 addition & 1 deletion demo/src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getPow = (hex) => {
}

export const verifyPow = (event) => {
console.log(event)
//console.log(event)
const hash = getEventHash(event);
const count = getPow(hash);
const nonceTag = event.tags.find(tag => tag[0] === 'nonce');
Expand Down
29 changes: 15 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ export class Notemine {
return this._totalHashRate;
}

mine(): void {
//console.log('mine()')
async mine(): Promise<void> {
////console.log('mine()')
if (this.mining$.getValue()) return;

if (!this.pubkey) {
Expand All @@ -156,7 +156,7 @@ export class Notemine {
this.workersPow$.next({});
this.highestPow$.next({});

this.initializeWorkers();
await this.initializeWorkers();
}

stop(): void {
Expand All @@ -173,12 +173,12 @@ export class Notemine {
this.cancelledEventSubject.next({ reason: 'Mining cancelled by user.' });
}

private initializeWorkers(): void {
private async initializeWorkers(): Promise<void> {
try {
//console.log('Initializing workers...');
////console.log('Initializing workers...');
const workers: Worker[] = [];
for (let i = 0; i < this.numberOfWorkers; i++) {
//console.log(`Creating worker ${i}`);
////console.log(`Creating worker ${i}`);
const worker = MineWorker();
worker.onmessage = this.handleWorkerMessage.bind(this);
worker.onerror = this.handleWorkerError.bind(this);
Expand All @@ -193,10 +193,11 @@ export class Notemine {
});

workers.push(worker);
await new Promise(resolve => setTimeout(resolve, 100));
}

this.workers$.next(workers);
//console.log(`Initialized ${workers.length} workers.`);
////console.log(`Initialized ${workers.length} workers.`);
} catch (error) {
this.errorSubject.next({ error });
console.error('Error initializing workers:', error);
Expand All @@ -207,10 +208,10 @@ export class Notemine {
const data = e.data;
const { type, workerId, hashRate } = data;

//console.log('Message from worker:', data);
////console.log('Message from worker:', data);

if (type === 'initialized') {
//console.log(`Worker ${workerId} initialized:`, data.message);
////console.log(`Worker ${workerId} initialized:`, data.message);
} else if (type === 'progress') {
let bestPowData: BestPowData | undefined;

Expand All @@ -221,11 +222,11 @@ export class Notemine {
workersPow[workerId] = bestPowData;
this.workersPow$.next(workersPow);

console.log(`Worker ${workerId} best PoW: ${bestPowData.bestPow}`);
//console.log(`Worker ${workerId} best PoW: ${bestPowData.bestPow}`);

const highestPow = this.highestPow$.getValue()

console.log(`Highest PoW: ${highestPow?.bestPow}`);
//console.log(`Highest PoW: ${highestPow?.bestPow}`);

if (!highestPow || (bestPowData && bestPowData.bestPow > (highestPow?.bestPow || 0))) {
this.highestPow$.next({
Expand All @@ -239,7 +240,7 @@ export class Notemine {

this.progressSubject.next({ workerId, hashRate, bestPowData });
} else if (type === 'result') {
//console.log('Mining result received:', data.data);
////console.log('Mining result received:', data.data);
this.result$.next(data.data);
this.mining$.next(false);

Expand Down Expand Up @@ -289,7 +290,7 @@ export class Notemine {
}

private async recordMaxRate(workerId: number, hashRate: number){
console.log(`Worker ${workerId} hash rate: ${Math.round(hashRate/1000)}`);
//console.log(`Worker ${workerId} hash rate: ${Math.round(hashRate/1000)}`);
const maxHashRate = this._workerMaxHashRates.get(workerId);
if (maxHashRate === undefined || hashRate > maxHashRate) {
this._workerMaxHashRates.set(workerId, Math.round(hashRate));
Expand All @@ -309,7 +310,7 @@ export class Notemine {
return;
}

console.log(`Refreshing hash rate... total: ${this.totalHashRate}`);
//console.log(`Refreshing hash rate... total: ${this.totalHashRate}`);

let totalRate = 0;
this._workerHashRates.forEach((hashRates) => {
Expand Down
20 changes: 10 additions & 10 deletions src/mine.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let workerId: number;
let miningCancelled = false;

const destructureBestPowData = (data: BestPowDataMap | any): BestPowData => {
//console.log('Destructuring bestPowData:', data, typeof data, data.get ? 'has get' : 'no get');
////console.log('Destructuring bestPowData:', data, typeof data, data.get ? 'has get' : 'no get');

let bestPow: number;
let nonce: string;
Expand Down Expand Up @@ -45,13 +45,13 @@ self.postMessage({ type: 'initialized', message: 'Worker initialized successfull
self.onmessage = async function (e: MessageEvent) {

if (e?.data?.name) {
//console.log("Ignoring injected message:", e.data);
////console.log("Ignoring injected message:", e.data);
return;
}
//console.log('Worker received message:', e.data);
////console.log('Worker received message:', e.data);
try {
const { type, event, difficulty, id, totalWorkers } = e.data;
//console.log(e.data)
////console.log(e.data)

// return

Expand All @@ -66,15 +66,15 @@ self.onmessage = async function (e: MessageEvent) {
mining = true;

try {
//console.log('Initializing WASM...');
////console.log('Initializing WASM...');
await initWasm(wasm);
//console.log('WASM Initialized successfully.');
////console.log('WASM Initialized successfully.');

const startNonce = BigInt(workerId);
const nonceStep = BigInt(totalWorkers);

const reportProgress = (hashRate: number, bestPowData: any) => {
//console.log('Progress:', hashRate, bestPowData);
////console.log('Progress:', hashRate, bestPowData);
const message: any = {
type: 'progress',
workerId,
Expand All @@ -86,7 +86,7 @@ self.onmessage = async function (e: MessageEvent) {

const shouldCancel = () => miningCancelled;

//console.log('Starting mining with event:', event, 'difficulty:', difficulty);
////console.log('Starting mining with event:', event, 'difficulty:', difficulty);
const minedResult = mine_event(
event,
difficulty,
Expand All @@ -96,7 +96,7 @@ self.onmessage = async function (e: MessageEvent) {
shouldCancel
);

//console.log('Mining completed successfully:', minedResult);
////console.log('Mining completed successfully:', minedResult);
self.postMessage({ type: 'result', data: minedResult, workerId });
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
Expand All @@ -107,7 +107,7 @@ self.onmessage = async function (e: MessageEvent) {
}
} else if (type === 'cancel') {
miningCancelled = true;
//console.log('Mining cancelled by user.');
////console.log('Mining cancelled by user.');
}
} catch (err: any) {
const errorMessage = err.message || 'Unknown error occurred in worker';
Expand Down

0 comments on commit 7a6f6a7

Please sign in to comment.