Skip to content

Commit

Permalink
feat: wait for confirmations in WaitBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
ralph-pichler committed Aug 17, 2021
1 parent 5a6ddbd commit 78f4dec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
13 changes: 4 additions & 9 deletions pkg/node/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
26 changes: 22 additions & 4 deletions pkg/transaction/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 78f4dec

Please sign in to comment.