Refactor vat init transformer to use factory

This commit is contained in:
Edvard 2018-10-11 16:06:37 +02:00
parent 177d212cc4
commit fedbce80f6
5 changed files with 34 additions and 112 deletions

View File

@ -72,12 +72,18 @@ var (
PriceFeedTransformerInitializer = price_feeds.PriceFeedTransformerInitializer{Config: price_feeds.PriceFeedConfig}.NewPriceFeedTransformer PriceFeedTransformerInitializer = price_feeds.PriceFeedTransformerInitializer{Config: price_feeds.PriceFeedConfig}.NewPriceFeedTransformer
TendTransformerInitializer = tend.TendTransformerInitializer{Config: tend.TendConfig}.NewTendTransformer TendTransformerInitializer = tend.TendTransformerInitializer{Config: tend.TendConfig}.NewTendTransformer
VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer VatGrabTransformerInitializer = vat_grab.VatGrabTransformerInitializer{Config: vat_grab.VatGrabConfig}.NewVatGrabTransformer
VatInitTransformerInitializer = vat_init.VatInitTransformerInitializer{Config: vat_init.VatInitConfig}.NewVatInitTransformer VatInitTransformerInitializer = factories.Transformer{
VatMoveTransformerInitializer = factories.Transformer{ Config: vat_init.VatInitConfig,
Config: vat_move.VatMoveConfig, Converter: &vat_init.VatInitConverter{},
Converter: &vat_move.VatMoveConverter{}, Repository: &vat_init.VatInitRepository{DB: nil},
Repository: &vat_move.VatMoveRepository{}, Fetcher: &shared.Fetcher{},
Fetcher: &shared.Fetcher{}, }.NewTransformer
VatMoveTransformerInitializer = factories.Transformer{
Config: vat_move.VatMoveConfig,
Converter: &vat_move.VatMoveConverter{},
Repository: &vat_move.VatMoveRepository{DB: nil},
Fetcher: &shared.Fetcher{},
}.NewTransformer }.NewTransformer
VatHealTransformerInitializer = vat_heal.VatHealTransformerInitializer{Config: vat_heal.VatHealConfig}.NewVatHealTransformer VatHealTransformerInitializer = vat_heal.VatHealTransformerInitializer{Config: vat_heal.VatHealConfig}.NewVatHealTransformer
VatFoldTransformerInitializer = vat_fold.VatFoldTransformerInitializer{Config: vat_fold.VatFoldConfig}.NewVatFoldTransformer VatFoldTransformerInitializer = vat_fold.VatFoldTransformerInitializer{Config: vat_fold.VatFoldConfig}.NewVatFoldTransformer

View File

@ -19,6 +19,7 @@ import (
) )
var VatInitConfig = shared.TransformerConfig{ var VatInitConfig = shared.TransformerConfig{
TransformerName: "vat init",
ContractAddresses: []string{shared.VatContractAddress}, ContractAddresses: []string{shared.VatContractAddress},
ContractAbi: shared.VatABI, ContractAbi: shared.VatABI,
Topics: []string{shared.VatInitSignature}, Topics: []string{shared.VatInitSignature},

View File

@ -18,18 +18,13 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
) )
type Converter interface {
ToModels(ethLogs []types.Log) ([]VatInitModel, error)
}
type VatInitConverter struct{} type VatInitConverter struct{}
func (VatInitConverter) ToModels(ethLogs []types.Log) ([]VatInitModel, error) { func (VatInitConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
var models []VatInitModel 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

@ -17,40 +17,34 @@ package vat_init
import ( import (
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"log"
) )
type Repository interface {
Create(headerID int64, models []VatInitModel) error
MarkHeaderChecked(headerID int64) error
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
}
type VatInitRepository struct { type VatInitRepository struct {
db *postgres.DB DB *postgres.DB
} }
func NewVatInitRepository(db *postgres.DB) VatInitRepository { func (repository VatInitRepository) Create(headerID int64, models []interface{}) error {
return VatInitRepository{ tx, err := repository.DB.Begin()
db: db,
}
}
func (repository VatInitRepository) Create(headerID int64, models []VatInitModel) error {
tx, err := repository.db.Begin()
if err != nil { if err != nil {
return err return err
} }
var vatInit VatInitModel
for _, model := range models { for _, model := range models {
vatInit = model.(VatInitModel)
log.Printf("VatInit model: %v", vatInit)
_, err = tx.Exec( _, err = tx.Exec(
`INSERT into maker.vat_init (header_id, ilk, tx_idx, raw_log) `INSERT INTO maker.vat_init (header_id, ilk, tx_idx, raw_log)
VALUES($1, $2, $3, $4)`, VALUES($1, $2, $3, $4)`,
headerID, model.Ilk, model.TransactionIndex, model.Raw, headerID, vatInit.Ilk, vatInit.TransactionIndex, vatInit.Raw,
) )
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
log.Printf("Error: %v \n", err)
return err return err
} }
} }
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vat_init_checked) _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vat_init_checked)
VALUES($1, $2) VALUES($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
@ -64,7 +58,7 @@ func (repository VatInitRepository) Create(headerID int64, models []VatInitModel
} }
func (repository VatInitRepository) MarkHeaderChecked(headerID int64) error { func (repository VatInitRepository) MarkHeaderChecked(headerID int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_init_checked) _, err := repository.DB.Exec(`INSERT INTO public.checked_headers (header_id, vat_init_checked)
VALUES ($1, $2) VALUES ($1, $2)
ON CONFLICT (header_id) DO ON CONFLICT (header_id) DO
UPDATE SET vat_init_checked = $2`, headerID, true) UPDATE SET vat_init_checked = $2`, headerID, true)
@ -73,7 +67,7 @@ func (repository VatInitRepository) MarkHeaderChecked(headerID int64) error {
func (repository VatInitRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository VatInitRepository) 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
@ -83,7 +77,11 @@ func (repository VatInitRepository) MissingHeaders(startingBlockNumber, endingBl
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 *VatInitRepository) SetDB(db *postgres.DB) {
repository.DB = db
}

View File

@ -1,78 +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 vat_init
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/shared"
)
type VatInitTransformerInitializer struct {
Config shared.TransformerConfig
}
func (initializer VatInitTransformerInitializer) NewVatInitTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
converter := VatInitConverter{}
fetcher := shared.NewFetcher(blockChain)
repository := NewVatInitRepository(db)
return VatInitTransformer{
Config: initializer.Config,
Converter: converter,
Fetcher: fetcher,
Repository: repository,
}
}
type VatInitTransformer struct {
Config shared.TransformerConfig
Converter Converter
Fetcher shared.LogFetcher
Repository Repository
}
func (transformer VatInitTransformer) Execute() error {
missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber)
if err != nil {
return err
}
log.Printf("Fetching vat init event logs for %d headers \n", len(missingHeaders))
for _, header := range missingHeaders {
topics := [][]common.Hash{{common.HexToHash(shared.VatInitSignature)}}
matchingLogs, err := transformer.Fetcher.FetchLogs(VatInitConfig.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
}