forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.go
62 lines (55 loc) · 1.21 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
package rpcevents
// 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,
}
}