-
Notifications
You must be signed in to change notification settings - Fork 211
/
server.go
292 lines (260 loc) · 7.65 KB
/
server.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
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
package main
import (
"context"
"encoding/binary"
"errors"
"fmt"
"net"
"net/http"
"path/filepath"
"regexp"
"strconv"
"time"
"github.com/spf13/afero"
"golang.org/x/sync/errgroup"
"github.com/spacemeshos/go-spacemesh/bootstrap"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
)
const fileRegex = "/epoch-(?P<Epoch>[0-9]+)-update-(?P<Suffix>[a-z]+)"
type NetworkParam struct {
Genesis time.Time
LyrsPerEpoch uint32
LyrDuration time.Duration
Offset uint32
}
func (np *NetworkParam) updateBeaconTime(targetEpoch types.EpochID) time.Time {
start := np.Genesis.Add(time.Duration(targetEpoch) * time.Duration(np.LyrsPerEpoch) * np.LyrDuration)
return start.Add(-1 * time.Duration(np.Offset) * np.LyrDuration)
}
func (np *NetworkParam) updateActiveSetTime(targetEpoch types.EpochID) time.Time {
start := np.Genesis.Add(time.Duration(targetEpoch) * time.Duration(np.LyrsPerEpoch) * np.LyrDuration)
return start.Add(time.Duration(np.Offset) * np.LyrDuration)
}
// Server is used to serve bootstrap update data during systest. NOT intended for production use.
// in particular, it does not protect against data loss and will serve whatever is the latest
// one on disk, even tho it's for an old epoch.
type Server struct {
*http.Server
eg errgroup.Group
logger log.Log
fs afero.Fs
gen *Generator
genFallback bool
bootstrapEpochs []types.EpochID
regex *regexp.Regexp
}
type SrvOpt func(*Server)
func WithSrvLogger(logger log.Log) SrvOpt {
return func(s *Server) {
s.logger = logger
}
}
func WithSrvFilesystem(fs afero.Fs) SrvOpt {
return func(s *Server) {
s.fs = fs
}
}
func WithBootstrapEpochs(epochs []types.EpochID) SrvOpt {
return func(s *Server) {
s.bootstrapEpochs = epochs
}
}
func NewServer(gen *Generator, fallback bool, port int, opts ...SrvOpt) *Server {
s := &Server{
Server: &http.Server{Addr: fmt.Sprintf(":%d", port)},
logger: log.NewNop(),
fs: afero.NewOsFs(),
gen: gen,
genFallback: fallback,
bootstrapEpochs: []types.EpochID{2},
regex: regexp.MustCompile(fileRegex),
}
for _, opt := range opts {
opt(s)
}
return s
}
func (s *Server) Start(ctx context.Context, errCh chan error, params *NetworkParam) {
if err := s.fs.MkdirAll(dataDir, 0o700); err != nil {
errCh <- fmt.Errorf("create persist dir %v: %w", dataDir, err)
}
s.eg.Go(func() error {
s.startHttp(errCh)
return nil
})
s.eg.Go(func() error {
s.loop(ctx, errCh, params)
return nil
})
}
func (s *Server) loop(ctx context.Context, errCh chan error, params *NetworkParam) {
var last types.EpochID
for _, epoch := range s.bootstrapEpochs {
wait := time.Until(params.updateBeaconTime(epoch))
select {
case <-time.After(wait):
if err := s.GenBootstrap(ctx, epoch); err != nil {
errCh <- err
return
}
last = epoch
case <-ctx.Done():
return
}
}
if !s.genFallback {
return
}
// start generating fallback data
s.eg.Go(
func() error {
s.genDataLoop(ctx, errCh, last, params.updateActiveSetTime, s.GenFallbackActiveSet)
return nil
})
s.eg.Go(
func() error {
s.genDataLoop(ctx, errCh, last+1, params.updateBeaconTime, s.GenFallbackBeacon)
return nil
})
}
// in systests, we want to be sure the nodes use the fallback data unconditionally.
// use a fixed known value for beacon to be sure that fallback is used during testing.
func epochBeacon(epoch types.EpochID) types.Beacon {
b := make([]byte, types.BeaconSize)
binary.LittleEndian.PutUint32(b, uint32(epoch))
return types.BytesToBeacon(b)
}
func (s *Server) GenBootstrap(ctx context.Context, epoch types.EpochID) error {
actives, err := getActiveSet(ctx, s.gen.SmEndpoint(), epoch-1)
if err != nil {
return err
}
suffix := bootstrap.SuffixBootstrap
_, err = s.gen.GenUpdate(epoch, epochBeacon(epoch), actives, suffix)
return err
}
func (s *Server) GenFallbackBeacon(_ context.Context, epoch types.EpochID) error {
suffix := bootstrap.SuffixBeacon
_, err := s.gen.GenUpdate(epoch, epochBeacon(epoch), nil, suffix)
return err
}
func (s *Server) GenFallbackActiveSet(ctx context.Context, epoch types.EpochID) error {
suffix := bootstrap.SuffixActiveSet
actives, err := getPartialActiveSet(ctx, s.gen.SmEndpoint(), epoch)
if err != nil {
return err
}
_, err = s.gen.GenUpdate(epoch, types.EmptyBeacon, actives, suffix)
return err
}
// in systests, we want to be sure the nodes use the fallback data unconditionally
// we only use half of the active set as fallback value, so we can be sure that fallback is used during testing.
func getPartialActiveSet(ctx context.Context, smEndpoint string, targetEpoch types.EpochID) ([]types.ATXID, error) {
actives, err := getActiveSet(ctx, smEndpoint, targetEpoch-1)
if err != nil {
return nil, err
}
// enough to allow hare to pass
cutoff := len(actives) * 3 / 4
return actives[:cutoff], nil
}
func (s *Server) genDataLoop(
ctx context.Context,
errCh chan error,
start types.EpochID,
timeFunc func(types.EpochID) time.Time,
genFunc func(context.Context, types.EpochID) error,
) {
for epoch := start; ; epoch++ {
wait := time.Until(timeFunc(epoch))
select {
case <-time.After(wait):
if err := genFunc(ctx, epoch); err != nil {
errCh <- err
return
}
case <-ctx.Done():
return
}
}
}
func (s *Server) startHttp(ch chan error) {
ln, err := net.Listen("tcp", s.Addr)
if err != nil {
ch <- err
return
}
http.HandleFunc("/", s.handle)
http.HandleFunc("/checkpoint", s.handleCheckpoint)
http.HandleFunc("/updateCheckpoint", s.handleUpdate)
s.logger.With().Info("server starts serving", log.String("addr", ln.Addr().String()))
if err = s.Serve(ln); err != nil {
ch <- err
}
}
func (s *Server) Stop(ctx context.Context) {
s.logger.With().Info("shutting down server")
_ = s.Shutdown(ctx)
_ = s.eg.Wait()
}
func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
matches := s.regex.FindStringSubmatch(r.URL.String())
if len(matches) != 3 {
s.logger.With().Error("unrecognized url", log.String("url", r.URL.String()))
w.WriteHeader(http.StatusNotFound)
return
}
e, err := strconv.Atoi(matches[1])
if err != nil {
s.logger.With().Error("unrecognized url", log.String("url", r.URL.String()), log.Err(err))
w.WriteHeader(http.StatusNotFound)
return
}
epoch := types.EpochID(e)
suffix := matches[2]
serveFile := PersistedFilename(epoch, suffix)
s.servefile(serveFile, w)
}
func (s *Server) handleCheckpoint(w http.ResponseWriter, _ *http.Request) {
s.servefile(CheckpointFilename(), w)
}
func (s *Server) servefile(f string, w http.ResponseWriter) {
data, err := afero.ReadFile(s.fs, f)
if err != nil && errors.Is(err, afero.ErrFileNotFound) {
w.WriteHeader(http.StatusNotFound)
return
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "servefile %s: %v", f, err)
return
}
if _, err = w.Write(data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "write response %s: %v", f, err)
return
}
}
func CheckpointFilename() string {
return filepath.Join(dataDir, "spacemesh-checkpoint")
}
func (s *Server) handleUpdate(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "ParseForm err: %v", err)
return
}
data := r.FormValue("checkpoint")
filename := CheckpointFilename()
err := afero.WriteFile(s.fs, filename, []byte(data), 0o600)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "save checkpoint err: %v", err)
return
}
s.logger.With().Info("saved checkpoint data",
log.String("data", data),
log.String("filename", filename),
)
}