From 58e619b55d0701a0cbaeb461676cce683d6e99d0 Mon Sep 17 00:00:00 2001 From: RafilxTenfen Date: Fri, 17 Jun 2022 23:04:50 -0300 Subject: [PATCH] add getCurrentBlock func to return latest block confirmed --- orchestrator/oracle_resync.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/orchestrator/oracle_resync.go b/orchestrator/oracle_resync.go index df30a773..a09a1bc4 100644 --- a/orchestrator/oracle_resync.go +++ b/orchestrator/oracle_resync.go @@ -32,15 +32,13 @@ func (p *gravityOrchestrator) GetLastCheckedBlock( lastEventNonce = 1 } - latestHeader, err := p.ethProvider.HeaderByNumber(ctx, nil) + // add delay to ensure minimum confirmations are received and block is finalized + currentBlock, err := p.getCurrentBlock(ctx, ethBlockConfirmationDelay) if err != nil { err = errors.Wrap(err, "failed to get latest header") return 0, err } - // add delay to ensure minimum confirmations are received and block is finalized - currentBlock := latestHeader.Number.Uint64() - ethBlockConfirmationDelay - for currentBlock > 0 { endSearch := uint64(0) if currentBlock < p.ethBlocksPerLoop { @@ -189,3 +187,28 @@ func (p *gravityOrchestrator) GetLastCheckedBlock( return 0, errors.New("reached the end of block history without finding the Gravity contract deploy event") } + +// getCurrentBlock returns the latest block in the eth +// if the latest block in eth is less than the confirmation +// it returns 0, if is bigger than confirmation it removes +// the amount of confirmations +func (p *gravityOrchestrator) getCurrentBlock( + ctx context.Context, + ethBlockConfirmationDelay uint64, +) (uint64, error) { + latestHeader, err := p.ethProvider.HeaderByNumber(ctx, nil) + if err != nil { + err = errors.Wrap(err, "failed to get latest header") + return 0, err + } + + latestBlock := latestHeader.Number.Uint64() + + // checks if the latest block is less than the amount of confirmation + if latestBlock < ethBlockConfirmationDelay { + return 0, nil + } + + // add delay to ensure minimum confirmations are received and block is finalized + return latestBlock - ethBlockConfirmationDelay, nil +}