Skip to content

Commit

Permalink
apps: use fd-lock instead of file-lock for cross-platform support
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Jul 10, 2023
1 parent f61b635 commit d75aa45
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 116 deletions.
49 changes: 14 additions & 35 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ ethabi = "18.0.0"
ethers = "2.0.0"
expectrl = "0.7.0"
eyre = "0.6.5"
fd-lock = "3.0.12"
ferveo = {git = "https://github.com/anoma/ferveo", rev = "e5abd0acc938da90140351a65a26472eb495ce4d"}
ferveo-common = {git = "https://github.com/anoma/ferveo", rev = "e5abd0acc938da90140351a65a26472eb495ce4d"}
file-lock = "2.0.2"
file-serve = "0.2.0"
flate2 = "1.0.22"
fs_extra = "1.2.0"
Expand Down
2 changes: 1 addition & 1 deletion apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ ethbridge-bridge-events = {git = "https://github.com/heliaxdev/ethbridge-rs", ta
ethbridge-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.18.0"}
ethbridge-governance-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.18.0"}
eyre.workspace = true
fd-lock.workspace = true
ferveo-common.workspace = true
ferveo.workspace = true
file-lock.workspace = true
flate2.workspace = true
futures.workspace = true
itertools.workspace = true
Expand Down
114 changes: 59 additions & 55 deletions apps/src/lib/wallet/pre_genesis.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::fs;
use std::io::BufReader;
use std::path::{Path, PathBuf};

use ark_serialize::{Read, Write};
use file_lock::{FileLock, FileOptions};
use fd_lock::RwLock;
use namada::ledger::wallet::pre_genesis::{
ReadError, ValidatorStore, ValidatorWallet,
};
Expand Down Expand Up @@ -36,70 +37,73 @@ pub fn gen_and_store(
let wallet_dir = wallet_path.parent().unwrap();
fs::create_dir_all(wallet_dir)?;
// Write the file
let options = FileOptions::new().create(true).write(true).truncate(true);
let mut filelock =
FileLock::lock(wallet_path.to_str().unwrap(), true, options)?;
filelock.file.write_all(&data)?;
let mut options = fs::OpenOptions::new();
options.create(true).write(true).truncate(true);
let mut lock = RwLock::new(options.open(wallet_path)?);
let mut guard = lock.write()?;
guard.write_all(&data)?;
Ok(validator)
}

/// Try to load and decrypt keys, if encrypted, in a [`ValidatorWallet`]
/// from a TOML file.
pub fn load(store_dir: &Path) -> Result<ValidatorWallet, ReadError> {
let wallet_file = validator_file_name(store_dir);
match FileLock::lock(
wallet_file.to_str().unwrap(),
true,
FileOptions::new().read(true).write(false),
) {
Ok(mut filelock) => {
let mut store = Vec::<u8>::new();
filelock.file.read_to_end(&mut store).map_err(|err| {
ReadError::ReadWallet(
store_dir.to_str().unwrap().into(),
err.to_string(),
)
})?;
let store =
ValidatorStore::decode(store).map_err(ReadError::Decode)?;
let mut options = fs::OpenOptions::new();
options.read(true).write(false);
let lock = RwLock::new(options.open(&wallet_file).map_err(|err| {
ReadError::ReadWallet(
wallet_file.to_string_lossy().into_owned(),
err.to_string(),
)
})?);
let guard = lock.read().map_err(|err| {
ReadError::ReadWallet(
wallet_file.to_string_lossy().into_owned(),
err.to_string(),
)
})?;
let mut store = Vec::<u8>::new();
let mut reader = BufReader::new(&*guard);
reader.read_to_end(&mut store).map_err(|err| {
ReadError::ReadWallet(
store_dir.to_str().unwrap().into(),
err.to_string(),
)
})?;
let store = ValidatorStore::decode(store).map_err(ReadError::Decode)?;

let password = if store.account_key.is_encrypted()
|| store.consensus_key.is_encrypted()
|| store.account_key.is_encrypted()
{
Some(CliWalletUtils::read_decryption_password())
} else {
None
};
let password = if store.account_key.is_encrypted()
|| store.consensus_key.is_encrypted()
|| store.account_key.is_encrypted()
{
Some(CliWalletUtils::read_decryption_password())
} else {
None
};

let account_key = store
.account_key
.get::<CliWalletUtils>(true, password.clone())?;
let consensus_key = store
.consensus_key
.get::<CliWalletUtils>(true, password.clone())?;
let eth_cold_key = store
.eth_cold_key
.get::<CliWalletUtils>(true, password.clone())?;
let eth_hot_key = store.validator_keys.eth_bridge_keypair.clone();
let tendermint_node_key = store
.tendermint_node_key
.get::<CliWalletUtils>(true, password)?;
let account_key = store
.account_key
.get::<CliWalletUtils>(true, password.clone())?;
let consensus_key = store
.consensus_key
.get::<CliWalletUtils>(true, password.clone())?;
let eth_cold_key = store
.eth_cold_key
.get::<CliWalletUtils>(true, password.clone())?;
let eth_hot_key = store.validator_keys.eth_bridge_keypair.clone();
let tendermint_node_key = store
.tendermint_node_key
.get::<CliWalletUtils>(true, password)?;

Ok(ValidatorWallet {
store,
account_key,
consensus_key,
eth_cold_key,
eth_hot_key,
tendermint_node_key,
})
}
Err(err) => Err(ReadError::ReadWallet(
wallet_file.to_string_lossy().into_owned(),
err.to_string(),
)),
}
Ok(ValidatorWallet {
store,
account_key,
consensus_key,
eth_cold_key,
eth_hot_key,
tendermint_node_key,
})
}

/// Generate a new [`ValidatorWallet`] with required pre-genesis keys. Will
Expand Down
52 changes: 28 additions & 24 deletions apps/src/lib/wallet/store.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::fs;
use std::io::prelude::*;
use std::io::Write;
use std::io::{BufReader, Write};
use std::path::{Path, PathBuf};
#[cfg(not(feature = "dev"))]
use std::str::FromStr;

use ark_std::rand::prelude::*;
use ark_std::rand::SeedableRng;
use file_lock::{FileLock, FileOptions};
use fd_lock::RwLock;
#[cfg(not(feature = "dev"))]
use namada::ledger::wallet::store::AddressVpType;
#[cfg(feature = "dev")]
Expand Down Expand Up @@ -48,10 +48,11 @@ pub fn save(store: &Store, store_dir: &Path) -> std::io::Result<()> {
let wallet_dir = wallet_path.parent().unwrap();
fs::create_dir_all(wallet_dir)?;
// Write the file
let options = FileOptions::new().create(true).write(true).truncate(true);
let mut filelock =
FileLock::lock(wallet_path.to_str().unwrap(), true, options)?;
filelock.file.write_all(&data)
let mut options = fs::OpenOptions::new();
options.create(true).write(true).truncate(true);
let mut lock = RwLock::new(options.open(wallet_path)?);
let mut guard = lock.write()?;
guard.write_all(&data)
}

/// Load the store file or create a new one without any keys or addresses.
Expand Down Expand Up @@ -88,26 +89,29 @@ pub fn load_or_new_from_genesis(
/// Attempt to load the store file.
pub fn load(store_dir: &Path) -> Result<Store, LoadStoreError> {
let wallet_file = wallet_file(store_dir);
match FileLock::lock(
wallet_file.to_str().unwrap(),
true,
FileOptions::new().read(true).write(false),
) {
Ok(mut filelock) => {
let mut store = Vec::<u8>::new();
filelock.file.read_to_end(&mut store).map_err(|err| {
LoadStoreError::ReadWallet(
store_dir.to_str().unwrap().parse().unwrap(),
err.to_string(),
)
})?;
Store::decode(store).map_err(LoadStoreError::Decode)
}
Err(err) => Err(LoadStoreError::ReadWallet(
let mut options = fs::OpenOptions::new();
options.read(true).write(false);
let lock = RwLock::new(options.open(&wallet_file).map_err(|err| {
LoadStoreError::ReadWallet(
wallet_file.to_string_lossy().into_owned(),
err.to_string(),
)),
}
)
})?);
let guard = lock.read().map_err(|err| {
LoadStoreError::ReadWallet(
wallet_file.to_string_lossy().into_owned(),
err.to_string(),
)
})?;
let mut store = Vec::<u8>::new();
let mut reader = BufReader::new(&*guard);
reader.read_to_end(&mut store).map_err(|err| {
LoadStoreError::ReadWallet(
store_dir.to_str().unwrap().parse().unwrap(),
err.to_string(),
)
})?;
Store::decode(store).map_err(LoadStoreError::Decode)
}

/// Add addresses from a genesis configuration.
Expand Down

0 comments on commit d75aa45

Please sign in to comment.