-
Notifications
You must be signed in to change notification settings - Fork 17
/
unspent.go
801 lines (619 loc) · 18.6 KB
/
unspent.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
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
package transactions
import (
"bytes"
"encoding/gob"
"encoding/hex"
"errors"
"log"
"sort"
"github.com/gelembjuk/oursql/lib/remoteclient"
"github.com/gelembjuk/oursql/lib/utils"
"github.com/gelembjuk/oursql/node/blockchain"
"github.com/gelembjuk/oursql/node/database"
"github.com/gelembjuk/oursql/node/structures"
)
// unspentTransactions represents UTXO set
type unspentTransactions struct {
DB database.DBManager
Logger *utils.LoggerMan
}
// Create transaction index object
func (u unspentTransactions) newTransactionIndex() *transactionsIndex {
return newTransactionIndex(u.DB, u.Logger)
}
// Serialize. We need this to store data in DB in bytes
func (u unspentTransactions) serializeOutputs(outs []structures.TXOutputIndependent) ([]byte, error) {
var buff bytes.Buffer
enc := gob.NewEncoder(&buff)
err := enc.Encode(outs)
if err != nil {
return nil, err
}
return buff.Bytes(), nil
}
/*
* Deserialize data from bytes loaded fom DB
*/
func (u unspentTransactions) deserializeOutputs(data []byte) ([]structures.TXOutputIndependent, error) {
var outputs []structures.TXOutputIndependent
dec := gob.NewDecoder(bytes.NewReader(data))
err := dec.Decode(&outputs)
if err != nil {
return nil, err
log.Panic(err)
}
return outputs, nil
}
/*
* Calculates address balance using the cache of unspent transactions outputs
*/
func (u unspentTransactions) GetAddressBalance(address string) (float64, error) {
if address == "" {
return 0, errors.New("Address is missed")
}
w := remoteclient.Wallet{}
if !w.ValidateAddress(address) {
return 0, errors.New("Address is not valid")
}
balance := float64(0)
UnspentTXs, err2 := u.GetunspentTransactionsOutputs(address)
if err2 != nil {
return 0, err2
}
for _, out := range UnspentTXs {
balance += out.Value
}
return balance, nil
}
// Get input value. Input is unspent TX output
func (u unspentTransactions) GetInputValue(input structures.TXCurrencyInput) (float64, error) {
uodb, err := u.DB.GetUnspentOutputsObject()
if err != nil {
return 0, err
}
outsBytes, err := uodb.GetDataForTransaction(input.Txid)
if err != nil {
u.Logger.Trace.Printf("Data reading error: %s", err.Error())
return 0, err
}
if outsBytes == nil {
return 0, NewTXNotFoundUOTError("Input TX is not found in unspent outputs")
}
outs, err := u.deserializeOutputs(outsBytes)
if err != nil {
return 0, err
}
for _, o := range outs {
if o.OIndex == input.Vout {
return o.Value, nil
}
}
return 0, errors.New("Output index is not found in unspent outputs")
}
// Choose inputs for new transaction
func (u unspentTransactions) ChooseSpendableOutputs(pubKeyHash []byte, amount float64,
pendinguse []structures.TXCurrencyInput) (float64, []structures.TXOutputIndependent, error) {
uodb, err := u.DB.GetUnspentOutputsObject()
if err != nil {
return 0, nil, err
}
unspentOutputs := []structures.TXOutputIndependent{}
accumulated := float64(0)
err = uodb.ForEach(func(txID, txData []byte) error {
outs, err := u.deserializeOutputs(txData)
if err != nil {
return err
}
for _, out := range outs {
if out.IsLockedWithKey(pubKeyHash) {
// check if this output is not used in some pending transaction
used := false
for _, pin := range pendinguse {
if bytes.Compare(pin.Txid, out.TXID) == 0 &&
pin.Vout == out.OIndex {
used = true
break
}
}
if used {
continue
}
accumulated += out.Value
unspentOutputs = append(unspentOutputs, out)
}
}
return nil
})
if err != nil {
return 0, nil, err
}
if accumulated >= amount {
// choose longest number of outputs to spent. it must be outs with smallest amounts
sort.Sort(structures.TXOutputIndependentList(unspentOutputs))
accumulated = 0
uo := []structures.TXOutputIndependent{}
for _, out := range unspentOutputs {
accumulated += out.Value
uo = append(uo, out)
if accumulated >= amount {
break
}
}
unspentOutputs = uo
}
return accumulated, unspentOutputs, nil
}
// execute callback function for every record in unspent outputs
func (u unspentTransactions) forEachUnspentOutput(address string, callback UnspentTransactionOutputCallbackInterface) error {
pubKeyHash, err := utils.AddresToPubKeyHash(address)
if err != nil {
return err
}
uodb, err := u.DB.GetUnspentOutputsObject()
if err != nil {
return err
}
err = uodb.ForEach(func(txID, txData []byte) error {
outs, err := u.deserializeOutputs(txData)
if err != nil {
return err
}
for _, out := range outs {
if out.IsLockedWithKey(pubKeyHash) {
var fromaddr string
if len(out.SendPubKeyHash) > 0 {
fromaddr, _ = utils.PubKeyHashToAddres(out.SendPubKeyHash)
} else {
fromaddr = "Coin base"
}
err := callback(fromaddr, out.Value, out.TXID, out.OIndex, out.IsBase)
if err != nil {
return err
}
}
}
return nil
})
if err != nil {
return err
}
return nil
}
// Returns list of unspent transactions outputs for address
func (u unspentTransactions) GetunspentTransactionsOutputs(address string) ([]structures.TXOutputIndependent, error) {
if address == "" {
return nil, errors.New("Address is missed")
}
w := remoteclient.Wallet{}
if !w.ValidateAddress(address) {
return nil, errors.New("Address is not valid")
}
pubKeyHash, err := utils.AddresToPubKeyHash(address)
if err != nil {
return nil, err
}
uodb, err := u.DB.GetUnspentOutputsObject()
if err != nil {
return nil, err
}
UTXOs := []structures.TXOutputIndependent{}
//u.Logger.Trace.Printf("findoutputs for %s", address)
err = uodb.ForEach(func(txID, txData []byte) error {
outs, err := u.deserializeOutputs(txData)
if err != nil {
return err
}
for _, out := range outs {
//u.Logger.Trace.Printf("output %x %d", out.TXID, out.OIndex)
if out.IsLockedWithKey(pubKeyHash) {
UTXOs = append(UTXOs, out)
}
}
return nil
})
if err != nil {
return nil, err
}
return UTXOs, nil
}
// Returns total number of unspent transactions in a cache.
func (u unspentTransactions) CountTransactions() (int, error) {
uodb, err := u.DB.GetUnspentOutputsObject()
if err != nil {
return 0, err
}
return uodb.GetCount()
}
// Returns toal number of transactions outputs in a cache
func (u unspentTransactions) CountUnspentOutputs() (int, error) {
uodb, err := u.DB.GetUnspentOutputsObject()
if err != nil {
return 0, err
}
counter := 0
err = uodb.ForEach(func(txID, txData []byte) error {
outs, err := u.deserializeOutputs(txData)
if err != nil {
return err
}
counter += len(outs)
return nil
})
if err != nil {
return 0, err
}
return counter, nil
}
// Rebuilds the DB of unspent transactions
// NOTE . We don't really need this. Normal code should work without reindexing.
// TODO to remove this function in future
func (u unspentTransactions) Reindex() (int, error) {
u.Logger.Trace.Println("Reindex UTXO: Prepare")
uodb, err := u.DB.GetUnspentOutputsObject()
if err != nil {
return 0, err
}
err = uodb.TruncateDB()
if err != nil {
return 0, err
}
u.Logger.Trace.Println("Reindex UTXO: Prepare done")
UTXO, err := u.FindunspentTransactions()
if err != nil {
return 0, err
}
u.Logger.Trace.Println("Reindex UTXO: Store records")
for txID, outs := range UTXO {
u.Logger.Trace.Printf("Reindex UTXO: Save %s %d", txID, len(outs))
key, err := hex.DecodeString(txID)
if err != nil {
return 0, err
}
outsData, err := u.serializeOutputs(outs)
if err != nil {
return 0, err
}
err = uodb.PutDataForTransaction(key, outsData)
if err != nil {
return 0, err
}
}
u.Logger.Trace.Println("Reindex UTXO: Done. Return counts")
return u.CountUnspentOutputs()
}
// Returns full list of unspent transactions outputs
// Iterates over full blockchain
// TODO this will not work for big blockchain. It keeps data in memory
func (u unspentTransactions) FindunspentTransactions() (map[string][]structures.TXOutputIndependent, error) {
UTXO := make(map[string][]structures.TXOutputIndependent)
spentTXOs := make(map[string][]int)
bci, err := blockchain.NewBlockchainIterator(u.DB)
if err != nil {
return nil, err
}
u.Logger.Trace.Println("Get All UTXO: Start")
for {
block, _ := bci.Next()
for j := len(block.Transactions) - 1; j >= 0; j-- {
tx := &block.Transactions[j]
txID := hex.EncodeToString(tx.GetID())
sender := []byte{}
if !tx.IsCoinbaseTransfer() {
sender, _ = utils.HashPubKey(tx.ByPubKey)
}
var spent bool
for outIdx, out := range tx.Vout {
if !out.HasOutAddress() {
// output can be visout address
continue
}
// Was the output spent?
spent = false
if list, ok := spentTXOs[txID]; ok {
for _, spentOutIdx := range list {
if spentOutIdx == outIdx {
// this output of the transaction was already spent
// go to next output of this transaction
spent = true
break
}
}
}
if spent {
continue
}
// add to unspent
if _, ok := UTXO[txID]; !ok {
UTXO[txID] = []structures.TXOutputIndependent{}
}
outs := UTXO[txID]
oute := structures.TXOutputIndependent{}
oute.LoadFromSimple(out, tx.GetID(), outIdx, sender, tx.IsCoinbaseTransfer(), block.Hash)
outs = append(outs, oute)
UTXO[txID] = outs
}
if tx.IsCoinbaseTransfer() {
continue
}
for _, in := range tx.Vin {
inTxID := hex.EncodeToString(in.Txid)
if _, ok := spentTXOs[inTxID]; !ok {
spentTXOs[inTxID] = []int{}
}
spentTXOs[inTxID] = append(spentTXOs[inTxID], in.Vout)
}
}
if len(block.PrevBlockHash) == 0 {
break
}
}
u.Logger.Trace.Printf("Get All UTXO: Return %d records", len(UTXO))
return UTXO, nil
}
/*
* New Block added
* Input of all tranactions are removed from unspent
* OUtput of all transactions are added to unspent
* Update the UTXO set with transactions from the Block
* The Block is considered to be the tip of a blockchain
*/
func (u unspentTransactions) UpdateOnBlockAdd(block *structures.Block) error {
uodb, err := u.DB.GetUnspentOutputsObject()
if err != nil {
u.Logger.Trace.Printf("UTXO, db err %e", err.Error())
return err
}
//u.Logger.Trace.Printf("UPdate UTXO on block add %x", block.Hash)
for _, tx := range block.Transactions {
//u.Logger.Trace.Printf("UpdateOnBlockAdd check tx %x", tx.GetID())
// skip if this is not currency transaction. it can be only SQL with 0 price
if !tx.IsCurrencyTransfer() {
continue
}
sender := []byte{}
if !tx.IsCoinbaseTransfer() {
sender, _ = utils.HashPubKey(tx.ByPubKey)
for _, vin := range tx.Vin {
outsBytes, err := uodb.GetDataForTransaction(vin.Txid)
if err != nil {
return err
}
if outsBytes == nil {
u.Logger.Trace.Printf("UpdateOnBlockAdd in tx is not found %x", vin.Txid)
continue
}
outs, err := u.deserializeOutputs(outsBytes)
if err != nil {
return err
}
updatedOuts := []structures.TXOutputIndependent{}
for _, out := range outs {
if out.OIndex != vin.Vout {
updatedOuts = append(updatedOuts, out)
}
}
if len(updatedOuts) == 0 {
err = uodb.DeleteDataForTransaction(vin.Txid)
} else {
d, err := u.serializeOutputs(updatedOuts)
if err != nil {
return err
}
err = uodb.PutDataForTransaction(vin.Txid, d)
if err != nil {
return err
}
}
if err != nil {
return err
}
}
}
newOutputs := []structures.TXOutputIndependent{}
for outInd, out := range tx.Vout {
if !out.HasOutAddress() {
// output can be visout address
continue
}
no := structures.TXOutputIndependent{}
no.LoadFromSimple(out, tx.ID, outInd, sender, tx.IsCoinbaseTransfer(), block.Hash)
newOutputs = append(newOutputs, no)
}
d, err := u.serializeOutputs(newOutputs)
if err != nil {
return err
}
//u.Logger.Trace.Printf("BA tx save as unspent %x %d outputs", tx.ID, len(newOutputs))
err = uodb.PutDataForTransaction(tx.ID, d)
if err != nil {
return err
}
}
return nil
}
// This is executed when a block is canceled.
// All input transactions must be return to "unspent"
// And all output must be deleted from "unspent"
func (u unspentTransactions) UpdateOnBlockCancel(block *structures.Block) error {
uodb, err := u.DB.GetUnspentOutputsObject()
if err != nil {
return err
}
u.Logger.Trace.Printf("block cancel at unspent %x , prev hash %x", block.Hash, block.PrevBlockHash) //REM
for _, tx := range block.Transactions {
u.Logger.Trace.Printf("BC check tx %x", tx.GetID()) //REM
// delete this transaction from list of unspent
uodb.DeleteDataForTransaction(tx.GetID())
if tx.IsCoinbaseTransfer() {
continue
}
sender, _ := utils.HashPubKey(tx.ByPubKey)
// all input outputs must be added back to unspent
// but only if inputs are in current BC
for _, vin := range tx.Vin {
// when we execute cancel, current top can be already changed. we use this block hash as a top
// to find this TX
txi, spending, blockHash, err := u.newTransactionIndex().GetCurrencyTransactionAllInfo(vin.Txid, block.PrevBlockHash)
//u.Logger.Trace.Printf("input tx find input %x", vin.Txid) //REM
if err != nil {
//u.Logger.Trace.Printf("error finding tx %x %s", tx.ID, err.Error()) //REM
return err
}
if txi == nil {
// TX is not found in current BC . no sense to add it to unspent
u.Logger.Trace.Printf("tx not found in current BC") //REM
break
}
//u.Logger.Trace.Printf("found tx in block %x", blockHash) //REM
//u.Logger.Trace.Printf("spendings count %d", len(spending)) //REM
//u.Logger.Trace.Printf("spendings count %s", spending) //REM
UnspentOuts := []structures.TXOutputIndependent{}
for outInd, out := range txi.Vout {
if !out.HasOutAddress() {
// output can be visout address
continue
}
spent := false
for _, so := range spending {
if so.OutInd == outInd {
spent = true
break
}
}
if !spent {
no := structures.TXOutputIndependent{}
no.LoadFromSimple(out, txi.ID, outInd, sender, tx.IsCoinbaseTransfer(), blockHash)
UnspentOuts = append(UnspentOuts, no)
}
}
//u.Logger.Trace.Printf("BC tx save as unspent %x %d outputs", vin.Txid, len(UnspentOuts))
if len(UnspentOuts) > 0 {
txData, err := u.serializeOutputs(UnspentOuts)
if err != nil {
return err
}
err = uodb.PutDataForTransaction(vin.Txid, txData)
if err != nil {
return err
}
} else {
uodb.DeleteDataForTransaction(vin.Txid)
}
}
}
return nil
}
// Find inputs for new structures. Receives list of pending inputs used in other
// not yet confirmed transactions
// Returns list of inputs prepared. Even if less then requested
// Returns previous transactions. It later will be used to prepare data to sign
func (u unspentTransactions) GetNewTransactionInputs(PubKey []byte, to string, amount float64,
pendinguse []structures.TXCurrencyInput) ([]structures.TXCurrencyInput, map[string]*structures.Transaction, float64, error) {
localError := func(err error) ([]structures.TXCurrencyInput, map[string]*structures.Transaction, float64, error) {
return nil, nil, 0, err
}
inputs := []structures.TXCurrencyInput{}
pubKeyHash, _ := utils.HashPubKey(PubKey)
totalamount, validOutputs, err := u.ChooseSpendableOutputs(pubKeyHash, amount, pendinguse)
if err != nil {
return localError(err)
}
bcMan, err := blockchain.NewBlockchainManager(u.DB, u.Logger)
if err != nil {
return localError(err)
}
// here we don't calculate is total amount is good or no.
// later we will add unconfirmed transactions if no enough funds
// build list of previous transactions
prevTXs := make(map[string]*structures.Transaction)
// Build a list of inputs
for _, out := range validOutputs {
input := structures.TXCurrencyInput{out.TXID, out.OIndex}
inputs = append(inputs, input)
prevTX, err := bcMan.GetTransactionFromBlock(out.TXID, out.BlockHash)
if err != nil {
return localError(err)
}
prevTXs[hex.EncodeToString(prevTX.GetID())] = prevTX
}
return inputs, prevTXs, totalamount, nil
}
// Returns previous transactions. It later will be used to prepare data to sign
func (u unspentTransactions) ExtendNewTransactionInputs(PubKey []byte, amount, totalamount float64,
inputs []structures.TXCurrencyInput, prevTXs map[string]*structures.Transaction,
pendingoutputs []*structures.TXOutputIndependent) ([]structures.TXCurrencyInput, map[string]*structures.Transaction, float64, error) {
// Build a list of inputs
for _, out := range pendingoutputs {
input := structures.TXCurrencyInput{out.TXID, out.OIndex}
inputs = append(inputs, input)
prevTX, err := structures.DeserializeTransaction(out.BlockHash) // here we have transaction serialised, not block hash
if err != nil {
return inputs, prevTXs, totalamount, err
}
prevTXs[hex.EncodeToString(prevTX.GetID())] = prevTX
totalamount += out.Value
if totalamount >= amount {
break
}
}
return inputs, prevTXs, totalamount, nil
}
/*
NOTE this function was removed as there is better duplicate in manager getCurrencyInputTransactionsState
this is left commented, maybe in future will be needed
// Verifies which transactions outputs are not yet spent.
// Returns list of inputs that are not found in list of unspent outputs
func (u unspentTransactions) VerifyTransactionsOutputsAreNotSpent(txilist []structures.TXCurrencyInput) (map[int]structures.TXCurrencyInput, map[int]*structures.Transaction, error) {
localError := func(err error) (map[int]structures.TXCurrencyInput, map[int]*structures.Transaction, error) {
return nil, nil, err
}
// list of full input transactions. it can be used to verify signature later
inputTX := map[int]*structures.Transaction{}
notFoundInputs := make(map[int]structures.TXCurrencyInput)
uodb, err := u.DB.GetUnspentOutputsObject()
if err != nil {
return localError(err)
}
bcMan, err := blockchain.NewBlockchainManager(u.DB, u.Logger)
if err != nil {
return localError(err)
}
for txiInd, txi := range txilist {
txdata, err := uodb.GetDataForTransaction(txi.Txid)
if err != nil {
return localError(err)
}
if txdata == nil {
// not found
inputTX[txiInd] = nil
notFoundInputs[txiInd] = txi
continue
}
exists := false
blockHash := []byte{}
outs, err := u.deserializeOutputs(txdata)
if err != nil {
return localError(err)
}
for _, out := range outs {
if out.OIndex == txi.Vout {
exists = true
blockHash = out.BlockHash
break
}
}
if !exists {
notFoundInputs[txiInd] = txi
inputTX[txiInd] = nil
} else {
// find this TX and get full info about it
prevTX, err := bcMan.GetTransactionFromBlock(txi.Txid, blockHash)
if err != nil {
return localError(err)
}
inputTX[txiInd] = prevTX
}
}
return notFoundInputs, inputTX, nil
}
*/