-
Notifications
You must be signed in to change notification settings - Fork 17
/
service.go
373 lines (326 loc) · 9.15 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package service
import (
"context"
"crypto/sha1"
"encoding/binary"
"fmt"
"math/rand"
"sync"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/protocols"
"github.com/ethereum/go-ethereum/rpc"
"../protocol"
)
// TODO: Change the id to sha1(peerid|data|submits.lastid), so moocher can find it in resource updates later
// Demo implements the node.Service interface
type Demo struct {
// a unique identifier used to track a request across messages
id []byte
running bool
// worker mode params
maxJobs int // maximum number of simultaneous hashing jobs the node will accept
currentJobs int // how many jobs currently executing
maxDifficulty uint8 // the maximum difficulty of jobs this node will handle
maxTimePerJob time.Duration // maximum time one hashing job will run
// moocher mode params
workers map[*protocols.Peer]uint8 // an address book of hasher peers for nodes that send requests
submitDelay time.Duration
submitDataSize int
minSubmitDifficulty uint8
maxSubmitDifficulty uint8
submits *submitStore
results *resultStore
save SaveFunc
// internal stuff
protocol *p2p.Protocol
mu sync.RWMutex
ctx context.Context
cancel func()
}
type SaveFunc func(nid []byte, mid protocol.ID, difficulty uint8, data []byte, nonce []byte, hash []byte)
type DemoParams struct {
Id []byte
MaxDifficulty uint8
MaxJobs int
MaxTimePerJob time.Duration
SubmitDelay time.Duration
SubmitDataSize int
MaxSubmitDifficulty uint8
MinSubmitDifficulty uint8
ResultSink ResultSinkFunc
Save SaveFunc
}
func NewDemoParams(sinkFunc ResultSinkFunc, saveFunc SaveFunc) *DemoParams {
return &DemoParams{
ResultSink: sinkFunc,
Save: saveFunc,
}
}
func NewDemo(params *DemoParams) (*Demo, error) {
ctx, cancel := context.WithCancel(context.Background())
d := &Demo{
id: params.Id,
running: true,
maxJobs: params.MaxJobs,
maxDifficulty: params.MaxDifficulty,
maxTimePerJob: params.MaxTimePerJob,
submitDelay: params.SubmitDelay,
submitDataSize: params.SubmitDataSize,
maxSubmitDifficulty: params.MaxSubmitDifficulty,
minSubmitDifficulty: params.MinSubmitDifficulty,
workers: make(map[*protocols.Peer]uint8),
submits: newSubmitStore(),
results: newResultStore(ctx, params.ResultSink),
save: params.Save,
ctx: ctx,
cancel: cancel,
}
if err := d.initProtocol(); err != nil {
return nil, err
}
return d, nil
}
func (self *Demo) IsWorker() bool {
return self.maxDifficulty > 0
}
func (self *Demo) APIs() []rpc.API {
return []rpc.API{
{
Namespace: "demo",
Version: "1.0",
Service: newDemoAPI(self),
Public: true,
},
}
}
func (self *Demo) initProtocol() error {
proto, err := protocol.NewDemoProtocol(self.Run)
if err != nil {
return fmt.Errorf("cant't create demo protocol")
}
proto.SkillsHandler = self.skillsHandlerLocked
proto.StatusHandler = self.statusHandlerLocked
proto.RequestHandler = self.requestHandlerLocked
proto.ResultHandler = self.resultHandlerLocked
if err := proto.Init(); err != nil {
return fmt.Errorf("can't init demo protocol")
}
self.protocol = &proto.Protocol
return nil
}
func (self *Demo) Protocol() *p2p.Protocol {
return self.protocol
}
func (self *Demo) Spec() *protocols.Spec {
return protocol.Spec
}
func (self *Demo) Protocols() (protos []p2p.Protocol) {
return []p2p.Protocol{*self.protocol}
}
func (self *Demo) Start(srv *p2p.Server) error {
self.results.Start()
return nil
}
func (self *Demo) Stop() error {
log.Error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUNNING STOP")
self.cancel()
return nil
}
// The protocol code provides Hook to run when protocol starts on a peer
func (self *Demo) Run(p *protocols.Peer) error {
self.mu.RLock()
log.Info("run protocol hook", "peer", p, "difficulty", self.maxDifficulty)
self.mu.RUnlock()
go func(self *Demo, p *protocols.Peer) {
self.mu.RLock()
maxdifficulty := self.maxDifficulty
self.mu.RUnlock()
p.Send(context.TODO(),
&protocol.Skills{
Difficulty: maxdifficulty,
},
)
if maxdifficulty > 0 {
return
}
data := make([]byte, self.submitDataSize)
tick := time.NewTicker(self.submitDelay)
for {
select {
case <-self.ctx.Done():
tick.Stop()
return
case <-tick.C:
}
_, err := rand.Read(data)
if err != nil {
return
}
self.mu.RLock()
difficulty := rand.Intn(int(self.maxSubmitDifficulty-self.minSubmitDifficulty)) + int(self.minSubmitDifficulty)
self.mu.RUnlock()
prid, err := self.submitRequest(data, uint8(difficulty))
if err != nil {
return
}
log.Debug("submitted job", "nid", fmt.Sprintf("%x", self.id[:8]), "prid", fmt.Sprintf("%x", prid))
}
}(self, p)
return nil
}
func (self *Demo) getNextWorker(difficulty uint8) *protocols.Peer {
for p, d := range self.workers {
if d >= difficulty {
return p
}
}
return nil
}
func (self *Demo) submitRequest(data []byte, difficulty uint8) (protocol.ID, error) {
self.mu.Lock()
p := self.getNextWorker(difficulty)
if p == nil {
return protocol.ID{}, fmt.Errorf("Couldn't find any workers for difficulty %d", difficulty)
}
id := newID(data, self.submits.IncSerial())
self.mu.Unlock()
//go func(id protocol.ID) {
req := &protocol.Request{
Id: id,
Data: data,
Difficulty: difficulty,
}
err := p.Send(context.TODO(), req)
if err == nil {
if err := self.submits.Put(req, id); err != nil {
log.Error("submits put fail", "err", err)
}
}
//}(id)
return id, err
}
func (self *Demo) skillsHandlerLocked(msg *protocol.Skills, p *protocols.Peer) error {
self.mu.Lock()
defer self.mu.Unlock()
log.Trace("have skills type", "msg", msg, "peer", p)
self.workers[p] = msg.Difficulty
return nil
}
func (self *Demo) statusHandlerLocked(msg *protocol.Status, p *protocols.Peer) error {
log.Trace("have status type", "msg", msg, "peer", p)
self.mu.Lock()
defer self.mu.Unlock()
switch msg.Code {
case protocol.StatusThanksABunch:
if self.IsWorker() {
log.Debug("got thanks, how polite!", "msg", msg.Id)
self.results.Del(msg.Id)
}
case protocol.StatusBusy:
if self.IsWorker() {
return nil
}
log.Debug("peer is busy. please implement throttling")
case protocol.StatusAreYouKidding:
if self.IsWorker() {
return nil
}
log.Debug("we sent wrong difficulty or it changed. please implement adjusting it")
case protocol.StatusGaveup:
if self.IsWorker() {
return nil
}
log.Debug("peer gave up on the job. please implement how to select someone else for the job")
}
return nil
}
func (self *Demo) requestHandlerLocked(msg *protocol.Request, p *protocols.Peer) error {
self.mu.Lock()
defer self.mu.Unlock()
log.Trace("have request type", "msg", msg, "currentjobs", self.currentJobs, "ourdifficulty", self.maxDifficulty, "peer", p)
if self.currentJobs >= self.maxJobs || self.results.IsFull() {
go p.Send(context.TODO(),
&protocol.Status{
Id: msg.Id,
Code: protocol.StatusBusy,
},
)
log.Error("Too busy!")
return nil
}
if self.maxDifficulty < msg.Difficulty {
go p.Send(
context.TODO(),
&protocol.Status{
Id: msg.Id,
Code: protocol.StatusAreYouKidding,
},
)
return fmt.Errorf("too hard!")
}
self.currentJobs++
go func(msg *protocol.Request) {
ctx, cancel := context.WithTimeout(self.ctx, self.maxTimePerJob)
defer cancel()
log.Debug("took job", "id", fmt.Sprintf("%x", msg.Id), "peer", p.ID().TerminalString)
j, err := doJob(ctx, msg.Data, msg.Difficulty)
if err != nil {
go p.Send(
context.TODO(),
&protocol.Status{
Id: msg.Id,
Code: protocol.StatusGaveup,
},
)
log.Debug("too long!")
return
}
res := &protocol.Result{
Id: msg.Id,
Nonce: j.Nonce,
Hash: j.Hash,
}
self.results.Put(msg.Id, res)
self.mu.Lock()
self.currentJobs--
self.mu.Unlock()
go p.Send(context.TODO(), res)
log.Debug("finished job", "id", fmt.Sprintf("%x", msg.Id), "nonce", j.Nonce, "hash", j.Hash)
}(msg)
return nil
}
func (self *Demo) resultHandlerLocked(msg *protocol.Result, p *protocols.Peer) error {
self.mu.RLock()
defer self.mu.RUnlock()
if self.maxDifficulty > 0 {
log.Trace("ignored result type", "msg", msg)
}
log.Trace("got result type", "msg", msg, "peer", p)
if !self.submits.Have(msg.Id) {
log.Debug("stale or fake request id", "id", fmt.Sprintf("%x", msg.Id))
return nil // in case it's stale not fake don't punish the peer
}
if !checkJob(msg.Hash, self.submits.GetData(msg.Id), msg.Nonce) {
return fmt.Errorf("Got incorrect result job %x from %s", msg.Id, p.ID())
}
go p.Send(
context.TODO(),
&protocol.Status{
Id: msg.Id,
Code: protocol.StatusThanksABunch,
},
)
self.save(self.id, msg.Id, self.submits.GetDifficulty(msg.Id), self.submits.GetData(msg.Id), msg.Nonce, msg.Hash)
return nil
}
func newID(data []byte, nonce uint64) (id protocol.ID) {
c := make([]byte, 8)
binary.LittleEndian.PutUint64(c, nonce)
h := sha1.New()
h.Write(data)
h.Sum(c)
copy(id[:], h.Sum(nil)[:8])
return id
}