diff --git a/pkg/transformers/integration_tests/vat_fold.go b/pkg/transformers/integration_tests/vat_fold.go index a0ccb12b..cfbfa4e4 100644 --- a/pkg/transformers/integration_tests/vat_fold.go +++ b/pkg/transformers/integration_tests/vat_fold.go @@ -19,6 +19,8 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/transformers/factories" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_fold" "github.com/vulcanize/vulcanizedb/test_config" ) @@ -41,8 +43,13 @@ var _ = Describe("VatFold Transformer", func() { err = persistHeader(db, blockNumber) Expect(err).NotTo(HaveOccurred()) - initializer := vat_fold.VatFoldTransformerInitializer{Config: config} - transformer := initializer.NewVatFoldTransformer(db, blockchain) + initializer := factories.Transformer{ + Config: config, + Fetcher: &shared.Fetcher{}, + Converter: &vat_fold.VatFoldConverter{}, + Repository: &vat_fold.VatFoldRepository{}, + } + transformer := initializer.NewTransformer(db, blockchain) err = transformer.Execute() Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/transformers/test_data/mocks/repository.go b/pkg/transformers/test_data/mocks/repository.go index c8ec6cb2..e4ea61b4 100644 --- a/pkg/transformers/test_data/mocks/repository.go +++ b/pkg/transformers/test_data/mocks/repository.go @@ -10,7 +10,7 @@ import ( type MockRepository struct { createError error markHeaderCheckedError error - markHeaderCheckedPassedHeaderIDs []int64 + MarkHeaderCheckedPassedHeaderIDs []int64 missingHeaders []core.Header missingHeadersError error PassedStartingBlockNumber int64 @@ -27,7 +27,7 @@ func (repository *MockRepository) Create(headerID int64, models []interface{}) e } func (repository *MockRepository) MarkHeaderChecked(headerID int64) error { - repository.markHeaderCheckedPassedHeaderIDs = append(repository.markHeaderCheckedPassedHeaderIDs, headerID) + repository.MarkHeaderCheckedPassedHeaderIDs = append(repository.MarkHeaderCheckedPassedHeaderIDs, headerID) return repository.markHeaderCheckedError } @@ -50,7 +50,7 @@ func (repository *MockRepository) SetMissingHeaders(headers []core.Header) { } func (repository *MockRepository) AssertMarkHeaderCheckedCalledWith(i int64) { - Expect(repository.markHeaderCheckedPassedHeaderIDs).To(ContainElement(i)) + Expect(repository.MarkHeaderCheckedPassedHeaderIDs).To(ContainElement(i)) } func (repository *MockRepository) SetMarkHeaderCheckedError(e error) { diff --git a/pkg/transformers/test_data/mocks/vat_fold/converter.go b/pkg/transformers/test_data/mocks/vat_fold/converter.go deleted file mode 100644 index c9b992bf..00000000 --- a/pkg/transformers/test_data/mocks/vat_fold/converter.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2018 Vulcanize -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package vat_fold - -import ( - "github.com/ethereum/go-ethereum/core/types" - - "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" - "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_fold" -) - -type MockVatFoldConverter struct { - converterErr error - PassedLogs []types.Log -} - -func (converter *MockVatFoldConverter) ToModels(ethLogs []types.Log) ([]vat_fold.VatFoldModel, error) { - converter.PassedLogs = ethLogs - return []vat_fold.VatFoldModel{test_data.VatFoldModel}, converter.converterErr -} - -func (converter *MockVatFoldConverter) SetConverterError(e error) { - converter.converterErr = e -} diff --git a/pkg/transformers/test_data/mocks/vat_fold/repository.go b/pkg/transformers/test_data/mocks/vat_fold/repository.go deleted file mode 100644 index bbe2fec2..00000000 --- a/pkg/transformers/test_data/mocks/vat_fold/repository.go +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright 2018 Vulcanize -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package vat_fold - -import ( - . "github.com/onsi/gomega" - - "github.com/vulcanize/vulcanizedb/pkg/core" - "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_fold" -) - -type MockVatFoldRepository struct { - createErr error - markHeaderCheckedErr error - MarkHeaderCheckedPassedHeaderID int64 - missingHeaders []core.Header - missingHeadersErr error - PassedStartingBlockNumber int64 - PassedEndingBlockNumber int64 - PassedHeaderID int64 - PassedModels []vat_fold.VatFoldModel -} - -func (repository *MockVatFoldRepository) Create(headerID int64, models []vat_fold.VatFoldModel) error { - repository.PassedHeaderID = headerID - repository.PassedModels = models - return repository.createErr -} - -func (repository *MockVatFoldRepository) MarkHeaderChecked(headerID int64) error { - repository.MarkHeaderCheckedPassedHeaderID = headerID - return repository.markHeaderCheckedErr -} - -func (repository *MockVatFoldRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - repository.PassedStartingBlockNumber = startingBlockNumber - repository.PassedEndingBlockNumber = endingBlockNumber - return repository.missingHeaders, repository.missingHeadersErr -} - -func (repository *MockVatFoldRepository) SetMarkHeaderCheckedErr(e error) { - repository.markHeaderCheckedErr = e -} - -func (repository *MockVatFoldRepository) SetMissingHeadersErr(e error) { - repository.missingHeadersErr = e -} - -func (repository *MockVatFoldRepository) SetMissingHeaders(headers []core.Header) { - repository.missingHeaders = headers -} - -func (repository *MockVatFoldRepository) SetCreateError(e error) { - repository.createErr = e -} - -func (repository *MockVatFoldRepository) AssertMarkHeaderCheckedCalledWith(i int64) { - Expect(repository.MarkHeaderCheckedPassedHeaderID).To(Equal(i)) -} diff --git a/pkg/transformers/transformers.go b/pkg/transformers/transformers.go index 83fd5015..c4e0348b 100644 --- a/pkg/transformers/transformers.go +++ b/pkg/transformers/transformers.go @@ -125,7 +125,12 @@ var ( }.NewTransformer VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer VatHealTransformerInitializer = vat_heal.VatHealTransformerInitializer{Config: vat_heal.VatHealConfig}.NewVatHealTransformer - VatFoldTransformerInitializer = vat_fold.VatFoldTransformerInitializer{Config: vat_fold.VatFoldConfig}.NewVatFoldTransformer + VatFoldTransformerInitializer = factories.Transformer{ + Config: vat_fold.VatFoldConfig, + Converter: &vat_fold.VatFoldConverter{}, + Repository: &vat_fold.VatFoldRepository{}, + Fetcher: &shared.Fetcher{}, + }.NewTransformer VatMoveTransformerInitializer = factories.Transformer{ Config: vat_move.VatMoveConfig, Converter: &vat_move.VatMoveConverter{}, diff --git a/pkg/transformers/vat_fold/config.go b/pkg/transformers/vat_fold/config.go index 56e8983d..747be3fd 100644 --- a/pkg/transformers/vat_fold/config.go +++ b/pkg/transformers/vat_fold/config.go @@ -18,10 +18,11 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" ) -var VatFoldConfig = shared.TransformerConfig{ +var VatFoldConfig = shared.SingleTransformerConfig{ + TransformerName: shared.VatFoldLabel, ContractAddresses: []string{shared.VatContractAddress}, ContractAbi: shared.VatABI, - Topics: []string{shared.VatFoldSignature}, + Topic: shared.VatFoldSignature, StartingBlockNumber: 0, EndingBlockNumber: 10000000, } diff --git a/pkg/transformers/vat_fold/converter.go b/pkg/transformers/vat_fold/converter.go index 95ade467..f22e64de 100644 --- a/pkg/transformers/vat_fold/converter.go +++ b/pkg/transformers/vat_fold/converter.go @@ -23,14 +23,10 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" ) -type Converter interface { - ToModels(ethLogs []types.Log) ([]VatFoldModel, error) -} - type VatFoldConverter struct{} -func (VatFoldConverter) ToModels(ethLogs []types.Log) ([]VatFoldModel, error) { - var models []VatFoldModel +func (VatFoldConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) { + var models []interface{} for _, ethLog := range ethLogs { err := verifyLog(ethLog) if err != nil { diff --git a/pkg/transformers/vat_fold/converter_test.go b/pkg/transformers/vat_fold/converter_test.go index 97d687ad..dc0afc7f 100644 --- a/pkg/transformers/vat_fold/converter_test.go +++ b/pkg/transformers/vat_fold/converter_test.go @@ -39,6 +39,6 @@ var _ = Describe("Vat fold converter", func() { model, err := converter.ToModels([]types.Log{test_data.EthVatFoldLog}) Expect(err).NotTo(HaveOccurred()) - Expect(model).To(Equal([]vat_fold.VatFoldModel{test_data.VatFoldModel})) + Expect(model).To(Equal([]interface{}{test_data.VatFoldModel})) }) }) diff --git a/pkg/transformers/vat_fold/repository.go b/pkg/transformers/vat_fold/repository.go index 6b60d20b..c336ea13 100644 --- a/pkg/transformers/vat_fold/repository.go +++ b/pkg/transformers/vat_fold/repository.go @@ -15,59 +15,55 @@ package vat_fold import ( + "fmt" + "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" ) -type Repository interface { - Create(headerID int64, models []VatFoldModel) error - MarkHeaderChecked(headerID int64) error - MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) -} - type VatFoldRepository struct { - DB *postgres.DB + db *postgres.DB } -func NewVatFoldRepository(db *postgres.DB) VatFoldRepository { - return VatFoldRepository{ - DB: db, - } -} - -func (repository VatFoldRepository) Create(headerID int64, models []VatFoldModel) error { - tx, err := repository.DB.Begin() +func (repository VatFoldRepository) Create(headerID int64, models []interface{}) error { + tx, err := repository.db.Begin() if err != nil { return err } for _, model := range models { + vatFold, ok := model.(VatFoldModel) + if !ok { + tx.Rollback() + return fmt.Errorf("model of type %T, not %T", model, VatFoldModel{}) + } + _, err = tx.Exec( `INSERT into maker.vat_fold (header_id, ilk, urn, rate, log_idx, tx_idx, raw_log) VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`, - headerID, model.Ilk, model.Urn, model.Rate, model.LogIndex, model.TransactionIndex, model.Raw, + headerID, vatFold.Ilk, vatFold.Urn, vatFold.Rate, vatFold.LogIndex, vatFold.TransactionIndex, vatFold.Raw, ) if err != nil { tx.Rollback() return err } - _, err = tx.Exec( - `INSERT INTO public.checked_headers (header_id, vat_fold_checked) + } + _, err = tx.Exec( + `INSERT INTO public.checked_headers (header_id, vat_fold_checked) VALUES($1, $2) ON CONFLICT (header_id) DO UPDATE SET vat_fold_checked = $2`, - headerID, true, - ) - if err != nil { - tx.Rollback() - return err - } + headerID, true, + ) + if err != nil { + tx.Rollback() + return err } return tx.Commit() } func (repository VatFoldRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.DB.Exec( + _, err := repository.db.Exec( `INSERT INTO public.checked_headers (header_id, vat_fold_checked) VALUES ($1, $2) ON CONFLICT (header_id) DO @@ -79,7 +75,7 @@ func (repository VatFoldRepository) MarkHeaderChecked(headerID int64) error { func (repository VatFoldRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { var result []core.Header - err := repository.DB.Select( + err := repository.db.Select( &result, `SELECT headers.id, headers.block_number FROM headers LEFT JOIN checked_headers on headers.id = header_id @@ -89,7 +85,11 @@ func (repository VatFoldRepository) MissingHeaders(startingBlockNumber, endingBl AND headers.eth_node_fingerprint = $3`, startingBlockNumber, endingBlockNumber, - repository.DB.Node.ID, + repository.db.Node.ID, ) return result, err } + +func (repository *VatFoldRepository) SetDB(db *postgres.DB) { + repository.db = db +} diff --git a/pkg/transformers/vat_fold/repository_test.go b/pkg/transformers/vat_fold/repository_test.go index 5ba12483..937b88df 100644 --- a/pkg/transformers/vat_fold/repository_test.go +++ b/pkg/transformers/vat_fold/repository_test.go @@ -16,6 +16,7 @@ package vat_fold_test import ( "database/sql" + "math/rand" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -41,7 +42,8 @@ var _ = Describe("Vat.fold repository", func() { node := test_config.NewTestNode() db = test_config.NewTestDB(node) test_config.CleanTestDB(db) - repository = vat_fold.NewVatFoldRepository(db) + repository = vat_fold.VatFoldRepository{} + repository.SetDB(db) headerRepository = repositories.NewHeaderRepository(db) }) @@ -53,8 +55,8 @@ var _ = Describe("Vat.fold repository", func() { Expect(err).NotTo(HaveOccurred()) }) - It("adds a vat event", func() { - err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + It("adds a vat fold event", func() { + err = repository.Create(headerID, []interface{}{test_data.VatFoldModel}) Expect(err).NotTo(HaveOccurred()) var dbVatFold vat_fold.VatFoldModel @@ -70,7 +72,7 @@ var _ = Describe("Vat.fold repository", func() { }) It("marks header as checked for logs", func() { - err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + err = repository.Create(headerID, []interface{}{test_data.VatFoldModel}) Expect(err).NotTo(HaveOccurred()) var headerChecked bool @@ -83,7 +85,7 @@ var _ = Describe("Vat.fold repository", func() { _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID) Expect(err).NotTo(HaveOccurred()) - err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + err = repository.Create(headerID, []interface{}{test_data.VatFoldModel}) Expect(err).NotTo(HaveOccurred()) var headerChecked bool @@ -92,18 +94,28 @@ var _ = Describe("Vat.fold repository", func() { Expect(headerChecked).To(BeTrue()) }) - It("does not duplicate vat events", func() { - err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + It("does not duplicate vat fold events", func() { + err = repository.Create(headerID, []interface{}{test_data.VatFoldModel}) Expect(err).NotTo(HaveOccurred()) - err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + err = repository.Create(headerID, []interface{}{test_data.VatFoldModel}) 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 = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + It("does not mark header checked if not all models persisted", func() { + err = repository.Create(headerID, []interface{}{test_data.VatFoldModel, test_data.VatFoldModel}) + + Expect(err).To(HaveOccurred()) + var headerChecked bool + err = db.Get(&headerChecked, `SELECT vat_fold_checked FROM public.checked_headers WHERE header_id = $1`, headerID) + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(sql.ErrNoRows)) + }) + + It("removes vat fold if corresponding header is deleted", func() { + err = repository.Create(headerID, []interface{}{test_data.VatFoldModel}) Expect(err).NotTo(HaveOccurred()) _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) @@ -114,6 +126,13 @@ var _ = Describe("Vat.fold repository", func() { Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(sql.ErrNoRows)) }) + + It("returns an error if model is of wrong type", func() { + err = repository.Create(headerID, []interface{}{test_data.WrongModel{}}) + + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("model of type")) + }) }) Describe("MarkHeaderChecked", func() { @@ -139,7 +158,7 @@ var _ = Describe("Vat.fold repository", func() { }) It("updates an existing checked header", func() { - _, err := repository.DB.Exec(`INSERT INTO checked_headers (header_id) VALUES($1)`, headerID) + _, err := db.Exec(`INSERT INTO checked_headers (header_id) VALUES($1)`, headerID) Expect(err).NotTo(HaveOccurred()) var checkedHeaderResult CheckedHeaderResult @@ -163,7 +182,7 @@ var _ = Describe("Vat.fold repository", func() { ) BeforeEach(func() { - startingBlock = GinkgoRandomSeed() + startingBlock = rand.Int63() vatFoldBlock = startingBlock + 1 endingBlock = startingBlock + 2 @@ -211,7 +230,8 @@ var _ = Describe("Vat.fold repository", func() { Expect(err).NotTo(HaveOccurred()) } - repositoryTwo := vat_fold.NewVatFoldRepository(dbTwo) + repositoryTwo := vat_fold.VatFoldRepository{} + repositoryTwo.SetDB(dbTwo) err := repository.MarkHeaderChecked(headerIDs[0]) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/transformers/vat_fold/transformer.go b/pkg/transformers/vat_fold/transformer.go deleted file mode 100644 index 7f0cd298..00000000 --- a/pkg/transformers/vat_fold/transformer.go +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2018 Vulcanize -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package vat_fold - -import ( - "log" - - "github.com/ethereum/go-ethereum/common" - - "github.com/vulcanize/vulcanizedb/pkg/core" - "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" - "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" -) - -type VatFoldTransformerInitializer struct { - Config shared.TransformerConfig -} - -func (initializer VatFoldTransformerInitializer) NewVatFoldTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { - converter := VatFoldConverter{} - fetcher := shared.NewFetcher(blockChain) - repository := NewVatFoldRepository(db) - return VatFoldTransformer{ - Config: initializer.Config, - Converter: converter, - Fetcher: fetcher, - Repository: repository, - } -} - -type VatFoldTransformer struct { - Config shared.TransformerConfig - Converter Converter - Fetcher shared.LogFetcher - Repository Repository -} - -func (transformer VatFoldTransformer) Execute() error { - missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber) - if err != nil { - return err - } - log.Printf("Fetching vat fold event logs for %d headers \n", len(missingHeaders)) - for _, header := range missingHeaders { - topics := [][]common.Hash{{common.HexToHash(shared.VatFoldSignature)}} - matchingLogs, err := transformer.Fetcher.FetchLogs(VatFoldConfig.ContractAddresses, topics, header.BlockNumber) - if err != nil { - return err - } - if len(matchingLogs) < 1 { - err = transformer.Repository.MarkHeaderChecked(header.Id) - if err != nil { - return err - } - continue - } - models, err := transformer.Converter.ToModels(matchingLogs) - if err != nil { - return err - } - err = transformer.Repository.Create(header.Id, models) - if err != nil { - return err - } - } - return nil -} diff --git a/pkg/transformers/vat_fold/transformer_test.go b/pkg/transformers/vat_fold/transformer_test.go index 8d5c74a1..e72e404f 100644 --- a/pkg/transformers/vat_fold/transformer_test.go +++ b/pkg/transformers/vat_fold/transformer_test.go @@ -15,6 +15,8 @@ package vat_fold_test import ( + "math/rand" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" . "github.com/onsi/ginkgo" @@ -22,77 +24,53 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/fakes" + "github.com/vulcanize/vulcanizedb/pkg/transformers/factories" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" - vat_fold_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/vat_fold" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_fold" ) -type setupOptions struct { - setMissingHeadersError bool - setFetcherError bool - setConverterError bool - setCreateError bool - fetchedLogs []types.Log - missingHeaders []core.Header -} - -func setup(options setupOptions) ( - vat_fold.VatFoldTransformer, - *mocks.MockLogFetcher, - *vat_fold_mocks.MockVatFoldConverter, - *vat_fold_mocks.MockVatFoldRepository, -) { - fetcher := &mocks.MockLogFetcher{} - if options.setFetcherError { - fetcher.SetFetcherError(fakes.FakeError) - } - if len(options.fetchedLogs) > 0 { - fetcher.SetFetchedLogs(options.fetchedLogs) - } - - converter := &vat_fold_mocks.MockVatFoldConverter{} - if options.setConverterError { - converter.SetConverterError(fakes.FakeError) - } - - repository := &vat_fold_mocks.MockVatFoldRepository{} - if options.setMissingHeadersError { - repository.SetMissingHeadersErr(fakes.FakeError) - } - if options.setCreateError { - repository.SetCreateError(fakes.FakeError) - } - if len(options.missingHeaders) > 0 { - repository.SetMissingHeaders(options.missingHeaders) - } - - transformer := vat_fold.VatFoldTransformer{ - Config: vat_fold.VatFoldConfig, - Fetcher: fetcher, - Converter: converter, - Repository: repository, - } - - return transformer, fetcher, converter, repository -} - var _ = Describe("Vat fold transformer", func() { - It("gets missing headers for block numbers specified in config", func() { - transformer, _, _, repository := setup(setupOptions{}) + var ( + config = vat_fold.VatFoldConfig + fetcher mocks.MockLogFetcher + converter mocks.MockConverter + repository mocks.MockRepository + transformer shared.Transformer + headerOne core.Header + headerTwo core.Header + ) + BeforeEach(func() { + fetcher = mocks.MockLogFetcher{} + converter = mocks.MockConverter{} + repository = mocks.MockRepository{} + headerOne = core.Header{Id: rand.Int63(), BlockNumber: rand.Int63()} + headerTwo = core.Header{Id: rand.Int63(), BlockNumber: rand.Int63()} + transformer = factories.Transformer{ + Config: config, + Converter: &converter, + Fetcher: &fetcher, + Repository: &repository, + }.NewTransformer(nil, nil) + }) + + It("sets the blockchain and database", func() { + Expect(fetcher.SetBcCalled).To(BeTrue()) + Expect(repository.SetDbCalled).To(BeTrue()) + }) + + It("gets missing headers for block numbers specified in config", func() { err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(repository.PassedStartingBlockNumber).To(Equal(vat_fold.VatFoldConfig.StartingBlockNumber)) - Expect(repository.PassedEndingBlockNumber).To(Equal(vat_fold.VatFoldConfig.EndingBlockNumber)) + Expect(repository.PassedStartingBlockNumber).To(Equal(config.StartingBlockNumber)) + Expect(repository.PassedEndingBlockNumber).To(Equal(config.EndingBlockNumber)) }) It("returns error if repository returns error for missing headers", func() { - transformer, _, _, _ := setup(setupOptions{ - setMissingHeadersError: true, - }) + repository.SetMissingHeadersError(fakes.FakeError) err := transformer.Execute() @@ -101,23 +79,19 @@ var _ = Describe("Vat fold transformer", func() { }) It("fetches logs for missing headers", func() { - transformer, fetcher, _, _ := setup(setupOptions{ - missingHeaders: []core.Header{{BlockNumber: 1}, {BlockNumber: 2}}, - }) + repository.SetMissingHeaders([]core.Header{headerOne, headerTwo}) err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2})) - Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{vat_fold.VatFoldConfig.ContractAddresses, vat_fold.VatFoldConfig.ContractAddresses})) + Expect(fetcher.FetchedBlocks).To(Equal([]int64{headerOne.BlockNumber, headerTwo.BlockNumber})) + Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{config.ContractAddresses, config.ContractAddresses})) Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.VatFoldSignature)}})) }) It("returns error if fetcher returns error", func() { - transformer, _, _, _ := setup(setupOptions{ - setFetcherError: true, - missingHeaders: []core.Header{{BlockNumber: 1}}, - }) + repository.SetMissingHeaders([]core.Header{headerOne}) + fetcher.SetFetcherError(fakes.FakeError) err := transformer.Execute() @@ -126,10 +100,8 @@ var _ = Describe("Vat fold transformer", func() { }) It("converts matching logs", func() { - transformer, _, converter, _ := setup(setupOptions{ - fetchedLogs: []types.Log{test_data.EthVatFoldLog}, - missingHeaders: []core.Header{{BlockNumber: 1}}, - }) + repository.SetMissingHeaders([]core.Header{headerOne}) + fetcher.SetFetchedLogs([]types.Log{test_data.EthVatFoldLog}) err := transformer.Execute() @@ -138,11 +110,9 @@ var _ = Describe("Vat fold transformer", func() { }) It("returns error if converter returns error", func() { - transformer, _, _, _ := setup(setupOptions{ - setConverterError: true, - fetchedLogs: []types.Log{test_data.EthVatFoldLog}, - missingHeaders: []core.Header{{BlockNumber: 1}}, - }) + repository.SetMissingHeaders([]core.Header{headerOne}) + fetcher.SetFetchedLogs([]types.Log{test_data.EthVatFoldLog}) + converter.SetConverterError(fakes.FakeError) err := transformer.Execute() @@ -151,25 +121,21 @@ var _ = Describe("Vat fold transformer", func() { }) It("persists vat fold model", func() { - fakeHeader := core.Header{BlockNumber: 1, Id: 2} - transformer, _, _, repository := setup(setupOptions{ - fetchedLogs: []types.Log{test_data.EthVatFoldLog}, - missingHeaders: []core.Header{fakeHeader}, - }) + repository.SetMissingHeaders([]core.Header{headerOne}) + fetcher.SetFetchedLogs([]types.Log{test_data.EthVatFoldLog}) + converter.SetReturnModels([]interface{}{test_data.VatFoldModel}) err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id)) - Expect(repository.PassedModels).To(Equal([]vat_fold.VatFoldModel{test_data.VatFoldModel})) + Expect(repository.PassedHeaderID).To(Equal(headerOne.Id)) + Expect(repository.PassedModels).To(Equal([]interface{}{test_data.VatFoldModel})) }) It("returns error if repository returns error for create", func() { - transformer, _, _, _ := setup(setupOptions{ - fetchedLogs: []types.Log{test_data.EthVatFoldLog}, - missingHeaders: []core.Header{{BlockNumber: 1, Id: 2}}, - setCreateError: true, - }) + repository.SetMissingHeaders([]core.Header{headerOne}) + fetcher.SetFetchedLogs([]types.Log{test_data.EthVatFoldLog}) + repository.SetCreateError(fakes.FakeError) err := transformer.Execute() @@ -178,34 +144,27 @@ var _ = Describe("Vat fold transformer", func() { }) It("marks the header as checked when there are no logs", func() { - header := core.Header{Id: GinkgoRandomSeed()} - transformer, _, _, repository := setup(setupOptions{ - missingHeaders: []core.Header{header}, - }) + repository.SetMissingHeaders([]core.Header{headerOne}) err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(repository.MarkHeaderCheckedPassedHeaderID).To(Equal(header.Id)) + repository.AssertMarkHeaderCheckedCalledWith(headerOne.Id) }) It("doesn't call MarkHeaderChecked when there are logs", func() { - transformer, _, _, repository := setup(setupOptions{ - missingHeaders: []core.Header{{Id: GinkgoRandomSeed()}}, - fetchedLogs: []types.Log{test_data.EthVatFoldLog}, - }) + repository.SetMissingHeaders([]core.Header{headerOne}) + fetcher.SetFetchedLogs([]types.Log{test_data.EthVatFoldLog}) err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(repository.MarkHeaderCheckedPassedHeaderID).To(Equal(int64(0))) + Expect(repository.MarkHeaderCheckedPassedHeaderIDs).To(BeEmpty()) }) It("returns an error if MarkHeaderChecked fails", func() { - transformer, _, _, _ := setup(setupOptions{ - missingHeaders: []core.Header{{Id: GinkgoRandomSeed()}}, - setMissingHeadersError: true, - }) + repository.SetMissingHeaders([]core.Header{headerOne}) + repository.SetMarkHeaderCheckedError(fakes.FakeError) err := transformer.Execute()