Mode improvements for UI (#3109)

* `--mode=off` now works.

* Add Mode::Off as a persistent CLI option.

* "last" not "auto" as default.

* Commit accidentally unsaved file.

* Whitespace

[ci:skip]

* Mode CLI parse fix

* or offline

* Save mode when it gets changed.

* Fix Offline mode

* Fix up mode operations.

* Make passive default, but not overriding.

* Fix test

* Maybe not everyone wants to run an archive node...
This commit is contained in:
Gav Wood 2016-11-05 10:38:00 +01:00 committed by GitHub
parent 8351c5d500
commit 744501c454
20 changed files with 154 additions and 66 deletions

View File

@ -147,6 +147,7 @@ pub struct Client {
factories: Factories, factories: Factories,
history: u64, history: u64,
rng: Mutex<OsRng>, rng: Mutex<OsRng>,
on_mode_change: Mutex<Option<Box<FnMut(&Mode) + 'static + Send>>>,
} }
impl Client { impl Client {
@ -211,7 +212,7 @@ impl Client {
let panic_handler = PanicHandler::new_in_arc(); let panic_handler = PanicHandler::new_in_arc();
panic_handler.forward_from(&block_queue); panic_handler.forward_from(&block_queue);
let awake = match config.mode { Mode::Dark(..) => false, _ => true }; let awake = match config.mode { Mode::Dark(..) | Mode::Off => false, _ => true };
let factories = Factories { let factories = Factories {
vm: EvmFactory::new(config.vm_type.clone(), config.jump_table_size), vm: EvmFactory::new(config.vm_type.clone(), config.jump_table_size),
@ -243,6 +244,7 @@ impl Client {
factories: factories, factories: factories,
history: history, history: history,
rng: Mutex::new(try!(OsRng::new().map_err(::util::UtilError::StdIo))), rng: Mutex::new(try!(OsRng::new().map_err(::util::UtilError::StdIo))),
on_mode_change: Mutex::new(None),
}; };
Ok(Arc::new(client)) Ok(Arc::new(client))
} }
@ -260,6 +262,11 @@ impl Client {
} }
} }
/// Register an action to be done if a mode change happens.
pub fn on_mode_change<F>(&self, f: F) where F: 'static + FnMut(&Mode) + Send {
*self.on_mode_change.lock() = Some(Box::new(f));
}
/// Flush the block import queue. /// Flush the block import queue.
pub fn flush_queue(&self) { pub fn flush_queue(&self) {
self.block_queue.flush(); self.block_queue.flush();
@ -856,18 +863,37 @@ impl BlockChainClient for Client {
} }
fn keep_alive(&self) { fn keep_alive(&self) {
let mode = self.mode.lock().clone(); let should_wake = match &*self.mode.lock() {
if mode != Mode::Active { &Mode::Dark(..) | &Mode::Passive(..) => true,
_ => false,
};
if should_wake {
self.wake_up(); self.wake_up();
(*self.sleep_state.lock()).last_activity = Some(Instant::now()); (*self.sleep_state.lock()).last_activity = Some(Instant::now());
} }
} }
fn mode(&self) -> IpcMode { self.mode.lock().clone().into() } fn mode(&self) -> IpcMode {
let r = self.mode.lock().clone().into();
trace!(target: "mode", "Asked for mode = {:?}. returning {:?}", &*self.mode.lock(), r);
r
}
fn set_mode(&self, mode: IpcMode) { fn set_mode(&self, new_mode: IpcMode) {
*self.mode.lock() = mode.clone().into(); trace!(target: "mode", "Client::set_mode({:?})", new_mode);
match mode { {
let mut mode = self.mode.lock();
*mode = new_mode.clone().into();
trace!(target: "mode", "Mode now {:?}", &*mode);
match *self.on_mode_change.lock() {
Some(ref mut f) => {
trace!(target: "mode", "Making callback...");
f(&*mode)
},
_ => {}
}
}
match new_mode {
IpcMode::Active => self.wake_up(), IpcMode::Active => self.wake_up(),
IpcMode::Off => self.sleep(), IpcMode::Off => self.sleep(),
_ => {(*self.sleep_state.lock()).last_activity = Some(Instant::now()); } _ => {(*self.sleep_state.lock()).last_activity = Some(Instant::now()); }

View File

@ -16,6 +16,7 @@
use std::str::FromStr; use std::str::FromStr;
use std::path::Path; use std::path::Path;
use std::fmt::{Display, Formatter, Error as FmtError};
pub use std::time::Duration; pub use std::time::Duration;
pub use blockchain::Config as BlockChainConfig; pub use blockchain::Config as BlockChainConfig;
pub use trace::Config as TraceConfig; pub use trace::Config as TraceConfig;
@ -86,6 +87,17 @@ impl Default for Mode {
} }
} }
impl Display for Mode {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
match *self {
Mode::Active => write!(f, "active"),
Mode::Passive(..) => write!(f, "passive"),
Mode::Dark(..) => write!(f, "dark"),
Mode::Off => write!(f, "offline"),
}
}
}
/// Client configuration. Includes configs for all sub-systems. /// Client configuration. Includes configs for all sub-systems.
#[derive(Debug, PartialEq, Default)] #[derive(Debug, PartialEq, Default)]
pub struct ClientConfig { pub struct ClientConfig {

View File

@ -20,7 +20,7 @@ pub use std::time::Duration;
use client::Mode as ClientMode; use client::Mode as ClientMode;
/// IPC-capable shadow-type for client::config::Mode /// IPC-capable shadow-type for client::config::Mode
#[derive(Clone, Binary)] #[derive(Clone, Binary, Debug)]
pub enum Mode { pub enum Mode {
/// Same as ClientMode::Off. /// Same as ClientMode::Off.
Off, Off,

View File

@ -166,7 +166,7 @@ export default {
}, },
mode: { mode: {
desc: 'Get the mode. Results one of: "active", "passive", "dark", "off".', desc: 'Get the mode. Results one of: "active", "passive", "dark", "offline".',
params: [], params: [],
returns: { returns: {
type: String, type: String,
@ -330,7 +330,7 @@ export default {
params: [ params: [
{ {
type: String, type: String,
desc: 'The mode to set, one of "active", "passive", "dark", "off"' desc: 'The mode to set, one of "active", "passive", "dark", "offline"'
} }
], ],
returns: { returns: {

View File

@ -926,6 +926,16 @@
], ],
"outputFormatter": null "outputFormatter": null
}, },
{
"name": "ethcore_setMode",
"desc": "Changes current operating mode",
"params": [
"`DATA`- Mode name"
],
"returns": "`Boolean` - whether the call was successful",
"inputFormatters": null,
"outputFormatter": null
},
{ {
"name": "ethcore_setMinGasPrice", "name": "ethcore_setMinGasPrice",
"desc": "Changes minimal gas price for transaction to be accepted to the queue.", "desc": "Changes minimal gas price for transaction to be accepted to the queue.",
@ -1030,6 +1040,14 @@
"inputFormatters": [], "inputFormatters": [],
"outputFormatter": null "outputFormatter": null
}, },
{
"name": "ethcore_mode",
"desc": "Returns current mode",
"params": [],
"returns": "`DATA` - Mode",
"inputFormatters": [],
"outputFormatter": null
},
{ {
"name": "trace_filter", "name": "trace_filter",
"desc": "Returns traces matching given filter", "desc": "Returns traces matching given filter",

View File

@ -11,8 +11,6 @@ cat > $HOME/Library/LaunchAgents/io.parity.ethereum.plist <<EOF
<key>ProgramArguments</key> <key>ProgramArguments</key>
<array> <array>
<string>/usr/local/libexec/parity</string> <string>/usr/local/libexec/parity</string>
<string>--mode</string>
<string>passive</string>
<string>--warp</string> <string>--warp</string>
</array> </array>
<key>KeepAlive</key> <key>KeepAlive</key>
@ -26,9 +24,11 @@ cat > $HOME/Library/LaunchAgents/io.parity.ethereum.plist <<EOF
</dict> </dict>
</plist> </plist>
EOF EOF
mkdir -p $HOME/.parity
chown $USER $HOME/.parity $HOME/Library/LaunchAgents $HOME/Library/LaunchAgents/io.parity.ethereum.plist mkdir -p $HOME/.parity/906a34e69aec8c0d
echo -n '{"fat_db":false,"mode":"passive","pruning":"fast","tracing":false}' > $HOME/.parity/906a34e69aec8c0d/user_defaults
chown -R $USER $HOME/.parity $HOME/Library/LaunchAgents $HOME/Library/LaunchAgents/io.parity.ethereum.plist
su $USER -c "launchctl load $HOME/Library/LaunchAgents/io.parity.ethereum.plist" su $USER -c "launchctl load $HOME/Library/LaunchAgents/io.parity.ethereum.plist"
sleep 1 sleep 1

View File

@ -78,7 +78,6 @@ pub struct ImportBlockchain {
pub pruning_history: u64, pub pruning_history: u64,
pub compaction: DatabaseCompactionProfile, pub compaction: DatabaseCompactionProfile,
pub wal: bool, pub wal: bool,
pub mode: Mode,
pub tracing: Switch, pub tracing: Switch,
pub fat_db: Switch, pub fat_db: Switch,
pub vm_type: VMType, pub vm_type: VMType,
@ -97,7 +96,6 @@ pub struct ExportBlockchain {
pub pruning_history: u64, pub pruning_history: u64,
pub compaction: DatabaseCompactionProfile, pub compaction: DatabaseCompactionProfile,
pub wal: bool, pub wal: bool,
pub mode: Mode,
pub fat_db: Switch, pub fat_db: Switch,
pub tracing: Switch, pub tracing: Switch,
pub from_block: BlockID, pub from_block: BlockID,
@ -155,7 +153,7 @@ fn execute_import(cmd: ImportBlockchain) -> Result<String, String> {
try!(execute_upgrades(&db_dirs, algorithm, cmd.compaction.compaction_profile(db_dirs.fork_path().as_path()))); try!(execute_upgrades(&db_dirs, algorithm, cmd.compaction.compaction_profile(db_dirs.fork_path().as_path())));
// prepare client config // prepare client config
let client_config = to_client_config(&cmd.cache_config, cmd.mode, tracing, fat_db, cmd.compaction, cmd.wal, cmd.vm_type, "".into(), algorithm, cmd.pruning_history, cmd.check_seal); let client_config = to_client_config(&cmd.cache_config, Mode::Active, tracing, fat_db, cmd.compaction, cmd.wal, cmd.vm_type, "".into(), algorithm, cmd.pruning_history, cmd.check_seal);
// build client // build client
let service = try!(ClientService::start( let service = try!(ClientService::start(
@ -303,7 +301,7 @@ fn execute_export(cmd: ExportBlockchain) -> Result<String, String> {
try!(execute_upgrades(&db_dirs, algorithm, cmd.compaction.compaction_profile(db_dirs.fork_path().as_path()))); try!(execute_upgrades(&db_dirs, algorithm, cmd.compaction.compaction_profile(db_dirs.fork_path().as_path())));
// prepare client config // prepare client config
let client_config = to_client_config(&cmd.cache_config, cmd.mode, tracing, fat_db, cmd.compaction, cmd.wal, VMType::default(), "".into(), algorithm, cmd.pruning_history, cmd.check_seal); let client_config = to_client_config(&cmd.cache_config, Mode::Active, tracing, fat_db, cmd.compaction, cmd.wal, VMType::default(), "".into(), algorithm, cmd.pruning_history, cmd.check_seal);
let service = try!(ClientService::start( let service = try!(ClientService::start(
client_config, client_config,

View File

@ -1,5 +1,5 @@
[parity] [parity]
mode = "active" mode = "last"
mode_timeout = 300 mode_timeout = 300
mode_alarm = 3600 mode_alarm = 3600
chain = "homestead" chain = "homestead"

View File

@ -74,7 +74,7 @@ usage! {
} }
{ {
// -- Operating Options // -- Operating Options
flag_mode: String = "active", or |c: &Config| otry!(c.parity).mode.clone(), flag_mode: String = "last", or |c: &Config| otry!(c.parity).mode.clone(),
flag_mode_timeout: u64 = 300u64, or |c: &Config| otry!(c.parity).mode_timeout.clone(), flag_mode_timeout: u64 = 300u64, or |c: &Config| otry!(c.parity).mode_timeout.clone(),
flag_mode_alarm: u64 = 3600u64, or |c: &Config| otry!(c.parity).mode_alarm.clone(), flag_mode_alarm: u64 = 3600u64, or |c: &Config| otry!(c.parity).mode_alarm.clone(),
flag_chain: String = "homestead", or |c: &Config| otry!(c.parity).chain.clone(), flag_chain: String = "homestead", or |c: &Config| otry!(c.parity).chain.clone(),
@ -104,8 +104,6 @@ usage! {
flag_signer_no_validation: bool = false, or |_| None, flag_signer_no_validation: bool = false, or |_| None,
// -- Networking Options // -- Networking Options
flag_no_network: bool = false,
or |c: &Config| otry!(c.network).disable.clone(),
flag_warp: bool = false, flag_warp: bool = false,
or |c: &Config| otry!(c.network).warp.clone(), or |c: &Config| otry!(c.network).warp.clone(),
flag_port: u16 = 30303u16, flag_port: u16 = 30303u16,
@ -500,7 +498,7 @@ mod tests {
arg_path: vec![], arg_path: vec![],
// -- Operating Options // -- Operating Options
flag_mode: "active".into(), flag_mode: "last".into(),
flag_mode_timeout: 300u64, flag_mode_timeout: 300u64,
flag_mode_alarm: 3600u64, flag_mode_alarm: 3600u64,
flag_chain: "xyz".into(), flag_chain: "xyz".into(),
@ -521,7 +519,6 @@ mod tests {
flag_signer_no_validation: false, flag_signer_no_validation: false,
// -- Networking Options // -- Networking Options
flag_no_network: false,
flag_warp: true, flag_warp: true,
flag_port: 30303u16, flag_port: 30303u16,
flag_min_peers: 25u16, flag_min_peers: 25u16,

View File

@ -18,11 +18,12 @@ Usage:
Operating Options: Operating Options:
--mode MODE Set the operating mode. MODE can be one of: --mode MODE Set the operating mode. MODE can be one of:
last - Uses the last-used mode, active if none.
active - Parity continuously syncs the chain. active - Parity continuously syncs the chain.
passive - Parity syncs initially, then sleeps and passive - Parity syncs initially, then sleeps and
wakes regularly to resync. wakes regularly to resync.
dark - Parity syncs only when an external interface dark - Parity syncs only when the RPC is active.
is active. (default: {flag_mode}). offline - Parity doesn't sync. (default: {flag_mode}).
--mode-timeout SECS Specify the number of seconds before inactivity --mode-timeout SECS Specify the number of seconds before inactivity
timeout occurs when mode is dark or passive timeout occurs when mode is dark or passive
(default: {flag_mode_timeout}). (default: {flag_mode_timeout}).
@ -66,7 +67,6 @@ Account Options:
development. (default: {flag_signer_no_validation}) development. (default: {flag_signer_no_validation})
Networking Options: Networking Options:
--no-network Disable p2p networking. (default: {flag_no_network})
--warp Enable syncing from the snapshot over the network. (default: {flag_warp}) --warp Enable syncing from the snapshot over the network. (default: {flag_warp})
--port PORT Override the port on which the node should listen --port PORT Override the port on which the node should listen
(default: {flag_port}). (default: {flag_port}).

View File

@ -23,7 +23,7 @@ use cli::{Args, ArgsError};
use util::{Hashable, U256, Uint, Bytes, version_data, Secret, Address}; use util::{Hashable, U256, Uint, Bytes, version_data, Secret, Address};
use util::log::Colour; use util::log::Colour;
use ethsync::{NetworkConfiguration, is_valid_node_url, AllowIP}; use ethsync::{NetworkConfiguration, is_valid_node_url, AllowIP};
use ethcore::client::{VMType, Mode}; use ethcore::client::VMType;
use ethcore::miner::{MinerOptions, Banning}; use ethcore::miner::{MinerOptions, Banning};
use rpc::{IpcConfiguration, HttpConfiguration}; use rpc::{IpcConfiguration, HttpConfiguration};
@ -80,7 +80,7 @@ impl Configuration {
let pruning = try!(self.args.flag_pruning.parse()); let pruning = try!(self.args.flag_pruning.parse());
let pruning_history = self.args.flag_pruning_history; let pruning_history = self.args.flag_pruning_history;
let vm_type = try!(self.vm_type()); let vm_type = try!(self.vm_type());
let mode = try!(to_mode(&self.args.flag_mode, self.args.flag_mode_timeout, self.args.flag_mode_alarm)); let mode = match self.args.flag_mode.as_ref() { "last" => None, mode => Some(try!(to_mode(&mode, self.args.flag_mode_timeout, self.args.flag_mode_alarm))), };
let miner_options = try!(self.miner_options()); let miner_options = try!(self.miner_options());
let logger_config = self.logger_config(); let logger_config = self.logger_config();
let http_conf = try!(self.http_config()); let http_conf = try!(self.http_config());
@ -93,7 +93,6 @@ impl Configuration {
let fat_db = try!(self.args.flag_fat_db.parse()); let fat_db = try!(self.args.flag_fat_db.parse());
let compaction = try!(self.args.flag_db_compaction.parse()); let compaction = try!(self.args.flag_db_compaction.parse());
let wal = !self.args.flag_fast_and_loose; let wal = !self.args.flag_fast_and_loose;
let enable_network = self.enable_network(&mode);
let warp_sync = self.args.flag_warp; let warp_sync = self.args.flag_warp;
let geth_compatibility = self.args.flag_geth; let geth_compatibility = self.args.flag_geth;
let signer_port = self.signer_port(); let signer_port = self.signer_port();
@ -156,7 +155,6 @@ impl Configuration {
pruning_history: pruning_history, pruning_history: pruning_history,
compaction: compaction, compaction: compaction,
wal: wal, wal: wal,
mode: mode,
tracing: tracing, tracing: tracing,
fat_db: fat_db, fat_db: fat_db,
vm_type: vm_type, vm_type: vm_type,
@ -175,7 +173,6 @@ impl Configuration {
pruning_history: pruning_history, pruning_history: pruning_history,
compaction: compaction, compaction: compaction,
wal: wal, wal: wal,
mode: mode,
tracing: tracing, tracing: tracing,
fat_db: fat_db, fat_db: fat_db,
from_block: try!(to_block_id(&self.args.flag_from)), from_block: try!(to_block_id(&self.args.flag_from)),
@ -190,7 +187,6 @@ impl Configuration {
spec: spec, spec: spec,
pruning: pruning, pruning: pruning,
pruning_history: pruning_history, pruning_history: pruning_history,
mode: mode,
tracing: tracing, tracing: tracing,
fat_db: fat_db, fat_db: fat_db,
compaction: compaction, compaction: compaction,
@ -207,7 +203,6 @@ impl Configuration {
spec: spec, spec: spec,
pruning: pruning, pruning: pruning,
pruning_history: pruning_history, pruning_history: pruning_history,
mode: mode,
tracing: tracing, tracing: tracing,
fat_db: fat_db, fat_db: fat_db,
compaction: compaction, compaction: compaction,
@ -246,7 +241,6 @@ impl Configuration {
compaction: compaction, compaction: compaction,
wal: wal, wal: wal,
vm_type: vm_type, vm_type: vm_type,
enable_network: enable_network,
warp_sync: warp_sync, warp_sync: warp_sync,
geth_compatibility: geth_compatibility, geth_compatibility: geth_compatibility,
signer_port: signer_port, signer_port: signer_port,
@ -268,13 +262,6 @@ impl Configuration {
}) })
} }
fn enable_network(&self, mode: &Mode) -> bool {
match *mode {
Mode::Dark(_) => false,
_ => !self.args.flag_no_network,
}
}
fn vm_type(&self) -> Result<VMType, String> { fn vm_type(&self) -> Result<VMType, String> {
if self.args.flag_jitvm { if self.args.flag_jitvm {
VMType::jit().ok_or("Parity is built without the JIT EVM.".into()) VMType::jit().ok_or("Parity is built without the JIT EVM.".into())
@ -767,7 +754,6 @@ mod tests {
pruning_history: 64, pruning_history: 64,
compaction: Default::default(), compaction: Default::default(),
wal: true, wal: true,
mode: Default::default(),
tracing: Default::default(), tracing: Default::default(),
fat_db: Default::default(), fat_db: Default::default(),
vm_type: VMType::Interpreter, vm_type: VMType::Interpreter,
@ -790,7 +776,6 @@ mod tests {
format: Default::default(), format: Default::default(),
compaction: Default::default(), compaction: Default::default(),
wal: true, wal: true,
mode: Default::default(),
tracing: Default::default(), tracing: Default::default(),
fat_db: Default::default(), fat_db: Default::default(),
from_block: BlockID::Number(1), from_block: BlockID::Number(1),
@ -813,7 +798,6 @@ mod tests {
format: Some(DataFormat::Hex), format: Some(DataFormat::Hex),
compaction: Default::default(), compaction: Default::default(),
wal: true, wal: true,
mode: Default::default(),
tracing: Default::default(), tracing: Default::default(),
fat_db: Default::default(), fat_db: Default::default(),
from_block: BlockID::Number(1), from_block: BlockID::Number(1),
@ -858,7 +842,6 @@ mod tests {
compaction: Default::default(), compaction: Default::default(),
wal: true, wal: true,
vm_type: Default::default(), vm_type: Default::default(),
enable_network: true,
geth_compatibility: false, geth_compatibility: false,
signer_port: Some(8180), signer_port: Some(8180),
net_settings: Default::default(), net_settings: Default::default(),

View File

@ -46,7 +46,7 @@ fn to_seconds(s: &str) -> Result<u64, String> {
"hourly" | "1hour" | "1 hour" | "hour" => Ok(60 * 60), "hourly" | "1hour" | "1 hour" | "hour" => Ok(60 * 60),
"daily" | "1day" | "1 day" | "day" => Ok(24 * 60 * 60), "daily" | "1day" | "1 day" | "day" => Ok(24 * 60 * 60),
x if x.ends_with("seconds") => x[0..x.len() - 7].parse().map_err(bad), x if x.ends_with("seconds") => x[0..x.len() - 7].parse().map_err(bad),
x if x.ends_with("minutes") => x[0..x.len() -7].parse::<u64>().map_err(bad).map(|x| x * 60), x if x.ends_with("minutes") => x[0..x.len() - 7].parse::<u64>().map_err(bad).map(|x| x * 60),
x if x.ends_with("hours") => x[0..x.len() - 5].parse::<u64>().map_err(bad).map(|x| x * 60 * 60), x if x.ends_with("hours") => x[0..x.len() - 5].parse::<u64>().map_err(bad).map(|x| x * 60 * 60),
x if x.ends_with("days") => x[0..x.len() - 4].parse::<u64>().map_err(bad).map(|x| x * 24 * 60 * 60), x if x.ends_with("days") => x[0..x.len() - 4].parse::<u64>().map_err(bad).map(|x| x * 24 * 60 * 60),
x => x.parse().map_err(bad), x => x.parse().map_err(bad),
@ -58,7 +58,8 @@ pub fn to_mode(s: &str, timeout: u64, alarm: u64) -> Result<Mode, String> {
"active" => Ok(Mode::Active), "active" => Ok(Mode::Active),
"passive" => Ok(Mode::Passive(Duration::from_secs(timeout), Duration::from_secs(alarm))), "passive" => Ok(Mode::Passive(Duration::from_secs(timeout), Duration::from_secs(alarm))),
"dark" => Ok(Mode::Dark(Duration::from_secs(timeout))), "dark" => Ok(Mode::Dark(Duration::from_secs(timeout))),
_ => Err(format!("{}: Invalid address for --mode. Must be one of active, passive or dark.", s)), "offline" => Ok(Mode::Off),
_ => Err(format!("{}: Invalid value for --mode. Must be one of active, passive, dark or offline.", s)),
} }
} }

View File

@ -20,6 +20,7 @@ use util::{Address, U256, version_data};
use util::journaldb::Algorithm; use util::journaldb::Algorithm;
use ethcore::spec::Spec; use ethcore::spec::Spec;
use ethcore::ethereum; use ethcore::ethereum;
use ethcore::client::Mode;
use ethcore::miner::{GasPricer, GasPriceCalibratorOptions}; use ethcore::miner::{GasPricer, GasPriceCalibratorOptions};
use user_defaults::UserDefaults; use user_defaults::UserDefaults;
@ -264,6 +265,10 @@ pub fn fatdb_switch_to_bool(switch: Switch, user_defaults: &UserDefaults, algori
result result
} }
pub fn mode_switch_to_bool(switch: Option<Mode>, user_defaults: &UserDefaults) -> Result<Mode, String> {
Ok(switch.unwrap_or(user_defaults.mode.clone()))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use util::journaldb::Algorithm; use util::journaldb::Algorithm;

View File

@ -37,7 +37,7 @@ use dapps::WebappServer;
use io_handler::ClientIoHandler; use io_handler::ClientIoHandler;
use params::{ use params::{
SpecType, Pruning, AccountsConfig, GasPricerConfig, MinerExtras, Switch, SpecType, Pruning, AccountsConfig, GasPricerConfig, MinerExtras, Switch,
tracing_switch_to_bool, fatdb_switch_to_bool, tracing_switch_to_bool, fatdb_switch_to_bool, mode_switch_to_bool
}; };
use helpers::{to_client_config, execute_upgrades, passwords_from_files}; use helpers::{to_client_config, execute_upgrades, passwords_from_files};
use dir::Directories; use dir::Directories;
@ -75,13 +75,12 @@ pub struct RunCmd {
pub acc_conf: AccountsConfig, pub acc_conf: AccountsConfig,
pub gas_pricer: GasPricerConfig, pub gas_pricer: GasPricerConfig,
pub miner_extras: MinerExtras, pub miner_extras: MinerExtras,
pub mode: Mode, pub mode: Option<Mode>,
pub tracing: Switch, pub tracing: Switch,
pub fat_db: Switch, pub fat_db: Switch,
pub compaction: DatabaseCompactionProfile, pub compaction: DatabaseCompactionProfile,
pub wal: bool, pub wal: bool,
pub vm_type: VMType, pub vm_type: VMType,
pub enable_network: bool,
pub geth_compatibility: bool, pub geth_compatibility: bool,
pub signer_port: Option<u16>, pub signer_port: Option<u16>,
pub net_settings: NetworkSettings, pub net_settings: NetworkSettings,
@ -137,6 +136,11 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
// check if fatdb is on // check if fatdb is on
let fat_db = try!(fatdb_switch_to_bool(cmd.fat_db, &user_defaults, algorithm)); let fat_db = try!(fatdb_switch_to_bool(cmd.fat_db, &user_defaults, algorithm));
// get the mode
let mode = try!(mode_switch_to_bool(cmd.mode, &user_defaults));
trace!(target: "mode", "mode is {:?}", mode);
let network_enabled = match &mode { &Mode::Dark(_) | &Mode::Off => false, _ => true, };
// prepare client and snapshot paths. // prepare client and snapshot paths.
let client_path = db_dirs.client_path(algorithm); let client_path = db_dirs.client_path(algorithm);
let snapshot_path = db_dirs.snapshot_path(); let snapshot_path = db_dirs.snapshot_path();
@ -162,6 +166,7 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
false => "".to_owned(), false => "".to_owned(),
} }
); );
info!("Operating mode: {}", Colour::White.bold().paint(format!("{}", mode)));
// display warning about using experimental journaldb alorithm // display warning about using experimental journaldb alorithm
if !algorithm.is_stable() { if !algorithm.is_stable() {
@ -196,7 +201,7 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
// create client config // create client config
let client_config = to_client_config( let client_config = to_client_config(
&cmd.cache_config, &cmd.cache_config,
cmd.mode, mode,
tracing, tracing,
fat_db, fat_db,
cmd.compaction, cmd.compaction,
@ -248,7 +253,7 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
service.add_notify(chain_notify.clone()); service.add_notify(chain_notify.clone());
// start network // start network
if cmd.enable_network { if network_enabled {
chain_notify.start(); chain_notify.start();
} }
@ -321,6 +326,19 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
}); });
service.register_io_handler(io_handler.clone()).expect("Error registering IO handler"); service.register_io_handler(io_handler.clone()).expect("Error registering IO handler");
// save user defaults
user_defaults.pruning = algorithm;
user_defaults.tracing = tracing;
try!(user_defaults.save(&user_defaults_path));
let on_mode_change = move |mode: &Mode| {
user_defaults.mode = mode.clone();
let _ = user_defaults.save(&user_defaults_path); // discard failures - there's nothing we can do
};
// tell client how to save the default mode if it gets changed.
client.on_mode_change(on_mode_change);
// the watcher must be kept alive. // the watcher must be kept alive.
let _watcher = match cmd.no_periodic_snapshot { let _watcher = match cmd.no_periodic_snapshot {
true => None, true => None,
@ -347,11 +365,6 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
url::open(&format!("http://{}:{}/", cmd.dapps_conf.interface, cmd.dapps_conf.port)); url::open(&format!("http://{}:{}/", cmd.dapps_conf.interface, cmd.dapps_conf.port));
} }
// save user defaults
user_defaults.pruning = algorithm;
user_defaults.tracing = tracing;
try!(user_defaults.save(&user_defaults_path));
// Handle exit // Handle exit
wait_for_exit(panic_handler, http_server, ipc_server, dapps_server, signer_server); wait_for_exit(panic_handler, http_server, ipc_server, dapps_server, signer_server);

View File

@ -54,7 +54,6 @@ pub struct SnapshotCommand {
pub spec: SpecType, pub spec: SpecType,
pub pruning: Pruning, pub pruning: Pruning,
pub pruning_history: u64, pub pruning_history: u64,
pub mode: Mode,
pub tracing: Switch, pub tracing: Switch,
pub fat_db: Switch, pub fat_db: Switch,
pub compaction: DatabaseCompactionProfile, pub compaction: DatabaseCompactionProfile,
@ -158,7 +157,7 @@ impl SnapshotCommand {
try!(execute_upgrades(&db_dirs, algorithm, self.compaction.compaction_profile(db_dirs.fork_path().as_path()))); try!(execute_upgrades(&db_dirs, algorithm, self.compaction.compaction_profile(db_dirs.fork_path().as_path())));
// prepare client config // prepare client config
let client_config = to_client_config(&self.cache_config, self.mode, tracing, fat_db, self.compaction, self.wal, VMType::default(), "".into(), algorithm, self.pruning_history, true); let client_config = to_client_config(&self.cache_config, Mode::Active, tracing, fat_db, self.compaction, self.wal, VMType::default(), "".into(), algorithm, self.pruning_history, true);
let service = try!(ClientService::start( let service = try!(ClientService::start(
client_config, client_config,

View File

@ -18,6 +18,7 @@ use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::time::Duration;
use serde::{Serialize, Serializer, Error, Deserialize, Deserializer}; use serde::{Serialize, Serializer, Error, Deserialize, Deserializer};
use serde::de::{Visitor, MapVisitor}; use serde::de::{Visitor, MapVisitor};
use serde::de::impls::BTreeMapVisitor; use serde::de::impls::BTreeMapVisitor;
@ -25,12 +26,14 @@ use serde_json::Value;
use serde_json::de::from_reader; use serde_json::de::from_reader;
use serde_json::ser::to_string; use serde_json::ser::to_string;
use util::journaldb::Algorithm; use util::journaldb::Algorithm;
use ethcore::client::Mode;
pub struct UserDefaults { pub struct UserDefaults {
pub is_first_launch: bool, pub is_first_launch: bool,
pub pruning: Algorithm, pub pruning: Algorithm,
pub tracing: bool, pub tracing: bool,
pub fat_db: bool, pub fat_db: bool,
pub mode: Mode,
} }
impl Serialize for UserDefaults { impl Serialize for UserDefaults {
@ -40,6 +43,21 @@ impl Serialize for UserDefaults {
map.insert("pruning".into(), Value::String(self.pruning.as_str().into())); map.insert("pruning".into(), Value::String(self.pruning.as_str().into()));
map.insert("tracing".into(), Value::Bool(self.tracing)); map.insert("tracing".into(), Value::Bool(self.tracing));
map.insert("fat_db".into(), Value::Bool(self.fat_db)); map.insert("fat_db".into(), Value::Bool(self.fat_db));
let mode_str = match self.mode {
Mode::Off => "offline",
Mode::Dark(timeout) => {
map.insert("mode.timeout".into(), Value::U64(timeout.as_secs()));
"dark"
},
Mode::Passive(timeout, alarm) => {
map.insert("mode.timeout".into(), Value::U64(timeout.as_secs()));
map.insert("mode.alarm".into(), Value::U64(alarm.as_secs()));
"passive"
},
Mode::Active => "active",
};
map.insert("mode".into(), Value::String(mode_str.into()));
map.serialize(serializer) map.serialize(serializer)
} }
} }
@ -67,11 +85,28 @@ impl Visitor for UserDefaultsVisitor {
let fat_db: Value = map.remove("fat_db".into()).unwrap_or_else(|| Value::Bool(false)); let fat_db: Value = map.remove("fat_db".into()).unwrap_or_else(|| Value::Bool(false));
let fat_db = try!(fat_db.as_bool().ok_or_else(|| Error::custom("invalid fat_db value"))); let fat_db = try!(fat_db.as_bool().ok_or_else(|| Error::custom("invalid fat_db value")));
let mode: Value = map.remove("mode".into()).unwrap_or_else(|| Value::String("active".to_owned()));
let mode = match try!(mode.as_str().ok_or_else(|| Error::custom("invalid mode value"))) {
"offline" => Mode::Off,
"dark" => {
let timeout = try!(map.remove("mode.timeout".into()).and_then(|v| v.as_u64()).ok_or_else(|| Error::custom("invalid/missing mode.timeout value")));
Mode::Dark(Duration::from_secs(timeout))
},
"passive" => {
let timeout = try!(map.remove("mode.timeout".into()).and_then(|v| v.as_u64()).ok_or_else(|| Error::custom("invalid/missing mode.timeout value")));
let alarm = try!(map.remove("mode.alarm".into()).and_then(|v| v.as_u64()).ok_or_else(|| Error::custom("invalid/missing mode.alarm value")));
Mode::Passive(Duration::from_secs(timeout), Duration::from_secs(alarm))
},
"active" => Mode::Active,
_ => { return Err(Error::custom("invalid mode value")); },
};
let user_defaults = UserDefaults { let user_defaults = UserDefaults {
is_first_launch: false, is_first_launch: false,
pruning: pruning, pruning: pruning,
tracing: tracing, tracing: tracing,
fat_db: fat_db, fat_db: fat_db,
mode: mode,
}; };
Ok(user_defaults) Ok(user_defaults)
@ -85,6 +120,7 @@ impl Default for UserDefaults {
pruning: Algorithm::default(), pruning: Algorithm::default(),
tracing: false, tracing: false,
fat_db: false, fat_db: false,
mode: Mode::Active,
} }
} }
} }
@ -97,7 +133,7 @@ impl UserDefaults {
} }
} }
pub fn save<P>(self, path: P) -> Result<(), String> where P: AsRef<Path> { pub fn save<P>(&self, path: P) -> Result<(), String> where P: AsRef<Path> {
let mut file: File = try!(File::create(path).map_err(|_| "Cannot create user defaults file".to_owned())); let mut file: File = try!(File::create(path).map_err(|_| "Cannot create user defaults file".to_owned()));
file.write_all(to_string(&self).unwrap().as_bytes()).map_err(|_| "Failed to save user defaults".to_owned()) file.write_all(to_string(&self).unwrap().as_bytes()).map_err(|_| "Failed to save user defaults".to_owned())
} }

View File

@ -351,7 +351,7 @@ impl<C, M, S: ?Sized, F> Ethcore for EthcoreClient<C, M, S, F> where
fn mode(&self) -> Result<String, Error> { fn mode(&self) -> Result<String, Error> {
Ok(match take_weak!(self.client).mode() { Ok(match take_weak!(self.client).mode() {
Mode::Off => "off", Mode::Off => "offline",
Mode::Dark(..) => "dark", Mode::Dark(..) => "dark",
Mode::Passive(..) => "passive", Mode::Passive(..) => "passive",
Mode::Active => "active", Mode::Active => "active",

View File

@ -151,7 +151,7 @@ impl<C, M> EthcoreSet for EthcoreSetClient<C, M> where
fn set_mode(&self, mode: String) -> Result<bool, Error> { fn set_mode(&self, mode: String) -> Result<bool, Error> {
take_weak!(self.client).set_mode(match mode.as_str() { take_weak!(self.client).set_mode(match mode.as_str() {
"off" => Mode::Off, "offline" => Mode::Off,
"dark" => Mode::Dark(300), "dark" => Mode::Dark(300),
"passive" => Mode::Passive(300, 3600), "passive" => Mode::Passive(300, 3600),
"active" => Mode::Active, "active" => Mode::Active,

View File

@ -130,7 +130,7 @@ build_rpc_trait! {
#[rpc(name = "ethcore_nextNonce")] #[rpc(name = "ethcore_nextNonce")]
fn next_nonce(&self, H160) -> Result<U256, Error>; fn next_nonce(&self, H160) -> Result<U256, Error>;
/// Get the mode. Results one of: "active", "passive", "dark", "off". /// Get the mode. Results one of: "active", "passive", "dark", "offline".
#[rpc(name = "ethcore_mode")] #[rpc(name = "ethcore_mode")]
fn mode(&self) -> Result<String, Error>; fn mode(&self) -> Result<String, Error>;

View File

@ -76,11 +76,11 @@ build_rpc_trait! {
/// Stop the network. /// Stop the network.
/// ///
/// Deprecated. Use `set_mode("off")` instead. /// Deprecated. Use `set_mode("offline")` instead.
#[rpc(name = "ethcore_stopNetwork")] #[rpc(name = "ethcore_stopNetwork")]
fn stop_network(&self) -> Result<bool, Error>; fn stop_network(&self) -> Result<bool, Error>;
/// Set the mode. Argument must be one of: "active", "passive", "dark", "off". /// Set the mode. Argument must be one of: "active", "passive", "dark", "offline".
#[rpc(name = "ethcore_setMode")] #[rpc(name = "ethcore_setMode")]
fn set_mode(&self, String) -> Result<bool, Error>; fn set_mode(&self, String) -> Result<bool, Error>;
} }