-
Notifications
You must be signed in to change notification settings - Fork 458
/
schedule.go
43 lines (34 loc) · 1.08 KB
/
schedule.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
package beacon
import (
"sort"
"github.com/filecoin-project/go-state-types/abi"
xerrors "github.com/pkg/errors"
cfg "github.com/filecoin-project/venus/pkg/config"
)
type Schedule []BeaconPoint
//BeaconForEpoch select beacon at specify epoch
func (bs Schedule) BeaconForEpoch(e abi.ChainEpoch) RandomBeacon {
for i := len(bs) - 1; i >= 0; i-- {
bp := bs[i]
if e >= bp.Start {
return bp.Beacon
}
}
return bs[0].Beacon
}
//DrandConfigSchedule create new beacon schedule , used to select beacon server at specify chain height
func DrandConfigSchedule(genTimeStamp uint64, blockDelay uint64, drandSchedule map[abi.ChainEpoch]cfg.DrandEnum) (Schedule, error) {
shd := Schedule{}
for start, config := range drandSchedule {
bc, err := NewDrandBeacon(genTimeStamp, blockDelay, cfg.DrandConfigs[config])
if err != nil {
return nil, xerrors.Errorf("creating drand beacon: %v", err)
}
shd = append(shd, BeaconPoint{Start: start, Beacon: bc})
}
sort.Slice(shd, func(i, j int) bool {
return shd[i].Start < shd[j].Start
})
log.Infof("Schedule: %v", shd)
return shd, nil
}