2019-08-05 18:28:06 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
2019-02-28 17:51:54 +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.
|
2019-08-05 18:28:06 +00:00
|
|
|
|
2019-02-28 17:51:54 +00:00
|
|
|
// 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.
|
2019-08-05 18:28:06 +00:00
|
|
|
|
2019-02-28 17:51:54 +00:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2019-08-05 18:28:06 +00:00
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-02-28 17:51:54 +00:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-11 13:01:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/fetcher"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/streamer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fs"
|
2019-02-28 17:51:54 +00:00
|
|
|
"plugin"
|
|
|
|
syn "sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/constants"
|
2019-04-25 17:44:14 +00:00
|
|
|
storageUtils "github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2019-02-28 17:51:54 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/watcher"
|
|
|
|
"github.com/vulcanize/vulcanizedb/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// executeCmd represents the execute command
|
|
|
|
var executeCmd = &cobra.Command{
|
|
|
|
Use: "execute",
|
|
|
|
Short: "executes a precomposed transformer initializer plugin",
|
|
|
|
Long: `This command needs a config .toml file of form:
|
|
|
|
|
|
|
|
[database]
|
|
|
|
name = "vulcanize_public"
|
|
|
|
hostname = "localhost"
|
|
|
|
user = "vulcanize"
|
|
|
|
password = "vulcanize"
|
|
|
|
port = 5432
|
|
|
|
|
|
|
|
[client]
|
2019-03-14 21:49:27 +00:00
|
|
|
ipcPath = "/Users/user/Library/Ethereum/geth.ipc"
|
2019-02-28 17:51:54 +00:00
|
|
|
|
|
|
|
[exporter]
|
|
|
|
name = "exampleTransformerExporter"
|
|
|
|
|
|
|
|
Note: If any of the plugin transformer need additional
|
|
|
|
configuration variables include them in the .toml file as well
|
|
|
|
|
|
|
|
The exporter.name is the name (without extension) of the plugin to be loaded.
|
|
|
|
The plugin file needs to be located in the /plugins directory and this command assumes
|
|
|
|
the db migrations remain from when the plugin was composed. Additionally, the plugin
|
|
|
|
must have been composed by the same version of vulcanizedb or else it will not be compatible.
|
|
|
|
|
|
|
|
Specify config location when executing the command:
|
|
|
|
./vulcanizedb execute --config=./environments/config_name.toml`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-07-19 01:58:01 +00:00
|
|
|
SubCommand = cmd.CalledAs()
|
|
|
|
LogWithCommand = *log.WithField("SubCommand", SubCommand)
|
2019-02-28 17:51:54 +00:00
|
|
|
execute()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func execute() {
|
|
|
|
// Build plugin generator config
|
|
|
|
prepConfig()
|
|
|
|
|
|
|
|
// Get the plugin path and load the plugin
|
|
|
|
_, pluginPath, err := genConfig.GetPluginPaths()
|
|
|
|
if err != nil {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal(err)
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Executing plugin %s", pluginPath)
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Info("linking plugin ", pluginPath)
|
2019-02-28 17:51:54 +00:00
|
|
|
plug, err := plugin.Open(pluginPath)
|
|
|
|
if err != nil {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Warn("linking plugin failed")
|
|
|
|
LogWithCommand.Fatal(err)
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the `Exporter` symbol from the plugin
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Info("loading transformers from plugin")
|
2019-02-28 17:51:54 +00:00
|
|
|
symExporter, err := plug.Lookup("Exporter")
|
|
|
|
if err != nil {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Warn("loading Exporter symbol failed")
|
|
|
|
LogWithCommand.Fatal(err)
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that the symbol is of type Exporter
|
|
|
|
exporter, ok := symExporter.(Exporter)
|
|
|
|
if !ok {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal("plugged-in symbol not of type Exporter")
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
// Use the Exporters export method to load the EventTransformerInitializer, StorageTransformerInitializer, and ContractTransformerInitializer sets
|
|
|
|
ethEventInitializers, ethStorageInitializers, ethContractInitializers := exporter.Export()
|
2019-02-28 17:51:54 +00:00
|
|
|
|
|
|
|
// Setup bc and db objects
|
2019-07-19 01:58:01 +00:00
|
|
|
blockChain := getBlockChain()
|
2019-02-28 17:51:54 +00:00
|
|
|
db := utils.LoadPostgres(databaseConfig, blockChain.Node())
|
|
|
|
|
|
|
|
// Execute over transformer sets returned by the exporter
|
|
|
|
// Use WaitGroup to wait on both goroutines
|
|
|
|
var wg syn.WaitGroup
|
|
|
|
if len(ethEventInitializers) > 0 {
|
|
|
|
ew := watcher.NewEventWatcher(&db, blockChain)
|
2019-08-26 21:43:14 +00:00
|
|
|
err = ew.AddTransformers(ethEventInitializers)
|
|
|
|
if err != nil {
|
|
|
|
LogWithCommand.Fatalf("failed to add event transformer initializers to watcher: %s", err.Error())
|
|
|
|
}
|
2019-02-28 17:51:54 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go watchEthEvents(&ew, &wg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ethStorageInitializers) > 0 {
|
2019-07-11 13:01:01 +00:00
|
|
|
switch stateDiffSource {
|
|
|
|
case "geth":
|
|
|
|
log.Debug("fetching storage diffs from geth pub sub")
|
|
|
|
rpcClient, _ := getClients()
|
|
|
|
stateDiffStreamer := streamer.NewStateDiffStreamer(rpcClient)
|
|
|
|
payloadChan := make(chan statediff.Payload)
|
|
|
|
storageFetcher := fetcher.NewGethRpcStorageFetcher(&stateDiffStreamer, payloadChan)
|
2019-08-01 13:26:12 +00:00
|
|
|
sw := watcher.NewGethStorageWatcher(storageFetcher, &db)
|
2019-07-11 13:01:01 +00:00
|
|
|
sw.AddTransformers(ethStorageInitializers)
|
|
|
|
wg.Add(1)
|
|
|
|
go watchEthStorage(&sw, &wg)
|
|
|
|
default:
|
|
|
|
log.Debug("fetching storage diffs from csv")
|
|
|
|
tailer := fs.FileTailer{Path: storageDiffsPath}
|
|
|
|
storageFetcher := fetcher.NewCsvTailStorageFetcher(tailer)
|
2019-07-30 14:03:03 +00:00
|
|
|
sw := watcher.NewCsvStorageWatcher(storageFetcher, &db)
|
2019-07-11 13:01:01 +00:00
|
|
|
sw.AddTransformers(ethStorageInitializers)
|
|
|
|
wg.Add(1)
|
|
|
|
go watchEthStorage(&sw, &wg)
|
|
|
|
}
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
2019-03-11 16:43:43 +00:00
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
if len(ethContractInitializers) > 0 {
|
|
|
|
gw := watcher.NewContractWatcher(&db, blockChain)
|
|
|
|
gw.AddTransformers(ethContractInitializers)
|
2019-03-11 16:43:43 +00:00
|
|
|
wg.Add(1)
|
2019-03-14 21:49:27 +00:00
|
|
|
go watchEthContract(&gw, &wg)
|
2019-03-11 16:43:43 +00:00
|
|
|
}
|
2019-02-28 17:51:54 +00:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(executeCmd)
|
2019-04-29 20:21:32 +00:00
|
|
|
executeCmd.Flags().BoolVarP(&recheckHeadersArg, "recheck-headers", "r", false, "whether to re-check headers for watched events")
|
2019-05-15 15:12:38 +00:00
|
|
|
executeCmd.Flags().DurationVarP(&queueRecheckInterval, "queue-recheck-interval", "q", 5*time.Minute, "interval duration for rechecking queued storage diffs (ex: 5m30s)")
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Exporter interface {
|
2019-03-13 16:14:35 +00:00
|
|
|
Export() ([]transformer.EventTransformerInitializer, []transformer.StorageTransformerInitializer, []transformer.ContractTransformerInitializer)
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func watchEthEvents(w *watcher.EventWatcher, wg *syn.WaitGroup) {
|
|
|
|
defer wg.Done()
|
2019-03-14 16:59:39 +00:00
|
|
|
// Execute over the EventTransformerInitializer set using the watcher
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Info("executing event transformers")
|
2019-02-28 17:51:54 +00:00
|
|
|
var recheck constants.TransformerExecution
|
|
|
|
if recheckHeadersArg {
|
|
|
|
recheck = constants.HeaderRecheck
|
|
|
|
} else {
|
2019-09-10 19:39:45 +00:00
|
|
|
recheck = constants.HeaderUnchecked
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
2019-09-19 01:55:15 +00:00
|
|
|
err := w.Execute(recheck)
|
|
|
|
if err != nil {
|
|
|
|
LogWithCommand.Fatalf("error executing event watcher: %s", err.Error())
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 14:03:03 +00:00
|
|
|
func watchEthStorage(w watcher.IStorageWatcher, wg *syn.WaitGroup) {
|
2019-02-28 17:51:54 +00:00
|
|
|
defer wg.Done()
|
2019-03-11 16:43:43 +00:00
|
|
|
// Execute over the StorageTransformerInitializer set using the storage watcher
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Info("executing storage transformers")
|
2019-02-28 17:51:54 +00:00
|
|
|
ticker := time.NewTicker(pollingInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for range ticker.C {
|
2019-04-25 17:44:14 +00:00
|
|
|
errs := make(chan error)
|
2019-07-30 14:03:03 +00:00
|
|
|
diffs := make(chan storageUtils.StorageDiff)
|
|
|
|
w.Execute(diffs, errs, queueRecheckInterval)
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-11 16:43:43 +00:00
|
|
|
|
2019-03-14 21:49:27 +00:00
|
|
|
func watchEthContract(w *watcher.ContractWatcher, wg *syn.WaitGroup) {
|
2019-03-11 16:43:43 +00:00
|
|
|
defer wg.Done()
|
2019-03-13 16:14:35 +00:00
|
|
|
// Execute over the ContractTransformerInitializer set using the contract watcher
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Info("executing contract_watcher transformers")
|
2019-03-11 16:43:43 +00:00
|
|
|
ticker := time.NewTicker(pollingInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for range ticker.C {
|
2019-03-14 21:49:27 +00:00
|
|
|
w.Execute()
|
2019-03-11 16:43:43 +00:00
|
|
|
}
|
|
|
|
}
|