-
Notifications
You must be signed in to change notification settings - Fork 1
/
wallethelpers.go
291 lines (291 loc) · 8.69 KB
/
wallethelpers.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
package pfcregtest
//
//import (
// "fmt"
// "github.com/picfight/pfcd/chaincfg/chainhash"
// "github.com/picfight/pfcd/dcrjson"
// "github.com/picfight/pfcd/dcrutil"
// "github.com/picfight/pfcd/rpcclient"
// "github.com/picfight/pfcd/wire"
// "github.com/decred/pfcwallet/errors"
// "github.com/jfixby/coinharness"
// "github.com/jfixby/pin"
// "math"
// "testing"
// "time"
//)
//
//func mineBlock(t *testing.T, r *coinharness.Harness) {
// _, heightBefore, err := r.NodeRPCClient().Internal().(*rpcclient.Client).GetBestBlock()
// if err != nil {
// t.Fatal("Failed to get chain height:", err)
// }
//
// err = coinharness.GenerateTestChain(1, r.NodeRPCClient())
// if err != nil {
// t.Fatal("Failed to mine block:", err)
// }
//
// _, heightAfter, err := r.NodeRPCClient().Internal().(*rpcclient.Client).GetBestBlock()
//
// if heightAfter != heightBefore+1 {
// t.Fatal("Failed to mine block:", heightAfter, heightBefore)
// }
//
// if err != nil {
// t.Fatal("Failed to GetBestBlock:", err)
// }
// count := r.Wallet.Sync(heightAfter)
// if heightAfter != count {
// t.Fatal("Failed to sync wallet to target:", heightAfter)
// }
//}
//
//func reverse(results []dcrjson.ListTransactionsResult) []dcrjson.ListTransactionsResult {
// i := 0
// j := len(results) - 1
// for i < j {
// results[i], results[j] = results[j], results[i]
// i++
// j--
// }
// return results
//}
//
////// Create a test chain with the desired number of mature coinbase outputs
////func generateTestChain(numToGenerate uint32, node *rpcclient.Client) error {
//// fmt.Printf("Generating %v blocks...\n", numToGenerate)
//// _, err := node.Generate(numToGenerate)
//// if err != nil {
//// return err
//// }
//// fmt.Println("Block generation complete.")
//// return nil
////}
////
////// Waits for wallet to sync to the target height
////func syncWalletTo(rpcClient *rpcclient.Client, desiredHeight int64) (int64, error) {
//// var count int64 = 0
//// var err error = nil
//// for count != desiredHeight {
//// Sleep(1000)
//// count, err = rpcClient.GetBlockCount()
//// if err != nil {
//// return -1, err
//// }
//// fmt.Println(" sync to: " + strconv.FormatInt(count, 10))
//// }
//// return count, nil
////}
//
//// generateListeningPorts returns 3 subsequent network ports starting from base
//func generateListeningPorts(index, base int) (int, int, int) {
// x := base + index*3 + 0
// y := base + index*3 + 1
// z := base + index*3 + 2
// return x, y, z
//}
//
//func getMiningAddr(walletClient *rpcclient.Client) dcrutil.Address {
// var miningAddr dcrutil.Address
// var err error = nil
// for i := 0; i < 100; i++ {
// miningAddr, err = walletClient.GetNewAddress("default")
// if err != nil {
// fmt.Println("err: " + err.Error())
// time.Sleep(time.Duration(math.Log(float64(i+3))) * 50 * time.Millisecond)
// continue
// }
// break
// }
// if miningAddr == nil {
// pin.ReportTestSetupMalfunction(errors.Errorf(
// "RPC not up for mining addr"))
// }
// return miningAddr
//}
//
//// GenerateBlock is a helper function to ensure that the chain has actually
//// incremented due to FORK blocks after stake voting height that may occur.
//func GenerateBlock(h *coinharness.Harness, startHeight uint32) ([]*chainhash.Hash, error) {
// blockHashes, err := h.NodeRPCClient().Internal().(*rpcclient.Client).Generate(1)
// if err != nil {
// return nil, errors.Errorf("unable to generate single block: %v", err)
// }
// blockHeader, err := h.NodeRPCClient().Internal().(*rpcclient.Client).GetBlockHeader(blockHashes[0])
// if err != nil {
// return nil, errors.Errorf("unable to get block header: %v", err)
// }
// newHeight := blockHeader.Height
// for newHeight == startHeight {
// blockHashes, err = h.NodeRPCClient().Internal().(*rpcclient.Client).Generate(1)
// if err != nil {
// return nil, errors.Errorf("unable to generate single block: %v", err)
// }
// blockHeader, err = h.NodeRPCClient().Internal().(*rpcclient.Client).GetBlockHeader(blockHashes[0])
// if err != nil {
// return nil, errors.Errorf("unable to get block header: %v", err)
// }
// newHeight = blockHeader.Height
// }
// return blockHashes, nil
//}
//
//func mustGetStakeInfo(wcl *rpcclient.Client, t *testing.T) *dcrjson.GetStakeInfoResult {
// stakeinfo, err := wcl.GetStakeInfo()
// if err != nil {
// t.Fatal("GetStakeInfo failed: ", err)
// }
// return stakeinfo
//}
//
//func mustGetStakeDiff(r *coinharness.Harness, t *testing.T) float64 {
// stakeDiffResult, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetStakeDifficulty()
// if err != nil {
// t.Fatal("GetStakeDifficulty failed:", err)
// }
//
// return stakeDiffResult.CurrentStakeDifficulty
//}
//
//func mustGetStakeDiffNext(r *coinharness.Harness, t *testing.T) float64 {
// stakeDiffResult, err := r.WalletRPCClient().Internal().(*rpcclient.Client).GetStakeDifficulty()
// if err != nil {
// t.Fatal("GetStakeDifficulty failed:", err)
// }
//
// return stakeDiffResult.NextStakeDifficulty
//}
//
//func advanceToHeight(r *coinharness.Harness, t *testing.T, height uint32) {
// curBlockHeight := getBestBlockHeight(r, t)
// initHeight := curBlockHeight
//
// if curBlockHeight >= height {
// return
// }
//
// for curBlockHeight != height {
// curBlockHeight, _, _ = newBlockAtQuick(curBlockHeight, r, t)
// time.Sleep(75 * time.Millisecond)
// }
// t.Logf("Advanced %d blocks to block height %d", curBlockHeight-initHeight,
// curBlockHeight)
//}
//
//func newBlockAt(currentHeight uint32, r *coinharness.Harness,
// t *testing.T) (uint32, *dcrutil.Block, []*chainhash.Hash) {
// height, block, blockHashes := newBlockAtQuick(currentHeight, r, t)
//
// time.Sleep(700 * time.Millisecond)
//
// return height, block, blockHashes
//}
//
//func newBlockAtQuick(currentHeight uint32, r *coinharness.Harness,
// t *testing.T) (uint32, *dcrutil.Block, []*chainhash.Hash) {
//
// blockHashes, err := GenerateBlock(r, currentHeight)
// if err != nil {
// t.Fatalf("Unable to generate single block: %v", err)
// }
//
// block, err := r.NodeRPCClient().Internal().(*rpcclient.Client).GetBlock(blockHashes[0])
// if err != nil {
// t.Fatalf("Unable to get block: %v", err)
// }
//
// return block.Header.Height, dcrutil.NewBlock(block), blockHashes
//}
//
//func getBestBlock(r *coinharness.Harness, t *testing.T) (uint32, *dcrutil.Block, *chainhash.Hash) {
// bestBlockHash, err := r.NodeRPCClient().Internal().(*rpcclient.Client).GetBestBlockHash()
// if err != nil {
// t.Fatalf("Unable to get best block hash: %v", err)
// }
// bestBlock, err := r.NodeRPCClient().Internal().(*rpcclient.Client).GetBlock(bestBlockHash)
// if err != nil {
// t.Fatalf("Unable to get block: %v", err)
// }
// curBlockHeight := bestBlock.Header.Height
//
// return curBlockHeight, dcrutil.NewBlock(bestBlock), bestBlockHash
//}
//
//func getBestBlockHeight(r *coinharness.Harness, t *testing.T) uint32 {
// _, height, err := r.NodeRPCClient().Internal().(*rpcclient.Client).GetBestBlock()
// if err != nil {
// t.Fatalf("Failed to GetBestBlock: %v", err)
// }
//
// return uint32(height)
//}
//
//func newBestBlock(r *coinharness.Harness,
// t *testing.T) (uint32, *dcrutil.Block, []*chainhash.Hash) {
// height := getBestBlockHeight(r, t)
// height, block, blockHash := newBlockAt(height, r, t)
// return height, block, blockHash
//}
//
//// includesTx checks if a block contains a transaction hash
//func includesTx(txHash *chainhash.Hash, block *dcrutil.Block) bool {
// if len(block.Transactions()) <= 1 {
// return false
// }
//
// blockTxs := block.Transactions()
//
// for _, minedTx := range blockTxs {
// minedTxHash := minedTx.Hash()
// if *txHash == *minedTxHash {
// return true
// }
// }
//
// return false
//}
//
//// includesTx checks if a block contains a transaction hash
//func includesStakeTx(txHash *chainhash.Hash, block *dcrutil.Block) bool {
// if len(block.STransactions()) <= 1 {
// return false
// }
//
// blockTxs := block.STransactions()
//
// for _, minedTx := range blockTxs {
// minedTxHash := minedTx.Hash()
// if *txHash == *minedTxHash {
// return true
// }
// }
//
// return false
//}
//
//// getWireMsgTxFee computes the effective absolute fee from a Tx as the amount
//// spent minus sent.
//func getWireMsgTxFee(tx *dcrutil.Tx) dcrutil.Amount {
// var totalSpent int64
// for _, txIn := range tx.MsgTx().TxIn {
// totalSpent += txIn.ValueIn
// }
//
// var totalSent int64
// for _, txOut := range tx.MsgTx().TxOut {
// totalSent += txOut.Value
// }
//
// return dcrutil.Amount(totalSpent - totalSent)
//}
//
//// getOutPointString uses OutPoint.String() to combine the tx hash with vout
//// index from a ListUnspentResult.
//func getOutPointString(utxo *dcrjson.ListUnspentResult) (string, error) {
// txhash, err := chainhash.NewHashFromStr(utxo.TxID)
// if err != nil {
// return "", err
// }
// return wire.NewOutPoint(txhash, utxo.Vout, utxo.Tree).String(), nil
//}