decred.org/dcrwallet/v3@v3.1.0/wallet/txsizes/size_test.go (about) 1 package txsizes_test 2 3 import ( 4 "testing" 5 6 . "decred.org/dcrwallet/v3/wallet/txsizes" 7 "github.com/decred/dcrd/wire" 8 ) 9 10 const ( 11 p2pkhScriptSize = P2PKHPkScriptSize 12 p2shScriptSize = 23 13 ) 14 15 func makeScriptSizes(count int, size int) *[]int { 16 scriptSizes := make([]int, count) 17 for idx := 0; idx < count; idx++ { 18 scriptSizes[idx] = size 19 } 20 return &scriptSizes 21 } 22 23 func makeInts(value int, n int) []int { 24 v := make([]int, n) 25 for i := range v { 26 v[i] = value 27 } 28 return v 29 } 30 31 func TestEstimateSerializeSize(t *testing.T) { 32 tests := []struct { 33 InputScriptSizes []int 34 OutputScriptLengths []int 35 ChangeScriptSize int 36 ExpectedSizeEstimate int 37 }{ 38 0: {[]int{RedeemP2PKHSigScriptSize}, []int{}, 0, 181}, 39 1: {[]int{RedeemP2PKHSigScriptSize}, []int{p2pkhScriptSize}, 0, 217}, 40 2: {[]int{RedeemP2PKHSigScriptSize}, []int{}, p2pkhScriptSize, 217}, 41 3: {[]int{RedeemP2PKHSigScriptSize}, []int{p2pkhScriptSize}, p2pkhScriptSize, 253}, 42 4: {[]int{RedeemP2PKHSigScriptSize}, []int{p2shScriptSize}, 0, 215}, 43 5: {[]int{RedeemP2PKHSigScriptSize}, []int{p2shScriptSize}, p2pkhScriptSize, 251}, 44 45 6: {[]int{RedeemP2PKHSigScriptSize, RedeemP2PKHSigScriptSize}, []int{}, 0, 347}, 46 7: {[]int{RedeemP2PKHSigScriptSize, RedeemP2PKHSigScriptSize}, []int{p2pkhScriptSize}, 0, 383}, 47 8: {[]int{RedeemP2PKHSigScriptSize, RedeemP2PKHSigScriptSize}, []int{}, p2pkhScriptSize, 383}, 48 9: {[]int{RedeemP2PKHSigScriptSize, RedeemP2PKHSigScriptSize}, []int{p2pkhScriptSize}, p2pkhScriptSize, 419}, 49 10: {[]int{RedeemP2PKHSigScriptSize, RedeemP2PKHSigScriptSize}, []int{p2shScriptSize}, 0, 381}, 50 11: {[]int{RedeemP2PKHSigScriptSize, RedeemP2PKHSigScriptSize}, []int{p2shScriptSize}, p2pkhScriptSize, 417}, 51 52 // 0xfd is discriminant for 16-bit compact ints, compact int 53 // total size increases from 1 byte to 3. 54 12: {[]int{RedeemP2PKHSigScriptSize}, makeInts(p2pkhScriptSize, 0xfc), 0, 9253}, 55 13: {[]int{RedeemP2PKHSigScriptSize}, makeInts(p2pkhScriptSize, 0xfd), 0, 9253 + P2PKHOutputSize + 2}, 56 14: {[]int{RedeemP2PKHSigScriptSize}, makeInts(p2pkhScriptSize, 0xfc), p2pkhScriptSize, 9253 + P2PKHOutputSize + 2}, 57 15: {*makeScriptSizes(0xfc, RedeemP2PKHSigScriptSize), []int{}, 0, 41847}, 58 16: {*makeScriptSizes(0xfd, RedeemP2PKHSigScriptSize), []int{}, 0, 41847 + RedeemP2PKHInputSize + 4}, // 4 not 2, varint encoded twice. 59 } 60 for i, test := range tests { 61 outputs := make([]*wire.TxOut, 0, len(test.OutputScriptLengths)) 62 for _, l := range test.OutputScriptLengths { 63 outputs = append(outputs, &wire.TxOut{PkScript: make([]byte, l)}) 64 } 65 actualEstimate := EstimateSerializeSize(test.InputScriptSizes, outputs, test.ChangeScriptSize) 66 if actualEstimate != test.ExpectedSizeEstimate { 67 t.Errorf("Test %d: Got %v: Expected %v", i, actualEstimate, test.ExpectedSizeEstimate) 68 } 69 } 70 }