Update vat heal to use transformer factory (#88)

* Update vat heal to use transformer factory
This commit is contained in:
Elizabeth 2018-10-26 11:40:24 -05:00 committed by GitHub
parent 5de949f4c5
commit d522d33531
11 changed files with 124 additions and 266 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_heal" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
@ -41,8 +43,13 @@ var _ = Describe("VatHeal Transformer", func() {
err = persistHeader(db, blockNumber) err = persistHeader(db, blockNumber)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
initializer := vat_heal.VatHealTransformerInitializer{Config: config} transformer := factories.Transformer{
transformer := initializer.NewVatHealTransformer(db, blockchain) Config: vat_heal.VatHealConfig,
Converter: &vat_heal.VatHealConverter{},
Repository: &vat_heal.VatHealRepository{},
Fetcher: &shared.Fetcher{},
}.NewTransformer(db, blockchain)
err = transformer.Execute() err = transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())

View File

@ -49,10 +49,6 @@ func (repository *MockRepository) SetMissingHeaders(headers []core.Header) {
repository.missingHeaders = headers repository.missingHeaders = headers
} }
func (repository *MockRepository) AssertMarkHeaderCheckedCalledWith(i int64) {
Expect(repository.MarkHeaderCheckedPassedHeaderIDs).To(ContainElement(i))
}
func (repository *MockRepository) SetMarkHeaderCheckedError(e error) { func (repository *MockRepository) SetMarkHeaderCheckedError(e error) {
repository.markHeaderCheckedError = e repository.markHeaderCheckedError = e
} }
@ -60,3 +56,11 @@ func (repository *MockRepository) SetMarkHeaderCheckedError(e error) {
func (repository *MockRepository) SetCreateError(e error) { func (repository *MockRepository) SetCreateError(e error) {
repository.createError = e repository.createError = e
} }
func (repository *MockRepository) AssertMarkHeaderCheckedCalledWith(i int64) {
Expect(repository.MarkHeaderCheckedPassedHeaderIDs).To(ContainElement(i))
}
func (repository *MockRepository) AssertMarkHeaderCheckedNotCalled() {
Expect(len(repository.MarkHeaderCheckedPassedHeaderIDs)).To(Equal(0))
}

View File

@ -1,35 +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_heal
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal"
)
type MockVatHealConverter struct {
converterErr error
PassedLogs []types.Log
}
func (converter *MockVatHealConverter) ToModels(ethLogs []types.Log) ([]vat_heal.VatHealModel, error) {
converter.PassedLogs = ethLogs
return []vat_heal.VatHealModel{test_data.VatHealModel}, converter.converterErr
}
func (converter *MockVatHealConverter) SetConverterError(e error) {
converter.converterErr = e
}

View File

@ -1,65 +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_heal
import (
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal"
)
type MockVatHealRepository struct {
createErr error
markHeaderCheckedErr error
MarkHeaderCheckedPassedHeaderID int64
missingHeaders []core.Header
missingHeadersErr error
PassedStartingBlockNumber int64
PassedEndingBlockNumber int64
PassedHeaderID int64
PassedModels []vat_heal.VatHealModel
}
func (repository *MockVatHealRepository) MarkCheckedHeader(headerId int64) error {
repository.MarkHeaderCheckedPassedHeaderID = headerId
return repository.markHeaderCheckedErr
}
func (repository *MockVatHealRepository) Create(headerID int64, models []vat_heal.VatHealModel) error {
repository.PassedHeaderID = headerID
repository.PassedModels = models
return repository.createErr
}
func (repository *MockVatHealRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
repository.PassedStartingBlockNumber = startingBlockNumber
repository.PassedEndingBlockNumber = endingBlockNumber
return repository.missingHeaders, repository.missingHeadersErr
}
func (repository *MockVatHealRepository) SetMarkHeaderCheckedErr(e error) {
repository.markHeaderCheckedErr = e
}
func (repository *MockVatHealRepository) SetMissingHeadersErr(e error) {
repository.missingHeadersErr = e
}
func (repository *MockVatHealRepository) SetMissingHeaders(headers []core.Header) {
repository.missingHeaders = headers
}
func (repository *MockVatHealRepository) SetCreateError(e error) {
repository.createErr = e
}

View File

@ -151,13 +151,18 @@ var (
Repository: &vat_grab.VatGrabRepository{}, Repository: &vat_grab.VatGrabRepository{},
Fetcher: &shared.Fetcher{}, Fetcher: &shared.Fetcher{},
}.NewTransformer }.NewTransformer
VatHealTransformerInitializer = vat_heal.VatHealTransformerInitializer{Config: vat_heal.VatHealConfig}.NewVatHealTransformer
VatFoldTransformerInitializer = factories.Transformer{ VatFoldTransformerInitializer = factories.Transformer{
Config: vat_fold.VatFoldConfig, Config: vat_fold.VatFoldConfig,
Converter: &vat_fold.VatFoldConverter{}, Converter: &vat_fold.VatFoldConverter{},
Repository: &vat_fold.VatFoldRepository{}, Repository: &vat_fold.VatFoldRepository{},
Fetcher: &shared.Fetcher{}, Fetcher: &shared.Fetcher{},
}.NewTransformer }.NewTransformer
VatHealTransformerInitializer = factories.Transformer{
Config: vat_heal.VatHealConfig,
Converter: &vat_heal.VatHealConverter{},
Repository: &vat_heal.VatHealRepository{},
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

@ -16,10 +16,11 @@ package vat_heal
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
var VatHealConfig = shared.TransformerConfig{ var VatHealConfig = shared.SingleTransformerConfig{
TransformerName: shared.VatHealLabel,
ContractAddresses: []string{shared.VatContractAddress}, ContractAddresses: []string{shared.VatContractAddress},
ContractAbi: shared.VatABI, ContractAbi: shared.VatABI,
Topics: []string{shared.VatHealSignature}, Topic: shared.VatHealSignature,
StartingBlockNumber: 0, StartingBlockNumber: 0,
EndingBlockNumber: 10000000, EndingBlockNumber: 10000000,
} }

View File

@ -22,14 +22,10 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
) )
type Converter interface {
ToModels(ethLogs []types.Log) ([]VatHealModel, error)
}
type VatHealConverter struct{} type VatHealConverter struct{}
func (VatHealConverter) ToModels(ethLogs []types.Log) ([]VatHealModel, error) { func (VatHealConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
var models []VatHealModel 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

@ -15,34 +15,35 @@
package vat_heal package vat_heal
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 []VatHealModel) error
MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error)
MarkCheckedHeader(headerId int64) error
}
type VatHealRepository struct { type VatHealRepository struct {
DB *postgres.DB db *postgres.DB
} }
func NewVatHealRepository(db *postgres.DB) VatHealRepository { func (repository *VatHealRepository) SetDB(db *postgres.DB) {
return VatHealRepository{DB: db} repository.db = db
} }
func (repository VatHealRepository) Create(headerId int64, models []VatHealModel) error { func (repository VatHealRepository) Create(headerId int64, models []interface{}) error {
tx, err := repository.DB.Begin() tx, err := repository.db.Begin()
if err != nil { if err != nil {
return err return err
} }
for _, model := range models { for _, model := range models {
vatHeal, ok := model.(VatHealModel)
if !ok {
tx.Rollback()
return fmt.Errorf("model of type %T, not %T", model, VatHealModel{})
}
_, err := tx.Exec(`INSERT INTO maker.vat_heal (header_id, urn, v, rad, log_idx, tx_idx, raw_log) _, err := tx.Exec(`INSERT INTO maker.vat_heal (header_id, urn, v, rad, 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.Urn, model.V, model.Rad, model.LogIndex, model.TransactionIndex, model.Raw) headerId, vatHeal.Urn, vatHeal.V, vatHeal.Rad, vatHeal.LogIndex, vatHeal.TransactionIndex, vatHeal.Raw)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -62,20 +63,20 @@ func (repository VatHealRepository) Create(headerId int64, models []VatHealModel
func (repository VatHealRepository) MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error) { func (repository VatHealRepository) MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error) {
var headers []core.Header var headers []core.Header
err := repository.DB.Select(&headers, err := repository.db.Select(&headers,
`SELECT headers.id, block_number from headers `SELECT headers.id, block_number from headers
LEFT JOIN checked_headers on headers.id = header_id LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR vat_heal_checked IS FALSE) WHERE (header_id ISNULL OR vat_heal_checked IS FALSE)
AND headers.block_number >= $1 AND headers.block_number >= $1
AND headers.block_number <= $2 AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`, AND headers.eth_node_fingerprint = $3`,
startingBlock, endingBlock, repository.DB.Node.ID) startingBlock, endingBlock, repository.db.Node.ID)
return headers, err return headers, err
} }
func (repository VatHealRepository) MarkCheckedHeader(headerId int64) error { func (repository VatHealRepository) MarkHeaderChecked(headerId int64) error {
_, err := repository.DB.Exec(`INSERT INTO public.checked_headers (header_id, vat_heal_checked) _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_heal_checked)
VALUES($1, $2) VALUES($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
UPDATE SET vat_heal_checked = $2`, headerId, true) UPDATE SET vat_heal_checked = $2`, headerId, true)

View File

@ -15,6 +15,8 @@
package vat_heal_test package vat_heal_test
import ( import (
"math/rand"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@ -38,7 +40,8 @@ var _ = Describe("VatHeal Repository", func() {
BeforeEach(func() { BeforeEach(func() {
db = test_config.NewTestDB(test_config.NewTestNode()) db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
repository = vat_heal.VatHealRepository{DB: db} repository = vat_heal.VatHealRepository{}
repository.SetDB(db)
headerRepository = repositories.NewHeaderRepository(db) headerRepository = repositories.NewHeaderRepository(db)
}) })
@ -63,7 +66,7 @@ var _ = Describe("VatHeal Repository", func() {
It("persists vat heal records", func() { It("persists vat heal records", func() {
anotherVatHeal := test_data.VatHealModel anotherVatHeal := test_data.VatHealModel
anotherVatHeal.LogIndex = test_data.VatHealModel.LogIndex + 1 anotherVatHeal.LogIndex = test_data.VatHealModel.LogIndex + 1
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel, anotherVatHeal}) err = repository.Create(headerId, []interface{}{test_data.VatHealModel, anotherVatHeal})
var dbResult []VatHealDBResult var dbResult []VatHealDBResult
err = db.Select(&dbResult, `SELECT * from maker.vat_heal where header_id = $1`, headerId) err = db.Select(&dbResult, `SELECT * from maker.vat_heal where header_id = $1`, headerId)
@ -80,17 +83,17 @@ var _ = Describe("VatHeal Repository", func() {
}) })
It("returns an error if the insertion fails", func() { It("returns an error if the insertion fails", func() {
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
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("marks the header as checked for vat heal logs", func() { It("marks the header as checked for vat heal logs", func() {
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var headerChecked bool var headerChecked bool
@ -103,7 +106,7 @@ var _ = Describe("VatHeal 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_heal.VatHealModel{test_data.VatHealModel}) err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var headerChecked bool var headerChecked bool
@ -113,7 +116,7 @@ var _ = Describe("VatHeal Repository", func() {
}) })
It("removes vat heal if corresponding header is deleted", func() { It("removes vat heal if corresponding header is deleted", func() {
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
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)
@ -125,11 +128,17 @@ var _ = Describe("VatHeal Repository", func() {
Expect(count).To(Equal(0)) Expect(count).To(Equal(0))
}) })
It("returns an error if the wrong model types are passed in", func() {
err = repository.Create(headerId, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type test_data.WrongModel, not vat_heal.VatHealModel"))
})
It("wraps create in a transaction", func() { It("wraps create in a transaction", func() {
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel, test_data.VatHealModel}) err = repository.Create(headerId, []interface{}{test_data.VatHealModel, test_data.VatHealModel})
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
var count int var count int
err = repository.DB.QueryRowx(`SELECT count(*) FROM maker.vat_heal`).Scan(&count) err = db.QueryRowx(`SELECT count(*) FROM maker.vat_heal`).Scan(&count)
Expect(count).To(Equal(0)) Expect(count).To(Equal(0))
}) })
}) })
@ -141,7 +150,7 @@ var _ = Describe("VatHeal Repository", func() {
) )
BeforeEach(func() { BeforeEach(func() {
startingBlock = GinkgoRandomSeed() startingBlock = rand.Int63()
vatHealBlock = startingBlock + 1 vatHealBlock = startingBlock + 1
endingBlock = startingBlock + 2 endingBlock = startingBlock + 2
outsideRangeBlock = startingBlock + 3 outsideRangeBlock = startingBlock + 3
@ -156,7 +165,7 @@ var _ = Describe("VatHeal Repository", func() {
}) })
It("returns headers that haven't been checked", func() { It("returns headers that haven't been checked", func() {
err = repository.MarkCheckedHeader(headerIds[0]) err = repository.MarkHeaderChecked(headerIds[0])
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
headers, err := repository.MissingHeaders(startingBlock, endingBlock) headers, err := repository.MissingHeaders(startingBlock, endingBlock)
@ -168,8 +177,8 @@ var _ = Describe("VatHeal Repository", func() {
}) })
It("returns header ids when checked_headers.vat_heal is false", func() { It("returns header ids when checked_headers.vat_heal is false", func() {
err = repository.MarkCheckedHeader(headerIds[0]) err = repository.MarkHeaderChecked(headerIds[0])
_, err = repository.DB.Exec(`INSERT INTO checked_headers (header_id) VALUES ($1)`, headerIds[1]) _, err = db.Exec(`INSERT INTO checked_headers (header_id) VALUES ($1)`, headerIds[1])
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
headers, err := repository.MissingHeaders(startingBlock, endingBlock) headers, err := repository.MissingHeaders(startingBlock, endingBlock)
@ -183,14 +192,15 @@ var _ = Describe("VatHeal Repository", func() {
It("only returns header ids for the current node", func() { It("only returns header ids for the current node", func() {
db2 := test_config.NewTestDB(core.Node{ID: "second node"}) db2 := test_config.NewTestDB(core.Node{ID: "second node"})
headerRepository2 := repositories.NewHeaderRepository(db2) headerRepository2 := repositories.NewHeaderRepository(db2)
repository2 := vat_heal.NewVatHealRepository(db2) repository2 := vat_heal.VatHealRepository{}
repository2.SetDB(db2)
for _, n := range blockNumbers { for _, n := range blockNumbers {
_, err = headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) _, err = headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
} }
err = repository.MarkCheckedHeader(headerIds[0]) err = repository.MarkHeaderChecked(headerIds[0])
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := repository.MissingHeaders(startingBlock, endingBlock) nodeOneMissingHeaders, err := repository.MissingHeaders(startingBlock, endingBlock)
@ -212,7 +222,7 @@ var _ = Describe("VatHeal Repository", func() {
}) })
It("creates a new checked_header record", func() { It("creates a new checked_header record", func() {
err := repository.MarkCheckedHeader(headerId) err := repository.MarkHeaderChecked(headerId)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var checkedHeaderResult = CheckedHeaderResult{} var checkedHeaderResult = CheckedHeaderResult{}
@ -222,7 +232,7 @@ var _ = Describe("VatHeal 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
@ -230,7 +240,7 @@ var _ = Describe("VatHeal Repository", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(checkedHeaderResult.VatHealChecked).To(BeFalse()) Expect(checkedHeaderResult.VatHealChecked).To(BeFalse())
err = repository.MarkCheckedHeader(headerId) err = repository.MarkHeaderChecked(headerId)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
err = db.Get(&checkedHeaderResult, `SELECT vat_heal_checked FROM checked_headers WHERE header_id = $1`, headerId) err = db.Get(&checkedHeaderResult, `SELECT vat_heal_checked FROM checked_headers WHERE header_id = $1`, headerId)

View File

@ -1,80 +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_heal
import (
"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 VatHealTransformer struct {
Config shared.TransformerConfig
Converter Converter
Fetcher shared.LogFetcher
Repository Repository
}
type VatHealTransformerInitializer struct {
Config shared.TransformerConfig
}
func (i VatHealTransformerInitializer) NewVatHealTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
fetcher := shared.NewFetcher(blockChain)
repository := NewVatHealRepository(db)
transformer := VatHealTransformer{
Fetcher: fetcher,
Repository: repository,
Converter: VatHealConverter{},
Config: i.Config,
}
return transformer
}
func (transformer VatHealTransformer) Execute() error {
config := transformer.Config
topics := [][]common.Hash{{common.HexToHash(config.Topics[0])}}
headers, err := transformer.Repository.MissingHeaders(config.StartingBlockNumber, config.EndingBlockNumber)
if err != nil {
return err
}
for _, header := range headers {
logs, err := transformer.Fetcher.FetchLogs(config.ContractAddresses, topics, header.BlockNumber)
if err != nil {
return err
}
if len(logs) < 1 {
err = transformer.Repository.MarkCheckedHeader(header.Id)
}
if err != nil {
return err
}
models, err := transformer.Converter.ToModels(logs)
if err != nil {
return err
}
err = transformer.Repository.Create(header.Id, models)
if err != nil {
return err
}
}
return nil
}

View File

@ -15,47 +15,60 @@
package vat_heal_test package vat_heal_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"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"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"
mock_vat_heal "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/vat_heal"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal"
) )
var _ = Describe("VatHeal Transformer", func() { var _ = Describe("VatHeal Transformer", func() {
var mockRepository mock_vat_heal.MockVatHealRepository var (
var transformer vat_heal.VatHealTransformer repository mocks.MockRepository
var fetcher mocks.MockLogFetcher transformer shared.Transformer
var converter mock_vat_heal.MockVatHealConverter fetcher mocks.MockLogFetcher
converter mocks.MockConverter
config = vat_heal.VatHealConfig
)
BeforeEach(func() { BeforeEach(func() {
mockRepository = mock_vat_heal.MockVatHealRepository{} repository = mocks.MockRepository{}
fetcher = mocks.MockLogFetcher{} fetcher = mocks.MockLogFetcher{}
converter = mock_vat_heal.MockVatHealConverter{} converter = mocks.MockConverter{}
transformer = vat_heal.VatHealTransformer{ transformer = factories.Transformer{
Repository: &mockRepository, Repository: &repository,
Config: vat_heal.VatHealConfig, Config: config,
Fetcher: &fetcher, Fetcher: &fetcher,
Converter: &converter, Converter: &converter,
} }.NewTransformer(nil, nil)
})
It("sets the database and blockchain", func() {
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(repository.SetDbCalled).To(BeTrue())
}) })
It("gets all of the missing header ids", func() { It("gets all of the missing header ids", func() {
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(mockRepository.PassedStartingBlockNumber).To(Equal(vat_heal.VatHealConfig.StartingBlockNumber)) Expect(repository.PassedStartingBlockNumber).To(Equal(config.StartingBlockNumber))
Expect(mockRepository.PassedEndingBlockNumber).To(Equal(vat_heal.VatHealConfig.EndingBlockNumber)) Expect(repository.PassedEndingBlockNumber).To(Equal(config.EndingBlockNumber))
}) })
It("returns and error if getting the missing headers fails", func() { It("returns and error if getting the missing headers fails", func() {
mockRepository.SetMissingHeadersErr(fakes.FakeError) repository.SetMissingHeadersError(fakes.FakeError)
err := transformer.Execute() err := transformer.Execute()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
@ -63,8 +76,8 @@ var _ = Describe("VatHeal Transformer", func() {
}) })
It("fetches vat heal logs for the headers", func() { It("fetches vat heal logs for the headers", func() {
header := core.Header{BlockNumber: GinkgoRandomSeed()} header := core.Header{BlockNumber: rand.Int63()}
mockRepository.SetMissingHeaders([]core.Header{header}) repository.SetMissingHeaders([]core.Header{header})
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -74,8 +87,8 @@ var _ = Describe("VatHeal Transformer", func() {
}) })
It("returns and error if fetching the logs fails", func() { It("returns and error if fetching the logs fails", func() {
header := core.Header{BlockNumber: GinkgoRandomSeed()} header := core.Header{BlockNumber: rand.Int63()}
mockRepository.SetMissingHeaders([]core.Header{header}) repository.SetMissingHeaders([]core.Header{header})
fetcher.SetFetcherError(fakes.FakeError) fetcher.SetFetcherError(fakes.FakeError)
err := transformer.Execute() err := transformer.Execute()
@ -84,8 +97,8 @@ var _ = Describe("VatHeal Transformer", func() {
}) })
It("converts the logs to models", func() { It("converts the logs to models", func() {
header := core.Header{BlockNumber: GinkgoRandomSeed()} header := core.Header{BlockNumber: rand.Int63()}
mockRepository.SetMissingHeaders([]core.Header{header}) repository.SetMissingHeaders([]core.Header{header})
fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog}) fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog})
err := transformer.Execute() err := transformer.Execute()
@ -95,8 +108,8 @@ var _ = Describe("VatHeal Transformer", func() {
}) })
It("returns an error if converting fails", func() { It("returns an error if converting fails", func() {
header := core.Header{BlockNumber: GinkgoRandomSeed()} header := core.Header{BlockNumber: rand.Int63()}
mockRepository.SetMissingHeaders([]core.Header{header}) repository.SetMissingHeaders([]core.Header{header})
fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog}) fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog})
converter.SetConverterError(fakes.FakeError) converter.SetConverterError(fakes.FakeError)
@ -107,22 +120,23 @@ var _ = Describe("VatHeal Transformer", func() {
}) })
It("persists the vat heal models", func() { It("persists the vat heal models", func() {
header := core.Header{Id: GinkgoRandomSeed()} header := core.Header{Id: rand.Int63()}
mockRepository.SetMissingHeaders([]core.Header{header}) repository.SetMissingHeaders([]core.Header{header})
fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog}) fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog})
converter.SetReturnModels([]interface{}{test_data.VatHealModel})
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(mockRepository.PassedModels).To(ContainElement(test_data.VatHealModel)) Expect(repository.PassedModels).To(ContainElement(test_data.VatHealModel))
Expect(mockRepository.PassedHeaderID).To(Equal(header.Id)) Expect(repository.PassedHeaderID).To(Equal(header.Id))
}) })
It("returns an error if persisting the vat heal models fails", func() { It("returns an error if persisting the vat heal models fails", func() {
header := core.Header{Id: GinkgoRandomSeed()} header := core.Header{Id: rand.Int63()}
mockRepository.SetMissingHeaders([]core.Header{header}) repository.SetMissingHeaders([]core.Header{header})
fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog}) fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog})
mockRepository.SetCreateError(fakes.FakeError) repository.SetCreateError(fakes.FakeError)
err := transformer.Execute() err := transformer.Execute()
@ -131,29 +145,29 @@ var _ = Describe("VatHeal 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()} header := core.Header{Id: rand.Int63()}
mockRepository.SetMissingHeaders([]core.Header{header}) repository.SetMissingHeaders([]core.Header{header})
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(mockRepository.MarkHeaderCheckedPassedHeaderID).To(Equal(header.Id)) repository.AssertMarkHeaderCheckedCalledWith(header.Id)
}) })
It("doesn't call MarkCheckedHeader when there are logs", func() { It("doesn't call MarkCheckedHeader when there are logs", func() {
header := core.Header{Id: GinkgoRandomSeed()} header := core.Header{Id: GinkgoRandomSeed()}
mockRepository.SetMissingHeaders([]core.Header{header}) repository.SetMissingHeaders([]core.Header{header})
fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog}) fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog})
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(mockRepository.MarkHeaderCheckedPassedHeaderID).To(Equal(int64(0))) repository.AssertMarkHeaderCheckedNotCalled()
}) })
It("returns an error if MarkCheckedHeader fails", func() { It("returns an error if MarkCheckedHeader fails", func() {
header := core.Header{Id: GinkgoRandomSeed()} header := core.Header{Id: rand.Int63()}
mockRepository.SetMissingHeaders([]core.Header{header}) repository.SetMissingHeaders([]core.Header{header})
mockRepository.SetMissingHeadersErr(fakes.FakeError) repository.SetMissingHeadersError(fakes.FakeError)
err := transformer.Execute() err := transformer.Execute()