2022-05-18 16:12:54 +00:00
// VulcanizeDB
// Copyright © 2022 Vulcanize
// 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/>.
2022-04-19 21:09:59 +00:00
package cmd
import (
2022-05-24 20:18:55 +00:00
"fmt"
2022-04-22 16:27:54 +00:00
"os"
2022-04-26 17:57:01 +00:00
"time"
2022-04-22 16:27:54 +00:00
2022-04-19 21:09:59 +00:00
"github.com/spf13/cobra"
2022-04-22 16:27:54 +00:00
"github.com/spf13/viper"
)
var (
2022-05-24 20:18:55 +00:00
dbUsername string
dbPassword string
dbName string
dbAddress string
dbDriver string
dbPort int
bcAddress string
bcPort int
bcBootRetryInterval int
bcBootMaxRetry int
bcConnectionProtocol string
bcType string
bcMaxHistoricProcessWorker int
2022-06-03 16:47:13 +00:00
bcUniqueNodeIdentifier int
2022-06-06 13:02:43 +00:00
bcCheckDb bool
2022-05-24 20:18:55 +00:00
kgMaxWorker int
kgTableIncrement int
kgProcessGaps bool
2022-05-25 14:19:29 +00:00
pmMetrics bool
pmAddress string
pmPort int
2022-05-24 20:18:55 +00:00
maxWaitSecondsShutdown time . Duration = time . Duration ( 20 ) * time . Second
notifierCh chan os . Signal = make ( chan os . Signal , 1 )
testDisregardSync bool
2022-04-19 21:09:59 +00:00
)
// captureCmd represents the capture command
var captureCmd = & cobra . Command {
Use : "capture" ,
2022-04-20 13:25:47 +00:00
Short : "Capture the SignedBeaconBlocks and BeaconStates from the Beacon Chain" ,
Long : ` Capture SignedBeaconBlocks and BeaconStates from the Beacon Chain .
These blocks and states will be captured in
2022-04-20 22:12:44 +00:00
Postgres . They require a beacon client to be connected . You can run this to
2022-04-20 13:25:47 +00:00
capture blocks and states at head or historic blocks . ` ,
2022-04-19 21:09:59 +00:00
}
func init ( ) {
rootCmd . AddCommand ( captureCmd )
2022-04-22 16:27:54 +00:00
// Required Flags
//// DB Specific
captureCmd . PersistentFlags ( ) . StringVarP ( & dbUsername , "db.username" , "" , "" , "Database username (required)" )
captureCmd . PersistentFlags ( ) . StringVarP ( & dbPassword , "db.password" , "" , "" , "Database Password (required)" )
captureCmd . PersistentFlags ( ) . StringVarP ( & dbAddress , "db.address" , "" , "" , "Port to connect to DB(required)" )
captureCmd . PersistentFlags ( ) . StringVarP ( & dbName , "db.name" , "n" , "" , "Database name connect to DB(required)" )
captureCmd . PersistentFlags ( ) . StringVarP ( & dbDriver , "db.driver" , "" , "" , "Database Driver to connect to DB(required)" )
captureCmd . PersistentFlags ( ) . IntVarP ( & dbPort , "db.port" , "" , 0 , "Port to connect to DB(required)" )
2022-05-24 20:18:55 +00:00
//err := captureCmd.MarkPersistentFlagRequired("db.username")
// exitErr(err)
// err = captureCmd.MarkPersistentFlagRequired("db.password")
// exitErr(err)
// err = captureCmd.MarkPersistentFlagRequired("db.address")
// exitErr(err)
// err = captureCmd.MarkPersistentFlagRequired("db.port")
// exitErr(err)
// err = captureCmd.MarkPersistentFlagRequired("db.name")
// exitErr(err)
// err = captureCmd.MarkPersistentFlagRequired("db.driver")
// exitErr(err)
2022-04-22 16:27:54 +00:00
//// Beacon Client Specific
2022-05-13 12:48:31 +00:00
captureCmd . PersistentFlags ( ) . StringVarP ( & bcAddress , "bc.address" , "l" , "" , "Address to connect to beacon node (required)" )
captureCmd . PersistentFlags ( ) . StringVarP ( & bcType , "bc.type" , "" , "lighthouse" , "The beacon client we are using, options are prysm and lighthouse." )
captureCmd . PersistentFlags ( ) . IntVarP ( & bcPort , "bc.port" , "r" , 0 , "Port to connect to beacon node (required )" )
2022-04-27 18:01:59 +00:00
captureCmd . PersistentFlags ( ) . StringVarP ( & bcConnectionProtocol , "bc.connectionProtocol" , "" , "http" , "protocol for connecting to the beacon node." )
2022-05-19 13:46:38 +00:00
captureCmd . PersistentFlags ( ) . IntVarP ( & bcBootRetryInterval , "bc.bootRetryInterval" , "" , 30 , "The amount of time to wait between retries while booting the application" )
captureCmd . PersistentFlags ( ) . IntVarP ( & bcBootMaxRetry , "bc.bootMaxRetry" , "" , 5 , "The amount of time to wait between retries while booting the application" )
2022-06-09 21:32:46 +00:00
captureCmd . PersistentFlags ( ) . IntVarP ( & bcMaxHistoricProcessWorker , "bc.maxHistoricProcessWorker" , "" , 30 , "The number of workers that should be actively processing slots from the eth-beacon.historic_process table. Be careful of system memory." )
2022-06-03 16:47:13 +00:00
captureCmd . PersistentFlags ( ) . IntVarP ( & bcUniqueNodeIdentifier , "bc.uniqueNodeIdentifier" , "" , 0 , "The unique identifier of this application. Each application connecting to the DB should have a unique identifier." )
2022-06-06 13:02:43 +00:00
captureCmd . PersistentFlags ( ) . BoolVarP ( & bcCheckDb , "bc.checkDb" , "" , true , "Should we check to see if the slot exists in the DB before writing it?" )
2022-05-24 20:18:55 +00:00
// err = captureCmd.MarkPersistentFlagRequired("bc.address")
// exitErr(err)
// err = captureCmd.MarkPersistentFlagRequired("bc.port")
// exitErr(err)
//// Known Gaps specific
2022-06-09 21:32:46 +00:00
captureCmd . PersistentFlags ( ) . BoolVarP ( & kgProcessGaps , "kg.processKnownGaps" , "" , true , "Should we process the slots within the eth-beacon.known_gaps table." )
2022-05-24 20:18:55 +00:00
captureCmd . PersistentFlags ( ) . IntVarP ( & kgTableIncrement , "kg.increment" , "" , 10000 , "The max slots within a single entry to the known_gaps table." )
2022-06-09 21:32:46 +00:00
captureCmd . PersistentFlags ( ) . IntVarP ( & kgMaxWorker , "kg.maxKnownGapsWorker" , "" , 30 , "The number of workers that should be actively processing slots from the eth-beacon.known_gaps table. Be careful of system memory." )
2022-04-22 16:27:54 +00:00
2022-05-25 14:19:29 +00:00
// Prometheus Specific
captureCmd . PersistentFlags ( ) . BoolVarP ( & pmMetrics , "pm.metrics" , "" , true , "Should we capture prometheus metrics." )
captureCmd . PersistentFlags ( ) . StringVarP ( & pmAddress , "pm.address" , "" , "localhost" , "Address to send the prometheus metrics." )
captureCmd . PersistentFlags ( ) . IntVarP ( & pmPort , "pm.port" , "" , 9000 , "The port to send prometheus metrics." )
2022-05-13 12:48:31 +00:00
//// Testing Specific
captureCmd . PersistentFlags ( ) . BoolVar ( & testDisregardSync , "t.skipSync" , false , "Should we disregard the head sync?" )
2022-04-22 16:27:54 +00:00
// Bind Flags with Viper
//// DB Flags
2022-05-24 20:18:55 +00:00
err := viper . BindPFlag ( "db.username" , captureCmd . PersistentFlags ( ) . Lookup ( "db.username" ) )
2022-04-22 16:27:54 +00:00
exitErr ( err )
err = viper . BindPFlag ( "db.password" , captureCmd . PersistentFlags ( ) . Lookup ( "db.password" ) )
exitErr ( err )
err = viper . BindPFlag ( "db.address" , captureCmd . PersistentFlags ( ) . Lookup ( "db.address" ) )
exitErr ( err )
err = viper . BindPFlag ( "db.port" , captureCmd . PersistentFlags ( ) . Lookup ( "db.port" ) )
exitErr ( err )
err = viper . BindPFlag ( "db.name" , captureCmd . PersistentFlags ( ) . Lookup ( "db.name" ) )
exitErr ( err )
2022-06-03 16:47:13 +00:00
err = viper . BindPFlag ( "db.driver" , captureCmd . PersistentFlags ( ) . Lookup ( "db.driver" ) )
exitErr ( err )
2022-05-13 12:48:31 +00:00
2022-05-24 20:18:55 +00:00
//// Testing Specific
err = viper . BindPFlag ( "t.skipSync" , captureCmd . PersistentFlags ( ) . Lookup ( "t.skipSync" ) )
2022-04-22 16:27:54 +00:00
exitErr ( err )
2022-05-24 20:18:55 +00:00
//// LH specific
2022-04-22 16:27:54 +00:00
err = viper . BindPFlag ( "bc.address" , captureCmd . PersistentFlags ( ) . Lookup ( "bc.address" ) )
exitErr ( err )
2022-05-13 12:48:31 +00:00
err = viper . BindPFlag ( "bc.type" , captureCmd . PersistentFlags ( ) . Lookup ( "bc.type" ) )
exitErr ( err )
2022-04-22 16:27:54 +00:00
err = viper . BindPFlag ( "bc.port" , captureCmd . PersistentFlags ( ) . Lookup ( "bc.port" ) )
exitErr ( err )
2022-04-27 18:01:59 +00:00
err = viper . BindPFlag ( "bc.connectionProtocol" , captureCmd . PersistentFlags ( ) . Lookup ( "bc.connectionProtocol" ) )
exitErr ( err )
2022-05-19 13:46:38 +00:00
err = viper . BindPFlag ( "bc.bootRetryInterval" , captureCmd . PersistentFlags ( ) . Lookup ( "bc.bootRetryInterval" ) )
exitErr ( err )
err = viper . BindPFlag ( "bc.bootMaxRetry" , captureCmd . PersistentFlags ( ) . Lookup ( "bc.bootMaxRetry" ) )
exitErr ( err )
2022-05-24 20:18:55 +00:00
err = viper . BindPFlag ( "bc.maxHistoricProcessWorker" , captureCmd . PersistentFlags ( ) . Lookup ( "bc.maxHistoricProcessWorker" ) )
exitErr ( err )
2022-06-03 16:47:13 +00:00
err = viper . BindPFlag ( "bc.uniqueNodeIdentifier" , captureCmd . PersistentFlags ( ) . Lookup ( "bc.uniqueNodeIdentifier" ) )
exitErr ( err )
2022-06-06 13:02:43 +00:00
err = viper . BindPFlag ( "bc.checkDb" , captureCmd . PersistentFlags ( ) . Lookup ( "bc.checkDb" ) )
exitErr ( err )
2022-04-19 21:09:59 +00:00
// Here you will define your flags and configuration settings.
2022-05-24 20:18:55 +00:00
//// Known Gap Specific
err = viper . BindPFlag ( "kg.processKnownGaps" , captureCmd . PersistentFlags ( ) . Lookup ( "kg.processKnownGaps" ) )
exitErr ( err )
err = viper . BindPFlag ( "kg.increment" , captureCmd . PersistentFlags ( ) . Lookup ( "kg.increment" ) )
exitErr ( err )
err = viper . BindPFlag ( "kg.processKnownGaps" , captureCmd . PersistentFlags ( ) . Lookup ( "kg.maxKnownGapsWorker" ) )
exitErr ( err )
2022-05-25 14:19:29 +00:00
// Prometheus Specific
err = viper . BindPFlag ( "pm.metrics" , captureCmd . PersistentFlags ( ) . Lookup ( "pm.metrics" ) )
exitErr ( err )
err = viper . BindPFlag ( "pm.address" , captureCmd . PersistentFlags ( ) . Lookup ( "pm.address" ) )
exitErr ( err )
err = viper . BindPFlag ( "pm.port" , captureCmd . PersistentFlags ( ) . Lookup ( "pm.port" ) )
exitErr ( err )
2022-04-22 16:27:54 +00:00
}
2022-04-19 21:09:59 +00:00
2022-04-22 16:27:54 +00:00
// Helper function to catch any errors.
// We need to capture these errors for the linter.
func exitErr ( err error ) {
if err != nil {
2022-05-24 20:18:55 +00:00
fmt . Println ( "Error: " , err )
2022-04-22 16:27:54 +00:00
os . Exit ( 1 )
}
2022-04-19 21:09:59 +00:00
}