From aa4e698240d3db270c6e18dcbe2e05b218d667aa Mon Sep 17 00:00:00 2001 From: Ian Norden Date: Tue, 15 Jan 2019 15:49:18 -0600 Subject: [PATCH] move interface_getter.go from ens_watcher to vDB pkging- used to construct a custom ABI based on results from calls to supportsInterface; also moved fetcher.go from examples to pkg --- examples/erc20_watcher/every_block/getter.go | 6 +- examples/generic/every_block/getter.go | 6 +- pkg/omni/shared/constants/constants.go | 1 + pkg/omni/shared/constants/interface.go | 127 ++++++++++++++++++ .../omni/shared/fetcher}/fetcher.go | 2 +- pkg/omni/shared/getter/getter_suite_test.go | 35 +++++ pkg/omni/shared/getter/getter_test.go | 55 ++++++++ pkg/omni/shared/getter/interface_getter.go | 105 +++++++++++++++ .../shared/helpers/test_helpers/database.go | 7 +- pkg/omni/shared/parser/parser.go | 10 ++ 10 files changed, 342 insertions(+), 12 deletions(-) create mode 100644 pkg/omni/shared/constants/interface.go rename {examples/generic => pkg/omni/shared/fetcher}/fetcher.go (99%) create mode 100644 pkg/omni/shared/getter/getter_suite_test.go create mode 100644 pkg/omni/shared/getter/getter_test.go create mode 100644 pkg/omni/shared/getter/interface_getter.go diff --git a/examples/erc20_watcher/every_block/getter.go b/examples/erc20_watcher/every_block/getter.go index 7afbf3bb..5df9da0b 100644 --- a/examples/erc20_watcher/every_block/getter.go +++ b/examples/erc20_watcher/every_block/getter.go @@ -19,8 +19,8 @@ package every_block import ( "math/big" - "github.com/vulcanize/vulcanizedb/examples/generic" "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/omni/shared/fetcher" ) // Getter serves as a higher level data fetcher that invokes its underlying Fetcher methods for a given contract method @@ -35,13 +35,13 @@ type ERC20GetterInterface interface { // Getter struct type ERC20Getter struct { - generic.Fetcher + fetcher.Fetcher } // Initializes and returns a Getter with the given blockchain func NewGetter(blockChain core.BlockChain) ERC20Getter { return ERC20Getter{ - Fetcher: generic.Fetcher{ + Fetcher: fetcher.Fetcher{ BlockChain: blockChain, }, } diff --git a/examples/generic/every_block/getter.go b/examples/generic/every_block/getter.go index e8f1e80c..f3a228be 100644 --- a/examples/generic/every_block/getter.go +++ b/examples/generic/every_block/getter.go @@ -17,12 +17,12 @@ package every_block import ( - "github.com/vulcanize/vulcanizedb/examples/generic" "math/big" "github.com/ethereum/go-ethereum/common" "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/omni/shared/fetcher" ) // Getter serves as a higher level data fetcher that invokes its underlying Fetcher methods for a given contract method @@ -41,13 +41,13 @@ type GenericGetterInterface interface { // Getter struct type GenericGetter struct { - generic.Fetcher // Underlying Fetcher + fetcher.Fetcher // Underlying Fetcher } // Initializes and returns a Getter with the given blockchain func NewGetter(blockChain core.BlockChain) GenericGetter { return GenericGetter{ - Fetcher: generic.Fetcher{ + Fetcher: fetcher.Fetcher{ BlockChain: blockChain, }, } diff --git a/pkg/omni/shared/constants/constants.go b/pkg/omni/shared/constants/constants.go index 97ed2037..594e0fce 100644 --- a/pkg/omni/shared/constants/constants.go +++ b/pkg/omni/shared/constants/constants.go @@ -70,6 +70,7 @@ func (e Event) Signature() string { var DaiContractAddress = "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359" var TusdContractAddress = "0x8dd5fbCe2F6a956C3022bA3663759011Dd51e73E" var EnsContractAddress = "0x314159265dD8dbb310642f98f50C066173C1259b" +var PublicResolverAddress = "0x1da022710dF5002339274AaDEe8D58218e9D6AB5" // Contract Owner var DaiContractOwner = "0x0000000000000000000000000000000000000000" diff --git a/pkg/omni/shared/constants/interface.go b/pkg/omni/shared/constants/interface.go new file mode 100644 index 00000000..a1bf56b0 --- /dev/null +++ b/pkg/omni/shared/constants/interface.go @@ -0,0 +1,127 @@ +// VulcanizeDB +// Copyright © 2018 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 . + +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] +} diff --git a/examples/generic/fetcher.go b/pkg/omni/shared/fetcher/fetcher.go similarity index 99% rename from examples/generic/fetcher.go rename to pkg/omni/shared/fetcher/fetcher.go index 2f083c72..f481b6aa 100644 --- a/examples/generic/fetcher.go +++ b/pkg/omni/shared/fetcher/fetcher.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package generic +package fetcher import ( "fmt" diff --git a/pkg/omni/shared/getter/getter_suite_test.go b/pkg/omni/shared/getter/getter_suite_test.go new file mode 100644 index 00000000..78fdc7bf --- /dev/null +++ b/pkg/omni/shared/getter/getter_suite_test.go @@ -0,0 +1,35 @@ +// VulcanizeDB +// Copyright © 2018 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 . + +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) +}) diff --git a/pkg/omni/shared/getter/getter_test.go b/pkg/omni/shared/getter/getter_test.go new file mode 100644 index 00000000..1c440972 --- /dev/null +++ b/pkg/omni/shared/getter/getter_test.go @@ -0,0 +1,55 @@ +// VulcanizeDB +// Copyright © 2018 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 . + +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/geth" + "github.com/vulcanize/vulcanizedb/pkg/geth/client" + rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc" + "github.com/vulcanize/vulcanizedb/pkg/geth/node" + "github.com/vulcanize/vulcanizedb/pkg/omni/shared/constants" + "github.com/vulcanize/vulcanizedb/pkg/omni/shared/getter" +) + +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()) + }) + }) +}) diff --git a/pkg/omni/shared/getter/interface_getter.go b/pkg/omni/shared/getter/interface_getter.go new file mode 100644 index 00000000..a49ac669 --- /dev/null +++ b/pkg/omni/shared/getter/interface_getter.go @@ -0,0 +1,105 @@ +// VulcanizeDB +// Copyright © 2018 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 . + +package getter + +import ( + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/omni/shared/constants" + "github.com/vulcanize/vulcanizedb/pkg/omni/shared/fetcher" +) + +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 +} diff --git a/pkg/omni/shared/helpers/test_helpers/database.go b/pkg/omni/shared/helpers/test_helpers/database.go index 75db6497..3bea427b 100644 --- a/pkg/omni/shared/helpers/test_helpers/database.go +++ b/pkg/omni/shared/helpers/test_helpers/database.go @@ -245,9 +245,6 @@ func TearDown(db *postgres.DB) { _, err = tx.Exec(`DELETE FROM headers`) Expect(err).NotTo(HaveOccurred()) - _, err = tx.Exec(`DELETE FROM checked_headers`) - Expect(err).NotTo(HaveOccurred()) - _, err = tx.Exec(`DELETE FROM logs`) Expect(err).NotTo(HaveOccurred()) @@ -260,10 +257,10 @@ func TearDown(db *postgres.DB) { _, err = tx.Exec(`DELETE FROM receipts`) Expect(err).NotTo(HaveOccurred()) - _, err = tx.Exec(`DROP TABLE public.checked_headers`) + _, err = tx.Exec(`DROP TABLE checked_headers`) Expect(err).NotTo(HaveOccurred()) - _, err = tx.Exec(`CREATE TABLE public.checked_headers (id SERIAL PRIMARY KEY, header_id INTEGER UNIQUE NOT NULL REFERENCES headers (id) ON DELETE CASCADE);`) + _, 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`) diff --git a/pkg/omni/shared/parser/parser.go b/pkg/omni/shared/parser/parser.go index 85e4a50f..14860f43 100644 --- a/pkg/omni/shared/parser/parser.go +++ b/pkg/omni/shared/parser/parser.go @@ -31,6 +31,7 @@ import ( // 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 @@ -83,6 +84,15 @@ func (p *parser) Parse(contractAddr string) error { 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