aboutsummaryrefslogtreecommitdiffstats
path: root/eth/peer.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-07-25 20:14:14 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-07-25 20:14:14 +0800
commit1dd272080dfb49a07a87c46e18d8aeaa0fd41a08 (patch)
treeec47d1ede87ac09607689277e0b90f40712556fe /eth/peer.go
parent771655e3fee585ce4bc47dfaa279557c6c1c2421 (diff)
downloaddexon-1dd272080dfb49a07a87c46e18d8aeaa0fd41a08.tar
dexon-1dd272080dfb49a07a87c46e18d8aeaa0fd41a08.tar.gz
dexon-1dd272080dfb49a07a87c46e18d8aeaa0fd41a08.tar.bz2
dexon-1dd272080dfb49a07a87c46e18d8aeaa0fd41a08.tar.lz
dexon-1dd272080dfb49a07a87c46e18d8aeaa0fd41a08.tar.xz
dexon-1dd272080dfb49a07a87c46e18d8aeaa0fd41a08.tar.zst
dexon-1dd272080dfb49a07a87c46e18d8aeaa0fd41a08.zip
eth, eth/downloader: better remote head tracking
Diffstat (limited to 'eth/peer.go')
-rw-r--r--eth/peer.go34
1 files changed, 11 insertions, 23 deletions
diff --git a/eth/peer.go b/eth/peer.go
index c8c207ecb..aa85631ea 100644
--- a/eth/peer.go
+++ b/eth/peer.go
@@ -84,43 +84,31 @@ func newPeer(version int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
// Info gathers and returns a collection of metadata known about a peer.
func (p *peer) Info() *PeerInfo {
+ hash, td := p.Head()
+
return &PeerInfo{
Version: p.version,
- Difficulty: p.Td(),
- Head: fmt.Sprintf("%x", p.Head()),
+ Difficulty: td,
+ Head: hash.Hex(),
}
}
-// Head retrieves a copy of the current head (most recent) hash of the peer.
-func (p *peer) Head() (hash common.Hash) {
+// Head retrieves a copy of the current head hash and total difficulty of the
+// peer.
+func (p *peer) Head() (hash common.Hash, td *big.Int) {
p.lock.RLock()
defer p.lock.RUnlock()
copy(hash[:], p.head[:])
- return hash
+ return hash, new(big.Int).Set(p.td)
}
-// SetHead updates the head (most recent) hash of the peer.
-func (p *peer) SetHead(hash common.Hash) {
+// SetHead updates the head hash and total difficulty of the peer.
+func (p *peer) SetHead(hash common.Hash, td *big.Int) {
p.lock.Lock()
defer p.lock.Unlock()
copy(p.head[:], hash[:])
-}
-
-// Td retrieves the current total difficulty of a peer.
-func (p *peer) Td() *big.Int {
- p.lock.RLock()
- defer p.lock.RUnlock()
-
- return new(big.Int).Set(p.td)
-}
-
-// SetTd updates the current total difficulty of a peer.
-func (p *peer) SetTd(td *big.Int) {
- p.lock.Lock()
- defer p.lock.Unlock()
-
p.td.Set(td)
}
@@ -411,7 +399,7 @@ func (ps *peerSet) BestPeer() *peer {
bestTd *big.Int
)
for _, p := range ps.peers {
- if td := p.Td(); bestPeer == nil || td.Cmp(bestTd) > 0 {
+ if _, td := p.Head(); bestPeer == nil || td.Cmp(bestTd) > 0 {
bestPeer, bestTd = p, td
}
}