From d43f4165b8fa463c881dfa611cdfb22c767f9daa Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Thu, 12 Nov 2020 13:13:04 +0100 Subject: [PATCH 1/2] 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. --- pkg/chain/ethereum/ethutil/ethutil.go | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pkg/chain/ethereum/ethutil/ethutil.go b/pkg/chain/ethereum/ethutil/ethutil.go index 9fb70f5..1819ca3 100644 --- a/pkg/chain/ethereum/ethutil/ethutil.go +++ b/pkg/chain/ethereum/ethutil/ethutil.go @@ -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 +} From 70d5d647ce08198e4cf7880bf860618e631b93a4 Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Thu, 12 Nov 2020 13:26:23 +0100 Subject: [PATCH 2/2] Move confirmations code to separate file --- pkg/chain/ethereum/ethutil/confirmations.go | 35 +++++++++++++++++++++ pkg/chain/ethereum/ethutil/ethutil.go | 32 ------------------- 2 files changed, 35 insertions(+), 32 deletions(-) create mode 100644 pkg/chain/ethereum/ethutil/confirmations.go diff --git a/pkg/chain/ethereum/ethutil/confirmations.go b/pkg/chain/ethereum/ethutil/confirmations.go new file mode 100644 index 0000000..bc1b0da --- /dev/null +++ b/pkg/chain/ethereum/ethutil/confirmations.go @@ -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 +} diff --git a/pkg/chain/ethereum/ethutil/ethutil.go b/pkg/chain/ethereum/ethutil/ethutil.go index 1819ca3..9fb70f5 100644 --- a/pkg/chain/ethereum/ethutil/ethutil.go +++ b/pkg/chain/ethereum/ethutil/ethutil.go @@ -213,35 +213,3 @@ 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 -}