OSDN Git Service

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