forked from btcsuite/btcwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendbulletin.go
293 lines (252 loc) · 8.16 KB
/
sendbulletin.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
package main
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"sort"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/soapboxsys/ombudslib/protocol/ombproto"
"github.com/soapboxsys/ombudslib/rpcexten"
"github.com/soapboxsys/ombwallet/chain"
"github.com/soapboxsys/ombwallet/txstore"
)
// Handles a sendbulletin json request. Attempts to send a bulletin from the
// specified address. If the address does not have enough funds or there is some
// other problem then the request throws a resonable error.
func SendBulletin(w *Wallet, chainSrv *chain.Client, icmd btcjson.Cmd) (interface{}, error) {
log.Trace("Starting SendBulletin")
msgtx, err := createBulletinTx(w, chainSrv, icmd)
if err != nil {
return nil, err
}
log.Trace("Inserting new tx into the TxStore.")
// Handle updating the TxStore
if err = insertIntoStore(w.TxStore, msgtx); err != nil {
return nil, err
}
txSha, err := chainSrv.SendRawTransaction(msgtx, false)
if err != nil {
return nil, err
}
log.Infof("Successfully sent bulletin %v", txSha)
return txSha.String(), nil
}
// ComposeBulletin creates a bulletin using the wallet's internal state and the
// the fields specified in the btcjson.cmd struct. It validates the returned tx
// and if successful, returns that wire.msgtx as a raw hex string.
func ComposeBulletin(w *Wallet, chainSrv *chain.Client, icmd btcjson.Cmd) (interface{}, error) {
log.Trace("Composing a Bulletin")
msgtx, err := createBulletinTx(w, chainSrv, icmd)
if err != nil {
return nil, err
}
mhx, err := messageToHex(msgtx)
if err != nil {
return nil, err
}
return mhx, nil
}
// createBulletinTx uses the wallets internal structures to produce a bulletin
// from the fields provided in the btcjson.cmd. Before the function returns it
// validates the txin scripts against the internal state of the wallet. If the
// scripts execute successfully, the function returns its 'valid' bulletin.
func createBulletinTx(w *Wallet, chainSrv *chain.Client, icmd btcjson.Cmd) (*wire.MsgTx, error) {
// NOTE because send and compose have the same fields this works.
cmd := icmd.(rpcexten.BulletinCmd)
// NOTE Rapid requests will serially block due to locking
heldUnlock, err := w.HoldUnlock()
if err != nil {
return nil, err
}
defer heldUnlock.Release()
log.Trace("Grabbed wallet lock")
addr, err := btcutil.DecodeAddress(cmd.GetAddress(), activeNet.Params)
if err != nil {
return nil, err
}
// NOTE checks to see if addr is in the wallet
_, err = w.Manager.Address(addr)
if err != nil {
log.Trace("The address is not in the manager")
return nil, err
}
bs, err := chainSrv.BlockStamp()
if err != nil {
return nil, err
}
log.Trace("Looking into elgible outputs")
// NOTE minconf is set to 1
var eligible []txstore.Credit
eligible, err = w.findEligibleOutputs(1, bs)
if err != nil {
return nil, err
}
msgtx := wire.NewMsgTx()
// Create the bulletin and add bulletin TxOuts to msgtx
addrStr, board, msg := cmd.GetAddress(), cmd.GetBoard(), cmd.GetMessage()
bltn, err := ombproto.NewBulletinFromStr(addrStr, board, msg)
if err != nil {
return nil, err
}
txouts, err := bltn.TxOuts(ombproto.DustAmnt(), activeNet.Params)
if err != nil {
return nil, err
}
// The amount of bitcoin burned by sending the bulletin
var totalBurn btcutil.Amount
for _, txout := range txouts {
msgtx.AddTxOut(txout)
totalBurn += btcutil.Amount(txout.Value)
}
log.Trace("Searching for a UTXO with target address.")
// Find the index of the credit with the target address and use that as the
// first txin in the bulletin.
i, err := findAddrCredit(eligible, addr)
if err != nil {
log.Trace("No eligible credits found for addr: ", addr)
return nil, err
}
authc := eligible[i]
// Add authoring txin
msgtx.AddTxIn(wire.NewTxIn(authc.OutPoint(), nil))
// Remove the author credit
eligible = append(eligible[:i], eligible[i+1:]...)
sort.Sort(sort.Reverse(ByAmount(eligible)))
totalAdded := authc.Amount()
inputs := []txstore.Credit{authc}
var input txstore.Credit
for totalAdded < totalBurn {
if len(eligible) == 0 {
return nil, InsufficientFundsError{totalAdded, totalBurn, 0}
}
input, eligible = eligible[0], eligible[1:]
inputs = append(inputs, input)
msgtx.AddTxIn(wire.NewTxIn(input.OutPoint(), nil))
totalAdded += input.Amount()
}
log.Trace("Estimating fee")
// Initial fee estimate
szEst := estimateTxSize(len(inputs), len(msgtx.TxOut))
feeEst := minimumFee(w.FeeIncrement, szEst, msgtx.TxOut, inputs, bs.Height)
// Ensure that we cover the fee and the total burn and if not add another
// input.
for totalAdded < totalBurn+feeEst {
if len(eligible) == 0 {
return nil, InsufficientFundsError{totalAdded, totalBurn, feeEst}
}
input, eligible = eligible[0], eligible[1:]
inputs = append(inputs, input)
msgtx.AddTxIn(wire.NewTxIn(input.OutPoint(), nil))
szEst += txInEstimate
totalAdded += input.Amount()
feeEst = minimumFee(w.FeeIncrement, szEst, msgtx.TxOut, inputs, bs.Height)
}
// Shameless copy from createtx
// changeIdx is -1 unless there's a change output.
changeIdx := -1
log.Trace("Formulating the transaction and computing fees")
for {
change := totalAdded - totalBurn - feeEst
if change > 0 {
// Send the change back to the authoring addr.
pkScript, err := txscript.PayToAddrScript(addr)
if err != nil {
return nil, err
}
msgtx.AddTxOut(wire.NewTxOut(int64(change), pkScript))
changeIdx = len(msgtx.TxOut) - 1
if err != nil {
return nil, err
}
}
log.Trace("Signing the transaction")
if err = signMsgTx(msgtx, inputs, w.Manager); err != nil {
return nil, err
}
if feeForSize(w.FeeIncrement, msgtx.SerializeSize()) <= feeEst {
// The required fee for this size is less than or equal to what
// we guessed, so we're done.
break
}
if change > 0 {
// Remove the change output since the next iteration will add
// it again (with a new amount) if necessary.
tmp := msgtx.TxOut[:changeIdx]
tmp = append(tmp, msgtx.TxOut[changeIdx+1:]...)
msgtx.TxOut = tmp
}
feeEst += w.FeeIncrement
for totalAdded < totalBurn+feeEst {
if len(eligible) == 0 {
return nil, InsufficientFundsError{totalAdded, totalBurn, feeEst}
}
input, eligible = eligible[0], eligible[1:]
inputs = append(inputs, input)
msgtx.AddTxIn(wire.NewTxIn(input.OutPoint(), nil))
szEst += txInEstimate
totalAdded += input.Amount()
feeEst = minimumFee(w.FeeIncrement, szEst, msgtx.TxOut, inputs, bs.Height)
}
}
if err := validateMsgTx(msgtx, inputs); err != nil {
return nil, err
}
return msgtx, nil
}
// TODO NOTICE
var ErrNoUnspentForAddr error = errors.New("No unspent outputs for this address")
// TODO NOTICE finds a credit that is a P2PKH to the target address
func findAddrCredit(credits []txstore.Credit, target btcutil.Address) (int, error) {
var idx int = -1
for i, credit := range credits {
class, addrs, _, err := credit.Addresses(activeNet.Params)
if err != nil {
return -1, err
}
switch class {
case txscript.PubKeyHashTy:
if target.EncodeAddress() == addrs[0].EncodeAddress() {
idx = i
break
}
// Ignore all non P2PKH txouts
default:
continue
}
}
if idx == -1 {
return -1, ErrNoUnspentForAddr
}
return idx, nil
}
// Inserts a new transaction into the TxStore, updating credits and debits
// of the store.
func insertIntoStore(store *txstore.Store, tx *wire.MsgTx) error {
// Add to the transaction store.
txr, err := store.InsertTx(btcutil.NewTx(tx), nil)
if err != nil {
log.Errorf("Error adding sent tx history: %v", err)
return btcjson.ErrInternal
}
_, err = txr.AddDebits()
if err != nil {
log.Errorf("Error adding sent tx history: %v", err)
return btcjson.ErrInternal
}
store.MarkDirty()
return nil
}
// messageToHex serializes a message to the wire protocol encoding using the
// latest protocol version and returns a hex-encoded string of the result.
func messageToHex(msg wire.Message) (string, error) {
var buf bytes.Buffer
if err := msg.BtcEncode(&buf, wire.ProtocolVersion); err != nil {
context := fmt.Sprintf("Failed to encode msg of type %T", msg)
return "", errors.New(context)
}
return hex.EncodeToString(buf.Bytes()), nil
}