From 29e11bd7954dbf4c83e86c399a81dd1dffe940a9 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Tue, 30 Apr 2024 09:35:55 -0500 Subject: [PATCH] rpcserver: Remove unneeded AddedNodeInfo method. The RPC server was refactored some time ago to decouple it from the internals of the main server and part of that refactoring modified the RPC server to make use of a local peer interface to obtain all necessary information about peers which is then used to create RPC server results. Due to that, the AddedNodeInfo and PersistentPeers methods now do the exact same thing. This modifies the rpcserver.ConnManager interface to remove the now unnecessary AddedNodeInfo method in favor of the PersistentPeers method and updates all relevant code accordingly. --- internal/rpcserver/interface.go | 8 ++------ internal/rpcserver/rpcserver.go | 4 ++-- internal/rpcserver/rpcserverhandlers_test.go | 14 +------------- rpcadaptors.go | 20 ++------------------ server.go | 14 +------------- 5 files changed, 8 insertions(+), 52 deletions(-) diff --git a/internal/rpcserver/interface.go b/internal/rpcserver/interface.go index 17bafa419b..304ad83909 100644 --- a/internal/rpcserver/interface.go +++ b/internal/rpcserver/interface.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2023 The Decred developers +// Copyright (c) 2019-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -113,8 +113,7 @@ type ConnManager interface { // ConnectedPeers returns an array consisting of all connected peers. ConnectedPeers() []Peer - // PersistentPeers returns an array consisting of all the persistent - // peers. + // PersistentPeers returns an array consisting of all the persistent peers. PersistentPeers() []Peer // BroadcastMessage sends the provided message to all currently @@ -130,9 +129,6 @@ type ConnManager interface { // the passed transactions to all connected peers. RelayTransactions(txns []*dcrutil.Tx) - // AddedNodeInfo returns information describing persistent (added) nodes. - AddedNodeInfo() []Peer - // Lookup defines the DNS lookup function to be used. Lookup(host string) ([]net.IP, error) } diff --git a/internal/rpcserver/rpcserver.go b/internal/rpcserver/rpcserver.go index 37b4c4fe05..6a9c52330d 100644 --- a/internal/rpcserver/rpcserver.go +++ b/internal/rpcserver/rpcserver.go @@ -1,5 +1,5 @@ // Copyright (c) 2013-2016 The btcsuite developers -// Copyright (c) 2015-2023 The Decred developers +// Copyright (c) 2015-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -1795,7 +1795,7 @@ func handleGetAddedNodeInfo(_ context.Context, s *Server, cmd interface{}) (inte // Retrieve a list of persistent (added) peers from the Decred server // and filter the list of peers per the specified address (if any). - peers := s.cfg.ConnMgr.AddedNodeInfo() + peers := s.cfg.ConnMgr.PersistentPeers() if c.Node != nil { found := false for i, peer := range peers { diff --git a/internal/rpcserver/rpcserverhandlers_test.go b/internal/rpcserver/rpcserverhandlers_test.go index d228a3ede3..2e6f36dcd9 100644 --- a/internal/rpcserver/rpcserverhandlers_test.go +++ b/internal/rpcserver/rpcserverhandlers_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2023 The Decred developers +// Copyright (c) 2020-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -802,7 +802,6 @@ type testConnManager struct { netTotalSent uint64 connectedPeers []Peer persistentPeers []Peer - addedNodeInfo []Peer lookup func(host string) ([]net.IP, error) } @@ -870,11 +869,6 @@ func (c *testConnManager) AddRebroadcastInventory(iv *wire.InvVect, data interfa // inventory vectors for all of the passed transactions to all connected peers. func (c *testConnManager) RelayTransactions(txns []*dcrutil.Tx) {} -// AddedNodeInfo returns a mocked slice of persistent (added) peers. -func (c *testConnManager) AddedNodeInfo() []Peer { - return c.addedNodeInfo -} - // Lookup defines a mocked DNS lookup function to be used. func (c *testConnManager) Lookup(host string) ([]net.IP, error) { return c.lookup(host) @@ -1704,12 +1698,6 @@ func defaultMockConnManager() *testConnManager { testPeer3, testPeer4, }, - addedNodeInfo: []Peer{ - testPeer1, - testPeer2, - testPeer3, - testPeer4, - }, lookup: func(host string) ([]net.IP, error) { if host == "mydomain.org" { return []net.IP{net.ParseIP("127.0.0.211")}, nil diff --git a/rpcadaptors.go b/rpcadaptors.go index 643f9f802e..3477929cf4 100644 --- a/rpcadaptors.go +++ b/rpcadaptors.go @@ -1,5 +1,5 @@ // Copyright (c) 2017 The btcsuite developers -// Copyright (c) 2015-2022 The Decred developers +// Copyright (c) 2015-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -247,7 +247,7 @@ func (cm *rpcConnManager) PersistentPeers() []rpcserver.Peer { cm.server.query <- getAddedNodesMsg{reply: replyChan} serverPeers := <-replyChan - // Convert to generic peers. + // Convert to RPC server peers. peers := make([]rpcserver.Peer, 0, len(serverPeers)) for _, sp := range serverPeers { peers = append(peers, (*rpcPeer)(sp)) @@ -282,22 +282,6 @@ func (cm *rpcConnManager) RelayTransactions(txns []*dcrutil.Tx) { cm.server.relayTransactions(txns) } -// AddedNodeInfo returns information describing persistent (added) nodes. -// -// This function is safe for concurrent access and is part of the -// rpcserver.ConnManager interface implementation. -func (cm *rpcConnManager) AddedNodeInfo() []rpcserver.Peer { - serverPeers := cm.server.AddedNodeInfo() - - // Convert to RPC server peers. - peers := make([]rpcserver.Peer, 0, len(serverPeers)) - for _, sp := range serverPeers { - peers = append(peers, (*rpcPeer)(sp)) - } - - return peers -} - // Lookup defines the DNS lookup function to be used. // // This function is safe for concurrent access and is part of the diff --git a/server.go b/server.go index 0f4d6c16c9..bc41ea29fc 100644 --- a/server.go +++ b/server.go @@ -1,5 +1,5 @@ // Copyright (c) 2013-2016 The btcsuite developers -// Copyright (c) 2015-2023 The Decred developers +// Copyright (c) 2015-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -2550,18 +2550,6 @@ func (s *server) OutboundGroupCount(key string) int { } } -// AddedNodeInfo returns an array of dcrjson.GetAddedNodeInfoResult structures -// describing the persistent (added) nodes. -func (s *server) AddedNodeInfo() []*serverPeer { - replyChan := make(chan []*serverPeer) - select { - case <-s.quit: - return nil - case s.query <- getAddedNodesMsg{reply: replyChan}: - return <-replyChan - } -} - // AddBytesSent adds the passed number of bytes to the total bytes sent counter // for the server. It is safe for concurrent access. func (s *server) AddBytesSent(bytesSent uint64) {