From 3a000055349b03472c1ec114dae8ef8783b24680 Mon Sep 17 00:00:00 2001 From: Marketen Date: Tue, 11 Jul 2023 19:13:22 +0200 Subject: [PATCH] refactor_sync_checks --- main.go | 21 ++++++++++++++++++--- oracle/onchain.go | 30 +----------------------------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/main.go b/main.go index bfb31d2..27ed557 100644 --- a/main.go +++ b/main.go @@ -97,6 +97,24 @@ func main() { log.Fatal("Could not create new onchain object: ", err) } + var cfg *oracle.Config + + // Populate config, most of the parameters are directly loaded from the smart contract or chain data. Need to make sure that + // the contract is deployed and the nodes are in sync before loading the config + for { + inSync, err := onchain.AreNodesInSync() + if err != nil { + log.Fatal("Could not check if nodes are in sync: ", err) + } else if inSync { + log.Info("Nodes are in sync, loading parameters from remote chain...") + cfg = onchain.GetConfigFromContract(cliCfg) + break // Exit the loop if nodes are in sync + } + + log.Println("Nodes are not in sync. Oracle could not load parameters from remote chain. Waiting 1 minute to check again") + time.Sleep(1 * time.Minute) + } + if !cliCfg.DryRun { log.Info("Checking if configured address ", updaterAddress.String(), " is whitelisted to update the contract") isWhitelisted, err := onchain.IsAddressWhitelisted(updaterAddress) @@ -122,9 +140,6 @@ func main() { } } - // Populate config, most of the parameters are loaded from the smart contract - cfg := onchain.GetConfigFromContract(cliCfg) - // Create the oracle instance oracleInstance := oracle.NewOracle(cfg) diff --git a/oracle/onchain.go b/oracle/onchain.go index 40c7383..85eceb0 100644 --- a/oracle/onchain.go +++ b/oracle/onchain.go @@ -97,34 +97,6 @@ func NewOnchain(cliCfg *config.CliConfig, updaterKey *ecdsa.PrivateKey) (*Onchai depositContract.ChainID, chainId)) } - // Print sync status of consensus and execution client - execSync, err := executionClient.SyncProgress(context.Background()) - if err != nil { - return nil, errors.Wrap(err, "Error fetching execution client sync progress") - } - - // nil means synced - if execSync == nil { - header, err := executionClient.HeaderByNumber(context.Background(), nil) - if err != nil { - return nil, errors.Wrap(err, "Error fetching execution client header") - } - log.Info("Execution client is in sync, block number: ", header.Number) - } else { - log.Info("Execution client is NOT in sync, current block: ", execSync.CurrentBlock) - } - - consSync, err := consensusClient.NodeSyncing(context.Background()) - if err != nil { - return nil, errors.Wrap(err, "Error fetching consensus client sync progress") - } - - if consSync.SyncDistance == 0 { - log.Info("Consensus client is in sync, head slot: ", consSync.HeadSlot) - } else { - log.Info("Consensus client is NOT in sync, slots behind: ", consSync.SyncDistance) - } - // Instantiate the smoothing pool contract to run get/set operations on it address := common.HexToAddress(cliCfg.PoolAddress) contract, err := contract.NewContract(address, executionClient) @@ -180,7 +152,7 @@ func (o *Onchain) AreNodesInSync(opts ...retry.Option) (bool, error) { } // If no errors arised while fetching the sync progress of both clients, check if the clients are in sync - // If the execution client is not nil, it means it is not synced + // If the execution client is not nil, it means it is still syncing, so it is not "in sync" if execSync != nil { log.Info("Exec client not in sync") return false, nil