2018-11-23 18:12:24 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-23 18:12:24 +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/>.
|
|
|
|
|
|
|
|
package transformer_test
|
|
|
|
|
|
|
|
import (
|
2019-10-04 16:58:44 +00:00
|
|
|
"database/sql"
|
2019-10-15 18:12:17 +00:00
|
|
|
|
2018-11-23 18:12:24 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2019-03-14 21:49:27 +00:00
|
|
|
|
2019-05-01 06:02:30 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/header/retriever"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/header/transformer"
|
2019-03-13 16:14:35 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
2019-03-11 23:18:54 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers/mocks"
|
2019-03-13 16:14:35 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/parser"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/poller"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
2018-11-23 18:12:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Transformer", func() {
|
2019-03-13 16:14:35 +00:00
|
|
|
var fakeAddress = "0x1234567890abcdef"
|
2019-03-11 16:19:18 +00:00
|
|
|
Describe("Init", func() {
|
|
|
|
It("Initializes transformer's contract objects", func() {
|
2019-05-01 06:02:30 +00:00
|
|
|
blockRetriever := &fakes.MockHeaderSyncBlockRetriever{}
|
2019-03-13 16:14:35 +00:00
|
|
|
firstBlock := int64(1)
|
|
|
|
blockRetriever.FirstBlock = firstBlock
|
2019-03-11 16:19:18 +00:00
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
parsr := &fakes.MockParser{}
|
|
|
|
fakeAbi := "fake_abi"
|
|
|
|
parsr.AbiToReturn = fakeAbi
|
2018-11-23 18:12:24 +00:00
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
pollr := &fakes.MockPoller{}
|
|
|
|
fakeContractName := "fake_contract_name"
|
|
|
|
pollr.ContractName = fakeContractName
|
2018-12-14 17:52:02 +00:00
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
t := getFakeTransformer(blockRetriever, parsr, pollr)
|
2019-03-11 16:19:18 +00:00
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
err := t.Init()
|
2018-12-14 17:52:02 +00:00
|
|
|
|
2019-03-11 16:19:18 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2018-11-23 18:12:24 +00:00
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
c, ok := t.Contracts[fakeAddress]
|
2019-03-11 16:19:18 +00:00
|
|
|
Expect(ok).To(Equal(true))
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
Expect(c.StartingBlock).To(Equal(firstBlock))
|
2019-03-14 21:49:27 +00:00
|
|
|
Expect(c.Abi).To(Equal(fakeAbi))
|
|
|
|
Expect(c.Name).To(Equal(fakeContractName))
|
|
|
|
Expect(c.Address).To(Equal(fakeAddress))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Fails to initialize if first block cannot be fetched from vDB headers table", func() {
|
2019-05-01 06:02:30 +00:00
|
|
|
blockRetriever := &fakes.MockHeaderSyncBlockRetriever{}
|
2019-03-14 21:49:27 +00:00
|
|
|
blockRetriever.FirstBlockErr = fakes.FakeError
|
|
|
|
t := getFakeTransformer(blockRetriever, &fakes.MockParser{}, &fakes.MockPoller{})
|
|
|
|
|
|
|
|
err := t.Init()
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
2019-09-24 03:39:04 +00:00
|
|
|
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
|
2019-03-14 21:49:27 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Execute", func() {
|
|
|
|
It("Executes contract transformations", func() {
|
2019-05-01 06:02:30 +00:00
|
|
|
blockRetriever := &fakes.MockHeaderSyncBlockRetriever{}
|
2019-03-14 21:49:27 +00:00
|
|
|
firstBlock := int64(1)
|
|
|
|
blockRetriever.FirstBlock = firstBlock
|
|
|
|
|
|
|
|
parsr := &fakes.MockParser{}
|
|
|
|
fakeAbi := "fake_abi"
|
|
|
|
parsr.AbiToReturn = fakeAbi
|
|
|
|
|
|
|
|
pollr := &fakes.MockPoller{}
|
|
|
|
fakeContractName := "fake_contract_name"
|
|
|
|
pollr.ContractName = fakeContractName
|
|
|
|
|
|
|
|
t := getFakeTransformer(blockRetriever, parsr, pollr)
|
|
|
|
|
|
|
|
err := t.Init()
|
|
|
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
c, ok := t.Contracts[fakeAddress]
|
|
|
|
Expect(ok).To(Equal(true))
|
|
|
|
|
|
|
|
Expect(c.StartingBlock).To(Equal(firstBlock))
|
2019-03-13 16:14:35 +00:00
|
|
|
Expect(c.Abi).To(Equal(fakeAbi))
|
|
|
|
Expect(c.Name).To(Equal(fakeContractName))
|
|
|
|
Expect(c.Address).To(Equal(fakeAddress))
|
2019-03-11 16:19:18 +00:00
|
|
|
})
|
|
|
|
|
2019-10-04 16:58:44 +00:00
|
|
|
It("uses first block from config if vDB headers table has no rows", func() {
|
|
|
|
blockRetriever := &fakes.MockHeaderSyncBlockRetriever{}
|
|
|
|
blockRetriever.FirstBlockErr = sql.ErrNoRows
|
|
|
|
t := getFakeTransformer(blockRetriever, &fakes.MockParser{}, &fakes.MockPoller{})
|
|
|
|
|
|
|
|
err := t.Init()
|
|
|
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if fetching first block fails for other reason", func() {
|
2019-05-01 06:02:30 +00:00
|
|
|
blockRetriever := &fakes.MockHeaderSyncBlockRetriever{}
|
2019-03-13 16:14:35 +00:00
|
|
|
blockRetriever.FirstBlockErr = fakes.FakeError
|
|
|
|
t := getFakeTransformer(blockRetriever, &fakes.MockParser{}, &fakes.MockPoller{})
|
2019-03-11 16:19:18 +00:00
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
err := t.Init()
|
2019-03-11 16:19:18 +00:00
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
2019-09-24 03:39:04 +00:00
|
|
|
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
|
2019-03-11 16:19:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-03-13 16:14:35 +00:00
|
|
|
|
|
|
|
func getFakeTransformer(blockRetriever retriever.BlockRetriever, parsr parser.Parser, pollr poller.Poller) transformer.Transformer {
|
|
|
|
return transformer.Transformer{
|
|
|
|
Parser: parsr,
|
2019-03-14 21:49:27 +00:00
|
|
|
Retriever: blockRetriever,
|
2019-03-13 16:14:35 +00:00
|
|
|
Poller: pollr,
|
2019-05-01 06:02:30 +00:00
|
|
|
HeaderRepository: &fakes.MockHeaderSyncHeaderRepository{},
|
2019-03-13 16:14:35 +00:00
|
|
|
Contracts: map[string]*contract.Contract{},
|
|
|
|
Config: mocks.MockConfig,
|
|
|
|
}
|
|
|
|
}
|