forked from cerc-io/ipld-eth-server
Remove unused RecheckHeaders method and update Repo tests
This commit is contained in:
parent
f292888195
commit
fd126c7bea
@ -69,31 +69,6 @@ func MissingHeaders(startingBlockNumber, endingBlockNumber int64, db *postgres.D
|
|||||||
return result, err
|
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) {
|
func GetCheckedColumnNames(db *postgres.DB) ([]string, error) {
|
||||||
// Query returns `[]driver.Value`, nullable polymorphic interface
|
// Query returns `[]driver.Value`, nullable polymorphic interface
|
||||||
var queryResult []driver.Value
|
var queryResult []driver.Value
|
||||||
|
@ -36,12 +36,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Repository", func() {
|
var _ = Describe("Repository", func() {
|
||||||
Describe("MarkHeaderChecked", func() {
|
var (
|
||||||
var (
|
checkedHeadersColumn string
|
||||||
checkedHeadersColumn string
|
db *postgres.DB
|
||||||
db *postgres.DB
|
)
|
||||||
)
|
|
||||||
|
|
||||||
|
Describe("MarkHeaderChecked", func() {
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
db = test_config.NewTestDB(test_config.NewTestNode())
|
db = test_config.NewTestDB(test_config.NewTestNode())
|
||||||
test_config.CleanTestDB(db)
|
test_config.CleanTestDB(db)
|
||||||
@ -73,11 +73,6 @@ var _ = Describe("Repository", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Describe("MarkHeaderCheckedInTransaction", func() {
|
Describe("MarkHeaderCheckedInTransaction", func() {
|
||||||
var (
|
|
||||||
checkedHeadersColumn string
|
|
||||||
db *postgres.DB
|
|
||||||
)
|
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
db = test_config.NewTestDB(test_config.NewTestNode())
|
db = test_config.NewTestDB(test_config.NewTestNode())
|
||||||
test_config.CleanTestDB(db)
|
test_config.CleanTestDB(db)
|
||||||
@ -114,16 +109,17 @@ var _ = Describe("Repository", func() {
|
|||||||
|
|
||||||
Describe("MissingHeaders", func() {
|
Describe("MissingHeaders", func() {
|
||||||
var (
|
var (
|
||||||
db *postgres.DB
|
|
||||||
headerRepository datastore.HeaderRepository
|
headerRepository datastore.HeaderRepository
|
||||||
startingBlockNumber int64
|
startingBlockNumber int64
|
||||||
endingBlockNumber int64
|
endingBlockNumber int64
|
||||||
eventSpecificBlockNumber int64
|
eventSpecificBlockNumber int64
|
||||||
|
outOfRangeBlockNumber int64
|
||||||
blockNumbers []int64
|
blockNumbers []int64
|
||||||
headerIDs []int64
|
headerIDs []int64
|
||||||
notCheckedSQL string
|
notCheckedSQL string
|
||||||
err error
|
err error
|
||||||
hr r2.HeaderRepository
|
hr r2.HeaderRepository
|
||||||
|
columnNames []string
|
||||||
)
|
)
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
@ -133,14 +129,14 @@ var _ = Describe("Repository", func() {
|
|||||||
hr = r2.NewHeaderRepository(db)
|
hr = r2.NewHeaderRepository(db)
|
||||||
hr.AddCheckColumns(getExpectedColumnNames())
|
hr.AddCheckColumns(getExpectedColumnNames())
|
||||||
|
|
||||||
columnNames, err := shared.GetCheckedColumnNames(db)
|
columnNames, err = shared.GetCheckedColumnNames(db)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
notCheckedSQL = shared.CreateHeaderCheckedPredicateSQL(columnNames, constants.HeaderMissing)
|
notCheckedSQL = shared.CreateHeaderCheckedPredicateSQL(columnNames, constants.HeaderMissing)
|
||||||
|
|
||||||
startingBlockNumber = rand.Int63()
|
startingBlockNumber = rand.Int63()
|
||||||
eventSpecificBlockNumber = startingBlockNumber + 1
|
eventSpecificBlockNumber = startingBlockNumber + 1
|
||||||
endingBlockNumber = startingBlockNumber + 2
|
endingBlockNumber = startingBlockNumber + 2
|
||||||
outOfRangeBlockNumber := endingBlockNumber + 1
|
outOfRangeBlockNumber = endingBlockNumber + 1
|
||||||
|
|
||||||
blockNumbers = []int64{startingBlockNumber, eventSpecificBlockNumber, endingBlockNumber, outOfRangeBlockNumber}
|
blockNumbers = []int64{startingBlockNumber, eventSpecificBlockNumber, endingBlockNumber, outOfRangeBlockNumber}
|
||||||
|
|
||||||
@ -153,10 +149,11 @@ var _ = Describe("Repository", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
AfterEach(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() {
|
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])
|
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1])
|
||||||
Expect(err).NotTo(HaveOccurred())
|
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[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)))
|
Expect(nodeTwoMissingHeaders[1].BlockNumber).To(Or(Equal(startingBlockNumber+10), Equal(eventSpecificBlockNumber+10), Equal(endingBlockNumber+10)))
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
Describe("RecheckHeaders", func() {
|
It("handles an ending block of -1 ", func() {
|
||||||
var (
|
endingBlock := int64(-1)
|
||||||
checkedHeadersColumn string
|
headers, err := shared.MissingHeaders(startingBlockNumber, endingBlock, db, notCheckedSQL)
|
||||||
db *postgres.DB
|
|
||||||
headerOneID, headerTwoID, headerThreeID, headerFourID int64
|
|
||||||
headerOneErr, headerTwoErr, headerThreeErr, headerFourErr error
|
|
||||||
)
|
|
||||||
|
|
||||||
BeforeEach(func() {
|
Expect(err).NotTo(HaveOccurred())
|
||||||
db = test_config.NewTestDB(test_config.NewTestNode())
|
Expect(len(headers)).To(Equal(4))
|
||||||
test_config.CleanTestDB(db)
|
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
|
It("when a the `notCheckedSQL` argument allows for rechecks it returns headers where the checked count is less than the maximum", func() {
|
||||||
headerRepository := repositories.NewHeaderRepository(db)
|
columnName := columnNames[0]
|
||||||
headerOneID, headerOneErr = headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(1))
|
recheckedSQL := shared.CreateHeaderCheckedPredicateSQL([]string{columnName}, constants.HeaderRecheck)
|
||||||
Expect(headerOneErr).NotTo(HaveOccurred())
|
// mark every header checked at least once
|
||||||
headerTwoID, headerTwoErr = headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(2))
|
// header 4 is marked the maximum number of times, it it is not longer checked
|
||||||
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())
|
|
||||||
|
|
||||||
// mark every header checked at least once, with one fully rechecked (headerThree)
|
|
||||||
maxCheckCount, intConversionErr := strconv.Atoi(constants.RecheckHeaderCap)
|
maxCheckCount, intConversionErr := strconv.Atoi(constants.RecheckHeaderCap)
|
||||||
Expect(intConversionErr).NotTo(HaveOccurred())
|
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() {
|
markHeaderOneErr := shared.MarkHeaderChecked(headerIDs[0], db, columnName)
|
||||||
_, cleanupMigrateErr := db.Exec(`ALTER TABLE public.checked_headers DROP COLUMN ` + checkedHeadersColumn)
|
Expect(markHeaderOneErr).NotTo(HaveOccurred())
|
||||||
Expect(cleanupMigrateErr).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() {
|
headers, err := shared.MissingHeaders(1, -1, db, recheckedSQL)
|
||||||
It("returns all headers since starting block where checked count is less than cap", func() {
|
|
||||||
headers, err := shared.RecheckHeaders(1, -1, db, checkedHeadersColumn)
|
|
||||||
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(len(headers)).To(Equal(3))
|
Expect(len(headers)).To(Equal(3))
|
||||||
Expect(headers[0].Id).To(Or(Equal(headerOneID), Equal(headerTwoID), Equal(headerFourID)))
|
Expect(headers[0].Id).To(Or(Equal(headerIDs[0]), Equal(headerIDs[1]), Equal(headerIDs[2])))
|
||||||
Expect(headers[1].Id).To(Or(Equal(headerOneID), Equal(headerTwoID), Equal(headerFourID)))
|
Expect(headers[1].Id).To(Or(Equal(headerIDs[0]), Equal(headerIDs[1]), Equal(headerIDs[2])))
|
||||||
Expect(headers[2].Id).To(Or(Equal(headerOneID), Equal(headerTwoID), Equal(headerFourID)))
|
Expect(headers[2].Id).To(Or(Equal(headerIDs[0]), Equal(headerIDs[1]), Equal(headerIDs[2])))
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
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)))
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user