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"
|
2019-09-30 17:37:16 +00:00
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
var (
|
|
|
|
ErrNoUncheckedHeaders = errors.New("no unchecked headers available for log fetching")
|
|
|
|
ErrNoWatchedAddresses = errors.New("no watched addresses configured in the log extractor")
|
2019-08-14 22:29:04 +00:00
|
|
|
)
|
|
|
|
|
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-09-19 01:55:15 +00:00
|
|
|
ExtractLogs(recheckHeaders constants.TransformerExecution) error
|
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-09-19 01:55:15 +00:00
|
|
|
func (extractor LogExtractor) ExtractLogs(recheckHeaders constants.TransformerExecution) error {
|
2019-08-06 21:57:08 +00:00
|
|
|
if len(extractor.Addresses) < 1 {
|
|
|
|
logrus.Errorf("error extracting logs: %s", ErrNoWatchedAddresses.Error())
|
2019-09-19 01:55:15 +00:00
|
|
|
return ErrNoWatchedAddresses
|
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)
|
2019-09-19 01:55:15 +00:00
|
|
|
return uncheckedHeadersErr
|
2019-08-12 17:26:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:30:50 +00:00
|
|
|
if len(uncheckedHeaders) < 1 {
|
2019-09-19 01:55:15 +00:00
|
|
|
return ErrNoUncheckedHeaders
|
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-09-19 01:55:15 +00:00
|
|
|
return fetchLogsErr
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(logs) > 0 {
|
2019-10-18 16:16:19 +00:00
|
|
|
transactionsSyncErr := extractor.Syncer.SyncTransactions(header.ID, logs)
|
2019-08-06 21:57:08 +00:00
|
|
|
if transactionsSyncErr != nil {
|
|
|
|
logError("error syncing transactions: %s", transactionsSyncErr, header)
|
2019-09-19 01:55:15 +00:00
|
|
|
return transactionsSyncErr
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
createLogsErr := extractor.LogRepository.CreateHeaderSyncLogs(header.ID, logs)
|
2019-08-06 21:57:08 +00:00
|
|
|
if createLogsErr != nil {
|
|
|
|
logError("error persisting logs: %s", createLogsErr, header)
|
2019-09-19 01:55:15 +00:00
|
|
|
return createLogsErr
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
markHeaderCheckedErr := extractor.CheckedHeadersRepository.MarkHeaderChecked(header.ID)
|
2019-08-06 21:57:08 +00:00
|
|
|
if markHeaderCheckedErr != nil {
|
|
|
|
logError("error marking header checked: %s", markHeaderCheckedErr, header)
|
2019-09-19 01:55:15 +00:00
|
|
|
return markHeaderCheckedErr
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 01:55:15 +00:00
|
|
|
return nil
|
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{
|
2019-10-18 16:16:19 +00:00
|
|
|
"headerId": header.ID,
|
2019-08-06 21:57:08 +00:00
|
|
|
"headerHash": header.Hash,
|
|
|
|
"blockNumber": header.BlockNumber,
|
|
|
|
}).Errorf(description, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCheckCount(recheckHeaders constants.TransformerExecution) int64 {
|
2019-09-10 19:39:45 +00:00
|
|
|
if recheckHeaders == constants.HeaderUnchecked {
|
2019-08-06 21:57:08 +00:00
|
|
|
return 1
|
|
|
|
}
|
2019-10-18 15:24:33 +00:00
|
|
|
return constants.RecheckHeaderCap
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
2019-08-26 21:43:14 +00:00
|
|
|
|
|
|
|
func (extractor *LogExtractor) updateCheckedHeaders(config transformer.EventTransformerConfig) error {
|
2019-09-11 02:22:14 +00:00
|
|
|
alreadyWatchingLog, watchingLogErr := extractor.CheckedLogsRepository.AlreadyWatchingLog(config.ContractAddresses, config.Topic)
|
|
|
|
if watchingLogErr != nil {
|
|
|
|
return watchingLogErr
|
2019-08-26 21:43:14 +00:00
|
|
|
}
|
2019-09-11 02:22:14 +00:00
|
|
|
if !alreadyWatchingLog {
|
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-09-11 02:22:14 +00:00
|
|
|
markLogWatchedErr := extractor.CheckedLogsRepository.MarkLogWatched(config.ContractAddresses, config.Topic)
|
|
|
|
if markLogWatchedErr != nil {
|
|
|
|
return markLogWatchedErr
|
2019-08-26 21:43:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|