Repository navigation
Expand file tree
/
Copy pathdeadline.go
More file actions
342 lines (289 loc) · 11.5 KB
/
Copy pathdeadline.go
File metadata and controls
342 lines (289 loc) · 11.5 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright © 2022-2026 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1
package core
import (
"context"
"testing"
"time"
"github.com/jonboulle/clockwork"
"github.com/obolnetwork/charon/app/eth2wrap"
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/app/z"
)
//go:generate mockery --name=Deadliner --output=mocks --outpkg=mocks --case=underscore
const (
// marginFactor defines the fraction of the slot duration to use as a margin.
// This is to consider network delays and other factors that may affect the timing.
marginFactor = 12
)
// DeadlineFunc returns the time at which a duty expires, or false if it never does: by when it is
// submitted, see NewDutyDeadlineFunc, or until when its data is retained, see NewDutyRetentionFunc.
type DeadlineFunc func(Duty) (time.Time, bool)
// DeadlineStatus is the result of adding a duty to a Deadliner.
type DeadlineStatus int
const (
// DeadlineExpired indicates the duty's deadline has already passed,
// so it was not scheduled and will never be emitted on C().
DeadlineExpired DeadlineStatus = iota
// DeadlineScheduled indicates the duty was scheduled for future deadline expiry
// and will eventually be emitted on C().
DeadlineScheduled
// DeadlineExempt indicates the duty type never expires (e.g. exits),
// so it was not scheduled and will never be emitted on C().
DeadlineExempt
)
// Deadliner tracks duties until they expire, at the time the expiry function it is created with
// provides, their submission deadline or their retention, and emits them then. The C method isn’t
// thread safe and may only be used by a single goroutine. So, multiple instances are required
// for different components and use cases.
type Deadliner interface {
// Add schedules the duty for future deadline expiry and returns DeadlineScheduled.
// It is idempotent and returns DeadlineScheduled if the duty was previously added and still awaits expiry.
// It returns DeadlineExpired if the duty's deadline has already passed.
// It returns DeadlineExempt if the duty type never expires.
// In the latter two cases the duty is not scheduled and will never be emitted on C().
Add(duty Duty) DeadlineStatus
// C returns the same read channel every time and contains expired duties.
// It should only be called by a single goroutine.
C() <-chan Duty
}
// expiryInput represents the input to inputChan.
type expiryInput struct {
duty Duty
success chan<- DeadlineStatus
}
// deadliner implements the Deadliner interface.
type deadliner struct {
label string
inputChan chan expiryInput
expiryChan chan Duty
clock clockwork.Clock
quit chan struct{}
}
// NewDeadlinerForT returns a Deadliner for use in tests.
func NewDeadlinerForT(ctx context.Context, t *testing.T, expiryFunc DeadlineFunc, clock clockwork.Clock) Deadliner {
t.Helper()
return newDeadliner(ctx, "test", expiryFunc, clock)
}
// NewDeadliner returns a new Deadliner expiring duties at the time the expiry function provides.
//
// It also starts a goroutine which is responsible for reading and storing duties,
// and sending the expired duty to receiver's expiryChan until the context is closed.
func NewDeadliner(ctx context.Context, label string, expiryFunc DeadlineFunc) Deadliner {
return newDeadliner(ctx, label, expiryFunc, clockwork.NewRealClock())
}
// NewDutyDeadlineFunc returns the function that provides the deadlines by which duties are
// submitted, or false if the duty never deadlines: until then the cluster spends on producing the
// duty. Deadlines derived from gloas slot offsets follow the fork schedule at runtime.
func NewDutyDeadlineFunc(ctx context.Context, eth2Cl eth2wrap.Client, forkSchedule func() eth2wrap.ForkForkSchedule) (DeadlineFunc, error) {
timing, err := newDutyTiming(ctx, eth2Cl, forkSchedule)
if err != nil {
return nil, err
}
return timing.expiryFunc(timing.deadline), nil
}
// NewDutyRetentionFunc returns the function that provides the deadlines until which the data of
// duties is retained for other components, or false if it is retained forever. It is the
// submission deadline of NewDutyDeadlineFunc, except for proposals, which are retained until the
// end of the slot to serve the reveal of their execution payload envelope.
func NewDutyRetentionFunc(ctx context.Context, eth2Cl eth2wrap.Client, forkSchedule func() eth2wrap.ForkForkSchedule) (DeadlineFunc, error) {
timing, err := newDutyTiming(ctx, eth2Cl, forkSchedule)
if err != nil {
return nil, err
}
return timing.expiryFunc(timing.retention), nil
}
// dutyTiming derives the deadlines of duties from the slot timing of the network.
type dutyTiming struct {
genesisTime time.Time
slotDuration time.Duration
slotsPerEpoch uint64
slotOffset SlotOffsetFunc
}
// newDutyTiming returns the slot timing of the network fetched from the beacon node.
func newDutyTiming(ctx context.Context, eth2Cl eth2wrap.Client, forkSchedule func() eth2wrap.ForkForkSchedule) (dutyTiming, error) {
genesisTime, err := eth2wrap.FetchGenesisTime(ctx, eth2Cl)
if err != nil {
return dutyTiming{}, err
}
slotDuration, slotsPerEpoch, err := eth2wrap.FetchSlotsConfig(ctx, eth2Cl)
if err != nil {
return dutyTiming{}, err
}
timing, err := eth2wrap.FetchSlotTimingConfig(ctx, eth2Cl)
if err != nil {
return dutyTiming{}, err
}
return dutyTiming{
genesisTime: genesisTime,
slotDuration: slotDuration,
slotsPerEpoch: slotsPerEpoch,
slotOffset: newSlotOffsetFunc(slotDuration, slotsPerEpoch, timing, forkSchedule),
}, nil
}
// expiryFunc returns the function providing when duties expire, by submission deadline or
// retention: the duration the duration function provides after the start of their slot, plus a
// margin for network delays, or false if the duty never expires.
func (t dutyTiming) expiryFunc(durationFunc func(Duty) (time.Duration, bool)) DeadlineFunc {
return func(duty Duty) (time.Time, bool) {
duration, ok := durationFunc(duty)
if !ok {
return time.Time{}, false
}
start := t.genesisTime.Add(t.slotDuration * time.Duration(duty.Slot))
margin := t.slotDuration / marginFactor
return start.Add(duration + margin), true
}
}
// deadline returns how long after the start of its slot the duty is submitted by, or false if it
// never deadlines.
func (t dutyTiming) deadline(duty Duty) (time.Duration, bool) {
//nolint: revive // prioritise clarity
switch duty.Type {
case DutyExit, DutyBuilderRegistration:
// Do not timeout exit or registration duties.
return 0, false
case DutyProposer, DutyRandao:
// A proposal's useful life ends once attesters vote at the attestation due offset,
// a third of the slot pre-gloas and a quarter from the gloas fork onwards.
return t.slotOffset(Duty{Slot: duty.Slot, Type: DutyAttester}), true
case DutySyncMessage, DutySyncContribution:
return t.slotDuration, true
case DutyAttester, DutyAggregator:
// Attestations and aggregations are kept for a full epoch so late partial signatures are not dropped.
return time.Duration(t.slotsPerEpoch) * t.slotDuration, true
case DutyPrepareAggregator, DutyPrepareSyncContribution:
return 2 * time.Duration(t.slotsPerEpoch) * t.slotDuration, true
case DutyPayloadAttestation:
// Payload attestation messages are only accepted on gossip within their own slot.
return t.slotDuration, true
case DutyProposerPreferences, DutyBuilderPreferences:
// Preferences are submitted from the start of the epoch before the proposal epoch
// (the earliest the assignment is derivable) and are keyed by the proposal slot,
// so entries live from submission (up to ~2 epochs early) until the slot passes.
return t.slotDuration, true
case DutyExecutionPayloadEnvelope:
// The payload envelope is revealed within the proposal slot itself, after the block
// it commits to and before the payload timeliness committee deadline.
return t.slotDuration, true
default:
return t.slotDuration, true
}
}
// retention returns how long after the start of its slot the duty's data is retained for other
// components, or false if it is retained forever.
func (t dutyTiming) retention(duty Duty) (time.Duration, bool) {
switch duty.Type {
case DutyProposer:
// An agreed proposal is retained until the end of the slot, past its submission deadline
// at the attestation due offset: from gloas the execution payload envelope of a self-built
// block is revealed after the block, from the agreed block and its payload contents.
return t.slotDuration, true
default:
return t.deadline(duty)
}
}
// newDeadliner returns a new Deadliner, this is for internal use only.
func newDeadliner(ctx context.Context, label string, expiryFunc DeadlineFunc, clock clockwork.Clock) Deadliner {
// outputBuffer big enough to support all duty types, which can expire at the same time
// while external consumer is synchronously adding duties (so not reading output).
const outputBuffer = 10
d := &deadliner{
label: label,
inputChan: make(chan expiryInput), // Not buffering this since writer wait for response.
expiryChan: make(chan Duty, outputBuffer),
clock: clock,
quit: make(chan struct{}),
}
go d.run(ctx, expiryFunc)
return d
}
func (d *deadliner) run(ctx context.Context, expiryFunc DeadlineFunc) {
duties := make(map[Duty]bool)
currDuty, currExpiry := getCurrDuty(duties, expiryFunc)
currTimer := d.clock.NewTimer(currExpiry.Sub(d.clock.Now()))
defer func() {
close(d.quit)
currTimer.Stop()
}()
setCurrState := func() {
currTimer.Stop()
currDuty, currExpiry = getCurrDuty(duties, expiryFunc)
currTimer = d.clock.NewTimer(currExpiry.Sub(d.clock.Now()))
}
// TODO(dhruv): optimise getCurrDuty and updating current state if earlier expiry detected,
// using min heap or ordered map
for {
select {
case <-ctx.Done():
return
case input := <-d.inputChan:
expiry, canExpire := expiryFunc(input.duty)
if !canExpire {
// Drop duties that never expire
input.success <- DeadlineExempt
continue
}
// Ignore (and signal) duties that have already expired.
if expiry.Before(d.clock.Now()) {
input.success <- DeadlineExpired
continue
}
input.success <- DeadlineScheduled
duties[input.duty] = true
if expiry.Before(currExpiry) {
setCurrState()
}
case <-currTimer.Chan():
// Send expired duty to receiver.
select {
case <-ctx.Done():
return
case d.expiryChan <- currDuty:
default:
log.Warn(ctx, "Deadliner output channel full", nil,
z.Str("label", d.label),
z.Any("duty", currDuty),
)
}
delete(duties, currDuty)
setCurrState()
}
}
}
// Add adds a duty to be notified of the deadline.
// See the Deadliner interface for the meaning of the returned DeadlineStatus.
func (d *deadliner) Add(duty Duty) DeadlineStatus {
success := make(chan DeadlineStatus)
select {
case <-d.quit:
return DeadlineExpired
case d.inputChan <- expiryInput{duty: duty, success: success}:
}
select {
case <-d.quit:
return DeadlineExpired
case status := <-success:
return status
}
}
// C returns the deadline channel.
func (d *deadliner) C() <-chan Duty {
return d.expiryChan
}
// getCurrDuty gets the duty to process next along-with the duty deadline. It selects duty with the latest deadline.
func getCurrDuty(duties map[Duty]bool, expiryFunc DeadlineFunc) (Duty, time.Time) {
var currDuty Duty
currExpiry := time.Date(9999, 1, 1, 0, 0, 0, 0, time.UTC)
for duty := range duties {
dutyExpiry, ok := expiryFunc(duty)
if !ok {
// Ignore the duties that never expire.
continue
}
if currExpiry.After(dutyExpiry) {
currDuty = duty
currExpiry = dutyExpiry
}
}
return currDuty, currExpiry
}