7 "github.com/davecgh/go-spew/spew"
9 "github.com/vapor/consensus"
10 "github.com/vapor/protocol/bc"
11 "github.com/vapor/testutil"
14 func TestMapSpendTx(t *testing.T) {
18 NewSpendInput(nil, testutil.MustDecodeHash("fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409"), *consensus.BTMAssetID, 88, 3, []byte{1}),
21 NewTxOutput(*consensus.BTMAssetID, 80, []byte{1}),
26 NewIssuanceInput([]byte("nonce"), 254354, []byte("issuanceProgram"), [][]byte{[]byte("arguments1"), []byte("arguments2")}, []byte("assetDefinition")),
29 NewTxOutput(*consensus.BTMAssetID, 80, []byte{1}),
34 NewIssuanceInput([]byte("nonce"), 254354, []byte("issuanceProgram"), [][]byte{[]byte("arguments1"), []byte("arguments2")}, []byte("assetDefinition")),
35 NewSpendInput(nil, testutil.MustDecodeHash("db7b16ac737440d6e38559996ddabb207d7ce84fbd6f3bfd2525d234761dc863"), *consensus.BTMAssetID, 88, 3, []byte{1}),
38 NewTxOutput(*consensus.BTMAssetID, 80, []byte{1}),
39 NewTxOutput(*consensus.BTMAssetID, 80, []byte{1}),
44 for _, txData := range cases {
46 if len(tx.ResultIds) != len(txData.Outputs) {
47 t.Errorf("ResultIds contains %d item(s), expected %d", len(tx.ResultIds), len(txData.Outputs))
50 for i, oldIn := range txData.Inputs {
51 resultEntry, ok := tx.Entries[tx.InputIDs[i]]
53 t.Errorf("entryMap contains nothing for tx.InputIDs[%d] (%x)", i, tx.InputIDs[i].Bytes())
55 switch newInput := resultEntry.(type) {
57 if *newInput.Value.AssetId != oldIn.AssetID() || newInput.Value.Amount != oldIn.Amount() {
58 t.Errorf("tx.InputIDs[%d]'s asset amount is not equal after map'", i)
61 spendOut, err := tx.Output(*newInput.SpentOutputId)
65 if *spendOut.Source.Value != oldIn.AssetAmount() {
66 t.Errorf("tx.InputIDs[%d]'s asset amount is not equal after map'", i)
69 t.Errorf("unexpect input type")
73 for i, oldOut := range txData.Outputs {
74 resultEntry, ok := tx.Entries[*tx.ResultIds[i]]
76 t.Errorf("entryMap contains nothing for header.ResultIds[%d] (%x)", i, tx.ResultIds[i].Bytes())
78 newOut, ok := resultEntry.(*bc.Output)
80 t.Errorf("header.ResultIds[%d] has type %T, expected *Output", i, resultEntry)
83 if *newOut.Source.Value != oldOut.AssetAmount {
84 t.Errorf("header.ResultIds[%d].(*output).Source is %v, expected %v", i, newOut.Source.Value, oldOut.AssetAmount)
86 if newOut.ControlProgram.VmVersion != 1 {
87 t.Errorf("header.ResultIds[%d].(*output).ControlProgram.VMVersion is %d, expected 1", i, newOut.ControlProgram.VmVersion)
89 if !bytes.Equal(newOut.ControlProgram.Code, oldOut.ControlProgram) {
90 t.Errorf("header.ResultIds[%d].(*output).ControlProgram.Code is %x, expected %x", i, newOut.ControlProgram.Code, oldOut.ControlProgram)
97 func TestMapCoinbaseTx(t *testing.T) {
100 NewCoinbaseInput([]byte("TestMapCoinbaseTx")),
102 Outputs: []*TxOutput{
103 NewTxOutput(*consensus.BTMAssetID, 800000000000, []byte{1}),
106 oldOut := txData.Outputs[0]
109 t.Log(spew.Sdump(tx.Entries))
111 if len(tx.InputIDs) != 1 {
112 t.Errorf("expect to only have coinbase input id")
114 if len(tx.SpentOutputIDs) != 0 {
115 t.Errorf("coinbase tx doesn't spend any utxo")
117 if len(tx.GasInputIDs) != 1 {
118 t.Errorf("coinbase tx should have 1 gas input")
120 if len(tx.ResultIds) != 1 {
121 t.Errorf("expect to only have one output")
124 outEntry, ok := tx.Entries[*tx.ResultIds[0]]
126 t.Errorf("entryMap contains nothing for output")
128 newOut, ok := outEntry.(*bc.Output)
130 t.Errorf("header.ResultIds[0] has type %T, expected *Output", outEntry)
132 if *newOut.Source.Value != oldOut.AssetAmount {
133 t.Errorf("(*output).Source is %v, expected %v", newOut.Source.Value, oldOut.AssetAmount)
136 muxEntry, ok := tx.Entries[*newOut.Source.Ref]
138 t.Errorf("entryMap contains nothing for mux")
140 mux, ok := muxEntry.(*bc.Mux)
142 t.Errorf("muxEntry has type %T, expected *Mux", muxEntry)
144 if *mux.WitnessDestinations[0].Value != *newOut.Source.Value {
145 t.Errorf("(*Mux).Destinations is %v, expected %v", *mux.WitnessDestinations[0].Value, *newOut.Source.Value)
148 coinbaseEntry, ok := tx.Entries[tx.InputIDs[0]]
150 t.Errorf("entryMap contains nothing for coinbase input")
152 coinbase, ok := coinbaseEntry.(*bc.Coinbase)
154 t.Errorf("inputEntry has type %T, expected *Coinbase", coinbaseEntry)
156 if coinbase.WitnessDestination.Value != mux.Sources[0].Value {
157 t.Errorf("(*Coinbase).Destination is %v, expected %v", coinbase.WitnessDestination.Value, *mux.Sources[0].Value)