-
Notifications
You must be signed in to change notification settings - Fork 0
/
btcd.go
214 lines (178 loc) · 4.57 KB
/
btcd.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
package bitbox
import (
"log"
"os/exec"
"github.com/angrypie/procutil"
"github.com/angrypie/rndport"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcutil"
)
const defaultAccount = "default"
type Btcd struct {
BitboxDefaults
}
func newBtcd() *Btcd {
return &Btcd{}
}
//Start runs specified number of btcd nodes in simnet mode.
func (b *Btcd) Start(nodes int) (err error) {
return b.CreateNodes(nodes, startBtcdNode)
}
//Send sends funds from node to specified address.
func (b *Btcd) Send(node int, address string, amount float64) (tx string, err error) {
addr, err := btcutil.DecodeAddress(address, &chaincfg.SimNetParams)
if err != nil {
return
}
btcAmount, err := btcutil.NewAmount(amount)
if err != nil {
return
}
hash, err := b.Client(node).SendFrom(defaultAccount, addr, btcAmount)
if err != nil {
return "", err
}
return hash.String(), nil
}
//Balance returns avaliable balance of specified nodes wallet.
func (b *Btcd) Balance(node int) (balance float64, err error) {
return b.BitboxDefaults.AccountBalance(node, defaultAccount)
}
//Address generates new adderess of specified nodes wallet.
func (b *Btcd) Address(node int) (address string, err error) {
return b.BitboxDefaults.AccountAddress(node, defaultAccount)
}
//InitMempool makes mempool usable by sending transaction and generating blocks.
func (b *Btcd) InitMempool() error {
return initBitboxMempool(b)
}
type btcdNode struct {
index int
datadir string
port string
rpcport string
walletRPCPort string
masterNodePort string
client *rpcclient.Client
btcdCmd *exec.Cmd
walletCmd *exec.Cmd
}
func (node *btcdNode) Client() *rpcclient.Client {
return node.client
}
func (node *btcdNode) Info() *State {
return &State{
NodePort: node.port,
RPCPort: node.rpcport,
ZmqAddress: "not supported",
}
}
func (node *btcdNode) Start() (err error) {
opts := append([]string{},
"--simnet",
"--notls",
"--datadir="+node.datadir,
"--listen=127.0.0.1:"+node.port,
"--rpclisten=127.0.0.1:"+node.rpcport,
"--rpcuser=test",
"--rpcpass=test",
)
if node.index > 0 {
// First node will have empty masterNodePort but its' will not be executed
opts = append(opts, "--connect=127.0.0.1:"+node.masterNodePort)
} else {
opts = append(opts,
"--txindex",
)
}
node.btcdCmd = exec.Command("btcd", opts...)
err = node.btcdCmd.Start()
if err != nil {
log.Println("ERR starting btcd")
return
}
walletOpts := append([]string{},
"--simnet",
"--createtemp",
"--noservertls",
"--noclienttls",
"--appdata="+node.datadir,
"--rpcconnect=127.0.0.1:"+node.rpcport,
"--rpclisten=127.0.0.1:"+node.walletRPCPort,
"--username=test",
"--password=test",
)
node.walletCmd = exec.Command("btcwallet", walletOpts...)
err = node.walletCmd.Start()
if err != nil {
log.Println("ERR starting btcwallet")
return
}
node.client, err = newRpcClient("127.0.0.1:" + node.walletRPCPort)
if err != nil {
return
}
waitUntilNodeStart(node)
addr, err := node.Client().GetNewAddress(defaultAccount)
if err != nil {
log.Println("ERR starting node: getting address")
return
}
opts = append(opts, "--miningaddr="+addr.String())
err = procutil.Terminate(node.btcdCmd.Process)
if err != nil {
log.Println("ERR starting node: terminating node")
return
}
node.btcdCmd = exec.Command("btcd", opts...)
err = node.btcdCmd.Start()
if err != nil {
log.Println("ERR starting btcd")
return
}
waitUntilNodeStart(node)
return
}
func (node *btcdNode) Stop() (err error) {
node.client.Shutdown()
err = procutil.Terminate(node.btcdCmd.Process)
if err != nil {
log.Println("ERR terminating btcd node process")
}
err = procutil.Terminate(node.walletCmd.Process)
if err != nil {
log.Println("ERR terminating wallet process")
}
exec.Command("rm", "-rf", node.datadir).Run()
return
}
func startBtcdNode(index int, masterNodePort, strIndex string) (node Node, err error) {
datadir := "/tmp/bitbox_btcd_" + strIndex
port, _ := rndport.GetAddress()
rpcPort, _ := rndport.GetAddress()
walletPort, _ := rndport.GetAddress()
node = &btcdNode{
index: index,
datadir: datadir,
port: port,
rpcport: rpcPort,
walletRPCPort: walletPort,
masterNodePort: masterNodePort,
}
//Create directory for node data
err = exec.Command("mkdir", datadir).Run()
if err != nil {
log.Println("ERR creating datadir", err)
return
}
err = node.Start()
if err != nil {
return
}
err = node.Client().WalletPassphrase("password", 1e6)
if err != nil {
return
}
return
}