OSDN Git Service

update
[bytom/vapor.git] / wallet / utxo_test.go
1 package wallet
2
3 import (
4         "encoding/hex"
5         "encoding/json"
6         "fmt"
7         "os"
8         "sort"
9         "testing"
10
11         "github.com/vapor/account"
12         "github.com/vapor/consensus"
13         dbm "github.com/vapor/database/leveldb"
14         "github.com/vapor/protocol/bc"
15         "github.com/vapor/protocol/bc/types"
16         "github.com/vapor/test/mock"
17         "github.com/vapor/testutil"
18 )
19
20 func TestGetAccountUtxos(t *testing.T) {
21         testDB := dbm.NewDB("testdb", "leveldb", "temp")
22         testStore := mock.NewMockWalletStore(testDB)
23         defer func() {
24                 testDB.Close()
25                 os.RemoveAll("temp")
26         }()
27
28         cases := []struct {
29                 dbUtxos          map[string]*account.UTXO
30                 unconfirmedUtxos []*account.UTXO
31                 id               string
32                 unconfirmed      bool
33                 isSmartContract  bool
34                 wantUtxos        []*account.UTXO
35         }{
36                 {
37                         dbUtxos:         map[string]*account.UTXO{},
38                         id:              "",
39                         unconfirmed:     false,
40                         isSmartContract: false,
41                         wantUtxos:       []*account.UTXO{},
42                 },
43                 {
44                         dbUtxos: map[string]*account.UTXO{
45                                 string(mock.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
46                                         OutputID: bc.Hash{V0: 1},
47                                 },
48                                 string(mock.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
49                                         OutputID: bc.Hash{V0: 2},
50                                 },
51                                 string(mock.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
52                                         OutputID: bc.Hash{V0: 3},
53                                 },
54                                 string(mock.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
55                                         OutputID: bc.Hash{V0: 4},
56                                 },
57                         },
58                         unconfirmedUtxos: []*account.UTXO{},
59                         id:               "",
60                         isSmartContract:  false,
61                         wantUtxos: []*account.UTXO{
62                                 &account.UTXO{OutputID: bc.Hash{V0: 1}},
63                                 &account.UTXO{OutputID: bc.Hash{V0: 2}},
64                                 &account.UTXO{OutputID: bc.Hash{V0: 3}},
65                         },
66                 },
67                 {
68                         dbUtxos: map[string]*account.UTXO{
69                                 string(mock.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
70                                         OutputID: bc.Hash{V0: 1},
71                                 },
72                                 string(mock.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
73                                         OutputID: bc.Hash{V0: 2},
74                                 },
75                                 string(mock.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
76                                         OutputID: bc.Hash{V0: 3},
77                                 },
78                                 string(mock.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
79                                         OutputID: bc.Hash{V0: 4},
80                                 },
81                         },
82                         unconfirmedUtxos: []*account.UTXO{
83                                 &account.UTXO{
84                                         OutputID:       bc.Hash{V0: 5},
85                                         ControlProgram: []byte("smart contract"),
86                                 },
87                         },
88                         id:              "",
89                         unconfirmed:     false,
90                         isSmartContract: true,
91                         wantUtxos: []*account.UTXO{
92                                 &account.UTXO{OutputID: bc.Hash{V0: 4}},
93                         },
94                 },
95                 {
96                         dbUtxos: map[string]*account.UTXO{
97                                 string(mock.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
98                                         OutputID: bc.Hash{V0: 1},
99                                 },
100                                 string(mock.StandardUTXOKey(bc.Hash{V0: 1, V1: 2})): &account.UTXO{
101                                         OutputID: bc.Hash{V0: 1, V1: 2},
102                                 },
103                                 string(mock.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
104                                         OutputID: bc.Hash{V0: 2},
105                                 },
106                                 string(mock.StandardUTXOKey(bc.Hash{V0: 2, V1: 2})): &account.UTXO{
107                                         OutputID: bc.Hash{V0: 2, V1: 2},
108                                 },
109                         },
110                         unconfirmedUtxos: []*account.UTXO{
111                                 &account.UTXO{
112                                         OutputID:       bc.Hash{V0: 6},
113                                         ControlProgram: []byte{0x51},
114                                 },
115                         },
116                         id:              "0000000000000002",
117                         unconfirmed:     false,
118                         isSmartContract: false,
119                         wantUtxos: []*account.UTXO{
120                                 &account.UTXO{OutputID: bc.Hash{V0: 2}},
121                                 &account.UTXO{OutputID: bc.Hash{V0: 2, V1: 2}},
122                         },
123                 },
124                 {
125                         dbUtxos: map[string]*account.UTXO{
126                                 string(mock.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
127                                         OutputID: bc.Hash{V0: 3},
128                                 },
129                                 string(mock.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
130                                         OutputID: bc.Hash{V0: 4},
131                                 },
132                         },
133                         unconfirmedUtxos: []*account.UTXO{
134                                 &account.UTXO{
135                                         OutputID:       bc.Hash{V0: 5},
136                                         ControlProgram: []byte("smart contract"),
137                                 },
138                                 &account.UTXO{
139                                         OutputID:       bc.Hash{V0: 6},
140                                         ControlProgram: []byte{0x51},
141                                 },
142                         },
143                         id:              "",
144                         unconfirmed:     true,
145                         isSmartContract: true,
146                         wantUtxos: []*account.UTXO{
147                                 &account.UTXO{
148                                         OutputID:       bc.Hash{V0: 5},
149                                         ControlProgram: []byte("smart contract"),
150                                 },
151                                 &account.UTXO{
152                                         OutputID: bc.Hash{V0: 4},
153                                 },
154                         },
155                 },
156                 {
157                         dbUtxos: map[string]*account.UTXO{
158                                 string(mock.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
159                                         OutputID: bc.Hash{V0: 3},
160                                 },
161                                 string(mock.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
162                                         OutputID: bc.Hash{V0: 4},
163                                 },
164                         },
165                         unconfirmedUtxos: []*account.UTXO{
166                                 &account.UTXO{
167                                         OutputID:       bc.Hash{V0: 5},
168                                         ControlProgram: []byte("smart contract"),
169                                 },
170                                 &account.UTXO{
171                                         OutputID:       bc.Hash{V0: 6},
172                                         ControlProgram: []byte{0x51},
173                                 },
174                         },
175                         id:              "",
176                         unconfirmed:     true,
177                         isSmartContract: false,
178                         wantUtxos: []*account.UTXO{
179                                 &account.UTXO{
180                                         OutputID:       bc.Hash{V0: 6},
181                                         ControlProgram: []byte{0x51},
182                                 },
183                                 &account.UTXO{
184                                         OutputID: bc.Hash{V0: 3},
185                                 },
186                         },
187                 },
188         }
189
190         w := &Wallet{store: testStore}
191         for i, c := range cases {
192                 for k, u := range c.dbUtxos {
193                         data, err := json.Marshal(u)
194                         if err != nil {
195                                 t.Error(err)
196                         }
197                         testDB.Set([]byte(k), data)
198                 }
199
200                 acccountStore := mock.NewMockAccountStore(testDB)
201                 w.AccountMgr = account.NewManager(acccountStore, nil)
202                 w.AccountMgr.AddUnconfirmedUtxo(c.unconfirmedUtxos)
203                 gotUtxos := w.GetAccountUtxos("", c.id, c.unconfirmed, c.isSmartContract, false)
204                 if !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
205                         t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos)
206                 }
207
208                 for k := range c.dbUtxos {
209                         testDB.Delete([]byte(k))
210                 }
211         }
212 }
213
214 func TestFilterAccountUtxo(t *testing.T) {
215         testDB := dbm.NewDB("testdb", "leveldb", "temp")
216         testStore := mock.NewMockWalletStore(testDB)
217         defer func() {
218                 testDB.Close()
219                 os.RemoveAll("temp")
220         }()
221
222         cases := []struct {
223                 dbPrograms map[string]*account.CtrlProgram
224                 input      []*account.UTXO
225                 wantUtxos  []*account.UTXO
226         }{
227                 {
228                         dbPrograms: map[string]*account.CtrlProgram{},
229                         input:      []*account.UTXO{},
230                         wantUtxos:  []*account.UTXO{},
231                 },
232                 {
233                         dbPrograms: map[string]*account.CtrlProgram{
234                                 "436f6e74726163743a2a37a64a4e15a772ab43bf3f5956d0d1f353946496788e7f40d0ff1796286a6f": &account.CtrlProgram{
235                                         AccountID: "testAccount",
236                                         Address:   "testAddress",
237                                         KeyIndex:  53,
238                                         Change:    true,
239                                 },
240                         },
241                         input: []*account.UTXO{
242                                 &account.UTXO{
243                                         ControlProgram: []byte{0x00, 0x14, 0x62, 0x50, 0x18, 0xb6, 0x85, 0x77, 0xba, 0x9b, 0x26, 0x19, 0xc8, 0x1d, 0x2e, 0x96, 0xba, 0x22, 0xbe, 0x77, 0x77, 0xd7},
244                                         AssetID:        bc.AssetID{V0: 1},
245                                         Amount:         3,
246                                 },
247                                 &account.UTXO{
248                                         ControlProgram: []byte{0x91},
249                                         AssetID:        bc.AssetID{V0: 1},
250                                         Amount:         4,
251                                 },
252                         },
253                         wantUtxos: []*account.UTXO{
254                                 &account.UTXO{
255                                         ControlProgram:      []byte{0x00, 0x14, 0x62, 0x50, 0x18, 0xb6, 0x85, 0x77, 0xba, 0x9b, 0x26, 0x19, 0xc8, 0x1d, 0x2e, 0x96, 0xba, 0x22, 0xbe, 0x77, 0x77, 0xd7},
256                                         AssetID:             bc.AssetID{V0: 1},
257                                         Amount:              3,
258                                         AccountID:           "testAccount",
259                                         Address:             "testAddress",
260                                         ControlProgramIndex: 53,
261                                         Change:              true,
262                                 },
263                                 &account.UTXO{
264                                         ControlProgram: []byte{0x91},
265                                         AssetID:        bc.AssetID{V0: 1},
266                                         Amount:         4,
267                                 },
268                         },
269                 },
270                 {
271                         dbPrograms: map[string]*account.CtrlProgram{},
272                         input: []*account.UTXO{
273                                 &account.UTXO{
274                                         ControlProgram: []byte{0x00, 0x14, 0x62, 0x50, 0x18, 0xb6, 0x85, 0x77, 0xba, 0x9b, 0x26, 0x19, 0xc8, 0x1d, 0x2e, 0x96, 0xba, 0x22, 0xbe, 0x77, 0x77, 0xd7},
275                                         AssetID:        bc.AssetID{V0: 1},
276                                         Amount:         3,
277                                 },
278                                 &account.UTXO{
279                                         ControlProgram: []byte{0x91},
280                                         AssetID:        bc.AssetID{V0: 1},
281                                         Amount:         3,
282                                 },
283                         },
284                         wantUtxos: []*account.UTXO{
285                                 &account.UTXO{
286                                         ControlProgram: []byte{0x91},
287                                         AssetID:        bc.AssetID{V0: 1},
288                                         Amount:         3,
289                                 },
290                         },
291                 },
292                 {
293                         dbPrograms: map[string]*account.CtrlProgram{
294                                 "436f6e74726163743a2a37a64a4e15a772ab43bf3f5956d0d1f353946496788e7f40d0ff1796286a6f": &account.CtrlProgram{
295                                         AccountID: "testAccount",
296                                         Address:   "testAddress",
297                                         KeyIndex:  53,
298                                         Change:    true,
299                                 },
300                                 "436f6e74726163743adb4d86262c12ba70d50b3ca3ae102d5682436243bd1e8c79569603f75675036a": &account.CtrlProgram{
301                                         AccountID: "testAccount2",
302                                         Address:   "testAddress2",
303                                         KeyIndex:  72,
304                                         Change:    false,
305                                 },
306                         },
307                         input: []*account.UTXO{
308                                 &account.UTXO{
309                                         ControlProgram: []byte{0x00, 0x14, 0x62, 0x50, 0x18, 0xb6, 0x85, 0x77, 0xba, 0x9b, 0x26, 0x19, 0xc8, 0x1d, 0x2e, 0x96, 0xba, 0x22, 0xbe, 0x77, 0x77, 0xd7},
310                                         AssetID:        bc.AssetID{V0: 1},
311                                         Amount:         3,
312                                 },
313                                 &account.UTXO{
314                                         ControlProgram: []byte{0x00, 0x14, 0x62, 0x50, 0x18, 0xb6, 0x85, 0x77, 0xba, 0x9b, 0x26, 0x19, 0xc8, 0x1d, 0x2e, 0x96, 0xba, 0x22, 0xbe, 0x77, 0x77, 0xd7},
315                                         AssetID:        bc.AssetID{V0: 1},
316                                         Amount:         5,
317                                 },
318                                 &account.UTXO{
319                                         ControlProgram: []byte{0x00, 0x14, 0xc6, 0xbf, 0x22, 0x19, 0x64, 0x2a, 0xc5, 0x9e, 0x5b, 0xe4, 0xeb, 0xdf, 0x5b, 0x22, 0x49, 0x56, 0xa7, 0x98, 0xa4, 0xdf},
320                                         AssetID:        bc.AssetID{V0: 1},
321                                         Amount:         7,
322                                 },
323                         },
324                         wantUtxos: []*account.UTXO{
325                                 &account.UTXO{
326                                         ControlProgram:      []byte{0x00, 0x14, 0x62, 0x50, 0x18, 0xb6, 0x85, 0x77, 0xba, 0x9b, 0x26, 0x19, 0xc8, 0x1d, 0x2e, 0x96, 0xba, 0x22, 0xbe, 0x77, 0x77, 0xd7},
327                                         AssetID:             bc.AssetID{V0: 1},
328                                         Amount:              3,
329                                         AccountID:           "testAccount",
330                                         Address:             "testAddress",
331                                         ControlProgramIndex: 53,
332                                         Change:              true,
333                                 },
334                                 &account.UTXO{
335                                         ControlProgram:      []byte{0x00, 0x14, 0x62, 0x50, 0x18, 0xb6, 0x85, 0x77, 0xba, 0x9b, 0x26, 0x19, 0xc8, 0x1d, 0x2e, 0x96, 0xba, 0x22, 0xbe, 0x77, 0x77, 0xd7},
336                                         AssetID:             bc.AssetID{V0: 1},
337                                         Amount:              5,
338                                         AccountID:           "testAccount",
339                                         Address:             "testAddress",
340                                         ControlProgramIndex: 53,
341                                         Change:              true,
342                                 },
343                                 &account.UTXO{
344                                         ControlProgram:      []byte{0x00, 0x14, 0xc6, 0xbf, 0x22, 0x19, 0x64, 0x2a, 0xc5, 0x9e, 0x5b, 0xe4, 0xeb, 0xdf, 0x5b, 0x22, 0x49, 0x56, 0xa7, 0x98, 0xa4, 0xdf},
345                                         AssetID:             bc.AssetID{V0: 1},
346                                         Amount:              7,
347                                         AccountID:           "testAccount2",
348                                         Address:             "testAddress2",
349                                         ControlProgramIndex: 72,
350                                         Change:              false,
351                                 },
352                         },
353                 },
354         }
355
356         w := &Wallet{store: testStore}
357         for i, c := range cases {
358                 for s, p := range c.dbPrograms {
359                         data, err := json.Marshal(p)
360                         if err != nil {
361                                 t.Error(err)
362                         }
363                         key, err := hex.DecodeString(s)
364                         if err != nil {
365                                 t.Error(err)
366                         }
367                         testDB.Set(key, data)
368                 }
369
370                 gotUtxos := w.filterAccountUtxo(c.input)
371                 sort.Slice(gotUtxos[:], func(i, j int) bool {
372                         return gotUtxos[i].Amount < gotUtxos[j].Amount
373                 })
374
375                 if !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
376                         t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos)
377                 }
378                 for s := range c.dbPrograms {
379                         key, err := hex.DecodeString(s)
380                         if err != nil {
381                                 t.Error(err)
382                         }
383                         testDB.Delete(key)
384                 }
385         }
386 }
387
388 func TestTxInToUtxos(t *testing.T) {
389         cases := []struct {
390                 tx         *types.Tx
391                 statusFail bool
392                 wantUtxos  []*account.UTXO
393         }{
394                 {
395                         tx: types.NewTx(types.TxData{
396                                 Inputs: []*types.TxInput{
397                                         types.NewCoinbaseInput([]byte{0x51}),
398                                 },
399                                 Outputs: []*types.TxOutput{
400                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 41250000000, []byte{0x51}),
401                                 },
402                         }),
403                         statusFail: false,
404                         wantUtxos:  []*account.UTXO{},
405                 },
406                 {
407                         tx: types.NewTx(types.TxData{
408                                 Inputs: []*types.TxInput{
409                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 1, 1, []byte{0x51}),
410                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 2}, bc.AssetID{V0: 1}, 3, 2, []byte{0x52}),
411                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 3}, *consensus.BTMAssetID, 5, 3, []byte{0x53}),
412                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 4}, *consensus.BTMAssetID, 7, 4, []byte{0x54}),
413                                 },
414                                 Outputs: []*types.TxOutput{
415                                         types.NewIntraChainOutput(bc.AssetID{V0: 1}, 4, []byte{0x51}),
416                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 12, []byte{0x53}),
417                                 },
418                         }),
419                         statusFail: false,
420                         wantUtxos: []*account.UTXO{
421                                 &account.UTXO{
422                                         OutputID:       bc.NewHash([32]byte{0x62, 0xf2, 0xc4, 0xa0, 0x9b, 0x47, 0xd1, 0x53, 0x58, 0xe7, 0x8c, 0x49, 0x36, 0x75, 0x02, 0xc1, 0x63, 0x46, 0x51, 0xc4, 0x0f, 0xef, 0x63, 0xe2, 0x7d, 0xe4, 0x3c, 0xb3, 0x2c, 0xfe, 0x97, 0xa2}),
423                                         AssetID:        bc.AssetID{V0: 1},
424                                         Amount:         1,
425                                         ControlProgram: []byte{0x51},
426                                         SourceID:       bc.Hash{V0: 1},
427                                         SourcePos:      1,
428                                 },
429                                 &account.UTXO{
430                                         OutputID:       bc.NewHash([32]byte{0x99, 0x30, 0x35, 0x15, 0x9b, 0x0b, 0xcc, 0xdf, 0xbd, 0x15, 0x49, 0xb5, 0x2b, 0x4c, 0xc8, 0x71, 0x20, 0xe7, 0x2f, 0x77, 0x87, 0xcd, 0x88, 0x92, 0xba, 0xd8, 0x97, 0xfa, 0x4a, 0x2a, 0x1a, 0x10}),
431                                         AssetID:        bc.AssetID{V0: 1},
432                                         Amount:         3,
433                                         ControlProgram: []byte{0x52},
434                                         SourceID:       bc.Hash{V0: 2},
435                                         SourcePos:      2,
436                                 },
437                                 &account.UTXO{
438                                         OutputID:       bc.NewHash([32]byte{0xe5, 0x21, 0x0a, 0x9f, 0x17, 0xa2, 0x3a, 0xcf, 0x47, 0x57, 0xf2, 0x16, 0x12, 0x9d, 0xd8, 0xea, 0x7a, 0x9f, 0x5a, 0x14, 0xa8, 0xd6, 0x32, 0x6f, 0xe8, 0xa8, 0x8e, 0xb7, 0xf4, 0xb4, 0xfb, 0xbd}),
439                                         AssetID:        *consensus.BTMAssetID,
440                                         Amount:         5,
441                                         ControlProgram: []byte{0x53},
442                                         SourceID:       bc.Hash{V0: 3},
443                                         SourcePos:      3,
444                                 },
445                                 &account.UTXO{
446                                         OutputID:       bc.NewHash([32]byte{0x57, 0x65, 0x8d, 0x41, 0xed, 0xb7, 0x49, 0xd5, 0x1c, 0xf5, 0x95, 0x93, 0x16, 0x57, 0xf8, 0x66, 0x54, 0x1b, 0xb3, 0x45, 0x84, 0x19, 0x73, 0x2f, 0xb3, 0x3e, 0x44, 0x7c, 0x97, 0x33, 0x77, 0x12}),
447                                         AssetID:        *consensus.BTMAssetID,
448                                         Amount:         7,
449                                         ControlProgram: []byte{0x54},
450                                         SourceID:       bc.Hash{V0: 4},
451                                         SourcePos:      4,
452                                 },
453                         },
454                 },
455                 {
456                         tx: types.NewTx(types.TxData{
457                                 Inputs: []*types.TxInput{
458                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 1, 1, []byte{0x51}),
459                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 2}, bc.AssetID{V0: 1}, 3, 2, []byte{0x52}),
460                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 3}, *consensus.BTMAssetID, 5, 3, []byte{0x53}),
461                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 4}, *consensus.BTMAssetID, 7, 4, []byte{0x54}),
462                                 },
463                                 Outputs: []*types.TxOutput{
464                                         types.NewIntraChainOutput(bc.AssetID{V0: 1}, 4, []byte{0x51}),
465                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 12, []byte{0x53}),
466                                 },
467                         }),
468                         statusFail: true,
469                         wantUtxos: []*account.UTXO{
470                                 &account.UTXO{
471                                         OutputID:       bc.NewHash([32]byte{0xe5, 0x21, 0x0a, 0x9f, 0x17, 0xa2, 0x3a, 0xcf, 0x47, 0x57, 0xf2, 0x16, 0x12, 0x9d, 0xd8, 0xea, 0x7a, 0x9f, 0x5a, 0x14, 0xa8, 0xd6, 0x32, 0x6f, 0xe8, 0xa8, 0x8e, 0xb7, 0xf4, 0xb4, 0xfb, 0xbd}),
472                                         AssetID:        *consensus.BTMAssetID,
473                                         Amount:         5,
474                                         ControlProgram: []byte{0x53},
475                                         SourceID:       bc.Hash{V0: 3},
476                                         SourcePos:      3,
477                                 },
478                                 &account.UTXO{
479                                         OutputID:       bc.NewHash([32]byte{0x57, 0x65, 0x8d, 0x41, 0xed, 0xb7, 0x49, 0xd5, 0x1c, 0xf5, 0x95, 0x93, 0x16, 0x57, 0xf8, 0x66, 0x54, 0x1b, 0xb3, 0x45, 0x84, 0x19, 0x73, 0x2f, 0xb3, 0x3e, 0x44, 0x7c, 0x97, 0x33, 0x77, 0x12}),
480                                         AssetID:        *consensus.BTMAssetID,
481                                         Amount:         7,
482                                         ControlProgram: []byte{0x54},
483                                         SourceID:       bc.Hash{V0: 4},
484                                         SourcePos:      4,
485                                 },
486                         },
487                 },
488                 {
489                         tx: types.NewTx(types.TxData{
490                                 Inputs: []*types.TxInput{
491                                         types.NewVetoInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 1, 1, []byte{0x51}, []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269")),
492                                 },
493                                 Outputs: []*types.TxOutput{
494                                         types.NewIntraChainOutput(bc.AssetID{V0: 1}, 1, []byte{0x51}),
495                                 },
496                         }),
497                         statusFail: false,
498                         wantUtxos: []*account.UTXO{
499                                 &account.UTXO{
500                                         OutputID:       bc.NewHash([32]byte{0x7c, 0x75, 0x7f, 0x03, 0x67, 0x9b, 0xc2, 0x8f, 0x8f, 0xbd, 0x04, 0x25, 0x72, 0x42, 0x4b, 0x0b, 0x2a, 0xa4, 0x0e, 0x10, 0x0a, 0x6e, 0x99, 0x0e, 0x6d, 0x58, 0x92, 0x1d, 0xdd, 0xbe, 0xeb, 0x1a}),
501                                         AssetID:        bc.AssetID{V0: 1},
502                                         Amount:         1,
503                                         ControlProgram: []byte{0x51},
504                                         Vote:           []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"),
505                                         SourceID:       bc.Hash{V0: 1},
506                                         SourcePos:      1,
507                                 },
508                         },
509                 },
510         }
511
512         for i, c := range cases {
513                 if gotUtxos := txInToUtxos(c.tx, c.statusFail); !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
514                         for k, v := range gotUtxos {
515                                 data, _ := json.Marshal(v)
516                                 fmt.Println(k, string(data))
517                         }
518                         for k, v := range c.wantUtxos {
519                                 data, _ := json.Marshal(v)
520                                 fmt.Println(k, string(data))
521                         }
522                         t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos)
523                 }
524         }
525 }
526
527 func TestTxOutToUtxos(t *testing.T) {
528         cases := []struct {
529                 tx          *types.Tx
530                 statusFail  bool
531                 blockHeight uint64
532                 wantUtxos   []*account.UTXO
533         }{
534                 {
535                         tx: types.NewTx(types.TxData{
536                                 Inputs: []*types.TxInput{
537                                         types.NewCoinbaseInput([]byte{0x51}),
538                                 },
539                                 Outputs: []*types.TxOutput{
540                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 41250000000, []byte{0x51}),
541                                 },
542                         }),
543                         statusFail:  false,
544                         blockHeight: 98,
545                         wantUtxos: []*account.UTXO{
546                                 &account.UTXO{
547                                         OutputID:       bc.Hash{V0: 1728735075694344097, V1: 884766857607786922, V2: 12293210594955921685, V3: 11109045974561998790},
548                                         AssetID:        *consensus.BTMAssetID,
549                                         Amount:         41250000000,
550                                         ControlProgram: []byte{0x51},
551                                         SourceID:       bc.NewHash([32]byte{0xb4, 0x7e, 0x94, 0x31, 0x88, 0xfe, 0xd3, 0xe9, 0xac, 0x99, 0x7c, 0xfc, 0x99, 0x6d, 0xd7, 0x4d, 0x04, 0x10, 0x77, 0xcb, 0x1c, 0xf8, 0x95, 0x14, 0x00, 0xe3, 0x42, 0x00, 0x8d, 0x05, 0xec, 0xdc}),
552                                         SourcePos:      0,
553                                         ValidHeight:    198,
554                                 },
555                         },
556                 },
557                 {
558                         tx: types.NewTx(types.TxData{
559                                 Inputs: []*types.TxInput{
560                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 5, 1, []byte{0x51}),
561                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 2}, *consensus.BTMAssetID, 7, 1, []byte{0x51}),
562                                 },
563                                 Outputs: []*types.TxOutput{
564                                         types.NewIntraChainOutput(bc.AssetID{V0: 1}, 2, []byte{0x51}),
565                                         types.NewIntraChainOutput(bc.AssetID{V0: 1}, 3, []byte{0x52}),
566                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 2, []byte{0x53}),
567                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 5, []byte{0x54}),
568                                 },
569                         }),
570                         statusFail:  false,
571                         blockHeight: 0,
572                         wantUtxos: []*account.UTXO{
573                                 &account.UTXO{
574                                         OutputID:       bc.Hash{V0: 8675398163687045889, V1: 7549510466747714094, V2: 13693077838209211470, V3: 6878568403630757599},
575                                         AssetID:        bc.AssetID{V0: 1},
576                                         Amount:         2,
577                                         ControlProgram: []byte{0x51},
578                                         SourceID:       bc.Hash{V0: 968805671293010031, V1: 9297014342000792994, V2: 16963674611624423333, V3: 2728293460397542670},
579                                         SourcePos:      0,
580                                 },
581                                 &account.UTXO{
582                                         OutputID:       bc.Hash{V0: 10393356437681643401, V1: 233963481123580514, V2: 17312171816916184445, V3: 16199332547392196559},
583                                         AssetID:        bc.AssetID{V0: 1},
584                                         Amount:         3,
585                                         ControlProgram: []byte{0x52},
586                                         SourceID:       bc.Hash{V0: 968805671293010031, V1: 9297014342000792994, V2: 16963674611624423333, V3: 2728293460397542670},
587                                         SourcePos:      1,
588                                 },
589                                 &account.UTXO{
590                                         OutputID:       bc.Hash{V0: 7067560744282869147, V1: 8991714784298240423, V2: 2595857933262917893, V3: 11490631006811252506},
591                                         AssetID:        *consensus.BTMAssetID,
592                                         Amount:         2,
593                                         ControlProgram: []byte{0x53},
594                                         SourceID:       bc.Hash{V0: 968805671293010031, V1: 9297014342000792994, V2: 16963674611624423333, V3: 2728293460397542670},
595                                         SourcePos:      2,
596                                 },
597                                 &account.UTXO{
598                                         OutputID:       bc.Hash{V0: 15425148469684856658, V1: 11568657474526458285, V2: 11930588814405533063, V3: 5058456773104068022},
599                                         AssetID:        *consensus.BTMAssetID,
600                                         Amount:         5,
601                                         ControlProgram: []byte{0x54},
602                                         SourceID:       bc.Hash{V0: 968805671293010031, V1: 9297014342000792994, V2: 16963674611624423333, V3: 2728293460397542670},
603                                         SourcePos:      3,
604                                 },
605                         },
606                 },
607                 {
608                         tx: types.NewTx(types.TxData{
609                                 Inputs: []*types.TxInput{
610                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 5, 1, []byte{0x51}),
611                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 2}, *consensus.BTMAssetID, 7, 1, []byte{0x51}),
612                                 },
613                                 Outputs: []*types.TxOutput{
614                                         types.NewIntraChainOutput(bc.AssetID{V0: 1}, 2, []byte{0x51}),
615                                         types.NewIntraChainOutput(bc.AssetID{V0: 1}, 3, []byte{0x52}),
616                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 2, []byte{0x53}),
617                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 5, []byte{0x54}),
618                                 },
619                         }),
620                         statusFail:  true,
621                         blockHeight: 0,
622                         wantUtxos: []*account.UTXO{
623                                 &account.UTXO{
624                                         OutputID:       bc.Hash{V0: 7067560744282869147, V1: 8991714784298240423, V2: 2595857933262917893, V3: 11490631006811252506},
625                                         AssetID:        *consensus.BTMAssetID,
626                                         Amount:         2,
627                                         ControlProgram: []byte{0x53},
628                                         SourceID:       bc.Hash{V0: 968805671293010031, V1: 9297014342000792994, V2: 16963674611624423333, V3: 2728293460397542670},
629                                         SourcePos:      2,
630                                 },
631                                 &account.UTXO{
632                                         OutputID:       bc.Hash{V0: 15425148469684856658, V1: 11568657474526458285, V2: 11930588814405533063, V3: 5058456773104068022},
633                                         AssetID:        *consensus.BTMAssetID,
634                                         Amount:         5,
635                                         ControlProgram: []byte{0x54},
636                                         SourceID:       bc.Hash{V0: 968805671293010031, V1: 9297014342000792994, V2: 16963674611624423333, V3: 2728293460397542670},
637                                         SourcePos:      3,
638                                 },
639                         },
640                 },
641                 {
642                         tx: types.NewTx(types.TxData{
643                                 Inputs: []*types.TxInput{
644                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 5, 1, []byte{0x51}),
645                                         types.NewSpendInput([][]byte{}, bc.Hash{V0: 2}, *consensus.BTMAssetID, 7, 1, []byte{0x51}),
646                                 },
647                                 Outputs: []*types.TxOutput{
648                                         types.NewIntraChainOutput(bc.AssetID{V0: 1}, 2, []byte{0x51}),
649                                         types.NewCrossChainOutput(bc.AssetID{V0: 1}, 3, []byte{0x52}),
650                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 2, []byte{0x53}),
651                                         types.NewCrossChainOutput(*consensus.BTMAssetID, 5, []byte{0x54}),
652                                 },
653                         }),
654                         statusFail:  false,
655                         blockHeight: 0,
656                         wantUtxos: []*account.UTXO{
657                                 &account.UTXO{
658                                         OutputID:       bc.Hash{V0: 8675398163687045889, V1: 7549510466747714094, V2: 13693077838209211470, V3: 6878568403630757599},
659                                         AssetID:        bc.AssetID{V0: 1},
660                                         Amount:         2,
661                                         ControlProgram: []byte{0x51},
662                                         SourceID:       bc.Hash{V0: 968805671293010031, V1: 9297014342000792994, V2: 16963674611624423333, V3: 2728293460397542670},
663                                         SourcePos:      0,
664                                 },
665                                 &account.UTXO{
666                                         OutputID:       bc.Hash{V0: 7067560744282869147, V1: 8991714784298240423, V2: 2595857933262917893, V3: 11490631006811252506},
667                                         AssetID:        *consensus.BTMAssetID,
668                                         Amount:         2,
669                                         ControlProgram: []byte{0x53},
670                                         SourceID:       bc.Hash{V0: 968805671293010031, V1: 9297014342000792994, V2: 16963674611624423333, V3: 2728293460397542670},
671                                         SourcePos:      2,
672                                 },
673                         },
674                 },
675                 {
676                         tx: types.NewTx(types.TxData{
677                                 Inputs: []*types.TxInput{
678                                         types.NewCrossChainInput([][]byte{}, bc.Hash{V0: 1}, bc.AssetID{V0: 1}, 5, 1, 1, []byte("asset1"), []byte("IssuanceProgram")),
679                                         types.NewCrossChainInput([][]byte{}, bc.Hash{V0: 2}, *consensus.BTMAssetID, 7, 1, 1, []byte("assetbtm"), []byte("IssuanceProgram"))},
680                                 Outputs: []*types.TxOutput{
681                                         types.NewIntraChainOutput(bc.AssetID{V0: 1}, 2, []byte{0x51}),
682                                         types.NewIntraChainOutput(bc.AssetID{V0: 1}, 3, []byte{0x52}),
683                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 2, []byte{0x53}),
684                                         types.NewIntraChainOutput(*consensus.BTMAssetID, 5, []byte{0x54}),
685                                 },
686                         }),
687                         statusFail:  false,
688                         blockHeight: 0,
689                         wantUtxos: []*account.UTXO{
690                                 &account.UTXO{
691                                         OutputID:       bc.Hash{V0: 5429526025956869574, V1: 12188959875155232503, V2: 14722092106507294798, V3: 27876074648890075},
692                                         AssetID:        bc.AssetID{V0: 1},
693                                         Amount:         2,
694                                         ControlProgram: []byte{0x51},
695                                         SourceID:       bc.Hash{V0: 10187915247323429348, V1: 4770401581694266753, V2: 4182269187154655368, V3: 9030883832705174512},
696                                         SourcePos:      0,
697                                 },
698                                 &account.UTXO{
699                                         OutputID:       bc.Hash{V0: 18100481287404207387, V1: 3365694797435565990, V2: 8136211093499423216, V3: 12028531817690438568},
700                                         AssetID:        bc.AssetID{V0: 1},
701                                         Amount:         3,
702                                         ControlProgram: []byte{0x52},
703                                         SourceID:       bc.Hash{V0: 10187915247323429348, V1: 4770401581694266753, V2: 4182269187154655368, V3: 9030883832705174512},
704                                         SourcePos:      1,
705                                 },
706                                 &account.UTXO{
707                                         OutputID:       bc.Hash{V0: 15745816911932387102, V1: 5035893487696724781, V2: 10084725527786878517, V3: 11270352747873435606},
708                                         AssetID:        *consensus.BTMAssetID,
709                                         Amount:         2,
710                                         ControlProgram: []byte{0x53},
711                                         SourceID:       bc.Hash{V0: 10187915247323429348, V1: 4770401581694266753, V2: 4182269187154655368, V3: 9030883832705174512},
712                                         SourcePos:      2,
713                                 },
714                                 &account.UTXO{
715                                         OutputID:       bc.Hash{V0: 10165799535720725897, V1: 9618876671942765420, V2: 17982649347111502590, V3: 15837286550437859084},
716                                         AssetID:        *consensus.BTMAssetID,
717                                         Amount:         5,
718                                         ControlProgram: []byte{0x54},
719                                         SourceID:       bc.Hash{V0: 10187915247323429348, V1: 4770401581694266753, V2: 4182269187154655368, V3: 9030883832705174512},
720                                         SourcePos:      3,
721                                 },
722                         },
723                 },
724         }
725
726         for i, c := range cases {
727                 if gotUtxos := txOutToUtxos(c.tx, c.statusFail, c.blockHeight); !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
728                         t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos)
729
730                         for j, u := range gotUtxos {
731                                 t.Errorf("case %d: gotUtxos[%d] %v", i, j, u)
732                         }
733
734                         for j, u := range c.wantUtxos {
735                                 t.Errorf("case %d: c.wantUtxos[%d] %v", i, j, u)
736                         }
737                 }
738         }
739 }