-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.test.js
105 lines (90 loc) · 3.62 KB
/
block.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const hexToBinary = require('hex-to-binary'); //Trouver le nombre de zéro en binaire plutot qu'en hexadécimal
const Block = require('./block');
const cryptoHash = require('./crypto-hash');
const { GENESIS_DATA, MINE_RATE } = require('./config');
describe('Block', () => {
const timestamp = 2000;
const lastHash = 'foo-hash';
const hash = 'bar-hash';
const data = ['blockchain','data'];
const nonce = 1;
const difficulty = 1;
const block = new Block({
timestamp,
lastHash,
hash,
data,
nonce,
difficulty
});
it('has a timestamp, a lastHash, a hash, and 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()', () => {
const genesisBlock = Block.genesis();
console.log('genesisBlock', genesisBlock)
it('return a block instance', () => {
expect(genesisBlock instanceof Block).toBe(true);
});
it('return genesis data', () => {
expect(genesisBlock).toEqual(GENESIS_DATA);
});
});
describe('mineBlock()', () => {
const lastBlock = Block.genesis();
const data = "mined data";
const minedBlock = Block.mineBlock({lastBlock, data});
it('returns a Block instance', () => {
expect(minedBlock instanceof Block).toBe(true);
});
it('sets the "lastHash" to be the "hash" of the lastBlock', () => {
expect(minedBlock.lastHash).toEqual(lastBlock.hash);
});
it("set the 'data'", () => {
expect(minedBlock.data).toEqual(data);
});
it('set a "timestamp"', () => {
expect(minedBlock.timestamp).not.toEqual(undefined);
});
it('creates a SHA-256 `hash` based on the proper inputs', () => {
expect(minedBlock.hash)
.toEqual(
cryptoHash(
minedBlock.timestamp,
minedBlock.nonce,
minedBlock.difficulty,
lastBlock.hash,
data)
);
});
it('set a `hash` that matches the difficulty criteria', () => {
expect(hexToBinary(minedBlock.hash).substring(0, minedBlock.difficulty))
.toEqual('0'.repeat(minedBlock.difficulty));
});
it('adjust the difficulty', () => {
const possibleResults = [lastBlock.difficulty+1, lastBlock.difficulty-1];
expect(possibleResults.includes(minedBlock.difficulty)).toBe(true);
});
});
describe('adjustDifficulty()', ()=> {
it('raises the difficulty for a quickly mined block', () => {
expect(Block.adjustDifficulty({
originalBlock : block, timestamp: block.timestamp + MINE_RATE - 100
})).toEqual(block.difficulty+1);
});
it('lowers the difficulty for a slowly mined block', () => {
expect(Block.adjustDifficulty({
originalBlock : block, timestamp: block.timestamp + MINE_RATE + 100
})).toEqual(block.difficulty-1);
});
it('has a lower limit of 1', () => {
block.difficulty = -1;
expect(Block.adjustDifficulty({originalBlock: block})).toEqual(1);
});
});
});