9 "github.com/vapor/encoding/blockchain"
10 "github.com/vapor/errors"
11 "github.com/vapor/protocol/bc"
12 "github.com/vapor/protocol/bc/types/bytom"
15 const serRequired = 0x7 // Bit mask accepted serialization flag.
17 // Tx holds a transaction along with its hash.
23 // NewTx returns a new Tx containing data and its hash. If you have already
24 // computed the hash, use struct literal notation to make a Tx object directly.
25 func NewTx(data TxData) *Tx {
32 // OutputID return the hash of the output position
33 func (tx *Tx) OutputID(outputIndex int) *bc.Hash {
34 return tx.ResultIds[outputIndex]
37 // UnmarshalText fulfills the encoding.TextUnmarshaler interface.
38 func (tx *Tx) UnmarshalText(p []byte) error {
39 if err := tx.TxData.UnmarshalText(p); err != nil {
43 tx.Tx = MapTx(&tx.TxData)
47 // SetInputArguments sets the Arguments field in input n.
48 func (tx *Tx) SetInputArguments(n uint32, args [][]byte) {
49 tx.Inputs[n].SetArguments(args)
50 id := tx.Tx.InputIDs[n]
52 switch e := e.(type) {
54 e.WitnessArguments = args
56 e.WitnessArguments = args
60 // TxData encodes a transaction in the blockchain.
69 // MarshalText fulfills the json.Marshaler interface.
70 func (tx *TxData) MarshalText() ([]byte, error) {
72 if _, err := tx.WriteTo(&buf); err != nil {
76 b := make([]byte, hex.EncodedLen(buf.Len()))
77 hex.Encode(b, buf.Bytes())
81 // UnmarshalText fulfills the encoding.TextUnmarshaler interface.
82 func (tx *TxData) UnmarshalText(p []byte) error {
83 b := make([]byte, hex.DecodedLen(len(p)))
84 if _, err := hex.Decode(b, p); err != nil {
88 r := blockchain.NewReader(b)
89 if err := tx.readFrom(r); err != nil {
93 if trailing := r.Len(); trailing > 0 {
94 return fmt.Errorf("trailing garbage (%d bytes)", trailing)
99 func (tx *TxData) readFrom(r *blockchain.Reader) (err error) {
100 startSerializedSize := r.Len()
102 if _, err = io.ReadFull(r, serflags[:]); err != nil {
103 return errors.Wrap(err, "reading serialization flags")
105 if serflags[0] != serRequired {
106 return fmt.Errorf("unsupported serflags %#x", serflags[0])
109 if tx.Version, err = blockchain.ReadVarint63(r); err != nil {
110 return errors.Wrap(err, "reading transaction version")
112 if tx.TimeRange, err = blockchain.ReadVarint63(r); err != nil {
116 n, err := blockchain.ReadVarint31(r)
118 return errors.Wrap(err, "reading number of transaction inputs")
123 if err = ti.readFrom(r); err != nil {
124 return errors.Wrapf(err, "reading input %d", len(tx.Inputs))
126 tx.Inputs = append(tx.Inputs, ti)
129 n, err = blockchain.ReadVarint31(r)
131 return errors.Wrap(err, "reading number of transaction outputs")
136 if err = to.readFrom(r); err != nil {
137 return errors.Wrapf(err, "reading output %d", len(tx.Outputs))
139 tx.Outputs = append(tx.Outputs, to)
141 tx.SerializedSize = uint64(startSerializedSize - r.Len())
145 // WriteTo writes tx to w.
146 func (tx *TxData) WriteTo(w io.Writer) (int64, error) {
147 ew := errors.NewWriter(w)
148 if err := tx.writeTo(ew, serRequired); err != nil {
151 return ew.Written(), ew.Err()
154 func (tx *TxData) writeTo(w io.Writer, serflags byte) error {
155 if _, err := w.Write([]byte{serflags}); err != nil {
156 return errors.Wrap(err, "writing serialization flags")
158 if _, err := blockchain.WriteVarint63(w, tx.Version); err != nil {
159 return errors.Wrap(err, "writing transaction version")
161 if _, err := blockchain.WriteVarint63(w, tx.TimeRange); err != nil {
162 return errors.Wrap(err, "writing transaction maxtime")
165 if _, err := blockchain.WriteVarint31(w, uint64(len(tx.Inputs))); err != nil {
166 return errors.Wrap(err, "writing tx input count")
169 for i, ti := range tx.Inputs {
170 if err := ti.writeTo(w); err != nil {
171 return errors.Wrapf(err, "writing tx input %d", i)
175 if _, err := blockchain.WriteVarint31(w, uint64(len(tx.Outputs))); err != nil {
176 return errors.Wrap(err, "writing tx output count")
179 for i, to := range tx.Outputs {
180 if err := to.writeTo(w); err != nil {
181 return errors.Wrapf(err, "writing tx output %d", i)