From 205378016fc342a9ee683b3d040b3e7d0e2ebb51 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 16 Apr 2015 02:16:33 +0200 Subject: [PATCH] downloader: added demotion / promotion in prep. for rep. system --- eth/downloader/downloader.go | 5 +++++ eth/downloader/peer.go | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 56671dc2e..91cc65249 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -245,6 +245,7 @@ out: for { select { case blockPack := <-d.blockCh: + d.peers[blockPack.peerId].promote() d.queue.deliver(blockPack.peerId, blockPack.blocks) d.peers.setState(blockPack.peerId, idleState) case <-ticker.C: @@ -310,6 +311,9 @@ out: // 2) Measure their speed; // 3) Amount and availability. d.queue.deliver(pid, nil) + if peer := p.peers[pid]; peer != nil { + peer.demote() + } } } @@ -343,6 +347,7 @@ func (d *Downloader) AddBlock(id string, block *types.Block, td *big.Int) { peer.td = td peer.recentHash = block.Hash() peer.mu.Unlock() + peer.promote() glog.V(logger.Detail).Infoln("Inserting new block from:", id) d.queue.addBlock(id, block, td) diff --git a/eth/downloader/peer.go b/eth/downloader/peer.go index f66e5afd8..4cd306a05 100644 --- a/eth/downloader/peer.go +++ b/eth/downloader/peer.go @@ -95,3 +95,23 @@ func (p *peer) fetch(chunk *chunk) error { return nil } + +// promote increases the peer's reputation +func (p *peer) promote() { + p.mu.Lock() + defer p.mu.Unlock() + + p.rep++ +} + +// demote decreases the peer's reputation or leaves it at 0 +func (p *peer) demote() { + p.mu.Lock() + defer p.mu.Unlock() + + if p.rep > 1 { + p.rep -= 2 + } else { + p.rep = 0 + } +}