forked from cerc-io/ipld-eth-server
Update vat heal to use transformer factory (#88)
* Update vat heal to use transformer factory
This commit is contained in:
parent
5de949f4c5
commit
d522d33531
@ -19,6 +19,8 @@ import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "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_heal"
|
||||
"github.com/vulcanize/vulcanizedb/test_config"
|
||||
)
|
||||
@ -41,8 +43,13 @@ var _ = Describe("VatHeal Transformer", func() {
|
||||
err = persistHeader(db, blockNumber)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
initializer := vat_heal.VatHealTransformerInitializer{Config: config}
|
||||
transformer := initializer.NewVatHealTransformer(db, blockchain)
|
||||
transformer := factories.Transformer{
|
||||
Config: vat_heal.VatHealConfig,
|
||||
Converter: &vat_heal.VatHealConverter{},
|
||||
Repository: &vat_heal.VatHealRepository{},
|
||||
Fetcher: &shared.Fetcher{},
|
||||
}.NewTransformer(db, blockchain)
|
||||
|
||||
err = transformer.Execute()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
|
@ -49,10 +49,6 @@ func (repository *MockRepository) SetMissingHeaders(headers []core.Header) {
|
||||
repository.missingHeaders = headers
|
||||
}
|
||||
|
||||
func (repository *MockRepository) AssertMarkHeaderCheckedCalledWith(i int64) {
|
||||
Expect(repository.MarkHeaderCheckedPassedHeaderIDs).To(ContainElement(i))
|
||||
}
|
||||
|
||||
func (repository *MockRepository) SetMarkHeaderCheckedError(e error) {
|
||||
repository.markHeaderCheckedError = e
|
||||
}
|
||||
@ -60,3 +56,11 @@ func (repository *MockRepository) SetMarkHeaderCheckedError(e error) {
|
||||
func (repository *MockRepository) SetCreateError(e error) {
|
||||
repository.createError = e
|
||||
}
|
||||
|
||||
func (repository *MockRepository) AssertMarkHeaderCheckedCalledWith(i int64) {
|
||||
Expect(repository.MarkHeaderCheckedPassedHeaderIDs).To(ContainElement(i))
|
||||
}
|
||||
|
||||
func (repository *MockRepository) AssertMarkHeaderCheckedNotCalled() {
|
||||
Expect(len(repository.MarkHeaderCheckedPassedHeaderIDs)).To(Equal(0))
|
||||
}
|
||||
|
@ -1,35 +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 vat_heal
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal"
|
||||
)
|
||||
|
||||
type MockVatHealConverter struct {
|
||||
converterErr error
|
||||
PassedLogs []types.Log
|
||||
}
|
||||
|
||||
func (converter *MockVatHealConverter) ToModels(ethLogs []types.Log) ([]vat_heal.VatHealModel, error) {
|
||||
converter.PassedLogs = ethLogs
|
||||
return []vat_heal.VatHealModel{test_data.VatHealModel}, converter.converterErr
|
||||
}
|
||||
|
||||
func (converter *MockVatHealConverter) SetConverterError(e error) {
|
||||
converter.converterErr = e
|
||||
}
|
@ -1,65 +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 vat_heal
|
||||
|
||||
import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal"
|
||||
)
|
||||
|
||||
type MockVatHealRepository struct {
|
||||
createErr error
|
||||
markHeaderCheckedErr error
|
||||
MarkHeaderCheckedPassedHeaderID int64
|
||||
missingHeaders []core.Header
|
||||
missingHeadersErr error
|
||||
PassedStartingBlockNumber int64
|
||||
PassedEndingBlockNumber int64
|
||||
PassedHeaderID int64
|
||||
PassedModels []vat_heal.VatHealModel
|
||||
}
|
||||
|
||||
func (repository *MockVatHealRepository) MarkCheckedHeader(headerId int64) error {
|
||||
repository.MarkHeaderCheckedPassedHeaderID = headerId
|
||||
return repository.markHeaderCheckedErr
|
||||
}
|
||||
|
||||
func (repository *MockVatHealRepository) Create(headerID int64, models []vat_heal.VatHealModel) error {
|
||||
repository.PassedHeaderID = headerID
|
||||
repository.PassedModels = models
|
||||
return repository.createErr
|
||||
}
|
||||
|
||||
func (repository *MockVatHealRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
|
||||
repository.PassedStartingBlockNumber = startingBlockNumber
|
||||
repository.PassedEndingBlockNumber = endingBlockNumber
|
||||
return repository.missingHeaders, repository.missingHeadersErr
|
||||
}
|
||||
|
||||
func (repository *MockVatHealRepository) SetMarkHeaderCheckedErr(e error) {
|
||||
repository.markHeaderCheckedErr = e
|
||||
}
|
||||
|
||||
func (repository *MockVatHealRepository) SetMissingHeadersErr(e error) {
|
||||
repository.missingHeadersErr = e
|
||||
}
|
||||
|
||||
func (repository *MockVatHealRepository) SetMissingHeaders(headers []core.Header) {
|
||||
repository.missingHeaders = headers
|
||||
}
|
||||
|
||||
func (repository *MockVatHealRepository) SetCreateError(e error) {
|
||||
repository.createErr = e
|
||||
}
|
@ -151,13 +151,18 @@ var (
|
||||
Repository: &vat_grab.VatGrabRepository{},
|
||||
Fetcher: &shared.Fetcher{},
|
||||
}.NewTransformer
|
||||
VatHealTransformerInitializer = vat_heal.VatHealTransformerInitializer{Config: vat_heal.VatHealConfig}.NewVatHealTransformer
|
||||
VatFoldTransformerInitializer = factories.Transformer{
|
||||
Config: vat_fold.VatFoldConfig,
|
||||
Converter: &vat_fold.VatFoldConverter{},
|
||||
Repository: &vat_fold.VatFoldRepository{},
|
||||
Fetcher: &shared.Fetcher{},
|
||||
}.NewTransformer
|
||||
VatHealTransformerInitializer = factories.Transformer{
|
||||
Config: vat_heal.VatHealConfig,
|
||||
Converter: &vat_heal.VatHealConverter{},
|
||||
Repository: &vat_heal.VatHealRepository{},
|
||||
Fetcher: &shared.Fetcher{},
|
||||
}.NewTransformer
|
||||
VatMoveTransformerInitializer = factories.Transformer{
|
||||
Config: vat_move.VatMoveConfig,
|
||||
Converter: &vat_move.VatMoveConverter{},
|
||||
|
@ -16,10 +16,11 @@ package vat_heal
|
||||
|
||||
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||
|
||||
var VatHealConfig = shared.TransformerConfig{
|
||||
var VatHealConfig = shared.SingleTransformerConfig{
|
||||
TransformerName: shared.VatHealLabel,
|
||||
ContractAddresses: []string{shared.VatContractAddress},
|
||||
ContractAbi: shared.VatABI,
|
||||
Topics: []string{shared.VatHealSignature},
|
||||
Topic: shared.VatHealSignature,
|
||||
StartingBlockNumber: 0,
|
||||
EndingBlockNumber: 10000000,
|
||||
}
|
||||
|
@ -22,14 +22,10 @@ import (
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
)
|
||||
|
||||
type Converter interface {
|
||||
ToModels(ethLogs []types.Log) ([]VatHealModel, error)
|
||||
}
|
||||
|
||||
type VatHealConverter struct{}
|
||||
|
||||
func (VatHealConverter) ToModels(ethLogs []types.Log) ([]VatHealModel, error) {
|
||||
var models []VatHealModel
|
||||
func (VatHealConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
|
||||
var models []interface{}
|
||||
for _, ethLog := range ethLogs {
|
||||
err := verifyLog(ethLog)
|
||||
if err != nil {
|
||||
|
@ -15,34 +15,35 @@
|
||||
package vat_heal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
Create(headerId int64, models []VatHealModel) error
|
||||
MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error)
|
||||
MarkCheckedHeader(headerId int64) error
|
||||
}
|
||||
|
||||
type VatHealRepository struct {
|
||||
DB *postgres.DB
|
||||
db *postgres.DB
|
||||
}
|
||||
|
||||
func NewVatHealRepository(db *postgres.DB) VatHealRepository {
|
||||
return VatHealRepository{DB: db}
|
||||
func (repository *VatHealRepository) SetDB(db *postgres.DB) {
|
||||
repository.db = db
|
||||
}
|
||||
|
||||
func (repository VatHealRepository) Create(headerId int64, models []VatHealModel) error {
|
||||
tx, err := repository.DB.Begin()
|
||||
func (repository VatHealRepository) Create(headerId int64, models []interface{}) error {
|
||||
tx, err := repository.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
vatHeal, ok := model.(VatHealModel)
|
||||
if !ok {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("model of type %T, not %T", model, VatHealModel{})
|
||||
}
|
||||
_, err := tx.Exec(`INSERT INTO maker.vat_heal (header_id, urn, v, rad, log_idx, tx_idx, raw_log)
|
||||
VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`,
|
||||
headerId, model.Urn, model.V, model.Rad, model.LogIndex, model.TransactionIndex, model.Raw)
|
||||
headerId, vatHeal.Urn, vatHeal.V, vatHeal.Rad, vatHeal.LogIndex, vatHeal.TransactionIndex, vatHeal.Raw)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
@ -62,20 +63,20 @@ func (repository VatHealRepository) Create(headerId int64, models []VatHealModel
|
||||
|
||||
func (repository VatHealRepository) MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error) {
|
||||
var headers []core.Header
|
||||
err := repository.DB.Select(&headers,
|
||||
err := repository.db.Select(&headers,
|
||||
`SELECT headers.id, block_number from headers
|
||||
LEFT JOIN checked_headers on headers.id = header_id
|
||||
WHERE (header_id ISNULL OR vat_heal_checked IS FALSE)
|
||||
AND headers.block_number >= $1
|
||||
AND headers.block_number <= $2
|
||||
AND headers.eth_node_fingerprint = $3`,
|
||||
startingBlock, endingBlock, repository.DB.Node.ID)
|
||||
startingBlock, endingBlock, repository.db.Node.ID)
|
||||
|
||||
return headers, err
|
||||
}
|
||||
|
||||
func (repository VatHealRepository) MarkCheckedHeader(headerId int64) error {
|
||||
_, err := repository.DB.Exec(`INSERT INTO public.checked_headers (header_id, vat_heal_checked)
|
||||
func (repository VatHealRepository) MarkHeaderChecked(headerId int64) error {
|
||||
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_heal_checked)
|
||||
VALUES($1, $2)
|
||||
ON CONFLICT (header_id) DO
|
||||
UPDATE SET vat_heal_checked = $2`, headerId, true)
|
||||
|
@ -15,6 +15,8 @@
|
||||
package vat_heal_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
@ -38,7 +40,8 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
BeforeEach(func() {
|
||||
db = test_config.NewTestDB(test_config.NewTestNode())
|
||||
test_config.CleanTestDB(db)
|
||||
repository = vat_heal.VatHealRepository{DB: db}
|
||||
repository = vat_heal.VatHealRepository{}
|
||||
repository.SetDB(db)
|
||||
headerRepository = repositories.NewHeaderRepository(db)
|
||||
})
|
||||
|
||||
@ -63,7 +66,7 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
It("persists vat heal records", func() {
|
||||
anotherVatHeal := test_data.VatHealModel
|
||||
anotherVatHeal.LogIndex = test_data.VatHealModel.LogIndex + 1
|
||||
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel, anotherVatHeal})
|
||||
err = repository.Create(headerId, []interface{}{test_data.VatHealModel, anotherVatHeal})
|
||||
|
||||
var dbResult []VatHealDBResult
|
||||
err = db.Select(&dbResult, `SELECT * from maker.vat_heal where header_id = $1`, headerId)
|
||||
@ -80,17 +83,17 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
})
|
||||
|
||||
It("returns an error if the insertion fails", func() {
|
||||
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel})
|
||||
err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel})
|
||||
err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
|
||||
})
|
||||
|
||||
It("marks the header as checked for vat heal logs", func() {
|
||||
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel})
|
||||
err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
var headerChecked bool
|
||||
@ -103,7 +106,7 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerId)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel})
|
||||
err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
@ -113,7 +116,7 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
})
|
||||
|
||||
It("removes vat heal if corresponding header is deleted", func() {
|
||||
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel})
|
||||
err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerId)
|
||||
@ -125,11 +128,17 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
Expect(count).To(Equal(0))
|
||||
})
|
||||
|
||||
It("returns an error if the wrong model types are passed in", func() {
|
||||
err = repository.Create(headerId, []interface{}{test_data.WrongModel{}})
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("model of type test_data.WrongModel, not vat_heal.VatHealModel"))
|
||||
})
|
||||
|
||||
It("wraps create in a transaction", func() {
|
||||
err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel, test_data.VatHealModel})
|
||||
err = repository.Create(headerId, []interface{}{test_data.VatHealModel, test_data.VatHealModel})
|
||||
Expect(err).To(HaveOccurred())
|
||||
var count int
|
||||
err = repository.DB.QueryRowx(`SELECT count(*) FROM maker.vat_heal`).Scan(&count)
|
||||
err = db.QueryRowx(`SELECT count(*) FROM maker.vat_heal`).Scan(&count)
|
||||
Expect(count).To(Equal(0))
|
||||
})
|
||||
})
|
||||
@ -141,7 +150,7 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
startingBlock = GinkgoRandomSeed()
|
||||
startingBlock = rand.Int63()
|
||||
vatHealBlock = startingBlock + 1
|
||||
endingBlock = startingBlock + 2
|
||||
outsideRangeBlock = startingBlock + 3
|
||||
@ -156,7 +165,7 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
})
|
||||
|
||||
It("returns headers that haven't been checked", func() {
|
||||
err = repository.MarkCheckedHeader(headerIds[0])
|
||||
err = repository.MarkHeaderChecked(headerIds[0])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
headers, err := repository.MissingHeaders(startingBlock, endingBlock)
|
||||
@ -168,8 +177,8 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
})
|
||||
|
||||
It("returns header ids when checked_headers.vat_heal is false", func() {
|
||||
err = repository.MarkCheckedHeader(headerIds[0])
|
||||
_, err = repository.DB.Exec(`INSERT INTO checked_headers (header_id) VALUES ($1)`, headerIds[1])
|
||||
err = repository.MarkHeaderChecked(headerIds[0])
|
||||
_, err = db.Exec(`INSERT INTO checked_headers (header_id) VALUES ($1)`, headerIds[1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
headers, err := repository.MissingHeaders(startingBlock, endingBlock)
|
||||
@ -183,14 +192,15 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
It("only returns header ids for the current node", func() {
|
||||
db2 := test_config.NewTestDB(core.Node{ID: "second node"})
|
||||
headerRepository2 := repositories.NewHeaderRepository(db2)
|
||||
repository2 := vat_heal.NewVatHealRepository(db2)
|
||||
repository2 := vat_heal.VatHealRepository{}
|
||||
repository2.SetDB(db2)
|
||||
|
||||
for _, n := range blockNumbers {
|
||||
_, err = headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
err = repository.MarkCheckedHeader(headerIds[0])
|
||||
err = repository.MarkHeaderChecked(headerIds[0])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
nodeOneMissingHeaders, err := repository.MissingHeaders(startingBlock, endingBlock)
|
||||
@ -212,7 +222,7 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
})
|
||||
|
||||
It("creates a new checked_header record", func() {
|
||||
err := repository.MarkCheckedHeader(headerId)
|
||||
err := repository.MarkHeaderChecked(headerId)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
var checkedHeaderResult = CheckedHeaderResult{}
|
||||
@ -222,7 +232,7 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
})
|
||||
|
||||
It("updates an existing checked header", func() {
|
||||
_, err := repository.DB.Exec(`INSERT INTO checked_headers (header_id) VALUES($1)`, headerId)
|
||||
_, err := db.Exec(`INSERT INTO checked_headers (header_id) VALUES($1)`, headerId)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
var checkedHeaderResult CheckedHeaderResult
|
||||
@ -230,7 +240,7 @@ var _ = Describe("VatHeal Repository", func() {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(checkedHeaderResult.VatHealChecked).To(BeFalse())
|
||||
|
||||
err = repository.MarkCheckedHeader(headerId)
|
||||
err = repository.MarkHeaderChecked(headerId)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = db.Get(&checkedHeaderResult, `SELECT vat_heal_checked FROM checked_headers WHERE header_id = $1`, headerId)
|
||||
|
@ -1,80 +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 vat_heal
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
type VatHealTransformer struct {
|
||||
Config shared.TransformerConfig
|
||||
Converter Converter
|
||||
Fetcher shared.LogFetcher
|
||||
Repository Repository
|
||||
}
|
||||
|
||||
type VatHealTransformerInitializer struct {
|
||||
Config shared.TransformerConfig
|
||||
}
|
||||
|
||||
func (i VatHealTransformerInitializer) NewVatHealTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
|
||||
fetcher := shared.NewFetcher(blockChain)
|
||||
repository := NewVatHealRepository(db)
|
||||
transformer := VatHealTransformer{
|
||||
Fetcher: fetcher,
|
||||
Repository: repository,
|
||||
Converter: VatHealConverter{},
|
||||
Config: i.Config,
|
||||
}
|
||||
|
||||
return transformer
|
||||
}
|
||||
|
||||
func (transformer VatHealTransformer) Execute() error {
|
||||
config := transformer.Config
|
||||
topics := [][]common.Hash{{common.HexToHash(config.Topics[0])}}
|
||||
headers, err := transformer.Repository.MissingHeaders(config.StartingBlockNumber, config.EndingBlockNumber)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, header := range headers {
|
||||
logs, err := transformer.Fetcher.FetchLogs(config.ContractAddresses, topics, header.BlockNumber)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(logs) < 1 {
|
||||
err = transformer.Repository.MarkCheckedHeader(header.Id)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
models, err := transformer.Converter.ToModels(logs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = transformer.Repository.Create(header.Id, models)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -15,47 +15,60 @@
|
||||
package vat_heal_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"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/test_data"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks"
|
||||
mock_vat_heal "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/vat_heal"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal"
|
||||
)
|
||||
|
||||
var _ = Describe("VatHeal Transformer", func() {
|
||||
var mockRepository mock_vat_heal.MockVatHealRepository
|
||||
var transformer vat_heal.VatHealTransformer
|
||||
var fetcher mocks.MockLogFetcher
|
||||
var converter mock_vat_heal.MockVatHealConverter
|
||||
var (
|
||||
repository mocks.MockRepository
|
||||
transformer shared.Transformer
|
||||
fetcher mocks.MockLogFetcher
|
||||
converter mocks.MockConverter
|
||||
config = vat_heal.VatHealConfig
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
mockRepository = mock_vat_heal.MockVatHealRepository{}
|
||||
repository = mocks.MockRepository{}
|
||||
fetcher = mocks.MockLogFetcher{}
|
||||
converter = mock_vat_heal.MockVatHealConverter{}
|
||||
transformer = vat_heal.VatHealTransformer{
|
||||
Repository: &mockRepository,
|
||||
Config: vat_heal.VatHealConfig,
|
||||
converter = mocks.MockConverter{}
|
||||
transformer = factories.Transformer{
|
||||
Repository: &repository,
|
||||
Config: config,
|
||||
Fetcher: &fetcher,
|
||||
Converter: &converter,
|
||||
}
|
||||
}.NewTransformer(nil, nil)
|
||||
})
|
||||
|
||||
It("sets the database and blockchain", func() {
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(repository.SetDbCalled).To(BeTrue())
|
||||
})
|
||||
|
||||
It("gets all of the missing header ids", func() {
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(mockRepository.PassedStartingBlockNumber).To(Equal(vat_heal.VatHealConfig.StartingBlockNumber))
|
||||
Expect(mockRepository.PassedEndingBlockNumber).To(Equal(vat_heal.VatHealConfig.EndingBlockNumber))
|
||||
Expect(repository.PassedStartingBlockNumber).To(Equal(config.StartingBlockNumber))
|
||||
Expect(repository.PassedEndingBlockNumber).To(Equal(config.EndingBlockNumber))
|
||||
})
|
||||
|
||||
It("returns and error if getting the missing headers fails", func() {
|
||||
mockRepository.SetMissingHeadersErr(fakes.FakeError)
|
||||
repository.SetMissingHeadersError(fakes.FakeError)
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
@ -63,8 +76,8 @@ var _ = Describe("VatHeal Transformer", func() {
|
||||
})
|
||||
|
||||
It("fetches vat heal logs for the headers", func() {
|
||||
header := core.Header{BlockNumber: GinkgoRandomSeed()}
|
||||
mockRepository.SetMissingHeaders([]core.Header{header})
|
||||
header := core.Header{BlockNumber: rand.Int63()}
|
||||
repository.SetMissingHeaders([]core.Header{header})
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
@ -74,8 +87,8 @@ var _ = Describe("VatHeal Transformer", func() {
|
||||
})
|
||||
|
||||
It("returns and error if fetching the logs fails", func() {
|
||||
header := core.Header{BlockNumber: GinkgoRandomSeed()}
|
||||
mockRepository.SetMissingHeaders([]core.Header{header})
|
||||
header := core.Header{BlockNumber: rand.Int63()}
|
||||
repository.SetMissingHeaders([]core.Header{header})
|
||||
fetcher.SetFetcherError(fakes.FakeError)
|
||||
err := transformer.Execute()
|
||||
|
||||
@ -84,8 +97,8 @@ var _ = Describe("VatHeal Transformer", func() {
|
||||
})
|
||||
|
||||
It("converts the logs to models", func() {
|
||||
header := core.Header{BlockNumber: GinkgoRandomSeed()}
|
||||
mockRepository.SetMissingHeaders([]core.Header{header})
|
||||
header := core.Header{BlockNumber: rand.Int63()}
|
||||
repository.SetMissingHeaders([]core.Header{header})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog})
|
||||
|
||||
err := transformer.Execute()
|
||||
@ -95,8 +108,8 @@ var _ = Describe("VatHeal Transformer", func() {
|
||||
})
|
||||
|
||||
It("returns an error if converting fails", func() {
|
||||
header := core.Header{BlockNumber: GinkgoRandomSeed()}
|
||||
mockRepository.SetMissingHeaders([]core.Header{header})
|
||||
header := core.Header{BlockNumber: rand.Int63()}
|
||||
repository.SetMissingHeaders([]core.Header{header})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog})
|
||||
converter.SetConverterError(fakes.FakeError)
|
||||
|
||||
@ -107,22 +120,23 @@ var _ = Describe("VatHeal Transformer", func() {
|
||||
})
|
||||
|
||||
It("persists the vat heal models", func() {
|
||||
header := core.Header{Id: GinkgoRandomSeed()}
|
||||
mockRepository.SetMissingHeaders([]core.Header{header})
|
||||
header := core.Header{Id: rand.Int63()}
|
||||
repository.SetMissingHeaders([]core.Header{header})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog})
|
||||
converter.SetReturnModels([]interface{}{test_data.VatHealModel})
|
||||
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(mockRepository.PassedModels).To(ContainElement(test_data.VatHealModel))
|
||||
Expect(mockRepository.PassedHeaderID).To(Equal(header.Id))
|
||||
Expect(repository.PassedModels).To(ContainElement(test_data.VatHealModel))
|
||||
Expect(repository.PassedHeaderID).To(Equal(header.Id))
|
||||
})
|
||||
|
||||
It("returns an error if persisting the vat heal models fails", func() {
|
||||
header := core.Header{Id: GinkgoRandomSeed()}
|
||||
mockRepository.SetMissingHeaders([]core.Header{header})
|
||||
header := core.Header{Id: rand.Int63()}
|
||||
repository.SetMissingHeaders([]core.Header{header})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog})
|
||||
mockRepository.SetCreateError(fakes.FakeError)
|
||||
repository.SetCreateError(fakes.FakeError)
|
||||
|
||||
err := transformer.Execute()
|
||||
|
||||
@ -131,29 +145,29 @@ var _ = Describe("VatHeal Transformer", func() {
|
||||
})
|
||||
|
||||
It("marks the header as checked when there are no logs", func() {
|
||||
header := core.Header{Id: GinkgoRandomSeed()}
|
||||
mockRepository.SetMissingHeaders([]core.Header{header})
|
||||
header := core.Header{Id: rand.Int63()}
|
||||
repository.SetMissingHeaders([]core.Header{header})
|
||||
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(mockRepository.MarkHeaderCheckedPassedHeaderID).To(Equal(header.Id))
|
||||
repository.AssertMarkHeaderCheckedCalledWith(header.Id)
|
||||
})
|
||||
|
||||
It("doesn't call MarkCheckedHeader when there are logs", func() {
|
||||
header := core.Header{Id: GinkgoRandomSeed()}
|
||||
mockRepository.SetMissingHeaders([]core.Header{header})
|
||||
repository.SetMissingHeaders([]core.Header{header})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog})
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(mockRepository.MarkHeaderCheckedPassedHeaderID).To(Equal(int64(0)))
|
||||
repository.AssertMarkHeaderCheckedNotCalled()
|
||||
})
|
||||
|
||||
It("returns an error if MarkCheckedHeader fails", func() {
|
||||
header := core.Header{Id: GinkgoRandomSeed()}
|
||||
mockRepository.SetMissingHeaders([]core.Header{header})
|
||||
mockRepository.SetMissingHeadersErr(fakes.FakeError)
|
||||
header := core.Header{Id: rand.Int63()}
|
||||
repository.SetMissingHeaders([]core.Header{header})
|
||||
repository.SetMissingHeadersError(fakes.FakeError)
|
||||
|
||||
err := transformer.Execute()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user