From 2cc303a2f5679f8b377ba9688736c8e1a68512a8 Mon Sep 17 00:00:00 2001 From: Edvard Date: Tue, 16 Oct 2018 11:35:05 +0200 Subject: [PATCH] Refactor stability_fee transfomer --- .../pit_file/stability_fee/config.go | 28 +++++++ .../pit_file/stability_fee/converter.go | 8 +- .../pit_file/stability_fee/repository.go | 37 +++++---- .../pit_file/stability_fee/transformer.go | 79 ------------------- pkg/transformers/transformers.go | 10 ++- 5 files changed, 56 insertions(+), 106 deletions(-) create mode 100644 pkg/transformers/pit_file/stability_fee/config.go delete mode 100644 pkg/transformers/pit_file/stability_fee/transformer.go diff --git a/pkg/transformers/pit_file/stability_fee/config.go b/pkg/transformers/pit_file/stability_fee/config.go new file mode 100644 index 00000000..5c6e4e07 --- /dev/null +++ b/pkg/transformers/pit_file/stability_fee/config.go @@ -0,0 +1,28 @@ +// 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 stability_fee + +import ( + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" +) + +var StabilityFeeFileConfig = shared.TransformerConfig{ + TransformerName: "stability fee", + ContractAddresses: []string{shared.PitContractAddress}, + ContractAbi: shared.PitABI, + Topics: []string{shared.PitFileStabilityFeeSignature}, + StartingBlockNumber: 0, + EndingBlockNumber: 10000000, +} diff --git a/pkg/transformers/pit_file/stability_fee/converter.go b/pkg/transformers/pit_file/stability_fee/converter.go index 27d20155..6f47ec51 100644 --- a/pkg/transformers/pit_file/stability_fee/converter.go +++ b/pkg/transformers/pit_file/stability_fee/converter.go @@ -23,14 +23,10 @@ import ( "github.com/ethereum/go-ethereum/core/types" ) -type Converter interface { - ToModels(ethLogs []types.Log) ([]PitFileStabilityFeeModel, error) -} - type PitFileStabilityFeeConverter struct{} -func (PitFileStabilityFeeConverter) ToModels(ethLogs []types.Log) ([]PitFileStabilityFeeModel, error) { - var models []PitFileStabilityFeeModel +func (PitFileStabilityFeeConverter) 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/stability_fee/repository.go b/pkg/transformers/pit_file/stability_fee/repository.go index 6942512f..e94b6a5e 100644 --- a/pkg/transformers/pit_file/stability_fee/repository.go +++ b/pkg/transformers/pit_file/stability_fee/repository.go @@ -19,51 +19,46 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" ) -type Repository interface { - Create(headerID int64, models []PitFileStabilityFeeModel) error - MarkHeaderChecked(headerID int64) error - MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) -} - type PitFileStabilityFeeRepository struct { - db *postgres.DB + DB *postgres.DB } -func NewPitFileStabilityFeeRepository(db *postgres.DB) PitFileStabilityFeeRepository { - return PitFileStabilityFeeRepository{ - db: db, - } -} - -func (repository PitFileStabilityFeeRepository) Create(headerID int64, models []PitFileStabilityFeeModel) error { - tx, err := repository.db.Begin() +func (repository PitFileStabilityFeeRepository) Create(headerID int64, models []interface{}) error { + tx, err := repository.DB.Begin() if err != nil { return err } + + var pitFileSF PitFileStabilityFeeModel for _, model := range models { + pitFileSF = model.(PitFileStabilityFeeModel) _, err = tx.Exec( `INSERT into maker.pit_file_stability_fee (header_id, what, data, tx_idx, raw_log) VALUES($1, $2, $3, $4, $5)`, - headerID, model.What, model.Data, model.TransactionIndex, model.Raw, + headerID, pitFileSF.What, pitFileSF.Data, pitFileSF.TransactionIndex, pitFileSF.Raw, ) + if err != nil { tx.Rollback() return err } } + _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_stability_fee_checked) VALUES ($1, $2) ON CONFLICT (header_id) DO UPDATE SET pit_file_stability_fee_checked = $2`, headerID, true) + if err != nil { tx.Rollback() return err } + return tx.Commit() } func (repository PitFileStabilityFeeRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_stability_fee_checked) + _, err := repository.DB.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_stability_fee_checked) VALUES ($1, $2) ON CONFLICT (header_id) DO UPDATE SET pit_file_stability_fee_checked = $2`, headerID, true) @@ -72,7 +67,7 @@ func (repository PitFileStabilityFeeRepository) MarkHeaderChecked(headerID int64 func (repository PitFileStabilityFeeRepository) 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 @@ -82,7 +77,11 @@ func (repository PitFileStabilityFeeRepository) MissingHeaders(startingBlockNumb AND headers.eth_node_fingerprint = $3`, startingBlockNumber, endingBlockNumber, - repository.db.Node.ID, + repository.DB.Node.ID, ) return result, err } + +func (repository *PitFileStabilityFeeRepository) SetDB(db *postgres.DB) { + repository.DB = db +} diff --git a/pkg/transformers/pit_file/stability_fee/transformer.go b/pkg/transformers/pit_file/stability_fee/transformer.go deleted file mode 100644 index 781c223e..00000000 --- a/pkg/transformers/pit_file/stability_fee/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 stability_fee - -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 PitFileStabilityFeeTransformer struct { - Config shared.TransformerConfig - Converter Converter - Fetcher shared.LogFetcher - Repository Repository -} - -type PitFileStabilityFeeTransformerInitializer struct { - Config shared.TransformerConfig -} - -func (initializer PitFileStabilityFeeTransformerInitializer) NewPitFileStabilityFeeTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { - converter := PitFileStabilityFeeConverter{} - fetcher := shared.NewFetcher(blockChain) - repository := NewPitFileStabilityFeeRepository(db) - return PitFileStabilityFeeTransformer{ - Config: initializer.Config, - Converter: converter, - Fetcher: fetcher, - Repository: repository, - } -} - -func (transformer PitFileStabilityFeeTransformer) Execute() error { - missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber) - if err != nil { - return err - } - log.Printf("Fetching pit file stability fee event logs for %d headers \n", len(missingHeaders)) - for _, header := range missingHeaders { - topics := [][]common.Hash{{common.HexToHash(shared.PitFileStabilityFeeSignature)}} - 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 - } - } - models, err := transformer.Converter.ToModels(matchingLogs) - if err != nil { - return err - } - err = transformer.Repository.Create(header.Id, models) - if err != nil { - return err - } - } - return nil -} diff --git a/pkg/transformers/transformers.go b/pkg/transformers/transformers.go index 96aa26ab..0499b4ab 100644 --- a/pkg/transformers/transformers.go +++ b/pkg/transformers/transformers.go @@ -68,10 +68,16 @@ var ( pitFileConfig = pit_file.PitFileConfig PitFileDebtCeilingTransformerInitializer = debt_ceiling.PitFileDebtCeilingTransformerInitializer{Config: pitFileConfig}.NewPitFileDebtCeilingTransformer PitFileIlkTransformerInitializer = ilk.PitFileIlkTransformerInitializer{Config: pitFileConfig}.NewPitFileIlkTransformer - PitFileStabilityFeeTransformerInitializer = stability_fee.PitFileStabilityFeeTransformerInitializer{Config: pitFileConfig}.NewPitFileStabilityFeeTransformer PriceFeedTransformerInitializer = price_feeds.PriceFeedTransformerInitializer{Config: price_feeds.PriceFeedConfig}.NewPriceFeedTransformer VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer - TendTransformerInitializer = factories.Transformer{ + PitFileStabilityFeeTransformerInitializer = factories.Transformer{ + Config: stability_fee.StabilityFeeFileConfig, + Converter: &stability_fee.PitFileStabilityFeeConverter{}, + Repository: &stability_fee.PitFileStabilityFeeRepository{}, + Fetcher: &shared.Fetcher{}, + }.NewTransformer + + TendTransformerInitializer = factories.Transformer{ Config: tend.TendConfig, Converter: &tend.TendConverter{}, Repository: &tend.TendRepository{},