mirror of
https://github.com/btcsuite/btcd.git
synced 2025-02-24 06:47:59 +01:00
Limit the number of addresses announced.
Additionally, add the addresses being sent to the peer to the known addresses map.
This commit is contained in:
parent
04d47de262
commit
e5a825324c
1 changed files with 15 additions and 14 deletions
27
peer.go
27
peer.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"container/list"
|
"container/list"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
prand "math/rand"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -1131,11 +1132,19 @@ func (p *peer) pushAddrMsg(addresses []*btcwire.NetAddress) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r := prand.New(prand.NewSource(time.Now().UnixNano()))
|
||||||
numAdded := 0
|
numAdded := 0
|
||||||
msg := btcwire.NewMsgAddr()
|
msg := btcwire.NewMsgAddr()
|
||||||
for _, na := range addresses {
|
for _, na := range addresses {
|
||||||
// Filter addresses the peer already knows about.
|
// Filter addresses the peer already knows about.
|
||||||
if _, ok := p.knownAddresses[addrmgr.NetAddressKey(na)]; ok {
|
if _, exists := p.knownAddresses[addrmgr.NetAddressKey(na)]; exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the maxAddrs limit has been reached, randomize the list
|
||||||
|
// with the remaining addresses.
|
||||||
|
if numAdded == btcwire.MaxAddrPerMsg {
|
||||||
|
msg.AddrList[r.Intn(btcwire.MaxAddrPerMsg)] = na
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1145,20 +1154,13 @@ func (p *peer) pushAddrMsg(addresses []*btcwire.NetAddress) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
numAdded++
|
numAdded++
|
||||||
|
|
||||||
// Split into multiple messages as needed.
|
|
||||||
if numAdded > 0 && numAdded%btcwire.MaxAddrPerMsg == 0 {
|
|
||||||
p.QueueMessage(msg, nil)
|
|
||||||
|
|
||||||
// NOTE: This needs to be a new address message and not
|
|
||||||
// simply call ClearAddresses since the message is a
|
|
||||||
// pointer and queueing it does not make a copy.
|
|
||||||
msg = btcwire.NewMsgAddr()
|
|
||||||
}
|
}
|
||||||
|
if numAdded > 0 {
|
||||||
|
for _, na := range msg.AddrList {
|
||||||
|
// Add address to known addresses for this peer.
|
||||||
|
p.knownAddresses[addrmgr.NetAddressKey(na)] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send message with remaining addresses if needed.
|
|
||||||
if numAdded%btcwire.MaxAddrPerMsg != 0 {
|
|
||||||
p.QueueMessage(msg, nil)
|
p.QueueMessage(msg, nil)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -1729,7 +1731,6 @@ out:
|
||||||
// Should get us block, tx, or not found.
|
// Should get us block, tx, or not found.
|
||||||
case *btcwire.MsgGetHeaders:
|
case *btcwire.MsgGetHeaders:
|
||||||
// Should get us headers back.
|
// Should get us headers back.
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Not one of the above, no sure reply.
|
// Not one of the above, no sure reply.
|
||||||
// We want to ping if nothing else
|
// We want to ping if nothing else
|
||||||
|
|
Loading…
Add table
Reference in a new issue