Try to improve performance

This commit is contained in:
Boris 2017-12-26 19:46:19 +01:00
parent 52f4f2be3f
commit 0771c655ef
3 changed files with 41 additions and 17 deletions

File diff suppressed because one or more lines are too long

View file

@ -5,6 +5,7 @@ const vanity = require('./vanity');
let count = 0; let count = 0;
let stop = false; let stop = false;
let lastTick = null; let lastTick = null;
let firstTick = null;
let difficulty = 0; let difficulty = 0;
const step = 250; const step = 250;
const elements = {}; const elements = {};
@ -41,7 +42,7 @@ const incrementCounter = incr => {
elements.counter.innerText = count.toString() + (count === 1 ? ' address' : ' addresses'); elements.counter.innerText = count.toString() + (count === 1 ? ' address' : ' addresses');
const currentTick = performance.now(); const currentTick = performance.now();
elements.speed.innerText = Math.floor(1000 * incr / (currentTick - lastTick)) + ' addr/s'; elements.speed.innerText = incr > 0 ? Math.floor(1000 * incr / (currentTick - lastTick)) + ' addr/s' : '0 addr/s';
lastTick = currentTick; lastTick = currentTick;
}; };
@ -52,16 +53,24 @@ const updateStats = () => {
}; };
const displayResult = result => { const displayResult = result => {
incrementCounter(result ? result.attempts : 0); incrementCounter(result.attempts);
document.getElementById('address').innerText = result.address;
document.getElementById('private-key').innerText = result.privKey;
elements.status.innerText = 'Address found';
console.info('Average speed: ' + Math.floor(1000 * count / (performance.now() - firstTick)) + ' addr/s');
updateStats(); updateStats();
document.getElementById('address').innerText = result ? result.address : '';
document.getElementById('private-key').innerText = result ? result.privKey : '';
elements.status.innerText = result ? 'Address found' : 'Running';
}; };
const toggleButtons = genBtn => { const clearResult = () => {
const enabled = genBtn ? elements.genBtn : elements.stopBtn; document.getElementById('address').innerText = '';
const disabled = genBtn ? elements.stopBtn : elements.genBtn; document.getElementById('private-key').innerText = '';
elements.status.innerText = 'Running';
updateStats();
};
const toggleButtons = () => {
const enabled = stop ? elements.genBtn : elements.stopBtn;
const disabled = stop ? elements.stopBtn : elements.genBtn;
enabled.removeAttribute('disabled'); enabled.removeAttribute('disabled');
disabled.setAttribute('disabled', ''); disabled.setAttribute('disabled', '');
}; };
@ -69,7 +78,8 @@ const toggleButtons = genBtn => {
const generate = input => { const generate = input => {
const add = vanity.getVanityWallet(input.prefix, input.checksum, step); const add = vanity.getVanityWallet(input.prefix, input.checksum, step);
if (add !== null) { if (add !== null) {
toggleButtons(true); stop = true;
toggleButtons();
return displayResult(add); return displayResult(add);
} }
@ -91,19 +101,20 @@ for (const e in ids) { // eslint-disable-line guard-for-in
// Add event listeners on buttons // Add event listeners on buttons
elements.genBtn.addEventListener('click', () => { elements.genBtn.addEventListener('click', () => {
firstTick = performance.now();
incrementCounter(-count); incrementCounter(-count);
displayResult(null); clearResult();
stop = false;
const input = parseInput(); const input = parseInput();
toggleButtons(false); stop = false;
toggleButtons();
setTimeout(() => generate(input), 0); setTimeout(() => generate(input), 0);
}); });
elements.stopBtn.addEventListener('click', () => { elements.stopBtn.addEventListener('click', () => {
toggleButtons(true);
stop = true; stop = true;
toggleButtons();
}); });
elements.form.addEventListener('change', () => parseInput()); elements.form.addEventListener('change', () => parseInput());

View file

@ -32,9 +32,22 @@ const isValidHex = hex => hex.length ? /^[0-9A-F]+$/g.test(hex.toUpperCase()) :
* @returns {boolean} * @returns {boolean}
*/ */
const isValidVanityWallet = (wallet, input, isChecksum) => { const isValidVanityWallet = (wallet, input, isChecksum) => {
let address = wallet.address; if (!isChecksum) {
address = isChecksum ? ethUtils.toChecksumAddress(address) : address; return wallet.address.substr(2, input.length) === input;
return address.substr(2, input.length) === input; }
const address = ethUtils.stripHexPrefix(wallet.address).toLowerCase();
const hash = ethUtils.sha3(address).toString('hex');
for (let i = 0; i < input.length; i++) {
if (parseInt(hash[i], 16) >= 8) {
if (address[i].toUpperCase() !== input[i]) {
return false;
}
} else if (address[i] !== input[i]) {
return false;
}
}
return true;
}; };
const computeDifficulty = (pattern, isChecksum) => { const computeDifficulty = (pattern, isChecksum) => {