diff --git a/libraries/shared/repository/repository.go b/libraries/shared/repository/repository.go index d88f1600..d6062898 100644 --- a/libraries/shared/repository/repository.go +++ b/libraries/shared/repository/repository.go @@ -69,31 +69,6 @@ func MissingHeaders(startingBlockNumber, endingBlockNumber int64, db *postgres.D return result, err } -func RecheckHeaders(startingBlockNumber, endingBlockNumber int64, db *postgres.DB, checkedHeadersColumn string) ([]core.Header, error) { - var result []core.Header - var query string - var err error - - if endingBlockNumber == -1 { - query = `SELECT headers.id, headers.block_number, headers.hash FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE ` + checkedHeadersColumn + ` between 1 and ` + constants.RecheckHeaderCap + ` - AND headers.block_number >= $1 - AND headers.eth_node_fingerprint = $2` - err = db.Select(&result, query, startingBlockNumber, db.Node.ID) - } else { - query = `SELECT headers.id, headers.block_number, headers.hash FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE ` + checkedHeadersColumn + ` between 1 and ` + constants.RecheckHeaderCap + ` - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3` - err = db.Select(&result, query, startingBlockNumber, endingBlockNumber, db.Node.ID) - } - - return result, err -} - func GetCheckedColumnNames(db *postgres.DB) ([]string, error) { // Query returns `[]driver.Value`, nullable polymorphic interface var queryResult []driver.Value diff --git a/libraries/shared/repository/repository_test.go b/libraries/shared/repository/repository_test.go index 3413fd40..1e409b0b 100644 --- a/libraries/shared/repository/repository_test.go +++ b/libraries/shared/repository/repository_test.go @@ -36,12 +36,12 @@ import ( ) var _ = Describe("Repository", func() { - Describe("MarkHeaderChecked", func() { - var ( - checkedHeadersColumn string - db *postgres.DB - ) + var ( + checkedHeadersColumn string + db *postgres.DB + ) + Describe("MarkHeaderChecked", func() { BeforeEach(func() { db = test_config.NewTestDB(test_config.NewTestNode()) test_config.CleanTestDB(db) @@ -73,11 +73,6 @@ var _ = Describe("Repository", func() { }) Describe("MarkHeaderCheckedInTransaction", func() { - var ( - checkedHeadersColumn string - db *postgres.DB - ) - BeforeEach(func() { db = test_config.NewTestDB(test_config.NewTestNode()) test_config.CleanTestDB(db) @@ -114,16 +109,17 @@ var _ = Describe("Repository", func() { Describe("MissingHeaders", func() { var ( - db *postgres.DB headerRepository datastore.HeaderRepository startingBlockNumber int64 endingBlockNumber int64 eventSpecificBlockNumber int64 + outOfRangeBlockNumber int64 blockNumbers []int64 headerIDs []int64 notCheckedSQL string err error hr r2.HeaderRepository + columnNames []string ) BeforeEach(func() { @@ -133,14 +129,14 @@ var _ = Describe("Repository", func() { hr = r2.NewHeaderRepository(db) hr.AddCheckColumns(getExpectedColumnNames()) - columnNames, err := shared.GetCheckedColumnNames(db) + columnNames, err = shared.GetCheckedColumnNames(db) Expect(err).NotTo(HaveOccurred()) notCheckedSQL = shared.CreateHeaderCheckedPredicateSQL(columnNames, constants.HeaderMissing) startingBlockNumber = rand.Int63() eventSpecificBlockNumber = startingBlockNumber + 1 endingBlockNumber = startingBlockNumber + 2 - outOfRangeBlockNumber := endingBlockNumber + 1 + outOfRangeBlockNumber = endingBlockNumber + 1 blockNumbers = []int64{startingBlockNumber, eventSpecificBlockNumber, endingBlockNumber, outOfRangeBlockNumber} @@ -153,10 +149,11 @@ var _ = Describe("Repository", func() { }) AfterEach(func() { - test_config.CleanCheckedHeadersTable(db, getExpectedColumnNames()) + //test_config.CleanCheckedHeadersTable(db, getExpectedColumnNames()) }) It("only treats headers as checked if the event specific logs have been checked", func() { + //add a checked_header record, but don't mark it check for any of the columns _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1]) Expect(err).NotTo(HaveOccurred()) @@ -191,83 +188,47 @@ var _ = Describe("Repository", func() { Expect(nodeTwoMissingHeaders[0].BlockNumber).To(Or(Equal(startingBlockNumber+10), Equal(eventSpecificBlockNumber+10), Equal(endingBlockNumber+10))) Expect(nodeTwoMissingHeaders[1].BlockNumber).To(Or(Equal(startingBlockNumber+10), Equal(eventSpecificBlockNumber+10), Equal(endingBlockNumber+10))) }) - }) - Describe("RecheckHeaders", func() { - var ( - checkedHeadersColumn string - db *postgres.DB - headerOneID, headerTwoID, headerThreeID, headerFourID int64 - headerOneErr, headerTwoErr, headerThreeErr, headerFourErr error - ) + It("handles an ending block of -1 ", func() { + endingBlock := int64(-1) + headers, err := shared.MissingHeaders(startingBlockNumber, endingBlock, db, notCheckedSQL) - BeforeEach(func() { - db = test_config.NewTestDB(test_config.NewTestNode()) - test_config.CleanTestDB(db) + Expect(err).NotTo(HaveOccurred()) + Expect(len(headers)).To(Equal(4)) + Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(eventSpecificBlockNumber), Equal(outOfRangeBlockNumber))) + Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(eventSpecificBlockNumber), Equal(outOfRangeBlockNumber))) + Expect(headers[2].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(eventSpecificBlockNumber), Equal(outOfRangeBlockNumber))) + Expect(headers[3].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(eventSpecificBlockNumber), Equal(outOfRangeBlockNumber))) - // create header checked column - checkedHeadersColumn = "test_column_checked" - _, migrateErr := db.Exec(`ALTER TABLE public.checked_headers ADD COLUMN ` + checkedHeadersColumn + ` integer`) - Expect(migrateErr).NotTo(HaveOccurred()) + }) - // create headers - headerRepository := repositories.NewHeaderRepository(db) - headerOneID, headerOneErr = headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(1)) - Expect(headerOneErr).NotTo(HaveOccurred()) - headerTwoID, headerTwoErr = headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(2)) - Expect(headerTwoErr).NotTo(HaveOccurred()) - headerThreeID, headerThreeErr = headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(3)) - Expect(headerThreeErr).NotTo(HaveOccurred()) - headerFourID, headerFourErr = headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(4)) - Expect(headerFourErr).NotTo(HaveOccurred()) + It("when a the `notCheckedSQL` argument allows for rechecks it returns headers where the checked count is less than the maximum", func() { + columnName := columnNames[0] + recheckedSQL := shared.CreateHeaderCheckedPredicateSQL([]string{columnName}, constants.HeaderRecheck) + // mark every header checked at least once + // header 4 is marked the maximum number of times, it it is not longer checked - // mark every header checked at least once, with one fully rechecked (headerThree) maxCheckCount, intConversionErr := strconv.Atoi(constants.RecheckHeaderCap) Expect(intConversionErr).NotTo(HaveOccurred()) - _, markHeaderOneCheckedErr := db.Exec( - `INSERT INTO public.checked_headers (header_id, `+checkedHeadersColumn+`) VALUES ($1, $2)`, - headerOneID, maxCheckCount) - Expect(markHeaderOneCheckedErr).NotTo(HaveOccurred()) - _, markHeaderTwoCheckedErr := db.Exec( - `INSERT INTO public.checked_headers (header_id, `+checkedHeadersColumn+`) VALUES ($1, $2)`, - headerTwoID, maxCheckCount) - Expect(markHeaderTwoCheckedErr).NotTo(HaveOccurred()) - _, markHeaderThreeCheckedErr := db.Exec( - `INSERT INTO public.checked_headers (header_id, `+checkedHeadersColumn+`) VALUES ($1, $2)`, - headerThreeID, maxCheckCount+1) - Expect(markHeaderThreeCheckedErr).NotTo(HaveOccurred()) - _, markHeaderFourCheckedErr := db.Exec( - `INSERT INTO public.checked_headers (header_id, `+checkedHeadersColumn+`) VALUES ($1, $2)`, - headerFourID, maxCheckCount) - Expect(markHeaderFourCheckedErr).NotTo(HaveOccurred()) - }) - AfterEach(func() { - _, cleanupMigrateErr := db.Exec(`ALTER TABLE public.checked_headers DROP COLUMN ` + checkedHeadersColumn) - Expect(cleanupMigrateErr).NotTo(HaveOccurred()) - }) + markHeaderOneErr := shared.MarkHeaderChecked(headerIDs[0], db, columnName) + Expect(markHeaderOneErr).NotTo(HaveOccurred()) + markHeaderTwoErr := shared.MarkHeaderChecked(headerIDs[1], db, columnName) + Expect(markHeaderTwoErr).NotTo(HaveOccurred()) + markHeaderThreeErr := shared.MarkHeaderChecked(headerIDs[2], db, columnName) + Expect(markHeaderThreeErr).NotTo(HaveOccurred()) + for i := 0; i <= maxCheckCount; i++ { + markHeaderFourErr := shared.MarkHeaderChecked(headerIDs[3], db, columnName) + Expect(markHeaderFourErr).NotTo(HaveOccurred()) + } - Describe("when no ending block number (ending block number == -1)", func() { - It("returns all headers since starting block where checked count is less than cap", func() { - headers, err := shared.RecheckHeaders(1, -1, db, checkedHeadersColumn) + headers, err := shared.MissingHeaders(1, -1, db, recheckedSQL) - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(3)) - Expect(headers[0].Id).To(Or(Equal(headerOneID), Equal(headerTwoID), Equal(headerFourID))) - Expect(headers[1].Id).To(Or(Equal(headerOneID), Equal(headerTwoID), Equal(headerFourID))) - Expect(headers[2].Id).To(Or(Equal(headerOneID), Equal(headerTwoID), Equal(headerFourID))) - }) - }) - - Describe("when ending block number specified", func() { - It("returns headers between starting and ending block where checked count is less than cap", func() { - headers, err := shared.RecheckHeaders(1, 3, db, checkedHeadersColumn) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(2)) - Expect(headers[0].Id).To(Or(Equal(headerOneID), Equal(headerTwoID))) - Expect(headers[1].Id).To(Or(Equal(headerOneID), Equal(headerTwoID))) - }) + Expect(err).NotTo(HaveOccurred()) + Expect(len(headers)).To(Equal(3)) + Expect(headers[0].Id).To(Or(Equal(headerIDs[0]), Equal(headerIDs[1]), Equal(headerIDs[2]))) + Expect(headers[1].Id).To(Or(Equal(headerIDs[0]), Equal(headerIDs[1]), Equal(headerIDs[2]))) + Expect(headers[2].Id).To(Or(Equal(headerIDs[0]), Equal(headerIDs[1]), Equal(headerIDs[2]))) }) })