ipld-eth-server/libraries/shared/watcher.go

99 lines
3.2 KiB
Go
Raw Normal View History

package shared
import (
"github.com/ethereum/go-ethereum/common"
2018-12-10 14:40:45 +00:00
"github.com/ethereum/go-ethereum/log"
2018-12-11 10:35:13 +00:00
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
2018-08-09 21:55:02 +00:00
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
)
2018-12-11 10:35:13 +00:00
type WatcherRepository interface {
GetCheckedColumnNames(db *postgres.DB) ([]string, error)
CreateNotCheckedSQL(boolColumns []string) string
MissingHeaders(startingBlockNumber int64, endingBlockNumber int64, db *postgres.DB, notCheckedSQL string) ([]core.Header, error)
}
type Watcher struct {
2018-08-09 21:55:02 +00:00
Transformers []shared.Transformer
DB *postgres.DB
Fetcher shared.LogFetcher
2018-12-12 14:41:29 +00:00
Chunker shared.Chunker
Addresses []common.Address
Topics []common.Hash
2018-12-11 10:35:13 +00:00
Repository WatcherRepository
}
func NewWatcher(db *postgres.DB, fetcher shared.LogFetcher, repository WatcherRepository) Watcher {
chunker := shared.NewLogChunker()
return Watcher{
2018-12-11 10:35:13 +00:00
DB: db,
Fetcher: fetcher,
Chunker: chunker,
Repository: repository,
}
}
// Adds transformers to the watcher and updates the chunker, so that it will consider the new transformers.
func (watcher *Watcher) AddTransformers(initializers []shared.TransformerInitializer) {
var contractAddresses []common.Address
var topic0s []common.Hash
var configs []shared.TransformerConfig
2018-12-12 14:41:29 +00:00
for _, initializer := range initializers {
transformer := initializer(watcher.DB)
2018-03-16 19:34:18 +00:00
watcher.Transformers = append(watcher.Transformers, transformer)
2018-12-12 14:41:29 +00:00
config := transformer.GetConfig()
configs = append(configs, config)
2018-12-12 14:41:29 +00:00
addresses := shared.HexStringsToAddresses(config.ContractAddresses)
contractAddresses = append(contractAddresses, addresses...)
2018-12-12 14:41:29 +00:00
topic0s = append(topic0s, common.HexToHash(config.Topic))
}
watcher.Addresses = append(watcher.Addresses, contractAddresses...)
watcher.Topics = append(watcher.Topics, topic0s...)
watcher.Chunker.AddConfigs(configs)
}
func (watcher *Watcher) Execute() error {
2018-12-11 10:35:13 +00:00
checkedColumnNames, err := watcher.Repository.GetCheckedColumnNames(watcher.DB)
if err != nil {
return err
}
2018-12-11 10:35:13 +00:00
notCheckedSQL := watcher.Repository.CreateNotCheckedSQL(checkedColumnNames)
2018-12-11 10:35:13 +00:00
// TODO Handle start and end numbers in transformers
missingHeaders, err := watcher.Repository.MissingHeaders(0, -1, watcher.DB, notCheckedSQL)
if err != nil {
log.Error("Fetching of missing headers failed in watcher!")
return err
}
for _, header := range missingHeaders {
// TODO Extend FetchLogs for doing several blocks at a time
logs, err := watcher.Fetcher.FetchLogs(watcher.Addresses, watcher.Topics, header)
if err != nil {
// TODO Handle fetch error in watcher
2018-12-11 10:35:13 +00:00
log.Error("Error while fetching logs for header %v in watcher", header.Id)
return err
}
chunkedLogs := watcher.Chunker.ChunkLogs(logs)
// Can't quit early and mark as checked if there are no logs. If we are running continuousLogSync,
// not all logs we're interested in might have been fetched.
for _, transformer := range watcher.Transformers {
transformerName := transformer.GetConfig().TransformerName
logChunk := chunkedLogs[transformerName]
err = transformer.Execute(logChunk, header)
2018-12-10 14:40:45 +00:00
if err != nil {
log.Error("%v transformer failed to execute in watcher: %v", transformerName, err)
2018-12-10 14:40:45 +00:00
return err
}
}
}
return err
}