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

Commit

Permalink
allow optional casting of engine client to full client
Browse files Browse the repository at this point in the history
  • Loading branch information
rphmeier committed Sep 5, 2017
1 parent ad39446 commit 7d1c7a0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 43 deletions.
18 changes: 4 additions & 14 deletions ethcore/light/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use std::sync::{Weak, Arc};

use ethcore::block_status::BlockStatus;
use ethcore::client::{TransactionImportResult, ClientReport, EnvInfo};
use ethcore::client::{ClientReport, EnvInfo};
use ethcore::engines::{epoch, Engine, EpochChange, EpochTransition, Proof, Unsure};
use ethcore::error::{TransactionError, BlockImportError, Error as EthcoreError};
use ethcore::error::BlockImportError;
use ethcore::ids::BlockId;
use ethcore::header::{BlockNumber, Header};
use ethcore::verification::queue::{self, HeaderQueue};
Expand All @@ -35,7 +35,6 @@ use bigint::prelude::U256;
use bigint::hash::H256;
use futures::{IntoFuture, Future};

use util::Address;
use util::kvdb::{KeyValueDB, CompactionProfile};

use self::fetch::ChainDataFetcher;
Expand Down Expand Up @@ -619,17 +618,8 @@ impl<T: ChainDataFetcher> ::ethcore::client::EngineClient for Client<T> {
Client::chain_info(self)
}

fn call_contract(&self, _id: BlockId, _address: Address, _data: Vec<u8>) -> Result<Vec<u8>, String> {
Err("Contract calling not supported by light client".into())
}

fn transact_contract(&self, _address: Address, _data: Vec<u8>)
-> Result<TransactionImportResult, EthcoreError>
{
// TODO: these are only really used for misbehavior reporting.
// no relevant clients will be running light clients, but maybe
// they could be at some point?
Err(TransactionError::LimitReached.into())
fn as_full_client(&self) -> Option<&::ethcore::client::BlockChainClient> {
None
}

fn block_number(&self, id: BlockId) -> Option<BlockNumber> {
Expand Down
8 changes: 1 addition & 7 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1960,13 +1960,7 @@ impl super::traits::EngineClient for Client {
BlockChainClient::chain_info(self)
}

fn call_contract(&self, id: BlockId, address: Address, data: Bytes) -> Result<Bytes, String> {
BlockChainClient::call_contract(self, id, address, data)
}

fn transact_contract(&self, address: Address, data: Bytes) -> Result<TransactionImportResult, EthcoreError> {
BlockChainClient::transact_contract(self, address, data)
}
fn as_full_client(&self) -> Option<&BlockChainClient> { Some(self) }

fn block_number(&self, id: BlockId) -> Option<BlockNumber> {
BlockChainClient::block_number(self, id)
Expand Down
8 changes: 1 addition & 7 deletions ethcore/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,13 +828,7 @@ impl super::traits::EngineClient for TestBlockChainClient {
BlockChainClient::chain_info(self)
}

fn call_contract(&self, id: BlockId, address: Address, data: Bytes) -> Result<Bytes, String> {
BlockChainClient::call_contract(self, id, address, data)
}

fn transact_contract(&self, address: Address, data: Bytes) -> Result<TransactionImportResult, EthcoreError> {
BlockChainClient::transact_contract(self, address, data)
}
fn as_full_client(&self) -> Option<&BlockChainClient> { Some(self) }

fn block_number(&self, id: BlockId) -> Option<BlockNumber> {
BlockChainClient::block_number(self, id)
Expand Down
8 changes: 3 additions & 5 deletions ethcore/src/client/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,10 @@ pub trait EngineClient: Sync + Send {
/// Get block chain info.
fn chain_info(&self) -> BlockChainInfo;

/// Like `call`, but with various defaults. Designed to be used for calling contracts.
fn call_contract(&self, id: BlockId, address: Address, data: Bytes) -> Result<Bytes, String>;

/// Import a transaction: used for misbehaviour reporting.
fn transact_contract(&self, address: Address, data: Bytes) -> Result<TransactionImportResult, EthcoreError>;
/// Attempt to cast the engine client to a full client.
fn as_full_client(&self) -> Option<&BlockChainClient>;

/// Get a block number by ID.
fn block_number(&self, id: BlockId) -> Option<BlockNumber>;
}

Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/engines/tendermint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use bigint::hash::{H256, H520};
use parking_lot::RwLock;
use util::*;
use unexpected::{OutOfBounds, Mismatch};
use client::{Client, EngineClient};
use client::EngineClient;
use error::{Error, BlockError};
use header::{Header, BlockNumber};
use builtin::Builtin;
Expand Down
8 changes: 7 additions & 1 deletion ethcore/src/engines/validator_set/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ impl ValidatorContract {
Box::new(move |a, d| client.as_ref()
.and_then(Weak::upgrade)
.ok_or("No client!".into())
.and_then(|c| c.transact_contract(a, d).map_err(|e| format!("Transaction import error: {}", e)))
.and_then(|c| {
match c.as_full_client() {
Some(c) => c.transact_contract(a, d)
.map_err(|e| format!("Transaction import error: {}", e)),
None => Err("No full client!".into()),
}
})
.map(|_| Default::default()))
}
}
Expand Down
7 changes: 6 additions & 1 deletion ethcore/src/engines/validator_set/safe_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,12 @@ impl ValidatorSet for ValidatorSafeContract {
Box::new(move |addr, data| client.as_ref()
.and_then(Weak::upgrade)
.ok_or("No client!".into())
.and_then(|c| c.call_contract(id, addr, data))
.and_then(|c| {
match c.as_full_client() {
Some(c) => c.call_contract(id, addr, data),
None => Err("No full client!".into()),
}
})
.map(|out| (out, Vec::new()))) // generate no proofs in general
}

Expand Down
6 changes: 3 additions & 3 deletions ethcore/src/ethereum/ethash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use rlp::{self, UntrustedRlp};
use vm::LastHashes;
use semantic_version::SemanticVersion;
use tx_filter::{TransactionFilter};
use client::{Client, BlockChainClient};
use client::EngineClient;

/// Parity tries to round block.gas_limit to multiple of this constant
pub const PARITY_GAS_LIMIT_DETERMINANT: U256 = U256([37, 0, 0, 0]);
Expand Down Expand Up @@ -460,9 +460,9 @@ impl Engine for Arc<Ethash> {
Some(Box::new(::snapshot::PowSnapshot::new(SNAPSHOT_BLOCKS, MAX_SNAPSHOT_BLOCKS)))
}

fn register_client(&self, client: Weak<Client>) {
fn register_client(&self, client: Weak<EngineClient>) {
if let Some(ref filter) = self.tx_filter {
filter.register_client(client as Weak<BlockChainClient>);
filter.register_client(client);
}
}

Expand Down
15 changes: 11 additions & 4 deletions ethcore/src/tx_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
use std::sync::Weak;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use bigint::hash::H256;
use native_contracts::TransactAcl as Contract;
use client::{BlockChainClient, BlockId, ChainNotify};
use util::{Address, H256, Bytes};
use client::{EngineClient, BlockId, ChainNotify};
use util::{Address, Bytes};
use parking_lot::{Mutex, RwLock};
use futures::{self, Future};
use spec::CommonParams;
Expand All @@ -42,7 +43,7 @@ mod tx_permissions {
/// Connection filter that uses a contract to manage permissions.
pub struct TransactionFilter {
contract: Mutex<Option<Contract>>,
client: RwLock<Option<Weak<BlockChainClient>>>,
client: RwLock<Option<Weak<EngineClient>>>,
contract_address: Address,
permission_cache: Mutex<HashMap<(H256, Address), u32>>,
}
Expand All @@ -66,7 +67,7 @@ impl TransactionFilter {
}

/// Set client reference to be used for contract call.
pub fn register_client(&self, client: Weak<BlockChainClient>) {
pub fn register_client(&self, client: Weak<EngineClient>) {
*self.client.write() = Some(client);
}

Expand All @@ -78,6 +79,12 @@ impl TransactionFilter {
Some(client) => client,
_ => return false,
};

let client = match client.as_full_client() {
Some(client) => client,
_ => return false, // TODO: how to handle verification for light clients?
};

let tx_type = match transaction.action {
Action::Create => tx_permissions::CREATE,
Action::Call(address) => if client.code_hash(&address, BlockId::Hash(*parent_hash)).map_or(false, |c| c != KECCAK_EMPTY) {
Expand Down

0 comments on commit 7d1c7a0

Please sign in to comment.