Skip to content

Commit

Permalink
chore: add docstring to syncChainFpStatusLoop and stop the loop if th…
Browse files Browse the repository at this point in the history
…ere is any fp instance running already
  • Loading branch information
RafilxTenfen committed Sep 18, 2024
1 parent d3216f1 commit 5021829
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions finality-provider/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (app *FinalityProviderApp) getFpPrivKey(fpPk []byte) (*btcec.PrivateKey, er
}

// SyncFinalityProviderStatus syncs the status of the finality-providers with the chain.
func (app *FinalityProviderApp) SyncFinalityProviderStatus() (fpInstanceStarted bool, err error) {
func (app *FinalityProviderApp) SyncFinalityProviderStatus() (fpInstanceRunning bool, err error) {
latestBlock, err := app.cc.QueryBestBlock()
if err != nil {
return false, err
Expand All @@ -255,11 +255,12 @@ func (app *FinalityProviderApp) SyncFinalityProviderStatus() (fpInstanceStarted
for _, fp := range fps {
vp, err := app.cc.QueryFinalityProviderVotingPower(fp.BtcPk, latestBlock.Height)
if err != nil {
// if error occured then the finality-provider is not registered in the Babylon chain yet or
// there is nothing in the voting power table, so it should not start the fp.
// if ther error is that there is nothing in the voting power table
// it should continue and consider the voting power
// as zero to start the finality provider and send public randomness
allowedErr := fmt.Sprintf("failed to query Finality Voting Power at Height %d: rpc error: code = Unknown desc = %s: unknown request", latestBlock.Height, bstypes.ErrVotingPowerTableNotUpdated.Wrapf("height: %d", latestBlock.Height).Error())
if !strings.EqualFold(err.Error(), allowedErr) {
// if nothing was updated in the voting power table, it should consider as zero VP to start to send pub random
// if some other error occured then the finality-provider is not registered in the Babylon chain yet
continue
}
}
Expand All @@ -270,7 +271,9 @@ func (app *FinalityProviderApp) SyncFinalityProviderStatus() (fpInstanceStarted

bip340PubKey := fp.GetBIP340BTCPK()
if app.fpManager.IsFinalityProviderRunning(bip340PubKey) {
// if it is running, no need to update status
// there is a instance running, no need to keep syncing
fpInstanceRunning = true
// if it is already running, no need to update status
continue
}

Expand All @@ -295,10 +298,10 @@ func (app *FinalityProviderApp) SyncFinalityProviderStatus() (fpInstanceStarted
if err := app.fpManager.StartFinalityProvider(bip340PubKey, ""); err != nil {
return false, err
}
fpInstanceStarted = true
fpInstanceRunning = true
}

return fpInstanceStarted, nil
return fpInstanceRunning, nil
}

// Start starts only the finality-provider daemon without any finality-provider instances
Expand Down Expand Up @@ -679,6 +682,12 @@ func (app *FinalityProviderApp) metricsUpdateLoop() {
}
}

// syncChainFpStatusLoop keeps querying the chain for the finality
// provider voting power and update the FP status accordingly.
// If there is some voting power it sets to active, for zero voting power
// it goes from: CREATED -> REGISTERED or ACTIVE -> INACTIVE.
// if there is any node running or a new finality provider instance
// is started, the loop stops.
func (app *FinalityProviderApp) syncChainFpStatusLoop() {
defer app.wg.Done()

Expand Down

0 comments on commit 5021829

Please sign in to comment.