mv pkg/omni pkg/contract_watcher
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,127 @@
|
||||
// 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 constants
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
|
||||
// Basic abi needed to check which interfaces are adhered to
|
||||
var SupportsInterfaceABI = `[{"constant":true,"inputs":[{"name":"interfaceID","type":"bytes4"}],"name":"supportsInterface","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"}]`
|
||||
|
||||
// Individual event interfaces for constructing ABI from
|
||||
var SupportsInterace = `{"constant":true,"inputs":[{"name":"interfaceID","type":"bytes4"}],"name":"supportsInterface","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"}`
|
||||
var AddrChangeInterface = `{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"a","type":"address"}],"name":"AddrChanged","type":"event"}`
|
||||
var ContentChangeInterface = `{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"hash","type":"bytes32"}],"name":"ContentChanged","type":"event"}`
|
||||
var NameChangeInterface = `{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"name","type":"string"}],"name":"NameChanged","type":"event"}`
|
||||
var AbiChangeInterface = `{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":true,"name":"contentType","type":"uint256"}],"name":"ABIChanged","type":"event"}`
|
||||
var PubkeyChangeInterface = `{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"x","type":"bytes32"},{"indexed":false,"name":"y","type":"bytes32"}],"name":"PubkeyChanged","type":"event"}`
|
||||
var TextChangeInterface = `{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"indexedKey","type":"string"},{"indexed":false,"name":"key","type":"string"}],"name":"TextChanged","type":"event"}`
|
||||
var MultihashChangeInterface = `{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"hash","type":"bytes"}],"name":"MultihashChanged","type":"event"}`
|
||||
var ContenthashChangeInterface = `{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"hash","type":"bytes"}],"name":"ContenthashChanged","type":"event"}`
|
||||
|
||||
var StartingBlock = int64(3648359)
|
||||
|
||||
// Resolver interface signatures
|
||||
type Interface int
|
||||
|
||||
const (
|
||||
MetaSig Interface = iota
|
||||
AddrChangeSig
|
||||
ContentChangeSig
|
||||
NameChangeSig
|
||||
AbiChangeSig
|
||||
PubkeyChangeSig
|
||||
TextChangeSig
|
||||
MultihashChangeSig
|
||||
ContentHashChangeSig
|
||||
)
|
||||
|
||||
func (e Interface) Hex() string {
|
||||
strings := [...]string{
|
||||
"0x01ffc9a7",
|
||||
"0x3b3b57de",
|
||||
"0xd8389dc5",
|
||||
"0x691f3431",
|
||||
"0x2203ab56",
|
||||
"0xc8690233",
|
||||
"0x59d1d43c",
|
||||
"0xe89401a1",
|
||||
"0xbc1c58d1",
|
||||
}
|
||||
|
||||
if e < MetaSig || e > ContentHashChangeSig {
|
||||
return "Unknown"
|
||||
}
|
||||
|
||||
return strings[e]
|
||||
}
|
||||
|
||||
func (e Interface) Bytes() [4]uint8 {
|
||||
if e < MetaSig || e > ContentHashChangeSig {
|
||||
return [4]byte{}
|
||||
}
|
||||
|
||||
str := e.Hex()
|
||||
by, _ := hexutil.Decode(str)
|
||||
var byArray [4]uint8
|
||||
for i := 0; i < 4; i++ {
|
||||
byArray[i] = by[i]
|
||||
}
|
||||
|
||||
return byArray
|
||||
}
|
||||
|
||||
func (e Interface) EventSig() string {
|
||||
strings := [...]string{
|
||||
"",
|
||||
"AddrChanged(bytes32,address)",
|
||||
"ContentChanged(bytes32,bytes32)",
|
||||
"NameChanged(bytes32,string)",
|
||||
"ABIChanged(bytes32,uint256)",
|
||||
"PubkeyChanged(bytes32,bytes32,bytes32)",
|
||||
"TextChanged(bytes32,string,string)",
|
||||
"MultihashChanged(bytes32,bytes)",
|
||||
"ContenthashChanged(bytes32,bytes)",
|
||||
}
|
||||
|
||||
if e < MetaSig || e > ContentHashChangeSig {
|
||||
return "Unknown"
|
||||
}
|
||||
|
||||
return strings[e]
|
||||
}
|
||||
|
||||
func (e Interface) MethodSig() string {
|
||||
strings := [...]string{
|
||||
"supportsInterface(bytes4)",
|
||||
"addr(bytes32)",
|
||||
"content(bytes32)",
|
||||
"name(bytes32)",
|
||||
"ABI(bytes32,uint256)",
|
||||
"pubkey(bytes32)",
|
||||
"text(bytes32,string)",
|
||||
"multihash(bytes32)",
|
||||
"setContenthash(bytes32,bytes)",
|
||||
}
|
||||
|
||||
if e < MetaSig || e > ContentHashChangeSig {
|
||||
return "Unknown"
|
||||
}
|
||||
|
||||
return strings[e]
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
// 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 contract
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/filters"
|
||||
)
|
||||
|
||||
// Contract object to hold our contract data
|
||||
type Contract struct {
|
||||
Name string // Name of the contract
|
||||
Address string // Address of the contract
|
||||
Network string // Network on which the contract is deployed; default empty "" is Ethereum mainnet
|
||||
StartingBlock int64 // Starting block of the contract
|
||||
LastBlock int64 // Most recent block on the network
|
||||
Abi string // Abi string
|
||||
ParsedAbi abi.ABI // Parsed abi
|
||||
Events map[string]types.Event // List of events to watch
|
||||
Methods []types.Method // List of methods to poll
|
||||
Filters map[string]filters.LogFilter // Map of event filters to their event names; used only for full sync watcher
|
||||
FilterArgs map[string]bool // User-input list of values to filter event logs for
|
||||
MethodArgs map[string]bool // User-input list of values to limit method polling to
|
||||
EmittedAddrs map[interface{}]bool // List of all unique addresses collected from converted event logs
|
||||
EmittedHashes map[interface{}]bool // List of all unique hashes collected from converted event logs
|
||||
CreateAddrList bool // Whether or not to persist address list to postgres
|
||||
CreateHashList bool // Whether or not to persist hash list to postgres
|
||||
Piping bool // Whether or not to pipe method results forward as arguments to subsequent methods
|
||||
}
|
||||
|
||||
// If we will be calling methods that use addr, hash, or byte arrays
|
||||
// as arguments then we initialize maps to hold these types of values
|
||||
func (c Contract) Init() *Contract {
|
||||
for _, method := range c.Methods {
|
||||
for _, arg := range method.Args {
|
||||
switch arg.Type.T {
|
||||
case abi.AddressTy:
|
||||
c.EmittedAddrs = map[interface{}]bool{}
|
||||
case abi.HashTy, abi.BytesTy, abi.FixedBytesTy:
|
||||
c.EmittedHashes = map[interface{}]bool{}
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &c
|
||||
}
|
||||
|
||||
// Use contract info to generate event filters - full sync omni watcher only
|
||||
func (c *Contract) GenerateFilters() error {
|
||||
c.Filters = map[string]filters.LogFilter{}
|
||||
|
||||
for name, event := range c.Events {
|
||||
c.Filters[name] = filters.LogFilter{
|
||||
Name: c.Address + "_" + event.Name,
|
||||
FromBlock: c.StartingBlock,
|
||||
ToBlock: -1,
|
||||
Address: common.HexToAddress(c.Address).Hex(),
|
||||
Topics: core.Topics{event.Sig().Hex()},
|
||||
}
|
||||
}
|
||||
// If no filters were generated, throw an error (no point in continuing with this contract)
|
||||
if len(c.Filters) == 0 {
|
||||
return errors.New("error: no filters created")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns true if address is in list of arguments to
|
||||
// filter events for or if no filtering is specified
|
||||
func (c *Contract) WantedEventArg(arg string) bool {
|
||||
if c.FilterArgs == nil {
|
||||
return false
|
||||
} else if len(c.FilterArgs) == 0 {
|
||||
return true
|
||||
} else if a, ok := c.FilterArgs[arg]; ok {
|
||||
return a
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Returns true if address is in list of arguments to
|
||||
// poll methods with or if no filtering is specified
|
||||
func (c *Contract) WantedMethodArg(arg interface{}) bool {
|
||||
if c.MethodArgs == nil {
|
||||
return false
|
||||
} else if len(c.MethodArgs) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
// resolve interface to one of the three types we handle as arguments
|
||||
str := StringifyArg(arg)
|
||||
|
||||
// See if it's hex string has been filtered for
|
||||
if a, ok := c.MethodArgs[str]; ok {
|
||||
return a
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Returns true if any mapping value matches filtered for address or if no filter exists
|
||||
// Used to check if an event log name-value mapping should be filtered or not
|
||||
func (c *Contract) PassesEventFilter(args map[string]string) bool {
|
||||
for _, arg := range args {
|
||||
if c.WantedEventArg(arg) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Add event emitted address to our list if it passes filter and method polling is on
|
||||
func (c *Contract) AddEmittedAddr(addresses ...interface{}) {
|
||||
for _, addr := range addresses {
|
||||
if c.WantedMethodArg(addr) && c.Methods != nil {
|
||||
c.EmittedAddrs[addr] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add event emitted hash to our list if it passes filter and method polling is on
|
||||
func (c *Contract) AddEmittedHash(hashes ...interface{}) {
|
||||
for _, hash := range hashes {
|
||||
if c.WantedMethodArg(hash) && c.Methods != nil {
|
||||
c.EmittedHashes[hash] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func StringifyArg(arg interface{}) (str string) {
|
||||
switch arg.(type) {
|
||||
case string:
|
||||
str = arg.(string)
|
||||
case common.Address:
|
||||
a := arg.(common.Address)
|
||||
str = a.String()
|
||||
case common.Hash:
|
||||
a := arg.(common.Hash)
|
||||
str = a.String()
|
||||
case []byte:
|
||||
a := arg.([]byte)
|
||||
str = hexutil.Encode(a)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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 contract_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestContract(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Contract Suite Test")
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
})
|
||||
@@ -0,0 +1,236 @@
|
||||
// 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 contract_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers/mocks"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
)
|
||||
|
||||
var _ = Describe("Contract", func() {
|
||||
var err error
|
||||
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))
|
||||
Expect(val).To(Equal(mocks.ExpectedTransferFilter))
|
||||
|
||||
val, ok = info.Filters["Approval"]
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(val).To(Equal(mocks.ExpectedApprovalFilter))
|
||||
|
||||
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{}
|
||||
info.MethodArgs = map[string]bool{}
|
||||
info.FilterArgs = map[string]bool{}
|
||||
})
|
||||
|
||||
It("Returns true if address is in event address filter list", func() {
|
||||
info.FilterArgs["testAddress1"] = true
|
||||
info.FilterArgs["testAddress2"] = true
|
||||
|
||||
is := info.WantedEventArg("testAddress1")
|
||||
Expect(is).To(Equal(true))
|
||||
is = info.WantedEventArg("testAddress2")
|
||||
Expect(is).To(Equal(true))
|
||||
|
||||
info.MethodArgs["testAddress3"] = true
|
||||
is = info.WantedEventArg("testAddress3")
|
||||
Expect(is).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Returns true if event address filter is empty (no filter)", func() {
|
||||
is := info.WantedEventArg("testAddress1")
|
||||
Expect(is).To(Equal(true))
|
||||
is = info.WantedEventArg("testAddress2")
|
||||
Expect(is).To(Equal(true))
|
||||
})
|
||||
|
||||
It("Returns false if address is not in event address filter list", func() {
|
||||
info.FilterArgs["testAddress1"] = true
|
||||
info.FilterArgs["testAddress2"] = true
|
||||
|
||||
is := info.WantedEventArg("testAddress3")
|
||||
Expect(is).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Returns false if event address filter is nil (block all)", func() {
|
||||
info.FilterArgs = nil
|
||||
|
||||
is := info.WantedEventArg("testAddress1")
|
||||
Expect(is).To(Equal(false))
|
||||
is = info.WantedEventArg("testAddress2")
|
||||
Expect(is).To(Equal(false))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("IsMethodAddr", func() {
|
||||
BeforeEach(func() {
|
||||
info = &contract.Contract{}
|
||||
info.MethodArgs = map[string]bool{}
|
||||
info.FilterArgs = map[string]bool{}
|
||||
})
|
||||
|
||||
It("Returns true if address is in method address filter list", func() {
|
||||
info.MethodArgs["testAddress1"] = true
|
||||
info.MethodArgs["testAddress2"] = true
|
||||
|
||||
is := info.WantedMethodArg("testAddress1")
|
||||
Expect(is).To(Equal(true))
|
||||
is = info.WantedMethodArg("testAddress2")
|
||||
Expect(is).To(Equal(true))
|
||||
|
||||
info.FilterArgs["testAddress3"] = true
|
||||
is = info.WantedMethodArg("testAddress3")
|
||||
Expect(is).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Returns true if method address filter list is empty (no filter)", func() {
|
||||
is := info.WantedMethodArg("testAddress1")
|
||||
Expect(is).To(Equal(true))
|
||||
is = info.WantedMethodArg("testAddress2")
|
||||
Expect(is).To(Equal(true))
|
||||
})
|
||||
|
||||
It("Returns false if address is not in method address filter list", func() {
|
||||
info.MethodArgs["testAddress1"] = true
|
||||
info.MethodArgs["testAddress2"] = true
|
||||
|
||||
is := info.WantedMethodArg("testAddress3")
|
||||
Expect(is).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Returns false if method address filter list is nil (block all)", func() {
|
||||
info.MethodArgs = nil
|
||||
|
||||
is := info.WantedMethodArg("testAddress1")
|
||||
Expect(is).To(Equal(false))
|
||||
is = info.WantedMethodArg("testAddress2")
|
||||
Expect(is).To(Equal(false))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("PassesEventFilter", func() {
|
||||
var mapping map[string]string
|
||||
BeforeEach(func() {
|
||||
info = &contract.Contract{}
|
||||
info.FilterArgs = map[string]bool{}
|
||||
mapping = map[string]string{}
|
||||
|
||||
})
|
||||
|
||||
It("Return true if event log name-value mapping has filtered for address as a value", func() {
|
||||
info.FilterArgs["testAddress1"] = true
|
||||
info.FilterArgs["testAddress2"] = true
|
||||
|
||||
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() {
|
||||
info.FilterArgs["testAddress1"] = true
|
||||
info.FilterArgs["testAddress2"] = true
|
||||
|
||||
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() {
|
||||
info.FilterArgs = nil
|
||||
|
||||
mapping["testInputName1"] = "testAddress1"
|
||||
mapping["testInputName2"] = "testAddress2"
|
||||
mapping["testInputName3"] = "testAddress3"
|
||||
|
||||
pass := info.PassesEventFilter(mapping)
|
||||
Expect(pass).To(Equal(false))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("AddEmittedAddr", func() {
|
||||
BeforeEach(func() {
|
||||
info = &contract.Contract{}
|
||||
info.FilterArgs = map[string]bool{}
|
||||
info.MethodArgs = map[string]bool{}
|
||||
info.Methods = []types.Method{}
|
||||
info.EmittedAddrs = map[interface{}]bool{}
|
||||
})
|
||||
|
||||
It("Adds address to list if it is on the method filter address list", func() {
|
||||
info.MethodArgs["testAddress2"] = true
|
||||
info.AddEmittedAddr("testAddress2")
|
||||
b := info.EmittedAddrs["testAddress2"]
|
||||
Expect(b).To(Equal(true))
|
||||
})
|
||||
|
||||
It("Adds address to list if method filter is empty", func() {
|
||||
info.AddEmittedAddr("testAddress2")
|
||||
b := info.EmittedAddrs["testAddress2"]
|
||||
Expect(b).To(Equal(true))
|
||||
})
|
||||
|
||||
It("Does not add address to list if both filters are closed (nil)", func() {
|
||||
info.FilterArgs = nil // close both
|
||||
info.MethodArgs = nil
|
||||
info.AddEmittedAddr("testAddress1")
|
||||
b := info.EmittedAddrs["testAddress1"]
|
||||
Expect(b).To(Equal(false))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,124 @@
|
||||
// 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 fetcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
// Fetcher serves as the lower level data fetcher that calls the underlying
|
||||
// blockchain's FetchConctractData method for a given return type
|
||||
|
||||
// Interface definition for a Fetcher
|
||||
type FetcherInterface interface {
|
||||
FetchBigInt(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (big.Int, error)
|
||||
FetchBool(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (bool, error)
|
||||
FetchAddress(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Address, error)
|
||||
FetchString(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (string, error)
|
||||
FetchHash(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Hash, error)
|
||||
}
|
||||
|
||||
// Used to create a new Fetcher error for a given error and fetch method
|
||||
func newFetcherError(err error, fetchMethod string) *fetcherError {
|
||||
e := fetcherError{err.Error(), fetchMethod}
|
||||
log.Println(e.Error())
|
||||
return &e
|
||||
}
|
||||
|
||||
// Fetcher struct
|
||||
type Fetcher struct {
|
||||
BlockChain core.BlockChain // Underyling Blockchain
|
||||
}
|
||||
|
||||
// Fetcher error
|
||||
type fetcherError struct {
|
||||
err string
|
||||
fetchMethod string
|
||||
}
|
||||
|
||||
// Fetcher error method
|
||||
func (fe *fetcherError) Error() string {
|
||||
return fmt.Sprintf("Error fetching %s: %s", fe.fetchMethod, fe.err)
|
||||
}
|
||||
|
||||
// Generic Fetcher methods used by Getters to call contract methods
|
||||
|
||||
// Method used to fetch big.Int value from contract
|
||||
func (f Fetcher) FetchBigInt(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (big.Int, error) {
|
||||
var result = new(big.Int)
|
||||
err := f.BlockChain.FetchContractData(contractAbi, contractAddress, method, methodArgs, &result, blockNumber)
|
||||
|
||||
if err != nil {
|
||||
return *result, newFetcherError(err, method)
|
||||
}
|
||||
|
||||
return *result, nil
|
||||
}
|
||||
|
||||
// Method used to fetch bool value from contract
|
||||
func (f Fetcher) FetchBool(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (bool, error) {
|
||||
var result = new(bool)
|
||||
err := f.BlockChain.FetchContractData(contractAbi, contractAddress, method, methodArgs, &result, blockNumber)
|
||||
|
||||
if err != nil {
|
||||
return *result, newFetcherError(err, method)
|
||||
}
|
||||
|
||||
return *result, nil
|
||||
}
|
||||
|
||||
// Method used to fetch address value from contract
|
||||
func (f Fetcher) FetchAddress(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Address, error) {
|
||||
var result = new(common.Address)
|
||||
err := f.BlockChain.FetchContractData(contractAbi, contractAddress, method, methodArgs, &result, blockNumber)
|
||||
|
||||
if err != nil {
|
||||
return *result, newFetcherError(err, method)
|
||||
}
|
||||
|
||||
return *result, nil
|
||||
}
|
||||
|
||||
// Method used to fetch string value from contract
|
||||
func (f Fetcher) FetchString(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (string, error) {
|
||||
var result = new(string)
|
||||
err := f.BlockChain.FetchContractData(contractAbi, contractAddress, method, methodArgs, &result, blockNumber)
|
||||
|
||||
if err != nil {
|
||||
return *result, newFetcherError(err, method)
|
||||
}
|
||||
|
||||
return *result, nil
|
||||
}
|
||||
|
||||
// Method used to fetch hash value from contract
|
||||
func (f Fetcher) FetchHash(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Hash, error) {
|
||||
var result = new(common.Hash)
|
||||
err := f.BlockChain.FetchContractData(contractAbi, contractAddress, method, methodArgs, &result, blockNumber)
|
||||
|
||||
if err != nil {
|
||||
return *result, newFetcherError(err, method)
|
||||
}
|
||||
|
||||
return *result, nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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 getter_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestRepository(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Getter Suite Test")
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
})
|
||||
@@ -0,0 +1,55 @@
|
||||
// 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 getter_test
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/getter"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
|
||||
rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
|
||||
)
|
||||
|
||||
var _ = Describe("Interface Getter", func() {
|
||||
Describe("GetAbi", func() {
|
||||
It("Constructs and returns a custom abi based on results from supportsInterface calls", func() {
|
||||
expectedABI := `[` + constants.AddrChangeInterface + `,` + constants.NameChangeInterface + `,` + constants.ContentChangeInterface + `,` + constants.AbiChangeInterface + `,` + constants.PubkeyChangeInterface + `]`
|
||||
|
||||
blockNumber := int64(6885696)
|
||||
infuraIPC := "https://mainnet.infura.io/v3/b09888c1113640cc9ab42750ce750c05"
|
||||
rawRpcClient, err := rpc.Dial(infuraIPC)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
rpcClient := client.NewRpcClient(rawRpcClient, infuraIPC)
|
||||
ethClient := ethclient.NewClient(rawRpcClient)
|
||||
blockChainClient := client.NewEthClient(ethClient)
|
||||
node := node.MakeNode(rpcClient)
|
||||
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
|
||||
blockChain := geth.NewBlockChain(blockChainClient, rpcClient, node, transactionConverter)
|
||||
interfaceGetter := getter.NewInterfaceGetter(blockChain)
|
||||
abi := interfaceGetter.GetABI(constants.PublicResolverAddress, blockNumber)
|
||||
Expect(abi).To(Equal(expectedABI))
|
||||
_, err = geth.ParseAbi(abi)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,105 @@
|
||||
// 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 getter
|
||||
|
||||
import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/fetcher"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
type InterfaceGetter interface {
|
||||
GetABI(resolverAddr string, blockNumber int64) string
|
||||
GetBlockChain() core.BlockChain
|
||||
}
|
||||
|
||||
type interfaceGetter struct {
|
||||
fetcher.Fetcher
|
||||
}
|
||||
|
||||
func NewInterfaceGetter(blockChain core.BlockChain) *interfaceGetter {
|
||||
return &interfaceGetter{
|
||||
Fetcher: fetcher.Fetcher{
|
||||
BlockChain: blockChain,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Used to construct a custom ABI based on the results from calling supportsInterface
|
||||
func (g *interfaceGetter) GetABI(resolverAddr string, blockNumber int64) string {
|
||||
a := constants.SupportsInterfaceABI
|
||||
args := make([]interface{}, 1)
|
||||
args[0] = constants.MetaSig.Bytes()
|
||||
supports, err := g.getSupportsInterface(a, resolverAddr, blockNumber, args)
|
||||
if err != nil || !supports {
|
||||
return ""
|
||||
}
|
||||
abiStr := `[`
|
||||
args[0] = constants.AddrChangeSig.Bytes()
|
||||
supports, err = g.getSupportsInterface(a, resolverAddr, blockNumber, args)
|
||||
if err == nil && supports {
|
||||
abiStr += constants.AddrChangeInterface + ","
|
||||
}
|
||||
args[0] = constants.NameChangeSig.Bytes()
|
||||
supports, err = g.getSupportsInterface(a, resolverAddr, blockNumber, args)
|
||||
if err == nil && supports {
|
||||
abiStr += constants.NameChangeInterface + ","
|
||||
}
|
||||
args[0] = constants.ContentChangeSig.Bytes()
|
||||
supports, err = g.getSupportsInterface(a, resolverAddr, blockNumber, args)
|
||||
if err == nil && supports {
|
||||
abiStr += constants.ContentChangeInterface + ","
|
||||
}
|
||||
args[0] = constants.AbiChangeSig.Bytes()
|
||||
supports, err = g.getSupportsInterface(a, resolverAddr, blockNumber, args)
|
||||
if err == nil && supports {
|
||||
abiStr += constants.AbiChangeInterface + ","
|
||||
}
|
||||
args[0] = constants.PubkeyChangeSig.Bytes()
|
||||
supports, err = g.getSupportsInterface(a, resolverAddr, blockNumber, args)
|
||||
if err == nil && supports {
|
||||
abiStr += constants.PubkeyChangeInterface + ","
|
||||
}
|
||||
args[0] = constants.ContentHashChangeSig.Bytes()
|
||||
supports, err = g.getSupportsInterface(a, resolverAddr, blockNumber, args)
|
||||
if err == nil && supports {
|
||||
abiStr += constants.ContenthashChangeInterface + ","
|
||||
}
|
||||
args[0] = constants.MultihashChangeSig.Bytes()
|
||||
supports, err = g.getSupportsInterface(a, resolverAddr, blockNumber, args)
|
||||
if err == nil && supports {
|
||||
abiStr += constants.MultihashChangeInterface + ","
|
||||
}
|
||||
args[0] = constants.TextChangeSig.Bytes()
|
||||
supports, err = g.getSupportsInterface(a, resolverAddr, blockNumber, args)
|
||||
if err == nil && supports {
|
||||
abiStr += constants.TextChangeInterface + ","
|
||||
}
|
||||
abiStr = abiStr[:len(abiStr)-1] + `]`
|
||||
|
||||
return abiStr
|
||||
}
|
||||
|
||||
// Use this method to check whether or not a contract supports a given method/event interface
|
||||
func (g *interfaceGetter) getSupportsInterface(contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (bool, error) {
|
||||
return g.Fetcher.FetchBool("supportsInterface", contractAbi, contractAddress, blockNumber, methodArgs)
|
||||
}
|
||||
|
||||
// Method to retrieve the Getter's blockchain
|
||||
func (g *interfaceGetter) GetBlockChain() core.BlockChain {
|
||||
return g.Fetcher.BlockChain
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// 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 helpers
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
func ConvertToLog(watchedEvent core.WatchedEvent) types.Log {
|
||||
allTopics := []string{watchedEvent.Topic0, watchedEvent.Topic1, watchedEvent.Topic2, watchedEvent.Topic3}
|
||||
var nonNilTopics []string
|
||||
for _, topic := range allTopics {
|
||||
if topic != "" {
|
||||
nonNilTopics = append(nonNilTopics, topic)
|
||||
}
|
||||
}
|
||||
return types.Log{
|
||||
Address: common.HexToAddress(watchedEvent.Address),
|
||||
Topics: createTopics(nonNilTopics...),
|
||||
Data: hexutil.MustDecode(watchedEvent.Data),
|
||||
BlockNumber: uint64(watchedEvent.BlockNumber),
|
||||
TxHash: common.HexToHash(watchedEvent.TxHash),
|
||||
TxIndex: 0,
|
||||
BlockHash: common.HexToHash("0x0"),
|
||||
Index: uint(watchedEvent.Index),
|
||||
Removed: false,
|
||||
}
|
||||
}
|
||||
|
||||
func createTopics(topics ...string) []common.Hash {
|
||||
var topicsArray []common.Hash
|
||||
for _, topic := range topics {
|
||||
topicsArray = append(topicsArray, common.HexToHash(topic))
|
||||
}
|
||||
return topicsArray
|
||||
}
|
||||
|
||||
func BigFromString(n string) *big.Int {
|
||||
b := new(big.Int)
|
||||
b.SetString(n, 10)
|
||||
return b
|
||||
}
|
||||
|
||||
func GenerateSignature(s string) string {
|
||||
eventSignature := []byte(s)
|
||||
hash := crypto.Keccak256Hash(eventSignature)
|
||||
return hash.Hex()
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
// 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 test_helpers
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/config"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers/mocks"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
|
||||
rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
|
||||
)
|
||||
|
||||
type TransferLog struct {
|
||||
Id int64 `db:"id"`
|
||||
VulvanizeLogId int64 `db:"vulcanize_log_id"`
|
||||
TokenName string `db:"token_name"`
|
||||
Block int64 `db:"block"`
|
||||
Tx string `db:"tx"`
|
||||
From string `db:"from_"`
|
||||
To string `db:"to_"`
|
||||
Value string `db:"value_"`
|
||||
}
|
||||
|
||||
type NewOwnerLog struct {
|
||||
Id int64 `db:"id"`
|
||||
VulvanizeLogId int64 `db:"vulcanize_log_id"`
|
||||
TokenName string `db:"token_name"`
|
||||
Block int64 `db:"block"`
|
||||
Tx string `db:"tx"`
|
||||
Node string `db:"node_"`
|
||||
Label string `db:"label_"`
|
||||
Owner string `db:"owner_"`
|
||||
}
|
||||
|
||||
type LightTransferLog struct {
|
||||
Id int64 `db:"id"`
|
||||
HeaderID int64 `db:"header_id"`
|
||||
TokenName string `db:"token_name"`
|
||||
LogIndex int64 `db:"log_idx"`
|
||||
TxIndex int64 `db:"tx_idx"`
|
||||
From string `db:"from_"`
|
||||
To string `db:"to_"`
|
||||
Value string `db:"value_"`
|
||||
RawLog []byte `db:"raw_log"`
|
||||
}
|
||||
|
||||
type LightNewOwnerLog struct {
|
||||
Id int64 `db:"id"`
|
||||
HeaderID int64 `db:"header_id"`
|
||||
TokenName string `db:"token_name"`
|
||||
LogIndex int64 `db:"log_idx"`
|
||||
TxIndex int64 `db:"tx_idx"`
|
||||
Node string `db:"node_"`
|
||||
Label string `db:"label_"`
|
||||
Owner string `db:"owner_"`
|
||||
RawLog []byte `db:"raw_log"`
|
||||
}
|
||||
|
||||
type BalanceOf struct {
|
||||
Id int64 `db:"id"`
|
||||
TokenName string `db:"token_name"`
|
||||
Block int64 `db:"block"`
|
||||
Address string `db:"who_"`
|
||||
Balance string `db:"returned"`
|
||||
}
|
||||
|
||||
type Resolver struct {
|
||||
Id int64 `db:"id"`
|
||||
TokenName string `db:"token_name"`
|
||||
Block int64 `db:"block"`
|
||||
Node string `db:"node_"`
|
||||
Address string `db:"returned"`
|
||||
}
|
||||
|
||||
type Owner struct {
|
||||
Id int64 `db:"id"`
|
||||
TokenName string `db:"token_name"`
|
||||
Block int64 `db:"block"`
|
||||
Node string `db:"node_"`
|
||||
Address string `db:"returned"`
|
||||
}
|
||||
|
||||
func SetupBC() core.BlockChain {
|
||||
infuraIPC := "https://mainnet.infura.io/v3/b09888c1113640cc9ab42750ce750c05"
|
||||
rawRpcClient, err := rpc.Dial(infuraIPC)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
rpcClient := client.NewRpcClient(rawRpcClient, infuraIPC)
|
||||
ethClient := ethclient.NewClient(rawRpcClient)
|
||||
blockChainClient := client.NewEthClient(ethClient)
|
||||
node := node.MakeNode(rpcClient)
|
||||
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
|
||||
blockChain := geth.NewBlockChain(blockChainClient, rpcClient, node, transactionConverter)
|
||||
|
||||
return blockChain
|
||||
}
|
||||
|
||||
func SetupDBandBC() (*postgres.DB, core.BlockChain) {
|
||||
infuraIPC := "https://mainnet.infura.io/v3/b09888c1113640cc9ab42750ce750c05"
|
||||
rawRpcClient, err := rpc.Dial(infuraIPC)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
rpcClient := client.NewRpcClient(rawRpcClient, infuraIPC)
|
||||
ethClient := ethclient.NewClient(rawRpcClient)
|
||||
blockChainClient := client.NewEthClient(ethClient)
|
||||
node := node.MakeNode(rpcClient)
|
||||
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
|
||||
blockChain := geth.NewBlockChain(blockChainClient, rpcClient, node, transactionConverter)
|
||||
|
||||
db, err := postgres.NewDB(config.Database{
|
||||
Hostname: "localhost",
|
||||
Name: "vulcanize_private",
|
||||
Port: 5432,
|
||||
}, blockChain.Node())
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
return db, blockChain
|
||||
}
|
||||
|
||||
func SetupTusdRepo(vulcanizeLogId *int64, wantedEvents, wantedMethods []string) (*postgres.DB, *contract.Contract) {
|
||||
db, err := postgres.NewDB(config.Database{
|
||||
Hostname: "localhost",
|
||||
Name: "vulcanize_private",
|
||||
Port: 5432,
|
||||
}, core.Node{})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
receiptRepository := repositories.ReceiptRepository{DB: db}
|
||||
logRepository := repositories.LogRepository{DB: db}
|
||||
blockRepository := *repositories.NewBlockRepository(db)
|
||||
|
||||
blockNumber := rand.Int63()
|
||||
blockId := CreateBlock(blockNumber, blockRepository)
|
||||
|
||||
receipts := []core.Receipt{{Logs: []core.Log{{}}}}
|
||||
|
||||
err = receiptRepository.CreateReceiptsAndLogs(blockId, receipts)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
err = logRepository.Get(vulcanizeLogId, `SELECT id FROM logs`)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
info := SetupTusdContract(wantedEvents, wantedMethods)
|
||||
|
||||
return db, info
|
||||
}
|
||||
|
||||
func SetupTusdContract(wantedEvents, wantedMethods []string) *contract.Contract {
|
||||
p := mocks.NewParser(constants.TusdAbiString)
|
||||
err := p.Parse()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
return contract.Contract{
|
||||
Name: "TrueUSD",
|
||||
Address: constants.TusdContractAddress,
|
||||
Abi: p.Abi(),
|
||||
ParsedAbi: p.ParsedAbi(),
|
||||
StartingBlock: 6194634,
|
||||
LastBlock: 6507323,
|
||||
Events: p.GetEvents(wantedEvents),
|
||||
Methods: p.GetSelectMethods(wantedMethods),
|
||||
MethodArgs: map[string]bool{},
|
||||
FilterArgs: map[string]bool{},
|
||||
}.Init()
|
||||
}
|
||||
|
||||
func SetupENSRepo(vulcanizeLogId *int64, wantedEvents, wantedMethods []string) (*postgres.DB, *contract.Contract) {
|
||||
db, err := postgres.NewDB(config.Database{
|
||||
Hostname: "localhost",
|
||||
Name: "vulcanize_private",
|
||||
Port: 5432,
|
||||
}, core.Node{})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
receiptRepository := repositories.ReceiptRepository{DB: db}
|
||||
logRepository := repositories.LogRepository{DB: db}
|
||||
blockRepository := *repositories.NewBlockRepository(db)
|
||||
|
||||
blockNumber := rand.Int63()
|
||||
blockId := CreateBlock(blockNumber, blockRepository)
|
||||
|
||||
receipts := []core.Receipt{{Logs: []core.Log{{}}}}
|
||||
|
||||
err = receiptRepository.CreateReceiptsAndLogs(blockId, receipts)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
err = logRepository.Get(vulcanizeLogId, `SELECT id FROM logs`)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
info := SetupENSContract(wantedEvents, wantedMethods)
|
||||
|
||||
return db, info
|
||||
}
|
||||
|
||||
func SetupENSContract(wantedEvents, wantedMethods []string) *contract.Contract {
|
||||
p := mocks.NewParser(constants.ENSAbiString)
|
||||
err := p.Parse()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
return contract.Contract{
|
||||
Name: "ENS-Registry",
|
||||
Address: constants.EnsContractAddress,
|
||||
Abi: p.Abi(),
|
||||
ParsedAbi: p.ParsedAbi(),
|
||||
StartingBlock: 6194634,
|
||||
LastBlock: 6507323,
|
||||
Events: p.GetEvents(wantedEvents),
|
||||
Methods: p.GetSelectMethods(wantedMethods),
|
||||
MethodArgs: map[string]bool{},
|
||||
FilterArgs: map[string]bool{},
|
||||
}.Init()
|
||||
}
|
||||
|
||||
func TearDown(db *postgres.DB) {
|
||||
tx, err := db.Begin()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DELETE FROM blocks`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DELETE FROM headers`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DELETE FROM logs`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DELETE FROM log_filters`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DELETE FROM transactions`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DELETE FROM receipts`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DROP TABLE checked_headers`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`CREATE TABLE checked_headers (id SERIAL PRIMARY KEY, header_id INTEGER UNIQUE NOT NULL REFERENCES headers (id) ON DELETE CASCADE);`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DROP SCHEMA IF EXISTS full_0x8dd5fbce2f6a956c3022ba3663759011dd51e73e CASCADE`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DROP SCHEMA IF EXISTS light_0x8dd5fbce2f6a956c3022ba3663759011dd51e73e CASCADE`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DROP SCHEMA IF EXISTS full_0x314159265dd8dbb310642f98f50c066173c1259b CASCADE`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DROP SCHEMA IF EXISTS light_0x314159265dd8dbb310642f98f50c066173c1259b CASCADE`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = tx.Commit()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = db.Exec(`VACUUM checked_headers`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
func CreateBlock(blockNumber int64, repository repositories.BlockRepository) (blockId int64) {
|
||||
blockId, err := repository.CreateOrUpdateBlock(core.Block{Number: blockNumber})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
return blockId
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
// 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 mocks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/config"
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/filters"
|
||||
)
|
||||
|
||||
var TransferBlock1 = core.Block{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad123ert",
|
||||
Number: 6194633,
|
||||
Transactions: []core.Transaction{{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654aaa",
|
||||
Receipt: core.Receipt{
|
||||
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654aaa",
|
||||
ContractAddress: "",
|
||||
Logs: []core.Log{{
|
||||
BlockNumber: 6194633,
|
||||
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654aaa",
|
||||
Address: constants.TusdContractAddress,
|
||||
Topics: core.Topics{
|
||||
constants.TransferEvent.Signature(),
|
||||
"0x000000000000000000000000000000000000000000000000000000000000af21",
|
||||
"0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391",
|
||||
"",
|
||||
},
|
||||
Index: 1,
|
||||
Data: "0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe",
|
||||
}},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
var TransferBlock2 = core.Block{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad123ooo",
|
||||
Number: 6194634,
|
||||
Transactions: []core.Transaction{{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654eee",
|
||||
Receipt: core.Receipt{
|
||||
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654eee",
|
||||
ContractAddress: "",
|
||||
Logs: []core.Log{{
|
||||
BlockNumber: 6194634,
|
||||
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654eee",
|
||||
Address: constants.TusdContractAddress,
|
||||
Topics: core.Topics{
|
||||
constants.TransferEvent.Signature(),
|
||||
"0x000000000000000000000000000000000000000000000000000000000000af21",
|
||||
"0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391",
|
||||
"",
|
||||
},
|
||||
Index: 1,
|
||||
Data: "0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe",
|
||||
}},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
var NewOwnerBlock1 = core.Block{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad123ppp",
|
||||
Number: 6194635,
|
||||
Transactions: []core.Transaction{{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654bbb",
|
||||
Receipt: core.Receipt{
|
||||
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654bbb",
|
||||
ContractAddress: "",
|
||||
Logs: []core.Log{{
|
||||
BlockNumber: 6194635,
|
||||
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654bbb",
|
||||
Address: constants.EnsContractAddress,
|
||||
Topics: core.Topics{
|
||||
constants.NewOwnerEvent.Signature(),
|
||||
"0x0000000000000000000000000000000000000000000000000000c02aaa39b223",
|
||||
"0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391",
|
||||
"",
|
||||
},
|
||||
Index: 1,
|
||||
Data: "0x000000000000000000000000000000000000000000000000000000000000af21",
|
||||
}},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
var NewOwnerBlock2 = core.Block{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad123ggg",
|
||||
Number: 6194636,
|
||||
Transactions: []core.Transaction{{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654lll",
|
||||
Receipt: core.Receipt{
|
||||
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654lll",
|
||||
ContractAddress: "",
|
||||
Logs: []core.Log{{
|
||||
BlockNumber: 6194636,
|
||||
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad654lll",
|
||||
Address: constants.EnsContractAddress,
|
||||
Topics: core.Topics{
|
||||
constants.NewOwnerEvent.Signature(),
|
||||
"0x0000000000000000000000000000000000000000000000000000c02aaa39b223",
|
||||
"0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba400",
|
||||
"",
|
||||
},
|
||||
Index: 1,
|
||||
Data: "0x000000000000000000000000000000000000000000000000000000000000af21",
|
||||
}},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
var ExpectedTransferFilter = filters.LogFilter{
|
||||
Name: constants.TusdContractAddress + "_" + "Transfer",
|
||||
Address: constants.TusdContractAddress,
|
||||
ToBlock: -1,
|
||||
FromBlock: 6194634,
|
||||
Topics: core.Topics{constants.TransferEvent.Signature()},
|
||||
}
|
||||
|
||||
var ExpectedApprovalFilter = filters.LogFilter{
|
||||
Name: constants.TusdContractAddress + "_" + "Approval",
|
||||
Address: constants.TusdContractAddress,
|
||||
ToBlock: -1,
|
||||
FromBlock: 6194634,
|
||||
Topics: core.Topics{constants.ApprovalEvent.Signature()},
|
||||
}
|
||||
|
||||
var MockTranferEvent = core.WatchedEvent{
|
||||
LogID: 1,
|
||||
Name: constants.TransferEvent.String(),
|
||||
BlockNumber: 5488076,
|
||||
Address: constants.TusdContractAddress,
|
||||
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
|
||||
Index: 110,
|
||||
Topic0: constants.TransferEvent.Signature(),
|
||||
Topic1: "0x000000000000000000000000000000000000000000000000000000000000af21",
|
||||
Topic2: "0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391",
|
||||
Topic3: "",
|
||||
Data: "0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe",
|
||||
}
|
||||
|
||||
var rawFakeHeader, _ = json.Marshal(core.Header{})
|
||||
|
||||
var MockHeader1 = core.Header{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad123ert",
|
||||
BlockNumber: 6194632,
|
||||
Raw: rawFakeHeader,
|
||||
Timestamp: "50000000",
|
||||
}
|
||||
|
||||
var MockHeader2 = core.Header{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad456yui",
|
||||
BlockNumber: 6194633,
|
||||
Raw: rawFakeHeader,
|
||||
Timestamp: "50000015",
|
||||
}
|
||||
|
||||
var MockHeader3 = core.Header{
|
||||
Hash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad234hfs",
|
||||
BlockNumber: 6194634,
|
||||
Raw: rawFakeHeader,
|
||||
Timestamp: "50000030",
|
||||
}
|
||||
|
||||
var MockTransferLog1 = types.Log{
|
||||
Index: 1,
|
||||
Address: common.HexToAddress(constants.TusdContractAddress),
|
||||
BlockNumber: 5488076,
|
||||
TxIndex: 110,
|
||||
TxHash: common.HexToHash("0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae"),
|
||||
Topics: []common.Hash{
|
||||
common.HexToHash(constants.TransferEvent.Signature()),
|
||||
common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000af21"),
|
||||
common.HexToHash("0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391"),
|
||||
},
|
||||
Data: hexutil.MustDecode("0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe"),
|
||||
}
|
||||
|
||||
var MockTransferLog2 = types.Log{
|
||||
Index: 3,
|
||||
Address: common.HexToAddress(constants.TusdContractAddress),
|
||||
BlockNumber: 5488077,
|
||||
TxIndex: 2,
|
||||
TxHash: common.HexToHash("0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546df"),
|
||||
Topics: []common.Hash{
|
||||
common.HexToHash(constants.TransferEvent.Signature()),
|
||||
common.HexToHash("0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391"),
|
||||
common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000af21"),
|
||||
},
|
||||
Data: hexutil.MustDecode("0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe"),
|
||||
}
|
||||
|
||||
var MockMintLog = types.Log{
|
||||
Index: 10,
|
||||
Address: common.HexToAddress(constants.TusdContractAddress),
|
||||
BlockNumber: 5488080,
|
||||
TxIndex: 50,
|
||||
TxHash: common.HexToHash("0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6minty"),
|
||||
Topics: []common.Hash{
|
||||
common.HexToHash(constants.MintEvent.Signature()),
|
||||
common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000af21"),
|
||||
},
|
||||
Data: hexutil.MustDecode("0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe"),
|
||||
}
|
||||
|
||||
var MockNewOwnerLog1 = types.Log{
|
||||
Index: 1,
|
||||
Address: common.HexToAddress(constants.EnsContractAddress),
|
||||
BlockNumber: 5488076,
|
||||
TxIndex: 110,
|
||||
TxHash: common.HexToHash("0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae"),
|
||||
Topics: []common.Hash{
|
||||
common.HexToHash(constants.NewOwnerEvent.Signature()),
|
||||
common.HexToHash("0x000000000000000000000000c02aaa39b223helloa0e5c4f27ead9083c752553"),
|
||||
common.HexToHash("0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391"),
|
||||
},
|
||||
Data: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000af21"),
|
||||
}
|
||||
|
||||
var MockNewOwnerLog2 = types.Log{
|
||||
Index: 3,
|
||||
Address: common.HexToAddress(constants.EnsContractAddress),
|
||||
BlockNumber: 5488077,
|
||||
TxIndex: 2,
|
||||
TxHash: common.HexToHash("0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546df"),
|
||||
Topics: []common.Hash{
|
||||
common.HexToHash(constants.NewOwnerEvent.Signature()),
|
||||
common.HexToHash("0x000000000000000000000000c02aaa39b223helloa0e5c4f27ead9083c752553"),
|
||||
common.HexToHash("0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba400"),
|
||||
},
|
||||
Data: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000af21"),
|
||||
}
|
||||
|
||||
var ens = strings.ToLower(constants.EnsContractAddress)
|
||||
var tusd = strings.ToLower(constants.TusdContractAddress)
|
||||
|
||||
var TusdConfig = config.ContractConfig{
|
||||
Network: "",
|
||||
Addresses: map[string]bool{
|
||||
tusd: true,
|
||||
},
|
||||
Abis: map[string]string{
|
||||
tusd: "",
|
||||
},
|
||||
Events: map[string][]string{
|
||||
tusd: []string{"Transfer"},
|
||||
},
|
||||
Methods: map[string][]string{
|
||||
tusd: nil,
|
||||
},
|
||||
MethodArgs: map[string][]string{
|
||||
tusd: nil,
|
||||
},
|
||||
EventArgs: map[string][]string{
|
||||
tusd: nil,
|
||||
},
|
||||
}
|
||||
|
||||
var ENSConfig = config.ContractConfig{
|
||||
Network: "",
|
||||
Addresses: map[string]bool{
|
||||
ens: true,
|
||||
},
|
||||
Abis: map[string]string{
|
||||
ens: "",
|
||||
},
|
||||
Events: map[string][]string{
|
||||
ens: []string{"NewOwner"},
|
||||
},
|
||||
Methods: map[string][]string{
|
||||
ens: nil,
|
||||
},
|
||||
MethodArgs: map[string][]string{
|
||||
ens: nil,
|
||||
},
|
||||
EventArgs: map[string][]string{
|
||||
ens: nil,
|
||||
},
|
||||
}
|
||||
|
||||
var ENSandTusdConfig = config.ContractConfig{
|
||||
Network: "",
|
||||
Addresses: map[string]bool{
|
||||
ens: true,
|
||||
tusd: true,
|
||||
},
|
||||
Abis: map[string]string{
|
||||
ens: "",
|
||||
tusd: "",
|
||||
},
|
||||
Events: map[string][]string{
|
||||
ens: []string{"NewOwner"},
|
||||
tusd: []string{"Transfer"},
|
||||
},
|
||||
Methods: map[string][]string{
|
||||
ens: nil,
|
||||
tusd: nil,
|
||||
},
|
||||
MethodArgs: map[string][]string{
|
||||
ens: nil,
|
||||
tusd: nil,
|
||||
},
|
||||
EventArgs: map[string][]string{
|
||||
ens: nil,
|
||||
tusd: nil,
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
// 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 mocks
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
||||
)
|
||||
|
||||
// Mock parser
|
||||
// Is given ABI string instead of address
|
||||
// Performs all other functions of the real parser
|
||||
type parser struct {
|
||||
abi string
|
||||
parsedAbi abi.ABI
|
||||
}
|
||||
|
||||
func NewParser(abi string) *parser {
|
||||
|
||||
return &parser{
|
||||
abi: abi,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *parser) Abi() string {
|
||||
return p.abi
|
||||
}
|
||||
|
||||
func (p *parser) ParsedAbi() abi.ABI {
|
||||
return p.parsedAbi
|
||||
}
|
||||
|
||||
// Retrieves and parses the abi string
|
||||
// for the given contract address
|
||||
func (p *parser) Parse() error {
|
||||
var err error
|
||||
p.parsedAbi, err = geth.ParseAbi(p.abi)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Returns only specified methods, if they meet the criteria
|
||||
// Returns as array with methods in same order they were specified
|
||||
// Nil wanted array => no events are returned
|
||||
func (p *parser) GetSelectMethods(wanted []string) []types.Method {
|
||||
wLen := len(wanted)
|
||||
if wLen == 0 {
|
||||
return nil
|
||||
}
|
||||
methods := make([]types.Method, wLen)
|
||||
for _, m := range p.parsedAbi.Methods {
|
||||
for i, name := range wanted {
|
||||
if name == m.Name && okTypes(m, wanted) {
|
||||
methods[i] = types.NewMethod(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return methods
|
||||
}
|
||||
|
||||
// Returns wanted methods
|
||||
// Empty wanted array => all methods are returned
|
||||
// Nil wanted array => no methods are returned
|
||||
func (p *parser) GetMethods(wanted []string) []types.Method {
|
||||
if wanted == nil {
|
||||
return nil
|
||||
}
|
||||
methods := make([]types.Method, 0)
|
||||
length := len(wanted)
|
||||
for _, m := range p.parsedAbi.Methods {
|
||||
if length == 0 || stringInSlice(wanted, m.Name) {
|
||||
methods = append(methods, types.NewMethod(m))
|
||||
}
|
||||
}
|
||||
|
||||
return methods
|
||||
}
|
||||
|
||||
// Returns wanted events as map of types.Events
|
||||
// If no events are specified, all events are returned
|
||||
func (p *parser) GetEvents(wanted []string) map[string]types.Event {
|
||||
events := map[string]types.Event{}
|
||||
|
||||
for _, e := range p.parsedAbi.Events {
|
||||
if len(wanted) == 0 || stringInSlice(wanted, e.Name) {
|
||||
event := types.NewEvent(e)
|
||||
events[e.Name] = event
|
||||
}
|
||||
}
|
||||
|
||||
return events
|
||||
}
|
||||
|
||||
func stringInSlice(list []string, s string) bool {
|
||||
for _, b := range list {
|
||||
if b == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func okTypes(m abi.Method, wanted []string) bool {
|
||||
// Only return method if it has less than 3 arguments, a single output value, and it is a method we want or we want all methods (empty 'wanted' slice)
|
||||
if len(m.Inputs) < 3 && len(m.Outputs) == 1 && (len(wanted) == 0 || stringInSlice(wanted, m.Name)) {
|
||||
// Only return methods if inputs are all of accepted types and output is of the accepted types
|
||||
if !okReturnType(m.Outputs[0]) {
|
||||
return false
|
||||
}
|
||||
for _, input := range m.Inputs {
|
||||
switch input.Type.T {
|
||||
case abi.AddressTy, abi.HashTy, abi.BytesTy, abi.FixedBytesTy:
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func okReturnType(arg abi.Argument) bool {
|
||||
wantedTypes := []byte{
|
||||
abi.UintTy,
|
||||
abi.IntTy,
|
||||
abi.BoolTy,
|
||||
abi.StringTy,
|
||||
abi.AddressTy,
|
||||
abi.HashTy,
|
||||
abi.BytesTy,
|
||||
abi.FixedBytesTy,
|
||||
abi.FixedPointTy,
|
||||
}
|
||||
|
||||
for _, ty := range wantedTypes {
|
||||
if arg.Type.T == ty {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
// 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 parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
||||
)
|
||||
|
||||
// Parser is used to fetch and parse contract ABIs
|
||||
// It is dependent on etherscan's api
|
||||
type Parser interface {
|
||||
Parse(contractAddr string) error
|
||||
ParseAbiStr(abiStr string) error
|
||||
Abi() string
|
||||
ParsedAbi() abi.ABI
|
||||
GetMethods(wanted []string) []types.Method
|
||||
GetSelectMethods(wanted []string) []types.Method
|
||||
GetEvents(wanted []string) map[string]types.Event
|
||||
}
|
||||
|
||||
type parser struct {
|
||||
client *geth.EtherScanAPI
|
||||
abi string
|
||||
parsedAbi abi.ABI
|
||||
}
|
||||
|
||||
func NewParser(network string) *parser {
|
||||
url := geth.GenURL(network)
|
||||
|
||||
return &parser{
|
||||
client: geth.NewEtherScanClient(url),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *parser) Abi() string {
|
||||
return p.abi
|
||||
}
|
||||
|
||||
func (p *parser) ParsedAbi() abi.ABI {
|
||||
return p.parsedAbi
|
||||
}
|
||||
|
||||
// Retrieves and parses the abi string
|
||||
// for the given contract address
|
||||
func (p *parser) Parse(contractAddr string) error {
|
||||
// If the abi is one our locally stored abis, fetch
|
||||
// TODO: Allow users to pass abis through config
|
||||
knownAbi, err := p.lookUp(contractAddr)
|
||||
if err == nil {
|
||||
p.abi = knownAbi
|
||||
p.parsedAbi, err = geth.ParseAbi(knownAbi)
|
||||
return err
|
||||
}
|
||||
// Try getting abi from etherscan
|
||||
abiStr, err := p.client.GetAbi(contractAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//TODO: Implement other ways to fetch abi
|
||||
p.abi = abiStr
|
||||
p.parsedAbi, err = geth.ParseAbi(abiStr)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Loads and parses an abi from a given abi string
|
||||
func (p *parser) ParseAbiStr(abiStr string) error {
|
||||
var err error
|
||||
p.abi = abiStr
|
||||
p.parsedAbi, err = geth.ParseAbi(abiStr)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *parser) lookUp(contractAddr string) (string, error) {
|
||||
if v, ok := constants.Abis[common.HexToAddress(contractAddr)]; ok {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
return "", errors.New("ABI not present in lookup tabe")
|
||||
}
|
||||
|
||||
// Returns only specified methods, if they meet the criteria
|
||||
// Returns as array with methods in same order they were specified
|
||||
// Nil or empty wanted array => no events are returned
|
||||
func (p *parser) GetSelectMethods(wanted []string) []types.Method {
|
||||
wLen := len(wanted)
|
||||
if wLen == 0 {
|
||||
return nil
|
||||
}
|
||||
methods := make([]types.Method, wLen)
|
||||
for _, m := range p.parsedAbi.Methods {
|
||||
for i, name := range wanted {
|
||||
if name == m.Name && okTypes(m, wanted) {
|
||||
methods[i] = types.NewMethod(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return methods
|
||||
}
|
||||
|
||||
// Returns wanted methods
|
||||
// Empty wanted array => all methods are returned
|
||||
// Nil wanted array => no methods are returned
|
||||
func (p *parser) GetMethods(wanted []string) []types.Method {
|
||||
if wanted == nil {
|
||||
return nil
|
||||
}
|
||||
methods := make([]types.Method, 0)
|
||||
length := len(wanted)
|
||||
for _, m := range p.parsedAbi.Methods {
|
||||
if length == 0 || stringInSlice(wanted, m.Name) {
|
||||
methods = append(methods, types.NewMethod(m))
|
||||
}
|
||||
}
|
||||
|
||||
return methods
|
||||
}
|
||||
|
||||
// Returns wanted events as map of types.Events
|
||||
// Empty wanted array => all events are returned
|
||||
// Nil wanted array => no events are returned
|
||||
func (p *parser) GetEvents(wanted []string) map[string]types.Event {
|
||||
events := map[string]types.Event{}
|
||||
if wanted == nil {
|
||||
return events
|
||||
}
|
||||
|
||||
length := len(wanted)
|
||||
for _, e := range p.parsedAbi.Events {
|
||||
if length == 0 || stringInSlice(wanted, e.Name) {
|
||||
events[e.Name] = types.NewEvent(e)
|
||||
}
|
||||
}
|
||||
|
||||
return events
|
||||
}
|
||||
|
||||
func okReturnType(arg abi.Argument) bool {
|
||||
wantedTypes := []byte{
|
||||
abi.UintTy,
|
||||
abi.IntTy,
|
||||
abi.BoolTy,
|
||||
abi.StringTy,
|
||||
abi.AddressTy,
|
||||
abi.HashTy,
|
||||
abi.BytesTy,
|
||||
abi.FixedBytesTy,
|
||||
abi.FixedPointTy,
|
||||
}
|
||||
|
||||
for _, ty := range wantedTypes {
|
||||
if arg.Type.T == ty {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func okTypes(m abi.Method, wanted []string) bool {
|
||||
// Only return method if it has less than 3 arguments, a single output value, and it is a method we want or we want all methods (empty 'wanted' slice)
|
||||
if len(m.Inputs) < 3 && len(m.Outputs) == 1 && (len(wanted) == 0 || stringInSlice(wanted, m.Name)) {
|
||||
// Only return methods if inputs are all of accepted types and output is of the accepted types
|
||||
if !okReturnType(m.Outputs[0]) {
|
||||
return false
|
||||
}
|
||||
for _, input := range m.Inputs {
|
||||
switch input.Type.T {
|
||||
// Addresses are properly labeled and caught
|
||||
// But hashes tend to not be explicitly labeled and caught
|
||||
// Instead bytes32 are assumed to be hashes
|
||||
case abi.AddressTy, abi.HashTy:
|
||||
case abi.FixedBytesTy:
|
||||
if input.Type.Size != 32 {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func stringInSlice(list []string, s string) bool {
|
||||
for _, b := range list {
|
||||
if b == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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 parser_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestParser(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Parser Suite Test")
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
})
|
||||
@@ -0,0 +1,226 @@
|
||||
// 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 parser_test
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers/mocks"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/parser"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
||||
)
|
||||
|
||||
var _ = Describe("Parser", func() {
|
||||
|
||||
var p parser.Parser
|
||||
var err error
|
||||
|
||||
BeforeEach(func() {
|
||||
p = parser.NewParser("")
|
||||
})
|
||||
|
||||
Describe("Mock Parse", func() {
|
||||
It("Uses parses given abi string", func() {
|
||||
mp := mocks.NewParser(constants.DaiAbiString)
|
||||
err = mp.Parse()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
parsedAbi := mp.ParsedAbi()
|
||||
expectedAbi, err := geth.ParseAbi(constants.DaiAbiString)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(parsedAbi).To(Equal(expectedAbi))
|
||||
|
||||
methods := mp.GetSelectMethods([]string{"balanceOf"})
|
||||
Expect(len(methods)).To(Equal(1))
|
||||
balOf := methods[0]
|
||||
Expect(balOf.Name).To(Equal("balanceOf"))
|
||||
Expect(len(balOf.Args)).To(Equal(1))
|
||||
Expect(len(balOf.Return)).To(Equal(1))
|
||||
|
||||
events := mp.GetEvents([]string{"Transfer"})
|
||||
_, ok := events["Mint"]
|
||||
Expect(ok).To(Equal(false))
|
||||
e, ok := events["Transfer"]
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(len(e.Fields)).To(Equal(3))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Parse", func() {
|
||||
It("Fetches and parses abi from etherscan using contract address", func() {
|
||||
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359" // dai contract address
|
||||
err = p.Parse(contractAddr)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
expectedAbi := constants.DaiAbiString
|
||||
Expect(p.Abi()).To(Equal(expectedAbi))
|
||||
|
||||
expectedParsedAbi, err := geth.ParseAbi(expectedAbi)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(p.ParsedAbi()).To(Equal(expectedParsedAbi))
|
||||
})
|
||||
|
||||
It("Fails with a normal, non-contract, account address", func() {
|
||||
addr := "0xAb2A8F7cB56D9EC65573BA1bE0f92Fa2Ff7dd165"
|
||||
err = p.Parse(addr)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetEvents", func() {
|
||||
It("Returns parsed events", func() {
|
||||
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
||||
err = p.Parse(contractAddr)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
events := p.GetEvents([]string{"Transfer"})
|
||||
|
||||
e, ok := events["Transfer"]
|
||||
Expect(ok).To(Equal(true))
|
||||
|
||||
abiTy := e.Fields[0].Type.T
|
||||
Expect(abiTy).To(Equal(abi.AddressTy))
|
||||
|
||||
pgTy := e.Fields[0].PgType
|
||||
Expect(pgTy).To(Equal("CHARACTER VARYING(66)"))
|
||||
|
||||
abiTy = e.Fields[1].Type.T
|
||||
Expect(abiTy).To(Equal(abi.AddressTy))
|
||||
|
||||
pgTy = e.Fields[1].PgType
|
||||
Expect(pgTy).To(Equal("CHARACTER VARYING(66)"))
|
||||
|
||||
abiTy = e.Fields[2].Type.T
|
||||
Expect(abiTy).To(Equal(abi.UintTy))
|
||||
|
||||
pgTy = e.Fields[2].PgType
|
||||
Expect(pgTy).To(Equal("NUMERIC"))
|
||||
|
||||
_, ok = events["Approval"]
|
||||
Expect(ok).To(Equal(false))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetSelectMethods", func() {
|
||||
It("Parses and returns only methods specified in passed array", func() {
|
||||
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
||||
err = p.Parse(contractAddr)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
methods := p.GetSelectMethods([]string{"balanceOf"})
|
||||
Expect(len(methods)).To(Equal(1))
|
||||
|
||||
balOf := methods[0]
|
||||
Expect(balOf.Name).To(Equal("balanceOf"))
|
||||
Expect(len(balOf.Args)).To(Equal(1))
|
||||
Expect(len(balOf.Return)).To(Equal(1))
|
||||
|
||||
abiTy := balOf.Args[0].Type.T
|
||||
Expect(abiTy).To(Equal(abi.AddressTy))
|
||||
|
||||
pgTy := balOf.Args[0].PgType
|
||||
Expect(pgTy).To(Equal("CHARACTER VARYING(66)"))
|
||||
|
||||
abiTy = balOf.Return[0].Type.T
|
||||
Expect(abiTy).To(Equal(abi.UintTy))
|
||||
|
||||
pgTy = balOf.Return[0].PgType
|
||||
Expect(pgTy).To(Equal("NUMERIC"))
|
||||
|
||||
})
|
||||
|
||||
It("Parses and returns methods in the order they were specified", func() {
|
||||
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
||||
err = p.Parse(contractAddr)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
selectMethods := p.GetSelectMethods([]string{"balanceOf", "allowance"})
|
||||
Expect(len(selectMethods)).To(Equal(2))
|
||||
|
||||
balOf := selectMethods[0]
|
||||
allow := selectMethods[1]
|
||||
|
||||
Expect(balOf.Name).To(Equal("balanceOf"))
|
||||
Expect(allow.Name).To(Equal("allowance"))
|
||||
})
|
||||
|
||||
It("Returns nil if given a nil or empty array", func() {
|
||||
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
||||
err = p.Parse(contractAddr)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
var nilArr []types.Method
|
||||
selectMethods := p.GetSelectMethods([]string{})
|
||||
Expect(selectMethods).To(Equal(nilArr))
|
||||
selectMethods = p.GetMethods(nil)
|
||||
Expect(selectMethods).To(Equal(nilArr))
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Describe("GetMethods", func() {
|
||||
It("Parses and returns only methods specified in passed array", func() {
|
||||
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
||||
err = p.Parse(contractAddr)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
methods := p.GetMethods([]string{"balanceOf"})
|
||||
Expect(len(methods)).To(Equal(1))
|
||||
|
||||
balOf := methods[0]
|
||||
Expect(balOf.Name).To(Equal("balanceOf"))
|
||||
Expect(len(balOf.Args)).To(Equal(1))
|
||||
Expect(len(balOf.Return)).To(Equal(1))
|
||||
|
||||
abiTy := balOf.Args[0].Type.T
|
||||
Expect(abiTy).To(Equal(abi.AddressTy))
|
||||
|
||||
pgTy := balOf.Args[0].PgType
|
||||
Expect(pgTy).To(Equal("CHARACTER VARYING(66)"))
|
||||
|
||||
abiTy = balOf.Return[0].Type.T
|
||||
Expect(abiTy).To(Equal(abi.UintTy))
|
||||
|
||||
pgTy = balOf.Return[0].PgType
|
||||
Expect(pgTy).To(Equal("NUMERIC"))
|
||||
|
||||
})
|
||||
|
||||
It("Returns nil if given a nil array", func() {
|
||||
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
||||
err = p.Parse(contractAddr)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
var nilArr []types.Method
|
||||
selectMethods := p.GetMethods(nil)
|
||||
Expect(selectMethods).To(Equal(nilArr))
|
||||
})
|
||||
|
||||
It("Returns every method if given an empty array", func() {
|
||||
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
||||
err = p.Parse(contractAddr)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
selectMethods := p.GetMethods([]string{})
|
||||
Expect(len(selectMethods)).To(Equal(22))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,292 @@
|
||||
// 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 poller
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/repository"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
type Poller interface {
|
||||
PollContract(con contract.Contract) error
|
||||
PollContractAt(con contract.Contract, blockNumber int64) error
|
||||
FetchContractData(contractAbi, contractAddress, method string, methodArgs []interface{}, result interface{}, blockNumber int64) error
|
||||
}
|
||||
|
||||
type poller struct {
|
||||
repository.MethodRepository
|
||||
bc core.BlockChain
|
||||
contract contract.Contract
|
||||
}
|
||||
|
||||
func NewPoller(blockChain core.BlockChain, db *postgres.DB, mode types.Mode) *poller {
|
||||
return &poller{
|
||||
MethodRepository: repository.NewMethodRepository(db, mode),
|
||||
bc: blockChain,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *poller) PollContract(con contract.Contract) error {
|
||||
for i := con.StartingBlock; i <= con.LastBlock; i++ {
|
||||
p.PollContractAt(con, i)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *poller) PollContractAt(con contract.Contract, blockNumber int64) error {
|
||||
p.contract = con
|
||||
for _, m := range con.Methods {
|
||||
switch len(m.Args) {
|
||||
case 0:
|
||||
if err := p.pollNoArgAt(m, blockNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
case 1:
|
||||
if err := p.pollSingleArgAt(m, blockNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
case 2:
|
||||
if err := p.pollDoubleArgAt(m, blockNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return errors.New("poller error: too many arguments to handle")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *poller) pollNoArgAt(m types.Method, bn int64) error {
|
||||
result := types.Result{
|
||||
Block: bn,
|
||||
Method: m,
|
||||
Inputs: nil,
|
||||
PgType: m.Return[0].PgType,
|
||||
}
|
||||
|
||||
var out interface{}
|
||||
err := p.bc.FetchContractData(p.contract.Abi, p.contract.Address, m.Name, nil, &out, bn)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("poller error calling 0 argument method\r\nblock: %d, method: %s, contract: %s\r\nerr: %v", bn, m.Name, p.contract.Address, err))
|
||||
}
|
||||
|
||||
strOut, err := stringify(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Cache returned value if piping is turned on
|
||||
p.cache(out)
|
||||
result.Output = strOut
|
||||
|
||||
// Persist result immediately
|
||||
err = p.PersistResults([]types.Result{result}, m, p.contract.Address, p.contract.Name)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("poller error persisting 0 argument method result\r\nblock: %d, method: %s, contract: %s\r\nerr: %v", bn, m.Name, p.contract.Address, err))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Use token holder address to poll methods that take 1 address argument (e.g. balanceOf)
|
||||
func (p *poller) pollSingleArgAt(m types.Method, bn int64) error {
|
||||
result := types.Result{
|
||||
Block: bn,
|
||||
Method: m,
|
||||
Inputs: make([]interface{}, 1),
|
||||
PgType: m.Return[0].PgType,
|
||||
}
|
||||
|
||||
// Depending on the type of the arg choose
|
||||
// the correct argument set to iterate over
|
||||
var args map[interface{}]bool
|
||||
switch m.Args[0].Type.T {
|
||||
case abi.HashTy, abi.FixedBytesTy:
|
||||
args = p.contract.EmittedHashes
|
||||
case abi.AddressTy:
|
||||
args = p.contract.EmittedAddrs
|
||||
}
|
||||
if len(args) == 0 { // If we haven't collected any args by now we can't call the method
|
||||
return nil
|
||||
}
|
||||
results := make([]types.Result, 0, len(args))
|
||||
|
||||
for arg := range args {
|
||||
in := []interface{}{arg}
|
||||
strIn := []interface{}{contract.StringifyArg(arg)}
|
||||
|
||||
var out interface{}
|
||||
err := p.bc.FetchContractData(p.contract.Abi, p.contract.Address, m.Name, in, &out, bn)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("poller error calling 1 argument method\r\nblock: %d, method: %s, contract: %s\r\nerr: %v", bn, m.Name, p.contract.Address, err))
|
||||
}
|
||||
strOut, err := stringify(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.cache(out)
|
||||
|
||||
// Write inputs and outputs to result and append result to growing set
|
||||
result.Inputs = strIn
|
||||
result.Output = strOut
|
||||
results = append(results, result)
|
||||
}
|
||||
|
||||
// Persist result set as batch
|
||||
err := p.PersistResults(results, m, p.contract.Address, p.contract.Name)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("poller error persisting 1 argument method result\r\nblock: %d, method: %s, contract: %s\r\nerr: %v", bn, m.Name, p.contract.Address, err))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Use token holder address to poll methods that take 2 address arguments (e.g. allowance)
|
||||
func (p *poller) pollDoubleArgAt(m types.Method, bn int64) error {
|
||||
result := types.Result{
|
||||
Block: bn,
|
||||
Method: m,
|
||||
Inputs: make([]interface{}, 2),
|
||||
PgType: m.Return[0].PgType,
|
||||
}
|
||||
|
||||
// Depending on the type of the args choose
|
||||
// the correct argument sets to iterate over
|
||||
var firstArgs map[interface{}]bool
|
||||
switch m.Args[0].Type.T {
|
||||
case abi.HashTy, abi.FixedBytesTy:
|
||||
firstArgs = p.contract.EmittedHashes
|
||||
case abi.AddressTy:
|
||||
firstArgs = p.contract.EmittedAddrs
|
||||
}
|
||||
if len(firstArgs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var secondArgs map[interface{}]bool
|
||||
switch m.Args[1].Type.T {
|
||||
case abi.HashTy, abi.FixedBytesTy:
|
||||
secondArgs = p.contract.EmittedHashes
|
||||
case abi.AddressTy:
|
||||
secondArgs = p.contract.EmittedAddrs
|
||||
}
|
||||
if len(secondArgs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
results := make([]types.Result, 0, len(firstArgs)*len(secondArgs))
|
||||
|
||||
for arg1 := range firstArgs {
|
||||
for arg2 := range secondArgs {
|
||||
in := []interface{}{arg1, arg2}
|
||||
strIn := []interface{}{contract.StringifyArg(arg1), contract.StringifyArg(arg2)}
|
||||
|
||||
var out interface{}
|
||||
err := p.bc.FetchContractData(p.contract.Abi, p.contract.Address, m.Name, in, &out, bn)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("poller error calling 2 argument method\r\nblock: %d, method: %s, contract: %s\r\nerr: %v", bn, m.Name, p.contract.Address, err))
|
||||
}
|
||||
|
||||
strOut, err := stringify(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.cache(out)
|
||||
|
||||
result.Output = strOut
|
||||
result.Inputs = strIn
|
||||
results = append(results, result)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
err := p.PersistResults(results, m, p.contract.Address, p.contract.Name)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("poller error persisting 2 argument method result\r\nblock: %d, method: %s, contract: %s\r\nerr: %v", bn, m.Name, p.contract.Address, err))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// This is just a wrapper around the poller blockchain's FetchContractData method
|
||||
func (p *poller) FetchContractData(contractAbi, contractAddress, method string, methodArgs []interface{}, result interface{}, blockNumber int64) error {
|
||||
return p.bc.FetchContractData(contractAbi, contractAddress, method, methodArgs, result, blockNumber)
|
||||
}
|
||||
|
||||
// This is used to cache an method return value if method piping is turned on
|
||||
func (p *poller) cache(out interface{}) {
|
||||
// Cache returned value if piping is turned on
|
||||
if p.contract.Piping {
|
||||
switch out.(type) {
|
||||
case common.Hash:
|
||||
if p.contract.EmittedHashes != nil {
|
||||
p.contract.AddEmittedHash(out.(common.Hash))
|
||||
}
|
||||
case []byte:
|
||||
if p.contract.EmittedHashes != nil && len(out.([]byte)) == 32 {
|
||||
p.contract.AddEmittedHash(common.BytesToHash(out.([]byte)))
|
||||
}
|
||||
case common.Address:
|
||||
if p.contract.EmittedAddrs != nil {
|
||||
p.contract.AddEmittedAddr(out.(common.Address))
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func stringify(input interface{}) (string, error) {
|
||||
switch input.(type) {
|
||||
case *big.Int:
|
||||
b := input.(*big.Int)
|
||||
return b.String(), nil
|
||||
case common.Address:
|
||||
a := input.(common.Address)
|
||||
return a.String(), nil
|
||||
case common.Hash:
|
||||
h := input.(common.Hash)
|
||||
return h.String(), nil
|
||||
case string:
|
||||
return input.(string), nil
|
||||
case []byte:
|
||||
b := hexutil.Encode(input.([]byte))
|
||||
return b, nil
|
||||
case byte:
|
||||
b := input.(byte)
|
||||
return string(b), nil
|
||||
case bool:
|
||||
return strconv.FormatBool(input.(bool)), nil
|
||||
default:
|
||||
return "", errors.New("error: unhandled return type")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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 poller_test
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestPoller(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Poller Suite Test")
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
logrus.SetOutput(ioutil.Discard)
|
||||
})
|
||||
@@ -0,0 +1,257 @@
|
||||
// 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 poller_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/poller"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
var _ = Describe("Poller", func() {
|
||||
|
||||
var p poller.Poller
|
||||
var con *contract.Contract
|
||||
var db *postgres.DB
|
||||
var bc core.BlockChain
|
||||
|
||||
AfterEach(func() {
|
||||
test_helpers.TearDown(db)
|
||||
})
|
||||
|
||||
Describe("Full sync mode", func() {
|
||||
BeforeEach(func() {
|
||||
db, bc = test_helpers.SetupDBandBC()
|
||||
p = poller.NewPoller(bc, db, types.FullSync)
|
||||
})
|
||||
|
||||
Describe("PollContract", func() {
|
||||
It("Polls specified contract methods using contract's argument list", func() {
|
||||
con = test_helpers.SetupTusdContract(nil, []string{"balanceOf"})
|
||||
Expect(con.Abi).To(Equal(constants.TusdAbiString))
|
||||
con.StartingBlock = 6707322
|
||||
con.LastBlock = 6707323
|
||||
con.AddEmittedAddr(common.HexToAddress("0xfE9e8709d3215310075d67E3ed32A380CCf451C8"), common.HexToAddress("0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE"))
|
||||
|
||||
err := p.PollContract(*con)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
scanStruct := test_helpers.BalanceOf{}
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM full_%s.balanceof_method WHERE who_ = '0xfE9e8709d3215310075d67E3ed32A380CCf451C8' AND block = '6707322'", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Balance).To(Equal("66386309548896882859581786"))
|
||||
Expect(scanStruct.TokenName).To(Equal("TrueUSD"))
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM full_%s.balanceof_method WHERE who_ = '0xfE9e8709d3215310075d67E3ed32A380CCf451C8' AND block = '6707323'", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Balance).To(Equal("66386309548896882859581786"))
|
||||
Expect(scanStruct.TokenName).To(Equal("TrueUSD"))
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM full_%s.balanceof_method WHERE who_ = '0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE' AND block = '6707322'", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Balance).To(Equal("17982350181394112023885864"))
|
||||
Expect(scanStruct.TokenName).To(Equal("TrueUSD"))
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM full_%s.balanceof_method WHERE who_ = '0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE' AND block = '6707323'", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Balance).To(Equal("17982350181394112023885864"))
|
||||
Expect(scanStruct.TokenName).To(Equal("TrueUSD"))
|
||||
})
|
||||
|
||||
It("Polls specified contract methods using contract's hash list", func() {
|
||||
con = test_helpers.SetupENSContract(nil, []string{"owner"})
|
||||
Expect(con.Abi).To(Equal(constants.ENSAbiString))
|
||||
Expect(len(con.Methods)).To(Equal(1))
|
||||
con.AddEmittedHash(common.HexToHash("0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae"), common.HexToHash("0x7e74a86b6e146964fb965db04dc2590516da77f720bb6759337bf5632415fd86"))
|
||||
|
||||
err := p.PollContractAt(*con, 6885877)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
scanStruct := test_helpers.Owner{}
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM full_%s.owner_method WHERE node_ = '0x7e74a86b6e146964fb965db04dc2590516da77f720bb6759337bf5632415fd86' AND block = '6885877'", constants.EnsContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Address).To(Equal("0x546aA2EaE2514494EeaDb7bbb35243348983C59d"))
|
||||
Expect(scanStruct.TokenName).To(Equal("ENS-Registry"))
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM full_%s.owner_method WHERE node_ = '0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae' AND block = '6885877'", constants.EnsContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Address).To(Equal("0x6090A6e47849629b7245Dfa1Ca21D94cd15878Ef"))
|
||||
Expect(scanStruct.TokenName).To(Equal("ENS-Registry"))
|
||||
})
|
||||
|
||||
It("Does not poll and persist any methods if none are specified", func() {
|
||||
con = test_helpers.SetupTusdContract(nil, nil)
|
||||
Expect(con.Abi).To(Equal(constants.TusdAbiString))
|
||||
con.StartingBlock = 6707322
|
||||
con.LastBlock = 6707323
|
||||
con.AddEmittedAddr(common.HexToAddress("0xfE9e8709d3215310075d67E3ed32A380CCf451C8"), common.HexToAddress("0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE"))
|
||||
|
||||
err := p.PollContract(*con)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
scanStruct := test_helpers.BalanceOf{}
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM full_%s.balanceof_method WHERE who_ = '0xfE9e8709d3215310075d67E3ed32A380CCf451C8' AND block = '6707322'", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("FetchContractData", func() {
|
||||
It("Calls a single contract method", func() {
|
||||
var name = new(string)
|
||||
err := p.FetchContractData(constants.TusdAbiString, constants.TusdContractAddress, "name", nil, &name, 6197514)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(*name).To(Equal("TrueUSD"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Light sync mode", func() {
|
||||
BeforeEach(func() {
|
||||
db, bc = test_helpers.SetupDBandBC()
|
||||
p = poller.NewPoller(bc, db, types.LightSync)
|
||||
})
|
||||
|
||||
Describe("PollContract", func() {
|
||||
It("Polls specified contract methods using contract's token holder address list", func() {
|
||||
con = test_helpers.SetupTusdContract(nil, []string{"balanceOf"})
|
||||
Expect(con.Abi).To(Equal(constants.TusdAbiString))
|
||||
con.StartingBlock = 6707322
|
||||
con.LastBlock = 6707323
|
||||
con.AddEmittedAddr(common.HexToAddress("0xfE9e8709d3215310075d67E3ed32A380CCf451C8"), common.HexToAddress("0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE"))
|
||||
|
||||
err := p.PollContract(*con)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
scanStruct := test_helpers.BalanceOf{}
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.balanceof_method WHERE who_ = '0xfE9e8709d3215310075d67E3ed32A380CCf451C8' AND block = '6707322'", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Balance).To(Equal("66386309548896882859581786"))
|
||||
Expect(scanStruct.TokenName).To(Equal("TrueUSD"))
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.balanceof_method WHERE who_ = '0xfE9e8709d3215310075d67E3ed32A380CCf451C8' AND block = '6707323'", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Balance).To(Equal("66386309548896882859581786"))
|
||||
Expect(scanStruct.TokenName).To(Equal("TrueUSD"))
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.balanceof_method WHERE who_ = '0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE' AND block = '6707322'", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Balance).To(Equal("17982350181394112023885864"))
|
||||
Expect(scanStruct.TokenName).To(Equal("TrueUSD"))
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.balanceof_method WHERE who_ = '0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE' AND block = '6707323'", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Balance).To(Equal("17982350181394112023885864"))
|
||||
Expect(scanStruct.TokenName).To(Equal("TrueUSD"))
|
||||
})
|
||||
|
||||
It("Polls specified contract methods using contract's hash list", func() {
|
||||
con = test_helpers.SetupENSContract(nil, []string{"owner"})
|
||||
Expect(con.Abi).To(Equal(constants.ENSAbiString))
|
||||
Expect(len(con.Methods)).To(Equal(1))
|
||||
con.AddEmittedHash(common.HexToHash("0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae"), common.HexToHash("0x7e74a86b6e146964fb965db04dc2590516da77f720bb6759337bf5632415fd86"))
|
||||
|
||||
err := p.PollContractAt(*con, 6885877)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
scanStruct := test_helpers.Owner{}
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.owner_method WHERE node_ = '0x7e74a86b6e146964fb965db04dc2590516da77f720bb6759337bf5632415fd86' AND block = '6885877'", constants.EnsContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Address).To(Equal("0x546aA2EaE2514494EeaDb7bbb35243348983C59d"))
|
||||
Expect(scanStruct.TokenName).To(Equal("ENS-Registry"))
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.owner_method WHERE node_ = '0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae' AND block = '6885877'", constants.EnsContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Address).To(Equal("0x6090A6e47849629b7245Dfa1Ca21D94cd15878Ef"))
|
||||
Expect(scanStruct.TokenName).To(Equal("ENS-Registry"))
|
||||
})
|
||||
|
||||
It("Does not poll and persist any methods if none are specified", func() {
|
||||
con = test_helpers.SetupTusdContract(nil, nil)
|
||||
Expect(con.Abi).To(Equal(constants.TusdAbiString))
|
||||
con.StartingBlock = 6707322
|
||||
con.LastBlock = 6707323
|
||||
con.AddEmittedAddr(common.HexToAddress("0xfE9e8709d3215310075d67E3ed32A380CCf451C8"), common.HexToAddress("0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE"))
|
||||
|
||||
err := p.PollContract(*con)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
scanStruct := test_helpers.BalanceOf{}
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.balanceof_method WHERE who_ = '0xfE9e8709d3215310075d67E3ed32A380CCf451C8' AND block = '6707322'", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Caches returned values of the appropriate types for downstream method polling if method piping is turned on", func() {
|
||||
con = test_helpers.SetupENSContract(nil, []string{"resolver"})
|
||||
Expect(con.Abi).To(Equal(constants.ENSAbiString))
|
||||
con.StartingBlock = 6921967
|
||||
con.LastBlock = 6921968
|
||||
con.EmittedAddrs = map[interface{}]bool{}
|
||||
con.Piping = false
|
||||
con.AddEmittedHash(common.HexToHash("0x495b6e6efdedb750aa519919b5cf282bdaa86067b82a2293a3ff5723527141e8"))
|
||||
err := p.PollContract(*con)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
scanStruct := test_helpers.Resolver{}
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.resolver_method WHERE node_ = '0x495b6e6efdedb750aa519919b5cf282bdaa86067b82a2293a3ff5723527141e8' AND block = '6921967'", constants.EnsContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Address).To(Equal("0x5FfC014343cd971B7eb70732021E26C35B744cc4"))
|
||||
Expect(scanStruct.TokenName).To(Equal("ENS-Registry"))
|
||||
Expect(len(con.EmittedAddrs)).To(Equal(0)) // With piping off the address is not saved
|
||||
|
||||
test_helpers.TearDown(db)
|
||||
db, bc = test_helpers.SetupDBandBC()
|
||||
p = poller.NewPoller(bc, db, types.LightSync)
|
||||
|
||||
con.Piping = true
|
||||
err = p.PollContract(*con)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.resolver_method WHERE node_ = '0x495b6e6efdedb750aa519919b5cf282bdaa86067b82a2293a3ff5723527141e8' AND block = '6921967'", constants.EnsContractAddress)).StructScan(&scanStruct)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanStruct.Address).To(Equal("0x5FfC014343cd971B7eb70732021E26C35B744cc4"))
|
||||
Expect(scanStruct.TokenName).To(Equal("ENS-Registry"))
|
||||
Expect(len(con.EmittedAddrs)).To(Equal(1)) // With piping on it is saved
|
||||
Expect(con.EmittedAddrs[common.HexToAddress("0x5FfC014343cd971B7eb70732021E26C35B744cc4")]).To(Equal(true))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("FetchContractData", func() {
|
||||
It("Calls a single contract method", func() {
|
||||
var name = new(string)
|
||||
err := p.FetchContractData(constants.TusdAbiString, constants.TusdContractAddress, "name", nil, &name, 6197514)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(*name).To(Equal("TrueUSD"))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,313 @@
|
||||
// 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 repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/golang-lru"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/light/repository"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
const (
|
||||
// Number of contract address and method ids to keep in cache
|
||||
contractCacheSize = 100
|
||||
eventChacheSize = 1000
|
||||
)
|
||||
|
||||
// Event repository is used to persist event data into custom tables
|
||||
type EventRepository interface {
|
||||
PersistLogs(logs []types.Log, eventInfo types.Event, contractAddr, contractName string) error
|
||||
CreateEventTable(contractAddr string, event types.Event) (bool, error)
|
||||
CreateContractSchema(contractName string) (bool, error)
|
||||
CheckSchemaCache(key string) (interface{}, bool)
|
||||
CheckTableCache(key string) (interface{}, bool)
|
||||
}
|
||||
|
||||
type eventRepository struct {
|
||||
db *postgres.DB
|
||||
mode types.Mode
|
||||
schemas *lru.Cache // Cache names of recently used schemas to minimize db connections
|
||||
tables *lru.Cache // Cache names of recently used tables to minimize db connections
|
||||
}
|
||||
|
||||
func NewEventRepository(db *postgres.DB, mode types.Mode) *eventRepository {
|
||||
ccs, _ := lru.New(contractCacheSize)
|
||||
ecs, _ := lru.New(eventChacheSize)
|
||||
return &eventRepository{
|
||||
db: db,
|
||||
mode: mode,
|
||||
schemas: ccs,
|
||||
tables: ecs,
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a schema for the contract if needed
|
||||
// Creates table for the watched contract event if needed
|
||||
// Persists converted event log data into this custom table
|
||||
func (r *eventRepository) PersistLogs(logs []types.Log, eventInfo types.Event, contractAddr, contractName string) error {
|
||||
if len(logs) == 0 {
|
||||
return errors.New("event repository error: passed empty logs slice")
|
||||
}
|
||||
_, err := r.CreateContractSchema(contractAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = r.CreateEventTable(contractAddr, eventInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return r.persistLogs(logs, eventInfo, contractAddr, contractName)
|
||||
}
|
||||
|
||||
func (r *eventRepository) persistLogs(logs []types.Log, eventInfo types.Event, contractAddr, contractName string) error {
|
||||
var err error
|
||||
switch r.mode {
|
||||
case types.LightSync:
|
||||
err = r.persistLightSyncLogs(logs, eventInfo, contractAddr, contractName)
|
||||
case types.FullSync:
|
||||
err = r.persistFullSyncLogs(logs, eventInfo, contractAddr, contractName)
|
||||
default:
|
||||
return errors.New("event repository error: unhandled mode")
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Creates a custom postgres command to persist logs for the given event (compatible with light synced vDB)
|
||||
func (r *eventRepository) persistLightSyncLogs(logs []types.Log, eventInfo types.Event, contractAddr, contractName string) error {
|
||||
tx, err := r.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, event := range logs {
|
||||
// Begin pg query string
|
||||
pgStr := fmt.Sprintf("INSERT INTO %s_%s.%s_event ", r.mode.String(), strings.ToLower(contractAddr), strings.ToLower(eventInfo.Name))
|
||||
pgStr = pgStr + "(header_id, token_name, raw_log, log_idx, tx_idx"
|
||||
el := len(event.Values)
|
||||
|
||||
// Preallocate slice of needed capacity and proceed to pack variables into it in same order they appear in string
|
||||
data := make([]interface{}, 0, 5+el)
|
||||
data = append(data,
|
||||
event.Id,
|
||||
contractName,
|
||||
event.Raw,
|
||||
event.LogIndex,
|
||||
event.TransactionIndex)
|
||||
|
||||
// Iterate over inputs and append name to query string and value to input data
|
||||
for inputName, input := range event.Values {
|
||||
pgStr = pgStr + fmt.Sprintf(", %s_", strings.ToLower(inputName)) // Add underscore after to avoid any collisions with reserved pg words
|
||||
data = append(data, input)
|
||||
}
|
||||
|
||||
// For each input entry we created we add its postgres command variable to the string
|
||||
pgStr = pgStr + ") VALUES ($1, $2, $3, $4, $5"
|
||||
for i := 0; i < el; i++ {
|
||||
pgStr = pgStr + fmt.Sprintf(", $%d", i+6)
|
||||
}
|
||||
pgStr = pgStr + ")"
|
||||
|
||||
// Add this query to the transaction
|
||||
_, err = tx.Exec(pgStr, data...)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Mark header as checked for this eventId
|
||||
eventId := strings.ToLower(eventInfo.Name + "_" + contractAddr)
|
||||
err = repository.MarkHeaderCheckedInTransaction(logs[0].Id, tx, eventId) // This assumes all logs are from same block
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// Creates a custom postgres command to persist logs for the given event (compatible with fully synced vDB)
|
||||
func (r *eventRepository) persistFullSyncLogs(logs []types.Log, eventInfo types.Event, contractAddr, contractName string) error {
|
||||
tx, err := r.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, event := range logs {
|
||||
pgStr := fmt.Sprintf("INSERT INTO %s_%s.%s_event ", r.mode.String(), strings.ToLower(contractAddr), strings.ToLower(eventInfo.Name))
|
||||
pgStr = pgStr + "(vulcanize_log_id, token_name, block, tx"
|
||||
el := len(event.Values)
|
||||
|
||||
data := make([]interface{}, 0, 4+el)
|
||||
data = append(data,
|
||||
event.Id,
|
||||
contractName,
|
||||
event.Block,
|
||||
event.Tx)
|
||||
|
||||
for inputName, input := range event.Values {
|
||||
pgStr = pgStr + fmt.Sprintf(", %s_", strings.ToLower(inputName))
|
||||
data = append(data, input)
|
||||
}
|
||||
|
||||
pgStr = pgStr + ") VALUES ($1, $2, $3, $4"
|
||||
for i := 0; i < el; i++ {
|
||||
pgStr = pgStr + fmt.Sprintf(", $%d", i+5)
|
||||
}
|
||||
pgStr = pgStr + ") ON CONFLICT (vulcanize_log_id) DO NOTHING"
|
||||
|
||||
_, err = tx.Exec(pgStr, data...)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// Checks for event table and creates it if it does not already exist
|
||||
// Returns true if it created a new table; returns false if table already existed
|
||||
func (r *eventRepository) CreateEventTable(contractAddr string, event types.Event) (bool, error) {
|
||||
tableID := fmt.Sprintf("%s_%s.%s_event", r.mode.String(), strings.ToLower(contractAddr), strings.ToLower(event.Name))
|
||||
// Check cache before querying pq to see if table exists
|
||||
_, ok := r.tables.Get(tableID)
|
||||
if ok {
|
||||
return false, nil
|
||||
}
|
||||
tableExists, err := r.checkForTable(contractAddr, event.Name)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if !tableExists {
|
||||
err = r.newEventTable(tableID, event)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
// Add table id to cache
|
||||
r.tables.Add(tableID, true)
|
||||
|
||||
return !tableExists, nil
|
||||
}
|
||||
|
||||
// Creates a table for the given contract and event
|
||||
func (r *eventRepository) newEventTable(tableID string, event types.Event) error {
|
||||
// Begin pg string
|
||||
var pgStr = fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ", tableID)
|
||||
var err error
|
||||
|
||||
// Handle different modes
|
||||
switch r.mode {
|
||||
case types.FullSync:
|
||||
pgStr = pgStr + "(id SERIAL, vulcanize_log_id INTEGER NOT NULL UNIQUE, token_name CHARACTER VARYING(66) NOT NULL, block INTEGER NOT NULL, tx CHARACTER VARYING(66) NOT NULL,"
|
||||
|
||||
// Iterate over event fields, using their name and pgType to grow the string
|
||||
for _, field := range event.Fields {
|
||||
pgStr = pgStr + fmt.Sprintf(" %s_ %s NOT NULL,", strings.ToLower(field.Name), field.PgType)
|
||||
}
|
||||
pgStr = pgStr + " CONSTRAINT log_index_fk FOREIGN KEY (vulcanize_log_id) REFERENCES logs (id) ON DELETE CASCADE)"
|
||||
case types.LightSync:
|
||||
pgStr = pgStr + "(id SERIAL, header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, token_name CHARACTER VARYING(66) NOT NULL, raw_log JSONB, log_idx INTEGER NOT NULL, tx_idx INTEGER NOT NULL,"
|
||||
|
||||
for _, field := range event.Fields {
|
||||
pgStr = pgStr + fmt.Sprintf(" %s_ %s NOT NULL,", strings.ToLower(field.Name), field.PgType)
|
||||
}
|
||||
pgStr = pgStr + " UNIQUE (header_id, tx_idx, log_idx))"
|
||||
default:
|
||||
return errors.New("unhandled repository mode")
|
||||
}
|
||||
|
||||
_, err = r.db.Exec(pgStr)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Checks if a table already exists for the given contract and event
|
||||
func (r *eventRepository) checkForTable(contractAddr string, eventName string) (bool, error) {
|
||||
pgStr := fmt.Sprintf("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = '%s_%s' AND table_name = '%s_event')", r.mode.String(), strings.ToLower(contractAddr), strings.ToLower(eventName))
|
||||
|
||||
var exists bool
|
||||
err := r.db.Get(&exists, pgStr)
|
||||
|
||||
return exists, err
|
||||
}
|
||||
|
||||
// Checks for contract schema and creates it if it does not already exist
|
||||
// Returns true if it created a new schema; returns false if schema already existed
|
||||
func (r *eventRepository) CreateContractSchema(contractAddr string) (bool, error) {
|
||||
if contractAddr == "" {
|
||||
return false, errors.New("error: no contract address specified")
|
||||
}
|
||||
|
||||
// Check cache before querying pq to see if schema exists
|
||||
_, ok := r.schemas.Get(contractAddr)
|
||||
if ok {
|
||||
return false, nil
|
||||
}
|
||||
schemaExists, err := r.checkForSchema(contractAddr)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !schemaExists {
|
||||
err = r.newContractSchema(contractAddr)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
// Add schema name to cache
|
||||
r.schemas.Add(contractAddr, true)
|
||||
|
||||
return !schemaExists, nil
|
||||
}
|
||||
|
||||
// Creates a schema for the given contract
|
||||
func (r *eventRepository) newContractSchema(contractAddr string) error {
|
||||
_, err := r.db.Exec("CREATE SCHEMA IF NOT EXISTS " + r.mode.String() + "_" + strings.ToLower(contractAddr))
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Checks if a schema already exists for the given contract
|
||||
func (r *eventRepository) checkForSchema(contractAddr string) (bool, error) {
|
||||
pgStr := fmt.Sprintf("SELECT EXISTS (SELECT schema_name FROM information_schema.schemata WHERE schema_name = '%s_%s')", r.mode.String(), strings.ToLower(contractAddr))
|
||||
|
||||
var exists bool
|
||||
err := r.db.QueryRow(pgStr).Scan(&exists)
|
||||
|
||||
return exists, err
|
||||
}
|
||||
|
||||
func (r *eventRepository) CheckSchemaCache(key string) (interface{}, bool) {
|
||||
return r.schemas.Get(key)
|
||||
}
|
||||
|
||||
func (r *eventRepository) CheckTableCache(key string) (interface{}, bool) {
|
||||
return r.tables.Get(key)
|
||||
}
|
||||
@@ -0,0 +1,351 @@
|
||||
// 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 repository_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
geth "github.com/ethereum/go-ethereum/core/types"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
fc "github.com/vulcanize/vulcanizedb/pkg/contract_watcher/full/converter"
|
||||
lc "github.com/vulcanize/vulcanizedb/pkg/contract_watcher/light/converter"
|
||||
lr "github.com/vulcanize/vulcanizedb/pkg/contract_watcher/light/repository"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers/mocks"
|
||||
sr "github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/repository"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
||||
)
|
||||
|
||||
var _ = Describe("Repository", func() {
|
||||
var db *postgres.DB
|
||||
var dataStore sr.EventRepository
|
||||
var err error
|
||||
var log *types.Log
|
||||
var logs []types.Log
|
||||
var con *contract.Contract
|
||||
var vulcanizeLogId int64
|
||||
var wantedEvents = []string{"Transfer"}
|
||||
var wantedMethods = []string{"balanceOf"}
|
||||
var event types.Event
|
||||
var headerID int64
|
||||
var mockEvent = mocks.MockTranferEvent
|
||||
var mockLog1 = mocks.MockTransferLog1
|
||||
var mockLog2 = mocks.MockTransferLog2
|
||||
|
||||
BeforeEach(func() {
|
||||
db, con = test_helpers.SetupTusdRepo(&vulcanizeLogId, wantedEvents, wantedMethods)
|
||||
mockEvent.LogID = vulcanizeLogId
|
||||
|
||||
event = con.Events["Transfer"]
|
||||
err = con.GenerateFilters()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
test_helpers.TearDown(db)
|
||||
})
|
||||
|
||||
Describe("Full sync mode", func() {
|
||||
BeforeEach(func() {
|
||||
dataStore = sr.NewEventRepository(db, types.FullSync)
|
||||
})
|
||||
|
||||
Describe("CreateContractSchema", func() {
|
||||
It("Creates schema if it doesn't exist", func() {
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Caches schema it creates so that it does not need to repeatedly query the database to check for it's existence", func() {
|
||||
_, ok := dataStore.CheckSchemaCache(con.Address)
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
v, ok := dataStore.CheckSchemaCache(con.Address)
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(v).To(Equal(true))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("CreateEventTable", func() {
|
||||
It("Creates table if it doesn't exist", func() {
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateEventTable(con.Address, event)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateEventTable(con.Address, event)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Caches table it creates so that it does not need to repeatedly query the database to check for it's existence", func() {
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
tableID := fmt.Sprintf("%s_%s.%s_event", types.FullSync, strings.ToLower(con.Address), strings.ToLower(event.Name))
|
||||
_, ok := dataStore.CheckTableCache(tableID)
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
created, err = dataStore.CreateEventTable(con.Address, event)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
v, ok := dataStore.CheckTableCache(tableID)
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(v).To(Equal(true))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("PersistLogs", func() {
|
||||
BeforeEach(func() {
|
||||
c := fc.NewConverter(con)
|
||||
log, err = c.Convert(mockEvent, event)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Persists contract event log values into custom tables", func() {
|
||||
err = dataStore.PersistLogs([]types.Log{*log}, event, con.Address, con.Name)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
b, ok := con.EmittedAddrs[common.HexToAddress("0x000000000000000000000000000000000000Af21")]
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(b).To(Equal(true))
|
||||
|
||||
b, ok = con.EmittedAddrs[common.HexToAddress("0x09BbBBE21a5975cAc061D82f7b843bCE061BA391")]
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(b).To(Equal(true))
|
||||
|
||||
scanLog := test_helpers.TransferLog{}
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM full_%s.transfer_event", constants.TusdContractAddress)).StructScan(&scanLog)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
expectedLog := test_helpers.TransferLog{
|
||||
Id: 1,
|
||||
VulvanizeLogId: vulcanizeLogId,
|
||||
TokenName: "TrueUSD",
|
||||
Block: 5488076,
|
||||
Tx: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
|
||||
From: "0x000000000000000000000000000000000000Af21",
|
||||
To: "0x09BbBBE21a5975cAc061D82f7b843bCE061BA391",
|
||||
Value: "1097077688018008265106216665536940668749033598146",
|
||||
}
|
||||
Expect(scanLog).To(Equal(expectedLog))
|
||||
})
|
||||
|
||||
It("Doesn't persist duplicate event logs", func() {
|
||||
// Try to persist the same log twice in a single call
|
||||
err = dataStore.PersistLogs([]types.Log{*log, *log}, event, con.Address, con.Name)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
scanLog := test_helpers.TransferLog{}
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM full_%s.transfer_event", constants.TusdContractAddress)).StructScan(&scanLog)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
expectedLog := test_helpers.TransferLog{
|
||||
Id: 1,
|
||||
VulvanizeLogId: vulcanizeLogId,
|
||||
TokenName: "TrueUSD",
|
||||
Block: 5488076,
|
||||
Tx: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
|
||||
From: "0x000000000000000000000000000000000000Af21",
|
||||
To: "0x09BbBBE21a5975cAc061D82f7b843bCE061BA391",
|
||||
Value: "1097077688018008265106216665536940668749033598146",
|
||||
}
|
||||
Expect(scanLog).To(Equal(expectedLog))
|
||||
|
||||
// Attempt to persist the same log again in seperate call
|
||||
err = dataStore.PersistLogs([]types.Log{*log}, event, con.Address, con.Name)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// Show that no new logs were entered
|
||||
var count int
|
||||
err = db.Get(&count, fmt.Sprintf("SELECT COUNT(*) FROM full_%s.transfer_event", constants.TusdContractAddress))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(count).To(Equal(1))
|
||||
})
|
||||
|
||||
It("Fails with empty log", func() {
|
||||
err = dataStore.PersistLogs([]types.Log{}, event, con.Address, con.Name)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Light sync mode", func() {
|
||||
BeforeEach(func() {
|
||||
dataStore = sr.NewEventRepository(db, types.LightSync)
|
||||
})
|
||||
|
||||
Describe("CreateContractSchema", func() {
|
||||
It("Creates schema if it doesn't exist", func() {
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Caches schema it creates so that it does not need to repeatedly query the database to check for it's existence", func() {
|
||||
_, ok := dataStore.CheckSchemaCache(con.Address)
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
v, ok := dataStore.CheckSchemaCache(con.Address)
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(v).To(Equal(true))
|
||||
})
|
||||
|
||||
It("Caches table it creates so that it does not need to repeatedly query the database to check for it's existence", func() {
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
tableID := fmt.Sprintf("%s_%s.%s_event", types.LightSync, strings.ToLower(con.Address), strings.ToLower(event.Name))
|
||||
_, ok := dataStore.CheckTableCache(tableID)
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
created, err = dataStore.CreateEventTable(con.Address, event)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
v, ok := dataStore.CheckTableCache(tableID)
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(v).To(Equal(true))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("CreateEventTable", func() {
|
||||
It("Creates table if it doesn't exist", func() {
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateEventTable(con.Address, event)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateEventTable(con.Address, event)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(false))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("PersistLogs", func() {
|
||||
BeforeEach(func() {
|
||||
headerRepository := repositories.NewHeaderRepository(db)
|
||||
headerID, err = headerRepository.CreateOrUpdateHeader(mocks.MockHeader1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
c := lc.NewConverter(con)
|
||||
logs, err = c.Convert([]geth.Log{mockLog1, mockLog2}, event, headerID)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Persists contract event log values into custom tables", func() {
|
||||
hr := lr.NewHeaderRepository(db)
|
||||
err = hr.AddCheckColumn(event.Name + "_" + con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
err = dataStore.PersistLogs(logs, event, con.Address, con.Name)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
var count int
|
||||
err = db.Get(&count, fmt.Sprintf("SELECT COUNT(*) FROM light_%s.transfer_event", constants.TusdContractAddress))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(count).To(Equal(2))
|
||||
|
||||
scanLog := test_helpers.LightTransferLog{}
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.transfer_event LIMIT 1", constants.TusdContractAddress)).StructScan(&scanLog)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(scanLog.HeaderID).To(Equal(headerID))
|
||||
Expect(scanLog.TokenName).To(Equal("TrueUSD"))
|
||||
Expect(scanLog.TxIndex).To(Equal(int64(110)))
|
||||
Expect(scanLog.LogIndex).To(Equal(int64(1)))
|
||||
Expect(scanLog.From).To(Equal("0x000000000000000000000000000000000000Af21"))
|
||||
Expect(scanLog.To).To(Equal("0x09BbBBE21a5975cAc061D82f7b843bCE061BA391"))
|
||||
Expect(scanLog.Value).To(Equal("1097077688018008265106216665536940668749033598146"))
|
||||
|
||||
var expectedRawLog, rawLog geth.Log
|
||||
err = json.Unmarshal(logs[0].Raw, &expectedRawLog)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
err = json.Unmarshal(scanLog.RawLog, &rawLog)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(rawLog).To(Equal(expectedRawLog))
|
||||
})
|
||||
|
||||
It("Doesn't persist duplicate event logs", func() {
|
||||
hr := lr.NewHeaderRepository(db)
|
||||
err = hr.AddCheckColumn(event.Name + "_" + con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// Try and fail to persist the same log twice in a single call
|
||||
err = dataStore.PersistLogs([]types.Log{logs[0], logs[0]}, event, con.Address, con.Name)
|
||||
Expect(err).To(HaveOccurred())
|
||||
|
||||
// Successfuly persist the two unique logs
|
||||
err = dataStore.PersistLogs(logs, event, con.Address, con.Name)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// Try and fail to persist the same logs again in separate call
|
||||
err = dataStore.PersistLogs([]types.Log{*log}, event, con.Address, con.Name)
|
||||
Expect(err).To(HaveOccurred())
|
||||
|
||||
// Show that no new logs were entered
|
||||
var count int
|
||||
err = db.Get(&count, fmt.Sprintf("SELECT COUNT(*) FROM light_%s.transfer_event", constants.TusdContractAddress))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(count).To(Equal(2))
|
||||
})
|
||||
|
||||
It("Fails if the persisted event does not have a corresponding eventID column in the checked_headers table", func() {
|
||||
err = dataStore.PersistLogs(logs, event, con.Address, con.Name)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Fails with empty log", func() {
|
||||
err = dataStore.PersistLogs([]types.Log{}, event, con.Address, con.Name)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,227 @@
|
||||
// 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 repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/golang-lru"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
const methodCacheSize = 1000
|
||||
|
||||
type MethodRepository interface {
|
||||
PersistResults(results []types.Result, methodInfo types.Method, contractAddr, contractName string) error
|
||||
CreateMethodTable(contractAddr string, method types.Method) (bool, error)
|
||||
CreateContractSchema(contractAddr string) (bool, error)
|
||||
CheckSchemaCache(key string) (interface{}, bool)
|
||||
CheckTableCache(key string) (interface{}, bool)
|
||||
}
|
||||
|
||||
type methodRepository struct {
|
||||
*postgres.DB
|
||||
mode types.Mode
|
||||
schemas *lru.Cache // Cache names of recently used schemas to minimize db connections
|
||||
tables *lru.Cache // Cache names of recently used tables to minimize db connections
|
||||
}
|
||||
|
||||
func NewMethodRepository(db *postgres.DB, mode types.Mode) *methodRepository {
|
||||
ccs, _ := lru.New(contractCacheSize)
|
||||
mcs, _ := lru.New(methodCacheSize)
|
||||
return &methodRepository{
|
||||
DB: db,
|
||||
mode: mode,
|
||||
schemas: ccs,
|
||||
tables: mcs,
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a schema for the contract if needed
|
||||
// Creates table for the contract method if needed
|
||||
// Persists method polling data into this custom table
|
||||
func (r *methodRepository) PersistResults(results []types.Result, methodInfo types.Method, contractAddr, contractName string) error {
|
||||
if len(results) == 0 {
|
||||
return errors.New("method repository error: passed empty results slice")
|
||||
}
|
||||
_, err := r.CreateContractSchema(contractAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = r.CreateMethodTable(contractAddr, methodInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return r.persistResults(results, methodInfo, contractAddr, contractName)
|
||||
}
|
||||
|
||||
// Creates a custom postgres command to persist logs for the given event
|
||||
func (r *methodRepository) persistResults(results []types.Result, methodInfo types.Method, contractAddr, contractName string) error {
|
||||
tx, err := r.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, result := range results {
|
||||
// Begin postgres string
|
||||
pgStr := fmt.Sprintf("INSERT INTO %s_%s.%s_method ", r.mode.String(), strings.ToLower(contractAddr), strings.ToLower(result.Name))
|
||||
pgStr = pgStr + "(token_name, block"
|
||||
ml := len(result.Args)
|
||||
|
||||
// Preallocate slice of needed capacity and proceed to pack variables into it in same order they appear in string
|
||||
data := make([]interface{}, 0, 3+ml)
|
||||
data = append(data,
|
||||
contractName,
|
||||
result.Block)
|
||||
|
||||
// Iterate over method args and return value, adding names
|
||||
// to the string and pushing values to the slice
|
||||
for i, arg := range result.Args {
|
||||
pgStr = pgStr + fmt.Sprintf(", %s_", strings.ToLower(arg.Name)) // Add underscore after to avoid any collisions with reserved pg words
|
||||
data = append(data, result.Inputs[i])
|
||||
}
|
||||
pgStr = pgStr + ", returned) VALUES ($1, $2"
|
||||
data = append(data, result.Output)
|
||||
|
||||
// For each input entry we created we add its postgres command variable to the string
|
||||
for i := 0; i <= ml; i++ {
|
||||
pgStr = pgStr + fmt.Sprintf(", $%d", i+3)
|
||||
}
|
||||
pgStr = pgStr + ")"
|
||||
|
||||
// Add this query to the transaction
|
||||
_, err = tx.Exec(pgStr, data...)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// Checks for event table and creates it if it does not already exist
|
||||
func (r *methodRepository) CreateMethodTable(contractAddr string, method types.Method) (bool, error) {
|
||||
tableID := fmt.Sprintf("%s_%s.%s_method", r.mode.String(), strings.ToLower(contractAddr), strings.ToLower(method.Name))
|
||||
|
||||
// Check cache before querying pq to see if table exists
|
||||
_, ok := r.tables.Get(tableID)
|
||||
if ok {
|
||||
return false, nil
|
||||
}
|
||||
tableExists, err := r.checkForTable(contractAddr, method.Name)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !tableExists {
|
||||
err = r.newMethodTable(tableID, method)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
// Add schema name to cache
|
||||
r.tables.Add(tableID, true)
|
||||
|
||||
return !tableExists, nil
|
||||
}
|
||||
|
||||
// Creates a table for the given contract and event
|
||||
func (r *methodRepository) newMethodTable(tableID string, method types.Method) error {
|
||||
// Begin pg string
|
||||
pgStr := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ", tableID)
|
||||
pgStr = pgStr + "(id SERIAL, token_name CHARACTER VARYING(66) NOT NULL, block INTEGER NOT NULL,"
|
||||
|
||||
// Iterate over method inputs and outputs, using their name and pgType to grow the string
|
||||
for _, arg := range method.Args {
|
||||
pgStr = pgStr + fmt.Sprintf(" %s_ %s NOT NULL,", strings.ToLower(arg.Name), arg.PgType)
|
||||
}
|
||||
|
||||
pgStr = pgStr + fmt.Sprintf(" returned %s NOT NULL)", method.Return[0].PgType)
|
||||
|
||||
_, err := r.DB.Exec(pgStr)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Checks if a table already exists for the given contract and event
|
||||
func (r *methodRepository) checkForTable(contractAddr string, methodName string) (bool, error) {
|
||||
pgStr := fmt.Sprintf("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = '%s_%s' AND table_name = '%s_method')", r.mode.String(), strings.ToLower(contractAddr), strings.ToLower(methodName))
|
||||
var exists bool
|
||||
err := r.DB.Get(&exists, pgStr)
|
||||
|
||||
return exists, err
|
||||
}
|
||||
|
||||
// Checks for contract schema and creates it if it does not already exist
|
||||
func (r *methodRepository) CreateContractSchema(contractAddr string) (bool, error) {
|
||||
if contractAddr == "" {
|
||||
return false, errors.New("error: no contract address specified")
|
||||
}
|
||||
|
||||
// Check cache before querying pq to see if schema exists
|
||||
_, ok := r.schemas.Get(contractAddr)
|
||||
if ok {
|
||||
return false, nil
|
||||
}
|
||||
schemaExists, err := r.checkForSchema(contractAddr)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !schemaExists {
|
||||
err = r.newContractSchema(contractAddr)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
// Add schema name to cache
|
||||
r.schemas.Add(contractAddr, true)
|
||||
|
||||
return !schemaExists, nil
|
||||
}
|
||||
|
||||
// Creates a schema for the given contract
|
||||
func (r *methodRepository) newContractSchema(contractAddr string) error {
|
||||
_, err := r.DB.Exec("CREATE SCHEMA IF NOT EXISTS " + r.mode.String() + "_" + strings.ToLower(contractAddr))
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Checks if a schema already exists for the given contract
|
||||
func (r *methodRepository) checkForSchema(contractAddr string) (bool, error) {
|
||||
pgStr := fmt.Sprintf("SELECT EXISTS (SELECT schema_name FROM information_schema.schemata WHERE schema_name = '%s_%s')", r.mode.String(), strings.ToLower(contractAddr))
|
||||
|
||||
var exists bool
|
||||
err := r.DB.Get(&exists, pgStr)
|
||||
|
||||
return exists, err
|
||||
}
|
||||
|
||||
func (r *methodRepository) CheckSchemaCache(key string) (interface{}, bool) {
|
||||
return r.schemas.Get(key)
|
||||
}
|
||||
|
||||
func (r *methodRepository) CheckTableCache(key string) (interface{}, bool) {
|
||||
return r.tables.Get(key)
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
// 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 repository_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/repository"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
var _ = Describe("Repository", func() {
|
||||
var db *postgres.DB
|
||||
var dataStore repository.MethodRepository
|
||||
var con *contract.Contract
|
||||
var err error
|
||||
var mockResult types.Result
|
||||
var method types.Method
|
||||
|
||||
BeforeEach(func() {
|
||||
con = test_helpers.SetupTusdContract([]string{}, []string{"balanceOf"})
|
||||
Expect(len(con.Methods)).To(Equal(1))
|
||||
method = con.Methods[0]
|
||||
mockResult = types.Result{
|
||||
Method: method,
|
||||
PgType: method.Return[0].PgType,
|
||||
Inputs: make([]interface{}, 1),
|
||||
Output: new(interface{}),
|
||||
Block: 6707323,
|
||||
}
|
||||
mockResult.Inputs[0] = "0xfE9e8709d3215310075d67E3ed32A380CCf451C8"
|
||||
mockResult.Output = "66386309548896882859581786"
|
||||
db, _ = test_helpers.SetupDBandBC()
|
||||
dataStore = repository.NewMethodRepository(db, types.FullSync)
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
test_helpers.TearDown(db)
|
||||
})
|
||||
|
||||
Describe("Full Sync Mode", func() {
|
||||
BeforeEach(func() {
|
||||
dataStore = repository.NewMethodRepository(db, types.FullSync)
|
||||
})
|
||||
|
||||
Describe("CreateContractSchema", func() {
|
||||
It("Creates schema if it doesn't exist", func() {
|
||||
created, err := dataStore.CreateContractSchema(constants.TusdContractAddress)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateContractSchema(constants.TusdContractAddress)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Caches schema it creates so that it does not need to repeatedly query the database to check for it's existence", func() {
|
||||
_, ok := dataStore.CheckSchemaCache(con.Address)
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
v, ok := dataStore.CheckSchemaCache(con.Address)
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(v).To(Equal(true))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("CreateMethodTable", func() {
|
||||
It("Creates table if it doesn't exist", func() {
|
||||
created, err := dataStore.CreateContractSchema(constants.TusdContractAddress)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateMethodTable(constants.TusdContractAddress, method)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateMethodTable(constants.TusdContractAddress, method)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Caches table it creates so that it does not need to repeatedly query the database to check for it's existence", func() {
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
tableID := fmt.Sprintf("%s_%s.%s_method", types.FullSync, strings.ToLower(con.Address), strings.ToLower(method.Name))
|
||||
_, ok := dataStore.CheckTableCache(tableID)
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
created, err = dataStore.CreateMethodTable(con.Address, method)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
v, ok := dataStore.CheckTableCache(tableID)
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(v).To(Equal(true))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("PersistResult", func() {
|
||||
It("Persists result from method polling in custom pg table", func() {
|
||||
err = dataStore.PersistResults([]types.Result{mockResult}, method, con.Address, con.Name)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
scanStruct := test_helpers.BalanceOf{}
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM full_%s.balanceof_method", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
expectedLog := test_helpers.BalanceOf{
|
||||
Id: 1,
|
||||
TokenName: "TrueUSD",
|
||||
Block: 6707323,
|
||||
Address: "0xfE9e8709d3215310075d67E3ed32A380CCf451C8",
|
||||
Balance: "66386309548896882859581786",
|
||||
}
|
||||
Expect(scanStruct).To(Equal(expectedLog))
|
||||
})
|
||||
|
||||
It("Fails with empty result", func() {
|
||||
err = dataStore.PersistResults([]types.Result{}, method, con.Address, con.Name)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Light Sync Mode", func() {
|
||||
BeforeEach(func() {
|
||||
dataStore = repository.NewMethodRepository(db, types.LightSync)
|
||||
})
|
||||
|
||||
Describe("CreateContractSchema", func() {
|
||||
It("Creates schema if it doesn't exist", func() {
|
||||
created, err := dataStore.CreateContractSchema(constants.TusdContractAddress)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateContractSchema(constants.TusdContractAddress)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Caches schema it creates so that it does not need to repeatedly query the database to check for it's existence", func() {
|
||||
_, ok := dataStore.CheckSchemaCache(con.Address)
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
v, ok := dataStore.CheckSchemaCache(con.Address)
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(v).To(Equal(true))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("CreateMethodTable", func() {
|
||||
It("Creates table if it doesn't exist", func() {
|
||||
created, err := dataStore.CreateContractSchema(constants.TusdContractAddress)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateMethodTable(constants.TusdContractAddress, method)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
created, err = dataStore.CreateMethodTable(constants.TusdContractAddress, method)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Caches table it creates so that it does not need to repeatedly query the database to check for it's existence", func() {
|
||||
created, err := dataStore.CreateContractSchema(con.Address)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
tableID := fmt.Sprintf("%s_%s.%s_method", types.LightSync, strings.ToLower(con.Address), strings.ToLower(method.Name))
|
||||
_, ok := dataStore.CheckTableCache(tableID)
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
created, err = dataStore.CreateMethodTable(con.Address, method)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(created).To(Equal(true))
|
||||
|
||||
v, ok := dataStore.CheckTableCache(tableID)
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(v).To(Equal(true))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("PersistResult", func() {
|
||||
It("Persists result from method polling in custom pg table for light sync mode vDB", func() {
|
||||
err = dataStore.PersistResults([]types.Result{mockResult}, method, con.Address, con.Name)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
scanStruct := test_helpers.BalanceOf{}
|
||||
|
||||
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM light_%s.balanceof_method", constants.TusdContractAddress)).StructScan(&scanStruct)
|
||||
expectedLog := test_helpers.BalanceOf{
|
||||
Id: 1,
|
||||
TokenName: "TrueUSD",
|
||||
Block: 6707323,
|
||||
Address: "0xfE9e8709d3215310075d67E3ed32A380CCf451C8",
|
||||
Balance: "66386309548896882859581786",
|
||||
}
|
||||
Expect(scanStruct).To(Equal(expectedLog))
|
||||
})
|
||||
|
||||
It("Fails with empty result", func() {
|
||||
err = dataStore.PersistResults([]types.Result{}, method, con.Address, con.Name)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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 repository_test
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestRepository(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Shared Repository Suite Test")
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
logrus.SetOutput(ioutil.Discard)
|
||||
})
|
||||
@@ -0,0 +1,120 @@
|
||||
// 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 retriever
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
// Address retriever is used to retrieve the addresses associated with a contract
|
||||
type AddressRetriever interface {
|
||||
RetrieveTokenHolderAddresses(info contract.Contract) (map[common.Address]bool, error)
|
||||
}
|
||||
|
||||
type addressRetriever struct {
|
||||
db *postgres.DB
|
||||
mode types.Mode
|
||||
}
|
||||
|
||||
func NewAddressRetriever(db *postgres.DB, mode types.Mode) (r *addressRetriever) {
|
||||
return &addressRetriever{
|
||||
db: db,
|
||||
mode: mode,
|
||||
}
|
||||
}
|
||||
|
||||
// Method to retrieve list of token-holding/contract-related addresses by iterating over available events
|
||||
// This generic method should work whether or not the argument/input names of the events meet the expected standard
|
||||
// This could be generalized to iterate over ALL events and pull out any address arguments
|
||||
func (r *addressRetriever) RetrieveTokenHolderAddresses(info contract.Contract) (map[common.Address]bool, error) {
|
||||
addrList := make([]string, 0)
|
||||
|
||||
_, ok := info.Filters["Transfer"]
|
||||
if ok {
|
||||
addrs, err := r.retrieveTransferAddresses(info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addrList = append(addrList, addrs...)
|
||||
}
|
||||
|
||||
_, ok = info.Filters["Mint"]
|
||||
if ok {
|
||||
addrs, err := r.retrieveTokenMintees(info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addrList = append(addrList, addrs...)
|
||||
}
|
||||
|
||||
contractAddresses := make(map[common.Address]bool)
|
||||
for _, addr := range addrList {
|
||||
contractAddresses[common.HexToAddress(addr)] = true
|
||||
}
|
||||
|
||||
return contractAddresses, nil
|
||||
}
|
||||
|
||||
func (r *addressRetriever) retrieveTransferAddresses(con contract.Contract) ([]string, error) {
|
||||
transferAddrs := make([]string, 0)
|
||||
event := con.Events["Transfer"]
|
||||
|
||||
for _, field := range event.Fields { // Iterate over event fields, finding the ones with address type
|
||||
|
||||
if field.Type.T == abi.AddressTy { // If they have address type, retrieve those addresses
|
||||
addrs := make([]string, 0)
|
||||
pgStr := fmt.Sprintf("SELECT %s_ FROM %s_%s.%s_event", strings.ToLower(field.Name), r.mode.String(), strings.ToLower(con.Address), strings.ToLower(event.Name))
|
||||
err := r.db.Select(&addrs, pgStr)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
transferAddrs = append(transferAddrs, addrs...) // And append them to the growing list
|
||||
}
|
||||
}
|
||||
|
||||
return transferAddrs, nil
|
||||
}
|
||||
|
||||
func (r *addressRetriever) retrieveTokenMintees(con contract.Contract) ([]string, error) {
|
||||
mintAddrs := make([]string, 0)
|
||||
event := con.Events["Mint"]
|
||||
|
||||
for _, field := range event.Fields { // Iterate over event fields, finding the ones with address type
|
||||
|
||||
if field.Type.T == abi.AddressTy { // If they have address type, retrieve those addresses
|
||||
addrs := make([]string, 0)
|
||||
pgStr := fmt.Sprintf("SELECT %s_ FROM %s_%s.%s_event", strings.ToLower(field.Name), r.mode.String(), strings.ToLower(con.Address), strings.ToLower(event.Name))
|
||||
err := r.db.Select(&addrs, pgStr)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
mintAddrs = append(mintAddrs, addrs...) // And append them to the growing list
|
||||
}
|
||||
}
|
||||
|
||||
return mintAddrs, nil
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// 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 retriever_test
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/full/converter"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/repository"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/retriever"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
var mockEvent = core.WatchedEvent{
|
||||
Name: constants.TransferEvent.String(),
|
||||
BlockNumber: 5488076,
|
||||
Address: constants.TusdContractAddress,
|
||||
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
|
||||
Index: 110,
|
||||
Topic0: constants.TransferEvent.Signature(),
|
||||
Topic1: "0x000000000000000000000000000000000000000000000000000000000000af21",
|
||||
Topic2: "0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391",
|
||||
Topic3: "",
|
||||
Data: "0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe",
|
||||
}
|
||||
|
||||
var _ = Describe("Address Retriever Test", func() {
|
||||
var db *postgres.DB
|
||||
var dataStore repository.EventRepository
|
||||
var err error
|
||||
var info *contract.Contract
|
||||
var vulcanizeLogId int64
|
||||
var log *types.Log
|
||||
var r retriever.AddressRetriever
|
||||
var addresses map[common.Address]bool
|
||||
var wantedEvents = []string{"Transfer"}
|
||||
|
||||
BeforeEach(func() {
|
||||
db, info = test_helpers.SetupTusdRepo(&vulcanizeLogId, wantedEvents, []string{})
|
||||
mockEvent.LogID = vulcanizeLogId
|
||||
|
||||
event := info.Events["Transfer"]
|
||||
err = info.GenerateFilters()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
c := converter.NewConverter(info)
|
||||
log, err = c.Convert(mockEvent, event)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
dataStore = repository.NewEventRepository(db, types.FullSync)
|
||||
dataStore.PersistLogs([]types.Log{*log}, event, info.Address, info.Name)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
r = retriever.NewAddressRetriever(db, types.FullSync)
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
test_helpers.TearDown(db)
|
||||
})
|
||||
|
||||
Describe("RetrieveTokenHolderAddresses", func() {
|
||||
It("Retrieves a list of token holder addresses from persisted event logs", func() {
|
||||
addresses, err = r.RetrieveTokenHolderAddresses(*info)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, ok := addresses[common.HexToAddress("0x000000000000000000000000000000000000000000000000000000000000af21")]
|
||||
Expect(ok).To(Equal(true))
|
||||
|
||||
_, ok = addresses[common.HexToAddress("0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391")]
|
||||
Expect(ok).To(Equal(true))
|
||||
|
||||
_, ok = addresses[common.HexToAddress("0x")]
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
_, ok = addresses[common.HexToAddress(constants.TusdContractAddress)]
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
})
|
||||
|
||||
It("Returns empty list when empty contract info is used", func() {
|
||||
addresses, err = r.RetrieveTokenHolderAddresses(contract.Contract{})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(len(addresses)).To(Equal(0))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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 retriever_test
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestRetriever(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Address Retriever Suite Test")
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
logrus.SetOutput(ioutil.Discard)
|
||||
})
|
||||
@@ -0,0 +1,96 @@
|
||||
// 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 types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
Name string
|
||||
Anonymous bool
|
||||
Fields []Field
|
||||
}
|
||||
|
||||
type Field struct {
|
||||
abi.Argument // Name, Type, Indexed
|
||||
PgType string // Holds type used when committing data held in this field to postgres
|
||||
}
|
||||
|
||||
// Struct to hold instance of an event log data
|
||||
type Log struct {
|
||||
Id int64 // VulcanizeIdLog for full sync and header ID for light sync omni watcher
|
||||
Values map[string]string // Map of event input names to their values
|
||||
|
||||
// Used for full sync only
|
||||
Block int64
|
||||
Tx string
|
||||
|
||||
// Used for lightSync only
|
||||
LogIndex uint
|
||||
TransactionIndex uint
|
||||
Raw []byte // json.Unmarshalled byte array of geth/core/types.Log{}
|
||||
}
|
||||
|
||||
// Unpack abi.Event into our custom Event struct
|
||||
func NewEvent(e abi.Event) Event {
|
||||
fields := make([]Field, len(e.Inputs))
|
||||
for i, input := range e.Inputs {
|
||||
fields[i] = Field{}
|
||||
fields[i].Name = input.Name
|
||||
fields[i].Type = input.Type
|
||||
fields[i].Indexed = input.Indexed
|
||||
// Fill in pg type based on abi type
|
||||
switch fields[i].Type.T {
|
||||
case abi.HashTy, abi.AddressTy:
|
||||
fields[i].PgType = "CHARACTER VARYING(66)"
|
||||
case abi.IntTy, abi.UintTy:
|
||||
fields[i].PgType = "NUMERIC"
|
||||
case abi.BoolTy:
|
||||
fields[i].PgType = "BOOLEAN"
|
||||
case abi.BytesTy, abi.FixedBytesTy:
|
||||
fields[i].PgType = "BYTEA"
|
||||
case abi.ArrayTy:
|
||||
fields[i].PgType = "TEXT[]"
|
||||
case abi.FixedPointTy:
|
||||
fields[i].PgType = "MONEY" // use shopspring/decimal for fixed point numbers in go and money type in postgres?
|
||||
default:
|
||||
fields[i].PgType = "TEXT"
|
||||
}
|
||||
}
|
||||
|
||||
return Event{
|
||||
Name: e.Name,
|
||||
Anonymous: e.Anonymous,
|
||||
Fields: fields,
|
||||
}
|
||||
}
|
||||
|
||||
func (e Event) Sig() common.Hash {
|
||||
types := make([]string, len(e.Fields))
|
||||
|
||||
for i, input := range e.Fields {
|
||||
types[i] = input.Type.String()
|
||||
}
|
||||
|
||||
return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ",")))))
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
// 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 types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
)
|
||||
|
||||
type Method struct {
|
||||
Name string
|
||||
Const bool
|
||||
Args []Field
|
||||
Return []Field
|
||||
}
|
||||
|
||||
// Struct to hold instance of result from method call with given inputs and block
|
||||
type Result struct {
|
||||
Method
|
||||
Inputs []interface{} // Will only use addresses
|
||||
Output interface{}
|
||||
PgType string // Holds output pg type
|
||||
Block int64
|
||||
}
|
||||
|
||||
// Unpack abi.Method into our custom Method struct
|
||||
func NewMethod(m abi.Method) Method {
|
||||
inputs := make([]Field, len(m.Inputs))
|
||||
for i, input := range m.Inputs {
|
||||
inputs[i] = Field{}
|
||||
inputs[i].Name = input.Name
|
||||
inputs[i].Type = input.Type
|
||||
inputs[i].Indexed = input.Indexed
|
||||
switch inputs[i].Type.T {
|
||||
case abi.HashTy, abi.AddressTy:
|
||||
inputs[i].PgType = "CHARACTER VARYING(66)"
|
||||
case abi.IntTy, abi.UintTy:
|
||||
inputs[i].PgType = "NUMERIC"
|
||||
case abi.BoolTy:
|
||||
inputs[i].PgType = "BOOLEAN"
|
||||
case abi.BytesTy, abi.FixedBytesTy:
|
||||
inputs[i].PgType = "BYTEA"
|
||||
case abi.ArrayTy:
|
||||
inputs[i].PgType = "TEXT[]"
|
||||
case abi.FixedPointTy:
|
||||
inputs[i].PgType = "MONEY" // use shopspring/decimal for fixed point numbers in go and money type in postgres?
|
||||
default:
|
||||
inputs[i].PgType = "TEXT"
|
||||
}
|
||||
}
|
||||
|
||||
outputs := make([]Field, len(m.Outputs))
|
||||
for i, output := range m.Outputs {
|
||||
outputs[i] = Field{}
|
||||
outputs[i].Name = output.Name
|
||||
outputs[i].Type = output.Type
|
||||
outputs[i].Indexed = output.Indexed
|
||||
switch outputs[i].Type.T {
|
||||
case abi.HashTy, abi.AddressTy:
|
||||
outputs[i].PgType = "CHARACTER VARYING(66)"
|
||||
case abi.IntTy, abi.UintTy:
|
||||
outputs[i].PgType = "NUMERIC"
|
||||
case abi.BoolTy:
|
||||
outputs[i].PgType = "BOOLEAN"
|
||||
case abi.BytesTy, abi.FixedBytesTy:
|
||||
outputs[i].PgType = "BYTEA"
|
||||
case abi.ArrayTy:
|
||||
outputs[i].PgType = "TEXT[]"
|
||||
case abi.FixedPointTy:
|
||||
outputs[i].PgType = "MONEY" // use shopspring/decimal for fixed point numbers in go and money type in postgres?
|
||||
default:
|
||||
outputs[i].PgType = "TEXT"
|
||||
}
|
||||
}
|
||||
|
||||
return Method{
|
||||
Name: m.Name,
|
||||
Const: m.Const,
|
||||
Args: inputs,
|
||||
Return: outputs,
|
||||
}
|
||||
}
|
||||
|
||||
func (m Method) Sig() common.Hash {
|
||||
types := make([]string, len(m.Args))
|
||||
i := 0
|
||||
for _, arg := range m.Args {
|
||||
types[i] = arg.Type.String()
|
||||
i++
|
||||
}
|
||||
|
||||
return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", m.Name, strings.Join(types, ",")))))
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// 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 types
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Mode int
|
||||
|
||||
const (
|
||||
LightSync Mode = iota
|
||||
FullSync
|
||||
)
|
||||
|
||||
func (mode Mode) IsValid() bool {
|
||||
return mode >= LightSync && mode <= FullSync
|
||||
}
|
||||
|
||||
func (mode Mode) String() string {
|
||||
switch mode {
|
||||
case LightSync:
|
||||
return "light"
|
||||
case FullSync:
|
||||
return "full"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func (mode Mode) MarshalText() ([]byte, error) {
|
||||
switch mode {
|
||||
case LightSync:
|
||||
return []byte("light"), nil
|
||||
case FullSync:
|
||||
return []byte("full"), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("omni watcher: unknown mode %d, want LightSync or FullSync", mode)
|
||||
}
|
||||
}
|
||||
|
||||
func (mode *Mode) UnmarshalText(text []byte) error {
|
||||
switch string(text) {
|
||||
case "light":
|
||||
*mode = LightSync
|
||||
case "full":
|
||||
*mode = FullSync
|
||||
default:
|
||||
return fmt.Errorf(`omni watcher: unknown mode %q, want "light" or "full"`, text)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user