forked from decred/dcrwallet
/
txquery.go
635 lines (567 loc) · 19.3 KB
/
txquery.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright (c) 2015 The btcsuite developers
// Copyright (c) 2015-2017 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package udb
import (
"fmt"
"github.com/hybridnetwork/hxd/blockchain/stake"
"github.com/hybridnetwork/hxd/chaincfg/chainhash"
"github.com/hybridnetwork/hxd/wire"
dcrutil "github.com/hybridnetwork/hxutil"
"github.com/hybridnetwork/hxwallet/apperrors"
"github.com/hybridnetwork/hxwallet/walletdb"
)
// CreditRecord contains metadata regarding a transaction credit for a known
// transaction. Further details may be looked up by indexing a wire.MsgTx.TxOut
// with the Index field.
type CreditRecord struct {
Index uint32
Amount dcrutil.Amount
Spent bool
Change bool
OpCode uint8
IsCoinbase bool
}
// DebitRecord contains metadata regarding a transaction debit for a known
// transaction. Further details may be looked up by indexing a wire.MsgTx.TxIn
// with the Index field.
type DebitRecord struct {
Amount dcrutil.Amount
Index uint32
}
// TxDetails is intended to provide callers with access to rich details
// regarding a relevant transaction and which inputs and outputs are credit or
// debits.
type TxDetails struct {
TxRecord
Block BlockMeta
Credits []CreditRecord
Debits []DebitRecord
}
// Height returns the height of a transaction according to the BlockMeta.
func (t *TxDetails) Height() int32 {
return t.Block.Block.Height
}
// minedTxDetails fetches the TxDetails for the mined transaction with hash
// txHash and the passed tx record key and value.
func (s *Store) minedTxDetails(ns walletdb.ReadBucket, txHash *chainhash.Hash, recKey, recVal []byte) (*TxDetails, error) {
var details TxDetails
// Parse transaction record k/v, lookup the full block record for the
// block time, and read all matching credits, debits.
err := readRawTxRecord(txHash, recVal, &details.TxRecord)
if err != nil {
return nil, err
}
err = readRawTxRecordBlock(recKey, &details.Block.Block)
if err != nil {
return nil, err
}
details.Block.Time, err = fetchBlockTime(ns, details.Block.Height)
if err != nil {
return nil, err
}
credIter := makeReadCreditIterator(ns, recKey)
for credIter.next() {
if int(credIter.elem.Index) >= len(details.MsgTx.TxOut) {
str := "saved credit index exceeds number of outputs"
return nil, storeError(apperrors.ErrData, str, nil)
}
// The credit iterator does not record whether this credit was
// spent by an unmined transaction, so check that here.
if !credIter.elem.Spent {
k := canonicalOutPoint(txHash, credIter.elem.Index)
spent := existsRawUnminedInput(ns, k) != nil
credIter.elem.Spent = spent
}
details.Credits = append(details.Credits, credIter.elem)
}
if credIter.err != nil {
return nil, credIter.err
}
debIter := makeReadDebitIterator(ns, recKey)
for debIter.next() {
if int(debIter.elem.Index) >= len(details.MsgTx.TxIn) {
str := "saved debit index exceeds number of inputs"
return nil, storeError(apperrors.ErrData, str, nil)
}
details.Debits = append(details.Debits, debIter.elem)
}
return &details, debIter.err
}
// unminedTxDetails fetches the TxDetails for the unmined transaction with the
// hash txHash and the passed unmined record value.
func (s *Store) unminedTxDetails(ns walletdb.ReadBucket, txHash *chainhash.Hash, v []byte) (*TxDetails, error) {
details := TxDetails{
Block: BlockMeta{Block: Block{Height: -1}},
}
err := readRawTxRecord(txHash, v, &details.TxRecord)
if err != nil {
return nil, err
}
it := makeReadUnminedCreditIterator(ns, txHash)
for it.next() {
if int(it.elem.Index) >= len(details.MsgTx.TxOut) {
str := "saved credit index exceeds number of outputs"
return nil, storeError(apperrors.ErrData, str, nil)
}
// Set the Spent field since this is not done by the iterator.
it.elem.Spent = existsRawUnminedInput(ns, it.ck) != nil
details.Credits = append(details.Credits, it.elem)
}
if it.err != nil {
return nil, it.err
}
// Debit records are not saved for unmined transactions. Instead, they
// must be looked up for each transaction input manually. There are two
// kinds of previous credits that may be debited by an unmined
// transaction: mined unspent outputs (which remain marked unspent even
// when spent by an unmined transaction), and credits from other unmined
// transactions. Both situations must be considered.
for i, output := range details.MsgTx.TxIn {
opKey := canonicalOutPoint(&output.PreviousOutPoint.Hash,
output.PreviousOutPoint.Index)
credKey := existsRawUnspent(ns, opKey)
if credKey != nil {
v := existsRawCredit(ns, credKey)
amount, err := fetchRawCreditAmount(v)
if err != nil {
return nil, err
}
details.Debits = append(details.Debits, DebitRecord{
Amount: amount,
Index: uint32(i),
})
continue
}
v := existsRawUnminedCredit(ns, opKey)
if v == nil {
continue
}
amount, err := fetchRawCreditAmount(v)
if err != nil {
return nil, err
}
details.Debits = append(details.Debits, DebitRecord{
Amount: amount,
Index: uint32(i),
})
}
return &details, nil
}
// TxDetails looks up all recorded details regarding a transaction with some
// hash. In case of a hash collision, the most recent transaction with a
// matching hash is returned.
//
// Not finding a transaction with this hash is not an error. In this case,
// a nil TxDetails is returned.
func (s *Store) TxDetails(ns walletdb.ReadBucket, txHash *chainhash.Hash) (*TxDetails, error) {
// First, check whether there exists an unmined transaction with this
// hash. Use it if found.
v := existsRawUnmined(ns, txHash[:])
if v != nil {
return s.unminedTxDetails(ns, txHash, v)
}
// Otherwise, if there exists a mined transaction with this matching
// hash, skip over to the newest and begin fetching all details.
k, v := latestTxRecord(ns, txHash[:])
if v == nil {
// not found
return nil, nil
}
return s.minedTxDetails(ns, txHash, k, v)
}
// TicketDetails is intended to provide callers with access to rich details
// regarding a relevant transaction and which inputs and outputs are credit or
// debits.
type TicketDetails struct {
Ticket *TxDetails
Spender *TxDetails
}
// TicketDetails looks up all recorded details regarding a ticket with some
// hash.
//
// Not finding a ticket with this hash is not an error. In this case,
// a nil TicketDetiails is returned.
func (s *Store) TicketDetails(ns walletdb.ReadBucket, txDetails *TxDetails) (*TicketDetails, error) {
var ticketDetails = &TicketDetails{}
ok, _ := stake.IsSStx(&txDetails.MsgTx)
if !ok {
return nil, nil
}
ticketDetails.Ticket = txDetails
var spenderHash = chainhash.Hash{}
// Check if the ticket is spent or not. Look up the credit for output 0
// and check if either a debit is recorded or the output is spent by an
// unmined transaction.
_, credVal := existsCredit(ns, &txDetails.Hash, 0, &txDetails.Block.Block)
if credVal != nil {
if extractRawCreditIsSpent(credVal) {
debKey := extractRawCreditSpenderDebitKey(credVal)
debHash := extractRawDebitHash(debKey)
copy(spenderHash[:], debHash)
}
} else {
opKey := canonicalOutPoint(&txDetails.Hash, 0)
spenderVal := existsRawUnminedInput(ns, opKey)
if spenderVal != nil {
copy(spenderHash[:], spenderVal)
}
}
spenderDetails, err := s.TxDetails(ns, &spenderHash)
if err != nil {
return nil, err
}
ticketDetails.Spender = spenderDetails
return ticketDetails, nil
}
// parseTx deserializes a transaction into a MsgTx using the readRawTxRecord
// method.
func (s *Store) parseTx(txHash chainhash.Hash, v []byte) (*wire.MsgTx, error) {
details := TxDetails{
Block: BlockMeta{Block: Block{Height: -1}},
}
err := readRawTxRecord(&txHash, v, &details.TxRecord)
if err != nil {
return nil, err
}
return &details.MsgTx, nil
}
// Tx looks up all the stored wire.MsgTx for a transaction with some
// hash. In case of a hash collision, the most recent transaction with a
// matching hash is returned.
//
// Not finding a transaction with this hash is not an error. In this case,
// a nil TxDetails is returned.
func (s *Store) Tx(ns walletdb.ReadBucket, txHash *chainhash.Hash) (*wire.MsgTx, error) {
// First, check whether there exists an unmined transaction with this
// hash. Use it if found.
v := existsRawUnmined(ns, txHash[:])
if v != nil {
return s.parseTx(*txHash, v)
}
// Otherwise, if there exists a mined transaction with this matching
// hash, skip over to the newest and begin fetching the msgTx.
_, v = latestTxRecord(ns, txHash[:])
if v == nil {
// not found
return nil, nil
}
return s.parseTx(*txHash, v)
}
// ExistsTx checks to see if a transaction exists in the database.
func (s *Store) ExistsTx(ns walletdb.ReadBucket, txHash *chainhash.Hash) bool {
// First, check whether there exists an unmined transaction with this
// hash. Use it if found.
v := existsRawUnmined(ns, txHash[:])
if v != nil {
return true
}
// Otherwise, if there exists a mined transaction with this matching
// hash, skip over to the newest and begin fetching the msgTx.
_, v = latestTxRecord(ns, txHash[:])
return v != nil
}
// ExistsUTXO checks to see if op refers to an unspent transaction output or a
// credit spent by an unmined transaction. This check is sufficient to
// determine whether a transaction input is relevant to the wallet by spending a
// UTXO or conflicting with another mempool transaction that double spends the
// output.
func (s *Store) ExistsUTXO(dbtx walletdb.ReadTx, op *wire.OutPoint) bool {
ns := dbtx.ReadBucket(wtxmgrBucketKey)
k, v := existsUnspent(ns, op)
if v != nil {
return true
}
return existsRawUnminedCredit(ns, k) != nil
}
// UniqueTxDetails looks up all recorded details for a transaction recorded
// mined in some particular block, or an unmined transaction if block is nil.
//
// Not finding a transaction with this hash from this block is not an error. In
// this case, a nil TxDetails is returned.
func (s *Store) UniqueTxDetails(ns walletdb.ReadBucket, txHash *chainhash.Hash,
block *Block) (*TxDetails, error) {
if block == nil {
v := existsRawUnmined(ns, txHash[:])
if v == nil {
return nil, nil
}
return s.unminedTxDetails(ns, txHash, v)
}
k, v := existsTxRecord(ns, txHash, block)
if v == nil {
return nil, nil
}
return s.minedTxDetails(ns, txHash, k, v)
}
// TxBlockHeight returns the block height of a mined transaction, or -1 for any
// unmined transactions.
func (s *Store) TxBlockHeight(dbtx walletdb.ReadTx, txHash *chainhash.Hash) (int32, error) {
ns := dbtx.ReadBucket(wtxmgrBucketKey)
v := existsRawUnmined(ns, txHash[:])
if v != nil {
return -1, nil
}
k, _ := latestTxRecord(ns, txHash[:])
if k == nil {
const str = "transaction not found"
return 0, apperrors.E{ErrorCode: apperrors.ErrValueNoExists, Description: str, Err: nil}
}
var height int32
err := readRawTxRecordBlockHeight(k, &height)
return height, err
}
// rangeUnminedTransactions executes the function f with TxDetails for every
// unmined transaction. f is not executed if no unmined transactions exist.
// Error returns from f (if any) are propigated to the caller. Returns true
// (signaling breaking out of a RangeTransactions) iff f executes and returns
// true.
func (s *Store) rangeUnminedTransactions(ns walletdb.ReadBucket, f func([]TxDetails) (bool, error)) (bool, error) {
var details []TxDetails
err := ns.NestedReadBucket(bucketUnmined).ForEach(func(k, v []byte) error {
if len(k) < 32 {
str := fmt.Sprintf("%s: short key (expected %d "+
"bytes, read %d)", bucketUnmined, 32, len(k))
return storeError(apperrors.ErrData, str, nil)
}
var txHash chainhash.Hash
copy(txHash[:], k)
detail, err := s.unminedTxDetails(ns, &txHash, v)
if err != nil {
return err
}
// Because the key was created while foreach-ing over the
// bucket, it should be impossible for unminedTxDetails to ever
// successfully return a nil details struct.
details = append(details, *detail)
return nil
})
if err == nil && len(details) > 0 {
return f(details)
}
return false, err
}
// rangeBlockTransactions executes the function f with TxDetails for every block
// between heights begin and end (reverse order when end > begin) until f
// returns true, or the transactions from block is processed. Returns true iff
// f executes and returns true.
func (s *Store) rangeBlockTransactions(ns walletdb.ReadBucket, begin, end int32,
f func([]TxDetails) (bool, error)) (bool, error) {
// Mempool height is considered a high bound.
if begin < 0 {
begin = int32(^uint32(0) >> 1)
}
if end < 0 {
end = int32(^uint32(0) >> 1)
}
var blockIter blockIterator
var advance func(*blockIterator) bool
if begin < end {
// Iterate in forwards order
blockIter = makeReadBlockIterator(ns, begin)
advance = func(it *blockIterator) bool {
if !it.next() {
return false
}
return it.elem.Height <= end
}
} else {
// Iterate in backwards order, from begin -> end.
blockIter = makeReadBlockIterator(ns, begin)
advance = func(it *blockIterator) bool {
if !it.prev() {
return false
}
return end <= it.elem.Height
}
}
var details []TxDetails
for advance(&blockIter) {
block := &blockIter.elem
if cap(details) < len(block.transactions) {
details = make([]TxDetails, 0, len(block.transactions))
} else {
details = details[:0]
}
for _, txHash := range block.transactions {
k := keyTxRecord(&txHash, &block.Block)
v := existsRawTxRecord(ns, k)
if v == nil {
str := fmt.Sprintf("missing transaction %v for "+
"block %v", txHash, block.Height)
return false, storeError(apperrors.ErrData, str, nil)
}
detail := TxDetails{
Block: BlockMeta{
Block: block.Block,
Time: block.Time,
},
}
err := readRawTxRecord(&txHash, v, &detail.TxRecord)
if err != nil {
return false, err
}
credIter := makeReadCreditIterator(ns, k)
for credIter.next() {
if int(credIter.elem.Index) >= len(detail.MsgTx.TxOut) {
str := "saved credit index exceeds number of outputs"
return false, storeError(apperrors.ErrData, str, nil)
}
// The credit iterator does not record whether
// this credit was spent by an unmined
// transaction, so check that here.
if !credIter.elem.Spent {
k := canonicalOutPoint(&txHash, credIter.elem.Index)
spent := existsRawUnminedInput(ns, k) != nil
credIter.elem.Spent = spent
}
detail.Credits = append(detail.Credits, credIter.elem)
}
if credIter.err != nil {
return false, credIter.err
}
debIter := makeReadDebitIterator(ns, k)
for debIter.next() {
if int(debIter.elem.Index) >= len(detail.MsgTx.TxIn) {
str := "saved debit index exceeds number of inputs"
return false, storeError(apperrors.ErrData, str, nil)
}
detail.Debits = append(detail.Debits, debIter.elem)
}
if debIter.err != nil {
return false, debIter.err
}
details = append(details, detail)
}
// Decred: Block records are saved even when no transactions are
// included. This is used to save the votebits from every
// block. This differs from btcwallet where every block must
// have one transaction. Since f may only be called when
// len(details) > 0, this must be explicitly tested.
if len(details) == 0 {
continue
}
brk, err := f(details)
if err != nil || brk {
return brk, err
}
}
return false, blockIter.err
}
// RangeTransactions runs the function f on all transaction details between
// blocks on the best chain over the height range [begin,end]. The special
// height -1 may be used to also include unmined transactions. If the end
// height comes before the begin height, blocks are iterated in reverse order
// and unmined transactions (if any) are processed first.
//
// The function f may return an error which, if non-nil, is propagated to the
// caller. Additionally, a boolean return value allows exiting the function
// early without reading any additional transactions early when true.
//
// All calls to f are guaranteed to be passed a slice with more than zero
// elements. The slice may be reused for multiple blocks, so it is not safe to
// use it after the loop iteration it was acquired.
func (s *Store) RangeTransactions(ns walletdb.ReadBucket, begin, end int32,
f func([]TxDetails) (bool, error)) error {
var addedUnmined bool
if begin < 0 {
brk, err := s.rangeUnminedTransactions(ns, f)
if err != nil || brk {
return err
}
addedUnmined = true
}
brk, err := s.rangeBlockTransactions(ns, begin, end, f)
if err == nil && !brk && !addedUnmined && end < 0 {
_, err = s.rangeUnminedTransactions(ns, f)
}
return err
}
// PreviousPkScripts returns a slice of previous output scripts for each credit
// output this transaction record debits from.
func (s *Store) PreviousPkScripts(ns walletdb.ReadBucket, rec *TxRecord, block *Block) ([][]byte, error) {
var pkScripts [][]byte
if block == nil {
for _, input := range rec.MsgTx.TxIn {
prevOut := &input.PreviousOutPoint
// Input may spend a previous unmined output, a
// mined output (which would still be marked
// unspent), or neither.
v := existsRawUnmined(ns, prevOut.Hash[:])
if v != nil {
// Ensure a credit exists for this
// unmined transaction before including
// the output script.
k := canonicalOutPoint(&prevOut.Hash, prevOut.Index)
vUC := existsRawUnminedCredit(ns, k)
if vUC == nil {
continue
}
// If we encounter an error here, it likely means
// we have a legacy outpoint. Ignore the error and
// just let the scrPos be 0, which will trigger
// whole transaction deserialization to retrieve
// the script.
scrPos := fetchRawUnminedCreditScriptOffset(vUC)
scrLen := fetchRawUnminedCreditScriptLength(vUC)
pkScript, err := fetchRawTxRecordPkScript(
prevOut.Hash[:], v, prevOut.Index, scrPos, scrLen)
if err != nil {
return nil, err
}
pkScripts = append(pkScripts, pkScript)
continue
}
_, credKey := existsUnspent(ns, prevOut)
if credKey != nil {
credVal := existsRawCredit(ns, credKey)
if credVal == nil {
return nil, storeError(apperrors.ErrDatabase, fmt.Sprintf("credit "+
"val for %x missing", credVal),
fmt.Errorf("no exists"))
}
// Legacy outputs in the credit bucket may be of the
// wrong size.
scrPos := fetchRawCreditScriptOffset(credVal)
scrLen := fetchRawCreditScriptLength(credVal)
k := extractRawCreditTxRecordKey(credKey)
v = existsRawTxRecord(ns, k)
pkScript, err := fetchRawTxRecordPkScript(k, v,
prevOut.Index, scrPos, scrLen)
if err != nil {
return nil, err
}
pkScripts = append(pkScripts, pkScript)
}
}
}
recKey := keyTxRecord(&rec.Hash, block)
it := makeReadDebitIterator(ns, recKey)
for it.next() {
credKey := extractRawDebitCreditKey(it.cv)
index := extractRawCreditIndex(credKey)
credVal := existsRawCredit(ns, credKey)
if credVal == nil {
return nil, storeError(apperrors.ErrDatabase, fmt.Sprintf("credit "+
"val for %x missing in debit itr", credVal),
fmt.Errorf("no exists"))
}
// Legacy credit output values may be of the wrong
// size.
scrPos := fetchRawCreditScriptOffset(credVal)
scrLen := fetchRawCreditScriptLength(credVal)
k := extractRawCreditTxRecordKey(credKey)
v := existsRawTxRecord(ns, k)
pkScript, err := fetchRawTxRecordPkScript(k, v, index,
scrPos, scrLen)
if err != nil {
return nil, err
}
pkScripts = append(pkScripts, pkScript)
}
if it.err != nil {
return nil, it.err
}
return pkScripts, nil
}