forked from robvanmieghem/gominer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
miner.go
157 lines (136 loc) · 4.68 KB
/
miner.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
package main
import (
"log"
"time"
"github.com/robvanmieghem/go-opencl/cl"
)
//HashRateReport is sent from the mining routines for giving combined information as output
type HashRateReport struct {
MinerID int
HashRate float64
}
//MiningWork is sent to the mining routines and defines what ranges should be searched for a matching nonce
type MiningWork struct {
Header []byte
Offset uint64
}
// Miner actually mines :-)
type Miner struct {
clDevice *cl.Device
minerID int
hashRateReports chan *HashRateReport
miningWorkChannel chan *MiningWork
GlobalItemSize int
siad HeaderReporter
}
func (miner *Miner) mine() {
log.Println(miner.minerID, "- Initializing", miner.clDevice.Type(), "-", miner.clDevice.Name())
context, err := cl.CreateContext([]*cl.Device{miner.clDevice})
if err != nil {
log.Fatalln(miner.minerID, "-", err)
}
defer context.Release()
commandQueue, err := context.CreateCommandQueue(miner.clDevice, 0)
if err != nil {
log.Fatalln(miner.minerID, "-", err)
}
defer commandQueue.Release()
program, err := context.CreateProgramWithSource([]string{kernelSource})
if err != nil {
log.Fatalln(miner.minerID, "-", err)
}
defer program.Release()
err = program.BuildProgram([]*cl.Device{miner.clDevice}, "")
if err != nil {
log.Fatalln(miner.minerID, "-", err)
}
kernel, err := program.CreateKernel("nonceGrind")
if err != nil {
log.Fatalln(miner.minerID, "-", err)
}
defer kernel.Release()
blockHeaderObj, err := context.CreateEmptyBuffer(cl.MemReadOnly, 88)
if err != nil {
log.Fatalln(miner.minerID, "-", err)
}
defer blockHeaderObj.Release()
kernel.SetArgBuffer(0, blockHeaderObj)
nonceOutObj, err := context.CreateEmptyBuffer(cl.MemReadWrite, 8)
if err != nil {
log.Fatalln(miner.minerID, "-", err)
}
defer nonceOutObj.Release()
kernel.SetArgBuffer(1, nonceOutObj)
localItemSize, err := kernel.WorkGroupSize(miner.clDevice)
if err != nil {
log.Fatalln(miner.minerID, "- WorkGroupSize failed -", err)
}
log.Println(miner.minerID, "- Global item size:", miner.GlobalItemSize, "(Intensity", intensity, ")", "- Local item size:", localItemSize)
log.Println(miner.minerID, "- Initialized ", miner.clDevice.Type(), "-", miner.clDevice.Name())
nonceOut := make([]byte, 8, 8)
if _, err = commandQueue.EnqueueWriteBufferByte(nonceOutObj, true, 0, nonceOut, nil); err != nil {
log.Fatalln(miner.minerID, "-", err)
}
for {
start := time.Now()
var work *MiningWork
continueMining := true
select {
case work, continueMining = <-miner.miningWorkChannel:
default:
log.Println(miner.minerID, "-", "No work ready")
work, continueMining = <-miner.miningWorkChannel
log.Println(miner.minerID, "-", "Continuing")
}
if !continueMining {
log.Println("Halting miner ", miner.minerID)
break
}
//Copy high 32 bits of Offset to Header, clear low 32 bits
for i := 0; i < 4; i++ {
work.Header[i+32] = 0
}
for i := 4; i < 8; i++ {
work.Header[i+32] = byte(work.Offset >> uint(i*8))
}
//Copy input to kernel args
if _, err = commandQueue.EnqueueWriteBufferByte(blockHeaderObj, true, 0, work.Header, nil); err != nil {
log.Fatalln(miner.minerID, "-", err)
}
//Run the kernel
if _, err = commandQueue.EnqueueNDRangeKernel(kernel, []int{int(work.Offset)}, []int{miner.GlobalItemSize}, []int{localItemSize}, nil); err != nil {
log.Fatalln(miner.minerID, "-", err)
}
//Get output
if _, err = commandQueue.EnqueueReadBufferByte(nonceOutObj, true, 0, nonceOut, nil); err != nil {
log.Fatalln(miner.minerID, "-", err)
}
//Check if match found
if nonceOut[0] != 0 || nonceOut[1] != 0 || nonceOut[2] != 0 || nonceOut[3] != 0 || nonceOut[4] != 0 || nonceOut[5] != 0 || nonceOut[6] != 0 || nonceOut[7] != 0 {
log.Println(miner.minerID, "-", "Yay, solution found!")
if nonceOut[0] == 0 {
log.Println(miner.minerID, "-", "Solution found with a nonce that started with 0...")
}
// Copy nonce to a new header.
header := append([]byte(nil), work.Header[:80]...)
for i := 0; i < 8; i++ {
header[i+32] = nonceOut[i]
}
go func() {
if err := miner.siad.SubmitHeader(header); err != nil {
log.Println(miner.minerID, "- Error submitting solution -", err)
}
}()
log.Println("Work header:", work.Header)
log.Println("Offset:", work.Offset)
log.Println("Submitted header:", header)
//Clear the output since it is dirty now
nonceOut = make([]byte, 8, 8)
if _, err = commandQueue.EnqueueWriteBufferByte(nonceOutObj, true, 0, nonceOut, nil); err != nil {
log.Fatalln(miner.minerID, "-", err)
}
}
hashRate := float64(miner.GlobalItemSize) / (time.Since(start).Seconds() * 1000000)
miner.hashRateReports <- &HashRateReport{miner.minerID, hashRate}
}
}