2018-02-13 16:31:57 +00:00
|
|
|
package shared_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-12-11 12:47:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-12-07 17:10:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-02-13 16:31:57 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-12-07 17:10:36 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
2018-08-09 21:55:02 +00:00
|
|
|
shared2 "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
2018-12-10 14:40:45 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks"
|
2018-12-07 17:10:36 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/test_config"
|
2018-02-13 16:31:57 +00:00
|
|
|
)
|
|
|
|
|
2018-03-16 19:34:18 +00:00
|
|
|
type MockTransformer struct {
|
2018-02-13 16:31:57 +00:00
|
|
|
executeWasCalled bool
|
|
|
|
executeError error
|
2018-12-07 17:10:36 +00:00
|
|
|
passedLogs []types.Log
|
|
|
|
passedHeader core.Header
|
2018-12-11 14:02:32 +00:00
|
|
|
transformerName string
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 17:10:36 +00:00
|
|
|
func (mh *MockTransformer) Execute(logs []types.Log, header core.Header) error {
|
2018-02-13 16:31:57 +00:00
|
|
|
if mh.executeError != nil {
|
|
|
|
return mh.executeError
|
|
|
|
}
|
|
|
|
mh.executeWasCalled = true
|
2018-12-07 17:10:36 +00:00
|
|
|
mh.passedLogs = logs
|
|
|
|
mh.passedHeader = header
|
2018-02-13 16:31:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:10:36 +00:00
|
|
|
func (mh *MockTransformer) Name() string {
|
2018-12-11 14:02:32 +00:00
|
|
|
return mh.transformerName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *MockTransformer) SetTransformerName(name string) {
|
|
|
|
mh.transformerName = name
|
2018-12-07 17:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fakeTransformerInitializer(db *postgres.DB) shared2.Transformer {
|
2018-03-16 19:34:18 +00:00
|
|
|
return &MockTransformer{}
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 14:41:44 +00:00
|
|
|
var fakeTransformerConfig = []shared2.TransformerConfig{{
|
|
|
|
TransformerName: "FakeTransformer",
|
|
|
|
ContractAddresses: []string{"FakeAddress"},
|
|
|
|
Topic: "FakeTopic",
|
|
|
|
}}
|
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
var _ = Describe("Watcher", func() {
|
2018-12-10 20:12:19 +00:00
|
|
|
It("initialises correctly", func() {
|
2018-12-11 12:47:04 +00:00
|
|
|
db := test_config.NewTestDB(core.Node{ID: "testNode"})
|
2018-12-12 14:41:44 +00:00
|
|
|
fetcher := &mocks.MockLogFetcher{}
|
2018-12-11 12:47:04 +00:00
|
|
|
repository := &mocks.MockWatcherRepository{}
|
2018-12-12 14:41:44 +00:00
|
|
|
chunker := shared2.NewLogChunker()
|
|
|
|
|
|
|
|
watcher := shared.NewWatcher(db, fetcher, repository, chunker)
|
2018-12-11 12:47:04 +00:00
|
|
|
|
|
|
|
Expect(watcher.DB).To(Equal(db))
|
2018-12-12 14:41:44 +00:00
|
|
|
Expect(watcher.Fetcher).To(Equal(fetcher))
|
|
|
|
Expect(watcher.Chunker).To(Equal(chunker))
|
2018-12-11 12:47:04 +00:00
|
|
|
Expect(watcher.Repository).To(Equal(repository))
|
2018-12-10 20:12:19 +00:00
|
|
|
})
|
2018-12-07 17:10:36 +00:00
|
|
|
|
2018-12-10 14:40:45 +00:00
|
|
|
It("adds transformers", func() {
|
2018-12-12 14:41:44 +00:00
|
|
|
chunker := shared2.NewLogChunker()
|
|
|
|
watcher := shared.NewWatcher(nil, nil, nil, chunker)
|
2018-02-13 16:31:57 +00:00
|
|
|
|
2018-12-12 14:41:44 +00:00
|
|
|
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}, fakeTransformerConfig)
|
2018-02-13 16:31:57 +00:00
|
|
|
|
2018-03-16 19:34:18 +00:00
|
|
|
Expect(len(watcher.Transformers)).To(Equal(1))
|
|
|
|
Expect(watcher.Transformers).To(ConsistOf(&MockTransformer{}))
|
2018-12-12 14:41:44 +00:00
|
|
|
Expect(watcher.Topics).To(Equal([]common.Hash{common.HexToHash("FakeTopic")}))
|
|
|
|
Expect(watcher.Addresses).To(Equal([]common.Address{common.HexToAddress("FakeAddress")}))
|
2018-02-13 16:31:57 +00:00
|
|
|
})
|
|
|
|
|
2018-12-10 14:40:45 +00:00
|
|
|
It("adds transformers from multiple sources", func() {
|
2018-12-12 14:41:44 +00:00
|
|
|
chunker := shared2.NewLogChunker()
|
|
|
|
watcher := shared.NewWatcher(nil, nil, nil, chunker)
|
2018-02-13 16:31:57 +00:00
|
|
|
|
2018-12-12 14:41:44 +00:00
|
|
|
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}, fakeTransformerConfig)
|
|
|
|
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}, fakeTransformerConfig)
|
2018-02-13 16:31:57 +00:00
|
|
|
|
2018-03-16 19:34:18 +00:00
|
|
|
Expect(len(watcher.Transformers)).To(Equal(2))
|
2018-12-12 14:41:44 +00:00
|
|
|
Expect(watcher.Topics).To(Equal([]common.Hash{common.HexToHash("FakeTopic"),
|
|
|
|
common.HexToHash("FakeTopic")}))
|
|
|
|
Expect(watcher.Addresses).To(Equal([]common.Address{common.HexToAddress("FakeAddress"),
|
|
|
|
common.HexToAddress("FakeAddress")}))
|
2018-02-13 16:31:57 +00:00
|
|
|
})
|
|
|
|
|
2018-12-07 17:10:36 +00:00
|
|
|
Describe("with missing headers", func() {
|
|
|
|
var (
|
|
|
|
db *postgres.DB
|
|
|
|
watcher shared.Watcher
|
|
|
|
fakeTransformer *MockTransformer
|
|
|
|
headerRepository repositories.HeaderRepository
|
2018-12-11 12:47:04 +00:00
|
|
|
mockFetcher mocks.MockLogFetcher
|
2018-12-11 14:02:32 +00:00
|
|
|
repository mocks.MockWatcherRepository
|
2018-12-12 14:41:44 +00:00
|
|
|
chunker *shared2.LogChunker
|
2018-12-07 17:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
db = test_config.NewTestDB(test_config.NewTestNode())
|
|
|
|
test_config.CleanTestDB(db)
|
2018-12-11 12:47:04 +00:00
|
|
|
mockFetcher = mocks.MockLogFetcher{}
|
2018-12-07 17:10:36 +00:00
|
|
|
headerRepository = repositories.NewHeaderRepository(db)
|
|
|
|
_, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-12-12 14:41:44 +00:00
|
|
|
chunker = shared2.NewLogChunker()
|
2018-12-11 12:47:04 +00:00
|
|
|
|
|
|
|
repository = mocks.MockWatcherRepository{}
|
2018-12-12 14:41:44 +00:00
|
|
|
watcher = shared.NewWatcher(db, &mockFetcher, &repository, chunker)
|
2018-12-07 17:10:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("executes each transformer", func() {
|
|
|
|
fakeTransformer = &MockTransformer{}
|
|
|
|
watcher.Transformers = []shared2.Transformer{fakeTransformer}
|
2018-12-11 12:47:04 +00:00
|
|
|
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
|
2018-12-07 17:10:36 +00:00
|
|
|
|
|
|
|
err := watcher.Execute()
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(fakeTransformer.executeWasCalled).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error if transformer returns an error", func() {
|
|
|
|
fakeTransformer = &MockTransformer{executeError: errors.New("Something bad happened")}
|
|
|
|
watcher.Transformers = []shared2.Transformer{fakeTransformer}
|
2018-12-11 12:47:04 +00:00
|
|
|
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
|
2018-12-07 17:10:36 +00:00
|
|
|
|
|
|
|
err := watcher.Execute()
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(fakeTransformer.executeWasCalled).To(BeFalse())
|
|
|
|
})
|
2018-12-10 20:12:19 +00:00
|
|
|
|
2018-12-11 12:47:04 +00:00
|
|
|
It("passes only relevant logs to each transformer", func() {
|
2018-12-11 14:02:32 +00:00
|
|
|
transformerA := &MockTransformer{}
|
|
|
|
transformerA.SetTransformerName("transformerA")
|
|
|
|
transformerB := &MockTransformer{}
|
|
|
|
transformerB.SetTransformerName("transformerB")
|
|
|
|
|
|
|
|
configA := shared2.TransformerConfig{TransformerName: "transformerA",
|
|
|
|
ContractAddresses: []string{"0x000000000000000000000000000000000000000A"},
|
2018-12-11 14:19:27 +00:00
|
|
|
Topic: "0xA"}
|
2018-12-11 14:02:32 +00:00
|
|
|
configB := shared2.TransformerConfig{TransformerName: "transformerB",
|
|
|
|
ContractAddresses: []string{"0x000000000000000000000000000000000000000b"},
|
2018-12-11 14:19:27 +00:00
|
|
|
Topic: "0xB"}
|
2018-12-11 14:02:32 +00:00
|
|
|
configs := []shared2.TransformerConfig{configA, configB}
|
|
|
|
|
|
|
|
logA := types.Log{Address: common.HexToAddress("0xA"),
|
|
|
|
Topics: []common.Hash{common.HexToHash("0xA")}}
|
|
|
|
logB := types.Log{Address: common.HexToAddress("0xB"),
|
|
|
|
Topics: []common.Hash{common.HexToHash("0xB")}}
|
|
|
|
mockFetcher.SetFetchedLogs([]types.Log{logA, logB})
|
|
|
|
|
2018-12-12 14:41:44 +00:00
|
|
|
chunker.AddConfigs(configs)
|
|
|
|
|
2018-12-11 14:02:32 +00:00
|
|
|
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
|
2018-12-12 14:41:44 +00:00
|
|
|
watcher = shared.NewWatcher(db, &mockFetcher, &repository, chunker)
|
2018-12-11 14:02:32 +00:00
|
|
|
watcher.Transformers = []shared2.Transformer{transformerA, transformerB}
|
|
|
|
|
|
|
|
err := watcher.Execute()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(transformerA.passedLogs).To(Equal([]types.Log{logA}))
|
|
|
|
Expect(transformerB.passedLogs).To(Equal([]types.Log{logB}))
|
2018-12-10 20:12:19 +00:00
|
|
|
})
|
|
|
|
|
2018-12-11 12:47:04 +00:00
|
|
|
Describe("uses the repository correctly:", func() {
|
|
|
|
|
|
|
|
It("calls MissingHeaders", func() {
|
|
|
|
err := watcher.Execute()
|
|
|
|
Expect(err).To(Not(HaveOccurred()))
|
|
|
|
Expect(repository.MissingHeadersCalled).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("propagates MissingHeaders errors", func() {
|
|
|
|
missingHeadersError := errors.New("MissingHeadersError")
|
|
|
|
repository.MissingHeadersError = missingHeadersError
|
|
|
|
|
|
|
|
err := watcher.Execute()
|
|
|
|
Expect(err).To(MatchError(missingHeadersError))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("calls CreateNotCheckedSQL", func() {
|
|
|
|
err := watcher.Execute()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(repository.CreateNotCheckedSQLCalled).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("calls GetCheckedColumnNames", func() {
|
|
|
|
err := watcher.Execute()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(repository.GetCheckedColumnNamesCalled).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("propagates GetCheckedColumnNames errors", func() {
|
|
|
|
getCheckedColumnNamesError := errors.New("GetCheckedColumnNamesError")
|
|
|
|
repository.GetCheckedColumnNamesError = getCheckedColumnNamesError
|
|
|
|
|
|
|
|
err := watcher.Execute()
|
|
|
|
Expect(err).To(MatchError(getCheckedColumnNamesError))
|
|
|
|
})
|
2018-12-10 20:12:19 +00:00
|
|
|
})
|
|
|
|
|
2018-12-11 12:47:04 +00:00
|
|
|
Describe("uses the LogFetcher correctly:", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
|
|
|
|
})
|
2018-12-10 20:12:19 +00:00
|
|
|
|
2018-12-11 12:47:04 +00:00
|
|
|
It("fetches logs", func() {
|
|
|
|
err := watcher.Execute()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(mockFetcher.FetchLogsCalled).To(BeTrue())
|
|
|
|
})
|
2018-12-10 20:12:19 +00:00
|
|
|
|
2018-12-11 12:47:04 +00:00
|
|
|
It("propagates log fetcher errors", func() {
|
|
|
|
fetcherError := errors.New("FetcherError")
|
|
|
|
mockFetcher.SetFetcherError(fetcherError)
|
|
|
|
|
|
|
|
err := watcher.Execute()
|
|
|
|
Expect(err).To(MatchError(fetcherError))
|
|
|
|
})
|
2018-12-10 20:12:19 +00:00
|
|
|
})
|
2018-02-13 16:31:57 +00:00
|
|
|
})
|
|
|
|
})
|