aboutsummaryrefslogtreecommitdiffstats
path: root/blockpool/peers.go
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2015-03-20 06:53:15 +0800
committerzelig <viktor.tron@gmail.com>2015-03-20 18:41:40 +0800
commit391e89d70a43b4a2153db8acac9a6af7a4f76adf (patch)
treea2412ea8fcac7f7d52e727675f7cf3a0daa4fc4c /blockpool/peers.go
parent50661f0e683b4975894a0e8fe16024724adef72d (diff)
downloaddexon-391e89d70a43b4a2153db8acac9a6af7a4f76adf.tar.gz
dexon-391e89d70a43b4a2153db8acac9a6af7a4f76adf.tar.zst
dexon-391e89d70a43b4a2153db8acac9a6af7a4f76adf.zip
use own total difficulty to limit best peer
- update blockpool td by subscribing to ChainHeadEvent - if ahead of best peer, demote it - addPeer now take own td as current td - removePeer now take own td as current td - add relevant tests to peers_test - eth: backend now calls blockpool with eth.eventMux and chainManager.Td
Diffstat (limited to 'blockpool/peers.go')
-rw-r--r--blockpool/peers.go15
1 files changed, 6 insertions, 9 deletions
diff --git a/blockpool/peers.go b/blockpool/peers.go
index 81bab31e7..41782983c 100644
--- a/blockpool/peers.go
+++ b/blockpool/peers.go
@@ -7,7 +7,6 @@ import (
"sync"
"time"
- "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/errs"
)
@@ -256,7 +255,7 @@ func (self *peers) addPeer(
}
best = true
} else {
- currentTD := common.Big0
+ currentTD := self.bp.getTD()
if self.best != nil {
currentTD = self.best.td
}
@@ -264,7 +263,7 @@ func (self *peers) addPeer(
self.status.lock.Lock()
self.status.bestPeers[p.id]++
self.status.lock.Unlock()
- plog.Debugf("addPeer: peer <%v> promoted best peer", id)
+ plog.Debugf("addPeer: peer <%v> (td: %v > current td %v) promoted best peer", id, td, currentTD)
self.bp.switchPeer(self.best, p)
self.best = p
best = true
@@ -275,10 +274,8 @@ func (self *peers) addPeer(
// removePeer is called (via RemovePeer) by the eth protocol when the peer disconnects
func (self *peers) removePeer(id string) {
- plog.Debugf("addPeer: remove peer 0 <%v>", id)
self.lock.Lock()
defer self.lock.Unlock()
- plog.Debugf("addPeer: remove peer 1 <%v>", id)
p, found := self.peers[id]
if !found {
@@ -286,13 +283,13 @@ func (self *peers) removePeer(id string) {
}
delete(self.peers, id)
- plog.Debugf("addPeer: remove peer <%v>", id)
+ plog.Debugf("addPeer: remove peer <%v> (td: %v)", id, p.td)
// if current best peer is removed, need to find a better one
if self.best == p {
var newp *peer
- // FIXME: own TD
- max := common.Big0
+ // only peers that are ahead of us are considered
+ max := self.bp.getTD()
// peer with the highest self-acclaimed TD is chosen
for _, pp := range self.peers {
if pp.td.Cmp(max) > 0 {
@@ -304,7 +301,7 @@ func (self *peers) removePeer(id string) {
self.status.lock.Lock()
self.status.bestPeers[p.id]++
self.status.lock.Unlock()
- plog.Debugf("addPeer: peer <%v> with td %v promoted best peer", newp.id, newp.td)
+ plog.Debugf("addPeer: peer <%v> (td: %v) promoted best peer", newp.id, newp.td)
} else {
plog.Warnln("addPeer: no suitable peers found")
}