OSDN Git Service

459c107b857e828733848f3a2f06d62d02284894
[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/vapor/consensus"
10         "github.com/vapor/protocol/bc"
11         "github.com/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                                 NewTxOutput(*consensus.BTMAssetID, 80, []byte{1}),
22                         },
23                 },
24                 &TxData{
25                         Inputs: []*TxInput{
26                                 NewIssuanceInput([]byte("nonce"), 254354, []byte("issuanceProgram"), [][]byte{[]byte("arguments1"), []byte("arguments2")}, []byte("assetDefinition")),
27                         },
28                         Outputs: []*TxOutput{
29                                 NewTxOutput(*consensus.BTMAssetID, 80, []byte{1}),
30                         },
31                 },
32                 &TxData{
33                         Inputs: []*TxInput{
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}),
36                         },
37                         Outputs: []*TxOutput{
38                                 NewTxOutput(*consensus.BTMAssetID, 80, []byte{1}),
39                                 NewTxOutput(*consensus.BTMAssetID, 80, []byte{1}),
40                         },
41                 },
42         }
43
44         for _, txData := range cases {
45                 tx := MapTx(txData)
46                 if len(tx.ResultIds) != len(txData.Outputs) {
47                         t.Errorf("ResultIds contains %d item(s), expected %d", len(tx.ResultIds), len(txData.Outputs))
48                 }
49
50                 for i, oldIn := range txData.Inputs {
51                         resultEntry, ok := tx.Entries[tx.InputIDs[i]]
52                         if !ok {
53                                 t.Errorf("entryMap contains nothing for tx.InputIDs[%d] (%x)", i, tx.InputIDs[i].Bytes())
54                         }
55                         switch newInput := resultEntry.(type) {
56                         case *bc.Issuance:
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)
59                                 }
60                         case *bc.Spend:
61                                 spendOut, err := tx.Output(*newInput.SpentOutputId)
62                                 if err != nil {
63                                         t.Fatal(err)
64                                 }
65                                 if *spendOut.Source.Value != oldIn.AssetAmount() {
66                                         t.Errorf("tx.InputIDs[%d]'s asset amount is not equal after map'", i)
67                                 }
68                         default:
69                                 t.Errorf("unexpect input type")
70                         }
71                 }
72
73                 for i, oldOut := range txData.Outputs {
74                         resultEntry, ok := tx.Entries[*tx.ResultIds[i]]
75                         if !ok {
76                                 t.Errorf("entryMap contains nothing for header.ResultIds[%d] (%x)", i, tx.ResultIds[i].Bytes())
77                         }
78                         newOut, ok := resultEntry.(*bc.Output)
79                         if !ok {
80                                 t.Errorf("header.ResultIds[%d] has type %T, expected *Output", i, resultEntry)
81                         }
82
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)
85                         }
86                         if newOut.ControlProgram.VmVersion != 1 {
87                                 t.Errorf("header.ResultIds[%d].(*output).ControlProgram.VMVersion is %d, expected 1", i, newOut.ControlProgram.VmVersion)
88                         }
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)
91                         }
92
93                 }
94         }
95 }
96
97 func TestMapCoinbaseTx(t *testing.T) {
98         txData := &TxData{
99                 Inputs: []*TxInput{
100                         NewCoinbaseInput([]byte("TestMapCoinbaseTx")),
101                 },
102                 Outputs: []*TxOutput{
103                         NewTxOutput(*consensus.BTMAssetID, 800000000000, []byte{1}),
104                 },
105         }
106         oldOut := txData.Outputs[0]
107
108         tx := MapTx(txData)
109         t.Log(spew.Sdump(tx.Entries))
110
111         if len(tx.InputIDs) != 1 {
112                 t.Errorf("expect to  only have coinbase input id")
113         }
114         if len(tx.SpentOutputIDs) != 0 {
115                 t.Errorf("coinbase tx doesn't spend any utxo")
116         }
117         if len(tx.GasInputIDs) != 1 {
118                 t.Errorf("coinbase tx should have 1 gas input")
119         }
120         if len(tx.ResultIds) != 1 {
121                 t.Errorf("expect to  only have one output")
122         }
123
124         outEntry, ok := tx.Entries[*tx.ResultIds[0]]
125         if !ok {
126                 t.Errorf("entryMap contains nothing for output")
127         }
128         newOut, ok := outEntry.(*bc.Output)
129         if !ok {
130                 t.Errorf("header.ResultIds[0] has type %T, expected *Output", outEntry)
131         }
132         if *newOut.Source.Value != oldOut.AssetAmount {
133                 t.Errorf("(*output).Source is %v, expected %v", newOut.Source.Value, oldOut.AssetAmount)
134         }
135
136         muxEntry, ok := tx.Entries[*newOut.Source.Ref]
137         if !ok {
138                 t.Errorf("entryMap contains nothing for mux")
139         }
140         mux, ok := muxEntry.(*bc.Mux)
141         if !ok {
142                 t.Errorf("muxEntry has type %T, expected *Mux", muxEntry)
143         }
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)
146         }
147
148         coinbaseEntry, ok := tx.Entries[tx.InputIDs[0]]
149         if !ok {
150                 t.Errorf("entryMap contains nothing for coinbase input")
151         }
152         coinbase, ok := coinbaseEntry.(*bc.Coinbase)
153         if !ok {
154                 t.Errorf("inputEntry has type %T, expected *Coinbase", coinbaseEntry)
155         }
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)
158         }
159 }