forked from Dirbaio/gominer
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathgetwork.go
71 lines (57 loc) · 1.44 KB
/
getwork.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
// Copyright (c) 2016-2023 The Decred developers.
package main
import (
"encoding/json"
"fmt"
"github.com/decred/gominer/stratum"
"github.com/decred/gominer/work"
)
// GetPoolWork gets work from a stratum enabled pool.
func GetPoolWork(pool *stratum.Stratum) (*work.Work, error) {
// Get Next work for stratum and mark it as used.
if pool.PoolWork.NewWork {
poolLog.Debug("Received new work from pool.")
// Mark used.
pool.PoolWork.NewWork = false
if pool.PoolWork.JobID == "" {
return nil, fmt.Errorf("no work available (no job id)")
}
err := pool.PrepWork()
if err != nil {
return nil, err
}
poolLog.Debugf("new job %q height %v", pool.PoolWork.JobID,
pool.PoolWork.Height)
return pool.PoolWork.Work, nil
}
// Return the work we already had, do not recalculate
if pool.PoolWork.Work != nil {
return pool.PoolWork.Work, nil
}
return nil, fmt.Errorf("no work available")
}
// GetPoolWorkSubmit sends the result to the stratum enabled pool.
func GetPoolWorkSubmit(data []byte, pool *stratum.Stratum) (bool, error) {
pool.Lock()
defer pool.Unlock()
sub, err := pool.PrepSubmit(data)
if err != nil {
return false, err
}
// JSON encode.
m, err := json.Marshal(sub)
if err != nil {
return false, err
}
// Send.
poolLog.Tracef("%s", m)
_, err = pool.Conn.Write(m)
if err != nil {
return false, err
}
_, err = pool.Conn.Write([]byte("\n"))
if err != nil {
return false, err
}
return true, nil
}