2019-01-28 22:52:47 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2019-01-28 22:52:47 +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-31 23:14:42 +00:00
|
|
|
package watcher_test
|
2019-01-28 22:52:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-04-25 17:44:14 +00:00
|
|
|
"time"
|
2019-01-28 22:52:47 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/mocks"
|
2019-02-10 22:42:41 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2019-01-31 23:14:42 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/watcher"
|
2019-01-28 22:52:47 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
"github.com/vulcanize/vulcanizedb/test_config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Storage Watcher", func() {
|
|
|
|
It("adds transformers", func() {
|
|
|
|
fakeAddress := common.HexToAddress("0x12345")
|
|
|
|
fakeTransformer := &mocks.MockStorageTransformer{Address: fakeAddress}
|
2019-04-24 20:05:57 +00:00
|
|
|
w := watcher.NewStorageWatcher(mocks.NewMockStorageFetcher(), test_config.NewTestDB(test_config.NewTestNode()))
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
w.AddTransformers([]transformer.StorageTransformerInitializer{fakeTransformer.FakeTransformerInitializer})
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
Expect(w.Transformers[fakeAddress]).To(Equal(fakeTransformer))
|
2019-01-28 22:52:47 +00:00
|
|
|
})
|
|
|
|
|
2019-04-24 20:05:57 +00:00
|
|
|
Describe("executing watcher", func() {
|
|
|
|
var (
|
2019-04-25 17:44:14 +00:00
|
|
|
errs chan error
|
2019-04-24 20:05:57 +00:00
|
|
|
mockFetcher *mocks.MockStorageFetcher
|
|
|
|
mockQueue *mocks.MockStorageQueue
|
|
|
|
mockTransformer *mocks.MockStorageTransformer
|
|
|
|
row utils.StorageDiffRow
|
2019-04-25 17:44:14 +00:00
|
|
|
rows chan utils.StorageDiffRow
|
2019-04-24 20:05:57 +00:00
|
|
|
storageWatcher watcher.StorageWatcher
|
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2019-04-25 17:44:14 +00:00
|
|
|
errs = make(chan error)
|
|
|
|
rows = make(chan utils.StorageDiffRow)
|
2019-04-24 20:05:57 +00:00
|
|
|
address := common.HexToAddress("0x0123456789abcdef")
|
|
|
|
mockFetcher = mocks.NewMockStorageFetcher()
|
|
|
|
mockQueue = &mocks.MockStorageQueue{}
|
|
|
|
mockTransformer = &mocks.MockStorageTransformer{Address: address}
|
|
|
|
row = utils.StorageDiffRow{
|
2019-04-25 17:44:14 +00:00
|
|
|
Id: 1337,
|
2019-04-24 20:05:57 +00:00
|
|
|
Contract: address,
|
|
|
|
BlockHash: common.HexToHash("0xfedcba9876543210"),
|
|
|
|
BlockHeight: 0,
|
|
|
|
StorageKey: common.HexToHash("0xabcdef1234567890"),
|
|
|
|
StorageValue: common.HexToHash("0x9876543210abcdef"),
|
|
|
|
}
|
2019-04-25 17:44:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("logs error if fetching storage diffs fails", func(done Done) {
|
|
|
|
mockFetcher.ErrsToReturn = []error{fakes.FakeError}
|
2019-04-24 20:05:57 +00:00
|
|
|
storageWatcher = watcher.NewStorageWatcher(mockFetcher, test_config.NewTestDB(test_config.NewTestNode()))
|
|
|
|
storageWatcher.Queue = mockQueue
|
|
|
|
storageWatcher.AddTransformers([]transformer.StorageTransformerInitializer{mockTransformer.FakeTransformerInitializer})
|
2019-04-25 17:44:14 +00:00
|
|
|
tempFile, fileErr := ioutil.TempFile("", "log")
|
|
|
|
Expect(fileErr).NotTo(HaveOccurred())
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
logrus.SetOutput(tempFile)
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
go storageWatcher.Execute(rows, errs, time.Hour)
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
|
|
|
}).Should(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
close(done)
|
2019-04-24 20:05:57 +00:00
|
|
|
})
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
Describe("transforming new storage diffs", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
mockFetcher.RowsToReturn = []utils.StorageDiffRow{row}
|
|
|
|
storageWatcher = watcher.NewStorageWatcher(mockFetcher, test_config.NewTestDB(test_config.NewTestNode()))
|
|
|
|
storageWatcher.Queue = mockQueue
|
|
|
|
storageWatcher.AddTransformers([]transformer.StorageTransformerInitializer{mockTransformer.FakeTransformerInitializer})
|
|
|
|
})
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
It("executes transformer for recognized storage row", func(done Done) {
|
|
|
|
go storageWatcher.Execute(rows, errs, time.Hour)
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
Eventually(func() utils.StorageDiffRow {
|
|
|
|
return mockTransformer.PassedRow
|
|
|
|
}).Should(Equal(row))
|
|
|
|
close(done)
|
|
|
|
})
|
2019-02-19 21:01:07 +00:00
|
|
|
|
2019-04-29 19:20:57 +00:00
|
|
|
It("queues row for later processing if transformer execution fails", func(done Done) {
|
|
|
|
mockTransformer.ExecuteErr = fakes.FakeError
|
2019-04-25 17:44:14 +00:00
|
|
|
|
|
|
|
go storageWatcher.Execute(rows, errs, time.Hour)
|
|
|
|
|
|
|
|
Expect(<-errs).To(BeNil())
|
|
|
|
Eventually(func() bool {
|
|
|
|
return mockQueue.AddCalled
|
|
|
|
}).Should(BeTrue())
|
|
|
|
Eventually(func() utils.StorageDiffRow {
|
|
|
|
return mockQueue.AddPassedRow
|
|
|
|
}).Should(Equal(row))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("logs error if queueing row 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-02-19 21:01:07 +00:00
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
go storageWatcher.Execute(rows, errs, time.Hour)
|
2019-04-24 20:05:57 +00:00
|
|
|
|
2019-04-25 17:44:14 +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)
|
|
|
|
})
|
2019-02-19 21:01:07 +00:00
|
|
|
})
|
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
Describe("transforming queued storage diffs", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
mockQueue.RowsToReturn = []utils.StorageDiffRow{row}
|
|
|
|
storageWatcher = watcher.NewStorageWatcher(mockFetcher, test_config.NewTestDB(test_config.NewTestNode()))
|
|
|
|
storageWatcher.Queue = mockQueue
|
|
|
|
storageWatcher.AddTransformers([]transformer.StorageTransformerInitializer{mockTransformer.FakeTransformerInitializer})
|
|
|
|
})
|
|
|
|
|
|
|
|
It("logs error if getting queued storage fails", func(done Done) {
|
|
|
|
mockQueue.GetAllErr = fakes.FakeError
|
|
|
|
tempFile, fileErr := ioutil.TempFile("", "log")
|
|
|
|
Expect(fileErr).NotTo(HaveOccurred())
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
logrus.SetOutput(tempFile)
|
|
|
|
|
|
|
|
go storageWatcher.Execute(rows, errs, time.Nanosecond)
|
|
|
|
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
|
|
|
}).Should(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("executes transformer for storage row", func(done Done) {
|
|
|
|
go storageWatcher.Execute(rows, errs, time.Nanosecond)
|
|
|
|
|
|
|
|
Eventually(func() utils.StorageDiffRow {
|
|
|
|
return mockTransformer.PassedRow
|
|
|
|
}).Should(Equal(row))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("deletes row from queue if transformer execution successful", func(done Done) {
|
|
|
|
go storageWatcher.Execute(rows, errs, time.Nanosecond)
|
2019-02-19 21:01:07 +00:00
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
Eventually(func() int {
|
|
|
|
return mockQueue.DeletePassedId
|
|
|
|
}).Should(Equal(row.Id))
|
|
|
|
close(done)
|
|
|
|
})
|
2019-04-24 20:05:57 +00:00
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
It("logs error if deleting persisted row 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)
|
|
|
|
|
|
|
|
go storageWatcher.Execute(rows, errs, time.Nanosecond)
|
|
|
|
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
|
|
|
}).Should(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("deletes obsolete row from queue if contract not recognized", func(done Done) {
|
|
|
|
obsoleteRow := utils.StorageDiffRow{
|
|
|
|
Id: row.Id + 1,
|
|
|
|
Contract: common.HexToAddress("0xfedcba9876543210"),
|
|
|
|
}
|
|
|
|
mockQueue.RowsToReturn = []utils.StorageDiffRow{obsoleteRow}
|
|
|
|
|
|
|
|
go storageWatcher.Execute(rows, errs, time.Nanosecond)
|
|
|
|
|
|
|
|
Eventually(func() int {
|
|
|
|
return mockQueue.DeletePassedId
|
|
|
|
}).Should(Equal(obsoleteRow.Id))
|
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("logs error if deleting obsolete row fails", func(done Done) {
|
|
|
|
obsoleteRow := utils.StorageDiffRow{
|
|
|
|
Id: row.Id + 1,
|
|
|
|
Contract: common.HexToAddress("0xfedcba9876543210"),
|
|
|
|
}
|
|
|
|
mockQueue.RowsToReturn = []utils.StorageDiffRow{obsoleteRow}
|
|
|
|
mockQueue.DeleteErr = fakes.FakeError
|
|
|
|
tempFile, fileErr := ioutil.TempFile("", "log")
|
|
|
|
Expect(fileErr).NotTo(HaveOccurred())
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
logrus.SetOutput(tempFile)
|
|
|
|
|
|
|
|
go storageWatcher.Execute(rows, errs, time.Nanosecond)
|
|
|
|
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
logContent, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
return string(logContent), err
|
|
|
|
}).Should(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
close(done)
|
|
|
|
})
|
2019-02-19 21:01:07 +00:00
|
|
|
})
|
2019-04-25 17:44:14 +00:00
|
|
|
|
2019-01-28 22:52:47 +00:00
|
|
|
})
|
|
|
|
})
|