-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.test.js
More file actions
152 lines (120 loc) · 5.46 KB
/
blockchain.test.js
File metadata and controls
152 lines (120 loc) · 5.46 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const Blockchain= require('./blockchain');
const Block=require('./block');
const cryptoHash=require('./crypto-hash');
describe("Blockchain", ()=>{
let blockchain, newChain;
beforeEach(()=>{
blockchain=new Blockchain();
newChain= new Blockchain();
originalChain=blockchain.chain;
})
// it('contain a chain of array instance', ()=>{
// expect(blockchain.chain instanceof Array).toBe(true);
// });
it('start with the genesis block', ()=>{
expect(blockchain.chain[0]).toEqual(Block.genesis());
});
it("adds a new block to the chain", ()=>{
const newData="foo bar"
blockchain.addBlock({data:newData});
expect(blockchain.chain[blockchain.chain.length-1].data).toEqual(newData);
});
describe('isValidChain()', ()=>{
describe('when the chain does not start with the genesis block',()=>{
it('return false', ()=>{
blockchain.chain[0]={data:'fake-genesis'};
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
});
});
describe('when the chain start with the genesis block and has multiple blocks', () => {
describe('the lastHash refrence has changed', () => {
it('return false',()=>{
blockchain.addBlock({data:'Bears'});
blockchain.addBlock({data:"beets"});
blockchain.addBlock({data:"battlestar Galtico"});
blockchain.chain[2].lastHash='broken-lastHash';
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
});
});
describe('a chain contain a block with invalid fields', () => {
it('returns false', ()=>{
blockchain.addBlock({data:'Bears'});
blockchain.addBlock({data:"beets"});
blockchain.addBlock({data:"battlestar Galtico"});
blockchain.chain[2].data='some-bad-and-evil-data';
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
});
});
describe('and the chain contains a block with a jumped difficulty', () => {
it('return false', ()=>{
const lastblock=blockchain.chain[blockchain.chain.length-1];
const lastHash=lastblock.hash;
const timestamp=Date.now();
const data=[];
const nonce=0;
const difficulty=lastblock.difficulty-3;
const hash=cryptoHash(timestamp,lastHash,difficulty,nonce,data);
const badBlock=new Block({timestamp,lastHash,hash,nonce,difficulty,data})
blockchain.chain.push(badBlock);
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
})
})
describe('the chain does not contain any invalid field', () => {
it('return true',()=>{
});
})
})
});
describe('replaceChain', () => {
let errorMock, logMock;
beforeEach(()=>{
// replace the global console log with jestfn
errorMock= jest.fn();
logMock=jest.fn();
global.console.error=errorMock;
global.console.log=logMock;
});
describe('the new chain is not longer', () => {
beforeEach(()=>{
newChain.chain[0]={new:'chain'};
blockchain.replaceChain(newChain.chain);
});
it('it does not replace the chain',()=>{
expect(blockchain.chain).toEqual(originalChain);
});
// it('logs an error',()=>{
// expect(errorMock).toHaveBeenCalled();
// });
});
describe('the new chain is longer', () => {
beforeEach(()=>{
newChain.addBlock({data:'Bears'});
newChain.addBlock({data:"beets"});
newChain.addBlock({data:"battlestar Galtico"});
});
describe('the new chain is invalid', () => {
beforeEach(()=>{
newChain.chain[2].hash= 'some fake hash';
blockchain.replaceChain(newChain.chain);
});
it('it does not replace the chain',()=>{
expect(blockchain.chain).toEqual(originalChain);
});
// it('logs an error',()=>{
// expect(errorMock).toHaveBeenCalled();
// });
});
describe('the chain is valid', () => {
beforeEach(()=>{
blockchain.replaceChain(newChain.chain);
});
it('repaces the chain',()=>{
expect(blockchain.chain).toEqual(newChain.chain);
});
// it('log about the chain replacement',()=>{
// expect(logMock).toHaveBeenCalled();
// });
});
})
})
});