Show pow speed, and estimate remaining time

This commit is contained in:
Thomas Lynch
2022-10-02 22:11:38 +11:00
parent 64e26f65b5
commit 4dd6eec87e
3 changed files with 24 additions and 1 deletions

View File

@ -1,9 +1,15 @@
function updateElem(selector, text) {
document.querySelector(selector)
.innerText = text;
}
function insertError(str) {
const ring = document.querySelector('.lds-ring');
const captcha = document.querySelector('#captcha');
(ring || captcha).insertAdjacentHTML('afterend', `<p class="red">Error: ${str}</p>`);
ring && ring.remove();
captcha && captcha.remove();
updateElem('.powstatus', '');
}
function finishRedirect() {
@ -65,6 +71,7 @@ const powFinished = new Promise((resolve, reject) => {
type: argon2.ArgonType.Argon2id,
};
console.log('Got pow', pow, 'with difficulty', diff);
const eHashes = Math.pow(16, Math.floor(diff/8)) * ((diff%8)*2);
const diffString = '0'.repeat(Math.floor(diff/8));
const combined = pow;
const [userkey, challenge, signature] = combined.split("#");
@ -77,8 +84,17 @@ const powFinished = new Promise((resolve, reject) => {
const workerThreads = isTor ? cpuThreads : Math.max(Math.ceil(cpuThreads/2),cpuThreads-1);
let finished = false;
const messageHandler = (e) => {
if (e.data.length === 1) {
const totalHashes = e.data[0]*workerThreads; //assumes all worker threads are same speed
const elapsedSec = Math.floor((Date.now()-start)/1000);
const hps = Math.floor(totalHashes/elapsedSec);
const requiredSec = Math.floor(eHashes/hps) * 1.5; //estimate 1.5x time
return updateElem('.powstatus', `Proof-of-work: ${hps}H/s, ~${requiredSec-elapsedSec}s remaining.`);
}
if (finished) { return; }
finished = true;
const hasCaptcha = document.getElementById('captcha');
updateElem('.powstatus', `Found proof-of-work solution.${!hasCaptcha?' Submitting...':''}`);
workers.forEach(w => w.terminate());
const [workerId, answer] = e.data;
console.log('Worker', workerId, 'returned answer', answer, 'in', Date.now()-start+'ms');