2016-11-09 01:01:56 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2016-03-17 17:27:37 +00: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 (
|
2017-03-22 17:20:33 +00:00
|
|
|
"context"
|
2016-08-22 21:20:13 +00:00
|
|
|
"errors"
|
2016-08-22 12:01:28 +00:00
|
|
|
"fmt"
|
2016-03-17 17:27:37 +00:00
|
|
|
"math/big"
|
2016-08-22 21:20:13 +00:00
|
|
|
"sync"
|
2017-09-15 13:20:29 +00:00
|
|
|
"time"
|
2016-03-17 17:27:37 +00:00
|
|
|
|
2016-08-22 12:01:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
2016-03-21 12:34:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2016-03-17 17:27:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 21:21:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2017-04-04 22:16:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2016-03-17 17:27:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-01-05 10:39:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/bloombits"
|
2018-05-07 11:35:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2016-03-17 17:27:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2016-03-01 22:32:43 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2018-01-05 10:39:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/filters"
|
2016-03-17 17:27:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2018-01-05 10:39:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2016-10-20 11:36:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2018-01-05 10:39:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2016-03-17 17:27:37 +00:00
|
|
|
)
|
|
|
|
|
2016-03-21 12:34:49 +00:00
|
|
|
// This nil assignment ensures compile time that SimulatedBackend implements bind.ContractBackend.
|
|
|
|
var _ bind.ContractBackend = (*SimulatedBackend)(nil)
|
|
|
|
|
2016-08-22 21:20:13 +00:00
|
|
|
var errBlockNumberUnsupported = errors.New("SimulatedBackend cannot access blocks other than the latest block")
|
2017-11-14 16:26:31 +00:00
|
|
|
var errGasEstimationFailed = errors.New("gas required exceeds allowance or always failing transaction")
|
2016-08-22 21:20:13 +00:00
|
|
|
|
2016-03-17 17:27:37 +00: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-22 21:20:13 +00:00
|
|
|
mu sync.Mutex
|
2016-03-17 17:27:37 +00: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 11:36:29 +00:00
|
|
|
|
2018-01-05 10:39:24 +00:00
|
|
|
events *filters.EventSystem // Event system for filtering log events live
|
|
|
|
|
2016-10-20 11:36:29 +00:00
|
|
|
config *params.ChainConfig
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSimulatedBackend creates a new binding backend using a simulated blockchain
|
|
|
|
// for testing purposes.
|
2018-08-15 07:15:42 +00:00
|
|
|
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
|
2018-09-24 12:57:49 +00:00
|
|
|
database := rawdb.NewMemoryDatabase()
|
2018-08-15 07:15:42 +00:00
|
|
|
genesis := core.Genesis{Config: params.AllEthashProtocolChanges, GasLimit: gasLimit, Alloc: alloc}
|
2017-03-02 13:03:33 +00:00
|
|
|
genesis.MustCommit(database)
|
2018-09-20 12:09:30 +00:00
|
|
|
blockchain, _ := core.NewBlockChain(database, nil, genesis.Config, ethash.NewFaker(), vm.Config{}, nil)
|
2018-01-05 10:39:24 +00:00
|
|
|
|
|
|
|
backend := &SimulatedBackend{
|
|
|
|
database: database,
|
|
|
|
blockchain: blockchain,
|
|
|
|
config: genesis.Config,
|
|
|
|
events: filters.NewEventSystem(new(event.TypeMux), &filterBackend{database, blockchain}, false),
|
|
|
|
}
|
2016-08-22 21:20:13 +00:00
|
|
|
backend.rollback()
|
2016-03-17 17:27:37 +00: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-22 21:20:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-03-17 17:27:37 +00: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-22 21:20:13 +00:00
|
|
|
b.rollback()
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback aborts all pending transactions, reverting to the last committed state.
|
|
|
|
func (b *SimulatedBackend) Rollback() {
|
2016-08-22 21:20:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
b.rollback()
|
|
|
|
}
|
2016-03-17 17:27:37 +00:00
|
|
|
|
2016-08-22 21:20:13 +00:00
|
|
|
func (b *SimulatedBackend) rollback() {
|
2017-12-22 12:37:50 +00:00
|
|
|
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, 1, func(int, *core.BlockGen) {})
|
2018-02-05 16:40:32 +00:00
|
|
|
statedb, _ := b.blockchain.State()
|
|
|
|
|
2016-03-17 17:27:37 +00:00
|
|
|
b.pendingBlock = blocks[0]
|
2018-02-05 16:40:32 +00:00
|
|
|
b.pendingState, _ = state.New(b.pendingBlock.Root(), statedb.Database())
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 21:20:13 +00:00
|
|
|
// CodeAt returns the code associated with a certain account in the blockchain.
|
2016-08-22 12:01:28 +00:00
|
|
|
func (b *SimulatedBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
|
2016-08-22 21:20:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-08-22 12:01:28 +00:00
|
|
|
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
|
2016-08-22 21:20:13 +00:00
|
|
|
return nil, errBlockNumberUnsupported
|
2016-05-20 09:29:28 +00:00
|
|
|
}
|
|
|
|
statedb, _ := b.blockchain.State()
|
2016-08-22 12:01:28 +00:00
|
|
|
return statedb.GetCode(contract), nil
|
2016-05-20 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 21:20:13 +00: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 19:04:58 +00:00
|
|
|
val := statedb.GetState(contract, key)
|
|
|
|
return val[:], nil
|
2016-08-22 21:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TransactionReceipt returns the receipt of a transaction.
|
|
|
|
func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
|
2018-05-07 11:35:06 +00:00
|
|
|
receipt, _, _, _ := rawdb.ReadReceipt(b.database, txHash)
|
2017-07-14 16:39:53 +00:00
|
|
|
return receipt, nil
|
2016-08-22 21:20:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 23:23:10 +00:00
|
|
|
// TransactionByHash checks the pool of pending transactions in addition to the
|
|
|
|
// blockchain. The isPending return value indicates whether the transaction has been
|
|
|
|
// mined yet. Note that the transaction may not be part of the canonical chain even if
|
|
|
|
// it's not pending.
|
2019-02-20 07:08:54 +00:00
|
|
|
func (b *SimulatedBackend) TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2019-02-12 23:23:10 +00:00
|
|
|
|
2019-02-20 07:08:54 +00:00
|
|
|
tx := b.pendingBlock.Transaction(txHash)
|
2019-02-12 23:23:10 +00:00
|
|
|
if tx != nil {
|
|
|
|
return tx, true, nil
|
|
|
|
}
|
|
|
|
tx, _, _, _ = rawdb.ReadTransaction(b.database, txHash)
|
|
|
|
if tx != nil {
|
|
|
|
return tx, false, nil
|
|
|
|
}
|
|
|
|
return nil, false, ethereum.NotFound
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:20:13 +00:00
|
|
|
// PendingCodeAt returns the code associated with an account in the pending state.
|
2016-08-22 12:01:28 +00:00
|
|
|
func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
|
2016-08-22 21:20:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-08-22 12:01:28 +00:00
|
|
|
return b.pendingState.GetCode(contract), nil
|
|
|
|
}
|
2016-03-17 17:27:37 +00:00
|
|
|
|
2016-08-22 12:01:28 +00:00
|
|
|
// CallContract executes a contract call.
|
|
|
|
func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
|
2016-08-22 21:20:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-08-22 12:01:28 +00:00
|
|
|
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
|
2016-08-22 21:20:13 +00:00
|
|
|
return nil, errBlockNumberUnsupported
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
2016-08-22 12:01:28 +00:00
|
|
|
state, err := b.blockchain.State()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-24 03:18:21 +00:00
|
|
|
rval, _, _, err := b.callContract(ctx, call, b.blockchain.CurrentBlock(), state)
|
2016-08-22 12:01:28 +00:00
|
|
|
return rval, err
|
|
|
|
}
|
2016-03-17 17:27:37 +00:00
|
|
|
|
2016-08-22 12:01:28 +00:00
|
|
|
// PendingCallContract executes a contract call on the pending state.
|
|
|
|
func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) {
|
2016-08-22 21:20:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2016-10-04 10:36:02 +00:00
|
|
|
defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot())
|
2016-08-22 21:20:13 +00:00
|
|
|
|
2017-08-24 03:18:21 +00:00
|
|
|
rval, _, _, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
|
2016-08-22 12:01:28 +00:00
|
|
|
return rval, err
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 12:01:28 +00:00
|
|
|
// PendingNonceAt implements PendingStateReader.PendingNonceAt, retrieving
|
2016-03-21 12:34:49 +00:00
|
|
|
// the nonce currently pending for the account.
|
2016-08-22 12:01:28 +00:00
|
|
|
func (b *SimulatedBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
|
2016-08-22 21:20:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-03-17 17:27:37 +00:00
|
|
|
return b.pendingState.GetOrNewStateObject(account).Nonce(), nil
|
|
|
|
}
|
|
|
|
|
2016-03-21 12:34:49 +00:00
|
|
|
// SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated
|
2018-09-28 20:47:46 +00:00
|
|
|
// chain doesn't have miners, we just return a gas price of 1 for any call.
|
2015-12-16 03:26:23 +00:00
|
|
|
func (b *SimulatedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
|
2016-03-17 17:27:37 +00:00
|
|
|
return big.NewInt(1), nil
|
|
|
|
}
|
|
|
|
|
2016-08-22 12:01:28 +00:00
|
|
|
// EstimateGas executes the requested code against the currently pending block/state and
|
|
|
|
// returns the used amount of gas.
|
2017-11-13 11:47:27 +00:00
|
|
|
func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error) {
|
2016-08-22 21:20:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2017-11-14 16:26:31 +00:00
|
|
|
// Determine the lowest and highest possible gas limits to binary search in between
|
2017-08-24 03:18:21 +00:00
|
|
|
var (
|
2017-11-14 16:26:31 +00:00
|
|
|
lo uint64 = params.TxGas - 1
|
|
|
|
hi uint64
|
|
|
|
cap uint64
|
2017-08-24 03:18:21 +00:00
|
|
|
)
|
2017-11-13 11:47:27 +00:00
|
|
|
if call.Gas >= params.TxGas {
|
|
|
|
hi = call.Gas
|
2017-01-20 22:39:16 +00:00
|
|
|
} else {
|
2017-11-13 11:47:27 +00:00
|
|
|
hi = b.pendingBlock.GasLimit()
|
2017-01-20 22:39:16 +00:00
|
|
|
}
|
2017-11-14 16:26:31 +00:00
|
|
|
cap = hi
|
|
|
|
|
|
|
|
// Create a helper to check if a gas allowance results in an executable transaction
|
|
|
|
executable := func(gas uint64) bool {
|
2017-11-13 11:47:27 +00:00
|
|
|
call.Gas = gas
|
2017-01-20 22:39:16 +00:00
|
|
|
|
|
|
|
snapshot := b.pendingState.Snapshot()
|
2017-08-24 03:18:21 +00:00
|
|
|
_, _, failed, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
|
2017-01-20 22:39:16 +00:00
|
|
|
b.pendingState.RevertToSnapshot(snapshot)
|
|
|
|
|
2017-08-24 03:18:21 +00:00
|
|
|
if err != nil || failed {
|
2017-11-14 16:26:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Execute the binary search and hone in on an executable gas limit
|
|
|
|
for lo+1 < hi {
|
|
|
|
mid := (hi + lo) / 2
|
|
|
|
if !executable(mid) {
|
2017-01-20 22:39:16 +00:00
|
|
|
lo = mid
|
2017-11-14 16:26:31 +00:00
|
|
|
} else {
|
|
|
|
hi = mid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Reject the transaction as invalid if it still fails at the highest allowance
|
|
|
|
if hi == cap {
|
|
|
|
if !executable(hi) {
|
2017-11-13 11:47:27 +00:00
|
|
|
return 0, errGasEstimationFailed
|
2017-01-20 22:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-13 11:47:27 +00:00
|
|
|
return hi, nil
|
2016-08-22 12:01:28 +00:00
|
|
|
}
|
2016-03-17 17:27:37 +00:00
|
|
|
|
2018-01-05 10:39:24 +00:00
|
|
|
// callContract implements common code between normal and pending contract calls.
|
2016-08-22 12:01:28 +00:00
|
|
|
// state is modified during execution, make sure to copy it if necessary.
|
2017-11-13 11:47:27 +00:00
|
|
|
func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.StateDB) ([]byte, uint64, bool, error) {
|
2016-08-22 12:01:28 +00:00
|
|
|
// Ensure message is initialized properly.
|
|
|
|
if call.GasPrice == nil {
|
|
|
|
call.GasPrice = big.NewInt(1)
|
|
|
|
}
|
2017-11-13 11:47:27 +00:00
|
|
|
if call.Gas == 0 {
|
|
|
|
call.Gas = 50000000
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
2016-08-22 12:01:28 +00:00
|
|
|
if call.Value == nil {
|
|
|
|
call.Value = new(big.Int)
|
|
|
|
}
|
|
|
|
// Set infinite balance to the fake caller account.
|
|
|
|
from := statedb.GetOrNewStateObject(call.From)
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 21:21:51 +00:00
|
|
|
from.SetBalance(math.MaxBig256)
|
2016-08-22 12:01:28 +00:00
|
|
|
// Execute the call.
|
|
|
|
msg := callmsg{call}
|
2016-12-06 01:16:03 +00:00
|
|
|
|
2017-04-12 13:38:31 +00:00
|
|
|
evmContext := core.NewEVMContext(msg, block.Header(), b.blockchain, nil)
|
2016-12-06 01:16:03 +00:00
|
|
|
// Create a new environment which holds all relevant information
|
|
|
|
// about the transaction and calling mechanisms.
|
2017-03-02 13:03:33 +00:00
|
|
|
vmenv := vm.NewEVM(evmContext, statedb, b.config, vm.Config{})
|
2017-11-13 11:47:27 +00:00
|
|
|
gaspool := new(core.GasPool).AddGas(math.MaxUint64)
|
|
|
|
|
|
|
|
return core.NewStateTransition(vmenv, msg, gaspool).TransitionDb()
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 12:01:28 +00:00
|
|
|
// SendTransaction updates the pending block to include the given transaction.
|
|
|
|
// It panics if the transaction is invalid.
|
2015-12-16 03:26:23 +00:00
|
|
|
func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
|
2016-08-22 21:20:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
sender, err := types.Sender(types.HomesteadSigner{}, tx)
|
2016-08-22 12:01:28 +00: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))
|
|
|
|
}
|
|
|
|
|
2017-12-22 12:37:50 +00:00
|
|
|
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
|
2016-03-17 17:27:37 +00:00
|
|
|
for _, tx := range b.pendingBlock.Transactions() {
|
2018-01-29 10:47:08 +00:00
|
|
|
block.AddTxWithChain(b.blockchain, tx)
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
2018-01-29 10:47:08 +00:00
|
|
|
block.AddTxWithChain(b.blockchain, tx)
|
2016-03-17 17:27:37 +00:00
|
|
|
})
|
2018-02-05 16:40:32 +00:00
|
|
|
statedb, _ := b.blockchain.State()
|
|
|
|
|
2016-03-17 17:27:37 +00:00
|
|
|
b.pendingBlock = blocks[0]
|
2018-02-05 16:40:32 +00:00
|
|
|
b.pendingState, _ = state.New(b.pendingBlock.Root(), statedb.Database())
|
2016-03-17 17:27:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-05 10:39:24 +00:00
|
|
|
// FilterLogs executes a log filter operation, blocking during execution and
|
|
|
|
// returning all the results in one batch.
|
|
|
|
//
|
|
|
|
// TODO(karalabe): Deprecate when the subscription one can return past data too.
|
|
|
|
func (b *SimulatedBackend) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) {
|
2018-07-12 14:36:07 +00:00
|
|
|
var filter *filters.Filter
|
|
|
|
if query.BlockHash != nil {
|
|
|
|
// Block filter requested, construct a single-shot filter
|
|
|
|
filter = filters.NewBlockFilter(&filterBackend{b.database, b.blockchain}, *query.BlockHash, query.Addresses, query.Topics)
|
|
|
|
} else {
|
|
|
|
// Initialize unset filter boundaried to run from genesis to chain head
|
|
|
|
from := int64(0)
|
|
|
|
if query.FromBlock != nil {
|
|
|
|
from = query.FromBlock.Int64()
|
|
|
|
}
|
|
|
|
to := int64(-1)
|
|
|
|
if query.ToBlock != nil {
|
|
|
|
to = query.ToBlock.Int64()
|
|
|
|
}
|
|
|
|
// Construct the range filter
|
|
|
|
filter = filters.NewRangeFilter(&filterBackend{b.database, b.blockchain}, from, to, query.Addresses, query.Topics)
|
2018-01-05 10:39:24 +00:00
|
|
|
}
|
2018-07-12 14:36:07 +00:00
|
|
|
// Run the filter and return all the logs
|
2018-01-05 10:39:24 +00:00
|
|
|
logs, err := filter.Logs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res := make([]types.Log, len(logs))
|
|
|
|
for i, log := range logs {
|
|
|
|
res[i] = *log
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeFilterLogs creates a background log filtering operation, returning a
|
|
|
|
// subscription immediately, which can be used to stream the found events.
|
|
|
|
func (b *SimulatedBackend) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
|
|
|
|
// Subscribe to contract events
|
|
|
|
sink := make(chan []*types.Log)
|
|
|
|
|
|
|
|
sub, err := b.events.SubscribeLogs(query, sink)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Since we're getting logs in batches, we need to flatten them into a plain stream
|
|
|
|
return event.NewSubscription(func(quit <-chan struct{}) error {
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case logs := <-sink:
|
|
|
|
for _, log := range logs {
|
|
|
|
select {
|
|
|
|
case ch <- *log:
|
|
|
|
case err := <-sub.Err():
|
|
|
|
return err
|
|
|
|
case <-quit:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case err := <-sub.Err():
|
|
|
|
return err
|
|
|
|
case <-quit:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AdjustTime adds a time shift to the simulated clock.
|
2017-09-15 13:20:29 +00:00
|
|
|
func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2017-12-22 12:37:50 +00:00
|
|
|
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
|
2017-09-15 13:20:29 +00:00
|
|
|
for _, tx := range b.pendingBlock.Transactions() {
|
|
|
|
block.AddTx(tx)
|
|
|
|
}
|
|
|
|
block.OffsetTime(int64(adjustment.Seconds()))
|
|
|
|
})
|
2018-02-05 16:40:32 +00:00
|
|
|
statedb, _ := b.blockchain.State()
|
|
|
|
|
2017-09-15 13:20:29 +00:00
|
|
|
b.pendingBlock = blocks[0]
|
2018-02-05 16:40:32 +00:00
|
|
|
b.pendingState, _ = state.New(b.pendingBlock.Root(), statedb.Database())
|
2017-09-15 13:20:29 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:27:37 +00:00
|
|
|
// callmsg implements core.Message to allow passing it as a transaction simulator.
|
|
|
|
type callmsg struct {
|
2016-08-22 12:01:28 +00:00
|
|
|
ethereum.CallMsg
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 12:44:13 +00: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 }
|
2017-11-13 11:47:27 +00:00
|
|
|
func (m callmsg) Gas() uint64 { return m.CallMsg.Gas }
|
2016-11-02 12:44:13 +00:00
|
|
|
func (m callmsg) Value() *big.Int { return m.CallMsg.Value }
|
|
|
|
func (m callmsg) Data() []byte { return m.CallMsg.Data }
|
2018-01-05 10:39:24 +00:00
|
|
|
|
|
|
|
// filterBackend implements filters.Backend to support filtering for logs without
|
|
|
|
// taking bloom-bits acceleration structures into account.
|
|
|
|
type filterBackend struct {
|
|
|
|
db ethdb.Database
|
|
|
|
bc *core.BlockChain
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fb *filterBackend) ChainDb() ethdb.Database { return fb.db }
|
|
|
|
func (fb *filterBackend) EventMux() *event.TypeMux { panic("not supported") }
|
|
|
|
|
|
|
|
func (fb *filterBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumber) (*types.Header, error) {
|
|
|
|
if block == rpc.LatestBlockNumber {
|
|
|
|
return fb.bc.CurrentHeader(), nil
|
|
|
|
}
|
|
|
|
return fb.bc.GetHeaderByNumber(uint64(block.Int64())), nil
|
|
|
|
}
|
2018-02-22 10:48:14 +00:00
|
|
|
|
2018-07-12 14:36:07 +00:00
|
|
|
func (fb *filterBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
|
|
|
|
return fb.bc.GetHeaderByHash(hash), nil
|
|
|
|
}
|
|
|
|
|
2018-01-05 10:39:24 +00:00
|
|
|
func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
|
2018-05-07 11:35:06 +00:00
|
|
|
number := rawdb.ReadHeaderNumber(fb.db, hash)
|
|
|
|
if number == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return rawdb.ReadReceipts(fb.db, hash, *number), nil
|
2018-01-05 10:39:24 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 10:48:14 +00:00
|
|
|
func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
|
2018-05-07 11:35:06 +00:00
|
|
|
number := rawdb.ReadHeaderNumber(fb.db, hash)
|
|
|
|
if number == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
receipts := rawdb.ReadReceipts(fb.db, hash, *number)
|
2018-02-22 10:48:14 +00:00
|
|
|
if receipts == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
logs := make([][]*types.Log, len(receipts))
|
|
|
|
for i, receipt := range receipts {
|
|
|
|
logs[i] = receipt.Logs
|
|
|
|
}
|
|
|
|
return logs, nil
|
|
|
|
}
|
|
|
|
|
2018-05-18 08:45:52 +00:00
|
|
|
func (fb *filterBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
|
2018-01-05 10:39:24 +00:00
|
|
|
return event.NewSubscription(func(quit <-chan struct{}) error {
|
|
|
|
<-quit
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
func (fb *filterBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
|
|
|
|
return fb.bc.SubscribeChainEvent(ch)
|
|
|
|
}
|
|
|
|
func (fb *filterBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
|
|
|
|
return fb.bc.SubscribeRemovedLogsEvent(ch)
|
|
|
|
}
|
|
|
|
func (fb *filterBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
|
|
|
|
return fb.bc.SubscribeLogsEvent(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fb *filterBackend) BloomStatus() (uint64, uint64) { return 4096, 0 }
|
|
|
|
func (fb *filterBackend) ServiceFilter(ctx context.Context, ms *bloombits.MatcherSession) {
|
|
|
|
panic("not supported")
|
|
|
|
}
|