-
Notifications
You must be signed in to change notification settings - Fork 210
/
clock_options.go
83 lines (67 loc) · 1.74 KB
/
clock_options.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
package timesync
import (
"fmt"
"time"
"github.com/benbjohnson/clock"
"github.com/spacemeshos/go-spacemesh/log"
)
type option struct {
clock clock.Clock
genesisTime time.Time
layerDuration time.Duration
tickInterval time.Duration
log *log.Log
}
func (o *option) validate() error {
if o.genesisTime.IsZero() {
return fmt.Errorf("bad configuration: genesis time is zero")
}
if o.layerDuration == 0 {
return fmt.Errorf("bad configuration: layer duration is zero")
}
if o.tickInterval == 0 {
return fmt.Errorf("bad configuration: tick interval is zero")
}
if o.tickInterval < 0 || o.tickInterval > o.layerDuration {
return fmt.Errorf("bad configuration: tick interval must be between 0 and layer duration")
}
if o.log == nil {
return fmt.Errorf("bad configuration: logger is nil")
}
return nil
}
type OptionFunc func(*option) error
// withClock specifies which clock the NodeClock should use. Defaults to the real clock.
func withClock(clock clock.Clock) OptionFunc {
return func(opts *option) error {
opts.clock = clock
return nil
}
}
// WithGenesisTime sets the genesis time for the NodeClock.
func WithGenesisTime(genesis time.Time) OptionFunc {
return func(opts *option) error {
opts.genesisTime = genesis
return nil
}
}
// WithLayerDuration sets the layer duration for the NodeClock.
func WithLayerDuration(d time.Duration) OptionFunc {
return func(opts *option) error {
opts.layerDuration = d
return nil
}
}
func WithTickInterval(d time.Duration) OptionFunc {
return func(opts *option) error {
opts.tickInterval = d
return nil
}
}
// WithLogger sets the logger for the NodeClock.
func WithLogger(logger log.Log) OptionFunc {
return func(opts *option) error {
opts.log = &logger
return nil
}
}