OSDN Git Service

feat: add cross-chain output (#56)
[bytom/vapor.git] / protocol / bc / types / transaction_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "encoding/json"
7         "io/ioutil"
8         "strings"
9         "testing"
10
11         "github.com/davecgh/go-spew/spew"
12
13         "github.com/vapor/consensus"
14         "github.com/vapor/errors"
15         "github.com/vapor/protocol/bc"
16         "github.com/vapor/testutil"
17 )
18
19 func TestTransaction(t *testing.T) {
20         cases := []struct {
21                 tx   *Tx
22                 hex  string
23                 hash bc.Hash
24         }{
25                 {
26                         tx: NewTx(TxData{
27                                 Version:        1,
28                                 SerializedSize: uint64(5),
29                                 Inputs:         nil,
30                                 Outputs:        nil,
31                         }),
32                         hex: strings.Join([]string{
33                                 "07", // serflags
34                                 "01", // transaction version
35                                 "00", // tx time range
36                                 "00", // inputs count
37                                 "00", // outputs count
38                         }, ""),
39                         hash: testutil.MustDecodeHash("8e88b9cb4615128c7209dff695f68b8de5b38648bf3d44d2d0e6a674848539c9"),
40                 },
41                 {
42                         tx: NewTx(TxData{
43                                 Version:        1,
44                                 SerializedSize: uint64(263),
45                                 TimeRange:      654,
46                                 Inputs: []*TxInput{
47                                         NewIssuanceInput([]byte("nonce"), 254354, []byte("issuanceProgram"), [][]byte{[]byte("arguments1"), []byte("arguments2")}, []byte("assetDefinition")),
48                                         NewSpendInput([][]byte{[]byte("arguments3"), []byte("arguments4")}, testutil.MustDecodeHash("fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409"), *consensus.BTMAssetID, 254354, 3, []byte("spendProgram")),
49                                 },
50                                 Outputs: []*TxOutput{
51                                         NewIntraChainOutput(testutil.MustDecodeAsset("a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf"), 254354, []byte("true")),
52                                 },
53                         }),
54                         hex: strings.Join([]string{
55                                 "07",         // serflags
56                                 "01",         // transaction version
57                                 "8e05",       // tx time range
58                                 "02",         // inputs count
59                                 "01",         // input 0: asset version
60                                 "2a",         // input 0: serialization length
61                                 "00",         // input 0: issuance type flag
62                                 "05",         // input 0: nonce length
63                                 "6e6f6e6365", // input 0: nonce
64                                 "a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf", // input 0: assetID
65                                 "92c30f", // input 0: amount
66                                 "38",     // input 0: input witness length
67                                 "0f",     // input 0: asset definition length
68                                 "6173736574446566696e6974696f6e", // input 0: asset definition
69                                 "01", // input 0: vm version
70                                 "0f", // input 0: issuanceProgram length
71                                 "69737375616e636550726f6772616d", // input 0: issuance program
72                                 "02", // input 0: argument array length
73                                 "0a", // input 0: first argument length
74                                 "617267756d656e747331", // input 0: first argument data
75                                 "0a", // input 0: second argument length
76                                 "617267756d656e747332", // input 0: second argument data
77                                 "01", // input 1: asset version
78                                 "54", // input 1: input commitment length
79                                 "01", // input 1: spend type flag
80                                 "52", // input 1: spend commitment length
81                                 "fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409", // input 1: source id
82                                 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // input 1: assetID
83                                 "92c30f", // input 1: amount
84                                 "03",     // input 1: source position
85                                 "01",     // input 1: vm version
86                                 "0c",     // input 1: spend program length
87                                 "7370656e6450726f6772616d", // input 1: spend program
88                                 "17", // input 1: witness length
89                                 "02", // input 1: argument array length
90                                 "0a", // input 1: first argument length
91                                 "617267756d656e747333", // input 1: first argument data
92                                 "0a", // input 1: second argument length
93                                 "617267756d656e747334", // input 1: second argument data
94                                 "01", // outputs count
95                                 "01", // output 0: asset version
96                                 "2b", // output 0: serialization length
97                                 "00", // output 0: outType
98                                 "29", // output 0: output commitment length
99                                 "a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf", // output 0: assetID
100                                 "92c30f",   // output 0: amount
101                                 "01",       // output 0: version
102                                 "04",       // output 0: control program length
103                                 "74727565", // output 0: control program
104                                 "00",       // output 0: witness length
105                         }, ""),
106                         hash: testutil.MustDecodeHash("d6a1f6ea3cc3c53c3cdcd10bbd446c643f23b37ca5e87665d03f204de8c7dc2a"),
107                 },
108                 {
109                         tx: NewTx(TxData{
110                                 Version:        1,
111                                 SerializedSize: uint64(112),
112                                 Inputs: []*TxInput{
113                                         NewCoinbaseInput([]byte("arbitrary")),
114                                 },
115                                 Outputs: []*TxOutput{
116                                         NewIntraChainOutput(*consensus.BTMAssetID, 254354, []byte("true")),
117                                         NewIntraChainOutput(*consensus.BTMAssetID, 254354, []byte("false")),
118                                 },
119                         }),
120                         hex: strings.Join([]string{
121                                 "07",                 // serflags
122                                 "01",                 // transaction version
123                                 "00",                 // tx time range
124                                 "01",                 // inputs count
125                                 "01",                 // input 0: asset version
126                                 "0b",                 // input 0: input commitment length
127                                 "02",                 // input 0: coinbase type flag
128                                 "09",                 // input 0: arbitrary length
129                                 "617262697472617279", // input 0: arbitrary data
130                                 "00",                 // input 0: witness length
131                                 "02",                 // outputs count
132                                 "01",                 // output 0: asset version
133                                 "2b",                 // output 0: serialization length
134                                 "00",                 // output 0: outType
135                                 "29",                 // output 0: output commitment length
136                                 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // output 0: assetID
137                                 "92c30f",   // output 0: amount
138                                 "01",       // output 0: version
139                                 "04",       // output 0: control program length
140                                 "74727565", // output 0: control program
141                                 "00",       // output 0: witness length
142                                 "01",       // output 1: asset version
143                                 "2c",       // output 1: serialization length
144                                 "00",       // output 1: outType
145                                 "2a",       // output 1: output commitment length
146                                 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // output 1: assetID
147                                 "92c30f",     // output 1: amount
148                                 "01",         // output 1: version
149                                 "05",         // output 1: control program length
150                                 "66616c7365", // output 1: control program
151                                 "00",         // output 1: witness length
152                         }, ""),
153                         hash: testutil.MustDecodeHash("2ef0831a69f4da1188af94a87ed6fd234500b979947aa706d47eb2d76359ad78"),
154                 },
155         }
156         for i, test := range cases {
157                 got := testutil.Serialize(t, test.tx)
158                 want, err := hex.DecodeString(test.hex)
159                 if err != nil {
160                         t.Fatal(err)
161                 }
162
163                 if !bytes.Equal(got, want) {
164                         t.Errorf("test %d: bytes = %x want %x", i, got, want)
165                 }
166                 if test.tx.ID != test.hash {
167                         t.Errorf("test %d: hash = %x want %x", i, test.tx.ID.Bytes(), test.hash.Bytes())
168                 }
169
170                 txJSON, err := json.Marshal(test.tx)
171                 if err != nil {
172                         t.Errorf("test %d: error marshaling tx to json: %s", i, err)
173                 }
174                 txFromJSON := Tx{}
175                 if err := json.Unmarshal(txJSON, &txFromJSON); err != nil {
176                         t.Errorf("test %d: error unmarshaling tx from json: %s", i, err)
177                 }
178                 if !testutil.DeepEqual(test.tx.TxData, txFromJSON.TxData) {
179                         t.Errorf("test %d: types.TxData -> json -> types.TxData: got:\n%s\nwant:\n%s", i, spew.Sdump(txFromJSON.TxData), spew.Sdump(test.tx.TxData))
180                 }
181
182                 tx1 := new(TxData)
183                 if err := tx1.UnmarshalText([]byte(test.hex)); err != nil {
184                         t.Errorf("test %d: unexpected err %v", i, err)
185                 }
186                 if !testutil.DeepEqual(*tx1, test.tx.TxData) {
187                         t.Errorf("test %d: tx1 is:\n%swant:\n%s", i, spew.Sdump(*tx1), spew.Sdump(test.tx.TxData))
188                 }
189         }
190 }
191
192 func TestTransactionTrailingGarbage(t *testing.T) {
193         // validTxHex is a valid tx, we don't care what's inside as long as it's valid
194         validTxHex := `07010001012b00030a0908916133a0d64d1d973b631e226ef95338ad4a536b95635f32f0d04708a6f2a26380a094a58d1d0900010101010301020301012b0029000000000000000000000000000000000000000000000000000000000000000080a094a58d1d01010100`
195         validTx := Tx{}
196         if err := validTx.UnmarshalText([]byte(validTxHex)); err != nil {
197                 t.Fatal(err)
198         }
199
200         invalidTxHex := validTxHex + strings.Repeat("00", 10)
201         invalidTx := Tx{}
202         if err := invalidTx.UnmarshalText([]byte(invalidTxHex)); err == nil {
203                 t.Fatal("expected error with trailing garbage but got nil")
204         }
205 }
206
207 func TestInvalidIssuance(t *testing.T) {
208         hex := strings.Join([]string{
209                 "07",     // serflags
210                 "01",     // transaction version
211                 "00",     // tx maxtime
212                 "01",     // inputs count
213                 "01",     // input 0, asset version
214                 "2b",     // input 0, input commitment length prefix
215                 "00",     // input 0, input commitment, "issuance" type
216                 "03",     // input 0, input commitment, nonce length prefix
217                 "0a0908", // input 0, input commitment, nonce
218                 "0000000000000000000000000000000000000000000000000000000000000000", // input 0, input commitment, WRONG asset id
219                 "80a094a58d1d", // input 0, input commitment, amount
220                 "29",           // input 0, issuance input witness length prefix
221                 "03deff1d4319d67baa10a6d26c1fea9c3e8d30e33474efee1a610a9bb49d758d", // input 0, issuance input witness, initial block
222                 "00",     // input 0, issuance input witness, asset definition
223                 "01",     // input 0, issuance input witness, vm version
224                 "01",     // input 0, issuance input witness, issuance program length prefix
225                 "01",     // input 0, issuance input witness, issuance program
226                 "01",     // input 0, issuance input witness, arguments count
227                 "03",     // input 0, issuance input witness, argument 0 length prefix
228                 "010203", // input 0, issuance input witness, argument 0
229                 "01",     // outputs count
230                 "01",     // output 0, asset version
231                 "2b",     // output 0, serialization length
232                 "00",     // output 0, outType
233                 "29",     // output 0, output commitment length
234                 "0000000000000000000000000000000000000000000000000000000000000000", // output 0, output commitment, asset id
235                 "80a094a58d1d",   // output 0, output commitment, amount
236                 "01",             // output 0, output commitment, vm version
237                 "0101",           // output 0, output commitment, control program
238                 "066f7574707574", // output 0, reference data
239                 "00",             // output 0, output witness
240                 "0869737375616e6365",
241         }, "")
242
243         tx := new(TxData)
244         if err := tx.UnmarshalText([]byte(hex)); errors.Root(err) != errBadAssetID {
245                 t.Errorf("want errBadAssetID, got %v", err)
246         }
247 }
248
249 func TestFuzzUnknownAssetVersion(t *testing.T) {
250         rawTx := `07010001012b00030a0908916133a0d64d1d973b631e226ef95338ad4a536b95635f32f0d04708a6f2a26380a094a58d1d0900010101010301020301012b0029000000000000000000000000000000000000000000000000000000000000000080a094a58d1d01010100`
251         want := Tx{}
252         if err := want.UnmarshalText([]byte(rawTx)); err != nil {
253                 t.Fatal(err)
254         }
255
256         b, err := want.MarshalText()
257         if err != nil {
258                 t.Fatal(err)
259         }
260
261         got := Tx{}
262         if err = got.UnmarshalText(b); err != nil {
263                 t.Fatal(err)
264         }
265         if got.ID.String() != want.ID.String() {
266                 t.Errorf("tx id changed to %s", got.ID.String())
267         }
268 }
269
270 func BenchmarkTxWriteToTrue(b *testing.B) {
271         tx := &Tx{}
272         for i := 0; i < b.N; i++ {
273                 tx.writeTo(ioutil.Discard, 0)
274         }
275 }
276
277 func BenchmarkTxWriteToFalse(b *testing.B) {
278         tx := &Tx{}
279         for i := 0; i < b.N; i++ {
280                 tx.writeTo(ioutil.Discard, serRequired)
281         }
282 }
283
284 func BenchmarkTxWriteToTrue200(b *testing.B) {
285         tx := &Tx{}
286         for i := 0; i < 200; i++ {
287                 tx.Inputs = append(tx.Inputs, NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil))
288                 tx.Outputs = append(tx.Outputs, NewIntraChainOutput(bc.AssetID{}, 0, nil))
289         }
290         for i := 0; i < b.N; i++ {
291                 tx.writeTo(ioutil.Discard, 0)
292         }
293 }
294
295 func BenchmarkTxWriteToFalse200(b *testing.B) {
296         tx := &Tx{}
297         for i := 0; i < 200; i++ {
298                 tx.Inputs = append(tx.Inputs, NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil))
299                 tx.Outputs = append(tx.Outputs, NewIntraChainOutput(bc.AssetID{}, 0, nil))
300         }
301         for i := 0; i < b.N; i++ {
302                 tx.writeTo(ioutil.Discard, serRequired)
303         }
304 }
305
306 func BenchmarkTxInputWriteToTrue(b *testing.B) {
307         input := NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil)
308         ew := errors.NewWriter(ioutil.Discard)
309         for i := 0; i < b.N; i++ {
310                 input.writeTo(ew)
311         }
312 }
313
314 func BenchmarkTxInputWriteToFalse(b *testing.B) {
315         input := NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil)
316         ew := errors.NewWriter(ioutil.Discard)
317         for i := 0; i < b.N; i++ {
318                 input.writeTo(ew)
319         }
320 }
321
322 func BenchmarkTxOutputWriteToTrue(b *testing.B) {
323         output := NewIntraChainOutput(bc.AssetID{}, 0, nil)
324         ew := errors.NewWriter(ioutil.Discard)
325         for i := 0; i < b.N; i++ {
326                 output.writeTo(ew)
327         }
328 }
329
330 func BenchmarkTxOutputWriteToFalse(b *testing.B) {
331         output := NewIntraChainOutput(bc.AssetID{}, 0, nil)
332         ew := errors.NewWriter(ioutil.Discard)
333         for i := 0; i < b.N; i++ {
334                 output.writeTo(ew)
335         }
336 }
337
338 func BenchmarkAssetAmountWriteTo(b *testing.B) {
339         aa := bc.AssetAmount{}
340         for i := 0; i < b.N; i++ {
341                 aa.WriteTo(ioutil.Discard)
342         }
343 }