OSDN Git Service

2d data for butxo state (#1921)
[bytom/bytom.git] / protocol / bc / types / map_test.go
1 package types
2
3 import (
4         "bytes"
5         "reflect"
6         "testing"
7
8         "github.com/davecgh/go-spew/spew"
9
10         "github.com/bytom/bytom/consensus"
11         "github.com/bytom/bytom/protocol/bc"
12         "github.com/bytom/bytom/testutil"
13 )
14
15 func TestMapSpendTx(t *testing.T) {
16         cases := []*TxData{
17                 &TxData{
18                         Inputs: []*TxInput{
19                                 NewSpendInput(nil, testutil.MustDecodeHash("fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409"), *consensus.BTMAssetID, 88, 3, []byte{1}, [][]byte{[]byte{2}}),
20                         },
21                         Outputs: []*TxOutput{
22                                 NewOriginalTxOutput(*consensus.BTMAssetID, 80, []byte{1}, [][]byte{[]byte{2}}),
23                         },
24                 },
25                 &TxData{
26                         Inputs: []*TxInput{
27                                 NewIssuanceInput([]byte("nonce"), 254354, []byte("issuanceProgram"), [][]byte{[]byte("arguments1"), []byte("arguments2")}, []byte("assetDefinition")),
28                         },
29                         Outputs: []*TxOutput{
30                                 NewOriginalTxOutput(*consensus.BTMAssetID, 80, []byte{1}, [][]byte{[]byte{2}}),
31                         },
32                 },
33                 &TxData{
34                         Inputs: []*TxInput{
35                                 NewIssuanceInput([]byte("nonce"), 254354, []byte("issuanceProgram"), [][]byte{[]byte("arguments1"), []byte("arguments2")}, []byte("assetDefinition")),
36                                 NewSpendInput(nil, testutil.MustDecodeHash("db7b16ac737440d6e38559996ddabb207d7ce84fbd6f3bfd2525d234761dc863"), *consensus.BTMAssetID, 88, 3, []byte{1}, [][]byte{[]byte{2}}),
37                         },
38                         Outputs: []*TxOutput{
39                                 NewOriginalTxOutput(*consensus.BTMAssetID, 80, []byte{1}, [][]byte{[]byte{2}}),
40                                 NewOriginalTxOutput(*consensus.BTMAssetID, 80, []byte{1}, [][]byte{[]byte{2}}),
41                         },
42                 },
43         }
44
45         for _, txData := range cases {
46                 tx := MapTx(txData)
47                 if len(tx.ResultIds) != len(txData.Outputs) {
48                         t.Errorf("ResultIds contains %d item(s), expected %d", len(tx.ResultIds), len(txData.Outputs))
49                 }
50
51                 for i, oldIn := range txData.Inputs {
52                         resultEntry, ok := tx.Entries[tx.InputIDs[i]]
53                         if !ok {
54                                 t.Errorf("entryMap contains nothing for tx.InputIDs[%d] (%x)", i, tx.InputIDs[i].Bytes())
55                         }
56                         switch newInput := resultEntry.(type) {
57                         case *bc.Issuance:
58                                 if *newInput.Value.AssetId != oldIn.AssetID() || newInput.Value.Amount != oldIn.Amount() {
59                                         t.Errorf("tx.InputIDs[%d]'s asset amount is not equal after map'", i)
60                                 }
61                         case *bc.Spend:
62                                 spendOut, err := tx.Output(*newInput.SpentOutputId)
63                                 if err != nil {
64                                         t.Fatal(err)
65                                 }
66                                 if *spendOut.Source.Value != oldIn.AssetAmount() {
67                                         t.Errorf("tx.InputIDs[%d]'s asset amount is not equal after map'", i)
68                                 }
69                         default:
70                                 t.Errorf("unexpect input type")
71                         }
72                 }
73
74                 for i, oldOut := range txData.Outputs {
75                         resultEntry, ok := tx.Entries[*tx.ResultIds[i]]
76                         if !ok {
77                                 t.Errorf("entryMap contains nothing for header.ResultIds[%d] (%x)", i, tx.ResultIds[i].Bytes())
78                         }
79                         newOut, ok := resultEntry.(*bc.Output)
80                         if !ok {
81                                 t.Errorf("header.ResultIds[%d] has type %T, expected *Output", i, resultEntry)
82                         }
83
84                         if *newOut.Source.Value != oldOut.AssetAmount {
85                                 t.Errorf("header.ResultIds[%d].(*output).Source is %v, expected %v", i, newOut.Source.Value, oldOut.AssetAmount)
86                         }
87                         if newOut.ControlProgram.VmVersion != 1 {
88                                 t.Errorf("header.ResultIds[%d].(*output).ControlProgram.VMVersion is %d, expected 1", i, newOut.ControlProgram.VmVersion)
89                         }
90                         if !bytes.Equal(newOut.ControlProgram.Code, oldOut.ControlProgram) {
91                                 t.Errorf("header.ResultIds[%d].(*output).ControlProgram.Code is %x, expected %x", i, newOut.ControlProgram.Code, oldOut.ControlProgram)
92                         }
93                         if !reflect.DeepEqual(newOut.StateData.StateData, oldOut.StateData) {
94                                 t.Errorf("header.ResultIds[%d].(*output).StateData.StateData is %x, expected %x", i, newOut.StateData.StateData, oldOut.StateData)
95                         }
96
97                 }
98         }
99 }
100
101 func TestMapCoinbaseTx(t *testing.T) {
102         txData := &TxData{
103                 Inputs: []*TxInput{
104                         NewCoinbaseInput([]byte("TestMapCoinbaseTx")),
105                 },
106                 Outputs: []*TxOutput{
107                         NewOriginalTxOutput(*consensus.BTMAssetID, 800000000000, []byte{1}, [][]byte{[]byte{2}}),
108                 },
109         }
110         oldOut := txData.Outputs[0]
111
112         tx := MapTx(txData)
113         t.Log(spew.Sdump(tx.Entries))
114
115         if len(tx.InputIDs) != 1 {
116                 t.Errorf("expect to  only have coinbase input id")
117         }
118         if len(tx.SpentOutputIDs) != 0 {
119                 t.Errorf("coinbase tx doesn't spend any utxo")
120         }
121         if len(tx.GasInputIDs) != 1 {
122                 t.Errorf("coinbase tx should have 1 gas input")
123         }
124         if len(tx.ResultIds) != 1 {
125                 t.Errorf("expect to  only have one output")
126         }
127
128         outEntry, ok := tx.Entries[*tx.ResultIds[0]]
129         if !ok {
130                 t.Errorf("entryMap contains nothing for output")
131         }
132         newOut, ok := outEntry.(*bc.Output)
133         if !ok {
134                 t.Errorf("header.ResultIds[0] has type %T, expected *Output", outEntry)
135         }
136         if *newOut.Source.Value != oldOut.AssetAmount {
137                 t.Errorf("(*output).Source is %v, expected %v", newOut.Source.Value, oldOut.AssetAmount)
138         }
139
140         muxEntry, ok := tx.Entries[*newOut.Source.Ref]
141         if !ok {
142                 t.Errorf("entryMap contains nothing for mux")
143         }
144         mux, ok := muxEntry.(*bc.Mux)
145         if !ok {
146                 t.Errorf("muxEntry has type %T, expected *Mux", muxEntry)
147         }
148         if *mux.WitnessDestinations[0].Value != *newOut.Source.Value {
149                 t.Errorf("(*Mux).Destinations is %v, expected %v", *mux.WitnessDestinations[0].Value, *newOut.Source.Value)
150         }
151
152         coinbaseEntry, ok := tx.Entries[tx.InputIDs[0]]
153         if !ok {
154                 t.Errorf("entryMap contains nothing for coinbase input")
155         }
156         coinbase, ok := coinbaseEntry.(*bc.Coinbase)
157         if !ok {
158                 t.Errorf("inputEntry has type %T, expected *Coinbase", coinbaseEntry)
159         }
160         if coinbase.WitnessDestination.Value != mux.Sources[0].Value {
161                 t.Errorf("(*Coinbase).Destination is %v, expected %v", coinbase.WitnessDestination.Value, *mux.Sources[0].Value)
162         }
163 }