Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ethcore/light/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,12 @@ impl<T: ChainDataFetcher> ::ethcore::client::ChainInfo for Client<T> {
}
}

impl<T: ChainDataFetcher> ::ethcore::client::Nonce for Client<T> {
fn nonce(&self, _address: &ethereum_types::H160, _blockid: ethcore::client::BlockId) -> std::option::Option<ethereum_types::U256> {
panic!("we never call this function on a light client, so this is unreachable; qed")
}
}

impl<T: ChainDataFetcher> ::ethcore::client::EngineClient for Client<T> {
fn update_sealing(&self) { }
fn submit_seal(&self, _block_hash: H256, _seal: Vec<Vec<u8>>) { }
Expand Down
25 changes: 24 additions & 1 deletion ethcore/res/contracts/validator_set_aura.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,28 @@
],
"name": "InitiateChange",
"type": "event"
},
{
"constant": true,
"inputs": [
{
"name": "_maliciousMiningAddress",
"type": "address"
},
{
"name": "_blockNumber",
"type": "uint256"
}
],
"name": "maliceReportedForBlock",
"outputs": [
{
"name": "",
"type": "address[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
]
7 changes: 7 additions & 0 deletions ethcore/res/contracts/validator_set_aura.json.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
getValidators
initiateChange
emitInitiateChangeCallable
emitInitiateChange
maliceReportedForBlock
finalizeChange
InitiateChange
4 changes: 2 additions & 2 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2187,10 +2187,10 @@ impl BlockChainClient for Client {
Ok(SignedTransaction::new(transaction.with_signature(signature, chain_id))?)
}

fn transact(&self, action: Action, data: Bytes, gas: Option<U256>, gas_price: Option<U256>)
fn transact(&self, action: Action, data: Bytes, gas: Option<U256>, gas_price: Option<U256>, nonce: Option<U256>)
-> Result<(), transaction::Error>
{
let signed = self.create_transaction(action, data, gas, gas_price, None)?;
let signed = self.create_transaction(action, data, gas, gas_price, nonce)?;
self.importer.miner.import_own_transaction(self, signed.into())
}

Expand Down
10 changes: 8 additions & 2 deletions ethcore/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,8 +896,14 @@ impl BlockChainClient for TestBlockChainClient {
Ok(SignedTransaction::new(transaction.with_signature(sig, chain_id)).unwrap())
}

fn transact(&self, action: Action, data: Bytes, gas: Option<U256>, gas_price: Option<U256>)
-> Result<(), transaction::Error>
fn transact(
&self,
action: Action,
data: Bytes,
gas: Option<U256>,
gas_price: Option<U256>,
_nonce: Option<U256>,
) -> Result<(), transaction::Error>
{
let signed = self.create_transaction(action, data, gas, gas_price, None)?;
self.miner.import_own_transaction(self, signed.into())
Expand Down
14 changes: 8 additions & 6 deletions ethcore/src/client/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,12 @@ pub trait BlockChainClient : Sync + Send + AccountData + BlockChain + CallContra
fn pruning_info(&self) -> PruningInfo;

/// Schedule state-altering transaction to be executed on the next pending block.
fn transact_contract(&self, address: Address, data: Bytes) -> Result<(), transaction::Error> {
self.transact(Action::Call(address), data, None, None)
fn transact_contract(&self, address: Address, data: Bytes, nonce: Option<U256>) -> Result<(), transaction::Error> {
self.transact(Action::Call(address), data, None, None, nonce)
}

/// Returns a signed transaction. If gas limit, gas price, or nonce are not specified, the defaults are used.
/// Returns a signed transaction. If gas limit, gas price, or nonce are not
/// specified, the defaults are used.
fn create_transaction(
&self,
action: Action,
Expand All @@ -390,10 +391,11 @@ pub trait BlockChainClient : Sync + Send + AccountData + BlockChain + CallContra
nonce: Option<U256>
) -> Result<SignedTransaction, transaction::Error>;

/// Schedule state-altering transaction to be executed on the next pending block with the given gas parameters.
/// Schedule state-altering transaction to be executed on the next pending
/// block with the given gas and nonce parameters.
///
/// If they are `None`, sensible values are selected automatically.
fn transact(&self, action: Action, data: Bytes, gas: Option<U256>, gas_price: Option<U256>)
fn transact(&self, action: Action, data: Bytes, gas: Option<U256>, gas_price: Option<U256>, nonce: Option<U256>)
-> Result<(), transaction::Error>;

/// Get the address of the registry itself.
Expand Down Expand Up @@ -441,7 +443,7 @@ pub trait BroadcastProposalBlock {
pub trait SealedBlockImporter: ImportSealedBlock + BroadcastProposalBlock {}

/// Client facilities used by internally sealing Engines.
pub trait EngineClient: Sync + Send + ChainInfo {
pub trait EngineClient: Sync + Send + ChainInfo + Nonce {
/// Make a new block and seal it.
fn update_sealing(&self);

Expand Down
7 changes: 6 additions & 1 deletion ethcore/src/engines/authority_round/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,12 @@ impl Engine<EthereumMachine> for AuthorityRound {
},
};

block_reward::apply_block_rewards(&rewards, block, &self.machine)
block_reward::apply_block_rewards(&rewards, block, &self.machine)?;

match self.signer.read().as_ref() {
Some(signer) => self.validators.on_close_block(block.header(), &signer.address()),
None => Ok(()), // We are not a validator, so we can't report malicious validators.
}
}

/// Make calls to the randomness and validator set contracts.
Expand Down
40 changes: 21 additions & 19 deletions ethcore/src/engines/validator_set/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use machine::{AuxiliaryData, Call, EthereumMachine};
use parking_lot::RwLock;
use types::BlockNumber;
use types::header::Header;
use types::transaction::Action;
use types::transaction::{self, Action};

use client::EngineClient;

Expand All @@ -38,7 +38,7 @@ use_contract!(validator_report, "res/contracts/validator_report.json");
pub struct ValidatorContract {
contract_address: Address,
validators: ValidatorSafeContract,
client: RwLock<Option<Weak<EngineClient>>>, // TODO [keorn]: remove
client: RwLock<Option<Weak<dyn EngineClient>>>, // TODO [keorn]: remove
}

impl ValidatorContract {
Expand All @@ -52,18 +52,13 @@ impl ValidatorContract {
}

impl ValidatorContract {
fn transact(&self, data: Bytes) -> Result<(), String> {
let client = self.client.read().as_ref()
.and_then(Weak::upgrade)
.ok_or_else(|| "No client!")?;

match client.as_full_client() {
Some(c) => {
c.transact(Action::Call(self.contract_address), data, None, Some(0.into()))
.map_err(|e| format!("Transaction import error: {}", e))?;
Ok(())
},
None => Err("No full client!".into()),
fn transact(&self, data: Bytes) -> Result<(), ::error::Error> {
let client = self.client.read().as_ref().and_then(Weak::upgrade).ok_or("No client!")?;
let full_client = client.as_full_client().ok_or("No full client!")?;

match full_client.transact(Action::Call(self.contract_address), data, None, Some(0.into()), None) {
Ok(()) | Err(transaction::Error::AlreadyImported) => Ok(()),
Err(e) => Err(e)?,
}
}
}
Expand All @@ -84,6 +79,10 @@ impl ValidatorSet for ValidatorContract {
self.validators.on_epoch_begin(first, header, call)
}

fn on_close_block(&self, header: &Header, address: &Address) -> Result<(), ::error::Error> {
self.validators.on_close_block(header, address)
}

fn genesis_epoch_data(&self, header: &Header, call: &Call) -> Result<Vec<u8>, String> {
self.validators.genesis_epoch_data(header, call)
}
Expand Down Expand Up @@ -117,18 +116,21 @@ impl ValidatorSet for ValidatorContract {
self.validators.count_with_caller(bh, caller)
}

fn report_malicious(&self, address: &Address, _set_block: BlockNumber, block: BlockNumber, proof: Bytes) {
fn report_malicious(&self, address: &Address, set_block: BlockNumber, block: BlockNumber, proof: Bytes) {
let data = validator_report::functions::report_malicious::encode_input(*address, block, proof);
self.validators.queue_report((*address, set_block, data.clone()));
match self.transact(data) {
Ok(_) => warn!(target: "engine", "Reported malicious validator {}", address),
Err(s) => warn!(target: "engine", "Validator {} could not be reported {}", address, s),
Ok(()) => warn!(target: "engine", "Reported malicious validator {} at block {}", address, set_block),
Err(s) => {
warn!(target: "engine", "Validator {} could not be reported ({}) on block {}", address, s, set_block);
}
}
}

fn report_benign(&self, address: &Address, _set_block: BlockNumber, block: BlockNumber) {
let data = validator_report::functions::report_benign::encode_input(*address, block);
match self.transact(data) {
Ok(_) => warn!(target: "engine", "Reported benign validator misbehaviour {}", address),
Ok(()) => warn!(target: "engine", "Reported benign validator misbehaviour {}", address),
Err(s) => warn!(target: "engine", "Validator {} could not be reported {}", address, s),
}
}
Expand Down Expand Up @@ -223,7 +225,7 @@ mod tests {
assert_eq!(client.chain_info().best_block_number, 2);

// Check if misbehaving validator was removed.
client.transact_contract(Default::default(), Default::default()).unwrap();
client.transact_contract(Default::default(), Default::default(), Default::default()).unwrap();
client.engine().step();
client.engine().step();
assert_eq!(client.chain_info().best_block_number, 2);
Expand Down
6 changes: 5 additions & 1 deletion ethcore/src/engines/validator_set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ pub trait ValidatorSet: Send + Sync + 'static {
Ok(())
}

#[cfg(all())]
/// Called on the close of every block.
fn on_close_block(&self, _header: &Header, _address: &Address) -> Result<(), ::error::Error> {
Ok(())
}

/// Called for each new block this node is creating. If this block is
/// the first block of an epoch, this is called *after* on_epoch_begin(),
/// but with the same parameters.
Expand Down
9 changes: 6 additions & 3 deletions ethcore/src/engines/validator_set/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ impl ValidatorSet for Multi {
self.map_children(header, &mut |set: &dyn ValidatorSet, first| set.on_prepare_block(first, header, call))
}

fn on_close_block(&self, header: &Header, address: &Address) -> Result<(), ::error::Error> {
self.map_children(header, &mut |set: &dyn ValidatorSet, _first| set.on_close_block(header, address))
}

fn on_epoch_begin(&self, _first: bool, header: &Header, call: &mut SystemCall) -> Result<(), ::error::Error> {
self.map_children(header, &mut |set: &dyn ValidatorSet, first| set.on_epoch_begin(first, header, call))
Expand Down Expand Up @@ -192,7 +195,7 @@ mod tests {
// Wrong signer for the first block.
let signer = Box::new((tap.clone(), v1, "".into()));
client.miner().set_author(miner::Author::Sealer(signer));
client.transact_contract(Default::default(), Default::default()).unwrap();
client.transact_contract(Default::default(), Default::default(), Default::default()).unwrap();
::client::EngineClient::update_sealing(&*client);
assert_eq!(client.chain_info().best_block_number, 0);
// Right signer for the first block.
Expand All @@ -201,15 +204,15 @@ mod tests {
::client::EngineClient::update_sealing(&*client);
assert_eq!(client.chain_info().best_block_number, 1);
// This time v0 is wrong.
client.transact_contract(Default::default(), Default::default()).unwrap();
client.transact_contract(Default::default(), Default::default(), Default::default()).unwrap();
::client::EngineClient::update_sealing(&*client);
assert_eq!(client.chain_info().best_block_number, 1);
let signer = Box::new((tap.clone(), v1, "".into()));
client.miner().set_author(miner::Author::Sealer(signer));
::client::EngineClient::update_sealing(&*client);
assert_eq!(client.chain_info().best_block_number, 2);
// v1 is still good.
client.transact_contract(Default::default(), Default::default()).unwrap();
client.transact_contract(Default::default(), Default::default(), Default::default()).unwrap();
::client::EngineClient::update_sealing(&*client);
assert_eq!(client.chain_info().best_block_number, 3);

Expand Down
Loading