X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=protocol%2Fbc%2Ftypes%2Ftxinput_test.go;h=6ff94a938b6a357b34b3eb31fe8aefb8006dbc53;hp=ec78bbef533e0805bf35dcac2f9f7c3337ca1490;hb=b9b4d0f75f4d5844b88e7d3bc33baae6a1e6ec4d;hpb=6bf0548429896ef136792b04c6dc21dfe95de016 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 }, "")