2019-07-30 14:03:03 +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 watcher_test
|
|
|
|
|
|
|
|
import (
|
2019-11-01 05:29:57 +00:00
|
|
|
"errors"
|
2019-09-30 17:37:16 +00:00
|
|
|
"io/ioutil"
|
2019-10-31 16:04:53 +00:00
|
|
|
"math/rand"
|
2019-09-30 17:37:16 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-07-30 14:03:03 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/sirupsen/logrus"
|
2019-10-24 16:35:39 +00:00
|
|
|
|
2019-07-30 14:03:03 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/mocks"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2019-10-24 16:35:39 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/test_data"
|
2019-07-30 14:03:03 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/watcher"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/datastore/postgres/repositories"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/fakes"
|
2019-07-30 14:03:03 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/test_config"
|
|
|
|
)
|
|
|
|
|
2019-08-16 21:01:01 +00:00
|
|
|
var _ = Describe("Storage Watcher", func() {
|
2019-10-31 16:04:53 +00:00
|
|
|
var (
|
|
|
|
mockFetcher *mocks.StorageFetcher
|
|
|
|
mockQueue *mocks.MockStorageQueue
|
|
|
|
mockTransformer *mocks.MockStorageTransformer
|
|
|
|
storageWatcher *watcher.StorageWatcher
|
|
|
|
mockStorageDiffRepository *fakes.MockStorageDiffRepository
|
|
|
|
fakeDiffId = rand.Int63()
|
|
|
|
hashedAddress = utils.HexToKeccak256Hash("0x0123456789abcdef")
|
|
|
|
csvDiff utils.StorageDiffInput
|
|
|
|
)
|
2019-08-16 21:01:01 +00:00
|
|
|
Describe("AddTransformer", func() {
|
|
|
|
It("adds transformers", func() {
|
2019-08-16 21:30:26 +00:00
|
|
|
fakeHashedAddress := utils.HexToKeccak256Hash("0x12345")
|
2019-08-16 21:01:01 +00:00
|
|
|
fakeTransformer := &mocks.MockStorageTransformer{KeccakOfAddress: fakeHashedAddress}
|
2019-11-01 05:29:57 +00:00
|
|
|
w := watcher.NewStorageWatcher(mocks.NewStorageFetcher(), test_config.NewTestDB(test_config.NewTestNode()))
|
2019-07-30 14:03:03 +00:00
|
|
|
|
2019-08-16 21:01:01 +00:00
|
|
|
w.AddTransformers([]transformer.StorageTransformerInitializer{fakeTransformer.FakeTransformerInitializer})
|
2019-07-30 14:03:03 +00:00
|
|
|
|
2019-08-16 21:01:01 +00:00
|
|
|
Expect(w.KeccakAddressTransformers[fakeHashedAddress]).To(Equal(fakeTransformer))
|
|
|
|
})
|
2019-07-30 14:03:03 +00:00
|
|
|
})
|
2019-08-16 21:01:01 +00:00
|
|
|
Describe("Execute", func() {
|
2019-07-30 14:03:03 +00:00
|
|
|
BeforeEach(func() {
|
2019-11-01 05:29:57 +00:00
|
|
|
mockFetcher = mocks.NewStorageFetcher()
|
2019-07-30 14:03:03 +00:00
|
|
|
mockQueue = &mocks.MockStorageQueue{}
|
2019-10-31 16:04:53 +00:00
|
|
|
mockStorageDiffRepository = &fakes.MockStorageDiffRepository{}
|
2019-08-16 21:01:01 +00:00
|
|
|
mockTransformer = &mocks.MockStorageTransformer{KeccakOfAddress: hashedAddress}
|
2019-10-31 16:04:53 +00:00
|
|
|
csvDiff = utils.StorageDiffInput{
|
2019-09-10 21:26:54 +00:00
|
|
|
HashedAddress: hashedAddress,
|
|
|
|
BlockHash: common.HexToHash("0xfedcba9876543210"),
|
|
|
|
BlockHeight: 0,
|
|
|
|
StorageKey: common.HexToHash("0xabcdef1234567890"),
|
|
|
|
StorageValue: common.HexToHash("0x9876543210abcdef"),
|
2019-07-30 14:03:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("logs error if fetching storage diffs fails", func(done Done) {
|
|
|
|
mockFetcher.ErrsToReturn = []error{fakes.FakeError}
|
2019-08-16 21:01:01 +00:00
|
|
|
storageWatcher = watcher.NewStorageWatcher(mockFetcher, test_config.NewTestDB(test_config.NewTestNode()))
|
2019-07-30 14:03:03 +00:00
|
|
|
storageWatcher.Queue = mockQueue
|
|
|
|
storageWatcher.AddTransformers([]transformer.StorageTransformerInitializer{mockTransformer.FakeTransformerInitializer})
|
2019-10-31 16:04:53 +00:00
|
|
|
storageWatcher.StorageDiffRepository = mockStorageDiffRepository
|
2019-07-30 14:03:03 +00:00
|
|
|
tempFile, fileErr := ioutil.TempFile("", "log")
|
|
|
|
Expect(fileErr).NotTo(HaveOccurred())
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
logrus.SetOutput(tempFile)
|
|
|
|
|
2019-10-24 16:35:39 +00:00
|
|
|
go storageWatcher.Execute(time.Hour, false)
|
2019-07-30 14:03:03 +00:00
|
|
|
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
|
|
|
}).Should(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("transforming new storage diffs from csv", func() {
|
2019-10-31 16:04:53 +00:00
|
|
|
var fakePersistedDiff utils.PersistedStorageDiff
|
2019-07-30 14:03:03 +00:00
|
|
|
BeforeEach(func() {
|
2019-10-31 16:04:53 +00:00
|
|
|
mockFetcher.DiffsToReturn = []utils.StorageDiffInput{csvDiff}
|
2019-08-16 21:01:01 +00:00
|
|
|
storageWatcher = watcher.NewStorageWatcher(mockFetcher, test_config.NewTestDB(test_config.NewTestNode()))
|
2019-07-30 14:03:03 +00:00
|
|
|
storageWatcher.Queue = mockQueue
|
|
|
|
storageWatcher.AddTransformers([]transformer.StorageTransformerInitializer{mockTransformer.FakeTransformerInitializer})
|
2019-10-31 16:04:53 +00:00
|
|
|
fakePersistedDiff = utils.PersistedStorageDiff{
|
|
|
|
ID: fakeDiffId,
|
|
|
|
StorageDiffInput: utils.StorageDiffInput{
|
|
|
|
HashedAddress: csvDiff.HashedAddress,
|
|
|
|
BlockHash: csvDiff.BlockHash,
|
|
|
|
BlockHeight: csvDiff.BlockHeight,
|
|
|
|
StorageValue: csvDiff.StorageValue,
|
|
|
|
StorageKey: csvDiff.StorageKey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mockStorageDiffRepository.CreateReturnID = fakeDiffId
|
|
|
|
storageWatcher.StorageDiffRepository = mockStorageDiffRepository
|
|
|
|
})
|
|
|
|
|
|
|
|
It("writes raw diff before processing", func(done Done) {
|
|
|
|
go storageWatcher.Execute(time.Hour, false)
|
|
|
|
|
|
|
|
Eventually(func() []utils.StorageDiffInput {
|
|
|
|
return mockStorageDiffRepository.CreatePassedInputs
|
|
|
|
}).Should(ContainElement(csvDiff))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("discards raw diff if it's already been persisted", func(done Done) {
|
|
|
|
mockStorageDiffRepository.CreateReturnError = repositories.ErrDuplicateDiff
|
|
|
|
|
|
|
|
go storageWatcher.Execute(time.Hour, false)
|
|
|
|
|
|
|
|
Consistently(func() []utils.PersistedStorageDiff {
|
|
|
|
return mockTransformer.PassedDiffs
|
|
|
|
}).Should(BeZero())
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("logs error if persisting raw diff fails", func(done Done) {
|
|
|
|
mockStorageDiffRepository.CreateReturnError = fakes.FakeError
|
|
|
|
tempFile, fileErr := ioutil.TempFile("", "log")
|
|
|
|
Expect(fileErr).NotTo(HaveOccurred())
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
logrus.SetOutput(tempFile)
|
|
|
|
|
|
|
|
go storageWatcher.Execute(time.Hour, false)
|
|
|
|
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
|
|
|
}).Should(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
close(done)
|
2019-07-30 14:03:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("executes transformer for recognized storage diff", func(done Done) {
|
2019-10-24 16:35:39 +00:00
|
|
|
go storageWatcher.Execute(time.Hour, false)
|
2019-07-30 14:03:03 +00:00
|
|
|
|
2019-10-31 16:04:53 +00:00
|
|
|
Eventually(func() []utils.PersistedStorageDiff {
|
2019-10-24 16:35:39 +00:00
|
|
|
return mockTransformer.PassedDiffs
|
2019-10-31 16:04:53 +00:00
|
|
|
}).Should(Equal([]utils.PersistedStorageDiff{
|
|
|
|
fakePersistedDiff,
|
2019-11-04 20:50:10 +00:00
|
|
|
}))
|
2019-07-30 14:03:03 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("queues diff for later processing if transformer execution fails", func(done Done) {
|
|
|
|
mockTransformer.ExecuteErr = fakes.FakeError
|
|
|
|
|
2019-10-24 16:35:39 +00:00
|
|
|
go storageWatcher.Execute(time.Hour, false)
|
2019-07-30 14:03:03 +00:00
|
|
|
|
|
|
|
Eventually(func() bool {
|
|
|
|
return mockQueue.AddCalled
|
|
|
|
}).Should(BeTrue())
|
2019-10-31 16:04:53 +00:00
|
|
|
Eventually(func() utils.PersistedStorageDiff {
|
2019-10-24 16:35:39 +00:00
|
|
|
if len(mockQueue.AddPassedDiffs) > 0 {
|
2019-10-31 16:04:53 +00:00
|
|
|
return mockQueue.AddPassedDiffs[0]
|
2019-10-24 16:35:39 +00:00
|
|
|
}
|
2019-10-31 16:04:53 +00:00
|
|
|
return utils.PersistedStorageDiff{}
|
|
|
|
}).Should(Equal(fakePersistedDiff))
|
2019-07-30 14:03:03 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("logs error if queueing diff fails", func(done Done) {
|
|
|
|
mockTransformer.ExecuteErr = utils.ErrStorageKeyNotFound{}
|
|
|
|
mockQueue.AddError = fakes.FakeError
|
|
|
|
tempFile, fileErr := ioutil.TempFile("", "log")
|
|
|
|
Expect(fileErr).NotTo(HaveOccurred())
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
logrus.SetOutput(tempFile)
|
|
|
|
|
2019-10-24 16:35:39 +00:00
|
|
|
go storageWatcher.Execute(time.Hour, false)
|
2019-07-30 14:03:03 +00:00
|
|
|
|
|
|
|
Eventually(func() bool {
|
|
|
|
return mockQueue.AddCalled
|
|
|
|
}).Should(BeTrue())
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
|
|
|
}).Should(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("transforming queued storage diffs", func() {
|
2019-10-31 16:04:53 +00:00
|
|
|
var queuedDiff utils.PersistedStorageDiff
|
2019-07-30 14:03:03 +00:00
|
|
|
BeforeEach(func() {
|
2019-10-31 16:04:53 +00:00
|
|
|
queuedDiff = utils.PersistedStorageDiff{
|
|
|
|
ID: 1337,
|
|
|
|
StorageDiffInput: utils.StorageDiffInput{
|
|
|
|
HashedAddress: hashedAddress,
|
|
|
|
BlockHash: test_data.FakeHash(),
|
|
|
|
BlockHeight: rand.Int(),
|
|
|
|
StorageKey: test_data.FakeHash(),
|
|
|
|
StorageValue: test_data.FakeHash(),
|
|
|
|
},
|
2019-11-04 20:50:10 +00:00
|
|
|
}
|
2019-10-31 16:04:53 +00:00
|
|
|
mockQueue.DiffsToReturn = []utils.PersistedStorageDiff{queuedDiff}
|
2019-08-16 21:01:01 +00:00
|
|
|
storageWatcher = watcher.NewStorageWatcher(mockFetcher, test_config.NewTestDB(test_config.NewTestNode()))
|
2019-07-30 14:03:03 +00:00
|
|
|
storageWatcher.Queue = mockQueue
|
|
|
|
storageWatcher.AddTransformers([]transformer.StorageTransformerInitializer{mockTransformer.FakeTransformerInitializer})
|
|
|
|
})
|
|
|
|
|
|
|
|
It("executes transformer for storage diff", func(done Done) {
|
2019-10-24 16:35:39 +00:00
|
|
|
go storageWatcher.Execute(time.Nanosecond, false)
|
2019-07-30 14:03:03 +00:00
|
|
|
|
2019-10-31 16:04:53 +00:00
|
|
|
Eventually(func() utils.PersistedStorageDiff {
|
2019-10-24 16:35:39 +00:00
|
|
|
if len(mockTransformer.PassedDiffs) > 0 {
|
2019-10-31 16:04:53 +00:00
|
|
|
return mockTransformer.PassedDiffs[0]
|
2019-10-24 16:35:39 +00:00
|
|
|
}
|
2019-10-31 16:04:53 +00:00
|
|
|
return utils.PersistedStorageDiff{}
|
|
|
|
}).Should(Equal(queuedDiff))
|
2019-07-30 14:03:03 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("deletes diff from queue if transformer execution successful", func(done Done) {
|
2019-10-24 16:35:39 +00:00
|
|
|
go storageWatcher.Execute(time.Nanosecond, false)
|
2019-07-30 14:03:03 +00:00
|
|
|
|
2019-10-31 16:04:53 +00:00
|
|
|
Eventually(func() int64 {
|
2019-10-24 16:35:39 +00:00
|
|
|
if len(mockQueue.DeletePassedIds) > 0 {
|
|
|
|
return mockQueue.DeletePassedIds[0]
|
|
|
|
}
|
|
|
|
return 0
|
2019-10-31 16:04:53 +00:00
|
|
|
}).Should(Equal(queuedDiff.ID))
|
2019-07-30 14:03:03 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("logs error if deleting persisted diff fails", func(done Done) {
|
|
|
|
mockQueue.DeleteErr = fakes.FakeError
|
|
|
|
tempFile, fileErr := ioutil.TempFile("", "log")
|
|
|
|
Expect(fileErr).NotTo(HaveOccurred())
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
logrus.SetOutput(tempFile)
|
|
|
|
|
2019-10-24 16:35:39 +00:00
|
|
|
go storageWatcher.Execute(time.Nanosecond, false)
|
2019-07-30 14:03:03 +00:00
|
|
|
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
|
|
|
}).Should(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("deletes obsolete diff from queue if contract not recognized", func(done Done) {
|
2019-10-31 16:04:53 +00:00
|
|
|
obsoleteDiff := utils.PersistedStorageDiff{
|
|
|
|
ID: queuedDiff.ID + 1,
|
|
|
|
StorageDiffInput: utils.StorageDiffInput{HashedAddress: test_data.FakeHash()},
|
2019-11-04 20:50:10 +00:00
|
|
|
}
|
2019-10-31 16:04:53 +00:00
|
|
|
mockQueue.DiffsToReturn = []utils.PersistedStorageDiff{obsoleteDiff}
|
2019-07-30 14:03:03 +00:00
|
|
|
|
2019-10-24 16:35:39 +00:00
|
|
|
go storageWatcher.Execute(time.Nanosecond, false)
|
2019-07-30 14:03:03 +00:00
|
|
|
|
2019-10-31 16:04:53 +00:00
|
|
|
Eventually(func() int64 {
|
2019-10-24 16:35:39 +00:00
|
|
|
if len(mockQueue.DeletePassedIds) > 0 {
|
|
|
|
return mockQueue.DeletePassedIds[0]
|
|
|
|
}
|
|
|
|
return 0
|
2019-10-18 16:16:19 +00:00
|
|
|
}).Should(Equal(obsoleteDiff.ID))
|
2019-07-30 14:03:03 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("logs error if deleting obsolete diff fails", func(done Done) {
|
2019-10-31 16:04:53 +00:00
|
|
|
obsoleteDiff := utils.PersistedStorageDiff{
|
|
|
|
ID: queuedDiff.ID + 1,
|
|
|
|
StorageDiffInput: utils.StorageDiffInput{HashedAddress: test_data.FakeHash()},
|
2019-11-04 20:50:10 +00:00
|
|
|
}
|
2019-10-31 16:04:53 +00:00
|
|
|
mockQueue.DiffsToReturn = []utils.PersistedStorageDiff{obsoleteDiff}
|
2019-07-30 14:03:03 +00:00
|
|
|
mockQueue.DeleteErr = fakes.FakeError
|
|
|
|
tempFile, fileErr := ioutil.TempFile("", "log")
|
|
|
|
Expect(fileErr).NotTo(HaveOccurred())
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
logrus.SetOutput(tempFile)
|
|
|
|
|
2019-10-24 16:35:39 +00:00
|
|
|
go storageWatcher.Execute(time.Nanosecond, false)
|
|
|
|
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
|
|
|
}).Should(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("BackFill", func() {
|
|
|
|
var (
|
2019-10-31 16:04:53 +00:00
|
|
|
mockBackFiller *mocks.BackFiller
|
|
|
|
mockTransformer2 *mocks.MockStorageTransformer
|
|
|
|
mockTransformer3 *mocks.MockStorageTransformer
|
|
|
|
createdPersistedDiff = utils.PersistedStorageDiff{
|
|
|
|
ID: fakeDiffId,
|
|
|
|
StorageDiffInput: test_data.CreatedExpectedStorageDiff,
|
|
|
|
}
|
|
|
|
updatedPersistedDiff1 = utils.PersistedStorageDiff{
|
|
|
|
ID: fakeDiffId,
|
|
|
|
StorageDiffInput: test_data.UpdatedExpectedStorageDiff,
|
|
|
|
}
|
|
|
|
deletedPersistedDiff = utils.PersistedStorageDiff{
|
|
|
|
ID: fakeDiffId,
|
|
|
|
StorageDiffInput: test_data.DeletedExpectedStorageDiff,
|
|
|
|
}
|
|
|
|
updatedPersistedDiff2 = utils.PersistedStorageDiff{
|
|
|
|
ID: fakeDiffId,
|
|
|
|
StorageDiffInput: test_data.UpdatedExpectedStorageDiff2,
|
|
|
|
}
|
|
|
|
csvDiff = utils.StorageDiffInput{
|
|
|
|
HashedAddress: hashedAddress,
|
|
|
|
BlockHash: common.HexToHash("0xfedcba9876543210"),
|
|
|
|
BlockHeight: int(test_data.BlockNumber2.Int64()) + 1,
|
|
|
|
StorageKey: common.HexToHash("0xabcdef1234567890"),
|
|
|
|
StorageValue: common.HexToHash("0x9876543210abcdef"),
|
|
|
|
}
|
|
|
|
csvPersistedDiff = utils.PersistedStorageDiff{
|
|
|
|
ID: fakeDiffId,
|
|
|
|
StorageDiffInput: csvDiff,
|
|
|
|
}
|
2019-10-24 16:35:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
mockBackFiller = new(mocks.BackFiller)
|
|
|
|
hashedAddress = utils.HexToKeccak256Hash("0x0123456789abcdef")
|
|
|
|
mockFetcher = mocks.NewStorageFetcher()
|
|
|
|
mockQueue = &mocks.MockStorageQueue{}
|
|
|
|
mockTransformer = &mocks.MockStorageTransformer{KeccakOfAddress: hashedAddress}
|
2019-11-01 05:29:57 +00:00
|
|
|
mockTransformer2 = &mocks.MockStorageTransformer{KeccakOfAddress: common.BytesToHash(test_data.ContractLeafKey[:])}
|
|
|
|
mockTransformer3 = &mocks.MockStorageTransformer{KeccakOfAddress: common.BytesToHash(test_data.AnotherContractLeafKey[:])}
|
2019-10-31 16:04:53 +00:00
|
|
|
mockStorageDiffRepository = &fakes.MockStorageDiffRepository{}
|
2019-10-24 16:35:39 +00:00
|
|
|
})
|
|
|
|
|
2019-11-04 20:50:10 +00:00
|
|
|
Describe("transforming streamed and backfilled storage diffs", func() {
|
2019-10-24 16:35:39 +00:00
|
|
|
BeforeEach(func() {
|
2019-10-31 16:04:53 +00:00
|
|
|
mockFetcher.DiffsToReturn = []utils.StorageDiffInput{csvDiff}
|
|
|
|
mockBackFiller.SetStorageDiffsToReturn([]utils.StorageDiffInput{
|
|
|
|
test_data.CreatedExpectedStorageDiff,
|
|
|
|
test_data.UpdatedExpectedStorageDiff,
|
|
|
|
test_data.DeletedExpectedStorageDiff,
|
|
|
|
test_data.UpdatedExpectedStorageDiff2,
|
2019-11-01 05:29:57 +00:00
|
|
|
})
|
2019-10-31 16:04:53 +00:00
|
|
|
mockQueue.DiffsToReturn = []utils.PersistedStorageDiff{}
|
2019-10-24 16:35:39 +00:00
|
|
|
storageWatcher = watcher.NewStorageWatcher(mockFetcher, test_config.NewTestDB(test_config.NewTestNode()))
|
|
|
|
storageWatcher.Queue = mockQueue
|
2019-11-01 05:29:57 +00:00
|
|
|
storageWatcher.AddTransformers([]transformer.StorageTransformerInitializer{
|
|
|
|
mockTransformer.FakeTransformerInitializer,
|
|
|
|
mockTransformer2.FakeTransformerInitializer,
|
|
|
|
mockTransformer3.FakeTransformerInitializer,
|
|
|
|
})
|
2019-10-31 16:04:53 +00:00
|
|
|
mockStorageDiffRepository.CreateReturnID = fakeDiffId
|
|
|
|
|
|
|
|
storageWatcher.StorageDiffRepository = mockStorageDiffRepository
|
2019-10-24 16:35:39 +00:00
|
|
|
})
|
|
|
|
|
2019-11-01 05:29:57 +00:00
|
|
|
It("executes transformer for storage diffs received from fetcher and backfiller", func(done Done) {
|
2019-11-04 20:50:10 +00:00
|
|
|
go storageWatcher.BackFill(test_data.BlockNumber.Uint64(), mockBackFiller)
|
2019-11-01 05:29:57 +00:00
|
|
|
go storageWatcher.Execute(time.Hour, true)
|
|
|
|
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer.PassedDiffs)
|
|
|
|
}).Should(Equal(1))
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer2.PassedDiffs)
|
|
|
|
}).Should(Equal(1))
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer3.PassedDiffs)
|
|
|
|
}).Should(Equal(3))
|
2019-10-31 16:04:53 +00:00
|
|
|
|
2019-11-01 05:29:57 +00:00
|
|
|
Expect(mockBackFiller.PassedEndingBlock).To(Equal(uint64(test_data.BlockNumber2.Int64())))
|
2019-10-31 16:04:53 +00:00
|
|
|
Expect(mockTransformer.PassedDiffs[0]).To(Equal(csvPersistedDiff))
|
|
|
|
Expect(mockTransformer2.PassedDiffs[0]).To(Equal(createdPersistedDiff))
|
|
|
|
Expect(mockTransformer3.PassedDiffs).To(ConsistOf(updatedPersistedDiff1, deletedPersistedDiff, updatedPersistedDiff2))
|
2019-11-01 05:29:57 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("adds diffs to the queue if transformation fails", func(done Done) {
|
|
|
|
mockTransformer3.ExecuteErr = fakes.FakeError
|
2019-11-04 20:50:10 +00:00
|
|
|
go storageWatcher.BackFill(test_data.BlockNumber.Uint64(), mockBackFiller)
|
2019-11-01 05:29:57 +00:00
|
|
|
go storageWatcher.Execute(time.Hour, true)
|
|
|
|
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer.PassedDiffs)
|
|
|
|
}).Should(Equal(1))
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer2.PassedDiffs)
|
|
|
|
}).Should(Equal(1))
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer3.PassedDiffs)
|
|
|
|
}).Should(Equal(3))
|
|
|
|
|
|
|
|
Eventually(func() bool {
|
|
|
|
return mockQueue.AddCalled
|
|
|
|
}).Should(BeTrue())
|
2019-10-31 16:04:53 +00:00
|
|
|
Eventually(func() []utils.PersistedStorageDiff {
|
2019-11-01 05:29:57 +00:00
|
|
|
if len(mockQueue.AddPassedDiffs) > 2 {
|
|
|
|
return mockQueue.AddPassedDiffs
|
2019-10-24 16:35:39 +00:00
|
|
|
}
|
2019-10-31 16:04:53 +00:00
|
|
|
return []utils.PersistedStorageDiff{}
|
|
|
|
}).Should(ConsistOf(updatedPersistedDiff1, deletedPersistedDiff, updatedPersistedDiff2))
|
2019-11-01 05:29:57 +00:00
|
|
|
|
2019-11-04 20:50:10 +00:00
|
|
|
Expect(mockBackFiller.PassedEndingBlock).To(Equal(uint64(test_data.BlockNumber2.Int64())))
|
2019-10-31 16:04:53 +00:00
|
|
|
Expect(mockTransformer.PassedDiffs[0]).To(Equal(csvPersistedDiff))
|
|
|
|
Expect(mockTransformer2.PassedDiffs[0]).To(Equal(createdPersistedDiff))
|
|
|
|
Expect(mockTransformer3.PassedDiffs).To(ConsistOf(updatedPersistedDiff1, deletedPersistedDiff, updatedPersistedDiff2))
|
2019-10-24 16:35:39 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
2019-11-01 05:29:57 +00:00
|
|
|
It("logs a backfill error", func(done Done) {
|
|
|
|
tempFile, fileErr := ioutil.TempFile("", "log")
|
|
|
|
Expect(fileErr).NotTo(HaveOccurred())
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
logrus.SetOutput(tempFile)
|
|
|
|
|
|
|
|
mockBackFiller.BackFillErrs = []error{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
errors.New("mock backfiller error"),
|
2019-10-24 16:35:39 +00:00
|
|
|
}
|
2019-11-01 05:29:57 +00:00
|
|
|
|
2019-11-04 20:50:10 +00:00
|
|
|
go storageWatcher.BackFill(test_data.BlockNumber.Uint64(), mockBackFiller)
|
2019-11-01 05:29:57 +00:00
|
|
|
go storageWatcher.Execute(time.Hour, true)
|
|
|
|
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer.PassedDiffs)
|
|
|
|
}).Should(Equal(1))
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer2.PassedDiffs)
|
|
|
|
}).Should(Equal(1))
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer3.PassedDiffs)
|
|
|
|
}).Should(Equal(2))
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
|
|
|
}).Should(ContainSubstring("mock backfiller error"))
|
2019-10-24 16:35:39 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
2019-11-01 05:29:57 +00:00
|
|
|
It("logs when backfill finishes", func(done Done) {
|
2019-10-24 16:35:39 +00:00
|
|
|
tempFile, fileErr := ioutil.TempFile("", "log")
|
|
|
|
Expect(fileErr).NotTo(HaveOccurred())
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
logrus.SetOutput(tempFile)
|
|
|
|
|
2019-11-04 20:50:10 +00:00
|
|
|
go storageWatcher.BackFill(test_data.BlockNumber.Uint64(), mockBackFiller)
|
2019-11-01 05:29:57 +00:00
|
|
|
go storageWatcher.Execute(time.Hour, true)
|
2019-07-30 14:03:03 +00:00
|
|
|
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
2019-11-01 05:29:57 +00:00
|
|
|
}).Should(ContainSubstring("storage watcher backfill process has finished"))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-04 20:50:10 +00:00
|
|
|
Describe("transforms queued storage diffs", func() {
|
2019-11-01 05:29:57 +00:00
|
|
|
BeforeEach(func() {
|
2019-10-31 16:04:53 +00:00
|
|
|
mockQueue.DiffsToReturn = []utils.PersistedStorageDiff{
|
|
|
|
csvPersistedDiff,
|
|
|
|
createdPersistedDiff,
|
|
|
|
updatedPersistedDiff1,
|
|
|
|
deletedPersistedDiff,
|
|
|
|
updatedPersistedDiff2,
|
2019-11-01 05:29:57 +00:00
|
|
|
}
|
|
|
|
storageWatcher = watcher.NewStorageWatcher(mockFetcher, test_config.NewTestDB(test_config.NewTestNode()))
|
|
|
|
storageWatcher.Queue = mockQueue
|
|
|
|
storageWatcher.AddTransformers([]transformer.StorageTransformerInitializer{
|
|
|
|
mockTransformer.FakeTransformerInitializer,
|
|
|
|
mockTransformer2.FakeTransformerInitializer,
|
|
|
|
mockTransformer3.FakeTransformerInitializer,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
It("executes transformers on queued storage diffs", func(done Done) {
|
2019-11-04 20:50:10 +00:00
|
|
|
go storageWatcher.BackFill(test_data.BlockNumber.Uint64(), mockBackFiller)
|
2019-11-01 05:29:57 +00:00
|
|
|
go storageWatcher.Execute(time.Nanosecond, true)
|
|
|
|
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer.PassedDiffs)
|
|
|
|
}).Should(Equal(1))
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer2.PassedDiffs)
|
|
|
|
}).Should(Equal(1))
|
|
|
|
Eventually(func() int {
|
|
|
|
return len(mockTransformer3.PassedDiffs)
|
|
|
|
}).Should(Equal(3))
|
|
|
|
Eventually(func() bool {
|
|
|
|
return mockQueue.GetAllCalled
|
|
|
|
}).Should(BeTrue())
|
2019-10-31 16:04:53 +00:00
|
|
|
expectedIDs := []int64{
|
|
|
|
fakeDiffId,
|
|
|
|
fakeDiffId,
|
|
|
|
fakeDiffId,
|
|
|
|
fakeDiffId,
|
|
|
|
fakeDiffId,
|
2019-11-04 20:50:10 +00:00
|
|
|
}
|
2019-10-31 16:04:53 +00:00
|
|
|
Eventually(func() []int64 {
|
2019-11-01 05:29:57 +00:00
|
|
|
if len(mockQueue.DeletePassedIds) > 4 {
|
|
|
|
return mockQueue.DeletePassedIds
|
|
|
|
}
|
2019-10-31 16:04:53 +00:00
|
|
|
return []int64{}
|
|
|
|
}).Should(Equal(expectedIDs))
|
2019-11-01 05:29:57 +00:00
|
|
|
|
|
|
|
Expect(mockQueue.AddCalled).To(Not(BeTrue()))
|
|
|
|
Expect(len(mockQueue.DiffsToReturn)).To(Equal(0))
|
2019-10-31 16:04:53 +00:00
|
|
|
Expect(mockTransformer.PassedDiffs[0]).To(Equal(csvPersistedDiff))
|
|
|
|
Expect(mockTransformer2.PassedDiffs[0]).To(Equal(createdPersistedDiff))
|
|
|
|
Expect(mockTransformer3.PassedDiffs).To(ConsistOf(updatedPersistedDiff1, deletedPersistedDiff, updatedPersistedDiff2))
|
2019-07-30 14:03:03 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|