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/>.
|
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
package watcher
|
2018-02-13 16:31:57 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-15 10:09:13 +00:00
|
|
|
"fmt"
|
2019-01-25 08:59:23 +00:00
|
|
|
|
2018-11-30 16:28:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-01-15 10:09:13 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-01-25 08:59:23 +00:00
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
chunk "github.com/vulcanize/vulcanizedb/libraries/shared/chunker"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/constants"
|
|
|
|
fetch "github.com/vulcanize/vulcanizedb/libraries/shared/fetcher"
|
|
|
|
repo "github.com/vulcanize/vulcanizedb/libraries/shared/repository"
|
2019-01-25 08:59:23 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
2018-12-11 10:35:13 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
|
|
)
|
|
|
|
|
2019-01-28 22:52:47 +00:00
|
|
|
type EventWatcher struct {
|
2019-01-31 23:14:42 +00:00
|
|
|
Transformers []transformer.EventTransformer
|
2019-01-15 10:09:13 +00:00
|
|
|
DB *postgres.DB
|
2019-01-31 23:14:42 +00:00
|
|
|
Fetcher fetch.LogFetcher
|
|
|
|
Chunker chunk.Chunker
|
2019-01-15 10:09:13 +00:00
|
|
|
Addresses []common.Address
|
|
|
|
Topics []common.Hash
|
|
|
|
StartingBlock *int64
|
2018-11-30 16:28:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 22:52:47 +00:00
|
|
|
func NewEventWatcher(db *postgres.DB, bc core.BlockChain) EventWatcher {
|
2019-01-31 23:14:42 +00:00
|
|
|
chunker := chunk.NewLogChunker()
|
|
|
|
fetcher := fetch.NewFetcher(bc)
|
2019-01-28 22:52:47 +00:00
|
|
|
return EventWatcher{
|
2019-01-07 14:19:31 +00:00
|
|
|
DB: db,
|
|
|
|
Fetcher: fetcher,
|
|
|
|
Chunker: chunker,
|
2018-11-30 16:28:52 +00:00
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 11:39:57 +00:00
|
|
|
// Adds transformers to the watcher and updates the chunker, so that it will consider the new transformers.
|
2019-01-25 08:59:23 +00:00
|
|
|
func (watcher *EventWatcher) AddTransformers(initializers []transformer.TransformerInitializer) {
|
2018-12-13 11:39:57 +00:00
|
|
|
var contractAddresses []common.Address
|
|
|
|
var topic0s []common.Hash
|
2019-01-25 08:59:23 +00:00
|
|
|
var configs []transformer.TransformerConfig
|
2018-12-12 14:41:29 +00:00
|
|
|
|
|
|
|
for _, initializer := range initializers {
|
2019-01-25 08:59:23 +00:00
|
|
|
t := initializer(watcher.DB)
|
|
|
|
watcher.Transformers = append(watcher.Transformers, t)
|
2018-12-12 14:41:29 +00:00
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
config := t.GetConfig()
|
2018-12-13 11:39:57 +00:00
|
|
|
configs = append(configs, config)
|
2018-12-12 14:41:29 +00:00
|
|
|
|
2019-01-15 10:09:13 +00:00
|
|
|
if watcher.StartingBlock == nil {
|
|
|
|
watcher.StartingBlock = &config.StartingBlockNumber
|
|
|
|
} else if earlierStartingBlockNumber(config.StartingBlockNumber, *watcher.StartingBlock) {
|
|
|
|
watcher.StartingBlock = &config.StartingBlockNumber
|
|
|
|
}
|
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
addresses := transformer.HexStringsToAddresses(config.ContractAddresses)
|
2018-12-13 11:39:57 +00:00
|
|
|
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)
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 16:35:46 +00:00
|
|
|
func (watcher *EventWatcher) Execute(recheckHeaders constants.TransformerExecution) error {
|
2019-01-15 10:09:13 +00:00
|
|
|
if watcher.Transformers == nil {
|
|
|
|
return fmt.Errorf("No transformers added to watcher")
|
|
|
|
}
|
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
checkedColumnNames, err := repo.GetCheckedColumnNames(watcher.DB)
|
2018-12-04 15:04:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-31 23:14:42 +00:00
|
|
|
notCheckedSQL := repo.CreateNotCheckedSQL(checkedColumnNames, recheckHeaders)
|
2018-12-04 15:04:13 +00:00
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
missingHeaders, err := repo.MissingHeaders(*watcher.StartingBlock, -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
|
2019-01-15 10:09:13 +00:00
|
|
|
log.Errorf("Error while fetching logs for header %v in watcher", header.Id)
|
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-12-13 11:39:57 +00:00
|
|
|
// 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.
|
2019-01-25 08:59:23 +00:00
|
|
|
for _, t := range watcher.Transformers {
|
|
|
|
transformerName := t.GetConfig().TransformerName
|
2018-12-13 11:39:57 +00:00
|
|
|
logChunk := chunkedLogs[transformerName]
|
2019-01-25 08:59:23 +00:00
|
|
|
err = t.Execute(logChunk, header, constants.HeaderMissing)
|
2018-12-10 14:40:45 +00:00
|
|
|
if err != nil {
|
2019-01-15 10:09:13 +00:00
|
|
|
log.Errorf("%v transformer failed to execute in watcher: %v", transformerName, err)
|
2018-12-10 14:40:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-11-30 16:28:52 +00:00
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-01-15 10:09:13 +00:00
|
|
|
|
|
|
|
func earlierStartingBlockNumber(transformerBlock, watcherBlock int64) bool {
|
|
|
|
return transformerBlock < watcherBlock
|
|
|
|
}
|