Refactor stability_fee transfomer

This commit is contained in:
Edvard 2018-10-16 11:35:05 +02:00
parent 57b3744085
commit 2cc303a2f5
5 changed files with 56 additions and 106 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 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,
}

View File

@ -23,14 +23,10 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
) )
type Converter interface {
ToModels(ethLogs []types.Log) ([]PitFileStabilityFeeModel, error)
}
type PitFileStabilityFeeConverter struct{} type PitFileStabilityFeeConverter struct{}
func (PitFileStabilityFeeConverter) ToModels(ethLogs []types.Log) ([]PitFileStabilityFeeModel, error) { func (PitFileStabilityFeeConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
var models []PitFileStabilityFeeModel 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 []PitFileStabilityFeeModel) error
MarkHeaderChecked(headerID int64) error
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
}
type PitFileStabilityFeeRepository struct { type PitFileStabilityFeeRepository struct {
db *postgres.DB DB *postgres.DB
} }
func NewPitFileStabilityFeeRepository(db *postgres.DB) PitFileStabilityFeeRepository { func (repository PitFileStabilityFeeRepository) Create(headerID int64, models []interface{}) error {
return PitFileStabilityFeeRepository{ tx, err := repository.DB.Begin()
db: db,
}
}
func (repository PitFileStabilityFeeRepository) Create(headerID int64, models []PitFileStabilityFeeModel) error {
tx, err := repository.db.Begin()
if err != nil { if err != nil {
return err return err
} }
var pitFileSF PitFileStabilityFeeModel
for _, model := range models { for _, model := range models {
pitFileSF = model.(PitFileStabilityFeeModel)
_, err = tx.Exec( _, err = tx.Exec(
`INSERT into maker.pit_file_stability_fee (header_id, what, data, tx_idx, raw_log) `INSERT into maker.pit_file_stability_fee (header_id, what, data, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5)`, 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 { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
} }
} }
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_stability_fee_checked) _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_stability_fee_checked)
VALUES ($1, $2) VALUES ($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
UPDATE SET pit_file_stability_fee_checked = $2`, headerID, true) UPDATE SET pit_file_stability_fee_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 PitFileStabilityFeeRepository) MarkHeaderChecked(headerID int64) error { 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) VALUES ($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
UPDATE SET pit_file_stability_fee_checked = $2`, headerID, true) 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) { func (repository PitFileStabilityFeeRepository) 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 PitFileStabilityFeeRepository) MissingHeaders(startingBlockNumb
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 *PitFileStabilityFeeRepository) 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 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
}

View File

@ -68,9 +68,15 @@ var (
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 PitFileIlkTransformerInitializer = ilk.PitFileIlkTransformerInitializer{Config: pitFileConfig}.NewPitFileIlkTransformer
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
VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer
PitFileStabilityFeeTransformerInitializer = factories.Transformer{
Config: stability_fee.StabilityFeeFileConfig,
Converter: &stability_fee.PitFileStabilityFeeConverter{},
Repository: &stability_fee.PitFileStabilityFeeRepository{},
Fetcher: &shared.Fetcher{},
}.NewTransformer
TendTransformerInitializer = factories.Transformer{ TendTransformerInitializer = factories.Transformer{
Config: tend.TendConfig, Config: tend.TendConfig,
Converter: &tend.TendConverter{}, Converter: &tend.TendConverter{},