2013-09-09 20:14:57 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Conformal Systems LLC <info@conformal.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/conformal/btcscript"
|
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
"github.com/conformal/btcwallet/tx"
|
2013-11-12 18:01:32 +01:00
|
|
|
"github.com/conformal/btcwallet/wallet"
|
2013-09-09 20:14:57 +02:00
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
"sort"
|
2013-10-07 21:14:39 +02:00
|
|
|
"sync"
|
2013-09-09 20:14:57 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrInsufficientFunds represents an error where there are not enough
|
|
|
|
// funds from unspent tx outputs for a wallet to create a transaction.
|
|
|
|
var ErrInsufficientFunds = errors.New("insufficient funds")
|
|
|
|
|
|
|
|
// ErrUnknownBitcoinNet represents an error where the parsed or
|
|
|
|
// requested bitcoin network is invalid (neither mainnet nor testnet).
|
|
|
|
var ErrUnknownBitcoinNet = errors.New("unknown bitcoin network")
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// ErrNonPositiveAmount represents an error where a bitcoin amount is
|
|
|
|
// not positive (either negative, or zero).
|
|
|
|
var ErrNonPositiveAmount = errors.New("amount is not positive")
|
|
|
|
|
|
|
|
// ErrNegativeFee represents an error where a fee is erroneously
|
|
|
|
// negative.
|
|
|
|
var ErrNegativeFee = errors.New("fee is negative")
|
|
|
|
|
2013-12-04 01:22:47 +01:00
|
|
|
// minTxFee is the default minimum transation fee (0.0001 BTC,
|
|
|
|
// measured in satoshis) added to transactions requiring a fee.
|
|
|
|
const minTxFee = 10000
|
|
|
|
|
|
|
|
// TxFeeIncrement represents the global transaction fee per KB of Tx
|
|
|
|
// added to newly-created transactions and sent as a reward to the block
|
|
|
|
// miner. i is measured in satoshis.
|
|
|
|
var TxFeeIncrement = struct {
|
2013-10-07 21:14:39 +02:00
|
|
|
sync.Mutex
|
|
|
|
i int64
|
2013-10-29 07:17:49 +01:00
|
|
|
}{
|
2013-12-04 01:22:47 +01:00
|
|
|
i: minTxFee,
|
2013-10-07 21:14:39 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 00:58:41 +01:00
|
|
|
// CreatedTx is a type holding information regarding a newly-created
|
|
|
|
// transaction, including the raw bytes, inputs, and an address and UTXO
|
|
|
|
// for change (if any).
|
|
|
|
type CreatedTx struct {
|
|
|
|
rawTx []byte
|
2013-12-17 19:18:09 +01:00
|
|
|
txid btcwire.ShaHash
|
2013-11-22 19:42:25 +01:00
|
|
|
time time.Time
|
2013-11-05 00:58:41 +01:00
|
|
|
inputs []*tx.Utxo
|
2013-11-22 19:42:25 +01:00
|
|
|
outputs []tx.Pair
|
2013-12-04 01:22:47 +01:00
|
|
|
btcspent int64
|
2013-11-22 19:42:25 +01:00
|
|
|
fee int64
|
2014-01-06 18:24:29 +01:00
|
|
|
changeAddr *btcutil.AddressPubKeyHash
|
2013-11-05 00:58:41 +01:00
|
|
|
changeUtxo *tx.Utxo
|
|
|
|
}
|
|
|
|
|
|
|
|
// TXID is a transaction hash identifying a transaction.
|
2013-11-22 19:42:25 +01:00
|
|
|
type TXID btcwire.ShaHash
|
2013-11-05 00:58:41 +01:00
|
|
|
|
2013-10-24 00:23:20 +02:00
|
|
|
// UnminedTXs holds a map of transaction IDs as keys mapping to a
|
2013-11-22 19:42:25 +01:00
|
|
|
// CreatedTx structure. If sending a raw transaction succeeds, the
|
|
|
|
// tx is added to this map and checked again after each new block.
|
|
|
|
// If the new block contains a tx, it is removed from this map.
|
2013-10-24 00:23:20 +02:00
|
|
|
var UnminedTxs = struct {
|
|
|
|
sync.Mutex
|
2013-11-05 00:58:41 +01:00
|
|
|
m map[TXID]*CreatedTx
|
2013-10-24 00:23:20 +02:00
|
|
|
}{
|
2013-11-05 00:58:41 +01:00
|
|
|
m: make(map[TXID]*CreatedTx),
|
2013-10-24 00:23:20 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// ByAmount defines the methods needed to satisify sort.Interface to
|
|
|
|
// sort a slice of Utxos by their amount.
|
|
|
|
type ByAmount []*tx.Utxo
|
|
|
|
|
|
|
|
func (u ByAmount) Len() int {
|
|
|
|
return len(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u ByAmount) Less(i, j int) bool {
|
|
|
|
return u[i].Amt < u[j].Amt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u ByAmount) Swap(i, j int) {
|
|
|
|
u[i], u[j] = u[j], u[i]
|
|
|
|
}
|
|
|
|
|
2013-10-14 22:14:04 +02:00
|
|
|
// selectInputs selects the minimum number possible of unspent
|
2013-09-09 20:14:57 +02:00
|
|
|
// outputs to use to create a new transaction that spends amt satoshis.
|
2013-10-14 22:14:04 +02:00
|
|
|
// Previous outputs with less than minconf confirmations are ignored. btcout
|
|
|
|
// is the total number of satoshis which would be spent by the combination
|
|
|
|
// of all selected previous outputs. err will equal ErrInsufficientFunds if there
|
2013-09-09 20:14:57 +02:00
|
|
|
// are not enough unspent outputs to spend amt.
|
2013-10-14 22:14:04 +02:00
|
|
|
func selectInputs(s tx.UtxoStore, amt uint64, minconf int) (inputs []*tx.Utxo, btcout uint64, err error) {
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2013-10-14 22:14:04 +02:00
|
|
|
// Create list of eligible unspent previous outputs to use as tx
|
|
|
|
// inputs, and sort by the amount in reverse order so a minimum number
|
|
|
|
// of inputs is needed.
|
2013-10-28 19:10:37 +01:00
|
|
|
eligible := make([]*tx.Utxo, 0, len(s))
|
2013-09-09 20:14:57 +02:00
|
|
|
for _, utxo := range s {
|
2013-10-28 19:10:37 +01:00
|
|
|
// TODO(jrick): if Height is -1, the UTXO is the result of spending
|
|
|
|
// to a change address, resulting in a UTXO not yet mined in a block.
|
|
|
|
// For now, disallow creating transactions until these UTXOs are mined
|
|
|
|
// into a block and show up as part of the balance.
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
if utxo.Height != -1 && int(bs.Height-utxo.Height) >= minconf {
|
2013-09-09 20:14:57 +02:00
|
|
|
eligible = append(eligible, utxo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(sort.Reverse(ByAmount(eligible)))
|
|
|
|
|
2013-10-14 22:14:04 +02:00
|
|
|
// Iterate throguh eligible transactions, appending to outputs and
|
2013-09-09 20:14:57 +02:00
|
|
|
// increasing btcout. This is finished when btcout is greater than the
|
|
|
|
// requested amt to spend.
|
|
|
|
for _, u := range eligible {
|
2013-10-14 22:14:04 +02:00
|
|
|
inputs = append(inputs, u)
|
2013-09-09 20:14:57 +02:00
|
|
|
if btcout += u.Amt; btcout >= amt {
|
2013-10-14 22:14:04 +02:00
|
|
|
return inputs, btcout, nil
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if btcout < amt {
|
|
|
|
return nil, 0, ErrInsufficientFunds
|
|
|
|
}
|
|
|
|
|
2013-10-14 22:14:04 +02:00
|
|
|
return inputs, btcout, nil
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// txToPairs creates a raw transaction sending the amounts for each
|
|
|
|
// address/amount pair and fee to each address and the miner. minconf
|
|
|
|
// specifies the minimum number of confirmations required before an
|
|
|
|
// unspent output is eligible for spending. Leftover input funds not sent
|
|
|
|
// to addr or as a fee for the miner are sent to a newly generated
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
// address. If change is needed to return funds back to an owned
|
|
|
|
// address, changeUtxo will point to a unconfirmed (height = -1, zeroed
|
|
|
|
// block hash) Utxo. ErrInsufficientFunds is returned if there are not
|
|
|
|
// enough eligible unspent outputs to create the transaction.
|
2013-12-04 01:22:47 +01:00
|
|
|
func (a *Account) txToPairs(pairs map[string]int64, minconf int) (*CreatedTx, error) {
|
2013-09-09 20:14:57 +02:00
|
|
|
// Recorded unspent transactions should not be modified until this
|
|
|
|
// finishes.
|
2013-12-04 01:22:47 +01:00
|
|
|
a.UtxoStore.RLock()
|
|
|
|
defer a.UtxoStore.RUnlock()
|
2013-09-09 20:14:57 +02:00
|
|
|
|
|
|
|
// Create a new transaction which will include all input scripts.
|
|
|
|
msgtx := btcwire.NewMsgTx()
|
|
|
|
|
|
|
|
// Calculate minimum amount needed for inputs.
|
2013-11-12 18:01:32 +01:00
|
|
|
var amt int64
|
2013-09-09 20:14:57 +02:00
|
|
|
for _, v := range pairs {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Error out if any amount is negative.
|
|
|
|
if v <= 0 {
|
|
|
|
return nil, ErrNonPositiveAmount
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
amt += v
|
|
|
|
}
|
|
|
|
|
2013-11-22 19:42:25 +01:00
|
|
|
// outputs is a tx.Pair slice representing each output that is created
|
|
|
|
// by the transaction.
|
|
|
|
outputs := make([]tx.Pair, 0, len(pairs)+1)
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// Add outputs to new tx.
|
2014-01-06 18:24:29 +01:00
|
|
|
for addrStr, amt := range pairs {
|
|
|
|
addr, err := btcutil.DecodeAddr(addrStr)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot decode address: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2014-01-06 18:24:29 +01:00
|
|
|
// Add output to spend amt to addr.
|
|
|
|
pkScript, err := btcscript.PayToAddrScript(addr)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot create txout script: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
txout := btcwire.NewTxOut(int64(amt), pkScript)
|
|
|
|
msgtx.AddTxOut(txout)
|
2013-11-22 19:42:25 +01:00
|
|
|
|
|
|
|
// Create amount, address pair and add to outputs.
|
|
|
|
out := tx.Pair{
|
|
|
|
Amount: amt,
|
2014-01-06 18:24:29 +01:00
|
|
|
PubkeyHash: addr.ScriptAddress(),
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
|
|
|
outputs = append(outputs, out)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2013-12-04 01:22:47 +01:00
|
|
|
// Get current block's height and hash.
|
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a copy of msgtx before any inputs are added. This will be
|
|
|
|
// used as a starting point when trying a fee and starting over with
|
|
|
|
// a higher fee if not enough was originally chosen.
|
|
|
|
txNoInputs := msgtx.Copy()
|
|
|
|
|
|
|
|
// These are nil/zeroed until a change address is needed, and reused
|
|
|
|
// again in case a change utxo has already been chosen.
|
2014-01-06 18:24:29 +01:00
|
|
|
var changeAddr *btcutil.AddressPubKeyHash
|
2013-11-21 17:57:28 +01:00
|
|
|
|
2013-12-04 01:22:47 +01:00
|
|
|
var btcspent int64
|
|
|
|
var selectedInputs []*tx.Utxo
|
|
|
|
var finalChangeUtxo *tx.Utxo
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2013-12-04 01:22:47 +01:00
|
|
|
// Get the number of satoshis to increment fee by when searching for
|
|
|
|
// the minimum tx fee needed.
|
|
|
|
var fee int64 = 0
|
|
|
|
for {
|
|
|
|
msgtx = txNoInputs.Copy()
|
|
|
|
|
|
|
|
// Select unspent outputs to be used in transaction based on the amount
|
|
|
|
// neededing to sent, and the current fee estimation.
|
|
|
|
inputs, btcin, err := selectInputs(a.UtxoStore.s, uint64(amt+fee),
|
|
|
|
minconf)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-12-04 01:22:47 +01:00
|
|
|
return nil, err
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
}
|
2013-11-22 19:42:25 +01:00
|
|
|
|
2013-12-04 01:22:47 +01:00
|
|
|
// Check if there are leftover unspent outputs, and return coins back to
|
|
|
|
// a new address we own.
|
|
|
|
var changeUtxo *tx.Utxo
|
|
|
|
change := btcin - uint64(amt+fee)
|
|
|
|
if change > 0 {
|
|
|
|
// Create a new address to spend leftover outputs to.
|
|
|
|
|
|
|
|
// Get a new change address if one has not already been found.
|
2014-01-06 18:24:29 +01:00
|
|
|
if changeAddr == nil {
|
2013-12-04 01:22:47 +01:00
|
|
|
changeAddr, err = a.NextChainedAddress(&bs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get next address: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-12-10 22:15:25 +01:00
|
|
|
// Mark change address as belonging to this account.
|
2014-01-06 18:24:29 +01:00
|
|
|
MarkAddressForAccount(changeAddr.EncodeAddress(), a.Name())
|
2013-12-04 01:22:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Spend change.
|
2014-01-06 18:24:29 +01:00
|
|
|
pkScript, err := btcscript.PayToAddrScript(changeAddr)
|
2013-12-04 01:22:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create txout script: %s", err)
|
|
|
|
}
|
|
|
|
msgtx.AddTxOut(btcwire.NewTxOut(int64(change), pkScript))
|
|
|
|
|
|
|
|
changeUtxo = &tx.Utxo{
|
|
|
|
Amt: change,
|
|
|
|
Out: tx.OutPoint{
|
|
|
|
// Hash is unset (zeroed) here and must be filled in
|
|
|
|
// with the transaction hash of the complete
|
|
|
|
// transaction.
|
|
|
|
Index: uint32(len(pairs)),
|
|
|
|
},
|
|
|
|
Height: -1,
|
|
|
|
Subscript: pkScript,
|
|
|
|
}
|
2014-01-06 18:24:29 +01:00
|
|
|
copy(changeUtxo.AddrHash[:], changeAddr.ScriptAddress())
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2013-12-04 01:22:47 +01:00
|
|
|
// Selected unspent outputs become new transaction's inputs.
|
|
|
|
for _, ip := range inputs {
|
|
|
|
msgtx.AddTxIn(btcwire.NewTxIn((*btcwire.OutPoint)(&ip.Out), nil))
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-12-04 01:22:47 +01:00
|
|
|
for i, ip := range inputs {
|
2014-01-06 18:24:29 +01:00
|
|
|
// Error is ignored as the length and network checks can never fail
|
|
|
|
// for these inputs.
|
|
|
|
addr, _ := btcutil.NewAddressPubKeyHash(ip.AddrHash[:],
|
2013-12-04 01:22:47 +01:00
|
|
|
a.Wallet.Net())
|
2014-01-06 18:24:29 +01:00
|
|
|
privkey, err := a.AddressKey(addr)
|
2013-12-04 01:22:47 +01:00
|
|
|
if err == wallet.ErrWalletLocked {
|
|
|
|
return nil, wallet.ErrWalletLocked
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get address key: %v", err)
|
|
|
|
}
|
2014-01-06 18:24:29 +01:00
|
|
|
ai, err := a.AddressInfo(addr)
|
2013-12-04 01:22:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get address info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sigscript, err := btcscript.SignatureScript(msgtx, i,
|
|
|
|
ip.Subscript, btcscript.SigHashAll, privkey,
|
|
|
|
ai.Compressed)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create sigscript: %s", err)
|
|
|
|
}
|
|
|
|
msgtx.TxIn[i].SignatureScript = sigscript
|
2013-11-04 17:50:32 +01:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2013-12-04 01:22:47 +01:00
|
|
|
noFeeAllowed := allowFree(bs.Height, inputs, msgtx.SerializeSize())
|
|
|
|
if minFee := minimumFee(msgtx, noFeeAllowed); fee < minFee {
|
|
|
|
fee = minFee
|
|
|
|
} else {
|
|
|
|
// Fill Tx hash of change outpoint with transaction hash.
|
|
|
|
if changeUtxo != nil {
|
|
|
|
txHash, err := msgtx.TxSha()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create transaction hash: %s", err)
|
|
|
|
}
|
|
|
|
copy(changeUtxo.Out.Hash[:], txHash[:])
|
|
|
|
|
|
|
|
// Add change to outputs.
|
|
|
|
out := tx.Pair{
|
|
|
|
Amount: int64(change),
|
2014-01-06 18:24:29 +01:00
|
|
|
PubkeyHash: changeAddr.ScriptAddress(),
|
2013-12-17 19:18:09 +01:00
|
|
|
Change: true,
|
2013-12-04 01:22:47 +01:00
|
|
|
}
|
|
|
|
outputs = append(outputs, out)
|
|
|
|
|
|
|
|
finalChangeUtxo = changeUtxo
|
|
|
|
}
|
|
|
|
|
|
|
|
selectedInputs = inputs
|
|
|
|
|
|
|
|
btcspent = int64(btcin)
|
|
|
|
|
|
|
|
break
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate msgtx before returning the raw transaction.
|
2013-10-25 21:21:38 +02:00
|
|
|
flags := btcscript.ScriptCanonicalSignatures
|
2013-10-11 16:53:55 +02:00
|
|
|
bip16 := time.Now().After(btcscript.Bip16Activation)
|
2013-10-25 21:21:38 +02:00
|
|
|
if bip16 {
|
|
|
|
flags |= btcscript.ScriptBip16
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
for i, txin := range msgtx.TxIn {
|
2013-12-04 01:22:47 +01:00
|
|
|
engine, err := btcscript.NewScript(txin.SignatureScript,
|
|
|
|
selectedInputs[i].Subscript, i, msgtx, flags)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot create script engine: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
if err = engine.Execute(); err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot validate transaction: %s", err)
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 19:18:09 +01:00
|
|
|
txid, err := msgtx.TxSha()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create txid for created tx: %v", err)
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
msgtx.BtcEncode(buf, btcwire.ProtocolVersion)
|
2013-11-22 19:42:25 +01:00
|
|
|
info := &CreatedTx{
|
|
|
|
rawTx: buf.Bytes(),
|
2013-12-17 19:18:09 +01:00
|
|
|
txid: txid,
|
2013-11-22 19:42:25 +01:00
|
|
|
time: time.Now(),
|
2013-12-04 01:22:47 +01:00
|
|
|
inputs: selectedInputs,
|
2013-11-22 19:42:25 +01:00
|
|
|
outputs: outputs,
|
2013-12-04 01:22:47 +01:00
|
|
|
btcspent: btcspent,
|
2013-11-22 19:42:25 +01:00
|
|
|
fee: fee,
|
|
|
|
changeAddr: changeAddr,
|
2013-12-04 01:22:47 +01:00
|
|
|
changeUtxo: finalChangeUtxo,
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
|
|
|
return info, nil
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-12-04 01:22:47 +01:00
|
|
|
|
|
|
|
// minimumFee calculates the minimum fee required for a transaction.
|
|
|
|
// If allowFree is true, a fee may be zero so long as the entire
|
|
|
|
// transaction has a serialized length less than 1 kilobyte
|
|
|
|
// and none of the outputs contain a value less than 1 bitcent.
|
|
|
|
// Otherwise, the fee will be calculated using TxFeeIncrement,
|
|
|
|
// incrementing the fee for each kilobyte of transaction.
|
|
|
|
func minimumFee(tx *btcwire.MsgTx, allowFree bool) int64 {
|
|
|
|
txLen := tx.SerializeSize()
|
|
|
|
TxFeeIncrement.Lock()
|
|
|
|
incr := TxFeeIncrement.i
|
|
|
|
TxFeeIncrement.Unlock()
|
|
|
|
fee := int64(1+txLen/1000) * incr
|
|
|
|
|
|
|
|
if allowFree && txLen < 1000 {
|
|
|
|
fee = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if fee < incr {
|
|
|
|
for _, txOut := range tx.TxOut {
|
|
|
|
if txOut.Value < btcutil.SatoshiPerBitcent {
|
|
|
|
return incr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fee < 0 || fee > btcutil.MaxSatoshi {
|
|
|
|
fee = btcutil.MaxSatoshi
|
|
|
|
}
|
|
|
|
|
|
|
|
return fee
|
|
|
|
}
|
|
|
|
|
|
|
|
// allowFree calculates the transaction priority and checks that the
|
|
|
|
// priority reaches a certain threshhold. If the threshhold is
|
|
|
|
// reached, a free transaction fee is allowed.
|
|
|
|
func allowFree(curHeight int32, inputs []*tx.Utxo, txSize int) bool {
|
|
|
|
const blocksPerDayEstimate = 144
|
|
|
|
const txSizeEstimate = 250
|
|
|
|
|
|
|
|
var weightedSum int64
|
|
|
|
for _, utxo := range inputs {
|
|
|
|
depth := chainDepth(utxo.Height, curHeight)
|
|
|
|
weightedSum += int64(utxo.Amt) * int64(depth)
|
|
|
|
}
|
|
|
|
priority := float64(weightedSum) / float64(txSize)
|
|
|
|
return priority > float64(btcutil.SatoshiPerBitcoin)*blocksPerDayEstimate/txSizeEstimate
|
|
|
|
}
|
|
|
|
|
|
|
|
// chainDepth returns the chaindepth of a target given the current
|
|
|
|
// blockchain height.
|
|
|
|
func chainDepth(target, current int32) int32 {
|
|
|
|
if target == -1 {
|
|
|
|
// target is not yet in a block.
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// target is in a block.
|
|
|
|
return current - target + 1
|
|
|
|
}
|