Refactor Tend transformer using factory

This commit is contained in:
Edvard 2018-10-12 16:17:49 +02:00
parent 4d36f6200d
commit a4df8f348d
8 changed files with 76 additions and 176 deletions

View File

@ -17,6 +17,7 @@ package tend
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
var TendConfig = shared.TransformerConfig{ var TendConfig = shared.TransformerConfig{
TransformerName: "tend",
ContractAddresses: []string{shared.FlipperContractAddress}, ContractAddresses: []string{shared.FlipperContractAddress},
ContractAbi: shared.FlipperABI, ContractAbi: shared.FlipperABI,
Topics: []string{shared.TendFunctionSignature}, Topics: []string{shared.TendFunctionSignature},

View File

@ -23,17 +23,9 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
) )
type Converter interface {
ToModels(ethLogs []types.Log) ([]TendModel, error)
}
type TendConverter struct{} type TendConverter struct{}
func NewTendConverter() TendConverter { func (TendConverter) ToModels(ethLogs []types.Log) (results []interface{}, err error) {
return TendConverter{}
}
func (c TendConverter) ToModels(ethLogs []types.Log) (results []TendModel, err error) {
for _, ethLog := range ethLogs { for _, ethLog := range ethLogs {
err := validateLog(ethLog) err := validateLog(ethLog)
if err != nil { if err != nil {

View File

@ -28,7 +28,7 @@ var _ = Describe("Tend TendConverter", func() {
var converter tend.TendConverter var converter tend.TendConverter
BeforeEach(func() { BeforeEach(func() {
converter = tend.NewTendConverter() converter = tend.TendConverter{}
}) })
Describe("ToModels", func() { Describe("ToModels", func() {

View File

@ -19,31 +19,24 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
) )
type Repository interface {
Create(headerId int64, tends []TendModel) error
MarkHeaderChecked(headerId int64) error
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
}
type TendRepository struct { type TendRepository struct {
DB *postgres.DB DB *postgres.DB
} }
func NewTendRepository(db *postgres.DB) TendRepository { func (repository TendRepository) Create(headerId int64, models []interface{}) error {
return TendRepository{DB: db} tx, err := repository.DB.Begin()
}
func (r TendRepository) Create(headerId int64, tends []TendModel) error {
tx, err := r.DB.Begin()
if err != nil { if err != nil {
return err return err
} }
for _, tend := range tends { var tend TendModel
for _, model := range models {
tend = model.(TendModel)
_, err = tx.Exec( _, err = tx.Exec(
`INSERT into maker.tend (header_id, bid_id, lot, bid, guy, tic, tx_idx, raw_log) `INSERT into maker.tend (header_id, bid_id, lot, bid, guy, tic, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6, $7, $8)`, VALUES($1, $2, $3, $4, $5, $6, $7, $8)`,
headerId, tend.BidId, tend.Lot, tend.Bid, tend.Guy, tend.Tic, tend.TransactionIndex, tend.Raw, headerId, tend.BidId, tend.Lot, tend.Bid, tend.Guy, tend.Tic, tend.TransactionIndex, tend.Raw,
) )
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -53,6 +46,7 @@ func (r TendRepository) Create(headerId int64, tends []TendModel) error {
VALUES ($1, $2) VALUES ($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
UPDATE SET tend_checked = $2`, headerId, true) UPDATE SET tend_checked = $2`, headerId, true)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -60,17 +54,17 @@ func (r TendRepository) Create(headerId int64, tends []TendModel) error {
return tx.Commit() return tx.Commit()
} }
func (r TendRepository) MarkHeaderChecked(headerId int64) error { func (repository TendRepository) MarkHeaderChecked(headerId int64) error {
_, err := r.DB.Exec(`INSERT INTO public.checked_headers (header_id, tend_checked) _, err := repository.DB.Exec(`INSERT INTO public.checked_headers (header_id, tend_checked)
VALUES ($1, $2) VALUES ($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
UPDATE SET tend_checked = $2`, headerId, true) UPDATE SET tend_checked = $2`, headerId, true)
return err return err
} }
func (r TendRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository TendRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header var result []core.Header
err := r.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
@ -80,8 +74,12 @@ func (r TendRepository) MissingHeaders(startingBlockNumber, endingBlockNumber in
AND headers.eth_node_fingerprint = $3`, AND headers.eth_node_fingerprint = $3`,
startingBlockNumber, startingBlockNumber,
endingBlockNumber, endingBlockNumber,
r.DB.Node.ID, repository.DB.Node.ID,
) )
return result, err return result, err
} }
func (repository *TendRepository) SetDB(db *postgres.DB) {
repository.DB = db
}

View File

@ -32,6 +32,7 @@ var _ = Describe("TendRepository", func() {
db *postgres.DB db *postgres.DB
tendRepository tend.TendRepository tendRepository tend.TendRepository
headerRepository repositories.HeaderRepository headerRepository repositories.HeaderRepository
headerId int64
err error err error
) )
@ -40,17 +41,15 @@ var _ = Describe("TendRepository", func() {
db = test_config.NewTestDB(node) db = test_config.NewTestDB(node)
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db) headerRepository = repositories.NewHeaderRepository(db)
tendRepository = tend.NewTendRepository(db) headerId, err = headerRepository.CreateOrUpdateHeader(core.Header{})
Expect(err).NotTo(HaveOccurred())
tendRepository = tend.TendRepository{DB: db}
}) })
Describe("Create", func() { Describe("Create", func() {
var headerId int64 It("persists a tend record", func() {
err := tendRepository.Create(headerId, []interface{}{test_data.TendModel})
BeforeEach(func() {
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err := tendRepository.Create(headerId, []tend.TendModel{test_data.TendModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
}) })
@ -81,13 +80,18 @@ var _ = Describe("TendRepository", func() {
}) })
It("returns an error if inserting a tend record fails", func() { It("returns an error if inserting a tend record fails", func() {
err = tendRepository.Create(headerId, []tend.TendModel{test_data.TendModel}) err := tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
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("deletes the tend record if its corresponding header record is deleted", func() { It("deletes the tend record if its corresponding header record is deleted", func() {
err := tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
var count int var count int
err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count) err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -182,7 +186,7 @@ var _ = Describe("TendRepository", func() {
node2 := core.Node{} node2 := core.Node{}
db2 := test_config.NewTestDB(node2) db2 := test_config.NewTestDB(node2)
headerRepository2 := repositories.NewHeaderRepository(db2) headerRepository2 := repositories.NewHeaderRepository(db2)
tendRepository2 := tend.NewTendRepository(db2) tendRepository2 := tend.TendRepository{DB: db2}
for _, number := range []int64{startingBlock, tendBlock, endingBlock} { for _, number := range []int64{startingBlock, tendBlock, endingBlock} {
headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(number)) headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(number))

View File

@ -1,88 +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 tend
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 TendTransformer struct {
Repository Repository
Fetcher shared.LogFetcher
Converter Converter
Config shared.TransformerConfig
}
type TendTransformerInitializer struct {
Config shared.TransformerConfig
}
func (i TendTransformerInitializer) NewTendTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
converter := NewTendConverter()
fetcher := shared.NewFetcher(blockChain)
repository := NewTendRepository(db)
return TendTransformer{
Fetcher: fetcher,
Repository: repository,
Converter: converter,
Config: i.Config,
}
}
func (t TendTransformer) Execute() error {
config := t.Config
topics := [][]common.Hash{{common.HexToHash(shared.TendFunctionSignature)}}
missingHeaders, err := t.Repository.MissingHeaders(config.StartingBlockNumber, config.EndingBlockNumber)
if err != nil {
log.Println("Error fetching missing headers:", err)
return err
}
log.Printf("Fetching tend event logs for %d headers \n", len(missingHeaders))
for _, header := range missingHeaders {
ethLogs, err := t.Fetcher.FetchLogs(config.ContractAddresses, topics, header.BlockNumber)
if err != nil {
log.Println("Error fetching matching logs:", err)
return err
}
if len(ethLogs) < 1 {
err := t.Repository.MarkHeaderChecked(header.Id)
if err != nil {
return err
}
}
models, err := t.Converter.ToModels(ethLogs)
if err != nil {
log.Println("Error converting logs:", err)
return err
}
err = t.Repository.Create(header.Id, models)
if err != nil {
log.Println("Error persisting tend record:", err)
return err
}
}
return nil
}

View File

@ -15,7 +15,7 @@
package tend_test package tend_test
import ( import (
"math/rand" "github.com/vulcanize/vulcanizedb/pkg/transformers/factories"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
@ -32,24 +32,28 @@ import (
) )
var _ = Describe("Tend Transformer", func() { var _ = Describe("Tend Transformer", func() {
var repository tend_mocks.MockTendRepository var (
var fetcher mocks.MockLogFetcher config = tend.TendConfig
var converter tend_mocks.MockTendConverter converter tend_mocks.MockTendConverter
var transformer tend.TendTransformer repository tend_mocks.MockTendRepository
var blockNumber1 = rand.Int63() fetcher mocks.MockLogFetcher
var blockNumber2 = rand.Int63() transformer shared.Transformer
headerOne core.Header
headerTwo core.Header
)
BeforeEach(func() { BeforeEach(func() {
converter = tend_mocks.MockTendConverter{}
repository = tend_mocks.MockTendRepository{} repository = tend_mocks.MockTendRepository{}
fetcher = mocks.MockLogFetcher{} fetcher = mocks.MockLogFetcher{}
converter = tend_mocks.MockTendConverter{} headerOne = core.Header{Id: GinkgoRandomSeed(), BlockNumber: GinkgoRandomSeed()}
headerTwo = core.Header{Id: GinkgoRandomSeed(), BlockNumber: GinkgoRandomSeed()}
transformer = tend.TendTransformer{ transformer = factories.Transformer{
Repository: &repository, Config: config,
Fetcher: &fetcher, Fetcher: &fetcher,
Converter: &converter, Converter: &converter,
Config: tend.TendConfig, Repository: &repository,
} }.NewTransformer(nil, nil)
}) })
It("gets missing headers for blocks in the configured range", func() { It("gets missing headers for blocks in the configured range", func() {
@ -68,12 +72,12 @@ var _ = Describe("Tend Transformer", func() {
}) })
It("fetches eth logs for each missing header", func() { It("fetches eth logs for each missing header", func() {
repository.SetMissingHeaders([]core.Header{{BlockNumber: blockNumber1}, {BlockNumber: blockNumber2}}) repository.SetMissingHeaders([]core.Header{headerOne, headerTwo})
expectedTopics := [][]common.Hash{{common.HexToHash(shared.TendFunctionSignature)}} expectedTopics := [][]common.Hash{{common.HexToHash(shared.TendFunctionSignature)}}
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(fetcher.FetchedBlocks).To(Equal([]int64{blockNumber1, blockNumber2})) Expect(fetcher.FetchedBlocks).To(Equal([]int64{headerOne.BlockNumber, headerTwo.BlockNumber}))
Expect(fetcher.FetchedTopics).To(Equal(expectedTopics)) Expect(fetcher.FetchedTopics).To(Equal(expectedTopics))
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{tend.TendConfig.ContractAddresses, tend.TendConfig.ContractAddresses})) Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{tend.TendConfig.ContractAddresses, tend.TendConfig.ContractAddresses}))
}) })
@ -88,34 +92,17 @@ var _ = Describe("Tend Transformer", func() {
}) })
It("marks header checked if no logs returned", func() { It("marks header checked if no logs returned", func() {
mockConverter := &tend_mocks.MockTendConverter{} repository.SetMissingHeaders([]core.Header{headerOne})
mockRepository := &tend_mocks.MockTendRepository{}
headerID := int64(123)
mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}})
mockFetcher := &mocks.MockLogFetcher{}
transformer := tend.TendTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
}
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
mockRepository.AssertMarkHeaderCheckedCalledWith(headerID) repository.AssertMarkHeaderCheckedCalledWith(headerOne.Id)
}) })
It("returns error if marking header checked returns err", func() { It("returns error if marking header checked returns err", func() {
mockConverter := &tend_mocks.MockTendConverter{} repository.SetMissingHeaders([]core.Header{headerOne})
mockRepository := &tend_mocks.MockTendRepository{} repository.SetMarkHeaderCheckedErr(fakes.FakeError)
mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}})
mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError)
mockFetcher := &mocks.MockLogFetcher{}
transformer := tend.TendTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
}
err := transformer.Execute() err := transformer.Execute()
@ -124,37 +111,37 @@ var _ = Describe("Tend Transformer", func() {
}) })
It("converts an eth log to a Model", func() { It("converts an eth log to a Model", func() {
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) repository.SetMissingHeaders([]core.Header{headerOne})
fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote}) fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote})
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(converter.LogsToConvert).To(Equal([]types.Log{test_data.TendLogNote})) Expect(converter.PassedLogs).To(Equal([]types.Log{test_data.TendLogNote}))
}) })
It("returns an error if converter fails", func() { It("returns an error if converter fails", func() {
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) repository.SetMissingHeaders([]core.Header{headerOne})
fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote}) fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote})
converter.SetConverterError(fakes.FakeError) converter.SetConverterError(fakes.FakeError)
err := transformer.Execute() err := transformer.Execute()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError)) Expect(err).To(MatchError(fakes.FakeError))
}) })
It("persists the tend record", func() { It("persists the tend record", func() {
headerId := int64(1) repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMissingHeaders([]core.Header{{BlockNumber: blockNumber1, Id: headerId}})
fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote}) fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote})
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(repository.PassedHeaderID).To(Equal(headerId)) Expect(repository.PassedHeaderID).To(Equal(headerOne.Id))
Expect(repository.PassedTendModel).To(Equal(test_data.TendModel)) Expect(repository.PassedTendModel).To(Equal(test_data.TendModel))
}) })
It("returns error if persisting tend record fails", func() { It("returns error if persisting tend record fails", func() {
repository.SetMissingHeaders([]core.Header{{BlockNumber: blockNumber1}}) repository.SetMissingHeaders([]core.Header{headerOne})
fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote}) fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote})
repository.SetCreateError(fakes.FakeError) repository.SetCreateError(fakes.FakeError)
err := transformer.Execute() err := transformer.Execute()

View File

@ -70,8 +70,14 @@ var (
PitFileIlkTransformerInitializer = ilk.PitFileIlkTransformerInitializer{Config: pitFileConfig}.NewPitFileIlkTransformer PitFileIlkTransformerInitializer = ilk.PitFileIlkTransformerInitializer{Config: pitFileConfig}.NewPitFileIlkTransformer
PitFileStabilityFeeTransformerInitializer = stability_fee.PitFileStabilityFeeTransformerInitializer{Config: pitFileConfig}.NewPitFileStabilityFeeTransformer PitFileStabilityFeeTransformerInitializer = stability_fee.PitFileStabilityFeeTransformerInitializer{Config: pitFileConfig}.NewPitFileStabilityFeeTransformer
PriceFeedTransformerInitializer = price_feeds.PriceFeedTransformerInitializer{Config: price_feeds.PriceFeedConfig}.NewPriceFeedTransformer PriceFeedTransformerInitializer = price_feeds.PriceFeedTransformerInitializer{Config: price_feeds.PriceFeedConfig}.NewPriceFeedTransformer
TendTransformerInitializer = tend.TendTransformerInitializer{Config: tend.TendConfig}.NewTendTransformer
VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer
TendTransformerInitializer = factories.Transformer{
Config: tend.TendConfig,
Converter: &tend.TendConverter{},
Repository: &tend.TendRepository{},
Fetcher: &shared.Fetcher{},
}.NewTransformer
VatInitTransformerInitializer = factories.Transformer{ VatInitTransformerInitializer = factories.Transformer{
Config: vat_init.VatInitConfig, Config: vat_init.VatInitConfig,
Converter: &vat_init.VatInitConverter{}, Converter: &vat_init.VatInitConverter{},