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 contract_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/contract"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/helpers/test_helpers"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/helpers/test_helpers/mocks"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/types"
|
2018-11-04 21:26:39 +00:00
|
|
|
)
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
var _ = Describe("Contract", func() {
|
2018-11-04 21:26:39 +00:00
|
|
|
var err error
|
2018-11-07 21:50:43 +00:00
|
|
|
var info *contract.Contract
|
|
|
|
var wantedEvents = []string{"Transfer", "Approval"}
|
|
|
|
|
|
|
|
Describe("GenerateFilters", func() {
|
|
|
|
|
|
|
|
It("Generates filters from contract data", func() {
|
|
|
|
info = test_helpers.SetupTusdContract(wantedEvents, nil)
|
|
|
|
err = info.GenerateFilters()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
val, ok := info.Filters["Transfer"]
|
|
|
|
Expect(ok).To(Equal(true))
|
2018-11-24 04:26:07 +00:00
|
|
|
Expect(val).To(Equal(mocks.ExpectedTransferFilter))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
val, ok = info.Filters["Approval"]
|
|
|
|
Expect(ok).To(Equal(true))
|
2018-11-24 04:26:07 +00:00
|
|
|
Expect(val).To(Equal(mocks.ExpectedApprovalFilter))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
val, ok = info.Filters["Mint"]
|
|
|
|
Expect(ok).To(Equal(false))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Fails with an empty contract", func() {
|
|
|
|
info = &contract.Contract{}
|
|
|
|
err = info.GenerateFilters()
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("IsEventAddr", func() {
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
info = &contract.Contract{}
|
2018-12-07 15:38:46 +00:00
|
|
|
info.MethodArgs = map[string]bool{}
|
|
|
|
info.FilterArgs = map[string]bool{}
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("Returns true if address is in event address filter list", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.FilterArgs["testAddress1"] = true
|
|
|
|
info.FilterArgs["testAddress2"] = true
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
is := info.WantedEventArg("testAddress1")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(true))
|
2018-12-07 15:38:46 +00:00
|
|
|
is = info.WantedEventArg("testAddress2")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(true))
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
info.MethodArgs["testAddress3"] = true
|
|
|
|
is = info.WantedEventArg("testAddress3")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(false))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Returns true if event address filter is empty (no filter)", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
is := info.WantedEventArg("testAddress1")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(true))
|
2018-12-07 15:38:46 +00:00
|
|
|
is = info.WantedEventArg("testAddress2")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(true))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Returns false if address is not in event address filter list", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.FilterArgs["testAddress1"] = true
|
|
|
|
info.FilterArgs["testAddress2"] = true
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
is := info.WantedEventArg("testAddress3")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(false))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Returns false if event address filter is nil (block all)", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.FilterArgs = nil
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
is := info.WantedEventArg("testAddress1")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(false))
|
2018-12-07 15:38:46 +00:00
|
|
|
is = info.WantedEventArg("testAddress2")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(false))
|
|
|
|
})
|
|
|
|
})
|
2018-11-04 21:26:39 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
Describe("IsMethodAddr", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
info = &contract.Contract{}
|
2018-12-07 15:38:46 +00:00
|
|
|
info.MethodArgs = map[string]bool{}
|
|
|
|
info.FilterArgs = map[string]bool{}
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("Returns true if address is in method address filter list", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.MethodArgs["testAddress1"] = true
|
|
|
|
info.MethodArgs["testAddress2"] = true
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
is := info.WantedMethodArg("testAddress1")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(true))
|
2018-12-07 15:38:46 +00:00
|
|
|
is = info.WantedMethodArg("testAddress2")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(true))
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
info.FilterArgs["testAddress3"] = true
|
|
|
|
is = info.WantedMethodArg("testAddress3")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(false))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Returns true if method address filter list is empty (no filter)", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
is := info.WantedMethodArg("testAddress1")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(true))
|
2018-12-07 15:38:46 +00:00
|
|
|
is = info.WantedMethodArg("testAddress2")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(true))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Returns false if address is not in method address filter list", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.MethodArgs["testAddress1"] = true
|
|
|
|
info.MethodArgs["testAddress2"] = true
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
is := info.WantedMethodArg("testAddress3")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(false))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Returns false if method address filter list is nil (block all)", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.MethodArgs = nil
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
is := info.WantedMethodArg("testAddress1")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(false))
|
2018-12-07 15:38:46 +00:00
|
|
|
is = info.WantedMethodArg("testAddress2")
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(is).To(Equal(false))
|
|
|
|
})
|
2018-11-04 21:26:39 +00:00
|
|
|
})
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
Describe("PassesEventFilter", func() {
|
|
|
|
var mapping map[string]string
|
|
|
|
BeforeEach(func() {
|
|
|
|
info = &contract.Contract{}
|
2018-12-07 15:38:46 +00:00
|
|
|
info.FilterArgs = map[string]bool{}
|
2018-11-07 21:50:43 +00:00
|
|
|
mapping = map[string]string{}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Return true if event log name-value mapping has filtered for address as a value", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.FilterArgs["testAddress1"] = true
|
|
|
|
info.FilterArgs["testAddress2"] = true
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
mapping["testInputName1"] = "testAddress1"
|
|
|
|
mapping["testInputName2"] = "testAddress2"
|
|
|
|
mapping["testInputName3"] = "testAddress3"
|
|
|
|
|
|
|
|
pass := info.PassesEventFilter(mapping)
|
|
|
|
Expect(pass).To(Equal(true))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Return true if event address filter list is empty (no filter)", func() {
|
|
|
|
mapping["testInputName1"] = "testAddress1"
|
|
|
|
mapping["testInputName2"] = "testAddress2"
|
|
|
|
mapping["testInputName3"] = "testAddress3"
|
|
|
|
|
|
|
|
pass := info.PassesEventFilter(mapping)
|
|
|
|
Expect(pass).To(Equal(true))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Return false if event log name-value mapping does not have filtered for address as a value", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.FilterArgs["testAddress1"] = true
|
|
|
|
info.FilterArgs["testAddress2"] = true
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
mapping["testInputName3"] = "testAddress3"
|
|
|
|
|
|
|
|
pass := info.PassesEventFilter(mapping)
|
|
|
|
Expect(pass).To(Equal(false))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Return false if event address filter list is nil (block all)", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.FilterArgs = nil
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
mapping["testInputName1"] = "testAddress1"
|
|
|
|
mapping["testInputName2"] = "testAddress2"
|
|
|
|
mapping["testInputName3"] = "testAddress3"
|
|
|
|
|
|
|
|
pass := info.PassesEventFilter(mapping)
|
|
|
|
Expect(pass).To(Equal(false))
|
|
|
|
})
|
2018-11-04 21:26:39 +00:00
|
|
|
})
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
Describe("AddEmittedAddr", func() {
|
2018-11-07 21:50:43 +00:00
|
|
|
BeforeEach(func() {
|
|
|
|
info = &contract.Contract{}
|
2018-12-07 15:38:46 +00:00
|
|
|
info.FilterArgs = map[string]bool{}
|
|
|
|
info.MethodArgs = map[string]bool{}
|
2018-12-19 18:42:59 +00:00
|
|
|
info.Methods = []types.Method{}
|
2018-12-07 15:38:46 +00:00
|
|
|
info.EmittedAddrs = map[interface{}]bool{}
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("Adds address to list if it is on the method filter address list", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.MethodArgs["testAddress2"] = true
|
|
|
|
info.AddEmittedAddr("testAddress2")
|
|
|
|
b := info.EmittedAddrs["testAddress2"]
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(b).To(Equal(true))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Adds address to list if method filter is empty", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.AddEmittedAddr("testAddress2")
|
|
|
|
b := info.EmittedAddrs["testAddress2"]
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(b).To(Equal(true))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Does not add address to list if both filters are closed (nil)", func() {
|
2018-12-07 15:38:46 +00:00
|
|
|
info.FilterArgs = nil // close both
|
|
|
|
info.MethodArgs = nil
|
|
|
|
info.AddEmittedAddr("testAddress1")
|
|
|
|
b := info.EmittedAddrs["testAddress1"]
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(b).To(Equal(false))
|
|
|
|
})
|
2018-11-04 21:26:39 +00:00
|
|
|
})
|
|
|
|
})
|