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

Add chain confirmation waiter #56

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions pkg/chain/ethereum/ethutil/ethutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,35 @@ func (lw *loggingWrapper) EstimateGas(ctx context.Context, msg ethereum.CallMsg)
func WrapCallLogging(logger log.EventLogger, client EthereumClient) EthereumClient {
return &loggingWrapper{client, logger}
}

// 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
}
pdyraga marked this conversation as resolved.
Show resolved Hide resolved