forked from Conflux-Chain/confura
-
Notifications
You must be signed in to change notification settings - Fork 12
/
epoch_window.go
110 lines (90 loc) · 2.84 KB
/
epoch_window.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
package sync
import (
"github.com/scroll-tech/rpc-gateway/types"
"github.com/scroll-tech/rpc-gateway/util"
)
// epochWindow maintains a continuous epoch window with a fixed size capacity.
type epochWindow struct {
capacity uint32
epochFrom, epochTo uint64
}
func newEpochWindow(capacity uint32) *epochWindow {
return &epochWindow{
capacity: capacity,
epochFrom: types.EpochNumberNil,
epochTo: types.EpochNumberNil,
}
}
// Reset epochFrom and epochTo of the epoch window to some specified epoch number.
func (win *epochWindow) reset(epochFrom, epochTo uint64) {
win.epochFrom, win.epochTo = epochFrom, epochTo
}
// Expand the epoch window from a smaller epoch number.
func (win *epochWindow) expandFrom(epochNo uint64) {
if win.isSet() {
win.epochFrom = util.MinUint64(win.epochFrom, epochNo)
} else {
win.reset(epochNo, epochNo)
}
}
// Expand the epoch window to a bigger epoch number.
func (win *epochWindow) expandTo(epochNo uint64) {
if win.isSet() {
win.epochTo = util.MaxUint64(win.epochTo, epochNo)
} else {
win.reset(epochNo, epochNo)
}
}
// Update epochTo of the epoch window to specified number no matter expansion or shrink.
func (win *epochWindow) updateTo(epochTo uint64) {
if win.isSet() {
win.epochTo = epochTo
} else {
win.reset(epochTo, epochTo)
}
}
// Peek if pivot switch will happen if new epoch appended to expand.
func (win *epochWindow) peekWillPivotSwitch(epochNo uint64) bool {
if win.isSet() && win.epochFrom > epochNo {
return true
}
return false
}
// Peek if overflow will happen if new epoch appended to expand.
func (win *epochWindow) peekWillOverflow(epochNo uint64) bool {
if win.isSet() && epochNo >= win.epochFrom && (epochNo-win.epochFrom+1) > uint64(win.capacity) {
return true
}
return false
}
// Peek sync info by shrinking no more than the specified size of epoch(s) from the epoch window.
func (win *epochWindow) peekShrinkFrom(specSize uint32) (syncFrom uint64, syncSize uint32) {
if win.isEmpty() {
return 0, 0
}
return win.epochFrom, util.MinUint32(win.size(), specSize)
}
// Shrink no more than the specified size of epoch(s) from the epoch window.
func (win *epochWindow) shrinkFrom(specSize uint32) (uint64, uint32) {
if win.isEmpty() {
return 0, 0
}
syncFrom, syncSize := win.epochFrom, util.MinUint32(win.size(), specSize)
win.epochFrom += uint64(syncSize)
return syncFrom, syncSize
}
// Check if the epoch window is empty.
func (win *epochWindow) isEmpty() bool {
return !win.isSet() || win.epochFrom > win.epochTo
}
// Check if the epoch window is set.
func (win *epochWindow) isSet() bool {
return win.epochFrom != types.EpochNumberNil && win.epochTo != types.EpochNumberNil
}
// Return the size of the sync window.
func (win *epochWindow) size() uint32 {
if !win.isSet() || win.epochFrom > win.epochTo {
return 0
}
return uint32(win.epochTo - win.epochFrom + 1)
}