forked from cerc-io/ipld-eth-server
Refactor pit file ilk transfomer
This commit is contained in:
parent
837a777c8a
commit
98062a7970
28
pkg/transformers/pit_file/ilk/config.go
Normal file
28
pkg/transformers/pit_file/ilk/config.go
Normal 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,
|
||||
}
|
@ -24,14 +24,10 @@ import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||
)
|
||||
|
||||
type Converter interface {
|
||||
ToModels(ethLogs []types.Log) ([]PitFileIlkModel, error)
|
||||
}
|
||||
|
||||
type PitFileIlkConverter struct{}
|
||||
|
||||
func (PitFileIlkConverter) ToModels(ethLogs []types.Log) ([]PitFileIlkModel, error) {
|
||||
var models []PitFileIlkModel
|
||||
func (PitFileIlkConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
|
||||
var models []interface{}
|
||||
for _, ethLog := range ethLogs {
|
||||
err := verifyLog(ethLog)
|
||||
if err != nil {
|
||||
|
@ -19,32 +19,23 @@ import (
|
||||
"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 {
|
||||
db *postgres.DB
|
||||
DB *postgres.DB
|
||||
}
|
||||
|
||||
func NewPitFileIlkRepository(db *postgres.DB) PitFileIlkRepository {
|
||||
return PitFileIlkRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (repository PitFileIlkRepository) Create(headerID int64, models []PitFileIlkModel) error {
|
||||
tx, err := repository.db.Begin()
|
||||
func (repository PitFileIlkRepository) Create(headerID int64, models []interface{}) error {
|
||||
tx, err := repository.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var pitFileIlk PitFileIlkModel
|
||||
for _, model := range models {
|
||||
pitFileIlk = model.(PitFileIlkModel)
|
||||
_, err = tx.Exec(
|
||||
`INSERT into maker.pit_file_ilk (header_id, ilk, what, data, tx_idx, raw_log)
|
||||
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 {
|
||||
tx.Rollback()
|
||||
@ -63,7 +54,7 @@ func (repository PitFileIlkRepository) Create(headerID int64, models []PitFileIl
|
||||
}
|
||||
|
||||
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)
|
||||
ON CONFLICT (header_id) DO
|
||||
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) {
|
||||
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 +73,11 @@ func (repository PitFileIlkRepository) MissingHeaders(startingBlockNumber, endin
|
||||
AND headers.eth_node_fingerprint = $3`,
|
||||
startingBlockNumber,
|
||||
endingBlockNumber,
|
||||
repository.db.Node.ID,
|
||||
repository.DB.Node.ID,
|
||||
)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (repository PitFileIlkRepository) SetDB(db *postgres.DB) {
|
||||
repository.DB = db
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -67,9 +67,15 @@ var (
|
||||
FrobTransformerInitializer = frob.FrobTransformerInitializer{Config: frob.FrobConfig}.NewFrobTransformer
|
||||
pitFileConfig = pit_file.PitFileConfig
|
||||
PitFileDebtCeilingTransformerInitializer = debt_ceiling.PitFileDebtCeilingTransformerInitializer{Config: pitFileConfig}.NewPitFileDebtCeilingTransformer
|
||||
PitFileIlkTransformerInitializer = ilk.PitFileIlkTransformerInitializer{Config: pitFileConfig}.NewPitFileIlkTransformer
|
||||
PriceFeedTransformerInitializer = price_feeds.PriceFeedTransformerInitializer{Config: price_feeds.PriceFeedConfig}.NewPriceFeedTransformer
|
||||
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{
|
||||
Config: stability_fee.StabilityFeeFileConfig,
|
||||
Converter: &stability_fee.PitFileStabilityFeeConverter{},
|
||||
|
Loading…
Reference in New Issue
Block a user