2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2018-07-17 21:23:07 +00:00
|
|
|
// Copyright © 2018 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 (
|
2018-07-20 16:37:46 +00:00
|
|
|
"log"
|
2018-01-25 19:21:55 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
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()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2018-07-17 21:23:07 +00:00
|
|
|
pollingInterval = 7 * time.Second
|
|
|
|
validationWindow = 15
|
2018-01-25 19:21:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2018-06-21 15:05:49 +00:00
|
|
|
missingBlocksPopulated <- history.PopulateMissingBlocks(blockchain, blockRepository, startingBlockNumber)
|
2018-01-25 19:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sync() {
|
|
|
|
ticker := time.NewTicker(pollingInterval)
|
|
|
|
defer ticker.Stop()
|
2018-11-15 18:53:08 +00:00
|
|
|
|
|
|
|
blockChain := getBlockChain()
|
2018-07-18 20:59:40 +00:00
|
|
|
|
|
|
|
lastBlock := blockChain.LastBlock().Int64()
|
2018-03-27 21:16:27 +00:00
|
|
|
if lastBlock == 0 {
|
2018-01-26 19:38:14 +00:00
|
|
|
log.Fatal("geth initial: state sync not finished")
|
|
|
|
}
|
2018-05-02 16:17:02 +00:00
|
|
|
if startingBlockNumber > lastBlock {
|
2018-03-27 21:16:27 +00:00
|
|
|
log.Fatal("starting block number > current block number")
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
window := validator.ValidateBlocks()
|
2018-07-17 21:23:07 +00:00
|
|
|
window.Log(os.Stdout)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|