diff options
author | Haoping Ku <haoping.ku@dexon.org> | 2018-08-10 17:57:28 +0800 |
---|---|---|
committer | missionliao <38416648+missionliao@users.noreply.github.com> | 2018-08-10 17:57:28 +0800 |
commit | 08c208c21f93d55bf3275610cbaf3ee6a545956a (patch) | |
tree | 83a0201040196c0fb953c6907edc799fee4a82d6 | |
parent | f4690458abc24a0a3877f4facb0947c31d29e8cb (diff) | |
download | dexon-consensus-08c208c21f93d55bf3275610cbaf3ee6a545956a.tar dexon-consensus-08c208c21f93d55bf3275610cbaf3ee6a545956a.tar.gz dexon-consensus-08c208c21f93d55bf3275610cbaf3ee6a545956a.tar.bz2 dexon-consensus-08c208c21f93d55bf3275610cbaf3ee6a545956a.tar.lz dexon-consensus-08c208c21f93d55bf3275610cbaf3ee6a545956a.tar.xz dexon-consensus-08c208c21f93d55bf3275610cbaf3ee6a545956a.tar.zst dexon-consensus-08c208c21f93d55bf3275610cbaf3ee6a545956a.zip |
core: reliable-broadcast: delete old blocks (#47)
Delete all blocks in received blocks array for avoiding using too
much memory space.
-rw-r--r-- | core/reliable-broadcast.go | 15 | ||||
-rw-r--r-- | core/types/block.go | 1 |
2 files changed, 13 insertions, 3 deletions
diff --git a/core/reliable-broadcast.go b/core/reliable-broadcast.go index 418ace6..904031d 100644 --- a/core/reliable-broadcast.go +++ b/core/reliable-broadcast.go @@ -140,8 +140,9 @@ func (rb *reliableBroadcast) processBlock(block *types.Block) (err error) { return } rb.blocks[block.Hash] = block - block.AckedValidators = make(map[types.ValidatorID]struct{}) rb.receivedBlocks[block.Hash] = block + block.AckedValidators = make(map[types.ValidatorID]struct{}) + block.ReceivedTime = time.Now() // Check blocks in receivedBlocks if its acks are all in lattice. If a block's // acking blocks are all in lattice, execute sanity check and add the block @@ -206,8 +207,16 @@ func (rb *reliableBroadcast) processBlock(block *types.Block) (err error) { b.Status = types.BlockStatusAcked } - // TODO(haoping): delete blocks in received array when it is received a long - // time ago + // Delete blocks in received array when it is received a long time ago. + oldBlocks := []common.Hash{} + for h, b := range rb.receivedBlocks { + if time.Now().Sub(b.ReceivedTime) >= 30*time.Second { + oldBlocks = append(oldBlocks, h) + } + } + for _, h := range oldBlocks { + delete(rb.receivedBlocks, h) + } // Delete old blocks in "lattice" and "blocks" for release memory space. // First, find the height that blocks below it can be deleted. This height diff --git a/core/types/block.go b/core/types/block.go index 9fd483e..4b8398c 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -71,6 +71,7 @@ type Block struct { Ackeds map[common.Hash]struct{} `json:"-"` AckedValidators map[ValidatorID]struct{} `json:"-"` Status Status `json:"-"` + ReceivedTime time.Time `json:"-"` } // Block implements BlockConverter interface. |