Skip to content

Commit

Permalink
refactor_sync_checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Marketen committed Jul 11, 2023
1 parent 92a52c4 commit 3a00005
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
21 changes: 18 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
30 changes: 1 addition & 29 deletions oracle/onchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3a00005

Please sign in to comment.