Skip to content

Commit

Permalink
Merge pull request #56 from keep-network/chain-confirmation-waiter
Browse files Browse the repository at this point in the history
Add chain confirmation waiter

Added the WaitForChainConfirmation function which provides the 
ability to check the chain state once the chain reaches the given block height.
  • Loading branch information
pdyraga authored Nov 12, 2020
2 parents a6cb29e + 70d5d64 commit 3f6d921
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pkg/chain/ethereum/ethutil/confirmations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ethutil

import "fmt"

// BlockHeightWaiter provides the ability to wait for a given block height.
type BlockHeightWaiter interface {
WaitForBlockHeight(blockNumber uint64) error
}

// WaitForChainConfirmation ensures that after receiving specific number of block
// confirmations the state of the chain is actually as expected. It waits for
// predefined number of blocks since the start block number provided. After the
// required block number is reached it performs a check of the chain state with
// a provided function returning a boolean value.
func WaitForChainConfirmation(
blockHeightWaiter BlockHeightWaiter,
startBlockNumber uint64,
blockConfirmations uint64,
stateCheck func() (bool, error),
) (bool, error) {
blockHeight := startBlockNumber + blockConfirmations
logger.Infof("waiting for block [%d] to confirm chain state", blockHeight)

err := blockHeightWaiter.WaitForBlockHeight(blockHeight)
if err != nil {
return false, fmt.Errorf("failed to wait for block height: [%v]", err)
}

result, err := stateCheck()
if err != nil {
return false, fmt.Errorf("failed to get chain state confirmation: [%v]", err)
}

return result, nil
}

0 comments on commit 3f6d921

Please sign in to comment.