forked from streamingfast/bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteps.go
More file actions
114 lines (102 loc) · 2.87 KB
/
steps.go
File metadata and controls
114 lines (102 loc) · 2.87 KB
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
// Copyright 2019 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bstream
import (
"strings"
pbbstream "github.com/streamingfast/pbgo/sf/bstream/v1"
)
type StepType int
const (
StepNew = StepType(1 << iota) //1 First time we're seeing this block
StepUndo //2 We are undoing this block (it was done previously)
StepRedo //4 We are redoing this block (it was done previously)
StepHandoff //8 The block passed a handoff from one producer to another
StepIrreversible //16 This block passed the LIB barrier and is in chain
StepStalled //32 This block passed the LIB and is definitely forked out
StepsAll = StepType(StepNew | StepUndo | StepRedo | StepHandoff | StepIrreversible | StepStalled)
)
func (t StepType) String() string {
var el []string
if t&StepNew != 0 {
el = append(el, "new")
}
if t&StepUndo != 0 {
el = append(el, "undo")
}
if t&StepRedo != 0 {
el = append(el, "redo")
}
if t&StepHandoff != 0 {
el = append(el, "handoff")
}
if t&StepIrreversible != 0 {
el = append(el, "irreversible")
}
if t&StepStalled != 0 {
el = append(el, "stalled")
}
if len(el) == 0 {
return "none"
}
out := strings.Join(el, ",")
if out == "new,undo,redo,handoff,irreversible,stalled" {
return "all"
}
return out
}
func (t StepType) IsSingleStep() bool {
switch t {
case StepNew,
StepUndo,
StepRedo,
StepHandoff,
StepIrreversible,
StepStalled:
return true
}
return false
}
func StepsFromProto(steps []pbbstream.ForkStep) StepType {
if len(steps) <= 0 {
return StepNew | StepRedo | StepUndo | StepIrreversible
}
var filter StepType
var containsNew bool
var containsUndo bool
for _, step := range steps {
if step == pbbstream.ForkStep_STEP_NEW {
containsNew = true
}
if step == pbbstream.ForkStep_STEP_UNDO {
containsUndo = true
}
filter |= StepFromProto(step)
}
// Redo is output into 'new' and has no proto equivalent
if containsNew && containsUndo {
filter |= StepRedo
}
return filter
}
func StepFromProto(step pbbstream.ForkStep) StepType {
switch step {
case pbbstream.ForkStep_STEP_NEW:
return StepNew
case pbbstream.ForkStep_STEP_UNDO:
return StepUndo
case pbbstream.ForkStep_STEP_IRREVERSIBLE:
return StepIrreversible
}
return StepType(0)
}