forked from bitcoin-s/bitcoin-s
/
BitcoindV17RpcClientTest.scala
261 lines (221 loc) · 8.35 KB
/
BitcoindV17RpcClientTest.scala
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
package org.bitcoins.rpc.v17
import org.bitcoins.core.config.RegTest
import org.bitcoins.core.crypto.{DoubleSha256DigestBE, ECPrivateKey}
import org.bitcoins.core.currency.Bitcoins
import org.bitcoins.core.number.UInt32
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.core.protocol.script.ScriptPubKey
import org.bitcoins.core.protocol.transaction.TransactionInput
import org.bitcoins.rpc.client.common.RpcOpts.{
AddressType,
LabelPurpose,
SignRawTransactionOutputParameter
}
import org.bitcoins.rpc.client.v17.BitcoindV17RpcClient
import org.bitcoins.rpc.util.AsyncUtil
import org.bitcoins.testkit.rpc.BitcoindRpcTestUtil
import org.bitcoins.testkit.util.BitcoindRpcTest
import java.time.LocalDateTime
import scala.concurrent.Future
class BitcoindV17RpcClientTest extends BitcoindRpcTest {
val usedLabel = "used_label"
val unusedLabel = "unused_label"
val clientsF: Future[(BitcoindV17RpcClient, BitcoindV17RpcClient)] =
BitcoindRpcTestUtil.createNodePairV17(clientAccum)
behavior of "BitcoindV17RpcClient"
it should "be able to get peer info" in {
for {
(freshClient, otherFreshClient) <- clientsF
infoList <- freshClient.getPeerInfo
} yield {
assert(infoList.length >= 0)
val info = infoList.head
assert(info.addnode)
assert(info.networkInfo.addr == otherFreshClient.getDaemon.uri)
}
}
it should "test mempool acceptance" in {
for {
(client, otherClient) <- clientsF
tx <- BitcoindRpcTestUtil.createRawCoinbaseTransaction(client,
otherClient)
acceptance <- client.testMempoolAccept(tx)
} yield {
assert(acceptance.rejectReason.isEmpty == acceptance.allowed)
}
}
it should "sign a raw transaction with wallet keys" in {
for {
(client, otherClient) <- clientsF
rawTx <- BitcoindRpcTestUtil.createRawCoinbaseTransaction(client,
otherClient)
signedTx <- client.signRawTransactionWithWallet(rawTx)
} yield assert(signedTx.complete)
}
// copied from Bitcoin Core: https://github.com/bitcoin/bitcoin/blob/fa6180188b8ab89af97860e6497716405a48bab6/test/functional/rpc_signrawtransaction.py
it should "sign a raw transaction with private keys" in {
val privkeys =
List("cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N",
"cVKpPfVKSJxKqVpE9awvXNWuLHCa5j5tiE7K6zbUSptFpTEtiFrA")
.map(ECPrivateKey.fromWIFToPrivateKey)
val txids =
List("9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71",
"83a4f6a6b73660e13ee6cb3c6063fa3759c50c9b7521d0536022961898f4fb02")
.map(DoubleSha256DigestBE.fromHex)
val vouts = List(0, 0)
val inputs: Vector[TransactionInput] = txids
.zip(vouts)
.map {
case (txid, vout) =>
TransactionInput.fromTxidAndVout(txid, UInt32(vout))
}
.toVector
val address =
BitcoinAddress.fromStringExn("mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB")
val outputs: Map[BitcoinAddress, Bitcoins] =
Map(address -> Bitcoins(0.1))
val scriptPubKeys =
List("76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac",
"76a914669b857c03a5ed269d5d85a1ffac9ed5d663072788ac")
.map(ScriptPubKey.fromAsmHex)
val utxoDeps = inputs.zip(scriptPubKeys).map {
case (input, pubKey) =>
SignRawTransactionOutputParameter.fromTransactionInput(input, pubKey)
}
for {
(client, _) <- clientsF
rawTx <- client.createRawTransaction(inputs, outputs)
signed <- client.signRawTransactionWithKey(rawTx,
privkeys.toVector,
utxoDeps)
} yield assert(signed.complete)
}
it should "be able to get the address info for a given address" in {
for {
(client, _) <- clientsF
addr <- client.getNewAddress
info <- client.getAddressInfo(addr)
} yield
assert(
info.timestamp.exists(_.getDayOfYear == LocalDateTime.now.getDayOfYear))
}
it should "be able to get the address info for a given P2SHSegwit address" in {
for {
(client, _) <- clientsF
addr <- client.getNewAddress(addressType = AddressType.P2SHSegwit)
info <- client.getAddressInfo(addr)
} yield
assert(
info.timestamp.exists(_.getDayOfYear == LocalDateTime.now.getDayOfYear))
}
it should "be able to get the address info for a given Legacy address" in {
for {
(client, _) <- clientsF
addr <- client.getNewAddress(addressType = AddressType.Legacy)
info <- client.getAddressInfo(addr)
} yield
assert(
info.timestamp.exists(_.getDayOfYear == LocalDateTime.now.getDayOfYear))
}
// needs #360 to be merged
it should "be able to get the address info for a given Bech32 address" in {
for {
(client, _) <- clientsF
addr <- client.getNewAddress(AddressType.Bech32)
info <- client.getAddressInfo(addr)
} yield {
assert(info.address.networkParameters == RegTest)
assert(
info.timestamp.exists(_.getDayOfYear == LocalDateTime.now.getDayOfYear))
}
}
it should "be able to get the amount received by a label" in {
for {
(client, _) <- clientsF
address <- client.getNewAddress(usedLabel)
_ <- BitcoindRpcTestUtil
.fundBlockChainTransaction(client, address, Bitcoins(1.5))
amount <- client.getReceivedByLabel(usedLabel)
} yield assert(amount == Bitcoins(1.5))
}
it should "list all labels" in {
for {
(client, _) <- clientsF
_ <- client.listLabels()
} yield succeed
}
it should "list all labels with purposes" in {
clientsF.flatMap {
case (client, otherClient) =>
val sendLabel = "sendLabel"
val isImportDone = () =>
client.ping().map(_ => true).recover {
case exc if exc.getMessage.contains("rescanning") => false
case exc =>
logger.error(s"throwing $exc")
throw exc
}
def importTx(n: Int): Future[Unit] =
for {
address <- otherClient.getNewAddress
_ <- client.importAddress(address, sendLabel + n)
_ <- AsyncUtil.retryUntilSatisfiedF(isImportDone)
} yield ()
for {
_ <- importTx(0)
_ <- importTx(1)
receiveLabels <- client.listLabels(Some(LabelPurpose.Receive))
sendLabels <- client.listLabels(Some(LabelPurpose.Send))
} yield assert(receiveLabels != sendLabels)
}
}
it should "set labels" in {
val l = "setLabel"
val btc = Bitcoins(1)
for {
(client, otherClient) <- clientsF
addr <- client.getNewAddress
_ <- BitcoindRpcTestUtil.fundBlockChainTransaction(otherClient, addr, btc)
newestBlock <- otherClient.getBestBlockHash
_ <- AsyncUtil.retryUntilSatisfiedF(() =>
BitcoindRpcTestUtil.hasSeenBlock(client, newestBlock))
oldAmount <- client.getReceivedByLabel(l)
_ = assert(oldAmount == Bitcoins(0))
_ <- client.setLabel(addr, l)
newAmount <- client.getReceivedByLabel(l)
} yield assert(newAmount == btc)
}
it should "list amounts received by all labels" in {
for {
(client, otherClient) <- clientsF
addressWithLabel <- client.getNewAddress(usedLabel)
addressNoLabel <- client.getNewAddress
_ <- otherClient.sendToAddress(addressNoLabel, Bitcoins.one)
_ <- otherClient.sendToAddress(addressWithLabel, Bitcoins.one)
newBlock +: _ <- client.getNewAddress.flatMap(
otherClient.generateToAddress(1, _))
_ <- AsyncUtil.retryUntilSatisfiedF(() =>
BitcoindRpcTestUtil.hasSeenBlock(client, newBlock))
list <- client.listReceivedByLabel()
} yield {
val receivedToUsedlabel = list.find(_.label == usedLabel)
assert(receivedToUsedlabel.isDefined)
assert(receivedToUsedlabel.get.amount > Bitcoins.zero)
val receivedDefaultLabel =
list
.find(_.label == "")
assert(receivedDefaultLabel.isDefined)
assert(receivedDefaultLabel.get.amount > Bitcoins.zero)
assert(list.forall(_.label != unusedLabel))
}
}
it should "create a wallet" in {
for {
(client, _) <- clientsF
_ <- client.createWallet("suredbits")
wallets <- client.listWallets
} yield {
assert(wallets.exists(_ == "suredbits"))
}
}
}