2018-01-25 19:21:55 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
|
2018-02-02 21:53:16 +00:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2018-01-26 00:08:26 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/history"
|
2018-02-02 21:53:16 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/repositories/postgres"
|
2018-01-26 00:08:26 +00:00
|
|
|
"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-01-25 21:46:55 +00:00
|
|
|
Short: "Syncs vulcanizedb with local ethereum node",
|
|
|
|
Long: `Syncs vulcanizedb with local ethereum node.
|
|
|
|
vulcanizedb sync --startingBlockNumber 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]
|
|
|
|
ipcPath = "/Users/mattkrump/Library/Ethereum/geth.ipc"
|
|
|
|
`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
sync()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
pollingInterval = 7 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
var startingBlockNumber int
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(syncCmd)
|
|
|
|
|
|
|
|
syncCmd.Flags().IntVarP(&startingBlockNumber, "starting-block-number", "s", 0, "Block number to start syncing from")
|
|
|
|
}
|
|
|
|
|
2018-02-02 21:53:16 +00:00
|
|
|
func backFillAllBlocks(blockchain core.Blockchain, repository postgres.DB, missingBlocksPopulated chan int, startingBlockNumber int64) {
|
2018-01-25 19:21:55 +00:00
|
|
|
go func() {
|
|
|
|
missingBlocksPopulated <- history.PopulateMissingBlocks(blockchain, repository, startingBlockNumber)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func sync() {
|
|
|
|
ticker := time.NewTicker(pollingInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
blockchain := geth.NewBlockchain(ipc)
|
2018-01-26 19:38:14 +00:00
|
|
|
if blockchain.LastBlock().Int64() == 0 {
|
|
|
|
log.Fatal("geth initial: state sync not finished")
|
|
|
|
}
|
2018-01-25 19:21:55 +00:00
|
|
|
repository := utils.LoadPostgres(databaseConfig, blockchain.Node())
|
|
|
|
validator := history.NewBlockValidator(blockchain, repository, 15)
|
|
|
|
|
|
|
|
missingBlocksPopulated := make(chan int)
|
|
|
|
_startingBlockNumber := int64(startingBlockNumber)
|
|
|
|
go backFillAllBlocks(blockchain, repository, missingBlocksPopulated, _startingBlockNumber)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
window := validator.ValidateBlocks()
|
|
|
|
validator.Log(os.Stdout, window)
|
|
|
|
case <-missingBlocksPopulated:
|
|
|
|
go backFillAllBlocks(blockchain, repository, missingBlocksPopulated, _startingBlockNumber)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|