OSDN Git Service

votetx output (#62)
[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 TestSerializationTxOutput(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 TestSerializationVoteTxOutput(t *testing.T) {
60         assetID := testutil.MustDecodeAsset("81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47")
61         voteTxOutput := NewVoteOutput(assetID, 1000, []byte("TestSerializationTxOutput"), []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"))
62
63         wantHex := strings.Join([]string{
64                 "01",   // asset version
65                 "c101", // serialization length
66                 "02",   // outType
67                 "8001", // output xpub length
68                 "6166353934303036613430383337643966303238646161626236643538396466306239313338646165666164353638336535323333633236343632373932313732393461386435333265363038363362636631393636323561333566623863656566666133633039363130656239326463666236353561393437663133323639", // xpub
69                 "3d", // output commitment length
70                 "81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47", // assetID
71                 "e807", // amount
72                 "01",   // version
73                 "19",   // control program length
74                 "5465737453657269616c697a6174696f6e54784f7574707574", // control program
75                 "00", // witness length
76         }, "")
77
78         // Test convert struct to hex
79         var buffer bytes.Buffer
80         if err := voteTxOutput.writeTo(&buffer); err != nil {
81                 t.Fatal(err)
82         }
83
84         gotHex := hex.EncodeToString(buffer.Bytes())
85         if gotHex != wantHex {
86                 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex)
87         }
88
89         // Test convert hex to struct
90         var gotTxOutput TxOutput
91         decodeHex, err := hex.DecodeString(wantHex)
92         if err != nil {
93                 t.Fatal(err)
94         }
95
96         if err := gotTxOutput.readFrom(blockchain.NewReader(decodeHex)); err != nil {
97                 t.Fatal(err)
98         }
99
100         if !testutil.DeepEqual(*voteTxOutput, gotTxOutput) {
101                 t.Errorf("expected marshaled/unmarshaled txoutput to be:\n%sgot:\n%s", spew.Sdump(*voteTxOutput), spew.Sdump(gotTxOutput))
102         }
103 }
104
105 func TestComputeOutputID(t *testing.T) {
106         btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
107         cases := []struct {
108                 sc           *SpendCommitment
109                 wantOutputID string
110         }{
111                 {
112                         sc: &SpendCommitment{
113                                 AssetAmount:    bc.AssetAmount{AssetId: &btmAssetID, Amount: 1000},
114                                 SourceID:       testutil.MustDecodeHash("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"),
115                                 SourcePosition: 2,
116                                 VMVersion:      1,
117                                 ControlProgram: testutil.MustDecodeHexString("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"),
118                         },
119                         wantOutputID: "73eea4d38b22ffd60fc30d0941f3875f45e29d424227bfde100193a08568605b",
120                 },
121                 {
122                         sc: &SpendCommitment{
123                                 AssetAmount:    bc.AssetAmount{AssetId: &btmAssetID, Amount: 999},
124                                 SourceID:       testutil.MustDecodeHash("9e74e35362ffc73c8967aa0008da8fcbc62a21d35673fb970445b5c2972f8603"),
125                                 SourcePosition: 2,
126                                 VMVersion:      1,
127                                 ControlProgram: testutil.MustDecodeHexString("001418549d84daf53344d32563830c7cf979dc19d5c0"),
128                         },
129                         wantOutputID: "8371e76fd1c873503a326268bfd286ffe13009a0f1140d2c858e8187825696ab",
130                 },
131         }
132
133         for _, c := range cases {
134                 outputID, err := ComputeOutputID(c.sc)
135                 if err != nil {
136                         t.Fatal(err)
137                 }
138
139                 if c.wantOutputID != outputID.String() {
140                         t.Errorf("test compute output id fail, got:%s, want:%s", outputID.String(), c.wantOutputID)
141                 }
142         }
143 }