Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue failed reports for later insertion into blocks #98

Merged
merged 18 commits into from
Mar 5, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions ethcore/light/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,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 @@ -2133,10 +2133,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 @@ -880,8 +880,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 @@ -388,11 +388,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 @@ -402,10 +403,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 @@ -453,7 +455,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 @@ -1215,7 +1215,12 @@ impl Engine<EthereumMachine> for AuthorityRound {
},
};

block_reward::apply_block_rewards(&rewards, block, &self.machine)
block_reward::apply_block_rewards(&rewards, block, &self.machine)?;
if let Some(ref address) = self.signer.read().address() {
self.validators.on_close_block(block.header(), address)
} else {
Ok(())
}
}

/// Make calls to the randomness and validator set contracts.
Expand Down
32 changes: 20 additions & 12 deletions ethcore/src/engines/validator_set/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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 @@ -51,16 +51,17 @@ impl ValidatorContract {
}

impl ValidatorContract {
fn transact(&self, data: Bytes) -> Result<(), String> {
fn transact(&self, data: Bytes) -> Result<(), ::error::Error> {
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(())
match c.transact(Action::Call(self.contract_address), data, None, Some(0.into()), None) {
Ok(()) | Err(::transaction::Error::AlreadyImported) => Ok(()),
Err(e) => Err(e)?,
}
},
None => Err("No full client!".into()),
}
Expand All @@ -83,6 +84,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 @@ -116,18 +121,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);
match self.transact(data) {
Ok(_) => warn!(target: "engine", "Reported malicious validator {}", address),
Err(s) => warn!(target: "engine", "Validator {} could not be reported {}", address, s),
match self.transact(data.clone()) {
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);
self.validators.queue_report((*address, set_block, data))
}
}
}

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 All @@ -143,7 +151,7 @@ mod tests {
use std::sync::Arc;
use rustc_hex::FromHex;
use hash::keccak;
use ethereum_types::{U256, H520, Address};
use ethereum_types::{H520, Address};
use bytes::ToPretty;
use rlp::encode;
use spec::Spec;
Expand Down Expand Up @@ -220,7 +228,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 @@ -89,7 +89,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 @@ -88,6 +88,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 @@ -188,22 +191,22 @@ mod tests {

// Wrong signer for the first block.
client.miner().set_author(v1, Some("".into())).unwrap();
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.
client.miner().set_author(v0, Some("".into())).unwrap();
::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);
client.miner().set_author(v1, Some("".into())).unwrap();
::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