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 NewIntraChainOutput(*consensus.BTMAssetID, 80, []byte{1}),
26 for _, txData := range cases {
28 if len(tx.ResultIds) != len(txData.Outputs) {
29 t.Errorf("ResultIds contains %d item(s), expected %d", len(tx.ResultIds), len(txData.Outputs))
32 for i, oldIn := range txData.Inputs {
33 resultEntry, ok := tx.Entries[tx.InputIDs[i]]
35 t.Errorf("entryMap contains nothing for tx.InputIDs[%d] (%x)", i, tx.InputIDs[i].Bytes())
37 switch newInput := resultEntry.(type) {
39 spendOut, err := tx.IntraChainOutput(*newInput.SpentOutputId)
43 if *spendOut.Source.Value != oldIn.AssetAmount() {
44 t.Errorf("tx.InputIDs[%d]'s asset amount is not equal after map'", i)
47 t.Errorf("unexpect input type")
51 for i, oldOut := range txData.Outputs {
52 resultEntry, ok := tx.Entries[*tx.ResultIds[i]]
54 t.Errorf("entryMap contains nothing for header.ResultIds[%d] (%x)", i, tx.ResultIds[i].Bytes())
56 newOut, ok := resultEntry.(*bc.IntraChainOutput)
58 t.Errorf("header.ResultIds[%d] has type %T, expected *Output", i, resultEntry)
61 if *newOut.Source.Value != oldOut.AssetAmount() {
62 t.Errorf("header.ResultIds[%d].(*output).Source is %v, expected %v", i, newOut.Source.Value, oldOut.AssetAmount())
64 if newOut.ControlProgram.VmVersion != 1 {
65 t.Errorf("header.ResultIds[%d].(*output).ControlProgram.VMVersion is %d, expected 1", i, newOut.ControlProgram.VmVersion)
67 if !bytes.Equal(newOut.ControlProgram.Code, oldOut.ControlProgram()) {
68 t.Errorf("header.ResultIds[%d].(*output).ControlProgram.Code is %x, expected %x", i, newOut.ControlProgram.Code, oldOut.ControlProgram())
75 func TestMapCoinbaseTx(t *testing.T) {
78 NewCoinbaseInput([]byte("TestMapCoinbaseTx")),
81 NewIntraChainOutput(*consensus.BTMAssetID, 800000000000, []byte{1}),
84 oldOut := txData.Outputs[0]
87 t.Log(spew.Sdump(tx.Entries))
89 if len(tx.InputIDs) != 1 {
90 t.Errorf("expect to only have coinbase input id")
92 if len(tx.SpentOutputIDs) != 0 {
93 t.Errorf("coinbase tx doesn't spend any utxo")
95 if len(tx.GasInputIDs) != 1 {
96 t.Errorf("coinbase tx should have 1 gas input")
98 if len(tx.ResultIds) != 1 {
99 t.Errorf("expect to only have one output")
102 outEntry, ok := tx.Entries[*tx.ResultIds[0]]
104 t.Errorf("entryMap contains nothing for output")
106 newOut, ok := outEntry.(*bc.IntraChainOutput)
108 t.Errorf("header.ResultIds[0] has type %T, expected *Output", outEntry)
110 if *newOut.Source.Value != oldOut.AssetAmount() {
111 t.Errorf("(*output).Source is %v, expected %v", newOut.Source.Value, oldOut.AssetAmount())
114 muxEntry, ok := tx.Entries[*newOut.Source.Ref]
116 t.Errorf("entryMap contains nothing for mux")
118 mux, ok := muxEntry.(*bc.Mux)
120 t.Errorf("muxEntry has type %T, expected *Mux", muxEntry)
122 if *mux.WitnessDestinations[0].Value != *newOut.Source.Value {
123 t.Errorf("(*Mux).Destinations is %v, expected %v", *mux.WitnessDestinations[0].Value, *newOut.Source.Value)
126 coinbaseEntry, ok := tx.Entries[tx.InputIDs[0]]
128 t.Errorf("entryMap contains nothing for coinbase input")
130 coinbase, ok := coinbaseEntry.(*bc.Coinbase)
132 t.Errorf("inputEntry has type %T, expected *Coinbase", coinbaseEntry)
134 if coinbase.WitnessDestination.Value != mux.Sources[0].Value {
135 t.Errorf("(*Coinbase).Destination is %v, expected %v", coinbase.WitnessDestination.Value, *mux.Sources[0].Value)