From 30450a254c8408bbdf198f129dcc864a0c3eeea1 Mon Sep 17 00:00:00 2001 From: Nickolai Zeldovich Date: Wed, 23 Aug 2023 13:02:28 -0400 Subject: [PATCH 1/8] simplify API for BlockService to handle multiple HTTP paths Add a helper BlockService.RegisterHandlers() that registers handlers for multiple HTTP paths, in preparation for adding more handlers. --- catchup/pref_test.go | 2 +- catchup/service_test.go | 18 +++++++++--------- catchup/universalFetcher_test.go | 2 +- network/gossipNode.go | 3 ++- network/p2pNetwork.go | 3 +++ network/wsNetwork.go | 5 +++++ rpcs/blockService.go | 23 +++++++++++++++++------ rpcs/blockService_test.go | 16 ++++++++-------- 8 files changed, 46 insertions(+), 26 deletions(-) diff --git a/catchup/pref_test.go b/catchup/pref_test.go index be3f67f473..377575f23a 100644 --- a/catchup/pref_test.go +++ b/catchup/pref_test.go @@ -50,7 +50,7 @@ func BenchmarkServiceFetchBlocks(b *testing.B) { net := &httpTestPeerSource{} ls := rpcs.MakeBlockService(logging.TestingLog(b), config.GetDefaultLocal(), remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() diff --git a/catchup/service_test.go b/catchup/service_test.go index 6807b11194..406f5ef819 100644 --- a/catchup/service_test.go +++ b/catchup/service_test.go @@ -150,7 +150,7 @@ func TestServiceFetchBlocksSameRange(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() @@ -201,7 +201,7 @@ func TestSyncRound(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() @@ -291,7 +291,7 @@ func TestPeriodicSync(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() @@ -357,7 +357,7 @@ func TestServiceFetchBlocksOneBlock(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() @@ -421,7 +421,7 @@ func TestAbruptWrites(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() @@ -479,7 +479,7 @@ func TestServiceFetchBlocksMultiBlocks(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() @@ -534,7 +534,7 @@ func TestServiceFetchBlocksMalformed(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() @@ -688,7 +688,7 @@ func helperTestOnSwitchToUnSupportedProtocol( ls := rpcs.MakeBlockService(logging.Base(), config, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() @@ -899,7 +899,7 @@ func TestCatchupUnmatchedCertificate(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() diff --git a/catchup/universalFetcher_test.go b/catchup/universalFetcher_test.go index bf74c95702..4414bb9c11 100644 --- a/catchup/universalFetcher_test.go +++ b/catchup/universalFetcher_test.go @@ -100,7 +100,7 @@ func TestUGetBlockHTTP(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, ledger, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() diff --git a/network/gossipNode.go b/network/gossipNode.go index 7ae667170a..8029114172 100644 --- a/network/gossipNode.go +++ b/network/gossipNode.go @@ -54,8 +54,9 @@ type GossipNode interface { Disconnect(badnode Peer) DisconnectPeers() // only used by testing - // RegisterHTTPHandler path accepts gorilla/mux path annotations + // RegisterHTTPHandler and RegisterHTTPHandlerFunc: path accepts gorilla/mux path annotations RegisterHTTPHandler(path string, handler http.Handler) + RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) // RequestConnectOutgoing asks the system to actually connect to peers. // `replace` optionally drops existing connections before making new ones. diff --git a/network/p2pNetwork.go b/network/p2pNetwork.go index cc37961d51..21cadadcd9 100644 --- a/network/p2pNetwork.go +++ b/network/p2pNetwork.go @@ -241,6 +241,9 @@ func (n *P2PNetwork) DisconnectPeers() { func (n *P2PNetwork) RegisterHTTPHandler(path string, handler http.Handler) { } +func (n *P2PNetwork) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) { +} + // RequestConnectOutgoing asks the system to actually connect to peers. // `replace` optionally drops existing connections before making new ones. // `quit` chan allows cancellation. diff --git a/network/wsNetwork.go b/network/wsNetwork.go index 2cce632bed..ff414f931b 100644 --- a/network/wsNetwork.go +++ b/network/wsNetwork.go @@ -504,6 +504,11 @@ func (wn *WebsocketNetwork) RegisterHTTPHandler(path string, handler http.Handle wn.router.Handle(path, handler) } +// RegisterHTTPHandlerFunc path accepts gorilla/mux path annotations +func (wn *WebsocketNetwork) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) { + wn.router.HandleFunc(path, handler) +} + // RequestConnectOutgoing tries to actually do the connect to new peers. // `replace` drop all connections first and find new peers. func (wn *WebsocketNetwork) RequestConnectOutgoing(replace bool, quit <-chan struct{}) { diff --git a/rpcs/blockService.go b/rpcs/blockService.go index 0f48f873c0..e7a4f30bad 100644 --- a/rpcs/blockService.go +++ b/rpcs/blockService.go @@ -55,7 +55,7 @@ const blockServerMaxBodyLength = 512 const blockServerCatchupRequestBufferSize = 10 // BlockServiceBlockPath is the path to register BlockService as a handler for when using gorilla/mux -// e.g. .Handle(BlockServiceBlockPath, &ls) +// e.g. .HandleFunc(BlockServiceBlockPath, ls.ServeBlockPath) const BlockServiceBlockPath = "/v{version:[0-9.]+}/{genesisID}/block/{round:[0-9a-z]+}" // Constant strings used as keys for topics @@ -145,11 +145,22 @@ func MakeBlockService(log logging.Logger, config config.Local, ledger LedgerForB memoryCap: config.BlockServiceMemCap, } if service.enableService { - net.RegisterHTTPHandler(BlockServiceBlockPath, service) + service.RegisterHandlers(net) } return service } +// HTTPRegistrar represents an HTTP request router that can be used by BlockService +// to register its request handlers. +type HTTPRegistrar interface { + RegisterHTTPHandlerFunc(path string, handler func(response http.ResponseWriter, request *http.Request)) +} + +// RegisterHTTP registers the request handlers for BlockService's paths with the registrar. +func (bs *BlockService) RegisterHandlers(registrar HTTPRegistrar) { + registrar.RegisterHTTPHandlerFunc(BlockServiceBlockPath, bs.ServeBlockPath) +} + // Start listening to catchup requests over ws func (bs *BlockService) Start() { bs.mu.Lock() @@ -174,10 +185,10 @@ func (bs *BlockService) Stop() { bs.closeWaitGroup.Wait() } -// ServerHTTP returns blocks +// ServeBlockPath returns blocks // Either /v{version}/{genesisID}/block/{round} or ?b={round}&v={version} // Uses gorilla/mux for path argument parsing. -func (bs *BlockService) ServeHTTP(response http.ResponseWriter, request *http.Request) { +func (bs *BlockService) ServeBlockPath(response http.ResponseWriter, request *http.Request) { pathVars := mux.Vars(request) versionStr, hasVersionStr := pathVars["version"] roundStr, hasRoundStr := pathVars["round"] @@ -254,13 +265,13 @@ func (bs *BlockService) ServeHTTP(response http.ResponseWriter, request *http.Re if !ok { response.Header().Set("Retry-After", blockResponseRetryAfter) response.WriteHeader(http.StatusServiceUnavailable) - bs.log.Debugf("ServeHTTP: returned retry-after: %v", err) + bs.log.Debugf("ServeBlockPath: returned retry-after: %v", err) } httpBlockMessagesDroppedCounter.Inc(nil) return default: // unexpected error. - bs.log.Warnf("ServeHTTP : failed to retrieve block %d %v", round, err) + bs.log.Warnf("ServeBlockPath: failed to retrieve block %d %v", round, err) response.WriteHeader(http.StatusInternalServerError) return } diff --git a/rpcs/blockService_test.go b/rpcs/blockService_test.go index 98d86ae36d..91123c09f8 100644 --- a/rpcs/blockService_test.go +++ b/rpcs/blockService_test.go @@ -149,11 +149,11 @@ func TestRedirectFallbackArchiver(t *testing.T) { nodeA := &basicRPCNode{} nodeB := &basicRPCNode{} - nodeA.RegisterHTTPHandler(BlockServiceBlockPath, bs1) + bs1.RegisterHandlers(nodeA) nodeA.start() defer nodeA.stop() - nodeB.RegisterHTTPHandler(BlockServiceBlockPath, bs2) + bs2.RegisterHandlers(nodeB) nodeB.start() defer nodeB.stop() @@ -200,7 +200,7 @@ func TestBlockServiceShutdown(t *testing.T) { nodeA := &basicRPCNode{} - nodeA.RegisterHTTPHandler(BlockServiceBlockPath, bs1) + bs1.RegisterHandlers(nodeA) nodeA.start() defer nodeA.stop() @@ -261,8 +261,8 @@ func TestRedirectFallbackEndpoints(t *testing.T) { bs1 := MakeBlockService(log, config, ledger1, net1, "{genesisID}") bs2 := MakeBlockService(log, config, ledger2, net2, "{genesisID}") - nodeA.RegisterHTTPHandler(BlockServiceBlockPath, bs1) - nodeB.RegisterHTTPHandler(BlockServiceBlockPath, bs2) + bs1.RegisterHandlers(nodeA) + bs2.RegisterHandlers(nodeB) parsedURL, err := network.ParseHostOrURL(nodeA.rootURL()) require.NoError(t, err) @@ -320,11 +320,11 @@ func TestRedirectOnFullCapacity(t *testing.T) { nodeA := &basicRPCNode{} nodeB := &basicRPCNode{} - nodeA.RegisterHTTPHandler(BlockServiceBlockPath, bs1) + bs1.RegisterHandlers(nodeA) nodeA.start() defer nodeA.stop() - nodeB.RegisterHTTPHandler(BlockServiceBlockPath, bs2) + bs2.RegisterHandlers(nodeB) nodeB.start() defer nodeB.stop() @@ -491,7 +491,7 @@ func TestRedirectExceptions(t *testing.T) { nodeA := &basicRPCNode{} - nodeA.RegisterHTTPHandler(BlockServiceBlockPath, bs1) + bs1.RegisterHandlers(nodeA) nodeA.start() defer nodeA.stop() From d50aa31c42677ad9b92ce0779ea551a09c78bc45 Mon Sep 17 00:00:00 2001 From: Nickolai Zeldovich Date: Wed, 30 Aug 2023 22:30:29 -0400 Subject: [PATCH 2/8] make lint happy --- network/p2pNetwork.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/network/p2pNetwork.go b/network/p2pNetwork.go index 21cadadcd9..a8391c1034 100644 --- a/network/p2pNetwork.go +++ b/network/p2pNetwork.go @@ -241,6 +241,8 @@ func (n *P2PNetwork) DisconnectPeers() { func (n *P2PNetwork) RegisterHTTPHandler(path string, handler http.Handler) { } +// RegisterHTTPHandlerFunc is like RegisterHTTPHandler but accepts +// a callback handler function instead of a method receiver. func (n *P2PNetwork) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) { } From 4b413af6ac330e27fb69862f6d01d68140619d89 Mon Sep 17 00:00:00 2001 From: Nickolai Zeldovich Date: Wed, 30 Aug 2023 23:00:04 -0400 Subject: [PATCH 3/8] make lint even happier --- rpcs/blockService.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpcs/blockService.go b/rpcs/blockService.go index e7a4f30bad..b185f224e5 100644 --- a/rpcs/blockService.go +++ b/rpcs/blockService.go @@ -156,7 +156,7 @@ type HTTPRegistrar interface { RegisterHTTPHandlerFunc(path string, handler func(response http.ResponseWriter, request *http.Request)) } -// RegisterHTTP registers the request handlers for BlockService's paths with the registrar. +// RegisterHandlers registers the request handlers for BlockService's paths with the registrar. func (bs *BlockService) RegisterHandlers(registrar HTTPRegistrar) { registrar.RegisterHTTPHandlerFunc(BlockServiceBlockPath, bs.ServeBlockPath) } From 172e27bd38f78b7ddd6fc547037d972b37b71543 Mon Sep 17 00:00:00 2001 From: Pavel Zbitskiy Date: Mon, 22 Jul 2024 13:02:49 -0400 Subject: [PATCH 4/8] tests: add RegisterHTTPHandlerFunc to basicRPCNode --- catchup/fetcher_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/catchup/fetcher_test.go b/catchup/fetcher_test.go index 983de01475..af85425a0c 100644 --- a/catchup/fetcher_test.go +++ b/catchup/fetcher_test.go @@ -143,6 +143,13 @@ func (b *basicRPCNode) RegisterHTTPHandler(path string, handler http.Handler) { b.rmux.Handle(path, handler) } +func (b *basicRPCNode) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) { + if b.rmux == nil { + b.rmux = mux.NewRouter() + } + b.rmux.HandleFunc(path, handler) +} + func (b *basicRPCNode) RegisterHandlers(dispatch []network.TaggedMessageHandler) { } From 9cb1125be109a097e2e150230d479e5b81362dbe Mon Sep 17 00:00:00 2001 From: Pavel Zbitskiy Date: Mon, 22 Jul 2024 13:29:09 -0400 Subject: [PATCH 5/8] tests: add RegisterHTTPHandlerFunc to rpcs.basicRPCNode --- rpcs/txService_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rpcs/txService_test.go b/rpcs/txService_test.go index dd999d6e65..d3701a7626 100644 --- a/rpcs/txService_test.go +++ b/rpcs/txService_test.go @@ -86,6 +86,13 @@ func (b *basicRPCNode) RegisterHTTPHandler(path string, handler http.Handler) { b.rmux.Handle(path, handler) } +func (b *basicRPCNode) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) { + if b.rmux == nil { + b.rmux = mux.NewRouter() + } + b.rmux.HandleFunc(path, handler) +} + func (b *basicRPCNode) RegisterHandlers(dispatch []network.TaggedMessageHandler) { } From 4bf0a194318b02dff5b68d897c1e354c76c8e255 Mon Sep 17 00:00:00 2001 From: Pavel Zbitskiy Date: Tue, 23 Jul 2024 13:13:00 -0400 Subject: [PATCH 6/8] post merge fixes + remaining tests fixes --- catchup/service_test.go | 4 ++-- components/mocks/mockNetwork.go | 4 ++++ network/hybridNetwork.go | 6 ++++++ network/p2p/http.go | 7 +++++++ network/p2pNetwork.go | 1 + rpcs/blockService_test.go | 6 ++---- 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/catchup/service_test.go b/catchup/service_test.go index 6239492fab..d20305ce82 100644 --- a/catchup/service_test.go +++ b/catchup/service_test.go @@ -1064,7 +1064,7 @@ func TestServiceLedgerUnavailable(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() @@ -1110,7 +1110,7 @@ func TestServiceNoBlockForRound(t *testing.T) { ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID") nodeA := basicRPCNode{} - nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls) + ls.RegisterHandlers(&nodeA) nodeA.start() defer nodeA.stop() rootURL := nodeA.rootURL() diff --git a/components/mocks/mockNetwork.go b/components/mocks/mockNetwork.go index 47b1a5b5e4..902eabb82e 100644 --- a/components/mocks/mockNetwork.go +++ b/components/mocks/mockNetwork.go @@ -103,6 +103,10 @@ func (network *MockNetwork) ClearProcessors() { func (network *MockNetwork) RegisterHTTPHandler(path string, handler http.Handler) { } +// RegisterHTTPHandler - empty implementation +func (network *MockNetwork) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) { +} + // OnNetworkAdvance - empty implementation func (network *MockNetwork) OnNetworkAdvance() {} diff --git a/network/hybridNetwork.go b/network/hybridNetwork.go index d30e03cee2..2c27fc071a 100644 --- a/network/hybridNetwork.go +++ b/network/hybridNetwork.go @@ -146,6 +146,12 @@ func (n *HybridP2PNetwork) RegisterHTTPHandler(path string, handler http.Handler n.wsNetwork.RegisterHTTPHandler(path, handler) } +// RegisterHTTPHandler implements GossipNode +func (n *HybridP2PNetwork) RegisterHTTPHandlerFunc(path string, handlerFunc func(http.ResponseWriter, *http.Request)) { + n.p2pNetwork.RegisterHTTPHandlerFunc(path, handlerFunc) + n.wsNetwork.RegisterHTTPHandlerFunc(path, handlerFunc) +} + // RequestConnectOutgoing implements GossipNode func (n *HybridP2PNetwork) RequestConnectOutgoing(replace bool, quit <-chan struct{}) {} diff --git a/network/p2p/http.go b/network/p2p/http.go index 9f2622d015..31f8f197a4 100644 --- a/network/p2p/http.go +++ b/network/p2p/http.go @@ -57,6 +57,13 @@ func (s *HTTPServer) RegisterHTTPHandler(path string, handler http.Handler) { }) } +func (s *HTTPServer) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) { + s.p2phttpMux.HandleFunc(path, handler) + s.p2phttpMuxRegistrarOnce.Do(func() { + s.Host.SetHTTPHandlerAtPath(algorandP2pHTTPProtocol, "/", s.p2phttpMux) + }) +} + // MakeHTTPClient creates a http.Client that uses libp2p transport for a given protocol and peer address. func MakeHTTPClient(addrInfo *peer.AddrInfo) (*http.Client, error) { clientStreamHost, err := libp2p.New(libp2p.NoListenAddrs) diff --git a/network/p2pNetwork.go b/network/p2pNetwork.go index 88d4372438..6ef3ff8786 100644 --- a/network/p2pNetwork.go +++ b/network/p2pNetwork.go @@ -580,6 +580,7 @@ func (n *P2PNetwork) RegisterHTTPHandler(path string, handler http.Handler) { // RegisterHTTPHandlerFunc is like RegisterHTTPHandler but accepts // a callback handler function instead of a method receiver. func (n *P2PNetwork) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) { + n.httpServer.RegisterHTTPHandlerFunc(path, handler) } // RequestConnectOutgoing asks the system to actually connect to peers. diff --git a/rpcs/blockService_test.go b/rpcs/blockService_test.go index 72b8941048..83cfd94ef9 100644 --- a/rpcs/blockService_test.go +++ b/rpcs/blockService_test.go @@ -272,8 +272,6 @@ func TestRedirectOnFullCapacity(t *testing.T) { nodeA := &basicRPCNode{} nodeB := &basicRPCNode{} - - bs1.RegisterHandlers(nodeA) nodeA.start() defer nodeA.stop() nodeB.start() @@ -372,11 +370,11 @@ forloop: // First node redirects, does not return retry require.True(t, strings.Contains(logBuffer1.String(), "redirectRequest: redirected block request to")) - require.False(t, strings.Contains(logBuffer1.String(), "ServeHTTP: returned retry-after: block service memory over capacity")) + require.False(t, strings.Contains(logBuffer1.String(), "ServeBlockPath: returned retry-after: block service memory over capacity")) // Second node cannot redirect, it returns retry-after when over capacity require.False(t, strings.Contains(logBuffer2.String(), "redirectRequest: redirected block request to")) - require.True(t, strings.Contains(logBuffer2.String(), "ServeHTTP: returned retry-after: block service memory over capacity")) + require.True(t, strings.Contains(logBuffer2.String(), "ServeBlockPath: returned retry-after: block service memory over capacity")) } // TestWsBlockLimiting ensures that limits are applied correctly on the websocket side of the service From 3d8ce8b4317f1d0f73fb9b4d3992f1f8a2a4c124 Mon Sep 17 00:00:00 2001 From: Pavel Zbitskiy Date: Tue, 23 Jul 2024 13:26:51 -0400 Subject: [PATCH 7/8] fix linter --- components/mocks/mockNetwork.go | 2 +- network/hybridNetwork.go | 2 +- network/p2p/http.go | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/components/mocks/mockNetwork.go b/components/mocks/mockNetwork.go index 902eabb82e..4f145b1841 100644 --- a/components/mocks/mockNetwork.go +++ b/components/mocks/mockNetwork.go @@ -103,7 +103,7 @@ func (network *MockNetwork) ClearProcessors() { func (network *MockNetwork) RegisterHTTPHandler(path string, handler http.Handler) { } -// RegisterHTTPHandler - empty implementation +// RegisterHTTPHandlerFunc - empty implementation func (network *MockNetwork) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) { } diff --git a/network/hybridNetwork.go b/network/hybridNetwork.go index 2c27fc071a..c955dfff1a 100644 --- a/network/hybridNetwork.go +++ b/network/hybridNetwork.go @@ -146,7 +146,7 @@ func (n *HybridP2PNetwork) RegisterHTTPHandler(path string, handler http.Handler n.wsNetwork.RegisterHTTPHandler(path, handler) } -// RegisterHTTPHandler implements GossipNode +// RegisterHTTPHandlerFunc implements GossipNode func (n *HybridP2PNetwork) RegisterHTTPHandlerFunc(path string, handlerFunc func(http.ResponseWriter, *http.Request)) { n.p2pNetwork.RegisterHTTPHandlerFunc(path, handlerFunc) n.wsNetwork.RegisterHTTPHandlerFunc(path, handlerFunc) diff --git a/network/p2p/http.go b/network/p2p/http.go index 31f8f197a4..07f27afff1 100644 --- a/network/p2p/http.go +++ b/network/p2p/http.go @@ -57,6 +57,7 @@ func (s *HTTPServer) RegisterHTTPHandler(path string, handler http.Handler) { }) } +// RegisterHTTPHandlerFunc registers a http handler with a given path. func (s *HTTPServer) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) { s.p2phttpMux.HandleFunc(path, handler) s.p2phttpMuxRegistrarOnce.Do(func() { From cddb78ed6cbe552dff7a8f7e5ef6d84af133d228 Mon Sep 17 00:00:00 2001 From: Pavel Zbitskiy Date: Fri, 26 Jul 2024 13:22:27 -0400 Subject: [PATCH 8/8] remove HTTPRegistrar interface in preference of Registrar --- rpcs/blockService.go | 8 +------- rpcs/registrar.go | 2 ++ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/rpcs/blockService.go b/rpcs/blockService.go index b0db652e12..410376a20f 100644 --- a/rpcs/blockService.go +++ b/rpcs/blockService.go @@ -152,14 +152,8 @@ func MakeBlockService(log logging.Logger, config config.Local, ledger LedgerForB return service } -// HTTPRegistrar represents an HTTP request router that can be used by BlockService -// to register its request handlers. -type HTTPRegistrar interface { - RegisterHTTPHandlerFunc(path string, handler func(response http.ResponseWriter, request *http.Request)) -} - // RegisterHandlers registers the request handlers for BlockService's paths with the registrar. -func (bs *BlockService) RegisterHandlers(registrar HTTPRegistrar) { +func (bs *BlockService) RegisterHandlers(registrar Registrar) { registrar.RegisterHTTPHandlerFunc(BlockServiceBlockPath, bs.ServeBlockPath) } diff --git a/rpcs/registrar.go b/rpcs/registrar.go index f488aebf26..f0122b552c 100644 --- a/rpcs/registrar.go +++ b/rpcs/registrar.go @@ -26,6 +26,8 @@ import ( type Registrar interface { // RegisterHTTPHandler path accepts gorilla/mux path annotations RegisterHTTPHandler(path string, handler http.Handler) + // RegisterHTTPHandlerFunc path accepts gorilla/mux path annotations and a HandlerFunc + RegisterHTTPHandlerFunc(path string, handler func(response http.ResponseWriter, request *http.Request)) // RegisterHandlers exposes global websocket handler registration RegisterHandlers(dispatch []network.TaggedMessageHandler) }