From c2a16e49bdebc3f41d7e877f0f2908efb48e747a Mon Sep 17 00:00:00 2001 From: Justin Ferrari <‘justinwesleyferrari@gmail.com’> Date: Fri, 16 Dec 2022 12:56:51 -0500 Subject: [PATCH] Added webworker file to q-chat.src --- .../messaging/q-chat/computePowWorker.src.js | 82 +++++++++++++++++++ .../core/messaging/q-chat/q-chat.src.js | 2 +- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 qortal-ui-plugins/plugins/core/messaging/q-chat/computePowWorker.src.js diff --git a/qortal-ui-plugins/plugins/core/messaging/q-chat/computePowWorker.src.js b/qortal-ui-plugins/plugins/core/messaging/q-chat/computePowWorker.src.js new file mode 100644 index 00000000..2ed60a20 --- /dev/null +++ b/qortal-ui-plugins/plugins/core/messaging/q-chat/computePowWorker.src.js @@ -0,0 +1,82 @@ +import { Sha256 } from 'asmcrypto.js' + + +function sbrk(size, heap){ + let brk = 512 * 1024 // stack top + let old = brk + brk += size + + if (brk > heap.length) + throw new Error('heap exhausted') + + return old +} + + + +self.addEventListener('message', async e => { + const response = await computePow(e.data.chatBytes, e.data.path, e.data.difficulty) + postMessage(response) + +}) + + +const memory = new WebAssembly.Memory({ initial: 256, maximum: 256 }) +const heap = new Uint8Array(memory.buffer) + + + +const computePow = async (chatBytes, path, difficulty) => { + + let response = null + + await new Promise((resolve, reject)=> { + + const _chatBytesArray = Object.keys(chatBytes).map(function (key) { return chatBytes[key]; }); + const chatBytesArray = new Uint8Array(_chatBytesArray); + const chatBytesHash = new Sha256().process(chatBytesArray).finish().result; + const hashPtr = sbrk(32, heap); + const hashAry = new Uint8Array(memory.buffer, hashPtr, 32); + hashAry.set(chatBytesHash); + + + const workBufferLength = 8 * 1024 * 1024; + const workBufferPtr = sbrk(workBufferLength, heap); + + + + const importObject = { + env: { + memory: memory + }, + }; + + function loadWebAssembly(filename, imports) { + // Fetch the file and compile it + return fetch(filename) + .then(response => response.arrayBuffer()) + .then(buffer => WebAssembly.compile(buffer)) + .then(module => { + + // Create the instance. + return new WebAssembly.Instance(module, importObject); + }); +} + + +loadWebAssembly(path) + .then(wasmModule => { + response = { + nonce : wasmModule.exports.compute2(hashPtr, workBufferPtr, workBufferLength, difficulty), + chatBytesArray + } + + resolve() + + }); + + + }) + + return response +} \ No newline at end of file diff --git a/qortal-ui-plugins/plugins/core/messaging/q-chat/q-chat.src.js b/qortal-ui-plugins/plugins/core/messaging/q-chat/q-chat.src.js index b980f2ff..c44919c5 100644 --- a/qortal-ui-plugins/plugins/core/messaging/q-chat/q-chat.src.js +++ b/qortal-ui-plugins/plugins/core/messaging/q-chat/q-chat.src.js @@ -2,7 +2,7 @@ import { LitElement, html, css } from 'lit'; import { render } from 'lit/html.js'; import { Epml } from '../../../../epml.js'; import { use, get, translate, translateUnsafeHTML, registerTranslateConfig } from 'lit-translate'; -import WebWorker from 'web-worker:./computePowWorker.js'; +import WebWorker from 'web-worker:./computePowWorker.src.js'; registerTranslateConfig({ loader: lang => fetch(`/language/${lang}.json`).then(res => res.json())