Update event transformer to take header ID

- rather than header
- enables executing transformers without full header lookup
This commit is contained in:
Rob Mulholand 2019-07-19 11:58:28 -05:00
parent 3693ed905f
commit 66a4e20b20
5 changed files with 25 additions and 28 deletions

View File

@ -18,10 +18,9 @@ package event
import (
"github.com/ethereum/go-ethereum/core/types"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
)
@ -36,14 +35,14 @@ func (transformer Transformer) NewTransformer(db *postgres.DB) transformer.Event
return transformer
}
func (transformer Transformer) Execute(logs []types.Log, header core.Header) error {
func (transformer Transformer) Execute(logs []types.Log, headerID int64) error {
transformerName := transformer.Config.TransformerName
config := transformer.Config
if len(logs) < 1 {
err := transformer.Repository.MarkHeaderChecked(header.Id)
err := transformer.Repository.MarkHeaderChecked(headerID)
if err != nil {
log.Printf("Error marking header as checked in %v: %v", transformerName, err)
logrus.Printf("Error marking header as checked in %v: %v", transformerName, err)
return err
}
return nil
@ -51,19 +50,19 @@ func (transformer Transformer) Execute(logs []types.Log, header core.Header) err
entities, err := transformer.Converter.ToEntities(config.ContractAbi, logs)
if err != nil {
log.Printf("Error converting logs to entities in %v: %v", transformerName, err)
logrus.Printf("Error converting logs to entities in %v: %v", transformerName, err)
return err
}
models, err := transformer.Converter.ToModels(entities)
if err != nil {
log.Printf("Error converting entities to models in %v: %v", transformerName, err)
logrus.Printf("Error converting entities to models in %v: %v", transformerName, err)
return err
}
err = transformer.Repository.Create(header.Id, models)
err = transformer.Repository.Create(headerID, models)
if err != nil {
log.Printf("Error persisting %v record: %v", transformerName, err)
logrus.Printf("Error persisting %v record: %v", transformerName, err)
return err
}

View File

@ -59,14 +59,14 @@ var _ = Describe("Transformer", func() {
})
It("marks header checked if no logs returned", func() {
err := t.Execute([]types.Log{}, headerOne)
err := t.Execute([]types.Log{}, headerOne.Id)
Expect(err).NotTo(HaveOccurred())
repository.AssertMarkHeaderCheckedCalledWith(headerOne.Id)
})
It("doesn't attempt to convert or persist an empty collection when there are no logs", func() {
err := t.Execute([]types.Log{}, headerOne)
err := t.Execute([]types.Log{}, headerOne.Id)
Expect(err).NotTo(HaveOccurred())
Expect(converter.ToEntitiesCalledCounter).To(Equal(0))
@ -75,7 +75,7 @@ var _ = Describe("Transformer", func() {
})
It("does not call repository.MarkCheckedHeader when there are logs", func() {
err := t.Execute(logs, headerOne)
err := t.Execute(logs, headerOne.Id)
Expect(err).NotTo(HaveOccurred())
repository.AssertMarkHeaderCheckedNotCalled()
@ -84,14 +84,14 @@ var _ = Describe("Transformer", func() {
It("returns error if marking header checked returns err", func() {
repository.SetMarkHeaderCheckedError(fakes.FakeError)
err := t.Execute([]types.Log{}, headerOne)
err := t.Execute([]types.Log{}, headerOne.Id)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
It("converts an eth log to an entity", func() {
err := t.Execute(logs, headerOne)
err := t.Execute(logs, headerOne.Id)
Expect(err).NotTo(HaveOccurred())
Expect(converter.ContractAbi).To(Equal(config.ContractAbi))
@ -101,7 +101,7 @@ var _ = Describe("Transformer", func() {
It("returns an error if converter fails", func() {
converter.ToEntitiesError = fakes.FakeError
err := t.Execute(logs, headerOne)
err := t.Execute(logs, headerOne.Id)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
@ -110,7 +110,7 @@ var _ = Describe("Transformer", func() {
It("converts an entity to a model", func() {
converter.EntitiesToReturn = []interface{}{test_data.GenericEntity{}}
err := t.Execute(logs, headerOne)
err := t.Execute(logs, headerOne.Id)
Expect(err).NotTo(HaveOccurred())
Expect(converter.EntitiesToConvert[0]).To(Equal(test_data.GenericEntity{}))
@ -120,7 +120,7 @@ var _ = Describe("Transformer", func() {
converter.EntitiesToReturn = []interface{}{test_data.GenericEntity{}}
converter.ToModelsError = fakes.FakeError
err := t.Execute(logs, headerOne)
err := t.Execute(logs, headerOne.Id)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
@ -129,7 +129,7 @@ var _ = Describe("Transformer", func() {
It("persists the record", func() {
converter.ModelsToReturn = []interface{}{test_data.GenericModel{}}
err := t.Execute(logs, headerOne)
err := t.Execute(logs, headerOne.Id)
Expect(err).NotTo(HaveOccurred())
Expect(repository.PassedHeaderID).To(Equal(headerOne.Id))
@ -138,7 +138,7 @@ var _ = Describe("Transformer", func() {
It("returns error if persisting the record fails", func() {
repository.SetCreateError(fakes.FakeError)
err := t.Execute(logs, headerOne)
err := t.Execute(logs, headerOne.Id)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))

View File

@ -20,7 +20,6 @@ import (
"github.com/ethereum/go-ethereum/core/types"
shared_t "github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
)
@ -28,17 +27,17 @@ type MockTransformer struct {
ExecuteWasCalled bool
ExecuteError error
PassedLogs []types.Log
PassedHeader core.Header
PassedHeaderID int64
config shared_t.EventTransformerConfig
}
func (mh *MockTransformer) Execute(logs []types.Log, header core.Header) error {
func (mh *MockTransformer) Execute(logs []types.Log, headerID int64) error {
if mh.ExecuteError != nil {
return mh.ExecuteError
}
mh.ExecuteWasCalled = true
mh.PassedLogs = logs
mh.PassedHeader = header
mh.PassedHeaderID = headerID
return nil
}

View File

@ -20,12 +20,11 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
)
type EventTransformer interface {
Execute(logs []types.Log, header core.Header) error
Execute(logs []types.Log, headerID int64) error
GetConfig() EventTransformerConfig
}

View File

@ -122,7 +122,7 @@ func (watcher *EventWatcher) Execute(recheckHeaders constants.TransformerExecuti
return transactionsSyncErr
}
transformErr := watcher.transformLogs(logs, header)
transformErr := watcher.transformLogs(logs, header.Id)
if transformErr != nil {
logrus.Error("Could not transform logs: ", transformErr)
return transformErr
@ -131,7 +131,7 @@ func (watcher *EventWatcher) Execute(recheckHeaders constants.TransformerExecuti
return err
}
func (watcher *EventWatcher) transformLogs(logs []types.Log, header core.Header) error {
func (watcher *EventWatcher) transformLogs(logs []types.Log, headerID int64) error {
chunkedLogs := watcher.Chunker.ChunkLogs(logs)
// Can't quit early and mark as checked if there are no logs. If we are running continuousLogSync,
@ -139,7 +139,7 @@ func (watcher *EventWatcher) transformLogs(logs []types.Log, header core.Header)
for _, t := range watcher.Transformers {
transformerName := t.GetConfig().TransformerName
logChunk := chunkedLogs[transformerName]
err := t.Execute(logChunk, header)
err := t.Execute(logChunk, headerID)
if err != nil {
logrus.Errorf("%v transformer failed to execute in watcher: %v", transformerName, err)
return err