From cba00153e2404668d19d453c2bf2e2d314f2b50a Mon Sep 17 00:00:00 2001 From: Dmitry Shulyak Date: Wed, 1 May 2019 13:33:18 +0300 Subject: [PATCH] Replace request ID when same request is restarted (#1453) --- db/history.go | 11 +++++++++++ services/shhext/api.go | 7 +------ services/shhext/service_test.go | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/db/history.go b/db/history.go index 6a457d010..5e5f5b048 100644 --- a/db/history.go +++ b/db/history.go @@ -166,6 +166,17 @@ func (req HistoryRequest) Save() error { return req.requestDB.Put(req.ID.Bytes(), val) } +// Replace saves request with new ID and all data attached to the old one. +func (req HistoryRequest) Replace(id common.Hash) error { + if (req.ID != common.Hash{}) { + if err := req.Delete(); err != nil { + return err + } + } + req.ID = id + return req.Save() +} + // Delete HistoryRequest from store and update every topic. func (req HistoryRequest) Delete() error { return req.requestDB.Delete(req.ID.Bytes()) diff --git a/services/shhext/api.go b/services/shhext/api.go index 65c1854dd..ea5babdc8 100644 --- a/services/shhext/api.go +++ b/services/shhext/api.go @@ -547,8 +547,7 @@ func (api *PublicAPI) requestMessagesUsingPayload(request db.HistoryRequest, pee } hash = envelope.Hash() - request.ID = hash - err = request.Save() + err = request.Replace(hash) if err != nil { return hash, err } @@ -561,10 +560,6 @@ func (api *PublicAPI) requestMessagesUsingPayload(request db.HistoryRequest, pee } if err := shh.RequestHistoricMessagesWithTimeout(mailServerNode.ID().Bytes(), envelope, timeout); err != nil { - err = request.Delete() - if err != nil { - return hash, err - } if !force { api.service.requestsRegistry.Unregister(hash) } diff --git a/services/shhext/service_test.go b/services/shhext/service_test.go index 07e6c5a89..43dc87fed 100644 --- a/services/shhext/service_test.go +++ b/services/shhext/service_test.go @@ -887,6 +887,24 @@ func (s *RequestWithTrackingHistorySuite) TestSingleRequest() { s.waitMessagesDelivered(filterid, hexes...) } +func (s *RequestWithTrackingHistorySuite) TestPreviousRequestReplaced() { + topic1 := whisper.TopicType{1, 1, 1, 1} + topic2 := whisper.TopicType{255, 255, 255, 255} + + requests := s.initiateHistoryRequest( + TopicRequest{Topic: topic1, Duration: time.Hour}, + TopicRequest{Topic: topic2, Duration: time.Hour}, + ) + s.Require().Len(requests, 1) + s.localService.requestsRegistry.Clear() + replaced := s.initiateHistoryRequest( + TopicRequest{Topic: topic1, Duration: time.Hour}, + TopicRequest{Topic: topic2, Duration: time.Hour}, + ) + s.Require().Len(replaced, 1) + s.Require().NotEqual(requests[0], replaced[0]) +} + func waitForArchival(events chan whisper.EnvelopeEvent, duration time.Duration, hashes ...hexutil.Bytes) error { waiting := map[common.Hash]struct{}{} for _, hash := range hashes {