forked from OpenBazaar/spvwallet
-
Notifications
You must be signed in to change notification settings - Fork 6
/
txstore.go
470 lines (436 loc) · 11.5 KB
/
txstore.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
// Copyright (C) 2015-2016 The Lightning Network Developers
// Copyright (c) 2016-2017 The OpenBazaar Developers
package spvwallet
import (
"bytes"
"errors"
"sync"
"time"
"github.com/phoreproject/btcd/blockchain"
"github.com/phoreproject/btcd/chaincfg"
"github.com/phoreproject/btcd/chaincfg/chainhash"
"github.com/phoreproject/btcd/txscript"
"github.com/phoreproject/btcd/wire"
"github.com/phoreproject/btcutil"
"github.com/phoreproject/btcutil/bloom"
"github.com/phoreproject/wallet-interface"
)
type TxStore struct {
adrs []btcutil.Address
watchedScripts [][]byte
txids map[string]int32
addrMutex *sync.Mutex
cbMutex *sync.Mutex
keyManager *KeyManager
params *chaincfg.Params
listeners []func(wallet.TransactionCallback)
wallet.Datastore
}
func NewTxStore(p *chaincfg.Params, db wallet.Datastore, keyManager *KeyManager) (*TxStore, error) {
txs := &TxStore{
params: p,
keyManager: keyManager,
addrMutex: new(sync.Mutex),
cbMutex: new(sync.Mutex),
txids: make(map[string]int32),
Datastore: db,
}
err := txs.PopulateAdrs()
if err != nil {
return nil, err
}
return txs, nil
}
// ... or I'm gonna fade away
func (ts *TxStore) GimmeFilter() (*bloom.Filter, error) {
ts.PopulateAdrs()
// get all utxos to add outpoints to filter
allUtxos, err := ts.Utxos().GetAll()
if err != nil {
return nil, err
}
allStxos, err := ts.Stxos().GetAll()
if err != nil {
return nil, err
}
ts.addrMutex.Lock()
elem := uint32(len(ts.adrs)+len(allUtxos)+len(allStxos)) + uint32(len(ts.watchedScripts))
f := bloom.NewFilter(elem, 0, 0.00003, wire.BloomUpdateAll)
// note there could be false positives since we're just looking
// for the 20 byte PKH without the opcodes.
for _, a := range ts.adrs { // add 20-byte pubkeyhash
f.Add(a.ScriptAddress())
}
ts.addrMutex.Unlock()
for _, u := range allUtxos {
f.AddOutPoint(&u.Op)
}
for _, s := range allStxos {
f.AddOutPoint(&s.Utxo.Op)
}
for _, w := range ts.watchedScripts {
_, addrs, _, err := txscript.ExtractPkScriptAddrs(w, ts.params)
if err != nil {
continue
}
f.Add(addrs[0].ScriptAddress())
}
return f, nil
}
// GetDoubleSpends takes a transaction and compares it with
// all transactions in the db. It returns a slice of all txids in the db
// which are double spent by the received tx.
func (ts *TxStore) CheckDoubleSpends(argTx *wire.MsgTx) ([]*chainhash.Hash, error) {
var dubs []*chainhash.Hash // slice of all double-spent txs
argTxid := argTx.TxHash()
txs, err := ts.Txns().GetAll(true)
if err != nil {
return dubs, err
}
for _, compTx := range txs {
if compTx.Height < 0 {
continue
}
r := bytes.NewReader(compTx.Bytes)
msgTx := wire.NewMsgTx(1)
msgTx.BtcDecode(r, 1, wire.WitnessEncoding)
compTxid := msgTx.TxHash()
for _, argIn := range argTx.TxIn {
// iterate through inputs of compTx
for _, compIn := range msgTx.TxIn {
if outPointsEqual(argIn.PreviousOutPoint, compIn.PreviousOutPoint) && !compTxid.IsEqual(&argTxid) {
// found double spend
dubs = append(dubs, &compTxid)
break // back to argIn loop
}
}
}
}
return dubs, nil
}
// GetPendingInv returns an inv message containing all txs known to the
// db which are at height 0 (not known to be confirmed).
// This can be useful on startup or to rebroadcast unconfirmed txs.
func (ts *TxStore) GetPendingInv() (*wire.MsgInv, error) {
// use a map (really a set) do avoid dupes
txidMap := make(map[chainhash.Hash]struct{})
utxos, err := ts.Utxos().GetAll() // get utxos from db
if err != nil {
return nil, err
}
stxos, err := ts.Stxos().GetAll() // get stxos from db
if err != nil {
return nil, err
}
// iterate through utxos, adding txids of anything with height 0
for _, utxo := range utxos {
if utxo.AtHeight == 0 {
txidMap[utxo.Op.Hash] = struct{}{} // adds to map
}
}
// do the same with stxos based on height at which spent
for _, stxo := range stxos {
if stxo.SpendHeight == 0 {
txidMap[stxo.SpendTxid] = struct{}{}
}
}
invMsg := wire.NewMsgInv()
for txid := range txidMap {
item := wire.NewInvVect(wire.InvTypeTx, &txid)
err = invMsg.AddInvVect(item)
if err != nil {
return nil, err
}
}
// return inv message with all txids (maybe none)
return invMsg, nil
}
// PopulateAdrs just puts a bunch of adrs in ram; it doesn't touch the DB
func (ts *TxStore) PopulateAdrs() error {
keys := ts.keyManager.GetKeys()
ts.addrMutex.Lock()
ts.adrs = []btcutil.Address{}
for _, k := range keys {
addr, err := k.Address(ts.params)
if err != nil {
continue
}
ts.adrs = append(ts.adrs, addr)
}
ts.watchedScripts, _ = ts.WatchedScripts().GetAll()
txns, _ := ts.Txns().GetAll(true)
for _, t := range txns {
ts.txids[t.Txid] = t.Height
}
ts.addrMutex.Unlock()
return nil
}
// Ingest puts a tx into the DB atomically. This can result in a
// gain, a loss, or no result. Gain or loss in satoshis is returned.
func (ts *TxStore) Ingest(tx *wire.MsgTx, height int32) (uint32, error) {
var hits uint32
var err error
// Tx has been OK'd by SPV; check tx sanity
utilTx := btcutil.NewTx(tx) // convert for validation
// Checks basic stuff like there are inputs and ouputs
err = blockchain.CheckTransactionSanity(utilTx)
if err != nil {
return hits, err
}
// Check to see if we've already processed this tx. If so, return.
sh, ok := ts.txids[tx.TxHash().String()]
if ok && (sh > 0 || (sh == 0 && height == 0)) {
return 1, nil
}
// Check to see if this is a double spend
doubleSpends, err := ts.CheckDoubleSpends(tx)
if err != nil {
return hits, err
}
if len(doubleSpends) > 0 {
// First seen rule
if height == 0 {
return 0, nil
} else {
// Mark any unconfirmed doubles as dead
for _, double := range doubleSpends {
ts.markAsDead(*double)
}
}
}
// Generate PKscripts for all addresses
ts.addrMutex.Lock()
PKscripts := make([][]byte, len(ts.adrs))
for i := range ts.adrs {
// Iterate through all our addresses
// TODO: This will need to test both segwit and legacy once segwit activates
PKscripts[i], err = txscript.PayToAddrScript(ts.adrs[i])
if err != nil {
return hits, err
}
}
ts.addrMutex.Unlock()
// Iterate through all outputs of this tx, see if we gain
cachedSha := tx.TxHash()
cb := wallet.TransactionCallback{Txid: cachedSha.String(), Height: height}
value := int64(0)
matchesWatchOnly := false
for i, txout := range tx.TxOut {
out := wallet.TransactionOutput{ScriptPubKey: txout.PkScript, Value: txout.Value, Index: uint32(i)}
for _, script := range PKscripts {
if bytes.Equal(txout.PkScript, script) { // new utxo found
scriptAddress, _ := ts.extractScriptAddress(txout.PkScript)
ts.keyManager.MarkKeyAsUsed(scriptAddress)
newop := wire.OutPoint{
Hash: cachedSha,
Index: uint32(i),
}
newu := wallet.Utxo{
AtHeight: height,
Value: txout.Value,
ScriptPubkey: txout.PkScript,
Op: newop,
WatchOnly: false,
}
value += newu.Value
ts.Utxos().Put(newu)
hits++
break
}
}
// Now check watched scripts
for _, script := range ts.watchedScripts {
if bytes.Equal(txout.PkScript, script) {
newop := wire.OutPoint{
Hash: cachedSha,
Index: uint32(i),
}
newu := wallet.Utxo{
AtHeight: height,
Value: txout.Value,
ScriptPubkey: txout.PkScript,
Op: newop,
WatchOnly: true,
}
ts.Utxos().Put(newu)
matchesWatchOnly = true
}
}
cb.Outputs = append(cb.Outputs, out)
}
utxos, err := ts.Utxos().GetAll()
if err != nil {
return 0, err
}
for _, txin := range tx.TxIn {
for i, u := range utxos {
if outPointsEqual(txin.PreviousOutPoint, u.Op) {
st := wallet.Stxo{
Utxo: u,
SpendHeight: height,
SpendTxid: cachedSha,
}
ts.Stxos().Put(st)
ts.Utxos().Delete(u)
utxos = append(utxos[:i], utxos[i+1:]...)
if !u.WatchOnly {
value -= u.Value
hits++
} else {
matchesWatchOnly = true
}
in := wallet.TransactionInput{
OutpointHash: u.Op.Hash.CloneBytes(),
OutpointIndex: u.Op.Index,
LinkedScriptPubKey: u.ScriptPubkey,
Value: u.Value,
}
cb.Inputs = append(cb.Inputs, in)
break
}
}
}
// Update height of any stxos
if height > 0 {
stxos, err := ts.Stxos().GetAll()
if err != nil {
return 0, err
}
for _, stxo := range stxos {
if stxo.SpendTxid.IsEqual(&cachedSha) {
stxo.SpendHeight = height
ts.Stxos().Put(stxo)
if !stxo.Utxo.WatchOnly {
hits++
} else {
matchesWatchOnly = true
}
break
}
}
}
// If hits is nonzero it's a relevant tx and we should store it
if hits > 0 || matchesWatchOnly {
ts.cbMutex.Lock()
txn, err := ts.Txns().Get(tx.TxHash())
shouldCallback := false
if err != nil {
cb.Value = value
txn.Timestamp = time.Now()
shouldCallback = true
var buf bytes.Buffer
tx.BtcEncode(&buf, wire.ProtocolVersion, wire.WitnessEncoding)
ts.Txns().Put(buf.Bytes(), tx.TxHash().String(), int(value), int(height), txn.Timestamp, hits == 0)
ts.txids[tx.TxHash().String()] = height
}
// Let's check the height before committing so we don't allow rogue peers to send us a lose
// tx that resets our height to zero.
if txn.Height <= 0 {
ts.Txns().UpdateHeight(tx.TxHash(), int(height), txn.Timestamp)
ts.txids[tx.TxHash().String()] = height
if height > 0 {
cb.Value = txn.Value
shouldCallback = true
}
}
if shouldCallback {
// Callback on listeners
for _, listener := range ts.listeners {
listener(cb)
}
}
ts.cbMutex.Unlock()
ts.PopulateAdrs()
hits++
}
return hits, err
}
func (ts *TxStore) markAsDead(txid chainhash.Hash) error {
stxos, err := ts.Stxos().GetAll()
if err != nil {
return err
}
markStxoAsDead := func(s wallet.Stxo) error {
err := ts.Stxos().Delete(s)
if err != nil {
return err
}
err = ts.Txns().UpdateHeight(s.SpendTxid, -1, time.Now())
if err != nil {
return err
}
return nil
}
for _, s := range stxos {
// If an stxo is marked dead, move it back into the utxo table
if txid.IsEqual(&s.SpendTxid) {
if err := markStxoAsDead(s); err != nil {
return err
}
if err := ts.Utxos().Put(s.Utxo); err != nil {
return err
}
}
// If a dependency of the spend is dead then mark the spend as dead
if txid.IsEqual(&s.Utxo.Op.Hash) {
if err := markStxoAsDead(s); err != nil {
return err
}
if err := ts.markAsDead(s.SpendTxid); err != nil {
return err
}
}
}
utxos, err := ts.Utxos().GetAll()
if err != nil {
return err
}
// Dead utxos should just be deleted
for _, u := range utxos {
if txid.IsEqual(&u.Op.Hash) {
err := ts.Utxos().Delete(u)
if err != nil {
return err
}
}
}
ts.Txns().UpdateHeight(txid, -1, time.Now())
return nil
}
func (ts *TxStore) processReorg(lastGoodHeight uint32) error {
txns, err := ts.Txns().GetAll(true)
if err != nil {
return err
}
for i := len(txns) - 1; i >= 0; i-- {
if txns[i].Height > int32(lastGoodHeight) {
txid, err := chainhash.NewHashFromStr(txns[i].Txid)
if err != nil {
log.Error(err)
continue
}
err = ts.markAsDead(*txid)
if err != nil {
log.Error(err)
continue
}
}
}
return nil
}
func (ts *TxStore) extractScriptAddress(script []byte) ([]byte, error) {
_, addrs, _, err := txscript.ExtractPkScriptAddrs(script, ts.params)
if err != nil {
return nil, err
}
if len(addrs) == 0 {
return nil, errors.New("unknown script")
}
return addrs[0].ScriptAddress(), nil
}
func outPointsEqual(a, b wire.OutPoint) bool {
if !a.Hash.IsEqual(&b.Hash) {
return false
}
return a.Index == b.Index
}