Use transformer factory for Vat fold

This commit is contained in:
Rob Mulholand 2018-10-25 10:55:03 -05:00
parent 0e7f316568
commit e37447ae9a
12 changed files with 144 additions and 342 deletions

View File

@ -19,6 +19,8 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "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/pkg/transformers/vat_fold"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
@ -41,8 +43,13 @@ var _ = Describe("VatFold Transformer", func() {
err = persistHeader(db, blockNumber) err = persistHeader(db, blockNumber)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
initializer := vat_fold.VatFoldTransformerInitializer{Config: config} initializer := factories.Transformer{
transformer := initializer.NewVatFoldTransformer(db, blockchain) Config: config,
Fetcher: &shared.Fetcher{},
Converter: &vat_fold.VatFoldConverter{},
Repository: &vat_fold.VatFoldRepository{},
}
transformer := initializer.NewTransformer(db, blockchain)
err = transformer.Execute() err = transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())

View File

@ -10,7 +10,7 @@ import (
type MockRepository struct { type MockRepository struct {
createError error createError error
markHeaderCheckedError error markHeaderCheckedError error
markHeaderCheckedPassedHeaderIDs []int64 MarkHeaderCheckedPassedHeaderIDs []int64
missingHeaders []core.Header missingHeaders []core.Header
missingHeadersError error missingHeadersError error
PassedStartingBlockNumber int64 PassedStartingBlockNumber int64
@ -27,7 +27,7 @@ func (repository *MockRepository) Create(headerID int64, models []interface{}) e
} }
func (repository *MockRepository) MarkHeaderChecked(headerID int64) error { func (repository *MockRepository) MarkHeaderChecked(headerID int64) error {
repository.markHeaderCheckedPassedHeaderIDs = append(repository.markHeaderCheckedPassedHeaderIDs, headerID) repository.MarkHeaderCheckedPassedHeaderIDs = append(repository.MarkHeaderCheckedPassedHeaderIDs, headerID)
return repository.markHeaderCheckedError return repository.markHeaderCheckedError
} }
@ -50,7 +50,7 @@ func (repository *MockRepository) SetMissingHeaders(headers []core.Header) {
} }
func (repository *MockRepository) AssertMarkHeaderCheckedCalledWith(i int64) { func (repository *MockRepository) AssertMarkHeaderCheckedCalledWith(i int64) {
Expect(repository.markHeaderCheckedPassedHeaderIDs).To(ContainElement(i)) Expect(repository.MarkHeaderCheckedPassedHeaderIDs).To(ContainElement(i))
} }
func (repository *MockRepository) SetMarkHeaderCheckedError(e error) { func (repository *MockRepository) SetMarkHeaderCheckedError(e error) {

View File

@ -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
}

View File

@ -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))
}

View File

@ -125,7 +125,12 @@ var (
}.NewTransformer }.NewTransformer
VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer
VatHealTransformerInitializer = vat_heal.VatHealTransformerInitializer{Config: vat_heal.VatHealConfig}.NewVatHealTransformer 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{ VatMoveTransformerInitializer = factories.Transformer{
Config: vat_move.VatMoveConfig, Config: vat_move.VatMoveConfig,
Converter: &vat_move.VatMoveConverter{}, Converter: &vat_move.VatMoveConverter{},

View File

@ -18,10 +18,11 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
) )
var VatFoldConfig = shared.TransformerConfig{ var VatFoldConfig = shared.SingleTransformerConfig{
TransformerName: shared.VatFoldLabel,
ContractAddresses: []string{shared.VatContractAddress}, ContractAddresses: []string{shared.VatContractAddress},
ContractAbi: shared.VatABI, ContractAbi: shared.VatABI,
Topics: []string{shared.VatFoldSignature}, Topic: shared.VatFoldSignature,
StartingBlockNumber: 0, StartingBlockNumber: 0,
EndingBlockNumber: 10000000, EndingBlockNumber: 10000000,
} }

View File

@ -23,14 +23,10 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
) )
type Converter interface {
ToModels(ethLogs []types.Log) ([]VatFoldModel, error)
}
type VatFoldConverter struct{} type VatFoldConverter struct{}
func (VatFoldConverter) ToModels(ethLogs []types.Log) ([]VatFoldModel, error) { func (VatFoldConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
var models []VatFoldModel var models []interface{}
for _, ethLog := range ethLogs { for _, ethLog := range ethLogs {
err := verifyLog(ethLog) err := verifyLog(ethLog)
if err != nil { if err != nil {

View File

@ -39,6 +39,6 @@ var _ = Describe("Vat fold converter", func() {
model, err := converter.ToModels([]types.Log{test_data.EthVatFoldLog}) model, err := converter.ToModels([]types.Log{test_data.EthVatFoldLog})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(model).To(Equal([]vat_fold.VatFoldModel{test_data.VatFoldModel})) Expect(model).To(Equal([]interface{}{test_data.VatFoldModel}))
}) })
}) })

View File

@ -15,59 +15,55 @@
package vat_fold package vat_fold
import ( import (
"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"
) )
type Repository interface {
Create(headerID int64, models []VatFoldModel) error
MarkHeaderChecked(headerID int64) error
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
}
type VatFoldRepository struct { type VatFoldRepository struct {
DB *postgres.DB db *postgres.DB
} }
func NewVatFoldRepository(db *postgres.DB) VatFoldRepository { func (repository VatFoldRepository) Create(headerID int64, models []interface{}) error {
return VatFoldRepository{ tx, err := repository.db.Begin()
DB: db,
}
}
func (repository VatFoldRepository) Create(headerID int64, models []VatFoldModel) error {
tx, err := repository.DB.Begin()
if err != nil { if err != nil {
return err return err
} }
for _, model := range models { 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( _, err = tx.Exec(
`INSERT into maker.vat_fold (header_id, ilk, urn, rate, log_idx, tx_idx, raw_log) `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)`, 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 { if err != nil {
tx.Rollback() tx.Rollback()
return err 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) VALUES($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
UPDATE SET vat_fold_checked = $2`, UPDATE SET vat_fold_checked = $2`,
headerID, true, headerID, true,
) )
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
}
} }
return tx.Commit() return tx.Commit()
} }
func (repository VatFoldRepository) MarkHeaderChecked(headerID int64) error { 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) `INSERT INTO public.checked_headers (header_id, vat_fold_checked)
VALUES ($1, $2) VALUES ($1, $2)
ON CONFLICT (header_id) DO 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) { func (repository VatFoldRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header var result []core.Header
err := repository.DB.Select( err := repository.db.Select(
&result, &result,
`SELECT headers.id, headers.block_number FROM headers `SELECT headers.id, headers.block_number FROM headers
LEFT JOIN checked_headers on headers.id = header_id 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`, AND headers.eth_node_fingerprint = $3`,
startingBlockNumber, startingBlockNumber,
endingBlockNumber, endingBlockNumber,
repository.DB.Node.ID, repository.db.Node.ID,
) )
return result, err return result, err
} }
func (repository *VatFoldRepository) SetDB(db *postgres.DB) {
repository.db = db
}

View File

@ -16,6 +16,7 @@ package vat_fold_test
import ( import (
"database/sql" "database/sql"
"math/rand"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@ -41,7 +42,8 @@ var _ = Describe("Vat.fold repository", func() {
node := test_config.NewTestNode() node := test_config.NewTestNode()
db = test_config.NewTestDB(node) db = test_config.NewTestDB(node)
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
repository = vat_fold.NewVatFoldRepository(db) repository = vat_fold.VatFoldRepository{}
repository.SetDB(db)
headerRepository = repositories.NewHeaderRepository(db) headerRepository = repositories.NewHeaderRepository(db)
}) })
@ -53,8 +55,8 @@ var _ = Describe("Vat.fold repository", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
}) })
It("adds a vat event", func() { It("adds a vat fold event", func() {
err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) err = repository.Create(headerID, []interface{}{test_data.VatFoldModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbVatFold vat_fold.VatFoldModel var dbVatFold vat_fold.VatFoldModel
@ -70,7 +72,7 @@ var _ = Describe("Vat.fold repository", func() {
}) })
It("marks header as checked for logs", 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()) Expect(err).NotTo(HaveOccurred())
var headerChecked bool 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) _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
Expect(err).NotTo(HaveOccurred()) 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()) Expect(err).NotTo(HaveOccurred())
var headerChecked bool var headerChecked bool
@ -92,18 +94,28 @@ var _ = Describe("Vat.fold repository", func() {
Expect(headerChecked).To(BeTrue()) Expect(headerChecked).To(BeTrue())
}) })
It("does not duplicate vat events", func() { It("does not duplicate vat fold events", func() {
err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) err = repository.Create(headerID, []interface{}{test_data.VatFoldModel})
Expect(err).NotTo(HaveOccurred()) 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).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
}) })
It("removes vat if corresponding header is deleted", func() { It("does not mark header checked if not all models persisted", func() {
err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) 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()) Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) _, 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(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows)) 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() { Describe("MarkHeaderChecked", func() {
@ -139,7 +158,7 @@ var _ = Describe("Vat.fold repository", func() {
}) })
It("updates an existing checked header", 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()) Expect(err).NotTo(HaveOccurred())
var checkedHeaderResult CheckedHeaderResult var checkedHeaderResult CheckedHeaderResult
@ -163,7 +182,7 @@ var _ = Describe("Vat.fold repository", func() {
) )
BeforeEach(func() { BeforeEach(func() {
startingBlock = GinkgoRandomSeed() startingBlock = rand.Int63()
vatFoldBlock = startingBlock + 1 vatFoldBlock = startingBlock + 1
endingBlock = startingBlock + 2 endingBlock = startingBlock + 2
@ -211,7 +230,8 @@ var _ = Describe("Vat.fold repository", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
} }
repositoryTwo := vat_fold.NewVatFoldRepository(dbTwo) repositoryTwo := vat_fold.VatFoldRepository{}
repositoryTwo.SetDB(dbTwo)
err := repository.MarkHeaderChecked(headerIDs[0]) err := repository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())

View File

@ -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
}

View File

@ -15,6 +15,8 @@
package vat_fold_test package vat_fold_test
import ( import (
"math/rand"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
@ -22,77 +24,53 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "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/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" "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" "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() { var _ = Describe("Vat fold transformer", func() {
It("gets missing headers for block numbers specified in config", func() { var (
transformer, _, _, repository := setup(setupOptions{}) 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() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(repository.PassedStartingBlockNumber).To(Equal(vat_fold.VatFoldConfig.StartingBlockNumber)) Expect(repository.PassedStartingBlockNumber).To(Equal(config.StartingBlockNumber))
Expect(repository.PassedEndingBlockNumber).To(Equal(vat_fold.VatFoldConfig.EndingBlockNumber)) Expect(repository.PassedEndingBlockNumber).To(Equal(config.EndingBlockNumber))
}) })
It("returns error if repository returns error for missing headers", func() { It("returns error if repository returns error for missing headers", func() {
transformer, _, _, _ := setup(setupOptions{ repository.SetMissingHeadersError(fakes.FakeError)
setMissingHeadersError: true,
})
err := transformer.Execute() err := transformer.Execute()
@ -101,23 +79,19 @@ var _ = Describe("Vat fold transformer", func() {
}) })
It("fetches logs for missing headers", func() { It("fetches logs for missing headers", func() {
transformer, fetcher, _, _ := setup(setupOptions{ repository.SetMissingHeaders([]core.Header{headerOne, headerTwo})
missingHeaders: []core.Header{{BlockNumber: 1}, {BlockNumber: 2}},
})
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2})) Expect(fetcher.FetchedBlocks).To(Equal([]int64{headerOne.BlockNumber, headerTwo.BlockNumber}))
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{vat_fold.VatFoldConfig.ContractAddresses, vat_fold.VatFoldConfig.ContractAddresses})) Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{config.ContractAddresses, config.ContractAddresses}))
Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.VatFoldSignature)}})) Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.VatFoldSignature)}}))
}) })
It("returns error if fetcher returns error", func() { It("returns error if fetcher returns error", func() {
transformer, _, _, _ := setup(setupOptions{ repository.SetMissingHeaders([]core.Header{headerOne})
setFetcherError: true, fetcher.SetFetcherError(fakes.FakeError)
missingHeaders: []core.Header{{BlockNumber: 1}},
})
err := transformer.Execute() err := transformer.Execute()
@ -126,10 +100,8 @@ var _ = Describe("Vat fold transformer", func() {
}) })
It("converts matching logs", func() { It("converts matching logs", func() {
transformer, _, converter, _ := setup(setupOptions{ repository.SetMissingHeaders([]core.Header{headerOne})
fetchedLogs: []types.Log{test_data.EthVatFoldLog}, fetcher.SetFetchedLogs([]types.Log{test_data.EthVatFoldLog})
missingHeaders: []core.Header{{BlockNumber: 1}},
})
err := transformer.Execute() err := transformer.Execute()
@ -138,11 +110,9 @@ var _ = Describe("Vat fold transformer", func() {
}) })
It("returns error if converter returns error", func() { It("returns error if converter returns error", func() {
transformer, _, _, _ := setup(setupOptions{ repository.SetMissingHeaders([]core.Header{headerOne})
setConverterError: true, fetcher.SetFetchedLogs([]types.Log{test_data.EthVatFoldLog})
fetchedLogs: []types.Log{test_data.EthVatFoldLog}, converter.SetConverterError(fakes.FakeError)
missingHeaders: []core.Header{{BlockNumber: 1}},
})
err := transformer.Execute() err := transformer.Execute()
@ -151,25 +121,21 @@ var _ = Describe("Vat fold transformer", func() {
}) })
It("persists vat fold model", func() { It("persists vat fold model", func() {
fakeHeader := core.Header{BlockNumber: 1, Id: 2} repository.SetMissingHeaders([]core.Header{headerOne})
transformer, _, _, repository := setup(setupOptions{ fetcher.SetFetchedLogs([]types.Log{test_data.EthVatFoldLog})
fetchedLogs: []types.Log{test_data.EthVatFoldLog}, converter.SetReturnModels([]interface{}{test_data.VatFoldModel})
missingHeaders: []core.Header{fakeHeader},
})
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id)) Expect(repository.PassedHeaderID).To(Equal(headerOne.Id))
Expect(repository.PassedModels).To(Equal([]vat_fold.VatFoldModel{test_data.VatFoldModel})) Expect(repository.PassedModels).To(Equal([]interface{}{test_data.VatFoldModel}))
}) })
It("returns error if repository returns error for create", func() { It("returns error if repository returns error for create", func() {
transformer, _, _, _ := setup(setupOptions{ repository.SetMissingHeaders([]core.Header{headerOne})
fetchedLogs: []types.Log{test_data.EthVatFoldLog}, fetcher.SetFetchedLogs([]types.Log{test_data.EthVatFoldLog})
missingHeaders: []core.Header{{BlockNumber: 1, Id: 2}}, repository.SetCreateError(fakes.FakeError)
setCreateError: true,
})
err := transformer.Execute() 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() { It("marks the header as checked when there are no logs", func() {
header := core.Header{Id: GinkgoRandomSeed()} repository.SetMissingHeaders([]core.Header{headerOne})
transformer, _, _, repository := setup(setupOptions{
missingHeaders: []core.Header{header},
})
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) 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() { It("doesn't call MarkHeaderChecked when there are logs", func() {
transformer, _, _, repository := setup(setupOptions{ repository.SetMissingHeaders([]core.Header{headerOne})
missingHeaders: []core.Header{{Id: GinkgoRandomSeed()}}, fetcher.SetFetchedLogs([]types.Log{test_data.EthVatFoldLog})
fetchedLogs: []types.Log{test_data.EthVatFoldLog},
})
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(repository.MarkHeaderCheckedPassedHeaderID).To(Equal(int64(0))) Expect(repository.MarkHeaderCheckedPassedHeaderIDs).To(BeEmpty())
}) })
It("returns an error if MarkHeaderChecked fails", func() { It("returns an error if MarkHeaderChecked fails", func() {
transformer, _, _, _ := setup(setupOptions{ repository.SetMissingHeaders([]core.Header{headerOne})
missingHeaders: []core.Header{{Id: GinkgoRandomSeed()}}, repository.SetMarkHeaderCheckedError(fakes.FakeError)
setMissingHeadersError: true,
})
err := transformer.Execute() err := transformer.Execute()