-
Notifications
You must be signed in to change notification settings - Fork 3
/
blocks.go
203 lines (185 loc) · 5.54 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
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
package proxy
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"hash"
"log"
"math/big"
"strconv"
"strings"
"sync"
"sync/atomic"
"github.com/sammy007/open-ethereum-pool/rpc"
//"github.com/sammy007/open-ethereum-pool/util"
"golang.org/x/crypto/sha3"
)
type hasher func(dest []byte, data []byte)
const (
epochLength = 30000 // Blocks per epoch
)
var pow256 = math.BigPow(2, 256)
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() {
rpc := s.rpc()
count := 0
//GetWork for all miners seperately
for m, _ := range s.sessions {
go func(cs *Session) {
s.sessionsMu.Lock()
s.updateMap[cs.login] = false
s.sessionsMu.Unlock()
reply, err := rpc.GetWorkWithID(s.config.Proxy.Stratum.ShardId, cs.login)
if err != nil {
log.Printf("Error while refreshing block template on %s: %s", rpc.Name, err)
return
}
// No need to update, we have fresh job
t := s.currentBlockTemplateWithId(cs.login)
if t == nil {
var inital_atomic atomic.Value
s.minerBlockTemplateMap[cs.login] = inital_atomic
}
if t != nil && t.Header == reply[0] {
return
}
diff_template_seperate := DiffHexToDiff(reply[2])
height_temp_seperate := HexToInt64(reply[1])
seed_seperate := seedHash(height_temp_seperate)
guardian_diff_seperate := diff_template_seperate
if len(reply) == 4 {
guardian_diff_seperate = new(big.Int).Div(diff_template_seperate, new(big.Int).SetInt64(10000))
}
// Seed equals to hex string Height
nTemplate := BlockTemplate{
Header: reply[0],
Seed: fmt.Sprintf("0x%x", seed_seperate),
Target: GetTargetHexFromDiff(guardian_diff_seperate),
Height: height_temp_seperate,
Difficulty: guardian_diff_seperate,
//Difficulty: big.NewInt(diff),
GetPendingBlockCache: nil,
headers: make(map[string]heightDiffPair),
}
// Copy job backlog and add current one
nTemplate.headers[reply[0]] = heightDiffPair{
diff: guardian_diff_seperate,
height: HexToInt64(reply[1]),
}
if t != nil {
for k, v := range t.headers {
//if v.height > height-maxBacklog {
nTemplate.headers[k] = v
//}
}
}
s.sessionsMu.Lock()
atomic_temp := s.minerBlockTemplateMap[cs.login]
atomic_temp.Store(&nTemplate)
s.minerBlockTemplateMap[cs.login] = atomic_temp
s.updateMap[cs.login] = true
s.sessionsMu.Unlock()
count++
if t != nil && t.Height > s.Height {
s.Height = t.Height
s.Difficulty = t.Difficulty
}
if s.config.Proxy.Stratum.Enabled {
go s.broadcastNewJobs()
}
}(m)
}
}
func (s *ProxyServer) fetchPendingBlock() (*rpc.GetBlockReplyPart, uint64, int64, error) {
rpc := s.rpc()
reply, err := rpc.GetPendingBlock(s.config.Proxy.Stratum.ShardId)
if err != nil {
log.Printf("Error while refreshing pending block on %s: %s", rpc.Name, err)
return nil, 0, 0, err
}
blockNumber, err := strconv.ParseUint(strings.Replace(reply.Number, "0x", "", -1), 16, 64)
if err != nil {
log.Println("Can't parse pending block number")
return nil, 0, 0, err
}
blockDiff, err := strconv.ParseInt(strings.Replace(reply.Difficulty, "0x", "", -1), 16, 64)
if err != nil {
log.Println("Can't parse pending block difficulty")
return nil, 0, 0, err
}
return reply, blockNumber, blockDiff, nil
}
// makeHasher creates a repetitive hasher, allowing the same hash data structures to
// be reused between hash runs instead of requiring new ones to be created. The returned
// function is not thread safe!
func makeHasher(h hash.Hash) hasher {
// sha3.state supports Read to get the sum, use it to avoid the overhead of Sum.
// Read alters the state but we reset the hash before every operation.
type readerHash interface {
hash.Hash
Read([]byte) (int, error)
}
rh, ok := h.(readerHash)
if !ok {
panic("can't find Read method on hash")
}
outputLen := rh.Size()
return func(dest []byte, data []byte) {
rh.Reset()
rh.Write(data)
rh.Read(dest[:outputLen])
}
}
// seedHash is the seed to use for generating a verification cache and the mining
// dataset.
func seedHash(block uint64) []byte {
seed := make([]byte, 32)
if block < epochLength {
return seed
}
keccak256 := makeHasher(sha3.NewLegacyKeccak256())
for i := 0; i < int(block/epochLength); i++ {
keccak256(seed, seed)
}
return seed
}
func HexToInt64(height string) uint64 {
value, _ := strconv.ParseUint(strings.Replace(height, "0x", "", -1), 16, 64)
return value
}
// QuarkChain new function to adjust RPC
func DiffHexToDiff(diffHex string) *big.Int {
diffBytes := common.FromHex(diffHex)
return new(big.Int).SetBytes(diffBytes)
}
func GetTargetHexFromDiff(diff *big.Int) string {
diff1 := new(big.Int).Div(pow256, diff)
return string(common.ToHex(diff1.Bytes()))
}