forked from btcsuite/btcsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
miner.go
141 lines (125 loc) · 4.29 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
/*
* Copyright (c) 2014-2015 Conformal Systems LLC <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package main
import (
"fmt"
"log"
"github.com/btcsuite/btcd/wire"
rpc "github.com/btcsuite/btcrpcclient"
"github.com/btcsuite/btcutil"
)
// Miner holds all the core features required to register, run, control,
// and kill a cpu-mining btcd instance.
type Miner struct {
*Node
}
// NewMiner starts a cpu-mining enabled btcd instane and returns an rpc client
// to control it.
func NewMiner(miningAddrs []btcutil.Address, exit chan struct{},
height chan<- int32, txpool chan<- struct{}) (*Miner, error) {
ntfnHandlers := &rpc.NotificationHandlers{
// When a block higher than stopBlock connects to the chain,
// send a signal to stop actors. This is used so main can break from
// select and call actor.Stop to stop actors.
OnBlockConnected: func(hash *wire.ShaHash, h int32) {
if h >= int32(*startBlock)-1 {
if height != nil {
height <- h
}
} else {
fmt.Printf("\r%d/%d", h, *startBlock)
}
},
// Send a signal that a tx has been accepted into the mempool. Based on
// the tx curve, the receiver will need to wait until required no of tx
// are filled up in the mempool
OnTxAccepted: func(hash *wire.ShaHash, amount btcutil.Amount) {
if txpool != nil {
// this will not be blocked because we're creating only
// required no of tx and receiving all of them
txpool <- struct{}{}
}
},
}
log.Println("Starting miner on simnet...")
args, err := newBtcdArgs("miner")
if err != nil {
return nil, err
}
// set miner args - it listens on a different port
// because a node is already running on the default port
args.Listen = "127.0.0.1:18550"
args.RPCListen = "127.0.0.1:18551"
// need to log mining details, so set debuglevel
args.DebugLevel = "MINR=trace"
// if passed, set blockmaxsize to allow mining large blocks
args.Extra = []string{fmt.Sprintf("--blockmaxsize=%d", *maxBlockSize)}
// set the actors' mining addresses
for _, addr := range miningAddrs {
// make sure addr was initialized
if addr != nil {
args.Extra = append(args.Extra, "--miningaddr="+addr.EncodeAddress())
}
}
logFile, err := getLogFile(args.prefix)
if err != nil {
log.Printf("Cannot get log file, logging disabled: %v", err)
}
node, err := NewNodeFromArgs(args, ntfnHandlers, logFile)
miner := &Miner{
Node: node,
}
if err := node.Start(); err != nil {
log.Printf("%s: Cannot start mining node: %v", miner, err)
return nil, err
}
if err := node.Connect(); err != nil {
log.Printf("%s: Cannot connect to node: %v", miner, err)
return nil, err
}
// Register for transaction notifications
if err := miner.client.NotifyNewTransactions(false); err != nil {
log.Printf("%s: Cannot register for transactions notifications: %v", miner, err)
return miner, err
}
// Use just one core for mining.
if err := miner.StartMining(); err != nil {
return miner, err
}
// Register for block notifications.
if err := miner.client.NotifyBlocks(); err != nil {
log.Printf("%s: Cannot register for block notifications: %v", miner, err)
return miner, err
}
log.Printf("%s: Generating %v blocks...", miner, *startBlock)
return miner, nil
}
// StartMining sets the cpu miner to mine coins
func (m *Miner) StartMining() error {
if err := m.client.SetGenerate(true, 1); err != nil {
log.Printf("%s: Cannot start mining: %v", m, err)
return err
}
return nil
}
// StopMining stops the cpu miner from mining coins
func (m *Miner) StopMining() error {
if err := m.client.SetGenerate(false, 0); err != nil {
log.Printf("%s: Cannot stop mining: %v", m, err)
return err
}
return nil
}