mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 14:16:44 +00:00
Merge c7c81f872f into 806e3cd075
This commit is contained in:
commit
45763ef95a
6 changed files with 285 additions and 332 deletions
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
@ -33,15 +34,10 @@ var ErrNoCode = errors.New("no contract code at given address")
|
||||||
// ContractCaller defines the methods needed to allow operating with contract on a read
|
// ContractCaller defines the methods needed to allow operating with contract on a read
|
||||||
// only basis.
|
// only basis.
|
||||||
type ContractCaller interface {
|
type ContractCaller interface {
|
||||||
// HasCode checks if the contract at the given address has any code associated
|
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
|
||||||
// with it or not. This is needed to differentiate between contract internal
|
PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
|
||||||
// errors and the local chain being out of sync.
|
CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
|
||||||
HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error)
|
PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
|
||||||
|
|
||||||
// ContractCall executes an Ethereum contract call with the specified data as
|
|
||||||
// the input. The pending flag requests execution against the pending block, not
|
|
||||||
// the stable head of the chain.
|
|
||||||
ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContractTransactor defines the methods needed to allow operating with contract
|
// ContractTransactor defines the methods needed to allow operating with contract
|
||||||
|
|
@ -49,27 +45,11 @@ type ContractCaller interface {
|
||||||
// used when the user does not provide some needed values, but rather leaves it up
|
// used when the user does not provide some needed values, but rather leaves it up
|
||||||
// to the transactor to decide.
|
// to the transactor to decide.
|
||||||
type ContractTransactor interface {
|
type ContractTransactor interface {
|
||||||
// PendingAccountNonce retrieves the current pending nonce associated with an
|
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
|
||||||
// account.
|
PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
|
||||||
PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error)
|
|
||||||
|
|
||||||
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
|
|
||||||
// execution of a transaction.
|
|
||||||
SuggestGasPrice(ctx context.Context) (*big.Int, error)
|
SuggestGasPrice(ctx context.Context) (*big.Int, error)
|
||||||
|
EstimateGas(ctx context.Context, call ethereum.CallMsg) (usedGas *big.Int, err error)
|
||||||
// HasCode checks if the contract at the given address has any code associated
|
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
|
||||||
// with it or not. This is needed to differentiate between contract internal
|
|
||||||
// errors and the local chain being out of sync.
|
|
||||||
HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error)
|
|
||||||
|
|
||||||
// EstimateGasLimit tries to estimate the gas needed to execute a specific
|
|
||||||
// transaction based on the current pending state of the backend blockchain.
|
|
||||||
// There is no guarantee that this is the true gas limit requirement as other
|
|
||||||
// transactions may be added or removed by miners, but it should provide a basis
|
|
||||||
// for setting a reasonable default.
|
|
||||||
EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error)
|
|
||||||
|
|
||||||
// SendTransaction injects the transaction into the pending pool for execution.
|
|
||||||
SendTransaction(ctx context.Context, tx *types.Transaction) error
|
SendTransaction(ctx context.Context, tx *types.Transaction) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,35 +58,14 @@ type ContractTransactor interface {
|
||||||
//
|
//
|
||||||
// This interface is essentially the union of ContractCaller and ContractTransactor
|
// This interface is essentially the union of ContractCaller and ContractTransactor
|
||||||
// but due to a bug in the Go compiler (https://github.com/golang/go/issues/6977),
|
// but due to a bug in the Go compiler (https://github.com/golang/go/issues/6977),
|
||||||
// we cannot simply list it as the two interfaces. The other solution is to add a
|
// we cannot simply list it as the two interfaces.
|
||||||
// third interface containing the common methods, but that convolutes the user API
|
|
||||||
// as it introduces yet another parameter to require for initialization.
|
|
||||||
type ContractBackend interface {
|
type ContractBackend interface {
|
||||||
// HasCode checks if the contract at the given address has any code associated
|
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
|
||||||
// with it or not. This is needed to differentiate between contract internal
|
PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
|
||||||
// errors and the local chain being out of sync.
|
CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
|
||||||
HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error)
|
PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
|
||||||
|
|
||||||
// ContractCall executes an Ethereum contract call with the specified data as
|
|
||||||
// the input. The pending flag requests execution against the pending block, not
|
|
||||||
// the stable head of the chain.
|
|
||||||
ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error)
|
|
||||||
|
|
||||||
// PendingAccountNonce retrieves the current pending nonce associated with an
|
|
||||||
// account.
|
|
||||||
PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error)
|
|
||||||
|
|
||||||
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
|
|
||||||
// execution of a transaction.
|
|
||||||
SuggestGasPrice(ctx context.Context) (*big.Int, error)
|
SuggestGasPrice(ctx context.Context) (*big.Int, error)
|
||||||
|
EstimateGas(ctx context.Context, call ethereum.CallMsg) (usedGas *big.Int, err error)
|
||||||
// EstimateGasLimit tries to estimate the gas needed to execute a specific
|
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
|
||||||
// transaction based on the current pending state of the backend blockchain.
|
|
||||||
// There is no guarantee that this is the true gas limit requirement as other
|
|
||||||
// transactions may be added or removed by miners, but it should provide a basis
|
|
||||||
// for setting a reasonable default.
|
|
||||||
EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error)
|
|
||||||
|
|
||||||
// SendTransaction injects the transaction into the pending pool for execution.
|
|
||||||
SendTransaction(ctx context.Context, tx *types.Transaction) error
|
SendTransaction(ctx context.Context, tx *types.Transaction) error
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,57 +0,0 @@
|
||||||
// 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 backends
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math/big"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
)
|
|
||||||
|
|
||||||
// This nil assignment ensures compile time that nilBackend implements bind.ContractBackend.
|
|
||||||
var _ bind.ContractBackend = (*nilBackend)(nil)
|
|
||||||
|
|
||||||
// nilBackend implements bind.ContractBackend, but panics on any method call.
|
|
||||||
// Its sole purpose is to support the binding tests to construct the generated
|
|
||||||
// wrappers without calling any methods on them.
|
|
||||||
type nilBackend struct{}
|
|
||||||
|
|
||||||
func (*nilBackend) ContractCall(context.Context, common.Address, []byte, bool) ([]byte, error) {
|
|
||||||
panic("not implemented")
|
|
||||||
}
|
|
||||||
func (*nilBackend) EstimateGasLimit(context.Context, common.Address, *common.Address, *big.Int, []byte) (*big.Int, error) {
|
|
||||||
panic("not implemented")
|
|
||||||
}
|
|
||||||
func (*nilBackend) HasCode(context.Context, common.Address, bool) (bool, error) {
|
|
||||||
panic("not implemented")
|
|
||||||
}
|
|
||||||
func (*nilBackend) SuggestGasPrice(context.Context) (*big.Int, error) { panic("not implemented") }
|
|
||||||
func (*nilBackend) PendingAccountNonce(context.Context, common.Address) (uint64, error) {
|
|
||||||
panic("not implemented")
|
|
||||||
}
|
|
||||||
func (*nilBackend) SendTransaction(context.Context, *types.Transaction) error {
|
|
||||||
panic("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewNilBackend creates a new binding backend that can be used for instantiation
|
|
||||||
// but will panic on any invocation. Its sole purpose is to help testing.
|
|
||||||
func NewNilBackend() bind.ContractBackend {
|
|
||||||
return new(nilBackend)
|
|
||||||
}
|
|
||||||
|
|
@ -1,136 +0,0 @@
|
||||||
// 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 backends
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math/big"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
)
|
|
||||||
|
|
||||||
// This nil assignment ensures compile time that rpcBackend implements bind.ContractBackend.
|
|
||||||
var _ bind.ContractBackend = (*rpcBackend)(nil)
|
|
||||||
|
|
||||||
// rpcBackend implements bind.ContractBackend, and acts as the data provider to
|
|
||||||
// Ethereum contracts bound to Go structs. It uses an RPC connection to delegate
|
|
||||||
// all its functionality.
|
|
||||||
type rpcBackend struct {
|
|
||||||
client *rpc.Client // RPC client connection to interact with an API server
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRPCBackend creates a new binding backend to an RPC provider that can be
|
|
||||||
// used to interact with remote contracts.
|
|
||||||
func NewRPCBackend(client *rpc.Client) bind.ContractBackend {
|
|
||||||
return &rpcBackend{client: client}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasCode implements ContractVerifier.HasCode by retrieving any code associated
|
|
||||||
// with the contract from the remote node, and checking its size.
|
|
||||||
func (b *rpcBackend) HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error) {
|
|
||||||
block := "latest"
|
|
||||||
if pending {
|
|
||||||
block = "pending"
|
|
||||||
}
|
|
||||||
var hex string
|
|
||||||
err := b.client.CallContext(ctx, &hex, "eth_getCode", contract, block)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return len(common.FromHex(hex)) > 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ContractCall implements ContractCaller.ContractCall, delegating the execution of
|
|
||||||
// a contract call to the remote node, returning the reply to for local processing.
|
|
||||||
func (b *rpcBackend) ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error) {
|
|
||||||
args := struct {
|
|
||||||
To common.Address `json:"to"`
|
|
||||||
Data string `json:"data"`
|
|
||||||
}{
|
|
||||||
To: contract,
|
|
||||||
Data: common.ToHex(data),
|
|
||||||
}
|
|
||||||
block := "latest"
|
|
||||||
if pending {
|
|
||||||
block = "pending"
|
|
||||||
}
|
|
||||||
var hex string
|
|
||||||
err := b.client.CallContext(ctx, &hex, "eth_call", args, block)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return common.FromHex(hex), nil
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// PendingAccountNonce implements ContractTransactor.PendingAccountNonce, delegating
|
|
||||||
// the current account nonce retrieval to the remote node.
|
|
||||||
func (b *rpcBackend) PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error) {
|
|
||||||
var hex rpc.HexNumber
|
|
||||||
err := b.client.CallContext(ctx, &hex, "eth_getTransactionCount", account.Hex(), "pending")
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return hex.Uint64(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SuggestGasPrice implements ContractTransactor.SuggestGasPrice, delegating the
|
|
||||||
// gas price oracle request to the remote node.
|
|
||||||
func (b *rpcBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
|
|
||||||
var hex rpc.HexNumber
|
|
||||||
if err := b.client.CallContext(ctx, &hex, "eth_gasPrice"); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return (*big.Int)(&hex), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// EstimateGasLimit implements ContractTransactor.EstimateGasLimit, delegating
|
|
||||||
// the gas estimation to the remote node.
|
|
||||||
func (b *rpcBackend) EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) {
|
|
||||||
args := struct {
|
|
||||||
From common.Address `json:"from"`
|
|
||||||
To *common.Address `json:"to"`
|
|
||||||
Value *rpc.HexNumber `json:"value"`
|
|
||||||
Data string `json:"data"`
|
|
||||||
}{
|
|
||||||
From: sender,
|
|
||||||
To: contract,
|
|
||||||
Data: common.ToHex(data),
|
|
||||||
Value: rpc.NewHexNumber(value),
|
|
||||||
}
|
|
||||||
// Execute the RPC call and retrieve the response
|
|
||||||
var hex rpc.HexNumber
|
|
||||||
err := b.client.CallContext(ctx, &hex, "eth_estimateGas", args)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return (*big.Int)(&hex), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SendTransaction implements ContractTransactor.SendTransaction, delegating the
|
|
||||||
// raw transaction injection to the remote node.
|
|
||||||
func (b *rpcBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
|
|
||||||
data, err := rlp.EncodeToBytes(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return b.client.CallContext(ctx, nil, "eth_sendRawTransaction", common.ToHex(data))
|
|
||||||
}
|
|
||||||
|
|
@ -18,7 +18,9 @@ package backends
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum"
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
|
|
@ -79,47 +81,51 @@ func (b *SimulatedBackend) Rollback() {
|
||||||
b.pendingState, _ = state.New(b.pendingBlock.Root(), b.database)
|
b.pendingState, _ = state.New(b.pendingBlock.Root(), b.database)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasCode implements ContractVerifier.HasCode, checking whether there is any
|
// CodeAt implements ChainStateReader.CodeAt, returning the code associated with
|
||||||
// code associated with a certain account in the blockchain.
|
// a certain account at a given block number in the blockchain.
|
||||||
func (b *SimulatedBackend) HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error) {
|
func (b *SimulatedBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
|
||||||
if pending {
|
// TODO: implement block number
|
||||||
return len(b.pendingState.GetCode(contract)) > 0, nil
|
|
||||||
}
|
|
||||||
statedb, _ := b.blockchain.State()
|
statedb, _ := b.blockchain.State()
|
||||||
return len(statedb.GetCode(contract)) > 0, nil
|
return statedb.GetCode(contract), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContractCall implements ContractCaller.ContractCall, executing the specified
|
// PendingCodeAt implements PendingStateReader.PendingCodeAt, returning the
|
||||||
// contract with the given input data.
|
// code associated with a certain account in the pending state of the blockchain.
|
||||||
func (b *SimulatedBackend) ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error) {
|
func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
|
||||||
|
return b.pendingState.GetCode(contract), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CallContract implements Contractcaller.CallContract, executing the specified
|
||||||
|
// contract with the given call message at the given block number.
|
||||||
|
func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
|
||||||
// Create a copy of the current state db to screw around with
|
// Create a copy of the current state db to screw around with
|
||||||
var (
|
block := b.blockchain.CurrentBlock()
|
||||||
block *types.Block
|
statedb, _ := b.blockchain.State()
|
||||||
statedb *state.StateDB
|
return b.callContract(ctx, call, block, statedb)
|
||||||
)
|
}
|
||||||
if pending {
|
|
||||||
block, statedb = b.pendingBlock, b.pendingState.Copy()
|
// PendingCallContract implements PendingContractCaller.PendingCallContract, executing
|
||||||
} else {
|
// the specified contract with the given call message against the pending state.
|
||||||
block = b.blockchain.CurrentBlock()
|
func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) {
|
||||||
statedb, _ = b.blockchain.State()
|
// Create a copy of the current state db to screw around with
|
||||||
}
|
block := b.pendingBlock
|
||||||
|
statedb := b.pendingState.Copy()
|
||||||
|
return b.callContract(ctx, call, block, statedb)
|
||||||
|
}
|
||||||
|
|
||||||
|
// callContract implemens common code between normal and pending contract calls.
|
||||||
|
func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.StateDB) ([]byte, error) {
|
||||||
// If there's no code to interact with, respond with an appropriate error
|
// If there's no code to interact with, respond with an appropriate error
|
||||||
if code := statedb.GetCode(contract); len(code) == 0 {
|
if code := statedb.GetCode(call.To); len(code) == 0 {
|
||||||
return nil, bind.ErrNoCode
|
return nil, bind.ErrNoCode
|
||||||
}
|
}
|
||||||
// Set infinite balance to the a fake caller account
|
// Set infinite balance to the a fake caller account
|
||||||
from := statedb.GetOrNewStateObject(common.Address{})
|
from := statedb.GetOrNewStateObject(call.From)
|
||||||
from.SetBalance(common.MaxBig)
|
from.SetBalance(common.MaxBig)
|
||||||
|
|
||||||
// Assemble the call invocation to measure the gas usage
|
// Wrap the call invocation to fulfil the core.Message interface
|
||||||
msg := callmsg{
|
msg := callmsg{call}
|
||||||
from: from,
|
|
||||||
to: &contract,
|
|
||||||
gasPrice: new(big.Int),
|
|
||||||
gasLimit: common.MaxBig,
|
|
||||||
value: new(big.Int),
|
|
||||||
data: data,
|
|
||||||
}
|
|
||||||
// Execute the call and return
|
// Execute the call and return
|
||||||
vmenv := core.NewEnv(statedb, chainConfig, b.blockchain, msg, block.Header(), vm.Config{})
|
vmenv := core.NewEnv(statedb, chainConfig, b.blockchain, msg, block.Header(), vm.Config{})
|
||||||
gaspool := new(core.GasPool).AddGas(common.MaxBig)
|
gaspool := new(core.GasPool).AddGas(common.MaxBig)
|
||||||
|
|
@ -128,9 +134,9 @@ func (b *SimulatedBackend) ContractCall(ctx context.Context, contract common.Add
|
||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// PendingAccountNonce implements ContractTransactor.PendingAccountNonce, retrieving
|
// PendingNonceAt implements PendingStateReader.PendingNonceAt, retrieving
|
||||||
// the nonce currently pending for the account.
|
// the nonce currently pending for the account.
|
||||||
func (b *SimulatedBackend) PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error) {
|
func (b *SimulatedBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
|
||||||
return b.pendingState.GetOrNewStateObject(account).Nonce(), nil
|
return b.pendingState.GetOrNewStateObject(account).Nonce(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -140,34 +146,27 @@ func (b *SimulatedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error
|
||||||
return big.NewInt(1), nil
|
return big.NewInt(1), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EstimateGasLimit implements ContractTransactor.EstimateGasLimit, executing the
|
// EstimateGas implements GasEstimator.EstimateGas, executing the
|
||||||
// requested code against the currently pending block/state and returning the used
|
// requested code against the currently pending block/state and returning the used
|
||||||
// gas.
|
// gas.
|
||||||
func (b *SimulatedBackend) EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) {
|
func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (*big.Int, error) {
|
||||||
// Create a copy of the currently pending state db to screw around with
|
// Create a copy of the currently pending state db to screw around with
|
||||||
var (
|
block := b.pendingBlock
|
||||||
block = b.pendingBlock
|
statedb := b.pendingState.Copy()
|
||||||
statedb = b.pendingState.Copy()
|
|
||||||
)
|
|
||||||
// If there's no code to interact with, respond with an appropriate error
|
// If there's no code to interact with, respond with an appropriate error
|
||||||
if contract != nil {
|
empty := common.Address{}
|
||||||
if code := statedb.GetCode(*contract); len(code) == 0 {
|
if !reflect.DeepEqual(call.To, empty) {
|
||||||
|
if code := statedb.GetCode(call.To); len(code) == 0 {
|
||||||
return nil, bind.ErrNoCode
|
return nil, bind.ErrNoCode
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Set infinite balance to the a fake caller account
|
// Set infinite balance to the a fake caller account
|
||||||
from := statedb.GetOrNewStateObject(sender)
|
from := statedb.GetOrNewStateObject(call.From)
|
||||||
from.SetBalance(common.MaxBig)
|
from.SetBalance(common.MaxBig)
|
||||||
|
|
||||||
// Assemble the call invocation to measure the gas usage
|
// Wrap the call invocation to fulfil the core.Message interface
|
||||||
msg := callmsg{
|
msg := callmsg{call}
|
||||||
from: from,
|
|
||||||
to: contract,
|
|
||||||
gasPrice: new(big.Int),
|
|
||||||
gasLimit: common.MaxBig,
|
|
||||||
value: value,
|
|
||||||
data: data,
|
|
||||||
}
|
|
||||||
// Execute the call and return
|
// Execute the call and return
|
||||||
vmenv := core.NewEnv(statedb, chainConfig, b.blockchain, msg, block.Header(), vm.Config{})
|
vmenv := core.NewEnv(statedb, chainConfig, b.blockchain, msg, block.Header(), vm.Config{})
|
||||||
gaspool := new(core.GasPool).AddGas(common.MaxBig)
|
gaspool := new(core.GasPool).AddGas(common.MaxBig)
|
||||||
|
|
@ -176,7 +175,7 @@ func (b *SimulatedBackend) EstimateGasLimit(ctx context.Context, sender common.A
|
||||||
return gas, err
|
return gas, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendTransaction implements ContractTransactor.SendTransaction, delegating the raw
|
// SendTransaction implements TransactionSender.SendTransaction, delegating the raw
|
||||||
// transaction injection to the remote node.
|
// transaction injection to the remote node.
|
||||||
func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
|
func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
|
||||||
blocks, _ := core.GenerateChain(nil, b.blockchain.CurrentBlock(), b.database, 1, func(number int, block *core.BlockGen) {
|
blocks, _ := core.GenerateChain(nil, b.blockchain.CurrentBlock(), b.database, 1, func(number int, block *core.BlockGen) {
|
||||||
|
|
@ -193,20 +192,15 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
|
||||||
|
|
||||||
// callmsg implements core.Message to allow passing it as a transaction simulator.
|
// callmsg implements core.Message to allow passing it as a transaction simulator.
|
||||||
type callmsg struct {
|
type callmsg struct {
|
||||||
from *state.StateObject
|
ethereum.CallMsg
|
||||||
to *common.Address
|
|
||||||
gasLimit *big.Int
|
|
||||||
gasPrice *big.Int
|
|
||||||
value *big.Int
|
|
||||||
data []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m callmsg) From() (common.Address, error) { return m.from.Address(), nil }
|
func (m callmsg) From() (common.Address, error) { return m.CallMsg.From, nil }
|
||||||
func (m callmsg) FromFrontier() (common.Address, error) { return m.from.Address(), nil }
|
func (m callmsg) FromFrontier() (common.Address, error) { return m.CallMsg.From, nil }
|
||||||
func (m callmsg) Nonce() uint64 { return 0 }
|
func (m callmsg) Nonce() uint64 { return 0 }
|
||||||
func (m callmsg) CheckNonce() bool { return false }
|
func (m callmsg) CheckNonce() bool { return false }
|
||||||
func (m callmsg) To() *common.Address { return m.to }
|
func (m callmsg) To() *common.Address { return &m.CallMsg.To }
|
||||||
func (m callmsg) GasPrice() *big.Int { return m.gasPrice }
|
func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
|
||||||
func (m callmsg) Gas() *big.Int { return m.gasLimit }
|
func (m callmsg) Gas() *big.Int { return m.CallMsg.Gas }
|
||||||
func (m callmsg) Value() *big.Int { return m.value }
|
func (m callmsg) Value() *big.Int { return m.CallMsg.Value }
|
||||||
func (m callmsg) Data() []byte { return m.data }
|
func (m callmsg) Data() []byte { return m.CallMsg.Data }
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum"
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
|
@ -106,10 +107,16 @@ func (c *BoundContract) Call(opts *CallOpts, result interface{}, method string,
|
||||||
opts = new(CallOpts)
|
opts = new(CallOpts)
|
||||||
}
|
}
|
||||||
// Make sure we have a contract to operate on, and bail out otherwise
|
// Make sure we have a contract to operate on, and bail out otherwise
|
||||||
if (opts.Pending && atomic.LoadUint32(&c.pendingHasCode) == 0) || (!opts.Pending && atomic.LoadUint32(&c.latestHasCode) == 0) {
|
var code []byte
|
||||||
if code, err := c.caller.HasCode(opts.Context, c.address, opts.Pending); err != nil {
|
var err error
|
||||||
|
if opts.Pending && atomic.LoadUint32(&c.pendingHasCode) == 0 {
|
||||||
|
code, err = c.caller.PendingCodeAt(opts.Context, c.address)
|
||||||
|
} else if !opts.Pending && atomic.LoadUint32(&c.latestHasCode) == 0 {
|
||||||
|
code, err = c.caller.CodeAt(opts.Context, c.address, nil)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !code {
|
} else if len(code) == 0 {
|
||||||
return ErrNoCode
|
return ErrNoCode
|
||||||
}
|
}
|
||||||
if opts.Pending {
|
if opts.Pending {
|
||||||
|
|
@ -117,13 +124,23 @@ func (c *BoundContract) Call(opts *CallOpts, result interface{}, method string,
|
||||||
} else {
|
} else {
|
||||||
atomic.StoreUint32(&c.latestHasCode, 1)
|
atomic.StoreUint32(&c.latestHasCode, 1)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// Pack the input, call and unpack the results
|
// Pack the input, call and unpack the results
|
||||||
input, err := c.abi.Pack(method, params...)
|
input, err := c.abi.Pack(method, params...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
output, err := c.caller.ContractCall(opts.Context, c.address, input, opts.Pending)
|
// Create the call message we use to make the call
|
||||||
|
callMsg := ethereum.CallMsg{
|
||||||
|
To: c.address,
|
||||||
|
Data: input,
|
||||||
|
}
|
||||||
|
var output []byte
|
||||||
|
// Call pending or not depending on options
|
||||||
|
if opts.Pending {
|
||||||
|
output, err = c.caller.PendingCallContract(opts.Context, callMsg)
|
||||||
|
} else {
|
||||||
|
output, err = c.caller.CallContract(opts.Context, callMsg, nil)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -158,7 +175,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
|
||||||
}
|
}
|
||||||
nonce := uint64(0)
|
nonce := uint64(0)
|
||||||
if opts.Nonce == nil {
|
if opts.Nonce == nil {
|
||||||
nonce, err = c.transactor.PendingAccountNonce(opts.Context, opts.From)
|
nonce, err = c.transactor.PendingNonceAt(opts.Context, opts.From)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to retrieve account nonce: %v", err)
|
return nil, fmt.Errorf("failed to retrieve account nonce: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -177,17 +194,25 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
|
||||||
if gasLimit == nil {
|
if gasLimit == nil {
|
||||||
// Gas estimation cannot succeed without code for method invocations
|
// Gas estimation cannot succeed without code for method invocations
|
||||||
if contract != nil && atomic.LoadUint32(&c.pendingHasCode) == 0 {
|
if contract != nil && atomic.LoadUint32(&c.pendingHasCode) == 0 {
|
||||||
if code, err := c.transactor.HasCode(opts.Context, c.address, true); err != nil {
|
var code []byte
|
||||||
|
if code, err = c.transactor.CodeAt(opts.Context, c.address, nil); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if !code {
|
} else if len(code) == 0 {
|
||||||
return nil, ErrNoCode
|
return nil, ErrNoCode
|
||||||
}
|
}
|
||||||
atomic.StoreUint32(&c.pendingHasCode, 1)
|
atomic.StoreUint32(&c.pendingHasCode, 1)
|
||||||
}
|
}
|
||||||
// If the contract surely has code (or code is not needed), estimate the transaction
|
// If the contract surely has code (or code is not needed), estimate the transaction
|
||||||
gasLimit, err = c.transactor.EstimateGasLimit(opts.Context, opts.From, contract, value, input)
|
callMsg := ethereum.CallMsg{
|
||||||
|
From: opts.From,
|
||||||
|
To: *contract,
|
||||||
|
GasPrice: gasPrice,
|
||||||
|
Value: value,
|
||||||
|
Data: input,
|
||||||
|
}
|
||||||
|
gasLimit, err = c.transactor.EstimateGas(opts.Context, callMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to exstimate gas needed: %v", err)
|
return nil, fmt.Errorf("failed to estimate gas needed: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Create the transaction, sign it and schedule it for execution
|
// Create the transaction, sign it and schedule it for execution
|
||||||
|
|
|
||||||
168
interfaces.go
Normal file
168
interfaces.go
Normal file
|
|
@ -0,0 +1,168 @@
|
||||||
|
// 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 ethereum defines interfaces for interacting with Ethereum.
|
||||||
|
package ethereum
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/core/vm"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: move subscription to package event
|
||||||
|
|
||||||
|
// Subscription represents an event subscription where events are
|
||||||
|
// delivered on a data channel.
|
||||||
|
type Subscription interface {
|
||||||
|
// Unsubscribe cancels the sending of events to the data channel
|
||||||
|
// and closes the error channel.
|
||||||
|
Unsubscribe()
|
||||||
|
// Err returns the subscription error channel. The error channel receives
|
||||||
|
// a value if there is an issue with the subscription (e.g. the network connection
|
||||||
|
// delivering the events has been closed). Only one value will ever be sent.
|
||||||
|
// The error channel is closed by Unsubscribe.
|
||||||
|
Err() <-chan error
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChainReader provides access to the blockchain. The methods in this interface access raw
|
||||||
|
// data from either the canonical chain (when requesting by block number) or any
|
||||||
|
// blockchain fork that was previously downloaded and processed by the node. The block
|
||||||
|
// number argument can be nil to select the latest canonical block. Reading block headers
|
||||||
|
// should be preferred over full blocks whenever possible.
|
||||||
|
type ChainReader interface {
|
||||||
|
BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
|
||||||
|
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
|
||||||
|
HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
|
||||||
|
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
|
||||||
|
TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error)
|
||||||
|
TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error)
|
||||||
|
TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, error)
|
||||||
|
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChainStateReader wraps access to the state trie of the canonical blockchain. Note that
|
||||||
|
// implementations of the interface may be unable to return state values for old blocks.
|
||||||
|
// In many cases, using CallContract can be preferable to reading raw contract storage.
|
||||||
|
type ChainStateReader interface {
|
||||||
|
BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
|
||||||
|
StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error)
|
||||||
|
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
|
||||||
|
NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A ChainHeadEventer returns notifications whenever the canonical head block is updated.
|
||||||
|
type ChainHeadEventer interface {
|
||||||
|
SubscribeNewHeaders(ctx context.Context, ch chan<- *types.Header) (Subscription, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CallMsg contains parameters for contract calls.
|
||||||
|
type CallMsg struct {
|
||||||
|
From common.Address // the sender of the 'transaction'
|
||||||
|
To common.Address // the destination contract
|
||||||
|
Gas *big.Int // if nil, the call executes with near-infinite gas
|
||||||
|
GasPrice *big.Int // wei <-> gas exchange ratio
|
||||||
|
Value *big.Int // amount of wei sent along with the call
|
||||||
|
Data []byte // input data, usually an ABI-encoded contract method invocation
|
||||||
|
}
|
||||||
|
|
||||||
|
// A ContractCaller provides contract calls, essentially transactions that are executed by
|
||||||
|
// the EVM but not mined into the blockchain. ContractCall is a low-level method to
|
||||||
|
// execute such calls. For applications which are structured around specific contracts,
|
||||||
|
// the abigen tool provides a nicer, properly typed way to perform calls.
|
||||||
|
type ContractCaller interface {
|
||||||
|
CallContract(ctx context.Context, call CallMsg, blockNumber *big.Int) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FilterQuery contains options for contact log filtering.
|
||||||
|
type FilterQuery struct {
|
||||||
|
FromBlock *big.Int // beginning of the queried range, nil means genesis block
|
||||||
|
ToBlock *big.Int // end of the range, nil means latest block
|
||||||
|
Addresses []common.Address // restricts matches to events created by specific contracts
|
||||||
|
|
||||||
|
// The Topic list restricts matches to particular event topics. Each event has a list
|
||||||
|
// of topics. Topics matches a prefix of that list. An empty element slice matches any
|
||||||
|
// topic. Non-empty elements represent an alternative that matches any of the
|
||||||
|
// contained topics.
|
||||||
|
//
|
||||||
|
// Examples:
|
||||||
|
// {} or nil matches any topic list
|
||||||
|
// {{A}} matches topic A in first position
|
||||||
|
// {{}, {B}} matches any topic in first position, B in second position
|
||||||
|
// {{A}}, {B}} matches topic A in first position, B in second position
|
||||||
|
// {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position
|
||||||
|
Topics [][]common.Hash
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogFilterer provides access to contract log events using a one-off query or continuous
|
||||||
|
// event subscription.
|
||||||
|
type LogFilterer interface {
|
||||||
|
FilterLogs(ctx context.Context, q FilterQuery) ([]vm.Log, error)
|
||||||
|
SubscribeFilterLogs(ctx context.Context, q FilterQuery, ch chan<- vm.Log) (Subscription, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransactionSender wraps transaction sending. The SendTransaction method injects a
|
||||||
|
// signed transaction into the pending transaction pool for execution. If the transaction
|
||||||
|
// was a contract creation, the TransactionReceipt method can be used to retrieve the
|
||||||
|
// contract address after the transaction has been mined.
|
||||||
|
//
|
||||||
|
// The transaction must be signed and have a valid nonce to be included. Consumers of the
|
||||||
|
// API can use package accounts to maintain local private keys and need can retrieve the
|
||||||
|
// next available nonce using PendingNonceAt.
|
||||||
|
type TransactionSender interface {
|
||||||
|
SendTransaction(ctx context.Context, tx *types.Transaction) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// GasPricer wraps the gas price oracle, which monitors the blockchain to determine the
|
||||||
|
// optimal gas price given current fee market conditions.
|
||||||
|
type GasPricer interface {
|
||||||
|
SuggestGasPrice(ctx context.Context) (*big.Int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A PendingStateReader provides access to the pending state, which is the result of all
|
||||||
|
// known executable transactions which have not yet been included in the blockchain. It is
|
||||||
|
// commonly used to display the result of ’unconfirmed’ actions (e.g. wallet value
|
||||||
|
// transfers) initiated by the user. The PendingNonceAt operation is a good way to
|
||||||
|
// retrieve the next available transaction nonce for a specific account.
|
||||||
|
type PendingStateReader interface {
|
||||||
|
PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error)
|
||||||
|
PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error)
|
||||||
|
PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
|
||||||
|
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
|
||||||
|
PendingTransactionCount(ctx context.Context) (uint, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PendingContractCaller can be used to perform calls against the pending state.
|
||||||
|
type PendingContractCaller interface {
|
||||||
|
PendingCallContract(ctx context.Context, call CallMsg) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GasEstimator wraps EstimateGas, which tries to estimate the gas needed to execute a
|
||||||
|
// specific transaction based on the pending state. There is no guarantee that this is the
|
||||||
|
// true gas limit requirement as other transactions may be added or removed by miners, but
|
||||||
|
// it should provide a basis for setting a reasonable default.
|
||||||
|
type GasEstimator interface {
|
||||||
|
EstimateGas(ctx context.Context, call CallMsg) (usedGas *big.Int, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A PendingStateEventer provides access to real time notifications about changes to the
|
||||||
|
// pending state.
|
||||||
|
type PendingStateEventer interface {
|
||||||
|
SubscribePendingTransactions(ctx context.Context, ch chan<- *types.Transaction) (Subscription, error)
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue