mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
move PeerList from protocol to peer, add debug logs (temporary)
This commit is contained in:
parent
6d848d0e0e
commit
8d77976946
2 changed files with 30 additions and 27 deletions
26
p2p/peer.go
26
p2p/peer.go
|
|
@ -11,6 +11,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/ethutil"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
)
|
||||
|
|
@ -45,7 +46,7 @@ func (d peerAddr) String() string {
|
|||
return fmt.Sprintf("%v:%d", d.IP, d.Port)
|
||||
}
|
||||
|
||||
func (d peerAddr) RlpData() interface{} {
|
||||
func (d *peerAddr) RlpData() interface{} {
|
||||
return []interface{}{d.IP, d.Port, d.Pubkey}
|
||||
}
|
||||
|
||||
|
|
@ -460,3 +461,26 @@ func (r *eofSignal) Read(buf []byte) (int, error) {
|
|||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (peer *Peer) PeerList() []ethutil.RlpEncodable {
|
||||
peers := peer.otherPeers()
|
||||
ds := make([]ethutil.RlpEncodable, 0, len(peers))
|
||||
for _, p := range peers {
|
||||
p.infolock.Lock()
|
||||
addr := p.listenAddr
|
||||
p.infolock.Unlock()
|
||||
// filter out this peer and peers that are not listening or
|
||||
// have not completed the handshake.
|
||||
// TODO: track previously sent peers and exclude them as well.
|
||||
if p == peer || addr == nil {
|
||||
continue
|
||||
}
|
||||
ds = append(ds, addr)
|
||||
}
|
||||
ourAddr := peer.ourListenAddr
|
||||
if ourAddr != nil && !ourAddr.IP.IsLoopback() && !ourAddr.IP.IsUnspecified() {
|
||||
ds = append(ds, ourAddr)
|
||||
}
|
||||
fmt.Printf("address length: %v\n", len(ds))
|
||||
return ds
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,8 @@ package p2p
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/ethutil"
|
||||
)
|
||||
|
||||
// Protocol represents a P2P subprotocol implementation.
|
||||
|
|
@ -166,7 +165,9 @@ func (bp *baseProtocol) handle(rw MsgReadWriter) error {
|
|||
case pongMsg:
|
||||
|
||||
case getPeersMsg:
|
||||
peers := bp.peerList()
|
||||
peers := bp.peer.PeerList()
|
||||
fmt.Printf("get Peers Msg: peers length:%v\n", len(peers))
|
||||
|
||||
// this is dangerous. the spec says that we should _delay_
|
||||
// sending the response if no new information is available.
|
||||
// this means that would need to send a response later when
|
||||
|
|
@ -180,7 +181,7 @@ func (bp *baseProtocol) handle(rw MsgReadWriter) error {
|
|||
case peersMsg:
|
||||
var peers []*peerAddr
|
||||
if err := msg.Decode(&peers); err != nil {
|
||||
return err
|
||||
return newPeerError(errInvalidMsg, "msg %v : %v", msg, err)
|
||||
}
|
||||
for _, addr := range peers {
|
||||
bp.peer.Debugf("received peer suggestion: %v", addr)
|
||||
|
|
@ -270,25 +271,3 @@ func (bp *baseProtocol) handshakeMsg() Msg {
|
|||
bp.peer.ourID.Pubkey()[1:],
|
||||
)
|
||||
}
|
||||
|
||||
func (bp *baseProtocol) peerList() []ethutil.RlpEncodable {
|
||||
peers := bp.peer.otherPeers()
|
||||
ds := make([]ethutil.RlpEncodable, 0, len(peers))
|
||||
for _, p := range peers {
|
||||
p.infolock.Lock()
|
||||
addr := p.listenAddr
|
||||
p.infolock.Unlock()
|
||||
// filter out this peer and peers that are not listening or
|
||||
// have not completed the handshake.
|
||||
// TODO: track previously sent peers and exclude them as well.
|
||||
if p == bp.peer || addr == nil {
|
||||
continue
|
||||
}
|
||||
ds = append(ds, addr)
|
||||
}
|
||||
ourAddr := bp.peer.ourListenAddr
|
||||
if ourAddr != nil && !ourAddr.IP.IsLoopback() && !ourAddr.IP.IsUnspecified() {
|
||||
ds = append(ds, ourAddr)
|
||||
}
|
||||
return ds
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue