diff options
author | Mission Liao <mission.liao@dexon.org> | 2019-04-02 22:04:28 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-15 22:09:55 +0800 |
commit | 2cc07197e4932c3201608026ebed43c4a4e79858 (patch) | |
tree | df05d8ca15536e7470c2e5037a653a5455849104 /dex/cache.go | |
parent | 7538cbfe0646688503abbe32c3b45700ed27ae4f (diff) | |
download | go-tangerine-2cc07197e4932c3201608026ebed43c4a4e79858.tar go-tangerine-2cc07197e4932c3201608026ebed43c4a4e79858.tar.gz go-tangerine-2cc07197e4932c3201608026ebed43c4a4e79858.tar.bz2 go-tangerine-2cc07197e4932c3201608026ebed43c4a4e79858.tar.lz go-tangerine-2cc07197e4932c3201608026ebed43c4a4e79858.tar.xz go-tangerine-2cc07197e4932c3201608026ebed43c4a4e79858.tar.zst go-tangerine-2cc07197e4932c3201608026ebed43c4a4e79858.zip |
dex: try to reduce the chance to pull non-finalized blocks (#327)
* Send non-finalized blocks to notary set only
* Update randomness field for blocks in cache upon receiving agreement
result
* Filter AgreementResult by its position
* Avoid overwriting finalized blocks with non-finalized ones
* Add blocks to finalized cache when pulling
* Update to finalized cache when we have corresponding element in
non-finalized one.
Diffstat (limited to 'dex/cache.go')
-rw-r--r-- | dex/cache.go | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/dex/cache.go b/dex/cache.go index 951657fae..dbd4b7b5d 100644 --- a/dex/cache.go +++ b/dex/cache.go @@ -95,9 +95,30 @@ func (c *cache) votes(pos coreTypes.Position) []*coreTypes.Vote { return votes } +func (c *cache) addBlocks(blocks []*coreTypes.Block) { + c.lock.Lock() + defer c.lock.Unlock() + for _, b := range blocks { + if b.IsFinalized() { + c.addFinalizedBlockNoLock(b) + } else { + c.addBlockNoLock(b) + } + } +} + func (c *cache) addBlock(block *coreTypes.Block) { c.lock.Lock() defer c.lock.Unlock() + c.addBlockNoLock(block) +} + +func (c *cache) addBlockNoLock(block *coreTypes.Block) { + // Avoid polluting cache by non-finalized blocks when we've received some + // finalized block from the same position. + if _, exist := c.finalizedBlockCache[block.Position]; exist { + return + } block = block.Clone() if len(c.blockCache) >= c.size { // Randomly delete one entry. @@ -112,6 +133,10 @@ func (c *cache) addBlock(block *coreTypes.Block) { func (c *cache) addFinalizedBlock(block *coreTypes.Block) { c.lock.Lock() defer c.lock.Unlock() + c.addFinalizedBlockNoLock(block) +} + +func (c *cache) addFinalizedBlockNoLock(block *coreTypes.Block) { block = block.Clone() if len(c.blockCache) >= c.size { // Randomly delete one entry. @@ -131,14 +156,14 @@ func (c *cache) addFinalizedBlock(block *coreTypes.Block) { c.finalizedBlockCache[block.Position] = block } -func (c *cache) blocks(hashes coreCommon.Hashes) []*coreTypes.Block { +func (c *cache) blocks(hashes coreCommon.Hashes, includeDB bool) []*coreTypes.Block { c.lock.RLock() defer c.lock.RUnlock() cacheBlocks := make([]*coreTypes.Block, 0, len(hashes)) for _, hash := range hashes { if block, exist := c.blockCache[hash]; exist { cacheBlocks = append(cacheBlocks, block) - } else { + } else if includeDB { block, err := c.db.GetBlock(hash) if err != nil { continue |