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-20 19:44:15 +00:00
|
|
|
package boot
|
|
|
|
|
|
|
|
import (
|
2022-04-26 17:57:01 +00:00
|
|
|
"context"
|
2022-05-13 12:48:31 +00:00
|
|
|
"fmt"
|
2022-05-19 13:46:38 +00:00
|
|
|
"strings"
|
2022-04-22 16:27:54 +00:00
|
|
|
"time"
|
2022-04-20 22:12:44 +00:00
|
|
|
|
2022-04-20 19:44:15 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-06-09 21:32:46 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/beaconclient"
|
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/database/sql"
|
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/database/sql/postgres"
|
2022-04-20 22:12:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-05-19 13:46:38 +00:00
|
|
|
DB sql.Database = &postgres.DB{}
|
|
|
|
BC *beaconclient.BeaconClient = &beaconclient.BeaconClient{}
|
2022-04-20 19:44:15 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 22:12:44 +00:00
|
|
|
// This function will perform some boot operations. If any steps fail, the application will fail to start.
|
|
|
|
// Keep in mind that the DB connection can be lost later in the lifecycle of the application or
|
|
|
|
// it might not be able to connect to the beacon client.
|
|
|
|
//
|
|
|
|
// 1. Make sure the Beacon client is up.
|
|
|
|
//
|
2022-04-20 19:44:15 +00:00
|
|
|
// 2. Connect to the database.
|
2022-04-20 22:12:44 +00:00
|
|
|
//
|
2022-05-13 12:48:31 +00:00
|
|
|
// 3. Make sure the node is synced, unless disregardSync is true.
|
2022-05-24 20:18:55 +00:00
|
|
|
func BootApplication(ctx context.Context, dbHostname string, dbPort int, dbName string, dbUsername string, dbPassword string, driverName string,
|
2022-09-23 20:23:22 +00:00
|
|
|
bcAddress string, bcPort int, bcConnectionProtocol string, bcKgTableIncrement int, disregardSync bool, uniqueNodeIdentifier int, checkDb bool, performBeaconBlockProcessing bool, performBeaconStateProcessing bool) (*beaconclient.BeaconClient, sql.Database, error) {
|
2022-04-22 16:27:54 +00:00
|
|
|
log.Info("Booting the Application")
|
|
|
|
|
2022-04-25 15:32:46 +00:00
|
|
|
log.Debug("Creating the Beacon Client")
|
2022-09-23 20:23:22 +00:00
|
|
|
Bc, err := beaconclient.CreateBeaconClient(ctx, bcConnectionProtocol, bcAddress, bcPort, bcKgTableIncrement, uniqueNodeIdentifier, checkDb, performBeaconBlockProcessing, performBeaconStateProcessing)
|
2022-06-03 16:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return Bc, nil, err
|
|
|
|
}
|
2022-04-25 15:32:46 +00:00
|
|
|
|
2022-04-26 17:57:01 +00:00
|
|
|
log.Debug("Checking Beacon Client")
|
2022-06-03 16:47:13 +00:00
|
|
|
err = Bc.CheckBeaconClient()
|
2022-04-20 22:12:44 +00:00
|
|
|
if err != nil {
|
2022-04-26 17:57:01 +00:00
|
|
|
return nil, nil, err
|
2022-04-20 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 19:44:15 +00:00
|
|
|
log.Debug("Setting up DB connection")
|
2022-05-13 12:48:31 +00:00
|
|
|
DB, err = postgres.SetupPostgresDb(dbHostname, dbPort, dbName, dbUsername, dbPassword, driverName)
|
2022-04-20 19:44:15 +00:00
|
|
|
if err != nil {
|
2022-04-26 17:57:01 +00:00
|
|
|
return nil, nil, err
|
2022-04-20 19:44:15 +00:00
|
|
|
}
|
2022-05-06 15:03:15 +00:00
|
|
|
|
2022-06-03 16:47:13 +00:00
|
|
|
Bc.Db = DB
|
2022-05-13 12:48:31 +00:00
|
|
|
|
|
|
|
var status bool
|
|
|
|
if !disregardSync {
|
2022-06-03 16:47:13 +00:00
|
|
|
status, err = Bc.CheckHeadSync()
|
2022-05-13 12:48:31 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to get the nodes sync status")
|
2022-06-03 16:47:13 +00:00
|
|
|
return Bc, DB, err
|
2022-05-13 12:48:31 +00:00
|
|
|
}
|
|
|
|
if status {
|
|
|
|
log.Error("The node is still syncing..")
|
|
|
|
err = fmt.Errorf("The node is still syncing.")
|
2022-06-03 16:47:13 +00:00
|
|
|
return Bc, DB, err
|
2022-05-13 12:48:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warn("We are not checking to see if the node has synced to head.")
|
|
|
|
}
|
2022-06-03 16:47:13 +00:00
|
|
|
return Bc, DB, nil
|
2022-04-20 19:44:15 +00:00
|
|
|
}
|
2022-04-22 16:27:54 +00:00
|
|
|
|
|
|
|
// Add retry logic to ensure that we are give the Beacon Client and the DB time to start.
|
2022-05-19 13:46:38 +00:00
|
|
|
func BootApplicationWithRetry(ctx context.Context, dbHostname string, dbPort int, dbName string, dbUsername string, dbPassword string, driverName string,
|
2022-06-03 16:47:13 +00:00
|
|
|
bcAddress string, bcPort int, bcConnectionProtocol string, bcType string, bcRetryInterval int, bcMaxRetry int, bcKgTableIncrement int,
|
2022-09-23 20:23:22 +00:00
|
|
|
startUpMode string, disregardSync bool, uniqueNodeIdentifier int, checkDb bool, performBeaconBlockProcessing bool, performBeaconStateProcessing bool) (*beaconclient.BeaconClient, sql.Database, error) {
|
2022-04-22 16:27:54 +00:00
|
|
|
var err error
|
2022-05-24 20:18:55 +00:00
|
|
|
|
|
|
|
if bcMaxRetry < 0 {
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
BC, DB, err = BootApplication(ctx, dbHostname, dbPort, dbName, dbUsername, dbPassword, driverName,
|
2022-09-23 20:23:22 +00:00
|
|
|
bcAddress, bcPort, bcConnectionProtocol, bcKgTableIncrement, disregardSync, uniqueNodeIdentifier, checkDb,
|
|
|
|
performBeaconBlockProcessing, performBeaconStateProcessing)
|
2022-05-24 20:18:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"retryNumber": i,
|
|
|
|
"err": err,
|
|
|
|
}).Warn("Unable to boot application. Going to try again")
|
|
|
|
time.Sleep(time.Duration(bcRetryInterval) * time.Second)
|
|
|
|
i = i + 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := 0; i < bcMaxRetry; i++ {
|
|
|
|
BC, DB, err = BootApplication(ctx, dbHostname, dbPort, dbName, dbUsername, dbPassword, driverName,
|
2022-09-23 20:23:22 +00:00
|
|
|
bcAddress, bcPort, bcConnectionProtocol, bcKgTableIncrement, disregardSync, uniqueNodeIdentifier, checkDb,
|
|
|
|
performBeaconBlockProcessing, performBeaconStateProcessing)
|
2022-05-24 20:18:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"retryNumber": i,
|
|
|
|
"err": err,
|
|
|
|
}).Warn("Unable to boot application. Going to try again")
|
|
|
|
time.Sleep(time.Duration(bcRetryInterval) * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
2022-04-22 16:27:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-19 13:46:38 +00:00
|
|
|
|
|
|
|
switch strings.ToLower(startUpMode) {
|
|
|
|
case "head":
|
2022-05-24 20:18:55 +00:00
|
|
|
BC.PerformHeadTracking = true
|
2022-05-19 13:46:38 +00:00
|
|
|
case "historic":
|
|
|
|
log.Debug("Performing additional boot steps for historical processing")
|
2022-05-24 20:18:55 +00:00
|
|
|
BC.PerformHistoricalProcessing = true
|
|
|
|
// This field is not currently used.
|
|
|
|
// The idea is, that if we are doing historially processing and we get a slot
|
|
|
|
// greater than this slot, then we would rerun this function.
|
|
|
|
// this would ensure that we have the slots necessary for processing
|
|
|
|
// within the beacon server.
|
|
|
|
|
|
|
|
// We can implement this feature if we notice any errors.
|
2022-05-19 13:46:38 +00:00
|
|
|
headSlot, err := BC.GetLatestSlotInBeaconServer(bcType)
|
|
|
|
if err != nil {
|
|
|
|
return BC, DB, err
|
|
|
|
}
|
|
|
|
BC.UpdateLatestSlotInBeaconServer(int64(headSlot))
|
|
|
|
// Add another switch case for bcType if its ever needed.
|
2022-06-03 16:47:13 +00:00
|
|
|
case "boot":
|
|
|
|
log.Debug("Running application in boot mode.")
|
2022-05-24 20:18:55 +00:00
|
|
|
default:
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"startUpMode": startUpMode,
|
|
|
|
}).Error("The startUpMode provided is not handled.")
|
2022-05-19 13:46:38 +00:00
|
|
|
}
|
2022-05-24 20:18:55 +00:00
|
|
|
|
2022-04-26 17:57:01 +00:00
|
|
|
return BC, DB, err
|
2022-04-22 16:27:54 +00:00
|
|
|
}
|