From 0e82bd813c3255387f7dbf95c994c585e1c6b169 Mon Sep 17 00:00:00 2001 From: Jin Qing <1091147665@qq.com> Date: Tue, 19 Sep 2017 14:23:54 +0800 Subject: [PATCH] If address list less than 1000 send getaddr req If address list less than 1000 send getaddr request Signed-off-by: Jin Qing <1091147665@qq.com> --- net/message/verack.go | 4 +++- net/node/infoUpdate.go | 4 +++- net/node/knowaddrlist.go | 13 +++++++++++++ net/protocol/protocol.go | 1 + 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/net/message/verack.go b/net/message/verack.go index b5c69674..7b5e4ece 100644 --- a/net/message/verack.go +++ b/net/message/verack.go @@ -69,7 +69,9 @@ func (msg verACK) Handle(node Noder) error { // Fixme, there is a race condition here, // but it doesn't matter to access the invalid // node which will trigger a warning - node.ReqNeighborList() + if node.LocalNode().NeedMoreAddresses() { + node.ReqNeighborList() + } addr := node.GetAddr() port := node.GetPort() nodeAddr := addr + ":" + strconv.Itoa(int(port)) diff --git a/net/node/infoUpdate.go b/net/node/infoUpdate.go index d92a978e..de9ffa5d 100644 --- a/net/node/infoUpdate.go +++ b/net/node/infoUpdate.go @@ -136,7 +136,9 @@ func (node *node) ConnectSeeds() { node.nbrNodes.Unlock() if found { if n.GetState() == ESTABLISH { - n.ReqNeighborList() + if node.LocalNode().NeedMoreAddresses() { + n.ReqNeighborList() + } } } else { //not found go node.Connect(nodeAddr) diff --git a/net/node/knowaddrlist.go b/net/node/knowaddrlist.go index d7baabc5..e74b51a8 100644 --- a/net/node/knowaddrlist.go +++ b/net/node/knowaddrlist.go @@ -7,6 +7,12 @@ import ( "sync" ) +const ( + // needAddressThreshold is the number of addresses under which the + // address manager will claim to need more addresses. + needAddressThreshold = 1000 +) + type KnownAddress struct { srcAddr NodeAddr } @@ -33,6 +39,13 @@ func (ka *KnownAddress) GetID() uint64 { return ka.srcAddr.ID } +func (al *KnownAddressList) NeedMoreAddresses() bool { + al.Lock() + defer al.Unlock() + + return al.addrCount < needAddressThreshold +} + func (al *KnownAddressList) AddressExisted(uid uint64) bool { _, ok := al.List[uid] return ok diff --git a/net/protocol/protocol.go b/net/protocol/protocol.go index c4c59548..67f5e51c 100644 --- a/net/protocol/protocol.go +++ b/net/protocol/protocol.go @@ -148,6 +148,7 @@ type Noder interface { GetDefaultMaxPeers() uint GetMaxOutboundCnt() uint GetGetAddrMax() uint + NeedMoreAddresses() bool } func (msg *NodeAddr) Deserialization(p []byte) error {