2016-11-09 04:01:56 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2016-03-17 19:27:37 +02:00
|
|
|
// 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 (
|
2016-08-23 00:20:13 +03:00
|
|
|
"errors"
|
2016-08-22 15:01:28 +03:00
|
|
|
"fmt"
|
2016-03-17 19:27:37 +02:00
|
|
|
"math/big"
|
2016-08-23 00:20:13 +03:00
|
|
|
"sync"
|
2016-03-17 19:27:37 +02:00
|
|
|
|
2016-08-22 15:01:28 +03:00
|
|
|
"github.com/ethereum/go-ethereum"
|
2016-03-21 14:34:49 +02:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2016-03-17 19:27:37 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2016-03-02 00:32:43 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2016-03-17 19:27:37 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2016-10-20 14:36:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-12-16 05:26:23 +02:00
|
|
|
"golang.org/x/net/context"
|
2016-03-17 19:27:37 +02:00
|
|
|
)
|
|
|
|
|
2016-03-02 00:32:43 +02:00
|
|
|
// Default chain configuration which sets homestead phase at block 0 (i.e. no frontier)
|
2016-10-20 14:36:29 +03:00
|
|
|
var chainConfig = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(0), EIP150Block: new(big.Int), EIP158Block: new(big.Int)}
|
2016-03-02 00:32:43 +02:00
|
|
|
|
2016-03-21 14:34:49 +02:00
|
|
|
// This nil assignment ensures compile time that SimulatedBackend implements bind.ContractBackend.
|
|
|
|
var _ bind.ContractBackend = (*SimulatedBackend)(nil)
|
|
|
|
|
2016-08-23 00:20:13 +03:00
|
|
|
var errBlockNumberUnsupported = errors.New("SimulatedBackend cannot access blocks other than the latest block")
|
|
|
|
|
2016-03-17 19:27:37 +02:00
|
|
|
// SimulatedBackend implements bind.ContractBackend, simulating a blockchain in
|
|
|
|
// the background. Its main purpose is to allow easily testing contract bindings.
|
|
|
|
type SimulatedBackend struct {
|
|
|
|
database ethdb.Database // In memory database to store our testing data
|
|
|
|
blockchain *core.BlockChain // Ethereum blockchain to handle the consensus
|
|
|
|
|
2016-08-23 00:20:13 +03:00
|
|
|
mu sync.Mutex
|
2016-03-17 19:27:37 +02:00
|
|
|
pendingBlock *types.Block // Currently pending block that will be imported on request
|
|
|
|
pendingState *state.StateDB // Currently pending state that will be the active on on request
|
2016-10-20 14:36:29 +03:00
|
|
|
|
|
|
|
config *params.ChainConfig
|
2016-03-17 19:27:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSimulatedBackend creates a new binding backend using a simulated blockchain
|
|
|
|
// for testing purposes.
|
|
|
|
func NewSimulatedBackend(accounts ...core.GenesisAccount) *SimulatedBackend {
|
|
|
|
database, _ := ethdb.NewMemDatabase()
|
|
|
|
core.WriteGenesisBlockForTesting(database, accounts...)
|
2017-01-17 14:19:50 +03:00
|
|
|
blockchain, _ := core.NewBlockChain(database, chainConfig, new(core.FakePow), new(event.TypeMux), vm.Config{})
|
2016-08-23 00:20:13 +03:00
|
|
|
backend := &SimulatedBackend{database: database, blockchain: blockchain}
|
|
|
|
backend.rollback()
|
2016-03-17 19:27:37 +02:00
|
|
|
return backend
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit imports all the pending transactions as a single block and starts a
|
|
|
|
// fresh new state.
|
|
|
|
func (b *SimulatedBackend) Commit() {
|
2016-08-23 00:20:13 +03:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-03-17 19:27:37 +02:00
|
|
|
if _, err := b.blockchain.InsertChain([]*types.Block{b.pendingBlock}); err != nil {
|
|
|
|
panic(err) // This cannot happen unless the simulator is wrong, fail in that case
|
|
|
|
}
|
2016-08-23 00:20:13 +03:00
|
|
|
b.rollback()
|
2016-03-17 19:27:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback aborts all pending transactions, reverting to the last committed state.
|
|
|
|
func (b *SimulatedBackend) Rollback() {
|
2016-08-23 00:20:13 +03:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
b.rollback()
|
|
|
|
}
|
2016-03-17 19:27:37 +02:00
|
|
|
|
2016-08-23 00:20:13 +03:00
|
|
|
func (b *SimulatedBackend) rollback() {
|
2016-10-20 14:36:29 +03:00
|
|
|
blocks, _ := core.GenerateChain(chainConfig, b.blockchain.CurrentBlock(), b.database, 1, func(int, *core.BlockGen) {})
|
2016-03-17 19:27:37 +02:00
|
|
|
b.pendingBlock = blocks[0]
|
|
|
|
b.pendingState, _ = state.New(b.pendingBlock.Root(), b.database)
|
|
|
|
}
|
|
|
|
|
2016-08-23 00:20:13 +03:00
|
|
|
// CodeAt returns the code associated with a certain account in the blockchain.
|
2016-08-22 15:01:28 +03:00
|
|
|
func (b *SimulatedBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
|
2016-08-23 00:20:13 +03:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-08-22 15:01:28 +03:00
|
|
|
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
|
2016-08-23 00:20:13 +03:00
|
|
|
return nil, errBlockNumberUnsupported
|
2016-05-20 12:29:28 +03:00
|
|
|
}
|
|
|
|
statedb, _ := b.blockchain.State()
|
2016-08-22 15:01:28 +03:00
|
|
|
return statedb.GetCode(contract), nil
|
2016-05-20 12:29:28 +03:00
|
|
|
}
|
|
|
|
|
2016-08-23 00:20:13 +03:00
|
|
|
// BalanceAt returns the wei balance of a certain account in the blockchain.
|
|
|
|
func (b *SimulatedBackend) BalanceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (*big.Int, error) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
|
|
|
|
return nil, errBlockNumberUnsupported
|
|
|
|
}
|
|
|
|
statedb, _ := b.blockchain.State()
|
|
|
|
return statedb.GetBalance(contract), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NonceAt returns the nonce of a certain account in the blockchain.
|
|
|
|
func (b *SimulatedBackend) NonceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (uint64, error) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
|
|
|
|
return 0, errBlockNumberUnsupported
|
|
|
|
}
|
|
|
|
statedb, _ := b.blockchain.State()
|
|
|
|
return statedb.GetNonce(contract), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageAt returns the value of key in the storage of an account in the blockchain.
|
|
|
|
func (b *SimulatedBackend) StorageAt(ctx context.Context, contract common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
|
|
|
|
return nil, errBlockNumberUnsupported
|
|
|
|
}
|
|
|
|
statedb, _ := b.blockchain.State()
|
2016-09-22 22:04:58 +03:00
|
|
|
val := statedb.GetState(contract, key)
|
|
|
|
return val[:], nil
|
2016-08-23 00:20:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TransactionReceipt returns the receipt of a transaction.
|
|
|
|
func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
|
|
|
|
return core.GetReceipt(b.database, txHash), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PendingCodeAt returns the code associated with an account in the pending state.
|
2016-08-22 15:01:28 +03:00
|
|
|
func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
|
2016-08-23 00:20:13 +03:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-08-22 15:01:28 +03:00
|
|
|
return b.pendingState.GetCode(contract), nil
|
|
|
|
}
|
2016-03-17 19:27:37 +02:00
|
|
|
|
2016-08-22 15:01:28 +03:00
|
|
|
// CallContract executes a contract call.
|
|
|
|
func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
|
2016-08-23 00:20:13 +03:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-08-22 15:01:28 +03:00
|
|
|
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
|
2016-08-23 00:20:13 +03:00
|
|
|
return nil, errBlockNumberUnsupported
|
2016-03-17 19:27:37 +02:00
|
|
|
}
|
2016-08-22 15:01:28 +03:00
|
|
|
state, err := b.blockchain.State()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rval, _, err := b.callContract(ctx, call, b.blockchain.CurrentBlock(), state)
|
|
|
|
return rval, err
|
|
|
|
}
|
2016-03-17 19:27:37 +02:00
|
|
|
|
2016-08-22 15:01:28 +03:00
|
|
|
// PendingCallContract executes a contract call on the pending state.
|
|
|
|
func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) {
|
2016-08-23 00:20:13 +03:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2016-10-04 13:36:02 +03:00
|
|
|
defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot())
|
2016-08-23 00:20:13 +03:00
|
|
|
|
2016-10-04 13:36:02 +03:00
|
|
|
rval, _, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
|
2016-08-22 15:01:28 +03:00
|
|
|
return rval, err
|
2016-03-17 19:27:37 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 15:01:28 +03:00
|
|
|
// PendingNonceAt implements PendingStateReader.PendingNonceAt, retrieving
|
2016-03-21 14:34:49 +02:00
|
|
|
// the nonce currently pending for the account.
|
2016-08-22 15:01:28 +03:00
|
|
|
func (b *SimulatedBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
|
2016-08-23 00:20:13 +03:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-03-17 19:27:37 +02:00
|
|
|
return b.pendingState.GetOrNewStateObject(account).Nonce(), nil
|
|
|
|
}
|
|
|
|
|
2016-03-21 14:34:49 +02:00
|
|
|
// SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated
|
|
|
|
// chain doens't have miners, we just return a gas price of 1 for any call.
|
2015-12-16 05:26:23 +02:00
|
|
|
func (b *SimulatedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
|
2016-03-17 19:27:37 +02:00
|
|
|
return big.NewInt(1), nil
|
|
|
|
}
|
|
|
|
|
2016-08-22 15:01:28 +03:00
|
|
|
// EstimateGas executes the requested code against the currently pending block/state and
|
|
|
|
// returns the used amount of gas.
|
|
|
|
func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (*big.Int, error) {
|
2016-08-23 00:20:13 +03:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2016-10-04 13:36:02 +03:00
|
|
|
defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot())
|
2016-08-23 00:20:13 +03:00
|
|
|
|
2016-10-04 13:36:02 +03:00
|
|
|
_, gas, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
|
2016-08-22 15:01:28 +03:00
|
|
|
return gas, err
|
|
|
|
}
|
2016-03-17 19:27:37 +02:00
|
|
|
|
2016-08-22 15:01:28 +03:00
|
|
|
// callContract implemens common code between normal and pending contract calls.
|
|
|
|
// state is modified during execution, make sure to copy it if necessary.
|
|
|
|
func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.StateDB) ([]byte, *big.Int, error) {
|
|
|
|
// Ensure message is initialized properly.
|
|
|
|
if call.GasPrice == nil {
|
|
|
|
call.GasPrice = big.NewInt(1)
|
|
|
|
}
|
|
|
|
if call.Gas == nil || call.Gas.BitLen() == 0 {
|
|
|
|
call.Gas = big.NewInt(50000000)
|
2016-03-17 19:27:37 +02:00
|
|
|
}
|
2016-08-22 15:01:28 +03:00
|
|
|
if call.Value == nil {
|
|
|
|
call.Value = new(big.Int)
|
|
|
|
}
|
|
|
|
// Set infinite balance to the fake caller account.
|
|
|
|
from := statedb.GetOrNewStateObject(call.From)
|
|
|
|
from.SetBalance(common.MaxBig)
|
|
|
|
// Execute the call.
|
|
|
|
msg := callmsg{call}
|
2016-12-06 04:16:03 +03:00
|
|
|
|
|
|
|
evmContext := core.NewEVMContext(msg, block.Header(), b.blockchain)
|
|
|
|
// Create a new environment which holds all relevant information
|
|
|
|
// about the transaction and calling mechanisms.
|
2017-01-05 13:52:10 +03:00
|
|
|
vmenv := vm.NewEVM(evmContext, statedb, chainConfig, vm.Config{})
|
2016-03-17 19:27:37 +02:00
|
|
|
gaspool := new(core.GasPool).AddGas(common.MaxBig)
|
2016-08-22 15:01:28 +03:00
|
|
|
ret, gasUsed, _, err := core.NewStateTransition(vmenv, msg, gaspool).TransitionDb()
|
|
|
|
return ret, gasUsed, err
|
2016-03-17 19:27:37 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 15:01:28 +03:00
|
|
|
// SendTransaction updates the pending block to include the given transaction.
|
|
|
|
// It panics if the transaction is invalid.
|
2015-12-16 05:26:23 +02:00
|
|
|
func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
|
2016-08-23 00:20:13 +03:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-11-02 15:44:13 +03:00
|
|
|
sender, err := types.Sender(types.HomesteadSigner{}, tx)
|
2016-08-22 15:01:28 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("invalid transaction: %v", err))
|
|
|
|
}
|
|
|
|
nonce := b.pendingState.GetNonce(sender)
|
|
|
|
if tx.Nonce() != nonce {
|
|
|
|
panic(fmt.Errorf("invalid transaction nonce: got %d, want %d", tx.Nonce(), nonce))
|
|
|
|
}
|
|
|
|
|
2016-10-20 14:36:29 +03:00
|
|
|
blocks, _ := core.GenerateChain(chainConfig, b.blockchain.CurrentBlock(), b.database, 1, func(number int, block *core.BlockGen) {
|
2016-03-17 19:27:37 +02:00
|
|
|
for _, tx := range b.pendingBlock.Transactions() {
|
|
|
|
block.AddTx(tx)
|
|
|
|
}
|
|
|
|
block.AddTx(tx)
|
|
|
|
})
|
|
|
|
b.pendingBlock = blocks[0]
|
|
|
|
b.pendingState, _ = state.New(b.pendingBlock.Root(), b.database)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// callmsg implements core.Message to allow passing it as a transaction simulator.
|
|
|
|
type callmsg struct {
|
2016-08-22 15:01:28 +03:00
|
|
|
ethereum.CallMsg
|
2016-03-17 19:27:37 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 15:44:13 +03:00
|
|
|
func (m callmsg) From() common.Address { return m.CallMsg.From }
|
|
|
|
func (m callmsg) Nonce() uint64 { return 0 }
|
|
|
|
func (m callmsg) CheckNonce() bool { return false }
|
|
|
|
func (m callmsg) To() *common.Address { return m.CallMsg.To }
|
|
|
|
func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
|
|
|
|
func (m callmsg) Gas() *big.Int { return m.CallMsg.Gas }
|
|
|
|
func (m callmsg) Value() *big.Int { return m.CallMsg.Value }
|
|
|
|
func (m callmsg) Data() []byte { return m.CallMsg.Data }
|