2019-04-15 16:30:52 +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-06-10 22:06:38 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-04-15 16:30:52 +00:00
|
|
|
syn "sync"
|
2019-10-02 14:10:37 +00:00
|
|
|
"time"
|
2019-04-15 16:30:52 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2019-10-08 19:51:38 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2019-04-15 16:30:52 +00:00
|
|
|
|
2019-04-18 12:39:37 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2019-10-14 14:41:29 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/client"
|
|
|
|
vRpc "github.com/vulcanize/vulcanizedb/pkg/eth/converters/rpc"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/node"
|
2019-10-08 19:51:38 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node"
|
2019-04-15 16:30:52 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// syncAndPublishCmd represents the syncAndPublish command
|
|
|
|
var syncAndPublishCmd = &cobra.Command{
|
|
|
|
Use: "syncAndPublish",
|
2019-04-17 18:26:48 +00:00
|
|
|
Short: "Syncs all Ethereum data into IPFS, indexing the CIDs",
|
|
|
|
Long: `This command works alongside a modified geth node which streams
|
|
|
|
all block and state (diff) data over a websocket subscription. This process
|
2019-06-06 17:55:59 +00:00
|
|
|
then converts the eth data to IPLD objects and publishes them to IPFS. Additionally,
|
2019-04-17 18:26:48 +00:00
|
|
|
it maintains a local index of the IPLD objects' CIDs in Postgres.`,
|
2019-04-15 16:30:52 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-10-14 14:41:29 +00:00
|
|
|
subCommand = cmd.CalledAs()
|
|
|
|
logWithCommand = *log.WithField("SubCommand", subCommand)
|
2019-04-17 18:26:48 +00:00
|
|
|
syncAndPublish()
|
2019-04-15 16:30:52 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
var ipfsPath string
|
|
|
|
|
2019-04-15 16:30:52 +00:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(syncAndPublishCmd)
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
func syncAndPublish() {
|
2019-10-08 19:51:38 +00:00
|
|
|
superNode, newNodeErr := newSuperNode()
|
|
|
|
if newNodeErr != nil {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Fatal(newNodeErr)
|
2019-04-15 16:30:52 +00:00
|
|
|
}
|
2019-04-18 12:39:37 +00:00
|
|
|
wg := &syn.WaitGroup{}
|
2019-10-08 19:51:38 +00:00
|
|
|
syncAndPubErr := superNode.SyncAndPublish(wg, nil, nil)
|
|
|
|
if syncAndPubErr != nil {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Fatal(syncAndPubErr)
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
2019-10-14 14:41:29 +00:00
|
|
|
if viper.GetBool("superNodeBackFill.on") && viper.GetString("superNodeBackFill.rpcPath") != "" {
|
2019-10-08 19:51:38 +00:00
|
|
|
backfiller, newBackFillerErr := newBackFiller()
|
|
|
|
if newBackFillerErr != nil {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Fatal(newBackFillerErr)
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
backfiller.FillGaps(wg, nil)
|
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
wg.Wait() // If an error was thrown, wg.Add was never called and this will fall through
|
2019-04-15 16:30:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 19:14:05 +00:00
|
|
|
func getBlockChainAndClient(path string) (*eth.BlockChain, core.RPCClient) {
|
2019-10-14 14:41:29 +00:00
|
|
|
rawRPCClient, dialErr := rpc.Dial(path)
|
2019-10-08 19:51:38 +00:00
|
|
|
if dialErr != nil {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Fatal(dialErr)
|
2019-04-15 16:30:52 +00:00
|
|
|
}
|
2019-11-04 19:14:05 +00:00
|
|
|
rpcClient := client.NewRPCClient(rawRPCClient, ipc)
|
2019-10-14 14:41:29 +00:00
|
|
|
ethClient := ethclient.NewClient(rawRPCClient)
|
2019-04-15 16:30:52 +00:00
|
|
|
vdbEthClient := client.NewEthClient(ethClient)
|
|
|
|
vdbNode := node.MakeNode(rpcClient)
|
2019-11-04 19:14:05 +00:00
|
|
|
transactionConverter := vRpc.NewRPCTransactionConverter(ethClient)
|
2019-10-14 14:41:29 +00:00
|
|
|
blockChain := eth.NewBlockChain(vdbEthClient, rpcClient, vdbNode, transactionConverter)
|
2019-08-26 02:13:40 +00:00
|
|
|
return blockChain, rpcClient
|
2019-04-18 12:39:37 +00:00
|
|
|
}
|
2019-10-02 14:10:37 +00:00
|
|
|
|
|
|
|
func newSuperNode() (super_node.NodeInterface, error) {
|
|
|
|
blockChain, rpcClient := getBlockChainAndClient(ipc)
|
|
|
|
db := utils.LoadPostgres(databaseConfig, blockChain.Node())
|
|
|
|
quitChan := make(chan bool)
|
|
|
|
ipfsPath = viper.GetString("client.ipfsPath")
|
|
|
|
if ipfsPath == "" {
|
2019-10-08 19:51:38 +00:00
|
|
|
home, homeDirErr := os.UserHomeDir()
|
|
|
|
if homeDirErr != nil {
|
2019-10-14 14:41:29 +00:00
|
|
|
logWithCommand.Fatal(homeDirErr)
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
ipfsPath = filepath.Join(home, ".ipfs")
|
|
|
|
}
|
|
|
|
workers := viper.GetInt("client.workers")
|
|
|
|
if workers < 1 {
|
|
|
|
workers = 1
|
|
|
|
}
|
|
|
|
return super_node.NewSuperNode(ipfsPath, &db, rpcClient, quitChan, workers, blockChain.Node())
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:51:38 +00:00
|
|
|
func newBackFiller() (super_node.BackFillInterface, error) {
|
2019-10-14 14:41:29 +00:00
|
|
|
blockChain, archivalRPCClient := getBlockChainAndClient(viper.GetString("superNodeBackFill.rpcPath"))
|
2019-10-02 14:10:37 +00:00
|
|
|
db := utils.LoadPostgres(databaseConfig, blockChain.Node())
|
2019-10-14 14:41:29 +00:00
|
|
|
freq := viper.GetInt("superNodeBackFill.frequency")
|
2019-10-02 14:10:37 +00:00
|
|
|
var frequency time.Duration
|
|
|
|
if freq <= 0 {
|
|
|
|
frequency = time.Minute * 5
|
|
|
|
} else {
|
|
|
|
frequency = time.Duration(freq)
|
|
|
|
}
|
2019-11-01 19:03:28 +00:00
|
|
|
return super_node.NewBackFillService(ipfsPath, &db, archivalRPCClient, time.Minute*frequency, super_node.DefaultMaxBatchSize)
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|