@@ -456,13 +456,15 @@ func (msg *MsgTx) Copy() *MsgTx {
456
456
// database, as opposed to decoding transactions from the wire.
457
457
func (msg * MsgTx ) BtcDecode (r io.Reader , pver uint32 , enc MessageEncoding ) error {
458
458
buf := binarySerializer .Borrow ()
459
- err := msg .btcDecode (r , pver , enc , buf )
459
+ sbuf := scriptPool .Borrow ()
460
+ err := msg .btcDecode (r , pver , enc , buf , sbuf [:])
461
+ scriptPool .Return (sbuf )
460
462
binarySerializer .Return (buf )
461
463
return err
462
464
}
463
465
464
466
func (msg * MsgTx ) btcDecode (r io.Reader , pver uint32 , enc MessageEncoding ,
465
- buf []byte ) error {
467
+ buf , sbuf []byte ) error {
466
468
467
469
if _ , err := io .ReadFull (r , buf [:4 ]); err != nil {
468
470
return err
@@ -509,9 +511,6 @@ func (msg *MsgTx) btcDecode(r io.Reader, pver uint32, enc MessageEncoding,
509
511
return messageError ("MsgTx.BtcDecode" , str )
510
512
}
511
513
512
- scriptBuf := scriptPool .Borrow ()
513
- sbuf := scriptBuf [:]
514
-
515
514
// Deserialize the inputs.
516
515
var totalScriptSize uint64
517
516
txIns := make ([]TxIn , count )
@@ -523,7 +522,6 @@ func (msg *MsgTx) btcDecode(r io.Reader, pver uint32, enc MessageEncoding,
523
522
msg .TxIn [i ] = ti
524
523
err = readTxInBuf (r , pver , msg .Version , ti , buf , sbuf )
525
524
if err != nil {
526
- scriptPool .Return (scriptBuf )
527
525
return err
528
526
}
529
527
totalScriptSize += uint64 (len (ti .SignatureScript ))
@@ -532,15 +530,13 @@ func (msg *MsgTx) btcDecode(r io.Reader, pver uint32, enc MessageEncoding,
532
530
533
531
count , err = ReadVarIntBuf (r , pver , buf )
534
532
if err != nil {
535
- scriptPool .Return (scriptBuf )
536
533
return err
537
534
}
538
535
539
536
// Prevent more output transactions than could possibly fit into a
540
537
// message. It would be possible to cause memory exhaustion and panics
541
538
// without a sane upper bound on this count.
542
539
if count > uint64 (maxTxOutPerMessage ) {
543
- scriptPool .Return (scriptBuf )
544
540
str := fmt .Sprintf ("too many output transactions to fit into " +
545
541
"max message size [count %d, max %d]" , count ,
546
542
maxTxOutPerMessage )
@@ -557,7 +553,6 @@ func (msg *MsgTx) btcDecode(r io.Reader, pver uint32, enc MessageEncoding,
557
553
msg .TxOut [i ] = to
558
554
err = readTxOutBuf (r , pver , msg .Version , to , buf , sbuf )
559
555
if err != nil {
560
- scriptPool .Return (scriptBuf )
561
556
return err
562
557
}
563
558
totalScriptSize += uint64 (len (to .PkScript ))
@@ -573,14 +568,12 @@ func (msg *MsgTx) btcDecode(r io.Reader, pver uint32, enc MessageEncoding,
573
568
// varint which encodes the number of stack items.
574
569
witCount , err := ReadVarIntBuf (r , pver , buf )
575
570
if err != nil {
576
- scriptPool .Return (scriptBuf )
577
571
return err
578
572
}
579
573
580
574
// Prevent a possible memory exhaustion attack by
581
575
// limiting the witCount value to a sane upper bound.
582
576
if witCount > maxWitnessItemsPerInput {
583
- scriptPool .Return (scriptBuf )
584
577
str := fmt .Sprintf ("too many witness items to fit " +
585
578
"into max message size [count %d, max %d]" ,
586
579
witCount , maxWitnessItemsPerInput )
@@ -597,7 +590,6 @@ func (msg *MsgTx) btcDecode(r io.Reader, pver uint32, enc MessageEncoding,
597
590
"script witness item" ,
598
591
)
599
592
if err != nil {
600
- scriptPool .Return (scriptBuf )
601
593
return err
602
594
}
603
595
totalScriptSize += uint64 (len (txin .Witness [j ]))
@@ -607,7 +599,6 @@ func (msg *MsgTx) btcDecode(r io.Reader, pver uint32, enc MessageEncoding,
607
599
}
608
600
609
601
if _ , err := io .ReadFull (r , buf [:4 ]); err != nil {
610
- scriptPool .Return (scriptBuf )
611
602
return err
612
603
}
613
604
msg .LockTime = littleEndian .Uint32 (buf [:4 ])
@@ -670,8 +661,6 @@ func (msg *MsgTx) btcDecode(r io.Reader, pver uint32, enc MessageEncoding,
670
661
offset += scriptSize
671
662
}
672
663
673
- scriptPool .Return (scriptBuf )
674
-
675
664
return nil
676
665
}
677
666
0 commit comments