2018-11-07 21:50:43 +00:00
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
2018-11-04 21:26:39 +00:00
|
|
|
|
|
|
|
package transformer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-11-04 21:26:39 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-11-04 21:26:39 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/contract"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/converter"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/parser"
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/poller"
|
2018-11-04 21:26:39 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/repository"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/retriever"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Omni transformer
|
|
|
|
// Used to extract all or a subset of event and method data
|
|
|
|
// for any contract and persist it to postgres in a manner
|
|
|
|
// that requires no prior knowledge of the contract
|
|
|
|
// other than its address and which network it is on
|
|
|
|
type Transformer interface {
|
2018-11-07 21:50:43 +00:00
|
|
|
SetEvents(contractAddr string, filterSet []string)
|
|
|
|
SetEventAddrs(contractAddr string, filterSet []string)
|
|
|
|
SetMethods(contractAddr string, filterSet []string)
|
|
|
|
SetMethodAddrs(contractAddr string, filterSet []string)
|
|
|
|
SetRange(contractAddr string, rng [2]int64)
|
|
|
|
Init() error
|
|
|
|
Execute() error
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type transformer struct {
|
2018-11-07 21:50:43 +00:00
|
|
|
Network string // Ethereum network name; default/"" is mainnet
|
2018-11-04 21:26:39 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// Database interfaces
|
|
|
|
datastore.FilterRepository // Holds watched event log filters
|
|
|
|
datastore.WatchedEventRepository // Holds watched event log views, created with the filters
|
|
|
|
repository.EventDatastore // Holds transformed watched event log data
|
2018-11-04 21:26:39 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// Processing interfaces
|
2018-11-04 21:26:39 +00:00
|
|
|
parser.Parser // Parses events out of contract abi fetched with addr
|
|
|
|
retriever.BlockRetriever // Retrieves first block with contract addr referenced
|
|
|
|
retriever.AddressRetriever // Retrieves token holder addresses
|
|
|
|
converter.Converter // Converts watched event logs into custom log
|
2018-11-07 21:50:43 +00:00
|
|
|
poller.Poller // Polls methods using contract's token holder addresses
|
2018-11-04 21:26:39 +00:00
|
|
|
|
|
|
|
// Store contract info as mapping to contract address
|
|
|
|
Contracts map[string]*contract.Contract
|
|
|
|
|
|
|
|
// Targeted subset of events/methods
|
2018-11-07 21:50:43 +00:00
|
|
|
// Stored as map sof contract address to events/method names of interest
|
|
|
|
WatchedEvents map[string][]string // Default/empty event list means all are watched
|
|
|
|
WantedMethods map[string][]string // Default/empty method list means none are polled
|
|
|
|
|
|
|
|
// Block ranges to watch contracts
|
|
|
|
ContractRanges map[string][2]int64
|
|
|
|
|
|
|
|
// Lists of addresses to filter event or method data
|
|
|
|
// before persisting; if empty no filter is applied
|
|
|
|
EventAddrs map[string][]string
|
|
|
|
MethodAddrs map[string][]string
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transformer takes in config for blockchain, database, and network id
|
2018-11-07 21:50:43 +00:00
|
|
|
func NewTransformer(network string, BC core.BlockChain, DB *postgres.DB) *transformer {
|
2018-11-04 21:26:39 +00:00
|
|
|
|
|
|
|
return &transformer{
|
2018-11-07 21:50:43 +00:00
|
|
|
Poller: poller.NewPoller(BC),
|
|
|
|
Parser: parser.NewParser(network),
|
|
|
|
BlockRetriever: retriever.NewBlockRetriever(DB),
|
|
|
|
Converter: converter.NewConverter(&contract.Contract{}),
|
2018-11-04 21:26:39 +00:00
|
|
|
Contracts: map[string]*contract.Contract{},
|
2018-11-07 21:50:43 +00:00
|
|
|
WatchedEventRepository: repositories.WatchedEventRepository{DB: DB},
|
|
|
|
FilterRepository: repositories.FilterRepository{DB: DB},
|
|
|
|
EventDatastore: repository.NewEventDataStore(DB),
|
|
|
|
WatchedEvents: map[string][]string{},
|
|
|
|
WantedMethods: map[string][]string{},
|
|
|
|
ContractRanges: map[string][2]int64{},
|
|
|
|
EventAddrs: map[string][]string{},
|
|
|
|
MethodAddrs: map[string][]string{},
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use after creating and setting transformer
|
|
|
|
// Loops over all of the addr => filter sets
|
|
|
|
// Uses parser to pull event info from abi
|
|
|
|
// Use this info to generate event filters
|
|
|
|
func (t *transformer) Init() error {
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
for contractAddr, subset := range t.WatchedEvents {
|
2018-11-04 21:26:39 +00:00
|
|
|
// Get Abi
|
|
|
|
err := t.Parser.Parse(contractAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// Get first block for contract and most recent block for the chain
|
2018-11-04 21:26:39 +00:00
|
|
|
firstBlock, err := t.BlockRetriever.RetrieveFirstBlock(contractAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lastBlock, err := t.BlockRetriever.RetrieveMostRecentBlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// Set to specified range if it falls within the contract's bounds
|
|
|
|
if firstBlock < t.ContractRanges[contractAddr][0] {
|
|
|
|
firstBlock = t.ContractRanges[contractAddr][0]
|
|
|
|
}
|
|
|
|
if lastBlock > t.ContractRanges[contractAddr][1] && t.ContractRanges[contractAddr][1] > firstBlock {
|
|
|
|
lastBlock = t.ContractRanges[contractAddr][1]
|
|
|
|
}
|
|
|
|
|
2018-11-04 21:26:39 +00:00
|
|
|
// Get contract name
|
2018-11-07 21:50:43 +00:00
|
|
|
var name = new(string)
|
|
|
|
err = t.PollMethod(t.Abi(), contractAddr, "name", nil, &name, lastBlock)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("unable to fetch contract name: %v\r\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any accidental duplicate inputs in filter addresses
|
|
|
|
EventAddrs := map[string]bool{}
|
|
|
|
for _, addr := range t.EventAddrs[contractAddr] {
|
|
|
|
EventAddrs[addr] = true
|
|
|
|
}
|
|
|
|
MethodAddrs := map[string]bool{}
|
|
|
|
for _, addr := range t.MethodAddrs[contractAddr] {
|
|
|
|
MethodAddrs[addr] = true
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Aggregate info into contract object
|
|
|
|
info := &contract.Contract{
|
2018-11-07 21:50:43 +00:00
|
|
|
Name: *name,
|
|
|
|
Address: contractAddr,
|
|
|
|
Abi: t.Abi(),
|
|
|
|
ParsedAbi: t.ParsedAbi(),
|
|
|
|
StartingBlock: firstBlock,
|
|
|
|
LastBlock: lastBlock,
|
|
|
|
Events: t.GetEvents(subset),
|
|
|
|
Methods: t.GetAddrMethods(t.WantedMethods[contractAddr]),
|
|
|
|
EventAddrs: EventAddrs,
|
|
|
|
MethodAddrs: MethodAddrs,
|
|
|
|
TknHolderAddrs: map[string]bool{},
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use info to create filters
|
2018-11-07 21:50:43 +00:00
|
|
|
err = info.GenerateFilters()
|
2018-11-04 21:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over filters and push them to the repo
|
|
|
|
for _, filter := range info.Filters {
|
|
|
|
t.CreateFilter(filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Contracts[contractAddr] = info
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through contracts, updating the
|
|
|
|
// converter with each one and using it to
|
|
|
|
// convert watched event logs.
|
|
|
|
// Then persist them into the postgres db
|
|
|
|
func (tr transformer) Execute() error {
|
|
|
|
// Iterate through all internal contracts
|
|
|
|
for _, con := range tr.Contracts {
|
|
|
|
|
|
|
|
// Update converter with current contract
|
2018-11-07 21:50:43 +00:00
|
|
|
tr.Converter.Update(con)
|
2018-11-04 21:26:39 +00:00
|
|
|
|
|
|
|
// Iterate through contract filters and get watched event logs
|
|
|
|
for eventName, filter := range con.Filters {
|
|
|
|
watchedEvents, err := tr.GetWatchedEvents(eventName)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(fmt.Sprintf("Error fetching events for %s:", filter.Name), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over watched event logs and convert them
|
|
|
|
for _, we := range watchedEvents {
|
|
|
|
err = tr.Converter.Convert(*we, con.Events[eventName])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// After converting all logs for events of interest, persist all of the data
|
2018-11-07 21:50:43 +00:00
|
|
|
err := tr.PersistContractEvents(con)
|
2018-11-04 21:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// Used to set which contract addresses and which of their events to watch
|
|
|
|
func (t *transformer) SetEvents(contractAddr string, filterSet []string) {
|
|
|
|
t.WatchedEvents[contractAddr] = filterSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to set subset of account addresses to watch events for
|
|
|
|
func (t *transformer) SetEventAddrs(contractAddr string, filterSet []string) {
|
|
|
|
t.EventAddrs[contractAddr] = filterSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to set which contract addresses and which of their methods to call
|
|
|
|
func (t *transformer) SetMethods(contractAddr string, filterSet []string) {
|
|
|
|
t.WantedMethods[contractAddr] = filterSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to set subset of account addresses to poll methods on
|
|
|
|
func (t *transformer) SetMethodAddrs(contractAddr string, filterSet []string) {
|
|
|
|
t.MethodAddrs[contractAddr] = filterSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to set the block range to watch for a given address
|
|
|
|
func (t *transformer) SetRange(contractAddr string, rng [2]int64) {
|
|
|
|
t.ContractRanges[contractAddr] = rng
|
|
|
|
}
|