2019-08-06 21:57:08 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package logs_test
|
|
|
|
|
|
|
|
import (
|
2019-09-30 17:37:16 +00:00
|
|
|
"math/rand"
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/constants"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/logs"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/mocks"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Log extractor", func() {
|
2019-08-26 21:43:14 +00:00
|
|
|
var (
|
|
|
|
checkedHeadersRepository *fakes.MockCheckedHeadersRepository
|
|
|
|
checkedLogsRepository *fakes.MockCheckedLogsRepository
|
|
|
|
extractor *logs.LogExtractor
|
|
|
|
)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2019-08-26 21:43:14 +00:00
|
|
|
checkedHeadersRepository = &fakes.MockCheckedHeadersRepository{}
|
|
|
|
checkedLogsRepository = &fakes.MockCheckedLogsRepository{}
|
2019-08-06 21:57:08 +00:00
|
|
|
extractor = &logs.LogExtractor{
|
2019-08-26 21:43:14 +00:00
|
|
|
CheckedHeadersRepository: checkedHeadersRepository,
|
|
|
|
CheckedLogsRepository: checkedLogsRepository,
|
2019-08-06 21:57:08 +00:00
|
|
|
Fetcher: &mocks.MockLogFetcher{},
|
|
|
|
LogRepository: &fakes.MockHeaderSyncLogRepository{},
|
|
|
|
Syncer: &fakes.MockTransactionSyncer{},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("AddTransformerConfig", func() {
|
2019-08-26 21:43:14 +00:00
|
|
|
It("updates extractor's starting block number to earliest available", func() {
|
2019-08-06 21:57:08 +00:00
|
|
|
earlierStartingBlockNumber := rand.Int63()
|
|
|
|
laterStartingBlockNumber := earlierStartingBlockNumber + 1
|
|
|
|
|
2019-08-26 21:43:14 +00:00
|
|
|
errOne := extractor.AddTransformerConfig(getTransformerConfig(laterStartingBlockNumber))
|
|
|
|
Expect(errOne).NotTo(HaveOccurred())
|
|
|
|
errTwo := extractor.AddTransformerConfig(getTransformerConfig(earlierStartingBlockNumber))
|
|
|
|
Expect(errTwo).NotTo(HaveOccurred())
|
2019-08-06 21:57:08 +00:00
|
|
|
|
|
|
|
Expect(*extractor.StartingBlock).To(Equal(earlierStartingBlockNumber))
|
|
|
|
})
|
|
|
|
|
2019-08-26 21:43:14 +00:00
|
|
|
It("adds transformer's addresses to extractor's watched addresses", func() {
|
2019-08-06 21:57:08 +00:00
|
|
|
addresses := []string{"0xA", "0xB"}
|
|
|
|
configWithAddresses := transformer.EventTransformerConfig{
|
|
|
|
ContractAddresses: addresses,
|
|
|
|
StartingBlockNumber: rand.Int63(),
|
|
|
|
}
|
|
|
|
|
2019-08-26 21:43:14 +00:00
|
|
|
err := extractor.AddTransformerConfig(configWithAddresses)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-26 21:43:14 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-08-06 21:57:08 +00:00
|
|
|
expectedAddresses := transformer.HexStringsToAddresses(addresses)
|
|
|
|
Expect(extractor.Addresses).To(Equal(expectedAddresses))
|
|
|
|
})
|
|
|
|
|
2019-08-26 21:43:14 +00:00
|
|
|
It("adds transformer's topic to extractor's watched topics", func() {
|
2019-08-06 21:57:08 +00:00
|
|
|
topic := "0x1"
|
|
|
|
configWithTopic := transformer.EventTransformerConfig{
|
|
|
|
ContractAddresses: []string{fakes.FakeAddress.Hex()},
|
|
|
|
Topic: topic,
|
|
|
|
StartingBlockNumber: rand.Int63(),
|
|
|
|
}
|
|
|
|
|
2019-08-26 21:43:14 +00:00
|
|
|
err := extractor.AddTransformerConfig(configWithTopic)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-26 21:43:14 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-08-06 21:57:08 +00:00
|
|
|
Expect(extractor.Topics).To(Equal([]common.Hash{common.HexToHash(topic)}))
|
|
|
|
})
|
2019-08-26 21:43:14 +00:00
|
|
|
|
|
|
|
It("returns error if checking whether log has been checked returns error", func() {
|
2019-09-11 02:22:14 +00:00
|
|
|
checkedLogsRepository.AlreadyWatchingLogError = fakes.FakeError
|
2019-08-26 21:43:14 +00:00
|
|
|
|
|
|
|
err := extractor.AddTransformerConfig(getTransformerConfig(rand.Int63()))
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("when log has previously been checked", func() {
|
|
|
|
It("does not mark any headers unchecked", func() {
|
2019-09-11 02:22:14 +00:00
|
|
|
checkedLogsRepository.AlreadyWatchingLogReturn = true
|
2019-08-26 21:43:14 +00:00
|
|
|
|
|
|
|
err := extractor.AddTransformerConfig(getTransformerConfig(rand.Int63()))
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(checkedHeadersRepository.MarkHeadersUncheckedCalled).To(BeFalse())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("when log has not previously been checked", func() {
|
|
|
|
BeforeEach(func() {
|
2019-09-11 02:22:14 +00:00
|
|
|
checkedLogsRepository.AlreadyWatchingLogReturn = false
|
2019-08-26 21:43:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("marks headers since transformer's starting block number as unchecked", func() {
|
|
|
|
blockNumber := rand.Int63()
|
|
|
|
|
|
|
|
err := extractor.AddTransformerConfig(getTransformerConfig(blockNumber))
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(checkedHeadersRepository.MarkHeadersUncheckedCalled).To(BeTrue())
|
|
|
|
Expect(checkedHeadersRepository.MarkHeadersUncheckedStartingBlockNumber).To(Equal(blockNumber))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if marking headers unchecked returns error", func() {
|
|
|
|
checkedHeadersRepository.MarkHeadersUncheckedReturnError = fakes.FakeError
|
|
|
|
|
|
|
|
err := extractor.AddTransformerConfig(getTransformerConfig(rand.Int63()))
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("persists that tranformer's log has been checked", func() {
|
|
|
|
config := getTransformerConfig(rand.Int63())
|
|
|
|
|
|
|
|
err := extractor.AddTransformerConfig(config)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-09-11 02:22:14 +00:00
|
|
|
Expect(checkedLogsRepository.MarkLogWatchedAddresses).To(Equal(config.ContractAddresses))
|
|
|
|
Expect(checkedLogsRepository.MarkLogWatchedTopicZero).To(Equal(config.Topic))
|
2019-08-26 21:43:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if marking logs checked returns error", func() {
|
2019-09-11 02:22:14 +00:00
|
|
|
checkedLogsRepository.MarkLogWatchedError = fakes.FakeError
|
2019-08-26 21:43:14 +00:00
|
|
|
|
|
|
|
err := extractor.AddTransformerConfig(getTransformerConfig(rand.Int63()))
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
})
|
2019-08-06 21:57:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Describe("ExtractLogs", func() {
|
2019-08-14 22:29:04 +00:00
|
|
|
It("returns error if no watched addresses configured", func() {
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(logs.ErrNoWatchedAddresses))
|
2019-08-06 21:57:08 +00:00
|
|
|
})
|
|
|
|
|
2019-09-10 19:39:45 +00:00
|
|
|
Describe("when checking unchecked headers", func() {
|
|
|
|
It("gets headers since configured starting block with check_count < 1", func() {
|
2019-08-06 21:57:08 +00:00
|
|
|
mockCheckedHeadersRepository := &fakes.MockCheckedHeadersRepository{}
|
2019-09-10 19:39:45 +00:00
|
|
|
mockCheckedHeadersRepository.UncheckedHeadersReturnHeaders = []core.Header{{}}
|
2019-08-06 21:57:08 +00:00
|
|
|
extractor.CheckedHeadersRepository = mockCheckedHeadersRepository
|
|
|
|
startingBlockNumber := rand.Int63()
|
|
|
|
extractor.AddTransformerConfig(getTransformerConfig(startingBlockNumber))
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-14 22:29:04 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-09-10 19:39:45 +00:00
|
|
|
Expect(mockCheckedHeadersRepository.UncheckedHeadersStartingBlockNumber).To(Equal(startingBlockNumber))
|
|
|
|
Expect(mockCheckedHeadersRepository.UncheckedHeadersEndingBlockNumber).To(Equal(int64(-1)))
|
|
|
|
Expect(mockCheckedHeadersRepository.UncheckedHeadersCheckCount).To(Equal(int64(1)))
|
2019-08-06 21:57:08 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("when rechecking headers", func() {
|
2019-09-10 19:39:45 +00:00
|
|
|
It("gets headers since configured starting block with check_count < RecheckHeaderCap", func() {
|
2019-08-06 21:57:08 +00:00
|
|
|
mockCheckedHeadersRepository := &fakes.MockCheckedHeadersRepository{}
|
2019-09-10 19:39:45 +00:00
|
|
|
mockCheckedHeadersRepository.UncheckedHeadersReturnHeaders = []core.Header{{}}
|
2019-08-06 21:57:08 +00:00
|
|
|
extractor.CheckedHeadersRepository = mockCheckedHeadersRepository
|
|
|
|
startingBlockNumber := rand.Int63()
|
|
|
|
extractor.AddTransformerConfig(getTransformerConfig(startingBlockNumber))
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderRecheck)
|
2019-08-14 22:29:04 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-09-10 19:39:45 +00:00
|
|
|
Expect(mockCheckedHeadersRepository.UncheckedHeadersStartingBlockNumber).To(Equal(startingBlockNumber))
|
|
|
|
Expect(mockCheckedHeadersRepository.UncheckedHeadersEndingBlockNumber).To(Equal(int64(-1)))
|
|
|
|
Expect(mockCheckedHeadersRepository.UncheckedHeadersCheckCount).To(Equal(constants.RecheckHeaderCap))
|
2019-08-06 21:57:08 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
It("returns error if getting unchecked headers fails", func() {
|
2019-08-06 21:57:08 +00:00
|
|
|
addTransformerConfig(extractor)
|
|
|
|
mockCheckedHeadersRepository := &fakes.MockCheckedHeadersRepository{}
|
2019-09-10 19:39:45 +00:00
|
|
|
mockCheckedHeadersRepository.UncheckedHeadersReturnError = fakes.FakeError
|
2019-08-06 21:57:08 +00:00
|
|
|
extractor.CheckedHeadersRepository = mockCheckedHeadersRepository
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
2019-08-06 21:57:08 +00:00
|
|
|
})
|
|
|
|
|
2019-09-10 19:39:45 +00:00
|
|
|
Describe("when no unchecked headers", func() {
|
2019-08-14 22:29:04 +00:00
|
|
|
It("does not fetch logs", func() {
|
2019-08-12 17:26:49 +00:00
|
|
|
addTransformerConfig(extractor)
|
|
|
|
mockLogFetcher := &mocks.MockLogFetcher{}
|
|
|
|
extractor.Fetcher = mockLogFetcher
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
_ = extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(mockLogFetcher.FetchCalled).To(BeFalse())
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
It("returns error that no unchecked headers were found", func() {
|
2019-08-12 17:26:49 +00:00
|
|
|
addTransformerConfig(extractor)
|
|
|
|
mockLogFetcher := &mocks.MockLogFetcher{}
|
|
|
|
extractor.Fetcher = mockLogFetcher
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
Expect(err).To(MatchError(logs.ErrNoUncheckedHeaders))
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
2019-08-06 21:57:08 +00:00
|
|
|
})
|
|
|
|
|
2019-09-10 19:39:45 +00:00
|
|
|
Describe("when there are unchecked headers", func() {
|
|
|
|
It("fetches logs for unchecked headers", func() {
|
|
|
|
addUncheckedHeader(extractor)
|
2019-08-12 17:26:49 +00:00
|
|
|
config := transformer.EventTransformerConfig{
|
|
|
|
ContractAddresses: []string{fakes.FakeAddress.Hex()},
|
|
|
|
Topic: fakes.FakeHash.Hex(),
|
|
|
|
StartingBlockNumber: rand.Int63(),
|
|
|
|
}
|
2019-09-10 19:39:45 +00:00
|
|
|
addTransformerErr := extractor.AddTransformerConfig(config)
|
|
|
|
Expect(addTransformerErr).NotTo(HaveOccurred())
|
2019-08-12 17:26:49 +00:00
|
|
|
mockLogFetcher := &mocks.MockLogFetcher{}
|
|
|
|
extractor.Fetcher = mockLogFetcher
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(mockLogFetcher.FetchCalled).To(BeTrue())
|
2019-08-12 17:26:49 +00:00
|
|
|
expectedTopics := []common.Hash{common.HexToHash(config.Topic)}
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(mockLogFetcher.Topics).To(Equal(expectedTopics))
|
2019-08-12 17:26:49 +00:00
|
|
|
expectedAddresses := transformer.HexStringsToAddresses(config.ContractAddresses)
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(mockLogFetcher.ContractAddresses).To(Equal(expectedAddresses))
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
It("returns error if fetching logs fails", func() {
|
2019-09-10 19:39:45 +00:00
|
|
|
addUncheckedHeader(extractor)
|
2019-08-12 17:26:49 +00:00
|
|
|
addTransformerConfig(extractor)
|
|
|
|
mockLogFetcher := &mocks.MockLogFetcher{}
|
|
|
|
mockLogFetcher.ReturnError = fakes.FakeError
|
|
|
|
extractor.Fetcher = mockLogFetcher
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-12 17:26:49 +00:00
|
|
|
Describe("when no fetched logs", func() {
|
2019-08-14 22:29:04 +00:00
|
|
|
It("does not sync transactions", func() {
|
2019-09-10 19:39:45 +00:00
|
|
|
addUncheckedHeader(extractor)
|
2019-08-12 17:26:49 +00:00
|
|
|
addTransformerConfig(extractor)
|
|
|
|
mockTransactionSyncer := &fakes.MockTransactionSyncer{}
|
|
|
|
extractor.Syncer = mockTransactionSyncer
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(mockTransactionSyncer.SyncTransactionsCalled).To(BeFalse())
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
|
|
|
})
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-12 17:26:49 +00:00
|
|
|
Describe("when there are fetched logs", func() {
|
2019-08-14 22:29:04 +00:00
|
|
|
It("syncs transactions", func() {
|
2019-09-10 19:39:45 +00:00
|
|
|
addUncheckedHeader(extractor)
|
2019-08-12 17:26:49 +00:00
|
|
|
addFetchedLog(extractor)
|
|
|
|
addTransformerConfig(extractor)
|
|
|
|
mockTransactionSyncer := &fakes.MockTransactionSyncer{}
|
|
|
|
extractor.Syncer = mockTransactionSyncer
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(mockTransactionSyncer.SyncTransactionsCalled).To(BeTrue())
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
It("returns error if syncing transactions fails", func() {
|
2019-09-10 19:39:45 +00:00
|
|
|
addUncheckedHeader(extractor)
|
2019-08-12 17:26:49 +00:00
|
|
|
addFetchedLog(extractor)
|
|
|
|
addTransformerConfig(extractor)
|
|
|
|
mockTransactionSyncer := &fakes.MockTransactionSyncer{}
|
|
|
|
mockTransactionSyncer.SyncTransactionsError = fakes.FakeError
|
|
|
|
extractor.Syncer = mockTransactionSyncer
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
It("persists fetched logs", func() {
|
2019-09-10 19:39:45 +00:00
|
|
|
addUncheckedHeader(extractor)
|
2019-08-12 17:26:49 +00:00
|
|
|
addTransformerConfig(extractor)
|
|
|
|
fakeLogs := []types.Log{{
|
|
|
|
Address: common.HexToAddress("0xA"),
|
|
|
|
Topics: []common.Hash{common.HexToHash("0xA")},
|
|
|
|
Data: []byte{},
|
|
|
|
Index: 0,
|
|
|
|
}}
|
|
|
|
mockLogFetcher := &mocks.MockLogFetcher{ReturnLogs: fakeLogs}
|
|
|
|
extractor.Fetcher = mockLogFetcher
|
|
|
|
mockLogRepository := &fakes.MockHeaderSyncLogRepository{}
|
|
|
|
extractor.LogRepository = mockLogRepository
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(mockLogRepository.PassedLogs).To(Equal(fakeLogs))
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
It("returns error if persisting logs fails", func() {
|
2019-09-10 19:39:45 +00:00
|
|
|
addUncheckedHeader(extractor)
|
2019-08-12 17:26:49 +00:00
|
|
|
addFetchedLog(extractor)
|
|
|
|
addTransformerConfig(extractor)
|
|
|
|
mockLogRepository := &fakes.MockHeaderSyncLogRepository{}
|
|
|
|
mockLogRepository.CreateError = fakes.FakeError
|
|
|
|
extractor.LogRepository = mockLogRepository
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
|
|
|
})
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
It("marks header checked", func() {
|
2019-08-12 17:26:49 +00:00
|
|
|
addFetchedLog(extractor)
|
|
|
|
addTransformerConfig(extractor)
|
|
|
|
mockCheckedHeadersRepository := &fakes.MockCheckedHeadersRepository{}
|
|
|
|
headerID := rand.Int63()
|
2019-10-18 16:16:19 +00:00
|
|
|
mockCheckedHeadersRepository.UncheckedHeadersReturnHeaders = []core.Header{{ID: headerID}}
|
2019-08-12 17:26:49 +00:00
|
|
|
extractor.CheckedHeadersRepository = mockCheckedHeadersRepository
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-08-26 21:43:14 +00:00
|
|
|
Expect(mockCheckedHeadersRepository.MarkHeaderCheckedHeaderID).To(Equal(headerID))
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
It("returns error if marking header checked fails", func() {
|
2019-08-12 17:26:49 +00:00
|
|
|
addFetchedLog(extractor)
|
|
|
|
addTransformerConfig(extractor)
|
|
|
|
mockCheckedHeadersRepository := &fakes.MockCheckedHeadersRepository{}
|
2019-10-18 16:16:19 +00:00
|
|
|
mockCheckedHeadersRepository.UncheckedHeadersReturnHeaders = []core.Header{{ID: rand.Int63()}}
|
2019-08-12 17:26:49 +00:00
|
|
|
mockCheckedHeadersRepository.MarkHeaderCheckedReturnError = fakes.FakeError
|
|
|
|
extractor.CheckedHeadersRepository = mockCheckedHeadersRepository
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
It("returns nil for error if everything succeeds", func() {
|
2019-09-10 19:39:45 +00:00
|
|
|
addUncheckedHeader(extractor)
|
2019-08-12 17:26:49 +00:00
|
|
|
addTransformerConfig(extractor)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := extractor.ExtractLogs(constants.HeaderUnchecked)
|
2019-08-06 21:57:08 +00:00
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
2019-08-06 21:57:08 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
func addTransformerConfig(extractor *logs.LogExtractor) {
|
|
|
|
fakeConfig := transformer.EventTransformerConfig{
|
|
|
|
ContractAddresses: []string{fakes.FakeAddress.Hex()},
|
|
|
|
Topic: fakes.FakeHash.Hex(),
|
|
|
|
StartingBlockNumber: rand.Int63(),
|
|
|
|
}
|
|
|
|
extractor.AddTransformerConfig(fakeConfig)
|
|
|
|
}
|
|
|
|
|
2019-09-10 19:39:45 +00:00
|
|
|
func addUncheckedHeader(extractor *logs.LogExtractor) {
|
2019-08-06 21:57:08 +00:00
|
|
|
mockCheckedHeadersRepository := &fakes.MockCheckedHeadersRepository{}
|
2019-09-10 19:39:45 +00:00
|
|
|
mockCheckedHeadersRepository.UncheckedHeadersReturnHeaders = []core.Header{{}}
|
2019-08-06 21:57:08 +00:00
|
|
|
extractor.CheckedHeadersRepository = mockCheckedHeadersRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
func addFetchedLog(extractor *logs.LogExtractor) {
|
|
|
|
mockLogFetcher := &mocks.MockLogFetcher{}
|
|
|
|
mockLogFetcher.ReturnLogs = []types.Log{{}}
|
|
|
|
extractor.Fetcher = mockLogFetcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTransformerConfig(startingBlockNumber int64) transformer.EventTransformerConfig {
|
|
|
|
return transformer.EventTransformerConfig{
|
|
|
|
ContractAddresses: []string{fakes.FakeAddress.Hex()},
|
|
|
|
Topic: fakes.FakeHash.Hex(),
|
|
|
|
StartingBlockNumber: startingBlockNumber,
|
|
|
|
}
|
|
|
|
}
|