2016-08-08 17:25:15 +02: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-10-15 14:44:08 +02:00
|
|
|
use util::{Address, H256, U256, Uint, Bytes};
|
2016-08-08 17:25:15 +02:00
|
|
|
use util::bytes::ToPretty;
|
2016-10-15 14:44:08 +02:00
|
|
|
use ethkey::Signature;
|
2016-08-08 17:25:15 +02:00
|
|
|
use ethcore::miner::MinerService;
|
|
|
|
use ethcore::client::MiningBlockChainClient;
|
|
|
|
use ethcore::transaction::{Action, SignedTransaction, Transaction};
|
|
|
|
use ethcore::account_provider::AccountProvider;
|
|
|
|
use jsonrpc_core::{Error, Value, to_value};
|
|
|
|
use v1::helpers::TransactionRequest;
|
2016-10-15 14:44:08 +02:00
|
|
|
use v1::types::{H256 as RpcH256, H520 as RpcH520, Bytes as RpcBytes};
|
2016-08-08 17:25:15 +02:00
|
|
|
use v1::helpers::errors;
|
|
|
|
|
2016-10-15 14:44:08 +02:00
|
|
|
pub const DEFAULT_MAC: [u8; 2] = [0, 0];
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
pub fn dispatch_transaction<C, M>(client: &C, miner: &M, signed_transaction: SignedTransaction) -> Result<RpcH256, Error>
|
2016-08-08 17:25:15 +02:00
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
|
|
|
let hash = RpcH256::from(signed_transaction.hash());
|
|
|
|
|
2016-10-15 14:44:08 +02:00
|
|
|
miner.import_own_transaction(client, signed_transaction)
|
2016-08-08 17:25:15 +02:00
|
|
|
.map_err(errors::from_transaction_error)
|
2016-09-23 19:42:33 +02:00
|
|
|
.map(|_| hash)
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 14:44:08 +02:00
|
|
|
fn signature(accounts: &AccountProvider, address: Address, password: Option<String>, hash: H256) -> Result<Signature, Error> {
|
|
|
|
accounts.sign(address, password.clone(), hash).map_err(|e| match password {
|
|
|
|
Some(_) => errors::from_password_error(e),
|
|
|
|
None => errors::from_signing_error(e),
|
|
|
|
})
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 14:44:08 +02:00
|
|
|
pub fn sign(accounts: &AccountProvider, address: Address, password: Option<String>, hash: H256) -> Result<Value, Error> {
|
|
|
|
signature(accounts, address, password, hash)
|
|
|
|
.map(RpcH520::from)
|
|
|
|
.map(to_value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decrypt(accounts: &AccountProvider, address: Address, password: Option<String>, msg: Bytes) -> Result<Value, Error> {
|
|
|
|
accounts.decrypt(address, password.clone(), &DEFAULT_MAC, &msg)
|
|
|
|
.map_err(|e| match password {
|
|
|
|
Some(_) => errors::from_password_error(e),
|
|
|
|
None => errors::from_signing_error(e),
|
|
|
|
})
|
|
|
|
.map(RpcBytes::from)
|
|
|
|
.map(to_value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sign_and_dispatch<C, M>(client: &C, miner: &M, accounts: &AccountProvider, request: TransactionRequest, password: Option<String>) -> Result<Value, Error>
|
2016-08-08 17:25:15 +02:00
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
|
|
|
|
|
|
|
let address = request.from;
|
|
|
|
let signed_transaction = {
|
|
|
|
let t = prepare_transaction(client, miner, request);
|
|
|
|
let hash = t.hash();
|
2016-10-15 14:44:08 +02:00
|
|
|
let signature = try!(signature(accounts, address, password, hash));
|
2016-08-08 17:25:15 +02:00
|
|
|
t.with_signature(signature)
|
|
|
|
};
|
|
|
|
|
2016-09-01 14:49:12 +02:00
|
|
|
trace!(target: "miner", "send_transaction: dispatching tx: {}", ::rlp::encode(&signed_transaction).to_vec().pretty());
|
2016-09-23 19:42:33 +02:00
|
|
|
dispatch_transaction(&*client, &*miner, signed_transaction).map(to_value)
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 14:44:08 +02:00
|
|
|
fn prepare_transaction<C, M>(client: &C, miner: &M, request: TransactionRequest) -> Transaction where C: MiningBlockChainClient, M: MinerService {
|
|
|
|
Transaction {
|
|
|
|
nonce: request.nonce
|
|
|
|
.or_else(|| miner
|
|
|
|
.last_nonce(&request.from)
|
|
|
|
.map(|nonce| nonce + U256::one()))
|
|
|
|
.unwrap_or_else(|| client.latest_nonce(&request.from)),
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2016-10-15 14:44:08 +02:00
|
|
|
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(|| default_gas_price(client, miner)),
|
|
|
|
value: request.value.unwrap_or_else(U256::zero),
|
|
|
|
data: request.data.map_or_else(Vec::new, |b| b.to_vec()),
|
|
|
|
}
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default_gas_price<C, M>(client: &C, miner: &M) -> U256 where C: MiningBlockChainClient, M: MinerService {
|
|
|
|
client
|
|
|
|
.gas_price_statistics(100, 8)
|
|
|
|
.map(|x| x[4])
|
|
|
|
.unwrap_or_else(|_| miner.sensible_gas_price())
|
|
|
|
}
|