-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminer.go
346 lines (285 loc) · 9.58 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
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
// Netrunner
// Copyright 2021-2023 DERO Foundation. All rights reserved.
// Use of this source code in any form is governed by RESEARCH license.
// license can be found in the LICENSE file.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package main
import (
"crypto/rand"
"crypto/tls"
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
"net/url"
"runtime"
"strconv"
"sync"
"sync/atomic"
"time"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
"github.com/deroproject/derohe/astrobwt/astrobwtv3"
"github.com/deroproject/derohe/block"
"github.com/deroproject/derohe/globals"
"github.com/deroproject/derohe/rpc"
"github.com/gorilla/websocket"
)
type Miner struct {
Mission int64
Height int64
Blocks uint64
MiniBlocks uint64
Hashrate string
NWHashrate string
Connection *websocket.Conn
Label *canvas.Text
LabelBlocks *canvas.Text
Address string
Threads int
Daemon string
BlockList []string
ScrollBox *widget.List
Data binding.StringList
}
var m Miner
var mutex sync.RWMutex
var job rpc.GetBlockTemplate_Result
var job_counter int64
var maxdelay int = 10000
var iterations int = 100
var max_pow_size int = 819200 //astrobwt.MAX_LENGTH
var counter uint64
var hash_rate uint64
var Difficulty uint64
var our_height int64
var block_counter uint64
var mini_block_counter uint64
func startRunner(w string, d string, t int) {
m.Mission = 1
globals.Arguments["--wallet-address"] = w
globals.Arguments["--daemon-rpc-address"] = d
globals.Arguments["--mining-threads"] = strconv.Itoa(t)
if status.network {
globals.Arguments["--testnet"] = true
} else {
globals.Arguments["--testnet"] = false
}
if globals.Arguments["--wallet-address"] != nil {
addr, err := globals.ParseValidateAddress(globals.Arguments["--wallet-address"].(string))
if err != nil {
globals.Logger.Error(err, "[Miner] Wallet address is invalid.")
return
}
m.Address = addr.String()
}
if globals.Arguments["--daemon-rpc-address"] != nil {
m.Daemon = globals.Arguments["--daemon-rpc-address"].(string)
}
m.Threads = runtime.GOMAXPROCS(0)
if globals.Arguments["--mining-threads"] != nil {
if s, err := strconv.Atoi(globals.Arguments["--mining-threads"].(string)); err == nil {
m.Threads = s
} else {
globals.Logger.Error(err, "[Miner] Mining threads argument cannot be parsed.")
}
if m.Threads > runtime.GOMAXPROCS(0) {
globals.Logger.Info("[Miner] Mining threads is more than available CPUs. This is NOT optimal", "thread_count", m.Threads, "max_possible", runtime.GOMAXPROCS(0))
}
}
globals.Logger.Info(fmt.Sprintf("[Miner] System will mine to \"%s\" with %d threads. Good Luck!!", m.Address, m.Threads))
if m.Threads < 1 || iterations < 1 || m.Threads > 2048 {
iterations = 1
m.Threads = 1
}
// This tiny goroutine continuously updates status as required
go func() {
for m.Mission == 1 {
last_our_height := int64(0)
last_best_height := int64(0)
last_counter := uint64(0)
last_counter_time := time.Now()
last_mining_state := false
_ = last_mining_state
mining := true
mining_speed := 0.00
for m.Mission == 1 {
if m.Mission == 0 {
mining_speed = 0
return
}
best_height := int64(0)
// only update prompt if needed
if last_our_height != our_height || last_best_height != best_height || last_counter != counter {
mining_string := ""
if mining {
mining_speed = float64(counter-last_counter) / (float64(uint64(time.Since(last_counter_time))) / 1000000000.0)
last_counter = counter
last_counter_time = time.Now()
switch {
case mining_speed > 1000000:
mining_string = fmt.Sprintf("%.3f MH/s", float32(mining_speed)/1000000.0)
case mining_speed > 1000:
mining_string = fmt.Sprintf("%.3f KH/s", float32(mining_speed)/1000.0)
case mining_speed > 0:
mining_string = fmt.Sprintf("%.0f H/s", mining_speed)
}
}
last_mining_state = mining
hash_rate_string := ""
switch {
case hash_rate > 1000000000000:
hash_rate_string = fmt.Sprintf("%.3f TH/s", float64(hash_rate)/1000000000000.0)
case hash_rate > 1000000000:
hash_rate_string = fmt.Sprintf("%.3f GH/s", float64(hash_rate)/1000000000.0)
case hash_rate > 1000000:
hash_rate_string = fmt.Sprintf("%.3f MH/s", float64(hash_rate)/1000000.0)
case hash_rate > 1000:
hash_rate_string = fmt.Sprintf("%.3f KH/s", float64(hash_rate)/1000.0)
case hash_rate > 0:
hash_rate_string = fmt.Sprintf("%d H/s", hash_rate)
}
m.Height = our_height
m.Blocks = block_counter
m.MiniBlocks = mini_block_counter
m.NWHashrate = hash_rate_string
m.Hashrate = mining_string
last_our_height = our_height
last_best_height = best_height
}
time.Sleep(1 * time.Second)
}
}
}()
if m.Threads > 255 {
globals.Logger.Error(nil, "[Miner] This program supports maximum 256 CPU cores.", "available", m.Threads)
m.Threads = 255
}
if m.Mission == 1 {
go getwork(m.Address)
for i := 0; i < m.Threads; i++ {
go mineblock(i)
}
}
return
}
func random_execution(wg *sync.WaitGroup, iterations int) {
var workbuf [255]byte
runtime.LockOSThread()
threadaffinity()
rand.Read(workbuf[:])
for i := 0; i < iterations; i++ {
_ = astrobwtv3.AstroBWTv3(workbuf[:])
}
wg.Done()
runtime.UnlockOSThread()
}
var connection_mutex sync.Mutex
func getwork(wallet_address string) {
if m.Mission == 0 {
m.Connection.Close()
return
}
var err error
for m.Mission == 1 {
if m.Mission == 0 {
break
}
u := url.URL{Scheme: "wss", Host: m.Daemon, Path: "/ws/" + wallet_address}
globals.Logger.Info("[Miner] Connecting to ", "url", u.String())
dialer := websocket.DefaultDialer
dialer.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
m.Connection, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
globals.Logger.Info("[Miner] Will try in 10 secs", "server adress", m.Daemon)
time.Sleep(10 * time.Second)
continue
}
var result rpc.GetBlockTemplate_Result
wait_for_another_job:
if m.Mission == 0 {
m.Connection.Close()
break
}
if err = m.Connection.ReadJSON(&result); err != nil {
globals.Logger.Error(err, "[Miner] Error connecting to server")
continue
}
mutex.Lock()
job = result
job_counter++
mutex.Unlock()
if job.LastError != "" {
globals.Logger.Error(nil, "[Miner] Received error", "err", job.LastError)
}
block_counter = job.Blocks
mini_block_counter = job.MiniBlocks
hash_rate = job.Difficultyuint64
our_height = int64(job.Height)
Difficulty = job.Difficultyuint64
//fmt.Printf("[Miner] recv: %+v diff %d\n", result, Difficulty)
goto wait_for_another_job
}
}
func mineblock(tid int) {
var diff big.Int
var work [block.MINIBLOCK_SIZE]byte
var random_buf [12]byte
rand.Read(random_buf[:])
time.Sleep(5 * time.Second)
nonce_buf := work[block.MINIBLOCK_SIZE-5:] //since slices are linked, it modifies parent
runtime.LockOSThread()
threadaffinity()
var local_job_counter int64
i := uint32(0)
for m.Mission == 1 {
if m.Mission == 0 {
break
}
mutex.RLock()
myjob := job
local_job_counter = job_counter
mutex.RUnlock()
n, err := hex.Decode(work[:], []byte(myjob.Blockhashing_blob))
if err != nil || n != block.MINIBLOCK_SIZE {
globals.Logger.Error(err, "[Miner] Blockwork could not decoded successfully", "blockwork", myjob.Blockhashing_blob, "n", n, "job", myjob)
time.Sleep(time.Second)
continue
}
copy(work[block.MINIBLOCK_SIZE-12:], random_buf[:]) // add more randomization in the mix
work[block.MINIBLOCK_SIZE-1] = byte(tid)
diff.SetString(myjob.Difficulty, 10)
if work[0]&0xf != 1 { // check version
globals.Logger.Error(nil, "[Miner] Unknown version, please check for updates", "version", work[0]&0x1f)
time.Sleep(time.Second)
continue
}
for local_job_counter == job_counter && m.Mission == 1 { // update job when it comes, expected rate 1 per second
i++
binary.BigEndian.PutUint32(nonce_buf, i)
powhash := astrobwtv3.AstroBWTv3(work[:])
atomic.AddUint64(&counter, 1)
if CheckPowHashBig(powhash, &diff) == true { // note we are doing a local, NW might have moved meanwhile
globals.Logger.Info("[Miner] Successfully found DERO miniblock (going to submit)", "difficulty", myjob.Difficulty, "height", myjob.Height)
func() {
defer globals.Recover(1)
connection_mutex.Lock()
defer connection_mutex.Unlock()
m.Connection.WriteJSON(rpc.SubmitBlock_Params{JobID: myjob.JobID, MiniBlockhashing_blob: fmt.Sprintf("%x", work[:])})
}()
}
}
}
}