This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
forked from streamingfast/bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.go
201 lines (165 loc) · 4.84 KB
/
cursor.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
package bstream
import (
"fmt"
"strconv"
"strings"
"github.com/streamingfast/opaque"
)
type Cursor struct {
Step StepType
Block BlockRef
LIB BlockRef // last block sent as irreversible if it exists, else known forkdb LIB
// HeadBlock will be the same as Block when you receive a 'new' Step, except during a reorg.
// During a reorg, (steps in ['new','redo','undo']) the HeadBlock will always point to the block that causes the reorg.
// When the LIB is advancing (ex: DPOSLibNum changes, etc.), step='irreversible' and the HeadBlock will be the block
// that causes previous blocks to become irreversible.
HeadBlock BlockRef
}
var EmptyCursor = &Cursor{
Block: BlockRefEmpty,
HeadBlock: BlockRefEmpty,
LIB: BlockRefEmpty,
}
func (c *Cursor) IsOnFinalBlock() bool {
return c.Block.Num() == c.LIB.Num() && c.Step.Matches(StepIrreversible)
}
func (c *Cursor) ToOpaque() string {
return opaque.EncodeString(c.String())
}
func CursorFromOpaque(in string) (*Cursor, error) {
payload, err := opaque.DecodeToString(in)
if err != nil {
return nil, fmt.Errorf("unable to decode: %w", err)
}
return FromString(payload)
}
func (c *Cursor) Equals(cc *Cursor) bool {
if c.IsEmpty() {
return cc.IsEmpty()
}
return c.Block.ID() == cc.Block.ID() &&
c.HeadBlock.ID() == cc.HeadBlock.ID() &&
c.LIB.ID() == cc.LIB.ID()
}
func (c *Cursor) IsEmpty() bool {
return c == nil ||
c.Block == nil ||
c.Block.ID() == "" ||
c.HeadBlock == nil ||
c.HeadBlock.ID() == "" ||
c.LIB == nil ||
c.LIB.ID() == ""
}
func (c *Cursor) String() string {
blkID := c.Block.ID()
headID := c.HeadBlock.ID()
libID := c.LIB.ID()
if headID == blkID {
return fmt.Sprintf("c1:%d:%d:%s:%d:%s", c.Step, c.Block.Num(), blkID, c.LIB.Num(), libID)
}
if blkID == libID {
return fmt.Sprintf("c2:%d:%d:%s:%d:%s", c.Step, c.Block.Num(), blkID, c.HeadBlock.Num(), headID)
}
return fmt.Sprintf("c3:%d:%d:%s:%d:%s:%d:%s", c.Step, c.Block.Num(), blkID, c.HeadBlock.Num(), headID, c.LIB.Num(), libID)
}
func FromString(cur string) (*Cursor, error) {
parts := strings.Split(cur, ":")
if len(parts) < 6 {
return nil, fmt.Errorf("invalid cursor: too short")
}
switch parts[0] {
case "c1":
if len(parts) != 6 {
return nil, fmt.Errorf("invalid cursor: invalid number of segments")
}
step, err := readCursorStep(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid step segment: %w", err)
}
blkRef, err := readCursorBlockRef(parts[2], parts[3])
if err != nil {
return nil, fmt.Errorf("invalid block ref segments: %w", err)
}
libRef, err := readCursorBlockRef(parts[4], parts[5])
if err != nil {
return nil, fmt.Errorf("invalid block ref segments: %w", err)
}
return &Cursor{
Step: step,
Block: blkRef,
HeadBlock: blkRef,
LIB: libRef,
}, nil
case "c2":
if len(parts) != 6 {
return nil, fmt.Errorf("invalid cursor: invalid number of segments")
}
step, err := readCursorStep(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid step segment: %w", err)
}
blkRef, err := readCursorBlockRef(parts[2], parts[3])
if err != nil {
return nil, fmt.Errorf("invalid block ref segments: %w", err)
}
headBlkRef, err := readCursorBlockRef(parts[4], parts[5])
if err != nil {
return nil, fmt.Errorf("invalid head block ref segments: %w", err)
}
return &Cursor{
Step: step,
Block: blkRef,
HeadBlock: headBlkRef,
LIB: blkRef,
}, nil
case "c3":
if len(parts) != 8 {
return nil, fmt.Errorf("invalid cursor: invalid number of segments")
}
step, err := readCursorStep(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid step segment: %w", err)
}
blkRef, err := readCursorBlockRef(parts[2], parts[3])
if err != nil {
return nil, fmt.Errorf("invalid block ref segments: %w", err)
}
headBlkRef, err := readCursorBlockRef(parts[4], parts[5])
if err != nil {
return nil, fmt.Errorf("invalid head block ref segments: %w", err)
}
libRef, err := readCursorBlockRef(parts[6], parts[7])
if err != nil {
return nil, fmt.Errorf("invalid block ref segments: %w", err)
}
return &Cursor{
Step: step,
Block: blkRef,
HeadBlock: headBlkRef,
LIB: libRef,
}, nil
default:
return nil, fmt.Errorf("invalid cursor: invalid prefix")
}
}
func readCursorBlockRef(numStr string, id string) (BlockRef, error) {
num, err := strconv.ParseUint(numStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid block num: %w", err)
}
return NewBlockRef(id, num), nil
}
func readCursorStep(part string) (StepType, error) {
step, err := strconv.ParseInt(part, 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid cursor step: %w", err)
}
out := StepType(step)
if out != StepNew &&
out != StepUndo &&
out != StepIrreversible &&
out != StepNewIrreversible {
return 0, fmt.Errorf("invalid step: %d", step)
}
return out, nil
}