mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-11 06:23:47 +00:00
accounts/abi/bind/v2: provide only one version of WaitMined, WaitDeployed
WaitMinedHash & WaitDeployedHash are more versatile, they only need the hash.
This commit is contained in:
parent
f937417fd8
commit
8200bbf4a0
5 changed files with 31 additions and 38 deletions
|
|
@ -239,23 +239,26 @@ func (m *MetaData) GetAbi() (*abi.ABI, error) {
|
||||||
// WaitMined waits for tx to be mined on the blockchain.
|
// WaitMined waits for tx to be mined on the blockchain.
|
||||||
// It stops waiting when the context is canceled.
|
// It stops waiting when the context is canceled.
|
||||||
func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {
|
func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {
|
||||||
return bind2.WaitMined(ctx, b, tx)
|
return bind2.WaitMined(ctx, b, tx.Hash())
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitMinedHash waits for a transaction with the provided hash to be mined on the blockchain.
|
// WaitMinedHash waits for a transaction with the provided hash to be mined on the blockchain.
|
||||||
// It stops waiting when the context is canceled.
|
// It stops waiting when the context is canceled.
|
||||||
func WaitMinedHash(ctx context.Context, b DeployBackend, hash common.Hash) (*types.Receipt, error) {
|
func WaitMinedHash(ctx context.Context, b DeployBackend, hash common.Hash) (*types.Receipt, error) {
|
||||||
return bind2.WaitMinedHash(ctx, b, hash)
|
return bind2.WaitMined(ctx, b, hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitDeployed waits for a contract deployment transaction and returns the on-chain
|
// WaitDeployed waits for a contract deployment transaction and returns the on-chain
|
||||||
// contract address when it is mined. It stops waiting when ctx is canceled.
|
// contract address when it is mined. It stops waiting when ctx is canceled.
|
||||||
func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
|
func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
|
||||||
return bind2.WaitDeployed(ctx, b, tx)
|
if tx.To() != nil {
|
||||||
|
return common.Address{}, errors.New("tx is not contract creation")
|
||||||
|
}
|
||||||
|
return bind2.WaitDeployed(ctx, b, tx.Hash())
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitDeployedHash waits for a contract deployment transaction with the provided hash and returns the on-chain
|
// WaitDeployedHash waits for a contract deployment transaction with the provided hash and returns the on-chain
|
||||||
// contract address when it is mined. It stops waiting when ctx is canceled.
|
// contract address when it is mined. It stops waiting when ctx is canceled.
|
||||||
func WaitDeployedHash(ctx context.Context, b DeployBackend, hash common.Hash) (common.Address, error) {
|
func WaitDeployedHash(ctx context.Context, b DeployBackend, hash common.Hash) (common.Address, error) {
|
||||||
return bind2.WaitDeployedHash(ctx, b, hash)
|
return bind2.WaitDeployed(ctx, b, hash)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,11 @@ var (
|
||||||
// ErrNoCodeAfterDeploy is returned by WaitDeployed if contract creation leaves
|
// ErrNoCodeAfterDeploy is returned by WaitDeployed if contract creation leaves
|
||||||
// an empty contract behind.
|
// an empty contract behind.
|
||||||
ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
|
ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
|
||||||
|
|
||||||
|
// Returned by WaitDeployed when the receipt for the transaction hash does not contain
|
||||||
|
// a contract address. This error may indicated that the transaction hash was not a
|
||||||
|
// CREATE transaction.
|
||||||
|
ErrNoAddressInReceipt = errors.New("no contract address in receipt")
|
||||||
)
|
)
|
||||||
|
|
||||||
// ContractCaller defines the methods needed to allow operating with a contract on a read
|
// ContractCaller defines the methods needed to allow operating with a contract on a read
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,7 @@ func TestDeploymentLibraries(t *testing.T) {
|
||||||
t.Fatalf("deployment should have generated 5 addresses. got %d", len(res.Addrs))
|
t.Fatalf("deployment should have generated 5 addresses. got %d", len(res.Addrs))
|
||||||
}
|
}
|
||||||
for _, tx := range res.Txs {
|
for _, tx := range res.Txs {
|
||||||
_, err = bind.WaitDeployed(context.Background(), bindBackend, tx)
|
_, err = bind.WaitDeployed(context.Background(), bindBackend, tx.Hash())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error deploying library: %+v", err)
|
t.Fatalf("error deploying library: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -161,7 +161,7 @@ func TestDeploymentWithOverrides(t *testing.T) {
|
||||||
t.Fatalf("deployment should have generated 4 addresses. got %d", len(res.Addrs))
|
t.Fatalf("deployment should have generated 4 addresses. got %d", len(res.Addrs))
|
||||||
}
|
}
|
||||||
for _, tx := range res.Txs {
|
for _, tx := range res.Txs {
|
||||||
_, err = bind.WaitDeployed(context.Background(), bindBackend, tx)
|
_, err = bind.WaitDeployed(context.Background(), bindBackend, tx.Hash())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error deploying library: %+v", err)
|
t.Fatalf("error deploying library: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -182,7 +182,7 @@ func TestDeploymentWithOverrides(t *testing.T) {
|
||||||
t.Fatalf("deployment should have generated 1 address. got %d", len(res.Addrs))
|
t.Fatalf("deployment should have generated 1 address. got %d", len(res.Addrs))
|
||||||
}
|
}
|
||||||
for _, tx := range res.Txs {
|
for _, tx := range res.Txs {
|
||||||
_, err = bind.WaitDeployed(context.Background(), bindBackend, tx)
|
_, err = bind.WaitDeployed(context.Background(), bindBackend, tx.Hash())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error deploying library: %+v", err)
|
t.Fatalf("error deploying library: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -215,7 +215,7 @@ func TestEvents(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
backend.Commit()
|
backend.Commit()
|
||||||
if _, err := bind.WaitDeployed(context.Background(), backend, res.Txs[events.CMetaData.Pattern]); err != nil {
|
if _, err := bind.WaitDeployed(context.Background(), backend, res.Txs[events.CMetaData.Pattern].Hash()); err != nil {
|
||||||
t.Fatalf("WaitDeployed failed %v", err)
|
t.Fatalf("WaitDeployed failed %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,7 +242,7 @@ func TestEvents(t *testing.T) {
|
||||||
t.Fatalf("failed to send transaction: %v", err)
|
t.Fatalf("failed to send transaction: %v", err)
|
||||||
}
|
}
|
||||||
backend.Commit()
|
backend.Commit()
|
||||||
if _, err := bind.WaitMined(context.Background(), backend, tx); err != nil {
|
if _, err := bind.WaitMined(context.Background(), backend, tx.Hash()); err != nil {
|
||||||
t.Fatalf("error waiting for tx to be mined: %v", err)
|
t.Fatalf("error waiting for tx to be mined: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -314,7 +314,7 @@ func TestErrors(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
backend.Commit()
|
backend.Commit()
|
||||||
if _, err := bind.WaitDeployed(context.Background(), backend, res.Txs[solc_errors.CMetaData.Pattern]); err != nil {
|
if _, err := bind.WaitDeployed(context.Background(), backend, res.Txs[solc_errors.CMetaData.Pattern].Hash()); err != nil {
|
||||||
t.Fatalf("WaitDeployed failed %v", err)
|
t.Fatalf("WaitDeployed failed %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,19 +29,13 @@ import (
|
||||||
|
|
||||||
// WaitMined waits for tx to be mined on the blockchain.
|
// WaitMined waits for tx to be mined on the blockchain.
|
||||||
// It stops waiting when the context is canceled.
|
// It stops waiting when the context is canceled.
|
||||||
func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {
|
func WaitMined(ctx context.Context, b DeployBackend, txHash common.Hash) (*types.Receipt, error) {
|
||||||
return WaitMinedHash(ctx, b, tx.Hash())
|
|
||||||
}
|
|
||||||
|
|
||||||
// WaitMinedHash waits for a transaction with the provided hash to be mined on the blockchain.
|
|
||||||
// It stops waiting when the context is canceled.
|
|
||||||
func WaitMinedHash(ctx context.Context, b DeployBackend, hash common.Hash) (*types.Receipt, error) {
|
|
||||||
queryTicker := time.NewTicker(time.Second)
|
queryTicker := time.NewTicker(time.Second)
|
||||||
defer queryTicker.Stop()
|
defer queryTicker.Stop()
|
||||||
|
|
||||||
logger := log.New("hash", hash)
|
logger := log.New("hash", txHash)
|
||||||
for {
|
for {
|
||||||
receipt, err := b.TransactionReceipt(ctx, hash)
|
receipt, err := b.TransactionReceipt(ctx, txHash)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return receipt, nil
|
return receipt, nil
|
||||||
}
|
}
|
||||||
|
|
@ -61,24 +55,16 @@ func WaitMinedHash(ctx context.Context, b DeployBackend, hash common.Hash) (*typ
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitDeployed waits for a contract deployment transaction and returns the on-chain
|
// WaitDeployed waits for a contract deployment transaction with the provided hash and
|
||||||
// contract address when it is mined. It stops waiting when ctx is canceled.
|
// returns the on-chain contract address when it is mined. It stops waiting when ctx is
|
||||||
func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
|
// canceled.
|
||||||
if tx.To() != nil {
|
func WaitDeployed(ctx context.Context, b DeployBackend, hash common.Hash) (common.Address, error) {
|
||||||
return common.Address{}, errors.New("tx is not contract creation")
|
receipt, err := WaitMined(ctx, b, hash)
|
||||||
}
|
|
||||||
return WaitDeployedHash(ctx, b, tx.Hash())
|
|
||||||
}
|
|
||||||
|
|
||||||
// WaitDeployedHash waits for a contract deployment transaction with the provided hash and returns the on-chain
|
|
||||||
// contract address when it is mined. It stops waiting when ctx is canceled.
|
|
||||||
func WaitDeployedHash(ctx context.Context, b DeployBackend, hash common.Hash) (common.Address, error) {
|
|
||||||
receipt, err := WaitMinedHash(ctx, b, hash)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.Address{}, err
|
return common.Address{}, err
|
||||||
}
|
}
|
||||||
if receipt.ContractAddress == (common.Address{}) {
|
if receipt.ContractAddress == (common.Address{}) {
|
||||||
return common.Address{}, errors.New("zero address")
|
return common.Address{}, ErrNoAddressInReceipt
|
||||||
}
|
}
|
||||||
// Check that code has indeed been deployed at the address.
|
// Check that code has indeed been deployed at the address.
|
||||||
// This matters on pre-Homestead chains: OOG in the constructor
|
// This matters on pre-Homestead chains: OOG in the constructor
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ func TestWaitDeployed(t *testing.T) {
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
)
|
)
|
||||||
go func() {
|
go func() {
|
||||||
address, err = bind.WaitDeployed(ctx, backend.Client(), tx)
|
address, err = bind.WaitDeployed(ctx, backend.Client(), tx.Hash())
|
||||||
close(mined)
|
close(mined)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
@ -120,9 +120,8 @@ func TestWaitDeployedCornerCases(t *testing.T) {
|
||||||
t.Errorf("failed to send transaction: %q", err)
|
t.Errorf("failed to send transaction: %q", err)
|
||||||
}
|
}
|
||||||
backend.Commit()
|
backend.Commit()
|
||||||
notContractCreation := errors.New("tx is not contract creation")
|
if _, err := bind.WaitDeployed(ctx, backend.Client(), tx.Hash()); err != bind.ErrNoAddressInReceipt {
|
||||||
if _, err := bind.WaitDeployed(ctx, backend.Client(), tx); err.Error() != notContractCreation.Error() {
|
t.Errorf("error mismatch: want %q, got %q, ", bind.ErrNoAddressInReceipt, err)
|
||||||
t.Errorf("error mismatch: want %q, got %q, ", notContractCreation, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a transaction that is not mined.
|
// Create a transaction that is not mined.
|
||||||
|
|
@ -131,7 +130,7 @@ func TestWaitDeployedCornerCases(t *testing.T) {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
contextCanceled := errors.New("context canceled")
|
contextCanceled := errors.New("context canceled")
|
||||||
if _, err := bind.WaitDeployed(ctx, backend.Client(), tx); err.Error() != contextCanceled.Error() {
|
if _, err := bind.WaitDeployed(ctx, backend.Client(), tx.Hash()); err.Error() != contextCanceled.Error() {
|
||||||
t.Errorf("error mismatch: want %q, got %q, ", contextCanceled, err)
|
t.Errorf("error mismatch: want %q, got %q, ", contextCanceled, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue