-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from keep-network/chain-confirmation-waiter
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
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |