2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +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.
|
|
|
|
|
|
|
|
// 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/>.
|
2018-05-02 16:17:02 +00:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-11-21 04:47:01 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-05-02 16:17:02 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-05-07 15:41:02 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/crypto"
|
2018-05-02 16:17:02 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/ethereum"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
2018-05-07 15:41:02 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fs"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/cold_import"
|
2018-05-02 16:17:02 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/converters/cold_db"
|
|
|
|
vulcCommon "github.com/vulcanize/vulcanizedb/pkg/geth/converters/common"
|
|
|
|
"github.com/vulcanize/vulcanizedb/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var coldImportCmd = &cobra.Command{
|
|
|
|
Use: "coldImport",
|
|
|
|
Short: "Sync vulcanize from a cold instance of LevelDB.",
|
|
|
|
Long: `Populate core vulcanize db data directly out of LevelDB, rather than over rpc calls. For example:
|
|
|
|
|
|
|
|
./vulcanizedb coldImport -s 0 -e 5000000
|
|
|
|
|
|
|
|
Geth must be synced over all of the desired blocks and must not be running in order to execute this command.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-07-16 08:57:30 +00:00
|
|
|
subCommand = cmd.CalledAs()
|
2018-05-02 16:17:02 +00:00
|
|
|
coldImport()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(coldImportCmd)
|
2018-07-17 21:23:07 +00:00
|
|
|
coldImportCmd.Flags().Int64VarP(&startingBlockNumber, "starting-block-number", "s", 0, "BlockNumber for first block to cold import.")
|
|
|
|
coldImportCmd.Flags().Int64VarP(&endingBlockNumber, "ending-block-number", "e", 5500000, "BlockNumber for last block to cold import.")
|
2018-05-07 15:41:02 +00:00
|
|
|
coldImportCmd.Flags().BoolVarP(&syncAll, "all", "a", false, "Option to sync all missing blocks.")
|
2018-05-02 16:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func coldImport() {
|
|
|
|
// init eth db
|
|
|
|
ethDBConfig := ethereum.CreateDatabaseConfig(ethereum.Level, levelDbPath)
|
|
|
|
ethDB, err := ethereum.CreateDatabase(ethDBConfig)
|
|
|
|
if err != nil {
|
2019-07-16 08:57:30 +00:00
|
|
|
log.WithField("subCommand", subCommand).Fatal("Error connecting to ethereum db: ", err)
|
2018-05-02 16:17:02 +00:00
|
|
|
}
|
2018-05-07 15:41:02 +00:00
|
|
|
mostRecentBlockNumberInDb := ethDB.GetHeadBlockNumber()
|
|
|
|
if syncAll {
|
|
|
|
startingBlockNumber = 0
|
|
|
|
endingBlockNumber = mostRecentBlockNumberInDb
|
|
|
|
}
|
2018-05-03 21:58:45 +00:00
|
|
|
if endingBlockNumber < startingBlockNumber {
|
2019-07-16 08:57:30 +00:00
|
|
|
log.WithField("subCommand", subCommand).Fatal("Ending block number must be greater than starting block number for cold import.")
|
2018-05-03 21:58:45 +00:00
|
|
|
}
|
|
|
|
if endingBlockNumber > mostRecentBlockNumberInDb {
|
2019-07-16 08:57:30 +00:00
|
|
|
log.WithField("subCommand", subCommand).Fatal("Ending block number is greater than most recent block in db: ", mostRecentBlockNumberInDb)
|
2018-05-03 21:58:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 16:17:02 +00:00
|
|
|
// init pg db
|
2018-05-07 15:41:02 +00:00
|
|
|
genesisBlock := ethDB.GetBlockHash(0)
|
|
|
|
reader := fs.FsReader{}
|
|
|
|
parser := crypto.EthPublicKeyParser{}
|
|
|
|
nodeBuilder := cold_import.NewColdImportNodeBuilder(reader, parser)
|
|
|
|
coldNode, err := nodeBuilder.GetNode(genesisBlock, levelDbPath)
|
|
|
|
if err != nil {
|
2019-07-16 08:57:30 +00:00
|
|
|
log.WithField("subCommand", subCommand).Fatal("Error getting node: ", err)
|
2018-05-02 16:17:02 +00:00
|
|
|
}
|
|
|
|
pgDB := utils.LoadPostgres(databaseConfig, coldNode)
|
|
|
|
|
|
|
|
// init cold importer deps
|
2018-05-07 15:41:02 +00:00
|
|
|
blockRepository := repositories.NewBlockRepository(&pgDB)
|
2018-05-02 16:17:02 +00:00
|
|
|
receiptRepository := repositories.ReceiptRepository{DB: &pgDB}
|
2018-07-18 20:59:40 +00:00
|
|
|
transactionConverter := cold_db.NewColdDbTransactionConverter()
|
|
|
|
blockConverter := vulcCommon.NewBlockConverter(transactionConverter)
|
2018-05-02 16:17:02 +00:00
|
|
|
|
|
|
|
// init and execute cold importer
|
2018-05-07 15:41:02 +00:00
|
|
|
coldImporter := cold_import.NewColdImporter(ethDB, blockRepository, receiptRepository, blockConverter)
|
|
|
|
err = coldImporter.Execute(startingBlockNumber, endingBlockNumber, coldNode.ID)
|
2018-05-02 16:17:02 +00:00
|
|
|
if err != nil {
|
2019-07-16 08:57:30 +00:00
|
|
|
log.WithField("subCommand", subCommand).Fatal("Error executing cold import: ", err)
|
2018-05-02 16:17:02 +00:00
|
|
|
}
|
|
|
|
}
|