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

feat: wait for confirmations in WaitBlock (#2416)

parent 2fc7fa29
...@@ -32,6 +32,7 @@ import ( ...@@ -32,6 +32,7 @@ import (
const ( const (
maxDelay = 1 * time.Minute maxDelay = 1 * time.Minute
cancellationDepth = 6 cancellationDepth = 6
additionalConfirmations = 2
) )
// InitChain will initialize the Ethereum backend at the given endpoint and // 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 ...@@ -293,13 +294,7 @@ func GetTxNextBlock(ctx context.Context, logger logging.Logger, backend transact
return blockHash, nil return blockHash, nil
} }
// if not found in statestore, fetch from chain block, err := transaction.WaitBlockAfterTransaction(ctx, backend, duration, common.BytesToHash(trx), additionalConfirmations)
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)))
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -76,16 +76,34 @@ func WaitSynced(ctx context.Context, logger logging.Logger, backend Backend, max ...@@ -76,16 +76,34 @@ 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 { for {
header, err := backend.HeaderByNumber(ctx, block) receipt, err := backend.TransactionReceipt(ctx, txHash)
if err != nil { if err != nil {
if !errors.Is(err, ethereum.NotFound) { if !errors.Is(err, ethereum.NotFound) {
return nil, err return nil, err
} }
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 { } else {
return header, nil return header, nil
} }
}
select { select {
case <-time.After(pollingInterval): case <-time.After(pollingInterval):
......
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