-
Notifications
You must be signed in to change notification settings - Fork 19
/
blockchain.test.js
158 lines (122 loc) · 4.46 KB
/
blockchain.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
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
153
154
155
156
157
158
const Blockchain = require('./blockchain');
const Block = require('./block');
const cryptoHash = require('./crypto-hash');
describe('Blockchain', () => {
let blockchain, newChain, originalChain;
beforeEach(() => {
blockchain = new Blockchain();
newChain = new Blockchain();
originalChain = blockchain.chain;
});
it('contains a `chain` Array instance', () => {
expect(blockchain.chain instanceof Array).toBe(true);
});
it('starts 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('returns false', () => {
blockchain.chain[0] = { data: 'fake-genesis' };
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
});
});
describe('when the chain starts with the genesis block and has multiple blocks', () => {
beforeEach(() => {
blockchain.addBlock({ data: 'Bears' });
blockchain.addBlock({ data: 'Beets' });
blockchain.addBlock({ data: 'Battlestar Galactica' });
});
describe('and a lastHash reference has changed', () => {
it('returns false', () => {
blockchain.chain[2].lastHash = 'broken-lastHash';
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
});
});
describe('and the chain contains a block with an invalid field', () => {
it('returns false', () => {
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('returns false', () => {
const lastBlock = blockchain.chain[blockchain.chain.length-1];
const lastHash = lastBlock.hash;
const timestamp = Date.now();
const nonce = 0;
const data = [];
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('and the chain does not contain any invalid blocks', () => {
it('returns true', () => {
expect(Blockchain.isValidChain(blockchain.chain)).toBe(true);
});
});
});
});
describe('replaceChain()', () => {
let errorMock, logMock;
beforeEach(() => {
errorMock = jest.fn();
logMock = jest.fn();
global.console.error = errorMock;
global.console.log = logMock;
});
describe('when the new chain is not longer', () => {
beforeEach(() => {
newChain.chain[0] = { new: 'chain' };
blockchain.replaceChain(newChain.chain);
});
it('does not replace the chain', () => {
expect(blockchain.chain).toEqual(originalChain);
});
it('logs an error', () => {
expect(errorMock).toHaveBeenCalled();
});
});
describe('when the new chain is longer', () => {
beforeEach(() => {
newChain.addBlock({ data: 'Bears' });
newChain.addBlock({ data: 'Beets' });
newChain.addBlock({ data: 'Battlestar Galactica' });
});
describe('and the chain is invalid', () => {
beforeEach(() => {
newChain.chain[2].hash = 'some-fake-hash';
blockchain.replaceChain(newChain.chain);
});
it('does not replace the chain', () => {
blockchain.replaceChain(newChain.chain);
expect(blockchain.chain).toEqual(originalChain);
});
it('logs an error', () => {
expect(errorMock).toHaveBeenCalled();
});
});
describe('and the chain is valid', () => {
beforeEach(() => {
blockchain.replaceChain(newChain.chain);
});
it('replaces the chain', () => {
expect(blockchain.chain).toEqual(newChain.chain);
});
it('logs about the chain replacement', () => {
expect(logMock).toHaveBeenCalled();
});
});
});
});
});