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-07-17 21:23:07 +00:00
|
|
|
|
2018-01-25 19:21:55 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2018-11-21 04:47:01 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-02-02 21:53:16 +00:00
|
|
|
"github.com/spf13/cobra"
|
2018-07-20 16:37:46 +00:00
|
|
|
|
2018-01-26 00:08:26 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
2018-01-26 00:08:26 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/history"
|
|
|
|
"github.com/vulcanize/vulcanizedb/utils"
|
2018-01-25 19:21:55 +00:00
|
|
|
)
|
|
|
|
|
2019-05-13 21:04:53 +00:00
|
|
|
// fullSyncCmd represents the fullSync command
|
|
|
|
var fullSyncCmd = &cobra.Command{
|
|
|
|
Use: "fullSync",
|
2018-07-17 21:23:07 +00:00
|
|
|
Short: "Syncs VulcanizeDB with local ethereum node",
|
|
|
|
Long: `Syncs VulcanizeDB with local ethereum node. Populates
|
|
|
|
Postgres with blocks, transactions, receipts, and logs.
|
|
|
|
|
2019-05-13 21:04:53 +00:00
|
|
|
./vulcanizedb fullSync --starting-block-number 0 --config public.toml
|
2018-01-25 19:21:55 +00:00
|
|
|
|
|
|
|
Expects ethereum node to be running and requires a .toml config:
|
|
|
|
|
|
|
|
[database]
|
|
|
|
name = "vulcanize_public"
|
|
|
|
hostname = "localhost"
|
|
|
|
port = 5432
|
|
|
|
|
|
|
|
[client]
|
2018-03-07 21:29:21 +00:00
|
|
|
ipcPath = "/Users/user/Library/Ethereum/geth.ipc"
|
2018-01-25 19:21:55 +00:00
|
|
|
`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-07-19 01:58:01 +00:00
|
|
|
SubCommand = cmd.CalledAs()
|
|
|
|
LogWithCommand = *log.WithField("SubCommand", SubCommand)
|
2019-05-13 21:04:53 +00:00
|
|
|
fullSync()
|
2018-01-25 19:21:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2019-05-13 21:04:53 +00:00
|
|
|
rootCmd.AddCommand(fullSyncCmd)
|
|
|
|
fullSyncCmd.Flags().Int64VarP(&startingBlockNumber, "starting-block-number", "s", 0, "Block number to start syncing from")
|
2018-01-25 19:21:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func backFillAllBlocks(blockchain core.BlockChain, blockRepository datastore.BlockRepository, missingBlocksPopulated chan int, startingBlockNumber int64) {
|
2019-02-14 15:03:57 +00:00
|
|
|
populated, err := history.PopulateMissingBlocks(blockchain, blockRepository, startingBlockNumber)
|
|
|
|
if err != nil {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Error("backfillAllBlocks: error in populateMissingBlocks: ", err)
|
2019-02-14 15:03:57 +00:00
|
|
|
}
|
|
|
|
missingBlocksPopulated <- populated
|
2018-01-25 19:21:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 21:04:53 +00:00
|
|
|
func fullSync() {
|
2018-01-25 19:21:55 +00:00
|
|
|
ticker := time.NewTicker(pollingInterval)
|
|
|
|
defer ticker.Stop()
|
2018-07-18 20:59:40 +00:00
|
|
|
|
2019-07-19 01:58:01 +00:00
|
|
|
blockChain := getBlockChain()
|
2019-02-14 15:03:57 +00:00
|
|
|
lastBlock, err := blockChain.LastBlock()
|
|
|
|
if err != nil {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Error("fullSync: Error getting last block: ", err)
|
2019-02-14 15:03:57 +00:00
|
|
|
}
|
|
|
|
if lastBlock.Int64() == 0 {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal("geth initial: state sync not finished")
|
2018-01-26 19:38:14 +00:00
|
|
|
}
|
2019-02-14 15:03:57 +00:00
|
|
|
if startingBlockNumber > lastBlock.Int64() {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal("fullSync: starting block number > current block number")
|
2018-03-27 21:16:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
db := utils.LoadPostgres(databaseConfig, blockChain.Node())
|
2018-05-07 15:41:02 +00:00
|
|
|
blockRepository := repositories.NewBlockRepository(&db)
|
2018-07-18 20:59:40 +00:00
|
|
|
validator := history.NewBlockValidator(blockChain, blockRepository, validationWindow)
|
2018-01-25 19:21:55 +00:00
|
|
|
missingBlocksPopulated := make(chan int)
|
2018-07-18 20:59:40 +00:00
|
|
|
go backFillAllBlocks(blockChain, blockRepository, missingBlocksPopulated, startingBlockNumber)
|
2018-01-25 19:21:55 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2019-07-19 01:58:01 +00:00
|
|
|
window, err := validator.ValidateBlocks()
|
2019-02-14 15:03:57 +00:00
|
|
|
if err != nil {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Error("fullSync: error in validateBlocks: ", err)
|
2019-02-14 15:03:57 +00:00
|
|
|
}
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Debug(window.GetString())
|
2018-01-25 19:21:55 +00:00
|
|
|
case <-missingBlocksPopulated:
|
2018-07-18 20:59:40 +00:00
|
|
|
go backFillAllBlocks(blockChain, blockRepository, missingBlocksPopulated, startingBlockNumber)
|
2018-01-25 19:21:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|