move PeerList from protocol to peer, add debug logs (temporary)

This commit is contained in:
zelig 2015-01-03 14:00:04 +00:00
parent 6d848d0e0e
commit 8d77976946
2 changed files with 30 additions and 27 deletions

View file

@ -11,6 +11,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
) )
@ -45,7 +46,7 @@ func (d peerAddr) String() string {
return fmt.Sprintf("%v:%d", d.IP, d.Port) 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} return []interface{}{d.IP, d.Port, d.Pubkey}
} }
@ -460,3 +461,26 @@ func (r *eofSignal) Read(buf []byte) (int, error) {
} }
return n, err 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
}

View file

@ -2,9 +2,8 @@ package p2p
import ( import (
"bytes" "bytes"
"fmt"
"time" "time"
"github.com/ethereum/go-ethereum/ethutil"
) )
// Protocol represents a P2P subprotocol implementation. // Protocol represents a P2P subprotocol implementation.
@ -166,7 +165,9 @@ func (bp *baseProtocol) handle(rw MsgReadWriter) error {
case pongMsg: case pongMsg:
case getPeersMsg: 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_ // this is dangerous. the spec says that we should _delay_
// sending the response if no new information is available. // sending the response if no new information is available.
// this means that would need to send a response later when // this means that would need to send a response later when
@ -180,7 +181,7 @@ func (bp *baseProtocol) handle(rw MsgReadWriter) error {
case peersMsg: case peersMsg:
var peers []*peerAddr var peers []*peerAddr
if err := msg.Decode(&peers); err != nil { if err := msg.Decode(&peers); err != nil {
return err return newPeerError(errInvalidMsg, "msg %v : %v", msg, err)
} }
for _, addr := range peers { for _, addr := range peers {
bp.peer.Debugf("received peer suggestion: %v", addr) bp.peer.Debugf("received peer suggestion: %v", addr)
@ -270,25 +271,3 @@ func (bp *baseProtocol) handshakeMsg() Msg {
bp.peer.ourID.Pubkey()[1:], 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
}