OSDN Git Service

feat: add cross-chain output (#56)
[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 TestComputeOutputID(t *testing.T) {
60         btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
61         cases := []struct {
62                 sc           *SpendCommitment
63                 wantOutputID string
64         }{
65                 {
66                         sc: &SpendCommitment{
67                                 AssetAmount:    bc.AssetAmount{AssetId: &btmAssetID, Amount: 1000},
68                                 SourceID:       testutil.MustDecodeHash("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"),
69                                 SourcePosition: 2,
70                                 VMVersion:      1,
71                                 ControlProgram: testutil.MustDecodeHexString("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"),
72                         },
73                         wantOutputID: "73eea4d38b22ffd60fc30d0941f3875f45e29d424227bfde100193a08568605b",
74                 },
75                 {
76                         sc: &SpendCommitment{
77                                 AssetAmount:    bc.AssetAmount{AssetId: &btmAssetID, Amount: 999},
78                                 SourceID:       testutil.MustDecodeHash("9e74e35362ffc73c8967aa0008da8fcbc62a21d35673fb970445b5c2972f8603"),
79                                 SourcePosition: 2,
80                                 VMVersion:      1,
81                                 ControlProgram: testutil.MustDecodeHexString("001418549d84daf53344d32563830c7cf979dc19d5c0"),
82                         },
83                         wantOutputID: "8371e76fd1c873503a326268bfd286ffe13009a0f1140d2c858e8187825696ab",
84                 },
85         }
86
87         for _, c := range cases {
88                 outputID, err := ComputeOutputID(c.sc)
89                 if err != nil {
90                         t.Fatal(err)
91                 }
92
93                 if c.wantOutputID != outputID.String() {
94                         t.Errorf("test compute output id fail, got:%s, want:%s", outputID.String(), c.wantOutputID)
95                 }
96         }
97 }