forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.go
86 lines (75 loc) · 1.81 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
package rpcevents
import (
"github.com/hyperledger/burrow/execution/exec"
)
// Get bounds suitable for events.Provider
func (br *BlockRange) Bounds(latestBlockHeight uint64) (startHeight, endHeight uint64, streaming bool) {
// End bound is exclusive in state.GetEvents so we increment the height
return br.GetStart().Bound(latestBlockHeight), br.GetEnd().Bound(latestBlockHeight) + 1,
br.GetEnd().GetType() == Bound_STREAM
}
func (b *Bound) Bound(latestBlockHeight uint64) uint64 {
if b == nil {
return latestBlockHeight
}
switch b.Type {
case Bound_ABSOLUTE:
return b.GetIndex()
case Bound_RELATIVE:
if b.Index < latestBlockHeight {
return latestBlockHeight - b.Index
}
return 0
case Bound_FIRST:
return 0
case Bound_LATEST, Bound_STREAM:
return latestBlockHeight
default:
return latestBlockHeight
}
}
func AbsoluteBound(index uint64) *Bound {
return &Bound{
Index: index,
Type: Bound_ABSOLUTE,
}
}
func RelativeBound(index uint64) *Bound {
return &Bound{
Index: index,
Type: Bound_RELATIVE,
}
}
func LatestBound() *Bound {
return &Bound{
Type: Bound_LATEST,
}
}
func StreamBound() *Bound {
return &Bound{
Type: Bound_STREAM,
}
}
func NewBlockRange(start, end *Bound) *BlockRange {
return &BlockRange{
Start: start,
End: end,
}
}
func AbsoluteRange(start, end uint64) *BlockRange {
return NewBlockRange(AbsoluteBound(start), AbsoluteBound(end))
}
func SingleBlock(height uint64) *BlockRange {
return AbsoluteRange(height, height+1)
}
func ConsumeBlockExecutions(stream ExecutionEvents_StreamClient, consumer func(*exec.BlockExecution) error) error {
var be *exec.BlockExecution
var err error
for be, err = exec.ConsumeBlockExecution(stream); err == nil; be, err = exec.ConsumeBlockExecution(stream) {
err = consumer(be)
if err != nil {
return err
}
}
return err
}