goimports -w; golinting, remove some unused code
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -20,11 +20,10 @@ import (
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
|
||||
// Basic abi needed to check which interfaces are adhered to
|
||||
// SupportsInterfaceABI is the 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 SupportsInterface = `{"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"}`
|
||||
@@ -34,11 +33,10 @@ var TextChangeInterface = `{"anonymous":false,"inputs":[{"indexed":true,"name":"
|
||||
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
|
||||
|
||||
// Interface enums
|
||||
const (
|
||||
MetaSig Interface = iota
|
||||
AddrChangeSig
|
||||
@@ -51,6 +49,7 @@ const (
|
||||
ContentHashChangeSig
|
||||
)
|
||||
|
||||
// Hex returns the hex signature for an interface
|
||||
func (e Interface) Hex() string {
|
||||
strings := [...]string{
|
||||
"0x01ffc9a7",
|
||||
@@ -71,6 +70,7 @@ func (e Interface) Hex() string {
|
||||
return strings[e]
|
||||
}
|
||||
|
||||
// Bytes returns the bytes signature for an interface
|
||||
func (e Interface) Bytes() [4]uint8 {
|
||||
if e < MetaSig || e > ContentHashChangeSig {
|
||||
return [4]byte{}
|
||||
@@ -86,6 +86,7 @@ func (e Interface) Bytes() [4]uint8 {
|
||||
return byArray
|
||||
}
|
||||
|
||||
// EventSig returns the event signature for an interface
|
||||
func (e Interface) EventSig() string {
|
||||
strings := [...]string{
|
||||
"",
|
||||
@@ -106,6 +107,7 @@ func (e Interface) EventSig() string {
|
||||
return strings[e]
|
||||
}
|
||||
|
||||
// MethodSig returns the method signature for an interface
|
||||
func (e Interface) MethodSig() string {
|
||||
strings := [...]string{
|
||||
"supportsInterface(bytes4)",
|
||||
|
||||
@@ -48,6 +48,7 @@ type Contract struct {
|
||||
Piping bool // Whether or not to pipe method results forward as arguments to subsequent methods
|
||||
}
|
||||
|
||||
// Init initializes a contract object
|
||||
// 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 {
|
||||
@@ -66,7 +67,7 @@ func (c Contract) Init() *Contract {
|
||||
return &c
|
||||
}
|
||||
|
||||
// Use contract info to generate event filters - full sync contract watcher only
|
||||
// GenerateFilters uses contract info to generate event filters - full sync contract watcher only
|
||||
func (c *Contract) GenerateFilters() error {
|
||||
c.Filters = map[string]filters.LogFilter{}
|
||||
|
||||
@@ -87,7 +88,7 @@ func (c *Contract) GenerateFilters() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns true if address is in list of arguments to
|
||||
// WantedEventArg 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 {
|
||||
@@ -101,7 +102,7 @@ func (c *Contract) WantedEventArg(arg string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Returns true if address is in list of arguments to
|
||||
// WantedMethodArg 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 {
|
||||
@@ -121,7 +122,7 @@ func (c *Contract) WantedMethodArg(arg interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Returns true if any mapping value matches filtered for address or if no filter exists
|
||||
// PassesEventFilter 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 {
|
||||
@@ -133,7 +134,7 @@ func (c *Contract) PassesEventFilter(args map[string]string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Add event emitted address to our list if it passes filter and method polling is on
|
||||
// AddEmittedAddr adds event emitted addresses 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 {
|
||||
@@ -142,7 +143,7 @@ func (c *Contract) AddEmittedAddr(addresses ...interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// Add event emitted hash to our list if it passes filter and method polling is on
|
||||
// AddEmittedHash adds event emitted hashes 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 {
|
||||
@@ -151,6 +152,7 @@ func (c *Contract) AddEmittedHash(hashes ...interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// StringifyArg resolves a method argument type to string type
|
||||
func StringifyArg(arg interface{}) (str string) {
|
||||
switch arg.(type) {
|
||||
case string:
|
||||
|
||||
@@ -29,7 +29,7 @@ import (
|
||||
// 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
|
||||
// FetcherInterface is the 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)
|
||||
@@ -56,14 +56,14 @@ type fetcherError struct {
|
||||
fetchMethod string
|
||||
}
|
||||
|
||||
// Fetcher error method
|
||||
// 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
|
||||
// FetchBigInt is the 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)
|
||||
@@ -75,7 +75,7 @@ func (f Fetcher) FetchBigInt(method, contractAbi, contractAddress string, blockN
|
||||
return *result, nil
|
||||
}
|
||||
|
||||
// Method used to fetch bool value from contract
|
||||
// FetchBool is the 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)
|
||||
@@ -87,7 +87,7 @@ func (f Fetcher) FetchBool(method, contractAbi, contractAddress string, blockNum
|
||||
return *result, nil
|
||||
}
|
||||
|
||||
// Method used to fetch address value from contract
|
||||
// FetchAddress is the 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)
|
||||
@@ -99,7 +99,7 @@ func (f Fetcher) FetchAddress(method, contractAbi, contractAddress string, block
|
||||
return *result, nil
|
||||
}
|
||||
|
||||
// Method used to fetch string value from contract
|
||||
// FetchString is the 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)
|
||||
@@ -111,7 +111,7 @@ func (f Fetcher) FetchString(method, contractAbi, contractAddress string, blockN
|
||||
return *result, nil
|
||||
}
|
||||
|
||||
// Method used to fetch hash value from contract
|
||||
// FetchHash is the 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)
|
||||
|
||||
@@ -17,12 +17,12 @@
|
||||
package getter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/fetcher"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
// InterfaceGetter is used to derive the interface of a contract
|
||||
type InterfaceGetter interface {
|
||||
GetABI(resolverAddr string, blockNumber int64) string
|
||||
GetBlockChain() core.BlockChain
|
||||
@@ -32,7 +32,8 @@ type interfaceGetter struct {
|
||||
fetcher.Fetcher
|
||||
}
|
||||
|
||||
func NewInterfaceGetter(blockChain core.BlockChain) *interfaceGetter {
|
||||
// NewInterfaceGetter returns a new InterfaceGetter
|
||||
func NewInterfaceGetter(blockChain core.BlockChain) InterfaceGetter {
|
||||
return &interfaceGetter{
|
||||
Fetcher: fetcher.Fetcher{
|
||||
BlockChain: blockChain,
|
||||
@@ -40,19 +41,15 @@ func NewInterfaceGetter(blockChain core.BlockChain) *interfaceGetter {
|
||||
}
|
||||
}
|
||||
|
||||
// Used to construct a custom ABI based on the results from calling supportsInterface
|
||||
func (g *interfaceGetter) GetABI(resolverAddr string, blockNumber int64) (string, error) {
|
||||
// GetABI is 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 {
|
||||
return "", fmt.Errorf("call to getSupportsInterface failed: %v", err)
|
||||
if err != nil || !supports {
|
||||
return ""
|
||||
}
|
||||
if !supports {
|
||||
return "", fmt.Errorf("contract does not support interface")
|
||||
}
|
||||
|
||||
abiStr := `[`
|
||||
args[0] = constants.AddrChangeSig.Bytes()
|
||||
supports, err = g.getSupportsInterface(a, resolverAddr, blockNumber, args)
|
||||
@@ -96,7 +93,7 @@ func (g *interfaceGetter) GetABI(resolverAddr string, blockNumber int64) (string
|
||||
}
|
||||
abiStr = abiStr[:len(abiStr)-1] + `]`
|
||||
|
||||
return abiStr, nil
|
||||
return abiStr
|
||||
}
|
||||
|
||||
// Use this method to check whether or not a contract supports a given method/event interface
|
||||
@@ -104,7 +101,7 @@ func (g *interfaceGetter) getSupportsInterface(contractAbi, contractAddress stri
|
||||
return g.Fetcher.FetchBool("supportsInterface", contractAbi, contractAddress, blockNumber, methodArgs)
|
||||
}
|
||||
|
||||
// Method to retrieve the Getter's blockchain
|
||||
// GetBlockChain is a method to retrieve the Getter's blockchain
|
||||
func (g *interfaceGetter) GetBlockChain() core.BlockChain {
|
||||
return g.Fetcher.BlockChain
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
// ConvertToLog converts a watched event to a log
|
||||
func ConvertToLog(watchedEvent core.WatchedEvent) types.Log {
|
||||
allTopics := []string{watchedEvent.Topic0, watchedEvent.Topic1, watchedEvent.Topic2, watchedEvent.Topic3}
|
||||
var nonNilTopics []string
|
||||
@@ -56,12 +57,14 @@ func createTopics(topics ...string) []common.Hash {
|
||||
return topicsArray
|
||||
}
|
||||
|
||||
// BigFromString creates a big.Int from a string
|
||||
func BigFromString(n string) *big.Int {
|
||||
b := new(big.Int)
|
||||
b.SetString(n, 10)
|
||||
return b
|
||||
}
|
||||
|
||||
// GenerateSignature returns the keccak256 hash hex of a string
|
||||
func GenerateSignature(s string) string {
|
||||
eventSignature := []byte(s)
|
||||
hash := crypto.Keccak256Hash(eventSignature)
|
||||
|
||||
@@ -45,7 +45,8 @@ type parser struct {
|
||||
parsedAbi abi.ABI
|
||||
}
|
||||
|
||||
func NewParser(network string) *parser {
|
||||
// NewParser returns a new Parser
|
||||
func NewParser(network string) Parser {
|
||||
url := eth.GenURL(network)
|
||||
|
||||
return &parser{
|
||||
@@ -53,15 +54,17 @@ func NewParser(network string) *parser {
|
||||
}
|
||||
}
|
||||
|
||||
// Abi returns the parser's configured abi string
|
||||
func (p *parser) Abi() string {
|
||||
return p.abi
|
||||
}
|
||||
|
||||
// ParsedAbi returns the parser's parsed abi
|
||||
func (p *parser) ParsedAbi() abi.ABI {
|
||||
return p.parsedAbi
|
||||
}
|
||||
|
||||
// Retrieves and parses the abi string
|
||||
// Parse 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
|
||||
@@ -84,7 +87,7 @@ func (p *parser) Parse(contractAddr string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Loads and parses an abi from a given abi string
|
||||
// ParseAbiStr loads and parses an abi from a given abi string
|
||||
func (p *parser) ParseAbiStr(abiStr string) error {
|
||||
var err error
|
||||
p.abi = abiStr
|
||||
@@ -94,14 +97,14 @@ func (p *parser) ParseAbiStr(abiStr string) error {
|
||||
}
|
||||
|
||||
func (p *parser) lookUp(contractAddr string) (string, error) {
|
||||
if v, ok := constants.Abis[common.HexToAddress(contractAddr)]; ok {
|
||||
if v, ok := constants.ABIs[common.HexToAddress(contractAddr)]; ok {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
return "", errors.New("ABI not present in lookup table")
|
||||
}
|
||||
|
||||
// Returns only specified methods, if they meet the criteria
|
||||
// GetSelectMethods 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 {
|
||||
@@ -121,7 +124,7 @@ func (p *parser) GetSelectMethods(wanted []string) []types.Method {
|
||||
return methods
|
||||
}
|
||||
|
||||
// Returns wanted methods
|
||||
// GetMethods 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 {
|
||||
@@ -139,7 +142,7 @@ func (p *parser) GetMethods(wanted []string) []types.Method {
|
||||
return methods
|
||||
}
|
||||
|
||||
// Returns wanted events as map of types.Events
|
||||
// GetEvents 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 {
|
||||
|
||||
@@ -33,6 +33,7 @@ import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
// Poller is the interface for polling public contract methods
|
||||
type Poller interface {
|
||||
PollContract(con contract.Contract, lastBlock int64) error
|
||||
PollContractAt(con contract.Contract, blockNumber int64) error
|
||||
@@ -45,13 +46,15 @@ type poller struct {
|
||||
contract contract.Contract
|
||||
}
|
||||
|
||||
func NewPoller(blockChain core.BlockChain, db *postgres.DB, mode types.Mode) *poller {
|
||||
// NewPoller returns a new Poller
|
||||
func NewPoller(blockChain core.BlockChain, db *postgres.DB, mode types.Mode) Poller {
|
||||
return &poller{
|
||||
MethodRepository: repository.NewMethodRepository(db, mode),
|
||||
bc: blockChain,
|
||||
}
|
||||
}
|
||||
|
||||
// PollContract polls a contract's public methods from the contracts starting block to specified last block
|
||||
func (p *poller) PollContract(con contract.Contract, lastBlock int64) error {
|
||||
for i := con.StartingBlock; i <= lastBlock; i++ {
|
||||
if err := p.PollContractAt(con, i); err != nil {
|
||||
@@ -62,6 +65,7 @@ func (p *poller) PollContract(con contract.Contract, lastBlock int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PollContractAt polls a contract's public getter methods at the specified block height
|
||||
func (p *poller) PollContractAt(con contract.Contract, blockNumber int64) error {
|
||||
p.contract = con
|
||||
for _, m := range con.Methods {
|
||||
@@ -98,7 +102,7 @@ func (p *poller) pollNoArgAt(m types.Method, bn int64) error {
|
||||
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))
|
||||
return fmt.Errorf("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 {
|
||||
@@ -112,7 +116,7 @@ func (p *poller) pollNoArgAt(m types.Method, bn int64) error {
|
||||
// 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 fmt.Errorf("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
|
||||
@@ -148,7 +152,7 @@ func (p *poller) pollSingleArgAt(m types.Method, bn int64) error {
|
||||
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))
|
||||
return fmt.Errorf("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 {
|
||||
@@ -164,7 +168,7 @@ func (p *poller) pollSingleArgAt(m types.Method, bn int64) error {
|
||||
// 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 fmt.Errorf("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
|
||||
@@ -212,7 +216,7 @@ func (p *poller) pollDoubleArgAt(m types.Method, bn int64) error {
|
||||
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))
|
||||
return fmt.Errorf("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 {
|
||||
@@ -228,13 +232,13 @@ func (p *poller) pollDoubleArgAt(m types.Method, bn int64) error {
|
||||
|
||||
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 fmt.Errorf("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
|
||||
// FetchContractData 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)
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ const (
|
||||
eventCacheSize = 1000
|
||||
)
|
||||
|
||||
// Event repository is used to persist event data into custom tables
|
||||
// EventRepository 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)
|
||||
@@ -51,7 +51,8 @@ type eventRepository struct {
|
||||
tables *lru.Cache // Cache names of recently used tables to minimize db connections
|
||||
}
|
||||
|
||||
func NewEventRepository(db *postgres.DB, mode types.Mode) *eventRepository {
|
||||
// NewEventRepository returns a new EventRepository
|
||||
func NewEventRepository(db *postgres.DB, mode types.Mode) EventRepository {
|
||||
ccs, _ := lru.New(contractCacheSize)
|
||||
ecs, _ := lru.New(eventCacheSize)
|
||||
return &eventRepository{
|
||||
@@ -62,7 +63,7 @@ func NewEventRepository(db *postgres.DB, mode types.Mode) *eventRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a schema for the contract if needed
|
||||
// PersistLogs 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 {
|
||||
@@ -112,7 +113,7 @@ func (r *eventRepository) persistHeaderSyncLogs(logs []types.Log, eventInfo type
|
||||
// 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,
|
||||
event.ID,
|
||||
contractName,
|
||||
event.Raw,
|
||||
event.LogIndex,
|
||||
@@ -144,8 +145,8 @@ func (r *eventRepository) persistHeaderSyncLogs(logs []types.Log, eventInfo type
|
||||
}
|
||||
|
||||
// Mark header as checked for this eventId
|
||||
eventId := strings.ToLower(eventInfo.Name + "_" + contractAddr)
|
||||
markCheckedErr := repository.MarkContractWatcherHeaderCheckedInTransaction(logs[0].Id, tx, eventId) // This assumes all logs are from same block
|
||||
eventID := strings.ToLower(eventInfo.Name + "_" + contractAddr)
|
||||
markCheckedErr := repository.MarkContractWatcherHeaderCheckedInTransaction(logs[0].ID, tx, eventID) // This assumes all logs are from same block
|
||||
if markCheckedErr != nil {
|
||||
rollbackErr := tx.Rollback()
|
||||
if rollbackErr != nil {
|
||||
@@ -171,7 +172,7 @@ func (r *eventRepository) persistFullSyncLogs(logs []types.Log, eventInfo types.
|
||||
|
||||
data := make([]interface{}, 0, 4+el)
|
||||
data = append(data,
|
||||
event.Id,
|
||||
event.ID,
|
||||
contractName,
|
||||
event.Block,
|
||||
event.Tx)
|
||||
@@ -201,7 +202,7 @@ func (r *eventRepository) persistFullSyncLogs(logs []types.Log, eventInfo types.
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// Checks for event table and creates it if it does not already exist
|
||||
// CreateEventTable 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))
|
||||
@@ -270,7 +271,7 @@ func (r *eventRepository) checkForTable(contractAddr string, eventName string) (
|
||||
return exists, err
|
||||
}
|
||||
|
||||
// Checks for contract schema and creates it if it does not already exist
|
||||
// CreateContractSchema 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 == "" {
|
||||
@@ -316,10 +317,12 @@ func (r *eventRepository) checkForSchema(contractAddr string) (bool, error) {
|
||||
return exists, err
|
||||
}
|
||||
|
||||
// CheckSchemaCache is used to query the schema name cache
|
||||
func (r *eventRepository) CheckSchemaCache(key string) (interface{}, bool) {
|
||||
return r.schemas.Get(key)
|
||||
}
|
||||
|
||||
// CheckTableCache is used to query the table name cache
|
||||
func (r *eventRepository) CheckTableCache(key string) (interface{}, bool) {
|
||||
return r.tables.Get(key)
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import (
|
||||
|
||||
const methodCacheSize = 1000
|
||||
|
||||
// MethodRepository is used to persist public getter method data
|
||||
type MethodRepository interface {
|
||||
PersistResults(results []types.Result, methodInfo types.Method, contractAddr, contractName string) error
|
||||
CreateMethodTable(contractAddr string, method types.Method) (bool, error)
|
||||
@@ -45,7 +46,8 @@ type methodRepository struct {
|
||||
tables *lru.Cache // Cache names of recently used tables to minimize db connections
|
||||
}
|
||||
|
||||
func NewMethodRepository(db *postgres.DB, mode types.Mode) *methodRepository {
|
||||
// NewMethodRepository returns a new MethodRepository
|
||||
func NewMethodRepository(db *postgres.DB, mode types.Mode) MethodRepository {
|
||||
ccs, _ := lru.New(contractCacheSize)
|
||||
mcs, _ := lru.New(methodCacheSize)
|
||||
return &methodRepository{
|
||||
@@ -56,7 +58,7 @@ func NewMethodRepository(db *postgres.DB, mode types.Mode) *methodRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a schema for the contract if needed
|
||||
// PersistResults 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 {
|
||||
@@ -124,7 +126,7 @@ func (r *methodRepository) persistResults(results []types.Result, methodInfo typ
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// Checks for event table and creates it if it does not already exist
|
||||
// CreateMethodTable 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))
|
||||
|
||||
@@ -177,7 +179,7 @@ func (r *methodRepository) checkForTable(contractAddr string, methodName string)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
// Checks for contract schema and creates it if it does not already exist
|
||||
// CreateContractSchema 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")
|
||||
@@ -222,10 +224,12 @@ func (r *methodRepository) checkForSchema(contractAddr string) (bool, error) {
|
||||
return exists, err
|
||||
}
|
||||
|
||||
// CheckSchemaCache is used to query the schema name cache
|
||||
func (r *methodRepository) CheckSchemaCache(key string) (interface{}, bool) {
|
||||
return r.schemas.Get(key)
|
||||
}
|
||||
|
||||
// CheckTableCache is used to query the table name cache
|
||||
func (r *methodRepository) CheckTableCache(key string) (interface{}, bool) {
|
||||
return r.tables.Get(key)
|
||||
}
|
||||
|
||||
@@ -17,12 +17,12 @@
|
||||
package repository_test
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TestRepository(t *testing.T) {
|
||||
|
||||
@@ -18,17 +18,17 @@ 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/contract_watcher/shared/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
// Address retriever is used to retrieve the addresses associated with a contract
|
||||
// AddressRetriever is used to retrieve the addresses associated with a contract
|
||||
type AddressRetriever interface {
|
||||
RetrieveTokenHolderAddresses(info contract.Contract) (map[common.Address]bool, error)
|
||||
}
|
||||
@@ -38,14 +38,15 @@ type addressRetriever struct {
|
||||
mode types.Mode
|
||||
}
|
||||
|
||||
func NewAddressRetriever(db *postgres.DB, mode types.Mode) (r *addressRetriever) {
|
||||
// NewAddressRetriever returns a new AddressRetriever
|
||||
func NewAddressRetriever(db *postgres.DB, mode types.Mode) AddressRetriever {
|
||||
return &addressRetriever{
|
||||
db: db,
|
||||
mode: mode,
|
||||
}
|
||||
}
|
||||
|
||||
// Method to retrieve list of token-holding/contract-related addresses by iterating over available events
|
||||
// RetrieveTokenHolderAddresses is used 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) {
|
||||
|
||||
@@ -17,12 +17,12 @@
|
||||
package retriever_test
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TestRetriever(t *testing.T) {
|
||||
|
||||
@@ -25,20 +25,22 @@ import (
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
)
|
||||
|
||||
// Event is our custom event type
|
||||
type Event struct {
|
||||
Name string
|
||||
Anonymous bool
|
||||
Fields []Field
|
||||
}
|
||||
|
||||
// Field is our custom event field type which associates a postgres type with the 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
|
||||
// Log is used to hold instance of an event log data
|
||||
type Log struct {
|
||||
Id int64 // VulcanizeIdLog for full sync and header ID for header sync contract watcher
|
||||
ID int64 // VulcanizeIdLog for full sync and header ID for header sync contract watcher
|
||||
Values map[string]string // Map of event input names to their values
|
||||
|
||||
// Used for full sync only
|
||||
@@ -51,7 +53,7 @@ type Log struct {
|
||||
Raw []byte // json.Unmarshalled byte array of geth/core/types.Log{}
|
||||
}
|
||||
|
||||
// Unpack abi.Event into our custom Event struct
|
||||
// NewEvent unpacks 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 {
|
||||
@@ -85,6 +87,7 @@ func NewEvent(e abi.Event) Event {
|
||||
}
|
||||
}
|
||||
|
||||
// Sig returns the hash signature for an event
|
||||
func (e Event) Sig() common.Hash {
|
||||
types := make([]string, len(e.Fields))
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
)
|
||||
|
||||
// Method is our custom method struct
|
||||
type Method struct {
|
||||
Name string
|
||||
Const bool
|
||||
@@ -32,7 +33,7 @@ type Method struct {
|
||||
Return []Field
|
||||
}
|
||||
|
||||
// Struct to hold instance of result from method call with given inputs and block
|
||||
// Result is used to hold instance of result from method call with given inputs and block
|
||||
type Result struct {
|
||||
Method
|
||||
Inputs []interface{} // Will only use addresses
|
||||
@@ -41,7 +42,7 @@ type Result struct {
|
||||
Block int64
|
||||
}
|
||||
|
||||
// Unpack abi.Method into our custom Method struct
|
||||
// NewMethod unpacks 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 {
|
||||
@@ -99,6 +100,7 @@ func NewMethod(m abi.Method) Method {
|
||||
}
|
||||
}
|
||||
|
||||
// Sig returns the hash signature for the method
|
||||
func (m Method) Sig() common.Hash {
|
||||
types := make([]string, len(m.Args))
|
||||
i := 0
|
||||
|
||||
@@ -16,19 +16,21 @@
|
||||
|
||||
package types
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Mode is used to explicitly represent the operating mode of the transformer
|
||||
type Mode int
|
||||
|
||||
// Mode enums
|
||||
const (
|
||||
HeaderSync Mode = iota
|
||||
FullSync
|
||||
)
|
||||
|
||||
// IsValid returns true is the Mode is valid
|
||||
func (mode Mode) IsValid() bool {
|
||||
return mode >= HeaderSync && mode <= FullSync
|
||||
}
|
||||
|
||||
// String returns the string representation of the mode
|
||||
func (mode Mode) String() string {
|
||||
switch mode {
|
||||
case HeaderSync:
|
||||
@@ -39,26 +41,3 @@ func (mode Mode) String() string {
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func (mode Mode) MarshalText() ([]byte, error) {
|
||||
switch mode {
|
||||
case HeaderSync:
|
||||
return []byte("header"), nil
|
||||
case FullSync:
|
||||
return []byte("full"), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("contract watcher: unknown mode %d, want HeaderSync or FullSync", mode)
|
||||
}
|
||||
}
|
||||
|
||||
func (mode *Mode) UnmarshalText(text []byte) error {
|
||||
switch string(text) {
|
||||
case "header":
|
||||
*mode = HeaderSync
|
||||
case "full":
|
||||
*mode = FullSync
|
||||
default:
|
||||
return fmt.Errorf(`contract watcher: unknown mode %q, want "header" or "full"`, text)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user