-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.test.js
More file actions
89 lines (77 loc) · 3.26 KB
/
block.test.js
File metadata and controls
89 lines (77 loc) · 3.26 KB
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
const Block= require('./block');
const {GENESIS_DATA,MINE_RATE}= require("./config");
const cryptoHash=require('./crypto-hash');
const hexToBinary=require('hex-to-binary');
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,lastHash,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 blockgenesis= Block.genesis()
it("return a block instance ", ()=>{
expect(blockgenesis instanceof Block).toBe(true);
});
it("returns the genesis `data`", ()=>{
expect(blockgenesis).toEqual(GENESIS_DATA);
});
});
describe("minedblock", ()=>{
const lastblock=Block.genesis();
const data="data mined";
const minedblock=Block.minedBlock({lastblock, data});
it("return a block instance ", ()=>{
expect(minedblock instanceof Block).toBe(true);
});
it("let `lasthash` be `hash` of the previous block", ()=>{
expect(minedblock.lastHash).toEqual(lastblock.hash);
});
it("set the `data`", ()=>{
expect(minedblock.data).toEqual(data);
});
it("set the `timestamp`", ()=>{
expect(minedblock.timestamp).not.toEqual(undefined);
});
it("create sha256 `hash` based on proper input",()=>{
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);
});
it("set the `difficulty`", ()=>{
expect(lastblock.difficulty).not.toEqual(null);
});
});
describe('raisedDifficulty', () => {
it('it raises dificulty level',()=>{
expect(Block.adjustDifficulty({
originalBlock:block, timestamp:block.timestamp+MINE_RATE -100
})).toEqual(block.difficulty+1);
});
it('it lowers the dificulty level',()=>{
expect(Block.adjustDifficulty({
originalBlock:block, timestamp:block.timestamp+MINE_RATE+1000
})).toEqual(block.difficulty-1);
});
it('has a limit of 1',()=>{
block.difficulty=-1
expect(Block.adjustDifficulty({originalBlock:block})).toEqual(1);
})
})
});