Frob transformer factory (#95)

* Updates to previous transformers

* Use transformer factory for Frob

* Move bite integration test to integrationt est pkg
This commit is contained in:
Elizabeth 2018-10-29 15:08:00 -05:00 committed by GitHub
parent 78b8173b37
commit ab4e904713
15 changed files with 155 additions and 444 deletions

View File

@ -59,6 +59,13 @@ var _ = Describe("Bite Converter", func() {
Expect(model).To(Equal(test_data.BiteModel)) Expect(model).To(Equal(test_data.BiteModel))
}) })
It("returns an error if the entity type is wrong", func() {
_, err := converter.ToModels([]interface{}{test_data.WrongEntity{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("entity of type test_data.WrongEntity, not bite.BiteEntity"))
})
It("handles nil values", func() { It("handles nil values", func() {
emptyLog, err := json.Marshal(types.Log{}) emptyLog, err := json.Marshal(types.Log{})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())

View File

@ -1,83 +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 bite_test
import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/ethereum/go-ethereum/rpc"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
"github.com/vulcanize/vulcanizedb/pkg/transformers/bite"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/test_config"
)
var _ = Describe("Integration tests", func() {
XIt("Fetches bite event logs from a local test chain", func() {
ipcPath := test_config.TestClient.IPCPath
rawRpcClient, err := rpc.Dial(ipcPath)
Expect(err).NotTo(HaveOccurred())
rpcClient := client.NewRpcClient(rawRpcClient, ipcPath)
ethClient := ethclient.NewClient(rawRpcClient)
blockChainClient := client.NewEthClient(ethClient)
realNode := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
realBlockChain := geth.NewBlockChain(blockChainClient, rpcClient, realNode, transactionConverter)
realFetcher := shared.NewFetcher(realBlockChain)
topic0 := common.HexToHash(shared.BiteSignature)
topics := [][]common.Hash{{topic0}}
result, err := realFetcher.FetchLogs(bite.BiteConfig.ContractAddresses, topics, int64(26))
Expect(err).NotTo(HaveOccurred())
Expect(len(result) > 0).To(BeTrue())
Expect(result[0].Address).To(Equal(common.HexToAddress(shared.CatContractAddress)))
Expect(result[0].TxHash).To(Equal(test_data.EthBiteLog.TxHash))
Expect(result[0].BlockNumber).To(Equal(test_data.EthBiteLog.BlockNumber))
Expect(result[0].Topics).To(Equal(test_data.EthBiteLog.Topics))
Expect(result[0].Index).To(Equal(test_data.EthBiteLog.Index))
})
It("unpacks an event log", func() {
address := common.HexToAddress(shared.CatContractAddress)
abi, err := geth.ParseAbi(shared.CatABI)
Expect(err).NotTo(HaveOccurred())
contract := bind.NewBoundContract(address, abi, nil, nil, nil)
entity := &bite.BiteEntity{}
var eventLog = test_data.EthBiteLog
err = contract.UnpackLog(entity, "Bite", eventLog)
Expect(err).NotTo(HaveOccurred())
expectedEntity := test_data.BiteEntity
Expect(entity.Art).To(Equal(expectedEntity.Art))
Expect(entity.Ilk).To(Equal(expectedEntity.Ilk))
Expect(entity.Ink).To(Equal(expectedEntity.Ink))
})
})

View File

@ -16,10 +16,11 @@ package frob
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
var FrobConfig = shared.TransformerConfig{ var FrobConfig = shared.SingleTransformerConfig{
TransformerName: shared.FrobLabel,
ContractAddresses: []string{shared.PitContractAddress}, ContractAddresses: []string{shared.PitContractAddress},
ContractAbi: shared.PitABI, ContractAbi: shared.PitABI,
Topics: []string{shared.FrobSignature}, Topic: shared.FrobSignature,
StartingBlockNumber: 0, StartingBlockNumber: 0,
EndingBlockNumber: 10000000, EndingBlockNumber: 10000000,
} }

View File

@ -15,24 +15,21 @@
package frob package frob
import ( import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/geth" "github.com/vulcanize/vulcanizedb/pkg/geth"
) )
type Converter interface {
ToEntities(contractAbi string, ethLogs []types.Log) ([]FrobEntity, error)
ToModels(entities []FrobEntity) ([]FrobModel, error)
}
type FrobConverter struct{} type FrobConverter struct{}
func (FrobConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]FrobEntity, error) { func (FrobConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]interface{}, error) {
var entities []FrobEntity var entities []interface{}
for _, ethLog := range ethLogs { for _, ethLog := range ethLogs {
entity := FrobEntity{} entity := FrobEntity{}
address := ethLog.Address address := ethLog.Address
@ -54,23 +51,28 @@ func (FrobConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]Frob
return entities, nil return entities, nil
} }
func (FrobConverter) ToModels(entities []FrobEntity) ([]FrobModel, error) { func (FrobConverter) ToModels(entities []interface{}) ([]interface{}, error) {
var models []FrobModel var models []interface{}
for _, entity := range entities { for _, entity := range entities {
rawLog, err := json.Marshal(entity.Raw) frobEntity, ok := entity.(FrobEntity)
if !ok {
return nil, fmt.Errorf("entity of type %T, not %T", entity, FrobEntity{})
}
rawLog, err := json.Marshal(frobEntity.Raw)
if err != nil { if err != nil {
return nil, err return nil, err
} }
model := FrobModel{ model := FrobModel{
Ilk: string(bytes.Trim(entity.Ilk[:], "\x00)")), Ilk: string(bytes.Trim(frobEntity.Ilk[:], "\x00)")),
Urn: common.BytesToAddress(entity.Urn[:]).String(), Urn: common.BytesToAddress(frobEntity.Urn[:]).String(),
Ink: entity.Ink.String(), Ink: frobEntity.Ink.String(),
Art: entity.Art.String(), Art: frobEntity.Art.String(),
Dink: entity.Dink.String(), Dink: frobEntity.Dink.String(),
Dart: entity.Dart.String(), Dart: frobEntity.Dart.String(),
IArt: entity.IArt.String(), IArt: frobEntity.IArt.String(),
LogIndex: entity.LogIndex, LogIndex: frobEntity.LogIndex,
TransactionIndex: entity.TransactionIndex, TransactionIndex: frobEntity.TransactionIndex,
Raw: rawLog, Raw: rawLog,
} }
models = append(models, model) models = append(models, model)

View File

@ -25,9 +25,8 @@ import (
) )
var _ = Describe("Frob converter", func() { var _ = Describe("Frob converter", func() {
var converter = frob.FrobConverter{}
It("converts a log to an entity", func() { It("converts a log to an entity", func() {
converter := frob.FrobConverter{}
entities, err := converter.ToEntities(shared.PitABI, []types.Log{test_data.EthFrobLog}) entities, err := converter.ToEntities(shared.PitABI, []types.Log{test_data.EthFrobLog})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -35,13 +34,24 @@ var _ = Describe("Frob converter", func() {
Expect(entities[0]).To(Equal(test_data.FrobEntity)) Expect(entities[0]).To(Equal(test_data.FrobEntity))
}) })
It("converts an entity to a model", func() { It("returns an error if converting to an entity fails", func() {
converter := frob.FrobConverter{} _, err := converter.ToEntities("bad abi", []types.Log{test_data.EthFrobLog})
models, err := converter.ToModels([]frob.FrobEntity{test_data.FrobEntity}) Expect(err).To(HaveOccurred())
})
It("converts an entity to a model", func() {
models, err := converter.ToModels([]interface{}{test_data.FrobEntity})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(len(models)).To(Equal(1)) Expect(len(models)).To(Equal(1))
Expect(models[0]).To(Equal(test_data.FrobModel)) Expect(models[0]).To(Equal(test_data.FrobModel))
}) })
It("returns an error if the entity type is wrong", func() {
_, err := converter.ToModels([]interface{}{test_data.WrongEntity{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("entity of type test_data.WrongEntity, not frob.FrobEntity"))
})
}) })

View File

@ -15,33 +15,30 @@
package frob package frob
import ( import (
"fmt"
"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"
) )
type Repository interface {
Create(headerID int64, models []FrobModel) error
MarkHeaderChecked(headerID int64) error
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
}
type FrobRepository struct { type FrobRepository struct {
db *postgres.DB db *postgres.DB
} }
func NewFrobRepository(db *postgres.DB) FrobRepository { func (repository FrobRepository) Create(headerID int64, models []interface{}) error {
return FrobRepository{db: db}
}
func (repository FrobRepository) Create(headerID int64, models []FrobModel) error {
tx, err := repository.db.Begin() tx, err := repository.db.Begin()
if err != nil { if err != nil {
return err return err
} }
for _, model := range models { for _, model := range models {
frobModel, ok := model.(FrobModel)
if !ok {
tx.Rollback()
return fmt.Errorf("model of type %T, not %T", model, FrobModel{})
}
_, err = tx.Exec(`INSERT INTO maker.frob (header_id, art, dart, dink, iart, ilk, ink, urn, raw_log, log_idx, tx_idx) _, err = tx.Exec(`INSERT INTO maker.frob (header_id, art, dart, dink, iart, ilk, ink, urn, raw_log, log_idx, tx_idx)
VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5::NUMERIC, $6, $7::NUMERIC, $8, $9, $10, $11)`, VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5::NUMERIC, $6, $7::NUMERIC, $8, $9, $10, $11)`,
headerID, model.Art, model.Dart, model.Dink, model.IArt, model.Ilk, model.Ink, model.Urn, model.Raw, model.LogIndex, model.TransactionIndex) headerID, frobModel.Art, frobModel.Dart, frobModel.Dink, frobModel.IArt, frobModel.Ilk, frobModel.Ink, frobModel.Urn, frobModel.Raw, frobModel.LogIndex, frobModel.TransactionIndex)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -82,3 +79,7 @@ func (repository FrobRepository) MissingHeaders(startingBlockNumber, endingBlock
) )
return result, err return result, err
} }
func (repository *FrobRepository) SetDB(db *postgres.DB) {
repository.db = db
}

View File

@ -33,7 +33,7 @@ import (
var _ = Describe("Frob repository", func() { var _ = Describe("Frob repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
frobRepository frob.Repository frobRepository frob.FrobRepository
err error err error
headerRepository datastore.HeaderRepository headerRepository datastore.HeaderRepository
) )
@ -42,7 +42,8 @@ var _ = Describe("Frob repository", func() {
db = test_config.NewTestDB(core.Node{}) db = test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db) headerRepository = repositories.NewHeaderRepository(db)
frobRepository = frob.NewFrobRepository(db) frobRepository = frob.FrobRepository{}
frobRepository.SetDB(db)
}) })
Describe("Create", func() { Describe("Create", func() {
@ -54,7 +55,7 @@ var _ = Describe("Frob repository", func() {
}) })
It("adds a frob", func() { It("adds a frob", func() {
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) err = frobRepository.Create(headerID, []interface{}{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbFrob frob.FrobModel var dbFrob frob.FrobModel
@ -73,7 +74,7 @@ var _ = Describe("Frob repository", func() {
}) })
It("marks header as checked for logs", func() { It("marks header as checked for logs", func() {
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) err = frobRepository.Create(headerID, []interface{}{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var headerChecked bool var headerChecked bool
@ -86,7 +87,7 @@ var _ = Describe("Frob repository", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID) _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) err = frobRepository.Create(headerID, []interface{}{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var headerChecked bool var headerChecked bool
@ -96,17 +97,23 @@ var _ = Describe("Frob repository", func() {
}) })
It("does not duplicate frob events", func() { It("does not duplicate frob events", func() {
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) err = frobRepository.Create(headerID, []interface{}{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) err = frobRepository.Create(headerID, []interface{}{test_data.FrobModel})
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
}) })
It("returns an error if the wrong model type is passed in", func() {
err = frobRepository.Create(headerID, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type test_data.WrongModel, not frob.FrobModel"))
})
It("removes frob if corresponding header is deleted", func() { It("removes frob if corresponding header is deleted", func() {
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) err = frobRepository.Create(headerID, []interface{}{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
@ -203,7 +210,8 @@ var _ = Describe("Frob repository", func() {
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) _, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
} }
frobRepositoryTwo := frob.NewFrobRepository(dbTwo) frobRepositoryTwo := frob.FrobRepository{}
frobRepositoryTwo.SetDB(dbTwo)
err := frobRepository.MarkHeaderChecked(headerIDs[0]) err := frobRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())

View File

@ -1,82 +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 frob
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 FrobTransformer struct {
Config shared.TransformerConfig
Converter Converter
Fetcher shared.LogFetcher
Repository Repository
}
type FrobTransformerInitializer struct {
Config shared.TransformerConfig
}
func (initializer FrobTransformerInitializer) NewFrobTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
converter := FrobConverter{}
fetcher := shared.NewFetcher(blockChain)
repository := NewFrobRepository(db)
return FrobTransformer{
Config: initializer.Config,
Converter: converter,
Fetcher: fetcher,
Repository: repository,
}
}
func (transformer FrobTransformer) Execute() error {
missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber)
if err != nil {
return err
}
log.Printf("Fetching frob event logs for %d headers \n", len(missingHeaders))
for _, header := range missingHeaders {
topics := [][]common.Hash{{common.HexToHash(shared.FrobSignature)}}
matchingLogs, err := transformer.Fetcher.FetchLogs(FrobConfig.ContractAddresses, topics, header.BlockNumber)
if err != nil {
return err
}
if len(matchingLogs) < 1 {
err = transformer.Repository.MarkHeaderChecked(header.Id)
if err != nil {
return err
}
}
entities, err := transformer.Converter.ToEntities(FrobConfig.ContractAbi, matchingLogs)
if err != nil {
return err
}
models, err := transformer.Converter.ToModels(entities)
if err != nil {
return err
}
err = transformer.Repository.Create(header.Id, models)
if err != nil {
return err
}
}
return nil
}

View File

@ -15,30 +15,41 @@
package frob_test package frob_test
import ( import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/transformers/factories"
"github.com/vulcanize/vulcanizedb/pkg/transformers/frob" "github.com/vulcanize/vulcanizedb/pkg/transformers/frob"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks"
frob_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/frob"
) )
var _ = Describe("Frob transformer", func() { var _ = Describe("Frob transformer", func() {
It("gets missing headers for block numbers specified in config", func() { var (
repository := &frob_mocks.MockFrobRepository{} repository mocks.MockRepository
transformer := frob.FrobTransformer{ transformer shared.Transformer
Config: frob.FrobConfig, fetcher mocks.MockLogFetcher
Fetcher: &mocks.MockLogFetcher{}, converter mocks.MockConverter
Converter: &frob_mocks.MockFrobConverter{}, config = frob.FrobConfig
Repository: repository, )
} BeforeEach(func() {
repository = mocks.MockRepository{}
fetcher = mocks.MockLogFetcher{}
converter = mocks.MockConverter{}
transformer = factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}.NewTransformer(nil, nil)
})
It("gets missing headers for block numbers specified in config", func() {
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -47,13 +58,7 @@ var _ = Describe("Frob transformer", func() {
}) })
It("returns error if repository returns error for missing headers", func() { It("returns error if repository returns error for missing headers", func() {
repository := &frob_mocks.MockFrobRepository{} repository.SetMissingHeadersError(fakes.FakeError)
repository.SetMissingHeadersErr(fakes.FakeError)
transformer := frob.FrobTransformer{
Fetcher: &mocks.MockLogFetcher{},
Converter: &frob_mocks.MockFrobConverter{},
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
@ -62,14 +67,7 @@ var _ = Describe("Frob transformer", func() {
}) })
It("fetches logs for missing headers", func() { It("fetches logs for missing headers", func() {
fetcher := &mocks.MockLogFetcher{}
repository := &frob_mocks.MockFrobRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}}) repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}})
transformer := frob.FrobTransformer{
Fetcher: fetcher,
Converter: &frob_mocks.MockFrobConverter{},
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
@ -80,15 +78,8 @@ var _ = Describe("Frob transformer", func() {
}) })
It("returns error if fetcher returns error", func() { It("returns error if fetcher returns error", func() {
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetcherError(fakes.FakeError) fetcher.SetFetcherError(fakes.FakeError)
repository := &frob_mocks.MockFrobRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := frob.FrobTransformer{
Fetcher: fetcher,
Converter: &frob_mocks.MockFrobConverter{},
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
@ -97,34 +88,18 @@ var _ = Describe("Frob transformer", func() {
}) })
It("marks header checked if no logs returned", func() { It("marks header checked if no logs returned", func() {
mockConverter := &frob_mocks.MockFrobConverter{}
mockRepository := &frob_mocks.MockFrobRepository{}
headerID := int64(123) headerID := int64(123)
mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}}) repository.SetMissingHeaders([]core.Header{{Id: headerID}})
mockFetcher := &mocks.MockLogFetcher{}
transformer := frob.FrobTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
}
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
mockRepository.AssertMarkHeaderCheckedCalledWith(headerID) repository.AssertMarkHeaderCheckedCalledWith(headerID)
}) })
It("returns error if marking header checked returns err", func() { It("returns error if marking header checked returns err", func() {
mockConverter := &frob_mocks.MockFrobConverter{} repository.SetMissingHeaders([]core.Header{{Id: int64(123)}})
mockRepository := &frob_mocks.MockFrobRepository{} repository.SetMarkHeaderCheckedError(fakes.FakeError)
mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}})
mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError)
mockFetcher := &mocks.MockLogFetcher{}
transformer := frob.FrobTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
}
err := transformer.Execute() err := transformer.Execute()
@ -133,36 +108,20 @@ var _ = Describe("Frob transformer", func() {
}) })
It("converts matching logs to entity", func() { It("converts matching logs to entity", func() {
converter := &frob_mocks.MockFrobConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog})
repository := &frob_mocks.MockFrobRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := frob.FrobTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(converter.PassedContractABI).To(Equal(frob.FrobConfig.ContractAbi)) Expect(converter.ContractAbi).To(Equal(frob.FrobConfig.ContractAbi))
Expect(converter.PassedLogs).To(Equal([]types.Log{test_data.EthFrobLog})) Expect(converter.LogsToConvert).To(Equal([]types.Log{test_data.EthFrobLog}))
}) })
It("returns error if converting to entity returns error", func() { It("returns error if converting to entity returns error", func() {
converter := &frob_mocks.MockFrobConverter{} converter.ToEntitiesError = fakes.FakeError
converter.SetToEntitiesError(fakes.FakeError)
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog})
repository := &frob_mocks.MockFrobRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := frob.FrobTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
@ -171,35 +130,20 @@ var _ = Describe("Frob transformer", func() {
}) })
It("converts frob entity to model", func() { It("converts frob entity to model", func() {
converter := &frob_mocks.MockFrobConverter{} converter.EntitiesToReturn = []interface{}{test_data.FrobEntity}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog})
repository := &frob_mocks.MockFrobRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := frob.FrobTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(converter.PassedEntities).To(Equal([]frob.FrobEntity{test_data.FrobEntity})) Expect(converter.EntitiesToConvert[0]).To(Equal(test_data.FrobEntity))
}) })
It("returns error if converting to model returns error", func() { It("returns error if converting to model returns error", func() {
converter := &frob_mocks.MockFrobConverter{} converter.ToModelsError = fakes.FakeError
converter.SetToModelsError(fakes.FakeError)
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog})
repository := &frob_mocks.MockFrobRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := frob.FrobTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
@ -208,37 +152,22 @@ var _ = Describe("Frob transformer", func() {
}) })
It("persists frob model", func() { It("persists frob model", func() {
converter := &frob_mocks.MockFrobConverter{} converter.ModelsToReturn = []interface{}{test_data.FrobModel}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog})
repository := &frob_mocks.MockFrobRepository{}
fakeHeader := core.Header{BlockNumber: 1, Id: 2} fakeHeader := core.Header{BlockNumber: 1, Id: 2}
repository.SetMissingHeaders([]core.Header{fakeHeader}) repository.SetMissingHeaders([]core.Header{fakeHeader})
transformer := frob.FrobTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id)) Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id))
Expect(repository.PassedModels).To(Equal([]frob.FrobModel{test_data.FrobModel})) Expect(repository.PassedModels[0]).To(Equal(test_data.FrobModel))
}) })
It("returns error if repository returns error for create", func() { It("returns error if repository returns error for create", func() {
converter := &frob_mocks.MockFrobConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthFrobLog})
repository := &frob_mocks.MockFrobRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}}) repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}})
repository.SetCreateError(fakes.FakeError) repository.SetCreateError(fakes.FakeError)
transformer := frob.FrobTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()

View File

@ -18,9 +18,13 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/transformers/bite" "github.com/vulcanize/vulcanizedb/pkg/transformers/bite"
"github.com/vulcanize/vulcanizedb/pkg/transformers/factories" "github.com/vulcanize/vulcanizedb/pkg/transformers/factories"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
@ -65,4 +69,23 @@ var _ = Describe("Bite Transformer", func() {
Expect(dbResult[0].Tab).To(Equal("149846666666666655744")) Expect(dbResult[0].Tab).To(Equal("149846666666666655744"))
Expect(dbResult[0].Urn).To(Equal("0x0000d8b4147eDa80Fec7122AE16DA2479Cbd7ffB")) Expect(dbResult[0].Urn).To(Equal("0x0000d8b4147eDa80Fec7122AE16DA2479Cbd7ffB"))
}) })
It("unpacks an event log", func() {
address := common.HexToAddress(shared.CatContractAddress)
abi, err := geth.ParseAbi(shared.CatABI)
Expect(err).NotTo(HaveOccurred())
contract := bind.NewBoundContract(address, abi, nil, nil, nil)
entity := &bite.BiteEntity{}
var eventLog = test_data.EthBiteLog
err = contract.UnpackLog(entity, "Bite", eventLog)
Expect(err).NotTo(HaveOccurred())
expectedEntity := test_data.BiteEntity
Expect(entity.Art).To(Equal(expectedEntity.Art))
Expect(entity.Ilk).To(Equal(expectedEntity.Ilk))
Expect(entity.Ink).To(Equal(expectedEntity.Ink))
})
}) })

View File

@ -21,6 +21,7 @@ import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/geth" "github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/transformers/factories"
"github.com/vulcanize/vulcanizedb/pkg/transformers/frob" "github.com/vulcanize/vulcanizedb/pkg/transformers/frob"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
@ -45,8 +46,13 @@ var _ = Describe("Frob Transformer", func() {
err = persistHeader(db, blockNumber) err = persistHeader(db, blockNumber)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
initializer := frob.FrobTransformerInitializer{Config: config} initializer := factories.Transformer{
transformer := initializer.NewFrobTransformer(db, blockchain) Config: frob.FrobConfig,
Converter: &frob.FrobConverter{},
Repository: &frob.FrobRepository{},
Fetcher: &shared.Fetcher{},
}
transformer := initializer.NewTransformer(db, blockchain)
err = transformer.Execute() err = transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())

View File

@ -1,49 +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 frob
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/frob"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
)
type MockFrobConverter struct {
PassedContractABI string
PassedLogs []types.Log
PassedEntities []frob.FrobEntity
toEntityError error
toModelError error
}
func (converter *MockFrobConverter) SetToEntitiesError(err error) {
converter.toEntityError = err
}
func (converter *MockFrobConverter) SetToModelsError(err error) {
converter.toModelError = err
}
func (converter *MockFrobConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]frob.FrobEntity, error) {
converter.PassedContractABI = contractAbi
converter.PassedLogs = ethLogs
return []frob.FrobEntity{test_data.FrobEntity}, converter.toEntityError
}
func (converter *MockFrobConverter) ToModels(frobEntities []frob.FrobEntity) ([]frob.FrobModel, error) {
converter.PassedEntities = frobEntities
return []frob.FrobModel{test_data.FrobModel}, converter.toModelError
}

View File

@ -1,70 +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 frob
import (
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/transformers/frob"
)
type MockFrobRepository struct {
createError error
PassedEndingBlockNumber int64
PassedModels []frob.FrobModel
PassedHeaderID int64
PassedStartingBlockNumber int64
markHeaderCheckedErr error
markHeaderCheckedPassedHeaderID int64
missingHeaders []core.Header
missingHeadersErr error
}
func (repository *MockFrobRepository) SetCreateError(err error) {
repository.createError = err
}
func (repository *MockFrobRepository) SetMarkHeaderCheckedErr(e error) {
repository.markHeaderCheckedErr = e
}
func (repository *MockFrobRepository) SetMissingHeadersErr(err error) {
repository.missingHeadersErr = err
}
func (repository *MockFrobRepository) SetMissingHeaders(headers []core.Header) {
repository.missingHeaders = headers
}
func (repository *MockFrobRepository) Create(headerID int64, models []frob.FrobModel) error {
repository.PassedHeaderID = headerID
repository.PassedModels = models
return repository.createError
}
func (repository *MockFrobRepository) MarkHeaderChecked(headerID int64) error {
repository.markHeaderCheckedPassedHeaderID = headerID
return repository.markHeaderCheckedErr
}
func (repository *MockFrobRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
repository.PassedStartingBlockNumber = startingBlockNumber
repository.PassedEndingBlockNumber = endingBlockNumber
return repository.missingHeaders, repository.missingHeadersErr
}
func (repository *MockFrobRepository) AssertMarkHeaderCheckedCalledWith(i int64) {
Expect(repository.markHeaderCheckedPassedHeaderID).To(Equal(i))
}

View File

@ -14,4 +14,5 @@
package test_data package test_data
type WrongEntity struct{}
type WrongModel struct{} type WrongModel struct{}

View File

@ -127,7 +127,14 @@ var (
}.NewLogNoteTransformer }.NewLogNoteTransformer
FlopKickTransformerInitializer = flop_kick.FlopKickTransformerInitializer{Config: flop_kick.Config}.NewFlopKickTransformer FlopKickTransformerInitializer = flop_kick.FlopKickTransformerInitializer{Config: flop_kick.Config}.NewFlopKickTransformer
FrobTransformerInitializer = frob.FrobTransformerInitializer{Config: frob.FrobConfig}.NewFrobTransformer
FrobTransformerInitializer = factories.Transformer{
Config: frob.FrobConfig,
Converter: &frob.FrobConverter{},
Repository: &frob.FrobRepository{},
Fetcher: &shared.Fetcher{},
}.NewTransformer
PitFileDebtCeilingTransformerInitializer = factories.LogNoteTransformer{ PitFileDebtCeilingTransformerInitializer = factories.LogNoteTransformer{
Config: debt_ceiling.DebtCeilingFileConfig, Config: debt_ceiling.DebtCeilingFileConfig,
Converter: &debt_ceiling.PitFileDebtCeilingConverter{}, Converter: &debt_ceiling.PitFileDebtCeilingConverter{},