• R/O
  • HTTP
  • SSH
  • HTTPS

bytom: Commit

Official Go implementation of the Bytom protocol


Commit MetaInfo

Revision68dd4068236efb0b2eab7b22c098e512af035b11 (tree)
Time2019-08-13 12:42:57
AuthorYahtoo Ma <yahtoo.ma@gmai...>
CommiterYahtoo Ma

Log Message

fix the orphan issue

Change Summary

Incremental Difference

--- a/protocol/orphan_manage.go
+++ b/protocol/orphan_manage.go
@@ -24,7 +24,7 @@ type OrphanBlock struct {
2424
2525 func NewOrphanBlock(block *types.Block, expiration time.Time) *OrphanBlock {
2626 return &OrphanBlock{
27- Block: block,
27+ Block: block,
2828 expiration: expiration,
2929 }
3030 }
@@ -70,8 +70,8 @@ func (o *OrphanManage) Add(block *types.Block) {
7070 }
7171
7272 if len(o.orphan) >= numOrphanBlockLimit {
73+ o.deleteLRU()
7374 log.WithFields(log.Fields{"module": logModule, "hash": blockHash.String(), "height": block.Height}).Info("the number of orphan blocks exceeds the limit")
74- return
7575 }
7676
7777 o.orphan[blockHash] = &OrphanBlock{block, time.Now().Add(orphanBlockTTL)}
@@ -137,13 +137,27 @@ func (o *OrphanManage) delete(hash *bc.Hash) {
137137 }
138138
139139 for i, preOrphan := range prevOrphans {
140- if preOrphan == hash {
140+ if *preOrphan == *hash {
141141 o.prevOrphans[block.Block.PreviousBlockHash] = append(prevOrphans[:i], prevOrphans[i+1:]...)
142142 return
143143 }
144144 }
145145 }
146146
147+func (o *OrphanManage) deleteLRU() {
148+ var deleteBlock *OrphanBlock
149+ for _, orphan := range o.orphan {
150+ if deleteBlock == nil || orphan.expiration.Before(deleteBlock.expiration) {
151+ deleteBlock = orphan
152+ }
153+ }
154+
155+ if deleteBlock != nil {
156+ blockHash := deleteBlock.Block.Hash()
157+ o.delete(&blockHash)
158+ }
159+}
160+
147161 func (o *OrphanManage) orphanExpireWorker() {
148162 ticker := time.NewTicker(orphanExpireWorkInterval)
149163 for now := range ticker.C {
--- a/protocol/orphan_manage_test.go
+++ b/protocol/orphan_manage_test.go
@@ -10,15 +10,15 @@ import (
1010 )
1111
1212 var testBlocks = []*types.Block{
13- &types.Block{BlockHeader: types.BlockHeader{
13+ {BlockHeader: types.BlockHeader{
1414 PreviousBlockHash: bc.Hash{V0: 1},
1515 Nonce: 0,
1616 }},
17- &types.Block{BlockHeader: types.BlockHeader{
17+ {BlockHeader: types.BlockHeader{
1818 PreviousBlockHash: bc.Hash{V0: 1},
1919 Nonce: 1,
2020 }},
21- &types.Block{BlockHeader: types.BlockHeader{
21+ {BlockHeader: types.BlockHeader{
2222 PreviousBlockHash: bc.Hash{V0: 2},
2323 Nonce: 3,
2424 }},
@@ -32,6 +32,65 @@ func init() {
3232 }
3333 }
3434
35+func TestDeleteLRU(t *testing.T) {
36+ now := time.Now()
37+ cases := []struct {
38+ before *OrphanManage
39+ after *OrphanManage
40+ }{
41+ {
42+ before: &OrphanManage{
43+ orphan: map[bc.Hash]*OrphanBlock{
44+ blockHashes[0]: {testBlocks[0], now},
45+ },
46+ prevOrphans: map[bc.Hash][]*bc.Hash{
47+ {V0: 1}: {&blockHashes[0]},
48+ },
49+ },
50+ after: &OrphanManage{
51+ orphan: map[bc.Hash]*OrphanBlock{},
52+ prevOrphans: map[bc.Hash][]*bc.Hash{},
53+ },
54+ },
55+ {
56+ before: &OrphanManage{
57+ orphan: map[bc.Hash]*OrphanBlock{},
58+ prevOrphans: map[bc.Hash][]*bc.Hash{},
59+ },
60+ after: &OrphanManage{
61+ orphan: map[bc.Hash]*OrphanBlock{},
62+ prevOrphans: map[bc.Hash][]*bc.Hash{},
63+ },
64+ },
65+ {
66+ before: &OrphanManage{
67+ orphan: map[bc.Hash]*OrphanBlock{
68+ blockHashes[0]: {testBlocks[0], now.Add(2)},
69+ blockHashes[1]: {testBlocks[1], now.Add(1)},
70+ },
71+ prevOrphans: map[bc.Hash][]*bc.Hash{
72+ {V0: 1}: {&blockHashes[0], &blockHashes[1]},
73+ },
74+ },
75+ after: &OrphanManage{
76+ orphan: map[bc.Hash]*OrphanBlock{
77+ blockHashes[0]: {testBlocks[0], now.Add(2)},
78+ },
79+ prevOrphans: map[bc.Hash][]*bc.Hash{
80+ {V0: 1}: {&blockHashes[0]},
81+ },
82+ },
83+ },
84+ }
85+
86+ for i, c := range cases {
87+ c.before.deleteLRU()
88+ if !testutil.DeepEqual(c.before, c.after) {
89+ t.Errorf("case %d: got %v want %v", i, c.before, c.after)
90+ }
91+ }
92+}
93+
3594 func TestOrphanManageAdd(t *testing.T) {
3695 cases := []struct {
3796 before *OrphanManage
@@ -45,10 +104,10 @@ func TestOrphanManageAdd(t *testing.T) {
45104 },
46105 after: &OrphanManage{
47106 orphan: map[bc.Hash]*OrphanBlock{
48- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
107+ blockHashes[0]: {testBlocks[0], time.Time{}},
49108 },
50109 prevOrphans: map[bc.Hash][]*bc.Hash{
51- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
110+ {V0: 1}: {&blockHashes[0]},
52111 },
53112 },
54113 addOrphan: testBlocks[0],
@@ -56,18 +115,18 @@ func TestOrphanManageAdd(t *testing.T) {
56115 {
57116 before: &OrphanManage{
58117 orphan: map[bc.Hash]*OrphanBlock{
59- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
118+ blockHashes[0]: {testBlocks[0], time.Time{}},
60119 },
61120 prevOrphans: map[bc.Hash][]*bc.Hash{
62- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
121+ {V0: 1}: {&blockHashes[0]},
63122 },
64123 },
65124 after: &OrphanManage{
66125 orphan: map[bc.Hash]*OrphanBlock{
67- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
126+ blockHashes[0]: {testBlocks[0], time.Time{}},
68127 },
69128 prevOrphans: map[bc.Hash][]*bc.Hash{
70- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
129+ {V0: 1}: {&blockHashes[0]},
71130 },
72131 },
73132 addOrphan: testBlocks[0],
@@ -75,19 +134,19 @@ func TestOrphanManageAdd(t *testing.T) {
75134 {
76135 before: &OrphanManage{
77136 orphan: map[bc.Hash]*OrphanBlock{
78- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
137+ blockHashes[0]: {testBlocks[0], time.Time{}},
79138 },
80139 prevOrphans: map[bc.Hash][]*bc.Hash{
81- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
140+ {V0: 1}: {&blockHashes[0]},
82141 },
83142 },
84143 after: &OrphanManage{
85144 orphan: map[bc.Hash]*OrphanBlock{
86- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
87- blockHashes[1]: &OrphanBlock{testBlocks[1], time.Time{}},
145+ blockHashes[0]: {testBlocks[0], time.Time{}},
146+ blockHashes[1]: {testBlocks[1], time.Time{}},
88147 },
89148 prevOrphans: map[bc.Hash][]*bc.Hash{
90- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0], &blockHashes[1]},
149+ {V0: 1}: {&blockHashes[0], &blockHashes[1]},
91150 },
92151 },
93152 addOrphan: testBlocks[1],
@@ -95,20 +154,20 @@ func TestOrphanManageAdd(t *testing.T) {
95154 {
96155 before: &OrphanManage{
97156 orphan: map[bc.Hash]*OrphanBlock{
98- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
157+ blockHashes[0]: {testBlocks[0], time.Time{}},
99158 },
100159 prevOrphans: map[bc.Hash][]*bc.Hash{
101- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
160+ {V0: 1}: {&blockHashes[0]},
102161 },
103162 },
104163 after: &OrphanManage{
105164 orphan: map[bc.Hash]*OrphanBlock{
106- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
107- blockHashes[2]: &OrphanBlock{testBlocks[2], time.Time{}},
165+ blockHashes[0]: {testBlocks[0], time.Time{}},
166+ blockHashes[2]: {testBlocks[2], time.Time{}},
108167 },
109168 prevOrphans: map[bc.Hash][]*bc.Hash{
110- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
111- bc.Hash{V0: 2}: []*bc.Hash{&blockHashes[2]},
169+ {V0: 1}: {&blockHashes[0]},
170+ {V0: 2}: {&blockHashes[2]},
112171 },
113172 },
114173 addOrphan: testBlocks[2],
@@ -135,18 +194,18 @@ func TestOrphanManageDelete(t *testing.T) {
135194 {
136195 before: &OrphanManage{
137196 orphan: map[bc.Hash]*OrphanBlock{
138- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
197+ blockHashes[0]: {testBlocks[0], time.Time{}},
139198 },
140199 prevOrphans: map[bc.Hash][]*bc.Hash{
141- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
200+ {V0: 1}: {&blockHashes[0]},
142201 },
143202 },
144203 after: &OrphanManage{
145204 orphan: map[bc.Hash]*OrphanBlock{
146- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
205+ blockHashes[0]: {testBlocks[0], time.Time{}},
147206 },
148207 prevOrphans: map[bc.Hash][]*bc.Hash{
149- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
208+ {V0: 1}: {&blockHashes[0]},
150209 },
151210 },
152211 remove: &blockHashes[1],
@@ -154,10 +213,10 @@ func TestOrphanManageDelete(t *testing.T) {
154213 {
155214 before: &OrphanManage{
156215 orphan: map[bc.Hash]*OrphanBlock{
157- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
216+ blockHashes[0]: {testBlocks[0], time.Time{}},
158217 },
159218 prevOrphans: map[bc.Hash][]*bc.Hash{
160- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
219+ {V0: 1}: {&blockHashes[0]},
161220 },
162221 },
163222 after: &OrphanManage{
@@ -169,19 +228,19 @@ func TestOrphanManageDelete(t *testing.T) {
169228 {
170229 before: &OrphanManage{
171230 orphan: map[bc.Hash]*OrphanBlock{
172- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
173- blockHashes[1]: &OrphanBlock{testBlocks[1], time.Time{}},
231+ blockHashes[0]: {testBlocks[0], time.Time{}},
232+ blockHashes[1]: {testBlocks[1], time.Time{}},
174233 },
175234 prevOrphans: map[bc.Hash][]*bc.Hash{
176- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0], &blockHashes[1]},
235+ {V0: 1}: {&blockHashes[0], &blockHashes[1]},
177236 },
178237 },
179238 after: &OrphanManage{
180239 orphan: map[bc.Hash]*OrphanBlock{
181- blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
240+ blockHashes[0]: {testBlocks[0], time.Time{}},
182241 },
183242 prevOrphans: map[bc.Hash][]*bc.Hash{
184- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
243+ {V0: 1}: {&blockHashes[0]},
185244 },
186245 },
187246 remove: &blockHashes[1],
@@ -204,13 +263,13 @@ func TestOrphanManageExpire(t *testing.T) {
204263 {
205264 before: &OrphanManage{
206265 orphan: map[bc.Hash]*OrphanBlock{
207- blockHashes[0]: &OrphanBlock{
266+ blockHashes[0]: {
208267 testBlocks[0],
209268 time.Unix(1633479700, 0),
210269 },
211270 },
212271 prevOrphans: map[bc.Hash][]*bc.Hash{
213- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
272+ {V0: 1}: {&blockHashes[0]},
214273 },
215274 },
216275 after: &OrphanManage{
@@ -221,24 +280,24 @@ func TestOrphanManageExpire(t *testing.T) {
221280 {
222281 before: &OrphanManage{
223282 orphan: map[bc.Hash]*OrphanBlock{
224- blockHashes[0]: &OrphanBlock{
283+ blockHashes[0]: {
225284 testBlocks[0],
226285 time.Unix(1633479702, 0),
227286 },
228287 },
229288 prevOrphans: map[bc.Hash][]*bc.Hash{
230- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
289+ {V0: 1}: {&blockHashes[0]},
231290 },
232291 },
233292 after: &OrphanManage{
234293 orphan: map[bc.Hash]*OrphanBlock{
235- blockHashes[0]: &OrphanBlock{
294+ blockHashes[0]: {
236295 testBlocks[0],
237296 time.Unix(1633479702, 0),
238297 },
239298 },
240299 prevOrphans: map[bc.Hash][]*bc.Hash{
241- bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
300+ {V0: 1}: {&blockHashes[0]},
242301 },
243302 },
244303 },
@@ -253,24 +312,24 @@ func TestOrphanManageExpire(t *testing.T) {
253312 }
254313
255314 func TestOrphanManageNumLimit(t *testing.T) {
256- cases := []struct{
257- addOrphanBlockNum int
315+ cases := []struct {
316+ addOrphanBlockNum int
258317 expectOrphanBlockNum int
259318 }{
260319 {
261- addOrphanBlockNum: 10,
320+ addOrphanBlockNum: 10,
262321 expectOrphanBlockNum: 10,
263322 },
264323 {
265- addOrphanBlockNum: numOrphanBlockLimit,
324+ addOrphanBlockNum: numOrphanBlockLimit,
266325 expectOrphanBlockNum: numOrphanBlockLimit,
267326 },
268327 {
269- addOrphanBlockNum: numOrphanBlockLimit + 1,
328+ addOrphanBlockNum: numOrphanBlockLimit + 1,
270329 expectOrphanBlockNum: numOrphanBlockLimit,
271330 },
272331 {
273- addOrphanBlockNum: numOrphanBlockLimit + 10,
332+ addOrphanBlockNum: numOrphanBlockLimit + 10,
274333 expectOrphanBlockNum: numOrphanBlockLimit,
275334 },
276335 }
@@ -283,7 +342,7 @@ func TestOrphanManageNumLimit(t *testing.T) {
283342 for num := 0; num < c.addOrphanBlockNum; num++ {
284343 orphanManage.Add(&types.Block{BlockHeader: types.BlockHeader{Height: uint64(num)}})
285344 }
286- if (len(orphanManage.orphan) != c.expectOrphanBlockNum) {
345+ if len(orphanManage.orphan) != c.expectOrphanBlockNum {
287346 t.Errorf("case %d: got %d want %d", i, len(orphanManage.orphan), c.expectOrphanBlockNum)
288347 }
289348 }
Show on old repository browser