Use transformer factory for Vat toll
This commit is contained in:
parent
6d662a6ce7
commit
1e7ee08af5
@ -3,7 +3,6 @@ package vat_toll
|
|||||||
import (
|
import (
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
||||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockVatTollConverter struct {
|
type MockVatTollConverter struct {
|
||||||
@ -11,9 +10,9 @@ type MockVatTollConverter struct {
|
|||||||
PassedLogs []types.Log
|
PassedLogs []types.Log
|
||||||
}
|
}
|
||||||
|
|
||||||
func (converter *MockVatTollConverter) ToModels(ethLogs []types.Log) ([]vat_toll.VatTollModel, error) {
|
func (converter *MockVatTollConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
|
||||||
converter.PassedLogs = ethLogs
|
converter.PassedLogs = ethLogs
|
||||||
return []vat_toll.VatTollModel{test_data.VatTollModel}, converter.err
|
return []interface{}{test_data.VatTollModel}, converter.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (converter *MockVatTollConverter) SetConverterError(e error) {
|
func (converter *MockVatTollConverter) SetConverterError(e error) {
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockVatTollRepository struct {
|
type MockVatTollRepository struct {
|
||||||
@ -16,10 +16,11 @@ type MockVatTollRepository struct {
|
|||||||
PassedStartingBlockNumber int64
|
PassedStartingBlockNumber int64
|
||||||
PassedEndingBlockNumber int64
|
PassedEndingBlockNumber int64
|
||||||
PassedHeaderID int64
|
PassedHeaderID int64
|
||||||
PassedModels []vat_toll.VatTollModel
|
PassedModels []interface{}
|
||||||
|
SetDbCalled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repository *MockVatTollRepository) Create(headerID int64, models []vat_toll.VatTollModel) error {
|
func (repository *MockVatTollRepository) Create(headerID int64, models []interface{}) error {
|
||||||
repository.PassedHeaderID = headerID
|
repository.PassedHeaderID = headerID
|
||||||
repository.PassedModels = models
|
repository.PassedModels = models
|
||||||
return repository.createErr
|
return repository.createErr
|
||||||
@ -36,6 +37,10 @@ func (repository *MockVatTollRepository) MissingHeaders(startingBlockNumber, end
|
|||||||
return repository.missingHeaders, repository.missingHeadersErr
|
return repository.missingHeaders, repository.missingHeadersErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (repository *MockVatTollRepository) SetDB(db *postgres.DB) {
|
||||||
|
repository.SetDbCalled = true
|
||||||
|
}
|
||||||
|
|
||||||
func (repository *MockVatTollRepository) SetCreateError(e error) {
|
func (repository *MockVatTollRepository) SetCreateError(e error) {
|
||||||
repository.createErr = e
|
repository.createErr = e
|
||||||
}
|
}
|
||||||
|
@ -133,7 +133,12 @@ var (
|
|||||||
Fetcher: &shared.Fetcher{},
|
Fetcher: &shared.Fetcher{},
|
||||||
}.NewTransformer
|
}.NewTransformer
|
||||||
VatSlipTransformerInitializer = vat_slip.VatSlipTransformerInitializer{Config: vat_slip.VatSlipConfig}.NewVatSlipTransformer
|
VatSlipTransformerInitializer = vat_slip.VatSlipTransformerInitializer{Config: vat_slip.VatSlipConfig}.NewVatSlipTransformer
|
||||||
VatTollTransformerInitializer = vat_toll.VatTollTransformerInitializer{Config: vat_toll.VatTollConfig}.NewVatTollTransformer
|
VatTollTransformerInitializer = factories.Transformer{
|
||||||
|
Config: vat_toll.VatTollConfig,
|
||||||
|
Converter: &vat_toll.VatTollConverter{},
|
||||||
|
Repository: &vat_toll.VatTollRepository{},
|
||||||
|
Fetcher: &shared.Fetcher{},
|
||||||
|
}.NewTransformer
|
||||||
VatTuneTransformerInitializer = factories.Transformer{
|
VatTuneTransformerInitializer = factories.Transformer{
|
||||||
Config: vat_tune.VatTuneConfig,
|
Config: vat_tune.VatTuneConfig,
|
||||||
Converter: &vat_tune.VatTuneConverter{},
|
Converter: &vat_tune.VatTuneConverter{},
|
||||||
|
@ -2,10 +2,11 @@ package vat_toll
|
|||||||
|
|
||||||
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||||
|
|
||||||
var VatTollConfig = shared.TransformerConfig{
|
var VatTollConfig = shared.SingleTransformerConfig{
|
||||||
|
TransformerName: shared.VatTollLabel,
|
||||||
ContractAddresses: []string{shared.VatContractAddress},
|
ContractAddresses: []string{shared.VatContractAddress},
|
||||||
ContractAbi: shared.VatABI,
|
ContractAbi: shared.VatABI,
|
||||||
Topics: []string{shared.VatTollSignature},
|
Topic: shared.VatTollSignature,
|
||||||
StartingBlockNumber: 0,
|
StartingBlockNumber: 0,
|
||||||
EndingBlockNumber: 10000000,
|
EndingBlockNumber: 10000000,
|
||||||
}
|
}
|
||||||
|
@ -9,14 +9,10 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Converter interface {
|
|
||||||
ToModels(ethLogs []types.Log) ([]VatTollModel, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type VatTollConverter struct{}
|
type VatTollConverter struct{}
|
||||||
|
|
||||||
func (VatTollConverter) ToModels(ethLogs []types.Log) ([]VatTollModel, error) {
|
func (VatTollConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
|
||||||
var models []VatTollModel
|
var models []interface{}
|
||||||
for _, ethLog := range ethLogs {
|
for _, ethLog := range ethLogs {
|
||||||
err := verifyLog(ethLog)
|
err := verifyLog(ethLog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,36 +1,30 @@
|
|||||||
package vat_toll
|
package vat_toll
|
||||||
|
|
||||||
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 []VatTollModel) error
|
|
||||||
MarkHeaderChecked(headerID int64) error
|
|
||||||
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type VatTollRepository struct {
|
type VatTollRepository struct {
|
||||||
db *postgres.DB
|
db *postgres.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVatTollRepository(db *postgres.DB) VatTollRepository {
|
func (repository VatTollRepository) Create(headerID int64, models []interface{}) error {
|
||||||
return VatTollRepository{
|
|
||||||
db: db,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repository VatTollRepository) Create(headerID int64, models []VatTollModel) 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 {
|
||||||
|
vatToll, ok := model.(VatTollModel)
|
||||||
|
if !ok {
|
||||||
|
tx.Rollback()
|
||||||
|
return fmt.Errorf("model of type %T, not %T", model, VatTollModel{})
|
||||||
|
}
|
||||||
_, err = tx.Exec(
|
_, err = tx.Exec(
|
||||||
`INSERT into maker.vat_toll (header_id, ilk, urn, take, tx_idx, log_idx, raw_log)
|
`INSERT into maker.vat_toll (header_id, ilk, urn, take, tx_idx, log_idx, raw_log)
|
||||||
VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`,
|
VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`,
|
||||||
headerID, model.Ilk, model.Urn, model.Take, model.TransactionIndex, model.LogIndex, model.Raw,
|
headerID, vatToll.Ilk, vatToll.Urn, vatToll.Take, vatToll.TransactionIndex, vatToll.LogIndex, vatToll.Raw,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
@ -72,3 +66,7 @@ func (repository VatTollRepository) MissingHeaders(startingBlockNumber, endingBl
|
|||||||
)
|
)
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (repository *VatTollRepository) SetDB(db *postgres.DB) {
|
||||||
|
repository.db = db
|
||||||
|
}
|
||||||
|
@ -14,12 +14,13 @@ import (
|
|||||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
||||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
|
||||||
"github.com/vulcanize/vulcanizedb/test_config"
|
"github.com/vulcanize/vulcanizedb/test_config"
|
||||||
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Vat toll repository", func() {
|
var _ = Describe("Vat toll repository", func() {
|
||||||
var (
|
var (
|
||||||
db *postgres.DB
|
db *postgres.DB
|
||||||
vatTollRepository vat_toll.Repository
|
vatTollRepository vat_toll.VatTollRepository
|
||||||
err error
|
err error
|
||||||
headerRepository datastore.HeaderRepository
|
headerRepository datastore.HeaderRepository
|
||||||
)
|
)
|
||||||
@ -28,7 +29,8 @@ var _ = Describe("Vat toll 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)
|
||||||
vatTollRepository = vat_toll.NewVatTollRepository(db)
|
vatTollRepository = vat_toll.VatTollRepository{}
|
||||||
|
vatTollRepository.SetDB(db)
|
||||||
})
|
})
|
||||||
|
|
||||||
Describe("Create", func() {
|
Describe("Create", func() {
|
||||||
@ -40,7 +42,7 @@ var _ = Describe("Vat toll repository", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("adds a vat toll event", func() {
|
It("adds a vat toll event", func() {
|
||||||
err = vatTollRepository.Create(headerID, []vat_toll.VatTollModel{test_data.VatTollModel})
|
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
var dbVatToll vat_toll.VatTollModel
|
var dbVatToll vat_toll.VatTollModel
|
||||||
@ -55,7 +57,7 @@ var _ = Describe("Vat toll repository", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("marks header as checked for logs", func() {
|
It("marks header as checked for logs", func() {
|
||||||
err = vatTollRepository.Create(headerID, []vat_toll.VatTollModel{test_data.VatTollModel})
|
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
var headerChecked bool
|
var headerChecked bool
|
||||||
@ -67,7 +69,7 @@ var _ = Describe("Vat toll repository", func() {
|
|||||||
It("updates the header to checked if checked headers row already exists", func() {
|
It("updates the header to checked if checked headers row already exists", 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 = vatTollRepository.Create(headerID, []vat_toll.VatTollModel{test_data.VatTollModel})
|
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
var headerChecked bool
|
var headerChecked bool
|
||||||
@ -77,21 +79,21 @@ var _ = Describe("Vat toll repository", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("does not duplicate vat toll events", func() {
|
It("does not duplicate vat toll events", func() {
|
||||||
err = vatTollRepository.Create(headerID, []vat_toll.VatTollModel{test_data.VatTollModel})
|
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
err = vatTollRepository.Create(headerID, []vat_toll.VatTollModel{test_data.VatTollModel})
|
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
|
||||||
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("allows for multiple vat toll events in one transaction if they have different log indexes", func() {
|
It("allows for multiple vat toll events in one transaction if they have different log indexes", func() {
|
||||||
err = vatTollRepository.Create(headerID, []vat_toll.VatTollModel{test_data.VatTollModel})
|
err = vatTollRepository.Create(headerID, []interface{}{test_data.VatTollModel})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
newVatToll := test_data.VatTollModel
|
newVatToll := test_data.VatTollModel
|
||||||
newVatToll.LogIndex = newVatToll.LogIndex + 1
|
newVatToll.LogIndex = newVatToll.LogIndex + 1
|
||||||
err := vatTollRepository.Create(headerID, []vat_toll.VatTollModel{newVatToll})
|
err := vatTollRepository.Create(headerID, []interface{}{newVatToll})
|
||||||
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
})
|
})
|
||||||
@ -105,6 +107,13 @@ var _ = Describe("Vat toll repository", func() {
|
|||||||
Expect(err).To(HaveOccurred())
|
Expect(err).To(HaveOccurred())
|
||||||
Expect(err).To(MatchError(sql.ErrNoRows))
|
Expect(err).To(MatchError(sql.ErrNoRows))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("returns an error if model is of wrong type", func() {
|
||||||
|
err = vatTollRepository.Create(headerID, []interface{}{test_data.WrongModel{}})
|
||||||
|
|
||||||
|
Expect(err).To(HaveOccurred())
|
||||||
|
Expect(err.Error()).To(ContainSubstring("model of type"))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Describe("MarkHeaderChecked", func() {
|
Describe("MarkHeaderChecked", func() {
|
||||||
@ -145,7 +154,7 @@ var _ = Describe("Vat toll repository", func() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
startingBlock = GinkgoRandomSeed()
|
startingBlock = rand.Int63()
|
||||||
vatTollBlock = startingBlock + 1
|
vatTollBlock = startingBlock + 1
|
||||||
endingBlock = startingBlock + 2
|
endingBlock = startingBlock + 2
|
||||||
|
|
||||||
@ -191,7 +200,8 @@ var _ = Describe("Vat toll repository", func() {
|
|||||||
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
|
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
}
|
}
|
||||||
vatTollRepositoryTwo := vat_toll.NewVatTollRepository(dbTwo)
|
vatTollRepositoryTwo := vat_toll.VatTollRepository{}
|
||||||
|
vatTollRepositoryTwo.SetDB(dbTwo)
|
||||||
err := vatTollRepository.MarkHeaderChecked(headerIDs[0])
|
err := vatTollRepository.MarkHeaderChecked(headerIDs[0])
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
@ -1,62 +0,0 @@
|
|||||||
package vat_toll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"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"
|
|
||||||
"log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type VatTollTransformer struct {
|
|
||||||
Config shared.TransformerConfig
|
|
||||||
Converter Converter
|
|
||||||
Fetcher shared.LogFetcher
|
|
||||||
Repository Repository
|
|
||||||
}
|
|
||||||
|
|
||||||
type VatTollTransformerInitializer struct {
|
|
||||||
Config shared.TransformerConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func (initializer VatTollTransformerInitializer) NewVatTollTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
|
|
||||||
converter := VatTollConverter{}
|
|
||||||
fetcher := shared.NewFetcher(blockChain)
|
|
||||||
repository := NewVatTollRepository(db)
|
|
||||||
return VatTollTransformer{
|
|
||||||
Config: initializer.Config,
|
|
||||||
Converter: converter,
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (transformer VatTollTransformer) Execute() error {
|
|
||||||
missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.Printf("Fetching vat toll event logs for %d headers \n", len(missingHeaders))
|
|
||||||
for _, header := range missingHeaders {
|
|
||||||
topics := [][]common.Hash{{common.HexToHash(shared.VatTollSignature)}}
|
|
||||||
matchingLogs, err := transformer.Fetcher.FetchLogs(VatTollConfig.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
|
|
||||||
}
|
|
@ -8,38 +8,55 @@ import (
|
|||||||
|
|
||||||
"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/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"
|
||||||
vat_toll_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/vat_toll"
|
vat_toll_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/vat_toll"
|
||||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
|
||||||
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Vat toll transformer", func() {
|
var _ = Describe("Vat toll transformer", func() {
|
||||||
It("gets missing headers for block numbers specified in config", func() {
|
var (
|
||||||
repository := &vat_toll_mocks.MockVatTollRepository{}
|
config = vat_toll.VatTollConfig
|
||||||
transformer := vat_toll.VatTollTransformer{
|
fetcher mocks.MockLogFetcher
|
||||||
Config: vat_toll.VatTollConfig,
|
converter vat_toll_mocks.MockVatTollConverter
|
||||||
Fetcher: &mocks.MockLogFetcher{},
|
repository vat_toll_mocks.MockVatTollRepository
|
||||||
Converter: &vat_toll_mocks.MockVatTollConverter{},
|
transformer shared.Transformer
|
||||||
Repository: repository,
|
headerOne core.Header
|
||||||
}
|
headerTwo core.Header
|
||||||
|
)
|
||||||
|
|
||||||
|
BeforeEach(func() {
|
||||||
|
fetcher = mocks.MockLogFetcher{}
|
||||||
|
converter = vat_toll_mocks.MockVatTollConverter{}
|
||||||
|
repository = vat_toll_mocks.MockVatTollRepository{}
|
||||||
|
headerOne = core.Header{Id: rand.Int63(), BlockNumber: rand.Int63()}
|
||||||
|
headerTwo = core.Header{Id: rand.Int63(), BlockNumber: rand.Int63()}
|
||||||
|
transformer = factories.Transformer{
|
||||||
|
Config: config,
|
||||||
|
Converter: &converter,
|
||||||
|
Fetcher: &fetcher,
|
||||||
|
Repository: &repository,
|
||||||
|
}.NewTransformer(nil, nil)
|
||||||
|
})
|
||||||
|
|
||||||
|
It("sets the blockchain and database", func() {
|
||||||
|
Expect(fetcher.SetBcCalled).To(BeTrue())
|
||||||
|
Expect(repository.SetDbCalled).To(BeTrue())
|
||||||
|
})
|
||||||
|
|
||||||
|
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())
|
||||||
Expect(repository.PassedStartingBlockNumber).To(Equal(vat_toll.VatTollConfig.StartingBlockNumber))
|
Expect(repository.PassedStartingBlockNumber).To(Equal(config.StartingBlockNumber))
|
||||||
Expect(repository.PassedEndingBlockNumber).To(Equal(vat_toll.VatTollConfig.EndingBlockNumber))
|
Expect(repository.PassedEndingBlockNumber).To(Equal(config.EndingBlockNumber))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("returns error if repository returns error for missing headers", func() {
|
It("returns error if repository returns error for missing headers", func() {
|
||||||
repository := &vat_toll_mocks.MockVatTollRepository{}
|
|
||||||
repository.SetMissingHeadersErr(fakes.FakeError)
|
repository.SetMissingHeadersErr(fakes.FakeError)
|
||||||
transformer := vat_toll.VatTollTransformer{
|
|
||||||
Fetcher: &mocks.MockLogFetcher{},
|
|
||||||
Converter: &vat_toll_mocks.MockVatTollConverter{},
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
@ -48,33 +65,19 @@ var _ = Describe("Vat toll transformer", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("fetches logs for missing headers", func() {
|
It("fetches logs for missing headers", func() {
|
||||||
fetcher := &mocks.MockLogFetcher{}
|
repository.SetMissingHeaders([]core.Header{headerOne, headerTwo})
|
||||||
repository := &vat_toll_mocks.MockVatTollRepository{}
|
|
||||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}})
|
|
||||||
transformer := vat_toll.VatTollTransformer{
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Converter: &vat_toll_mocks.MockVatTollConverter{},
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2}))
|
Expect(fetcher.FetchedBlocks).To(Equal([]int64{headerOne.BlockNumber, headerTwo.BlockNumber}))
|
||||||
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{vat_toll.VatTollConfig.ContractAddresses, vat_toll.VatTollConfig.ContractAddresses}))
|
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{config.ContractAddresses, config.ContractAddresses}))
|
||||||
Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.VatTollSignature)}}))
|
Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.VatTollSignature)}}))
|
||||||
})
|
})
|
||||||
|
|
||||||
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 := &vat_toll_mocks.MockVatTollRepository{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
|
||||||
transformer := vat_toll.VatTollTransformer{
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Converter: &vat_toll_mocks.MockVatTollConverter{},
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
@ -83,34 +86,17 @@ var _ = Describe("Vat toll transformer", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("marks header checked if no logs returned", func() {
|
It("marks header checked if no logs returned", func() {
|
||||||
mockConverter := &vat_toll_mocks.MockVatTollConverter{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
mockRepository := &vat_toll_mocks.MockVatTollRepository{}
|
|
||||||
headerID := int64(123)
|
|
||||||
mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}})
|
|
||||||
mockFetcher := &mocks.MockLogFetcher{}
|
|
||||||
transformer := vat_toll.VatTollTransformer{
|
|
||||||
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(headerOne.Id)
|
||||||
})
|
})
|
||||||
|
|
||||||
It("returns error if marking header checked returns err", func() {
|
It("returns error if marking header checked returns err", func() {
|
||||||
mockConverter := &vat_toll_mocks.MockVatTollConverter{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
mockRepository := &vat_toll_mocks.MockVatTollRepository{}
|
repository.SetMarkHeaderCheckedErr(fakes.FakeError)
|
||||||
mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}})
|
|
||||||
mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError)
|
|
||||||
mockFetcher := &mocks.MockLogFetcher{}
|
|
||||||
transformer := vat_toll.VatTollTransformer{
|
|
||||||
Converter: mockConverter,
|
|
||||||
Fetcher: mockFetcher,
|
|
||||||
Repository: mockRepository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
@ -119,16 +105,8 @@ var _ = Describe("Vat toll transformer", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("converts matching logs", func() {
|
It("converts matching logs", func() {
|
||||||
converter := &vat_toll_mocks.MockVatTollConverter{}
|
|
||||||
fetcher := &mocks.MockLogFetcher{}
|
|
||||||
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTollLog})
|
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTollLog})
|
||||||
repository := &vat_toll_mocks.MockVatTollRepository{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
|
||||||
transformer := vat_toll.VatTollTransformer{
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Converter: converter,
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
@ -137,17 +115,9 @@ var _ = Describe("Vat toll transformer", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("returns error if converter returns error", func() {
|
It("returns error if converter returns error", func() {
|
||||||
converter := &vat_toll_mocks.MockVatTollConverter{}
|
|
||||||
converter.SetConverterError(fakes.FakeError)
|
converter.SetConverterError(fakes.FakeError)
|
||||||
fetcher := &mocks.MockLogFetcher{}
|
|
||||||
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTollLog})
|
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTollLog})
|
||||||
repository := &vat_toll_mocks.MockVatTollRepository{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
|
||||||
transformer := vat_toll.VatTollTransformer{
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Converter: converter,
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
@ -156,37 +126,20 @@ var _ = Describe("Vat toll transformer", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("persists vat toll model", func() {
|
It("persists vat toll model", func() {
|
||||||
converter := &vat_toll_mocks.MockVatTollConverter{}
|
|
||||||
fetcher := &mocks.MockLogFetcher{}
|
|
||||||
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTollLog})
|
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTollLog})
|
||||||
repository := &vat_toll_mocks.MockVatTollRepository{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
fakeHeader := core.Header{BlockNumber: 1, Id: 2}
|
|
||||||
repository.SetMissingHeaders([]core.Header{fakeHeader})
|
|
||||||
transformer := vat_toll.VatTollTransformer{
|
|
||||||
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(headerOne.Id))
|
||||||
Expect(repository.PassedModels).To(Equal([]vat_toll.VatTollModel{test_data.VatTollModel}))
|
Expect(repository.PassedModels).To(Equal([]interface{}{test_data.VatTollModel}))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("returns error if repository returns error for create", func() {
|
It("returns error if repository returns error for create", func() {
|
||||||
converter := &vat_toll_mocks.MockVatTollConverter{}
|
|
||||||
fetcher := &mocks.MockLogFetcher{}
|
|
||||||
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTollLog})
|
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTollLog})
|
||||||
repository := &vat_toll_mocks.MockVatTollRepository{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}})
|
|
||||||
repository.SetCreateError(fakes.FakeError)
|
repository.SetCreateError(fakes.FakeError)
|
||||||
transformer := vat_toll.VatTollTransformer{
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Converter: converter,
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user