2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity 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 General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-21 01:19:29 +01:00
|
|
|
//! Ethereum rpc interface implementation.
|
2016-02-29 11:58:33 +01:00
|
|
|
|
|
|
|
macro_rules! take_weak {
|
|
|
|
($weak: expr) => {
|
|
|
|
match $weak.upgrade() {
|
|
|
|
Some(arc) => arc,
|
|
|
|
None => return Err(Error::internal_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-29 14:53:50 +02:00
|
|
|
macro_rules! rpc_unimplemented {
|
|
|
|
() => (Err(Error::internal_error()))
|
|
|
|
}
|
|
|
|
|
2016-01-26 11:37:24 +01:00
|
|
|
mod web3;
|
|
|
|
mod eth;
|
2016-06-01 19:37:34 +02:00
|
|
|
mod eth_filter;
|
|
|
|
mod eth_signing;
|
2016-01-26 11:37:24 +01:00
|
|
|
mod net;
|
2016-03-04 12:46:54 +01:00
|
|
|
mod personal;
|
2016-06-01 19:37:34 +02:00
|
|
|
mod personal_signer;
|
2016-04-11 21:06:32 +02:00
|
|
|
mod ethcore;
|
2016-06-07 22:52:48 +02:00
|
|
|
mod ethcore_set;
|
2016-05-02 12:17:30 +02:00
|
|
|
mod traces;
|
2016-04-29 20:52:08 +02:00
|
|
|
mod rpc;
|
2016-01-21 01:19:29 +01:00
|
|
|
|
|
|
|
pub use self::web3::Web3Client;
|
2016-06-01 19:37:34 +02:00
|
|
|
pub use self::eth::EthClient;
|
|
|
|
pub use self::eth_filter::EthFilterClient;
|
|
|
|
pub use self::eth_signing::{EthSigningUnsafeClient, EthSigningQueueClient};
|
2016-01-21 11:25:39 +01:00
|
|
|
pub use self::net::NetClient;
|
2016-03-04 12:46:54 +01:00
|
|
|
pub use self::personal::PersonalClient;
|
2016-06-01 19:37:34 +02:00
|
|
|
pub use self::personal_signer::SignerClient;
|
2016-04-11 21:06:32 +02:00
|
|
|
pub use self::ethcore::EthcoreClient;
|
2016-06-07 22:52:48 +02:00
|
|
|
pub use self::ethcore_set::EthcoreSetClient;
|
2016-05-02 12:17:30 +02:00
|
|
|
pub use self::traces::TracesClient;
|
2016-04-29 20:52:08 +02:00
|
|
|
pub use self::rpc::RpcClient;
|
2016-04-11 21:06:32 +02:00
|
|
|
|
2016-05-26 21:14:01 +02:00
|
|
|
use v1::types::TransactionRequest;
|
2016-06-15 00:17:23 +02:00
|
|
|
use ethcore::error::Error as EthcoreError;
|
2016-05-31 21:31:42 +02:00
|
|
|
use ethcore::miner::{AccountDetails, MinerService};
|
|
|
|
use ethcore::client::MiningBlockChainClient;
|
2016-05-26 21:14:01 +02:00
|
|
|
use ethcore::transaction::{Action, SignedTransaction, Transaction};
|
|
|
|
use util::numbers::*;
|
|
|
|
use util::rlp::encode;
|
|
|
|
use util::bytes::ToPretty;
|
2016-06-15 00:17:23 +02:00
|
|
|
use jsonrpc_core::{Error, ErrorCode, Value, to_value};
|
2016-05-26 21:14:01 +02:00
|
|
|
|
2016-06-15 18:32:21 +02:00
|
|
|
mod error_codes {
|
|
|
|
// NOTE [ToDr] Codes from [-32099, -32000]
|
|
|
|
pub const UNSUPPORTED_REQUEST_CODE: i64 = -32000;
|
|
|
|
pub const NO_WORK_CODE: i64 = -32001;
|
|
|
|
pub const UNKNOWN_ERROR: i64 = -32002;
|
|
|
|
pub const TRANSACTION_ERROR: i64 = -32010;
|
|
|
|
pub const ACCOUNT_LOCKED: i64 = -32020;
|
|
|
|
}
|
|
|
|
|
2016-06-15 00:17:23 +02:00
|
|
|
fn dispatch_transaction<C, M>(client: &C, miner: &M, signed_transaction: SignedTransaction) -> Result<Value, Error>
|
2016-05-31 21:31:42 +02:00
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
2016-05-26 21:14:01 +02:00
|
|
|
let hash = signed_transaction.hash();
|
|
|
|
|
2016-05-29 17:07:39 +02:00
|
|
|
let import = miner.import_own_transaction(client, signed_transaction, |a: &Address| {
|
|
|
|
AccountDetails {
|
|
|
|
nonce: client.latest_nonce(&a),
|
|
|
|
balance: client.latest_balance(&a),
|
2016-05-26 21:14:01 +02:00
|
|
|
}
|
2016-05-29 17:07:39 +02:00
|
|
|
});
|
|
|
|
|
2016-06-15 00:17:23 +02:00
|
|
|
import
|
|
|
|
.map_err(transaction_error)
|
|
|
|
.and_then(|_| to_value(&hash))
|
2016-05-26 21:14:01 +02:00
|
|
|
}
|
|
|
|
|
2016-06-15 00:17:23 +02:00
|
|
|
fn sign_and_dispatch<C, M>(client: &C, miner: &M, request: TransactionRequest, secret: H256) -> Result<Value, Error>
|
2016-05-31 21:31:42 +02:00
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
2016-05-26 21:14:01 +02:00
|
|
|
|
|
|
|
let signed_transaction = {
|
|
|
|
Transaction {
|
|
|
|
nonce: request.nonce
|
|
|
|
.or_else(|| miner
|
|
|
|
.last_nonce(&request.from)
|
|
|
|
.map(|nonce| nonce + U256::one()))
|
|
|
|
.unwrap_or_else(|| client.latest_nonce(&request.from)),
|
|
|
|
|
|
|
|
action: request.to.map_or(Action::Create, Action::Call),
|
|
|
|
gas: request.gas.unwrap_or_else(|| miner.sensible_gas_limit()),
|
|
|
|
gas_price: request.gas_price.unwrap_or_else(|| miner.sensible_gas_price()),
|
|
|
|
value: request.value.unwrap_or_else(U256::zero),
|
|
|
|
data: request.data.map_or_else(Vec::new, |b| b.to_vec()),
|
|
|
|
}.sign(&secret)
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!(target: "miner", "send_transaction: dispatching tx: {}", encode(&signed_transaction).to_vec().pretty());
|
|
|
|
dispatch_transaction(&*client, &*miner, signed_transaction)
|
2016-05-31 21:31:42 +02:00
|
|
|
}
|
2016-06-15 00:17:23 +02:00
|
|
|
|
|
|
|
fn transaction_error(error: EthcoreError) -> Error {
|
|
|
|
use ethcore::error::TransactionError::*;
|
|
|
|
|
|
|
|
if let EthcoreError::Transaction(e) = error {
|
|
|
|
let msg = match e {
|
|
|
|
AlreadyImported => "Transaction with the same hash was already imported.".into(),
|
|
|
|
Old => "Transaction nonce is too low. Try incrementing the nonce.".into(),
|
|
|
|
TooCheapToReplace => {
|
|
|
|
"Transaction fee is too low. There is another transaction with same nonce in the queue. Try increasing the fee or incrementing the nonce.".into()
|
|
|
|
},
|
|
|
|
LimitReached => {
|
|
|
|
"There is too many transactions in the queue. Your transaction was dropped due to limit. Try increasing the fee.".into()
|
|
|
|
},
|
|
|
|
InsufficientGasPrice { minimal, got } => {
|
|
|
|
format!("Transaction fee is to low. It does not satisfy your node's minimal fee (minimal: {}, got: {}). Try increasing the fee.", minimal, got)
|
|
|
|
},
|
|
|
|
InsufficientBalance { balance, cost } => {
|
|
|
|
format!("Insufficient funds. Account you try to send transaction from does not have enough funds. Required {} and got: {}.", cost, balance)
|
|
|
|
},
|
|
|
|
GasLimitExceeded { limit, got } => {
|
|
|
|
format!("Transaction cost exceeds current gas limit. Limit: {}, got: {}. Try decreasing supplied gas.", limit, got)
|
|
|
|
},
|
|
|
|
InvalidGasLimit(_) => "Supplied gas is beyond limit.".into(),
|
2016-06-17 22:15:18 +02:00
|
|
|
DAORescue => "Transaction removes funds from a DAO.".into(),
|
2016-06-15 00:17:23 +02:00
|
|
|
};
|
|
|
|
Error {
|
|
|
|
code: ErrorCode::ServerError(error_codes::TRANSACTION_ERROR),
|
|
|
|
message: msg,
|
|
|
|
data: None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Error {
|
|
|
|
code: ErrorCode::ServerError(error_codes::UNKNOWN_ERROR),
|
|
|
|
message: "Unknown error when sending transaction.".into(),
|
|
|
|
data: Some(Value::String(format!("{:?}", error))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|