Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

eth_call #783

Merged
merged 6 commits into from
Mar 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ use log_entry::LocalizedLogEntry;
use block_queue::{BlockQueue, BlockQueueInfo};
use blockchain::{BlockChain, BlockProvider, TreeRoute, ImportRoute};
use client::{BlockId, TransactionId, ClientConfig, BlockChainClient};
use env_info::EnvInfo;
use executive::{Executive, Executed};
pub use blockchain::CacheSize as BlockChainCacheSize;

/// General block status
Expand Down Expand Up @@ -385,6 +387,29 @@ impl<V> Client<V> where V: Verifier {
}

impl<V> BlockChainClient for Client<V> where V: Verifier {
fn call(&self, t: &SignedTransaction) -> Result<Executed, Error> {
let header = self.block_header(BlockId::Latest).unwrap();
let view = HeaderView::new(&header);
let last_hashes = self.build_last_hashes(view.hash());
let env_info = EnvInfo {
number: view.number(),
author: view.author(),
timestamp: view.timestamp(),
difficulty: view.difficulty(),
last_hashes: last_hashes,
gas_used: U256::zero(),
gas_limit: U256::max_value(),
};
// that's just a copy of the state.
let mut state = self.state();
let sender = try!(t.sender());
let balance = state.balance(&sender);
// give the sender max balance
state.sub_balance(&sender, &balance);
state.add_balance(&sender, &U256::max_value());
Executive::new(&mut state, &env_info, self.engine.deref().deref()).transact(t, false)
}

// TODO [todr] Should be moved to miner crate eventually.
fn try_seal(&self, block: ClosedBlock, seal: Vec<Bytes>) -> Result<SealedBlock, ClosedBlock> {
block.try_seal(self.engine.deref().deref(), seal)
Expand Down
5 changes: 4 additions & 1 deletion ethcore/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ use header::BlockNumber;
use transaction::{LocalizedTransaction, SignedTransaction};
use log_entry::LocalizedLogEntry;
use filter::Filter;
use error::{ImportResult};
use error::{ImportResult, Error};
use executive::Executed;

/// Blockchain database client. Owns and manages a blockchain and a block queue.
pub trait BlockChainClient : Sync + Send {
Expand Down Expand Up @@ -118,5 +119,7 @@ pub trait BlockChainClient : Sync + Send {
/// Attempts to seal given block. Returns `SealedBlock` on success and the same block in case of error.
fn try_seal(&self, block: ClosedBlock, seal: Vec<Bytes>) -> Result<SealedBlock, ClosedBlock>;

/// Makes a non-persistent transaction call.
fn call(&self, t: &SignedTransaction) -> Result<Executed, Error>;
}

6 changes: 6 additions & 0 deletions ethcore/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use error::{ImportResult};

use block_queue::BlockQueueInfo;
use block::{SealedBlock, ClosedBlock};
use executive::Executed;
use error::Error;

/// Test client.
pub struct TestBlockChainClient {
Expand Down Expand Up @@ -182,6 +184,10 @@ impl TestBlockChainClient {
}

impl BlockChainClient for TestBlockChainClient {
fn call(&self, _t: &SignedTransaction) -> Result<Executed, Error> {
unimplemented!()
}

fn block_total_difficulty(&self, _id: BlockId) -> Option<U256> {
Some(U256::zero())
}
Expand Down
3 changes: 3 additions & 0 deletions ethcore/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ impl<'a> HeaderView<'a> {
}
}

/// Returns header hash.
pub fn hash(&self) -> H256 { self.sha3() }

/// Returns raw rlp.
pub fn rlp(&self) -> &Rlp<'a> { &self.rlp }

Expand Down
22 changes: 22 additions & 0 deletions rpc/src/v1/impls/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,28 @@ impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM>
}
})
}

fn call(&self, params: Params) -> Result<Value, Error> {
from_params::<(TransactionRequest, BlockNumber)>(params)
.and_then(|(transaction_request, _block_number)| {
let accounts = take_weak!(self.accounts);
match accounts.account_secret(&transaction_request.from) {
Ok(secret) => {
let client = take_weak!(self.client);

let transaction: EthTransaction = transaction_request.into();
let signed_transaction = transaction.sign(&secret);

let output = client.call(&signed_transaction)
.map(|e| Bytes::new(e.output))
.unwrap_or(Bytes::default());

to_value(&output)
},
Err(_) => { to_value(&Bytes::default()) }
}
})
}
}

/// Eth filter rpc implementation.
Expand Down
11 changes: 11 additions & 0 deletions util/bigint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ pub trait Uint: Sized + Default + FromStr + From<u64> + fmt::Debug + fmt::Displa
fn zero() -> Self;
/// Returns new instance equalling one.
fn one() -> Self;
/// Returns the largest value that can be represented by this integer type.
fn max_value() -> Self;

/// Error type for converting from a decimal string.
type FromDecStrErr;
Expand Down Expand Up @@ -647,6 +649,15 @@ macro_rules! construct_uint {
From::from(1u64)
}

#[inline]
fn max_value() -> Self {
let mut result = [0; $n_words];
for i in 0..$n_words {
result[i] = u64::max_value();
}
$name(result)
}

/// Fast exponentation by squaring
/// https://en.wikipedia.org/wiki/Exponentiation_by_squaring
fn pow(self, expon: Self) -> Self {
Expand Down