From 409665703cdb5412696af869ac86f32c334f56e6 Mon Sep 17 00:00:00 2001 From: Chen Kai <281165273grape@gmail.com> Date: Thu, 22 Feb 2024 16:21:27 +0800 Subject: [PATCH] fix:add justseen node Signed-off-by: Chen Kai <281165273grape@gmail.com> --- cmd/hive/main.go | 6 ++++++ p2p/discover/api.go | 12 +++++++++--- p2p/discover/portal_protocol.go | 25 +++++++++++++++++++------ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/cmd/hive/main.go b/cmd/hive/main.go index 74e57dde5ff9..9e62a1995ea5 100644 --- a/cmd/hive/main.go +++ b/cmd/hive/main.go @@ -10,6 +10,7 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover/portalwire" "github.com/ethereum/go-ethereum/p2p/enode" @@ -18,6 +19,11 @@ import ( ) func main() { + glogger := log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, true)) + slogVerbosity := log.FromLegacyLevel(5) + glogger.Verbosity(slogVerbosity) + log.SetDefault(log.NewLogger(glogger)) + var privateKey *ecdsa.PrivateKey var err error privateKeyHex := os.Getenv("HIVE_CLIENT_PRIVATE_KEY") diff --git a/p2p/discover/api.go b/p2p/discover/api.go index 8e2b49d5bdc3..71ce4f641dd4 100644 --- a/p2p/discover/api.go +++ b/p2p/discover/api.go @@ -76,7 +76,9 @@ func (d *DiscV5API) AddEnr(enr string) (bool, error) { return false, err } - d.DiscV5.tab.addSeenNode(wrapNode(n)) + wn := wrapNode(n) + wn.livenessChecks++ + d.DiscV5.tab.addVerifiedNode(wn) return true, nil } @@ -225,7 +227,9 @@ func (p *PortalAPI) HistoryAddEnr(enr string) (bool, error) { return false, err } - p.portalProtocol.table.addSeenNode(wrapNode(n)) + wn := wrapNode(n) + wn.livenessChecks++ + p.portalProtocol.table.addVerifiedNode(wn) return true, nil } @@ -237,7 +241,9 @@ func (p *PortalAPI) AddEnrs(enrs []string) bool { continue } - p.portalProtocol.table.addSeenNode(wrapNode(n)) + wn := wrapNode(n) + wn.livenessChecks++ + p.portalProtocol.table.addVerifiedNode(wn) } return true diff --git a/p2p/discover/portal_protocol.go b/p2p/discover/portal_protocol.go index d2d80e31e7b0..b5dfe4671f5b 100644 --- a/p2p/discover/portal_protocol.go +++ b/p2p/discover/portal_protocol.go @@ -13,7 +13,6 @@ import ( "math/big" "math/rand" "net" - "os" "sort" "time" @@ -202,12 +201,12 @@ func NewPortalProtocol(config *PortalProtocolConfig, protocolId string, privateK if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { if ipnet.IP.To4() != nil { localNode.SetStaticIP(ipnet.IP) + break } } } closeCtx, cancelCloseCtx := context.WithCancel(context.Background()) - log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true))) protocol := &PortalProtocol{ protocolId: protocolId, ListenAddr: config.ListenAddr, @@ -248,6 +247,9 @@ func (p *PortalProtocol) Start() error { for i := 0; i < concurrentOffers; i++ { go p.offerWorker() } + + // wait for the routing table to be initialized + <-p.table.initDone return nil } @@ -516,6 +518,7 @@ func (p *PortalProtocol) processOffer(target *enode.Node, resp []byte, request * } p.log.Trace("Received accept response", "id", target.ID(), "accept", accept) + p.setJustSeen(target) var contentKeyLen int if request.Kind == TransientOfferRequestKind { @@ -636,6 +639,7 @@ func (p *PortalProtocol) processContent(target *enode.Node, resp []byte) (byte, } p.log.Trace("Received content response", "id", target.ID(), "content", content) + p.setJustSeen(target) return resp[1], content.Content, nil case portalwire.ContentConnIdSelector: connIdMsg := &portalwire.ConnectionId{} @@ -645,6 +649,7 @@ func (p *PortalProtocol) processContent(target *enode.Node, resp []byte) (byte, } p.log.Trace("Received content response", "id", target.ID(), "connIdMsg", connIdMsg) + p.setJustSeen(target) connctx, conncancel := context.WithTimeout(p.closeCtx, defaultUTPConnectTimeout) laddr := p.utp.Addr().(*utp.Addr) raddr := &utp.Addr{IP: target.IP(), Port: target.UDP()} @@ -686,7 +691,7 @@ func (p *PortalProtocol) processContent(target *enode.Node, resp []byte) (byte, } p.log.Trace("Received content response", "id", target.ID(), "enrs", enrs) - + p.setJustSeen(target) nodes := p.filterNodes(target, enrs.Enrs, nil) return resp[1], nodes, nil default: @@ -705,12 +710,18 @@ func (p *PortalProtocol) processNodes(target *enode.Node, resp []byte, distances return nil, err } - p.table.addVerifiedNode(wrapNode(target)) + p.setJustSeen(target) nodes := p.filterNodes(target, nodesResp.Enrs, distances) return nodes, nil } +func (p *PortalProtocol) setJustSeen(target *enode.Node) { + wn := wrapNode(target) + wn.livenessChecks++ + p.table.addVerifiedNode(wn) +} + func (p *PortalProtocol) filterNodes(target *enode.Node, enrs [][]byte, distances []uint) []*enode.Node { var ( nodes []*enode.Node @@ -761,9 +772,9 @@ func (p *PortalProtocol) processPong(target *enode.Node, resp []byte) (*portalwi } p.log.Trace("Received pong response", "id", target.ID(), "pong", pong, "customPayload", customPayload) + p.setJustSeen(target) p.radiusCache.Set([]byte(target.ID().String()), customPayload.Radius) - p.table.addVerifiedNode(wrapNode(target)) return pong, nil } @@ -1308,7 +1319,9 @@ func (p *PortalProtocol) lookupWorker(destNode *node, target enode.ID) ([]*node, } for _, n := range r { if n.ID() != p.Self().ID() { - nodes.push(wrapNode(n), portalFindnodesResultLimit) + wn := wrapNode(n) + p.table.addSeenNode(wn) + nodes.push(wn, portalFindnodesResultLimit) } } return nodes.entries, err