From b9b4d0f75f4d5844b88e7d3bc33baae6a1e6ec4d Mon Sep 17 00:00:00 2001 From: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Date: Mon, 20 May 2019 09:46:04 +0800 Subject: [PATCH] test: add read/write test for cross-chain input/output (#70) * fix read/wrire CrossChainInput * add TestSerializationCrossChainTxOutput --- protocol/bc/types/txinput.go | 2 +- protocol/bc/types/txinput_test.go | 98 ++++++++++++++++++++++++++++++-------- protocol/bc/types/txoutput_test.go | 45 ++++++++++++++++- 3 files changed, 122 insertions(+), 23 deletions(-) diff --git a/protocol/bc/types/txinput.go b/protocol/bc/types/txinput.go index c9fa0a06..7b9ba0ea 100644 --- a/protocol/bc/types/txinput.go +++ b/protocol/bc/types/txinput.go @@ -271,7 +271,7 @@ func (t *TxInput) writeInputCommitment(w io.Writer) (err error) { return inp.SpendCommitment.writeExtensibleString(w, inp.SpendCommitmentSuffix, t.AssetVersion) case *CrossChainInput: - if _, err = w.Write([]byte{SpendInputType}); err != nil { + if _, err = w.Write([]byte{CrossChainInputType}); err != nil { return err } return inp.SpendCommitment.writeExtensibleString(w, inp.SpendCommitmentSuffix, t.AssetVersion) diff --git a/protocol/bc/types/txinput_test.go b/protocol/bc/types/txinput_test.go index ec78bbef..6ff94a93 100644 --- a/protocol/bc/types/txinput_test.go +++ b/protocol/bc/types/txinput_test.go @@ -26,17 +26,17 @@ func TestSerializationSpend(t *testing.T) { "52", // spend commitment length "fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409", // source id "fe9791d71b67ee62515e08723c061b5ccb952a80d804417c8aeedf7f633c524a", // assetID - "92c30f", // amount - "03", // source position - "01", // vm version - "0c", // spend program length + "92c30f", // amount + "03", // source position + "01", // vm version + "0c", // spend program length "7370656e6450726f6772616d", // spend program - "17", // witness length - "02", // argument array length - "0a", // first argument length - "617267756d656e747331", // first argument data - "0a", // second argument length - "617267756d656e747332", // second argument data + "17", // witness length + "02", // argument array length + "0a", // first argument length + "617267756d656e747331", // first argument data + "0a", // second argument length + "617267756d656e747332", // second argument data }, "") // Test convert struct to hex @@ -66,6 +66,62 @@ func TestSerializationSpend(t *testing.T) { } } +func TestSerializationCrossIn(t *testing.T) { + arguments := [][]byte{ + []byte("arguments1"), + []byte("arguments2"), + } + crossIn := NewCrossChainInput(arguments, testutil.MustDecodeHash("fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409"), testutil.MustDecodeAsset("fe9791d71b67ee62515e08723c061b5ccb952a80d804417c8aeedf7f633c524a"), 254354, 3, []byte("crossInProgram"), []byte("whatever")) + + wantHex := strings.Join([]string{ + "01", // asset version + "56", // input commitment length + "00", // cross-chain input type flag + "54", // cross-chain input commitment length + "fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409", // source id + "fe9791d71b67ee62515e08723c061b5ccb952a80d804417c8aeedf7f633c524a", // assetID + "92c30f", // amount + "03", // source position + "01", // vm version + "0e", // spend program length + "63726f7373496e50726f6772616d", // spend program + "17", // witness length + "02", // argument array length + "0a", // first argument length + "617267756d656e747331", // first argument data + "0a", // second argument length + "617267756d656e747332", // second argument data + "08", // asset definition length + "7768617465766572", // asset definition data + }, "") + + // Test convert struct to hex + var buffer bytes.Buffer + if err := crossIn.writeTo(&buffer); err != nil { + t.Fatal(err) + } + + gotHex := hex.EncodeToString(buffer.Bytes()) + if gotHex != wantHex { + t.Errorf("serialization bytes = %s want %s", gotHex, wantHex) + } + + // Test convert hex to struct + var gotCrossIn TxInput + decodeHex, err := hex.DecodeString(wantHex) + if err != nil { + t.Fatal(err) + } + + if err := gotCrossIn.readFrom(blockchain.NewReader(decodeHex)); err != nil { + t.Fatal(err) + } + + if !testutil.DeepEqual(*crossIn, gotCrossIn) { + t.Errorf("expected marshaled/unmarshaled txinput to be:\n%sgot:\n%s", spew.Sdump(*crossIn), spew.Sdump(gotCrossIn)) + } +} + func TestSerializationUnvote(t *testing.T) { arguments := [][]byte{ []byte("arguments1"), @@ -81,18 +137,18 @@ func TestSerializationUnvote(t *testing.T) { "52", // unvote commitment length "fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409", // source id "fe9791d71b67ee62515e08723c061b5ccb952a80d804417c8aeedf7f633c524a", // assetID - "92c30f", // amount - "03", // source position - "01", // vm version - "0c", // unvote program length + "92c30f", // amount + "03", // source position + "01", // vm version + "0c", // unvote program length "7370656e6450726f6772616d", // unvote program - "9901", // witness length - "02", // argument array length - "0a", // first argument length - "617267756d656e747331", // first argument data - "0a", // second argument length - "617267756d656e747332", // second argument data - "8001", //xpub length + "9901", // witness length + "02", // argument array length + "0a", // first argument length + "617267756d656e747331", // first argument data + "0a", // second argument length + "617267756d656e747332", // second argument data + "8001", //xpub length "6166353934303036613430383337643966303238646161626236643538396466306239313338646165666164353638336535323333633236343632373932313732393461386435333265363038363362636631393636323561333566623863656566666133633039363130656239326463666236353561393437663133323639", //voter xpub }, "") diff --git a/protocol/bc/types/txoutput_test.go b/protocol/bc/types/txoutput_test.go index a6c4eba0..9dc854a8 100644 --- a/protocol/bc/types/txoutput_test.go +++ b/protocol/bc/types/txoutput_test.go @@ -13,7 +13,7 @@ import ( "github.com/vapor/testutil" ) -func TestSerializationTxOutput(t *testing.T) { +func TestSerializationIntraChainTxOutput(t *testing.T) { assetID := testutil.MustDecodeAsset("81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47") txOutput := NewIntraChainOutput(assetID, 254354, []byte("TestSerializationTxOutput")) wantHex := strings.Join([]string{ @@ -56,6 +56,49 @@ func TestSerializationTxOutput(t *testing.T) { } } +func TestSerializationCrossChainTxOutput(t *testing.T) { + assetID := testutil.MustDecodeAsset("81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47") + txOutput := NewCrossChainOutput(assetID, 254354, []byte("TestSerializationTxOutput")) + wantHex := strings.Join([]string{ + "01", // asset version + "40", // serialization length + "01", // outType + "3e", // output commitment length + "81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47", // assetID + "92c30f", // amount + "01", // version + "19", // control program length + "5465737453657269616c697a6174696f6e54784f7574707574", // control program + "00", // witness length + }, "") + + // Test convert struct to hex + var buffer bytes.Buffer + if err := txOutput.writeTo(&buffer); err != nil { + t.Fatal(err) + } + + gotHex := hex.EncodeToString(buffer.Bytes()) + if gotHex != wantHex { + t.Errorf("serialization bytes = %s want %s", gotHex, wantHex) + } + + // Test convert hex to struct + var gotTxOutput TxOutput + decodeHex, err := hex.DecodeString(wantHex) + if err != nil { + t.Fatal(err) + } + + if err := gotTxOutput.readFrom(blockchain.NewReader(decodeHex)); err != nil { + t.Fatal(err) + } + + if !testutil.DeepEqual(*txOutput, gotTxOutput) { + t.Errorf("expected marshaled/unmarshaled txoutput to be:\n%sgot:\n%s", spew.Sdump(*txOutput), spew.Sdump(gotTxOutput)) + } +} + func TestSerializationVoteTxOutput(t *testing.T) { assetID := testutil.MustDecodeAsset("81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47") voteTxOutput := NewVoteOutput(assetID, 1000, []byte("TestSerializationTxOutput"), []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269")) -- 2.11.0