2019-01-28 22:52:47 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2019-01-28 22:52:47 +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-31 23:14:42 +00:00
|
|
|
package watcher
|
2019-01-28 22:52:47 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-25 17:44:14 +00:00
|
|
|
"fmt"
|
2019-09-30 17:37:16 +00:00
|
|
|
"time"
|
|
|
|
|
2019-01-28 22:52:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/sirupsen/logrus"
|
2019-10-24 16:35:39 +00:00
|
|
|
|
2019-04-24 20:05:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/fetcher"
|
2019-02-25 04:26:42 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage"
|
2019-02-10 22:42:41 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2019-01-31 23:14:42 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2019-01-28 22:52:47 +00:00
|
|
|
)
|
|
|
|
|
2019-07-30 14:03:03 +00:00
|
|
|
type IStorageWatcher interface {
|
|
|
|
AddTransformers(initializers []transformer.StorageTransformerInitializer)
|
2019-11-01 05:29:57 +00:00
|
|
|
Execute(queueRecheckInterval time.Duration, backFillOn bool)
|
2019-11-04 20:50:10 +00:00
|
|
|
BackFill(startingBlock uint64, backFiller storage.BackFiller)
|
2019-07-30 14:03:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 22:52:47 +00:00
|
|
|
type StorageWatcher struct {
|
2019-07-30 12:52:36 +00:00
|
|
|
db *postgres.DB
|
|
|
|
StorageFetcher fetcher.IStorageFetcher
|
|
|
|
Queue storage.IStorageQueue
|
2019-08-06 14:20:50 +00:00
|
|
|
KeccakAddressTransformers map[common.Hash]transformer.StorageTransformer // keccak hash of an address => transformer
|
2019-10-24 16:35:39 +00:00
|
|
|
DiffsChan chan utils.StorageDiff
|
|
|
|
ErrsChan chan error
|
2019-11-01 05:29:57 +00:00
|
|
|
BackFillDoneChan chan bool
|
2019-11-04 20:50:10 +00:00
|
|
|
StartingSyncBlockChan chan uint64
|
2019-08-16 21:01:01 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 05:29:57 +00:00
|
|
|
func NewStorageWatcher(f fetcher.IStorageFetcher, db *postgres.DB) *StorageWatcher {
|
2019-08-16 21:01:01 +00:00
|
|
|
queue := storage.NewStorageQueue(db)
|
|
|
|
transformers := make(map[common.Hash]transformer.StorageTransformer)
|
2019-11-01 05:29:57 +00:00
|
|
|
return &StorageWatcher{
|
2019-08-16 21:01:01 +00:00
|
|
|
db: db,
|
2019-11-01 05:29:57 +00:00
|
|
|
StorageFetcher: f,
|
|
|
|
DiffsChan: make(chan utils.StorageDiff, fetcher.PayloadChanBufferSize),
|
2019-10-24 16:35:39 +00:00
|
|
|
ErrsChan: make(chan error),
|
2019-11-04 20:50:10 +00:00
|
|
|
StartingSyncBlockChan: make(chan uint64),
|
2019-11-01 05:29:57 +00:00
|
|
|
BackFillDoneChan: make(chan bool),
|
2019-08-16 21:01:01 +00:00
|
|
|
Queue: queue,
|
|
|
|
KeccakAddressTransformers: transformers,
|
|
|
|
}
|
2019-07-10 19:34:14 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 05:29:57 +00:00
|
|
|
func (storageWatcher *StorageWatcher) AddTransformers(initializers []transformer.StorageTransformerInitializer) {
|
2019-01-28 22:52:47 +00:00
|
|
|
for _, initializer := range initializers {
|
2019-04-25 17:44:14 +00:00
|
|
|
storageTransformer := initializer(storageWatcher.db)
|
2019-08-16 21:01:01 +00:00
|
|
|
storageWatcher.KeccakAddressTransformers[storageTransformer.KeccakContractAddress()] = storageTransformer
|
2019-01-28 22:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 05:29:57 +00:00
|
|
|
// BackFill uses a backFiller to backfill missing storage diffs for the storageWatcher
|
2019-11-04 20:50:10 +00:00
|
|
|
func (storageWatcher *StorageWatcher) BackFill(startingBlock uint64, backFiller storage.BackFiller) {
|
2019-10-24 16:35:39 +00:00
|
|
|
// this blocks until the Execute process sends us the first block number it sees
|
2019-11-04 20:50:10 +00:00
|
|
|
endBackFillBlock := <-storageWatcher.StartingSyncBlockChan
|
|
|
|
backFillInitErr := backFiller.BackFill(startingBlock, endBackFillBlock,
|
2019-11-01 05:29:57 +00:00
|
|
|
storageWatcher.DiffsChan, storageWatcher.ErrsChan, storageWatcher.BackFillDoneChan)
|
|
|
|
if backFillInitErr != nil {
|
|
|
|
logrus.Warn(backFillInitErr)
|
2019-10-24 16:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 05:29:57 +00:00
|
|
|
// Execute runs the StorageWatcher processes
|
|
|
|
func (storageWatcher *StorageWatcher) Execute(queueRecheckInterval time.Duration, backFillOn bool) {
|
2019-04-25 17:44:14 +00:00
|
|
|
ticker := time.NewTicker(queueRecheckInterval)
|
2019-10-24 16:35:39 +00:00
|
|
|
go storageWatcher.StorageFetcher.FetchStorageDiffs(storageWatcher.DiffsChan, storageWatcher.ErrsChan)
|
|
|
|
start := true
|
2019-04-24 20:05:57 +00:00
|
|
|
for {
|
|
|
|
select {
|
2019-10-24 16:35:39 +00:00
|
|
|
case fetchErr := <-storageWatcher.ErrsChan:
|
2019-11-01 05:29:57 +00:00
|
|
|
logrus.Warn(fmt.Sprintf("error fetching storage diffs: %s", fetchErr.Error()))
|
2019-10-24 16:35:39 +00:00
|
|
|
case diff := <-storageWatcher.DiffsChan:
|
2019-11-01 05:29:57 +00:00
|
|
|
if start && backFillOn {
|
2019-11-04 20:50:10 +00:00
|
|
|
storageWatcher.StartingSyncBlockChan <- uint64(diff.BlockHeight - 1)
|
2019-10-24 16:35:39 +00:00
|
|
|
start = false
|
|
|
|
}
|
2019-07-29 19:28:20 +00:00
|
|
|
storageWatcher.processRow(diff)
|
2019-04-25 17:44:14 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
storageWatcher.processQueue()
|
2019-11-01 05:29:57 +00:00
|
|
|
case <-storageWatcher.BackFillDoneChan:
|
|
|
|
logrus.Info("storage watcher backfill process has finished")
|
2019-01-28 22:52:47 +00:00
|
|
|
}
|
2019-04-24 20:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 05:29:57 +00:00
|
|
|
func (storageWatcher *StorageWatcher) getTransformer(diff utils.StorageDiff) (transformer.StorageTransformer, bool) {
|
2019-09-10 21:26:54 +00:00
|
|
|
storageTransformer, ok := storageWatcher.KeccakAddressTransformers[diff.HashedAddress]
|
2019-08-16 21:01:01 +00:00
|
|
|
return storageTransformer, ok
|
2019-07-10 19:34:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
func (storageWatcher StorageWatcher) processRow(diff utils.StorageDiff) {
|
2019-08-06 14:20:50 +00:00
|
|
|
storageTransformer, ok := storageWatcher.getTransformer(diff)
|
2019-04-24 20:05:57 +00:00
|
|
|
if !ok {
|
2019-07-29 19:28:20 +00:00
|
|
|
logrus.Debug("ignoring a diff from an unwatched contract")
|
2019-04-24 20:05:57 +00:00
|
|
|
return
|
|
|
|
}
|
2019-07-29 19:28:20 +00:00
|
|
|
executeErr := storageTransformer.Execute(diff)
|
2019-05-15 15:12:38 +00:00
|
|
|
if executeErr != nil {
|
2019-04-29 19:20:57 +00:00
|
|
|
logrus.Warn(fmt.Sprintf("error executing storage transformer: %s", executeErr))
|
2019-07-29 19:28:20 +00:00
|
|
|
queueErr := storageWatcher.Queue.Add(diff)
|
2019-04-29 19:20:57 +00:00
|
|
|
if queueErr != nil {
|
|
|
|
logrus.Warn(fmt.Sprintf("error queueing storage diff: %s", queueErr))
|
2019-01-28 22:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 21:01:07 +00:00
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
func (storageWatcher StorageWatcher) processQueue() {
|
2019-07-29 19:28:20 +00:00
|
|
|
diffs, fetchErr := storageWatcher.Queue.GetAll()
|
2019-04-25 17:44:14 +00:00
|
|
|
if fetchErr != nil {
|
|
|
|
logrus.Warn(fmt.Sprintf("error getting queued storage: %s", fetchErr))
|
|
|
|
}
|
2019-07-29 19:28:20 +00:00
|
|
|
for _, diff := range diffs {
|
2019-08-06 14:20:50 +00:00
|
|
|
storageTransformer, ok := storageWatcher.getTransformer(diff)
|
2019-04-25 17:44:14 +00:00
|
|
|
if !ok {
|
2019-07-29 19:28:20 +00:00
|
|
|
// delete diff from queue if address no longer watched
|
2019-10-18 16:16:19 +00:00
|
|
|
storageWatcher.deleteRow(diff.ID)
|
2019-04-25 17:44:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-07-29 19:28:20 +00:00
|
|
|
executeErr := storageTransformer.Execute(diff)
|
2019-05-15 15:12:38 +00:00
|
|
|
if executeErr == nil {
|
2019-10-18 16:16:19 +00:00
|
|
|
storageWatcher.deleteRow(diff.ID)
|
2019-04-25 17:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (storageWatcher StorageWatcher) deleteRow(id int) {
|
|
|
|
deleteErr := storageWatcher.Queue.Delete(id)
|
|
|
|
if deleteErr != nil {
|
2019-07-29 19:28:20 +00:00
|
|
|
logrus.Warn(fmt.Sprintf("error deleting persisted diff from queue: %s", deleteErr))
|
2019-04-25 17:44:14 +00:00
|
|
|
}
|
|
|
|
}
|