-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathepoch.go
More file actions
156 lines (133 loc) · 3.9 KB
/
epoch.go
File metadata and controls
156 lines (133 loc) · 3.9 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
// Copyright 2019 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.
package babe
import (
"fmt"
"github.com/ChainSafe/gossamer/dot/types"
)
// initiateEpoch sets the epochData for the given epoch, runs the lottery for the slots in the epoch,
// and stores updated EpochInfo in the database
func (b *Service) initiateEpoch(epoch uint64) error {
var (
startSlot uint64
err error
)
if epoch == 0 {
startSlot, err = b.epochState.GetStartSlotForEpoch(epoch)
if err != nil {
return err
}
} else if epoch > 0 {
has, err := b.epochState.HasEpochData(epoch) //nolint
if err != nil {
return err
}
var data *types.EpochData
if !has {
data = &types.EpochData{
Randomness: b.epochData.randomness,
Authorities: b.epochData.authorities,
}
err = b.epochState.SetEpochData(epoch, data)
} else {
data, err = b.epochState.GetEpochData(epoch)
}
if err != nil {
return err
}
idx, err := b.getAuthorityIndex(data.Authorities)
if err != nil && err != ErrNotAuthority {
return err
}
has, err = b.epochState.HasConfigData(epoch)
if err != nil {
return err
}
if has {
cfgData, err := b.epochState.GetConfigData(epoch) //nolint
if err != nil {
return err
}
threshold, err := CalculateThreshold(cfgData.C1, cfgData.C2, len(data.Authorities))
if err != nil {
return err
}
b.epochData = &epochData{
randomness: data.Randomness,
authorities: data.Authorities,
authorityIndex: idx,
threshold: threshold,
}
} else {
b.epochData = &epochData{
randomness: data.Randomness,
authorities: data.Authorities,
authorityIndex: idx,
threshold: b.epochData.threshold,
}
}
startSlot, err = b.epochState.GetStartSlotForEpoch(epoch)
if err != nil {
return err
}
} else if b.blockState.BestBlockHash() == b.blockState.GenesisHash() {
// we are at genesis, set first slot using current time
startSlot = getCurrentSlot(b.slotDuration)
err = b.epochState.SetFirstSlot(startSlot)
if err != nil {
return err
}
}
if !b.authority {
return nil
}
logger.Debug("initiating epoch", "epoch", epoch, "start slot", startSlot)
for i := startSlot; i < startSlot+b.epochLength; i++ {
if epoch > 0 {
delete(b.slotToProof, i-b.epochLength) // clear data from previous epoch
}
b.slotToProof[i], err = b.runLottery(i, epoch)
if err != nil {
return fmt.Errorf("error running slot lottery at slot %d: error %s", i, err)
}
}
return nil
}
// incrementEpoch increments the current epoch stored in the db and returns the new epoch number
func (b *Service) incrementEpoch() (uint64, error) {
epoch, err := b.epochState.GetCurrentEpoch()
if err != nil {
return 0, err
}
next := epoch + 1
err = b.epochState.SetCurrentEpoch(next)
if err != nil {
return 0, err
}
return next, nil
}
// runLottery runs the lottery for a specific slot number
// returns an encoded VrfOutput and VrfProof if validator is authorized to produce a block for that slot, nil otherwise
// output = return[0:32]; proof = return[32:96]
func (b *Service) runLottery(slot, epoch uint64) (*VrfOutputAndProof, error) {
return claimPrimarySlot(
b.epochData.randomness,
slot,
epoch,
b.epochData.threshold,
b.keypair,
)
}