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

apps: use fd-lock instead of file-lock for cross-platform support #1605

Merged
merged 2 commits into from
Jul 22, 2023
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
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/1605-win-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Replaced file-lock with fd-lock dependency to support Windows build.
([\#1605](https://github.com/anoma/namada/pull/1605))
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
112 changes: 57 additions & 55 deletions apps/src/lib/wallet/pre_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs;
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 +36,72 @@ 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();
(&*guard).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
49 changes: 26 additions & 23 deletions apps/src/lib/wallet/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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,28 @@ 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();
(&*guard).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