From 102b032c4350395a549f952f95acf9416c71e9dd Mon Sep 17 00:00:00 2001 From: Leo Balduf Date: Sun, 29 Jan 2017 18:47:07 +0100 Subject: [PATCH] udp: fix ordering of scrapes --- bittorrent/bittorrent.go | 6 +++++- frontend/http/writer.go | 4 ++-- middleware/hooks.go | 2 +- middleware/middleware.go | 2 +- storage/memory/peer_store.go | 1 + 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bittorrent/bittorrent.go b/bittorrent/bittorrent.go index 14971d3..0b90072 100644 --- a/bittorrent/bittorrent.go +++ b/bittorrent/bittorrent.go @@ -100,12 +100,16 @@ type ScrapeRequest struct { } // ScrapeResponse represents the parameters used to create a scrape response. +// +// The Scrapes must be in the same order as the InfoHashes in the corresponding +// ScrapeRequest. type ScrapeResponse struct { - Files map[InfoHash]Scrape + Files []Scrape } // Scrape represents the state of a swarm that is returned in a scrape response. type Scrape struct { + InfoHash InfoHash Snatches uint32 Complete uint32 Incomplete uint32 diff --git a/frontend/http/writer.go b/frontend/http/writer.go index 82765d6..57aca69 100644 --- a/frontend/http/writer.go +++ b/frontend/http/writer.go @@ -74,8 +74,8 @@ func WriteAnnounceResponse(w http.ResponseWriter, resp *bittorrent.AnnounceRespo // client over HTTP. func WriteScrapeResponse(w http.ResponseWriter, resp *bittorrent.ScrapeResponse) error { filesDict := bencode.NewDict() - for infohash, scrape := range resp.Files { - filesDict[string(infohash[:])] = bencode.Dict{ + for _, scrape := range resp.Files { + filesDict[string(scrape.InfoHash[:])] = bencode.Dict{ "complete": scrape.Complete, "incomplete": scrape.Incomplete, } diff --git a/middleware/hooks.go b/middleware/hooks.go index 5ac7525..3cd31b0 100644 --- a/middleware/hooks.go +++ b/middleware/hooks.go @@ -183,7 +183,7 @@ func (h *responseHook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeR } for _, infoHash := range req.InfoHashes { - resp.Files[infoHash] = h.store.ScrapeSwarm(infoHash, req.AddressFamily) + resp.Files = append(resp.Files, h.store.ScrapeSwarm(infoHash, req.AddressFamily)) } return ctx, nil diff --git a/middleware/middleware.go b/middleware/middleware.go index 9c8ba38..3a73afb 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -78,7 +78,7 @@ func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceReque // HandleScrape generates a response for a Scrape. func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (resp *bittorrent.ScrapeResponse, err error) { resp = &bittorrent.ScrapeResponse{ - Files: make(map[bittorrent.InfoHash]bittorrent.Scrape), + Files: make([]bittorrent.Scrape, 0, len(req.InfoHashes)), } for _, h := range l.preHooks { if ctx, err = h.HandleScrape(ctx, req, resp); err != nil { diff --git a/storage/memory/peer_store.go b/storage/memory/peer_store.go index 8c0d184..486adc1 100644 --- a/storage/memory/peer_store.go +++ b/storage/memory/peer_store.go @@ -361,6 +361,7 @@ func (s *peerStore) ScrapeSwarm(ih bittorrent.InfoHash, addressFamily bittorrent default: } + resp.InfoHash = ih shard := s.shards[s.shardIndex(ih, addressFamily)] shard.RLock()