lbcwallet/wallet/txauthor/author_test.go

220 lines
6.6 KiB
Go
Raw Normal View History

Refactor wallet transaction creation code. This began as a change to improve the fee calculation code and evolved into a much larger refactor which improves the readability and modularity of all of the transaction creation code. Transaction fee calculations have been switched from full increments of the relay fee to a proportion based on the transaction size. This means that for a relay fee of 1e3 satoshis/kB, a 500 byte transaction is only required to pay a 5e2 satoshi fee and a 1500 byte transaction only need pay a 1.5e3 fee. The previous code would end up estimating these fees to be 1e3 and 2e3 respectively. Because the previous code would add more fee than needed in almost every case, the transaction size estimations were optimistic (best/smallest case) and signing was done in a loop where the fee was incremented by the relay fee again each time the actual size of the signed transaction rendered the fee too low. This has switched to using worst case transaction size estimates rather than best case, and signing is only performed once. Transaction input signature creation has switched from using txscript.SignatureScript to txscript.SignTxOutput. The new API is able to redeem outputs other than just P2PKH, so the previous restrictions about P2SH outputs being unspendable (except through the signrawtransaction RPC) no longer hold. Several new public packages have been added: wallet/txauthor - transaction authoring and signing wallet/txfees - fee estimations and change output inclusion wallet/txrules - simple consensus and mempool policy rule checks Along with some internal packages: wallet/internal/txsizes - transaction size estimation internal/helpers - context free convenience functions The txsizes package is internal as the estimations it provides are specific for the algorithms used by these new packages.
2016-02-28 05:30:56 +01:00
// Copyright (c) 2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package txauthor_test
import (
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
. "github.com/btcsuite/btcwallet/wallet/txauthor"
"github.com/btcsuite/btcwallet/wallet/txrules"
"github.com/btcsuite/btcwallet/wallet/internal/txsizes"
)
func p2pkhOutputs(amounts ...btcutil.Amount) []*wire.TxOut {
v := make([]*wire.TxOut, 0, len(amounts))
for _, a := range amounts {
outScript := make([]byte, txsizes.P2PKHOutputSize)
v = append(v, wire.NewTxOut(int64(a), outScript))
}
return v
}
func makeInputSource(unspents []*wire.TxOut) InputSource {
// Return outputs in order.
currentTotal := btcutil.Amount(0)
currentInputs := make([]*wire.TxIn, 0, len(unspents))
f := func(target btcutil.Amount) (btcutil.Amount, []*wire.TxIn, [][]byte, error) {
for currentTotal < target && len(unspents) != 0 {
u := unspents[0]
unspents = unspents[1:]
nextInput := wire.NewTxIn(&wire.OutPoint{}, nil)
currentTotal += btcutil.Amount(u.Value)
currentInputs = append(currentInputs, nextInput)
}
return currentTotal, currentInputs, make([][]byte, len(currentInputs)), nil
}
return InputSource(f)
}
func TestNewUnsignedTransaction(t *testing.T) {
tests := []struct {
UnspentOutputs []*wire.TxOut
Outputs []*wire.TxOut
RelayFee btcutil.Amount
ChangeAmount btcutil.Amount
InputSourceError bool
InputCount int
}{
0: {
UnspentOutputs: p2pkhOutputs(1e8),
Outputs: p2pkhOutputs(1e8),
RelayFee: 1e3,
InputSourceError: true,
},
1: {
UnspentOutputs: p2pkhOutputs(1e8),
Outputs: p2pkhOutputs(1e6),
RelayFee: 1e3,
ChangeAmount: 1e8 - 1e6 - txrules.FeeForSerializeSize(1e3,
txsizes.EstimateSerializeSize(1, p2pkhOutputs(1e6), true)),
InputCount: 1,
},
2: {
UnspentOutputs: p2pkhOutputs(1e8),
Outputs: p2pkhOutputs(1e6),
RelayFee: 1e4,
ChangeAmount: 1e8 - 1e6 - txrules.FeeForSerializeSize(1e4,
txsizes.EstimateSerializeSize(1, p2pkhOutputs(1e6), true)),
InputCount: 1,
},
3: {
UnspentOutputs: p2pkhOutputs(1e8),
Outputs: p2pkhOutputs(1e6, 1e6, 1e6),
RelayFee: 1e4,
ChangeAmount: 1e8 - 3e6 - txrules.FeeForSerializeSize(1e4,
txsizes.EstimateSerializeSize(1, p2pkhOutputs(1e6, 1e6, 1e6), true)),
InputCount: 1,
},
4: {
UnspentOutputs: p2pkhOutputs(1e8),
Outputs: p2pkhOutputs(1e6, 1e6, 1e6),
RelayFee: 2.55e3,
ChangeAmount: 1e8 - 3e6 - txrules.FeeForSerializeSize(2.55e3,
txsizes.EstimateSerializeSize(1, p2pkhOutputs(1e6, 1e6, 1e6), true)),
InputCount: 1,
},
// Test dust thresholds (546 for a 1e3 relay fee).
5: {
UnspentOutputs: p2pkhOutputs(1e8),
Outputs: p2pkhOutputs(1e8 - 545 - txrules.FeeForSerializeSize(1e3,
txsizes.EstimateSerializeSize(1, p2pkhOutputs(0), true))),
RelayFee: 1e3,
ChangeAmount: 0,
InputCount: 1,
},
6: {
UnspentOutputs: p2pkhOutputs(1e8),
Outputs: p2pkhOutputs(1e8 - 546 - txrules.FeeForSerializeSize(1e3,
txsizes.EstimateSerializeSize(1, p2pkhOutputs(0), true))),
RelayFee: 1e3,
ChangeAmount: 546,
InputCount: 1,
},
// Test dust thresholds (1392.3 for a 2.55e3 relay fee).
7: {
UnspentOutputs: p2pkhOutputs(1e8),
Outputs: p2pkhOutputs(1e8 - 1392 - txrules.FeeForSerializeSize(2.55e3,
txsizes.EstimateSerializeSize(1, p2pkhOutputs(0), true))),
RelayFee: 2.55e3,
ChangeAmount: 0,
InputCount: 1,
},
8: {
UnspentOutputs: p2pkhOutputs(1e8),
Outputs: p2pkhOutputs(1e8 - 1393 - txrules.FeeForSerializeSize(2.55e3,
txsizes.EstimateSerializeSize(1, p2pkhOutputs(0), true))),
RelayFee: 2.55e3,
ChangeAmount: 1393,
InputCount: 1,
},
// Test two unspent outputs available but only one needed
// (tested fee only includes one input rather than using a
// serialize size for each).
9: {
UnspentOutputs: p2pkhOutputs(1e8, 1e8),
Outputs: p2pkhOutputs(1e8 - 546 - txrules.FeeForSerializeSize(1e3,
txsizes.EstimateSerializeSize(1, p2pkhOutputs(0), true))),
RelayFee: 1e3,
ChangeAmount: 546,
InputCount: 1,
},
// Test that second output is not included to make the change
// output not dust and be included in the transaction.
//
// It's debatable whether or not this is a good idea, but it's
// how the function was written, so test it anyways.
10: {
UnspentOutputs: p2pkhOutputs(1e8, 1e8),
Outputs: p2pkhOutputs(1e8 - 545 - txrules.FeeForSerializeSize(1e3,
txsizes.EstimateSerializeSize(1, p2pkhOutputs(0), true))),
RelayFee: 1e3,
ChangeAmount: 0,
InputCount: 1,
},
// Test two unspent outputs available where both are needed.
11: {
UnspentOutputs: p2pkhOutputs(1e8, 1e8),
Outputs: p2pkhOutputs(1e8),
RelayFee: 1e3,
ChangeAmount: 1e8 - txrules.FeeForSerializeSize(1e3,
txsizes.EstimateSerializeSize(2, p2pkhOutputs(1e8), true)),
InputCount: 2,
},
// Test that zero change outputs are not included
// (ChangeAmount=0 means don't include any change output).
12: {
UnspentOutputs: p2pkhOutputs(1e8),
Outputs: p2pkhOutputs(1e8),
RelayFee: 0,
ChangeAmount: 0,
InputCount: 1,
},
Refactor wallet transaction creation code. This began as a change to improve the fee calculation code and evolved into a much larger refactor which improves the readability and modularity of all of the transaction creation code. Transaction fee calculations have been switched from full increments of the relay fee to a proportion based on the transaction size. This means that for a relay fee of 1e3 satoshis/kB, a 500 byte transaction is only required to pay a 5e2 satoshi fee and a 1500 byte transaction only need pay a 1.5e3 fee. The previous code would end up estimating these fees to be 1e3 and 2e3 respectively. Because the previous code would add more fee than needed in almost every case, the transaction size estimations were optimistic (best/smallest case) and signing was done in a loop where the fee was incremented by the relay fee again each time the actual size of the signed transaction rendered the fee too low. This has switched to using worst case transaction size estimates rather than best case, and signing is only performed once. Transaction input signature creation has switched from using txscript.SignatureScript to txscript.SignTxOutput. The new API is able to redeem outputs other than just P2PKH, so the previous restrictions about P2SH outputs being unspendable (except through the signrawtransaction RPC) no longer hold. Several new public packages have been added: wallet/txauthor - transaction authoring and signing wallet/txfees - fee estimations and change output inclusion wallet/txrules - simple consensus and mempool policy rule checks Along with some internal packages: wallet/internal/txsizes - transaction size estimation internal/helpers - context free convenience functions The txsizes package is internal as the estimations it provides are specific for the algorithms used by these new packages.
2016-02-28 05:30:56 +01:00
}
changeSource := func() ([]byte, error) {
// Only length matters for these tests.
return make([]byte, txsizes.P2PKHPkScriptSize), nil
}
for i, test := range tests {
inputSource := makeInputSource(test.UnspentOutputs)
tx, err := NewUnsignedTransaction(test.Outputs, test.RelayFee, inputSource, changeSource)
switch e := err.(type) {
case nil:
case InputSourceError:
if !test.InputSourceError {
t.Errorf("Test %d: Returned InputSourceError but expected "+
"change output with amount %v", i, test.ChangeAmount)
}
continue
default:
t.Errorf("Test %d: Unexpected error: %v", i, e)
continue
}
if tx.ChangeIndex < 0 {
if test.ChangeAmount != 0 {
t.Errorf("Test %d: No change output added but expected output with amount %v",
i, test.ChangeAmount)
continue
}
} else {
changeAmount := btcutil.Amount(tx.Tx.TxOut[tx.ChangeIndex].Value)
if test.ChangeAmount == 0 {
t.Errorf("Test %d: Included change output with value %v but expected no change",
i, changeAmount)
continue
}
Refactor wallet transaction creation code. This began as a change to improve the fee calculation code and evolved into a much larger refactor which improves the readability and modularity of all of the transaction creation code. Transaction fee calculations have been switched from full increments of the relay fee to a proportion based on the transaction size. This means that for a relay fee of 1e3 satoshis/kB, a 500 byte transaction is only required to pay a 5e2 satoshi fee and a 1500 byte transaction only need pay a 1.5e3 fee. The previous code would end up estimating these fees to be 1e3 and 2e3 respectively. Because the previous code would add more fee than needed in almost every case, the transaction size estimations were optimistic (best/smallest case) and signing was done in a loop where the fee was incremented by the relay fee again each time the actual size of the signed transaction rendered the fee too low. This has switched to using worst case transaction size estimates rather than best case, and signing is only performed once. Transaction input signature creation has switched from using txscript.SignatureScript to txscript.SignTxOutput. The new API is able to redeem outputs other than just P2PKH, so the previous restrictions about P2SH outputs being unspendable (except through the signrawtransaction RPC) no longer hold. Several new public packages have been added: wallet/txauthor - transaction authoring and signing wallet/txfees - fee estimations and change output inclusion wallet/txrules - simple consensus and mempool policy rule checks Along with some internal packages: wallet/internal/txsizes - transaction size estimation internal/helpers - context free convenience functions The txsizes package is internal as the estimations it provides are specific for the algorithms used by these new packages.
2016-02-28 05:30:56 +01:00
if changeAmount != test.ChangeAmount {
t.Errorf("Test %d: Got change amount %v, Expected %v",
i, changeAmount, test.ChangeAmount)
continue
}
}
if len(tx.Tx.TxIn) != test.InputCount {
t.Errorf("Test %d: Used %d outputs from input source, Expected %d",
i, len(tx.Tx.TxIn), test.InputCount)
}
}
}