Replace request ID when same request is restarted (#1453)

This commit is contained in:
Dmitry Shulyak 2019-05-01 13:33:18 +03:00 committed by Igor Mandrigin
parent 218a35e609
commit cba00153e2
3 changed files with 30 additions and 6 deletions

View File

@ -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())

View File

@ -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)
}

View File

@ -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 {