This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
/
transactions.go
265 lines (230 loc) · 6.95 KB
/
transactions.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
package wallet
import (
"errors"
"github.com/threefoldtech/rivine/modules"
"github.com/threefoldtech/rivine/types"
)
var (
errOutOfBounds = errors.New("requesting transactions at unknown confirmation heights")
)
// AddressTransactions returns all of the wallet transactions associated with a
// single unlock hash.
func (w *Wallet) AddressTransactions(uh types.UnlockHash) (pts []modules.ProcessedTransaction, err error) {
w.mu.Lock()
defer w.mu.Unlock()
if !w.unlocked {
err = modules.ErrLockedWallet
return
}
for _, pt := range w.processedTransactions {
relevant := false
for _, input := range pt.Inputs {
if input.RelatedAddress == uh {
relevant = true
break
}
}
for _, output := range pt.Outputs {
if output.RelatedAddress == uh {
relevant = true
break
}
}
if relevant {
pts = append(pts, pt)
}
}
return
}
// AddressUnconfirmedTransactions returns all of the unconfirmed wallet transactions
// related to a specific address.
func (w *Wallet) AddressUnconfirmedTransactions(uh types.UnlockHash) (pts []modules.ProcessedTransaction, err error) {
w.mu.Lock()
defer w.mu.Unlock()
if !w.unlocked {
err = modules.ErrLockedWallet
return
}
// Scan the full list of unconfirmed transactions to see if there are any
// related transactions.
for _, pt := range w.unconfirmedProcessedTransactions {
relevant := false
for _, input := range pt.Inputs {
if input.RelatedAddress == uh {
relevant = true
break
}
}
for _, output := range pt.Outputs {
if output.RelatedAddress == uh {
relevant = true
break
}
}
if relevant {
pts = append(pts, pt)
}
}
return
}
// Transaction returns the transaction with the given id. 'False' is returned
// if the transaction does not exist.
func (w *Wallet) Transaction(txid types.TransactionID) (modules.ProcessedTransaction, bool, error) {
w.mu.Lock()
defer w.mu.Unlock()
if !w.unlocked {
return modules.ProcessedTransaction{}, false, modules.ErrLockedWallet
}
pt, exists := w.processedTransactionMap[txid]
if !exists {
return modules.ProcessedTransaction{}, exists, nil
}
return *pt, exists, nil
}
// Transactions returns all transactions relevant to the wallet that were
// confirmed in the range [startHeight, endHeight].
func (w *Wallet) Transactions(startHeight, endHeight types.BlockHeight) (pts []modules.ProcessedTransaction, err error) {
w.mu.Lock()
defer w.mu.Unlock()
if !w.unlocked {
err = modules.ErrLockedWallet
return
}
if startHeight > w.consensusSetHeight || startHeight > endHeight {
return nil, errOutOfBounds
}
if len(w.processedTransactions) == 0 {
return nil, nil
}
for _, pt := range w.processedTransactions {
if pt.ConfirmationHeight > endHeight {
break
}
if pt.ConfirmationHeight >= startHeight {
pts = append(pts, pt)
}
}
return pts, nil
}
// BlockStakeStats returns the blockstake statistical information of this wallet
func (w *Wallet) BlockStakeStats() (BCcountLast1000 uint64, BCfeeLast1000 types.Currency, BlockCount uint64, err error) {
w.mu.Lock()
defer w.mu.Unlock()
if !w.unlocked {
err = modules.ErrLockedWallet
return
}
BlockHeightCounter := w.cs.Height()
for BlockCount = 0; BlockCount < 1000; BlockCount++ {
block, _ := w.cs.BlockAtHeight(BlockHeightCounter)
ind := block.POBSOutput
blockOld, _ := w.cs.BlockAtHeight(ind.BlockHeight)
bso := blockOld.Transactions[ind.TransactionIndex].BlockStakeOutputs[ind.OutputIndex]
relevant := false
switch uh := bso.Condition.UnlockHash(); uh.Type {
case types.UnlockTypePubKey:
_, relevant = w.keys[uh]
case types.UnlockTypeNil:
relevant = true
case types.UnlockTypeMultiSig:
uhs, _ := getMultisigConditionProperties(bso.Condition.Condition)
if len(uhs) > 0 {
for _, uh := range uhs {
_, relevant = w.keys[uh]
if relevant {
break
}
}
}
}
if relevant {
BCcountLast1000++
BCfeeLast1000 = BCfeeLast1000.Add(w.chainCts.BlockCreatorFee)
if w.chainCts.TransactionFeeCondition.ConditionType() == types.ConditionTypeNil {
// only when tx fee beneficiary is not defined is the miner fees for the block creator
BCfeeLast1000 = BCfeeLast1000.Add(block.CalculateTotalMinerFees())
}
}
if BlockHeightCounter == 0 {
BlockCount++
break
}
BlockHeightCounter--
}
return
}
// UnconfirmedTransactions returns the set of unconfirmed transactions that are
// relevant to the wallet.
func (w *Wallet) UnconfirmedTransactions() ([]modules.ProcessedTransaction, error) {
w.mu.Lock()
defer w.mu.Unlock()
if !w.unlocked {
return nil, modules.ErrLockedWallet
}
return w.unconfirmedProcessedTransactions, nil
}
// CreateRawTransaction with the given inputs and outputs
func (w *Wallet) CreateRawTransaction(coids []types.CoinOutputID, bsoids []types.BlockStakeOutputID,
cos []types.CoinOutput, bsos []types.BlockStakeOutput, arb []byte) (types.Transaction, error) {
coinInputCount := types.ZeroCurrency
blockStakeInputCount := types.ZeroCurrency
// Make sure coin inputs and outputs + txnfee match
for _, id := range coids {
co, err := w.cs.GetCoinOutput(id)
if err != nil {
return types.Transaction{}, err
}
coinInputCount = coinInputCount.Add(co.Value)
}
requiredCoins := w.chainCts.MinimumTransactionFee
for _, co := range cos {
requiredCoins = requiredCoins.Add(co.Value)
}
if requiredCoins.Cmp(coinInputCount) != 0 {
return types.Transaction{}, errors.New("Mismatched coin input - output count")
}
for _, id := range bsoids {
bso, err := w.cs.GetBlockStakeOutput(id)
if err != nil {
return types.Transaction{}, err
}
blockStakeInputCount = blockStakeInputCount.Add(bso.Value)
}
requiredBlockStakes := types.ZeroCurrency
for _, bso := range bsos {
requiredBlockStakes = requiredBlockStakes.Add(bso.Value)
}
if requiredBlockStakes.Cmp(blockStakeInputCount) != 0 {
return types.Transaction{}, errors.New("Mismatched blockstake input - output count")
}
if uint64(len(arb)) > w.chainCts.ArbitraryDataSizeLimit {
return types.Transaction{}, errors.New("Arbitrary data too large")
}
txnBuilder := w.StartTransaction()
// No need to drop the builder since we manually added inputs,
// so they won't be consumed from the wallet
for _, ci := range coids {
txnBuilder.AddCoinInput(types.CoinInput{ParentID: ci})
}
for _, bsi := range bsoids {
txnBuilder.AddBlockStakeInput(types.BlockStakeInput{ParentID: bsi})
}
for _, co := range cos {
txnBuilder.AddCoinOutput(co)
}
for _, bso := range bsos {
txnBuilder.AddBlockStakeOutput(bso)
}
txnBuilder.AddMinerFee(w.chainCts.MinimumTransactionFee)
txnBuilder.SetArbitraryData(arb)
txn, _ := txnBuilder.View()
return txn, nil
}
// GreedySign attempts to sign every input in the transaction that can be signed
// using the keys loaded in this wallet. The transaction is assumed to be valid
func (w *Wallet) GreedySign(txn types.Transaction) (types.Transaction, error) {
txnBuilder := w.RegisterTransaction(txn, nil)
err := txnBuilder.SignAllPossible()
signedTxn, _ := txnBuilder.View()
return signedTxn, err
}