-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebminer.go
719 lines (642 loc) · 22.2 KB
/
webminer.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
package main
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"math"
"math/big"
"math/bits"
"net/http"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unicode"
"unsafe"
"golang.org/x/sync/errgroup"
)
/*
#cgo CFLAGS: -Ibstring/bstring
#cgo CFLAGS: -Ilibsha2/include
#cgo CFLAGS: -Ilibwebcash/include
#cgo CFLAGS: -Ilibwebcash/lib
#cgo amd64 386 CFLAGS: -march=znver1
#cgo arm64 CFLAGS: -march=armv8-a+crypto
// bstring
#include "bstring/bstring/bstrlib.c"
#include "bstring/bstring/bstraux.c"
// SHA256
#include "libsha2/include/sha2/sha256.h"
#include "libsha2/lib/common.c"
#include "libsha2/lib/compat/byteswap.c"
#include "libsha2/lib/sha256.c"
// ARM
#include "libsha2/lib/sha256_armv8.c"
// Intel
#include "libsha2/lib/sha256_sse4.c"
#include "libsha2/lib/sha256_sse41.c"
#include "libsha2/lib/sha256_avx2.c"
#include "libsha2/lib/sha256_shani.c"
// WebCash
#include "libwebcash/lib/support/cleanse.c"
#include "libwebcash/lib/webcash.c"
typedef struct sha256_ctx sha256_ctx_t;
void sha256_write_and_finalize_many(unsigned char* hashes, unsigned int n, struct sha256_ctx* ctx, const unsigned char nonce1[4], const unsigned char nonce2[4], const unsigned char final[4])
{
for (int k = 0; k < n; ++k) {
wc_mining_8way(&hashes[k*8*32], ctx, nonce1, &nonce2[k*8*4], final);
}
}
*/
import "C"
func GetTermsOfService() (string, error) {
const server = "https://webcash.org"
resp, err := http.Get(server + "/terms/text")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
type Uint256 [32]byte
func (hash Uint256) String() string {
// encode as hex
return fmt.Sprintf("0x%x", hash[:])
}
func ApparentDifficulty(hash Uint256) uint8 {
diff := 0
for i := 0; i < 32; i++ {
c := hash[i]
if c == 0 {
diff += 8
continue
}
diff += bits.LeadingZeros8(c)
break
}
return uint8(diff)
}
func CheckProofOfWork(hash Uint256, difficulty uint8) bool {
for i := 0; i < int(difficulty/8); i++ {
if hash[i] != 0 {
return false
}
}
if difficulty%8 != 0 {
if hash[difficulty/8]>>(8-difficulty%8) != 0 {
return false
}
}
return true
}
type Amount uint64
func (amt Amount) String() string {
integer := uint64(amt) / 1_000_000_00
decimal := uint64(amt) % 1_000_000_00
decimalString := strconv.FormatUint(decimal, 10)
if decimalString != "0" {
// Pad with leading zeros
for i := len(decimalString); i < 8; i++ {
decimalString = "0" + decimalString
}
// Remove trailing zeros
for decimalString[len(decimalString)-1] == '0' {
decimalString = decimalString[:len(decimalString)-1]
}
return fmt.Sprintf("%d.%s", integer, decimalString)
}
return fmt.Sprintf("%d", integer)
}
func (amt Amount) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", fmt.Sprint(amt)[1:])), nil
}
func (amt *Amount) UnmarshalJSON(data []byte) error {
var inner string
if err := json.Unmarshal(data, &inner); err != nil {
// Must not be wrapped as a string
inner = string(data)
}
if strings.ContainsRune(inner, '.') {
parts := strings.Split(inner, ".")
if len(parts) != 2 {
return fmt.Errorf("invalid amount: %v", data)
}
for _, rune := range parts[1] {
if !unicode.IsDigit(rune) {
return fmt.Errorf("invalid amount: %v", data)
}
}
for i := len(parts[1]); i < 8; i++ {
parts[1] += "0"
}
integer, err := strconv.ParseInt(parts[0], 10, 63)
if err != nil {
return err
}
decimal, err := strconv.ParseInt(parts[1], 10, 63)
if err != nil {
return err
}
*amt = Amount(integer*1_000_000_00 + decimal)
} else {
integer, err := strconv.ParseInt(inner, 10, 63)
if err != nil {
return err
}
*amt = Amount(integer * 1_000_000_00)
}
return nil
}
type SecretWebcash struct {
// The actual secret, typically a 64-character hex string but in principle
// any Unicode string value.
Secret string `json:"secret"`
// The amount of Webcash held by the secret.
Amount Amount `json:"amount"`
}
func (sk SecretWebcash) String() string {
return fmt.Sprintf("e%v:secret:%s", sk.Amount, sk.Secret)
}
type PublicWebcash struct {
// The public hash, a 32-byte SHA-256 hash of the secret string.
Hash Uint256 `json:"hash"`
// The amount of Webcash held by the secret.
Amount Amount `json:"amount"`
}
func (pk PublicWebcash) String() string {
return fmt.Sprintf("e%v:public:%v", pk.Amount, pk.Hash)
}
// FromSecret converts a SecretWebcash to a PublicWebcash.
func FromSecret(sk SecretWebcash) PublicWebcash {
return PublicWebcash{
Hash: sha256.Sum256([]byte(sk.Secret)),
Amount: sk.Amount,
}
}
type ProtocolSettings struct {
// The number of leading bits which must be zero for a work candidate to be
// accepted by the server.
Difficulty uint8 `json:"difficulty_target_bits"`
// The ratio of initial issuance distributed to expected amount.
Ratio float32 `json:"ratio"`
// The amount the miner is allowed to claim.
TotalReward Amount `json:"mining_amount"`
// The amount which is surrendered to the server operator.
ServerSubsidy Amount `json:"mining_subsidy_amount"`
// The number of subsidy adjustment periods which have elapsed.
Epoch uint16 `json:"epoch"`
}
func get_protocol_settings() (ProtocolSettings, error) {
const server = "https://webcash.org"
resp, err := http.Get(server + "/api/v1/target")
if err != nil {
return ProtocolSettings{}, err
}
defer resp.Body.Close()
var settings ProtocolSettings
err = json.NewDecoder(resp.Body).Decode(&settings)
if err != nil {
return ProtocolSettings{}, err
}
return settings, nil
}
func get_speed_string(attempts uint64, elapsed time.Duration) string {
if elapsed == 0 {
return "0.00 H/s"
}
speed := float64(attempts) / elapsed.Seconds()
if speed < 1_000 {
return fmt.Sprintf("%.2f H/s", speed)
}
if speed < 1_000_000 {
return fmt.Sprintf("%.2f KH/s", speed/1_000)
}
if speed < 1_000_000_000 {
return fmt.Sprintf("%.2f MH/s", speed/1_000_000)
}
if speed < 1_000_000_000_000 {
return fmt.Sprintf("%.2f GH/s", speed/1_000_000_000)
}
return fmt.Sprintf("%.2f TH/s", speed/1_000_000_000_000)
}
func get_expect_string(attempts uint64, elapsed time.Duration, difficulty uint8) string {
if elapsed == 0 {
return "unknown"
}
speed := float64(attempts) / elapsed.Seconds()
expect := math.Round(math.Exp2(float64(difficulty)) / speed)
if expect >= math.Exp2(64) {
return "never"
}
sec := uint64(expect)
min := sec / 60
hr := min / 60
day := hr / 24
var res string
if day > 0 {
res += fmt.Sprintf("%dd ", day)
}
if hr > 0 {
res += fmt.Sprintf("%dh ", hr%24)
}
if min > 0 {
res += fmt.Sprintf("%dm ", min%60)
}
if sec > 0 {
res += fmt.Sprintf("%ds", sec%60)
}
return res
}
type Solution struct {
// The hash of the solution.
Hash Uint256 `json:"hash"`
// The base64-encoded mining payload.
Preimage string `json:"preimage"`
// The reward to the miner
Reward SecretWebcash `json:"reward"`
// The committed difficulty
Difficulty uint8 `json:"difficulty"`
// The committed timestamp
Timestamp time.Time `json:"timestamp"`
}
var g_state_mutex sync.Mutex
var g_settings ProtocolSettings
var g_attempts uint64
type MiningReport struct {
// The hash of the solution.
Hash Uint256
// The base64-encoded mining payload.
Preimage string
}
func (report MiningReport) MarshalJSON() ([]byte, error) {
// Serialize preimage as string
preimage, err := json.Marshal(report.Preimage)
if err != nil {
return nil, err
}
// Convert hash to decimal notation
work := new(big.Int).SetBytes(report.Hash[:]).String()
// Serialize as JSON
return []byte(fmt.Sprintf(`{"preimage":%s,"work":%s,"legalese":{"terms":true}}`, preimage, string(work))), nil
}
func submit_solution(soln Solution) error {
const server = "https://webcash.org"
// Serialize the mining report as JSON
report, err := json.Marshal(MiningReport{
Hash: soln.Hash,
Preimage: soln.Preimage,
})
if err != nil {
// Should never happen!
fmt.Println("Error: failed to serialize mining report:", err)
return err
}
// Send the mining report to the server
resp, err := http.Post(server+"/api/v1/mining_report", "application/json", bytes.NewReader(report))
if err != nil {
// A network error should not cause us to drop the solution.
// We requeue the solution to the channel.
fmt.Println("Error: invalid server response to mining report request:", err)
return err
}
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
// A malformed server response could also be a transient error.
// We requeue the solution to the channel.
fmt.Println("Error: invalid message body in response to mining report request:", err)
return err
}
defer resp.Body.Close()
var result map[string]interface{}
if err := json.Unmarshal([]byte(body), &result); err != nil {
// The server did not return a JSON object. Again, we assume
// this is due to a transient error and requeue the solution.
fmt.Println("Error: response to mining report request is not a JSON object:", err)
return err
}
// Update difficulty, if necessary
if difficulty, ok := result["difficulty_target"]; ok {
if difficulty, ok := difficulty.(float64); ok {
difficulty := uint8(difficulty)
g_state_mutex.Lock()
old_difficulty := g_settings.Difficulty
g_settings.Difficulty = difficulty
g_state_mutex.Unlock()
if difficulty != old_difficulty {
fmt.Printf("Difficulty adjustment occured! Server says difficulty=%d\n", difficulty)
}
}
}
// Handle server rejection by saving the proof-of-work solution to the
// orphan log.
if error, ok := result["error"]; resp.StatusCode != 200 && !(resp.StatusCode == 400 && ok && error == "Didn't use a new secret value.") {
// Server rejected the solution. Save it to the orphan log.
fmt.Println("Server rejected MiningReport:", resp.StatusCode, error)
// Save the solution to the orphan log
f, err := os.OpenFile("orphan.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error: failed to open orphan.log:", err)
// Do not return error to prevent the solution from being requeued.
return nil
}
io.WriteString(f, fmt.Sprintln(soln))
f.Close()
// No error is returned to prevent the solution from being requeued.
return nil
}
// Write the claim code for the newly generated coin to the log
f, err := os.OpenFile("webcash.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error: failed to open webcash.log:", err)
// Do not return error or else the solution will be requeued.
return nil
}
io.WriteString(f, fmt.Sprintln(soln.Reward))
f.Close()
return nil
}
func update_thread(ctx context.Context, solutions chan Solution) {
// Record start time
last_settings_fetch := time.Now()
timeout := 15 * time.Second
watchdog := time.NewTimer(timeout)
for {
// https://medium.com/@oboturov/golang-time-after-is-not-garbage-collected-4cbc94740082
watchdog.Reset(time.Until(last_settings_fetch.Add(timeout)))
select {
case <-ctx.Done():
fmt.Println("closing update thread")
return
case soln := <-solutions:
// Do not submit work less than the current difficulty
if soln.Difficulty < g_settings.Difficulty {
fmt.Println("Ignoring solution as difficulty commitment is too low: (", soln.Difficulty, "<", g_settings.Difficulty, ")")
continue
}
if ApparentDifficulty(soln.Hash) < g_settings.Difficulty {
fmt.Println("Ignoring solution as apparent difficulty is too low: (", ApparentDifficulty(soln.Hash), "<", g_settings.Difficulty, ")")
continue
}
// Do not submit stale work
now := time.Now()
if soln.Timestamp.Before(now.Add(-2 * time.Hour)) {
fmt.Println("Ignoring solution as timestamp is too old: (", soln.Timestamp, "<", now.Add(-2*time.Hour), ")")
continue
}
// Submit the solution to the server
if err := submit_solution(soln); err != nil {
fmt.Println("Possible transient error, or server timeout? Waiting to re-attempt.")
go func() {
// Wait 8 seconds before re-attempting
time.Sleep(8 * time.Second)
// Requeue the solution
solutions <- soln
}()
continue
}
case <-watchdog.C:
settings, err := get_protocol_settings()
// Update the watchdog timer to the current time, before checking
// the result of the fetch, so that there is a delay between
// attempts.
now := time.Now()
old_last_settings_fetch := last_settings_fetch
last_settings_fetch = now
// If we failed to fetch the settings, wait before trying again.
if err != nil {
fmt.Println(err)
continue
}
// Update global state
g_state_mutex.Lock()
g_settings = settings
attempts := atomic.SwapUint64(&g_attempts, 0)
g_state_mutex.Unlock()
// Record how much time has elapsed since the last update
elapsed := now.Sub(old_last_settings_fetch)
// Print the current difficulty and speed
fmt.Printf("server says difficulty=%v ratio=%v speed=%s expect=%v\n", settings.Difficulty, settings.Ratio, get_speed_string(attempts, elapsed), get_expect_string(attempts, elapsed, settings.Difficulty))
}
}
}
func mining_thread(ctx context.Context, id int, solutions chan Solution) {
// The largest difficulty we will attempt work on.
const max_difficulty = 50
// The numbes "000" through "999" concatenated together and encoded into
// base64. Any 3 ASCII digits encode to 4 base64 digits, so any number N in
// this range can be encoded as nonces[4*N : 4*N+4].
nonces := []byte("" +
"MDAwMDAxMDAyMDAzMDA0MDA1MDA2MDA3MDA4MDA5MDEwMDExMDEyMDEzMDE0MDE1MDE2MDE3MDE4MDE5" +
"MDIwMDIxMDIyMDIzMDI0MDI1MDI2MDI3MDI4MDI5MDMwMDMxMDMyMDMzMDM0MDM1MDM2MDM3MDM4MDM5" +
"MDQwMDQxMDQyMDQzMDQ0MDQ1MDQ2MDQ3MDQ4MDQ5MDUwMDUxMDUyMDUzMDU0MDU1MDU2MDU3MDU4MDU5" +
"MDYwMDYxMDYyMDYzMDY0MDY1MDY2MDY3MDY4MDY5MDcwMDcxMDcyMDczMDc0MDc1MDc2MDc3MDc4MDc5" +
"MDgwMDgxMDgyMDgzMDg0MDg1MDg2MDg3MDg4MDg5MDkwMDkxMDkyMDkzMDk0MDk1MDk2MDk3MDk4MDk5" +
"MTAwMTAxMTAyMTAzMTA0MTA1MTA2MTA3MTA4MTA5MTEwMTExMTEyMTEzMTE0MTE1MTE2MTE3MTE4MTE5" +
"MTIwMTIxMTIyMTIzMTI0MTI1MTI2MTI3MTI4MTI5MTMwMTMxMTMyMTMzMTM0MTM1MTM2MTM3MTM4MTM5" +
"MTQwMTQxMTQyMTQzMTQ0MTQ1MTQ2MTQ3MTQ4MTQ5MTUwMTUxMTUyMTUzMTU0MTU1MTU2MTU3MT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
"ODgwODgxODgyODgzODg0ODg1ODg2ODg3ODg4ODg5ODkwODkxODkyODkzODk0ODk1ODk2ODk3ODk4ODk5" +
"OTAwOTAxOTAyOTAzOTA0OTA1OTA2OTA3OTA4OTA5OTEwOTExOTEyOTEzOTE0OTE1OTE2OTE3OTE4OTE5" +
"OTIwOTIxOTIyOTIzOTI0OTI1OTI2OTI3OTI4OTI5OTMwOTMxOTMyOTMzOTM0OTM1OTM2OTM3OTM4OTM5" +
"OTQwOTQxOTQyOTQzOTQ0OTQ1OTQ2OTQ3OTQ4OTQ5OTUwOTUxOTUyOTUzOTU0OTU1OTU2OTU3OTU4OTU5" +
"OTYwOTYxOTYyOTYzOTY0OTY1OTY2OTY3OTY4OTY5OTcwOTcxOTcyOTczOTc0OTc1OTc2OTc3OTc4OTc5" +
"OTgwOTgxOTgyOTgzOTg0OTg1OTg2OTg3OTg4OTg5OTkwOTkxOTkyOTkzOTk0OTk1OTk2OTk3OTk4OTk5")
// Close the JSON object: '}'
final := []byte("fQ==")
Restart:
for {
select {
case <-ctx.Done():
fmt.Println("closing mining thread", id)
return
default:
}
// Get the current difficulty
g_state_mutex.Lock()
settings := g_settings
g_state_mutex.Unlock()
// If the difficulty is too high, wait a bit and try again
if settings.Difficulty > max_difficulty {
time.Sleep(5 * time.Second)
continue
}
// Generate a random secret using the runtime's CSPRNG. We don't need
// to go to excessively paranoid lengths to ensure the secret has good
// entropy, as the secret is going to be redeemed immediately after the
// solution is submitted. 18 bytes is 144 bits of preimage security, or
// 72 bits of collision resistance, which is plenty.
var sk [18]byte
_, err := rand.Read(sk[:])
if err != nil {
panic(err)
}
keep := SecretWebcash{
Secret: base64.StdEncoding.EncodeToString(sk[:]),
Amount: settings.TotalReward - settings.ServerSubsidy,
}
// Generate another secret for the server subsidy.
_, err = rand.Read(sk[:])
if err != nil {
panic(err)
}
subsidy := SecretWebcash{
Secret: base64.StdEncoding.EncodeToString(sk[:]),
Amount: settings.ServerSubsidy,
}
// Clear the secret from memory
for i := range sk {
sk[i] = 0
}
// Create the mining payload, a serialized JSON object.
// The miner won't get this far if the terms of service aren't agreed
// to, so we can safely hard-code acceptance here.
now := time.Now()
μsec := fmt.Sprintf("%06d", now.UnixMicro()%1000000)
for len(μsec) > 1 && μsec[len(μsec)-1] == '0' {
μsec = μsec[:len(μsec)-1]
}
prefix := []byte(fmt.Sprintf(`{"legalese":{"terms":true},"webcash":["%v","%v"],"subsidy":["%v"],"difficulty":%d,"timestamp":%d.%s,"nonce":`, keep, subsidy, subsidy, settings.Difficulty, now.Unix(), μsec))
// Extend the prefix to be a multiple of 48 in size...
for len(prefix)%48 != 47 {
prefix = append(prefix, ' ')
}
prefix = append(prefix, '1')
// ...which becomes 64 bytes when base64-encoded.
prefix = []byte(base64.StdEncoding.EncodeToString(prefix))
// And 64 bytes is the SHA256 block size, so we can compute a midstate
var midstate C.sha256_ctx_t
C.sha256_init(&midstate)
C.sha256_update(&midstate, unsafe.Pointer(&prefix[0]), C.size_t(len(prefix)))
const W = 25 * 8
var hashes [W]Uint256
for i := 0; i < 1000; i++ {
for j := 0; j < 1000; j += W {
atomic.AddUint64(&g_attempts, W)
// Compute W-many hashes at once
C.sha256_write_and_finalize_many((*C.uint8_t)(&hashes[0][0]), W/8, &midstate, (*C.uint8_t)(&nonces[4*i]), (*C.uint8_t)(&nonces[4*j]), (*C.uint8_t)(&final[0]))
for k := 0; k < W; k++ {
if hashes[k][0] == 0 && hashes[k][1] == 0 {
if CheckProofOfWork(hashes[k], settings.Difficulty) {
// We found a solution!
payload := string(bytes.Join([][]byte{prefix, nonces[4*i : 4*i+4], nonces[4*(j+k) : 4*(j+k)+4], final}, []byte{}))
fmt.Println("GOT SOLUTION!!!", payload, hashes[k], keep.String())
solutions <- Solution{
Hash: hashes[k],
Preimage: payload,
Reward: keep,
Difficulty: settings.Difficulty,
Timestamp: now,
}
// Any other valid solutions in this batch will
// conflict with the one that we have already found,
// since secrets may be used only once.
continue Restart
}
}
}
}
}
}
}
func main() {
terms, err := GetTermsOfService()
if err != nil {
panic(err)
}
fmt.Println(terms)
algo := C.GoString(C.sha256_auto_detect())
fmt.Println("Using SHA256 algorithm:", algo)
settings, err := get_protocol_settings()
if err != nil {
panic(err)
}
fmt.Println(settings)
g_settings = settings
ctx, done := context.WithCancel(context.Background())
defer done() // in case of early exit
g, gctx := errgroup.WithContext(ctx)
// goroutine to check for Ctrl-C
g.Go(func() error {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
select {
case sig := <-c:
fmt.Println("caught signal", sig)
done()
case <-ctx.Done():
}
fmt.Println("closing signal handler")
return gctx.Err()
})
solutions := make(chan Solution)
// goroutine which periodically queries the webcash server for change in
// difficulty or subsidy, and submits solution mining reports.
g.Go(func() error {
update_thread(gctx, solutions)
return nil
})
// goroutine which performs mining
for i := 0; i < runtime.NumCPU(); i++ {
id := i
g.Go(func() error {
mining_thread(gctx, id, solutions)
return nil
})
}
// wait for all goroutines to exit
err = g.Wait()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("all goroutines exited")
}
}