Commit c2c76ced authored by Ralph Pichler's avatar Ralph Pichler Committed by GitHub

feat: wait for confirmations in WaitBlock (#2416)

parent 2fc7fa29
......@@ -30,8 +30,9 @@ import (
)
const (
maxDelay = 1 * time.Minute
cancellationDepth = 6
maxDelay = 1 * time.Minute
cancellationDepth = 6
additionalConfirmations = 2
)
// InitChain will initialize the Ethereum backend at the given endpoint and
......@@ -293,13 +294,7 @@ func GetTxNextBlock(ctx context.Context, logger logging.Logger, backend transact
return blockHash, nil
}
// if not found in statestore, fetch from chain
tx, err := backend.TransactionReceipt(ctx, common.BytesToHash(trx))
if err != nil {
return nil, err
}
block, err := transaction.WaitBlock(ctx, backend, duration, big.NewInt(0).Add(tx.BlockNumber, big.NewInt(1)))
block, err := transaction.WaitBlockAfterTransaction(ctx, backend, duration, common.BytesToHash(trx), additionalConfirmations)
if err != nil {
return nil, err
}
......
......@@ -76,15 +76,33 @@ func WaitSynced(ctx context.Context, logger logging.Logger, backend Backend, max
}
}
func WaitBlock(ctx context.Context, backend Backend, pollingInterval time.Duration, block *big.Int) (*types.Header, error) {
func WaitBlockAfterTransaction(ctx context.Context, backend Backend, pollingInterval time.Duration, txHash common.Hash, additionalConfirmations uint64) (*types.Header, error) {
for {
header, err := backend.HeaderByNumber(ctx, block)
receipt, err := backend.TransactionReceipt(ctx, txHash)
if err != nil {
if !errors.Is(err, ethereum.NotFound) {
return nil, err
}
} else {
return header, nil
continue
}
bn, err := backend.BlockNumber(ctx)
if err != nil {
return nil, err
}
nextBlock := receipt.BlockNumber.Uint64() + 1
if bn >= nextBlock+additionalConfirmations {
header, err := backend.HeaderByNumber(ctx, new(big.Int).SetUint64(nextBlock))
if err != nil {
if !errors.Is(err, ethereum.NotFound) {
return nil, err
}
// in the case where we cannot find the block even though we already saw a higher number we keep on trying
} else {
return header, nil
}
}
select {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment