OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / rpcclient / chain.go
1 // Copyright (c) 2014-2017 The btcsuite developers
2 // Copyright (c) 2015-2017 The Decred developers
3 // Use of this source code is governed by an ISC
4 // license that can be found in the LICENSE file.
5
6 package rpcclient
7
8 import (
9         "bytes"
10         "encoding/hex"
11         "encoding/json"
12
13         "github.com/btcsuite/btcd/btcjson"
14         "github.com/btcsuite/btcd/chaincfg/chainhash"
15         "github.com/btcsuite/btcd/wire"
16 )
17
18 // FutureGetBestBlockHashResult is a future promise to deliver the result of a
19 // GetBestBlockAsync RPC invocation (or an applicable error).
20 type FutureGetBestBlockHashResult chan *response
21
22 // Receive waits for the response promised by the future and returns the hash of
23 // the best block in the longest block chain.
24 func (r FutureGetBestBlockHashResult) Receive() (*chainhash.Hash, error) {
25         res, err := receiveFuture(r)
26         if err != nil {
27                 return nil, err
28         }
29
30         // Unmarshal result as a string.
31         var txHashStr string
32         err = json.Unmarshal(res, &txHashStr)
33         if err != nil {
34                 return nil, err
35         }
36         return chainhash.NewHashFromStr(txHashStr)
37 }
38
39 // GetBestBlockHashAsync returns an instance of a type that can be used to get
40 // the result of the RPC at some future time by invoking the Receive function on
41 // the returned instance.
42 //
43 // See GetBestBlockHash for the blocking version and more details.
44 func (c *Client) GetBestBlockHashAsync() FutureGetBestBlockHashResult {
45         cmd := btcjson.NewGetBestBlockHashCmd()
46         return c.sendCmd(cmd)
47 }
48
49 // GetBestBlockHash returns the hash of the best block in the longest block
50 // chain.
51 func (c *Client) GetBestBlockHash() (*chainhash.Hash, error) {
52         return c.GetBestBlockHashAsync().Receive()
53 }
54
55 // FutureGetBlockResult is a future promise to deliver the result of a
56 // GetBlockAsync RPC invocation (or an applicable error).
57 type FutureGetBlockResult chan *response
58
59 // Receive waits for the response promised by the future and returns the raw
60 // block requested from the server given its hash.
61 func (r FutureGetBlockResult) Receive() (*wire.MsgBlock, error) {
62         res, err := receiveFuture(r)
63         if err != nil {
64                 return nil, err
65         }
66
67         // Unmarshal result as a string.
68         var blockHex string
69         err = json.Unmarshal(res, &blockHex)
70         if err != nil {
71                 return nil, err
72         }
73
74         // Decode the serialized block hex to raw bytes.
75         serializedBlock, err := hex.DecodeString(blockHex)
76         if err != nil {
77                 return nil, err
78         }
79
80         // Deserialize the block and return it.
81         var msgBlock wire.MsgBlock
82         err = msgBlock.Deserialize(bytes.NewReader(serializedBlock))
83         if err != nil {
84                 return nil, err
85         }
86         return &msgBlock, nil
87 }
88
89 // GetBlockAsync returns an instance of a type that can be used to get the
90 // result of the RPC at some future time by invoking the Receive function on the
91 // returned instance.
92 //
93 // See GetBlock for the blocking version and more details.
94 func (c *Client) GetBlockAsync(blockHash *chainhash.Hash) FutureGetBlockResult {
95         hash := ""
96         if blockHash != nil {
97                 hash = blockHash.String()
98         }
99
100         cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(false), nil)
101         return c.sendCmd(cmd)
102 }
103
104 // GetBlock returns a raw block from the server given its hash.
105 //
106 // See GetBlockVerbose to retrieve a data structure with information about the
107 // block instead.
108 func (c *Client) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock, error) {
109         return c.GetBlockAsync(blockHash).Receive()
110 }
111
112 // FutureGetBlockVerboseResult is a future promise to deliver the result of a
113 // GetBlockVerboseAsync RPC invocation (or an applicable error).
114 type FutureGetBlockVerboseResult chan *response
115
116 // Receive waits for the response promised by the future and returns the data
117 // structure from the server with information about the requested block.
118 func (r FutureGetBlockVerboseResult) Receive() (*btcjson.GetBlockVerboseResult, error) {
119         res, err := receiveFuture(r)
120         if err != nil {
121                 return nil, err
122         }
123
124         // Unmarshal the raw result into a BlockResult.
125         var blockResult btcjson.GetBlockVerboseResult
126         err = json.Unmarshal(res, &blockResult)
127         if err != nil {
128                 return nil, err
129         }
130         return &blockResult, nil
131 }
132
133 // GetBlockVerboseAsync returns an instance of a type that can be used to get
134 // the result of the RPC at some future time by invoking the Receive function on
135 // the returned instance.
136 //
137 // See GetBlockVerbose for the blocking version and more details.
138 func (c *Client) GetBlockVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseResult {
139         hash := ""
140         if blockHash != nil {
141                 hash = blockHash.String()
142         }
143
144         cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(true), nil)
145         return c.sendCmd(cmd)
146 }
147
148 // GetBlockVerbose returns a data structure from the server with information
149 // about a block given its hash.
150 //
151 // See GetBlockVerboseTx to retrieve transaction data structures as well.
152 // See GetBlock to retrieve a raw block instead.
153 func (c *Client) GetBlockVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error) {
154         return c.GetBlockVerboseAsync(blockHash).Receive()
155 }
156
157 // GetBlockVerboseTxAsync returns an instance of a type that can be used to get
158 // the result of the RPC at some future time by invoking the Receive function on
159 // the returned instance.
160 //
161 // See GetBlockVerboseTx or the blocking version and more details.
162 func (c *Client) GetBlockVerboseTxAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseResult {
163         hash := ""
164         if blockHash != nil {
165                 hash = blockHash.String()
166         }
167
168         cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(true), btcjson.Bool(true))
169         return c.sendCmd(cmd)
170 }
171
172 // GetBlockVerboseTx returns a data structure from the server with information
173 // about a block and its transactions given its hash.
174 //
175 // See GetBlockVerbose if only transaction hashes are preferred.
176 // See GetBlock to retrieve a raw block instead.
177 func (c *Client) GetBlockVerboseTx(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error) {
178         return c.GetBlockVerboseTxAsync(blockHash).Receive()
179 }
180
181 // FutureGetBlockCountResult is a future promise to deliver the result of a
182 // GetBlockCountAsync RPC invocation (or an applicable error).
183 type FutureGetBlockCountResult chan *response
184
185 // Receive waits for the response promised by the future and returns the number
186 // of blocks in the longest block chain.
187 func (r FutureGetBlockCountResult) Receive() (int64, error) {
188         res, err := receiveFuture(r)
189         if err != nil {
190                 return 0, err
191         }
192
193         // Unmarshal the result as an int64.
194         var count int64
195         err = json.Unmarshal(res, &count)
196         if err != nil {
197                 return 0, err
198         }
199         return count, nil
200 }
201
202 // GetBlockCountAsync returns an instance of a type that can be used to get the
203 // result of the RPC at some future time by invoking the Receive function on the
204 // returned instance.
205 //
206 // See GetBlockCount for the blocking version and more details.
207 func (c *Client) GetBlockCountAsync() FutureGetBlockCountResult {
208         cmd := btcjson.NewGetBlockCountCmd()
209         return c.sendCmd(cmd)
210 }
211
212 // GetBlockCount returns the number of blocks in the longest block chain.
213 func (c *Client) GetBlockCount() (int64, error) {
214         return c.GetBlockCountAsync().Receive()
215 }
216
217 // FutureGetDifficultyResult is a future promise to deliver the result of a
218 // GetDifficultyAsync RPC invocation (or an applicable error).
219 type FutureGetDifficultyResult chan *response
220
221 // Receive waits for the response promised by the future and returns the
222 // proof-of-work difficulty as a multiple of the minimum difficulty.
223 func (r FutureGetDifficultyResult) Receive() (float64, error) {
224         res, err := receiveFuture(r)
225         if err != nil {
226                 return 0, err
227         }
228
229         // Unmarshal the result as a float64.
230         var difficulty float64
231         err = json.Unmarshal(res, &difficulty)
232         if err != nil {
233                 return 0, err
234         }
235         return difficulty, nil
236 }
237
238 // GetDifficultyAsync returns an instance of a type that can be used to get the
239 // result of the RPC at some future time by invoking the Receive function on the
240 // returned instance.
241 //
242 // See GetDifficulty for the blocking version and more details.
243 func (c *Client) GetDifficultyAsync() FutureGetDifficultyResult {
244         cmd := btcjson.NewGetDifficultyCmd()
245         return c.sendCmd(cmd)
246 }
247
248 // GetDifficulty returns the proof-of-work difficulty as a multiple of the
249 // minimum difficulty.
250 func (c *Client) GetDifficulty() (float64, error) {
251         return c.GetDifficultyAsync().Receive()
252 }
253
254 // FutureGetBlockChainInfoResult is a promise to deliver the result of a
255 // GetBlockChainInfoAsync RPC invocation (or an applicable error).
256 type FutureGetBlockChainInfoResult chan *response
257
258 // Receive waits for the response promised by the future and returns chain info
259 // result provided by the server.
260 func (r FutureGetBlockChainInfoResult) Receive() (*btcjson.GetBlockChainInfoResult, error) {
261         res, err := receiveFuture(r)
262         if err != nil {
263                 return nil, err
264         }
265
266         var chainInfo btcjson.GetBlockChainInfoResult
267         if err := json.Unmarshal(res, &chainInfo); err != nil {
268                 return nil, err
269         }
270         return &chainInfo, nil
271 }
272
273 // GetBlockChainInfoAsync returns an instance of a type that can be used to get
274 // the result of the RPC at some future time by invoking the Receive function
275 // on the returned instance.
276 //
277 // See GetBlockChainInfo for the blocking version and more details.
278 func (c *Client) GetBlockChainInfoAsync() FutureGetBlockChainInfoResult {
279         cmd := btcjson.NewGetBlockChainInfoCmd()
280         return c.sendCmd(cmd)
281 }
282
283 // GetBlockChainInfo returns information related to the processing state of
284 // various chain-specific details such as the current difficulty from the tip
285 // of the main chain.
286 func (c *Client) GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult, error) {
287         return c.GetBlockChainInfoAsync().Receive()
288 }
289
290 // FutureGetBlockHashResult is a future promise to deliver the result of a
291 // GetBlockHashAsync RPC invocation (or an applicable error).
292 type FutureGetBlockHashResult chan *response
293
294 // Receive waits for the response promised by the future and returns the hash of
295 // the block in the best block chain at the given height.
296 func (r FutureGetBlockHashResult) Receive() (*chainhash.Hash, error) {
297         res, err := receiveFuture(r)
298         if err != nil {
299                 return nil, err
300         }
301
302         // Unmarshal the result as a string-encoded sha.
303         var txHashStr string
304         err = json.Unmarshal(res, &txHashStr)
305         if err != nil {
306                 return nil, err
307         }
308         return chainhash.NewHashFromStr(txHashStr)
309 }
310
311 // GetBlockHashAsync returns an instance of a type that can be used to get the
312 // result of the RPC at some future time by invoking the Receive function on the
313 // returned instance.
314 //
315 // See GetBlockHash for the blocking version and more details.
316 func (c *Client) GetBlockHashAsync(blockHeight int64) FutureGetBlockHashResult {
317         cmd := btcjson.NewGetBlockHashCmd(blockHeight)
318         return c.sendCmd(cmd)
319 }
320
321 // GetBlockHash returns the hash of the block in the best block chain at the
322 // given height.
323 func (c *Client) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
324         return c.GetBlockHashAsync(blockHeight).Receive()
325 }
326
327 // FutureGetBlockHeaderResult is a future promise to deliver the result of a
328 // GetBlockHeaderAsync RPC invocation (or an applicable error).
329 type FutureGetBlockHeaderResult chan *response
330
331 // Receive waits for the response promised by the future and returns the
332 // blockheader requested from the server given its hash.
333 func (r FutureGetBlockHeaderResult) Receive() (*wire.BlockHeader, error) {
334         res, err := receiveFuture(r)
335         if err != nil {
336                 return nil, err
337         }
338
339         // Unmarshal result as a string.
340         var bhHex string
341         err = json.Unmarshal(res, &bhHex)
342         if err != nil {
343                 return nil, err
344         }
345
346         serializedBH, err := hex.DecodeString(bhHex)
347         if err != nil {
348                 return nil, err
349         }
350
351         // Deserialize the blockheader and return it.
352         var bh wire.BlockHeader
353         err = bh.Deserialize(bytes.NewReader(serializedBH))
354         if err != nil {
355                 return nil, err
356         }
357
358         return &bh, err
359 }
360
361 // GetBlockHeaderAsync returns an instance of a type that can be used to get the
362 // result of the RPC at some future time by invoking the Receive function on the
363 // returned instance.
364 //
365 // See GetBlockHeader for the blocking version and more details.
366 func (c *Client) GetBlockHeaderAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderResult {
367         hash := ""
368         if blockHash != nil {
369                 hash = blockHash.String()
370         }
371
372         cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(false))
373         return c.sendCmd(cmd)
374 }
375
376 // GetBlockHeader returns the blockheader from the server given its hash.
377 //
378 // See GetBlockHeaderVerbose to retrieve a data structure with information about the
379 // block instead.
380 func (c *Client) GetBlockHeader(blockHash *chainhash.Hash) (*wire.BlockHeader, error) {
381         return c.GetBlockHeaderAsync(blockHash).Receive()
382 }
383
384 // FutureGetBlockHeaderVerboseResult is a future promise to deliver the result of a
385 // GetBlockAsync RPC invocation (or an applicable error).
386 type FutureGetBlockHeaderVerboseResult chan *response
387
388 // Receive waits for the response promised by the future and returns the
389 // data structure of the blockheader requested from the server given its hash.
390 func (r FutureGetBlockHeaderVerboseResult) Receive() (*btcjson.GetBlockHeaderVerboseResult, error) {
391         res, err := receiveFuture(r)
392         if err != nil {
393                 return nil, err
394         }
395
396         // Unmarshal result as a string.
397         var bh btcjson.GetBlockHeaderVerboseResult
398         err = json.Unmarshal(res, &bh)
399         if err != nil {
400                 return nil, err
401         }
402
403         return &bh, nil
404 }
405
406 // GetBlockHeaderVerboseAsync returns an instance of a type that can be used to get the
407 // result of the RPC at some future time by invoking the Receive function on the
408 // returned instance.
409 //
410 // See GetBlockHeader for the blocking version and more details.
411 func (c *Client) GetBlockHeaderVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderVerboseResult {
412         hash := ""
413         if blockHash != nil {
414                 hash = blockHash.String()
415         }
416
417         cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(true))
418         return c.sendCmd(cmd)
419 }
420
421 // GetBlockHeaderVerbose returns a data structure with information about the
422 // blockheader from the server given its hash.
423 //
424 // See GetBlockHeader to retrieve a blockheader instead.
425 func (c *Client) GetBlockHeaderVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockHeaderVerboseResult, error) {
426         return c.GetBlockHeaderVerboseAsync(blockHash).Receive()
427 }
428
429 // FutureGetMempoolEntryResult is a future promise to deliver the result of a
430 // GetMempoolEntryAsync RPC invocation (or an applicable error).
431 type FutureGetMempoolEntryResult chan *response
432
433 // Receive waits for the response promised by the future and returns a data
434 // structure with information about the transaction in the memory pool given
435 // its hash.
436 func (r FutureGetMempoolEntryResult) Receive() (*btcjson.GetMempoolEntryResult, error) {
437         res, err := receiveFuture(r)
438         if err != nil {
439                 return nil, err
440         }
441
442         // Unmarshal the result as an array of strings.
443         var mempoolEntryResult btcjson.GetMempoolEntryResult
444         err = json.Unmarshal(res, &mempoolEntryResult)
445         if err != nil {
446                 return nil, err
447         }
448
449         return &mempoolEntryResult, nil
450 }
451
452 // GetMempoolEntryAsync returns an instance of a type that can be used to get the
453 // result of the RPC at some future time by invoking the Receive function on the
454 // returned instance.
455 //
456 // See GetMempoolEntry for the blocking version and more details.
457 func (c *Client) GetMempoolEntryAsync(txHash string) FutureGetMempoolEntryResult {
458         cmd := btcjson.NewGetMempoolEntryCmd(txHash)
459         return c.sendCmd(cmd)
460 }
461
462 // GetMempoolEntry returns a data structure with information about the
463 // transaction in the memory pool given its hash.
464 func (c *Client) GetMempoolEntry(txHash string) (*btcjson.GetMempoolEntryResult, error) {
465         return c.GetMempoolEntryAsync(txHash).Receive()
466 }
467
468 // FutureGetRawMempoolResult is a future promise to deliver the result of a
469 // GetRawMempoolAsync RPC invocation (or an applicable error).
470 type FutureGetRawMempoolResult chan *response
471
472 // Receive waits for the response promised by the future and returns the hashes
473 // of all transactions in the memory pool.
474 func (r FutureGetRawMempoolResult) Receive() ([]*chainhash.Hash, error) {
475         res, err := receiveFuture(r)
476         if err != nil {
477                 return nil, err
478         }
479
480         // Unmarshal the result as an array of strings.
481         var txHashStrs []string
482         err = json.Unmarshal(res, &txHashStrs)
483         if err != nil {
484                 return nil, err
485         }
486
487         // Create a slice of ShaHash arrays from the string slice.
488         txHashes := make([]*chainhash.Hash, 0, len(txHashStrs))
489         for _, hashStr := range txHashStrs {
490                 txHash, err := chainhash.NewHashFromStr(hashStr)
491                 if err != nil {
492                         return nil, err
493                 }
494                 txHashes = append(txHashes, txHash)
495         }
496
497         return txHashes, nil
498 }
499
500 // GetRawMempoolAsync returns an instance of a type that can be used to get the
501 // result of the RPC at some future time by invoking the Receive function on the
502 // returned instance.
503 //
504 // See GetRawMempool for the blocking version and more details.
505 func (c *Client) GetRawMempoolAsync() FutureGetRawMempoolResult {
506         cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(false))
507         return c.sendCmd(cmd)
508 }
509
510 // GetRawMempool returns the hashes of all transactions in the memory pool.
511 //
512 // See GetRawMempoolVerbose to retrieve data structures with information about
513 // the transactions instead.
514 func (c *Client) GetRawMempool() ([]*chainhash.Hash, error) {
515         return c.GetRawMempoolAsync().Receive()
516 }
517
518 // FutureGetRawMempoolVerboseResult is a future promise to deliver the result of
519 // a GetRawMempoolVerboseAsync RPC invocation (or an applicable error).
520 type FutureGetRawMempoolVerboseResult chan *response
521
522 // Receive waits for the response promised by the future and returns a map of
523 // transaction hashes to an associated data structure with information about the
524 // transaction for all transactions in the memory pool.
525 func (r FutureGetRawMempoolVerboseResult) Receive() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
526         res, err := receiveFuture(r)
527         if err != nil {
528                 return nil, err
529         }
530
531         // Unmarshal the result as a map of strings (tx shas) to their detailed
532         // results.
533         var mempoolItems map[string]btcjson.GetRawMempoolVerboseResult
534         err = json.Unmarshal(res, &mempoolItems)
535         if err != nil {
536                 return nil, err
537         }
538         return mempoolItems, nil
539 }
540
541 // GetRawMempoolVerboseAsync returns an instance of a type that can be used to
542 // get the result of the RPC at some future time by invoking the Receive
543 // function on the returned instance.
544 //
545 // See GetRawMempoolVerbose for the blocking version and more details.
546 func (c *Client) GetRawMempoolVerboseAsync() FutureGetRawMempoolVerboseResult {
547         cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(true))
548         return c.sendCmd(cmd)
549 }
550
551 // GetRawMempoolVerbose returns a map of transaction hashes to an associated
552 // data structure with information about the transaction for all transactions in
553 // the memory pool.
554 //
555 // See GetRawMempool to retrieve only the transaction hashes instead.
556 func (c *Client) GetRawMempoolVerbose() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
557         return c.GetRawMempoolVerboseAsync().Receive()
558 }
559
560 // FutureVerifyChainResult is a future promise to deliver the result of a
561 // VerifyChainAsync, VerifyChainLevelAsyncRPC, or VerifyChainBlocksAsync
562 // invocation (or an applicable error).
563 type FutureVerifyChainResult chan *response
564
565 // Receive waits for the response promised by the future and returns whether
566 // or not the chain verified based on the check level and number of blocks
567 // to verify specified in the original call.
568 func (r FutureVerifyChainResult) Receive() (bool, error) {
569         res, err := receiveFuture(r)
570         if err != nil {
571                 return false, err
572         }
573
574         // Unmarshal the result as a boolean.
575         var verified bool
576         err = json.Unmarshal(res, &verified)
577         if err != nil {
578                 return false, err
579         }
580         return verified, nil
581 }
582
583 // VerifyChainAsync returns an instance of a type that can be used to get the
584 // result of the RPC at some future time by invoking the Receive function on the
585 // returned instance.
586 //
587 // See VerifyChain for the blocking version and more details.
588 func (c *Client) VerifyChainAsync() FutureVerifyChainResult {
589         cmd := btcjson.NewVerifyChainCmd(nil, nil)
590         return c.sendCmd(cmd)
591 }
592
593 // VerifyChain requests the server to verify the block chain database using
594 // the default check level and number of blocks to verify.
595 //
596 // See VerifyChainLevel and VerifyChainBlocks to override the defaults.
597 func (c *Client) VerifyChain() (bool, error) {
598         return c.VerifyChainAsync().Receive()
599 }
600
601 // VerifyChainLevelAsync returns an instance of a type that can be used to get
602 // the result of the RPC at some future time by invoking the Receive function on
603 // the returned instance.
604 //
605 // See VerifyChainLevel for the blocking version and more details.
606 func (c *Client) VerifyChainLevelAsync(checkLevel int32) FutureVerifyChainResult {
607         cmd := btcjson.NewVerifyChainCmd(&checkLevel, nil)
608         return c.sendCmd(cmd)
609 }
610
611 // VerifyChainLevel requests the server to verify the block chain database using
612 // the passed check level and default number of blocks to verify.
613 //
614 // The check level controls how thorough the verification is with higher numbers
615 // increasing the amount of checks done as consequently how long the
616 // verification takes.
617 //
618 // See VerifyChain to use the default check level and VerifyChainBlocks to
619 // override the number of blocks to verify.
620 func (c *Client) VerifyChainLevel(checkLevel int32) (bool, error) {
621         return c.VerifyChainLevelAsync(checkLevel).Receive()
622 }
623
624 // VerifyChainBlocksAsync returns an instance of a type that can be used to get
625 // the result of the RPC at some future time by invoking the Receive function on
626 // the returned instance.
627 //
628 // See VerifyChainBlocks for the blocking version and more details.
629 func (c *Client) VerifyChainBlocksAsync(checkLevel, numBlocks int32) FutureVerifyChainResult {
630         cmd := btcjson.NewVerifyChainCmd(&checkLevel, &numBlocks)
631         return c.sendCmd(cmd)
632 }
633
634 // VerifyChainBlocks requests the server to verify the block chain database
635 // using the passed check level and number of blocks to verify.
636 //
637 // The check level controls how thorough the verification is with higher numbers
638 // increasing the amount of checks done as consequently how long the
639 // verification takes.
640 //
641 // The number of blocks refers to the number of blocks from the end of the
642 // current longest chain.
643 //
644 // See VerifyChain and VerifyChainLevel to use defaults.
645 func (c *Client) VerifyChainBlocks(checkLevel, numBlocks int32) (bool, error) {
646         return c.VerifyChainBlocksAsync(checkLevel, numBlocks).Receive()
647 }
648
649 // FutureGetTxOutResult is a future promise to deliver the result of a
650 // GetTxOutAsync RPC invocation (or an applicable error).
651 type FutureGetTxOutResult chan *response
652
653 // Receive waits for the response promised by the future and returns a
654 // transaction given its hash.
655 func (r FutureGetTxOutResult) Receive() (*btcjson.GetTxOutResult, error) {
656         res, err := receiveFuture(r)
657         if err != nil {
658                 return nil, err
659         }
660
661         // take care of the special case where the output has been spent already
662         // it should return the string "null"
663         if string(res) == "null" {
664                 return nil, nil
665         }
666
667         // Unmarshal result as an gettxout result object.
668         var txOutInfo *btcjson.GetTxOutResult
669         err = json.Unmarshal(res, &txOutInfo)
670         if err != nil {
671                 return nil, err
672         }
673
674         return txOutInfo, nil
675 }
676
677 // GetTxOutAsync returns an instance of a type that can be used to get
678 // the result of the RPC at some future time by invoking the Receive function on
679 // the returned instance.
680 //
681 // See GetTxOut for the blocking version and more details.
682 func (c *Client) GetTxOutAsync(txHash *chainhash.Hash, index uint32, mempool bool) FutureGetTxOutResult {
683         hash := ""
684         if txHash != nil {
685                 hash = txHash.String()
686         }
687
688         cmd := btcjson.NewGetTxOutCmd(hash, index, &mempool)
689         return c.sendCmd(cmd)
690 }
691
692 // GetTxOut returns the transaction output info if it's unspent and
693 // nil, otherwise.
694 func (c *Client) GetTxOut(txHash *chainhash.Hash, index uint32, mempool bool) (*btcjson.GetTxOutResult, error) {
695         return c.GetTxOutAsync(txHash, index, mempool).Receive()
696 }
697
698 // FutureRescanBlocksResult is a future promise to deliver the result of a
699 // RescanBlocksAsync RPC invocation (or an applicable error).
700 //
701 // NOTE: This is a btcsuite extension ported from
702 // github.com/decred/dcrrpcclient.
703 type FutureRescanBlocksResult chan *response
704
705 // Receive waits for the response promised by the future and returns the
706 // discovered rescanblocks data.
707 //
708 // NOTE: This is a btcsuite extension ported from
709 // github.com/decred/dcrrpcclient.
710 func (r FutureRescanBlocksResult) Receive() ([]btcjson.RescannedBlock, error) {
711         res, err := receiveFuture(r)
712         if err != nil {
713                 return nil, err
714         }
715
716         var rescanBlocksResult []btcjson.RescannedBlock
717         err = json.Unmarshal(res, &rescanBlocksResult)
718         if err != nil {
719                 return nil, err
720         }
721
722         return rescanBlocksResult, nil
723 }
724
725 // RescanBlocksAsync returns an instance of a type that can be used to get the
726 // result of the RPC at some future time by invoking the Receive function on the
727 // returned instance.
728 //
729 // See RescanBlocks for the blocking version and more details.
730 //
731 // NOTE: This is a btcsuite extension ported from
732 // github.com/decred/dcrrpcclient.
733 func (c *Client) RescanBlocksAsync(blockHashes []chainhash.Hash) FutureRescanBlocksResult {
734         strBlockHashes := make([]string, len(blockHashes))
735         for i := range blockHashes {
736                 strBlockHashes[i] = blockHashes[i].String()
737         }
738
739         cmd := btcjson.NewRescanBlocksCmd(strBlockHashes)
740         return c.sendCmd(cmd)
741 }
742
743 // RescanBlocks rescans the blocks identified by blockHashes, in order, using
744 // the client's loaded transaction filter.  The blocks do not need to be on the
745 // main chain, but they do need to be adjacent to each other.
746 //
747 // NOTE: This is a btcsuite extension ported from
748 // github.com/decred/dcrrpcclient.
749 func (c *Client) RescanBlocks(blockHashes []chainhash.Hash) ([]btcjson.RescannedBlock, error) {
750         return c.RescanBlocksAsync(blockHashes).Receive()
751 }
752
753 // FutureInvalidateBlockResult is a future promise to deliver the result of a
754 // InvalidateBlockAsync RPC invocation (or an applicable error).
755 type FutureInvalidateBlockResult chan *response
756
757 // Receive waits for the response promised by the future and returns the raw
758 // block requested from the server given its hash.
759 func (r FutureInvalidateBlockResult) Receive() error {
760         _, err := receiveFuture(r)
761
762         return err
763 }
764
765 // InvalidateBlockAsync returns an instance of a type that can be used to get the
766 // result of the RPC at some future time by invoking the Receive function on the
767 // returned instance.
768 //
769 // See InvalidateBlock for the blocking version and more details.
770 func (c *Client) InvalidateBlockAsync(blockHash *chainhash.Hash) FutureInvalidateBlockResult {
771         hash := ""
772         if blockHash != nil {
773                 hash = blockHash.String()
774         }
775
776         cmd := btcjson.NewInvalidateBlockCmd(hash)
777         return c.sendCmd(cmd)
778 }
779
780 // InvalidateBlock invalidates a specific block.
781 func (c *Client) InvalidateBlock(blockHash *chainhash.Hash) error {
782         return c.InvalidateBlockAsync(blockHash).Receive()
783 }