github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/bc/types/txoutput_test.go (about) 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"), [][]byte{[]byte("stateData")}) 19 20 wantHex := strings.Join([]string{ 21 "01", // asset version 22 "00", // output type 23 "49", // serialization length 24 "81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47", // assetID 25 "92c30f", // amount 26 "01", // version 27 "19", // control program length 28 "5465737453657269616c697a6174696f6e54784f7574707574", // control program 29 "0109", // state data length 30 "737461746544617461", // state data 31 "00", // witness length 32 }, "") 33 34 // Test convert struct to hex 35 var buffer bytes.Buffer 36 if err := txOutput.writeTo(&buffer); err != nil { 37 t.Fatal(err) 38 } 39 40 gotHex := hex.EncodeToString(buffer.Bytes()) 41 if gotHex != wantHex { 42 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex) 43 } 44 45 // Test convert hex to struct 46 var gotTxOutput TxOutput 47 decodeHex, err := hex.DecodeString(wantHex) 48 if err != nil { 49 t.Fatal(err) 50 } 51 52 if err := gotTxOutput.readFrom(blockchain.NewReader(decodeHex)); err != nil { 53 t.Fatal(err) 54 } 55 56 if !testutil.DeepEqual(*txOutput, gotTxOutput) { 57 t.Errorf("expected marshaled/unmarshaled txoutput to be:\n%sgot:\n%s", spew.Sdump(*txOutput), spew.Sdump(gotTxOutput)) 58 } 59 } 60 61 func TestSerializationVoteOutput(t *testing.T) { 62 assetID := testutil.MustDecodeAsset("81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47") 63 voteTxOutput := NewVoteOutput(assetID, 1000, []byte("TestSerializationTxOutput"), []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"), [][]byte{[]byte("stateData")}) 64 65 wantHex := strings.Join([]string{ 66 "01", // asset version 67 "01", // outType 68 "ca01", // serialization length 69 "8001", // output xpub length 70 "6166353934303036613430383337643966303238646161626236643538396466306239313338646165666164353638336535323333633236343632373932313732393461386435333265363038363362636631393636323561333566623863656566666133633039363130656239326463666236353561393437663133323639", // xpub 71 "81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47", // assetID 72 "e807", // amount 73 "01", // version 74 "19", // control program length 75 "5465737453657269616c697a6174696f6e54784f7574707574", // control program 76 "0109", // state data length 77 "737461746544617461", // state datas 78 "00", // witness length 79 }, "") 80 81 // Test convert struct to hex 82 var buffer bytes.Buffer 83 if err := voteTxOutput.writeTo(&buffer); err != nil { 84 t.Fatal(err) 85 } 86 87 gotHex := hex.EncodeToString(buffer.Bytes()) 88 if gotHex != wantHex { 89 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex) 90 } 91 92 // Test convert hex to struct 93 var gotTxOutput TxOutput 94 decodeHex, err := hex.DecodeString(wantHex) 95 if err != nil { 96 t.Fatal(err) 97 } 98 99 if err := gotTxOutput.readFrom(blockchain.NewReader(decodeHex)); err != nil { 100 t.Fatal(err) 101 } 102 103 if !testutil.DeepEqual(*voteTxOutput, gotTxOutput) { 104 t.Errorf("expected marshaled/unmarshaled txoutput to be:\n%sgot:\n%s", spew.Sdump(*voteTxOutput), spew.Sdump(gotTxOutput)) 105 } 106 } 107 108 func TestComputeOutputID(t *testing.T) { 109 btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") 110 cases := []struct { 111 sc *SpendCommitment 112 wantOutputID string 113 }{ 114 { 115 sc: &SpendCommitment{ 116 AssetAmount: bc.AssetAmount{AssetId: &btmAssetID, Amount: 1000}, 117 SourceID: testutil.MustDecodeHash("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"), 118 SourcePosition: 2, 119 VMVersion: 1, 120 ControlProgram: testutil.MustDecodeHexString("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"), 121 }, 122 wantOutputID: "b8f8664c4179a7fae694e7cafcb250403b0e12738ed5aae4610d45082a33d44a", 123 }, 124 { 125 sc: &SpendCommitment{ 126 AssetAmount: bc.AssetAmount{AssetId: &btmAssetID, Amount: 999}, 127 SourceID: testutil.MustDecodeHash("9e74e35362ffc73c8967aa0008da8fcbc62a21d35673fb970445b5c2972f8603"), 128 SourcePosition: 2, 129 VMVersion: 1, 130 ControlProgram: testutil.MustDecodeHexString("001418549d84daf53344d32563830c7cf979dc19d5c0"), 131 }, 132 wantOutputID: "638fd48107ceb576ac7518310df01214ac75a8c667ac659ecfbc397fdf0c5a6f", 133 }, 134 } 135 136 for _, c := range cases { 137 outputID, err := ComputeOutputID(c.sc, SpendInputType, nil) 138 if err != nil { 139 t.Fatal(err) 140 } 141 142 if c.wantOutputID != outputID.String() { 143 t.Errorf("test compute output id fail, got:%s, want:%s", outputID.String(), c.wantOutputID) 144 } 145 } 146 }