re-lint proof-of-work

pull/25/head
sachaaaaa 7 years ago
parent 8a354c8e5c
commit bf69e9e897

@ -22,8 +22,8 @@ function incrementNonce(nonce) {
// Convert a Uint8Array to a base64 string // Convert a Uint8Array to a base64 string
function bufferToBase64(buf) { function bufferToBase64(buf) {
function mapFn(ch) { function mapFn(ch) {
return String.fromCharCode(ch); return String.fromCharCode(ch);
}; }
const binaryString = Array.prototype.map.call(buf, mapFn).join(''); const binaryString = Array.prototype.map.call(buf, mapFn).join('');
return bb.btoa(binaryString); return bb.btoa(binaryString);
} }
@ -36,7 +36,9 @@ function bigIntToUint8Array(bigInt) {
n = NONCE_LEN - (idx + 1); n = NONCE_LEN - (idx + 1);
// 256 ** n is the value of one bit in arr[idx], modulus to carry over // 256 ** n is the value of one bit in arr[idx], modulus to carry over
// (bigInt / 256**n) % 256; // (bigInt / 256**n) % 256;
const uint8Val = (bigInt.divide((new BigInteger('256')).pow(n))).mod(new BigInteger('256')); const uint8Val = bigInt
.divide(new BigInteger('256').pow(n))
.mod(new BigInteger('256'));
arr[idx] = uint8Val.intValue(); arr[idx] = uint8Val.intValue();
} }
return arr; return arr;
@ -45,14 +47,11 @@ function bigIntToUint8Array(bigInt) {
// Compare two Uint8Arrays, return true if arr1 is > arr2 // Compare two Uint8Arrays, return true if arr1 is > arr2
function greaterThan(arr1, arr2) { function greaterThan(arr1, arr2) {
// Early exit if lengths are not equal. Should never happen // Early exit if lengths are not equal. Should never happen
if (arr1.length !== arr2.length) if (arr1.length !== arr2.length) return false;
return false;
for (let i = 0, len = arr1.length; i < len; i += 1) { for (let i = 0, len = arr1.length; i < len; i += 1) {
if (arr1[i] > arr2[i]) if (arr1[i] > arr2[i]) return true;
return true; if (arr1[i] < arr2[i]) return false;
if (arr1[i] < arr2[i])
return false;
} }
return false; return false;
} }
@ -60,7 +59,9 @@ function greaterThan(arr1, arr2) {
// Return nonce that hashes together with payload lower than the target // Return nonce that hashes together with payload lower than the target
function calcPoW(timestamp, ttl, pubKey, data) { function calcPoW(timestamp, ttl, pubKey, data) {
const leadingString = timestamp.toString() + ttl.toString() + pubKey; const leadingString = timestamp.toString() + ttl.toString() + pubKey;
const leadingArray = new Uint8Array(bb.wrap(leadingString, 'binary').toArrayBuffer()); const leadingArray = new Uint8Array(
bb.wrap(leadingString, 'binary').toArrayBuffer()
);
// Payload constructed from concatenating timestamp, ttl and pubkey strings, // Payload constructed from concatenating timestamp, ttl and pubkey strings,
// converting to Uint8Array and then appending to the message data array // converting to Uint8Array and then appending to the message data array
const payload = new Uint8Array(leadingArray.length + data.length); const payload = new Uint8Array(leadingArray.length + data.length);
@ -68,24 +69,34 @@ function calcPoW(timestamp, ttl, pubKey, data) {
payload.set(data, leadingArray.length); payload.set(data, leadingArray.length);
// payloadLength + NONCE_LEN // payloadLength + NONCE_LEN
const totalLen = (new BigInteger(payload.length.toString())).add(new BigInteger(NONCE_LEN.toString())); const totalLen = new BigInteger(payload.length.toString()).add(
new BigInteger(NONCE_LEN.toString())
);
// ttl * totalLen // ttl * totalLen
const ttlMult = (new BigInteger(ttl.toString())).multiply(totalLen); const ttlMult = new BigInteger(ttl.toString()).multiply(totalLen);
// ttlMult / (2^16 - 1) // ttlMult / (2^16 - 1)
const innerFrac = ttlMult.divide((new BigInteger('2').pow(16)).subtract(new BigInteger('1'))); const innerFrac = ttlMult.divide(
new BigInteger('2').pow(16).subtract(new BigInteger('1'))
);
// totalLen + innerFrac // totalLen + innerFrac
const lenPlusInnerFrac = totalLen.add(innerFrac); const lenPlusInnerFrac = totalLen.add(innerFrac);
// NONCE_TRIALS * lenPlusInnerFrac // NONCE_TRIALS * lenPlusInnerFrac
const denominator = (new BigInteger(NONCE_TRIALS.toString())).multiply(lenPlusInnerFrac); const denominator = new BigInteger(NONCE_TRIALS.toString()).multiply(
lenPlusInnerFrac
);
// 2^64 - 1 // 2^64 - 1
const two64 = (new BigInteger('2').pow(64)).subtract(new BigInteger('1')); const two64 = new BigInteger('2').pow(64).subtract(new BigInteger('1'));
// two64 / denominator // two64 / denominator
const targetNum = two64.divide(denominator); const targetNum = two64.divide(denominator);
const target = bigIntToUint8Array(targetNum); const target = bigIntToUint8Array(targetNum);
let nonce = new Uint8Array(NONCE_LEN); let nonce = new Uint8Array(NONCE_LEN);
let trialValue = bigIntToUint8Array(new BigInteger(Number.MAX_SAFE_INTEGER.toString())); let trialValue = bigIntToUint8Array(
const initialHash = new Uint8Array(bb.wrap(hash(payload), 'hex').toArrayBuffer()); new BigInteger(Number.MAX_SAFE_INTEGER.toString())
);
const initialHash = new Uint8Array(
bb.wrap(hash(payload), 'hex').toArrayBuffer()
);
const innerPayload = new Uint8Array(initialHash.length + NONCE_LEN); const innerPayload = new Uint8Array(initialHash.length + NONCE_LEN);
innerPayload.set(initialHash, NONCE_LEN); innerPayload.set(initialHash, NONCE_LEN);
let resultHash; let resultHash;
@ -93,12 +104,21 @@ function calcPoW(timestamp, ttl, pubKey, data) {
nonce = incrementNonce(nonce); nonce = incrementNonce(nonce);
innerPayload.set(nonce); innerPayload.set(nonce);
resultHash = hash(innerPayload); resultHash = hash(innerPayload);
trialValue = (new Uint8Array(bb.wrap(resultHash, 'hex').toArrayBuffer())).slice(0, NONCE_LEN); trialValue = new Uint8Array(
bb.wrap(resultHash, 'hex').toArrayBuffer()
).slice(0, NONCE_LEN);
} }
return bufferToBase64(nonce); return bufferToBase64(nonce);
} }
// Start calculation in child process when main process sends message data // Start calculation in child process when main process sends message data
process.on('message', (msg) => { process.on('message', msg => {
process.send({nonce: calcPoW(msg.timestamp, msg.ttl, msg.pubKey, new Uint8Array(msg.data))}); process.send({
nonce: calcPoW(
msg.timestamp,
msg.ttl,
msg.pubKey,
new Uint8Array(msg.data)
),
});
}); });

Loading…
Cancel
Save