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
/
tx_service.go
195 lines (158 loc) · 5.16 KB
/
tx_service.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
package bux
import (
"context"
"encoding/hex"
"fmt"
"github.com/BuxOrg/bux/utils"
)
// The transaction is treat as external incoming transaction - transaction without a draft
// Only use this function when you know what you are doing!
func saveRawTransaction(ctx context.Context, c ClientInterface, allowUnknown bool, txHex string, opts ...ModelOps) (*Transaction, error) {
newOpts := c.DefaultModelOptions(append(opts, New())...)
tx, err := txFromHex(txHex, newOpts...)
if err != nil {
return nil, ErrMissingTxHex
}
// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, fmt.Sprintf(lockKeyRecordTx, tx.GetID()), c.Cachestore(),
)
defer unlock()
if err != nil {
return nil, err
}
if !allowUnknown && !tx.hasOneKnownDestination(ctx, c) {
return nil, ErrNoMatchingOutputs
}
if err = tx.processUtxos(ctx); err != nil {
return nil, err
}
if !tx.isMined() {
sync := newSyncTransaction(
tx.GetID(),
c.DefaultSyncConfig(),
tx.GetOptions(true)...,
)
sync.BroadcastStatus = SyncStatusSkipped
sync.P2PStatus = SyncStatusSkipped
sync.Metadata = tx.Metadata
tx.syncTransaction = sync
}
if err = tx.Save(ctx); err != nil {
return nil, err
}
return tx, nil
}
// processUtxos will process the inputs and outputs for UTXOs
func (m *Transaction) processUtxos(ctx context.Context) error {
// Input should be processed only for outgoing transactions
if m.draftTransaction != nil {
if err := m._processInputs(ctx); err != nil {
return err
}
}
if err := m._processOutputs(ctx); err != nil {
return err
}
m.TotalValue, m.Fee = m.getValues()
m.NumberOfInputs = uint32(len(m.parsedTx.Inputs))
m.NumberOfOutputs = uint32(len(m.parsedTx.Outputs))
return nil
}
// processTxInputs will process the transaction inputs
func (m *Transaction) _processInputs(ctx context.Context) (err error) {
// Pre-build the options
opts := m.GetOptions(false)
client := m.Client()
var utxo *Utxo
// check whether we are spending an internal utxo
for index := range m.TransactionBase.parsedTx.Inputs {
// todo: optimize this SQL SELECT to get all utxos in one query?
if utxo, err = m.transactionService.getUtxo(ctx,
hex.EncodeToString(m.TransactionBase.parsedTx.Inputs[index].PreviousTxID()),
m.TransactionBase.parsedTx.Inputs[index].PreviousTxOutIndex,
opts...,
); err != nil {
return
} else if utxo != nil { // Found a UTXO record
// Is Spent?
if len(utxo.SpendingTxID.String) > 0 {
return ErrUtxoAlreadySpent
}
// Only if IUC is enabled (or client is nil which means its enabled by default)
if client == nil || client.IsIUCEnabled() {
// check whether the utxo is spent
isReserved := len(utxo.DraftID.String) > 0
matchesDraft := m.draftTransaction != nil && utxo.DraftID.String == m.draftTransaction.ID
// Check whether the spending transaction was reserved by the draft transaction (in the utxo)
if !isReserved {
return ErrUtxoNotReserved
}
if !matchesDraft {
return ErrDraftIDMismatch
}
}
// Update the output value
if _, ok := m.XpubOutputValue[utxo.XpubID]; !ok {
m.XpubOutputValue[utxo.XpubID] = 0
}
m.XpubOutputValue[utxo.XpubID] -= int64(utxo.Satoshis)
// Mark utxo as spent
utxo.SpendingTxID.Valid = true
utxo.SpendingTxID.String = m.ID
m.utxos = append(m.utxos, *utxo)
// Add the xPub ID
if !utils.StringInSlice(utxo.XpubID, m.XpubInIDs) {
m.XpubInIDs = append(m.XpubInIDs, utxo.XpubID)
}
}
// todo: what if the utxo is nil (not found)?
}
return
}
// processTxOutputs will process the transaction outputs
func (m *Transaction) _processOutputs(ctx context.Context) (err error) {
// Pre-build the options
opts := m.GetOptions(false)
newOpts := append(opts, New())
var destination *Destination
// check all the outputs for a known destination
numberOfOutputsProcessed := 0
for i, output := range m.parsedTx.Outputs {
amount := output.Satoshis
// only save outputs with a satoshi value attached to it
if amount > 0 {
txLockingScript := output.LockingScript.String()
lockingScript := utils.GetDestinationLockingScript(txLockingScript)
// only Save utxos for known destinations
// todo: optimize this SQL SELECT by requesting all the scripts at once (vs in this loop)
// todo: how to handle tokens and other non-standard outputs ?
if destination, err = m.transactionService.getDestinationByLockingScript(
ctx, lockingScript, opts...,
); err != nil {
return
} else if destination != nil {
// Add value of output to xPub ID
if _, ok := m.XpubOutputValue[destination.XpubID]; !ok {
m.XpubOutputValue[destination.XpubID] = 0
}
m.XpubOutputValue[destination.XpubID] += int64(amount)
utxo, _ := m.client.GetUtxoByTransactionID(ctx, m.ID, uint32(i))
if utxo == nil {
utxo = newUtxo(
destination.XpubID, m.ID, txLockingScript, uint32(i),
amount, newOpts...,
)
}
// Append the UTXO model
m.utxos = append(m.utxos, *utxo)
// Add the xPub ID
if !utils.StringInSlice(destination.XpubID, m.XpubOutIDs) {
m.XpubOutIDs = append(m.XpubOutIDs, destination.XpubID)
}
numberOfOutputsProcessed++
}
}
}
return
}