From 24199cb67eeeffcce507eb03275912170c3c6bf3 Mon Sep 17 00:00:00 2001 From: Edvard Date: Tue, 16 Oct 2018 16:55:24 +0200 Subject: [PATCH] Refactor pit file debt ceiling transformer --- .../pit_file/{ => debt_ceiling}/config.go | 7 +- .../pit_file/debt_ceiling/converter.go | 8 +- .../pit_file/debt_ceiling/repository.go | 53 ++++++------- .../pit_file/debt_ceiling/transformer.go | 79 ------------------- pkg/transformers/transformers.go | 10 ++- 5 files changed, 39 insertions(+), 118 deletions(-) rename pkg/transformers/pit_file/{ => debt_ceiling}/config.go (80%) delete mode 100644 pkg/transformers/pit_file/debt_ceiling/transformer.go diff --git a/pkg/transformers/pit_file/config.go b/pkg/transformers/pit_file/debt_ceiling/config.go similarity index 80% rename from pkg/transformers/pit_file/config.go rename to pkg/transformers/pit_file/debt_ceiling/config.go index 73656bd4..23ee22b4 100644 --- a/pkg/transformers/pit_file/config.go +++ b/pkg/transformers/pit_file/debt_ceiling/config.go @@ -12,16 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -package pit_file +package debt_ceiling import ( "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" ) -var PitFileConfig = shared.TransformerConfig{ +var DebtCeilingFileConfig = shared.TransformerConfig{ + TransformerName: "pit file debt ceiling", ContractAddresses: []string{shared.PitContractAddress}, ContractAbi: shared.PitABI, - Topics: []string{shared.PitFileIlkSignature, shared.PitFileDebtCeilingSignature, shared.PitFileStabilityFeeSignature}, + Topics: []string{shared.PitFileDebtCeilingSignature}, StartingBlockNumber: 0, EndingBlockNumber: 10000000, } diff --git a/pkg/transformers/pit_file/debt_ceiling/converter.go b/pkg/transformers/pit_file/debt_ceiling/converter.go index f47044f3..620644a8 100644 --- a/pkg/transformers/pit_file/debt_ceiling/converter.go +++ b/pkg/transformers/pit_file/debt_ceiling/converter.go @@ -24,14 +24,10 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" ) -type Converter interface { - ToModels(ethLogs []types.Log) ([]PitFileDebtCeilingModel, error) -} - type PitFileDebtCeilingConverter struct{} -func (PitFileDebtCeilingConverter) ToModels(ethLogs []types.Log) ([]PitFileDebtCeilingModel, error) { - var models []PitFileDebtCeilingModel +func (PitFileDebtCeilingConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) { + var models []interface{} for _, ethLog := range ethLogs { err := verifyLog(ethLog) if err != nil { diff --git a/pkg/transformers/pit_file/debt_ceiling/repository.go b/pkg/transformers/pit_file/debt_ceiling/repository.go index a5e86335..831ce12c 100644 --- a/pkg/transformers/pit_file/debt_ceiling/repository.go +++ b/pkg/transformers/pit_file/debt_ceiling/repository.go @@ -19,70 +19,69 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" ) -type Repository interface { - Create(headerID int64, models []PitFileDebtCeilingModel) error - MarkHeaderChecked(headerID int64) error - MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) -} - type PitFileDebtCeilingRepository struct { - db *postgres.DB + DB *postgres.DB } -func NewPitFileDebtCeilingRepository(db *postgres.DB) PitFileDebtCeilingRepository { - return PitFileDebtCeilingRepository{ - db: db, - } -} - -func (repository PitFileDebtCeilingRepository) Create(headerID int64, models []PitFileDebtCeilingModel) error { - tx, err := repository.db.Begin() +func (repository PitFileDebtCeilingRepository) Create(headerID int64, models []interface{}) error { + tx, err := repository.DB.Begin() if err != nil { return err } + + var pitFileDC PitFileDebtCeilingModel for _, model := range models { + pitFileDC = model.(PitFileDebtCeilingModel) _, err = tx.Exec( `INSERT into maker.pit_file_debt_ceiling (header_id, what, data, tx_idx, raw_log) VALUES($1, $2, $3::NUMERIC, $4, $5)`, - headerID, model.What, model.Data, model.TransactionIndex, model.Raw, + headerID, pitFileDC.What, pitFileDC.Data, pitFileDC.TransactionIndex, pitFileDC.Raw, ) + if err != nil { tx.Rollback() return err } } + _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_debt_ceiling_checked) VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET pit_file_debt_ceiling_checked = $2`, headerID, true) + ON CONFLICT (header_id) DO + UPDATE SET pit_file_debt_ceiling_checked = $2`, headerID, true) + if err != nil { tx.Rollback() return err } + return tx.Commit() } func (repository PitFileDebtCeilingRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_debt_ceiling_checked) + _, err := repository.DB.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_debt_ceiling_checked) VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET pit_file_debt_ceiling_checked = $2`, headerID, true) + ON CONFLICT (header_id) DO + UPDATE SET pit_file_debt_ceiling_checked = $2`, headerID, true) return err } func (repository PitFileDebtCeilingRepository) 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 + LEFT JOIN checked_headers on headers.id = header_id WHERE (header_id ISNULL OR pit_file_debt_ceiling_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, + AND headers.block_number >= $1 + AND headers.block_number <= $2 + AND headers.eth_node_fingerprint = $3`, startingBlockNumber, endingBlockNumber, - repository.db.Node.ID, + repository.DB.Node.ID, ) return result, err } + +func (repository *PitFileDebtCeilingRepository) SetDB(db *postgres.DB) { + repository.DB = db +} diff --git a/pkg/transformers/pit_file/debt_ceiling/transformer.go b/pkg/transformers/pit_file/debt_ceiling/transformer.go deleted file mode 100644 index a82b2eff..00000000 --- a/pkg/transformers/pit_file/debt_ceiling/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 debt_ceiling - -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/pit_file" - "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" -) - -type PitFileDebtCeilingTransformer struct { - Config shared.TransformerConfig - Converter Converter - Fetcher shared.LogFetcher - Repository Repository -} - -type PitFileDebtCeilingTransformerInitializer struct { - Config shared.TransformerConfig -} - -func (initializer PitFileDebtCeilingTransformerInitializer) NewPitFileDebtCeilingTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { - converter := PitFileDebtCeilingConverter{} - fetcher := shared.NewFetcher(blockChain) - repository := NewPitFileDebtCeilingRepository(db) - return PitFileDebtCeilingTransformer{ - Config: initializer.Config, - Converter: converter, - Fetcher: fetcher, - Repository: repository, - } -} - -func (transformer PitFileDebtCeilingTransformer) Execute() error { - missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber) - if err != nil { - return err - } - log.Printf("Fetching pit file debt ceiling event logs for %d headers \n", len(missingHeaders)) - for _, header := range missingHeaders { - topics := [][]common.Hash{{common.HexToHash(shared.PitFileDebtCeilingSignature)}} - matchingLogs, err := transformer.Fetcher.FetchLogs(pit_file.PitFileConfig.ContractAddresses, topics, header.BlockNumber) - if err != nil { - return err - } - if len(matchingLogs) < 1 { - err = transformer.Repository.MarkHeaderChecked(header.Id) - if err != nil { - return err - } - } - model, err := transformer.Converter.ToModels(matchingLogs) - if err != nil { - return err - } - err = transformer.Repository.Create(header.Id, model) - if err != nil { - return err - } - } - return nil -} diff --git a/pkg/transformers/transformers.go b/pkg/transformers/transformers.go index dc181fdb..1612535e 100644 --- a/pkg/transformers/transformers.go +++ b/pkg/transformers/transformers.go @@ -31,7 +31,6 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/transformers/flip_kick" "github.com/vulcanize/vulcanizedb/pkg/transformers/flop_kick" "github.com/vulcanize/vulcanizedb/pkg/transformers/frob" - "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/debt_ceiling" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" @@ -65,10 +64,15 @@ var ( FlipKickTransformerInitializer = flip_kick.FlipKickTransformerInitializer{Config: flip_kick.FlipKickConfig}.NewFlipKickTransformer FlopKickTransformerInitializer = flop_kick.FlopKickTransformerInitializer{Config: flop_kick.Config}.NewFlopKickTransformer FrobTransformerInitializer = frob.FrobTransformerInitializer{Config: frob.FrobConfig}.NewFrobTransformer - pitFileConfig = pit_file.PitFileConfig - PitFileDebtCeilingTransformerInitializer = debt_ceiling.PitFileDebtCeilingTransformerInitializer{Config: pitFileConfig}.NewPitFileDebtCeilingTransformer PriceFeedTransformerInitializer = price_feeds.PriceFeedTransformerInitializer{Config: price_feeds.PriceFeedConfig}.NewPriceFeedTransformer VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer + PitFileDebtCeilingTransformerInitializer = factories.Transformer{ + Config: debt_ceiling.DebtCeilingFileConfig, + Converter: &debt_ceiling.PitFileDebtCeilingConverter{}, + Repository: &debt_ceiling.PitFileDebtCeilingRepository{}, + Fetcher: &shared.Fetcher{}, + }.NewTransformer + PitFileIlkTransformerInitializer = factories.Transformer{ Config: ilk.IlkFileConfig, Converter: &ilk.PitFileIlkConverter{},