Browse Source

fix merge

pull/456/head
Anton Kaliaev 8 years ago
parent
commit
0277e52bd5
No known key found for this signature in database GPG Key ID: 7B6881D965918214
4 changed files with 20 additions and 18 deletions
  1. +1
    -1
      addrbook.go
  2. +2
    -5
      addrbook_test.go
  3. +12
    -9
      pex_reactor.go
  4. +5
    -3
      pex_reactor_test.go

+ 1
- 1
addrbook.go View File

@ -15,6 +15,7 @@ import (
"time"
. "github.com/tendermint/go-common"
crypto "github.com/tendermint/go-crypto"
)
const (
@ -135,7 +136,6 @@ func (a *AddrBook) OnStop() {
}
func (a *AddrBook) Wait() {
a.wg.Wait()
a.saveToFile(a.filePath)
}


+ 2
- 5
addrbook_test.go View File

@ -9,9 +9,6 @@ import (
"github.com/stretchr/testify/assert"
)
"github.com/stretchr/testify/assert"
)
func createTempFileName(prefix string) string {
f, err := ioutil.TempFile("", prefix)
if err != nil {
@ -156,14 +153,14 @@ func TestAddrBookRemoveAddress(t *testing.T) {
fname := createTempFileName("addrbook_test")
book := NewAddrBook(fname, true)
addr := randIPv4Address()
addr := randIPv4Address(t)
book.AddAddress(addr, addr)
assert.Equal(t, 1, book.Size())
book.RemoveAddress(addr)
assert.Equal(t, 0, book.Size())
nonExistingAddr := randIPv4Address()
nonExistingAddr := randIPv4Address(t)
book.RemoveAddress(nonExistingAddr)
assert.Equal(t, 0, book.Size())
}

+ 12
- 9
pex_reactor.go View File

@ -93,19 +93,17 @@ func (r *PEXReactor) GetChannels() []*ChannelDescriptor {
// AddPeer implements Reactor by adding peer to the address book (if inbound)
// or by requesting more addresses (if outbound).
func (r *PEXReactor) AddPeer(p *Peer) {
netAddr, err := NewNetAddressString(p.ListenAddr)
if err != nil {
// this should never happen
log.Error("Error in AddPeer: invalid peer address", "addr", p.ListenAddr, "error", err)
return
}
if p.IsOutbound() { // For outbound peers, the address is already in the books
if r.book.NeedMoreAddrs() {
r.RequestPEX(p)
}
} else { // For inbound connections, the peer is its own source
addr := NewNetAddressString(p.ListenAddr)
addr, err := NewNetAddressString(p.ListenAddr)
if err != nil {
// this should never happen
log.Error("Error in AddPeer: invalid peer address", "addr", p.ListenAddr, "error", err)
return
}
r.book.AddAddress(addr, addr)
}
}
@ -117,7 +115,12 @@ func (r *PEXReactor) AddPeer(p *Peer) {
// will remove him too. The peer will need to send first requests to others by
// himself (he will have an addrbook or the seeds).
func (r *PEXReactor) RemovePeer(p *Peer, reason interface{}) {
addr := NewNetAddressString(p.ListenAddr)
addr, err := NewNetAddressString(p.ListenAddr)
if err != nil {
// this should never happen
log.Error("Error in AddPeer: invalid peer address", "addr", p.ListenAddr, "error", err)
return
}
r.book.RemoveAddress(addr)
}


+ 5
- 3
pex_reactor_test.go View File

@ -72,7 +72,7 @@ func TestPEXReactorRunning(t *testing.T) {
// fill the address book and add listeners
for _, s := range switches {
addr := NewNetAddressString(s.NodeInfo().ListenAddr)
addr, _ := NewNetAddressString(s.NodeInfo().ListenAddr)
book.AddAddress(addr, addr)
s.AddListener(NewDefaultListener("tcp", s.NodeInfo().ListenAddr, true))
}
@ -110,7 +110,8 @@ func TestPEXReactorReceive(t *testing.T) {
peer := createRandomPeer(false)
size := book.Size()
addrs := []*NetAddress{NewNetAddressString(peer.ListenAddr)}
netAddr, _ := NewNetAddressString(peer.ListenAddr)
addrs := []*NetAddress{netAddr}
msg := wire.BinaryBytes(struct{ PexMessage }{&pexAddrsMessage{Addrs: addrs}})
r.Receive(PexChannel, peer, msg)
assert.Equal(t, size+1, book.Size())
@ -140,12 +141,13 @@ func TestPEXReactorAbuseFromPeer(t *testing.T) {
func createRandomPeer(outbound bool) *Peer {
addr := cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256)
netAddr, _ := NewNetAddressString(addr)
return &Peer{
Key: cmn.RandStr(12),
NodeInfo: &NodeInfo{
ListenAddr: addr,
},
outbound: outbound,
mconn: &MConnection{RemoteAddress: NewNetAddressString(addr)},
mconn: &MConnection{RemoteAddress: netAddr},
}
}

Loading…
Cancel
Save