This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
blocks.go
542 lines (518 loc) · 15 KB
/
blocks.go
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package core
import (
"crypto/sha256"
"fmt"
cmn "github.com/tendermint/tendermint/libs/common"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
)
// Get block headers for minHeight <= height <= maxHeight.
// Block headers are returned in descending order (highest first).
//
// ```shell
// curl 'localhost:27147/blockchain?minHeight=10&maxHeight=10'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:27147", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// info, err := client.BlockchainInfo(10, 10)
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "error": "",
// "result": {
// "block_metas": [
// {
// "header": {
// "app_hash": "",
// "chain_id": "test-chain-6UTNIN",
// "height": "10",
// "time": "2017-05-29T15:05:53.877Z",
// "num_txs": "0",
// "last_block_id": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// },
// "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
// "data_hash": "",
// "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
// },
// "block_id": {
// "parts": {
// "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
// "total": "1"
// },
// "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
// }
// }
// ],
// "last_height": "5493"
// },
// "id": "",
// "jsonrpc": "2.0"
// }
// ```
//
// <aside class="notice">Returns at most 20 items.</aside>
func BlockchainInfo(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
// maximum 20 block metas
const limit int64 = 20
var err error
minHeight, maxHeight, err = filterMinMax(blockStore.Height(), minHeight, maxHeight, limit)
if err != nil {
return nil, err
}
logger.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight)
blockMetas := []*types.BlockMeta{}
for height := maxHeight; height >= minHeight; height-- {
blockMeta := blockStore.LoadBlockMeta(height)
blockMetas = append(blockMetas, blockMeta)
}
return &ctypes.ResultBlockchainInfo{
LastHeight: blockStore.Height(),
BlockMetas: blockMetas}, nil
}
// error if either min or max are negative or min < max
// if 0, use 1 for min, latest block height for max
// enforce limit.
// error if min > max
func filterMinMax(height, min, max, limit int64) (int64, int64, error) {
// filter negatives
if min < 0 || max < 0 {
return min, max, fmt.Errorf("heights must be non-negative")
}
// adjust for default values
if min == 0 {
min = 1
}
if max == 0 {
max = height
}
// limit max to the height
max = cmn.MinInt64(height, max)
// limit min to within `limit` of max
// so the total number of blocks returned will be `limit`
min = cmn.MaxInt64(min, max-limit+1)
if min > max {
return min, max, fmt.Errorf("min height %d can't be greater than max height %d", min, max)
}
return min, max, nil
}
// Get block at a given height.
// If no height is provided, it will fetch the latest block.
//
// ```shell
// curl 'localhost:27147/block?height=10'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:27147", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// info, err := client.Block(10)
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "error": "",
// "result": {
// "block": {
// "last_commit": {
// "precommits": [
// {
// "signature": {
// "data": "12C0D8893B8A38224488DC1DE6270DF76BB1A5E9DB1C68577706A6A97C6EC34FFD12339183D5CA8BC2F46148773823DE905B7F6F5862FD564038BB7AE03BF50D",
// "type": "ed25519"
// },
// "block_id": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// },
// "type": "2",
// "round": "0",
// "height": "9",
// "validator_index": "0",
// "validator_address": "E89A51D60F68385E09E716D353373B11F8FACD62"
// }
// ],
// "blockID": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// }
// },
// "data": {
// "txs": []
// },
// "header": {
// "app_hash": "",
// "chain_id": "test-chain-6UTNIN",
// "height": "10",
// "time": "2017-05-29T15:05:53.877Z",
// "num_txs": "0",
// "last_block_id": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// },
// "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
// "data_hash": "",
// "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
// }
// },
// "block_meta": {
// "header": {
// "app_hash": "",
// "chain_id": "test-chain-6UTNIN",
// "height": "10",
// "time": "2017-05-29T15:05:53.877Z",
// "num_txs": "0",
// "last_block_id": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// },
// "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
// "data_hash": "",
// "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
// },
// "block_id": {
// "parts": {
// "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
// "total": "1"
// },
// "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
// }
// }
// },
// "id": "",
// "jsonrpc": "2.0"
// }
// ```
func Block(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlock, error) {
storeHeight := blockStore.Height()
height, err := getHeight(storeHeight, heightPtr)
if err != nil {
return nil, err
}
blockMeta := blockStore.LoadBlockMeta(height)
block := blockStore.LoadBlock(height)
return &ctypes.ResultBlock{BlockMeta: blockMeta, Block: block}, nil
}
// Get block at a given block hash.
// If no block hash is provided, it will return with error.
//
// ```shell
// curl 'localhost:27147/block_by_hash?hash=0xF70588DAB36BDA5A953D548A16F7D48C6C2DFD78'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:27147", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// info, err := client.BlockByHash([]byte("F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"))
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "error": "",
// "result": {
// "block": {
// "last_commit": {
// "precommits": [
// {
// "signature": {
// "data": "12C0D8893B8A38224488DC1DE6270DF76BB1A5E9DB1C68577706A6A97C6EC34FFD12339183D5CA8BC2F46148773823DE905B7F6F5862FD564038BB7AE03BF50D",
// "type": "ed25519"
// },
// "block_id": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// },
// "type": "2",
// "round": "0",
// "height": "9",
// "validator_index": "0",
// "validator_address": "E89A51D60F68385E09E716D353373B11F8FACD62"
// }
// ],
// "blockID": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// }
// },
// "data": {
// "txs": []
// },
// "header": {
// "app_hash": "",
// "chain_id": "test-chain-6UTNIN",
// "height": "10",
// "time": "2017-05-29T15:05:53.877Z",
// "num_txs": "0",
// "last_block_id": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// },
// "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
// "data_hash": "",
// "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
// }
// },
// "block_meta": {
// "header": {
// "app_hash": "",
// "chain_id": "test-chain-6UTNIN",
// "height": "10",
// "time": "2017-05-29T15:05:53.877Z",
// "num_txs": "0",
// "last_block_id": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// },
// "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
// "data_hash": "",
// "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
// },
// "block_id": {
// "parts": {
// "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
// "total": "1"
// },
// "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
// }
// }
// },
// "id": "",
// "jsonrpc": "2.0"
// }
// ```
func BlockByHash(ctx *rpctypes.Context, blockHash []byte) (*ctypes.ResultBlock, error) {
height, err := getHeightViaHash(blockHash)
if err != nil {
return nil, err
}
blockMeta := blockStore.LoadBlockMeta(height)
block := blockStore.LoadBlock(height)
return &ctypes.ResultBlock{BlockMeta: blockMeta, Block: block}, nil
}
// Get block commit at a given height.
// If no height is provided, it will fetch the commit for the latest block.
//
// ```shell
// curl 'localhost:27147/commit?height=11'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:27147", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// info, err := client.Commit(11)
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "error": "",
// "result": {
// "canonical": true,
// "commit": {
// "precommits": [
// {
// "signature": {
// "data": "00970429FEC652E9E21D106A90AE8C5413759A7488775CEF4A3F44DC46C7F9D941070E4FBE9ED54DF247FA3983359A0C3A238D61DE55C75C9116D72ABC9CF50F",
// "type": "ed25519"
// },
// "block_id": {
// "parts": {
// "hash": "9E37CBF266BC044A779E09D81C456E653B89E006",
// "total": "1"
// },
// "hash": "CC6E861E31CA4334E9888381B4A9137D1458AB6A"
// },
// "type": "2",
// "round": "0",
// "height": "11",
// "validator_index": "0",
// "validator_address": "E89A51D60F68385E09E716D353373B11F8FACD62"
// }
// ],
// "blockID": {
// "parts": {
// "hash": "9E37CBF266BC044A779E09D81C456E653B89E006",
// "total": "1"
// },
// "hash": "CC6E861E31CA4334E9888381B4A9137D1458AB6A"
// }
// },
// "header": {
// "app_hash": "",
// "chain_id": "test-chain-6UTNIN",
// "height": "11",
// "time": "2017-05-29T15:05:54.893Z",
// "num_txs": "0",
// "last_block_id": {
// "parts": {
// "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
// "total": "1"
// },
// "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
// },
// "last_commit_hash": "3CE0C9727CE524BA9CB7C91E28F08E2B94001087",
// "data_hash": "",
// "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
// }
// },
// "id": "",
// "jsonrpc": "2.0"
// }
// ```
func Commit(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultCommit, error) {
storeHeight := blockStore.Height()
height, err := getHeight(storeHeight, heightPtr)
if err != nil {
return nil, err
}
header := blockStore.LoadBlockMeta(height).Header
// If the next block has not been committed yet,
// use a non-canonical commit
if height == storeHeight {
commit := blockStore.LoadSeenCommit(height)
return ctypes.NewResultCommit(&header, commit, false), nil
}
// Return the canonical commit (comes from the block at height+1)
commit := blockStore.LoadBlockCommit(height)
return ctypes.NewResultCommit(&header, commit, true), nil
}
// BlockResults gets ABCIResults at a given height.
// If no height is provided, it will fetch results for the latest block.
//
// Results are for the height of the block containing the txs.
// Thus response.results[5] is the results of executing getBlock(h).Txs[5]
//
// ```shell
// curl 'localhost:27147/block_results?height=10'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:27147", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// info, err := client.BlockResults(10)
// ```
//
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "height": "10",
// "results": [
// {
// "code": "0",
// "data": "CAFE00F00D"
// },
// {
// "code": "102",
// "data": ""
// }
// ]
// }
// ```
func BlockResults(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlockResults, error) {
storeHeight := blockStore.Height()
height, err := getHeight(storeHeight, heightPtr)
if err != nil {
return nil, err
}
// load the results
results, err := sm.LoadABCIResponses(stateDB, height)
if err != nil {
return nil, err
}
res := &ctypes.ResultBlockResults{
Height: height,
Results: results,
}
return res, nil
}
func getHeight(currentHeight int64, heightPtr *int64) (int64, error) {
if heightPtr != nil {
height := *heightPtr
if height <= 0 {
return 0, fmt.Errorf("Height must be greater than 0")
}
if height > currentHeight {
return 0, fmt.Errorf("Height must be less than or equal to the current blockchain height")
}
return height, nil
}
return currentHeight, nil
}
func getHeightViaHash(blockHash []byte) (int64, error) {
if blockHash == nil {
return 0, fmt.Errorf("blockHash is missing")
}
if len(blockHash) != sha256.Size {
return 0, fmt.Errorf("length of blockHash is not 32")
}
blockHeight, err := blockIndexer.Get(blockHash)
if err != nil {
return 0, fmt.Errorf("get block from blockHash indexer failed, err: %v", err)
}
if blockHeight == 0 {
return 0, fmt.Errorf("the specified block blockHash do not exist")
}
return blockHeight, nil
}