-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify stuff with pointers + 'using' keyword
- Loading branch information
Benedikt Strehle
committed
Sep 23, 2023
1 parent
6a7715c
commit 35384f3
Showing
4 changed files
with
75 additions
and
50 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,7 @@ | |
flex-direction: column; | ||
} | ||
} | ||
&>.tor { | ||
&>#tor { | ||
span { | ||
max-width: 1000px; | ||
margin: auto; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,74 @@ | ||
import { Path } from "uix/utils/path.ts"; | ||
import { UIX } from "uix"; | ||
import { spawnThreads, spawnThread, disposeThread } from "unyt_core/threads/threads.ts"; | ||
import { spawnThreads, spawnThread } from "unyt_core/threads/threads.ts"; | ||
import { always, map } from "unyt_core/functions.ts"; | ||
import type { AddressData } from "common/TOR-Worker.ts"; | ||
|
||
@UIX.template(function(this: MainPage) { | ||
return <div> | ||
<div class="tor"> | ||
<h2>Multi-Threading TOR Address</h2> | ||
<input id="torAddress" maxlength={3} type={"text"} placeholder={"Prefix of vanity address"}/> | ||
<div id="tor" class={always(()=>this.calculatingAddress?'hidden':'')}> | ||
<h2>Create TOR Address</h2> | ||
<input id="torAddress" maxlength="3" type="text" placeholder="Prefix of vanity address" value={this.$.addressPrefix}/> | ||
<div onclick={() => this.createVanityAddress()} class="button">Compute</div> | ||
<section class="results"></section> | ||
<section class="results"> | ||
{map(this.resultAddresses, address => | ||
<span> | ||
<a>{address.address}</a> | ||
<b>Pub: {address.public.b64}</b> | ||
<b>Priv: {address.private.b64}</b> | ||
</span> | ||
)} | ||
</section> | ||
</div> | ||
<div class="pi"> | ||
<h2>How many digits of PI to calculate?</h2> | ||
<input id="inputPiDigits" type={"number"} placeholder={"Input number"}/> | ||
<div id="pi" class={always(()=>this.calculatingPI?'hidden':'')}> | ||
<h2>Calculate PI</h2> | ||
<input id="inputPiDigits" type="number" placeholder="Number of digits" value={this.$.piDigits}/> | ||
<div onclick={() => this.computePI()} class="button">Compute</div> | ||
<section class="results"></section> | ||
<section class="results"> | ||
{map(this.resultPIs, pi => | ||
<span>{pi}</span> | ||
)} | ||
</section> | ||
</div> | ||
</div> | ||
}) | ||
export class MainPage extends UIX.BaseComponent { | ||
@id declare inputPiDigits: HTMLInputElement; | ||
@id declare torAddress: HTMLInputElement; | ||
|
||
// references properties to read input values | ||
@property piDigits = 5; | ||
@property addressPrefix = ""; | ||
|
||
// reference properties to get calculation state | ||
@property calculatingPI = false | ||
@property calculatingAddress = false | ||
|
||
// arrays containing history of calculated results | ||
@property resultPIs:string[] = [] | ||
@property resultAddresses:AddressData[] = [] | ||
|
||
async createVanityAddress() { | ||
const parent = this.torAddress.parentElement!; | ||
if (parent.classList.contains("hidden")) | ||
return; | ||
parent.classList.add("hidden"); | ||
|
||
const threads = await spawnThreads<typeof import('../TOR-Worker.ts')>(new Path('../TOR-Worker.ts'), 10); | ||
const calculations = threads.map(thread => thread.generateVanityAddress(this.torAddress.value)); | ||
const result = await Promise.any(calculations); | ||
console.log("Found address", result); | ||
parent.querySelector("section")!.prepend(<span> | ||
<a>{result.address}</a> | ||
<b>Pub: {result.public.b64}</b> | ||
<b>Priv: {result.private.b64}</b> | ||
</span>) | ||
parent.classList.remove("hidden"); | ||
disposeThread(...threads); | ||
this.calculatingAddress = true; | ||
|
||
// spawn 10 new threads | ||
using threads = await spawnThreads<typeof import('../TOR-Worker.ts')>('../TOR-Worker.ts', 10); | ||
// try to find an address in parallel | ||
const address = await Promise.any( | ||
threads.map(thread => thread.generateVanityAddress(this.addressPrefix)) | ||
); | ||
this.resultAddresses.unshift(address); | ||
|
||
this.calculatingAddress = false; | ||
} | ||
|
||
async computePI() { | ||
const parent = this.inputPiDigits.parentElement!; | ||
if (parent.classList.contains("hidden")) | ||
return; | ||
parent.classList.add("hidden"); | ||
|
||
const thread = await spawnThread<typeof import('../PI-Worker.ts')>(new Path('../PI-Worker.ts')); | ||
const pi = await thread.calculatePI(+this.inputPiDigits.value || 10); | ||
parent.querySelector("section")!.prepend(<span>{pi}</span>) | ||
parent.classList.remove("hidden"); | ||
disposeThread(thread); | ||
this.calculatingPI = true; | ||
|
||
// spawn a new thread | ||
using thread = await spawnThread<typeof import('../PI-Worker.ts')>('../PI-Worker.ts'); | ||
// calculate pi in the thread | ||
const pi = await thread.calculatePI(this.piDigits); | ||
this.resultPIs.unshift(pi); | ||
|
||
this.calculatingPI = false; | ||
|
||
} | ||
} | ||
} |