forked from btcsuite/btcwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoind_events.go
102 lines (82 loc) · 2.89 KB
/
bitcoind_events.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
package chain
import (
"encoding/json"
"fmt"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
)
// BitcoindEvents is the interface that must be satisfied by any type that
// serves bitcoind block and transactions events.
type BitcoindEvents interface {
// TxNotifications will return a channel which will deliver new
// transactions.
TxNotifications() <-chan *wire.MsgTx
// BlockNotifications will return a channel which will deliver new
// blocks.
BlockNotifications() <-chan *wire.MsgBlock
// LookupInputSpend will return the transaction found in mempool that
// spends the given input.
LookupInputSpend(op wire.OutPoint) (chainhash.Hash, bool)
// Start will kick off any goroutines required for operation.
Start() error
// Stop will clean up any resources and goroutines.
Stop() error
}
// Ensure rpcclient.Client implements the rpcClient interface at compile time.
var _ batchClient = (*rpcclient.Client)(nil)
// NewBitcoindEventSubscriber initialises a new BitcoinEvents object impl
// depending on the config passed.
func NewBitcoindEventSubscriber(cfg *BitcoindConfig, client *rpcclient.Client,
bClient batchClient) (BitcoindEvents, error) {
if cfg.PollingConfig != nil && cfg.ZMQConfig != nil {
return nil, fmt.Errorf("either PollingConfig or ZMQConfig " +
"should be specified, not both")
}
// Check if the bitcoind node is on a version that has the
// gettxspendingprevout RPC. If it does, then we don't need to maintain
// a mempool for ZMQ clients and can maintain a smaller mempool for RPC
// clients.
hasRPC, err := hasSpendingPrevoutRPC(client)
if err != nil {
return nil, err
}
if cfg.PollingConfig != nil {
if client == nil {
return nil, fmt.Errorf("rpc client must be given " +
"if rpc polling is to be used for event " +
"subscriptions")
}
pollingEvents := newBitcoindRPCPollingEvents(
cfg.PollingConfig, client, bClient, hasRPC,
)
return pollingEvents, nil
}
if cfg.ZMQConfig == nil {
return nil, fmt.Errorf("ZMQConfig must be specified if " +
"rpcpolling is disabled")
}
return newBitcoindZMQEvents(cfg.ZMQConfig, client, bClient, hasRPC)
}
// hasSpendingPrevoutRPC returns whether or not the bitcoind has the newer
// gettxspendingprevout RPC.
func hasSpendingPrevoutRPC(client *rpcclient.Client) (bool, error) {
// Fetch the bitcoind version.
resp, err := client.RawRequest("getnetworkinfo", nil)
if err != nil {
return false, err
}
info := struct {
Version int64 `json:"version"`
}{}
if err := json.Unmarshal(resp, &info); err != nil {
return false, err
}
// Bitcoind returns a single value representing the semantic version:
// 10000 * CLIENT_VERSION_MAJOR + 100 * CLIENT_VERSION_MINOR
// + 1 * CLIENT_VERSION_BUILD
//
// The gettxspendingprevout call was added in version 24.0.0, so we
// return for versions >= 240000.
return info.Version >= 240000, nil
}