OSDN Git Service

0ae1f62f8c3afc6c517fcc7bc7d85ac7333cb690
[bytom/vapor.git] / protocol / bc / types / txoutput_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "strings"
7         "testing"
8
9         "github.com/davecgh/go-spew/spew"
10
11         "github.com/vapor/encoding/blockchain"
12         "github.com/vapor/protocol/bc"
13         "github.com/vapor/testutil"
14 )
15
16 func TestSerializationIntraChainTxOutput(t *testing.T) {
17         assetID := testutil.MustDecodeAsset("81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47")
18         txOutput := NewIntraChainOutput(assetID, 254354, []byte("TestSerializationTxOutput"))
19         wantHex := strings.Join([]string{
20                 "01", // asset version
21                 "40", // serialization length
22                 "00", // outType
23                 "3e", // output commitment length
24                 "81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47", // assetID
25                 "92c30f", // amount
26                 "01",     // version
27                 "19",     // control program length
28                 "5465737453657269616c697a6174696f6e54784f7574707574", // control program
29                 "00", // witness length
30         }, "")
31
32         // Test convert struct to hex
33         var buffer bytes.Buffer
34         if err := txOutput.writeTo(&buffer); err != nil {
35                 t.Fatal(err)
36         }
37
38         gotHex := hex.EncodeToString(buffer.Bytes())
39         if gotHex != wantHex {
40                 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex)
41         }
42
43         // Test convert hex to struct
44         var gotTxOutput TxOutput
45         decodeHex, err := hex.DecodeString(wantHex)
46         if err != nil {
47                 t.Fatal(err)
48         }
49
50         if err := gotTxOutput.readFrom(blockchain.NewReader(decodeHex)); err != nil {
51                 t.Fatal(err)
52         }
53
54         if !testutil.DeepEqual(*txOutput, gotTxOutput) {
55                 t.Errorf("expected marshaled/unmarshaled txoutput to be:\n%sgot:\n%s", spew.Sdump(*txOutput), spew.Sdump(gotTxOutput))
56         }
57 }
58
59 func TestSerializationCrossChainTxOutput(t *testing.T) {
60         assetID := testutil.MustDecodeAsset("81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47")
61         txOutput := NewCrossChainOutput(assetID, 254354, []byte("TestSerializationTxOutput"))
62         wantHex := strings.Join([]string{
63                 "01", // asset version
64                 "40", // serialization length
65                 "01", // outType
66                 "3e", // output commitment length
67                 "81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47", // assetID
68                 "92c30f", // amount
69                 "01",     // version
70                 "19",     // control program length
71                 "5465737453657269616c697a6174696f6e54784f7574707574", // control program
72                 "00", // witness length
73         }, "")
74
75         // Test convert struct to hex
76         var buffer bytes.Buffer
77         if err := txOutput.writeTo(&buffer); err != nil {
78                 t.Fatal(err)
79         }
80
81         gotHex := hex.EncodeToString(buffer.Bytes())
82         if gotHex != wantHex {
83                 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex)
84         }
85
86         // Test convert hex to struct
87         var gotTxOutput TxOutput
88         decodeHex, err := hex.DecodeString(wantHex)
89         if err != nil {
90                 t.Fatal(err)
91         }
92
93         if err := gotTxOutput.readFrom(blockchain.NewReader(decodeHex)); err != nil {
94                 t.Fatal(err)
95         }
96
97         if !testutil.DeepEqual(*txOutput, gotTxOutput) {
98                 t.Errorf("expected marshaled/unmarshaled txoutput to be:\n%sgot:\n%s", spew.Sdump(*txOutput), spew.Sdump(gotTxOutput))
99         }
100 }
101
102 func TestSerializationVoteTxOutput(t *testing.T) {
103         assetID := testutil.MustDecodeAsset("81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47")
104         voteTxOutput := NewVoteOutput(assetID, 1000, []byte("TestSerializationTxOutput"), []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"))
105
106         wantHex := strings.Join([]string{
107                 "01",   // asset version
108                 "c101", // serialization length
109                 "02",   // outType
110                 "8001", // output xpub length
111                 "6166353934303036613430383337643966303238646161626236643538396466306239313338646165666164353638336535323333633236343632373932313732393461386435333265363038363362636631393636323561333566623863656566666133633039363130656239326463666236353561393437663133323639", // xpub
112                 "3d", // output commitment length
113                 "81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47", // assetID
114                 "e807", // amount
115                 "01",   // version
116                 "19",   // control program length
117                 "5465737453657269616c697a6174696f6e54784f7574707574", // control program
118                 "00", // witness length
119         }, "")
120
121         // Test convert struct to hex
122         var buffer bytes.Buffer
123         if err := voteTxOutput.writeTo(&buffer); err != nil {
124                 t.Fatal(err)
125         }
126
127         gotHex := hex.EncodeToString(buffer.Bytes())
128         if gotHex != wantHex {
129                 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex)
130         }
131
132         // Test convert hex to struct
133         var gotTxOutput TxOutput
134         decodeHex, err := hex.DecodeString(wantHex)
135         if err != nil {
136                 t.Fatal(err)
137         }
138
139         if err := gotTxOutput.readFrom(blockchain.NewReader(decodeHex)); err != nil {
140                 t.Fatal(err)
141         }
142
143         if !testutil.DeepEqual(*voteTxOutput, gotTxOutput) {
144                 t.Errorf("expected marshaled/unmarshaled txoutput to be:\n%sgot:\n%s", spew.Sdump(*voteTxOutput), spew.Sdump(gotTxOutput))
145         }
146 }
147
148 func TestComputeOutputID(t *testing.T) {
149         btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
150         cases := []struct {
151                 sc           *SpendCommitment
152                 inputType    uint8
153                 vote         []byte
154                 wantOutputID string
155         }{
156                 {
157                         sc: &SpendCommitment{
158                                 AssetAmount:    bc.AssetAmount{AssetId: &btmAssetID, Amount: 1000},
159                                 SourceID:       testutil.MustDecodeHash("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"),
160                                 SourcePosition: 2,
161                                 VMVersion:      1,
162                                 ControlProgram: testutil.MustDecodeHexString("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"),
163                         },
164                         inputType:    SpendInputType,
165                         vote:         nil,
166                         wantOutputID: "73eea4d38b22ffd60fc30d0941f3875f45e29d424227bfde100193a08568605b",
167                 },
168                 {
169                         sc: &SpendCommitment{
170                                 AssetAmount:    bc.AssetAmount{AssetId: &btmAssetID, Amount: 999},
171                                 SourceID:       testutil.MustDecodeHash("9e74e35362ffc73c8967aa0008da8fcbc62a21d35673fb970445b5c2972f8603"),
172                                 SourcePosition: 2,
173                                 VMVersion:      1,
174                                 ControlProgram: testutil.MustDecodeHexString("001418549d84daf53344d32563830c7cf979dc19d5c0"),
175                         },
176                         inputType:    SpendInputType,
177                         vote:         nil,
178                         wantOutputID: "8371e76fd1c873503a326268bfd286ffe13009a0f1140d2c858e8187825696ab",
179                 },
180                 {
181                         sc: &SpendCommitment{
182                                 AssetAmount:    bc.AssetAmount{AssetId: &btmAssetID, Amount: 999},
183                                 SourceID:       testutil.MustDecodeHash("993d3797fa3b2d958f300e599987dc10904b13f56ce89d158f60f9131424e0e2"),
184                                 SourcePosition: 2,
185                                 VMVersion:      1,
186                                 ControlProgram: testutil.MustDecodeHexString("00145c47f3a0dd3e1e9956fe5b0f897072ed33f9efb9"),
187                         },
188                         inputType:    VetoInputType,
189                         vote:         []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"),
190                         wantOutputID: "a4de5a81babc7949d6b38d1cd4bcbc83da340387e747b5f521af3e427c6b0132",
191                 },
192                 {
193                         sc: &SpendCommitment{
194                                 AssetAmount:    bc.AssetAmount{AssetId: &btmAssetID, Amount: 999},
195                                 SourceID:       testutil.MustDecodeHash("993d3797fa3b2d958f300e599987dc10904b13f56ce89d158f60f9131424e0e2"),
196                                 SourcePosition: 2,
197                                 VMVersion:      1,
198                                 ControlProgram: testutil.MustDecodeHexString("00145c47f3a0dd3e1e9956fe5b0f897072ed33f9efb9"),
199                         },
200                         inputType:    VetoInputType,
201                         vote:         []byte(""),
202                         wantOutputID: "e42a48ef401b993c5e523b6a7b5456ad4b297c7aeda163405f265d8d00af983e",
203                 },
204         }
205
206         for _, c := range cases {
207                 outputID, err := ComputeOutputID(c.sc, c.inputType, c.vote)
208                 if err != nil {
209                         t.Fatal(err)
210                 }
211
212                 if c.wantOutputID != outputID.String() {
213                         t.Errorf("test compute output id fail, got:%s, want:%s", outputID.String(), c.wantOutputID)
214                 }
215         }
216 }