2018-07-17 21:23:07 +00:00
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-07-20 16:37:46 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2018-07-17 21:23:07 +00:00
|
|
|
"github.com/spf13/cobra"
|
2018-07-20 16:37:46 +00:00
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
2018-07-18 20:59:40 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
|
2018-07-20 16:37:46 +00:00
|
|
|
vRpc "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
|
2018-07-18 20:59:40 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
|
2018-07-17 21:23:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/history"
|
2018-07-24 16:38:49 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers"
|
2018-07-17 21:23:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// lightSyncCmd represents the lightSync command
|
|
|
|
var lightSyncCmd = &cobra.Command{
|
|
|
|
Use: "lightSync",
|
|
|
|
Short: "Syncs VulcanizeDB with local ethereum node's block headers",
|
|
|
|
Long: `Syncs VulcanizeDB with local ethereum node. Populates
|
|
|
|
Postgres with block headers.
|
|
|
|
|
|
|
|
./vulcanizedb lightSync --starting-block-number 0 --config public.toml
|
|
|
|
|
|
|
|
Expects ethereum node to be running and requires a .toml config:
|
|
|
|
|
|
|
|
[database]
|
|
|
|
name = "vulcanize_public"
|
|
|
|
hostname = "localhost"
|
|
|
|
port = 5432
|
|
|
|
|
|
|
|
[client]
|
|
|
|
ipcPath = "/Users/user/Library/Ethereum/geth.ipc"
|
|
|
|
`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
lightSync()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(lightSyncCmd)
|
|
|
|
lightSyncCmd.Flags().Int64VarP(&startingBlockNumber, "starting-block-number", "s", 0, "Block number to start syncing from")
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func backFillAllHeaders(blockchain core.BlockChain, headerRepository datastore.HeaderRepository, missingBlocksPopulated chan int, startingBlockNumber int64) {
|
2018-07-24 16:38:49 +00:00
|
|
|
emptyTransformers := []transformers.Transformer{}
|
|
|
|
populated, err := history.PopulateMissingHeaders(blockchain, headerRepository, startingBlockNumber, emptyTransformers)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error populating headers: ", err)
|
|
|
|
}
|
|
|
|
missingBlocksPopulated <- populated
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func lightSync() {
|
|
|
|
ticker := time.NewTicker(pollingInterval)
|
|
|
|
defer ticker.Stop()
|
2018-07-26 22:01:24 +00:00
|
|
|
blockChain := getBlockChain()
|
|
|
|
validateArgs(blockChain)
|
2018-07-17 21:23:07 +00:00
|
|
|
db := utils.LoadPostgres(databaseConfig, blockChain.Node())
|
2018-07-26 22:01:24 +00:00
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
headerRepository := repositories.NewHeaderRepository(&db)
|
2018-07-26 21:15:24 +00:00
|
|
|
validator := history.NewHeaderValidator(blockChain, headerRepository, validationWindow, []transformers.Transformer{})
|
2018-07-17 21:23:07 +00:00
|
|
|
missingBlocksPopulated := make(chan int)
|
|
|
|
go backFillAllHeaders(blockChain, headerRepository, missingBlocksPopulated, startingBlockNumber)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
window := validator.ValidateHeaders()
|
|
|
|
window.Log(os.Stdout)
|
|
|
|
case <-missingBlocksPopulated:
|
|
|
|
go backFillAllHeaders(blockChain, headerRepository, missingBlocksPopulated, startingBlockNumber)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-26 22:01:24 +00:00
|
|
|
|
|
|
|
func validateArgs(blockChain *geth.BlockChain) {
|
|
|
|
lastBlock := blockChain.LastBlock().Int64()
|
|
|
|
if lastBlock == 0 {
|
|
|
|
log.Fatal("geth initial: state sync not finished")
|
|
|
|
}
|
|
|
|
if startingBlockNumber > lastBlock {
|
|
|
|
log.Fatal("starting block number > current block number")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBlockChain() *geth.BlockChain {
|
|
|
|
rawRpcClient, err := rpc.Dial(ipc)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
rpcClient := client.NewRpcClient(rawRpcClient, ipc)
|
|
|
|
ethClient := ethclient.NewClient(rawRpcClient)
|
|
|
|
client := client.NewEthClient(ethClient)
|
|
|
|
node := node.MakeNode(rpcClient)
|
|
|
|
transactionConverter := vRpc.NewRpcTransactionConverter(client)
|
|
|
|
blockChain := geth.NewBlockChain(client, node, transactionConverter)
|
|
|
|
return blockChain
|
|
|
|
}
|