2019-08-06 21:57:08 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 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/>.
|
|
|
|
|
|
|
|
package logs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/constants"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/fetcher"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transactions"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrNoWatchedAddresses = errors.New("no watched addresses configured in the log extractor")
|
|
|
|
|
2019-08-14 22:29:04 +00:00
|
|
|
const (
|
|
|
|
missingHeadersFound = true
|
|
|
|
noMissingHeadersFound = false
|
|
|
|
)
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
type ILogExtractor interface {
|
2019-08-26 21:43:14 +00:00
|
|
|
AddTransformerConfig(config transformer.EventTransformerConfig) error
|
2019-08-14 22:29:04 +00:00
|
|
|
ExtractLogs(recheckHeaders constants.TransformerExecution) (error, bool)
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LogExtractor struct {
|
|
|
|
Addresses []common.Address
|
|
|
|
CheckedHeadersRepository datastore.CheckedHeadersRepository
|
2019-08-26 21:43:14 +00:00
|
|
|
CheckedLogsRepository datastore.CheckedLogsRepository
|
2019-08-06 21:57:08 +00:00
|
|
|
Fetcher fetcher.ILogFetcher
|
|
|
|
LogRepository datastore.HeaderSyncLogRepository
|
|
|
|
StartingBlock *int64
|
|
|
|
Syncer transactions.ITransactionsSyncer
|
|
|
|
Topics []common.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add additional logs to extract
|
2019-08-26 21:43:14 +00:00
|
|
|
func (extractor *LogExtractor) AddTransformerConfig(config transformer.EventTransformerConfig) error {
|
|
|
|
checkedHeadersErr := extractor.updateCheckedHeaders(config)
|
|
|
|
if checkedHeadersErr != nil {
|
|
|
|
return checkedHeadersErr
|
|
|
|
}
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
if extractor.StartingBlock == nil {
|
|
|
|
extractor.StartingBlock = &config.StartingBlockNumber
|
|
|
|
} else if earlierStartingBlockNumber(config.StartingBlockNumber, *extractor.StartingBlock) {
|
|
|
|
extractor.StartingBlock = &config.StartingBlockNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
addresses := transformer.HexStringsToAddresses(config.ContractAddresses)
|
|
|
|
extractor.Addresses = append(extractor.Addresses, addresses...)
|
|
|
|
extractor.Topics = append(extractor.Topics, common.HexToHash(config.Topic))
|
2019-08-26 21:43:14 +00:00
|
|
|
return nil
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch and persist watched logs
|
2019-08-14 22:29:04 +00:00
|
|
|
func (extractor LogExtractor) ExtractLogs(recheckHeaders constants.TransformerExecution) (error, bool) {
|
2019-08-06 21:57:08 +00:00
|
|
|
if len(extractor.Addresses) < 1 {
|
|
|
|
logrus.Errorf("error extracting logs: %s", ErrNoWatchedAddresses.Error())
|
2019-08-14 22:29:04 +00:00
|
|
|
return ErrNoWatchedAddresses, noMissingHeadersFound
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:30:50 +00:00
|
|
|
uncheckedHeaders, uncheckedHeadersErr := extractor.CheckedHeadersRepository.UncheckedHeaders(*extractor.StartingBlock, -1, getCheckCount(recheckHeaders))
|
|
|
|
if uncheckedHeadersErr != nil {
|
|
|
|
logrus.Errorf("error fetching missing headers: %s", uncheckedHeadersErr)
|
|
|
|
return uncheckedHeadersErr, noMissingHeadersFound
|
2019-08-12 17:26:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:30:50 +00:00
|
|
|
if len(uncheckedHeaders) < 1 {
|
2019-08-14 22:29:04 +00:00
|
|
|
return nil, noMissingHeadersFound
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:30:50 +00:00
|
|
|
for _, header := range uncheckedHeaders {
|
2019-08-06 21:57:08 +00:00
|
|
|
logs, fetchLogsErr := extractor.Fetcher.FetchLogs(extractor.Addresses, extractor.Topics, header)
|
|
|
|
if fetchLogsErr != nil {
|
|
|
|
logError("error fetching logs for header: %s", fetchLogsErr, header)
|
2019-08-14 22:29:04 +00:00
|
|
|
return fetchLogsErr, missingHeadersFound
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(logs) > 0 {
|
|
|
|
transactionsSyncErr := extractor.Syncer.SyncTransactions(header.Id, logs)
|
|
|
|
if transactionsSyncErr != nil {
|
|
|
|
logError("error syncing transactions: %s", transactionsSyncErr, header)
|
2019-08-14 22:29:04 +00:00
|
|
|
return transactionsSyncErr, missingHeadersFound
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createLogsErr := extractor.LogRepository.CreateHeaderSyncLogs(header.Id, logs)
|
|
|
|
if createLogsErr != nil {
|
|
|
|
logError("error persisting logs: %s", createLogsErr, header)
|
2019-08-14 22:29:04 +00:00
|
|
|
return createLogsErr, missingHeadersFound
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
markHeaderCheckedErr := extractor.CheckedHeadersRepository.MarkHeaderChecked(header.Id)
|
|
|
|
if markHeaderCheckedErr != nil {
|
|
|
|
logError("error marking header checked: %s", markHeaderCheckedErr, header)
|
2019-08-14 22:29:04 +00:00
|
|
|
return markHeaderCheckedErr, missingHeadersFound
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-14 22:29:04 +00:00
|
|
|
return nil, missingHeadersFound
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func earlierStartingBlockNumber(transformerBlock, watcherBlock int64) bool {
|
|
|
|
return transformerBlock < watcherBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
func logError(description string, err error, header core.Header) {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"headerId": header.Id,
|
|
|
|
"headerHash": header.Hash,
|
|
|
|
"blockNumber": header.BlockNumber,
|
|
|
|
}).Errorf(description, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCheckCount(recheckHeaders constants.TransformerExecution) int64 {
|
|
|
|
if recheckHeaders == constants.HeaderMissing {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return constants.RecheckHeaderCap
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 21:43:14 +00:00
|
|
|
|
|
|
|
func (extractor *LogExtractor) updateCheckedHeaders(config transformer.EventTransformerConfig) error {
|
|
|
|
hasBeenChecked, hasBeenCheckedErr := extractor.CheckedLogsRepository.HaveLogsBeenChecked(config.ContractAddresses, config.Topic)
|
|
|
|
if hasBeenCheckedErr != nil {
|
|
|
|
return hasBeenCheckedErr
|
|
|
|
}
|
|
|
|
if !hasBeenChecked {
|
2019-08-27 21:30:50 +00:00
|
|
|
uncheckHeadersErr := extractor.CheckedHeadersRepository.MarkHeadersUnchecked(config.StartingBlockNumber)
|
|
|
|
if uncheckHeadersErr != nil {
|
|
|
|
return uncheckHeadersErr
|
2019-08-26 21:43:14 +00:00
|
|
|
}
|
2019-08-27 21:30:50 +00:00
|
|
|
markLogsCheckedErr := extractor.CheckedLogsRepository.MarkLogsChecked(config.ContractAddresses, config.Topic)
|
|
|
|
if markLogsCheckedErr != nil {
|
|
|
|
return markLogsCheckedErr
|
2019-08-26 21:43:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|