2018-09-19 15:14:49 +00:00
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-11-12 13:01:07 +00:00
|
|
|
"fmt"
|
2018-09-19 15:14:49 +00:00
|
|
|
"time"
|
|
|
|
|
2018-11-21 04:47:01 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-09-19 15:14:49 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers"
|
|
|
|
shared2 "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
2018-11-08 15:14:35 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
|
2018-09-19 15:14:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// continuousLogSyncCmd represents the continuousLogSync command
|
|
|
|
var continuousLogSyncCmd = &cobra.Command{
|
|
|
|
Use: "continuousLogSync",
|
|
|
|
Short: "Continuously sync logs at the head of the chain",
|
2018-11-12 13:01:07 +00:00
|
|
|
Long: fmt.Sprintf(`Continously syncs logs based on the configured transformers.
|
2018-09-19 15:14:49 +00:00
|
|
|
|
|
|
|
vulcanizedb continousLogSync --config environments/local.toml
|
2018-11-12 13:01:07 +00:00
|
|
|
|
|
|
|
Available transformers for (optional) selection with --transformers:
|
|
|
|
%v
|
2018-09-19 15:14:49 +00:00
|
|
|
|
|
|
|
This command expects a light sync to have been run, and the presence of header records in the Vulcanize database.`,
|
2018-11-12 20:59:16 +00:00
|
|
|
constants.AllTransformerLabels()),
|
2018-09-19 15:14:49 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
syncMakerLogs()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var transformerNames []string
|
|
|
|
|
|
|
|
func syncMakerLogs() {
|
|
|
|
ticker := time.NewTicker(pollingInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
blockChain := getBlockChain()
|
|
|
|
db, err := postgres.NewDB(databaseConfig, blockChain.Node())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to initialize database.")
|
|
|
|
}
|
|
|
|
|
2018-12-13 11:39:57 +00:00
|
|
|
initializers := getTransformerInitializers(transformerNames)
|
2018-09-19 15:14:49 +00:00
|
|
|
|
2019-01-07 14:19:31 +00:00
|
|
|
watcher := shared.NewWatcher(db, blockChain)
|
2018-12-13 11:39:57 +00:00
|
|
|
watcher.AddTransformers(initializers)
|
2018-09-19 15:14:49 +00:00
|
|
|
|
|
|
|
for range ticker.C {
|
2018-12-13 11:39:57 +00:00
|
|
|
err = watcher.Execute()
|
|
|
|
if err != nil {
|
|
|
|
// TODO Handle watcher errors in ContinuousLogSync
|
|
|
|
}
|
2018-09-19 15:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 11:39:57 +00:00
|
|
|
func getTransformerInitializers(transformerNames []string) []shared2.TransformerInitializer {
|
2018-12-12 14:41:29 +00:00
|
|
|
var initializers []shared2.TransformerInitializer
|
2018-09-19 15:14:49 +00:00
|
|
|
|
|
|
|
if transformerNames[0] == "all" {
|
2018-12-12 14:41:29 +00:00
|
|
|
initializers = transformers.TransformerInitializers()
|
2018-09-19 15:14:49 +00:00
|
|
|
} else {
|
2018-12-12 14:41:29 +00:00
|
|
|
initializerMap := buildTransformerInitializerMap()
|
2018-09-19 15:14:49 +00:00
|
|
|
for _, transformerName := range transformerNames {
|
2018-12-12 14:41:29 +00:00
|
|
|
initializers = append(initializers, initializerMap[transformerName])
|
2018-09-19 15:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-13 11:39:57 +00:00
|
|
|
return initializers
|
2018-09-19 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildTransformerInitializerMap() map[string]shared2.TransformerInitializer {
|
2018-12-12 14:41:29 +00:00
|
|
|
initializerMap := make(map[string]shared2.TransformerInitializer)
|
|
|
|
|
2019-01-23 18:44:09 +00:00
|
|
|
initializerMap[constants.BiteLabel] = transformers.GetBiteTransformer().NewTransformer
|
|
|
|
initializerMap[constants.CatFileChopLumpLabel] = transformers.GetCatFileChopLumpTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.CatFileFlipLabel] = transformers.GetCatFileFlipTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.CatFilePitVowLabel] = transformers.GetCatFilePitVowTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.DealLabel] = transformers.GetDealTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.DentLabel] = transformers.GetDentTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.DripDripLabel] = transformers.GetDripDripTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.DripFileIlkLabel] = transformers.GetDripFileIlkTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.DripFileRepoLabel] = transformers.GetDripFileRepoTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.DripFileVowLabel] = transformers.GetDripFileVowTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.FlapKickLabel] = transformers.GetFlapKickTransformer().NewTransformer
|
|
|
|
initializerMap[constants.FlipKickLabel] = transformers.GetFlipKickTransformer().NewTransformer
|
|
|
|
initializerMap[constants.FlopKickLabel] = transformers.GetFlopKickTransformer().NewTransformer
|
|
|
|
initializerMap[constants.FrobLabel] = transformers.GetFrobTransformer().NewTransformer
|
|
|
|
initializerMap[constants.PitFileDebtCeilingLabel] = transformers.GetPitFileDebtCeilingTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.PitFileIlkLabel] = transformers.GetPitFileIlkTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.PriceFeedLabel] = transformers.GetPriceFeedTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.TendLabel] = transformers.GetTendTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.VatFluxLabel] = transformers.GetVatFluxTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.VatFoldLabel] = transformers.GetVatFoldTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.VatGrabLabel] = transformers.GetVatGrabTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.VatHealLabel] = transformers.GetVatHealTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.VatInitLabel] = transformers.GetVatInitTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.VatMoveLabel] = transformers.GetVatMoveTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.VatSlipLabel] = transformers.GetVatSlipTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.VatTollLabel] = transformers.GetVatTollTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.VatTuneLabel] = transformers.GetVatTuneTransformer().NewLogNoteTransformer
|
|
|
|
initializerMap[constants.VowFlogLabel] = transformers.GetFlogTransformer().NewLogNoteTransformer
|
2018-12-12 14:41:29 +00:00
|
|
|
|
|
|
|
return initializerMap
|
|
|
|
}
|
|
|
|
|
2018-09-19 15:14:49 +00:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(continuousLogSyncCmd)
|
|
|
|
continuousLogSyncCmd.Flags().StringSliceVar(&transformerNames, "transformers", []string{"all"}, "transformer names to be run during this command")
|
|
|
|
}
|