9
9
"sync"
10
10
"testing"
11
11
12
+ "github.com/btcsuite/btcd/blockchain"
12
13
"github.com/btcsuite/btcd/btcec"
13
14
"github.com/btcsuite/btcd/btcjson"
14
15
"github.com/btcsuite/btcd/chaincfg"
@@ -31,6 +32,31 @@ type BTCSignerSuite struct {
31
32
32
33
var _ = Suite (& BTCSignerSuite {})
33
34
35
+ // 21 example UTXO txids to use in the test.
36
+ var exampleTxids = []string {
37
+ "c1729638e1c9b6bfca57d11bf93047d98b65594b0bf75d7ee68bf7dc80dc164e" ,
38
+ "54f9ebbd9e3ad39a297da54bf34a609b6831acbea0361cb5b7b5c8374f5046aa" ,
39
+ "b18a55a34319cfbedebfcfe1a80fef2b92ad8894d06caf8293a0344824c2cfbc" ,
40
+ "969fb309a4df7c299972700da788b5d601c0c04bab4ab46fff79d0335a7d75de" ,
41
+ "6c71913061246ffc20e268c1b0e65895055c36bfbf1f8faf92dcad6f8242121e" ,
42
+ "ba6d6e88cb5a97556684a1232719a3ffe409c5c9501061e1f59741bc412b3585" ,
43
+ "69b56c3c8c5d1851f9eaec256cd49f290b477a5d43e2aef42ef25d3c1d9f4b33" ,
44
+ "b87effd4cb46fe1a575b5b1ba0289313dc9b4bc9e615a3c6cbc0a14186921fdf" ,
45
+ "3135433054523f5e220621c9e3d48efbbb34a6a2df65635c2a3e7d462d3e1cda" ,
46
+ "8495c22a9ce6359ab53aa048c13b41c64fdf5fe141f516ba2573cc3f9313f06e" ,
47
+ "f31583544b475370d7b9187c9a01b92e44fb31ac5fcfa7fc55565ac64043aa9a" ,
48
+ "c03d55f9f717c1df978623e2e6b397b720999242f9ead7db9b5988fee3fb3933" ,
49
+ "ee55688439b47a5410cdc05bac46be0094f3af54d307456fdfe6ba8caf336e0b" ,
50
+ "61895f86c70f0bc3eef55d9a00347b509fa90f7a344606a9774be98a3ee9e02a" ,
51
+ "ffabb401a19d04327bd4a076671d48467dbcde95459beeab23df21686fd01525" ,
52
+ "b7e1c03b9b73e4e90fc06da893072c5604203c49e66699acbb2f61485d822981" ,
53
+ "185614d21973990138e478ce10e0a4014352df58044276d4e4c0093aa140f482" ,
54
+ "4a2800f13d15dc0c82308761d6fe8f6d13b65e42d7ca96a42a3a7048830e8c55" ,
55
+ "fb98f52e91db500735b185797cebb5848afbfe1289922d87e03b98c3da5b85ef" ,
56
+ "7901c5e36d9e8456ac61b29b82048650672a889596cbd30a9f8910a589ffc5b3" ,
57
+ "6bcd0850fd2fa1404290ed04d78d4ae718414f16d4fbfd344951add8dcf60326" ,
58
+ }
59
+
34
60
func (s * BTCSignerSuite ) SetUpTest (c * C ) {
35
61
// test private key with EVM address
36
62
//// EVM: 0x236C7f53a90493Bb423411fe4117Cb4c2De71DfB
@@ -219,11 +245,10 @@ func generateKeyPair(t *testing.T, net *chaincfg.Params) (*btcec.PrivateKey, []b
219
245
220
246
func addTxInputs (t * testing.T , tx * wire.MsgTx , txids []string ) {
221
247
preTxSize := tx .SerializeSize ()
222
- require .Equal (t , bytesEmptyTx , preTxSize )
223
- for i , txid := range txids {
248
+ for _ , txid := range txids {
224
249
hash , err := chainhash .NewHashFromStr (txid )
225
250
require .Nil (t , err )
226
- outpoint := wire .NewOutPoint (hash , uint32 (i % 3 ))
251
+ outpoint := wire .NewOutPoint (hash , uint32 (rand . Intn ( 100 ) ))
227
252
txIn := wire .NewTxIn (outpoint , nil , nil )
228
253
tx .AddTxIn (txIn )
229
254
require .Equal (t , bytesPerInput , tx .SerializeSize ()- preTxSize )
@@ -307,63 +332,71 @@ func TestP2WPHSize2In3Out(t *testing.T) {
307
332
// Payer sign the redeeming transaction.
308
333
signTx (t , tx , payerScript , privateKey )
309
334
310
- // Estimate the tx size
335
+ // Estimate the tx size in vByte
311
336
// #nosec G701 always positive
312
- txSize := uint64 (tx .SerializeSize ())
313
- sizeEstimated := EstimateSegWitTxSize (uint64 (len (utxosTxids )), 3 )
314
- require .Equal (t , outTxBytesMin , sizeEstimated )
315
- require .True (t , outTxBytesMin >= txSize )
316
- require .True (t , outTxBytesMin - txSize <= 2 ) // 2 witness may vary
337
+ vBytes := uint64 (blockchain .GetTransactionWeight (btcutil .NewTx (tx )) / blockchain .WitnessScaleFactor )
338
+ vBytesEstimated := EstimateSegWitTxSize (uint64 (len (utxosTxids )), 3 )
339
+ require .Equal (t , vBytes , vBytesEstimated )
340
+ require .Equal (t , vBytes , outTxBytesMin )
317
341
}
318
342
319
343
func TestP2WPHSize21In3Out (t * testing.T ) {
320
344
// Generate payer/payee private keys and P2WPKH addresss
321
345
privateKey , payerScript := generateKeyPair (t , & chaincfg .TestNet3Params )
322
346
_ , payeeScript := generateKeyPair (t , & chaincfg .TestNet3Params )
323
347
324
- // 21 example UTXO txids to use in the test.
325
- utxosTxids := []string {
326
- "c1729638e1c9b6bfca57d11bf93047d98b65594b0bf75d7ee68bf7dc80dc164e" ,
327
- "54f9ebbd9e3ad39a297da54bf34a609b6831acbea0361cb5b7b5c8374f5046aa" ,
328
- "b18a55a34319cfbedebfcfe1a80fef2b92ad8894d06caf8293a0344824c2cfbc" ,
329
- "969fb309a4df7c299972700da788b5d601c0c04bab4ab46fff79d0335a7d75de" ,
330
- "6c71913061246ffc20e268c1b0e65895055c36bfbf1f8faf92dcad6f8242121e" ,
331
- "ba6d6e88cb5a97556684a1232719a3ffe409c5c9501061e1f59741bc412b3585" ,
332
- "69b56c3c8c5d1851f9eaec256cd49f290b477a5d43e2aef42ef25d3c1d9f4b33" ,
333
- "b87effd4cb46fe1a575b5b1ba0289313dc9b4bc9e615a3c6cbc0a14186921fdf" ,
334
- "3135433054523f5e220621c9e3d48efbbb34a6a2df65635c2a3e7d462d3e1cda" ,
335
- "8495c22a9ce6359ab53aa048c13b41c64fdf5fe141f516ba2573cc3f9313f06e" ,
336
- "f31583544b475370d7b9187c9a01b92e44fb31ac5fcfa7fc55565ac64043aa9a" ,
337
- "c03d55f9f717c1df978623e2e6b397b720999242f9ead7db9b5988fee3fb3933" ,
338
- "ee55688439b47a5410cdc05bac46be0094f3af54d307456fdfe6ba8caf336e0b" ,
339
- "61895f86c70f0bc3eef55d9a00347b509fa90f7a344606a9774be98a3ee9e02a" ,
340
- "ffabb401a19d04327bd4a076671d48467dbcde95459beeab23df21686fd01525" ,
341
- "b7e1c03b9b73e4e90fc06da893072c5604203c49e66699acbb2f61485d822981" ,
342
- "185614d21973990138e478ce10e0a4014352df58044276d4e4c0093aa140f482" ,
343
- "4a2800f13d15dc0c82308761d6fe8f6d13b65e42d7ca96a42a3a7048830e8c55" ,
344
- "fb98f52e91db500735b185797cebb5848afbfe1289922d87e03b98c3da5b85ef" ,
345
- "7901c5e36d9e8456ac61b29b82048650672a889596cbd30a9f8910a589ffc5b3" ,
346
- "6bcd0850fd2fa1404290ed04d78d4ae718414f16d4fbfd344951add8dcf60326" ,
347
- }
348
-
349
348
// Create a new transaction and add inputs
350
349
tx := wire .NewMsgTx (wire .TxVersion )
351
- require .Equal (t , bytesEmptyTx , tx .SerializeSize ())
352
- addTxInputs (t , tx , utxosTxids )
350
+ addTxInputs (t , tx , exampleTxids )
353
351
354
352
// Add P2WPKH outputs
355
353
addTxOutputs (t , tx , payerScript , payeeScript )
356
354
357
355
// Payer sign the redeeming transaction.
358
356
signTx (t , tx , payerScript , privateKey )
359
357
360
- // Estimate the tx size
358
+ // Estimate the tx size in vByte
361
359
// #nosec G701 always positive
362
- txSize := uint64 (tx .SerializeSize ())
363
- sizeEstimated := EstimateSegWitTxSize (uint64 (len (utxosTxids )), 3 )
364
- require .Equal (t , outTxBytesMax , sizeEstimated )
365
- require .True (t , outTxBytesMax >= txSize )
366
- require .True (t , outTxBytesMax - txSize <= 21 ) // 21 witness may vary
360
+ vError := uint64 (21 / 4 ) // 5 vBytes error tolerance
361
+ vBytes := uint64 (blockchain .GetTransactionWeight (btcutil .NewTx (tx )) / blockchain .WitnessScaleFactor )
362
+ vBytesEstimated := EstimateSegWitTxSize (uint64 (len (exampleTxids )), 3 )
363
+ require .Equal (t , vBytesEstimated , outTxBytesMax )
364
+ if vBytes > vBytesEstimated {
365
+ require .True (t , vBytes - vBytesEstimated <= vError )
366
+ } else {
367
+ require .True (t , vBytesEstimated - vBytes <= vError )
368
+ }
369
+ }
370
+
371
+ func TestP2WPHSizeXIn3Out (t * testing.T ) {
372
+ // Generate payer/payee private keys and P2WPKH addresss
373
+ privateKey , payerScript := generateKeyPair (t , & chaincfg .TestNet3Params )
374
+ _ , payeeScript := generateKeyPair (t , & chaincfg .TestNet3Params )
375
+
376
+ // Create new transactions with X (2 <= X <= 21) inputs and 3 outputs respectively
377
+ for x := 2 ; x <= 21 ; x ++ {
378
+ tx := wire .NewMsgTx (wire .TxVersion )
379
+ addTxInputs (t , tx , exampleTxids [:x ])
380
+
381
+ // Add P2WPKH outputs
382
+ addTxOutputs (t , tx , payerScript , payeeScript )
383
+
384
+ // Payer sign the redeeming transaction.
385
+ signTx (t , tx , payerScript , privateKey )
386
+
387
+ // Estimate the tx size
388
+ // #nosec G701 always positive
389
+ vError := uint64 (0.25 + float64 (x )/ 4 ) // 1st witness incur 0.25 vByte error, other witness incur 1/4 vByte error tolerance,
390
+ vBytes := uint64 (blockchain .GetTransactionWeight (btcutil .NewTx (tx )) / blockchain .WitnessScaleFactor )
391
+ vBytesEstimated := EstimateSegWitTxSize (uint64 (len (exampleTxids [:x ])), 3 )
392
+ if vBytes > vBytesEstimated {
393
+ require .True (t , vBytes - vBytesEstimated <= vError )
394
+ //fmt.Printf("%d error percentage: %.2f%%\n", float64(vBytes-vBytesEstimated)/float64(vBytes)*100)
395
+ } else {
396
+ require .True (t , vBytesEstimated - vBytes <= vError )
397
+ //fmt.Printf("error percentage: %.2f%%\n", float64(vBytesEstimated-vBytes)/float64(vBytes)*100)
398
+ }
399
+ }
367
400
}
368
401
369
402
func TestP2WPHSizeBreakdown (t * testing.T ) {
@@ -374,14 +407,14 @@ func TestP2WPHSizeBreakdown(t *testing.T) {
374
407
fmt .Printf ("1 input, 1 output: %d\n " , sz )
375
408
376
409
txSizeDepositor := SegWitTxSizeDepositor ()
377
- require .Equal (t , uint64 (149 ), txSizeDepositor )
410
+ require .Equal (t , uint64 (68 ), txSizeDepositor )
378
411
379
412
txSizeWithdrawer := SegWitTxSizeWithdrawer ()
380
- require .Equal (t , uint64 (254 ), txSizeWithdrawer )
381
- require .Equal (t , txSize2In3Out , txSizeDepositor + txSizeWithdrawer ) // 403 = 149 + 254
413
+ require .Equal (t , uint64 (171 ), txSizeWithdrawer )
414
+ require .Equal (t , txSize2In3Out , txSizeDepositor + txSizeWithdrawer ) // 239 = 68 + 171
382
415
383
- depositFee := DepositorFee (5 )
384
- require .Equal (t , depositFee , 0.00000745 )
416
+ depositFee := DepositorFee (20 )
417
+ require .Equal (t , depositFee , 0.00001360 )
385
418
}
386
419
387
420
// helper function to create a new BitcoinChainClient
0 commit comments