-
Notifications
You must be signed in to change notification settings - Fork 594
/
unconfirmed.go
222 lines (195 loc) · 6.81 KB
/
unconfirmed.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
// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wtxmgr
import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/walletdb"
)
// insertMemPoolTx inserts the unmined transaction record. It also marks
// previous outputs referenced by the inputs as spent.
func (s *Store) insertMemPoolTx(ns walletdb.ReadWriteBucket, rec *TxRecord) error {
// Check whether the transaction has already been added to the store,
// regardless of whether is has confirmed or not. This ensures that we
// don't add it to the unconfirmed bucket again if it has already
// confirmed.
//
// TODO: compare serialized txs to ensure this isn't a hash
// collision?
if txDetails, _ := s.TxDetails(ns, &rec.Hash); txDetails != nil {
return ErrDuplicateTx
}
// Since transaction records within the store are keyed by their
// transaction _and_ block confirmation, we'll iterate through the
// transaction's outputs to determine if we've already seen them to
// prevent from adding this transaction to the unconfirmed bucket.
for i := range rec.MsgTx.TxOut {
k := canonicalOutPoint(&rec.Hash, uint32(i))
if existsRawUnspent(ns, k) != nil {
return nil
}
}
log.Infof("Inserting unconfirmed transaction %v", rec.Hash)
v, err := valueTxRecord(rec)
if err != nil {
return err
}
err = putRawUnmined(ns, rec.Hash[:], v)
if err != nil {
return err
}
for _, input := range rec.MsgTx.TxIn {
prevOut := &input.PreviousOutPoint
k := canonicalOutPoint(&prevOut.Hash, prevOut.Index)
err = putRawUnminedInput(ns, k, rec.Hash[:])
if err != nil {
return err
}
}
// TODO: increment credit amount for each credit (but those are unknown
// here currently).
return nil
}
// removeDoubleSpends checks for any unmined transactions which would introduce
// a double spend if tx was added to the store (either as a confirmed or unmined
// transaction). Each conflicting transaction and all transactions which spend
// it are recursively removed.
func (s *Store) removeDoubleSpends(ns walletdb.ReadWriteBucket, rec *TxRecord) error {
for _, input := range rec.MsgTx.TxIn {
prevOut := &input.PreviousOutPoint
prevOutKey := canonicalOutPoint(&prevOut.Hash, prevOut.Index)
doubleSpendHashes := fetchUnminedInputSpendTxHashes(ns, prevOutKey)
for _, doubleSpendHash := range doubleSpendHashes {
// We'll make sure not to remove ourselves.
if rec.Hash == doubleSpendHash {
continue
}
// If the spending transaction spends multiple outputs
// from the same transaction, we'll find duplicate
// entries within the store, so it's possible we're
// unable to find it if the conflicts have already been
// removed in a previous iteration.
doubleSpendVal := existsRawUnmined(
ns, doubleSpendHash[:],
)
if doubleSpendVal == nil {
continue
}
var doubleSpend TxRecord
doubleSpend.Hash = doubleSpendHash
err := readRawTxRecord(
&doubleSpend.Hash, doubleSpendVal, &doubleSpend,
)
if err != nil {
return err
}
log.Debugf("Removing double spending transaction %v",
doubleSpend.Hash)
if err := s.removeConflict(ns, &doubleSpend); err != nil {
return err
}
}
}
return nil
}
// removeConflict removes an unmined transaction record and all spend chains
// deriving from it from the store. This is designed to remove transactions
// that would otherwise result in double spend conflicts if left in the store,
// and to remove transactions that spend coinbase transactions on reorgs.
func (s *Store) removeConflict(ns walletdb.ReadWriteBucket, rec *TxRecord) error {
// For each potential credit for this record, each spender (if any) must
// be recursively removed as well. Once the spenders are removed, the
// credit is deleted.
for i := range rec.MsgTx.TxOut {
k := canonicalOutPoint(&rec.Hash, uint32(i))
spenderHashes := fetchUnminedInputSpendTxHashes(ns, k)
for _, spenderHash := range spenderHashes {
// If the spending transaction spends multiple outputs
// from the same transaction, we'll find duplicate
// entries within the store, so it's possible we're
// unable to find it if the conflicts have already been
// removed in a previous iteration.
spenderVal := existsRawUnmined(ns, spenderHash[:])
if spenderVal == nil {
continue
}
var spender TxRecord
spender.Hash = spenderHash
err := readRawTxRecord(&spender.Hash, spenderVal, &spender)
if err != nil {
return err
}
log.Debugf("Transaction %v is part of a removed conflict "+
"chain -- removing as well", spender.Hash)
if err := s.removeConflict(ns, &spender); err != nil {
return err
}
}
if err := deleteRawUnminedCredit(ns, k); err != nil {
return err
}
}
// If this tx spends any previous credits (either mined or unmined), set
// each unspent. Mined transactions are only marked spent by having the
// output in the unmined inputs bucket.
for _, input := range rec.MsgTx.TxIn {
prevOut := &input.PreviousOutPoint
k := canonicalOutPoint(&prevOut.Hash, prevOut.Index)
err := deleteRawUnminedInput(ns, k, rec.Hash)
if err != nil {
return err
}
}
return deleteRawUnmined(ns, rec.Hash[:])
}
// UnminedTxs returns the underlying transactions for all unmined transactions
// which are not known to have been mined in a block. Transactions are
// guaranteed to be sorted by their dependency order.
func (s *Store) UnminedTxs(ns walletdb.ReadBucket) ([]*wire.MsgTx, error) {
recSet, err := s.unminedTxRecords(ns)
if err != nil {
return nil, err
}
txSet := make(map[chainhash.Hash]*wire.MsgTx, len(recSet))
for txHash, txRec := range recSet {
txSet[txHash] = &txRec.MsgTx
}
return DependencySort(txSet), nil
}
func (s *Store) unminedTxRecords(ns walletdb.ReadBucket) (map[chainhash.Hash]*TxRecord, error) {
unmined := make(map[chainhash.Hash]*TxRecord)
err := ns.NestedReadBucket(bucketUnmined).ForEach(func(k, v []byte) error {
var txHash chainhash.Hash
err := readRawUnminedHash(k, &txHash)
if err != nil {
return err
}
rec := new(TxRecord)
err = readRawTxRecord(&txHash, v, rec)
if err != nil {
return err
}
unmined[rec.Hash] = rec
return nil
})
return unmined, err
}
// UnminedTxHashes returns the hashes of all transactions not known to have been
// mined in a block.
func (s *Store) UnminedTxHashes(ns walletdb.ReadBucket) ([]*chainhash.Hash, error) {
return s.unminedTxHashes(ns)
}
func (s *Store) unminedTxHashes(ns walletdb.ReadBucket) ([]*chainhash.Hash, error) {
var hashes []*chainhash.Hash
err := ns.NestedReadBucket(bucketUnmined).ForEach(func(k, v []byte) error {
hash := new(chainhash.Hash)
err := readRawUnminedHash(k, hash)
if err == nil {
hashes = append(hashes, hash)
}
return err
})
return hashes, err
}