Fix transformer factory
This commit is contained in:
parent
1b9e6491da
commit
15e33db33d
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user