|
|
@ -8,7 +8,8 @@ const {
|
|
|
|
greaterThan,
|
|
|
|
greaterThan,
|
|
|
|
} = pow;
|
|
|
|
} = pow;
|
|
|
|
|
|
|
|
|
|
|
|
describe('Proof of Work Worker', () => {
|
|
|
|
describe('Proof of Work', () => {
|
|
|
|
|
|
|
|
describe('#incrementNonce', () => {
|
|
|
|
it('should increment a Uint8Array nonce correctly', () => {
|
|
|
|
it('should increment a Uint8Array nonce correctly', () => {
|
|
|
|
const arr1Before = new Uint8Array([0,0,0,0,0,0,0,0]);
|
|
|
|
const arr1Before = new Uint8Array([0,0,0,0,0,0,0,0]);
|
|
|
|
const arr1After = incrementNonce(arr1Before);
|
|
|
|
const arr1After = incrementNonce(arr1Before);
|
|
|
@ -22,7 +23,7 @@ describe('Proof of Work Worker', () => {
|
|
|
|
assert.strictEqual(arr1After[7], 1);
|
|
|
|
assert.strictEqual(arr1After[7], 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should increment a Uint8Array nonce correctly', () => {
|
|
|
|
it('should increment a Uint8Array nonce correctly in a loop', () => {
|
|
|
|
let arr = new Uint8Array([0,0,0,0,0,0,0,0]);
|
|
|
|
let arr = new Uint8Array([0,0,0,0,0,0,0,0]);
|
|
|
|
assert.deepEqual(incrementNonce(arr), new Uint8Array([0,0,0,0,0,0,0,1]));
|
|
|
|
assert.deepEqual(incrementNonce(arr), new Uint8Array([0,0,0,0,0,0,0,1]));
|
|
|
|
arr = new Uint8Array([0,0,0,0,0,0,0,0]);
|
|
|
|
arr = new Uint8Array([0,0,0,0,0,0,0,0]);
|
|
|
@ -33,7 +34,9 @@ describe('Proof of Work Worker', () => {
|
|
|
|
arr = new Uint8Array([255,255,255,255,255,255,255,255]);
|
|
|
|
arr = new Uint8Array([255,255,255,255,255,255,255,255]);
|
|
|
|
assert.deepEqual(incrementNonce(arr), new Uint8Array([0,0,0,0,0,0,0,0]));
|
|
|
|
assert.deepEqual(incrementNonce(arr), new Uint8Array([0,0,0,0,0,0,0,0]));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('#calcTarget', () => {
|
|
|
|
it('should calculate a correct difficulty target', () => {
|
|
|
|
it('should calculate a correct difficulty target', () => {
|
|
|
|
// These values will need to be updated if we adjust the difficulty settings
|
|
|
|
// These values will need to be updated if we adjust the difficulty settings
|
|
|
|
let payloadLen = 625;
|
|
|
|
let payloadLen = 625;
|
|
|
@ -47,7 +50,9 @@ describe('Proof of Work Worker', () => {
|
|
|
|
actualTarget = calcTarget(ttl, payloadLen, 10);
|
|
|
|
actualTarget = calcTarget(ttl, payloadLen, 10);
|
|
|
|
assert.deepEqual(actualTarget, expectedTarget);
|
|
|
|
assert.deepEqual(actualTarget, expectedTarget);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('#greaterThan', () => {
|
|
|
|
it('should correclty compare two Uint8Arrays', () => {
|
|
|
|
it('should correclty compare two Uint8Arrays', () => {
|
|
|
|
let arr1 = new Uint8Array([0,0,0,0,0,0,0,0,0,1]);
|
|
|
|
let arr1 = new Uint8Array([0,0,0,0,0,0,0,0,0,1]);
|
|
|
|
let arr2 = new Uint8Array([0,0,0,0,0,0,0,0,0,1]);
|
|
|
|
let arr2 = new Uint8Array([0,0,0,0,0,0,0,0,0,1]);
|
|
|
@ -65,7 +70,9 @@ describe('Proof of Work Worker', () => {
|
|
|
|
arr2 = new Uint8Array([0,0]);
|
|
|
|
arr2 = new Uint8Array([0,0]);
|
|
|
|
assert.isFalse(greaterThan(arr1, arr2))
|
|
|
|
assert.isFalse(greaterThan(arr1, arr2))
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('#bufferToBase64', () => {
|
|
|
|
it('should correclty convert a Uint8Array to a base64 string', () => {
|
|
|
|
it('should correclty convert a Uint8Array to a base64 string', () => {
|
|
|
|
let arr = new Uint8Array([1,2,3]);
|
|
|
|
let arr = new Uint8Array([1,2,3]);
|
|
|
|
let expected = 'AQID';
|
|
|
|
let expected = 'AQID';
|
|
|
@ -77,7 +84,9 @@ describe('Proof of Work Worker', () => {
|
|
|
|
expected = '';
|
|
|
|
expected = '';
|
|
|
|
assert.strictEqual(bufferToBase64(arr), expected);
|
|
|
|
assert.strictEqual(bufferToBase64(arr), expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('#bigIntToUint8Array', () => {
|
|
|
|
it('should correclty convert a BigInteger to a Uint8Array', () => {
|
|
|
|
it('should correclty convert a BigInteger to a Uint8Array', () => {
|
|
|
|
let bigInt = JSBI.BigInt(Number.MAX_SAFE_INTEGER);
|
|
|
|
let bigInt = JSBI.BigInt(Number.MAX_SAFE_INTEGER);
|
|
|
|
let expected = new Uint8Array([0, 31, 255, 255, 255, 255, 255, 255]);
|
|
|
|
let expected = new Uint8Array([0, 31, 255, 255, 255, 255, 255, 255]);
|
|
|
@ -92,4 +101,5 @@ describe('Proof of Work Worker', () => {
|
|
|
|
expected = new Uint8Array([0, 0, 0, 0, 0, 0, 1, 0]);
|
|
|
|
expected = new Uint8Array([0, 0, 0, 0, 0, 0, 1, 0]);
|
|
|
|
assert.deepEqual(bigIntToUint8Array(bigInt), expected);
|
|
|
|
assert.deepEqual(bigIntToUint8Array(bigInt), expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|