/
transactions.go
344 lines (312 loc) · 8.93 KB
/
transactions.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package wallet
import (
"encoding/binary"
"errors"
"fmt"
"sort"
"github.com/HyperspaceApp/Hyperspace/build"
"github.com/HyperspaceApp/Hyperspace/encoding"
"github.com/HyperspaceApp/Hyperspace/modules"
"github.com/HyperspaceApp/Hyperspace/types"
)
var (
errOutOfBounds = errors.New("requesting transactions at unknown confirmation heights")
)
// AddressTransactions returns all of the wallet transactions associated with a
// single unlock hash.
func (w *Wallet) AddressTransactions(uh types.UnlockHash) (pts []modules.ProcessedTransaction, err error) {
if err := w.tg.Add(); err != nil {
return []modules.ProcessedTransaction{}, err
}
defer w.tg.Done()
// ensure durability of reported transactions
w.mu.Lock()
defer w.mu.Unlock()
if err = w.syncDB(); err != nil {
return
}
txnIndices, _ := dbGetAddrTransactions(w.dbTx, uh)
for _, i := range txnIndices {
pt, err := dbGetProcessedTransaction(w.dbTx, i)
if err != nil {
continue
}
pts = append(pts, pt)
}
return pts, nil
}
// AddressUnconfirmedTransactions returns all of the unconfirmed wallet transactions
// related to a specific address.
func (w *Wallet) AddressUnconfirmedTransactions(uh types.UnlockHash) (pts []modules.ProcessedTransaction, err error) {
if err := w.tg.Add(); err != nil {
return []modules.ProcessedTransaction{}, err
}
defer w.tg.Done()
// ensure durability of reported transactions
w.mu.Lock()
defer w.mu.Unlock()
if err = w.syncDB(); err != nil {
return
}
// Scan the full list of unconfirmed transactions to see if there are any
// related transactions.
for _, pt := range w.unconfirmedProcessedTransactions {
relevant := false
for _, input := range pt.Inputs {
if input.RelatedAddress == uh {
relevant = true
break
}
}
for _, output := range pt.Outputs {
if output.RelatedAddress == uh {
relevant = true
break
}
}
if relevant {
pts = append(pts, pt)
}
}
return pts, err
}
// Transaction returns the transaction with the given id. 'False' is returned
// if the transaction does not exist.
func (w *Wallet) Transaction(txid types.TransactionID) (pt modules.ProcessedTransaction, found bool, err error) {
if err := w.tg.Add(); err != nil {
return modules.ProcessedTransaction{}, false, err
}
defer w.tg.Done()
// ensure durability of reported transaction
w.mu.Lock()
defer w.mu.Unlock()
if err = w.syncDB(); err != nil {
return
}
// Get the keyBytes for the given txid
keyBytes, err := dbGetTransactionIndex(w.dbTx, txid)
if err != nil {
return modules.ProcessedTransaction{}, false, nil
}
// Retrieve the transaction
found = encoding.Unmarshal(w.dbTx.Bucket(bucketProcessedTransactions).Get(keyBytes), &pt) == nil
return
}
// FilteredTransactions returns all transactions matching a set of criteria
func (w *Wallet) FilteredTransactions(count uint64, watchOnly bool, category string) (pts []modules.ProcessedTransaction, err error) {
if err := w.tg.Add(); err != nil {
return nil, err
}
defer w.tg.Done()
// ensure durability of reported transactions
w.mu.Lock()
defer w.mu.Unlock()
if err = w.syncDB(); err != nil {
return
}
// if we don't have any watched address but we were passed watchOnly,
// bail out with an empty list right away
if len(w.watchedAddrs) == 0 && watchOnly {
return
}
// Get the bucket, the largest key in it and the cursor
bucket := w.dbTx.Bucket(bucketProcessedTransactions)
cursor := bucket.Cursor()
var pt modules.ProcessedTransaction
// Database is empty
if bucket.Sequence() == 0 {
return
}
// Get last processed transaction
key, ptBytes := cursor.Last()
txloop:
for {
// If the key is empty, we're at the end of the bucket
if key == nil {
return
}
// Return once we've found enough results
if len(pts) >= int(count) {
return
}
// Decode the transaction
if err = decodeProcessedTransaction(ptBytes, &pt); build.DEBUG && err != nil {
panic(err)
}
// Prepare the next iteration before we finish processing this one
key, ptBytes = cursor.Prev()
// handle the watchOnly filter, including category filters
// if necessary
if watchOnly {
for _, input := range pt.Inputs {
if _, ok := w.watchedAddrs[input.RelatedAddress]; ok {
if category == "" || category == "send" {
pts = append(pts, pt)
continue txloop
}
}
}
for _, output := range pt.Outputs {
if _, ok := w.watchedAddrs[output.RelatedAddress]; ok {
if category == "" || category == "receive" {
pts = append(pts, pt)
continue txloop
}
}
}
// handle no watch filter, including category filters if
// necessary
} else {
if len(pt.Inputs) > 0 && (category == "" || category == "send") {
pts = append(pts, pt)
continue
}
if len(pt.Outputs) > 0 && (category == "" || category == "receive") {
pts = append(pts, pt)
continue
}
}
}
}
// Transactions returns all transactions relevant to the wallet that were
// confirmed in the range [startHeight, endHeight].
func (w *Wallet) Transactions(startHeight, endHeight types.BlockHeight) (pts []modules.ProcessedTransaction, err error) {
if err := w.tg.Add(); err != nil {
return nil, err
}
defer w.tg.Done()
// ensure durability of reported transactions
w.mu.Lock()
defer w.mu.Unlock()
if err = w.syncDB(); err != nil {
return
}
height, err := dbGetConsensusHeight(w.dbTx)
if err != nil {
return
} else if startHeight > height || startHeight > endHeight {
return nil, errOutOfBounds
}
// Get the bucket, the largest key in it and the cursor
bucket := w.dbTx.Bucket(bucketProcessedTransactions)
cursor := bucket.Cursor()
nextKey := bucket.Sequence() + 1
// Database is empty
if nextKey == 1 {
return
}
var pt modules.ProcessedTransaction
keyBytes := make([]byte, 8)
var result int
func() {
// Recover from possible panic during binary search
defer func() {
r := recover()
if r != nil {
err = fmt.Errorf("%v", r)
}
}()
// Start binary searching
result = sort.Search(int(nextKey), func(i int) bool {
// Create the key for the index
binary.BigEndian.PutUint64(keyBytes, uint64(i))
// Retrieve the processed transaction
key, ptBytes := cursor.Seek(keyBytes)
if build.DEBUG && key == nil {
panic("Failed to retrieve processed Transaction by key")
}
// Decode the transaction
if err = decodeProcessedTransaction(ptBytes, &pt); build.DEBUG && err != nil {
panic(err)
}
return pt.ConfirmationHeight >= startHeight
})
}()
if err != nil {
return
}
if uint64(result) == nextKey {
// No transaction was found
return
}
// Create the key that corresponds to the result of the search
binary.BigEndian.PutUint64(keyBytes, uint64(result))
// Get the processed transaction and decode it
key, ptBytes := cursor.Seek(keyBytes)
if build.DEBUG && key == nil {
build.Critical("Couldn't find the processed transaction from the search.")
}
if err = decodeProcessedTransaction(ptBytes, &pt); build.DEBUG && err != nil {
build.Critical(err)
}
// Gather all transactions until endHeight is reached
for pt.ConfirmationHeight <= endHeight {
if build.DEBUG && pt.ConfirmationHeight < startHeight {
build.Critical("wallet processed transactions are not sorted")
}
pts = append(pts, pt)
// Get next processed transaction
key, ptBytes := cursor.Next()
if key == nil {
break
}
// Decode the transaction
if err := decodeProcessedTransaction(ptBytes, &pt); build.DEBUG && err != nil {
panic("Failed to decode the processed transaction")
}
}
return
}
// UnconfirmedTransactions returns the set of unconfirmed transactions that are
// relevant to the wallet.
func (w *Wallet) FilteredUnconfirmedTransactions(watchOnly bool, category string) ([]modules.ProcessedTransaction, error) {
if err := w.tg.Add(); err != nil {
return nil, err
}
defer w.tg.Done()
w.mu.RLock()
defer w.mu.RUnlock()
var pts []modules.ProcessedTransaction
txloop:
for _, txn := range w.unconfirmedProcessedTransactions {
if watchOnly {
for _, input := range txn.Inputs {
if _, ok := w.watchedAddrs[input.RelatedAddress]; ok {
if category == "" || category == "send" {
pts = append(pts, txn)
continue txloop
}
}
}
for _, output := range txn.Outputs {
if _, ok := w.watchedAddrs[output.RelatedAddress]; ok {
if category == "" || category == "receive" {
pts = append(pts, txn)
continue txloop
}
}
}
} else {
if len(txn.Inputs) > 0 && (category == "" || category == "send") {
pts = append(pts, txn)
continue
}
if len(txn.Outputs) > 0 && (category == "" || category == "receive") {
pts = append(pts, txn)
continue
}
}
}
return pts, nil
}
// FilteredUnconfirmedTransactions returns the set of unconfirmed transactions that are
// relevant to the wallet and pass a certain set of criteria.
func (w *Wallet) UnconfirmedTransactions() ([]modules.ProcessedTransaction, error) {
if err := w.tg.Add(); err != nil {
return nil, err
}
defer w.tg.Done()
w.mu.RLock()
defer w.mu.RUnlock()
return w.unconfirmedProcessedTransactions, nil
}