-
Notifications
You must be signed in to change notification settings - Fork 278
/
txdb.js
4025 lines (3183 loc) · 86.1 KB
/
txdb.js
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* txdb.js - persistent transaction pool
* Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
* https://github.com/handshake-org/hsd
*/
'use strict';
const assert = require('bsert');
const bio = require('bufio');
const {BufferSet} = require('buffer-map');
const util = require('../utils/util');
const Amount = require('../ui/amount');
const CoinView = require('../coins/coinview');
const Coin = require('../primitives/coin');
const Outpoint = require('../primitives/outpoint');
const records = require('./records');
const layout = require('./layout').txdb;
const consensus = require('../protocol/consensus');
const policy = require('../protocol/policy');
const rules = require('../covenants/rules');
const NameState = require('../covenants/namestate');
const NameUndo = require('../covenants/undo');
const {TXRecord} = records;
const {types} = rules;
/*
* Constants
*/
const EMPTY = Buffer.alloc(0);
/**
* TXDB
* @alias module:wallet.TXDB
*/
class TXDB {
/**
* Create a TXDB.
* @constructor
* @param {WalletDB} wdb
*/
constructor(wdb, wid) {
this.wdb = wdb;
this.db = wdb.db;
this.logger = wdb.logger;
this.wid = wid || 0;
this.bucket = null;
this.wallet = null;
this.locked = new BufferSet();
}
/**
* Open TXDB.
* @returns {Promise}
*/
async open(wallet) {
const prefix = layout.prefix.encode(wallet.wid);
this.wid = wallet.wid;
this.bucket = this.db.bucket(prefix);
this.wallet = wallet;
}
/**
* Emit transaction event.
* @private
* @param {String} event
* @param {Object} data
* @param {Details} details
*/
emit(event, data, details) {
this.wdb.emit(event, this.wallet, data, details);
this.wallet.emit(event, data, details);
}
/**
* Get wallet path for output.
* @param {Output} output
* @returns {Promise} - Returns {@link Path}.
*/
getPath(output) {
const hash = output.getHash();
if (!hash)
return null;
return this.wdb.getPath(this.wid, hash);
}
/**
* Test whether path exists for output.
* @param {Output} output
* @returns {Promise} - Returns Boolean.
*/
hasPath(output) {
const hash = output.getHash();
if (!hash)
return false;
return this.wdb.hasPath(this.wid, hash);
}
/**
* Save credit.
* @param {Credit} credit
* @param {Path} path
*/
async saveCredit(b, credit, path) {
const {coin} = credit;
b.put(layout.c.encode(coin.hash, coin.index), credit.encode());
b.put(layout.C.encode(path.account, coin.hash, coin.index), null);
return this.addOutpointMap(b, coin.hash, coin.index);
}
/**
* Remove credit.
* @param {Credit} credit
* @param {Path} path
*/
async removeCredit(b, credit, path) {
const {coin} = credit;
b.del(layout.c.encode(coin.hash, coin.index));
b.del(layout.C.encode(path.account, coin.hash, coin.index));
return this.removeOutpointMap(b, coin.hash, coin.index);
}
/**
* Spend credit.
* @param {Credit} credit
* @param {TX} tx
* @param {Number} index
*/
spendCredit(b, credit, tx, index) {
const prevout = tx.inputs[index].prevout;
const spender = Outpoint.fromTX(tx, index);
b.put(layout.s.encode(prevout.hash, prevout.index), spender.encode());
b.put(layout.d.encode(spender.hash, spender.index), credit.coin.encode());
}
/**
* Unspend credit.
* @param {TX} tx
* @param {Number} index
*/
unspendCredit(b, tx, index) {
const prevout = tx.inputs[index].prevout;
const spender = Outpoint.fromTX(tx, index);
b.del(layout.s.encode(prevout.hash, prevout.index));
b.del(layout.d.encode(spender.hash, spender.index));
}
/**
* Write input record.
* @param {TX} tx
* @param {Number} index
*/
async writeInput(b, tx, index) {
const prevout = tx.inputs[index].prevout;
const spender = Outpoint.fromTX(tx, index);
b.put(layout.s.encode(prevout.hash, prevout.index), spender.encode());
return this.addOutpointMap(b, prevout.hash, prevout.index);
}
/**
* Remove input record.
* @param {TX} tx
* @param {Number} index
*/
async removeInput(b, tx, index) {
const prevout = tx.inputs[index].prevout;
b.del(layout.s.encode(prevout.hash, prevout.index));
return this.removeOutpointMap(b, prevout.hash, prevout.index);
}
/**
* Update wallet balance.
* @param {BalanceDelta} state
*/
async updateBalance(b, state) {
const balance = await this.getWalletBalance();
state.applyTo(balance);
b.put(layout.R.encode(), balance.encode());
return balance;
}
/**
* Update account balance.
* @param {Number} acct
* @param {Balance} delta
*/
async updateAccountBalance(b, acct, delta) {
const balance = await this.getAccountBalance(acct);
delta.applyTo(balance);
b.put(layout.r.encode(acct), balance.encode());
return balance;
}
/**
* Test a whether a coin has been spent.
* @param {Hash} hash
* @param {Number} index
* @returns {Promise} - Returns Boolean.
*/
async getSpent(hash, index) {
const data = await this.bucket.get(layout.s.encode(hash, index));
if (!data)
return null;
return Outpoint.decode(data);
}
/**
* Test a whether a coin has been spent.
* @param {Hash} hash
* @param {Number} index
* @returns {Promise} - Returns Boolean.
*/
isSpent(hash, index) {
return this.bucket.has(layout.s.encode(hash, index));
}
/**
* Append to global map.
* @param {Number} height
* @returns {Promise}
*/
addBlockMap(b, height) {
return this.wdb.addBlockMap(b.root(), height, this.wid);
}
/**
* Remove from global map.
* @param {Number} height
* @returns {Promise}
*/
removeBlockMap(b, height) {
return this.wdb.removeBlockMap(b.root(), height, this.wid);
}
/**
* Append to global map.
* @param {Hash} hash
* @returns {Promise}
*/
addTXMap(b, hash) {
return this.wdb.addTXMap(b.root(), hash, this.wid);
}
/**
* Remove from global map.
* @param {Hash} hash
* @returns {Promise}
*/
removeTXMap(b, hash) {
return this.wdb.removeTXMap(b.root(), hash, this.wid);
}
/**
* Append to global map.
* @param {Hash} hash
* @param {Number} index
* @returns {Promise}
*/
addOutpointMap(b, hash, index) {
return this.wdb.addOutpointMap(b.root(), hash, index, this.wid);
}
/**
* Remove from global map.
* @param {Hash} hash
* @param {Number} index
* @returns {Promise}
*/
removeOutpointMap(b, hash, index) {
return this.wdb.removeOutpointMap(b.root(), hash, index, this.wid);
}
/**
* Append to global map.
* @param {Hash} hash
* @param {Number} index
* @returns {Promise}
*/
addNameMap(b, nameHash) {
return this.wdb.addNameMap(b.root(), nameHash, this.wid);
}
/**
* Remove from global map.
* @param {Hash} hash
* @param {Number} index
* @returns {Promise}
*/
removeNameMap(b, nameHash) {
return this.wdb.removeNameMap(b.root(), nameHash, this.wid);
}
/**
* List block records.
* @returns {Promise}
*/
getBlocks() {
return this.bucket.keys({
gte: layout.b.min(),
lte: layout.b.max(),
parse: key => layout.b.decode(key)[0]
});
}
/**
* Get block record.
* @param {Number} height
* @returns {Promise}
*/
async getBlock(height) {
const data = await this.bucket.get(layout.b.encode(height));
if (!data)
return null;
return BlockRecord.decode(data);
}
/**
* Append to the global block record.
* @param {Hash} hash
* @param {BlockMeta} block
* @returns {Promise}
*/
async addBlock(b, hash, block) {
const key = layout.b.encode(block.height);
const data = await this.bucket.get(key);
if (!data) {
const blk = BlockRecord.fromMeta(block);
blk.add(hash);
b.put(key, blk.encode());
return;
}
const raw = Buffer.allocUnsafe(data.length + 32);
data.copy(raw, 0);
const size = raw.readUInt32LE(40, true);
raw.writeUInt32LE(size + 1, 40, true);
hash.copy(raw, data.length);
b.put(key, raw);
}
/**
* Remove from the global block record.
* @param {Hash} hash
* @param {Number} height
* @returns {Promise}
*/
async removeBlock(b, hash, height) {
const key = layout.b.encode(height);
const data = await this.bucket.get(key);
if (!data)
return;
const size = data.readUInt32LE(40, true);
assert(size > 0);
assert(data.slice(-32).equals(hash));
if (size === 1) {
b.del(key);
return;
}
const raw = data.slice(0, -32);
raw.writeUInt32LE(size - 1, 40, true);
b.put(key, raw);
}
/**
* Remove from the global block record.
* @param {Hash} hash
* @param {Number} height
* @returns {Promise}
*/
async spliceBlock(b, hash, height) {
const block = await this.getBlock(height);
if (!block)
return;
if (!block.remove(hash))
return;
if (block.hashes.size === 0) {
b.del(layout.b.encode(height));
return;
}
b.put(layout.b.encode(height), block.encode());
}
/**
* Test whether we have a name.
* @param {Buffer} nameHash
* @returns {Boolean}
*/
async hasNameState(nameHash) {
return this.bucket.has(layout.A.encode(nameHash));
}
/**
* Get a name state if present.
* @param {Buffer} nameHash
* @returns {NameState}
*/
async getNameState(nameHash) {
const raw = await this.bucket.get(layout.A.encode(nameHash));
if (!raw)
return null;
const ns = NameState.decode(raw);
ns.nameHash = nameHash;
return ns;
}
/**
* Get all names.
* @returns {NameState[]}
*/
async getNames() {
const iter = this.bucket.iterator({
gte: layout.A.min(),
lte: layout.A.max(),
values: true
});
const names = [];
await iter.each((key, raw) => {
const [nameHash] = layout.A.decode(key);
const ns = NameState.decode(raw);
ns.nameHash = nameHash;
names.push(ns);
});
return names;
}
/**
* Test whether we have a bid.
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
* @returns {Boolean}
*/
async hasBid(nameHash, outpoint) {
const {hash, index} = outpoint;
return this.bucket.has(layout.i.encode(nameHash, hash, index));
}
/**
* Get a bid if present.
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
* @returns {BlindBid}
*/
async getBid(nameHash, outpoint) {
const {hash, index} = outpoint;
const raw = await this.bucket.get(layout.i.encode(nameHash, hash, index));
if (!raw)
return null;
const bb = BlindBid.decode(raw);
bb.nameHash = nameHash;
bb.prevout = outpoint;
return bb;
}
/**
* Write a bid.
* @param {Object} b
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
* @param {Object} options
*/
putBid(b, nameHash, outpoint, options) {
const {hash, index} = outpoint;
const bb = new BlindBid();
bb.nameHash = nameHash;
bb.name = options.name;
bb.lockup = options.lockup;
bb.blind = options.blind;
bb.own = options.own;
b.put(layout.i.encode(nameHash, hash, index), bb.encode());
}
/**
* Delete a bid.
* @param {Object} b
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
*/
removeBid(b, nameHash, outpoint) {
const {hash, index} = outpoint;
b.del(layout.i.encode(nameHash, hash, index));
}
/**
* Get all bids for name.
* @param {Buffer} nameHash
* @returns {BlindBid[]}
*/
async getBids(nameHash) {
const iter = this.bucket.iterator({
gte: nameHash ? layout.i.min(nameHash) : layout.i.min(),
lte: nameHash ? layout.i.max(nameHash) : layout.i.max(),
values: true
});
const bids = [];
await iter.each(async (key, raw) => {
const [nameHash, hash, index] = layout.i.decode(key);
const bb = BlindBid.decode(raw);
bb.nameHash = nameHash;
bb.prevout = new Outpoint(hash, index);
const bv = await this.getBlind(bb.blind);
if (bv)
bb.value = bv.value;
bids.push(bb);
});
return bids;
}
/**
* Remove all bids for name.
* @param {Buffer} nameHash
*/
async removeBids(b, nameHash) {
const iter = this.bucket.iterator({
gte: layout.i.min(nameHash),
lte: layout.i.max(nameHash)
});
await iter.each(k => b.del(k));
}
/**
* Test whether we have a reveal.
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
* @returns {Boolean}
*/
async hasReveal(nameHash, outpoint) {
const {hash, index} = outpoint;
return this.bucket.has(layout.B.encode(nameHash, hash, index));
}
/**
* Get a reveal if present.
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
* @returns {BidReveal}
*/
async getReveal(nameHash, outpoint) {
const {hash, index} = outpoint;
const raw = await this.bucket.get(layout.B.encode(nameHash, hash, index));
if (!raw)
return null;
const brv = BidReveal.decode(raw);
brv.nameHash = nameHash;
brv.prevout = outpoint;
return brv;
}
/**
* Write a reveal.
* @param {Object} b
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
* @param {Object} options
*/
putReveal(b, nameHash, outpoint, options) {
const {hash, index} = outpoint;
const brv = new BidReveal();
brv.nameHash = nameHash;
brv.name = options.name;
brv.value = options.value;
brv.height = options.height;
brv.own = options.own;
b.put(layout.B.encode(nameHash, hash, index), brv.encode());
}
/**
* Delete a reveal.
* @param {Object} b
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
*/
removeReveal(b, nameHash, outpoint) {
const {hash, index} = outpoint;
b.del(layout.B.encode(nameHash, hash, index));
}
/**
* Get all reveals by name.
* @param {Buffer} nameHash
* @returns {BidReveal[]}
*/
async getReveals(nameHash) {
const iter = this.bucket.iterator({
gte: nameHash ? layout.B.min(nameHash) : layout.B.min(),
lte: nameHash ? layout.B.max(nameHash) : layout.B.max(),
values: true
});
const reveals = [];
await iter.each(async (key, raw) => {
const [nameHash, hash, index] = layout.B.decode(key);
const brv = BidReveal.decode(raw);
brv.nameHash = nameHash;
brv.prevout = new Outpoint(hash, index);
reveals.push(brv);
});
return reveals;
}
/**
* Remove all reveals by name.
* @param {Object} b
* @param {Buffer} nameHash
*/
async removeReveals(b, nameHash) {
const iter = this.bucket.iterator({
gte: layout.B.min(nameHash),
lte: layout.B.max(nameHash)
});
await iter.each(k => b.del(k));
}
/**
* Test whether a blind value is present.
* @param {Buffer} blind - Blind hash.
* @returns {Boolean}
*/
async hasBlind(blind) {
return this.bucket.has(layout.v.encode(blind));
}
/**
* Get a blind value if present.
* @param {Buffer} blind - Blind hash.
* @returns {BlindValue}
*/
async getBlind(blind) {
const raw = await this.bucket.get(layout.v.encode(blind));
if (!raw)
return null;
return BlindValue.decode(raw);
}
/**
* Write a blind value.
* @param {Object} b
* @param {Buffer} blind
* @param {Object} options
*/
putBlind(b, blind, options) {
const {value, nonce} = options;
const bv = new BlindValue();
bv.value = value;
bv.nonce = nonce;
b.put(layout.v.encode(blind), bv.encode());
}
/**
* Save blind value.
* @param {Buffer} blind
* @param {Object} options
*/
async saveBlind(blind, options) {
const b = this.bucket.batch();
this.putBlind(b, blind, options);
await b.write();
}
/**
* Delete a blind value.
* @param {Object} b
* @param {Buffer} blind
*/
removeBlind(b, blind) {
b.del(layout.v.encode(blind));
}
/**
* Add transaction without a batch.
* @private
* @param {TX} tx
* @returns {Promise}
*/
async add(tx, block) {
const hash = tx.hash();
const existing = await this.getTX(hash);
assert(!tx.mutable, 'Cannot add mutable TX to wallet.');
if (existing) {
// Existing tx is already confirmed. Ignore.
if (existing.height !== -1)
return null;
// The incoming tx won't confirm the
// existing one anyway. Ignore.
if (!block)
return null;
// Confirm transaction.
return this.confirm(existing, block);
}
const wtx = TXRecord.fromTX(tx, block);
if (!block) {
// Potentially remove double-spenders.
// Only remove if they're not confirmed.
if (!await this.removeConflicts(tx, true))
return null;
if (await this.isDoubleOpen(tx))
return null;
} else {
// Potentially remove double-spenders.
await this.removeConflicts(tx, false);
await this.removeDoubleOpen(tx);
}
// Finally we can do a regular insertion.
return this.insert(wtx, block);
}
/**
* Test whether the transaction
* has a duplicate open.
* @param {TX}
* @returns {Boolean}
*/
async isDoubleOpen(tx) {
for (const {covenant} of tx.outputs) {
if (!covenant.isOpen())
continue;
const nameHash = covenant.getHash(0);
const key = layout.o.encode(nameHash);
const hash = await this.bucket.get(key);
// Allow a double open if previous auction period has expired
// this is not a complete check for name availability or status!
if (hash) {
const names = this.wdb.network.names;
const period = names.biddingPeriod + names.revealPeriod;
const oldTX = await this.getTX(hash);
if (oldTX.height !== -1 && oldTX.height + period < this.wdb.height)
return false;
return true;
}
}
return false;
}
/**
* Remove duplicate opens.
* @private
* @param {TX} tx
*/
async removeDoubleOpen(tx) {
for (const {covenant} of tx.outputs) {
if (!covenant.isOpen())
continue;
const nameHash = covenant.getHash(0);
const key = layout.o.encode(nameHash);
const hash = await this.bucket.get(key);
if (!hash)
continue;
await this.remove(hash);
}
}
/**
* Index open covenants.
* @private
* @param {Batch} b
* @param {TX} tx
*/
indexOpens(b, tx) {
for (const {covenant} of tx.outputs) {
if (!covenant.isOpen())
continue;
const nameHash = covenant.getHash(0);
const key = layout.o.encode(nameHash);
b.put(key, tx.hash());
}
}
/**
* Unindex open covenants.
* @private
* @param {Batch} b
* @param {TX} tx
*/
unindexOpens(b, tx) {
for (const {covenant} of tx.outputs) {
if (!covenant.isOpen())
continue;
const nameHash = covenant.getHash(0);
const key = layout.o.encode(nameHash);
b.del(key);
}
}
/**
* Insert transaction.
* @private
* @param {TXRecord} wtx
* @param {BlockMeta} block
* @returns {Promise}
*/
async insert(wtx, block) {
const b = this.bucket.batch();
const {tx, hash} = wtx;
const height = block ? block.height : -1;
const details = new Details(wtx, block);
const state = new BalanceDelta();
const view = new CoinView();
let own = false;
if (!tx.isCoinbase()) {
// We need to potentially spend some coins here.
for (let i = 0; i < tx.inputs.length; i++) {
const input = tx.inputs[i];
const {hash, index} = input.prevout;
const credit = await this.getCredit(hash, index);
if (!credit) {
// Watch all inputs for incoming txs.
// This allows us to check for double spends.
if (!block)
await this.writeInput(b, tx, i);
continue;
}
const coin = credit.coin;
const path = await this.getPath(coin);
assert(path);
// Build the tx details object
// as we go, for speed.
details.setInput(i, path, coin);
// Write an undo coin for the credit
// and add it to the stxo set.
this.spendCredit(b, credit, tx, i);
// Unconfirmed balance should always
// be updated as it reflects the on-chain
// balance _and_ mempool balance assuming
// everything in the mempool were to confirm.
state.tx(path, 1);
state.coin(path, -1);
state.unconfirmed(path, -coin.value);
// FINALIZE is a special case: locked coins _leave_ the wallet.
if (tx.output(i) && tx.covenant(i).isFinalize()) {
if (!block) {
state.ulocked(path, -tx.outputs[i].value);
} else {
state.clocked(path, -tx.outputs[i].value);
// This is the first time we've seen this tx and it is in a block
// (probably from a rescan). Update unconfirmed locked balance also.
state.ulocked(path, -tx.outputs[i].value);
}
}
if (!block) {
// If the tx is not mined, we do not
// disconnect the coin, we simply mark
// a `spent` flag on the credit. This
// effectively prevents the mempool
// from altering our utxo state
// permanently. It also makes it
// possible to compare the on-chain
// state vs. the mempool state.
credit.spent = true;
await this.saveCredit(b, credit, path);
} else {
// If the tx is mined, we can safely
// remove the coin being spent. This
// coin will be indexed as an undo
// coin so it can be reconnected
// later during a reorg.
state.confirmed(path, -coin.value);
await this.removeCredit(b, credit, path);
view.addCoin(coin);
}
own = true;
}
}
// Potentially add coins to the utxo set.