OSDN Git Service

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