diff --git a/pkg/contract_watcher/header/repository/header_repository.go b/pkg/contract_watcher/header/repository/header_repository.go index 91b8cd8c..78d96158 100644 --- a/pkg/contract_watcher/header/repository/header_repository.go +++ b/pkg/contract_watcher/header/repository/header_repository.go @@ -179,7 +179,7 @@ func (r *headerRepository) MissingHeaders(startingBlockNumber, endingBlockNumber ORDER BY headers.block_number` err = r.db.Select(&result, query, startingBlockNumber, endingBlockNumber, r.db.Node.ID) } - return contiguousHeaders(result, startingBlockNumber), err + return continuousHeaders(result), err } // Returns missing headers for all of the provided checked_headers column ids @@ -207,26 +207,7 @@ func (r *headerRepository) MissingHeadersForAll(startingBlockNumber, endingBlock query = baseQuery + endStr err = r.db.Select(&result, query, startingBlockNumber, endingBlockNumber, r.db.Node.ID) } - return contiguousHeaders(result, startingBlockNumber), err -} - -// Returns a continuous set of headers that is contiguous with the provided startingBlockNumber -func contiguousHeaders(headers []core.Header, startingBlockNumber int64) []core.Header { - if len(headers) < 1 { - return headers - } - previousHeader := headers[0].BlockNumber - if previousHeader != startingBlockNumber { - return []core.Header{} - } - for i := 1; i < len(headers); i++ { - previousHeader++ - if headers[i].BlockNumber != previousHeader { - return headers[:i] - } - } - - return headers + return continuousHeaders(result), err } // Returns headers that have been checked for all of the provided event ids but not for the provided method ids diff --git a/pkg/contract_watcher/header/repository/header_repository_test.go b/pkg/contract_watcher/header/repository/header_repository_test.go index b29a8a35..a9a90824 100644 --- a/pkg/contract_watcher/header/repository/header_repository_test.go +++ b/pkg/contract_watcher/header/repository/header_repository_test.go @@ -203,6 +203,18 @@ var _ = Describe("Repository", func() { Expect(missingHeaders[1].BlockNumber).To(Equal(int64(6194633))) }) + It("returns headers after starting header if starting header not missing", func() { + addLaterHeaders(coreHeaderRepo) + err := contractHeaderRepo.AddCheckColumns(eventIDs) + Expect(err).NotTo(HaveOccurred()) + + missingHeaders, err := contractHeaderRepo.MissingHeadersForAll(6194632, -1, eventIDs) + Expect(err).ToNot(HaveOccurred()) + Expect(len(missingHeaders)).To(Equal(2)) + Expect(missingHeaders[0].BlockNumber).To(Equal(mocks.MockHeader3.BlockNumber)) + Expect(missingHeaders[1].BlockNumber).To(Equal(mocks.MockHeader4.BlockNumber)) + }) + It("Fails if one of the eventIDs does not yet exist in check_headers table", func() { addHeaders(coreHeaderRepo) err := contractHeaderRepo.AddCheckColumns(eventIDs) @@ -329,19 +341,31 @@ var _ = Describe("Repository", func() { Expect(err).ToNot(HaveOccurred()) Expect(len(intersectionHeaders)).To(Equal(1)) Expect(intersectionHeaders[0].Id).To(Equal(headerID2)) - }) }) }) func addHeaders(coreHeaderRepo repositories.HeaderRepository) { - coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader1) - coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader2) - coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader3) + _, err := coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader1) + Expect(err).NotTo(HaveOccurred()) + _, err = coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader2) + Expect(err).NotTo(HaveOccurred()) + _, err = coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader3) + Expect(err).NotTo(HaveOccurred()) } func addDiscontinuousHeaders(coreHeaderRepo repositories.HeaderRepository) { - coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader1) - coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader2) - coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader4) + _, err := coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader1) + Expect(err).NotTo(HaveOccurred()) + _, err = coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader2) + Expect(err).NotTo(HaveOccurred()) + _, err = coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader4) + Expect(err).NotTo(HaveOccurred()) +} + +func addLaterHeaders(coreHeaderRepo repositories.HeaderRepository) { + _, err := coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader3) + Expect(err).NotTo(HaveOccurred()) + _, err = coreHeaderRepo.CreateOrUpdateHeader(mocks.MockHeader4) + Expect(err).NotTo(HaveOccurred()) }