feat: yey
This commit is contained in:
parent
b075fd9807
commit
236eef732a
2
.env
2
.env
|
@ -1,2 +1,2 @@
|
|||
NEXT_PUBLIC_DEFAULT_NANO_NODE_RPC=https://rainstorm.city/api
|
||||
NEXT_PUBLIC_DEFAULT_NANO_NODE_RPC=https://proxy.nanos.cc/proxy
|
||||
NEXT_PUBLIC_DEFAULT_NANO_NODE_WS=wss://rainstorm.city/websocket
|
|
@ -1,6 +1,10 @@
|
|||
import fetcher from './fetcher'
|
||||
import { WorkGenerateResponse } from './types'
|
||||
import { defaultUrls } from './xno/constants'
|
||||
|
||||
const computeWorkAsync = (
|
||||
frontier: string,
|
||||
{ send, workerCount = 4 }: { send: boolean; workerCount?: number }
|
||||
hash: string,
|
||||
{ send, workerCount }: { send: boolean; workerCount?: number }
|
||||
) => {
|
||||
const workers: Worker[] = []
|
||||
|
||||
|
@ -10,25 +14,25 @@ const computeWorkAsync = (
|
|||
}
|
||||
const abortController = new AbortController()
|
||||
|
||||
const onlineWorkPromise =
|
||||
process.env.NODE_ENV === 'production'
|
||||
? fetch(`/api/computeWork?frontier=${frontier}`, {
|
||||
const onlineWorkPromise = fetcher<WorkGenerateResponse>(defaultUrls.rpc, {
|
||||
signal: abortController.signal,
|
||||
method: 'POST',
|
||||
body: {
|
||||
action: 'work_generate',
|
||||
hash,
|
||||
},
|
||||
}).then(async res => {
|
||||
if (res.status !== 200) throw new Error()
|
||||
else {
|
||||
const { work } = await res.json()
|
||||
const { work } = res
|
||||
return work as string
|
||||
}
|
||||
})
|
||||
: Promise.reject(
|
||||
'not in production, so not generating work on the server'
|
||||
)
|
||||
|
||||
const offlineWorkPromise = new Promise<string | null>((res, rej) => {
|
||||
if (navigator.onLine) {
|
||||
rej('prefer going to server')
|
||||
return
|
||||
}
|
||||
const maxWorkers =
|
||||
Math.floor(navigator.hardwareConcurrency / 4) ?? workerCount
|
||||
|
||||
workerCount ?? Math.max((navigator.hardwareConcurrency ?? 2) - 1, 1)
|
||||
const createWorker = (id: number) => {
|
||||
const worker = new Worker(new URL('./workComputer.ts', import.meta.url))
|
||||
worker.onmessage = work => {
|
||||
|
@ -40,11 +44,9 @@ const computeWorkAsync = (
|
|||
cleanup()
|
||||
rej(work)
|
||||
}
|
||||
|
||||
worker.postMessage({ frontier, id, send })
|
||||
worker.postMessage({ hash, id, send })
|
||||
return worker
|
||||
}
|
||||
|
||||
for (let i = 0; i < maxWorkers; i++) workers.push(createWorker(i))
|
||||
})
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const fetcher = <T>(
|
||||
input: Parameters<typeof fetch>[0],
|
||||
init?: Omit<Exclude<Parameters<typeof fetch>[1], undefined>, 'body'> & {
|
||||
method: 'POST' | 'PUT' | 'PATCH'
|
||||
method?: 'POST' | 'PUT' | 'PATCH'
|
||||
body?: any
|
||||
}
|
||||
) =>
|
||||
|
|
|
@ -118,3 +118,10 @@ export interface ConfirmationMessage {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface WorkGenerateResponse {
|
||||
work: string
|
||||
difficulty: string
|
||||
multiplier: string
|
||||
hash: string
|
||||
}
|
||||
|
|
|
@ -3,7 +3,11 @@ import { computeWork } from 'nanocurrency'
|
|||
import { receiveDiff, sendDiff } from './xno/constants'
|
||||
|
||||
onmessage = async ev => {
|
||||
const { send, id, frontier } = ev.data as {
|
||||
const {
|
||||
send,
|
||||
id,
|
||||
frontier: hash,
|
||||
} = ev.data as {
|
||||
send: boolean
|
||||
id: number
|
||||
frontier: string
|
||||
|
@ -11,12 +15,12 @@ onmessage = async ev => {
|
|||
console.log(`started calculating work`)
|
||||
console.table({
|
||||
workerId: id,
|
||||
frontier,
|
||||
frontier: hash,
|
||||
send,
|
||||
difficulty: send ? sendDiff : receiveDiff,
|
||||
startedAt: new Date(),
|
||||
})
|
||||
const work = await computeWork(frontier, {
|
||||
const work = await computeWork(hash, {
|
||||
workThreshold: send ? sendDiff : receiveDiff,
|
||||
})
|
||||
console.log(`worker ${id} finished computing work: ${work}`)
|
||||
|
|
Reference in a new issue