VDB-207 refactor init move split toll tune flog (#110)

* Use shared repo tests for vat_init

* Use shared methods in vat_init repository

* Use shared repo tests for vat_move

* Use shared methods in vat_move repository

* Use shared repo tests for vat_slip

* Use shared methods in vat_slip repository

* Use shared repo tests for vat_toll

* Use shared methods in vat_toll repository

* Use shared methods in vat_tune repository

* Use shared repo tests for vow_flog (+small namespace fix)

* Use shared methods in vow_flog repository

* Go vet + tiny oopsie
This commit is contained in:
Edvard Hübinette 2018-11-12 14:17:58 +01:00 committed by GitHub
parent 571f300392
commit 4f295b9aec
14 changed files with 241 additions and 1137 deletions

View File

@ -20,4 +20,10 @@ var (
VatFoldChecked = "vat_fold_checked" VatFoldChecked = "vat_fold_checked"
VatGrabChecked = "vat_grab_checked" VatGrabChecked = "vat_grab_checked"
VatHealChecked = "vat_heal_checked" VatHealChecked = "vat_heal_checked"
VatInitChecked = "vat_init_checked"
VatMoveChecked = "vat_move_checked"
VatSlipChecked = "vat_slip_checked"
VatTollChecked = "vat_toll_checked"
VatTuneChecked = "vat_tune_checked"
VowFlogChecked = "vow_flog_checked"
) )

View File

@ -23,7 +23,7 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/vow_flog" "github.com/vulcanize/vulcanizedb/pkg/transformers/vow_flog"
) )
var EthFlogLog = types.Log{ var EthVowFlogLog = types.Log{
Address: common.HexToAddress(constants.VowContractAddress), Address: common.HexToAddress(constants.VowContractAddress),
Topics: []common.Hash{ Topics: []common.Hash{
common.HexToHash("0x35aee16f00000000000000000000000000000000000000000000000000000000"), common.HexToHash("0x35aee16f00000000000000000000000000000000000000000000000000000000"),
@ -40,10 +40,10 @@ var EthFlogLog = types.Log{
Removed: false, Removed: false,
} }
var rawFlogLog, _ = json.Marshal(EthFlogLog) var rawVowFlogLog, _ = json.Marshal(EthVowFlogLog)
var FlogModel = vow_flog.VowFlogModel{ var VowFlogModel = vow_flog.VowFlogModel{
Era: "1337", Era: "1337",
LogIndex: EthFlogLog.Index, LogIndex: EthVowFlogLog.Index,
TransactionIndex: EthFlogLog.TxIndex, TransactionIndex: EthVowFlogLog.TxIndex,
Raw: rawFlogLog, Raw: rawVowFlogLog,
} }

View File

@ -18,7 +18,8 @@ import (
"fmt" "fmt"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"log" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
) )
type VatInitRepository struct { type VatInitRepository struct {
@ -38,7 +39,6 @@ func (repository VatInitRepository) Create(headerID int64, models []interface{})
return fmt.Errorf("model of type %T, not %T", model, VatInitModel{}) return fmt.Errorf("model of type %T, not %T", model, VatInitModel{})
} }
log.Printf("VatInit model: %v", vatInit)
_, err = tx.Exec( _, err = tx.Exec(
`INSERT INTO maker.vat_init (header_id, ilk, log_idx, tx_idx, raw_log) `INSERT INTO maker.vat_init (header_id, ilk, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5)`, VALUES($1, $2, $3, $4, $5)`,
@ -46,15 +46,11 @@ func (repository VatInitRepository) Create(headerID int64, models []interface{})
) )
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
log.Printf("Error: %v \n", err)
return err return err
} }
} }
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vat_init_checked) err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.VatInitChecked)
VALUES($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_init_checked = $2`, headerID, true)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -64,28 +60,11 @@ func (repository VatInitRepository) Create(headerID int64, models []interface{})
} }
func (repository VatInitRepository) MarkHeaderChecked(headerID int64) error { func (repository VatInitRepository) MarkHeaderChecked(headerID int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_init_checked) return shared.MarkHeaderChecked(headerID, repository.db, constants.VatInitChecked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_init_checked = $2`, headerID, true)
return err
} }
func (repository VatInitRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository VatInitRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.VatInitChecked)
err := repository.db.Select(
&result,
`SELECT headers.id, headers.block_number FROM headers
LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR vat_init_checked IS FALSE)
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlockNumber,
endingBlockNumber,
repository.db.Node.ID,
)
return result, err
} }
func (repository *VatInitRepository) SetDB(db *postgres.DB) { func (repository *VatInitRepository) SetDB(db *postgres.DB) {

View File

@ -15,13 +15,11 @@
package vat_init_test package vat_init_test
import ( import (
"database/sql"
"math/rand"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/fakes"
@ -33,29 +31,35 @@ import (
var _ = Describe("Vat init repository", func() { var _ = Describe("Vat init repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
vatInitRepository vat_init.VatInitRepository repository vat_init.VatInitRepository
headerRepository repositories.HeaderRepository
err error
) )
BeforeEach(func() { BeforeEach(func() {
db = test_config.NewTestDB(core.Node{}) db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
vatInitRepository = vat_init.VatInitRepository{} repository = vat_init.VatInitRepository{}
vatInitRepository.SetDB(db) repository.SetDB(db)
headerRepository = repositories.NewHeaderRepository(db)
}) })
Describe("Create", func() { Describe("Create", func() {
var headerID int64 modelWithDifferentLogIdx := test_data.VatInitModel
modelWithDifferentLogIdx.LogIndex++
inputs := shared_behaviors.CreateBehaviorInputs{
CheckedHeaderColumnName: constants.VatInitChecked,
LogEventTableName: "maker.vat_init",
TestModel: test_data.VatInitModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
It("persists vat init records", func() {
headerRepository := repositories.NewHeaderRepository(db)
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
})
It("adds a vat event", func() { err = repository.Create(headerID, []interface{}{test_data.VatInitModel})
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbVatInit vat_init.VatInitModel var dbVatInit vat_init.VatInitModel
@ -66,157 +70,23 @@ var _ = Describe("Vat init repository", func() {
Expect(dbVatInit.TransactionIndex).To(Equal(test_data.VatInitModel.TransactionIndex)) Expect(dbVatInit.TransactionIndex).To(Equal(test_data.VatInitModel.TransactionIndex))
Expect(dbVatInit.Raw).To(MatchJSON(test_data.VatInitModel.Raw)) Expect(dbVatInit.Raw).To(MatchJSON(test_data.VatInitModel.Raw))
}) })
It("does not duplicate vat events", func() {
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("removes vat if corresponding header is deleted", func() {
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbVatInit vat_init.VatInitModel
err = db.Get(&dbVatInit, `SELECT ilk, log_idx, tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})
It("marks the header as checked for vat init logs", func() {
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_init_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates the header to checked if checked headers row already exists", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
Expect(err).NotTo(HaveOccurred())
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_init_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("returns an error if model is of wrong type", func() {
err = vatInitRepository.Create(headerID, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type"))
})
})
Describe("MarkHeaderChecked", func() {
var headerID int64
BeforeEach(func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("creates a row for a new headerID", func() {
err = vatInitRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_init_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates row when headerID already exists", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
err = vatInitRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_init_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
}) })
Describe("MissingHeaders", func() { Describe("MissingHeaders", func() {
var ( inputs := shared_behaviors.MissingHeadersBehaviorInputs{
startingBlock, vatInitBlock, endingBlock int64 Repository: &repository,
blockNumbers, headerIDs []int64 RepositoryTwo: &vat_init.VatInitRepository{},
)
BeforeEach(func() {
startingBlock = rand.Int63()
vatInitBlock = startingBlock + 1
endingBlock = startingBlock + 2
blockNumbers = []int64{startingBlock, vatInitBlock, endingBlock, endingBlock + 1}
headerIDs = []int64{}
for _, n := range blockNumbers {
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
headerIDs = append(headerIDs, headerID)
}
})
It("returns headers that haven't been checked", func() {
err := vatInitRepository.MarkHeaderChecked(headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := vatInitRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(2))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock)))
})
It("only treats headers as checked if vat init logs have been checked", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := vatInitRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(3))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatInitBlock)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatInitBlock)))
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatInitBlock)))
})
It("only returns headers associated with the current node", func() {
dbTwo := test_config.NewTestDB(core.Node{ID: "second"})
headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo)
for _, n := range blockNumbers {
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
} }
vatInitRepositoryTwo := vat_init.VatInitRepository{} shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
vatInitRepositoryTwo.SetDB(dbTwo) })
err = vatInitRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := vatInitRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) Describe("MarkHeaderChecked", func() {
Expect(err).NotTo(HaveOccurred()) inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) CheckedHeaderColumnName: constants.VatInitChecked,
Repository: &repository,
}
nodeTwoMissingHeaders, err := vatInitRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
})
}) })
}) })

View File

@ -18,6 +18,8 @@ import (
"fmt" "fmt"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
) )
type VatMoveRepository struct { type VatMoveRepository struct {
@ -49,13 +51,7 @@ func (repository VatMoveRepository) Create(headerID int64, models []interface{})
} }
} }
_, err = tx.Exec( err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.VatMoveChecked)
`INSERT INTO public.checked_headers (header_id, vat_move_checked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_move_checked = $2`,
headerID, true)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -65,29 +61,11 @@ func (repository VatMoveRepository) Create(headerID int64, models []interface{})
} }
func (repository VatMoveRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository VatMoveRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.VatMoveChecked)
err := repository.db.Select(
&result,
`SELECT headers.id, headers.block_number FROM headers
LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR vat_move_checked IS FALSE)
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlockNumber,
endingBlockNumber,
repository.db.Node.ID,
)
return result, err
} }
func (repository VatMoveRepository) MarkHeaderChecked(headerID int64) error { func (repository VatMoveRepository) MarkHeaderChecked(headerID int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_move_checked) return shared.MarkHeaderChecked(headerID, repository.db, constants.VatMoveChecked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_move_checked = $2`, headerID, true)
return err
} }
func (repository *VatMoveRepository) SetDB(db *postgres.DB) { func (repository *VatMoveRepository) SetDB(db *postgres.DB) {

View File

@ -15,44 +15,48 @@
package vat_move_test package vat_move_test
import ( import (
"database/sql"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"math/rand"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_move" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_move"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
var _ = Describe("Vat Move", func() { var _ = Describe("Vat Move", func() {
var db *postgres.DB var db *postgres.DB
var headerRepository repositories.HeaderRepository var repository vat_move.VatMoveRepository
var vatMoveRepository vat_move.VatMoveRepository
BeforeEach(func() { BeforeEach(func() {
db = test_config.NewTestDB(core.Node{}) db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db) repository = vat_move.VatMoveRepository{}
vatMoveRepository = vat_move.VatMoveRepository{} repository.SetDB(db)
vatMoveRepository.SetDB(db)
}) })
Describe("Create", func() { Describe("Create", func() {
var headerID int64 modelWithDifferentLogIdx := test_data.VatMoveModel
var err error modelWithDifferentLogIdx.LogIndex++
inputs := shared_behaviors.CreateBehaviorInputs{
CheckedHeaderColumnName: constants.VatMoveChecked,
LogEventTableName: "maker.vat_move",
TestModel: test_data.VatMoveModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
It("persists vat move records", func() {
headerRepository := repositories.NewHeaderRepository(db)
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
})
It("adds a vat move event", func() { err = repository.Create(headerID, []interface{}{test_data.VatMoveModel})
err = vatMoveRepository.Create(headerID, []interface{}{test_data.VatMoveModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbVatMove vat_move.VatMoveModel var dbVatMove vat_move.VatMoveModel
@ -65,162 +69,23 @@ var _ = Describe("Vat Move", func() {
Expect(dbVatMove.TransactionIndex).To(Equal(test_data.VatMoveModel.TransactionIndex)) Expect(dbVatMove.TransactionIndex).To(Equal(test_data.VatMoveModel.TransactionIndex))
Expect(dbVatMove.Raw).To(MatchJSON(test_data.VatMoveModel.Raw)) Expect(dbVatMove.Raw).To(MatchJSON(test_data.VatMoveModel.Raw))
}) })
It("marks header id as checked for vat move logs", func() {
err = vatMoveRepository.Create(headerID, []interface{}{test_data.VatMoveModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_move_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates the header to checked if checked headers row already exists", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
Expect(err).NotTo(HaveOccurred())
err = vatMoveRepository.Create(headerID, []interface{}{test_data.VatMoveModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_move_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("returns an error if insertion fails", func() {
err = vatMoveRepository.Create(headerID, []interface{}{test_data.VatMoveModel})
Expect(err).NotTo(HaveOccurred())
err = vatMoveRepository.Create(headerID, []interface{}{test_data.VatMoveModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("removes vat move event if corresponding header is deleted", func() {
err = vatMoveRepository.Create(headerID, []interface{}{test_data.VatMoveModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbVatMove vat_move.VatMoveModel
err = db.Get(&dbVatMove, `SELECT src, dst, rad, log_idx, tx_idx, raw_log FROM maker.vat_move WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})
It("returns an error if model is of wrong type", func() {
err = vatMoveRepository.Create(headerID, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type"))
})
}) })
Describe("MissingHeaders", func() { Describe("MissingHeaders", func() {
var eventBlockNumber = rand.Int63() inputs := shared_behaviors.MissingHeadersBehaviorInputs{
var startingBlockNumber = eventBlockNumber - 1 Repository: &repository,
var endingBlockNumber = eventBlockNumber + 1 RepositoryTwo: &vat_move.VatMoveRepository{},
var outOfRangeBlockNumber = eventBlockNumber + 2
It("returns headers haven't been checked", func() {
var headerIds []int64
for _, number := range []int64{startingBlockNumber, eventBlockNumber, endingBlockNumber, outOfRangeBlockNumber} {
headerId, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(number))
Expect(err).NotTo(HaveOccurred())
headerIds = append(headerIds, headerId)
} }
err := vatMoveRepository.MarkHeaderChecked(headerIds[1]) shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
Expect(err).NotTo(HaveOccurred())
headers, err := vatMoveRepository.MissingHeaders(startingBlockNumber, endingBlockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(2))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber)))
})
It("only treats headers as checked if vat_move has been checked", func() {
var headerIds []int64
for _, number := range []int64{startingBlockNumber, eventBlockNumber, endingBlockNumber, outOfRangeBlockNumber} {
headerId, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(number))
Expect(err).NotTo(HaveOccurred())
headerIds = append(headerIds, headerId)
}
// Just creates row, doesn't set this header as checked
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIds[1])
Expect(err).NotTo(HaveOccurred())
headers, err := vatMoveRepository.MissingHeaders(startingBlockNumber, endingBlockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(3))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(eventBlockNumber)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(eventBlockNumber)))
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(eventBlockNumber)))
})
It("only returns headers associated with the current node", func() {
blockNumbers := []int64{1, 2, 3}
dbTwo := test_config.NewTestDB(core.Node{ID: "second"})
headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo)
var headerIDs []int64
for _, n := range blockNumbers {
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
headerIDs = append(headerIDs, headerID)
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
}
vatMoveRepositoryTwo := vat_move.VatMoveRepository{}
vatMoveRepositoryTwo.SetDB(dbTwo)
err := vatMoveRepository.Create(headerIDs[0], []interface{}{test_data.VatMoveModel})
Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := vatMoveRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1))
nodeTwoMissingHeaders, err := vatMoveRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
})
}) })
Describe("MarkHeaderChecked", func() { Describe("MarkHeaderChecked", func() {
var headerID int64 inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
var err error CheckedHeaderColumnName: constants.VatMoveChecked,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("creates a new row for a new headerID", func() {
err = vatMoveRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_move_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates row when headerId already exists", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
Expect(err).NotTo(HaveOccurred())
err = vatMoveRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_move_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
}) })
}) })

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
) )
type VatSlipRepository struct { type VatSlipRepository struct {
@ -32,10 +34,8 @@ func (repository VatSlipRepository) Create(headerID int64, models []interface{})
return err return err
} }
} }
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vat_slip_checked)
VALUES ($1, $2) err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.VatSlipChecked)
ON CONFLICT (header_id) DO
UPDATE SET vat_slip_checked = $2`, headerID, true)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -45,28 +45,11 @@ func (repository VatSlipRepository) Create(headerID int64, models []interface{})
} }
func (repository VatSlipRepository) MarkHeaderChecked(headerID int64) error { func (repository VatSlipRepository) MarkHeaderChecked(headerID int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_slip_checked) return shared.MarkHeaderChecked(headerID, repository.db, constants.VatSlipChecked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_slip_checked = $2`, headerID, true)
return err
} }
func (repository VatSlipRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository VatSlipRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.VatSlipChecked)
err := repository.db.Select(
&result,
`SELECT headers.id, headers.block_number FROM headers
LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR vat_slip_checked IS FALSE)
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlockNumber,
endingBlockNumber,
repository.db.Node.ID,
)
return result, err
} }
func (repository *VatSlipRepository) SetDB(db *postgres.DB) { func (repository *VatSlipRepository) SetDB(db *postgres.DB) {

View File

@ -1,14 +1,11 @@
package vat_slip_test package vat_slip_test
import ( import (
"database/sql"
"math/rand"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/fakes"
@ -20,29 +17,35 @@ import (
var _ = Describe("Vat slip repository", func() { var _ = Describe("Vat slip repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
vatSlipRepository vat_slip.VatSlipRepository repository vat_slip.VatSlipRepository
err error
headerRepository datastore.HeaderRepository
) )
BeforeEach(func() { BeforeEach(func() {
db = test_config.NewTestDB(core.Node{}) db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db) repository = vat_slip.VatSlipRepository{}
vatSlipRepository = vat_slip.VatSlipRepository{} repository.SetDB(db)
vatSlipRepository.SetDB(db)
}) })
Describe("Create", func() { Describe("Create", func() {
var headerID int64 modelWithDifferentLogIdx := test_data.VatSlipModel
modelWithDifferentLogIdx.LogIndex++
inputs := shared_behaviors.CreateBehaviorInputs{
CheckedHeaderColumnName: constants.VatSlipChecked,
LogEventTableName: "maker.vat_slip",
TestModel: test_data.VatSlipModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("adds a vat slip event", func() { It("adds a vat slip event", func() {
err = vatSlipRepository.Create(headerID, []interface{}{test_data.VatSlipModel}) headerRepository := repositories.NewHeaderRepository(db)
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = repository.Create(headerID, []interface{}{test_data.VatSlipModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbVatSlip vat_slip.VatSlipModel var dbVatSlip vat_slip.VatSlipModel
@ -55,166 +58,23 @@ var _ = Describe("Vat slip repository", func() {
Expect(dbVatSlip.LogIndex).To(Equal(test_data.VatSlipModel.LogIndex)) Expect(dbVatSlip.LogIndex).To(Equal(test_data.VatSlipModel.LogIndex))
Expect(dbVatSlip.Raw).To(MatchJSON(test_data.VatSlipModel.Raw)) Expect(dbVatSlip.Raw).To(MatchJSON(test_data.VatSlipModel.Raw))
}) })
It("marks header as checked for logs", func() {
err = vatSlipRepository.Create(headerID, []interface{}{test_data.VatSlipModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_slip_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates the header to checked if checked headers row already exists", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
Expect(err).NotTo(HaveOccurred())
err = vatSlipRepository.Create(headerID, []interface{}{test_data.VatSlipModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_slip_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("does not duplicate vat slip events", func() {
err = vatSlipRepository.Create(headerID, []interface{}{test_data.VatSlipModel})
Expect(err).NotTo(HaveOccurred())
err = vatSlipRepository.Create(headerID, []interface{}{test_data.VatSlipModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("allows for multiple vat slip events in one transaction if they have different log indexes", func() {
err = vatSlipRepository.Create(headerID, []interface{}{test_data.VatSlipModel})
Expect(err).NotTo(HaveOccurred())
newVatSlip := test_data.VatSlipModel
newVatSlip.LogIndex = newVatSlip.LogIndex + 1
err := vatSlipRepository.Create(headerID, []interface{}{newVatSlip})
Expect(err).NotTo(HaveOccurred())
})
It("removes vat slip if corresponding header is deleted", func() {
err = vatSlipRepository.Create(headerID, []interface{}{test_data.VatSlipModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbVatSlip vat_slip.VatSlipModel
err = db.Get(&dbVatSlip, `SELECT ilk, guy, rad, tx_idx, raw_log FROM maker.vat_slip WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})
It("returns an error if model is of wrong type", func() {
err = vatSlipRepository.Create(headerID, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type"))
})
}) })
Describe("MarkHeaderChecked", func() { Describe("MarkHeaderChecked", func() {
var headerID int64 inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
CheckedHeaderColumnName: constants.VatSlipChecked,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("creates a row for a new headerID", func() {
err = vatSlipRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_slip_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates row when headerID already exists", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
err = vatSlipRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_slip_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
}) })
Describe("MissingHeaders", func() { Describe("MissingHeaders", func() {
var ( inputs := shared_behaviors.MissingHeadersBehaviorInputs{
startingBlock, endingBlock, vatSlipBlock int64 Repository: &repository,
blockNumbers, headerIDs []int64 RepositoryTwo: &vat_slip.VatSlipRepository{},
)
BeforeEach(func() {
startingBlock = rand.Int63()
vatSlipBlock = startingBlock + 1
endingBlock = startingBlock + 2
blockNumbers = []int64{startingBlock, vatSlipBlock, endingBlock, endingBlock + 1}
headerIDs = []int64{}
for _, n := range blockNumbers {
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
headerIDs = append(headerIDs, headerID)
} }
})
It("returns headers that haven't been checked", func() { shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
err := vatSlipRepository.MarkHeaderChecked(headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := vatSlipRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(2))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock)))
})
It("only treats headers as checked if vat slip logs have been checked", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := vatSlipRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(3))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatSlipBlock)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatSlipBlock)))
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatSlipBlock)))
})
It("only returns headers associated with the current node", func() {
dbTwo := test_config.NewTestDB(core.Node{ID: "second"})
headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo)
for _, n := range blockNumbers {
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
}
vatSlipRepositoryTwo := vat_slip.VatSlipRepository{}
vatSlipRepositoryTwo.SetDB(dbTwo)
err := vatSlipRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := vatSlipRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1))
nodeTwoMissingHeaders, err := vatSlipRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
})
}) })
}) })

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
) )
type VatTollRepository struct { type VatTollRepository struct {
@ -31,10 +33,8 @@ func (repository VatTollRepository) Create(headerID int64, models []interface{})
return err return err
} }
} }
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vat_toll_checked)
VALUES ($1, $2) err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.VatTollChecked)
ON CONFLICT (header_id) DO
UPDATE SET vat_toll_checked = $2`, headerID, true)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -43,28 +43,11 @@ func (repository VatTollRepository) Create(headerID int64, models []interface{})
} }
func (repository VatTollRepository) MarkHeaderChecked(headerID int64) error { func (repository VatTollRepository) MarkHeaderChecked(headerID int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_toll_checked) return shared.MarkHeaderChecked(headerID, repository.db, constants.VatTollChecked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_toll_checked = $2`, headerID, true)
return err
} }
func (repository VatTollRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository VatTollRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.VatTollChecked)
err := repository.db.Select(
&result,
`SELECT headers.id, headers.block_number FROM headers
LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR vat_toll_checked IS FALSE)
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlockNumber,
endingBlockNumber,
repository.db.Node.ID,
)
return result, err
} }
func (repository *VatTollRepository) SetDB(db *postgres.DB) { func (repository *VatTollRepository) SetDB(db *postgres.DB) {

View File

@ -1,48 +1,51 @@
package vat_toll_test package vat_toll_test
import ( import (
"database/sql" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
"math/rand"
) )
var _ = Describe("Vat toll repository", func() { var _ = Describe("Vat toll repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
vatTollRepository vat_toll.VatTollRepository repository vat_toll.VatTollRepository
err error
headerRepository datastore.HeaderRepository
) )
BeforeEach(func() { BeforeEach(func() {
db = test_config.NewTestDB(core.Node{}) db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db) repository = vat_toll.VatTollRepository{}
vatTollRepository = vat_toll.VatTollRepository{} repository.SetDB(db)
vatTollRepository.SetDB(db)
}) })
Describe("Create", func() { Describe("Create", func() {
var headerID int64 modelWithDifferentLogIdx := test_data.VatTollModel
modelWithDifferentLogIdx.LogIndex++
inputs := shared_behaviors.CreateBehaviorInputs{
CheckedHeaderColumnName: constants.VatTollChecked,
LogEventTableName: "maker.vat_toll",
TestModel: test_data.VatTollModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("adds a vat toll event", func() { It("adds a vat toll event", func() {
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel}) headerRepository := repositories.NewHeaderRepository(db)
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = repository.Create(headerID, []interface{}{test_data.VatTollModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbVatToll vat_toll.VatTollModel var dbVatToll vat_toll.VatTollModel
@ -55,163 +58,23 @@ var _ = Describe("Vat toll repository", func() {
Expect(dbVatToll.LogIndex).To(Equal(test_data.VatTollModel.LogIndex)) Expect(dbVatToll.LogIndex).To(Equal(test_data.VatTollModel.LogIndex))
Expect(dbVatToll.Raw).To(MatchJSON(test_data.VatTollModel.Raw)) Expect(dbVatToll.Raw).To(MatchJSON(test_data.VatTollModel.Raw))
}) })
It("marks header as checked for logs", func() {
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_toll_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates the header to checked if checked headers row already exists", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
Expect(err).NotTo(HaveOccurred())
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_toll_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("does not duplicate vat toll events", func() {
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
Expect(err).NotTo(HaveOccurred())
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("allows for multiple vat toll events in one transaction if they have different log indexes", func() {
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
Expect(err).NotTo(HaveOccurred())
newVatToll := test_data.VatTollModel
newVatToll.LogIndex = newVatToll.LogIndex + 1
err := vatTollRepository.Create(headerID, []interface{}{newVatToll})
Expect(err).NotTo(HaveOccurred())
})
It("removes vat toll if corresponding header is deleted", func() {
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbVatToll vat_toll.VatTollModel
err = db.Get(&dbVatToll, `SELECT ilk, urn, take, tx_idx, raw_log FROM maker.vat_toll WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})
It("returns an error if model is of wrong type", func() {
err = vatTollRepository.Create(headerID, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type"))
})
}) })
Describe("MarkHeaderChecked", func() { Describe("MarkHeaderChecked", func() {
var headerID int64 inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
CheckedHeaderColumnName: constants.VatTollChecked,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("creates a row for a new headerID", func() {
err = vatTollRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_toll_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates row when headerID already exists", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
err = vatTollRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_toll_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
}) })
Describe("MissingHeaders", func() { Describe("MissingHeaders", func() {
var ( inputs := shared_behaviors.MissingHeadersBehaviorInputs{
startingBlock, endingBlock, vatTollBlock int64 Repository: &repository,
blockNumbers, headerIDs []int64 RepositoryTwo: &vat_toll.VatTollRepository{},
)
BeforeEach(func() {
startingBlock = rand.Int63()
vatTollBlock = startingBlock + 1
endingBlock = startingBlock + 2
blockNumbers = []int64{startingBlock, vatTollBlock, endingBlock, endingBlock + 1}
headerIDs = []int64{}
for _, n := range blockNumbers {
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
headerIDs = append(headerIDs, headerID)
} }
})
It("returns headers that haven't been checked", func() { shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
err := vatTollRepository.MarkHeaderChecked(headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := vatTollRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(2))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock)))
})
It("only treats headers as checked if vat toll logs have been checked", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := vatTollRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(3))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatTollBlock)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatTollBlock)))
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatTollBlock)))
})
It("only returns headers associated with the current node", func() {
dbTwo := test_config.NewTestDB(core.Node{ID: "second"})
headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo)
for _, n := range blockNumbers {
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
}
vatTollRepositoryTwo := vat_toll.VatTollRepository{}
vatTollRepositoryTwo.SetDB(dbTwo)
err := vatTollRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := vatTollRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1))
nodeTwoMissingHeaders, err := vatTollRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
})
}) })
}) })

View File

@ -1,48 +1,51 @@
package vat_tune_test package vat_tune_test
import ( import (
"database/sql" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_tune" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_tune"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
"math/rand"
) )
var _ = Describe("Vat tune repository", func() { var _ = Describe("Vat tune repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
vatTuneRepository vat_tune.VatTuneRepository repository vat_tune.VatTuneRepository
err error
headerRepository datastore.HeaderRepository
) )
BeforeEach(func() { BeforeEach(func() {
db = test_config.NewTestDB(core.Node{}) db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db) repository = vat_tune.VatTuneRepository{}
vatTuneRepository = vat_tune.VatTuneRepository{} repository.SetDB(db)
vatTuneRepository.SetDB(db)
}) })
Describe("Create", func() { Describe("Create", func() {
var headerID int64 modelWithDifferentLogIdx := test_data.VatTuneModel
modelWithDifferentLogIdx.LogIndex++
BeforeEach(func() { inputs := shared_behaviors.CreateBehaviorInputs{
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) CheckedHeaderColumnName: constants.VatTuneChecked,
Expect(err).NotTo(HaveOccurred()) LogEventTableName: "maker.vat_heal",
}) TestModel: test_data.VatTuneModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &repository,
}
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
It("adds a vat tune event", func() { It("adds a vat tune event", func() {
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel}) headerRepository := repositories.NewHeaderRepository(db)
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = repository.Create(headerID, []interface{}{test_data.VatTuneModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbVatTune vat_tune.VatTuneModel var dbVatTune vat_tune.VatTuneModel
@ -58,166 +61,23 @@ var _ = Describe("Vat tune repository", func() {
Expect(dbVatTune.LogIndex).To(Equal(test_data.VatTuneModel.LogIndex)) Expect(dbVatTune.LogIndex).To(Equal(test_data.VatTuneModel.LogIndex))
Expect(dbVatTune.Raw).To(MatchJSON(test_data.VatTuneModel.Raw)) Expect(dbVatTune.Raw).To(MatchJSON(test_data.VatTuneModel.Raw))
}) })
It("marks header as checked for logs", func() {
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_tune_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates the header to checked if checked headers row already exists", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
Expect(err).NotTo(HaveOccurred())
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_tune_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("does not duplicate vat tune events", func() {
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
Expect(err).NotTo(HaveOccurred())
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("allows for multiple vat tune events in one transaction if they have different log indexes", func() {
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
Expect(err).NotTo(HaveOccurred())
newVatTune := test_data.VatTuneModel
newVatTune.LogIndex = newVatTune.LogIndex + 1
err := vatTuneRepository.Create(headerID, []interface{}{newVatTune})
Expect(err).NotTo(HaveOccurred())
})
It("removes vat tune if corresponding header is deleted", func() {
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbVatTune vat_tune.VatTuneModel
err = db.Get(&dbVatTune, `SELECT ilk, urn, v, w, dink, dart, tx_idx, raw_log FROM maker.vat_tune WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})
It("returns an error if model is of wrong type", func() {
err = vatTuneRepository.Create(headerID, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type"))
})
}) })
Describe("MarkHeaderChecked", func() { Describe("MarkHeaderChecked", func() {
var headerID int64 inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
CheckedHeaderColumnName: constants.VatTuneChecked,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("creates a row for a new headerID", func() {
err = vatTuneRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_tune_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates row when headerID already exists", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
err = vatTuneRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_tune_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
}) })
Describe("MissingHeaders", func() { Describe("MissingHeaders", func() {
var ( inputs := shared_behaviors.MissingHeadersBehaviorInputs{
startingBlockNumber, vatTuneBlockNumber, endingBlockNumber int64 Repository: &repository,
blockNumbers, headerIDs []int64 RepositoryTwo: &vat_tune.VatTuneRepository{},
)
BeforeEach(func() {
startingBlockNumber = rand.Int63()
vatTuneBlockNumber = startingBlockNumber + 1
endingBlockNumber = startingBlockNumber + 2
blockNumbers = []int64{startingBlockNumber, vatTuneBlockNumber, endingBlockNumber, endingBlockNumber + 1}
headerIDs = []int64{}
for _, n := range blockNumbers {
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
headerIDs = append(headerIDs, headerID)
} }
})
It("returns headers that haven't been checked", func() { shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
err := vatTuneRepository.MarkHeaderChecked(headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := vatTuneRepository.MissingHeaders(startingBlockNumber, endingBlockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(2))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber)))
})
It("only treats headers as checked if vat tune logs have been checked", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := vatTuneRepository.MissingHeaders(startingBlockNumber, endingBlockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(3))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(vatTuneBlockNumber)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(vatTuneBlockNumber)))
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(vatTuneBlockNumber)))
})
It("only returns headers associated with the current node", func() {
dbTwo := test_config.NewTestDB(core.Node{ID: "second"})
headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo)
for _, n := range blockNumbers {
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
}
vatTuneRepositoryTwo := vat_tune.VatTuneRepository{}
vatTuneRepositoryTwo.SetDB(dbTwo)
err := vatTuneRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := vatTuneRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1))
nodeTwoMissingHeaders, err := vatTuneRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
})
}) })
}) })

View File

@ -49,10 +49,10 @@ var _ = Describe("Vow flog converter", func() {
}) })
It("converts a log to a model", func() { It("converts a log to a model", func() {
models, err := converter.ToModels([]types.Log{test_data.EthFlogLog}) models, err := converter.ToModels([]types.Log{test_data.EthVowFlogLog})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(len(models)).To(Equal(1)) Expect(len(models)).To(Equal(1))
Expect(models[0].(vow_flog.VowFlogModel)).To(Equal(test_data.FlogModel)) Expect(models[0].(vow_flog.VowFlogModel)).To(Equal(test_data.VowFlogModel))
}) })
}) })

View File

@ -18,6 +18,8 @@ import (
"fmt" "fmt"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
) )
type VowFlogRepository struct { type VowFlogRepository struct {
@ -48,41 +50,22 @@ func (repository VowFlogRepository) Create(headerID int64, models []interface{})
return err return err
} }
} }
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vow_flog_checked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vow_flog_checked = $2`, headerID, true)
err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.VowFlogChecked)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
} }
return tx.Commit() return tx.Commit()
} }
func (repository VowFlogRepository) MarkHeaderChecked(headerID int64) error { func (repository VowFlogRepository) MarkHeaderChecked(headerID int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vow_flog_checked) return shared.MarkHeaderChecked(headerID, repository.db, constants.VowFlogChecked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vow_flog_checked = $2`, headerID, true)
return err
} }
func (repository VowFlogRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository VowFlogRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.VowFlogChecked)
err := repository.db.Select(
&result,
`SELECT headers.id, headers.block_number FROM headers
LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR vow_flog_checked IS FALSE)
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlockNumber,
endingBlockNumber,
repository.db.Node.ID,
)
return result, err
} }
func (repository *VowFlogRepository) SetDB(db *postgres.DB) { func (repository *VowFlogRepository) SetDB(db *postgres.DB) {

View File

@ -15,14 +15,11 @@
package vow_flog_test package vow_flog_test
import ( import (
"database/sql"
"math/rand"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/fakes"
@ -34,185 +31,62 @@ import (
var _ = Describe("Vow flog repository", func() { var _ = Describe("Vow flog repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
flogRepository vow_flog.VowFlogRepository repository vow_flog.VowFlogRepository
err error
headerRepository datastore.HeaderRepository
) )
BeforeEach(func() { BeforeEach(func() {
db = test_config.NewTestDB(core.Node{}) db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db) repository = vow_flog.VowFlogRepository{}
flogRepository = vow_flog.VowFlogRepository{} repository.SetDB(db)
flogRepository.SetDB(db)
}) })
Describe("Create", func() { Describe("Create", func() {
var headerID int64 modelWithDifferentLogIdx := test_data.VowFlogModel
modelWithDifferentLogIdx.LogIndex++
inputs := shared_behaviors.CreateBehaviorInputs{
CheckedHeaderColumnName: constants.VowFlogChecked,
LogEventTableName: "maker.vow_flog",
TestModel: test_data.VowFlogModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("adds a vow flog event", func() { It("adds a vow flog event", func() {
err = flogRepository.Create(headerID, []interface{}{test_data.FlogModel}) headerRepository := repositories.NewHeaderRepository(db)
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = repository.Create(headerID, []interface{}{test_data.VowFlogModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbFlog vow_flog.VowFlogModel var dbFlog vow_flog.VowFlogModel
err = db.Get(&dbFlog, `SELECT era, log_idx, tx_idx, raw_log FROM maker.vow_flog WHERE header_id = $1`, headerID) err = db.Get(&dbFlog, `SELECT era, log_idx, tx_idx, raw_log FROM maker.vow_flog WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(dbFlog.Era).To(Equal(test_data.FlogModel.Era)) Expect(dbFlog.Era).To(Equal(test_data.VowFlogModel.Era))
Expect(dbFlog.LogIndex).To(Equal(test_data.FlogModel.LogIndex)) Expect(dbFlog.LogIndex).To(Equal(test_data.VowFlogModel.LogIndex))
Expect(dbFlog.TransactionIndex).To(Equal(test_data.FlogModel.TransactionIndex)) Expect(dbFlog.TransactionIndex).To(Equal(test_data.VowFlogModel.TransactionIndex))
Expect(dbFlog.Raw).To(MatchJSON(test_data.FlogModel.Raw)) Expect(dbFlog.Raw).To(MatchJSON(test_data.VowFlogModel.Raw))
})
It("marks header as checked for logs", func() {
err = flogRepository.Create(headerID, []interface{}{test_data.FlogModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vow_flog_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates the header to checked if checked headers row already exists", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
Expect(err).NotTo(HaveOccurred())
err = flogRepository.Create(headerID, []interface{}{test_data.FlogModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vow_flog_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("does not duplicate vow flog events", func() {
err = flogRepository.Create(headerID, []interface{}{test_data.FlogModel})
Expect(err).NotTo(HaveOccurred())
err = flogRepository.Create(headerID, []interface{}{test_data.FlogModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("removes vow flog events if corresponding header is deleted", func() {
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbFlog vow_flog.VowFlogModel
err = db.Get(&dbFlog, `SELECT era, log_idx, tx_idx, raw_log FROM maker.vow_flog WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})
It("returns an error if model is of wrong type", func() {
err = flogRepository.Create(headerID, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type"))
}) })
}) })
Describe("MarkHeaderChecked", func() { Describe("MarkHeaderChecked", func() {
var headerID int64 inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
CheckedHeaderColumnName: constants.VowFlogChecked,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("creates a row for a new headerID", func() {
err = flogRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vow_flog_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("updates row when headerID already exists", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
err = flogRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vow_flog_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
}) })
Describe("MissingHeaders", func() { Describe("MissingHeaders", func() {
var ( inputs := shared_behaviors.MissingHeadersBehaviorInputs{
startingBlockNumber, flogBlockNumber, endingBlockNumber int64 Repository: &repository,
blockNumbers, headerIDs []int64 RepositoryTwo: &vow_flog.VowFlogRepository{},
)
BeforeEach(func() {
startingBlockNumber = rand.Int63()
flogBlockNumber = startingBlockNumber + 1
endingBlockNumber = startingBlockNumber + 2
blockNumbers = []int64{startingBlockNumber, flogBlockNumber, endingBlockNumber, endingBlockNumber + 1}
headerIDs = []int64{}
for _, n := range blockNumbers {
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
headerIDs = append(headerIDs, headerID)
} }
})
It("returns headers that haven't been checked", func() { shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
err := flogRepository.MarkHeaderChecked(headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := flogRepository.MissingHeaders(startingBlockNumber, endingBlockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(2))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber)))
})
It("only treats headers as checked if vow flog logs have been checked", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := flogRepository.MissingHeaders(startingBlockNumber, endingBlockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(3))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(flogBlockNumber)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(flogBlockNumber)))
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(flogBlockNumber)))
})
It("only returns headers associated with the current node", func() {
dbTwo := test_config.NewTestDB(core.Node{ID: "second"})
headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo)
for _, n := range blockNumbers {
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
}
flogRepositoryTwo := vow_flog.VowFlogRepository{}
flogRepositoryTwo.SetDB(dbTwo)
err := flogRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := flogRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1))
nodeTwoMissingHeaders, err := flogRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
})
}) })
}) })