SenShaMart/blockchain/block.test.js

34 lines
No EOL
966 B
JavaScript

const Block = require('./block');
describe('Block', () => {
let data, lastBlock, block;
beforeEach(() => {
reward = 'bar';
lastBlock = Block.genesis();
block = Block.debugMine(lastBlock, reward,[],[]);
});
it('sets the `data` to match the input', () => {
expect(block.reward).toEqual(reward);
});
it('sets the `lastHash` to match the hash of the last block', () => {
expect(block.lastHash).toEqual(lastBlock.hash);
});
it('generates a hash that matches the difficulty', () => {
expect(block.hash.substring(0, block.difficulty))
.toEqual('0'.repeat(block.difficulty));
});
it('lowers the difficulty for slowly mined blocks', () => {
expect(Block.adjustDifficulty(block, block.timestamp+360000))
.toEqual(block.difficulty-1);
});
it('raises the difficulty for quickly mined blocks', () => {
expect(Block.adjustDifficulty(block, block.timestamp+1))
.toEqual(block.difficulty+1);
});
});