forked from decred/dcrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgtx.go
1793 lines (1540 loc) · 50.8 KB
/
msgtx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"strconv"
"github.com/decred/dcrd/chaincfg/chainhash"
)
const (
// TxVersion is the current latest supported transaction version.
TxVersion uint16 = 1
// MaxTxInSequenceNum is the maximum sequence number the sequence field
// of a transaction input can be.
MaxTxInSequenceNum uint32 = 0xffffffff
// MaxPrevOutIndex is the maximum index the index field of a previous
// outpoint can be.
MaxPrevOutIndex uint32 = 0xffffffff
// NoExpiryValue is the value of expiry that indicates the transaction
// has no expiry.
NoExpiryValue uint32 = 0
// NullValue is a null value for an input witness.
NullValueIn int64 = -1
// NullBlockHeight is the null value for an input witness. It references
// the genesis block.
NullBlockHeight uint32 = 0x00000000
// NullBlockIndex is the null transaction index in a block for an input
// witness.
NullBlockIndex uint32 = 0xffffffff
// DefaultPkScriptVersion is the default pkScript version, referring to
// extended Decred script.
DefaultPkScriptVersion uint16 = 0x0000
)
// defaultTxInOutAlloc is the default size used for the backing array for
// transaction inputs and outputs. The array will dynamically grow as needed,
// but this figure is intended to provide enough space for the number of
// inputs and outputs in a typical transaction without needing to grow the
// backing array multiple times.
const defaultTxInOutAlloc = 15
const (
// minTxInPayload is the minimum payload size for a transaction input.
// PreviousOutPoint.Hash + PreviousOutPoint.Index 4 bytes +
// PreviousOutPoint.Tree 1 byte + Varint for SignatureScript length 1
// byte + Sequence 4 bytes.
minTxInPayload = 11 + chainhash.HashSize
// maxTxInPerMessage is the maximum number of transactions inputs that
// a transaction which fits into a message could possibly have.
maxTxInPerMessage = (MaxMessagePayload / minTxInPayload) + 1
// minTxOutPayload is the minimum payload size for a transaction output.
// Value 8 bytes + Varint for PkScript length 1 byte.
minTxOutPayload = 9
// maxTxOutPerMessage is the maximum number of transactions outputs that
// a transaction which fits into a message could possibly have.
maxTxOutPerMessage = (MaxMessagePayload / minTxOutPayload) + 1
// minTxPayload is the minimum payload size for any full encoded
// (prefix and witness transaction). Note that any realistically
// usable transaction must have at least one input or output, but
// that is a rule enforced at a higher layer, so it is intentionally
// not included here.
// Version 4 bytes + Varint number of transaction inputs 1 byte + Varint
// number of transaction outputs 1 byte + Varint representing the number
// of transaction signatures + LockTime 4 bytes + Expiry 4 bytes + min
// input payload + min output payload.
minTxPayload = 4 + 1 + 1 + 1 + 4 + 4
)
// TxSerializeType is a uint16 representing the serialized type of transaction
// this msgTx is. You can use a bitmask for this too, but Decred just splits
// the int32 version into 2x uint16s so that you have:
// {
// uint16 type
// uint16 version
// }
type TxSerializeType uint16
const (
TxSerializeFull = TxSerializeType(iota)
TxSerializeNoWitness
TxSerializeOnlyWitness
TxSerializeWitnessSigning
TxSerializeWitnessValueSigning
)
// TODO replace all these with predeclared int32 or [4]byte cj
// DefaultMsgTxVersion returns the default version int32 (serialize the tx
// fully, version number 1).
func DefaultMsgTxVersion() int32 {
verBytes := make([]byte, 4, 4)
binary.LittleEndian.PutUint16(verBytes[0:2], TxVersion)
binary.LittleEndian.PutUint16(verBytes[2:4], uint16(TxSerializeFull))
ver := binary.LittleEndian.Uint32(verBytes)
return int32(ver)
}
// NoWitnessMsgTxVersion returns the witness free serializing int32 (serialize
// the tx without witness, version number 1).
func NoWitnessMsgTxVersion() int32 {
verBytes := make([]byte, 4, 4)
binary.LittleEndian.PutUint16(verBytes[0:2], TxVersion)
binary.LittleEndian.PutUint16(verBytes[2:4], uint16(TxSerializeNoWitness))
ver := binary.LittleEndian.Uint32(verBytes)
return int32(ver)
}
// WitnessOnlyMsgTxVersion returns the witness only version int32 (serialize
// the tx witness, version number 1).
func WitnessOnlyMsgTxVersion() int32 {
verBytes := make([]byte, 4, 4)
binary.LittleEndian.PutUint16(verBytes[0:2], TxVersion)
binary.LittleEndian.PutUint16(verBytes[2:4], uint16(TxSerializeOnlyWitness))
ver := binary.LittleEndian.Uint32(verBytes)
return int32(ver)
}
// WitnessSigningMsgTxVersion returns the witness only version int32 (serialize
// the tx witness for signing, version number 1).
func WitnessSigningMsgTxVersion() int32 {
verBytes := make([]byte, 4, 4)
binary.LittleEndian.PutUint16(verBytes[0:2], TxVersion)
binary.LittleEndian.PutUint16(verBytes[2:4], uint16(TxSerializeWitnessSigning))
ver := binary.LittleEndian.Uint32(verBytes)
return int32(ver)
}
// WitnessValueSigningMsgTxVersion returns the witness only version int32
// (serialize the tx witness for signing with value, version number 1).
func WitnessValueSigningMsgTxVersion() int32 {
verBytes := make([]byte, 4, 4)
binary.LittleEndian.PutUint16(verBytes[0:2], TxVersion)
binary.LittleEndian.PutUint16(verBytes[2:4],
uint16(TxSerializeWitnessValueSigning))
ver := binary.LittleEndian.Uint32(verBytes)
return int32(ver)
}
// OutPoint defines a decred data type that is used to track previous
// transaction outputs.
type OutPoint struct {
Hash chainhash.Hash
Index uint32
Tree int8
}
// NewOutPoint returns a new decred transaction outpoint point with the
// provided hash and index.
func NewOutPoint(hash *chainhash.Hash, index uint32, tree int8) *OutPoint {
return &OutPoint{
Hash: *hash,
Index: index,
Tree: tree,
}
}
// String returns the OutPoint in the human-readable form "hash:index".
func (o OutPoint) String() string {
// Allocate enough for hash string, colon, and 10 digits. Although
// at the time of writing, the number of digits can be no greater than
// the length of the decimal representation of maxTxOutPerMessage, the
// maximum message payload may increase in the future and this
// optimization may go unnoticed, so allocate space for 10 decimal
// digits, which will fit any uint32.
buf := make([]byte, 2*chainhash.HashSize+1, 2*chainhash.HashSize+1+10)
copy(buf, o.Hash.String())
buf[2*chainhash.HashSize] = ':'
buf = strconv.AppendUint(buf, uint64(o.Index), 10)
return string(buf)
}
// TxIn defines a decred transaction input.
type TxIn struct {
// Non-witness
PreviousOutPoint OutPoint
Sequence uint32
// Witness
ValueIn int64
BlockHeight uint32
BlockIndex uint32
SignatureScript []byte
}
// SerializeSizePrefix returns the number of bytes it would take to serialize
// the transaction input for a prefix.
func (t *TxIn) SerializeSizePrefix() int {
// Outpoint Hash 32 bytes + Outpoint Index 4 bytes + Outpoint Tree 1 byte +
// Sequence 4 bytes.
return 41
}
// SerializeSizeWitness returns the number of bytes it would take to serialize the
// transaction input for a witness.
func (t *TxIn) SerializeSizeWitness() int {
// ValueIn (8 bytes) + BlockHeight (4 bytes) + BlockIndex (4 bytes) +
// serialized varint size for the length of SignatureScript +
// SignatureScript bytes.
return 8 + 4 + 4 + VarIntSerializeSize(uint64(len(t.SignatureScript))) +
len(t.SignatureScript)
}
// SerializeSizeWitnessSigning returns the number of bytes it would take to
// serialize the transaction input for a witness used in signing.
func (t *TxIn) SerializeSizeWitnessSigning() int {
// Serialized varint size for the length of SignatureScript +
// SignatureScript bytes.
return VarIntSerializeSize(uint64(len(t.SignatureScript))) +
len(t.SignatureScript)
}
// SerializeSizeWitnessValueSigning returns the number of bytes it would take to
// serialize the transaction input for a witness used in signing with value
// included.
func (t *TxIn) SerializeSizeWitnessValueSigning() int {
// ValueIn (8 bytes) + serialized varint size for the length of
// SignatureScript + SignatureScript bytes.
return 8 + VarIntSerializeSize(uint64(len(t.SignatureScript))) +
len(t.SignatureScript)
}
// LegacySerializeSize returns the number of bytes it would take to serialize the
// the transaction input.
func (t *TxIn) LegacySerializeSize() int {
// Outpoint Hash 32 bytes + Outpoint Index 4 bytes + Sequence 4 bytes +
// serialized varint size for the length of SignatureScript +
// SignatureScript bytes.
return 41 + VarIntSerializeSize(uint64(len(t.SignatureScript))) +
len(t.SignatureScript)
}
// NewTxIn returns a new decred transaction input with the provided
// previous outpoint point and signature script with a default sequence of
// MaxTxInSequenceNum.
func NewTxIn(prevOut *OutPoint, signatureScript []byte) *TxIn {
return &TxIn{
PreviousOutPoint: *prevOut,
Sequence: MaxTxInSequenceNum,
SignatureScript: signatureScript,
ValueIn: NullValueIn,
BlockHeight: NullBlockHeight,
BlockIndex: NullBlockIndex,
}
}
// TxOut defines a decred transaction output.
type TxOut struct {
Value int64
Version uint16
PkScript []byte
}
// SerializeSize returns the number of bytes it would take to serialize the
// the transaction output.
func (t *TxOut) SerializeSize() int {
// Value 8 bytes + Version 2 bytes + serialized varint size for
// the length of PkScript + PkScript bytes.
return 8 + 2 + VarIntSerializeSize(uint64(len(t.PkScript))) + len(t.PkScript)
}
// NewTxOut returns a new decred transaction output with the provided
// transaction value and public key script.
func NewTxOut(value int64, pkScript []byte) *TxOut {
return &TxOut{
Value: value,
Version: DefaultPkScriptVersion,
PkScript: pkScript,
}
}
// MsgTx implements the Message interface and represents a decred tx message.
// It is used to deliver transaction information in response to a getdata
// message (MsgGetData) for a given transaction.
//
// Use the AddTxIn and AddTxOut functions to build up the list of transaction
// inputs and outputs.
type MsgTx struct {
CachedHash *chainhash.Hash
Version int32
TxIn []*TxIn
TxOut []*TxOut
LockTime uint32
Expiry uint32
}
// AddTxIn adds a transaction input to the message.
func (msg *MsgTx) AddTxIn(ti *TxIn) {
msg.TxIn = append(msg.TxIn, ti)
}
// AddTxOut adds a transaction output to the message.
func (msg *MsgTx) AddTxOut(to *TxOut) {
msg.TxOut = append(msg.TxOut, to)
}
// msgTxVersionToBytes converts an int32 version into a 4 byte slice.
func msgTxVersionToBytes(version int32) []byte {
mVerBytes := make([]byte, 4, 4)
binary.LittleEndian.PutUint32(mVerBytes[0:4], uint32(version))
return mVerBytes
}
// msgTxVersionDecode converts an int32 version into serialization types and
// actual version.
func msgTxVersionToVars(version int32) (uint16, TxSerializeType) {
mVerBytes := make([]byte, 4, 4)
binary.LittleEndian.PutUint32(mVerBytes[0:4], uint32(version))
mVer := binary.LittleEndian.Uint16(mVerBytes[0:2])
mType := binary.LittleEndian.Uint16(mVerBytes[2:4])
return mVer, TxSerializeType(mType)
}
// msgTxVersionDecode converts a 4 byte slice into an int32 version.
func msgTxVersionDecode(verBytes []byte) (int32, error) {
if len(verBytes) != 4 {
return 0, messageError("msgTxVersionDecode", "tx version wrong size")
}
ver := binary.LittleEndian.Uint32(verBytes)
return int32(ver), nil
}
// shallowCopyForSerializing make a shallow copy of a tx with a new
// version, so that it can be hashed or serialized accordingly.
func (msg *MsgTx) shallowCopyForSerializing(version int32) *MsgTx {
return &MsgTx{
Version: version,
TxIn: msg.TxIn,
TxOut: msg.TxOut,
LockTime: msg.LockTime,
Expiry: msg.Expiry,
}
}
// TxSha generates the Hash name for the transaction prefix.
func (msg *MsgTx) TxSha() chainhash.Hash {
// Encode the transaction and calculate double sha256 on the result.
// Ignore the error returns since the only way the encode could fail
// is being out of memory or due to nil pointers, both of which would
// cause a run-time panic.
// TxSha should always calculate a non-witnessed hash.
mtxCopy := msg.shallowCopyForSerializing(NoWitnessMsgTxVersion())
buf := bytes.NewBuffer(make([]byte, 0, mtxCopy.SerializeSize()))
_ = mtxCopy.Serialize(buf)
return chainhash.HashFuncH(buf.Bytes())
}
// CachedTxSha generates the Hash name for the transaction prefix and stores
// it if it does not exist. The cached hash is then returned. It can be
// recalculated later with RecacheTxSha.
func (msg *MsgTx) CachedTxSha() *chainhash.Hash {
if msg.CachedHash == nil {
h := msg.TxSha()
msg.CachedHash = &h
}
return msg.CachedHash
}
// RecacheTxSha generates the Hash name for the transaction prefix and stores
// it. The cached hash is then returned.
func (msg *MsgTx) RecacheTxSha() *chainhash.Hash {
h := msg.TxSha()
msg.CachedHash = &h
return msg.CachedHash
}
// TxShaWitness generates the Hash name for the transaction witness.
func (msg *MsgTx) TxShaWitness() chainhash.Hash {
// Encode the transaction and calculate double sha256 on the result.
// Ignore the error returns since the only way the encode could fail
// is being out of memory or due to nil pointers, both of which would
// cause a run-time panic.
// TxShaWitness should always calculate a witnessed hash.
mtxCopy := msg.shallowCopyForSerializing(WitnessOnlyMsgTxVersion())
buf := bytes.NewBuffer(make([]byte, 0, mtxCopy.SerializeSize()))
_ = mtxCopy.Serialize(buf)
return chainhash.HashFuncH(buf.Bytes())
}
// TxShaWitnessSigning generates the Hash name for the transaction witness with
// the malleable portions (AmountIn, BlockHeight, BlockIndex) removed. These are
// verified and set by the miner instead.
func (msg *MsgTx) TxShaWitnessSigning() chainhash.Hash {
// Encode the transaction and calculate double sha256 on the result.
// Ignore the error returns since the only way the encode could fail
// is being out of memory or due to nil pointers, both of which would
// cause a run-time panic.
// TxShaWitness should always calculate a witnessed hash.
mtxCopy := msg.shallowCopyForSerializing(WitnessSigningMsgTxVersion())
buf := bytes.NewBuffer(make([]byte, 0, mtxCopy.SerializeSize()))
_ = mtxCopy.Serialize(buf)
return chainhash.HashFuncH(buf.Bytes())
}
// TxShaWitnessValueSigning generates the Hash name for the transaction witness
// with BlockHeight and BlockIndex removed, allowing the signer to specify the
// ValueIn.
func (msg *MsgTx) TxShaWitnessValueSigning() chainhash.Hash {
// Encode the transaction and calculate double sha256 on the result.
// Ignore the error returns since the only way the encode could fail
// is being out of memory or due to nil pointers, both of which would
// cause a run-time panic.
// TxShaWitness should always calculate a witnessed hash.
mtxCopy := msg.shallowCopyForSerializing(WitnessValueSigningMsgTxVersion())
buf := bytes.NewBuffer(make([]byte, 0, mtxCopy.SerializeSize()))
_ = mtxCopy.Serialize(buf)
return chainhash.HashFuncH(buf.Bytes())
}
// TxShaFull generates the Hash name for the transaction prefix || witness. It
// first obtains the hashes for both the transaction prefix and witness, then
// concatenates them and hashes these 64 bytes.
// Note that the inputs to the hashes, serialized prefix and serialized witnesses,
// have different uint32 versions because version is now actually two uint16s,
// with the last 16 bits referring to the serialization type. The first 16 bits
// refer to the actual version, and these must be the same in both serializations.
func (msg *MsgTx) TxShaFull() chainhash.Hash {
concat := make([]byte, 64, 64)
prefixHash := msg.TxSha()
witnessHash := msg.TxShaWitness()
copy(concat[0:32], prefixHash[:])
copy(concat[32:64], witnessHash[:])
return chainhash.HashFuncH(concat)
}
// TxShaLegacy generates the legacy transaction hash, for software
// compatibility.
func (msg *MsgTx) TxShaLegacy() chainhash.Hash {
// Encode the transaction and calculate double sha256 on the result.
// Ignore the error returns since the only way the encode could fail
// is being out of memory or due to nil pointers, both of which would
// cause a run-time panic.
buf := bytes.NewBuffer(make([]byte, 0, msg.SerializeSize()))
_ = msg.LegacySerialize(buf)
return chainhash.HashFuncH(buf.Bytes())
}
// Copy creates a deep copy of a transaction so that the original does not get
// modified when the copy is manipulated.
func (msg *MsgTx) Copy() *MsgTx {
// Create new tx and start by copying primitive values and making space
// for the transaction inputs and outputs.
newTx := MsgTx{
Version: msg.Version,
TxIn: make([]*TxIn, 0, len(msg.TxIn)),
TxOut: make([]*TxOut, 0, len(msg.TxOut)),
LockTime: msg.LockTime,
Expiry: msg.Expiry,
}
// Deep copy the old TxIn data.
for _, oldTxIn := range msg.TxIn {
// Deep copy the old previous outpoint.
oldOutPoint := oldTxIn.PreviousOutPoint
newOutPoint := OutPoint{}
newOutPoint.Hash.SetBytes(oldOutPoint.Hash[:])
newOutPoint.Index = oldOutPoint.Index
newOutPoint.Tree = oldOutPoint.Tree
// Deep copy the old signature script.
var newScript []byte
oldScript := oldTxIn.SignatureScript
oldScriptLen := len(oldScript)
if oldScriptLen > 0 {
newScript = make([]byte, oldScriptLen, oldScriptLen)
copy(newScript, oldScript[:oldScriptLen])
}
// Create new txIn with the deep copied data and append it to
// new Tx.
newTxIn := TxIn{
PreviousOutPoint: newOutPoint,
Sequence: oldTxIn.Sequence,
ValueIn: oldTxIn.ValueIn,
BlockHeight: oldTxIn.BlockHeight,
BlockIndex: oldTxIn.BlockIndex,
SignatureScript: newScript,
}
newTx.TxIn = append(newTx.TxIn, &newTxIn)
}
// Deep copy the old TxOut data.
for _, oldTxOut := range msg.TxOut {
// Deep copy the old PkScript
var newScript []byte
oldScript := oldTxOut.PkScript
oldScriptLen := len(oldScript)
if oldScriptLen > 0 {
newScript = make([]byte, oldScriptLen, oldScriptLen)
copy(newScript, oldScript[:oldScriptLen])
}
// Create new txOut with the deep copied data and append it to
// new Tx.
newTxOut := TxOut{
Value: oldTxOut.Value,
Version: oldTxOut.Version,
PkScript: newScript,
}
newTx.TxOut = append(newTx.TxOut, &newTxOut)
}
return &newTx
}
// decodePrefix decodes a transaction prefix and stores the contents
// in the embedded msgTx.
func (msg *MsgTx) decodePrefix(r io.Reader, pver uint32) error {
count, err := readVarInt(r, pver)
if err != nil {
return err
}
// Prevent more input transactions than could possibly fit into a
// message. It would be possible to cause memory exhaustion and panics
// without a sane upper bound on this count.
if count > uint64(maxTxInPerMessage) {
str := fmt.Sprintf("too many input transactions to fit into "+
"max message size [count %d, max %d]", count,
maxTxInPerMessage)
return messageError("MsgTx.decodePrefix", str)
}
// TxIns.
msg.TxIn = make([]*TxIn, count)
for i := uint64(0); i < count; i++ {
ti := TxIn{}
err = readTxInPrefix(r, pver, msg.Version, &ti)
if err != nil {
return err
}
msg.TxIn[i] = &ti
}
count, err = readVarInt(r, pver)
if err != nil {
return err
}
// Prevent more output transactions than could possibly fit into a
// message. It would be possible to cause memory exhaustion and panics
// without a sane upper bound on this count.
if count > uint64(maxTxOutPerMessage) {
str := fmt.Sprintf("too many output transactions to fit into "+
"max message size [count %d, max %d]", count,
maxTxOutPerMessage)
return messageError("MsgTx.decodePrefix", str)
}
// TxOuts.
msg.TxOut = make([]*TxOut, count)
for i := uint64(0); i < count; i++ {
to := TxOut{}
err = readTxOut(r, pver, msg.Version, &to)
if err != nil {
return err
}
msg.TxOut[i] = &to
}
// Locktime and expiry.
var buf [4]byte
_, err = io.ReadFull(r, buf[:])
if err != nil {
return err
}
msg.LockTime = binary.LittleEndian.Uint32(buf[:])
_, err = io.ReadFull(r, buf[:])
if err != nil {
return err
}
msg.Expiry = binary.LittleEndian.Uint32(buf[:])
return nil
}
func (msg *MsgTx) decodeWitness(r io.Reader, pver uint32, isFull bool) error {
// Witness only; generate the TxIn list and fill out only the
// sigScripts.
if !isFull {
count, err := readVarInt(r, pver)
if err != nil {
return err
}
// Prevent more input transactions than could possibly fit into a
// message. It would be possible to cause memory exhaustion and panics
// without a sane upper bound on this count.
if count > uint64(maxTxInPerMessage) {
str := fmt.Sprintf("too many input transactions to fit into "+
"max message size [count %d, max %d]", count,
maxTxInPerMessage)
return messageError("MsgTx.decodeWitness", str)
}
msg.TxIn = make([]*TxIn, count)
for i := uint64(0); i < count; i++ {
ti := TxIn{}
err = readTxInWitness(r, pver, msg.Version, &ti)
if err != nil {
return err
}
msg.TxIn[i] = &ti
}
msg.TxOut = make([]*TxOut, 0)
} else {
// We're decoding witnesses from a full transaction, so read in
// the number of signature scripts, check to make sure it's the
// same as the number of TxIns we currently have, then fill in
// the signature scripts.
count, err := readVarInt(r, pver)
if err != nil {
return err
}
// Don't allow the deserializer to panic by accessing memory
// that doesn't exist.
if int(count) != len(msg.TxIn) {
str := fmt.Sprintf("non equal witness and prefix txin quantities "+
"(witness %v, prefix %v)", count,
len(msg.TxIn))
return messageError("MsgTx.decodeWitness", str)
}
// Prevent more input transactions than could possibly fit into a
// message. It would be possible to cause memory exhaustion and panics
// without a sane upper bound on this count.
if count > uint64(maxTxInPerMessage) {
str := fmt.Sprintf("too many input transactions to fit into "+
"max message size [count %d, max %d]", count,
maxTxInPerMessage)
return messageError("MsgTx.decodeWitness", str)
}
// Read in the witnesses, and copy them into the already generated
// by decodePrefix TxIns.
for i := uint64(0); i < count; i++ {
ti := TxIn{}
err = readTxInWitness(r, pver, msg.Version, &ti)
if err != nil {
return err
}
msg.TxIn[i].ValueIn = ti.ValueIn
msg.TxIn[i].BlockHeight = ti.BlockHeight
msg.TxIn[i].BlockIndex = ti.BlockIndex
msg.TxIn[i].SignatureScript = ti.SignatureScript
}
}
return nil
}
// decodeWitnessSigning decodes a witness for signing.
func (msg *MsgTx) decodeWitnessSigning(r io.Reader, pver uint32) error {
// Witness only for signing; generate the TxIn list and fill out only the
// sigScripts.
count, err := readVarInt(r, pver)
if err != nil {
return err
}
// Prevent more input transactions than could possibly fit into a
// message. It would be possible to cause memory exhaustion and panics
// without a sane upper bound on this count.
if count > uint64(maxTxInPerMessage) {
str := fmt.Sprintf("too many input transactions to fit into "+
"max message size [count %d, max %d]", count,
maxTxInPerMessage)
return messageError("MsgTx.decodeWitness", str)
}
msg.TxIn = make([]*TxIn, count)
for i := uint64(0); i < count; i++ {
ti := TxIn{}
err = readTxInWitnessSigning(r, pver, msg.Version, &ti)
if err != nil {
return err
}
msg.TxIn[i] = &ti
}
msg.TxOut = make([]*TxOut, 0)
return nil
}
// decodeWitnessValueSigning decodes a witness for signing with value.
func (msg *MsgTx) decodeWitnessValueSigning(r io.Reader, pver uint32) error {
// Witness only for signing; generate the TxIn list and fill out only the
// sigScripts.
count, err := readVarInt(r, pver)
if err != nil {
return err
}
// Prevent more input transactions than could possibly fit into a
// message. It would be possible to cause memory exhaustion and panics
// without a sane upper bound on this count.
if count > uint64(maxTxInPerMessage) {
str := fmt.Sprintf("too many input transactions to fit into "+
"max message size [count %d, max %d]", count,
maxTxInPerMessage)
return messageError("MsgTx.decodeWitness", str)
}
msg.TxIn = make([]*TxIn, count)
for i := uint64(0); i < count; i++ {
ti := TxIn{}
err = readTxInWitnessValueSigning(r, pver, msg.Version, &ti)
if err != nil {
return err
}
msg.TxIn[i] = &ti
}
msg.TxOut = make([]*TxOut, 0)
return nil
}
// BtcDecode decodes r using the decred protocol encoding into the receiver.
// This is part of the Message interface implementation.
// See Deserialize for decoding transactions stored to disk, such as in a
// database, as opposed to decoding transactions from the wire.
func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32) error {
var buf [4]byte
_, err := io.ReadFull(r, buf[:])
if err != nil {
return err
}
msg.Version = int32(binary.LittleEndian.Uint32(buf[:]))
_, mType := msgTxVersionToVars(msg.Version)
switch {
case mType == TxSerializeNoWitness:
err := msg.decodePrefix(r, pver)
if err != nil {
return err
}
case mType == TxSerializeOnlyWitness:
err := msg.decodeWitness(r, pver, false)
if err != nil {
return err
}
case mType == TxSerializeWitnessSigning:
err := msg.decodeWitnessSigning(r, pver)
if err != nil {
return err
}
case mType == TxSerializeWitnessValueSigning:
err := msg.decodeWitnessValueSigning(r, pver)
if err != nil {
return err
}
case mType == TxSerializeFull:
err := msg.decodePrefix(r, pver)
if err != nil {
return err
}
err = msg.decodeWitness(r, pver, true)
if err != nil {
return err
}
default:
return messageError("MsgTx.BtcDecode", "unsupported transaction type")
}
return nil
}
// LegacyBtcDecode decodes r using the decred protocol encoding into the
// receiver. This is used for the decoding of legacy serialized transactions.
func (msg *MsgTx) LegacyBtcDecode(r io.Reader, pver uint32) error {
var buf [4]byte
_, err := io.ReadFull(r, buf[:])
if err != nil {
return err
}
msg.Version = int32(binary.LittleEndian.Uint32(buf[:]))
count, err := readVarInt(r, pver)
if err != nil {
return err
}
// Prevent more input transactions than could possibly fit into a
// message. It would be possible to cause memory exhaustion and panics
// without a sane upper bound on this count.
if count > uint64(maxTxInPerMessage) {
str := fmt.Sprintf("too many input transactions to fit into "+
"max message size [count %d, max %d]", count,
maxTxInPerMessage)
return messageError("MsgTx.BtcDecode", str)
}
msg.TxIn = make([]*TxIn, count)
for i := uint64(0); i < count; i++ {
ti := TxIn{}
err = legacyReadTxIn(r, pver, msg.Version, &ti)
if err != nil {
return err
}
msg.TxIn[i] = &ti
}
count, err = readVarInt(r, pver)
if err != nil {
return err
}
// Prevent more output transactions than could possibly fit into a
// message. It would be possible to cause memory exhaustion and panics
// without a sane upper bound on this count.
if count > uint64(maxTxOutPerMessage) {
str := fmt.Sprintf("too many output transactions to fit into "+
"max message size [count %d, max %d]", count,
maxTxOutPerMessage)
return messageError("MsgTx.BtcDecode", str)
}
msg.TxOut = make([]*TxOut, count)
for i := uint64(0); i < count; i++ {
to := TxOut{}
err = legacyReadTxOut(r, pver, msg.Version, &to)
if err != nil {
return err
}
msg.TxOut[i] = &to
}
_, err = io.ReadFull(r, buf[:])
if err != nil {
return err
}
msg.LockTime = binary.LittleEndian.Uint32(buf[:])
return nil
}
// Deserialize decodes a transaction from r into the receiver using a format
// that is suitable for long-term storage such as a database while respecting
// the Version field in the transaction. This function differs from BtcDecode
// in that BtcDecode decodes from the Decred wire protocol as it was sent
// across the network. The wire encoding can technically differ depending on
// the protocol version and doesn't even really need to match the format of a
// stored transaction at all. As of the time this comment was written, the
// encoded transaction is the same in both instances, but there is a distinct
// difference and separating the two allows the API to be flexible enough to
// deal with changes.
func (msg *MsgTx) Deserialize(r io.Reader) error {
// At the current time, there is no difference between the wire encoding
// at protocol version 0 and the stable long-term storage format. As
// a result, make use of BtcDecode.
return msg.BtcDecode(r, 0)
}
// LegacyDeserialize decodes a transaction that has been encoded in the legacy
// Decred format.
func (msg *MsgTx) LegacyDeserialize(r io.Reader) error {
// At the current time, there is no difference between the wire encoding
// at protocol version 0 and the stable long-term storage format. As
// a result, make use of BtcDecode.
return msg.LegacyBtcDecode(r, 0)
}
// FromBytes deserializes a transaction byte slice.
func (msg *MsgTx) FromBytes(b []byte) error {
r := bytes.NewReader(b)
return msg.Deserialize(r)
}
// encodePrefix encodes a transaction prefix into a writer.
func (msg *MsgTx) encodePrefix(w io.Writer, pver uint32) error {
var buf [4]byte
count := uint64(len(msg.TxIn))
err := writeVarInt(w, pver, count)
if err != nil {
return err
}
for _, ti := range msg.TxIn {
err = writeTxInPrefix(w, pver, msg.Version, ti)
if err != nil {
return err
}
}
count = uint64(len(msg.TxOut))
err = writeVarInt(w, pver, count)
if err != nil {
return err
}
for _, to := range msg.TxOut {
err = writeTxOut(w, pver, msg.Version, to)
if err != nil {
return err
}
}
binary.LittleEndian.PutUint32(buf[:], msg.LockTime)
_, err = w.Write(buf[:])
if err != nil {
return err
}
binary.LittleEndian.PutUint32(buf[:], msg.Expiry)
_, err = w.Write(buf[:])
if err != nil {
return err
}
return nil
}
// encodeWitness encodes a transaction witness into a writer.
func (msg *MsgTx) encodeWitness(w io.Writer, pver uint32) error {
count := uint64(len(msg.TxIn))
err := writeVarInt(w, pver, count)
if err != nil {
return err
}
for _, ti := range msg.TxIn {
err = writeTxInWitness(w, pver, msg.Version, ti)
if err != nil {
return err
}
}
return nil
}
// encodeWitnessSigning encodes a transaction witness into a writer for signing.
func (msg *MsgTx) encodeWitnessSigning(w io.Writer, pver uint32) error {
count := uint64(len(msg.TxIn))
err := writeVarInt(w, pver, count)
if err != nil {
return err
}
for _, ti := range msg.TxIn {
err = writeTxInWitnessSigning(w, pver, msg.Version, ti)
if err != nil {
return err
}
}
return nil
}
// encodeWitnessValueSigning encodes a transaction witness into a writer for
// signing, with the value included.
func (msg *MsgTx) encodeWitnessValueSigning(w io.Writer, pver uint32) error {
count := uint64(len(msg.TxIn))
err := writeVarInt(w, pver, count)
if err != nil {
return err
}
for _, ti := range msg.TxIn {
err = writeTxInWitnessValueSigning(w, pver, msg.Version, ti)
if err != nil {
return err