diff --git a/libraries/shared/watcher_test.go b/libraries/shared/watcher_test.go index cf11f1d4..7b91ece9 100644 --- a/libraries/shared/watcher_test.go +++ b/libraries/shared/watcher_test.go @@ -11,7 +11,6 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" - "github.com/vulcanize/vulcanizedb/pkg/transformers" shared2 "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" "github.com/vulcanize/vulcanizedb/test_config" @@ -47,48 +46,51 @@ func fakeTransformerInitializer(db *postgres.DB) shared2.Transformer { return &MockTransformer{} } +var fakeTransformerConfig = []shared2.TransformerConfig{{ + TransformerName: "FakeTransformer", + ContractAddresses: []string{"FakeAddress"}, + Topic: "FakeTopic", +}} + var _ = Describe("Watcher", func() { It("initialises correctly", func() { db := test_config.NewTestDB(core.Node{ID: "testNode"}) - fetcher := mocks.MockLogFetcher{} + fetcher := &mocks.MockLogFetcher{} repository := &mocks.MockWatcherRepository{} - configA := shared2.TransformerConfig{ - ContractAddresses: []string{"0xA"}, - Topic: "0xA", - } - configB := shared2.TransformerConfig{ - ContractAddresses: []string{"0xB"}, - Topic: "0xB", - } - configs := []shared2.TransformerConfig{configA, configB} - watcher := shared.NewWatcher(db, &fetcher, repository, configs) + chunker := shared2.NewLogChunker() + + watcher := shared.NewWatcher(db, fetcher, repository, chunker) Expect(watcher.DB).To(Equal(db)) - Expect(watcher.Fetcher).NotTo(BeNil()) - Expect(watcher.Chunker).NotTo(BeNil()) + Expect(watcher.Fetcher).To(Equal(fetcher)) + Expect(watcher.Chunker).To(Equal(chunker)) Expect(watcher.Repository).To(Equal(repository)) - Expect(watcher.Topics).To(And( - ContainElement(common.HexToHash("0xA")), ContainElement(common.HexToHash("0xB")))) - Expect(watcher.Addresses).To(And( - ContainElement(common.HexToAddress("0xA")), ContainElement(common.HexToAddress("0xB")))) }) It("adds transformers", func() { - watcher := shared.Watcher{} + chunker := shared2.NewLogChunker() + watcher := shared.NewWatcher(nil, nil, nil, chunker) - watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}) + watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}, fakeTransformerConfig) Expect(len(watcher.Transformers)).To(Equal(1)) Expect(watcher.Transformers).To(ConsistOf(&MockTransformer{})) + Expect(watcher.Topics).To(Equal([]common.Hash{common.HexToHash("FakeTopic")})) + Expect(watcher.Addresses).To(Equal([]common.Address{common.HexToAddress("FakeAddress")})) }) It("adds transformers from multiple sources", func() { - watcher := shared.Watcher{} + chunker := shared2.NewLogChunker() + watcher := shared.NewWatcher(nil, nil, nil, chunker) - watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}) - watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}) + watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}, fakeTransformerConfig) + watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}, fakeTransformerConfig) Expect(len(watcher.Transformers)).To(Equal(2)) + 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")})) }) Describe("with missing headers", func() { @@ -99,6 +101,7 @@ var _ = Describe("Watcher", func() { headerRepository repositories.HeaderRepository mockFetcher mocks.MockLogFetcher repository mocks.MockWatcherRepository + chunker *shared2.LogChunker ) BeforeEach(func() { @@ -108,9 +111,10 @@ var _ = Describe("Watcher", func() { headerRepository = repositories.NewHeaderRepository(db) _, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) Expect(err).NotTo(HaveOccurred()) + chunker = shared2.NewLogChunker() repository = mocks.MockWatcherRepository{} - watcher = shared.NewWatcher(db, &mockFetcher, &repository, transformers.TransformerConfigs()) + watcher = shared.NewWatcher(db, &mockFetcher, &repository, chunker) }) It("executes each transformer", func() { @@ -155,8 +159,10 @@ var _ = Describe("Watcher", func() { Topics: []common.Hash{common.HexToHash("0xB")}} mockFetcher.SetFetchedLogs([]types.Log{logA, logB}) + chunker.AddConfigs(configs) + repository.SetMissingHeaders([]core.Header{fakes.FakeHeader}) - watcher = shared.NewWatcher(db, &mockFetcher, &repository, configs) + watcher = shared.NewWatcher(db, &mockFetcher, &repository, chunker) watcher.Transformers = []shared2.Transformer{transformerA, transformerB} err := watcher.Execute() diff --git a/pkg/transformers/integration_tests/cat_file.go b/pkg/transformers/integration_tests/cat_file.go index 059e37af..06679e83 100644 --- a/pkg/transformers/integration_tests/cat_file.go +++ b/pkg/transformers/integration_tests/cat_file.go @@ -40,7 +40,7 @@ var _ = Describe("Cat File transformer", func() { rpcClient client.RpcClient err error ethClient *ethclient.Client - fetcher shared.Fetcher + fetcher *shared.Fetcher ) BeforeEach(func() { diff --git a/pkg/transformers/integration_tests/deal.go b/pkg/transformers/integration_tests/deal.go index 990b0d76..b010cced 100644 --- a/pkg/transformers/integration_tests/deal.go +++ b/pkg/transformers/integration_tests/deal.go @@ -34,7 +34,7 @@ var _ = Describe("Deal transformer", func() { blockChain core.BlockChain config shared.TransformerConfig initializer factories.LogNoteTransformer - fetcher shared.Fetcher + fetcher *shared.Fetcher addresses []common.Address topics []common.Hash ) diff --git a/pkg/transformers/integration_tests/dent.go b/pkg/transformers/integration_tests/dent.go index 196ca82b..d1c8050c 100644 --- a/pkg/transformers/integration_tests/dent.go +++ b/pkg/transformers/integration_tests/dent.go @@ -18,7 +18,7 @@ var _ = Describe("Dent transformer", func() { var ( db *postgres.DB blockChain core.BlockChain - fetcher shared.Fetcher + fetcher *shared.Fetcher transformer shared.Transformer config shared.TransformerConfig addresses []common.Address diff --git a/pkg/transformers/integration_tests/frob.go b/pkg/transformers/integration_tests/frob.go index b80e47c9..50cde0a0 100644 --- a/pkg/transformers/integration_tests/frob.go +++ b/pkg/transformers/integration_tests/frob.go @@ -35,7 +35,7 @@ var _ = Describe("Frob Transformer", func() { var ( db *postgres.DB blockChain core.BlockChain - fetcher shared.Fetcher + fetcher *shared.Fetcher config shared.TransformerConfig initializer factories.Transformer ) diff --git a/pkg/transformers/integration_tests/price_feeds.go b/pkg/transformers/integration_tests/price_feeds.go index 3cf8cc47..fac1f71d 100644 --- a/pkg/transformers/integration_tests/price_feeds.go +++ b/pkg/transformers/integration_tests/price_feeds.go @@ -33,7 +33,7 @@ var _ = Describe("Price feeds transformer", func() { db *postgres.DB blockChain core.BlockChain config shared.TransformerConfig - fetcher shared.Fetcher + fetcher *shared.Fetcher initializer factories.LogNoteTransformer topics []common.Hash ) diff --git a/pkg/transformers/integration_tests/tend.go b/pkg/transformers/integration_tests/tend.go index 726f2279..ae6938ba 100644 --- a/pkg/transformers/integration_tests/tend.go +++ b/pkg/transformers/integration_tests/tend.go @@ -32,7 +32,7 @@ var _ = Describe("Tend LogNoteTransformer", func() { db *postgres.DB blockChain core.BlockChain config shared.TransformerConfig - fetcher shared.Fetcher + fetcher *shared.Fetcher initializer factories.LogNoteTransformer addresses []common.Address topics []common.Hash diff --git a/pkg/transformers/shared/log_chunker_test.go b/pkg/transformers/shared/log_chunker_test.go index e657d1b4..7dfd3d69 100644 --- a/pkg/transformers/shared/log_chunker_test.go +++ b/pkg/transformers/shared/log_chunker_test.go @@ -25,7 +25,7 @@ import ( var _ = Describe("Log chunker", func() { var ( configs []shared.TransformerConfig - chunker shared.LogChunker + chunker *shared.LogChunker ) BeforeEach(func() { @@ -47,7 +47,8 @@ var _ = Describe("Log chunker", func() { } configs = []shared.TransformerConfig{configA, configB, configC} - chunker = shared.NewLogChunker(configs) + chunker = shared.NewLogChunker() + chunker.AddConfigs(configs) }) Describe("initialisation", func() { @@ -66,6 +67,20 @@ var _ = Describe("Log chunker", func() { }) }) + Describe("AddConfigs", func() { + It("can add more configs later", func() { + configD := shared.TransformerConfig{ + TransformerName: "TransformerD", + ContractAddresses: []string{"0x000000000000000000000000000000000000000D"}, + Topic: "0xD", + } + chunker.AddConfigs([]shared.TransformerConfig{configD}) + + Expect(chunker.AddressToNames).To(ContainElement([]string{"TransformerD"})) + Expect(chunker.NameToTopic0).To(ContainElement(common.HexToHash("0xD"))) + }) + }) + Describe("ChunkLogs", func() { It("only associates logs with relevant topic0 and address to transformers", func() { logs := []types.Log{log1, log2, log3, log4, log5}