Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getCurrentBlock func to return latest block confirmed #326

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}