2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
package watcher_test
|
2018-02-13 16:31:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2019-01-07 14:20:05 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
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"
|
2019-01-23 06:37:26 +00:00
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/constants"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/mocks"
|
2019-01-25 08:59:23 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/watcher"
|
2018-02-13 16:31:57 +00:00
|
|
|
"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"
|
|
|
|
"github.com/vulcanize/vulcanizedb/test_config"
|
2018-02-13 16:31:57 +00:00
|
|
|
)
|
|
|
|
|
2019-01-25 08:59:23 +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"})
|
2019-01-07 14:20:05 +00:00
|
|
|
bc := fakes.NewMockBlockChain()
|
2018-12-12 14:41:44 +00:00
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
w := watcher.NewEventWatcher(db, bc)
|
2018-12-11 12:47:04 +00:00
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
Expect(w.DB).To(Equal(db))
|
|
|
|
Expect(w.Fetcher).NotTo(BeNil())
|
|
|
|
Expect(w.Chunker).NotTo(BeNil())
|
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() {
|
2019-01-25 08:59:23 +00:00
|
|
|
w := watcher.NewEventWatcher(nil, nil)
|
2018-12-13 12:03:51 +00:00
|
|
|
fakeTransformer := &mocks.MockTransformer{}
|
|
|
|
fakeTransformer.SetTransformerConfig(mocks.FakeTransformerConfig)
|
2019-03-14 16:59:39 +00:00
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{fakeTransformer.FakeTransformerInitializer})
|
2018-02-13 16:31:57 +00:00
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
Expect(len(w.Transformers)).To(Equal(1))
|
|
|
|
Expect(w.Transformers).To(ConsistOf(fakeTransformer))
|
|
|
|
Expect(w.Topics).To(Equal([]common.Hash{common.HexToHash("FakeTopic")}))
|
|
|
|
Expect(w.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() {
|
2019-01-25 08:59:23 +00:00
|
|
|
w := watcher.NewEventWatcher(nil, nil)
|
2018-12-13 12:03:51 +00:00
|
|
|
fakeTransformer1 := &mocks.MockTransformer{}
|
|
|
|
fakeTransformer1.SetTransformerConfig(mocks.FakeTransformerConfig)
|
2018-12-13 11:39:57 +00:00
|
|
|
|
2018-12-13 12:03:51 +00:00
|
|
|
fakeTransformer2 := &mocks.MockTransformer{}
|
|
|
|
fakeTransformer2.SetTransformerConfig(mocks.FakeTransformerConfig)
|
2018-02-13 16:31:57 +00:00
|
|
|
|
2019-03-14 16:59:39 +00:00
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{fakeTransformer1.FakeTransformerInitializer})
|
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{fakeTransformer2.FakeTransformerInitializer})
|
2018-02-13 16:31:57 +00:00
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
Expect(len(w.Transformers)).To(Equal(2))
|
|
|
|
Expect(w.Topics).To(Equal([]common.Hash{common.HexToHash("FakeTopic"),
|
2018-12-12 14:41:44 +00:00
|
|
|
common.HexToHash("FakeTopic")}))
|
2019-01-25 08:59:23 +00:00
|
|
|
Expect(w.Addresses).To(Equal([]common.Address{common.HexToAddress("FakeAddress"),
|
2018-12-12 14:41:44 +00:00
|
|
|
common.HexToAddress("FakeAddress")}))
|
2018-02-13 16:31:57 +00:00
|
|
|
})
|
|
|
|
|
2019-01-15 10:09:13 +00:00
|
|
|
It("calculates earliest starting block number", func() {
|
|
|
|
fakeTransformer1 := &mocks.MockTransformer{}
|
2019-03-14 16:59:39 +00:00
|
|
|
fakeTransformer1.SetTransformerConfig(transformer.EventTransformerConfig{StartingBlockNumber: 5})
|
2019-01-15 10:09:13 +00:00
|
|
|
|
|
|
|
fakeTransformer2 := &mocks.MockTransformer{}
|
2019-03-14 16:59:39 +00:00
|
|
|
fakeTransformer2.SetTransformerConfig(transformer.EventTransformerConfig{StartingBlockNumber: 3})
|
2019-01-15 10:09:13 +00:00
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
w := watcher.NewEventWatcher(nil, nil)
|
2019-03-14 16:59:39 +00:00
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{
|
2019-01-15 10:09:13 +00:00
|
|
|
fakeTransformer1.FakeTransformerInitializer,
|
|
|
|
fakeTransformer2.FakeTransformerInitializer,
|
|
|
|
})
|
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
Expect(*w.StartingBlock).To(Equal(int64(3)))
|
2019-01-15 10:09:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error when run without transformers", func() {
|
2019-01-25 08:59:23 +00:00
|
|
|
w := watcher.NewEventWatcher(nil, nil)
|
|
|
|
err := w.Execute(constants.HeaderMissing)
|
2019-01-15 10:09:13 +00:00
|
|
|
Expect(err).To(MatchError("No transformers added to watcher"))
|
|
|
|
})
|
|
|
|
|
2018-12-07 17:10:36 +00:00
|
|
|
Describe("with missing headers", func() {
|
|
|
|
var (
|
|
|
|
db *postgres.DB
|
2019-01-25 08:59:23 +00:00
|
|
|
w watcher.EventWatcher
|
2019-01-07 14:20:05 +00:00
|
|
|
mockBlockChain fakes.MockBlockChain
|
2018-12-07 17:10:36 +00:00
|
|
|
headerRepository repositories.HeaderRepository
|
2018-12-11 14:02:32 +00:00
|
|
|
repository mocks.MockWatcherRepository
|
2018-12-07 17:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
db = test_config.NewTestDB(test_config.NewTestNode())
|
|
|
|
test_config.CleanTestDB(db)
|
2019-01-07 14:20:05 +00:00
|
|
|
mockBlockChain = fakes.MockBlockChain{}
|
2018-12-07 17:10:36 +00:00
|
|
|
headerRepository = repositories.NewHeaderRepository(db)
|
|
|
|
_, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-12-11 12:47:04 +00:00
|
|
|
|
|
|
|
repository = mocks.MockWatcherRepository{}
|
2019-01-25 08:59:23 +00:00
|
|
|
w = watcher.NewEventWatcher(db, &mockBlockChain)
|
2018-12-07 17:10:36 +00:00
|
|
|
})
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
It("syncs transactions for fetched logs", func() {
|
|
|
|
fakeTransformer := &mocks.MockTransformer{}
|
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{fakeTransformer.FakeTransformerInitializer})
|
|
|
|
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
|
|
|
|
mockTransactionSyncer := &fakes.MockTransactionSyncer{}
|
|
|
|
w.Syncer = mockTransactionSyncer
|
|
|
|
|
|
|
|
err := w.Execute(constants.HeaderMissing)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(mockTransactionSyncer.SyncTransactionsCalled).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if syncing transactions fails", func() {
|
|
|
|
fakeTransformer := &mocks.MockTransformer{}
|
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{fakeTransformer.FakeTransformerInitializer})
|
|
|
|
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
|
|
|
|
mockTransactionSyncer := &fakes.MockTransactionSyncer{}
|
|
|
|
mockTransactionSyncer.SyncTransactionsError = fakes.FakeError
|
|
|
|
w.Syncer = mockTransactionSyncer
|
|
|
|
|
|
|
|
err := w.Execute(constants.HeaderMissing)
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
|
2018-12-07 17:10:36 +00:00
|
|
|
It("executes each transformer", func() {
|
2019-01-15 10:09:13 +00:00
|
|
|
fakeTransformer := &mocks.MockTransformer{}
|
2019-03-14 16:59:39 +00:00
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{fakeTransformer.FakeTransformerInitializer})
|
2018-12-11 12:47:04 +00:00
|
|
|
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
|
2018-12-07 17:10:36 +00:00
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
err := w.Execute(constants.HeaderMissing)
|
2018-12-07 17:10:36 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-12-13 12:03:51 +00:00
|
|
|
Expect(fakeTransformer.ExecuteWasCalled).To(BeTrue())
|
2018-12-07 17:10:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error if transformer returns an error", func() {
|
2019-01-15 10:09:13 +00:00
|
|
|
fakeTransformer := &mocks.MockTransformer{ExecuteError: errors.New("Something bad happened")}
|
2019-03-14 16:59:39 +00:00
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{fakeTransformer.FakeTransformerInitializer})
|
2018-12-11 12:47:04 +00:00
|
|
|
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
|
2018-12-07 17:10:36 +00:00
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
err := w.Execute(constants.HeaderMissing)
|
2018-12-07 17:10:36 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
2018-12-13 12:03:51 +00:00
|
|
|
Expect(fakeTransformer.ExecuteWasCalled).To(BeFalse())
|
2018-12-07 17:10:36 +00:00
|
|
|
})
|
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-13 12:03:51 +00:00
|
|
|
transformerA := &mocks.MockTransformer{}
|
|
|
|
transformerB := &mocks.MockTransformer{}
|
2018-12-11 14:02:32 +00:00
|
|
|
|
2019-03-14 16:59:39 +00:00
|
|
|
configA := transformer.EventTransformerConfig{TransformerName: "transformerA",
|
2018-12-11 14:02:32 +00:00
|
|
|
ContractAddresses: []string{"0x000000000000000000000000000000000000000A"},
|
2018-12-11 14:19:27 +00:00
|
|
|
Topic: "0xA"}
|
2019-03-14 16:59:39 +00:00
|
|
|
configB := transformer.EventTransformerConfig{TransformerName: "transformerB",
|
2018-12-11 14:02:32 +00:00
|
|
|
ContractAddresses: []string{"0x000000000000000000000000000000000000000b"},
|
2018-12-11 14:19:27 +00:00
|
|
|
Topic: "0xB"}
|
2018-12-13 11:39:57 +00:00
|
|
|
|
|
|
|
transformerA.SetTransformerConfig(configA)
|
|
|
|
transformerB.SetTransformerConfig(configB)
|
2018-12-11 14:02:32 +00:00
|
|
|
|
|
|
|
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")}}
|
2019-01-07 14:20:05 +00:00
|
|
|
mockBlockChain.SetGetEthLogsWithCustomQueryReturnLogs([]types.Log{logA, logB})
|
2018-12-11 14:02:32 +00:00
|
|
|
|
|
|
|
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
|
2019-01-25 08:59:23 +00:00
|
|
|
w = watcher.NewEventWatcher(db, &mockBlockChain)
|
2019-03-14 16:59:39 +00:00
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{
|
2018-12-13 11:39:57 +00:00
|
|
|
transformerA.FakeTransformerInitializer, transformerB.FakeTransformerInitializer})
|
2018-12-11 14:02:32 +00:00
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
err := w.Execute(constants.HeaderMissing)
|
2018-12-11 14:02:32 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-12-13 12:03:51 +00:00
|
|
|
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 LogFetcher correctly:", func() {
|
2019-01-15 10:09:13 +00:00
|
|
|
var fakeTransformer mocks.MockTransformer
|
2018-12-11 12:47:04 +00:00
|
|
|
BeforeEach(func() {
|
|
|
|
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
|
2019-01-15 10:09:13 +00:00
|
|
|
fakeTransformer = mocks.MockTransformer{}
|
2018-12-11 12:47:04 +00:00
|
|
|
})
|
2018-12-10 20:12:19 +00:00
|
|
|
|
2019-01-15 10:09:13 +00:00
|
|
|
It("fetches logs for added transformers", func() {
|
|
|
|
addresses := []string{"0xA", "0xB"}
|
|
|
|
topic := "0x1"
|
2019-03-14 16:59:39 +00:00
|
|
|
fakeTransformer.SetTransformerConfig(transformer.EventTransformerConfig{
|
2019-01-15 10:09:13 +00:00
|
|
|
Topic: topic, ContractAddresses: addresses})
|
2019-03-14 16:59:39 +00:00
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{fakeTransformer.FakeTransformerInitializer})
|
2019-01-15 10:09:13 +00:00
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
err := w.Execute(constants.HeaderMissing)
|
2018-12-11 12:47:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-01-07 14:20:05 +00:00
|
|
|
|
|
|
|
fakeHash := common.HexToHash(fakes.FakeHeader.Hash)
|
|
|
|
mockBlockChain.AssertGetEthLogsWithCustomQueryCalledWith(ethereum.FilterQuery{
|
|
|
|
BlockHash: &fakeHash,
|
2019-01-25 08:59:23 +00:00
|
|
|
Addresses: transformer.HexStringsToAddresses(addresses),
|
2019-01-15 10:09:13 +00:00
|
|
|
Topics: [][]common.Hash{{common.HexToHash(topic)}},
|
2019-01-07 14:20:05 +00:00
|
|
|
})
|
2018-12-11 12:47:04 +00:00
|
|
|
})
|
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")
|
2019-01-07 14:20:05 +00:00
|
|
|
mockBlockChain.SetGetEthLogsWithCustomQueryErr(fetcherError)
|
2018-12-11 12:47:04 +00:00
|
|
|
|
2019-03-14 16:59:39 +00:00
|
|
|
w.AddTransformers([]transformer.EventTransformerInitializer{fakeTransformer.FakeTransformerInitializer})
|
2019-01-25 08:59:23 +00:00
|
|
|
err := w.Execute(constants.HeaderMissing)
|
2018-12-11 12:47:04 +00:00
|
|
|
Expect(err).To(MatchError(fetcherError))
|
|
|
|
})
|
2018-12-10 20:12:19 +00:00
|
|
|
})
|
2018-02-13 16:31:57 +00:00
|
|
|
})
|
|
|
|
})
|