mirror of
https://github.com/fluencelabs/tendermint
synced 2025-07-31 04:01:55 +00:00
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
*.swp
|
||||
*.swo
|
||||
*.bak
|
||||
.DS_Store
|
||||
vendor
|
@@ -5,6 +5,8 @@ import (
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const addrBookStrict = true
|
||||
@@ -38,7 +40,7 @@ func TestEmpty(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func randIPv4Address() *NetAddress {
|
||||
func randIPv4Address(t *testing.T) *NetAddress {
|
||||
for {
|
||||
ip := fmt.Sprintf("%v.%v.%v.%v",
|
||||
rand.Intn(254)+1,
|
||||
@@ -47,7 +49,8 @@ func randIPv4Address() *NetAddress {
|
||||
rand.Intn(255),
|
||||
)
|
||||
port := rand.Intn(65535-1) + 1
|
||||
addr := NewNetAddressString(fmt.Sprintf("%v:%v", ip, port))
|
||||
addr, err := NewNetAddressString(fmt.Sprintf("%v:%v", ip, port))
|
||||
assert.Nil(t, err, "error generating rand network address")
|
||||
if addr.Routable() {
|
||||
return addr
|
||||
}
|
||||
@@ -64,8 +67,8 @@ func TestSaveAddresses(t *testing.T) {
|
||||
src *NetAddress
|
||||
}{}
|
||||
for i := 0; i < 100; i++ {
|
||||
addr := randIPv4Address()
|
||||
src := randIPv4Address()
|
||||
addr := randIPv4Address(t)
|
||||
src := randIPv4Address(t)
|
||||
randAddrs = append(randAddrs, struct {
|
||||
addr *NetAddress
|
||||
src *NetAddress
|
||||
@@ -118,8 +121,8 @@ func TestPromoteToOld(t *testing.T) {
|
||||
src *NetAddress
|
||||
}{}
|
||||
for i := 0; i < 100; i++ {
|
||||
addr := randIPv4Address()
|
||||
src := randIPv4Address()
|
||||
addr := randIPv4Address(t)
|
||||
src := randIPv4Address(t)
|
||||
randAddrs = append(randAddrs, struct {
|
||||
addr *NetAddress
|
||||
src *NetAddress
|
||||
|
@@ -70,7 +70,11 @@ func NewDefaultListener(protocol string, lAddr string, skipUPNP bool) Listener {
|
||||
log.Info("Local listener", "ip", listenerIP, "port", listenerPort)
|
||||
|
||||
// Determine internal address...
|
||||
var intAddr *NetAddress = NewNetAddressString(lAddr)
|
||||
var intAddr *NetAddress
|
||||
intAddr, err = NewNetAddressString(lAddr)
|
||||
if err != nil {
|
||||
PanicCrisis(err)
|
||||
}
|
||||
|
||||
// Determine external address...
|
||||
var extAddr *NetAddress
|
||||
|
@@ -5,11 +5,12 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
. "github.com/tendermint/go-common"
|
||||
cmn "github.com/tendermint/go-common"
|
||||
)
|
||||
|
||||
type NetAddress struct {
|
||||
@@ -34,27 +35,43 @@ func NewNetAddress(addr net.Addr) *NetAddress {
|
||||
}
|
||||
|
||||
// Also resolves the host if host is not an IP.
|
||||
func NewNetAddressString(addr string) *NetAddress {
|
||||
func NewNetAddressString(addr string) (*NetAddress, error) {
|
||||
|
||||
host, portStr, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
PanicSanity(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
if len(host) > 0 {
|
||||
ips, err := net.LookupIP(host)
|
||||
if err != nil {
|
||||
PanicSanity(err)
|
||||
return nil, err
|
||||
}
|
||||
ip = ips[0]
|
||||
}
|
||||
}
|
||||
|
||||
port, err := strconv.ParseUint(portStr, 10, 16)
|
||||
if err != nil {
|
||||
PanicSanity(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
na := NewNetAddressIPPort(ip, uint16(port))
|
||||
return na
|
||||
return na, nil
|
||||
}
|
||||
|
||||
func NewNetAddressStrings(addrs []string) ([]*NetAddress, error) {
|
||||
netAddrs := make([]*NetAddress, len(addrs))
|
||||
for i, addr := range addrs {
|
||||
netAddr, err := NewNetAddressString(addr)
|
||||
if err != nil {
|
||||
return nil, errors.New(cmn.Fmt("Error in address %s: %v", addr, err))
|
||||
}
|
||||
netAddrs[i] = netAddr
|
||||
}
|
||||
return netAddrs, nil
|
||||
}
|
||||
|
||||
func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
|
||||
@@ -81,7 +98,7 @@ func (na *NetAddress) Less(other interface{}) bool {
|
||||
if o, ok := other.(*NetAddress); ok {
|
||||
return na.String() < o.String()
|
||||
} else {
|
||||
PanicSanity("Cannot compare unequal types")
|
||||
cmn.PanicSanity("Cannot compare unequal types")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@@ -64,7 +64,13 @@ func (pexR *PEXReactor) GetChannels() []*ChannelDescriptor {
|
||||
// Implements Reactor
|
||||
func (pexR *PEXReactor) AddPeer(peer *Peer) {
|
||||
// Add the peer to the address book
|
||||
netAddr := NewNetAddressString(peer.ListenAddr)
|
||||
netAddr, err := NewNetAddressString(peer.ListenAddr)
|
||||
if err != nil {
|
||||
// this should never happen
|
||||
log.Error("Error in AddPeer: invalid peer address", "addr", peer.ListenAddr, "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
if peer.IsOutbound() {
|
||||
if pexR.book.NeedMoreAddrs() {
|
||||
pexR.RequestPEX(peer)
|
||||
@@ -109,7 +115,6 @@ func (pexR *PEXReactor) Receive(chID byte, src *Peer, msgBytes []byte) {
|
||||
default:
|
||||
log.Warn(Fmt("Unknown message type %v", reflect.TypeOf(msg)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Asks peer for more addresses.
|
||||
|
16
switch.go
16
switch.go
@@ -296,20 +296,24 @@ func (sw *Switch) startInitPeer(peer *Peer) {
|
||||
sw.addPeerToReactors(peer) // run AddPeer on each reactor
|
||||
}
|
||||
|
||||
// Dial a list of seeds in random order
|
||||
// Spawns a go routine for each dial
|
||||
func (sw *Switch) DialSeeds(seeds []string) {
|
||||
// Dial a list of seeds asynchronously in random order
|
||||
func (sw *Switch) DialSeeds(seeds []string) error {
|
||||
|
||||
netAddrs, err := NewNetAddressStrings(seeds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// permute the list, dial them in random order.
|
||||
perm := rand.Perm(len(seeds))
|
||||
for i := 0; i < len(perm); i++ {
|
||||
go func(i int) {
|
||||
time.Sleep(time.Duration(rand.Int63n(3000)) * time.Millisecond)
|
||||
j := perm[i]
|
||||
addr := NewNetAddressString(seeds[j])
|
||||
|
||||
sw.dialSeed(addr)
|
||||
sw.dialSeed(netAddrs[j])
|
||||
}(i)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sw *Switch) dialSeed(addr *NetAddress) {
|
||||
|
@@ -1,3 +1,3 @@
|
||||
package p2p
|
||||
|
||||
const Version = "0.3.5" // minor fixes
|
||||
const Version = "0.4.0" // DialSeeds returns an error
|
||||
|
Reference in New Issue
Block a user