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

82 lines
2.3 KiB
Go
Raw Normal View History

package shared
import (
"github.com/ethereum/go-ethereum/common"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers"
2018-08-09 21:55:02 +00:00
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
)
type Watcher struct {
2018-08-09 21:55:02 +00:00
Transformers []shared.Transformer
2018-03-21 14:53:20 +00:00
DB postgres.DB
Blockchain core.BlockChain
Fetcher shared.LogFetcher
Chunker shared.LogChunker
Addresses []common.Address
Topics []common.Hash
}
func NewWatcher(db postgres.DB, bc core.BlockChain) Watcher {
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)
fetcher := shared.NewFetcher(bc)
return Watcher{
DB: db,
Blockchain: bc,
Fetcher: fetcher,
Chunker: chunker,
Addresses: contractAddresses,
Topics: topic0s,
}
}
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 {
transformer := transformerInitializer(&watcher.DB)
2018-03-16 19:34:18 +00:00
watcher.Transformers = append(watcher.Transformers, transformer)
}
}
func (watcher *Watcher) Execute() error {
checkedColumnNames, err := shared.GetCheckedColumnNames(&watcher.DB)
if err != nil {
return err
}
notCheckedSQL := shared.CreateNotCheckedSQL(checkedColumnNames)
// TODO Handle start and end numbers in transformers?
missingHeaders, err := shared.MissingHeaders(0, -1, &watcher.DB, notCheckedSQL)
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
return err
}
chunkedLogs := watcher.Chunker.ChunkLogs(logs)
for _, transformer := range watcher.Transformers {
// TODO delegate log chunks to respective transformers
// Need to get the transformer name... :/
logChunk := chunkedLogs["transformerName"]
err = transformer.Execute(logChunk, missingHeaders)
}
}
return err
}