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-06-10 22:06:38 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2019-04-15 16:30:52 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
|
2019-04-18 12:39:37 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2019-04-15 16:30:52 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
|
|
|
|
vRpc "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
|
|
|
"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-04-17 18:26:48 +00:00
|
|
|
syncAndPublish()
|
2019-04-15 16:30:52 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(syncAndPublishCmd)
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
func syncAndPublish() {
|
2019-04-15 16:30:52 +00:00
|
|
|
blockChain, ethClient, rpcClient := getBlockChainAndClients()
|
|
|
|
|
|
|
|
db := utils.LoadPostgres(databaseConfig, blockChain.Node())
|
|
|
|
quitChan := make(chan bool)
|
2019-06-10 22:06:38 +00:00
|
|
|
|
|
|
|
ipfsPath := viper.GetString("client.ipfsPath")
|
|
|
|
if ipfsPath == "" {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
ipfsPath = filepath.Join(home, ".ipfs")
|
|
|
|
}
|
2019-04-30 17:48:31 +00:00
|
|
|
processor, err := ipfs.NewIPFSProcessor(ipfsPath, &db, ethClient, rpcClient, quitChan)
|
2019-04-15 16:30:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-04-18 12:39:37 +00:00
|
|
|
wg := &syn.WaitGroup{}
|
2019-05-17 15:23:39 +00:00
|
|
|
err = processor.SyncAndPublish(wg, nil, nil)
|
2019-04-17 18:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func getBlockChainAndClients() (*geth.BlockChain, core.EthClient, core.RpcClient) {
|
|
|
|
rawRpcClient, err := rpc.Dial(ipc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
rpcClient := client.NewRpcClient(rawRpcClient, ipc)
|
|
|
|
ethClient := ethclient.NewClient(rawRpcClient)
|
|
|
|
vdbEthClient := client.NewEthClient(ethClient)
|
|
|
|
vdbNode := node.MakeNode(rpcClient)
|
|
|
|
transactionConverter := vRpc.NewRpcTransactionConverter(ethClient)
|
|
|
|
blockChain := geth.NewBlockChain(vdbEthClient, rpcClient, vdbNode, transactionConverter)
|
|
|
|
return blockChain, ethClient, rpcClient
|
2019-04-18 12:39:37 +00:00
|
|
|
}
|