lbcwallet/wallet/internal/txsizes/size_test.go
Josh Rickmar f084802fec 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-03-08 17:42:27 -05:00

62 lines
1.8 KiB
Go

package txsizes_test
import (
"testing"
"github.com/btcsuite/btcd/wire"
. "github.com/btcsuite/btcwallet/wallet/internal/txsizes"
)
const (
p2pkhScriptSize = P2PKHPkScriptSize
p2shScriptSize = 23
)
func makeInts(value int, n int) []int {
v := make([]int, n)
for i := range v {
v[i] = value
}
return v
}
func TestEstimateSerializeSize(t *testing.T) {
tests := []struct {
InputCount int
OutputScriptLengths []int
AddChangeOutput bool
ExpectedSizeEstimate int
}{
0: {1, []int{}, false, 159},
1: {1, []int{p2pkhScriptSize}, false, 193},
2: {1, []int{}, true, 193},
3: {1, []int{p2pkhScriptSize}, true, 227},
4: {1, []int{p2shScriptSize}, false, 191},
5: {1, []int{p2shScriptSize}, true, 225},
6: {2, []int{}, false, 308},
7: {2, []int{p2pkhScriptSize}, false, 342},
8: {2, []int{}, true, 342},
9: {2, []int{p2pkhScriptSize}, true, 376},
10: {2, []int{p2shScriptSize}, false, 340},
11: {2, []int{p2shScriptSize}, true, 374},
// 0xfd is discriminant for 16-bit compact ints, compact int
// total size increases from 1 byte to 3.
12: {1, makeInts(p2pkhScriptSize, 0xfc), false, 8727},
13: {1, makeInts(p2pkhScriptSize, 0xfd), false, 8727 + P2PKHOutputSize + 2},
14: {1, makeInts(p2pkhScriptSize, 0xfc), true, 8727 + P2PKHOutputSize + 2},
15: {0xfc, []int{}, false, 37558},
16: {0xfd, []int{}, false, 37558 + RedeemP2PKHInputSize + 2},
}
for i, test := range tests {
outputs := make([]*wire.TxOut, 0, len(test.OutputScriptLengths))
for _, l := range test.OutputScriptLengths {
outputs = append(outputs, &wire.TxOut{PkScript: make([]byte, l)})
}
actualEstimate := EstimateSerializeSize(test.InputCount, outputs, test.AddChangeOutput)
if actualEstimate != test.ExpectedSizeEstimate {
t.Errorf("Test %d: Got %v: Expected %v", i, actualEstimate, test.ExpectedSizeEstimate)
}
}
}