From 9000c6e9f9e50595ae8e90a9447655ad2018f737 Mon Sep 17 00:00:00 2001 From: Edvard Date: Tue, 11 Dec 2018 15:02:32 +0100 Subject: [PATCH] Add test for watcher log delegation --- libraries/shared/watcher_test.go | 41 ++++++++++++++++++++++---- pkg/transformers/shared/log_chunker.go | 2 ++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/libraries/shared/watcher_test.go b/libraries/shared/watcher_test.go index 0b32fcfe..d9f5976c 100644 --- a/libraries/shared/watcher_test.go +++ b/libraries/shared/watcher_test.go @@ -22,6 +22,7 @@ type MockTransformer struct { executeError error passedLogs []types.Log passedHeader core.Header + transformerName string } func (mh *MockTransformer) Execute(logs []types.Log, header core.Header) error { @@ -35,7 +36,11 @@ func (mh *MockTransformer) Execute(logs []types.Log, header core.Header) error { } func (mh *MockTransformer) Name() string { - return "MockTransformer" + return mh.transformerName +} + +func (mh *MockTransformer) SetTransformerName(name string) { + mh.transformerName = name } func fakeTransformerInitializer(db *postgres.DB) shared2.Transformer { @@ -49,11 +54,11 @@ var _ = Describe("Watcher", func() { repository := &mocks.MockWatcherRepository{} configA := shared2.TransformerConfig{ ContractAddresses: []string{"0xA"}, - Topic: "0xA", + Topic: "0xA", } configB := shared2.TransformerConfig{ ContractAddresses: []string{"0xB"}, - Topic: "0xB", + Topic: "0xB", } configs := []shared2.TransformerConfig{configA, configB} watcher := shared.NewWatcher(db, &fetcher, repository, configs) @@ -93,7 +98,7 @@ var _ = Describe("Watcher", func() { fakeTransformer *MockTransformer headerRepository repositories.HeaderRepository mockFetcher mocks.MockLogFetcher - repository mocks.MockWatcherRepository + repository mocks.MockWatcherRepository ) BeforeEach(func() { @@ -131,7 +136,33 @@ var _ = Describe("Watcher", func() { }) It("passes only relevant logs to each transformer", func() { - // TODO Test log delegation + transformerA := &MockTransformer{} + transformerA.SetTransformerName("transformerA") + transformerB := &MockTransformer{} + transformerB.SetTransformerName("transformerB") + + configA := shared2.TransformerConfig{TransformerName: "transformerA", + ContractAddresses: []string{"0x000000000000000000000000000000000000000A"}, + Topic: "0xA"} + configB := shared2.TransformerConfig{TransformerName: "transformerB", + ContractAddresses: []string{"0x000000000000000000000000000000000000000b"}, + Topic: "0xB"} + 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}) + + repository.SetMissingHeaders([]core.Header{fakes.FakeHeader}) + watcher = shared.NewWatcher(db, &mockFetcher, &repository, configs) + 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})) }) Describe("uses the repository correctly:", func() { diff --git a/pkg/transformers/shared/log_chunker.go b/pkg/transformers/shared/log_chunker.go index aede460a..a7ebcc17 100644 --- a/pkg/transformers/shared/log_chunker.go +++ b/pkg/transformers/shared/log_chunker.go @@ -48,6 +48,8 @@ func (chunker LogChunker) ChunkLogs(logs []types.Log) map[string][]types.Log { for _, log := range logs { // Topic0 is not unique to each transformer, also need to consider the contract address relevantTransformers := chunker.AddressToNames[log.Address.String()] + + // TODO What should happen if log can't be assigned? for _, transformer := range relevantTransformers { if chunker.NameToTopic0[transformer] == log.Topics[0] { chunks[transformer] = append(chunks[transformer], log)