/
service.go
205 lines (170 loc) · 4.57 KB
/
service.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
195
196
197
198
199
200
201
202
203
204
205
package service
import (
"errors"
"github.com/spacemeshos/poet-ref/shared"
"time"
)
type Config struct {
N uint `long:"n" description:"PoET time parameter"`
HashFunction string `long:"hashfunction" description:"PoET hash function"`
RoundsDuration time.Duration `long:"duration" description:"duration of the opening time for each round. If not specified, rounds duration will be determined by its previous round end of PoET execution"`
InitialRoundDuration time.Duration `long:"initialduration" description:"duration of the opening time for the initial round. if rounds duration isn't specified, this param is necessary"`
ExecuteEmpty bool `long:"empty" description:"whether to execution empty rounds, without any submitted commits"`
}
type Service struct {
cfg *Config
openRound *round
prevRound *round
rounds map[int]*round
executingRounds map[int]*round
executedRounds map[int]*round
errChan chan error
}
type InfoResponse struct {
OpenRoundId int
ExecutingRoundsIds []int
ExecutedRoundsIds []int
}
type RoundInfoResponse struct {
Opened time.Time
ExecuteStart time.Time
ExecuteEnd time.Time
NumOfCommits int
MerkleRoot []byte
Nip *shared.Proof
}
var (
ErrRoundNotFound = errors.New("round not found")
)
func NewService(cfg *Config) (*Service, error) {
s := new(Service)
s.cfg = cfg
s.rounds = make(map[int]*round)
s.executingRounds = make(map[int]*round)
s.executedRounds = make(map[int]*round)
s.errChan = make(chan error)
roundId := 1
s.openRound = s.newRound(roundId)
log.Infof("round %v opened", roundId)
go func() {
for {
// Proceed either on previous round end of execution
// or on the rounds ticker.
select {
case <-s.prevRoundExecuted():
case <-s.roundsTicker():
}
if len(s.openRound.commits) == 0 && !s.cfg.ExecuteEmpty {
continue
}
s.prevRound = s.openRound
roundId++
s.openRound = s.newRound(roundId)
log.Infof("round %v opened", roundId)
// Close previous round and execute it.
go func() {
// TODO(moshababo): apply safe concurrency
r := s.prevRound
s.executingRounds[r.Id] = r
err := r.close()
if err != nil {
s.errChan <- err
log.Error(err)
}
log.Infof("round %v closed, executing...", r.Id)
err = r.execute()
if err != nil {
s.errChan <- err
log.Error(err)
}
delete(s.executingRounds, r.Id)
s.executedRounds[r.Id] = r
log.Infof("round %v executed, phi=%v", r.Id, r.nip.Phi)
}()
}
}()
return s, nil
}
func (s *Service) Submit(data []byte) (*round, error) {
r := s.openRound
err := r.submit(data)
if err != nil {
return nil, err
}
return r, nil
}
func (s *Service) MembershipProof(roundId int, c []byte) ([][]byte, error) {
r := s.rounds[roundId]
if r == nil {
return nil, ErrRoundNotFound
}
proof, err := r.membershipProof(c)
if err != nil {
return nil, err
}
return proof, nil
}
func (s *Service) RoundInfo(roundId int) (*RoundInfoResponse, error) {
r := s.rounds[roundId]
if r == nil {
return nil, ErrRoundNotFound
}
res := new(RoundInfoResponse)
res.Opened = r.opened
res.ExecuteStart = r.executeStart
res.ExecuteEnd = r.executeEnd
res.NumOfCommits = len(r.commits)
res.MerkleRoot = r.merkleRoot
res.Nip = r.nip
return res, nil
}
func (s *Service) Info() *InfoResponse {
res := new(InfoResponse)
res.OpenRoundId = s.openRound.Id
ids := make([]int, 0, len(s.executingRounds))
for id := range s.executingRounds {
ids = append(ids, id)
}
res.ExecutingRoundsIds = ids
ids = make([]int, 0, len(s.executedRounds))
for id := range s.executedRounds {
ids = append(ids, id)
}
res.ExecutedRoundsIds = ids
return res
}
func (s *Service) round(roundId int) (*round, error) {
r := s.rounds[roundId]
if r == nil {
return nil, ErrRoundNotFound
}
return r, nil
}
func (s *Service) newRound(id int) *round {
r := newRound(s.cfg, id)
s.rounds[id] = r
return r
}
func (s *Service) prevRoundExecuted() <-chan struct{} {
if s.prevRound != nil {
return s.prevRound.executedChan
} else {
// If there's no previous round, then it's the initial round,
// So simulate the previous round end of execution with the
// initial round duration config.
executedChan := make(chan struct{})
go func() {
<-time.After(s.cfg.InitialRoundDuration)
close(executedChan)
}()
return executedChan
}
}
var dummyChan = make(chan time.Time)
func (s *Service) roundsTicker() <-chan time.Time {
if s.cfg.RoundsDuration > 0 {
return time.After(s.cfg.RoundsDuration)
} else {
return dummyChan
}
}