• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revisione58aa258123d47b2e6ff5755fd02f7eaa21a1cb7 (tree)
Time2019-06-20 10:28:57
Authorwz <mars@byto...>
CommiterGitHub

Log Message

Merge branch 'v0.1' into v0.1_modify_utxo_limit

Change Summary

Incremental Difference

--- a/protocol/orphan_manage.go
+++ b/protocol/orphan_manage.go
@@ -11,8 +11,8 @@ import (
1111 )
1212
1313 var (
14- orphanBlockTTL = 60 * time.Minute
15- orphanExpireWorkInterval = 3 * time.Minute
14+ orphanBlockTTL = 3 * time.Minute
15+ orphanExpireWorkInterval = 1 * time.Minute
1616 numOrphanBlockLimit = 256
1717 )
1818
@@ -58,8 +58,8 @@ func (o *OrphanManage) Add(block *types.Block) {
5858 }
5959
6060 if len(o.orphan) >= numOrphanBlockLimit {
61+ o.deleteLRU()
6162 log.WithFields(log.Fields{"module": logModule, "hash": blockHash.String(), "height": block.Height}).Info("the number of orphan blocks exceeds the limit")
62- return
6363 }
6464
6565 o.orphan[blockHash] = &orphanBlock{block, time.Now().Add(orphanBlockTTL)}
@@ -105,13 +105,27 @@ func (o *OrphanManage) delete(hash *bc.Hash) {
105105 }
106106
107107 for i, preOrphan := range prevOrphans {
108- if preOrphan == hash {
108+ if *preOrphan == *hash {
109109 o.prevOrphans[block.Block.PreviousBlockHash] = append(prevOrphans[:i], prevOrphans[i+1:]...)
110110 return
111111 }
112112 }
113113 }
114114
115+func (o *OrphanManage) deleteLRU() {
116+ var deleteBlock *orphanBlock
117+ for _, orphan := range o.orphan {
118+ if deleteBlock == nil || orphan.expiration.Before(deleteBlock.expiration) {
119+ deleteBlock = orphan
120+ }
121+ }
122+
123+ if deleteBlock != nil {
124+ blockHash := deleteBlock.Block.Hash()
125+ o.delete(&blockHash)
126+ }
127+}
128+
115129 func (o *OrphanManage) orphanExpireWorker() {
116130 ticker := time.NewTicker(orphanExpireWorkInterval)
117131 for now := range ticker.C {
--- a/protocol/orphan_manage_test.go
+++ b/protocol/orphan_manage_test.go
@@ -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]: &orphanBlock{testBlocks[0], now},
45+ },
46+ prevOrphans: map[bc.Hash][]*bc.Hash{
47+ bc.Hash{V0: 1}: []*bc.Hash{&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]: &orphanBlock{testBlocks[0], now.Add(2)},
69+ blockHashes[1]: &orphanBlock{testBlocks[1], now.Add(1)},
70+ },
71+ prevOrphans: map[bc.Hash][]*bc.Hash{
72+ bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0], &blockHashes[1]},
73+ },
74+ },
75+ after: &OrphanManage{
76+ orphan: map[bc.Hash]*orphanBlock{
77+ blockHashes[0]: &orphanBlock{testBlocks[0], now.Add(2)},
78+ },
79+ prevOrphans: map[bc.Hash][]*bc.Hash{
80+ bc.Hash{V0: 1}: []*bc.Hash{&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
Show on old repository browser