2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// 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-03-27 04:05:30 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/chunker"
|
2019-01-31 23:14:42 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/constants"
|
2019-03-27 04:05:30 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/fetcher"
|
2019-08-06 21:57:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/logs"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transactions"
|
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-08-06 21:57:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
2019-08-12 17:26:49 +00:00
|
|
|
"time"
|
2018-02-13 16:31:57 +00:00
|
|
|
)
|
|
|
|
|
2019-08-12 17:26:49 +00:00
|
|
|
const NoNewDataPause = time.Second * 7
|
|
|
|
|
2019-01-28 22:52:47 +00:00
|
|
|
type EventWatcher struct {
|
2019-08-06 21:57:08 +00:00
|
|
|
blockChain core.BlockChain
|
|
|
|
db *postgres.DB
|
|
|
|
LogDelegator logs.ILogDelegator
|
|
|
|
LogExtractor logs.ILogExtractor
|
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-08-06 21:57:08 +00:00
|
|
|
extractor := &logs.LogExtractor{
|
|
|
|
Fetcher: fetcher.NewLogFetcher(bc),
|
|
|
|
CheckedHeadersRepository: repositories.NewCheckedHeadersRepository(db),
|
|
|
|
LogRepository: repositories.NewHeaderSyncLogRepository(db),
|
|
|
|
Syncer: transactions.NewTransactionsSyncer(db, bc),
|
|
|
|
}
|
|
|
|
logTransformer := &logs.LogDelegator{
|
|
|
|
Chunker: chunker.NewLogChunker(),
|
|
|
|
LogRepository: repositories.NewHeaderSyncLogRepository(db),
|
|
|
|
}
|
2019-01-28 22:52:47 +00:00
|
|
|
return EventWatcher{
|
2019-08-06 21:57:08 +00:00
|
|
|
blockChain: bc,
|
|
|
|
db: db,
|
|
|
|
LogExtractor: extractor,
|
|
|
|
LogDelegator: logTransformer,
|
2018-11-30 16:28:52 +00:00
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
// Adds transformers to the watcher so that their logs will be extracted and delegated.
|
2019-03-14 16:59:39 +00:00
|
|
|
func (watcher *EventWatcher) AddTransformers(initializers []transformer.EventTransformerInitializer) {
|
2018-12-12 14:41:29 +00:00
|
|
|
for _, initializer := range initializers {
|
2019-08-06 21:57:08 +00:00
|
|
|
t := initializer(watcher.db)
|
2019-01-15 10:09:13 +00:00
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
watcher.LogDelegator.AddTransformer(t)
|
|
|
|
watcher.LogExtractor.AddTransformerConfig(t.GetConfig())
|
2018-12-12 14:41:29 +00:00
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
// Extracts and delegates watched log events.
|
2019-08-12 17:26:49 +00:00
|
|
|
func (watcher *EventWatcher) Execute(recheckHeaders constants.TransformerExecution, errsChan chan error) {
|
|
|
|
extractErrsChan := make(chan error)
|
|
|
|
delegateErrsChan := make(chan error)
|
|
|
|
|
|
|
|
go watcher.extractLogs(recheckHeaders, extractErrsChan)
|
|
|
|
go watcher.delegateLogs(delegateErrsChan)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case extractErr := <-extractErrsChan:
|
|
|
|
logrus.Errorf("error extracting logs in event watcher: %s", extractErr.Error())
|
|
|
|
errsChan <- extractErr
|
|
|
|
case delegateErr := <-delegateErrsChan:
|
|
|
|
logrus.Errorf("error delegating logs in event watcher: %s", delegateErr.Error())
|
|
|
|
errsChan <- delegateErr
|
|
|
|
}
|
2018-12-04 15:04:13 +00:00
|
|
|
}
|
2019-08-12 17:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (watcher *EventWatcher) extractLogs(recheckHeaders constants.TransformerExecution, errs chan error) {
|
|
|
|
extractLogsErr := make(chan error)
|
|
|
|
missingHeadersFound := make(chan bool)
|
|
|
|
go watcher.LogExtractor.ExtractLogs(recheckHeaders, extractLogsErr, missingHeadersFound)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-extractLogsErr:
|
|
|
|
errs <- err
|
|
|
|
case missingHeaders := <-missingHeadersFound:
|
|
|
|
if missingHeaders {
|
|
|
|
go watcher.extractLogs(recheckHeaders, errs)
|
|
|
|
} else {
|
|
|
|
time.Sleep(NoNewDataPause)
|
|
|
|
go watcher.extractLogs(recheckHeaders, errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (watcher *EventWatcher) delegateLogs(errs chan error) {
|
|
|
|
delegateLogsErr := make(chan error)
|
|
|
|
logsFound := make(chan bool)
|
|
|
|
go watcher.LogDelegator.DelegateLogs(delegateLogsErr, logsFound)
|
2018-12-04 15:04:13 +00:00
|
|
|
|
2019-08-12 17:26:49 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-delegateLogsErr:
|
|
|
|
errs <- err
|
|
|
|
case logs := <-logsFound:
|
|
|
|
if logs {
|
|
|
|
go watcher.delegateLogs(errs)
|
|
|
|
} else {
|
|
|
|
time.Sleep(NoNewDataPause)
|
|
|
|
go watcher.delegateLogs(errs)
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 20:12:19 +00:00
|
|
|
}
|
2018-11-30 16:28:52 +00:00
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
}
|