This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
beef_bump.go
174 lines (141 loc) · 4.84 KB
/
beef_bump.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
package bux
import (
"context"
"errors"
"fmt"
"sort"
"github.com/libsv/go-bt/v2"
)
func calculateMergedBUMP(txs []*Transaction) (BUMPs, error) {
bumps := make(map[uint64][]BUMP)
mergedBUMPs := make(BUMPs, 0)
for _, tx := range txs {
if tx.BUMP.BlockHeight == 0 || len(tx.BUMP.Path) == 0 {
continue
}
bumps[tx.BlockHeight] = append(bumps[tx.BlockHeight], tx.BUMP)
}
// ensure that BUMPs are sorted by block height and will always be put in beef in the same order
mapKeys := make([]uint64, 0, len(bumps))
for k := range bumps {
mapKeys = append(mapKeys, k)
}
sort.Slice(mapKeys, func(i, j int) bool { return mapKeys[i] < mapKeys[j] })
for _, k := range mapKeys {
bump, err := CalculateMergedBUMP(bumps[k])
if err != nil {
return nil, fmt.Errorf("Error while calculating Merged BUMP: %s", err.Error())
}
if bump == nil {
continue
}
mergedBUMPs = append(mergedBUMPs, bump)
}
return mergedBUMPs, nil
}
func validateBumps(bumps BUMPs) error {
if len(bumps) == 0 {
return errors.New("empty bump paths slice")
}
for _, p := range bumps {
if len(p.Path) == 0 {
return errors.New("one of bump path is empty")
}
}
return nil
}
func prepareBEEFFactors(ctx context.Context, tx *Transaction, store TransactionGetter) ([]*bt.Tx, []*Transaction, error) {
btTxsNeededForBUMP, txsNeededForBUMP, err := initializeRequiredTxsCollection(tx)
if err != nil {
return nil, nil, err
}
txIDs := make([]string, 0, len(tx.draftTransaction.Configuration.Inputs))
for _, input := range tx.draftTransaction.Configuration.Inputs {
txIDs = append(txIDs, input.UtxoPointer.TransactionID)
}
inputTxs, err := getRequiredTransactions(ctx, txIDs, store)
if err != nil {
return nil, nil, err
}
for _, inputTx := range inputTxs {
inputBtTx, err := bt.NewTxFromString(inputTx.Hex)
if err != nil {
return nil, nil, fmt.Errorf("cannot convert to bt.Tx from hex (tx.ID: %s). Reason: %w", inputTx.ID, err)
}
txsNeededForBUMP = append(txsNeededForBUMP, inputTx)
btTxsNeededForBUMP = append(btTxsNeededForBUMP, inputBtTx)
if inputTx.BUMP.BlockHeight == 0 && len(inputTx.BUMP.Path) == 0 {
parentBtTransactions, parentTransactions, err := checkParentTransactions(ctx, store, inputBtTx)
if err != nil {
return nil, nil, err
}
txsNeededForBUMP = append(txsNeededForBUMP, parentTransactions...)
btTxsNeededForBUMP = append(btTxsNeededForBUMP, parentBtTransactions...)
}
}
return btTxsNeededForBUMP, txsNeededForBUMP, nil
}
func checkParentTransactions(ctx context.Context, store TransactionGetter, btTx *bt.Tx) ([]*bt.Tx, []*Transaction, error) {
parentTxIDs := make([]string, 0, len(btTx.Inputs))
for _, txIn := range btTx.Inputs {
parentTxIDs = append(parentTxIDs, txIn.PreviousTxIDStr())
}
parentTxs, err := getRequiredTransactions(ctx, parentTxIDs, store)
if err != nil {
return nil, nil, err
}
validTxs := make([]*Transaction, 0, len(parentTxs))
validBtTxs := make([]*bt.Tx, 0, len(parentTxs))
for _, parentTx := range parentTxs {
parentBtTx, err := bt.NewTxFromString(parentTx.Hex)
if err != nil {
return nil, nil, fmt.Errorf("cannot convert to bt.Tx from hex (tx.ID: %s). Reason: %w", parentTx.ID, err)
}
validTxs = append(validTxs, parentTx)
validBtTxs = append(validBtTxs, parentBtTx)
if parentTx.BUMP.BlockHeight == 0 && len(parentTx.BUMP.Path) == 0 {
parentValidBtTxs, parentValidTxs, err := checkParentTransactions(ctx, store, parentBtTx)
if err != nil {
return nil, nil, err
}
validTxs = append(validTxs, parentValidTxs...)
validBtTxs = append(validBtTxs, parentValidBtTxs...)
}
}
return validBtTxs, validTxs, nil
}
func getRequiredTransactions(ctx context.Context, txIds []string, store TransactionGetter) ([]*Transaction, error) {
txs, err := store.GetTransactionsByIDs(ctx, txIds)
if err != nil {
return nil, fmt.Errorf("cannot get transactions from database: %w", err)
}
if len(txs) != len(txIds) {
missingTxIDs := getMissingTxs(txIds, txs)
return nil, fmt.Errorf("required transactions not found in database: %v", missingTxIDs)
}
return txs, nil
}
func getMissingTxs(txIDs []string, foundTxs []*Transaction) []string {
foundTxIDs := make(map[string]bool)
for _, tx := range foundTxs {
foundTxIDs[tx.ID] = true
}
var missingTxIDs []string
for _, txID := range txIDs {
if !foundTxIDs[txID] {
missingTxIDs = append(missingTxIDs, txID)
}
}
return missingTxIDs
}
func initializeRequiredTxsCollection(tx *Transaction) ([]*bt.Tx, []*Transaction, error) {
var btTxsNeededForBUMP []*bt.Tx
var txsNeededForBUMP []*Transaction
processedBtTx, err := bt.NewTxFromString(tx.Hex)
if err != nil {
return nil, nil, fmt.Errorf("cannot convert processed tx to bt.Tx from hex (tx.ID: %s). Reason: %w", tx.ID, err)
}
btTxsNeededForBUMP = append(btTxsNeededForBUMP, processedBtTx)
txsNeededForBUMP = append(txsNeededForBUMP, tx)
return btTxsNeededForBUMP, txsNeededForBUMP, nil
}