forked from bitpay/insight-api
/
blocks.js
274 lines (245 loc) · 8.66 KB
/
blocks.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
'use strict';
var should = require('should');
var sinon = require('sinon');
var BlockController = require('../lib/blocks');
var bitcore = require('bitcore-lib-zcash');
var _ = require('lodash');
var blocks = require('./data/blocks.json');
var blockIndexes = {
'0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7': {
hash: '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7',
chainWork: '0000000000000000000000000000000000000000000000054626b1839ade284a',
prevHash: '00000000000001a55f3214e9172eb34b20e0bc5bd6b8007f3f149fca2c8991a4',
nextHash: '000000000001e866a8057cde0c650796cb8a59e0e6038dc31c69d7ca6649627d',
confirmations: 119,
height: 533974
},
'000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7': {
hash: '000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7',
chainWork: '00000000000000000000000000000000000000000000000544ea52e1575ca753',
prevHash: '00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441',
confirmations: 119,
height: 533951
},
'00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441': {
hash: '00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441',
chainWork: '00000000000000000000000000000000000000000000000544ea52e0575ba752',
prevHash: '000000000001b9c41e6c4a7b81a068b50cf3f522ee4ac1e942e75ec16e090547',
height: 533950
},
'000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4': {
hash: '000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4',
prevHash: '00000000000000000a9d74a7b527f7b995fc21ceae5aa21087b443469351a362',
height: 375493
},
533974: {
hash: '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7',
chainWork: '0000000000000000000000000000000000000000000000054626b1839ade284a',
prevHash: '00000000000001a55f3214e9172eb34b20e0bc5bd6b8007f3f149fca2c8991a4',
height: 533974
}
};
describe('Blocks', function() {
describe('/blocks/:blockHash route', function() {
var insight = {
'hash': '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7',
'confirmations': 119,
'size': 1011,
'height': 533974,
'version': 536870919,
'merkleroot': 'b06437355844b8178173f3e18ca141472e4b0861daa81ef0f701cf9e51f0283e',
'tx': [
'25a988e54b02e0e5df146a0f8fa7b9db56210533a9f04bdfda5f4ceb6f77aadd',
'b85334bf2df35c6dd5b294efe92ffc793a78edff75a2ca666fc296ffb04bbba0',
'2e01c7a4a0e335112236b711c4aaddd02e8dc59ba2cda416e8f80ff06dddd7e1'
],
'time': 1440987503,
'nonce': 1868753784,
'bits': '1a0cf267',
'difficulty': 1295829.93087696,
'chainwork': '0000000000000000000000000000000000000000000000054626b1839ade284a',
'previousblockhash': '00000000000001a55f3214e9172eb34b20e0bc5bd6b8007f3f149fca2c8991a4',
'nextblockhash': '000000000001e866a8057cde0c650796cb8a59e0e6038dc31c69d7ca6649627d',
'reward': 12.5,
'isMainChain': true,
'poolInfo': {}
};
var bitcoreBlock = bitcore.Block.fromBuffer(new Buffer(blocks['0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7'], 'hex'));
var node = {
log: sinon.stub(),
getBlock: sinon.stub().callsArgWith(1, null, bitcoreBlock),
services: {
bitcoind: {
getBlockHeader: sinon.stub().callsArgWith(1, null, blockIndexes['0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7']),
isMainChain: sinon.stub().returns(true),
height: 534092
}
}
};
it('block data should be correct', function(done) {
var controller = new BlockController({node: node});
var hash = '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7';
var req = {
params: {
blockHash: hash
}
};
var res = {};
var next = function() {
should.exist(req.block);
var block = req.block;
should(block).eql(insight);
done();
};
controller.block(req, res, next);
});
it('block pool info should be correct', function(done) {
var block = bitcore.Block.fromString(blocks['000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4']);
var node = {
log: sinon.stub(),
getBlock: sinon.stub().callsArgWith(1, null, block),
services: {
bitcoind: {
getBlockHeader: sinon.stub().callsArgWith(1, null, blockIndexes['000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4']),
isMainChain: sinon.stub().returns(true),
height: 534092
}
}
};
var controller = new BlockController({node: node});
var req = {
params: {
blockHash: hash
}
};
var res = {};
var next = function() {
should.exist(req.block);
var block = req.block;
req.block.poolInfo.poolName.should.equal('Discus Fish');
req.block.poolInfo.url.should.equal('http://f2pool.com/');
done();
};
var hash = '000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4';
controller.block(req, res, next);
});
});
describe('/blocks route', function() {
var insight = {
'blocks': [
{
'height': 533951,
'size': 206,
'hash': '000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7',
'time': 1440978683,
'txlength': 1,
'poolInfo': {
'poolName': 'AntMiner',
'url': 'https://bitmaintech.com/'
}
},
{
'height': 533950,
'size': 206,
'hash': '00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441',
'time': 1440977479,
'txlength': 1,
'poolInfo': {
'poolName': 'AntMiner',
'url': 'https://bitmaintech.com/'
}
}
],
'length': 2,
'pagination': {
'current': '2015-08-30',
'currentTs': 1440979199,
'isToday': false,
'more': false,
'next': '2015-08-31',
'prev': '2015-08-29'
}
};
var stub = sinon.stub();
stub.onFirstCall().callsArgWith(1, null, new Buffer(blocks['000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7'], 'hex'));
stub.onSecondCall().callsArgWith(1, null, new Buffer(blocks['00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441'], 'hex'));
var hashes = [
'00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441',
'000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7'
];
var node = {
log: sinon.stub(),
services: {
bitcoind: {
getRawBlock: stub,
getBlockHeader: function(hash, callback) {
callback(null, blockIndexes[hash]);
},
getBlockHashesByTimestamp: sinon.stub().callsArgWith(2, null, hashes)
}
}
};
it('should have correct data', function(done) {
var blocks = new BlockController({node: node});
var req = {
query: {
limit: 2,
blockDate: '2015-08-30'
}
};
var res = {
jsonp: function(data) {
should(data).eql(insight);
done();
}
};
blocks.list(req, res);
});
});
describe('/block-index/:height route', function() {
var node = {
log: sinon.stub(),
services: {
bitcoind: {
getBlockHeader: function(height, callback) {
callback(null, blockIndexes[height]);
}
}
}
};
it('should have correct data', function(done) {
var blocks = new BlockController({node: node});
var insight = {
'blockHash': '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7'
};
var height = 533974;
var req = {
params: {
height: height
}
};
var res = {
jsonp: function(data) {
should(data).eql(insight);
done();
}
};
blocks.blockIndex(req, res);
});
});
describe('#getBlockReward', function() {
var node = {
log: sinon.stub()
};
var blocks = new BlockController({node: node});
it('should give a block reward of 50 * 1e8 for block before first halvening', function() {
blocks.getBlockReward(100000).should.equal(50 * 1e8);
});
it('should give a block reward of 25 * 1e8 for block between first and second halvenings', function() {
blocks.getBlockReward(373011).should.equal(25 * 1e8);
});
it('should give a block reward of 12.5 * 1e8 for block between second and third halvenings', function() {
blocks.getBlockReward(500000).should.equal(12.5 * 1e8);
});
});
});