Refactor pit file debt ceiling transformer

This commit is contained in:
Edvard 2018-10-16 16:55:24 +02:00
parent 0239c4daf6
commit 24199cb67e
5 changed files with 39 additions and 118 deletions

View File

@ -12,16 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package pit_file package debt_ceiling
import ( import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
) )
var PitFileConfig = shared.TransformerConfig{ var DebtCeilingFileConfig = shared.TransformerConfig{
TransformerName: "pit file debt ceiling",
ContractAddresses: []string{shared.PitContractAddress}, ContractAddresses: []string{shared.PitContractAddress},
ContractAbi: shared.PitABI, ContractAbi: shared.PitABI,
Topics: []string{shared.PitFileIlkSignature, shared.PitFileDebtCeilingSignature, shared.PitFileStabilityFeeSignature}, Topics: []string{shared.PitFileDebtCeilingSignature},
StartingBlockNumber: 0, StartingBlockNumber: 0,
EndingBlockNumber: 10000000, EndingBlockNumber: 10000000,
} }

View File

@ -24,14 +24,10 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
) )
type Converter interface {
ToModels(ethLogs []types.Log) ([]PitFileDebtCeilingModel, error)
}
type PitFileDebtCeilingConverter struct{} type PitFileDebtCeilingConverter struct{}
func (PitFileDebtCeilingConverter) ToModels(ethLogs []types.Log) ([]PitFileDebtCeilingModel, error) { func (PitFileDebtCeilingConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
var models []PitFileDebtCeilingModel 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

@ -19,51 +19,46 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "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 { type PitFileDebtCeilingRepository struct {
db *postgres.DB DB *postgres.DB
} }
func NewPitFileDebtCeilingRepository(db *postgres.DB) PitFileDebtCeilingRepository { func (repository PitFileDebtCeilingRepository) Create(headerID int64, models []interface{}) error {
return PitFileDebtCeilingRepository{ tx, err := repository.DB.Begin()
db: db,
}
}
func (repository PitFileDebtCeilingRepository) Create(headerID int64, models []PitFileDebtCeilingModel) error {
tx, err := repository.db.Begin()
if err != nil { if err != nil {
return err return err
} }
var pitFileDC PitFileDebtCeilingModel
for _, model := range models { for _, model := range models {
pitFileDC = model.(PitFileDebtCeilingModel)
_, err = tx.Exec( _, err = tx.Exec(
`INSERT into maker.pit_file_debt_ceiling (header_id, what, data, tx_idx, raw_log) `INSERT into maker.pit_file_debt_ceiling (header_id, what, data, tx_idx, raw_log)
VALUES($1, $2, $3::NUMERIC, $4, $5)`, 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 { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
} }
} }
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_debt_ceiling_checked) _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_debt_ceiling_checked)
VALUES ($1, $2) VALUES ($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
UPDATE SET pit_file_debt_ceiling_checked = $2`, headerID, true) UPDATE SET pit_file_debt_ceiling_checked = $2`, headerID, true)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
} }
return tx.Commit() return tx.Commit()
} }
func (repository PitFileDebtCeilingRepository) MarkHeaderChecked(headerID int64) error { 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) VALUES ($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
UPDATE SET pit_file_debt_ceiling_checked = $2`, headerID, true) UPDATE SET pit_file_debt_ceiling_checked = $2`, headerID, true)
@ -72,7 +67,7 @@ func (repository PitFileDebtCeilingRepository) MarkHeaderChecked(headerID int64)
func (repository PitFileDebtCeilingRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository PitFileDebtCeilingRepository) 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
@ -82,7 +77,11 @@ func (repository PitFileDebtCeilingRepository) MissingHeaders(startingBlockNumbe
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 *PitFileDebtCeilingRepository) SetDB(db *postgres.DB) {
repository.DB = db
}

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

View File

@ -31,7 +31,6 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/flip_kick" "github.com/vulcanize/vulcanizedb/pkg/transformers/flip_kick"
"github.com/vulcanize/vulcanizedb/pkg/transformers/flop_kick" "github.com/vulcanize/vulcanizedb/pkg/transformers/flop_kick"
"github.com/vulcanize/vulcanizedb/pkg/transformers/frob" "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/debt_ceiling"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee"
@ -65,10 +64,15 @@ var (
FlipKickTransformerInitializer = flip_kick.FlipKickTransformerInitializer{Config: flip_kick.FlipKickConfig}.NewFlipKickTransformer FlipKickTransformerInitializer = flip_kick.FlipKickTransformerInitializer{Config: flip_kick.FlipKickConfig}.NewFlipKickTransformer
FlopKickTransformerInitializer = flop_kick.FlopKickTransformerInitializer{Config: flop_kick.Config}.NewFlopKickTransformer FlopKickTransformerInitializer = flop_kick.FlopKickTransformerInitializer{Config: flop_kick.Config}.NewFlopKickTransformer
FrobTransformerInitializer = frob.FrobTransformerInitializer{Config: frob.FrobConfig}.NewFrobTransformer 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 PriceFeedTransformerInitializer = price_feeds.PriceFeedTransformerInitializer{Config: price_feeds.PriceFeedConfig}.NewPriceFeedTransformer
VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer 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{ PitFileIlkTransformerInitializer = factories.Transformer{
Config: ilk.IlkFileConfig, Config: ilk.IlkFileConfig,
Converter: &ilk.PitFileIlkConverter{}, Converter: &ilk.PitFileIlkConverter{},