-
Notifications
You must be signed in to change notification settings - Fork 38
/
block.go
194 lines (153 loc) · 3.3 KB
/
block.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
package timer
import (
"sync"
)
// BlockMeter calculates block time interval dynamically.
type BlockMeter func() (uint32, error)
// BlockTickHandler is a callback of a certain block advance.
type BlockTickHandler func()
// BlockTimer represents block timer.
//
// It can tick the blocks and perform certain actions
// on block time intervals.
type BlockTimer struct {
rolledBack bool
mtx *sync.Mutex
dur BlockMeter
baseDur uint32
mul, div uint32
cur, tgt uint32
last uint32
h BlockTickHandler
ps []BlockTimer
once bool
deltaCfg
}
// DeltaOption is an option of delta-interval handler.
type DeltaOption func(*deltaCfg)
type deltaCfg struct {
pulse bool
}
// WithPulse returns option to call delta-interval handler multiple
// times
func WithPulse() DeltaOption {
return func(c *deltaCfg) {
c.pulse = true
}
}
// StaticBlockMeter returns BlockMeters that always returns (d, nil).
func StaticBlockMeter(d uint32) BlockMeter {
return func() (uint32, error) {
return d, nil
}
}
// NewBlockTimer creates a new BlockTimer.
//
// Reset should be called before timer ticking.
func NewBlockTimer(dur BlockMeter, h BlockTickHandler) *BlockTimer {
return &BlockTimer{
mtx: new(sync.Mutex),
dur: dur,
mul: 1,
div: 1,
h: h,
deltaCfg: deltaCfg{
pulse: true,
},
}
}
// NewOneTickTimer creates a new BlockTimer that ticks only once.
//
// Do not use delta handlers with pulse in this timer.
func NewOneTickTimer(dur BlockMeter, h BlockTickHandler) *BlockTimer {
return &BlockTimer{
mtx: new(sync.Mutex),
dur: dur,
mul: 1,
div: 1,
h: h,
once: true,
}
}
// OnDelta registers handler which is executed on (mul / div * BlockMeter()) block
// after basic interval reset.
//
// If WithPulse option is provided, handler is executed (mul / div * BlockMeter()) block
// during base interval.
func (t *BlockTimer) OnDelta(mul, div uint32, h BlockTickHandler, opts ...DeltaOption) {
c := deltaCfg{
pulse: false,
}
for i := range opts {
opts[i](&c)
}
t.ps = append(t.ps, BlockTimer{
mul: mul,
div: div,
h: h,
once: t.once,
deltaCfg: c,
})
}
// Reset resets previous ticks of the BlockTimer.
//
// Returns BlockMeter's error upon occurrence.
func (t *BlockTimer) Reset() error {
d, err := t.dur()
if err != nil {
return err
}
t.mtx.Lock()
t.resetWithBaseInterval(d)
for i := range t.ps {
t.ps[i].resetWithBaseInterval(d)
}
t.mtx.Unlock()
return nil
}
func (t *BlockTimer) resetWithBaseInterval(d uint32) {
t.rolledBack = false
t.baseDur = d
t.reset()
}
func (t *BlockTimer) reset() {
mul, div := t.mul, t.div
if !t.pulse && t.rolledBack && mul < div {
mul, div = 1, 1
}
delta := mul * t.baseDur / div
if delta == 0 {
delta = 1
}
t.tgt = delta
t.cur = 0
}
// Tick ticks one block in the BlockTimer.
//
// Executes all callbacks which are awaiting execution at the new block.
func (t *BlockTimer) Tick(h uint32) {
t.mtx.Lock()
t.tick(h)
t.mtx.Unlock()
}
func (t *BlockTimer) tick(h uint32) {
if h != 0 && t.last == h {
return
}
t.last = h
t.cur++
if t.cur == t.tgt {
// it would be advisable to optimize such execution, for example:
// 1. push handler to worker pool t.wp.Submit(h);
// 2. call t.tickH(h)
t.h()
if !t.once {
t.cur = 0
t.rolledBack = true
t.reset()
}
}
for i := range t.ps {
t.ps[i].tick(h)
}
}