/
blocks.go
97 lines (85 loc) · 2.34 KB
/
blocks.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
package proxy
import (
"log"
"math/big"
"strconv"
"strings"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/sammy007/open-ethereum-pool/rpc"
"github.com/sammy007/open-ethereum-pool/util"
)
const maxBacklog = 3
type heightDiffPair struct {
diff *big.Int
height uint64
}
type BlockTemplate struct {
sync.RWMutex
Header string
Seed string
Target string
Difficulty *big.Int
Height uint64
GetPendingBlockCache *rpc.GetBlockReplyPart
nonces map[string]bool
headers map[string]heightDiffPair
}
type Block struct {
difficulty *big.Int
hashNoNonce common.Hash
nonce uint64
mixDigest common.Hash
number uint64
}
func (b Block) Difficulty() *big.Int { return b.difficulty }
func (b Block) HashNoNonce() common.Hash { return b.hashNoNonce }
func (b Block) Nonce() uint64 { return b.nonce }
func (b Block) MixDigest() common.Hash { return b.mixDigest }
func (b Block) NumberU64() uint64 { return b.number }
func (s *ProxyServer) fetchBlockTemplate() {
r := s.rpc()
t := s.currentBlockTemplate()
reply, err := r.GetWork()
if err != nil {
log.Printf("Error while refreshing block template on %s: %s", r.Name, err)
return
}
// No need to update, we have fresh job
if t != nil && t.Header == reply[0] {
return
}
diff := util.TargetHexToDiff(reply[2])
height, err := strconv.ParseUint(strings.Replace(reply[3], "0x", "", -1), 16, 64)
pendingReply := &rpc.GetBlockReplyPart{
Difficulty: util.ToHex(s.config.Proxy.Difficulty),
Number: reply[3],
}
newTemplate := BlockTemplate{
Header: reply[0],
Seed: reply[1],
Target: reply[2],
Height: height,
Difficulty: diff,
GetPendingBlockCache: pendingReply,
headers: make(map[string]heightDiffPair),
}
// Copy job backlog and add current one
newTemplate.headers[reply[0]] = heightDiffPair{
diff: diff,
height: height,
}
if t != nil {
for k, v := range t.headers {
if v.height > height-maxBacklog {
newTemplate.headers[k] = v
}
}
}
s.blockTemplate.Store(&newTemplate)
log.Printf("New block to mine on %s at height %d / %s / %d", r.Name, height, reply[0][0:10], diff)
// Stratum
if s.config.Proxy.Stratum.Enabled {
go s.broadcastNewJobs()
}
}