Skip to content

accounts/abi/bind/v2: modify the default contract deployer to block until the tx is accepted/rejected from the pool #32169

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions accounts/abi/bind/v2/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ type ContractTransactor interface {

// PendingNonceAt retrieves the current pending nonce associated with an account.
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)

// TransactionByHash retrieves the transaction corresponding to the given hash.
TransactionByHash(ctx context.Context, txHash common.Hash) (tx *types.Transaction, isPending bool, err error)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's IMO a bit unfortunate that this method is on the ContractTransactor and not the DeployBackend interface.

However, the method signature of DefaultDeployer takes a ContractBackend. In order to not break compatibility, and make sure that this fix can apply to the default deployer, I decided to put TransactionsByHash here.

}

// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
Expand Down
4 changes: 4 additions & 0 deletions accounts/abi/bind/v2/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (mt *mockTransactor) SendTransaction(ctx context.Context, tx *types.Transac
return nil
}

func (mt *mockTransactor) TransactionByHash(ctx context.Context, txHash common.Hash) (tx *types.Transaction, isPending bool, err error) {
return nil, false, nil
}

type mockCaller struct {
codeAtBlockNumber *big.Int
callContractBlockNumber *big.Int
Expand Down
36 changes: 36 additions & 0 deletions accounts/abi/bind/v2/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
package bind

import (
"context"
"errors"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
)

// ContractEvent is a type constraint for ABI event types.
Expand Down Expand Up @@ -227,6 +230,34 @@ func DeployContract(opts *TransactOpts, bytecode []byte, backend ContractBackend
return crypto.CreateAddress(opts.From, tx.Nonce()), tx, nil
}

// waitAccepted polls the backend until the transaction is accepted or the
// context is cancelled. If the transaction was not accepted into the pool,
// an error is returned.
func waitAccepted(ctx context.Context, d ContractBackend, txHash common.Hash) error {
queryTicker := time.NewTicker(time.Second)
defer queryTicker.Stop()
logger := log.New("hash", txHash)
for {
_, _, err := d.TransactionByHash(ctx, txHash)
if err == nil {
return nil
}

if errors.Is(err, ethereum.NotFound) {
logger.Trace("Transaction not yet accepted")
} else {
logger.Trace("Transaction submission failed", "err", err)
}

// Wait for the next round.
select {
case <-ctx.Done():
return ctx.Err()
case <-queryTicker.C:
}
}
}

// DefaultDeployer returns a DeployFn that signs and submits creation transactions
// using the given signer.
//
Expand All @@ -238,6 +269,11 @@ func DefaultDeployer(opts *TransactOpts, backend ContractBackend) DeployFn {
if err != nil {
return common.Address{}, nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := waitAccepted(ctx, backend, tx.Hash()); err != nil {
return common.Address{}, nil, err
}
return addr, tx, nil
}
}