From 15e33db33d99ecee05b2286bca4c8451d834dc9d Mon Sep 17 00:00:00 2001 From: Edvard Date: Tue, 9 Oct 2018 16:03:34 +0200 Subject: [PATCH] Fix transformer factory --- pkg/transformers/factories/transformer.go | 76 ++++++++----------- pkg/transformers/shared/log_fetcher.go | 9 +++ .../test_data/mocks/log_fetcher.go | 3 + 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/pkg/transformers/factories/transformer.go b/pkg/transformers/factories/transformer.go index dd50edd9..a1d30b11 100644 --- a/pkg/transformers/factories/transformer.go +++ b/pkg/transformers/factories/transformer.go @@ -16,52 +16,37 @@ package factories import ( "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "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 TransformerInitializer struct { - Config shared.TransformerConfig - Converter Converter - Repository Repository -} - -type Model struct { - TransactionIndex uint `db:"tx_idx"` - Raw []byte `db:"raw_log"` -} - -type Converter interface { - ToModel(ethLog types.Log) (Model, error) -} - -type Repository interface { - Create(headerID int64, models []Model) error - MarkHeaderChecked(headerID int64) error - MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) -} - - -func (initializer TransformerInitializer) NewTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { - fetcher := shared.NewFetcher(blockChain) - return Transformer{ - Config: initializer.Config, - Converter: initializer.Converter, - Fetcher: fetcher, - Repository: initializer.Repository, - } -} - type Transformer struct { Config shared.TransformerConfig Converter Converter - Fetcher shared.LogFetcher Repository Repository + Fetcher shared.SettableLogFetcher +} + +type Converter interface { + ToModels(ethLog []types.Log) ([]interface{}, error) +} + +type Repository interface { + Create(headerID int64, models []interface{}) error + MarkHeaderChecked(headerID int64) error + MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) + SetDB(db *postgres.DB) +} + +func (transformer Transformer) NewTransformer(db *postgres.DB, bc core.BlockChain) shared.Transformer { + transformer.Repository.SetDB(db) + transformer.Fetcher.SetBC(bc) + return transformer } func (transformer Transformer) Execute() error { @@ -72,6 +57,7 @@ func (transformer Transformer) Execute() error { log.Printf("Fetching vat move event logs for %d headers \n", len(missingHeaders)) for _, header := range missingHeaders { + // TODO Needs signature in config topics := [][]common.Hash{{common.HexToHash(shared.VatMoveSignature)}} matchingLogs, err := transformer.Fetcher.FetchLogs(transformer.Config.ContractAddresses, topics, header.BlockNumber) if err != nil { @@ -85,16 +71,20 @@ func (transformer Transformer) Execute() error { } } - for _, log := range matchingLogs { - model, err := transformer.Converter.ToModel(log) - if err != nil { - return err - } + models, err := transformer.Converter.ToModels(matchingLogs) + if err != nil { + return err + } - err = transformer.Repository.Create(header.Id, []Model{model}) - if err != nil { - return err - } + // Can't assert the whole collection, need to wash types individually + var typelessModels []interface{} + for _, m := range models { + typelessModels = append(typelessModels, m.(interface{})) + } + + err = transformer.Repository.Create(header.Id, typelessModels) + if err != nil { + return err } } return nil diff --git a/pkg/transformers/shared/log_fetcher.go b/pkg/transformers/shared/log_fetcher.go index a42a18ff..46d4f3dc 100644 --- a/pkg/transformers/shared/log_fetcher.go +++ b/pkg/transformers/shared/log_fetcher.go @@ -28,10 +28,19 @@ type LogFetcher interface { FetchLogs(contractAddresses []string, topics [][]common.Hash, blockNumber int64) ([]types.Log, error) } +type SettableLogFetcher interface { + LogFetcher + SetBC(bc core.BlockChain) +} + type Fetcher struct { blockChain core.BlockChain } +func (fetcher *Fetcher) SetBC(bc core.BlockChain) { + fetcher.blockChain = bc +} + func NewFetcher(blockchain core.BlockChain) Fetcher { return Fetcher{ blockChain: blockchain, diff --git a/pkg/transformers/test_data/mocks/log_fetcher.go b/pkg/transformers/test_data/mocks/log_fetcher.go index 463bf878..b6b591a7 100644 --- a/pkg/transformers/test_data/mocks/log_fetcher.go +++ b/pkg/transformers/test_data/mocks/log_fetcher.go @@ -17,6 +17,7 @@ package mocks import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/core" ) type MockLogFetcher struct { @@ -35,6 +36,8 @@ func (mlf *MockLogFetcher) FetchLogs(contractAddresses []string, topics [][]comm return mlf.FetchedLogs, mlf.fetcherError } +func (mlf *MockLogFetcher) SetBC(_ core.BlockChain) {} + func (mlf *MockLogFetcher) SetFetcherError(err error) { mlf.fetcherError = err }