Skip to content

Commit 4673c8b

Browse files
committed
Merge remote-tracking branch 'upstream/master' into pavel/p2p-enable-gossip-setting
2 parents c716774 + edda2ee commit 4673c8b

File tree

16 files changed

+93
-35
lines changed

16 files changed

+93
-35
lines changed

catchup/fetcher_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ func (b *basicRPCNode) RegisterHTTPHandler(path string, handler http.Handler) {
141141
b.rmux.Handle(path, handler)
142142
}
143143

144+
func (b *basicRPCNode) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) {
145+
if b.rmux == nil {
146+
b.rmux = mux.NewRouter()
147+
}
148+
b.rmux.HandleFunc(path, handler)
149+
}
150+
144151
func (b *basicRPCNode) RegisterHandlers(dispatch []network.TaggedMessageHandler) {
145152
}
146153

catchup/pref_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func BenchmarkServiceFetchBlocks(b *testing.B) {
5050
net := &httpTestPeerSource{}
5151
ls := rpcs.MakeBlockService(logging.TestingLog(b), config.GetDefaultLocal(), remote, net, "test genesisID")
5252
nodeA := basicRPCNode{}
53-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
53+
ls.RegisterHandlers(&nodeA)
5454
nodeA.start()
5555
defer nodeA.stop()
5656
rootURL := nodeA.rootURL()

catchup/service_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestServiceFetchBlocksSameRange(t *testing.T) {
151151
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID")
152152

153153
nodeA := basicRPCNode{}
154-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
154+
ls.RegisterHandlers(&nodeA)
155155
nodeA.start()
156156
defer nodeA.stop()
157157
rootURL := nodeA.rootURL()
@@ -223,7 +223,7 @@ func TestSyncRound(t *testing.T) {
223223
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID")
224224

225225
nodeA := basicRPCNode{}
226-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
226+
ls.RegisterHandlers(&nodeA)
227227
nodeA.start()
228228
defer nodeA.stop()
229229
rootURL := nodeA.rootURL()
@@ -313,7 +313,7 @@ func TestPeriodicSync(t *testing.T) {
313313
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID")
314314

315315
nodeA := basicRPCNode{}
316-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
316+
ls.RegisterHandlers(&nodeA)
317317
nodeA.start()
318318
defer nodeA.stop()
319319
rootURL := nodeA.rootURL()
@@ -379,7 +379,7 @@ func TestServiceFetchBlocksOneBlock(t *testing.T) {
379379
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID")
380380

381381
nodeA := basicRPCNode{}
382-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
382+
ls.RegisterHandlers(&nodeA)
383383
nodeA.start()
384384
defer nodeA.stop()
385385
rootURL := nodeA.rootURL()
@@ -443,7 +443,7 @@ func TestAbruptWrites(t *testing.T) {
443443
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID")
444444

445445
nodeA := basicRPCNode{}
446-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
446+
ls.RegisterHandlers(&nodeA)
447447
nodeA.start()
448448
defer nodeA.stop()
449449
rootURL := nodeA.rootURL()
@@ -501,7 +501,7 @@ func TestServiceFetchBlocksMultiBlocks(t *testing.T) {
501501
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID")
502502

503503
nodeA := basicRPCNode{}
504-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
504+
ls.RegisterHandlers(&nodeA)
505505
nodeA.start()
506506
defer nodeA.stop()
507507
rootURL := nodeA.rootURL()
@@ -555,7 +555,7 @@ func TestServiceFetchBlocksMalformed(t *testing.T) {
555555
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID")
556556

557557
nodeA := basicRPCNode{}
558-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
558+
ls.RegisterHandlers(&nodeA)
559559
nodeA.start()
560560
defer nodeA.stop()
561561
rootURL := nodeA.rootURL()
@@ -709,7 +709,7 @@ func helperTestOnSwitchToUnSupportedProtocol(
709709
ls := rpcs.MakeBlockService(logging.Base(), config, remote, net, "test genesisID")
710710

711711
nodeA := basicRPCNode{}
712-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
712+
ls.RegisterHandlers(&nodeA)
713713
nodeA.start()
714714
defer nodeA.stop()
715715
rootURL := nodeA.rootURL()
@@ -932,7 +932,7 @@ func TestCatchupUnmatchedCertificate(t *testing.T) {
932932
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID")
933933

934934
nodeA := basicRPCNode{}
935-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
935+
ls.RegisterHandlers(&nodeA)
936936
nodeA.start()
937937
defer nodeA.stop()
938938
rootURL := nodeA.rootURL()
@@ -1064,7 +1064,7 @@ func TestServiceLedgerUnavailable(t *testing.T) {
10641064
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID")
10651065

10661066
nodeA := basicRPCNode{}
1067-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
1067+
ls.RegisterHandlers(&nodeA)
10681068
nodeA.start()
10691069
defer nodeA.stop()
10701070
rootURL := nodeA.rootURL()
@@ -1110,7 +1110,7 @@ func TestServiceNoBlockForRound(t *testing.T) {
11101110
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, remote, net, "test genesisID")
11111111

11121112
nodeA := basicRPCNode{}
1113-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
1113+
ls.RegisterHandlers(&nodeA)
11141114
nodeA.start()
11151115
defer nodeA.stop()
11161116
rootURL := nodeA.rootURL()

catchup/universalFetcher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestUGetBlockHTTP(t *testing.T) {
101101
ls := rpcs.MakeBlockService(logging.Base(), blockServiceConfig, ledger, net, "test genesisID")
102102

103103
nodeA := basicRPCNode{}
104-
nodeA.RegisterHTTPHandler(rpcs.BlockServiceBlockPath, ls)
104+
ls.RegisterHandlers(&nodeA)
105105
nodeA.start()
106106
defer nodeA.stop()
107107
rootURL := nodeA.rootURL()

components/mocks/mockNetwork.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ func (network *MockNetwork) ClearProcessors() {
103103
func (network *MockNetwork) RegisterHTTPHandler(path string, handler http.Handler) {
104104
}
105105

106+
// RegisterHTTPHandlerFunc - empty implementation
107+
func (network *MockNetwork) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) {
108+
}
109+
106110
// OnNetworkAdvance - empty implementation
107111
func (network *MockNetwork) OnNetworkAdvance() {}
108112

network/gossipNode.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ type GossipNode interface {
5757
Disconnect(badnode DisconnectablePeer)
5858
DisconnectPeers() // only used by testing
5959

60-
// RegisterHTTPHandler path accepts gorilla/mux path annotations
60+
// RegisterHTTPHandler and RegisterHTTPHandlerFunc: path accepts gorilla/mux path annotations
6161
RegisterHTTPHandler(path string, handler http.Handler)
62+
RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request))
6263

6364
// RequestConnectOutgoing asks the system to actually connect to peers.
6465
// `replace` optionally drops existing connections before making new ones.

network/hybridNetwork.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ func (n *HybridP2PNetwork) RegisterHTTPHandler(path string, handler http.Handler
146146
n.wsNetwork.RegisterHTTPHandler(path, handler)
147147
}
148148

149+
// RegisterHTTPHandlerFunc implements GossipNode
150+
func (n *HybridP2PNetwork) RegisterHTTPHandlerFunc(path string, handlerFunc func(http.ResponseWriter, *http.Request)) {
151+
n.p2pNetwork.RegisterHTTPHandlerFunc(path, handlerFunc)
152+
n.wsNetwork.RegisterHTTPHandlerFunc(path, handlerFunc)
153+
}
154+
149155
// RequestConnectOutgoing implements GossipNode
150156
func (n *HybridP2PNetwork) RequestConnectOutgoing(replace bool, quit <-chan struct{}) {}
151157

network/p2p/http.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ func (s *HTTPServer) RegisterHTTPHandler(path string, handler http.Handler) {
5757
})
5858
}
5959

60+
// RegisterHTTPHandlerFunc registers a http handler with a given path.
61+
func (s *HTTPServer) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) {
62+
s.p2phttpMux.HandleFunc(path, handler)
63+
s.p2phttpMuxRegistrarOnce.Do(func() {
64+
s.Host.SetHTTPHandlerAtPath(algorandP2pHTTPProtocol, "/", s.p2phttpMux)
65+
})
66+
}
67+
6068
// MakeHTTPClient creates a http.Client that uses libp2p transport for a given protocol and peer address.
6169
func MakeHTTPClient(addrInfo *peer.AddrInfo) (*http.Client, error) {
6270
clientStreamHost, err := libp2p.New(libp2p.NoListenAddrs)

network/p2pNetwork.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,12 @@ func (n *P2PNetwork) RegisterHTTPHandler(path string, handler http.Handler) {
577577
n.httpServer.RegisterHTTPHandler(path, handler)
578578
}
579579

580+
// RegisterHTTPHandlerFunc is like RegisterHTTPHandler but accepts
581+
// a callback handler function instead of a method receiver.
582+
func (n *P2PNetwork) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) {
583+
n.httpServer.RegisterHTTPHandlerFunc(path, handler)
584+
}
585+
580586
// RequestConnectOutgoing asks the system to actually connect to peers.
581587
// `replace` optionally drops existing connections before making new ones.
582588
// `quit` chan allows cancellation.

network/wsNetwork.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,11 @@ func (wn *WebsocketNetwork) RegisterHTTPHandler(path string, handler http.Handle
523523
wn.router.Handle(path, handler)
524524
}
525525

526+
// RegisterHTTPHandlerFunc path accepts gorilla/mux path annotations
527+
func (wn *WebsocketNetwork) RegisterHTTPHandlerFunc(path string, handler func(http.ResponseWriter, *http.Request)) {
528+
wn.router.HandleFunc(path, handler)
529+
}
530+
526531
// RequestConnectOutgoing tries to actually do the connect to new peers.
527532
// `replace` drop all connections first and find new peers.
528533
func (wn *WebsocketNetwork) RequestConnectOutgoing(replace bool, quit <-chan struct{}) {

0 commit comments

Comments
 (0)