OSDN Git Service

init for remove issue (#63)
[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(112),
45                                 Inputs: []*TxInput{
46                                         NewCoinbaseInput([]byte("arbitrary")),
47                                 },
48                                 Outputs: []*TxOutput{
49                                         NewIntraChainOutput(*consensus.BTMAssetID, 254354, []byte("true")),
50                                         NewIntraChainOutput(*consensus.BTMAssetID, 254354, []byte("false")),
51                                 },
52                         }),
53                         hex: strings.Join([]string{
54                                 "07",                 // serflags
55                                 "01",                 // transaction version
56                                 "00",                 // tx time range
57                                 "01",                 // inputs count
58                                 "01",                 // input 0: asset version
59                                 "0b",                 // input 0: input commitment length
60                                 "02",                 // input 0: coinbase type flag
61                                 "09",                 // input 0: arbitrary length
62                                 "617262697472617279", // input 0: arbitrary data
63                                 "00",                 // input 0: witness length
64                                 "02",                 // outputs count
65                                 "01",                 // output 0: asset version
66                                 "2b",                 // output 0: serialization length
67                                 "00",                 // output 0: outType
68                                 "29",                 // output 0: output commitment length
69                                 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // output 0: assetID
70                                 "92c30f",   // output 0: amount
71                                 "01",       // output 0: version
72                                 "04",       // output 0: control program length
73                                 "74727565", // output 0: control program
74                                 "00",       // output 0: witness length
75                                 "01",       // output 1: asset version
76                                 "2c",       // output 1: serialization length
77                                 "00",       // output 1: outType
78                                 "2a",       // output 1: output commitment length
79                                 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // output 1: assetID
80                                 "92c30f",     // output 1: amount
81                                 "01",         // output 1: version
82                                 "05",         // output 1: control program length
83                                 "66616c7365", // output 1: control program
84                                 "00",         // output 1: witness length
85                         }, ""),
86                         hash: testutil.MustDecodeHash("2ef0831a69f4da1188af94a87ed6fd234500b979947aa706d47eb2d76359ad78"),
87                 },
88         }
89         for i, test := range cases {
90                 got := testutil.Serialize(t, test.tx)
91                 want, err := hex.DecodeString(test.hex)
92                 if err != nil {
93                         t.Fatal(err)
94                 }
95
96                 if !bytes.Equal(got, want) {
97                         t.Errorf("test %d: bytes = %x want %x", i, got, want)
98                 }
99                 if test.tx.ID != test.hash {
100                         t.Errorf("test %d: hash = %x want %x", i, test.tx.ID.Bytes(), test.hash.Bytes())
101                 }
102
103                 txJSON, err := json.Marshal(test.tx)
104                 if err != nil {
105                         t.Errorf("test %d: error marshaling tx to json: %s", i, err)
106                 }
107                 txFromJSON := Tx{}
108                 if err := json.Unmarshal(txJSON, &txFromJSON); err != nil {
109                         t.Errorf("test %d: error unmarshaling tx from json: %s", i, err)
110                 }
111                 if !testutil.DeepEqual(test.tx.TxData, txFromJSON.TxData) {
112                         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))
113                 }
114
115                 tx1 := new(TxData)
116                 if err := tx1.UnmarshalText([]byte(test.hex)); err != nil {
117                         t.Errorf("test %d: unexpected err %v", i, err)
118                 }
119                 if !testutil.DeepEqual(*tx1, test.tx.TxData) {
120                         t.Errorf("test %d: tx1 is:\n%swant:\n%s", i, spew.Sdump(*tx1), spew.Sdump(test.tx.TxData))
121                 }
122         }
123 }
124
125 func BenchmarkTxWriteToTrue(b *testing.B) {
126         tx := &Tx{}
127         for i := 0; i < b.N; i++ {
128                 tx.writeTo(ioutil.Discard, 0)
129         }
130 }
131
132 func BenchmarkTxWriteToFalse(b *testing.B) {
133         tx := &Tx{}
134         for i := 0; i < b.N; i++ {
135                 tx.writeTo(ioutil.Discard, serRequired)
136         }
137 }
138
139 func BenchmarkTxWriteToTrue200(b *testing.B) {
140         tx := &Tx{}
141         for i := 0; i < 200; i++ {
142                 tx.Inputs = append(tx.Inputs, NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil))
143                 tx.Outputs = append(tx.Outputs, NewIntraChainOutput(bc.AssetID{}, 0, nil))
144         }
145         for i := 0; i < b.N; i++ {
146                 tx.writeTo(ioutil.Discard, 0)
147         }
148 }
149
150 func BenchmarkTxWriteToFalse200(b *testing.B) {
151         tx := &Tx{}
152         for i := 0; i < 200; i++ {
153                 tx.Inputs = append(tx.Inputs, NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil))
154                 tx.Outputs = append(tx.Outputs, NewIntraChainOutput(bc.AssetID{}, 0, nil))
155         }
156         for i := 0; i < b.N; i++ {
157                 tx.writeTo(ioutil.Discard, serRequired)
158         }
159 }
160
161 func BenchmarkTxInputWriteToTrue(b *testing.B) {
162         input := NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil)
163         ew := errors.NewWriter(ioutil.Discard)
164         for i := 0; i < b.N; i++ {
165                 input.writeTo(ew)
166         }
167 }
168
169 func BenchmarkTxInputWriteToFalse(b *testing.B) {
170         input := NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil)
171         ew := errors.NewWriter(ioutil.Discard)
172         for i := 0; i < b.N; i++ {
173                 input.writeTo(ew)
174         }
175 }
176
177 func BenchmarkTxOutputWriteToTrue(b *testing.B) {
178         output := NewIntraChainOutput(bc.AssetID{}, 0, nil)
179         ew := errors.NewWriter(ioutil.Discard)
180         for i := 0; i < b.N; i++ {
181                 output.writeTo(ew)
182         }
183 }
184
185 func BenchmarkTxOutputWriteToFalse(b *testing.B) {
186         output := NewIntraChainOutput(bc.AssetID{}, 0, nil)
187         ew := errors.NewWriter(ioutil.Discard)
188         for i := 0; i < b.N; i++ {
189                 output.writeTo(ew)
190         }
191 }
192
193 func BenchmarkAssetAmountWriteTo(b *testing.B) {
194         aa := bc.AssetAmount{}
195         for i := 0; i < b.N; i++ {
196                 aa.WriteTo(ioutil.Discard)
197         }
198 }