OSDN Git Service

525624a00ffc61177990c53240fdd11570db1b5b
[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(true, 0, true),
27                         exist:     true,
28                 },
29                 {
30                         hash:      bc.Hash{V0: 1},
31                         utxoEntry: storage.NewUtxoEntry(true, 0, false),
32                         exist:     true,
33                 },
34                 {
35                         hash:      bc.Hash{V0: 2},
36                         utxoEntry: storage.NewUtxoEntry(false, 0, false),
37                         exist:     true,
38                 },
39                 {
40                         hash:      bc.Hash{V0: 3},
41                         utxoEntry: storage.NewUtxoEntry(false, 0, true),
42                         exist:     false,
43                 },
44         }
45
46         view := state.NewUtxoViewpoint()
47         for _, c := range cases {
48                 view.Entries[c.hash] = c.utxoEntry
49         }
50
51         saveUtxoView(batch, view)
52         batch.Write()
53
54         for _, c := range cases {
55                 entry, err := getUtxo(testDB, &c.hash)
56
57                 if !c.exist {
58                         if err == nil {
59                                 t.Errorf("%v should be unexisted, but it's in the db", c)
60                         }
61                         continue
62                 }
63
64                 if !testutil.DeepEqual(entry, c.utxoEntry) {
65                         t.Errorf("%v utxo in the db isn't match", c)
66                 }
67         }
68 }
69
70 func TestGetTransactionsUtxo(t *testing.T) {
71         testDB := dbm.NewDB("testdb", "leveldb", "temp")
72         defer os.RemoveAll("temp")
73
74         batch := testDB.NewBatch()
75         inputView := state.NewUtxoViewpoint()
76         for i := 0; i <= 2; i++ {
77                 inputView.Entries[bc.Hash{V0: uint64(i)}] = storage.NewUtxoEntry(false, uint64(i), false)
78         }
79         saveUtxoView(batch, inputView)
80         batch.Write()
81
82         cases := []struct {
83                 txs       []*bc.Tx
84                 inputView *state.UtxoViewpoint
85                 fetchView *state.UtxoViewpoint
86                 err       bool
87         }{
88
89                 {
90                         txs: []*bc.Tx{
91                                 &bc.Tx{
92                                         SpentOutputIDs: []bc.Hash{bc.Hash{V0: 10}},
93                                 },
94                         },
95                         inputView: state.NewUtxoViewpoint(),
96                         fetchView: state.NewUtxoViewpoint(),
97                         err:       false,
98                 },
99                 {
100                         txs: []*bc.Tx{
101                                 &bc.Tx{
102                                         SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
103                                 },
104                         },
105                         inputView: state.NewUtxoViewpoint(),
106                         fetchView: &state.UtxoViewpoint{
107                                 Entries: map[bc.Hash]*storage.UtxoEntry{
108                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
109                                 },
110                         },
111                         err: false,
112                 },
113                 {
114                         txs: []*bc.Tx{
115                                 &bc.Tx{
116                                         SpentOutputIDs: []bc.Hash{
117                                                 bc.Hash{V0: 0},
118                                                 bc.Hash{V0: 1},
119                                         },
120                                 },
121                         },
122                         inputView: state.NewUtxoViewpoint(),
123                         fetchView: &state.UtxoViewpoint{
124                                 Entries: map[bc.Hash]*storage.UtxoEntry{
125                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
126                                         bc.Hash{V0: 1}: storage.NewUtxoEntry(false, 1, false),
127                                 },
128                         },
129                         err: false,
130                 },
131                 {
132                         txs: []*bc.Tx{
133                                 &bc.Tx{
134                                         SpentOutputIDs: []bc.Hash{
135                                                 bc.Hash{V0: 0},
136                                                 bc.Hash{V0: 1},
137                                         },
138                                 },
139                                 &bc.Tx{
140                                         SpentOutputIDs: []bc.Hash{
141                                                 bc.Hash{V0: 2},
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(false, 0, false),
149                                         bc.Hash{V0: 1}: storage.NewUtxoEntry(false, 1, false),
150                                         bc.Hash{V0: 2}: storage.NewUtxoEntry(false, 2, false),
151                                 },
152                         },
153                         err: false,
154                 },
155                 {
156                         txs: []*bc.Tx{
157                                 &bc.Tx{
158                                         SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
159                                 },
160                         },
161                         inputView: &state.UtxoViewpoint{
162                                 Entries: map[bc.Hash]*storage.UtxoEntry{
163                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 1, false),
164                                 },
165                         },
166                         fetchView: &state.UtxoViewpoint{
167                                 Entries: map[bc.Hash]*storage.UtxoEntry{
168                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 1, false),
169                                 },
170                         },
171                         err: false,
172                 },
173         }
174
175         for i, c := range cases {
176                 if err := getTransactionsUtxo(testDB, c.inputView, c.txs); c.err != (err != nil) {
177                         t.Errorf("test case %d, want err = %v, get err = %v", i, c.err, err)
178                 }
179                 if !testutil.DeepEqual(c.inputView, c.fetchView) {
180                         t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)
181                 }
182         }
183 }