mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-19 01:51:19 +00:00
some fixes from review
This commit is contained in:
parent
9670519a21
commit
e4d52401cf
@ -190,7 +190,7 @@ func randIPv4Address(t *testing.T) *NetAddress {
|
|||||||
)
|
)
|
||||||
port := rand.Intn(65535-1) + 1
|
port := rand.Intn(65535-1) + 1
|
||||||
addr, err := NewNetAddressString(fmt.Sprintf("%v:%v", ip, port))
|
addr, err := NewNetAddressString(fmt.Sprintf("%v:%v", ip, port))
|
||||||
addr.ID = ID(hex.EncodeToString(cmn.RandBytes(20))) // TODO
|
addr.ID = ID(hex.EncodeToString(cmn.RandBytes(20)))
|
||||||
assert.Nil(t, err, "error generating rand network address")
|
assert.Nil(t, err, "error generating rand network address")
|
||||||
if addr.Routable() {
|
if addr.Routable() {
|
||||||
return addr
|
return addr
|
||||||
|
@ -11,8 +11,13 @@ import (
|
|||||||
cmn "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ID is a hex-encoded crypto.Address
|
||||||
type ID string
|
type ID string
|
||||||
|
|
||||||
|
// IDByteLength is the length of a crypto.Address. Currently only 20.
|
||||||
|
// TODO: support other length addresses ?
|
||||||
|
const IDByteLength = 20
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Persistent peer ID
|
// Persistent peer ID
|
||||||
// TODO: encrypt on disk
|
// TODO: encrypt on disk
|
||||||
|
@ -50,8 +50,8 @@ func NewNetAddress(id ID, addr net.Addr) *NetAddress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewNetAddressString returns a new NetAddress using the provided
|
// NewNetAddressString returns a new NetAddress using the provided
|
||||||
// address in the form of "IP:Port". Also resolves the host if host
|
// address in the form of "ID@IP:Port", where the ID is optional.
|
||||||
// is not an IP.
|
// Also resolves the host if host is not an IP.
|
||||||
func NewNetAddressString(addr string) (*NetAddress, error) {
|
func NewNetAddressString(addr string) (*NetAddress, error) {
|
||||||
addr = removeProtocolIfDefined(addr)
|
addr = removeProtocolIfDefined(addr)
|
||||||
|
|
||||||
@ -63,8 +63,9 @@ func NewNetAddressString(addr string) (*NetAddress, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, fmt.Sprintf("Address (%s) contains invalid ID", addr))
|
return nil, errors.Wrap(err, fmt.Sprintf("Address (%s) contains invalid ID", addr))
|
||||||
}
|
}
|
||||||
if len(idBytes) != 20 {
|
if len(idBytes) != IDByteLength {
|
||||||
return nil, fmt.Errorf("Address (%s) contains ID of invalid length (%d). Should be 20 hex-encoded bytes", len(idBytes))
|
return nil, fmt.Errorf("Address (%s) contains ID of invalid length (%d). Should be %d hex-encoded bytes",
|
||||||
|
addr, len(idBytes), IDByteLength)
|
||||||
}
|
}
|
||||||
id, addr = ID(idStr), spl[1]
|
id, addr = ID(idStr), spl[1]
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,13 @@ func TestNewNetAddressString(t *testing.T) {
|
|||||||
{"tcp://this-isnot-hex@127.0.0.1:8080", "", false},
|
{"tcp://this-isnot-hex@127.0.0.1:8080", "", false},
|
||||||
{"tcp://xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
|
{"tcp://xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
|
||||||
{"tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
|
{"tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
|
||||||
|
|
||||||
|
{"tcp://@127.0.0.1:8080", "", false},
|
||||||
|
{"tcp://@", "", false},
|
||||||
|
{"", "", false},
|
||||||
|
{"@", "", false},
|
||||||
|
{" @", "", false},
|
||||||
|
{" @ ", "", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
@ -109,19 +109,20 @@ func (r *PEXReactor) GetChannels() []*ChannelDescriptor {
|
|||||||
func (r *PEXReactor) AddPeer(p Peer) {
|
func (r *PEXReactor) AddPeer(p Peer) {
|
||||||
if p.IsOutbound() {
|
if p.IsOutbound() {
|
||||||
// For outbound peers, the address is already in the books.
|
// For outbound peers, the address is already in the books.
|
||||||
// Either it was added in DialPersistentPeers or when we
|
// Either it was added in DialPeersAsync or when we
|
||||||
// received the peer's address in r.Receive
|
// received the peer's address in r.Receive
|
||||||
if r.book.NeedMoreAddrs() {
|
if r.book.NeedMoreAddrs() {
|
||||||
r.RequestPEX(p)
|
r.RequestPEX(p)
|
||||||
}
|
}
|
||||||
} else { // For inbound connections, the peer is its own source
|
} else {
|
||||||
addr, err := NewNetAddressString(p.NodeInfo().ListenAddr)
|
addrStr := fmt.Sprintf("%s@%s", p.ID(), p.NodeInfo().ListenAddr)
|
||||||
addr.ID = p.ID() // TODO: handle in NewNetAddress func
|
addr, err := NewNetAddressString(addrStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// peer gave us a bad ListenAddr. TODO: punish
|
// peer gave us a bad ListenAddr. TODO: punish
|
||||||
r.Logger.Error("Error in AddPeer: invalid peer address", "addr", p.NodeInfo().ListenAddr, "err", err)
|
r.Logger.Error("Error in AddPeer: invalid peer address", "addr", p.NodeInfo().ListenAddr, "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// For inbound connections, the peer is its own source
|
||||||
r.book.AddAddress(addr, addr)
|
r.book.AddAddress(addr, addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,6 +94,7 @@ type Switch struct {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
ErrSwitchDuplicatePeer = errors.New("Duplicate peer")
|
ErrSwitchDuplicatePeer = errors.New("Duplicate peer")
|
||||||
|
ErrSwitchConnectToSelf = errors.New("Connect to self")
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewSwitch(config *cfg.P2PConfig) *Switch {
|
func NewSwitch(config *cfg.P2PConfig) *Switch {
|
||||||
@ -241,7 +242,7 @@ func (sw *Switch) OnStop() {
|
|||||||
func (sw *Switch) addPeer(peer *peer) error {
|
func (sw *Switch) addPeer(peer *peer) error {
|
||||||
// Avoid self
|
// Avoid self
|
||||||
if sw.nodeKey.ID() == peer.ID() {
|
if sw.nodeKey.ID() == peer.ID() {
|
||||||
return errors.New("Ignoring connection from self")
|
return ErrSwitchConnectToSelf
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter peer against white list
|
// Filter peer against white list
|
||||||
|
Loading…
x
Reference in New Issue
Block a user