OSDN Git Service

fix log (#388)
[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 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 os.RemoveAll("temp")
96
97         batch := testDB.NewBatch()
98         inputView := state.NewUtxoViewpoint()
99         for i := 0; i <= 2; i++ {
100                 inputView.Entries[bc.Hash{V0: uint64(i)}] = storage.NewUtxoEntry(storage.NormalUTXOType, uint64(i), false)
101         }
102         saveUtxoView(batch, inputView)
103         batch.Write()
104
105         cases := []struct {
106                 txs       []*bc.Tx
107                 inputView *state.UtxoViewpoint
108                 fetchView *state.UtxoViewpoint
109                 err       bool
110         }{
111
112                 {
113                         txs: []*bc.Tx{
114                                 &bc.Tx{
115                                         SpentOutputIDs: []bc.Hash{bc.Hash{V0: 10}},
116                                 },
117                         },
118                         inputView: state.NewUtxoViewpoint(),
119                         fetchView: state.NewUtxoViewpoint(),
120                         err:       false,
121                 },
122                 {
123                         txs: []*bc.Tx{
124                                 &bc.Tx{
125                                         SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
126                                 },
127                         },
128                         inputView: state.NewUtxoViewpoint(),
129                         fetchView: &state.UtxoViewpoint{
130                                 Entries: map[bc.Hash]*storage.UtxoEntry{
131                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
132                                 },
133                         },
134                         err: false,
135                 },
136                 {
137                         txs: []*bc.Tx{
138                                 &bc.Tx{
139                                         SpentOutputIDs: []bc.Hash{
140                                                 bc.Hash{V0: 0},
141                                                 bc.Hash{V0: 1},
142                                         },
143                                 },
144                         },
145                         inputView: state.NewUtxoViewpoint(),
146                         fetchView: &state.UtxoViewpoint{
147                                 Entries: map[bc.Hash]*storage.UtxoEntry{
148                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
149                                         bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
150                                 },
151                         },
152                         err: false,
153                 },
154                 {
155                         txs: []*bc.Tx{
156                                 &bc.Tx{
157                                         SpentOutputIDs: []bc.Hash{
158                                                 bc.Hash{V0: 0},
159                                                 bc.Hash{V0: 1},
160                                         },
161                                 },
162                                 &bc.Tx{
163                                         SpentOutputIDs: []bc.Hash{
164                                                 bc.Hash{V0: 2},
165                                         },
166                                 },
167                         },
168                         inputView: state.NewUtxoViewpoint(),
169                         fetchView: &state.UtxoViewpoint{
170                                 Entries: map[bc.Hash]*storage.UtxoEntry{
171                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
172                                         bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
173                                         bc.Hash{V0: 2}: storage.NewUtxoEntry(storage.NormalUTXOType, 2, false),
174                                 },
175                         },
176                         err: false,
177                 },
178                 {
179                         txs: []*bc.Tx{
180                                 &bc.Tx{
181                                         SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
182                                 },
183                         },
184                         inputView: &state.UtxoViewpoint{
185                                 Entries: map[bc.Hash]*storage.UtxoEntry{
186                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
187                                 },
188                         },
189                         fetchView: &state.UtxoViewpoint{
190                                 Entries: map[bc.Hash]*storage.UtxoEntry{
191                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
192                                 },
193                         },
194                         err: false,
195                 },
196         }
197
198         for i, c := range cases {
199                 if err := getTransactionsUtxo(testDB, c.inputView, c.txs); c.err != (err != nil) {
200                         t.Errorf("test case %d, want err = %v, get err = %v", i, c.err, err)
201                 }
202                 if !testutil.DeepEqual(c.inputView, c.fetchView) {
203                         t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)
204                 }
205         }
206 }