OSDN Git Service

update master (#487)
[bytom/bytom-spv.git] / protocol / state / utxo_view_test.go
1 package state
2
3 import (
4         "testing"
5
6         "github.com/bytom/database/storage"
7         "github.com/bytom/protocol/bc"
8         "github.com/bytom/testutil"
9 )
10
11 var defaultEntry = map[bc.Hash]bc.Entry{
12         bc.Hash{V0: 0}: &bc.Output{
13                 Source: &bc.ValueSource{
14                         Value: &bc.AssetAmount{
15                                 AssetId: &bc.AssetID{V0: 0},
16                         },
17                 },
18         },
19 }
20
21 func TestApplyBlock(t *testing.T) {
22         cases := []struct {
23                 block     *bc.Block
24                 inputView *UtxoViewpoint
25                 fetchView *UtxoViewpoint
26                 err       bool
27         }{
28                 {
29                         block: &bc.Block{
30                                 BlockHeader: &bc.BlockHeader{
31                                         TransactionStatus: bc.NewTransactionStatus(),
32                                 },
33                                 Transactions: []*bc.Tx{
34                                         &bc.Tx{
35                                                 SpentOutputIDs: []bc.Hash{
36                                                         bc.Hash{V0: 0},
37                                                 },
38                                                 Entries: defaultEntry,
39                                         },
40                                 },
41                         },
42                         inputView: NewUtxoViewpoint(),
43                         fetchView: NewUtxoViewpoint(),
44                         err:       true,
45                 },
46                 {
47                         block: &bc.Block{
48                                 BlockHeader: &bc.BlockHeader{
49                                         TransactionStatus: bc.NewTransactionStatus(),
50                                 },
51                                 Transactions: []*bc.Tx{
52                                         &bc.Tx{
53                                                 SpentOutputIDs: []bc.Hash{
54                                                         bc.Hash{V0: 0},
55                                                 },
56                                                 Entries: defaultEntry,
57                                         },
58                                 },
59                         },
60                         inputView: &UtxoViewpoint{
61                                 Entries: map[bc.Hash]*storage.UtxoEntry{
62                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, true),
63                                 },
64                         },
65                         err: true,
66                 },
67                 {
68                         block: &bc.Block{
69                                 BlockHeader: &bc.BlockHeader{
70                                         TransactionStatus: bc.NewTransactionStatus(),
71                                 },
72                                 Transactions: []*bc.Tx{
73                                         &bc.Tx{
74                                                 TxHeader: &bc.TxHeader{
75                                                         ResultIds: []*bc.Hash{},
76                                                 },
77                                                 SpentOutputIDs: []bc.Hash{
78                                                         bc.Hash{V0: 0},
79                                                 },
80                                                 Entries: defaultEntry,
81                                         },
82                                 },
83                         },
84                         inputView: &UtxoViewpoint{
85                                 Entries: map[bc.Hash]*storage.UtxoEntry{
86                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
87                                 },
88                         },
89                         fetchView: &UtxoViewpoint{
90                                 Entries: map[bc.Hash]*storage.UtxoEntry{
91                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, true),
92                                 },
93                         },
94                         err: false,
95                 },
96                 {
97                         block: &bc.Block{
98                                 BlockHeader: &bc.BlockHeader{
99                                         Height:            7,
100                                         TransactionStatus: bc.NewTransactionStatus(),
101                                 },
102                                 Transactions: []*bc.Tx{
103                                         &bc.Tx{
104                                                 TxHeader: &bc.TxHeader{
105                                                         ResultIds: []*bc.Hash{},
106                                                 },
107                                                 SpentOutputIDs: []bc.Hash{
108                                                         bc.Hash{V0: 0},
109                                                 },
110                                                 Entries: defaultEntry,
111                                         },
112                                 },
113                         },
114                         inputView: &UtxoViewpoint{
115                                 Entries: map[bc.Hash]*storage.UtxoEntry{
116                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(true, 0, false),
117                                 },
118                         },
119                         fetchView: &UtxoViewpoint{
120                                 Entries: map[bc.Hash]*storage.UtxoEntry{
121                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(true, 0, true),
122                                 },
123                         },
124                         err: false,
125                 },
126                 {
127                         block: &bc.Block{
128                                 BlockHeader: &bc.BlockHeader{
129                                         Height:            0,
130                                         TransactionStatus: bc.NewTransactionStatus(),
131                                 },
132                                 Transactions: []*bc.Tx{
133                                         &bc.Tx{
134                                                 TxHeader: &bc.TxHeader{
135                                                         ResultIds: []*bc.Hash{},
136                                                 },
137                                                 SpentOutputIDs: []bc.Hash{
138                                                         bc.Hash{V0: 0},
139                                                 },
140                                                 Entries: defaultEntry,
141                                         },
142                                 },
143                         },
144                         inputView: &UtxoViewpoint{
145                                 Entries: map[bc.Hash]*storage.UtxoEntry{
146                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(true, 0, false),
147                                 },
148                         },
149                         fetchView: &UtxoViewpoint{
150                                 Entries: map[bc.Hash]*storage.UtxoEntry{
151                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(true, 0, true),
152                                 },
153                         },
154                         err: true,
155                 },
156         }
157
158         for i, c := range cases {
159
160                 if err := c.inputView.ApplyBlock(c.block, c.block.TransactionStatus); c.err != (err != nil) {
161                         t.Errorf("want err = %v, get err = %v", c.err, err)
162                 }
163                 if c.err {
164                         continue
165                 }
166                 if !testutil.DeepEqual(c.inputView, c.fetchView) {
167                         t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)
168                 }
169         }
170 }
171
172 func TestDetachBlock(t *testing.T) {
173         cases := []struct {
174                 block     *bc.Block
175                 inputView *UtxoViewpoint
176                 fetchView *UtxoViewpoint
177                 err       bool
178         }{
179                 {
180                         block: &bc.Block{
181                                 BlockHeader: &bc.BlockHeader{
182                                         TransactionStatus: bc.NewTransactionStatus(),
183                                 },
184                                 Transactions: []*bc.Tx{
185                                         &bc.Tx{
186                                                 TxHeader: &bc.TxHeader{
187                                                         ResultIds: []*bc.Hash{},
188                                                 },
189                                                 SpentOutputIDs: []bc.Hash{
190                                                         bc.Hash{V0: 0},
191                                                 },
192                                                 Entries: defaultEntry,
193                                         },
194                                 },
195                         },
196                         inputView: NewUtxoViewpoint(),
197                         fetchView: &UtxoViewpoint{
198                                 Entries: map[bc.Hash]*storage.UtxoEntry{
199                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
200                                 },
201                         },
202                         err: false,
203                 },
204                 {
205                         block: &bc.Block{
206                                 BlockHeader: &bc.BlockHeader{
207                                         TransactionStatus: bc.NewTransactionStatus(),
208                                 },
209                                 Transactions: []*bc.Tx{
210                                         &bc.Tx{
211                                                 TxHeader: &bc.TxHeader{
212                                                         ResultIds: []*bc.Hash{},
213                                                 },
214                                                 SpentOutputIDs: []bc.Hash{
215                                                         bc.Hash{V0: 0},
216                                                 },
217                                                 Entries: defaultEntry,
218                                         },
219                                 },
220                         },
221                         inputView: &UtxoViewpoint{
222                                 Entries: map[bc.Hash]*storage.UtxoEntry{
223                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
224                                 },
225                         },
226                         err: true,
227                 },
228                 {
229                         block: &bc.Block{
230                                 BlockHeader: &bc.BlockHeader{
231                                         TransactionStatus: bc.NewTransactionStatus(),
232                                 },
233                                 Transactions: []*bc.Tx{
234                                         &bc.Tx{
235                                                 TxHeader: &bc.TxHeader{
236                                                         ResultIds: []*bc.Hash{},
237                                                 },
238                                                 SpentOutputIDs: []bc.Hash{
239                                                         bc.Hash{V0: 0},
240                                                 },
241                                                 Entries: defaultEntry,
242                                         },
243                                 },
244                         },
245                         inputView: &UtxoViewpoint{
246                                 Entries: map[bc.Hash]*storage.UtxoEntry{
247                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, true),
248                                 },
249                         },
250                         fetchView: &UtxoViewpoint{
251                                 Entries: map[bc.Hash]*storage.UtxoEntry{
252                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
253                                 },
254                         },
255                         err: false,
256                 },
257         }
258
259         for i, c := range cases {
260                 if err := c.inputView.DetachBlock(c.block, c.block.TransactionStatus); c.err != (err != nil) {
261                         t.Errorf("case %d want err = %v, get err = %v", i, c.err, err)
262                 }
263                 if c.err {
264                         continue
265                 }
266                 if !testutil.DeepEqual(c.inputView, c.fetchView) {
267                         t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)
268                 }
269         }
270 }