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/>.
|
2018-11-04 21:26:39 +00:00
|
|
|
|
|
|
|
package transformer_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/full/retriever"
|
2018-11-23 18:12:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/full/transformer"
|
2019-02-27 16:10:59 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/contract"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/parser"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/poller"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/types"
|
2018-11-04 21:26:39 +00:00
|
|
|
)
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
var _ = Describe("Transformer", func() {
|
2019-02-27 16:10:59 +00:00
|
|
|
var fakeAddress = "0x1234567890abcdef"
|
2018-11-04 21:26:39 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
Describe("SetEvents", func() {
|
|
|
|
It("Sets which events to watch from the given contract address", func() {
|
|
|
|
watchedEvents := []string{"Transfer", "Mint"}
|
2019-02-27 16:10:59 +00:00
|
|
|
t := getTransformer(&fakes.MockFullBlockRetriever{}, &fakes.MockParser{}, &fakes.MockPoller{})
|
|
|
|
t.SetEvents(fakeAddress, watchedEvents)
|
|
|
|
Expect(t.WatchedEvents[fakeAddress]).To(Equal(watchedEvents))
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("SetEventAddrs", func() {
|
|
|
|
It("Sets which account addresses to watch events for", func() {
|
|
|
|
eventAddrs := []string{"test1", "test2"}
|
2019-02-27 16:10:59 +00:00
|
|
|
t := getTransformer(&fakes.MockFullBlockRetriever{}, &fakes.MockParser{}, &fakes.MockPoller{})
|
|
|
|
t.SetEventArgs(fakeAddress, eventAddrs)
|
|
|
|
Expect(t.EventArgs[fakeAddress]).To(Equal(eventAddrs))
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("SetMethods", func() {
|
|
|
|
It("Sets which methods to poll at the given contract address", func() {
|
|
|
|
watchedMethods := []string{"balanceOf", "totalSupply"}
|
2019-02-27 16:10:59 +00:00
|
|
|
t := getTransformer(&fakes.MockFullBlockRetriever{}, &fakes.MockParser{}, &fakes.MockPoller{})
|
|
|
|
t.SetMethods(fakeAddress, watchedMethods)
|
|
|
|
Expect(t.WantedMethods[fakeAddress]).To(Equal(watchedMethods))
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("SetMethodAddrs", func() {
|
|
|
|
It("Sets which account addresses to poll methods against", func() {
|
|
|
|
methodAddrs := []string{"test1", "test2"}
|
2019-02-27 16:10:59 +00:00
|
|
|
t := getTransformer(&fakes.MockFullBlockRetriever{}, &fakes.MockParser{}, &fakes.MockPoller{})
|
|
|
|
t.SetMethodArgs(fakeAddress, methodAddrs)
|
|
|
|
Expect(t.MethodArgs[fakeAddress]).To(Equal(methodAddrs))
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-01-04 18:15:22 +00:00
|
|
|
Describe("SetStartingBlock", func() {
|
2018-11-07 21:50:43 +00:00
|
|
|
It("Sets the block range that the contract should be watched within", func() {
|
2019-02-27 16:10:59 +00:00
|
|
|
t := getTransformer(&fakes.MockFullBlockRetriever{}, &fakes.MockParser{}, &fakes.MockPoller{})
|
|
|
|
t.SetStartingBlock(fakeAddress, 11)
|
|
|
|
Expect(t.ContractStart[fakeAddress]).To(Equal(int64(11)))
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
2018-11-04 21:26:39 +00:00
|
|
|
})
|
|
|
|
|
2018-12-14 17:52:02 +00:00
|
|
|
Describe("SetCreateAddrList", func() {
|
|
|
|
It("Sets the block range that the contract should be watched within", func() {
|
2019-02-27 16:10:59 +00:00
|
|
|
t := getTransformer(&fakes.MockFullBlockRetriever{}, &fakes.MockParser{}, &fakes.MockPoller{})
|
|
|
|
t.SetCreateAddrList(fakeAddress, true)
|
|
|
|
Expect(t.CreateAddrList[fakeAddress]).To(Equal(true))
|
2018-12-14 17:52:02 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("SetCreateHashList", func() {
|
|
|
|
It("Sets the block range that the contract should be watched within", func() {
|
2019-02-27 16:10:59 +00:00
|
|
|
t := getTransformer(&fakes.MockFullBlockRetriever{}, &fakes.MockParser{}, &fakes.MockPoller{})
|
|
|
|
t.SetCreateHashList(fakeAddress, true)
|
|
|
|
Expect(t.CreateHashList[fakeAddress]).To(Equal(true))
|
2018-12-14 17:52:02 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
Describe("Init", func() {
|
|
|
|
It("Initializes transformer's contract objects", func() {
|
2019-02-27 16:10:59 +00:00
|
|
|
blockRetriever := &fakes.MockFullBlockRetriever{}
|
|
|
|
firstBlock := int64(1)
|
|
|
|
mostRecentBlock := int64(2)
|
|
|
|
blockRetriever.FirstBlock = firstBlock
|
|
|
|
blockRetriever.MostRecentBlock = mostRecentBlock
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
parsr := &fakes.MockParser{}
|
|
|
|
fakeAbi := "fake_abi"
|
|
|
|
eventName := "Transfer"
|
|
|
|
event := types.Event{}
|
|
|
|
parsr.AbiToReturn = fakeAbi
|
|
|
|
parsr.EventName = eventName
|
|
|
|
parsr.Event = event
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
pollr := &fakes.MockPoller{}
|
|
|
|
fakeContractName := "fake_contract_name"
|
|
|
|
pollr.ContractName = fakeContractName
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
t := getTransformer(blockRetriever, parsr, pollr)
|
|
|
|
t.SetEvents(fakeAddress, []string{"Transfer"})
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
err := t.Init()
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
c, ok := t.Contracts[fakeAddress]
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(ok).To(Equal(true))
|
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
Expect(c.StartingBlock).To(Equal(firstBlock))
|
|
|
|
Expect(c.LastBlock).To(Equal(mostRecentBlock))
|
|
|
|
Expect(c.Abi).To(Equal(fakeAbi))
|
|
|
|
Expect(c.Name).To(Equal(fakeContractName))
|
|
|
|
Expect(c.Address).To(Equal(fakeAddress))
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
2018-11-20 16:38:23 +00:00
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
It("Fails to initialize if first and most recent blocks cannot be fetched from vDB", func() {
|
|
|
|
blockRetriever := &fakes.MockFullBlockRetriever{}
|
|
|
|
blockRetriever.FirstBlockErr = fakes.FakeError
|
|
|
|
t := getTransformer(blockRetriever, &fakes.MockParser{}, &fakes.MockPoller{})
|
|
|
|
t.SetEvents(fakeAddress, []string{"Transfer"})
|
2018-11-20 16:38:23 +00:00
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
err := t.Init()
|
2018-11-20 16:38:23 +00:00
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
2019-02-27 16:10:59 +00:00
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
2018-12-14 17:52:02 +00:00
|
|
|
})
|
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
It("Does nothing if watched events are unset", func() {
|
|
|
|
t := getTransformer(&fakes.MockFullBlockRetriever{}, &fakes.MockParser{}, &fakes.MockPoller{})
|
2018-12-14 17:52:02 +00:00
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
err := t.Init()
|
2018-12-14 17:52:02 +00:00
|
|
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2019-02-27 16:10:59 +00:00
|
|
|
_, ok := t.Contracts[fakeAddress]
|
2018-12-14 17:52:02 +00:00
|
|
|
Expect(ok).To(Equal(false))
|
|
|
|
})
|
|
|
|
})
|
2018-11-04 21:26:39 +00:00
|
|
|
})
|
2019-02-27 16:10:59 +00:00
|
|
|
|
|
|
|
func getTransformer(blockRetriever retriever.BlockRetriever, parsr parser.Parser, pollr poller.Poller) transformer.Transformer {
|
|
|
|
return transformer.Transformer{
|
|
|
|
FilterRepository: &fakes.MockFilterRepository{},
|
|
|
|
Parser: parsr,
|
|
|
|
BlockRetriever: blockRetriever,
|
|
|
|
Poller: pollr,
|
|
|
|
Contracts: map[string]*contract.Contract{},
|
|
|
|
WatchedEvents: map[string][]string{},
|
|
|
|
WantedMethods: map[string][]string{},
|
|
|
|
ContractStart: map[string]int64{},
|
|
|
|
EventArgs: map[string][]string{},
|
|
|
|
MethodArgs: map[string][]string{},
|
|
|
|
CreateAddrList: map[string]bool{},
|
|
|
|
CreateHashList: map[string]bool{},
|
|
|
|
}
|
|
|
|
}
|