2018-02-13 16:31:57 +00:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
2018-11-30 16:28:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-12-10 14:40:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-11-30 16:28:52 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers"
|
2018-08-09 21:55:02 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
2018-02-13 16:31:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Watcher struct {
|
2018-08-09 21:55:02 +00:00
|
|
|
Transformers []shared.Transformer
|
2018-12-10 15:50:13 +00:00
|
|
|
DB *postgres.DB
|
2018-11-30 16:28:52 +00:00
|
|
|
Fetcher shared.LogFetcher
|
|
|
|
Chunker shared.LogChunker
|
|
|
|
Addresses []common.Address
|
|
|
|
Topics []common.Hash
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:50:13 +00:00
|
|
|
func NewWatcher(db *postgres.DB, fetcher shared.LogFetcher) Watcher {
|
2018-11-30 16:28:52 +00:00
|
|
|
transformerConfigs := transformers.TransformerConfigs()
|
|
|
|
var contractAddresses []common.Address
|
|
|
|
var topic0s []common.Hash
|
|
|
|
|
|
|
|
for _, config := range transformerConfigs {
|
|
|
|
for _, address := range config.ContractAddresses {
|
|
|
|
contractAddresses = append(contractAddresses, common.HexToAddress(address))
|
|
|
|
}
|
|
|
|
topic0s = append(topic0s, common.HexToHash(config.Topic))
|
|
|
|
}
|
|
|
|
|
|
|
|
chunker := shared.NewLogChunker(transformerConfigs)
|
|
|
|
|
|
|
|
return Watcher{
|
2018-12-10 15:50:13 +00:00
|
|
|
DB: db,
|
|
|
|
Fetcher: fetcher,
|
|
|
|
Chunker: chunker,
|
|
|
|
Addresses: contractAddresses,
|
|
|
|
Topics: topic0s,
|
2018-11-30 16:28:52 +00:00
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 21:55:02 +00:00
|
|
|
func (watcher *Watcher) AddTransformers(us []shared.TransformerInitializer) {
|
2018-03-16 19:34:18 +00:00
|
|
|
for _, transformerInitializer := range us {
|
2018-12-10 15:50:13 +00:00
|
|
|
transformer := transformerInitializer(watcher.DB)
|
2018-03-16 19:34:18 +00:00
|
|
|
watcher.Transformers = append(watcher.Transformers, transformer)
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (watcher *Watcher) Execute() error {
|
2018-12-10 15:50:13 +00:00
|
|
|
checkedColumnNames, err := shared.GetCheckedColumnNames(watcher.DB)
|
2018-12-04 15:04:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
notCheckedSQL := shared.CreateNotCheckedSQL(checkedColumnNames)
|
|
|
|
|
2018-11-28 16:00:04 +00:00
|
|
|
// TODO Handle start and end numbers in transformers?
|
2018-12-10 15:50:13 +00:00
|
|
|
missingHeaders, err := shared.MissingHeaders(0, -1, watcher.DB, notCheckedSQL)
|
2018-12-10 20:12:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Fetching of missing headers failed in watcher!")
|
|
|
|
return err
|
|
|
|
}
|
2018-11-30 16:28:52 +00:00
|
|
|
|
|
|
|
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-10 20:12:19 +00:00
|
|
|
log.Error("Error while fetching logs in watcher")
|
2018-11-30 16:28:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-11-28 16:00:04 +00:00
|
|
|
|
2018-11-30 16:28:52 +00:00
|
|
|
chunkedLogs := watcher.Chunker.ChunkLogs(logs)
|
2018-11-28 16:00:04 +00:00
|
|
|
|
2018-11-30 16:28:52 +00:00
|
|
|
for _, transformer := range watcher.Transformers {
|
2018-12-04 16:40:39 +00:00
|
|
|
logChunk := chunkedLogs[transformer.Name()]
|
2018-12-04 16:05:34 +00:00
|
|
|
err = transformer.Execute(logChunk, header)
|
2018-12-10 14:40:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("%v transformer failed to execute: %v", transformer.Name(), err)
|
|
|
|
return err
|
|
|
|
}
|
2018-11-30 16:28:52 +00:00
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|