11 "github.com/davecgh/go-spew/spew"
13 "github.com/vapor/consensus"
14 "github.com/vapor/errors"
15 "github.com/vapor/protocol/bc"
16 "github.com/vapor/testutil"
19 func TestTransaction(t *testing.T) {
28 SerializedSize: uint64(5),
32 hex: strings.Join([]string{
34 "01", // transaction version
35 "00", // tx time range
37 "00", // outputs count
39 hash: testutil.MustDecodeHash("8e88b9cb4615128c7209dff695f68b8de5b38648bf3d44d2d0e6a674848539c9"),
44 SerializedSize: uint64(112),
46 NewCoinbaseInput([]byte("arbitrary")),
49 NewIntraChainOutput(*consensus.BTMAssetID, 254354, []byte("true")),
50 NewIntraChainOutput(*consensus.BTMAssetID, 254354, []byte("false")),
53 hex: strings.Join([]string{
55 "01", // transaction version
56 "00", // tx time range
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
86 hash: testutil.MustDecodeHash("2591a2af0d3690107215c2a47ab60c4e8d7547f04154ecd5ccab1db0d31e66b4"),
89 for i, test := range cases {
90 got := testutil.Serialize(t, test.tx)
91 want, err := hex.DecodeString(test.hex)
96 if !bytes.Equal(got, want) {
97 t.Errorf("test %d: bytes = %x want %x", i, got, want)
99 if test.tx.ID != test.hash {
100 t.Errorf("test %d: hash = %x want %x", i, test.tx.ID.Bytes(), test.hash.Bytes())
103 txJSON, err := json.Marshal(test.tx)
105 t.Errorf("test %d: error marshaling tx to json: %s", i, err)
108 if err := json.Unmarshal(txJSON, &txFromJSON); err != nil {
109 t.Errorf("test %d: error unmarshaling tx from json: %s", i, err)
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))
116 if err := tx1.UnmarshalText([]byte(test.hex)); err != nil {
117 t.Errorf("test %d: unexpected err %v", i, err)
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))
125 func BenchmarkTxWriteToTrue(b *testing.B) {
127 for i := 0; i < b.N; i++ {
128 tx.writeTo(ioutil.Discard, 0)
132 func BenchmarkTxWriteToFalse(b *testing.B) {
134 for i := 0; i < b.N; i++ {
135 tx.writeTo(ioutil.Discard, serRequired)
139 func BenchmarkTxWriteToTrue200(b *testing.B) {
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))
145 for i := 0; i < b.N; i++ {
146 tx.writeTo(ioutil.Discard, 0)
150 func BenchmarkTxWriteToFalse200(b *testing.B) {
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))
156 for i := 0; i < b.N; i++ {
157 tx.writeTo(ioutil.Discard, serRequired)
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++ {
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++ {
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++ {
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++ {
193 func BenchmarkAssetAmountWriteTo(b *testing.B) {
194 aa := bc.AssetAmount{}
195 for i := 0; i < b.N; i++ {
196 aa.WriteTo(ioutil.Discard)