-
Notifications
You must be signed in to change notification settings - Fork 2
/
miner.go
130 lines (112 loc) · 2.96 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
// Copyright 2019 cruzbit developers
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
package miner
import (
"context"
"log"
"math/big"
"sync"
"time"
"github.com/cruzbit/cruzbit"
"golang.org/x/crypto/ed25519"
)
// Miner tries to mine a new tip block.
type Miner struct {
genesisID cruzbit.BlockID
peerAddr string
pubKeys []ed25519.PublicKey // receipients of any block rewards we mine
num int
shutdownChan chan struct{}
wg sync.WaitGroup
}
// NewMiner returns a new Miner instance.
func NewMiner(genesisID cruzbit.BlockID, peerAddr string, pubKeys []ed25519.PublicKey, num int) *Miner {
return &Miner{
genesisID: genesisID,
peerAddr: peerAddr,
pubKeys: pubKeys,
num: num,
shutdownChan: make(chan struct{}),
}
}
// Run executes the miner's main loop in its own goroutine.
func (m *Miner) Run() {
m.wg.Add(1)
go m.run()
}
func (m *Miner) run() {
defer m.wg.Done()
workChan := make(chan WorkMessage, 1)
submitChan := make(chan SubmitWorkMessage, 1)
peer := NewPeer(m.genesisID, workChan, submitChan)
_, err := peer.Connect(context.Background(), m.peerAddr, m.pubKeys)
if err != nil {
// XXX TODO: handle errors and reconnects
panic(err)
}
peer.Run()
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
var hashes, medianTimestamp int64
var work WorkMessage
var hasher *BlockHeaderHasher
var target cruzbit.BlockID
var targetInt *big.Int
work.WorkID = -1
// main mining loop
for {
select {
case work = <-workChan:
log.Printf("Miner %d received new work, ID: %d\n", m.num, work.WorkID)
if work.Target != nil {
target = *work.Target
} else {
target = work.Header.Target
}
targetInt = target.GetBigInt()
medianTimestamp = work.MinTime
hasher = NewBlockHeaderHasher()
case _, ok := <-m.shutdownChan:
if !ok {
close(submitChan)
peer.Shutdown()
log.Printf("Miner %d shutting down...\n", m.num)
return
}
case <-ticker.C:
// update block time every so often
now := time.Now().Unix()
if now > medianTimestamp {
work.Header.Time = now
}
default:
if work.WorkID == -1 {
time.Sleep(100 * time.Microsecond)
continue
}
// hash the block and check the proof-of-work
idInt, attempts := hasher.Update(m.num, work.Header, target)
hashes += attempts
if idInt.Cmp(targetInt) <= 0 {
// found a solution
id := new(cruzbit.BlockID).SetBigInt(idInt)
log.Printf("Miner %d calculated a share %s, submitting work, ID: %d\n",
m.num, *id, work.WorkID)
submitChan <- SubmitWorkMessage{WorkID: work.WorkID, Header: work.Header}
work.WorkID = -1
} else {
// no solution yet
work.Header.Nonce += attempts
if work.Header.Nonce > cruzbit.MAX_NUMBER {
work.Header.Nonce = 0
}
}
}
}
}
// Shutdown stops the miner synchronously.
func (m *Miner) Shutdown() {
close(m.shutdownChan)
m.wg.Wait()
log.Printf("Miner %d shutdown\n", m.num)
}