OSDN Git Service

feat: add build crosschain input (#91)
[bytom/vapor.git] / database / utxo_view_test.go
1 package database
2
3 import (
4         "os"
5         "testing"
6
7         dbm "github.com/vapor/database/leveldb"
8         "github.com/vapor/database/storage"
9         "github.com/vapor/protocol/bc"
10         "github.com/vapor/protocol/state"
11         "github.com/vapor/testutil"
12 )
13
14 func TestSaveUtxoView(t *testing.T) {
15         testDB := dbm.NewDB("testdb", "leveldb", "temp")
16         batch := testDB.NewBatch()
17         defer os.RemoveAll("temp")
18
19         cases := []struct {
20                 hash      bc.Hash
21                 utxoEntry *storage.UtxoEntry
22                 exist     bool
23         }{
24                 {
25                         hash:      bc.Hash{V0: 0},
26                         utxoEntry: storage.NewUtxoEntry(storage.CoinbaseUTXOType, 0, true),
27                         exist:     true,
28                 },
29                 {
30                         hash:      bc.Hash{V0: 1},
31                         utxoEntry: storage.NewUtxoEntry(storage.CoinbaseUTXOType, 0, false),
32                         exist:     true,
33                 },
34                 {
35                         hash:      bc.Hash{V0: 2},
36                         utxoEntry: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
37                         exist:     true,
38                 },
39                 {
40                         hash:      bc.Hash{V0: 3},
41                         utxoEntry: storage.NewUtxoEntry(storage.NormalUTXOType, 0, true),
42                         exist:     false,
43                 },
44                 {
45                         hash:      bc.Hash{V0: 4},
46                         utxoEntry: storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, true),
47                         exist:     true,
48                 },
49                 {
50                         hash:      bc.Hash{V0: 5},
51                         utxoEntry: storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, false),
52                         exist:     false,
53                 },
54         }
55
56         view := state.NewUtxoViewpoint()
57         for _, c := range cases {
58                 view.Entries[c.hash] = c.utxoEntry
59         }
60
61         saveUtxoView(batch, view)
62         batch.Write()
63
64         for _, c := range cases {
65                 entry, err := getUtxo(testDB, &c.hash)
66
67                 if !c.exist {
68                         if err == nil {
69                                 t.Errorf("%v should be unexisted, but it's in the db", c)
70                         }
71                         continue
72                 }
73
74                 if !testutil.DeepEqual(entry, c.utxoEntry) {
75                         t.Errorf("%v utxo in the db isn't match", c)
76                 }
77         }
78 }
79
80 func TestGetTransactionsUtxo(t *testing.T) {
81         testDB := dbm.NewDB("testdb", "leveldb", "temp")
82         defer os.RemoveAll("temp")
83
84         batch := testDB.NewBatch()
85         inputView := state.NewUtxoViewpoint()
86         for i := 0; i <= 2; i++ {
87                 inputView.Entries[bc.Hash{V0: uint64(i)}] = storage.NewUtxoEntry(storage.NormalUTXOType, uint64(i), false)
88         }
89         saveUtxoView(batch, inputView)
90         batch.Write()
91
92         cases := []struct {
93                 txs       []*bc.Tx
94                 inputView *state.UtxoViewpoint
95                 fetchView *state.UtxoViewpoint
96                 err       bool
97         }{
98
99                 {
100                         txs: []*bc.Tx{
101                                 &bc.Tx{
102                                         SpentOutputIDs: []bc.Hash{bc.Hash{V0: 10}},
103                                 },
104                         },
105                         inputView: state.NewUtxoViewpoint(),
106                         fetchView: state.NewUtxoViewpoint(),
107                         err:       false,
108                 },
109                 {
110                         txs: []*bc.Tx{
111                                 &bc.Tx{
112                                         SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
113                                 },
114                         },
115                         inputView: state.NewUtxoViewpoint(),
116                         fetchView: &state.UtxoViewpoint{
117                                 Entries: map[bc.Hash]*storage.UtxoEntry{
118                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
119                                 },
120                         },
121                         err: false,
122                 },
123                 {
124                         txs: []*bc.Tx{
125                                 &bc.Tx{
126                                         SpentOutputIDs: []bc.Hash{
127                                                 bc.Hash{V0: 0},
128                                                 bc.Hash{V0: 1},
129                                         },
130                                 },
131                         },
132                         inputView: state.NewUtxoViewpoint(),
133                         fetchView: &state.UtxoViewpoint{
134                                 Entries: map[bc.Hash]*storage.UtxoEntry{
135                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
136                                         bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
137                                 },
138                         },
139                         err: false,
140                 },
141                 {
142                         txs: []*bc.Tx{
143                                 &bc.Tx{
144                                         SpentOutputIDs: []bc.Hash{
145                                                 bc.Hash{V0: 0},
146                                                 bc.Hash{V0: 1},
147                                         },
148                                 },
149                                 &bc.Tx{
150                                         SpentOutputIDs: []bc.Hash{
151                                                 bc.Hash{V0: 2},
152                                         },
153                                 },
154                         },
155                         inputView: state.NewUtxoViewpoint(),
156                         fetchView: &state.UtxoViewpoint{
157                                 Entries: map[bc.Hash]*storage.UtxoEntry{
158                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
159                                         bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
160                                         bc.Hash{V0: 2}: storage.NewUtxoEntry(storage.NormalUTXOType, 2, false),
161                                 },
162                         },
163                         err: false,
164                 },
165                 {
166                         txs: []*bc.Tx{
167                                 &bc.Tx{
168                                         SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
169                                 },
170                         },
171                         inputView: &state.UtxoViewpoint{
172                                 Entries: map[bc.Hash]*storage.UtxoEntry{
173                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
174                                 },
175                         },
176                         fetchView: &state.UtxoViewpoint{
177                                 Entries: map[bc.Hash]*storage.UtxoEntry{
178                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
179                                 },
180                         },
181                         err: false,
182                 },
183         }
184
185         for i, c := range cases {
186                 if err := getTransactionsUtxo(testDB, c.inputView, c.txs); c.err != (err != nil) {
187                         t.Errorf("test case %d, want err = %v, get err = %v", i, c.err, err)
188                 }
189                 if !testutil.DeepEqual(c.inputView, c.fetchView) {
190                         t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)
191                 }
192         }
193 }