2019-01-25 08:59:23 +00:00
|
|
|
// Copyright © 2019 Vulcanize, Inc
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-02-07 21:35:25 +00:00
|
|
|
"errors"
|
2019-01-25 08:59:23 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"plugin"
|
2019-02-07 21:35:25 +00:00
|
|
|
syn "sync"
|
2019-01-25 08:59:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2019-01-31 23:14:42 +00:00
|
|
|
"github.com/spf13/viper"
|
2019-01-25 08:59:23 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/watcher"
|
2019-02-02 22:15:09 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
2019-02-07 21:35:25 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fs"
|
2019-02-02 22:15:09 +00:00
|
|
|
p2 "github.com/vulcanize/vulcanizedb/pkg/plugin"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
|
2019-01-25 08:59:23 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// executePluginCmd represents the execute command
|
|
|
|
var composeAndExecuteCmd = &cobra.Command{
|
|
|
|
Use: "composeAndExecute",
|
|
|
|
Short: "Composes, loads, and executes 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]
|
|
|
|
ipcPath = "http://kovan0.vulcanize.io:8545"
|
|
|
|
|
|
|
|
[exporter]
|
2019-01-31 23:14:42 +00:00
|
|
|
name = "exporter"
|
2019-01-25 08:59:23 +00:00
|
|
|
[exporter.transformers]
|
2019-01-31 01:38:56 +00:00
|
|
|
transformer1 = "github.com/path/to/transformer1"
|
|
|
|
transformer2 = "github.com/path/to/transformer2"
|
|
|
|
transformer3 = "github.com/path/to/transformer3"
|
|
|
|
transformer4 = "github.com/different/path/to/transformer1"
|
2019-02-07 21:35:25 +00:00
|
|
|
[exporter.types]
|
|
|
|
transformer1 = "eth_event"
|
|
|
|
transformer2 = "eth_event"
|
|
|
|
transformer3 = "eth_event"
|
|
|
|
transformer4 = "eth_storage"
|
2019-01-31 01:38:56 +00:00
|
|
|
[exporter.repositories]
|
|
|
|
transformers = "github.com/path/to"
|
2019-02-07 21:35:25 +00:00
|
|
|
transformer4 = "github.com/different/path"
|
2019-01-31 01:38:56 +00:00
|
|
|
[exporter.migrations]
|
|
|
|
transformers = "db/migrations"
|
|
|
|
transformer4 = "to/db/migrations"
|
2019-01-25 08:59:23 +00:00
|
|
|
|
|
|
|
Note: If any of the imported transformer need additional
|
|
|
|
config variables do not forget to include those as well
|
|
|
|
|
2019-02-07 21:35:25 +00:00
|
|
|
This information is used to write and build a go plugin with a transformer
|
2019-01-25 08:59:23 +00:00
|
|
|
set composed from the transformer imports specified in the config file
|
2019-02-07 21:35:25 +00:00
|
|
|
This plugin is loaded and the set of transformer initializers is exported
|
|
|
|
from it and loaded into and executed over by the appropriate watcher.
|
|
|
|
|
|
|
|
The type of watcher that the transformer works with is specified using the
|
|
|
|
exporter.types config variable as shown above. Currently there are watchers
|
|
|
|
of event data from an eth node (eth_event) and storage data from an eth node
|
|
|
|
(eth_storage). Soon there will be watchers for ipfs (ipfs_event and ipfs_storage).
|
|
|
|
|
|
|
|
Transformers of different types can be ran together in the same command using a
|
|
|
|
single config file or in separate command instances using different config files
|
|
|
|
|
|
|
|
Specify config location when executing the command:
|
|
|
|
./vulcanizedb composeAndExecute --config=./environments/config_name.toml`,
|
2019-01-25 08:59:23 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
composeAndExecute()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func composeAndExecute() {
|
2019-02-07 21:35:25 +00:00
|
|
|
// Build plugin generator config
|
|
|
|
prepConfig()
|
2019-01-31 23:14:42 +00:00
|
|
|
|
2019-02-07 21:35:25 +00:00
|
|
|
// Generate code to build the plugin according to the config file
|
2019-01-31 23:14:42 +00:00
|
|
|
fmt.Println("generating plugin")
|
2019-02-02 22:15:09 +00:00
|
|
|
generator, err := p2.NewGenerator(genConfig, databaseConfig)
|
2019-01-25 08:59:23 +00:00
|
|
|
if err != nil {
|
2019-02-02 22:15:09 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = generator.GenerateExporterPlugin()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(os.Stderr, "generating plugin failed")
|
2019-01-25 08:59:23 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:38:56 +00:00
|
|
|
// Get the plugin path and load the plugin
|
2019-02-02 22:15:09 +00:00
|
|
|
_, pluginPath, err := genConfig.GetPluginPaths()
|
2019-01-25 08:59:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-02-02 22:15:09 +00:00
|
|
|
if !genConfig.Save {
|
|
|
|
defer helpers.ClearFiles(pluginPath)
|
2019-01-31 23:14:42 +00:00
|
|
|
}
|
|
|
|
fmt.Println("opening plugin")
|
2019-01-25 08:59:23 +00:00
|
|
|
plug, err := plugin.Open(pluginPath)
|
|
|
|
if err != nil {
|
2019-02-02 22:15:09 +00:00
|
|
|
fmt.Fprint(os.Stderr, "opening pluggin failed")
|
2019-01-25 08:59:23 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:38:56 +00:00
|
|
|
// Load the `Exporter` symbol from the plugin
|
2019-01-31 23:14:42 +00:00
|
|
|
fmt.Println("loading transformers from plugin")
|
2019-01-25 08:59:23 +00:00
|
|
|
symExporter, err := plug.Lookup("Exporter")
|
|
|
|
if err != nil {
|
2019-02-02 22:15:09 +00:00
|
|
|
fmt.Fprint(os.Stderr, "loading Exporter symbol failed")
|
2019-01-25 08:59:23 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:38:56 +00:00
|
|
|
// Assert that the symbol is of type Exporter
|
2019-01-25 08:59:23 +00:00
|
|
|
exporter, ok := symExporter.(Exporter)
|
|
|
|
if !ok {
|
2019-02-02 22:15:09 +00:00
|
|
|
fmt.Fprint(os.Stderr, "plugged-in symbol not of type Exporter")
|
2019-01-25 08:59:23 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-02-07 21:35:25 +00:00
|
|
|
// Use the Exporters export method to load the TransformerInitializer and StorageTransformerInitializer sets
|
|
|
|
ethEventInitializers, ethStorageInitializers := exporter.Export()
|
2019-01-31 01:38:56 +00:00
|
|
|
|
|
|
|
// Setup bc and db objects
|
|
|
|
blockChain := getBlockChain()
|
|
|
|
db := utils.LoadPostgres(databaseConfig, blockChain.Node())
|
|
|
|
|
2019-02-07 21:35:25 +00:00
|
|
|
// Execute over transformer sets returned by the exporter
|
|
|
|
// Use WaitGroup to wait on both goroutines
|
|
|
|
var wg syn.WaitGroup
|
|
|
|
if len(ethEventInitializers) > 0 {
|
|
|
|
w := watcher.NewWatcher(&db, blockChain)
|
|
|
|
w.AddTransformers(ethEventInitializers)
|
|
|
|
wg.Add(1)
|
|
|
|
go watchEthEvents(&w, &wg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ethStorageInitializers) > 0 {
|
|
|
|
tailer := fs.FileTailer{Path: storageDiffsPath}
|
|
|
|
w := watcher.NewStorageWatcher(tailer, &db)
|
|
|
|
w.AddTransformers(ethStorageInitializers)
|
|
|
|
wg.Add(1)
|
|
|
|
go watchEthStorage(&w, &wg)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Exporter interface {
|
|
|
|
Export() ([]transformer.TransformerInitializer, []transformer.StorageTransformerInitializer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(composeAndExecuteCmd)
|
|
|
|
composeAndExecuteCmd.Flags().Int64VarP(&startingBlockNumber, "starting-block-number", "s", 0, "Block number to start transformer execution from")
|
|
|
|
}
|
2019-01-25 08:59:23 +00:00
|
|
|
|
2019-02-07 21:35:25 +00:00
|
|
|
func watchEthEvents(w *watcher.Watcher, wg *syn.WaitGroup) {
|
|
|
|
defer wg.Done()
|
2019-01-31 01:38:56 +00:00
|
|
|
// Execute over the TransformerInitializer set using the watcher
|
2019-01-31 23:14:42 +00:00
|
|
|
fmt.Println("executing transformers")
|
2019-01-31 01:38:56 +00:00
|
|
|
ticker := time.NewTicker(pollingInterval)
|
|
|
|
defer ticker.Stop()
|
2019-01-25 08:59:23 +00:00
|
|
|
for range ticker.C {
|
|
|
|
err := w.Execute()
|
|
|
|
if err != nil {
|
|
|
|
// TODO Handle watcher errors in composeAndExecute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 21:35:25 +00:00
|
|
|
func watchEthStorage(w *watcher.StorageWatcher, wg *syn.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
// Execute over the TransformerInitializer set using the watcher
|
|
|
|
fmt.Println("executing transformers")
|
|
|
|
ticker := time.NewTicker(pollingInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for range ticker.C {
|
|
|
|
err := w.Execute()
|
|
|
|
if err != nil {
|
|
|
|
// TODO Handle watcher errors in composeAndExecute
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 08:59:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:35:25 +00:00
|
|
|
func prepConfig() {
|
|
|
|
fmt.Println("configuring plugin")
|
|
|
|
types := viper.GetStringMapString("exporter.types")
|
|
|
|
genTypes := map[string]config.PluginType{}
|
|
|
|
for transformerName, transformerType := range types {
|
|
|
|
genType := config.GetPluginType(transformerType)
|
|
|
|
if genType == config.UnknownTransformerType {
|
|
|
|
log.Fatal(errors.New(`unknown transformer type in exporter config
|
|
|
|
accepted types are "eth_event", "eth_storage", "ipfs_event" and "ipfs_storage"`))
|
|
|
|
}
|
|
|
|
genTypes[transformerName] = genType
|
|
|
|
}
|
|
|
|
genConfig = config.Plugin{
|
|
|
|
FilePath: "$GOPATH/src/github.com/vulcanize/vulcanizedb/plugins",
|
|
|
|
FileName: viper.GetString("exporter.name"),
|
|
|
|
Save: viper.GetBool("exporter.save"),
|
|
|
|
Initializers: viper.GetStringMapString("exporter.transformers"),
|
|
|
|
Dependencies: viper.GetStringMapString("exporter.repositories"),
|
|
|
|
Migrations: viper.GetStringMapString("exporter.migrations"),
|
|
|
|
Types: genTypes,
|
|
|
|
}
|
2019-01-25 08:59:23 +00:00
|
|
|
}
|