OSDN Git Service

rename (#465)
[bytom/vapor.git] / protocol / bc / types / map_test.go
1 package types
2
3 import (
4         "bytes"
5         "testing"
6
7         "github.com/davecgh/go-spew/spew"
8
9         "github.com/bytom/vapor/consensus"
10         "github.com/bytom/vapor/protocol/bc"
11         "github.com/bytom/vapor/testutil"
12 )
13
14 func TestMapSpendTx(t *testing.T) {
15         cases := []*TxData{
16                 &TxData{
17                         Inputs: []*TxInput{
18                                 NewSpendInput(nil, testutil.MustDecodeHash("fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409"), *consensus.BTMAssetID, 88, 3, []byte{1}),
19                         },
20                         Outputs: []*TxOutput{
21                                 NewIntraChainOutput(*consensus.BTMAssetID, 80, []byte{1}),
22                         },
23                 },
24         }
25
26         for _, txData := range cases {
27                 tx := MapTx(txData)
28                 if len(tx.ResultIds) != len(txData.Outputs) {
29                         t.Errorf("ResultIds contains %d item(s), expected %d", len(tx.ResultIds), len(txData.Outputs))
30                 }
31
32                 for i, oldIn := range txData.Inputs {
33                         resultEntry, ok := tx.Entries[tx.InputIDs[i]]
34                         if !ok {
35                                 t.Errorf("entryMap contains nothing for tx.InputIDs[%d] (%x)", i, tx.InputIDs[i].Bytes())
36                         }
37                         switch newInput := resultEntry.(type) {
38                         case *bc.Spend:
39                                 spendOut, err := tx.IntraChainOutput(*newInput.SpentOutputId)
40                                 if err != nil {
41                                         t.Fatal(err)
42                                 }
43                                 if *spendOut.Source.Value != oldIn.AssetAmount() {
44                                         t.Errorf("tx.InputIDs[%d]'s asset amount is not equal after map'", i)
45                                 }
46                         default:
47                                 t.Errorf("unexpect input type")
48                         }
49                 }
50
51                 for i, oldOut := range txData.Outputs {
52                         resultEntry, ok := tx.Entries[*tx.ResultIds[i]]
53                         if !ok {
54                                 t.Errorf("entryMap contains nothing for header.ResultIds[%d] (%x)", i, tx.ResultIds[i].Bytes())
55                         }
56                         newOut, ok := resultEntry.(*bc.IntraChainOutput)
57                         if !ok {
58                                 t.Errorf("header.ResultIds[%d] has type %T, expected *Output", i, resultEntry)
59                         }
60
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())
63                         }
64                         if newOut.ControlProgram.VmVersion != 1 {
65                                 t.Errorf("header.ResultIds[%d].(*output).ControlProgram.VMVersion is %d, expected 1", i, newOut.ControlProgram.VmVersion)
66                         }
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())
69                         }
70
71                 }
72         }
73 }
74
75 func TestMapCoinbaseTx(t *testing.T) {
76         txData := &TxData{
77                 Inputs: []*TxInput{
78                         NewCoinbaseInput([]byte("TestMapCoinbaseTx")),
79                 },
80                 Outputs: []*TxOutput{
81                         NewIntraChainOutput(*consensus.BTMAssetID, 800000000000, []byte{1}),
82                 },
83         }
84         oldOut := txData.Outputs[0]
85
86         tx := MapTx(txData)
87         t.Log(spew.Sdump(tx.Entries))
88
89         if len(tx.InputIDs) != 1 {
90                 t.Errorf("expect to  only have coinbase input id")
91         }
92         if len(tx.SpentOutputIDs) != 0 {
93                 t.Errorf("coinbase tx doesn't spend any utxo")
94         }
95         if len(tx.GasInputIDs) != 1 {
96                 t.Errorf("coinbase tx should have 1 gas input")
97         }
98         if len(tx.ResultIds) != 1 {
99                 t.Errorf("expect to  only have one output")
100         }
101
102         outEntry, ok := tx.Entries[*tx.ResultIds[0]]
103         if !ok {
104                 t.Errorf("entryMap contains nothing for output")
105         }
106         newOut, ok := outEntry.(*bc.IntraChainOutput)
107         if !ok {
108                 t.Errorf("header.ResultIds[0] has type %T, expected *Output", outEntry)
109         }
110         if *newOut.Source.Value != oldOut.AssetAmount() {
111                 t.Errorf("(*output).Source is %v, expected %v", newOut.Source.Value, oldOut.AssetAmount())
112         }
113
114         muxEntry, ok := tx.Entries[*newOut.Source.Ref]
115         if !ok {
116                 t.Errorf("entryMap contains nothing for mux")
117         }
118         mux, ok := muxEntry.(*bc.Mux)
119         if !ok {
120                 t.Errorf("muxEntry has type %T, expected *Mux", muxEntry)
121         }
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)
124         }
125
126         coinbaseEntry, ok := tx.Entries[tx.InputIDs[0]]
127         if !ok {
128                 t.Errorf("entryMap contains nothing for coinbase input")
129         }
130         coinbase, ok := coinbaseEntry.(*bc.Coinbase)
131         if !ok {
132                 t.Errorf("inputEntry has type %T, expected *Coinbase", coinbaseEntry)
133         }
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)
136         }
137 }