-
Notifications
You must be signed in to change notification settings - Fork 9
/
manager.go
619 lines (482 loc) · 18.7 KB
/
manager.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
package transactions
import (
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
"strconv"
"github.com/gelembjuk/democoin/lib"
"github.com/gelembjuk/democoin/lib/utils"
"github.com/gelembjuk/democoin/lib/wallet"
"github.com/gelembjuk/democoin/node/blockchain"
"github.com/gelembjuk/democoin/node/database"
"github.com/gelembjuk/democoin/node/structures"
)
type txManager struct {
DB database.DBManager
Logger *utils.LoggerMan
}
func NewManager(DB database.DBManager, Logger *utils.LoggerMan) TransactionsManagerInterface {
return &txManager{DB, Logger}
}
// Create tx index object to use in this package
func (n txManager) getIndexManager() *transactionsIndex {
return newTransactionIndex(n.DB, n.Logger)
}
// Create unapproved tx manage object to use in this package
func (n txManager) getUnapprovedTransactionsManager() *unApprovedTransactions {
return &unApprovedTransactions{n.DB, n.Logger}
}
// Create unspent outputx manage object to use in this package
func (n txManager) getUnspentOutputsManager() *unspentTransactions {
return &unspentTransactions{n.DB, n.Logger}
}
// Reindex caches
func (n *txManager) ReindexData() (map[string]int, error) {
err := n.getIndexManager().Reindex()
if err != nil {
return nil, err
}
count, err := n.getUnspentOutputsManager().Reindex()
if err != nil {
return nil, err
}
info := map[string]int{"unspentoutputs": count}
return info, nil
}
// Calculates balance of address. Uses DB of unspent trasaction outputs
// and cache of pending transactions
func (n *txManager) GetAddressBalance(address string) (wallet.WalletBalance, error) {
balance := wallet.WalletBalance{}
n.Logger.Trace.Printf("Get balance %s", address)
result, err := n.getUnspentOutputsManager().GetAddressBalance(address)
if err != nil {
n.Logger.Trace.Printf("Error 1 %s", err.Error())
return balance, err
}
balance.Approved = result
// get pending
n.Logger.Trace.Printf("Get pending %s", address)
p, err := n.getAddressPendingBalance(address)
if err != nil {
n.Logger.Trace.Printf("Error 2 %s", err.Error())
return balance, err
}
balance.Pending = p
balance.Total = balance.Approved + balance.Pending
return balance, nil
}
// return count of transactions in pool
func (n *txManager) GetUnapprovedCount() (int, error) {
return n.getUnapprovedTransactionsManager().GetCount()
}
// return count of unspent outputs
func (n *txManager) GetUnspentCount() (int, error) {
return n.getUnspentOutputsManager().CountUnspentOutputs()
}
// return number of unapproved transactions for new block. detect conflicts
// if there are less, it returns less than requested
func (n *txManager) GetUnapprovedTransactionsForNewBlock(number int) ([]*structures.Transaction, error) {
txlist, err := n.getUnapprovedTransactionsManager().GetTransactions(number)
n.Logger.Trace.Printf("Found %d transaction to mine\n", len(txlist))
txs := []*structures.Transaction{}
for _, tx := range txlist {
n.Logger.Trace.Printf("Go to verify: %x\n", tx.ID)
// we need to verify each transaction
// we will do full deep check of transaction
// also, a transaction can have input from other transaction from thi block
vtx, err := n.VerifyTransaction(tx, txs, []byte{})
if err != nil {
// this can be case when a transaction is based on other unapproved transaction
// and that transaction was created in same second
n.Logger.Trace.Printf("Ignore transaction %x. Verify failed with error: %s\n", tx.ID, err.Error())
// we delete this transaction. no sense to keep it
n.CancelTransaction(tx.ID)
continue
}
if vtx {
// transaction is valid
txs = append(txs, tx)
} else {
// the transaction is invalid. some input was already used in other confirmed transaction
// or somethign wrong with signatures.
// remove this transaction from the DB of unconfirmed transactions
n.Logger.Trace.Printf("Delete transaction used in other block before: %x\n", tx.ID)
n.CancelTransaction(tx.ID)
}
}
txlist = nil
n.Logger.Trace.Printf("After verification %d transaction are left\n", len(txs))
if len(txs) == 0 {
return nil, errors.New("All transactions are invalid! Waiting for new ones...")
}
// now it is needed to check if transactions don't conflict one to other
var badtransactions []*structures.Transaction
txs, badtransactions, err = n.getUnapprovedTransactionsManager().DetectConflicts(txs)
n.Logger.Trace.Printf("After conflict detection %d - fine, %d - conflicts\n", len(txs), len(badtransactions))
if err != nil {
return nil, err
}
if len(badtransactions) > 0 {
// there are conflicts! remove conflicting transactions
for _, tx := range badtransactions {
n.Logger.Trace.Printf("Delete conflicting transaction: %x\n", tx.ID)
n.CancelTransaction(tx.ID)
}
}
return txs, nil
}
/*
* Cancels unapproved transaction.
* NOTE this can work only for local node. it a transaction was already sent to other nodes, it will not be canceled
* and can be added to next block
*/
func (n *txManager) CancelTransaction(txid []byte) error {
found, err := n.getUnapprovedTransactionsManager().Delete(txid)
if err == nil && !found {
return errors.New("Transaction ID not found in the list of unapproved transactions")
}
return nil
}
// Verify if transaction is correct.
// If it is build on correct outputs.This does checks agains blockchain. Needs more time
// NOTE Transaction can have outputs of other transactions that are not yet approved.
// This must be considered as correct case
func (n *txManager) VerifyTransaction(tx *structures.Transaction, prevtxs []*structures.Transaction, tip []byte) (bool, error) {
inputTXs, notFoundInputs, err := n.getInputTransactionsState(tx, tip)
if err != nil {
return false, err
}
if len(notFoundInputs) > 0 {
// some of inputs can be from other transactions in this pool
inputTXs, err = n.getUnapprovedTransactionsManager().CheckInputsWereBefore(notFoundInputs, prevtxs, inputTXs)
if err != nil {
return false, err
}
}
// do final check against inputs
err = tx.Verify(inputTXs)
if err != nil {
return false, err
}
return true, nil
}
// Iterate over unapproved transactions, for example to display them . Accepts callback as argument
func (n *txManager) ForEachUnapprovedTransaction(callback UnApprovedTransactionCallbackInterface) (int, error) {
return n.getUnapprovedTransactionsManager().forEachUnapprovedTransaction(callback)
}
// Iterate over unspent transactions outputs, for example to display them . Accepts callback as argument
func (n *txManager) ForEachUnspentOutput(address string, callback UnspentTransactionOutputCallbackInterface) error {
return n.getUnspentOutputsManager().forEachUnspentOutput(address, callback)
}
// Remove all transactions from unapproved cache (transactions pool)
func (n *txManager) CleanUnapprovedCache() error {
return n.getUnapprovedTransactionsManager().CleanUnapprovedCache()
}
// to execute when new block added . the block must not be on top
func (n *txManager) BlockAdded(block *structures.Block, ontopofchain bool) error {
// update caches
n.getIndexManager().BlockAdded(block)
if ontopofchain {
n.getUnapprovedTransactionsManager().DeleteFromBlock(block)
n.getUnspentOutputsManager().UpdateOnBlockAdd(block)
}
return nil
}
// Block was removed from the top of primary blockchain branch
func (n *txManager) BlockRemoved(block *structures.Block) error {
n.getUnapprovedTransactionsManager().AddFromCanceled(block.Transactions)
n.getUnspentOutputsManager().UpdateOnBlockCancel(block)
n.getIndexManager().BlockRemoved(block)
return nil
}
// block is now added to primary chain. it existed in DB before
func (n *txManager) BlockAddedToPrimaryChain(block *structures.Block) error {
n.getUnapprovedTransactionsManager().DeleteFromBlock(block)
n.getUnspentOutputsManager().UpdateOnBlockAdd(block)
return nil
}
// block is removed from primary chain. it continued to be in DB on side branch
func (n *txManager) BlockRemovedFromPrimaryChain(block *structures.Block) error {
n.getUnapprovedTransactionsManager().AddFromCanceled(block.Transactions)
n.getUnspentOutputsManager().UpdateOnBlockCancel(block)
return nil
}
// Send amount of money if a node is not running.
// This function only adds a transaction to queue
// Attempt to send the transaction to other nodes will be done in other place
//
// Returns new transaction hash. This return can be used to try to send transaction
// to other nodes or to try mining
func (n *txManager) CreateTransaction(PubKey []byte, privKey ecdsa.PrivateKey, to string, amount float64) (*structures.Transaction, error) {
if amount <= 0 {
return nil, errors.New("Amount must be positive value")
}
if to == "" {
return nil, errors.New("Recipient address is not provided")
}
txBytes, DataToSign, err := n.PrepareNewTransaction(PubKey, to, amount)
if err != nil {
return nil, errors.New(fmt.Sprintf("Prepare error: %s", err.Error()))
}
signatures, err := utils.SignDataSet(PubKey, privKey, DataToSign)
if err != nil {
return nil, errors.New(fmt.Sprintf("Sign error: %s", err.Error()))
}
NewTX, err := n.ReceivedNewTransactionData(txBytes, signatures)
if err != nil {
return nil, errors.New(fmt.Sprintf("Final ading TX error: %s", err.Error()))
}
return NewTX, nil
}
// New transactions created. It is received in serialysed view and signatures separately
// This data is ready to be convertd to complete gransaction
func (n *txManager) ReceivedNewTransactionData(txBytes []byte, Signatures [][]byte) (*structures.Transaction, error) {
tx := structures.Transaction{}
err := tx.DeserializeTransaction(txBytes)
if err != nil {
return nil, err
}
err = tx.SetSignatures(Signatures)
if err != nil {
return nil, err
}
err = n.ReceivedNewTransaction(&tx)
if err != nil {
return nil, err
}
return &tx, nil
}
// New transaction reveived from other node. We need to verify and add to cache of unapproved
func (n *txManager) ReceivedNewTransaction(tx *structures.Transaction) error {
// verify this transaction
good, err := n.verifyTransactionQuick(tx)
if err != nil {
return err
}
if !good {
return errors.New("Transaction verification failed")
}
// if all is ok, add it to the list of unapproved
return n.getUnapprovedTransactionsManager().Add(tx)
}
// Request to make new transaction and prepare data to sign
// This function should find good input transactions for this amount
// Including inputs from unapproved transactions if no good approved transactions yet
func (n *txManager) PrepareNewTransaction(PubKey []byte, to string, amount float64) ([]byte, [][]byte, error) {
amount, err := strconv.ParseFloat(fmt.Sprintf("%.8f", amount), 64)
if err != nil {
return nil, nil, err
}
PubKeyHash, _ := utils.HashPubKey(PubKey)
// get from pending transactions. find outputs used by this pubkey
pendinginputs, pendingoutputs, _, err := n.getUnapprovedTransactionsManager().GetPreparedBy(PubKeyHash)
n.Logger.Trace.Printf("Pending transactions state: %d- inputs, %d - unspent outputs", len(pendinginputs), len(pendingoutputs))
inputs, prevTXs, totalamount, err := n.getUnspentOutputsManager().GetNewTransactionInputs(PubKey, to, amount, pendinginputs)
if err != nil {
return nil, nil, err
}
n.Logger.Trace.Printf("First step prepared amount %f of %f", totalamount, amount)
if totalamount < amount {
// no anough funds in confirmed transactions
// pending must be used
if len(pendingoutputs) == 0 {
// nothing to add
return nil, nil, errors.New("No enough funds for requested transaction")
}
inputs, prevTXs, totalamount, err =
n.getUnspentOutputsManager().ExtendNewTransactionInputs(PubKey, amount, totalamount,
inputs, prevTXs, pendingoutputs)
if err != nil {
return nil, nil, err
}
}
n.Logger.Trace.Printf("Second step prepared amount %f of %f", totalamount, amount)
if totalamount < amount {
return nil, nil, errors.New("No anough funds to make new transaction")
}
return n.prepareNewTransactionComplete(PubKey, to, amount, inputs, totalamount, prevTXs)
}
//
func (n *txManager) prepareNewTransactionComplete(PubKey []byte, to string, amount float64,
inputs []structures.TXInput, totalamount float64, prevTXs map[string]structures.Transaction) ([]byte, [][]byte, error) {
var outputs []structures.TXOutput
// Build a list of outputs
from, _ := utils.PubKeyToAddres(PubKey)
outputs = append(outputs, *structures.NewTXOutput(amount, to))
if totalamount > amount && totalamount-amount > lib.SmallestUnit {
outputs = append(outputs, *structures.NewTXOutput(totalamount-amount, from)) // a change
}
inputTXs := make(map[int]*structures.Transaction)
for vinInd, vin := range inputs {
tx := prevTXs[hex.EncodeToString(vin.Txid)]
inputTXs[vinInd] = &tx
}
tx := structures.Transaction{nil, inputs, outputs, 0}
tx.TimeNow()
signdata, err := tx.PrepareSignData(inputTXs)
if err != nil {
return nil, nil, err
}
txBytes, err := tx.Serialize()
if err != nil {
return nil, nil, err
}
return txBytes, signdata, nil
}
// check if transaction exists. it checks in all places. in approved and pending
func (n *txManager) GetIfExists(txid []byte) (*structures.Transaction, error) {
// check in pending first
tx, err := n.getUnapprovedTransactionsManager().GetIfExists(txid)
if !(tx == nil && err == nil) {
return tx, err
}
// not exist on pending and no error
// try to check in approved . it will look only in primary branch
tx, _, _, err = n.getIndexManager().GetTransactionAllInfo(txid, []byte{})
return tx, err
}
// check if transaction exists in unapproved cache
func (n *txManager) GetIfUnapprovedExists(txid []byte) (*structures.Transaction, error) {
// check in pending first
tx, err := n.getUnapprovedTransactionsManager().GetIfExists(txid)
if !(tx == nil && err == nil) {
return tx, err
}
return nil, nil
}
// Calculates pending balance of address.
func (n *txManager) getAddressPendingBalance(address string) (float64, error) {
PubKeyHash, _ := utils.AddresToPubKeyHash(address)
// inputs this is what a wallet spent from his real approved balance
// outputs this is what a wallet receives (and didn't resulse in other pending TXs)
// slice inputs contains only inputs from approved transactions outputs
_, outputs, inputs, err := n.getUnapprovedTransactionsManager().GetPreparedBy(PubKeyHash)
if err != nil {
return 0, err
}
pendingbalance := float64(0)
for _, o := range outputs {
// this is amount sent to this wallet and this
// list contains only what was not spent in other prepared TX
pendingbalance += o.Value
}
// we need to know values for inputs. this are inputs based on TXs that are in approved
// input TX can be confirmed (in unspent outputs) or unconfirmed . we need to look for it in
// both places
for _, i := range inputs {
n.Logger.Trace.Printf("find input %s for tx %x", i, i.Txid)
v, err := n.getUnspentOutputsManager().GetInputValue(i)
if err != nil {
/*
if err, ok := err.(*TXNotFoundError); ok && err.GetKind() == TXNotFoundErrorUnspent {
// check this TX in prepared
v2, err := n.GetUnapprovedTransactionsManager().GetInputValue(i)
if err != nil {
return 0, errors.New(fmt.Sprintf("Pending Balance Error: input check fails on unapproved: %s", err.Error()))
}
v = v2
} else {
return 0, errors.New(fmt.Sprintf("Pending Balance Error: input check fails on unspent: %s", err.Error()))
}
*/
return 0, errors.New(fmt.Sprintf("Pending Balance Error: input check fails on unspent: %s", err.Error()))
}
pendingbalance -= v
}
return pendingbalance, nil
}
// Verify if transaction is correct.
// If it is build on correct outputs.It checks only cache of unspent transactions
// This function doesn't do full alidation with blockchain
// NOTE Transaction can have outputs of other transactions that are not yet approved.
// This must be considered as correct case
func (n *txManager) verifyTransactionQuick(tx *structures.Transaction) (bool, error) {
notFoundInputs, inputTXs, err := n.getUnspentOutputsManager().VerifyTransactionsOutputsAreNotSpent(tx.Vin)
if err != nil {
n.Logger.Trace.Printf("VT error 1: %s", err.Error())
return false, err
}
if len(notFoundInputs) > 0 {
// some inputs are not existent
// we need to try to find them in list of unapproved transactions
// if not found then it is bad transaction
err := n.getUnapprovedTransactionsManager().CheckInputsArePrepared(notFoundInputs, inputTXs)
if err != nil {
return false, err
}
}
// verify signatures
err = tx.Verify(inputTXs)
if err != nil {
return false, err
}
return true, nil
}
// Verifies transaction inputs. Check if that are real existent transactions. And that outputs are not yet used
// Is some transaction is not in blockchain, returns nil pointer in map and this input in separate map
// Missed inputs can be some unconfirmed transactions
// Returns: map of previous transactions (full info about input TX). map by input index
// next map is wrong input, where a TX is not found.
func (n *txManager) getInputTransactionsState(tx *structures.Transaction,
tip []byte) (map[int]*structures.Transaction, map[int]structures.TXInput, error) {
//n.Logger.Trace.Printf("get state %x , tip %x", tx.ID, tip)
prevTXs := make(map[int]*structures.Transaction)
badinputs := make(map[int]structures.TXInput)
if tx.IsCoinbase() {
return prevTXs, badinputs, nil
}
bcMan, err := blockchain.NewBlockchainManager(n.DB, n.Logger)
if err != nil {
return nil, nil, err
}
for vind, vin := range tx.Vin {
//n.Logger.Trace.Printf("Load in tx %x", vin.Txid)
txBockHashes, err := n.getIndexManager().GetTranactionBlocks(vin.Txid)
if err != nil {
n.Logger.Trace.Printf("Error %s", err.Error())
return nil, nil, err
}
txBockHash, err := bcMan.ChooseHashUnderTip(txBockHashes, tip)
if err != nil {
n.Logger.Trace.Printf("Error getting correct hash %s", err.Error())
return nil, nil, err
}
var prevTX *structures.Transaction
if txBockHash == nil {
//n.Logger.Trace.Printf("Not found TX")
prevTX = nil
} else {
// if block is in this chain
//n.Logger.Trace.Printf("block height %d", heigh)
prevTX, err = bcMan.GetTransactionFromBlock(vin.Txid, txBockHash)
if err != nil {
return nil, nil, err
}
}
if prevTX == nil {
// transaction not found
badinputs[vind] = vin
prevTXs[vind] = nil
//n.Logger.Trace.Printf("tx is not in blocks")
} else {
//n.Logger.Trace.Printf("tx found")
// check if this input was not yet spent somewhere
spentouts, err := n.getIndexManager().GetTranactionOutputsSpent(vin.Txid, txBockHash, tip)
if err != nil {
return nil, nil, err
}
//n.Logger.Trace.Printf("spending of tx count %d", len(spentouts))
if len(spentouts) > 0 {
for _, o := range spentouts {
if o.OutInd == vin.Vout {
return nil, nil, errors.New("Transaction input was already spent before")
}
}
}
// the transaction out was not yet spent
prevTXs[vind] = prevTX
}
}
return prevTXs, badinputs, nil
}