forked from ltcsuite/ltcwallet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.go
159 lines (141 loc) · 5.52 KB
/
interface.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
package chain
import (
"time"
"github.com/dcrlabs/ltcwallet/waddrmgr"
"github.com/dcrlabs/ltcwallet/wtxmgr"
"github.com/ltcsuite/ltcd/chaincfg/chainhash"
"github.com/ltcsuite/ltcd/ltcutil"
"github.com/ltcsuite/ltcd/rpcclient"
"github.com/ltcsuite/ltcd/wire"
)
// isCurrentDelta is the delta duration we'll use from the present time to
// determine if a backend is considered "current", i.e. synced to the tip of
// the chain.
const isCurrentDelta = 2 * time.Hour
// BackEnds returns a list of the available back ends.
// TODO: Refactor each into a driver and use dynamic registration.
func BackEnds() []string {
return []string{
"litecoind",
"ltcd",
"neutrino",
"bitcoind-rpc-polling",
}
}
// Interface allows more than one backing blockchain source, such as a
// btcd RPC chain server, or an SPV library, as long as we write a driver for
// it.
type Interface interface {
Start() error
Stop()
WaitForShutdown()
GetBestBlock() (*chainhash.Hash, int32, error)
GetBlock(*chainhash.Hash) (*wire.MsgBlock, error)
GetBlockHash(int64) (*chainhash.Hash, error)
GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader, error)
IsCurrent() bool
FilterBlocks(*FilterBlocksRequest) (*FilterBlocksResponse, error)
BlockStamp() (*waddrmgr.BlockStamp, error)
SendRawTransaction(*wire.MsgTx, bool) (*chainhash.Hash, error)
Rescan(*chainhash.Hash, []ltcutil.Address, map[wire.OutPoint]ltcutil.Address) error
NotifyReceived([]ltcutil.Address) error
NotifyBlocks() error
Notifications() <-chan interface{}
BackEnd() string
}
// Notification types. These are defined here and processed from from reading
// a notificationChan to avoid handling these notifications directly in
// rpcclient callbacks, which isn't very Go-like and doesn't allow
// blocking client calls.
type (
// ClientConnected is a notification for when a client connection is
// opened or reestablished to the chain server.
ClientConnected struct{}
// BlockConnected is a notification for a newly-attached block to the
// best chain.
BlockConnected wtxmgr.BlockMeta
// FilteredBlockConnected is an alternate notification that contains
// both block and relevant transaction information in one struct, which
// allows atomic updates.
FilteredBlockConnected struct {
Block *wtxmgr.BlockMeta
RelevantTxs []*wtxmgr.TxRecord
}
// FilterBlocksRequest specifies a range of blocks and the set of
// internal and external addresses of interest, indexed by corresponding
// scoped-index of the child address. A global set of watched outpoints
// is also included to monitor for spends.
FilterBlocksRequest struct {
Blocks []wtxmgr.BlockMeta
ExternalAddrs map[waddrmgr.ScopedIndex]ltcutil.Address
InternalAddrs map[waddrmgr.ScopedIndex]ltcutil.Address
WatchedOutPoints map[wire.OutPoint]ltcutil.Address
}
// FilterBlocksResponse reports the set of all internal and external
// addresses found in response to a FilterBlockRequest, any outpoints
// found that correspond to those addresses, as well as the relevant
// transactions that can modify the wallet's balance. The index of the
// block within the FilterBlocksRequest is returned, such that the
// caller can reinitiate a request for the subsequent block after
// updating the addresses of interest.
FilterBlocksResponse struct {
BatchIndex uint32
BlockMeta wtxmgr.BlockMeta
FoundExternalAddrs map[waddrmgr.KeyScope]map[uint32]struct{}
FoundInternalAddrs map[waddrmgr.KeyScope]map[uint32]struct{}
FoundOutPoints map[wire.OutPoint]ltcutil.Address
RelevantTxns []*wire.MsgTx
}
// BlockDisconnected is a notifcation that the block described by the
// BlockStamp was reorganized out of the best chain.
BlockDisconnected wtxmgr.BlockMeta
// RelevantTx is a notification for a transaction which spends wallet
// inputs or pays to a watched address.
RelevantTx struct {
TxRecord *wtxmgr.TxRecord
Block *wtxmgr.BlockMeta // nil if unmined
}
// RescanProgress is a notification describing the current status
// of an in-progress rescan.
RescanProgress struct {
Hash chainhash.Hash
Height int32
Time time.Time
}
// RescanFinished is a notification that a previous rescan request
// has finished.
RescanFinished struct {
Hash *chainhash.Hash
Height int32
Time time.Time
}
)
// batchClient defines an interface that is used to interact with the RPC
// client.
//
// NOTE: the client returned from `rpcclient.NewBatch` will implement this
// interface. Unlike the client from `rpcclient.New`, calling `GetRawMempool`
// on this client will block and won't return.
//
// TODO(yy): create a new type BatchClient in `rpcclient`.
type batchClient interface {
// GetRawMempoolAsync returns an instance of a type that can be used to
// get the result of the RPC at some future time by invoking the
// Receive function on the returned instance.
GetRawMempoolAsync() rpcclient.FutureGetRawMempoolResult
// GetRawTransactionAsync returns an instance of a type that can be
// used to get the result of the RPC at some future time by invoking
// the Receive function on the returned instance.
GetRawTransactionAsync(
txHash *chainhash.Hash) rpcclient.FutureGetRawTransactionResult
// Send marshalls bulk requests and sends to the server creates a
// response channel to receive the response
Send() error
}
// getRawTxReceiver defines an interface that's used to receive response from
// `GetRawTransactionAsync`.
type getRawTxReceiver interface {
// Receive waits for the Response promised by the future and returns a
// transaction given its hash.
Receive() (*ltcutil.Tx, error)
}