-
Notifications
You must be signed in to change notification settings - Fork 2
/
IridiumClientTest.groovy
570 lines (449 loc) · 15.1 KB
/
IridiumClientTest.groovy
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
package cash.ird.walletd
import cash.ird.walletd.model.body.*
import cash.ird.walletd.model.request.BlockHashRange
import cash.ird.walletd.model.request.BlockIndexRange
import cash.ird.walletd.model.request.PrivateKey
import cash.ird.walletd.model.request.PublicKey
import cash.ird.walletd.rpc.exception.IridiumWalletdException
import com.anotherchrisberry.spock.extensions.retry.RetryOnFailure
import groovy.util.logging.Slf4j
import spock.lang.Ignore
import spock.lang.Shared
import spock.lang.Specification
@Slf4j
class IridiumClientTest extends Specification {
@Shared
private IridiumAPI sut
@Shared
private IridiumAPI sut2
@Shared
private String wallet1
@Shared
private String wallet2
void setupSpec() {
sut = new IridiumClient("localhost", 14008)
wallet1 = getClass().getResource("/iridium/wallet1/wallet.adr").readLines().first()
sut2 = new IridiumClient("localhost", 14009)
waitForBalance(sut, wallet1, 1)
waitForBlockHeightTotal(sut, 10)
waitForBlockHeightTotal(sut2, 10)
}
void setup() {
wallet2 = sut2.createAddress()
def status = sut.getStatus()
waitForBlockHeightTotal(sut2, status.getBlockCount().toInteger())
}
void cleanupSpec() {
sut.reset()
sut2.reset()
}
@Ignore
def "Reset"() {
when:
Boolean success = sut.reset()
then:
success
}
@Ignore
def "Reset with viewSecretKey"() {
given:
String viewKey = sut.getViewKey()
when:
Boolean success = sut.reset(viewKey)
then:
success
}
@Ignore
def "Reset with address should fail"() {
given:
String address = sut.createAddress()
when:
Boolean success = sut.reset(address)
then:
IridiumWalletdException ex = thrown(IridiumWalletdException)
ex.code == -32000
ex.message.contains("Wrong key format")
}
def "Save"() {
when:
Boolean success = sut.save()
then:
success
}
def "GetStatus for wallet1"() {
when:
Status status = sut.getStatus()
then:
status.blockCount != null
}
def "GetStatus for wallet2"() {
when:
Status status = sut2.getStatus()
then:
status.blockCount != null
}
def "GetViewKey"() {
when:
String viewKey = sut.getViewKey()
then:
viewKey != null
}
def "GetSpendKeys"() {
when:
SpendKeyPair keyPair = sut.getSpendKeys(wallet1)
then:
keyPair != null
keyPair.getPublicKey() != null
keyPair.getSecretKey() != null
}
def "GetAddresses"() {
when:
List<String> addresses = sut.getAddresses()
then:
!addresses.isEmpty()
}
def "CreateAddress without param"() {
when:
String address = sut.createAddress()
then:
address != null
}
@Ignore("ignored until we figure out how that is working in the core")
def "CreateAddress with publicKey"() {
when:
String address = sut.createAddress(PublicKey.of("test123"))
then:
address != null
}
def "Import wallet from viewSecretKey and spendSecretKey"() {
given:
def testAddress = sut.createAddress()
def keyPair = sut.getSpendKeys(testAddress)
def viewKey = sut.getViewKey()
when:
sut2.reset(viewKey)
String address = sut2.createAddress(PrivateKey.of(keyPair.getSecretKey()))
then:
address == testAddress
when:
def addresses = sut2.getAddresses()
then:
addresses.size() == 1
}
def "DeleteAddress"() {
setup:
String addressToBeDeleted = sut.createAddress()
when:
boolean success = sut.deleteAddress(addressToBeDeleted)
then:
success
}
def "GetBalance"() {
when:
Balance balance = sut.getBalance()
then:
balance
balance.availableBalance >= 0
balance.lockedAmount >= 0
}
def "GetBalance with address"() {
setup:
String address = sut.createAddress()
when:
Balance balance = sut.getBalance(address)
then:
balance
balance.availableBalance == 0
balance.lockedAmount == 0
}
def "GetBalance for wallet1"() {
when:
Balance balance = sut.getBalance(wallet1)
then:
balance
balance.availableBalance >= 0
balance.lockedAmount >= 0
}
def "GetBalance for wallet2"() {
when:
Balance balance = sut2.getBalance(wallet2)
then:
balance
balance.availableBalance >= 0
balance.lockedAmount >= 0
}
def "GetBlockHashes"() {
when:
List<String> blockHashes = sut.getBlockHashes(BlockIndexRange.of(0, 1))
then:
!blockHashes.isEmpty()
blockHashes.size() == 1
}
def "GetTransactionHashes with BlockIndexRange"() {
given:
Status status = sut.getStatus()
when:
List<TransactionHashBag> hashBags = sut.getTransactionHashes(BlockIndexRange.of(0, status.knownBlockCount))
then:
!hashBags.isEmpty()
}
def "GetTransactionHashes with BlockHashRange"() {
given:
String firstBlockHash = sut.getBlockHashes(BlockIndexRange.of(0, 1)).first()
Status status = sut.getStatus()
when:
List<TransactionHashBag> hashBags = sut.getTransactionHashes(BlockHashRange.of(firstBlockHash, status.knownBlockCount))
then:
!hashBags.isEmpty()
!hashBags.first().transactionHashes.isEmpty()
}
def "GetTransactionHashes with BlockHashRange and addresses"() {
given:
String firstBlockHash = sut.getBlockHashes(BlockIndexRange.of(0, 1)).first()
List<String> addresses = [sut.createAddress()]
Status status = sut.getStatus()
when:
List<TransactionHashBag> hashBags = sut.getTransactionHashes(BlockHashRange.of(firstBlockHash, status.knownBlockCount), addresses)
then:
!hashBags.isEmpty()
hashBags.first().transactionHashes.isEmpty()
}
def "GetTransactionHashes with BlockIndexRange and addresses and paymentId"() {
given:
List<String> addresses = [sut.createAddress()]
String paymentId = generatePaymentId()
Status status = sut.getStatus()
when:
List<TransactionHashBag> hashBags = sut.getTransactionHashes(BlockIndexRange.of(0, status.knownBlockCount), addresses, paymentId)
then:
!hashBags.isEmpty()
hashBags.first().transactionHashes.isEmpty()
}
def "GetTransactions with index"() {
given:
Status status = sut.getStatus()
when:
List<TransactionItemBag> bagList = sut.getTransactions(BlockIndexRange.of(0, status.knownBlockCount))
then:
!bagList.empty
}
def "GetTransactions with hash"() {
given:
Status status = sut.getStatus()
String firstBlockHash = sut.getBlockHashes(BlockIndexRange.of(0, 1)).first()
when:
List<TransactionItemBag> bagList = sut.getTransactions(BlockHashRange.of(firstBlockHash, status.knownBlockCount))
then:
!bagList.empty
}
def "GetUnconfirmedTransactionHashes"() {
when:
List<String> unconfirmedTransactionHashes = sut.getUnconfirmedTransactionHashes()
then:
unconfirmedTransactionHashes != null
when:
List<String> unconfirmedTransactionHashes2 = sut2.getUnconfirmedTransactionHashes()
then:
unconfirmedTransactionHashes2 != null
}
@RetryOnFailure(delaySeconds = 10, times = 29)
def "SendTransaction to wallet2"() {
setup:
waitForBalance(sut, wallet1, 105000)
when:
String transactionHash = sut.sendTransaction(
[Transfer.of(100000, wallet2)],
5000,
2,
wallet1,
null,
0, //no confirmation at all, just next block has to be mined
null
)
then:
Transaction tx = sut.getTransaction(transactionHash)
tx.amount == -105000
waitForTransactionConfirmation(sut, transactionHash)
when:
Transaction tx2 = sut2.getTransaction(transactionHash)
then:
tx2.amount == 100000
}
@RetryOnFailure(delaySeconds = 10, times = 29)
def "SendTransaction to multiple addresses on wallet2"() {
setup:
waitForBalance(sut, wallet1, 205000)
def address1 = sut2.createAddress()
def address2 = sut2.createAddress()
def status = sut2.getStatus()
when:
String transactionHash = sut.sendTransaction(
[
Transfer.of(100000, address1),
Transfer.of(100000, address2)
],
5000,
2,
wallet1,
[wallet1],
null,
0, //no confirmation at all, just next block has to be mined
null
)
then:
Transaction tx = sut.getTransaction(transactionHash)
tx.amount == -205000
waitForTransactionConfirmation(sut, transactionHash)
waitForTransactionConfirmation(sut2, transactionHash)
when:
Transaction tx2 = sut2.getTransaction(transactionHash)
def balance1 = sut2.getBalance(address1)
def balance2 = sut2.getBalance(address2)
then:
tx2.amount == 200000
balance1.availableBalance == 100000 || balance1.lockedAmount == 100000
balance2.availableBalance == 100000 || balance2.lockedAmount == 100000
when:
def transactionHashList = sut2.getUnconfirmedTransactionHashes([address1, address2])
then:
transactionHashList.empty
when:
def txItemBagList = sut2.getTransactions(BlockHashRange.of(status.lastBlockHash, 10), [address1, address2])
then:
txItemBagList.find { it.transactions.find {it.transactionHash == transactionHash} } != null
}
@RetryOnFailure(delaySeconds = 10, times = 29)
def "Delayed Transactions"() {
given:
waitForBalance(sut, wallet1, 205000)
def address1 = sut2.createAddress()
def address2 = sut2.createAddress()
when:
def dTxHash = sut.createDelayedTransaction(
[
Transfer.of(100000, address1),
Transfer.of(100000, address2)
],
5000,
2,
wallet1,
[wallet1],
null,
0, //no confirmation at all, just next block has to be mined
null
)
then:
dTxHash != null
when:
def dTxHashes = sut.getDelayedTransactionHashes()
then:
dTxHashes.contains(dTxHash)
when:
def success = sut.deleteDelayedTransaction(dTxHash)
then:
success
when:
def dTxHash2 = sut.createDelayedTransaction(
[
Transfer.of(100000, address1),
Transfer.of(100000, address2)
],
5000,
2,
wallet1,
null,
0, //no confirmation at all, just next block has to be mined
null
)
then:
dTxHash != dTxHash2
!sut.getUnconfirmedTransactionHashes().contains(dTxHash2)
when:
success = sut.sendDelayedTransaction(dTxHash2)
then:
success
sut.getUnconfirmedTransactionHashes().contains(dTxHash2)
}
@Ignore("weird on travis, runs fine locally")
@RetryOnFailure(delaySeconds = 10, times = 9)
def "Fusion.... HA!"() { //hehehe ;)
given:
waitForBalance(sut, wallet1, 505000)
def address1 = sut2.createAddress()
def address2 = sut2.createAddress()
def address3 = sut2.createAddress()
def address4 = sut2.createAddress()
def address5 = sut2.createAddress()
def addresses = [address1, address2, address3, address4, address5]
String transactionHash = sut.sendTransaction(
[
Transfer.of(100000, address1),
Transfer.of(100000, address2),
Transfer.of(100000, address3),
Transfer.of(100000, address4),
Transfer.of(100000, address5)
],
5000,
2,
wallet1,
[wallet1],
null,
0, //no confirmation at all, just next block has to be mined
null
)
Transaction tx = sut.getTransaction(transactionHash)
tx.amount == -505000
waitForTransactionConfirmation(sut, transactionHash)
waitForTransactionConfirmation(sut2, transactionHash)
addresses.each {
waitForBalance(sut2, it, 10000)
}
when:
waitForBlockHeightIncrement(sut2, 1)
while (sut2.estimateFusion(5000000).fusionReadyCount < addresses.size()) {
log.info("Waiting for outputs to get ready to be fused...")
sleep(1000)
}
and:
def fTxHash = sut2.sendFusionTransaction(5000000, 2, wallet2)
waitForTransactionConfirmation(sut2, fTxHash)
then:
addresses.each {
sut2.getBalance(it).availableBalance == 0
}
}
private static String generatePaymentId() {
def r = new Random()
def result = (0..<64).collect { r.nextInt(16) }
.collect { Integer.toString(it, 16) }
.join()
return result
}
private static void waitForBalance(IridiumAPI api, String address, long threshold) {
while (api.getBalance(address).availableBalance < threshold) {
log.info("Waiting for valid balance >=$threshold on wallet $address...")
sleep(5000)
}
}
private static void waitForTransactionConfirmation(IridiumAPI api, String transactionHash) {
while (api.getUnconfirmedTransactionHashes().contains(transactionHash)) {
log.info("Waiting for tx $transactionHash to get confirmed...")
sleep(5000)
}
}
private static void waitForBlockHeightIncrement(IridiumAPI api, int increment) {
def desiredHeight = api.getStatus().knownBlockCount + increment
def currentHeight
while ((currentHeight = api.getStatus().knownBlockCount) && currentHeight < desiredHeight) {
log.info("Waiting for blockHeight>$desiredHeight, current blockHeight=$currentHeight...")
sleep(5000)
}
}
private static void waitForBlockHeightTotal(IridiumAPI api, int desiredHeight) {
def currentHeight
while ((currentHeight = api.getStatus().knownBlockCount) && currentHeight < desiredHeight) {
log.info("Waiting for blockHeight>$desiredHeight, current blockHeight=$currentHeight...")
sleep(5000)
}
}
}