Skip to content

Commit

Permalink
Allow specifying local accounts via CLI
Browse files Browse the repository at this point in the history
 * Add `tx-queue-locals` CLI option
 * ethcore: modify miner to check options vec before importing transaction
 * modify tests (ethcore/parity)
Resolves openethereum#9634
  • Loading branch information
Andrew Plaza authored and insipx committed Dec 18, 2018
1 parent 3f91699 commit ba4cc59
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 50 deletions.
48 changes: 5 additions & 43 deletions ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub struct MinerOptions {
pub infinite_pending_block: bool,

/// Prioritized Local Addresses
pub tx_queue_locals: HashSet<Address>,
pub tx_queue_locals: Vec<Address>,
/// Strategy to use for prioritizing transactions in the queue.
pub tx_queue_strategy: PrioritizationStrategy,
/// Simple senders penalization.
Expand Down Expand Up @@ -165,7 +165,7 @@ impl Default for MinerOptions {
work_queue_size: 20,
enable_resubmission: true,
infinite_pending_block: false,
tx_queue_locals: HashSet::new(),
tx_queue_locals: Vec::new(),
tx_queue_strategy: PrioritizationStrategy::GasPriceOnly,
tx_queue_penalization: Penalization::Disabled,
tx_queue_no_unfamiliar_locals: false,
Expand Down Expand Up @@ -917,12 +917,12 @@ impl miner::MinerService for Miner {
trusted: bool
) -> Result<(), transaction::Error> {
// treat the tx as local if the option is enabled, if we have the account, or if
// the account is specified as a Prioritized Local Addresses
// the acc is specified as local
let sender = pending.sender();
let treat_as_local = trusted
|| !self.options.tx_queue_no_unfamiliar_locals
|| self.accounts.as_ref().map(|accts| accts.has_account(sender)).unwrap_or(false)
|| self.options.tx_queue_locals.contains(&sender);
|| self.options.tx_queue_locals.iter().any(|addr| *addr == sender);

if treat_as_local {
self.import_own_transaction(chain, pending)
Expand Down Expand Up @@ -1286,8 +1286,6 @@ impl miner::MinerService for Miner {

#[cfg(test)]
mod tests {
use std::iter::FromIterator;

use super::*;
use ethkey::{Generator, Random};
use hash::keccak;
Expand Down Expand Up @@ -1346,7 +1344,7 @@ mod tests {
enable_resubmission: true,
infinite_pending_block: false,
tx_queue_penalization: Penalization::Disabled,
tx_queue_locals: HashSet::new(),
tx_queue_locals: Vec::new(),
tx_queue_strategy: PrioritizationStrategy::GasPriceOnly,
tx_queue_no_unfamiliar_locals: false,
refuse_service_transactions: false,
Expand Down Expand Up @@ -1515,42 +1513,6 @@ mod tests {
assert_eq!(miner.prepare_pending_block(&client), BlockPreparationStatus::NotPrepared);
}

#[test]
fn should_prioritize_locals() {
// Test borrowed from `should_treat_unfamiliar_locals_selectively` and modified for tx_queue_locals (PR#9960)
// given
let keypair = Random.generate().unwrap();
let client = TestBlockChainClient::default();
let account_provider = AccountProvider::transient_provider();
account_provider.insert_account(keypair.secret().clone(), &"".into()).expect("can add accounts to the provider we just created");

let transaction = transaction();
let miner = Miner::new(
MinerOptions {
tx_queue_no_unfamiliar_locals: true, // should work even with this enabled
tx_queue_locals: HashSet::from_iter(vec![transaction.sender()].into_iter()),
..miner().options
},
GasPricer::new_fixed(0u64.into()),
&Spec::new_test(),
Some(Arc::new(account_provider)),
);
let best_block = 0;

// when
// Miner with sender as a known local address should prioritize transactions from that address
let res2 = miner.import_claimed_local_transaction(&client, PendingTransaction::new(transaction, None), false);

// then
// check to make sure the prioritized transaction is pending
// This is borrowed from `should_make_pending_block_when_importing_own_transaction` and slightly modified.
assert_eq!(res2.unwrap(), ());
assert_eq!(miner.pending_transactions(best_block).unwrap().len(), 1);
assert_eq!(miner.pending_receipts(best_block).unwrap().len(), 1);
assert_eq!(miner.ready_transactions(&client, 10, PendingOrdering::Priority).len(), 1);
assert_eq!(miner.prepare_pending_block(&client), BlockPreparationStatus::NotPrepared);
}

#[test]
fn should_not_seal_unless_enabled() {
let miner = miner();
Expand Down
6 changes: 2 additions & 4 deletions parity/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
mod usage;
mod presets;

use std::collections::HashSet;

usage! {
{
// CLI subcommands
Expand Down Expand Up @@ -763,7 +761,7 @@ usage! {
"--tx-queue-per-sender=[LIMIT]",
"Maximum number of transactions per sender in the queue. By default it's 1% of the entire queue, but not less than 16.",

ARG arg_tx_queue_locals: (Option<String>) = None, or |c: &Config| c.mining.as_ref()?.tx_queue_locals.as_ref().map(|set| set.iter().map(|s| s.as_str()).collect::<Vec<&str>>().join(",")),
ARG arg_tx_queue_locals: (Option<String>) = None, or |c: &Config| c.mining.as_ref()?.tx_queue_locals.as_ref().map(|vec| vec.join(",")),
"--tx-queue-locals=[ACCOUNTS]",
"Specify local accounts for which transactions are prioritized in the queue. ACCOUNTS is a comma-delimited list of addresses.",

Expand Down Expand Up @@ -1354,7 +1352,7 @@ struct Mining {
tx_queue_size: Option<usize>,
tx_queue_per_sender: Option<usize>,
tx_queue_mem_limit: Option<u32>,
tx_queue_locals: Option<HashSet<String>>,
tx_queue_locals: Option<Vec<String>>,
tx_queue_strategy: Option<String>,
tx_queue_ban_count: Option<u16>,
tx_queue_ban_time: Option<u16>,
Expand Down
5 changes: 2 additions & 3 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use std::time::Duration;
use std::io::Read;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::collections::{HashSet, BTreeMap};
use std::iter::FromIterator;
use std::collections::BTreeMap;
use std::cmp;
use cli::{Args, ArgsError};
use hash::keccak;
Expand Down Expand Up @@ -560,7 +559,7 @@ impl Configuration {
infinite_pending_block: self.args.flag_infinite_pending_block,

tx_queue_penalization: to_queue_penalization(self.args.arg_tx_time_limit)?,
tx_queue_locals: HashSet::from_iter(to_addresses(&self.args.arg_tx_queue_locals)?.into_iter()),
tx_queue_locals: to_addresses(&self.args.arg_tx_queue_locals)?,
tx_queue_strategy: to_queue_strategy(&self.args.arg_tx_queue_strategy)?,
tx_queue_no_unfamiliar_locals: self.args.flag_tx_queue_no_unfamiliar_locals,
refuse_service_transactions: self.args.flag_refuse_service_transactions,
Expand Down

0 comments on commit ba4cc59

Please sign in to comment.