Refactor pit file ilk transfomer

This commit is contained in:
Edvard 2018-10-16 11:53:13 +02:00
parent 837a777c8a
commit 98062a7970
5 changed files with 55 additions and 109 deletions

View File

@ -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 ilk
import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
)
var IlkFileConfig = shared.TransformerConfig{
TransformerName: "pit file ilk",
ContractAddresses: []string{shared.PitContractAddress},
ContractAbi: shared.PitABI,
Topics: []string{shared.PitFileIlkSignature},
StartingBlockNumber: 0,
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) ([]PitFileIlkModel, error)
}
type PitFileIlkConverter struct{} type PitFileIlkConverter struct{}
func (PitFileIlkConverter) ToModels(ethLogs []types.Log) ([]PitFileIlkModel, error) { func (PitFileIlkConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
var models []PitFileIlkModel 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,32 +19,23 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
) )
type Repository interface {
Create(headerID int64, models []PitFileIlkModel) error
MarkHeaderChecked(headerID int64) error
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
}
type PitFileIlkRepository struct { type PitFileIlkRepository struct {
db *postgres.DB DB *postgres.DB
} }
func NewPitFileIlkRepository(db *postgres.DB) PitFileIlkRepository { func (repository PitFileIlkRepository) Create(headerID int64, models []interface{}) error {
return PitFileIlkRepository{ tx, err := repository.DB.Begin()
db: db,
}
}
func (repository PitFileIlkRepository) Create(headerID int64, models []PitFileIlkModel) error {
tx, err := repository.db.Begin()
if err != nil { if err != nil {
return err return err
} }
var pitFileIlk PitFileIlkModel
for _, model := range models { for _, model := range models {
pitFileIlk = model.(PitFileIlkModel)
_, err = tx.Exec( _, err = tx.Exec(
`INSERT into maker.pit_file_ilk (header_id, ilk, what, data, tx_idx, raw_log) `INSERT into maker.pit_file_ilk (header_id, ilk, what, data, tx_idx, raw_log)
VALUES($1, $2, $3, $4::NUMERIC, $5, $6)`, VALUES($1, $2, $3, $4::NUMERIC, $5, $6)`,
headerID, model.Ilk, model.What, model.Data, model.TransactionIndex, model.Raw, headerID, pitFileIlk.Ilk, pitFileIlk.What, pitFileIlk.Data, pitFileIlk.TransactionIndex, pitFileIlk.Raw,
) )
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
@ -63,7 +54,7 @@ func (repository PitFileIlkRepository) Create(headerID int64, models []PitFileIl
} }
func (repository PitFileIlkRepository) MarkHeaderChecked(headerID int64) error { func (repository PitFileIlkRepository) MarkHeaderChecked(headerID int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_ilk_checked) _, err := repository.DB.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_ilk_checked)
VALUES ($1, $2) VALUES ($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
UPDATE SET pit_file_ilk_checked = $2`, headerID, true) UPDATE SET pit_file_ilk_checked = $2`, headerID, true)
@ -72,7 +63,7 @@ func (repository PitFileIlkRepository) MarkHeaderChecked(headerID int64) error {
func (repository PitFileIlkRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository PitFileIlkRepository) 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 +73,11 @@ func (repository PitFileIlkRepository) MissingHeaders(startingBlockNumber, endin
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 PitFileIlkRepository) 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 ilk
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 PitFileIlkTransformer struct {
Config shared.TransformerConfig
Converter Converter
Fetcher shared.LogFetcher
Repository Repository
}
type PitFileIlkTransformerInitializer struct {
Config shared.TransformerConfig
}
func (initializer PitFileIlkTransformerInitializer) NewPitFileIlkTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
converter := PitFileIlkConverter{}
fetcher := shared.NewFetcher(blockChain)
repository := NewPitFileIlkRepository(db)
return PitFileIlkTransformer{
Config: initializer.Config,
Converter: converter,
Fetcher: fetcher,
Repository: repository,
}
}
func (transformer PitFileIlkTransformer) Execute() error {
missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber)
if err != nil {
return err
}
log.Printf("Fetching pit file ilk event logs for %d headers \n", len(missingHeaders))
for _, header := range missingHeaders {
topics := [][]common.Hash{{common.HexToHash(shared.PitFileIlkSignature)}}
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
}

View File

@ -67,9 +67,15 @@ var (
FrobTransformerInitializer = frob.FrobTransformerInitializer{Config: frob.FrobConfig}.NewFrobTransformer FrobTransformerInitializer = frob.FrobTransformerInitializer{Config: frob.FrobConfig}.NewFrobTransformer
pitFileConfig = pit_file.PitFileConfig pitFileConfig = pit_file.PitFileConfig
PitFileDebtCeilingTransformerInitializer = debt_ceiling.PitFileDebtCeilingTransformerInitializer{Config: pitFileConfig}.NewPitFileDebtCeilingTransformer PitFileDebtCeilingTransformerInitializer = debt_ceiling.PitFileDebtCeilingTransformerInitializer{Config: pitFileConfig}.NewPitFileDebtCeilingTransformer
PitFileIlkTransformerInitializer = ilk.PitFileIlkTransformerInitializer{Config: pitFileConfig}.NewPitFileIlkTransformer
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
PitFileIlkTransformerInitializer = factories.Transformer{
Config: ilk.IlkFileConfig,
Converter: &ilk.PitFileIlkConverter{},
Repository: &ilk.PitFileIlkRepository{},
Fetcher: &shared.Fetcher{},
}.NewTransformer
PitFileStabilityFeeTransformerInitializer = factories.Transformer{ PitFileStabilityFeeTransformerInitializer = factories.Transformer{
Config: stability_fee.StabilityFeeFileConfig, Config: stability_fee.StabilityFeeFileConfig,
Converter: &stability_fee.PitFileStabilityFeeConverter{}, Converter: &stability_fee.PitFileStabilityFeeConverter{},