forked from blkchain/blkchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
205 lines (176 loc) · 3.8 KB
/
tx.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
package blkchain
import (
"bytes"
"fmt"
"io"
)
type Tx struct {
Version uint32
TxIns TxInList
TxOuts TxOutList
LockTime uint32
SegWit bool
}
func (tx *Tx) Hash() Uint256 {
buf := new(bytes.Buffer)
tx.binWriteWithoutWitness(buf)
return ShaSha256(buf.Bytes())
}
func (tx *Tx) BaseSize() int {
if !tx.SegWit {
return tx.Size()
}
version, locktime := 4, 4
return version + tx.TxIns.BaseSize() + tx.TxOuts.Size() + locktime
}
func (tx *Tx) Size() int {
version, locktime, segwit := 4, 4, 0
if tx.SegWit {
segwit = 2 // marker+flag
}
return version + segwit + tx.TxIns.Size() + tx.TxOuts.Size() + locktime
}
func (tx *Tx) Weight() int {
return tx.BaseSize()*3 + tx.Size()
}
func (tx *Tx) sigOpCount() (count int) {
for _, tin := range tx.TxIns {
count += tin.sigOpCount()
}
return count
}
func (tx *Tx) VirtualSize() int {
// TODO: This only an approximation
// We need script parsing code for precise result
const (
witnessScaleFactor = 4
defaultBytesPerSigOp = 20
// maxPubkeysPerMultisig = 20
)
sigOpWeight := tx.sigOpCount() * witnessScaleFactor * defaultBytesPerSigOp
weight := tx.Weight()
if weight < sigOpWeight {
weight = sigOpWeight
}
return (weight + witnessScaleFactor - 1) / witnessScaleFactor
}
func (tx *Tx) BinRead(r io.Reader) (err error) {
var wcnt int
if err = BinRead(&tx.Version, r); err != nil {
return err
}
if err = BinRead(&tx.TxIns, r); err != nil {
return err
}
if len(tx.TxIns) == 0 { // SegWit
flag, err := readCompactSize(r)
if err != nil {
return err
}
if flag != 1 {
return fmt.Errorf("Invalid SegWit flag: %d", flag)
}
if err = BinRead(&tx.TxIns, r); err != nil { // Read txins again
return err
}
wcnt = len(tx.TxIns)
}
if err = BinRead(&tx.TxOuts, r); err != nil {
return err
}
if wcnt > 0 { // Read witness
for _, txin := range tx.TxIns {
var wits Witness
if err = BinRead(&wits, r); err != nil {
return err
}
txin.Witness = wits
}
tx.SegWit = true
}
if err = BinRead(&tx.LockTime, r); err != nil {
return err
}
return nil
}
func (tx *Tx) BinWrite(w io.Writer) (err error) {
if err = BinWrite(tx.Version, w); err != nil {
return err
}
if tx.SegWit {
_, err = w.Write([]byte{0x00, 0x01})
}
if err = BinWrite(&tx.TxIns, w); err != nil {
return err
}
if err = BinWrite(&tx.TxOuts, w); err != nil {
return err
}
if tx.SegWit {
for _, txin := range tx.TxIns {
if err = BinWrite(&txin.Witness, w); err != nil {
return err
}
}
}
if err = BinWrite(tx.LockTime, w); err != nil {
return err
}
return nil
}
func (tx *Tx) binWriteWithoutWitness(w io.Writer) (err error) {
// This is for computing the txid, it is the transaction without
// the segwit marker and without the witness data.
if err = BinWrite(tx.Version, w); err != nil {
return err
}
if err = BinWrite(&tx.TxIns, w); err != nil {
return err
}
if err = BinWrite(&tx.TxOuts, w); err != nil {
return err
}
if err = BinWrite(tx.LockTime, w); err != nil {
return err
}
return nil
}
type TxList []*Tx
func (tl *TxList) BinRead(r io.Reader) error {
return readList(r, func(r io.Reader) error {
var tx Tx
if err := BinRead(&tx, r); err != nil {
return err
}
*tl = append(*tl, &tx)
return nil
})
}
func (tl *TxList) BaseSize() int {
result := compactSizeSize(uint64(len(*tl)))
for _, t := range *tl {
result += t.BaseSize()
}
return result
}
func (tl *TxList) Size() int {
result := compactSizeSize(uint64(len(*tl)))
for _, t := range *tl {
result += t.Size()
}
return result
}
func (tl *TxList) Weight() int {
result := compactSizeSize(uint64(len(*tl)))
for _, t := range *tl {
result += t.Weight()
}
return result
}
func (tl *TxList) VirtualSize() int {
result := compactSizeSize(uint64(len(*tl)))
for _, t := range *tl {
result += t.VirtualSize()
}
return result
}