OSDN Git Service

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