Skip to content

Commit

Permalink
Difficulty and the Nonce Value | Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Sep 27, 2022
1 parent 116724d commit ac9cda9
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions block.test.js
Expand Up @@ -7,13 +7,17 @@ describe('Block', () => {
const lastHash = 'foo-hash';
const hash = 'bar-hash';
const data = ['blockchain', 'data'];
const block = new Block({ timestamp, lastHash, hash, data });
const nonce = 1;
const difficulty = 1;
const block = new Block({ timestamp, lastHash, hash, data, nonce, difficulty });

it('has a timestamp, lastHash, hash, and a data property', () => {
expect(block.timestamp).toEqual(timestamp);
expect(block.lastHash).toEqual(lastHash);
expect(block.hash).toEqual(hash);
expect(block.data).toEqual(data);
expect(block.nonce).toEqual(nonce);
expect(block.difficulty).toEqual(difficulty);
});

describe('genesis()', () => {
Expand Down Expand Up @@ -51,7 +55,20 @@ describe('Block', () => {

it('creates a SHA-256 `hash` based on the proper inputs', () => {
expect(minedBlock.hash)
.toEqual(cryptoHash(minedBlock.timestamp, lastBlock.hash, data));
.toEqual(
cryptoHash(
minedBlock.timestamp,
minedBlock.nonce,
minedBlock.difficulty,
lastBlock.hash,
data
)
);
});

it('sets a `hash` that matches the difficulty criteria', () => {
expect(minedBlock.hash.substring(0, minedBlock.difficulty))
.toEqual('0'.repeat(minedBlock.difficulty));
});
});
});

0 comments on commit ac9cda9

Please sign in to comment.