Skip to content

Commit

Permalink
add getCurrentBlock func to return latest block confirmed (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen authored Jul 26, 2022
1 parent 67c3e5f commit 38edbd0
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions orchestrator/oracle_resync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 38edbd0

Please sign in to comment.