Merge branch 'master' into signer-server

Conflicts:
	rpc/src/v1/types/bytes.rs
This commit is contained in:
Tomasz Drwięga 2016-05-27 18:50:47 +02:00
commit f60097ddb6
17 changed files with 326 additions and 201 deletions

View File

@ -346,6 +346,22 @@ impl<V> Client<V> where V: Verifier {
imported imported
} }
/// Attempt to get a copy of a specific block's state.
///
/// This will not fail if given BlockID::Latest.
/// Otherwise, this can fail (but may not) if the DB prunes state.
pub fn state_at(&self, id: BlockID) -> Option<State> {
// fast path for latest state.
if let BlockID::Latest = id.clone() {
return Some(self.state())
}
self.block_header(id).map(|header| {
let db = self.state_db.lock().unwrap().boxed_clone();
State::from_existing(db, HeaderView::new(&header).state_root(), self.engine.account_start_nonce())
})
}
/// Get a copy of the best block's state. /// Get a copy of the best block's state.
pub fn state(&self) -> State { pub fn state(&self) -> State {
State::from_existing(self.state_db.lock().unwrap().boxed_clone(), HeaderView::new(&self.best_block_header()).state_root(), self.engine.account_start_nonce()) State::from_existing(self.state_db.lock().unwrap().boxed_clone(), HeaderView::new(&self.best_block_header()).state_root(), self.engine.account_start_nonce())
@ -541,10 +557,11 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
Self::block_hash(&self.chain, id).and_then(|hash| self.chain.block_details(&hash)).map(|d| d.total_difficulty) Self::block_hash(&self.chain, id).and_then(|hash| self.chain.block_details(&hash)).map(|d| d.total_difficulty)
} }
fn nonce(&self, address: &Address) -> U256 { fn nonce(&self, address: &Address, id: BlockID) -> Option<U256> {
self.state().nonce(address) self.state_at(id).map(|s| s.nonce(address))
} }
fn block_hash(&self, id: BlockID) -> Option<H256> { fn block_hash(&self, id: BlockID) -> Option<H256> {
Self::block_hash(&self.chain, id) Self::block_hash(&self.chain, id)
} }
@ -553,12 +570,12 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
self.state().code(address) self.state().code(address)
} }
fn balance(&self, address: &Address) -> U256 { fn balance(&self, address: &Address, id: BlockID) -> Option<U256> {
self.state().balance(address) self.state_at(id).map(|s| s.balance(address))
} }
fn storage_at(&self, address: &Address, position: &H256) -> H256 { fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option<H256> {
self.state().storage_at(address, position) self.state_at(id).map(|s| s.storage_at(address, position))
} }
fn transaction(&self, id: TransactionID) -> Option<LocalizedTransaction> { fn transaction(&self, id: TransactionID) -> Option<LocalizedTransaction> {

View File

@ -65,8 +65,16 @@ pub trait BlockChainClient : Sync + Send {
/// Get block total difficulty. /// Get block total difficulty.
fn block_total_difficulty(&self, id: BlockID) -> Option<U256>; fn block_total_difficulty(&self, id: BlockID) -> Option<U256>;
/// Get address nonce. /// Attempt to get address nonce at given block.
fn nonce(&self, address: &Address) -> U256; /// May not fail on BlockID::Latest.
fn nonce(&self, address: &Address, id: BlockID) -> Option<U256>;
/// Get address nonce at the latest block's state.
fn latest_nonce(&self, address: &Address) -> U256 {
self.nonce(address, BlockID::Latest)
.expect("nonce will return Some when given BlockID::Latest. nonce was given BlockID::Latest. \
Therefore nonce has returned Some; qed")
}
/// Get block hash. /// Get block hash.
fn block_hash(&self, id: BlockID) -> Option<H256>; fn block_hash(&self, id: BlockID) -> Option<H256>;
@ -74,11 +82,31 @@ pub trait BlockChainClient : Sync + Send {
/// Get address code. /// Get address code.
fn code(&self, address: &Address) -> Option<Bytes>; fn code(&self, address: &Address) -> Option<Bytes>;
/// Get address balance. /// Get address balance at the given block's state.
fn balance(&self, address: &Address) -> U256; ///
/// May not return None if given BlockID::Latest.
/// Returns None if and only if the block's root hash has been pruned from the DB.
fn balance(&self, address: &Address, id: BlockID) -> Option<U256>;
/// Get value of the storage at given position. /// Get address balance at the latest block's state.
fn storage_at(&self, address: &Address, position: &H256) -> H256; fn latest_balance(&self, address: &Address) -> U256 {
self.balance(address, BlockID::Latest)
.expect("balance will return Some if given BlockID::Latest. balance was given BlockID::Latest \
Therefore balance has returned Some; qed")
}
/// Get value of the storage at given position at the given block's state.
///
/// May not return None if given BlockID::Latest.
/// Returns None if and only if the block's root hash has been pruned from the DB.
fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option<H256>;
/// Get value of the storage at given position at the latest block's state.
fn latest_storage_at(&self, address: &Address, position: &H256) -> H256 {
self.storage_at(address, position, BlockID::Latest)
.expect("storage_at will return Some if given BlockID::Latest. storage_at was given BlockID::Latest. \
Therefore storage_at has returned Some; qed")
}
/// Get transaction with given hash. /// Get transaction with given hash.
fn transaction(&self, id: TransactionID) -> Option<LocalizedTransaction>; fn transaction(&self, id: TransactionID) -> Option<LocalizedTransaction>;

View File

@ -245,20 +245,31 @@ impl BlockChainClient for TestBlockChainClient {
Self::block_hash(self, id) Self::block_hash(self, id)
} }
fn nonce(&self, address: &Address) -> U256 { fn nonce(&self, address: &Address, id: BlockID) -> Option<U256> {
self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero) match id {
BlockID::Latest => Some(self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)),
_ => None,
}
} }
fn code(&self, address: &Address) -> Option<Bytes> { fn code(&self, address: &Address) -> Option<Bytes> {
self.code.read().unwrap().get(address).cloned() self.code.read().unwrap().get(address).cloned()
} }
fn balance(&self, address: &Address) -> U256 { fn balance(&self, address: &Address, id: BlockID) -> Option<U256> {
self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero) if let BlockID::Latest = id {
Some(self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero))
} else {
None
}
} }
fn storage_at(&self, address: &Address, position: &H256) -> H256 { fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option<H256> {
self.storage.read().unwrap().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new) if let BlockID::Latest = id {
Some(self.storage.read().unwrap().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new))
} else {
None
}
} }
fn transaction(&self, _id: TransactionID) -> Option<LocalizedTransaction> { fn transaction(&self, _id: TransactionID) -> Option<LocalizedTransaction> {

View File

@ -63,8 +63,8 @@ pub use external::{ExternalMiner, ExternalMinerService};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use util::{H256, U256, Address, Bytes}; use util::{H256, U256, Address, Bytes};
use ethcore::client::{BlockChainClient, Executed}; use ethcore::client::{BlockChainClient, Executed};
use ethcore::block::{ClosedBlock}; use ethcore::block::ClosedBlock;
use ethcore::receipt::{Receipt}; use ethcore::receipt::Receipt;
use ethcore::error::{Error, ExecutionError}; use ethcore::error::{Error, ExecutionError};
use ethcore::transaction::SignedTransaction; use ethcore::transaction::SignedTransaction;
@ -154,7 +154,7 @@ pub trait MinerService : Send + Sync {
/// Suggested gas limit. /// Suggested gas limit.
fn sensible_gas_limit(&self) -> U256 { x!(21000) } fn sensible_gas_limit(&self) -> U256 { x!(21000) }
/// Account balance /// Latest account balance in pending state.
fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256; fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256;
/// Call into contract code using pending state. /// Call into contract code using pending state.

View File

@ -167,8 +167,8 @@ impl Miner {
}; };
let mut queue = self.transaction_queue.lock().unwrap(); let mut queue = self.transaction_queue.lock().unwrap();
let fetch_account = |a: &Address| AccountDetails { let fetch_account = |a: &Address| AccountDetails {
nonce: chain.nonce(a), nonce: chain.latest_nonce(a),
balance: chain.balance(a), balance: chain.latest_balance(a),
}; };
for hash in invalid_transactions.into_iter() { for hash in invalid_transactions.into_iter() {
queue.remove_invalid(&hash, &fetch_account); queue.remove_invalid(&hash, &fetch_account);
@ -290,17 +290,23 @@ impl MinerService for Miner {
fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256 { fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256 {
let sealing_work = self.sealing_work.lock().unwrap(); let sealing_work = self.sealing_work.lock().unwrap();
sealing_work.peek_last_ref().map_or_else(|| chain.balance(address), |b| b.block().fields().state.balance(address)) sealing_work.peek_last_ref().map_or_else(
|| chain.latest_balance(address),
|b| b.block().fields().state.balance(address)
)
} }
fn storage_at(&self, chain: &BlockChainClient, address: &Address, position: &H256) -> H256 { fn storage_at(&self, chain: &BlockChainClient, address: &Address, position: &H256) -> H256 {
let sealing_work = self.sealing_work.lock().unwrap(); let sealing_work = self.sealing_work.lock().unwrap();
sealing_work.peek_last_ref().map_or_else(|| chain.storage_at(address, position), |b| b.block().fields().state.storage_at(address, position)) sealing_work.peek_last_ref().map_or_else(
|| chain.latest_storage_at(address, position),
|b| b.block().fields().state.storage_at(address, position)
)
} }
fn nonce(&self, chain: &BlockChainClient, address: &Address) -> U256 { fn nonce(&self, chain: &BlockChainClient, address: &Address) -> U256 {
let sealing_work = self.sealing_work.lock().unwrap(); let sealing_work = self.sealing_work.lock().unwrap();
sealing_work.peek_last_ref().map_or_else(|| chain.nonce(address), |b| b.block().fields().state.nonce(address)) sealing_work.peek_last_ref().map_or_else(|| chain.latest_nonce(address), |b| b.block().fields().state.nonce(address))
} }
fn code(&self, chain: &BlockChainClient, address: &Address) -> Option<Bytes> { fn code(&self, chain: &BlockChainClient, address: &Address) -> Option<Bytes> {
@ -545,8 +551,8 @@ impl MinerService for Miner {
let _sender = tx.sender(); let _sender = tx.sender();
} }
let _ = self.import_transactions(txs, |a| AccountDetails { let _ = self.import_transactions(txs, |a| AccountDetails {
nonce: chain.nonce(a), nonce: chain.latest_nonce(a),
balance: chain.balance(a), balance: chain.latest_balance(a),
}); });
}); });
} }
@ -566,7 +572,7 @@ impl MinerService for Miner {
}) })
.collect::<HashSet<Address>>(); .collect::<HashSet<Address>>();
for sender in to_remove.into_iter() { for sender in to_remove.into_iter() {
transaction_queue.remove_all(sender, chain.nonce(&sender)); transaction_queue.remove_all(sender, chain.latest_nonce(&sender));
} }
}); });
} }

View File

@ -22,7 +22,7 @@ use ethsync::EthSync;
use ethminer::{Miner, ExternalMiner}; use ethminer::{Miner, ExternalMiner};
use util::RotatingLogger; use util::RotatingLogger;
use util::panics::PanicHandler; use util::panics::PanicHandler;
use util::keys::store::{AccountService}; use util::keys::store::AccountService;
use util::network_settings::NetworkSettings; use util::network_settings::NetworkSettings;
use die::*; use die::*;
@ -100,7 +100,7 @@ pub fn setup_dapps_server(
server.add_delegate(NetClient::new(&deps.sync).to_delegate()); server.add_delegate(NetClient::new(&deps.sync).to_delegate());
server.add_delegate(EthClient::new(&deps.client, &deps.sync, &deps.secret_store, &deps.miner, &deps.external_miner).to_delegate()); server.add_delegate(EthClient::new(&deps.client, &deps.sync, &deps.secret_store, &deps.miner, &deps.external_miner).to_delegate());
server.add_delegate(EthFilterClient::new(&deps.client, &deps.miner).to_delegate()); server.add_delegate(EthFilterClient::new(&deps.client, &deps.miner).to_delegate());
server.add_delegate(PersonalClient::new(&deps.secret_store).to_delegate()); server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner).to_delegate());
server.add_delegate(EthcoreClient::new(&deps.miner, deps.logger.clone(), deps.settings.clone()).to_delegate()); server.add_delegate(EthcoreClient::new(&deps.miner, deps.logger.clone(), deps.settings.clone()).to_delegate());
let start_result = match auth { let start_result = match auth {

View File

@ -24,7 +24,7 @@ use ethsync::EthSync;
use ethminer::{Miner, ExternalMiner}; use ethminer::{Miner, ExternalMiner};
use util::RotatingLogger; use util::RotatingLogger;
use util::panics::PanicHandler; use util::panics::PanicHandler;
use util::keys::store::{AccountService}; use util::keys::store::AccountService;
use util::network_settings::NetworkSettings; use util::network_settings::NetworkSettings;
use die::*; use die::*;
use jsonipc; use jsonipc;
@ -106,7 +106,7 @@ fn setup_rpc_server(apis: Vec<&str>, deps: &Arc<Dependencies>) -> Server {
}, },
"personal" => { "personal" => {
modules.insert("personal".to_owned(), "1.0".to_owned()); modules.insert("personal".to_owned(), "1.0".to_owned());
server.add_delegate(PersonalClient::new(&deps.secret_store).to_delegate()) server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner).to_delegate())
}, },
"ethcore" => { "ethcore" => {
modules.insert("ethcore".to_owned(), "1.0".to_owned()); modules.insert("ethcore".to_owned(), "1.0".to_owned());

View File

@ -22,11 +22,10 @@ use std::collections::HashSet;
use std::sync::{Arc, Weak, Mutex}; use std::sync::{Arc, Weak, Mutex};
use std::ops::Deref; use std::ops::Deref;
use ethsync::{SyncProvider, SyncState}; use ethsync::{SyncProvider, SyncState};
use ethminer::{MinerService, AccountDetails, ExternalMinerService}; use ethminer::{MinerService, ExternalMinerService};
use jsonrpc_core::*; use jsonrpc_core::*;
use util::numbers::*; use util::numbers::*;
use util::sha3::*; use util::sha3::*;
use util::bytes::{ToPretty};
use util::rlp::{encode, decode, UntrustedRlp, View}; use util::rlp::{encode, decode, UntrustedRlp, View};
use ethcore::client::{BlockChainClient, BlockID, TransactionID, UncleID}; use ethcore::client::{BlockChainClient, BlockID, TransactionID, UncleID};
use ethcore::block::IsBlock; use ethcore::block::IsBlock;
@ -39,6 +38,7 @@ use self::ethash::SeedHashCompute;
use v1::traits::{Eth, EthFilter}; use v1::traits::{Eth, EthFilter};
use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, TransactionRequest, CallRequest, OptionalValue, Index, Filter, Log, Receipt}; use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, TransactionRequest, CallRequest, OptionalValue, Index, Filter, Log, Receipt};
use v1::helpers::{PollFilter, PollManager}; use v1::helpers::{PollFilter, PollManager};
use v1::impls::{dispatch_transaction, sign_and_dispatch};
use util::keys::store::AccountProvider; use util::keys::store::AccountProvider;
use serde; use serde;
@ -155,33 +155,12 @@ impl<C, S, A, M, EM> EthClient<C, S, A, M, EM> where
} }
} }
fn sign_and_dispatch(&self, request: TransactionRequest, secret: H256) -> Result<Value, Error> {
let signed_transaction = {
let client = take_weak!(self.client);
let miner = take_weak!(self.miner);
EthTransaction {
nonce: request.nonce
.or_else(|| miner
.last_nonce(&request.from)
.map(|nonce| nonce + U256::one()))
.unwrap_or_else(|| client.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, |d| d.to_vec()),
}.sign(&secret)
};
trace!(target: "miner", "send_transaction: dispatching tx: {}", encode(&signed_transaction).to_vec().pretty());
self.dispatch_transaction(signed_transaction)
}
fn sign_call(&self, request: CallRequest) -> Result<SignedTransaction, Error> { fn sign_call(&self, request: CallRequest) -> Result<SignedTransaction, Error> {
let client = take_weak!(self.client); let client = take_weak!(self.client);
let miner = take_weak!(self.miner); let miner = take_weak!(self.miner);
let from = request.from.unwrap_or(Address::zero()); let from = request.from.unwrap_or(Address::zero());
Ok(EthTransaction { Ok(EthTransaction {
nonce: request.nonce.unwrap_or_else(|| client.nonce(&from)), nonce: request.nonce.unwrap_or_else(|| client.latest_nonce(&from)),
action: request.to.map_or(Action::Create, Action::Call), action: request.to.map_or(Action::Create, Action::Call),
gas: request.gas.unwrap_or(U256::from(50_000_000)), gas: request.gas.unwrap_or(U256::from(50_000_000)),
gas_price: request.gas_price.unwrap_or_else(|| miner.sensible_gas_price()), gas_price: request.gas_price.unwrap_or_else(|| miner.sensible_gas_price()),
@ -189,30 +168,6 @@ impl<C, S, A, M, EM> EthClient<C, S, A, M, EM> where
data: request.data.map_or_else(Vec::new, |d| d.to_vec()) data: request.data.map_or_else(Vec::new, |d| d.to_vec())
}.fake_sign(from)) }.fake_sign(from))
} }
fn dispatch_transaction(&self, signed_transaction: SignedTransaction) -> Result<Value, Error> {
let hash = signed_transaction.hash();
let import = {
let client = take_weak!(self.client);
let miner = take_weak!(self.miner);
miner.import_own_transaction(client.deref(), signed_transaction, |a: &Address| {
AccountDetails {
nonce: client.nonce(&a),
balance: client.balance(&a),
}
})
};
match import {
Ok(_) => to_value(&hash),
Err(e) => {
warn!("Error sending transaction: {:?}", e);
to_value(&H256::zero())
}
}
}
} }
const MAX_QUEUE_SIZE_TO_MINE_ON: usize = 4; // because uncles go back 6. const MAX_QUEUE_SIZE_TO_MINE_ON: usize = 4; // because uncles go back 6.
@ -257,6 +212,17 @@ fn pending_logs<M>(miner: &M, filter: &EthcoreFilter) -> Vec<Log> where M: Miner
result result
} }
// must be in range [-32099, -32000]
const UNSUPPORTED_REQUEST_CODE: i64 = -32000;
fn make_unsupported_err() -> Error {
Error {
code: ErrorCode::ServerError(UNSUPPORTED_REQUEST_CODE),
message: "Unsupported request.".into(),
data: None
}
}
impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM> where impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM> where
C: BlockChainClient + 'static, C: BlockChainClient + 'static,
S: SyncProvider + 'static, S: SyncProvider + 'static,
@ -341,18 +307,19 @@ impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM> where
fn balance(&self, params: Params) -> Result<Value, Error> { fn balance(&self, params: Params) -> Result<Value, Error> {
from_params_default_second(params) from_params_default_second(params)
.and_then(|(address, block_number,)| match block_number { .and_then(|(address, block_number,)| match block_number {
BlockNumber::Latest => to_value(&take_weak!(self.client).balance(&address)),
BlockNumber::Pending => to_value(&take_weak!(self.miner).balance(take_weak!(self.client).deref(), &address)), BlockNumber::Pending => to_value(&take_weak!(self.miner).balance(take_weak!(self.client).deref(), &address)),
_ => Err(Error::invalid_params()), id => to_value(&try!(take_weak!(self.client).balance(&address, id.into()).ok_or_else(make_unsupported_err))),
}) })
} }
fn storage_at(&self, params: Params) -> Result<Value, Error> { fn storage_at(&self, params: Params) -> Result<Value, Error> {
from_params_default_third::<Address, U256>(params) from_params_default_third::<Address, U256>(params)
.and_then(|(address, position, block_number,)| match block_number { .and_then(|(address, position, block_number,)| match block_number {
BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).storage_at(take_weak!(self.client).deref(), &address, &H256::from(position)))), BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).storage_at(&*take_weak!(self.client), &address, &H256::from(position)))),
BlockNumber::Latest => to_value(&U256::from(take_weak!(self.client).storage_at(&address, &H256::from(position)))), id => match take_weak!(self.client).storage_at(&address, &H256::from(position), id.into()) {
_ => Err(Error::invalid_params()), Some(s) => to_value(&U256::from(s)),
None => Err(make_unsupported_err()), // None is only returned on unsupported requests.
}
}) })
} }
@ -360,8 +327,7 @@ impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM> where
from_params_default_second(params) from_params_default_second(params)
.and_then(|(address, block_number,)| match block_number { .and_then(|(address, block_number,)| match block_number {
BlockNumber::Pending => to_value(&take_weak!(self.miner).nonce(take_weak!(self.client).deref(), &address)), BlockNumber::Pending => to_value(&take_weak!(self.miner).nonce(take_weak!(self.client).deref(), &address)),
BlockNumber::Latest => to_value(&take_weak!(self.client).nonce(&address)), id => to_value(&take_weak!(self.client).nonce(&address, id.into())),
_ => Err(Error::invalid_params()),
}) })
} }
@ -533,29 +499,18 @@ impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM> where
.and_then(|(request, )| { .and_then(|(request, )| {
let accounts = take_weak!(self.accounts); let accounts = take_weak!(self.accounts);
match accounts.account_secret(&request.from) { match accounts.account_secret(&request.from) {
Ok(secret) => self.sign_and_dispatch(request, secret), Ok(secret) => sign_and_dispatch(&self.client, &self.miner, request, secret),
Err(_) => to_value(&H256::zero()) Err(_) => to_value(&H256::zero())
} }
}) })
} }
fn sign_and_send_transaction(&self, params: Params) -> Result<Value, Error> {
from_params::<(TransactionRequest, String)>(params)
.and_then(|(request, password)| {
let accounts = take_weak!(self.accounts);
match accounts.locked_account_secret(&request.from, &password) {
Ok(secret) => self.sign_and_dispatch(request, secret),
Err(_) => to_value(&H256::zero()),
}
})
}
fn send_raw_transaction(&self, params: Params) -> Result<Value, Error> { fn send_raw_transaction(&self, params: Params) -> Result<Value, Error> {
from_params::<(Bytes, )>(params) from_params::<(Bytes, )>(params)
.and_then(|(raw_transaction, )| { .and_then(|(raw_transaction, )| {
let raw_transaction = raw_transaction.to_vec(); let raw_transaction = raw_transaction.to_vec();
match UntrustedRlp::new(&raw_transaction).as_val() { match UntrustedRlp::new(&raw_transaction).as_val() {
Ok(signed_transaction) => self.dispatch_transaction(signed_transaction), Ok(signed_transaction) => dispatch_transaction(&*take_weak!(self.client), &*take_weak!(self.miner), signed_transaction),
Err(_) => to_value(&H256::zero()), Err(_) => to_value(&H256::zero()),
} }
}) })

View File

@ -41,3 +41,59 @@ pub use self::ethcore::EthcoreClient;
pub use self::traces::TracesClient; pub use self::traces::TracesClient;
pub use self::rpc::RpcClient; pub use self::rpc::RpcClient;
use v1::types::TransactionRequest;
use std::sync::Weak;
use ethminer::{AccountDetails, MinerService};
use ethcore::client::BlockChainClient;
use ethcore::transaction::{Action, SignedTransaction, Transaction};
use util::numbers::*;
use util::rlp::encode;
use util::bytes::ToPretty;
use jsonrpc_core::{Error, to_value, Value};
fn dispatch_transaction<C, M>(client: &C, miner: &M, signed_transaction: SignedTransaction) -> Result<Value, Error>
where C: BlockChainClient, M: MinerService {
let hash = signed_transaction.hash();
let import = {
miner.import_own_transaction(client, signed_transaction, |a: &Address| {
AccountDetails {
nonce: client.latest_nonce(&a),
balance: client.latest_balance(&a),
}
})
};
match import {
Ok(_) => to_value(&hash),
Err(e) => {
warn!("Error sending transaction: {:?}", e);
to_value(&H256::zero())
}
}
}
fn sign_and_dispatch<C, M>(client: &Weak<C>, miner: &Weak<M>, request: TransactionRequest, secret: H256) -> Result<Value, Error>
where C: BlockChainClient, M: MinerService {
let client = take_weak!(client);
let miner = take_weak!(miner);
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)
}

View File

@ -18,24 +18,35 @@
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use jsonrpc_core::*; use jsonrpc_core::*;
use v1::traits::Personal; use v1::traits::Personal;
use v1::types::TransactionRequest;
use v1::impls::sign_and_dispatch;
use util::keys::store::*; use util::keys::store::*;
use util::Address; use util::numbers::*;
use ethcore::client::BlockChainClient;
use ethminer::MinerService;
/// Account management (personal) rpc implementation. /// Account management (personal) rpc implementation.
pub struct PersonalClient<A> where A: AccountProvider { pub struct PersonalClient<A, C, M>
where A: AccountProvider, C: BlockChainClient, M: MinerService {
accounts: Weak<A>, accounts: Weak<A>,
client: Weak<C>,
miner: Weak<M>,
} }
impl<A> PersonalClient<A> where A: AccountProvider { impl<A, C, M> PersonalClient<A, C, M>
where A: AccountProvider, C: BlockChainClient, M: MinerService {
/// Creates new PersonalClient /// Creates new PersonalClient
pub fn new(store: &Arc<A>) -> Self { pub fn new(store: &Arc<A>, client: &Arc<C>, miner: &Arc<M>) -> Self {
PersonalClient { PersonalClient {
accounts: Arc::downgrade(store), accounts: Arc::downgrade(store),
client: Arc::downgrade(client),
miner: Arc::downgrade(miner),
} }
} }
} }
impl<A> Personal for PersonalClient<A> where A: AccountProvider + 'static { impl<A: 'static, C: 'static, M: 'static> Personal for PersonalClient<A, C, M>
where A: AccountProvider, C: BlockChainClient, M: MinerService {
fn accounts(&self, _: Params) -> Result<Value, Error> { fn accounts(&self, _: Params) -> Result<Value, Error> {
let store = take_weak!(self.accounts); let store = take_weak!(self.accounts);
match store.accounts() { match store.accounts() {
@ -66,4 +77,15 @@ impl<A> Personal for PersonalClient<A> where A: AccountProvider + 'static {
} }
}) })
} }
fn sign_and_send_transaction(&self, params: Params) -> Result<Value, Error> {
from_params::<(TransactionRequest, String)>(params)
.and_then(|(request, password)| {
let accounts = take_weak!(self.accounts);
match accounts.locked_account_secret(&request.from, &password) {
Ok(secret) => sign_and_dispatch(&self.client, &self.miner, request, secret),
Err(_) => to_value(&H256::zero()),
}
})
}
} }

View File

@ -521,81 +521,6 @@ fn rpc_eth_send_transaction() {
assert_eq!(tester.io.handle_request(request.as_ref()), Some(response)); assert_eq!(tester.io.handle_request(request.as_ref()), Some(response));
} }
#[test]
fn rpc_eth_sign_and_send_transaction_with_invalid_password() {
let account = TestAccount::new("password123");
let address = account.address();
let tester = EthTester::default();
tester.accounts_provider.accounts.write().unwrap().insert(address.clone(), account);
let request = r#"{
"jsonrpc": "2.0",
"method": "eth_signAndSendTransaction",
"params": [{
"from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"value": "0x9184e72a"
}, "password321"],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x0000000000000000000000000000000000000000000000000000000000000000","id":1}"#;
assert_eq!(tester.io.handle_request(request.as_ref()), Some(response.into()));
}
#[test]
fn rpc_eth_sign_and_send_transaction() {
let account = TestAccount::new("password123");
let address = account.address();
let secret = account.secret.clone();
let tester = EthTester::default();
tester.accounts_provider.accounts.write().unwrap().insert(address.clone(), account);
let request = r#"{
"jsonrpc": "2.0",
"method": "eth_signAndSendTransaction",
"params": [{
"from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"value": "0x9184e72a"
}, "password123"],
"id": 1
}"#;
let t = Transaction {
nonce: U256::zero(),
gas_price: U256::from(0x9184e72a000u64),
gas: U256::from(0x76c0),
action: Action::Call(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap()),
value: U256::from(0x9184e72au64),
data: vec![]
}.sign(&secret);
let response = r#"{"jsonrpc":"2.0","result":""#.to_owned() + format!("0x{:?}", t.hash()).as_ref() + r#"","id":1}"#;
assert_eq!(tester.io.handle_request(request.as_ref()), Some(response));
tester.miner.last_nonces.write().unwrap().insert(address.clone(), U256::zero());
let t = Transaction {
nonce: U256::one(),
gas_price: U256::from(0x9184e72a000u64),
gas: U256::from(0x76c0),
action: Action::Call(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap()),
value: U256::from(0x9184e72au64),
data: vec![]
}.sign(&secret);
let response = r#"{"jsonrpc":"2.0","result":""#.to_owned() + format!("0x{:?}", t.hash()).as_ref() + r#"","id":1}"#;
assert_eq!(tester.io.handle_request(request.as_ref()), Some(response));
}
#[test] #[test]
#[ignore] #[ignore]
fn rpc_eth_send_raw_transaction() { fn rpc_eth_send_raw_transaction() {

View File

@ -15,11 +15,29 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::sync::Arc; use std::sync::Arc;
use std::str::FromStr;
use std::collections::HashMap;
use jsonrpc_core::IoHandler; use jsonrpc_core::IoHandler;
use util::numbers::*; use util::numbers::*;
use util::keys::{TestAccount, TestAccountProvider}; use util::keys::{TestAccount, TestAccountProvider};
use v1::{PersonalClient, Personal}; use v1::{PersonalClient, Personal};
use std::collections::*; use v1::tests::helpers::TestMinerService;
use ethcore::client::TestBlockChainClient;
use ethcore::transaction::{Action, Transaction};
struct PersonalTester {
accounts: Arc<TestAccountProvider>,
io: IoHandler,
miner: Arc<TestMinerService>,
// these unused fields are necessary to keep the data alive
// as the handler has only weak pointers.
_client: Arc<TestBlockChainClient>,
}
fn blockchain_client() -> Arc<TestBlockChainClient> {
let client = TestBlockChainClient::new();
Arc::new(client)
}
fn accounts_provider() -> Arc<TestAccountProvider> { fn accounts_provider() -> Arc<TestAccountProvider> {
let accounts = HashMap::new(); let accounts = HashMap::new();
@ -27,18 +45,33 @@ fn accounts_provider() -> Arc<TestAccountProvider> {
Arc::new(ap) Arc::new(ap)
} }
fn setup() -> (Arc<TestAccountProvider>, IoHandler) { fn miner_service() -> Arc<TestMinerService> {
let test_provider = accounts_provider(); Arc::new(TestMinerService::default())
let personal = PersonalClient::new(&test_provider); }
fn setup() -> PersonalTester {
let accounts = accounts_provider();
let client = blockchain_client();
let miner = miner_service();
let personal = PersonalClient::new(&accounts, &client, &miner);
let io = IoHandler::new(); let io = IoHandler::new();
io.add_delegate(personal.to_delegate()); io.add_delegate(personal.to_delegate());
(test_provider, io)
let tester = PersonalTester {
accounts: accounts,
io: io,
miner: miner,
_client: client,
};
tester
} }
#[test] #[test]
fn accounts() { fn accounts() {
let (test_provider, io) = setup(); let tester = setup();
test_provider.accounts tester.accounts.accounts
.write() .write()
.unwrap() .unwrap()
.insert(Address::from(1), TestAccount::new("test")); .insert(Address::from(1), TestAccount::new("test"));
@ -46,17 +79,17 @@ fn accounts() {
let request = r#"{"jsonrpc": "2.0", "method": "personal_listAccounts", "params": [], "id": 1}"#; let request = r#"{"jsonrpc": "2.0", "method": "personal_listAccounts", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":["0x0000000000000000000000000000000000000001"],"id":1}"#; let response = r#"{"jsonrpc":"2.0","result":["0x0000000000000000000000000000000000000001"],"id":1}"#;
assert_eq!(io.handle_request(request), Some(response.to_owned())); assert_eq!(tester.io.handle_request(request), Some(response.to_owned()));
} }
#[test] #[test]
fn new_account() { fn new_account() {
let (test_provider, io) = setup(); let tester = setup();
let request = r#"{"jsonrpc": "2.0", "method": "personal_newAccount", "params": ["pass"], "id": 1}"#; let request = r#"{"jsonrpc": "2.0", "method": "personal_newAccount", "params": ["pass"], "id": 1}"#;
let res = io.handle_request(request); let res = tester.io.handle_request(request);
let accounts = test_provider.accounts.read().unwrap(); let accounts = tester.accounts.accounts.read().unwrap();
assert_eq!(accounts.len(), 1); assert_eq!(accounts.len(), 1);
let address = accounts let address = accounts
@ -70,3 +103,77 @@ fn new_account() {
assert_eq!(res, Some(response)); assert_eq!(res, Some(response));
} }
#[test]
fn sign_and_send_transaction_with_invalid_password() {
let account = TestAccount::new("password123");
let address = account.address();
let tester = setup();
tester.accounts.accounts.write().unwrap().insert(address.clone(), account);
let request = r#"{
"jsonrpc": "2.0",
"method": "personal_signAndSendTransaction",
"params": [{
"from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"value": "0x9184e72a"
}, "password321"],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x0000000000000000000000000000000000000000000000000000000000000000","id":1}"#;
assert_eq!(tester.io.handle_request(request.as_ref()), Some(response.into()));
}
#[test]
fn sign_and_send_transaction() {
let account = TestAccount::new("password123");
let address = account.address();
let secret = account.secret.clone();
let tester = setup();
tester.accounts.accounts.write().unwrap().insert(address.clone(), account);
let request = r#"{
"jsonrpc": "2.0",
"method": "personal_signAndSendTransaction",
"params": [{
"from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"value": "0x9184e72a"
}, "password123"],
"id": 1
}"#;
let t = Transaction {
nonce: U256::zero(),
gas_price: U256::from(0x9184e72a000u64),
gas: U256::from(0x76c0),
action: Action::Call(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap()),
value: U256::from(0x9184e72au64),
data: vec![]
}.sign(&secret);
let response = r#"{"jsonrpc":"2.0","result":""#.to_owned() + format!("0x{:?}", t.hash()).as_ref() + r#"","id":1}"#;
assert_eq!(tester.io.handle_request(request.as_ref()), Some(response));
tester.miner.last_nonces.write().unwrap().insert(address.clone(), U256::zero());
let t = Transaction {
nonce: U256::one(),
gas_price: U256::from(0x9184e72a000u64),
gas: U256::from(0x76c0),
action: Action::Call(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap()),
value: U256::from(0x9184e72au64),
data: vec![]
}.sign(&secret);
let response = r#"{"jsonrpc":"2.0","result":""#.to_owned() + format!("0x{:?}", t.hash()).as_ref() + r#"","id":1}"#;
assert_eq!(tester.io.handle_request(request.as_ref()), Some(response));
}

View File

@ -3,6 +3,4 @@
pub mod helpers; pub mod helpers;
#[cfg(test)] #[cfg(test)]
mod mocked; mod mocked;
#[cfg(test)]
mod eth;

View File

@ -80,9 +80,6 @@ pub trait Eth: Sized + Send + Sync + 'static {
/// Sends transaction. /// Sends transaction.
fn send_transaction(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn send_transaction(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Sends transaction and signs it in single call. The account is not unlocked in such case.
fn sign_and_send_transaction(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Sends signed transaction. /// Sends signed transaction.
fn send_raw_transaction(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn send_raw_transaction(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
@ -155,7 +152,6 @@ pub trait Eth: Sized + Send + Sync + 'static {
delegate.add_method("eth_getCode", Eth::code_at); delegate.add_method("eth_getCode", Eth::code_at);
delegate.add_method("eth_sign", Eth::sign); delegate.add_method("eth_sign", Eth::sign);
delegate.add_method("eth_sendTransaction", Eth::send_transaction); delegate.add_method("eth_sendTransaction", Eth::send_transaction);
delegate.add_method("eth_signAndSendTransaction", Eth::sign_and_send_transaction);
delegate.add_method("eth_sendRawTransaction", Eth::send_raw_transaction); delegate.add_method("eth_sendRawTransaction", Eth::send_raw_transaction);
delegate.add_method("eth_call", Eth::call); delegate.add_method("eth_call", Eth::call);
delegate.add_method("eth_estimateGas", Eth::estimate_gas); delegate.add_method("eth_estimateGas", Eth::estimate_gas);

View File

@ -30,12 +30,16 @@ pub trait Personal: Sized + Send + Sync + 'static {
/// Unlocks specified account for use (can only be one unlocked account at one moment) /// Unlocks specified account for use (can only be one unlocked account at one moment)
fn unlock_account(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn unlock_account(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Sends transaction and signs it in single call. The account is not unlocked in such case.
fn sign_and_send_transaction(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Should be used to convert object to io delegate. /// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> { fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self)); let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("personal_listAccounts", Personal::accounts); delegate.add_method("personal_listAccounts", Personal::accounts);
delegate.add_method("personal_newAccount", Personal::new_account); delegate.add_method("personal_newAccount", Personal::new_account);
delegate.add_method("personal_unlockAccount", Personal::unlock_account); delegate.add_method("personal_unlockAccount", Personal::unlock_account);
delegate.add_method("personal_signAndSendTransaction", Personal::sign_and_send_transaction);
delegate delegate
} }
} }

View File

@ -900,8 +900,8 @@ impl ChainSync {
} }
let chain = io.chain(); let chain = io.chain();
let fetch_account = |a: &Address| AccountDetails { let fetch_account = |a: &Address| AccountDetails {
nonce: chain.nonce(a), nonce: chain.latest_nonce(a),
balance: chain.balance(a), balance: chain.latest_balance(a),
}; };
let _ = self.miner.import_transactions(transactions, fetch_account); let _ = self.miner.import_transactions(transactions, fetch_account);
Ok(()) Ok(())