-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet_test.go
2252 lines (2251 loc) · 73.7 KB
/
wallet_test.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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package btcregtest
//
//import (
// "encoding/hex"
// "github.com/btcsuite/btcd/chaincfg"
// "github.com/btcsuite/btcd/chaincfg/chainhash"
// "github.com/btcsuite/btcd/btcjson"
// "github.com/btcsuite/btcd/rpcclient"
// "github.com/btcsuite/btcd/txscript"
// "github.com/btcsuite/btcwallet/wallet"
// "github.com/google/go-cmp/cmp"
// "math"
// "math/big"
// "reflect"
// "strconv"
// "strings"
// "testing"
// "time"
//)
//
//const defaultWalletPassphrase = "password"
//
//func TestGetNewAddress(t *testing.T) {
//
// r := ObtainWalletHarness(mainWalletHarnessName)
// // Wallet RPC client
// wcl := r.Wallet
//
// err := wcl.WalletUnlock(defaultWalletPassphrase, 0)
// if err != nil {
// t.Fatal("Failed to unlock wallet:", err)
// }
//
// // Get a new address from "default" account
// // This is the first GetNewAddress call
// // in this test for the "default" account
// addr, err := wcl.GetNewAddress("default")
// if err != nil {
// t.Fatal(err)
// }
//
// // Verify that address is for current network
// if !addr.IsForNet(r.Node.Network()) {
// t.Fatalf("Address not for active network (%s)", r.Node.Network())
// }
//
// // ValidateAddress
// validRes, err := wcl.ValidateAddress(addr)
// if err != nil {
// t.Fatalf("Unable to validate address %s: %v", addr, err)
// }
// if !validRes.IsValid {
// t.Fatalf("Address not valid: %s", addr)
// }
//
// // Create new account
// accountName := "newAddressTest"
// err = wcl.CreateNewAccount(accountName)
// if err != nil {
// t.Fatal(err)
// }
//
// // Get a new address from new "newAddressTest" account
// addrA, err := r.WalletRPCClient().GetNewAddress(accountName)
// if err != nil {
// t.Fatal(err)
// }
//
// // Verify that address is for current network
// if !addrA.IsForNet(r.Node.Network()) {
// t.Fatalf("Address not for active network (%s)", r.Node.Network())
// }
//
// validRes, err = wcl.ValidateAddress(addrA)
// if err != nil {
// t.Fatalf("Unable to validate address %s: %v", addrA, err)
// }
// if !validRes.IsValid {
// t.Fatalf("Address not valid: %s", addr)
// }
//
// // respect DefaultGapLimit
// // -1 because of the first GetNewAddress("default") call above
// for i := 0; i < wallet.DefaultGapLimit-1; i++ {
// addr, err = wcl.GetNewAddress("default")
// if err != nil {
// t.Fatal(err)
// }
//
// validRes, err = wcl.ValidateAddress(addr)
// if err != nil {
// t.Fatalf(
// "Unable to validate address %s: %v",
// addr,
// err,
// )
// }
// if !validRes.IsValid {
// t.Fatalf("Address not valid: %s", addr)
// }
// }
//
// // Expecting error:
// // "policy violation: generating next address violates
// // the unused address gap limit policy"
// addr, err = wcl.GetNewAddress("default")
// if err == nil {
// t.Fatalf(
// "Should report gap policy violation (%d)",
// wallet.DefaultGapLimit,
// )
// }
//
// // gap policy with wrapping
// // reuse each address numOfReusages times
// numOfReusages := 3
// addrCounter := make(map[string]int)
// for i := 0; i < wallet.DefaultGapLimit*numOfReusages; i++ {
// addr, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetNewAddressGapPolicy(
// "default", rpcclient.GapPolicyWrap)
//
// // count address
// num := addrCounter[addr.String()]
// num++
// addrCounter[addr.String()] = num
//
// if err != nil {
// t.Fatal(err)
// }
//
// validRes, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ValidateAddress(addr)
// if err != nil {
// t.Fatalf(
// "Unable to validate address %s: %v",
// addr,
// err,
// )
// }
// if !validRes.IsValid {
// t.Fatalf("Address not valid: %s", addr)
// }
// }
//
// // check reusages
// for _, reused := range addrCounter {
// if reused != numOfReusages {
// t.Fatalf(
// "Each address is expected to be reused: %d times, actual %d",
// numOfReusages,
// reused,
// )
// }
// }
//
// // ignore gap policy
// for i := 0; i < wallet.DefaultGapLimit*2; i++ {
// addr, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetNewAddressGapPolicy(
// "default", rpcclient.GapPolicyIgnore)
// if err != nil {
// t.Fatal(err)
// }
//
// validRes, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ValidateAddress(addr)
// if err != nil {
// t.Fatalf(
// "Unable to validate address %s: %v",
// addr,
// err,
// )
// }
// if !validRes.IsValid {
// t.Fatalf("Address not valid: %s", addr)
// }
// }
//
//}
//
//func TestValidateAddress(t *testing.T) {
//
// r := ObtainWalletHarness(mainWalletHarnessName)
// // Wallet RPC client
// wcl := r.Wallet
//
// // Check that wallet is now unlocked
// walletInfo, err := wcl.WalletInfo()
// if err != nil {
// t.Fatal("walletinfo failed.")
// }
// if !walletInfo.Unlocked {
// t.Fatal("WalletPassphrase failed to unlock the wallet with the correct passphrase")
// }
//
// //-----------------------------------------
// newAccountName := "testValidateAddress"
// // Create a non-default account
// err = wcl.CreateNewAccount(newAccountName)
// if err != nil {
// t.Fatalf("Unable to create account %s: %v", newAccountName, err)
// }
// accounts := []string{"default", newAccountName}
// //-----------------------------------------
// addrStr := "SsqvxBX8MZC5iiKCgBscwt69jg4u4hHhDKU"
// // Try to validate an address that is not owned by wallet
// otherAddress, err := btcutil.DecodeAddress(addrStr)
// if err != nil {
// t.Fatalf("Unable to decode address %v: %v", otherAddress, err)
// }
// validRes, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ValidateAddress(otherAddress)
// if err != nil {
// t.Fatalf("Unable to validate address %s with secondary wallet: %v",
// addrStr, err)
// }
// if !validRes.IsValid {
// t.Fatalf("Address not valid: %s", addrStr)
// }
// if validRes.IsMine {
// t.Fatalf("Address incorrectly identified as mine: %s", addrStr)
// }
// if validRes.IsScript {
// t.Fatalf("Address incorrectly identified as script: %s", addrStr)
// }
// //-----------------------------------------
// // Validate simnet dev subsidy address
// devSubPkScript := chaincfg.SimNetParams.OrganizationPkScript // "ScuQxvveKGfpG1ypt6u27F99Anf7EW3cqhq"
// devSubPkScrVer := chaincfg.SimNetParams.OrganizationPkScriptVersion
// _, addrs, _, err := txscript.ExtractPkScriptAddrs(
// devSubPkScrVer, devSubPkScript, r.Node.Network().Params().(*chaincfg.Params))
// if err != nil {
// t.Fatal("Failed to extract addresses from PkScript:", err)
// }
// devSubAddrStr := addrs[0].String()
//
// DevAddr, err := btcutil.DecodeAddress(devSubAddrStr)
// if err != nil {
// t.Fatalf("Unable to decode address %s: %v", devSubAddrStr, err)
// }
//
// validRes, err = r.WalletRPCClient().Internal().(*rpcclient.Client).ValidateAddress(DevAddr)
// if err != nil {
// t.Fatalf("Unable to validate address %s: ", devSubAddrStr)
// }
// if !validRes.IsValid {
// t.Fatalf("Address not valid: %s", devSubAddrStr)
// }
// if validRes.IsMine {
// t.Fatalf("Address incorrectly identified as mine: %s", devSubAddrStr)
// }
// // final address overflow check for each account
// for _, acct := range accounts {
// // let's overflow DefaultGapLimit
// for i := 0; i < wallet.DefaultGapLimit+5; i++ {
// // Get a new address from current account
// addr, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetNewAddressGapPolicy(
// acct, rpcclient.GapPolicyIgnore)
// if err != nil {
// t.Fatal(err)
// }
// // Verify that address is for current network
// if !addr.IsForNet(r.Node.Network().Params().(*chaincfg.Params)) {
// t.Fatalf(
// "Address[%d] not for active network (%s), <%s>",
// i,
// r.Node.Network(),
// acct,
// )
// }
// // ValidateAddress
// addrStr := addr.String()
// validRes, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ValidateAddress(addr)
// if err != nil {
// t.Fatalf(
// "Unable to validate address[%d] %s: %v for <%s>",
// i,
// addrStr,
// err,
// acct,
// )
// }
// if !validRes.IsValid {
// t.Fatalf(
// "Address[%d] not valid: %s for <%s>",
// i,
// addrStr,
// acct,
// )
// }
// if !validRes.IsMine {
// t.Fatalf(
// "Address[%d] incorrectly identified as NOT mine: %s for <%s>",
// i,
// addrStr,
// acct,
// )
// }
// if validRes.IsScript {
// t.Fatalf(
// "Address[%d] incorrectly identified as script: %s for <%s>",
// i,
// addrStr,
// acct,
// )
// }
// // Address is "mine", so we can check account
// if strings.Compare(acct, validRes.Account) != 0 {
// t.Fatalf("Address[%d] %s reported as not from <%s> account",
// i,
// addrStr,
// acct,
// )
// }
// // Decode address
// _, err = btcutil.DecodeAddress(addrStr)
// if err != nil {
// t.Fatalf("Unable to decode address[%d] %s: %v for <%s>",
// i,
// addr.String(),
// err,
// acct,
// )
// }
// }
//
// }
//
//}
//
//func TestGetBalance(t *testing.T) {
//
// r := ObtainWalletHarness(mainWalletHarnessName)
// // Wallet RPC client
// wcl := r.Wallet
//
// accountName := "getBalanceTest"
// err := wcl.CreateNewAccount(accountName)
// if err != nil {
// t.Fatalf("CreateNewAccount failed: %v", err)
// }
//
// // Grab a fresh address from the test account
// addr, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetNewAddressGapPolicy(
// accountName,
// rpcclient.GapPolicyWrap,
// )
// if err != nil {
// t.Fatalf("GetNewAddress failed: %v", err)
// }
//
// // Check invalid account name
// _, err = r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf("invalid account", 0)
// // -4: account name 'invalid account' not found
// if err == nil {
// t.Fatalf("GetBalanceMinConfType failed to return non-nil error for invalid account name: %v", err)
// }
//
// // Check invalid minconf
// _, err = r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf("default", -1)
// if err == nil {
// t.Fatalf("GetBalanceMinConf failed to return non-nil error for invalid minconf (-1)")
// }
//
// preBalances, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf("*", 0)
// if err != nil {
// t.Fatalf("GetBalanceMinConf(\"*\", 0) failed: %v", err)
// }
//
// preAccountBalanceSpendable := 0.0
// preAccountBalances := make(map[string]btcjson.GetAccountBalanceResult)
// for _, bal := range preBalances.Balances {
// preAccountBalanceSpendable += bal.Spendable
// preAccountBalances[bal.AccountName] = bal
// }
//
// // Send from default to test account
// sendAmount := btcutil.Amount(700000000)
// if _, err = r.WalletRPCClient().Internal().(*rpcclient.Client).SendFromMinConf("default", addr, sendAmount, 1); err != nil {
// t.Fatalf("SendFromMinConf failed: %v", err)
// }
//
// // Check invalid minconf
// postBalances, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf("*", 0)
// if err != nil {
// t.Fatalf("GetBalanceMinConf failed: %v", err)
// }
//
// postAccountBalanceSpendable := 0.0
// postAccountBalances := make(map[string]btcjson.GetAccountBalanceResult)
// for _, bal := range postBalances.Balances {
// postAccountBalanceSpendable += bal.Spendable
// postAccountBalances[bal.AccountName] = bal
// }
//
// // Fees prevent easy exact comparison
// if preAccountBalances["default"].Spendable <= postAccountBalances["default"].Spendable {
// t.Fatalf("spendable balance of account 'default' not decreased: %v <= %v",
// preAccountBalances["default"].Spendable,
// postAccountBalances["default"].Spendable)
// }
//
// if sendAmount.ToCoin() != (postAccountBalances[accountName].Spendable - preAccountBalances[accountName].Spendable) {
// t.Fatalf("spendable balance of account '%s' not increased: %v >= %v",
// accountName,
// preAccountBalances[accountName].Spendable,
// postAccountBalances[accountName].Spendable)
// }
//
// // Make sure "*" account balance has decreased (fees)
// if postAccountBalanceSpendable >= preAccountBalanceSpendable {
// t.Fatalf("Total balance over all accounts not decreased after send.")
// }
//
// // Test vanilla GetBalance()
// amtGetBalance, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalance("default")
// if err != nil {
// t.Fatalf("GetBalance failed: %v", err)
// }
//
// // For GetBalance(), default minconf=1.
// defaultBalanceMinConf1, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf("default", 1)
// if err != nil {
// t.Fatalf("GetBalanceMinConfType failed: %v", err)
// }
//
// if amtGetBalance.Balances[0].Spendable != defaultBalanceMinConf1.Balances[0].Spendable {
// t.Fatalf(`Balance from GetBalance("default") does not equal amount `+
// `from GetBalanceMinConf: %v != %v`,
// amtGetBalance.Balances[0].Spendable,
// defaultBalanceMinConf1.Balances[0].Spendable)
// }
//
// // Verify minconf=1 balances of receiving account before/after new block
// // Before, getbalance minconf=1
// amtTestMinconf1BeforeBlock, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf(accountName, 1)
// if err != nil {
// t.Fatalf("GetBalanceMinConf failed: %v", err)
// }
//
// // Mine 2 new blocks to validate tx
// newBestBlock(r, t)
// newBestBlock(r, t)
//
// // After, getbalance minconf=1
// amtTestMinconf1AfterBlock, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf(accountName, 1)
// if err != nil {
// t.Fatalf("GetBalanceMinConf failed: %v", err)
// }
//
// // Verify that balance (minconf=1) has increased
// if sendAmount.ToCoin() != (amtTestMinconf1AfterBlock.Balances[0].Spendable - amtTestMinconf1BeforeBlock.Balances[0].Spendable) {
// t.Fatalf(`Balance (minconf=1) not increased after new block: %v - %v != %v`,
// amtTestMinconf1AfterBlock.Balances[0].Spendable,
// amtTestMinconf1BeforeBlock.Balances[0].Spendable,
// sendAmount)
// }
//}
//
//func TestListAccounts(t *testing.T) {
//
// r := ObtainWalletHarness(mainWalletHarnessName)
// // Wallet RPC client
// wcl := r.Wallet
//
// // Create a new account and verify that we can see it
// listBeforeCreateAccount, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListAccounts()
// if err != nil {
// t.Fatal("Failed to create new account ", err)
// }
//
// // New account
// accountName := "listaccountsTestAcct"
// err = wcl.CreateNewAccount(accountName)
// if err != nil {
// t.Fatal(err)
// }
//
// // Account list after creating new
// accountsBalancesDefault1, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListAccounts()
// if err != nil {
// t.Fatal(err)
// }
//
// // Verify that new account is in the list, with zero balance
// foundNewAcct := false
// for acct, amt := range accountsBalancesDefault1 {
// if _, ok := listBeforeCreateAccount[acct]; !ok {
// // Found new account. Now check name and balance
// if amt != 0 {
// t.Fatalf("New account (%v) found with non-zero balance: %v",
// acct, amt)
// }
// if accountName == acct {
// foundNewAcct = true
// break
// }
// t.Fatalf("Found new account, %v; Expected %v", acct, accountName)
// }
// }
// if !foundNewAcct {
// t.Fatalf("Failed to find newly created account, %v.", accountName)
// }
//
// // Grab a fresh address from the test account
// addr, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetNewAddressGapPolicy(
// accountName,
// rpcclient.GapPolicyWrap,
// )
// if err != nil {
// t.Fatal(err)
// }
//
// // For ListAccountsCmd: MinConf *int `jsonrpcdefault:"1"`
// // Let's test that ListAccounts() is equivalent to explicit minconf=1
// accountsBalancesMinconf1, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListAccountsMinConf(1)
// if err != nil {
// t.Fatal(err)
// }
//
// if !reflect.DeepEqual(accountsBalancesDefault1, accountsBalancesMinconf1) {
// t.Fatal("ListAccounts() returned different result from ListAccountsMinConf(1): ",
// accountsBalancesDefault1, accountsBalancesMinconf1)
// }
//
// // Get accounts with minconf=0 pre-send
// accountsBalancesMinconf0PreSend, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListAccountsMinConf(0)
// if err != nil {
// t.Fatal(err)
// }
//
// // Get balance of test account prior to a send
// acctBalancePreSend := accountsBalancesMinconf0PreSend[accountName]
//
// // Send from default to test account
// sendAmount := btcutil.Amount(700000000)
// if _, err = r.WalletRPCClient().Internal().(*rpcclient.Client).SendFromMinConf("default", addr, sendAmount, 1); err != nil {
// t.Fatal("SendFromMinConf failed.", err)
// }
//
// // Get accounts with minconf=0 post-send
// accountsBalancesMinconf0PostSend, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListAccountsMinConf(0)
// if err != nil {
// t.Fatal(err)
// }
//
// // Get balance of test account prior to a send
// acctBalancePostSend := accountsBalancesMinconf0PostSend[accountName]
//
// // Check if reported balances match expectations
// if sendAmount != (acctBalancePostSend - acctBalancePreSend) {
// t.Fatalf("Test account balance not changed by expected amount after send: "+
// "%v -%v != %v", acctBalancePostSend, acctBalancePreSend, sendAmount)
// }
//
// // Verify minconf>0 works: list, mine, list
//
// // List BEFORE mining a block
// accountsBalancesMinconf1PostSend, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListAccountsMinConf(1)
// if err != nil {
// t.Fatal(err)
// }
//
// // Get balance of test account prior to a send
// acctBalanceMin1PostSend := accountsBalancesMinconf1PostSend[accountName]
//
// // Mine 2 new blocks to validate tx
// newBestBlock(r, t)
// newBestBlock(r, t)
//
// // List AFTER mining a block
// accountsBalancesMinconf1PostMine, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListAccountsMinConf(1)
// if err != nil {
// t.Fatal(err)
// }
//
// // Get balance of test account prior to a send
// acctBalanceMin1PostMine := accountsBalancesMinconf1PostMine[accountName]
//
// // Check if reported balances match expectations
// if sendAmount != (acctBalanceMin1PostMine - acctBalanceMin1PostSend) {
// t.Fatalf("Test account balance (minconf=1) not changed by expected "+
// "amount after new block: %v - %v != %v", acctBalanceMin1PostMine,
// acctBalanceMin1PostSend, sendAmount)
// }
//
// // Note that ListAccounts uses Store.balanceFullScan to handle a UTXO scan
// // for each specific account. We can compare against GetBalanceMinConfType.
// // Also, I think there is the same bug that allows negative minconf values,
// // but does not handle unconfirmed outputs the same way as minconf=0.
//
// GetBalancePostSend, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf(accountName, 0)
// if err != nil {
// t.Fatal(err)
// }
// // Note that BFBalanceSpendable is used with GetBalanceMinConf (not Type),
// // which uses BFBalanceFullScan when a single account is specified.
// // Recall thet fullscan is used by listaccounts.
//
// if GetBalancePostSend.Balances[0].Spendable != acctBalancePostSend.ToCoin() {
// t.Fatalf("Balance for default account from GetBalanceMinConf does not "+
// "match balance from ListAccounts: %v != %v", GetBalancePostSend,
// acctBalancePostSend)
// }
//
// // Mine 2 blocks to validate the tx and clean up UTXO set
// newBestBlock(r, t)
// newBestBlock(r, t)
//}
//
//func TestListUnspent(t *testing.T) {
//
// r := ObtainWalletHarness(mainWalletHarnessName)
// // Wallet RPC client
// wcl := r.Wallet
//
// // New account
// accountName := "listUnspentTestAcct"
// err := wcl.CreateNewAccount(accountName)
// if err != nil {
// t.Fatal(err)
// }
//
// // Grab an address from the test account
// addr, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetNewAddressGapPolicy(
// accountName,
// rpcclient.GapPolicyWrap,
// )
// if err != nil {
// t.Fatal(err)
// }
//
// // UTXOs before send
// list, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListUnspent()
// if err != nil {
// t.Fatalf("failed to get utxos")
// }
// utxosBeforeSend := make(map[string]float64)
// for _, utxo := range list {
// // Get a OutPoint string in the form of hash:index
// outpointStr, err := getOutPointString(&utxo)
// if err != nil {
// t.Fatal(err)
// }
// // if utxo.Spendable ...
// utxosBeforeSend[outpointStr] = utxo.Amount
// }
//
// // Check Min/Maxconf arguments
// defaultMaxConf := 9999999
//
// listMin1MaxBig, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListUnspentMinMax(1, defaultMaxConf)
// if err != nil {
// t.Fatalf("failed to get utxos")
// }
// if !reflect.DeepEqual(list, listMin1MaxBig) {
// t.Fatal("Outputs from ListUnspent() and ListUnspentMinMax() do not match.")
// }
//
// // Grab an address from known unspents to test the filter
// refOut := list[0]
// PkScript, err := hex.DecodeString(refOut.ScriptPubKey)
// if err != nil {
// t.Fatalf("Failed to decode ScriptPubKey into PkScript.")
// }
// // The Address field is broken, including only one address, so don't use it
// _, addrs, _, err := txscript.ExtractPkScriptAddrs(
// txscript.DefaultScriptVersion, PkScript, r.Node.Network().Params().(*chaincfg.Params))
// if err != nil {
// t.Fatal("Failed to extract addresses from PkScript:", err)
// }
//
// // List with all of the above address
// listAddressesKnown, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListUnspentMinMaxAddresses(1, defaultMaxConf, addrs)
// if err != nil {
// t.Fatalf("Failed to get utxos with addresses argument.")
// }
//
// // Check that there is at least one output for the input addresses
// if len(listAddressesKnown) == 0 {
// t.Fatalf("Failed to find expected UTXOs with addresses.")
// }
//
// // Make sure each found output's txid:vout is in original list
// var foundTxID = false
// for _, listRes := range listAddressesKnown {
// // Get a OutPoint string in the form of hash:index
// outpointStr, err := getOutPointString(&listRes)
// if err != nil {
// t.Fatal(err)
// }
// if _, ok := utxosBeforeSend[outpointStr]; !ok {
// t.Fatalf("Failed to find TxID")
// }
// // Also verify that the txid of the reference output is in the list
// if listRes.TxID == refOut.TxID {
// foundTxID = true
// }
// }
// if !foundTxID {
// t.Fatal("Original TxID not found in list by addresses.")
// }
//
// // SendFromMinConf to addr
// amountToSend := btcutil.Amount(700000000)
// txid, err := r.WalletRPCClient().Internal().(*rpcclient.Client).SendFromMinConf("default", addr, amountToSend, 0)
// if err != nil {
// t.Fatalf("sendfromminconf failed: %v", err)
// }
//
// newBestBlock(r, t)
// time.Sleep(1 * time.Second)
// // New block is necessary for GetRawTransaction to give a tx with sensible
// // MsgTx().TxIn[:].ValueIn values.
//
// // Get *btcutil.Tx of send to check the inputs
// rawTx, err := r.NodeRPCClient().Internal().(*rpcclient.Client).GetRawTransaction(txid)
// if err != nil {
// t.Fatalf("getrawtransaction failed: %v", err)
// }
//
// // Get previous OutPoint of each TxIn for send transaction
// txInIDs := make(map[string]float64)
// for _, txIn := range rawTx.MsgTx().TxIn {
// prevOut := &txIn.PreviousOutPoint
// // Outpoint.String() appends :index to the hash
// txInIDs[prevOut.String()] = btcutil.Amount(txIn.ValueIn).ToCoin()
// }
//
// // First check to make sure we see these in the UTXO list prior to send,
// // then not in the UTXO list after send.
// for txinID, amt := range txInIDs {
// if _, ok := utxosBeforeSend[txinID]; !ok {
// t.Fatalf("Failed to find txid %v (%v BTC) in list of UTXOs",
// txinID, amt)
// }
// }
//
// // Validate the send Tx with 2 new blocks
// newBestBlock(r, t)
// newBestBlock(r, t)
//
// // Make sure these txInIDS are not in the new UTXO set
// time.Sleep(2 * time.Second)
// list, err = r.WalletRPCClient().Internal().(*rpcclient.Client).ListUnspent()
// if err != nil {
// t.Fatalf("Failed to get UTXOs")
// }
// for _, utxo := range list {
// // Get a OutPoint string in the form of hash:index
// outpointStr, err := getOutPointString(&utxo)
// if err != nil {
// t.Fatal(err)
// }
// if amt, ok := txInIDs[outpointStr]; ok {
// t.Fatalf("Found PreviousOutPoint of send still in UTXO set: %v, "+
// "%v BTC", outpointStr, amt)
// }
// }
//}
//
//func TestSendToAddress(t *testing.T) {
//
// r := ObtainWalletHarness(mainWalletHarnessName)
//
// // Grab a fresh address from the wallet.
// addr, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetNewAddressGapPolicy(
// "default", rpcclient.GapPolicyIgnore)
// if err != nil {
// t.Fatal(err)
// }
//
// // Check balance of default account
// _, err = r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf("default", 1)
// if err != nil {
// t.Fatalf("GetBalanceMinConfType failed: %v", err)
// }
//
// // SendToAddress
// txid, err := r.WalletRPCClient().Internal().(*rpcclient.Client).SendToAddress(addr, 1000000)
// if err != nil {
// t.Fatalf("SendToAddress failed: %v", err)
// }
//
// // Generate a single block, in which the transaction the wallet created
// // should be found.
// _, block, _ := newBestBlock(r, t)
//
// if len(block.Transactions()) <= 1 {
// t.Fatalf("expected transaction not included in block")
// }
// // Confirm that the expected tx was mined into the block.
// minedTx := block.Transactions()[1]
// txHash := minedTx.Hash()
// if *txid != *txHash {
// t.Fatalf("txid's don't match, %v vs %v", txHash, txid)
// }
//
// // We should now check to confirm that the utxo that wallet used to create
// // that sendfrom was properly marked as spent and removed from utxo set. Use
// // GetTxOut to tell if the outpoint is spent.
// //
// // The spending transaction has to be off the tip block for the previous
// // outpoint to be spent, out of the UTXO set. Generate another block.
// _, err = GenerateBlock(r, block.MsgBlock().Header.Height)
// if err != nil {
// t.Fatal(err)
// }
//
// // Check each PreviousOutPoint for the sending tx.
// time.Sleep(1 * time.Second)
// // Get the sending Tx
// rawTx, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetRawTransaction(txid)
// if err != nil {
// t.Fatalf("Unable to get raw transaction %v: %v", txid, err)
// }
// // txid is rawTx.MsgTx().TxIn[0].PreviousOutPoint.Hash
//
// // Check all inputs
// for i, txIn := range rawTx.MsgTx().TxIn {
// prevOut := &txIn.PreviousOutPoint
//
// // If a txout is spent (not in the UTXO set) GetTxOutResult will be nil
// res, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetTxOut(&prevOut.Hash, prevOut.Index, false)
// if err != nil {
// t.Fatal("GetTxOut failure:", err)
// }
// if res != nil {
// t.Fatalf("Transaction output %v still unspent.", i)
// }
// }
//}
//
//func TestSendFrom(t *testing.T) {
// r := ObtainWalletHarness(mainWalletHarnessName)
//
// err := r.Wallet.WalletUnlock(defaultWalletPassphrase, 0)
// if err != nil {
// t.Fatal("Failed to unlock wallet:", err)
// }
//
// accountName := "sendFromTest"
// err = r.WalletRPCClient().Internal().(*rpcclient.Client).CreateNewAccount(accountName)
// if err != nil {
// t.Fatal(err)
// }
//
// // Grab a fresh address from the wallet.
// addr, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetNewAddressGapPolicy(
// accountName,
// rpcclient.GapPolicyWrap,
// )
// if err != nil {
// t.Fatal(err)
// }
//
// amountToSend := btcutil.Amount(1000000)
// // Check spendable balance of default account
// defaultBalanceBeforeSend, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf("default", 0)
// if err != nil {
// t.Fatalf("GetBalanceMinConf failed: %v", err)
// }
//
// // Get utxo list before send
// list, err := r.WalletRPCClient().Internal().(*rpcclient.Client).ListUnspent()
// if err != nil {
// t.Fatalf("failed to get utxos")
// }
// utxosBeforeSend := make(map[string]float64)
// for _, utxo := range list {
// // Get a OutPoint string in the form of hash:index
// outpointStr, err := getOutPointString(&utxo)
// if err != nil {
// t.Fatal(err)
// }
// // if utxo.Spendable ...
// utxosBeforeSend[outpointStr] = utxo.Amount
// }
//
// // SendFromMinConf 1000 to addr
// txid, err := r.WalletRPCClient().Internal().(*rpcclient.Client).SendFromMinConf("default", addr, amountToSend, 0)
// if err != nil {
// t.Fatalf("sendfromminconf failed: %v", err)
// }
//
// // Check spendable balance of default account
// defaultBalanceAfterSendNoBlock, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf("default", 0)
// if err != nil {
// t.Fatalf("GetBalanceMinConf failed: %v", err)
// }
//
// // Check balance of sendfrom account
// sendFromBalanceAfterSendNoBlock, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf(accountName, 0)
// if err != nil {
// t.Fatalf("GetBalanceMinConf failed: %v", err)
// }
// if sendFromBalanceAfterSendNoBlock.Balances[0].Spendable != amountToSend.ToCoin() {
// t.Fatalf("balance for %s account incorrect: want %v got %v",
// accountName, amountToSend, sendFromBalanceAfterSendNoBlock.Balances[0].Spendable)
// }
//
// // Generate a single block, the transaction the wallet created should
// // be found in this block.
// _, block, _ := newBestBlock(r, t)
//
// // Check to make sure the transaction that was sent was included in the block
// if len(block.Transactions()) <= 1 {
// t.Fatalf("expected transaction not included in block")
// }
// minedTx := block.Transactions()[1]
// txHash := minedTx.Hash()
// if *txid != *txHash {
// t.Fatalf("txid's don't match, %v vs. %v (actual vs. expected)",
// txHash, txid)
// }
//
// // Generate another block, since it takes 2 blocks to validate a tx
// newBestBlock(r, t)
//
// // Get rawTx of sent txid so we can calculate the fee that was used
// time.Sleep(1 * time.Second)
// rawTx, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetRawTransaction(txid)
// if err != nil {
// t.Fatalf("getrawtransaction failed: %v", err)
// }
//
// var totalSpent int64
// for _, txIn := range rawTx.MsgTx().TxIn {
// totalSpent += txIn.ValueIn
// }
//
// var totalSent int64
// for _, txOut := range rawTx.MsgTx().TxOut {
// totalSent += txOut.Value
// }
//
// feeAtoms := btcutil.Amount(totalSpent - totalSent)
//
// // Calculate the expected balance for the default account after the tx was sent
// sentAtoms := uint64(amountToSend + feeAtoms)
//
// m1 := new(big.Float)
// m1.SetFloat64(-1)
//
// E8 := new(big.Float)
// E8.SetFloat64(math.Pow10(int(8)))
//
// sentAtomsNegative := new(big.Float)
// sentAtomsNegative.SetUint64(sentAtoms)
// sentAtomsNegative = sentAtomsNegative.Mul(sentAtomsNegative, m1)
//
// oldBalanceCoins := new(big.Float)
// oldBalanceCoins.SetFloat64(defaultBalanceBeforeSend.Balances[0].Spendable)
// oldBalanceAtoms := new(big.Float)
// oldBalanceAtoms = oldBalanceAtoms.Mul(oldBalanceCoins, E8)
//
// expectedBalanceAtoms := new(big.Float)
// expectedBalanceAtoms.Add(oldBalanceAtoms, sentAtomsNegative)
//
// currentBalanceCoinsNegative := new(big.Float)
// currentBalanceCoinsNegative.SetFloat64(defaultBalanceAfterSendNoBlock.Balances[0].Spendable)
// currentBalanceCoinsNegative = currentBalanceCoinsNegative.Mul(currentBalanceCoinsNegative, m1)
//
// currentBalanceAtomsNegative := new(big.Float)
// currentBalanceAtomsNegative = currentBalanceAtomsNegative.Mul(currentBalanceCoinsNegative, E8)
//
// diff := new(big.Float)
// diff.Add(currentBalanceAtomsNegative, expectedBalanceAtoms)
//
// zero := new(big.Float)
// zero.SetFloat64(0)
//
// if diff.Cmp(zero) != 0 {
// t.Fatalf("balance for %s account incorrect: want %v got %v, diff %V",
// "default",
// currentBalanceAtomsNegative,
// defaultBalanceAfterSendNoBlock.Balances[0].Spendable,
// diff,
// )
// }
//
// // Check balance of sendfrom account
// sendFromBalanceAfterSend1Block, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetBalanceMinConf(accountName, 1)
// if err != nil {
// t.Fatalf("getbalanceminconftype failed: %v", err)
// }
//
// if sendFromBalanceAfterSend1Block.Balances[0].Total != amountToSend.ToCoin() {
// t.Fatalf("balance for %s account incorrect: want %v got %v",
// accountName, amountToSend, sendFromBalanceAfterSend1Block.Balances[0].Total)
// }
//
// // We have confirmed that the expected tx was mined into the block.
// // We should now check to confirm that the utxo that wallet used to create
// // that sendfrom was properly marked to spent and removed from utxo set.
//
// // Get the sending Tx
// rawTx, err = r.WalletRPCClient().Internal().(*rpcclient.Client).GetRawTransaction(txid)
// if err != nil {
// t.Fatalf("Unable to get raw transaction %v: %v", txid, err)
// }
//
// // Check all inputs