OSDN Git Service

Hulk did something
[bytom/vapor.git] / protocol / orphan_manage_test.go
1 package protocol
2
3 import (
4         "testing"
5         "time"
6
7         "github.com/vapor/protocol/bc"
8         "github.com/vapor/protocol/bc/types"
9         "github.com/vapor/testutil"
10 )
11
12 var testBlocks = []*types.Block{
13         &types.Block{BlockHeader: types.BlockHeader{
14                 PreviousBlockHash: bc.Hash{V0: 1},
15                 Nonce:             0,
16         }},
17         &types.Block{BlockHeader: types.BlockHeader{
18                 PreviousBlockHash: bc.Hash{V0: 1},
19                 Nonce:             1,
20         }},
21         &types.Block{BlockHeader: types.BlockHeader{
22                 PreviousBlockHash: bc.Hash{V0: 2},
23                 Nonce:             3,
24         }},
25 }
26
27 var blockHashes = []bc.Hash{}
28
29 func init() {
30         for _, block := range testBlocks {
31                 blockHashes = append(blockHashes, block.Hash())
32         }
33 }
34
35 func TestOrphanManageAdd(t *testing.T) {
36         cases := []struct {
37                 before    *OrphanManage
38                 after     *OrphanManage
39                 addOrphan *types.Block
40         }{
41                 {
42                         before: &OrphanManage{
43                                 orphan:      map[bc.Hash]*orphanBlock{},
44                                 prevOrphans: map[bc.Hash][]*bc.Hash{},
45                         },
46                         after: &OrphanManage{
47                                 orphan: map[bc.Hash]*orphanBlock{
48                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
49                                 },
50                                 prevOrphans: map[bc.Hash][]*bc.Hash{
51                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
52                                 },
53                         },
54                         addOrphan: testBlocks[0],
55                 },
56                 {
57                         before: &OrphanManage{
58                                 orphan: map[bc.Hash]*orphanBlock{
59                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
60                                 },
61                                 prevOrphans: map[bc.Hash][]*bc.Hash{
62                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
63                                 },
64                         },
65                         after: &OrphanManage{
66                                 orphan: map[bc.Hash]*orphanBlock{
67                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
68                                 },
69                                 prevOrphans: map[bc.Hash][]*bc.Hash{
70                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
71                                 },
72                         },
73                         addOrphan: testBlocks[0],
74                 },
75                 {
76                         before: &OrphanManage{
77                                 orphan: map[bc.Hash]*orphanBlock{
78                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
79                                 },
80                                 prevOrphans: map[bc.Hash][]*bc.Hash{
81                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
82                                 },
83                         },
84                         after: &OrphanManage{
85                                 orphan: map[bc.Hash]*orphanBlock{
86                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
87                                         blockHashes[1]: &orphanBlock{testBlocks[1], time.Time{}},
88                                 },
89                                 prevOrphans: map[bc.Hash][]*bc.Hash{
90                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0], &blockHashes[1]},
91                                 },
92                         },
93                         addOrphan: testBlocks[1],
94                 },
95                 {
96                         before: &OrphanManage{
97                                 orphan: map[bc.Hash]*orphanBlock{
98                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
99                                 },
100                                 prevOrphans: map[bc.Hash][]*bc.Hash{
101                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
102                                 },
103                         },
104                         after: &OrphanManage{
105                                 orphan: map[bc.Hash]*orphanBlock{
106                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
107                                         blockHashes[2]: &orphanBlock{testBlocks[2], time.Time{}},
108                                 },
109                                 prevOrphans: map[bc.Hash][]*bc.Hash{
110                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
111                                         bc.Hash{V0: 2}: []*bc.Hash{&blockHashes[2]},
112                                 },
113                         },
114                         addOrphan: testBlocks[2],
115                 },
116         }
117
118         for i, c := range cases {
119                 c.before.Add(c.addOrphan)
120                 for _, orphan := range c.before.orphan {
121                         orphan.expiration = time.Time{}
122                 }
123                 if !testutil.DeepEqual(c.before, c.after) {
124                         t.Errorf("case %d: got %v want %v", i, c.before, c.after)
125                 }
126         }
127 }
128
129 func TestOrphanManageDelete(t *testing.T) {
130         cases := []struct {
131                 before *OrphanManage
132                 after  *OrphanManage
133                 remove *bc.Hash
134         }{
135                 {
136                         before: &OrphanManage{
137                                 orphan: map[bc.Hash]*orphanBlock{
138                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
139                                 },
140                                 prevOrphans: map[bc.Hash][]*bc.Hash{
141                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
142                                 },
143                         },
144                         after: &OrphanManage{
145                                 orphan: map[bc.Hash]*orphanBlock{
146                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
147                                 },
148                                 prevOrphans: map[bc.Hash][]*bc.Hash{
149                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
150                                 },
151                         },
152                         remove: &blockHashes[1],
153                 },
154                 {
155                         before: &OrphanManage{
156                                 orphan: map[bc.Hash]*orphanBlock{
157                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
158                                 },
159                                 prevOrphans: map[bc.Hash][]*bc.Hash{
160                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
161                                 },
162                         },
163                         after: &OrphanManage{
164                                 orphan:      map[bc.Hash]*orphanBlock{},
165                                 prevOrphans: map[bc.Hash][]*bc.Hash{},
166                         },
167                         remove: &blockHashes[0],
168                 },
169                 {
170                         before: &OrphanManage{
171                                 orphan: map[bc.Hash]*orphanBlock{
172                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
173                                         blockHashes[1]: &orphanBlock{testBlocks[1], time.Time{}},
174                                 },
175                                 prevOrphans: map[bc.Hash][]*bc.Hash{
176                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0], &blockHashes[1]},
177                                 },
178                         },
179                         after: &OrphanManage{
180                                 orphan: map[bc.Hash]*orphanBlock{
181                                         blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
182                                 },
183                                 prevOrphans: map[bc.Hash][]*bc.Hash{
184                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
185                                 },
186                         },
187                         remove: &blockHashes[1],
188                 },
189         }
190
191         for i, c := range cases {
192                 c.before.delete(c.remove)
193                 if !testutil.DeepEqual(c.before, c.after) {
194                         t.Errorf("case %d: got %v want %v", i, c.before, c.after)
195                 }
196         }
197 }
198
199 func TestOrphanManageExpire(t *testing.T) {
200         cases := []struct {
201                 before *OrphanManage
202                 after  *OrphanManage
203         }{
204                 {
205                         before: &OrphanManage{
206                                 orphan: map[bc.Hash]*orphanBlock{
207                                         blockHashes[0]: &orphanBlock{
208                                                 testBlocks[0],
209                                                 time.Unix(1633479700, 0),
210                                         },
211                                 },
212                                 prevOrphans: map[bc.Hash][]*bc.Hash{
213                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
214                                 },
215                         },
216                         after: &OrphanManage{
217                                 orphan:      map[bc.Hash]*orphanBlock{},
218                                 prevOrphans: map[bc.Hash][]*bc.Hash{},
219                         },
220                 },
221                 {
222                         before: &OrphanManage{
223                                 orphan: map[bc.Hash]*orphanBlock{
224                                         blockHashes[0]: &orphanBlock{
225                                                 testBlocks[0],
226                                                 time.Unix(1633479702, 0),
227                                         },
228                                 },
229                                 prevOrphans: map[bc.Hash][]*bc.Hash{
230                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
231                                 },
232                         },
233                         after: &OrphanManage{
234                                 orphan: map[bc.Hash]*orphanBlock{
235                                         blockHashes[0]: &orphanBlock{
236                                                 testBlocks[0],
237                                                 time.Unix(1633479702, 0),
238                                         },
239                                 },
240                                 prevOrphans: map[bc.Hash][]*bc.Hash{
241                                         bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
242                                 },
243                         },
244                 },
245         }
246
247         for i, c := range cases {
248                 c.before.orphanExpire(time.Unix(1633479701, 0))
249                 if !testutil.DeepEqual(c.before, c.after) {
250                         t.Errorf("case %d: got %v want %v", i, c.before, c.after)
251                 }
252         }
253 }
254
255 func TestOrphanManageNumLimit(t *testing.T) {
256         cases := []struct {
257                 addOrphanBlockNum    int
258                 expectOrphanBlockNum int
259         }{
260                 {
261                         addOrphanBlockNum:    10,
262                         expectOrphanBlockNum: 10,
263                 },
264                 {
265                         addOrphanBlockNum:    numOrphanBlockLimit,
266                         expectOrphanBlockNum: numOrphanBlockLimit,
267                 },
268                 {
269                         addOrphanBlockNum:    numOrphanBlockLimit + 1,
270                         expectOrphanBlockNum: numOrphanBlockLimit,
271                 },
272                 {
273                         addOrphanBlockNum:    numOrphanBlockLimit + 10,
274                         expectOrphanBlockNum: numOrphanBlockLimit,
275                 },
276         }
277
278         for i, c := range cases {
279                 orphanManage := &OrphanManage{
280                         orphan:      map[bc.Hash]*orphanBlock{},
281                         prevOrphans: map[bc.Hash][]*bc.Hash{},
282                 }
283                 for num := 0; num < c.addOrphanBlockNum; num++ {
284                         orphanManage.Add(&types.Block{BlockHeader: types.BlockHeader{Height: uint64(num)}})
285                 }
286                 if len(orphanManage.orphan) != c.expectOrphanBlockNum {
287                         t.Errorf("case %d: got %d want %d", i, len(orphanManage.orphan), c.expectOrphanBlockNum)
288                 }
289         }
290 }