Skip to content

Commit 89d07e1

Browse files
committed
swarm/network/stream: package-wide subscriptionFunc
1 parent 689aad2 commit 89d07e1

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

swarm/network/stream/stream.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ const (
7272
RetrievalEnabled
7373
)
7474

75+
// subscriptionFunc is used to determine what to do in order to perform subscriptions
76+
// usually we would start to really subscribe to nodes, but for tests other functionality may be needed
77+
// (see TestRequestPeerSubscriptions in streamer_test.go)
78+
var subscriptionFunc func(r *Registry, p *network.Peer, bin uint8, subs map[enode.ID]map[Stream]struct{}) bool = doRequestSubscription
79+
7580
// Registry registry for outgoing and incoming streamer constructors
7681
type Registry struct {
7782
addr enode.ID
@@ -90,10 +95,6 @@ type Registry struct {
9095
spec *protocols.Spec //this protocol's spec
9196
balance protocols.Balance //implements protocols.Balance, for accounting
9297
prices protocols.Prices //implements protocols.Prices, provides prices to accounting
93-
// the subscriptionFunc is used to determine what to do in order to perform subscriptions
94-
// usually we would start to really subscribe to nodes, but for tests other functionality may be needed
95-
// (see TestRequestPeerSubscriptions in streamer_test.go)
96-
subscriptionFunc func(p *network.Peer, bin uint8, subs map[enode.ID]map[Stream]struct{}) bool
9798
}
9899

99100
// RegistryOptions holds optional values for NewRegistry constructor.
@@ -128,8 +129,7 @@ func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.Sy
128129
maxPeerServers: options.MaxPeerServers,
129130
balance: balance,
130131
}
131-
//assign the default subscription func: actually do request subscriptions from nodes
132-
streamer.subscriptionFunc = streamer.doRequestSubscription
132+
133133
streamer.setupSpec()
134134

135135
streamer.api = NewAPI(streamer)
@@ -530,15 +530,15 @@ func (r *Registry) requestPeerSubscriptions(kad *network.Kademlia, subs map[enod
530530

531531
for bin := startPo; bin <= endPo; bin++ {
532532
//do the actual subscription
533-
ok = r.subscriptionFunc(p, uint8(bin), subs)
533+
ok = subscriptionFunc(r, p, uint8(bin), subs)
534534
}
535535
return ok
536536
})
537537
}
538538

539539
// doRequestSubscription sends the actual RequestSubscription to the peer
540-
func (r *Registry) doRequestSubscription(p *network.Peer, bin uint8, subs map[enode.ID]map[Stream]struct{}) bool {
541-
log.Debug(fmt.Sprintf("Requesting subscription by: registry %s from peer %s for bin: %d", r.addr, p.ID(), bin))
540+
func doRequestSubscription(r *Registry, p *network.Peer, bin uint8, subs map[enode.ID]map[Stream]struct{}) bool {
541+
log.Debug("Requesting subscription by registry:", "registry", r.addr, "peer", p.ID(), "bin", bin)
542542
// bin is always less then 256 and it is safe to convert it to type uint8
543543
stream := NewStream("SYNC", FormatSyncBinKey(bin), true)
544544
if streams, ok := subs[p.ID()]; ok {

swarm/network/stream/streamer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,9 +1050,11 @@ func TestRequestPeerSubscriptions(t *testing.T) {
10501050

10511051
// simulate that we would do subscriptions: just store the bin numbers
10521052
fakeSubscriptions := make(map[string][]int)
1053+
//after the test, we need to reset the subscriptionFunc to the default
1054+
defer func() { subscriptionFunc = doRequestSubscription }()
10531055
// define the function which should run for each connection
10541056
// instead of doing real subscriptions, we just store the bin numbers
1055-
requestSubscriptionFunc := func(p *network.Peer, bin uint8, subs map[enode.ID]map[Stream]struct{}) bool {
1057+
subscriptionFunc = func(r *Registry, p *network.Peer, bin uint8, subs map[enode.ID]map[Stream]struct{}) bool {
10561058
// get the peer ID
10571059
peerstr := fmt.Sprintf("%x", p.Over())
10581060
// create the array of bins per peer
@@ -1066,8 +1068,6 @@ func TestRequestPeerSubscriptions(t *testing.T) {
10661068
}
10671069
// create just a simple Registry object in order to be able to call...
10681070
r := &Registry{}
1069-
// ...the requestPeerSubscriptions function, which contains the logic for subscriptions
1070-
r.subscriptionFunc = requestSubscriptionFunc
10711071
r.requestPeerSubscriptions(k, nil)
10721072
// calculate the kademlia depth
10731073
kdepth := k.NeighbourhoodDepth()

0 commit comments

Comments
 (0)