github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/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 TestSerializationTxOutput(t *testing.T) {
    17  	assetID := testutil.MustDecodeAsset("81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47")
    18  	txOutput := NewTxOutput(assetID, 254354, []byte("TestSerializationTxOutput"))
    19  
    20  	wantHex := strings.Join([]string{
    21  		"01", // asset version
    22  		"3e", // serialization length
    23  		"81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47", // assetID
    24  		"92c30f", // amount
    25  		"01",     // version
    26  		"19",     // control program length
    27  		"5465737453657269616c697a6174696f6e54784f7574707574", // control program
    28  		"00", // witness length
    29  	}, "")
    30  
    31  	// Test convert struct to hex
    32  	var buffer bytes.Buffer
    33  	if err := txOutput.writeTo(&buffer); err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	gotHex := hex.EncodeToString(buffer.Bytes())
    38  	if gotHex != wantHex {
    39  		t.Errorf("serialization bytes = %s want %s", gotHex, wantHex)
    40  	}
    41  
    42  	// Test convert hex to struct
    43  	var gotTxOutput TxOutput
    44  	decodeHex, err := hex.DecodeString(wantHex)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	if err := gotTxOutput.readFrom(blockchain.NewReader(decodeHex)); err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	if !testutil.DeepEqual(*txOutput, gotTxOutput) {
    54  		t.Errorf("expected marshaled/unmarshaled txoutput to be:\n%sgot:\n%s", spew.Sdump(*txOutput), spew.Sdump(gotTxOutput))
    55  	}
    56  }
    57  
    58  func TestComputeOutputID(t *testing.T) {
    59  	btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
    60  	cases := []struct {
    61  		sc           *SpendCommitment
    62  		wantOutputID string
    63  	}{
    64  		{
    65  			sc: &SpendCommitment{
    66  				AssetAmount:    bc.AssetAmount{AssetId: &btmAssetID, Amount: 1000},
    67  				SourceID:       testutil.MustDecodeHash("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"),
    68  				SourcePosition: 2,
    69  				VMVersion:      1,
    70  				ControlProgram: testutil.MustDecodeHexString("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"),
    71  			},
    72  			wantOutputID: "c9902bad769008917d14710d60391a43fe6cbd255c839045425c65f749c39d81",
    73  		},
    74  		{
    75  			sc: &SpendCommitment{
    76  				AssetAmount:    bc.AssetAmount{AssetId: &btmAssetID, Amount: 999},
    77  				SourceID:       testutil.MustDecodeHash("9e74e35362ffc73c8967aa0008da8fcbc62a21d35673fb970445b5c2972f8603"),
    78  				SourcePosition: 2,
    79  				VMVersion:      1,
    80  				ControlProgram: testutil.MustDecodeHexString("001418549d84daf53344d32563830c7cf979dc19d5c0"),
    81  			},
    82  			wantOutputID: "4d038eed93338f4dfc8603101bc70f4b8e662e69828c6dadf4207b5dfaf66275",
    83  		},
    84  	}
    85  
    86  	for _, c := range cases {
    87  		outputID, err := ComputeOutputID(c.sc)
    88  		if err != nil {
    89  			t.Fatal(err)
    90  		}
    91  
    92  		if c.wantOutputID != outputID.String() {
    93  			t.Errorf("test compute output id fail, got:%s, want:%s", outputID.String(), c.wantOutputID)
    94  		}
    95  	}
    96  }