forked from blkchain/blkchain
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx_in.go
92 lines (79 loc) · 1.72 KB
/
tx_in.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
package blkchain
import "io"
type OutPoint struct {
Hash Uint256
N uint32
}
type TxIn struct {
PrevOut OutPoint
ScriptSig []byte
Sequence uint32
Witness Witness
}
func (tin *TxIn) BaseSize() int {
outpoint := 32 + 4
scriptsig := compactSizeSize(uint64(len(tin.ScriptSig))) + len(tin.ScriptSig)
sequence := 4
return outpoint + scriptsig + sequence
}
func (tin *TxIn) Size() int {
return tin.BaseSize() + tin.Witness.Size()
}
func (tin *TxIn) sigOpCount() int {
// TODO
return 1
}
func (tin *TxIn) BinRead(r io.Reader) (err error) {
if err = BinRead(&tin.PrevOut, r); err != nil {
return err
}
if tin.ScriptSig, err = readString(r); err != nil {
return err
}
if err = BinRead(&tin.Sequence, r); err != nil {
return err
}
return nil
}
func (tin *TxIn) BinWrite(w io.Writer) (err error) {
if err = BinWrite(tin.PrevOut, w); err != nil {
return err
}
if err = writeString(tin.ScriptSig, w); err != nil {
return err
}
if err = BinWrite(tin.Sequence, w); err != nil {
return err
}
return nil
}
type TxInList []*TxIn
func (tins *TxInList) BinRead(r io.Reader) error {
return readList(r, func(r io.Reader) error {
var txin TxIn
if err := BinRead(&txin, r); err != nil {
return err
}
*tins = append(*tins, &txin)
return nil
})
}
func (tins *TxInList) BinWrite(w io.Writer) error {
return writeList(w, len(*tins), func(r io.Writer, i int) error {
return BinWrite((*tins)[i], w)
})
}
func (tins *TxInList) BaseSize() int {
result := compactSizeSize(uint64(len(*tins)))
for _, t := range *tins {
result += t.BaseSize()
}
return result
}
func (tins *TxInList) Size() int {
result := compactSizeSize(uint64(len(*tins)))
for _, t := range *tins {
result += t.Size()
}
return result
}