forked from cerc-io/ipld-eth-server
Use transformer factory for Vat tune
This commit is contained in:
parent
5682ee988b
commit
78d746bb5b
@ -20,6 +20,8 @@ import (
|
|||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/factories"
|
||||||
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_tune"
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_tune"
|
||||||
"github.com/vulcanize/vulcanizedb/test_config"
|
"github.com/vulcanize/vulcanizedb/test_config"
|
||||||
)
|
)
|
||||||
@ -42,8 +44,13 @@ var _ = Describe("VatTune Transformer", func() {
|
|||||||
err = persistHeader(rpcClient, db, blockNumber)
|
err = persistHeader(rpcClient, db, blockNumber)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
initializer := vat_tune.VatTuneTransformerInitializer{Config: config}
|
initializer := factories.Transformer{
|
||||||
transformer := initializer.NewVatTuneTransformer(db, blockchain)
|
Config: config,
|
||||||
|
Fetcher: &shared.Fetcher{},
|
||||||
|
Converter: &vat_tune.VatTuneConverter{},
|
||||||
|
Repository: &vat_tune.VatTuneRepository{},
|
||||||
|
}
|
||||||
|
transformer := initializer.NewTransformer(db, blockchain)
|
||||||
err = transformer.Execute()
|
err = transformer.Execute()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ package vat_tune
|
|||||||
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_tune"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockVatTuneConverter struct {
|
type MockVatTuneConverter struct {
|
||||||
@ -11,9 +10,9 @@ type MockVatTuneConverter struct {
|
|||||||
PassedLogs []types.Log
|
PassedLogs []types.Log
|
||||||
}
|
}
|
||||||
|
|
||||||
func (converter *MockVatTuneConverter) ToModels(ethLogs []types.Log) ([]vat_tune.VatTuneModel, error) {
|
func (converter *MockVatTuneConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
|
||||||
converter.PassedLogs = ethLogs
|
converter.PassedLogs = ethLogs
|
||||||
return []vat_tune.VatTuneModel{test_data.VatTuneModel}, converter.err
|
return []interface{}{test_data.VatTuneModel}, converter.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (converter *MockVatTuneConverter) SetConverterError(e error) {
|
func (converter *MockVatTuneConverter) 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_tune"
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockVatTuneRepository struct {
|
type MockVatTuneRepository struct {
|
||||||
@ -16,10 +16,11 @@ type MockVatTuneRepository struct {
|
|||||||
PassedStartingBlockNumber int64
|
PassedStartingBlockNumber int64
|
||||||
PassedEndingBlockNumber int64
|
PassedEndingBlockNumber int64
|
||||||
PassedHeaderID int64
|
PassedHeaderID int64
|
||||||
PassedModels []vat_tune.VatTuneModel
|
PassedModels []interface{}
|
||||||
|
SetDbCalled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repository *MockVatTuneRepository) Create(headerID int64, models []vat_tune.VatTuneModel) error {
|
func (repository *MockVatTuneRepository) 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 *MockVatTuneRepository) MissingHeaders(startingBlockNumber, end
|
|||||||
return repository.missingHeaders, repository.missingHeadersErr
|
return repository.missingHeaders, repository.missingHeadersErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (repository *MockVatTuneRepository) SetDB(db *postgres.DB) {
|
||||||
|
repository.SetDbCalled = true
|
||||||
|
}
|
||||||
|
|
||||||
func (repository *MockVatTuneRepository) SetMissingHeadersErr(e error) {
|
func (repository *MockVatTuneRepository) SetMissingHeadersErr(e error) {
|
||||||
repository.missingHeadersErr = e
|
repository.missingHeadersErr = e
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,12 @@ var (
|
|||||||
}.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 = vat_toll.VatTollTransformerInitializer{Config: vat_toll.VatTollConfig}.NewVatTollTransformer
|
||||||
VatTuneTransformerInitializer = vat_tune.VatTuneTransformerInitializer{Config: vat_tune.VatTuneConfig}.NewVatTuneTransformer
|
VatTuneTransformerInitializer = factories.Transformer{
|
||||||
|
Config: vat_tune.VatTuneConfig,
|
||||||
|
Converter: &vat_tune.VatTuneConverter{},
|
||||||
|
Repository: &vat_tune.VatTuneRepository{},
|
||||||
|
Fetcher: &shared.Fetcher{},
|
||||||
|
}.NewTransformer
|
||||||
VatFluxTransformerInitializer = vat_flux.VatFluxTransformerInitializer{Config: vat_flux.VatFluxConfig}.NewVatFluxTransformer
|
VatFluxTransformerInitializer = vat_flux.VatFluxTransformerInitializer{Config: vat_flux.VatFluxConfig}.NewVatFluxTransformer
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -2,10 +2,11 @@ package vat_tune
|
|||||||
|
|
||||||
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||||
|
|
||||||
var VatTuneConfig = shared.TransformerConfig{
|
var VatTuneConfig = shared.SingleTransformerConfig{
|
||||||
|
TransformerName: shared.VatTuneLabel,
|
||||||
ContractAddresses: []string{shared.VatContractAddress},
|
ContractAddresses: []string{shared.VatContractAddress},
|
||||||
ContractAbi: shared.VatABI,
|
ContractAbi: shared.VatABI,
|
||||||
Topics: []string{shared.VatTuneSignature},
|
Topic: shared.VatTuneSignature,
|
||||||
StartingBlockNumber: 0,
|
StartingBlockNumber: 0,
|
||||||
EndingBlockNumber: 10000000,
|
EndingBlockNumber: 10000000,
|
||||||
}
|
}
|
||||||
|
@ -12,14 +12,10 @@ import (
|
|||||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Converter interface {
|
|
||||||
ToModels(ethLogs []types.Log) ([]VatTuneModel, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type VatTuneConverter struct{}
|
type VatTuneConverter struct{}
|
||||||
|
|
||||||
func (VatTuneConverter) ToModels(ethLogs []types.Log) ([]VatTuneModel, error) {
|
func (VatTuneConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
|
||||||
var models []VatTuneModel
|
var models []interface{}
|
||||||
for _, ethLog := range ethLogs {
|
for _, ethLog := range ethLogs {
|
||||||
err := verifyLog(ethLog)
|
err := verifyLog(ethLog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -31,9 +27,9 @@ func (VatTuneConverter) ToModels(ethLogs []types.Log) ([]VatTuneModel, error) {
|
|||||||
wBytes := shared.GetDataBytesAtIndex(-3, ethLog.Data)
|
wBytes := shared.GetDataBytesAtIndex(-3, ethLog.Data)
|
||||||
w := common.BytesToAddress(wBytes)
|
w := common.BytesToAddress(wBytes)
|
||||||
dinkBytes := shared.GetDataBytesAtIndex(-2, ethLog.Data)
|
dinkBytes := shared.GetDataBytesAtIndex(-2, ethLog.Data)
|
||||||
dink := big.NewInt(0).SetBytes(dinkBytes).String()
|
dink := big.NewInt(0).SetBytes(dinkBytes)
|
||||||
dartBytes := shared.GetDataBytesAtIndex(-1, ethLog.Data)
|
dartBytes := shared.GetDataBytesAtIndex(-1, ethLog.Data)
|
||||||
dart := big.NewInt(0).SetBytes(dartBytes).String()
|
dart := big.NewInt(0).SetBytes(dartBytes)
|
||||||
|
|
||||||
raw, err := json.Marshal(ethLog)
|
raw, err := json.Marshal(ethLog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -44,8 +40,8 @@ func (VatTuneConverter) ToModels(ethLogs []types.Log) ([]VatTuneModel, error) {
|
|||||||
Urn: urn.String(),
|
Urn: urn.String(),
|
||||||
V: v.String(),
|
V: v.String(),
|
||||||
W: w.String(),
|
W: w.String(),
|
||||||
Dink: dink,
|
Dink: dink.String(),
|
||||||
Dart: dart,
|
Dart: dart.String(),
|
||||||
TransactionIndex: ethLog.TxIndex,
|
TransactionIndex: ethLog.TxIndex,
|
||||||
LogIndex: ethLog.Index,
|
LogIndex: ethLog.Index,
|
||||||
Raw: raw,
|
Raw: raw,
|
||||||
|
@ -1,36 +1,30 @@
|
|||||||
package vat_tune
|
package vat_tune
|
||||||
|
|
||||||
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 []VatTuneModel) error
|
|
||||||
MarkHeaderChecked(headerID int64) error
|
|
||||||
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type VatTuneRepository struct {
|
type VatTuneRepository struct {
|
||||||
db *postgres.DB
|
db *postgres.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVatTuneRepository(db *postgres.DB) VatTuneRepository {
|
func (repository VatTuneRepository) Create(headerID int64, models []interface{}) error {
|
||||||
return VatTuneRepository{
|
|
||||||
db: db,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repository VatTuneRepository) Create(headerID int64, models []VatTuneModel) 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 {
|
||||||
|
vatTune, ok := model.(VatTuneModel)
|
||||||
|
if !ok {
|
||||||
|
tx.Rollback()
|
||||||
|
return fmt.Errorf("model of type %T, not %T", model, VatTuneModel{})
|
||||||
|
}
|
||||||
_, err = tx.Exec(
|
_, err = tx.Exec(
|
||||||
`INSERT into maker.vat_tune (header_id, ilk, urn, v, w, dink, dart, tx_idx, log_idx, raw_log)
|
`INSERT into maker.vat_tune (header_id, ilk, urn, v, w, dink, dart, tx_idx, log_idx, raw_log)
|
||||||
VALUES($1, $2, $3, $4, $5, $6::NUMERIC, $7::NUMERIC, $8, $9, $10)`,
|
VALUES($1, $2, $3, $4, $5, $6::NUMERIC, $7::NUMERIC, $8, $9, $10)`,
|
||||||
headerID, model.Ilk, model.Urn, model.V, model.W, model.Dink, model.Dart, model.TransactionIndex, model.LogIndex, model.Raw,
|
headerID, vatTune.Ilk, vatTune.Urn, vatTune.V, vatTune.W, vatTune.Dink, vatTune.Dart, vatTune.TransactionIndex, vatTune.LogIndex, vatTune.Raw,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
@ -72,3 +66,7 @@ func (repository VatTuneRepository) MissingHeaders(startingBlockNumber, endingBl
|
|||||||
)
|
)
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (repository *VatTuneRepository) SetDB(db *postgres.DB) {
|
||||||
|
repository.db = db
|
||||||
|
}
|
||||||
|
@ -19,7 +19,7 @@ import (
|
|||||||
var _ = Describe("Vat tune repository", func() {
|
var _ = Describe("Vat tune repository", func() {
|
||||||
var (
|
var (
|
||||||
db *postgres.DB
|
db *postgres.DB
|
||||||
vatTuneRepository vat_tune.Repository
|
vatTuneRepository vat_tune.VatTuneRepository
|
||||||
err error
|
err error
|
||||||
headerRepository datastore.HeaderRepository
|
headerRepository datastore.HeaderRepository
|
||||||
)
|
)
|
||||||
@ -28,7 +28,8 @@ var _ = Describe("Vat tune 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)
|
||||||
vatTuneRepository = vat_tune.NewVatTuneRepository(db)
|
vatTuneRepository = vat_tune.VatTuneRepository{}
|
||||||
|
vatTuneRepository.SetDB(db)
|
||||||
})
|
})
|
||||||
|
|
||||||
Describe("Create", func() {
|
Describe("Create", func() {
|
||||||
@ -40,7 +41,7 @@ var _ = Describe("Vat tune repository", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("adds a vat tune event", func() {
|
It("adds a vat tune event", func() {
|
||||||
err = vatTuneRepository.Create(headerID, []vat_tune.VatTuneModel{test_data.VatTuneModel})
|
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
var dbVatTune vat_tune.VatTuneModel
|
var dbVatTune vat_tune.VatTuneModel
|
||||||
@ -58,7 +59,7 @@ var _ = Describe("Vat tune repository", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("marks header as checked for logs", func() {
|
It("marks header as checked for logs", func() {
|
||||||
err = vatTuneRepository.Create(headerID, []vat_tune.VatTuneModel{test_data.VatTuneModel})
|
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
var headerChecked bool
|
var headerChecked bool
|
||||||
@ -70,7 +71,7 @@ var _ = Describe("Vat tune 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 = vatTuneRepository.Create(headerID, []vat_tune.VatTuneModel{test_data.VatTuneModel})
|
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
var headerChecked bool
|
var headerChecked bool
|
||||||
@ -80,28 +81,28 @@ var _ = Describe("Vat tune repository", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("does not duplicate pit file vat_tune events", func() {
|
It("does not duplicate pit file vat_tune events", func() {
|
||||||
err = vatTuneRepository.Create(headerID, []vat_tune.VatTuneModel{test_data.VatTuneModel})
|
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
err = vatTuneRepository.Create(headerID, []vat_tune.VatTuneModel{test_data.VatTuneModel})
|
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
|
||||||
|
|
||||||
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 flop kick events in one transaction if they have different log indexes", func() {
|
It("allows for multiple flop kick events in one transaction if they have different log indexes", func() {
|
||||||
err = vatTuneRepository.Create(headerID, []vat_tune.VatTuneModel{test_data.VatTuneModel})
|
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
newVatTune := test_data.VatTuneModel
|
newVatTune := test_data.VatTuneModel
|
||||||
newVatTune.LogIndex = newVatTune.LogIndex + 1
|
newVatTune.LogIndex = newVatTune.LogIndex + 1
|
||||||
err := vatTuneRepository.Create(headerID, []vat_tune.VatTuneModel{newVatTune})
|
err := vatTuneRepository.Create(headerID, []interface{}{newVatTune})
|
||||||
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
It("removes pit file vat_tune if corresponding header is deleted", func() {
|
It("removes pit file vat_tune if corresponding header is deleted", func() {
|
||||||
err = vatTuneRepository.Create(headerID, []vat_tune.VatTuneModel{test_data.VatTuneModel})
|
err = vatTuneRepository.Create(headerID, []interface{}{test_data.VatTuneModel})
|
||||||
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)
|
||||||
@ -198,8 +199,8 @@ var _ = Describe("Vat tune repository", func() {
|
|||||||
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
|
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
}
|
}
|
||||||
vatTuneRepository := vat_tune.NewVatTuneRepository(db)
|
vatTuneRepositoryTwo := vat_tune.VatTuneRepository{}
|
||||||
vatTuneRepositoryTwo := vat_tune.NewVatTuneRepository(dbTwo)
|
vatTuneRepositoryTwo.SetDB(dbTwo)
|
||||||
err := vatTuneRepository.MarkHeaderChecked(headerIDs[0])
|
err := vatTuneRepository.MarkHeaderChecked(headerIDs[0])
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
@ -1,62 +0,0 @@
|
|||||||
package vat_tune
|
|
||||||
|
|
||||||
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 VatTuneTransformerInitializer struct {
|
|
||||||
Config shared.TransformerConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func (initializer VatTuneTransformerInitializer) NewVatTuneTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
|
|
||||||
converter := VatTuneConverter{}
|
|
||||||
fetcher := shared.NewFetcher(blockChain)
|
|
||||||
repository := NewVatTuneRepository(db)
|
|
||||||
return VatTuneTransformer{
|
|
||||||
Config: initializer.Config,
|
|
||||||
Converter: converter,
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type VatTuneTransformer struct {
|
|
||||||
Config shared.TransformerConfig
|
|
||||||
Converter Converter
|
|
||||||
Fetcher shared.LogFetcher
|
|
||||||
Repository Repository
|
|
||||||
}
|
|
||||||
|
|
||||||
func (transformer VatTuneTransformer) Execute() error {
|
|
||||||
missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.Printf("Fetching vat tune event logs for %d headers \n", len(missingHeaders))
|
|
||||||
for _, header := range missingHeaders {
|
|
||||||
topics := [][]common.Hash{{common.HexToHash(shared.VatTuneSignature)}}
|
|
||||||
matchingLogs, err := transformer.Fetcher.FetchLogs(VatTuneConfig.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,53 @@ 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_tune_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/vat_tune"
|
vat_tune_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/vat_tune"
|
||||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_tune"
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_tune"
|
||||||
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Vat tune transformer", func() {
|
var _ = Describe("Vat tune transformer", func() {
|
||||||
It("gets missing headers for block numbers specified in config", func() {
|
var config = vat_tune.VatTuneConfig
|
||||||
repository := &vat_tune_mocks.MockVatTuneRepository{}
|
var fetcher mocks.MockLogFetcher
|
||||||
transformer := vat_tune.VatTuneTransformer{
|
var converter vat_tune_mocks.MockVatTuneConverter
|
||||||
Config: vat_tune.VatTuneConfig,
|
var repository vat_tune_mocks.MockVatTuneRepository
|
||||||
Fetcher: &mocks.MockLogFetcher{},
|
var transformer shared.Transformer
|
||||||
Converter: &vat_tune_mocks.MockVatTuneConverter{},
|
var headerOne core.Header
|
||||||
Repository: repository,
|
var headerTwo core.Header
|
||||||
}
|
|
||||||
|
|
||||||
|
BeforeEach(func() {
|
||||||
|
fetcher = mocks.MockLogFetcher{}
|
||||||
|
converter = vat_tune_mocks.MockVatTuneConverter{}
|
||||||
|
repository = vat_tune_mocks.MockVatTuneRepository{}
|
||||||
|
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_tune.VatTuneConfig.StartingBlockNumber))
|
Expect(repository.PassedStartingBlockNumber).To(Equal(config.StartingBlockNumber))
|
||||||
Expect(repository.PassedEndingBlockNumber).To(Equal(vat_tune.VatTuneConfig.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_tune_mocks.MockVatTuneRepository{}
|
|
||||||
repository.SetMissingHeadersErr(fakes.FakeError)
|
repository.SetMissingHeadersErr(fakes.FakeError)
|
||||||
transformer := vat_tune.VatTuneTransformer{
|
|
||||||
Fetcher: &mocks.MockLogFetcher{},
|
|
||||||
Converter: &vat_tune_mocks.MockVatTuneConverter{},
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
@ -48,33 +63,19 @@ var _ = Describe("Vat tune 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_tune_mocks.MockVatTuneRepository{}
|
|
||||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}})
|
|
||||||
transformer := vat_tune.VatTuneTransformer{
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Converter: &vat_tune_mocks.MockVatTuneConverter{},
|
|
||||||
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_tune.VatTuneConfig.ContractAddresses, vat_tune.VatTuneConfig.ContractAddresses}))
|
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{config.ContractAddresses, config.ContractAddresses}))
|
||||||
Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.VatTuneSignature)}}))
|
Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.VatTuneSignature)}}))
|
||||||
})
|
})
|
||||||
|
|
||||||
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_tune_mocks.MockVatTuneRepository{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
|
||||||
transformer := vat_tune.VatTuneTransformer{
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Converter: &vat_tune_mocks.MockVatTuneConverter{},
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
@ -83,34 +84,17 @@ var _ = Describe("Vat tune transformer", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("marks header checked if no logs returned", func() {
|
It("marks header checked if no logs returned", func() {
|
||||||
mockConverter := &vat_tune_mocks.MockVatTuneConverter{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
mockRepository := &vat_tune_mocks.MockVatTuneRepository{}
|
|
||||||
headerID := int64(123)
|
|
||||||
mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}})
|
|
||||||
mockFetcher := &mocks.MockLogFetcher{}
|
|
||||||
transformer := vat_tune.VatTuneTransformer{
|
|
||||||
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_tune_mocks.MockVatTuneConverter{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
mockRepository := &vat_tune_mocks.MockVatTuneRepository{}
|
repository.SetMarkHeaderCheckedErr(fakes.FakeError)
|
||||||
mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}})
|
|
||||||
mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError)
|
|
||||||
mockFetcher := &mocks.MockLogFetcher{}
|
|
||||||
transformer := vat_tune.VatTuneTransformer{
|
|
||||||
Converter: mockConverter,
|
|
||||||
Fetcher: mockFetcher,
|
|
||||||
Repository: mockRepository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
@ -119,16 +103,8 @@ var _ = Describe("Vat tune transformer", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("converts matching logs", func() {
|
It("converts matching logs", func() {
|
||||||
converter := &vat_tune_mocks.MockVatTuneConverter{}
|
|
||||||
fetcher := &mocks.MockLogFetcher{}
|
|
||||||
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTuneLog})
|
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTuneLog})
|
||||||
repository := &vat_tune_mocks.MockVatTuneRepository{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
|
||||||
transformer := vat_tune.VatTuneTransformer{
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Converter: converter,
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
@ -137,17 +113,9 @@ var _ = Describe("Vat tune transformer", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("returns error if converter returns error", func() {
|
It("returns error if converter returns error", func() {
|
||||||
converter := &vat_tune_mocks.MockVatTuneConverter{}
|
|
||||||
converter.SetConverterError(fakes.FakeError)
|
converter.SetConverterError(fakes.FakeError)
|
||||||
fetcher := &mocks.MockLogFetcher{}
|
|
||||||
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTuneLog})
|
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTuneLog})
|
||||||
repository := &vat_tune_mocks.MockVatTuneRepository{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
|
||||||
transformer := vat_tune.VatTuneTransformer{
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Converter: converter,
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
@ -156,37 +124,20 @@ var _ = Describe("Vat tune transformer", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("persists vat tune model", func() {
|
It("persists vat tune model", func() {
|
||||||
converter := &vat_tune_mocks.MockVatTuneConverter{}
|
|
||||||
fetcher := &mocks.MockLogFetcher{}
|
|
||||||
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTuneLog})
|
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTuneLog})
|
||||||
repository := &vat_tune_mocks.MockVatTuneRepository{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
fakeHeader := core.Header{BlockNumber: 1, Id: 2}
|
|
||||||
repository.SetMissingHeaders([]core.Header{fakeHeader})
|
|
||||||
transformer := vat_tune.VatTuneTransformer{
|
|
||||||
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_tune.VatTuneModel{test_data.VatTuneModel}))
|
Expect(repository.PassedModels).To(Equal([]interface{}{test_data.VatTuneModel}))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("returns error if repository returns error for create", func() {
|
It("returns error if repository returns error for create", func() {
|
||||||
converter := &vat_tune_mocks.MockVatTuneConverter{}
|
|
||||||
fetcher := &mocks.MockLogFetcher{}
|
|
||||||
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTuneLog})
|
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatTuneLog})
|
||||||
repository := &vat_tune_mocks.MockVatTuneRepository{}
|
repository.SetMissingHeaders([]core.Header{headerOne})
|
||||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}})
|
|
||||||
repository.SetCreateError(fakes.FakeError)
|
repository.SetCreateError(fakes.FakeError)
|
||||||
transformer := vat_tune.VatTuneTransformer{
|
|
||||||
Fetcher: fetcher,
|
|
||||||
Converter: converter,
|
|
||||||
Repository: repository,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := transformer.Execute()
|
err := transformer.Execute()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user