2019-01-28 22:52:47 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2018 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/>.
|
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
package watcher
|
2019-01-28 22:52:47 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-31 23:14:42 +00:00
|
|
|
"reflect"
|
2019-02-25 04:26:42 +00:00
|
|
|
"strings"
|
2019-01-31 23:14:42 +00:00
|
|
|
|
2019-01-28 22:52:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
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
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StorageWatcher struct {
|
|
|
|
db *postgres.DB
|
|
|
|
tailer fs.Tailer
|
2019-02-25 04:26:42 +00:00
|
|
|
Queue storage.IStorageQueue
|
2019-01-31 23:14:42 +00:00
|
|
|
Transformers map[common.Address]transformer.StorageTransformer
|
2019-01-28 22:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStorageWatcher(tailer fs.Tailer, db *postgres.DB) StorageWatcher {
|
2019-01-31 23:14:42 +00:00
|
|
|
transformers := make(map[common.Address]transformer.StorageTransformer)
|
2019-02-25 04:26:42 +00:00
|
|
|
queue := storage.NewStorageQueue(db)
|
2019-01-28 22:52:47 +00:00
|
|
|
return StorageWatcher{
|
|
|
|
db: db,
|
|
|
|
tailer: tailer,
|
2019-02-19 21:01:07 +00:00
|
|
|
Queue: queue,
|
2019-01-28 22:52:47 +00:00
|
|
|
Transformers: transformers,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
func (watcher StorageWatcher) AddTransformers(initializers []transformer.StorageTransformerInitializer) {
|
2019-01-28 22:52:47 +00:00
|
|
|
for _, initializer := range initializers {
|
2019-02-28 14:59:04 +00:00
|
|
|
storageTransformer := initializer(watcher.db)
|
|
|
|
watcher.Transformers[storageTransformer.ContractAddress()] = storageTransformer
|
2019-01-28 22:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (watcher StorageWatcher) Execute() error {
|
|
|
|
t, tailErr := watcher.tailer.Tail()
|
|
|
|
if tailErr != nil {
|
|
|
|
return tailErr
|
|
|
|
}
|
|
|
|
for line := range t.Lines {
|
2019-01-31 23:14:42 +00:00
|
|
|
row, parseErr := utils.FromStrings(strings.Split(line.Text, ","))
|
2019-01-28 22:52:47 +00:00
|
|
|
if parseErr != nil {
|
|
|
|
return parseErr
|
|
|
|
}
|
2019-02-28 14:59:04 +00:00
|
|
|
storageTransformer, ok := watcher.Transformers[row.Contract]
|
2019-01-28 22:52:47 +00:00
|
|
|
if !ok {
|
2019-01-31 23:14:42 +00:00
|
|
|
logrus.Warn(utils.ErrContractNotFound{Contract: row.Contract.Hex()}.Error())
|
2019-01-28 22:52:47 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-02-28 14:59:04 +00:00
|
|
|
executeErr := storageTransformer.Execute(row)
|
2019-01-28 22:52:47 +00:00
|
|
|
if executeErr != nil {
|
2019-02-19 21:01:07 +00:00
|
|
|
if isKeyNotFound(executeErr) {
|
|
|
|
queueErr := watcher.Queue.Add(row)
|
|
|
|
if queueErr != nil {
|
|
|
|
logrus.Warn(queueErr.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Warn(executeErr.Error())
|
|
|
|
}
|
2019-01-28 22:52:47 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-19 21:01:07 +00:00
|
|
|
|
|
|
|
func isKeyNotFound(executeErr error) bool {
|
2019-01-31 23:14:42 +00:00
|
|
|
return reflect.TypeOf(executeErr) == reflect.TypeOf(utils.ErrStorageKeyNotFound{})
|
2019-02-19 21:01:07 +00:00
|
|
|
}
|