forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtc_test.go
More file actions
162 lines (139 loc) · 3.29 KB
/
btc_test.go
File metadata and controls
162 lines (139 loc) · 3.29 KB
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
package main
import (
"testing"
"fmt"
"time"
"bytes"
"github.com/conformal/btcutil"
"github.com/conformal/btcwire"
"github.com/FactomProject/FactomCode/notaryapi"
)
func init() {
err := initRPCClient()
if err != nil {
fmt.Println(err.Error())
}
//defer shutdown(client)
if err := initWallet(); err != nil {
fmt.Println(err.Error())
}
}
func TestWallet(t *testing.T) {
accountMap, err := client.ListAccounts()
if err != nil {
t.Fatal(err)
}
// fmt.Println("account & balance: ", accountMap)
if accountMap[""] <= 0 {
t.Errorf("wallet account name is not empty")
}
if len(accountMap) != 1 {
t.Errorf("wallet account map len =%d", len(accountMap), ", is not empty")
}
allAddr := make([]btcutil.Address, 0, 1000)
for key, _ := range accountMap {
addresses, err := client.GetAddressesByAccount(key)
if err != nil {
t.Fatal(err)
}
allAddr = append(allAddr, addresses...)
}
if len(allAddr) != 268 {
t.Errorf("allAddr.len=%d", len(allAddr), ", NOT 268")
}
var i int
ca := make([]btcutil.Address, 1)
for _, a := range allAddr {
ca[0] = a
balance, err := client.ListUnspentMinMaxAddresses(1, 999999, ca)
if err != nil {
t.Fatal(err)
}
if len(balance) > 0 {
// fmt.Print(a, " ")
// for _, b := range balance {
// fmt.Print(b.Amount, " ")
// }
// fmt.Println()
i++
}
}
if i != 87 {
t.Errorf("num of addresses with balance=%d", i, ", NOT 87")
}
}
func TestListUnspent(t *testing.T) {
balance, err := client.ListUnspent()
if err != nil {
t.Fatal(err)
}
if len(balance) != 93 {
t.Errorf("test.listunspent.len==%d", len(balance), ", NOT 93")
}
var i int
for _, b := range balance {
if b.Amount > float64(0.1) {
//fmt.Println(b)
i++
}
}
if i != 21 {
t.Errorf("qualified unspent len==%d", i, ", NOT 21")
}
var included bool
for _, b := range balance {
if compareUnspentResult(spentResult, b) {
included = true
break
}
}
if !included {
t.Errorf("qualified unspent does NOT include the bug")
}
}
func TestUnconfirmedSpent(t *testing.T) {
b1, _ := client.ListUnspent() //minConf=1
b2, _ := client.ListUnspentMin(0) //minConf=0
for _, b := range b1 {
var i int = len(b2) + 1
for j, a := range b2 {
if compareUnspentResult(a, b) {
i = j
break
}
}
if i < len(b2)+1 {
b2 = append(b2[:i], b2[(i+1):]...)
}
}
if len(b2) != 4 {
t.Errorf("Unconfirmed unspent len=%d", len(b2), ", NOT 4")
}
var sum float64
for i:=0; i<len(b2); i++ {
sum += b2[i].Amount
}
// the same as unconfirmed balance in OnAccountBalance call back
if sum != 1.9936741999999998 {
t.Errorf("Unconfirmed unspent sum = %f", sum, ", not 1.9936742")
}
}
func TestRepeatedSpending(t *testing.T) {
for i:=0; i<100; i++ {
hash, err := writeToBTC(notaryapi.Sha([]byte{byte(i)}))
if err != nil {
t.Fatal(err)
}
fmt.Println("repeating=", i, ", hash= ", hash, "\n")
time.Sleep(30 * time.Second)
}
}
func TestToHash(t *testing.T) {
s := "e517043a9770aacc7406db5f2ae8b3d687ce9bca3c8f76bc0be1ed18aed7ad68"
txHash, err := btcwire.NewShaHashFromStr(s)
if err != nil {fmt.Println(err.Error()) }
h := toHash(txHash)
fmt.Println("txHash=", txHash.String(), ", toHash=", h.String())
fmt.Println("equal in string: ", txHash.String() == h.String())
fmt.Println("equal in bytes: ", bytes.Compare(txHash.Bytes(), h.Bytes))
}