network: add getaddr message handling
Respond with known good addresses.
This commit is contained in:
parent
b2530a4c93
commit
b9ff4d929c
3 changed files with 34 additions and 5 deletions
|
@ -62,6 +62,14 @@ type AddressList struct {
|
|||
Addrs []*AddressAndTime
|
||||
}
|
||||
|
||||
// NewAddressList creates a list for n AddressAndTime elements.
|
||||
func NewAddressList(n int) *AddressList {
|
||||
alist := AddressList{
|
||||
Addrs: make([]*AddressAndTime, n),
|
||||
}
|
||||
return &alist
|
||||
}
|
||||
|
||||
// DecodeBinary implements the Payload interface.
|
||||
func (p *AddressList) DecodeBinary(r io.Reader) error {
|
||||
br := util.BinReader{R: r}
|
||||
|
|
|
@ -35,7 +35,7 @@ func TestEncodeDecodeAddress(t *testing.T) {
|
|||
|
||||
func TestEncodeDecodeAddressList(t *testing.T) {
|
||||
var lenList uint8 = 4
|
||||
addrList := &AddressList{make([]*AddressAndTime, lenList)}
|
||||
addrList := NewAddressList(int(lenList))
|
||||
for i := 0; i < int(lenList); i++ {
|
||||
e, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:200%d", i))
|
||||
addrList.Addrs[i] = NewAddressAndTime(e, time.Now())
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -19,6 +20,7 @@ const (
|
|||
minPeers = 5
|
||||
maxPeers = 20
|
||||
maxBlockBatch = 200
|
||||
maxAddrsToSend = 200
|
||||
minPoolCount = 30
|
||||
)
|
||||
|
||||
|
@ -294,6 +296,22 @@ func (s *Server) handleAddrCmd(p Peer, addrs *payload.AddressList) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// handleGetAddrCmd sends to the peer some good addresses that we know of.
|
||||
func (s *Server) handleGetAddrCmd(p Peer) error {
|
||||
addrs := s.discovery.GoodPeers()
|
||||
if len(addrs) > maxAddrsToSend {
|
||||
addrs = addrs[:maxAddrsToSend]
|
||||
}
|
||||
alist := payload.NewAddressList(len(addrs))
|
||||
ts := time.Now()
|
||||
for i, addr := range addrs {
|
||||
// we know it's a good address, so it can't fail
|
||||
netaddr, _ := net.ResolveTCPAddr("tcp", addr)
|
||||
alist.Addrs[i] = payload.NewAddressAndTime(netaddr, ts)
|
||||
}
|
||||
return p.WriteMsg(NewMessage(s.Net, CMDAddr, alist))
|
||||
}
|
||||
|
||||
// requestHeaders will send a getheaders message to the peer.
|
||||
// The peer will respond with headers op to a count of 2000.
|
||||
func (s *Server) requestHeaders(p Peer) error {
|
||||
|
@ -338,6 +356,9 @@ func (s *Server) handleMessage(peer Peer, msg *Message) error {
|
|||
case CMDAddr:
|
||||
addrs := msg.Payload.(*payload.AddressList)
|
||||
return s.handleAddrCmd(peer, addrs)
|
||||
case CMDGetAddr:
|
||||
// it has no payload
|
||||
return s.handleGetAddrCmd(peer)
|
||||
case CMDHeaders:
|
||||
headers := msg.Payload.(*payload.Headers)
|
||||
go s.handleHeadersCmd(peer, headers)
|
||||
|
|
Loading…
Reference in a new issue