-
Notifications
You must be signed in to change notification settings - Fork 9
/
transaction.go
186 lines (159 loc) · 4.58 KB
/
transaction.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
package types
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"github.com/bytom/vapor/encoding/blockchain"
"github.com/bytom/vapor/errors"
"github.com/bytom/vapor/protocol/bc"
)
const serRequired = 0x7 // Bit mask accepted serialization flag.
// Tx holds a transaction along with its hash.
type Tx struct {
TxData
*bc.Tx `json:"-"`
}
// NewTx returns a new Tx containing data and its hash. If you have already
// computed the hash, use struct literal notation to make a Tx object directly.
func NewTx(data TxData) *Tx {
return &Tx{
TxData: data,
Tx: MapTx(&data),
}
}
// OutputID return the hash of the output position
func (tx *Tx) OutputID(outputIndex int) *bc.Hash {
return tx.ResultIds[outputIndex]
}
// UnmarshalText fulfills the encoding.TextUnmarshaler interface.
func (tx *Tx) UnmarshalText(p []byte) error {
if err := tx.TxData.UnmarshalText(p); err != nil {
return err
}
tx.Tx = MapTx(&tx.TxData)
return nil
}
// SetInputArguments sets the Arguments field in input n.
func (tx *Tx) SetInputArguments(n uint32, args [][]byte) {
tx.Inputs[n].SetArguments(args)
id := tx.Tx.InputIDs[n]
e := tx.Entries[id]
switch e := e.(type) {
case *bc.Spend:
e.WitnessArguments = args
case *bc.CrossChainInput:
e.WitnessArguments = args
case *bc.VetoInput:
e.WitnessArguments = args
}
}
// TxData encodes a transaction in the blockchain.
type TxData struct {
Version uint64
SerializedSize uint64
TimeRange uint64
Inputs []*TxInput
Outputs []*TxOutput
}
// MarshalText fulfills the json.Marshaler interface.
func (tx *TxData) MarshalText() ([]byte, error) {
var buf bytes.Buffer
if _, err := tx.WriteTo(&buf); err != nil {
return nil, err
}
b := make([]byte, hex.EncodedLen(buf.Len()))
hex.Encode(b, buf.Bytes())
return b, nil
}
// UnmarshalText fulfills the encoding.TextUnmarshaler interface.
func (tx *TxData) UnmarshalText(p []byte) error {
b := make([]byte, hex.DecodedLen(len(p)))
if _, err := hex.Decode(b, p); err != nil {
return err
}
r := blockchain.NewReader(b)
if err := tx.readFrom(r); err != nil {
return err
}
if trailing := r.Len(); trailing > 0 {
return fmt.Errorf("trailing garbage (%d bytes)", trailing)
}
return nil
}
func (tx *TxData) readFrom(r *blockchain.Reader) (err error) {
startSerializedSize := r.Len()
var serflags [1]byte
if _, err = io.ReadFull(r, serflags[:]); err != nil {
return errors.Wrap(err, "reading serialization flags")
}
if serflags[0] != serRequired {
return fmt.Errorf("unsupported serflags %#x", serflags[0])
}
if tx.Version, err = blockchain.ReadVarint63(r); err != nil {
return errors.Wrap(err, "reading transaction version")
}
if tx.TimeRange, err = blockchain.ReadVarint63(r); err != nil {
return err
}
n, err := blockchain.ReadVarint31(r)
if err != nil {
return errors.Wrap(err, "reading number of transaction inputs")
}
for ; n > 0; n-- {
ti := new(TxInput)
if err = ti.readFrom(r); err != nil {
return errors.Wrapf(err, "reading input %d", len(tx.Inputs))
}
tx.Inputs = append(tx.Inputs, ti)
}
n, err = blockchain.ReadVarint31(r)
if err != nil {
return errors.Wrap(err, "reading number of transaction outputs")
}
for ; n > 0; n-- {
to := new(TxOutput)
if err = to.readFrom(r); err != nil {
return errors.Wrapf(err, "reading output %d", len(tx.Outputs))
}
tx.Outputs = append(tx.Outputs, to)
}
tx.SerializedSize = uint64(startSerializedSize - r.Len())
return nil
}
// WriteTo writes tx to w.
func (tx *TxData) WriteTo(w io.Writer) (int64, error) {
ew := errors.NewWriter(w)
if err := tx.writeTo(ew, serRequired); err != nil {
return 0, err
}
return ew.Written(), ew.Err()
}
func (tx *TxData) writeTo(w io.Writer, serflags byte) error {
if _, err := w.Write([]byte{serflags}); err != nil {
return errors.Wrap(err, "writing serialization flags")
}
if _, err := blockchain.WriteVarint63(w, tx.Version); err != nil {
return errors.Wrap(err, "writing transaction version")
}
if _, err := blockchain.WriteVarint63(w, tx.TimeRange); err != nil {
return errors.Wrap(err, "writing transaction maxtime")
}
if _, err := blockchain.WriteVarint31(w, uint64(len(tx.Inputs))); err != nil {
return errors.Wrap(err, "writing tx input count")
}
for i, ti := range tx.Inputs {
if err := ti.writeTo(w); err != nil {
return errors.Wrapf(err, "writing tx input %d", i)
}
}
if _, err := blockchain.WriteVarint31(w, uint64(len(tx.Outputs))); err != nil {
return errors.Wrap(err, "writing tx output count")
}
for i, to := range tx.Outputs {
if err := to.writeTo(w); err != nil {
return errors.Wrapf(err, "writing tx output %d", i)
}
}
return nil
}