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
|
|
|
)
|
|
|
|
|
|
|
|
// syncCmd represents the sync command
|
|
|
|
var syncCmd = &cobra.Command{
|
|
|
|
Use: "sync",
|
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.
|
|
|
|
|
|
|
|
./vulcanizedb sync --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) {
|
|
|
|
sync()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(syncCmd)
|
|
|
|
|
2018-05-02 16:17:02 +00:00
|
|
|
syncCmd.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 {
|
|
|
|
log.Error("backfillAllBlocks: error in populateMissingBlocks: ", err)
|
|
|
|
}
|
|
|
|
missingBlocksPopulated <- populated
|
2018-01-25 19:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sync() {
|
|
|
|
ticker := time.NewTicker(pollingInterval)
|
|
|
|
defer ticker.Stop()
|
2018-07-18 20:59:40 +00:00
|
|
|
|
2018-09-19 15:14:49 +00:00
|
|
|
blockChain := getBlockChain()
|
2019-02-14 15:03:57 +00:00
|
|
|
lastBlock, err := blockChain.LastBlock()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("sync: Error getting last block: ", err)
|
|
|
|
}
|
|
|
|
if lastBlock.Int64() == 0 {
|
2018-01-26 19:38:14 +00:00
|
|
|
log.Fatal("geth initial: state sync not finished")
|
|
|
|
}
|
2019-02-14 15:03:57 +00:00
|
|
|
if startingBlockNumber > lastBlock.Int64() {
|
|
|
|
log.Fatal("sync: 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-02-14 15:03:57 +00:00
|
|
|
window, err := validator.ValidateBlocks()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("sync: error in validateBlocks: ", err)
|
|
|
|
}
|
2019-01-31 14:55:28 +00:00
|
|
|
log.Info(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|