2016-08-22 21:20:13 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package bind
|
|
|
|
|
|
|
|
import (
|
2017-03-22 17:20:33 +00:00
|
|
|
"context"
|
2020-05-12 10:21:40 +00:00
|
|
|
"errors"
|
2016-08-22 21:20:13 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-02-22 12:10:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-08-22 21:20:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// WaitMined waits for tx to be mined on the blockchain.
|
|
|
|
// It stops waiting when the context is canceled.
|
|
|
|
func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {
|
2017-02-22 14:58:00 +00:00
|
|
|
queryTicker := time.NewTicker(time.Second)
|
2016-08-22 21:20:13 +00:00
|
|
|
defer queryTicker.Stop()
|
2017-02-22 14:58:00 +00:00
|
|
|
|
2017-02-28 11:35:17 +00:00
|
|
|
logger := log.New("hash", tx.Hash())
|
2016-08-22 21:20:13 +00:00
|
|
|
for {
|
|
|
|
receipt, err := b.TransactionReceipt(ctx, tx.Hash())
|
|
|
|
if receipt != nil {
|
|
|
|
return receipt, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-02-27 11:17:58 +00:00
|
|
|
logger.Trace("Receipt retrieval failed", "err", err)
|
2016-08-22 21:20:13 +00:00
|
|
|
} else {
|
2017-02-22 14:58:00 +00:00
|
|
|
logger.Trace("Transaction not yet mined")
|
2016-08-22 21:20:13 +00:00
|
|
|
}
|
|
|
|
// Wait for the next round.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
case <-queryTicker.C:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
|
|
|
|
if tx.To() != nil {
|
2020-05-12 10:21:40 +00:00
|
|
|
return common.Address{}, errors.New("tx is not contract creation")
|
2016-08-22 21:20:13 +00:00
|
|
|
}
|
|
|
|
receipt, err := WaitMined(ctx, b, tx)
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
if receipt.ContractAddress == (common.Address{}) {
|
2020-05-12 10:21:40 +00:00
|
|
|
return common.Address{}, errors.New("zero address")
|
2016-08-22 21:20:13 +00:00
|
|
|
}
|
|
|
|
// Check that code has indeed been deployed at the address.
|
|
|
|
// This matters on pre-Homestead chains: OOG in the constructor
|
|
|
|
// could leave an empty account behind.
|
|
|
|
code, err := b.CodeAt(ctx, receipt.ContractAddress, nil)
|
|
|
|
if err == nil && len(code) == 0 {
|
|
|
|
err = ErrNoCodeAfterDeploy
|
|
|
|
}
|
|
|
|
return receipt.ContractAddress, err
|
|
|
|
}
|