OSDN Git Service

Merge pull request #204 from Bytom/dev
[bytom/bytom.git] / protocol / state / utxo_view_test.go
1 package state
2
3 import (
4         "testing"
5
6         "github.com/bytom/blockchain/txdb/storage"
7         "github.com/bytom/protocol/bc"
8         "github.com/bytom/testutil"
9 )
10
11 func TestApplyBlock(t *testing.T) {
12         cases := []struct {
13                 block     *bc.Block
14                 inputView *UtxoViewpoint
15                 fetchView *UtxoViewpoint
16                 err       bool
17         }{
18                 {
19                         block: &bc.Block{
20                                 Transactions: []*bc.Tx{
21                                         &bc.Tx{
22                                                 SpentOutputIDs: []bc.Hash{
23                                                         bc.Hash{V0: 0},
24                                                 },
25                                         },
26                                 },
27                         },
28                         inputView: NewUtxoViewpoint(),
29                         fetchView: NewUtxoViewpoint(),
30                         err:       true,
31                 },
32                 {
33                         block: &bc.Block{
34                                 Transactions: []*bc.Tx{
35                                         &bc.Tx{
36                                                 SpentOutputIDs: []bc.Hash{
37                                                         bc.Hash{V0: 0},
38                                                 },
39                                         },
40                                 },
41                         },
42                         inputView: &UtxoViewpoint{
43                                 Entries: map[bc.Hash]*storage.UtxoEntry{
44                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, true),
45                                 },
46                         },
47                         err: true,
48                 },
49                 {
50                         block: &bc.Block{
51                                 Transactions: []*bc.Tx{
52                                         &bc.Tx{
53                                                 TxHeader: &bc.TxHeader{
54                                                         ResultIds: []*bc.Hash{},
55                                                 },
56                                                 SpentOutputIDs: []bc.Hash{
57                                                         bc.Hash{V0: 0},
58                                                 },
59                                         },
60                                 },
61                         },
62                         inputView: &UtxoViewpoint{
63                                 Entries: map[bc.Hash]*storage.UtxoEntry{
64                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
65                                 },
66                         },
67                         fetchView: &UtxoViewpoint{
68                                 Entries: map[bc.Hash]*storage.UtxoEntry{
69                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, true),
70                                 },
71                         },
72                         err: false,
73                 },
74         }
75
76         for i, c := range cases {
77                 if err := c.inputView.ApplyBlock(c.block); c.err != (err != nil) {
78                         t.Errorf("want err = %v, get err = %v", c.err, err)
79                 }
80                 if c.err {
81                         continue
82                 }
83                 if !testutil.DeepEqual(c.inputView, c.fetchView) {
84                         t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)
85                 }
86         }
87 }
88
89 func TestDetachBlock(t *testing.T) {
90         cases := []struct {
91                 block     *bc.Block
92                 inputView *UtxoViewpoint
93                 fetchView *UtxoViewpoint
94                 err       bool
95         }{
96                 {
97                         block: &bc.Block{
98                                 Transactions: []*bc.Tx{
99                                         &bc.Tx{
100                                                 TxHeader: &bc.TxHeader{
101                                                         ResultIds: []*bc.Hash{},
102                                                 },
103                                                 SpentOutputIDs: []bc.Hash{
104                                                         bc.Hash{V0: 0},
105                                                 },
106                                         },
107                                 },
108                         },
109                         inputView: NewUtxoViewpoint(),
110                         err:       true,
111                 },
112                 {
113                         block: &bc.Block{
114                                 Transactions: []*bc.Tx{
115                                         &bc.Tx{
116                                                 TxHeader: &bc.TxHeader{
117                                                         ResultIds: []*bc.Hash{},
118                                                 },
119                                                 SpentOutputIDs: []bc.Hash{
120                                                         bc.Hash{V0: 0},
121                                                 },
122                                         },
123                                 },
124                         },
125                         inputView: &UtxoViewpoint{
126                                 Entries: map[bc.Hash]*storage.UtxoEntry{
127                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
128                                 },
129                         },
130                         err: true,
131                 },
132                 {
133                         block: &bc.Block{
134                                 Transactions: []*bc.Tx{
135                                         &bc.Tx{
136                                                 TxHeader: &bc.TxHeader{
137                                                         ResultIds: []*bc.Hash{},
138                                                 },
139                                                 SpentOutputIDs: []bc.Hash{
140                                                         bc.Hash{V0: 0},
141                                                 },
142                                         },
143                                 },
144                         },
145                         inputView: &UtxoViewpoint{
146                                 Entries: map[bc.Hash]*storage.UtxoEntry{
147                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, true),
148                                 },
149                         },
150                         fetchView: &UtxoViewpoint{
151                                 Entries: map[bc.Hash]*storage.UtxoEntry{
152                                         bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
153                                 },
154                         },
155                         err: false,
156                 },
157         }
158
159         for i, c := range cases {
160                 if err := c.inputView.DetachBlock(c.block); 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 }