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

Commit

Permalink
Merge branch 'master' of github.com:ethcore/parity into cli_commands
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar committed Jul 25, 2016
2 parents 0218c6e + 435ba18 commit ce6f59e
Show file tree
Hide file tree
Showing 27 changed files with 107 additions and 63 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "Frontier/Homestead",
"name": "Ethereum Classic",
"forkName": "classic",
"engine": {
"Ethash": {
"params": {
Expand Down
4 changes: 2 additions & 2 deletions ethcore/src/account_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ impl KeyDirectory for NullDir {
Ok(vec![])
}

fn insert(&self, _account: SafeAccount) -> Result<(), SSError> {
Ok(())
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, SSError> {
Ok(account)
}

fn remove(&self, _address: &SSAddress) -> Result<(), SSError> {
Expand Down
8 changes: 5 additions & 3 deletions ethcore/src/blockchain/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use blockchain::best_block::BestBlock;
use types::tree_route::TreeRoute;
use blockchain::update::ExtrasUpdate;
use blockchain::{CacheSize, ImportRoute, Config};
use db::{Writable, Readable, CacheUpdatePolicy, Key};
use db::{Writable, Readable, CacheUpdatePolicy};

const LOG_BLOOMS_LEVELS: usize = 3;
const LOG_BLOOMS_ELEMENTS_PER_INDEX: usize = 16;
Expand Down Expand Up @@ -296,7 +296,7 @@ impl BlockChain {
// load best block
let best_block_hash = match bc.extras_db.get(b"best").unwrap() {
Some(best) => {
let mut new_best = H256::from_slice(&best);
let new_best = H256::from_slice(&best);
if !bc.blocks_db.get(&new_best).unwrap().is_some() {
warn!("Best block {} not found", new_best.hex());
}
Expand Down Expand Up @@ -358,7 +358,9 @@ impl BlockChain {
}

/// Rewind to a previous block
pub fn rewind(&self) -> Option<H256> {
#[cfg(test)]
fn rewind(&self) -> Option<H256> {
use db::Key;
let batch = DBTransaction::new();
// track back to the best block we have in the blocks database
if let Some(best_block_hash) = self.extras_db.get(b"best").unwrap() {
Expand Down
8 changes: 2 additions & 6 deletions ethcore/src/ethereum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ use super::spec::*;
pub fn new_olympic() -> Spec { Spec::load(include_bytes!("../../res/ethereum/olympic.json")) }

/// Create a new Frontier mainnet chain spec.
pub fn new_frontier() -> Spec {
Spec::load(include_bytes!("../../res/ethereum/frontier.json"))
}
pub fn new_frontier() -> Spec { Spec::load(include_bytes!("../../res/ethereum/frontier.json")) }

/// Create a new Frontier mainnet chain spec without the DAO hardfork.
pub fn new_frontier_dogmatic() -> Spec {
Spec::load(include_bytes!("../../res/ethereum/frontier-dogmatic.json"))
}
pub fn new_classic() -> Spec { Spec::load(include_bytes!("../../res/ethereum/classic.json")) }

/// Create a new Frontier chain spec as though it never changes to Homestead.
pub fn new_frontier_test() -> Spec { Spec::load(include_bytes!("../../res/ethereum/frontier_test.json")) }
Expand Down
3 changes: 3 additions & 0 deletions ethcore/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ impl ClientService {
panic_handler.forward_from(&io_service);

info!("Configured for {} using {} engine", Colour::White.bold().paint(spec.name.clone()), Colour::Yellow.bold().paint(spec.engine.name()));
if spec.fork_name.is_some() {
warn!("Your chain is an alternative fork. {}", Colour::Red.bold().paint("TRANSACTIONS MAY BE REPLAYED ON THE MAINNET!"));
}
let client = try!(Client::new(config, spec, db_path, miner, io_service.channel()));
panic_handler.forward_from(client.deref());
let client_io = Arc::new(ClientIoHandler {
Expand Down
5 changes: 4 additions & 1 deletion ethcore/src/spec/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct Spec {
pub name: String,
/// What engine are we using for this?
pub engine: Box<Engine>,
/// The fork identifier for this chain. Only needed to distinguish two chains sharing the same genesis.
pub fork_name: Option<String>,

/// Known nodes on the network in enode format.
pub nodes: Vec<String>,
Expand Down Expand Up @@ -105,6 +107,7 @@ impl From<ethjson::spec::Spec> for Spec {
name: s.name.into(),
params: params.clone(),
engine: Spec::engine(s.engine, params, builtins),
fork_name: s.fork_name.map(Into::into),
nodes: s.nodes.unwrap_or_else(Vec::new),
parent_hash: g.parent_hash,
transactions_root: g.transactions_root,
Expand All @@ -118,7 +121,7 @@ impl From<ethjson::spec::Spec> for Spec {
seal_fields: seal.fields,
seal_rlp: seal.rlp,
state_root_memo: RwLock::new(g.state_root),
genesis_state: From::from(s.accounts)
genesis_state: From::from(s.accounts),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/trace/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use bloomchain::{Number, Config as BloomConfig};
use bloomchain::group::{BloomGroupDatabase, BloomGroupChain, GroupPosition, BloomGroup};
use util::{H256, H264, Database, DatabaseConfig, DBTransaction, RwLock};
use header::BlockNumber;
use trace::{BlockTraces, LocalizedTrace, Config, Switch, Filter, Database as TraceDatabase, ImportRequest, DatabaseExtras, Error};
use trace::{LocalizedTrace, Config, Switch, Filter, Database as TraceDatabase, ImportRequest, DatabaseExtras, Error};
use db::{Key, Writable, Readable, CacheUpdatePolicy};
use blooms;
use super::flat::{FlatTrace, FlatBlockTraces, FlatTransactionTraces};
Expand Down
2 changes: 2 additions & 0 deletions ethcore/src/trace/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Decodable for FlatTrace {
pub struct FlatTransactionTraces(Vec<FlatTrace>);

impl FlatTransactionTraces {
/// Returns bloom of all traces in the collection.
pub fn bloom(&self) -> LogBloom {
self.0.iter().fold(Default::default(), | bloom, trace | bloom | trace.bloom())
}
Expand Down Expand Up @@ -102,6 +103,7 @@ impl Into<Vec<FlatTrace>> for FlatTransactionTraces {
pub struct FlatBlockTraces(Vec<FlatTransactionTraces>);

impl FlatBlockTraces {
/// Returns bloom of all traces in the block.
pub fn bloom(&self) -> LogBloom {
self.0.iter().fold(Default::default(), | bloom, tx_traces | bloom | tx_traces.bloom())
}
Expand Down
1 change: 1 addition & 0 deletions ethstore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rustc-serialize = "0.3"
rust-crypto = "0.2.36"
tiny-keccak = "1.0"
docopt = { version = "0.6", optional = true }
time = "0.1.34"

[build-dependencies]
serde_codegen = { version = "0.7", optional = true }
Expand Down
31 changes: 24 additions & 7 deletions ethstore/src/account/safe_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

use std::ops::{Deref, DerefMut};
use std::path::{PathBuf};
use ethkey::{KeyPair, sign, Address, Secret, Signature, Message};
use {json, Error, crypto};
use crypto::Keccak256;
Expand All @@ -35,16 +36,17 @@ pub struct SafeAccount {
pub version: Version,
pub address: Address,
pub crypto: Crypto,
pub path: Option<PathBuf>,
pub name: String,
pub meta: String,
}

impl From<json::Crypto> for Crypto {
fn from(json: json::Crypto) -> Self {
Crypto {
cipher: From::from(json.cipher),
cipher: json.cipher.into(),
ciphertext: json.ciphertext.into(),
kdf: From::from(json.kdf),
kdf: json.kdf.into(),
mac: json.mac.into(),
}
}
Expand All @@ -54,9 +56,9 @@ impl Into<json::Crypto> for Crypto {
fn into(self) -> json::Crypto {
json::Crypto {
cipher: self.cipher.into(),
ciphertext: From::from(self.ciphertext),
ciphertext: self.ciphertext.into(),
kdf: self.kdf.into(),
mac: From::from(self.mac),
mac: self.mac.into(),
}
}
}
Expand All @@ -65,9 +67,10 @@ impl From<json::KeyFile> for SafeAccount {
fn from(json: json::KeyFile) -> Self {
SafeAccount {
id: json.id.into(),
version: From::from(json.version),
address: From::from(json.address), //json.address.into(),
crypto: From::from(json.crypto),
version: json.version.into(),
address: json.address.into(),
crypto: json.crypto.into(),
path: None,
name: json.name.unwrap_or(String::new()),
meta: json.meta.unwrap_or("{}".to_owned()),
}
Expand Down Expand Up @@ -151,11 +154,24 @@ impl SafeAccount {
version: Version::V3,
crypto: Crypto::create(keypair.secret(), password, iterations),
address: keypair.address(),
path: None,
name: name,
meta: meta,
}
}

pub fn from_file(json: json::KeyFile, path: PathBuf) -> Self {
SafeAccount {
id: json.id.into(),
version: json.version.into(),
address: json.address.into(),
crypto: json.crypto.into(),
path: Some(path),
name: json.name.unwrap_or(String::new()),
meta: json.meta.unwrap_or("{}".to_owned()),
}
}

pub fn sign(&self, password: &str, message: &Message) -> Result<Signature, Error> {
let secret = try!(self.crypto.secret(password));
sign(&secret, message).map_err(From::from)
Expand All @@ -168,6 +184,7 @@ impl SafeAccount {
version: self.version.clone(),
crypto: Crypto::create(&secret, new_password, iterations),
address: self.address.clone(),
path: self.path.clone(),
name: self.name.clone(),
meta: self.meta.clone(),
};
Expand Down
37 changes: 23 additions & 14 deletions ethstore/src/dir/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use std::{fs, io};
use std::path::{PathBuf, Path};
use std::collections::HashMap;
use time;
use ethkey::Address;
use {json, SafeAccount, Error};
use super::KeyDirectory;
Expand Down Expand Up @@ -75,7 +76,7 @@ impl DiskDirectory {
let accounts = files.into_iter()
.map(json::KeyFile::load)
.zip(paths.into_iter())
.filter_map(|(file, path)| file.ok().map(|file| (path, SafeAccount::from(file))))
.filter_map(|(file, path)| file.ok().map(|file| (path.clone(), SafeAccount::from_file(file, path))))
.collect();

Ok(accounts)
Expand All @@ -91,24 +92,32 @@ impl KeyDirectory for DiskDirectory {
Ok(accounts)
}

fn insert(&self, account: SafeAccount) -> Result<(), Error> {
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
// transform account into key file
let keyfile: json::KeyFile = account.into();
let keyfile: json::KeyFile = account.clone().into();

// build file path
let mut keyfile_path = self.path.clone();
keyfile_path.push(format!("{}", keyfile.id));

// save the file
let mut file = try!(fs::File::create(&keyfile_path));
try!(keyfile.write(&mut file).map_err(|e| Error::Custom(format!("{:?}", e))));

if let Err(_) = restrict_permissions_to_owner(&keyfile_path) {
fs::remove_file(&keyfile_path).expect("Expected to remove recently created file");
return Err(Error::Io(io::Error::last_os_error()));
let mut account = account;
account.path = account.path.or_else(|| {
let mut keyfile_path = self.path.clone();
let timestamp = time::strftime("%Y-%m-%d_%H:%M:%S_%Z", &time::now()).unwrap_or("???".to_owned());
keyfile_path.push(format!("{}-{}.json", keyfile.id, timestamp));
Some(keyfile_path)
});

{
// save the file
let path = account.path.as_ref().expect("build-file-path ensures is not None; qed");
let mut file = try!(fs::File::create(path));
try!(keyfile.write(&mut file).map_err(|e| Error::Custom(format!("{:?}", e))));

if let Err(_) = restrict_permissions_to_owner(path) {
fs::remove_file(path).expect("Expected to remove recently created file");
return Err(Error::Io(io::Error::last_os_error()));
}
}

Ok(())
Ok(account)
}

fn remove(&self, address: &Address) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion ethstore/src/dir/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl KeyDirectory for GethDirectory {
self.dir.load()
}

fn insert(&self, account: SafeAccount) -> Result<(), Error> {
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
self.dir.insert(account)
}

Expand Down
2 changes: 1 addition & 1 deletion ethstore/src/dir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum DirectoryType {

pub trait KeyDirectory: Send + Sync {
fn load(&self) -> Result<Vec<SafeAccount>, Error>;
fn insert(&self, account: SafeAccount) -> Result<(), Error>;
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error>;
fn remove(&self, address: &Address) -> Result<(), Error>;
}

Expand Down
2 changes: 1 addition & 1 deletion ethstore/src/dir/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl KeyDirectory for ParityDirectory {
self.dir.load()
}

fn insert(&self, account: SafeAccount) -> Result<(), Error> {
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
self.dir.insert(account)
}

Expand Down
2 changes: 1 addition & 1 deletion ethstore/src/ethstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl EthStore {

fn save(&self, account: SafeAccount) -> Result<(), Error> {
// save to file
try!(self.dir.insert(account.clone()));
let account = try!(self.dir.insert(account.clone()));

// update cache
let mut cache = self.cache.write().unwrap();
Expand Down
1 change: 1 addition & 0 deletions ethstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

extern crate libc;
extern crate rand;
extern crate time;
extern crate serde;
extern crate serde_json;
extern crate rustc_serialize;
Expand Down
2 changes: 1 addition & 1 deletion ethstore/tests/util/transient_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl KeyDirectory for TransientDir {
self.dir.load()
}

fn insert(&self, account: SafeAccount) -> Result<(), Error> {
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
self.dir.insert(account)
}

Expand Down
3 changes: 3 additions & 0 deletions json/src/spec/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ use spec::{Params, Genesis, Engine, State};
pub struct Spec {
/// Spec name.
pub name: String,
/// Special fork name.
#[serde(rename="forkName")]
pub fork_name: Option<String>,
/// Engine.
pub engine: Engine,
/// Spec params.
Expand Down
16 changes: 8 additions & 8 deletions parity/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ fn execute_import(cmd: ImportBlockchain) -> Result<String, String> {
fdlimit::raise_fd_limit();

// select pruning algorithm
let algorithm = cmd.pruning.to_algorithm(&cmd.dirs, genesis_hash);
let algorithm = cmd.pruning.to_algorithm(&cmd.dirs, genesis_hash, spec.fork_name.as_ref());

// prepare client_path
let client_path = cmd.dirs.client_path(genesis_hash, algorithm);
let client_path = cmd.dirs.client_path(genesis_hash, spec.fork_name.as_ref(), algorithm);

// execute upgrades
try!(execute_upgrades(&cmd.dirs, genesis_hash, algorithm));
try!(execute_upgrades(&cmd.dirs, genesis_hash, spec.fork_name.as_ref(), algorithm));

// prepare client config
let client_config = to_client_config(&cmd.cache_config, &cmd.dirs, genesis_hash, cmd.mode, cmd.tracing, cmd.pruning, cmd.compaction, cmd.vm_type, "".into());
let client_config = to_client_config(&cmd.cache_config, &cmd.dirs, genesis_hash, cmd.mode, cmd.tracing, cmd.pruning, cmd.compaction, cmd.vm_type, "".into(), spec.fork_name.as_ref());

// build client
let service = try!(ClientService::start(
Expand Down Expand Up @@ -231,16 +231,16 @@ fn execute_export(cmd: ExportBlockchain) -> Result<String, String> {
fdlimit::raise_fd_limit();

// select pruning algorithm
let algorithm = cmd.pruning.to_algorithm(&cmd.dirs, genesis_hash);
let algorithm = cmd.pruning.to_algorithm(&cmd.dirs, genesis_hash, spec.fork_name.as_ref());

// prepare client_path
let client_path = cmd.dirs.client_path(genesis_hash, algorithm);
let client_path = cmd.dirs.client_path(genesis_hash, spec.fork_name.as_ref(), algorithm);

// execute upgrades
try!(execute_upgrades(&cmd.dirs, genesis_hash, algorithm));
try!(execute_upgrades(&cmd.dirs, genesis_hash, spec.fork_name.as_ref(), algorithm));

// prepare client config
let client_config = to_client_config(&cmd.cache_config, &cmd.dirs, genesis_hash, cmd.mode, cmd.tracing, cmd.pruning, cmd.compaction, VMType::default(), "".into());
let client_config = to_client_config(&cmd.cache_config, &cmd.dirs, genesis_hash, cmd.mode, cmd.tracing, cmd.pruning, cmd.compaction, VMType::default(), "".into(), spec.fork_name.as_ref());

let service = try!(ClientService::start(
client_config,
Expand Down
Loading

0 comments on commit ce6f59e

Please sign in to comment.