2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package poller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-11-20 16:38:23 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"strconv"
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-12-07 15:38:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2019-03-11 23:18:54 +00:00
|
|
|
"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"
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-11-20 16:38:23 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-11-07 21:50:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Poller interface {
|
2019-03-14 21:49:27 +00:00
|
|
|
PollContract(con contract.Contract, lastBlock int64) error
|
2018-12-07 15:38:46 +00:00
|
|
|
PollContractAt(con contract.Contract, blockNumber int64) error
|
2018-11-20 16:38:23 +00:00
|
|
|
FetchContractData(contractAbi, contractAddress, method string, methodArgs []interface{}, result interface{}, blockNumber int64) error
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type poller struct {
|
2018-11-23 18:12:24 +00:00
|
|
|
repository.MethodRepository
|
2018-11-07 21:50:43 +00:00
|
|
|
bc core.BlockChain
|
2018-11-20 16:38:23 +00:00
|
|
|
contract contract.Contract
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 04:26:07 +00:00
|
|
|
func NewPoller(blockChain core.BlockChain, db *postgres.DB, mode types.Mode) *poller {
|
2018-11-07 21:50:43 +00:00
|
|
|
return &poller{
|
2018-11-24 04:26:07 +00:00
|
|
|
MethodRepository: repository.NewMethodRepository(db, mode),
|
2018-11-23 18:12:24 +00:00
|
|
|
bc: blockChain,
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 21:49:27 +00:00
|
|
|
func (p *poller) PollContract(con contract.Contract, lastBlock int64) error {
|
|
|
|
for i := con.StartingBlock; i <= lastBlock; i++ {
|
2019-03-12 00:02:04 +00:00
|
|
|
if err := p.PollContractAt(con, i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-07 15:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *poller) PollContractAt(con contract.Contract, blockNumber int64) error {
|
2018-11-07 21:50:43 +00:00
|
|
|
p.contract = con
|
|
|
|
for _, m := range con.Methods {
|
2018-11-20 16:38:23 +00:00
|
|
|
switch len(m.Args) {
|
2018-11-07 21:50:43 +00:00
|
|
|
case 0:
|
2018-12-07 15:38:46 +00:00
|
|
|
if err := p.pollNoArgAt(m, blockNumber); err != nil {
|
2018-11-07 21:50:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case 1:
|
2018-12-07 15:38:46 +00:00
|
|
|
if err := p.pollSingleArgAt(m, blockNumber); err != nil {
|
2018-11-07 21:50:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case 2:
|
2018-12-07 15:38:46 +00:00
|
|
|
if err := p.pollDoubleArgAt(m, blockNumber); err != nil {
|
2018-11-07 21:50:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.New("poller error: too many arguments to handle")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
func (p *poller) pollNoArgAt(m types.Method, bn int64) error {
|
2018-11-20 16:38:23 +00:00
|
|
|
result := types.Result{
|
2018-12-07 15:38:46 +00:00
|
|
|
Block: bn,
|
2018-11-20 16:38:23 +00:00
|
|
|
Method: m,
|
|
|
|
Inputs: nil,
|
|
|
|
PgType: m.Return[0].PgType,
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
// Cache returned value if piping is turned on
|
|
|
|
p.cache(out)
|
2018-12-07 15:38:46 +00:00
|
|
|
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 {
|
2018-12-19 18:42:59 +00:00
|
|
|
case abi.HashTy, abi.FixedBytesTy:
|
2018-12-07 15:38:46 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-12 00:02:04 +00:00
|
|
|
results := make([]types.Result, 0, len(args))
|
2018-12-07 15:38:46 +00:00
|
|
|
for arg := range args {
|
|
|
|
in := []interface{}{arg}
|
|
|
|
strIn := []interface{}{contract.StringifyArg(arg)}
|
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
var out interface{}
|
2018-12-07 15:38:46 +00:00
|
|
|
err := p.bc.FetchContractData(p.contract.Abi, p.contract.Address, m.Name, in, &out, bn)
|
2018-11-20 16:38:23 +00:00
|
|
|
if err != nil {
|
2018-12-07 15:38:46 +00:00
|
|
|
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))
|
2018-11-20 16:38:23 +00:00
|
|
|
}
|
|
|
|
strOut, err := stringify(out)
|
2018-11-07 21:50:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-19 18:42:59 +00:00
|
|
|
p.cache(out)
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
// Write inputs and outputs to result and append result to growing set
|
2018-12-07 15:38:46 +00:00
|
|
|
result.Inputs = strIn
|
2018-11-20 16:38:23 +00:00
|
|
|
result.Output = strOut
|
2018-12-07 15:38:46 +00:00
|
|
|
results = append(results, result)
|
|
|
|
}
|
2018-12-19 18:42:59 +00:00
|
|
|
// Persist result set as batch
|
2018-12-07 15:38:46 +00:00
|
|
|
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))
|
2018-11-20 16:38:23 +00:00
|
|
|
}
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
// 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 {
|
2018-11-20 16:38:23 +00:00
|
|
|
result := types.Result{
|
2018-12-07 15:38:46 +00:00
|
|
|
Block: bn,
|
2018-11-20 16:38:23 +00:00
|
|
|
Method: m,
|
2018-12-07 15:38:46 +00:00
|
|
|
Inputs: make([]interface{}, 2),
|
2018-11-20 16:38:23 +00:00
|
|
|
PgType: m.Return[0].PgType,
|
|
|
|
}
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
// 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 {
|
2018-12-19 18:42:59 +00:00
|
|
|
case abi.HashTy, abi.FixedBytesTy:
|
2018-12-07 15:38:46 +00:00
|
|
|
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 {
|
2018-12-19 18:42:59 +00:00
|
|
|
case abi.HashTy, abi.FixedBytesTy:
|
2018-12-07 15:38:46 +00:00
|
|
|
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)}
|
2018-11-20 16:38:23 +00:00
|
|
|
|
|
|
|
var out interface{}
|
2018-12-07 15:38:46 +00:00
|
|
|
err := p.bc.FetchContractData(p.contract.Abi, p.contract.Address, m.Name, in, &out, bn)
|
2018-11-20 16:38:23 +00:00
|
|
|
if err != nil {
|
2018-12-07 15:38:46 +00:00
|
|
|
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))
|
2018-11-20 16:38:23 +00:00
|
|
|
}
|
|
|
|
strOut, err := stringify(out)
|
2018-11-07 21:50:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-19 18:42:59 +00:00
|
|
|
p.cache(out)
|
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
result.Output = strOut
|
|
|
|
result.Inputs = strIn
|
2018-12-07 15:38:46 +00:00
|
|
|
results = append(results, result)
|
2018-11-20 16:38:23 +00:00
|
|
|
}
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
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))
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is just a wrapper around the poller blockchain's FetchContractData method
|
2018-11-20 16:38:23 +00:00
|
|
|
func (p *poller) FetchContractData(contractAbi, contractAddress, method string, methodArgs []interface{}, result interface{}, blockNumber int64) error {
|
2018-11-07 21:50:43 +00:00
|
|
|
return p.bc.FetchContractData(contractAbi, contractAddress, method, methodArgs, result, blockNumber)
|
|
|
|
}
|
2018-11-20 16:38:23 +00:00
|
|
|
|
2019-03-12 00:02:04 +00:00
|
|
|
// This is used to cache a method return value if method piping is turned on
|
2018-12-19 18:42:59 +00:00
|
|
|
func (p *poller) cache(out interface{}) {
|
|
|
|
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:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
func stringify(input interface{}) (string, error) {
|
|
|
|
switch input.(type) {
|
|
|
|
case *big.Int:
|
2018-12-07 15:38:46 +00:00
|
|
|
b := input.(*big.Int)
|
2018-11-20 16:38:23 +00:00
|
|
|
return b.String(), nil
|
|
|
|
case common.Address:
|
2018-12-07 15:38:46 +00:00
|
|
|
a := input.(common.Address)
|
2018-11-20 16:38:23 +00:00
|
|
|
return a.String(), nil
|
|
|
|
case common.Hash:
|
2018-12-07 15:38:46 +00:00
|
|
|
h := input.(common.Hash)
|
2018-11-20 16:38:23 +00:00
|
|
|
return h.String(), nil
|
|
|
|
case string:
|
|
|
|
return input.(string), nil
|
2018-12-07 15:38:46 +00:00
|
|
|
case []byte:
|
|
|
|
b := hexutil.Encode(input.([]byte))
|
|
|
|
return b, nil
|
|
|
|
case byte:
|
|
|
|
b := input.(byte)
|
|
|
|
return string(b), nil
|
2018-11-20 16:38:23 +00:00
|
|
|
case bool:
|
|
|
|
return strconv.FormatBool(input.(bool)), nil
|
|
|
|
default:
|
|
|
|
return "", errors.New("error: unhandled return type")
|
|
|
|
}
|
|
|
|
}
|