Skip to content

Commit

Permalink
Merge pull request #2742 from subspace/farmer-configure-creation
Browse files Browse the repository at this point in the history
Allow disabling farm creation
  • Loading branch information
nazar-pc authored May 6, 2024
2 parents 01d49c1 + a8f0904 commit d0aaf10
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
22 changes: 20 additions & 2 deletions crates/subspace-farmer/src/bin/subspace-farmer/commands/farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ pub(crate) struct FarmingArgs {
/// Disable farm locking, for example if file system doesn't support it
#[arg(long)]
disable_farm_locking: bool,
/// Whether to create missing farms during start.
///
/// If set to `false` farmer will exit with error if one of the farms doesn't already exist.
#[arg(long, default_value_t = true, action = clap::ArgAction::Set)]
create: bool,
/// Exit on farm error.
///
/// By default, farmer will continue running if there are still other working farms.
Expand Down Expand Up @@ -244,6 +249,7 @@ where
plotting_thread_priority,
plot_cache,
disable_farm_locking,
create,
exit_on_farm_error,
} = farming_args;

Expand Down Expand Up @@ -305,8 +311,19 @@ where
.expect("Disk farm collection is not be empty as checked above; qed")
.directory;

let identity = Identity::open_or_create(first_farm_directory)
.map_err(|error| anyhow!("Failed to open or create identity: {error}"))?;
let identity = if create {
Identity::open_or_create(first_farm_directory)
.map_err(|error| anyhow!("Failed to open or create identity: {error}"))?
} else {
Identity::open(first_farm_directory)
.map_err(|error| anyhow!("Failed to open identity of the first farm: {error}"))?
.ok_or_else(|| {
anyhow!(
"Failed to open identity of the first farm: Farm doesn't exist and creation \
was explicitly disabled"
)
})?
};
let keypair = derive_libp2p_keypair(identity.secret_key());
let peer_id = keypair.public().to_peer_id();

Expand Down Expand Up @@ -541,6 +558,7 @@ where
faster_read_sector_record_chunks_mode_barrier,
faster_read_sector_record_chunks_mode_concurrency,
plotter,
create,
},
farm_index,
);
Expand Down
14 changes: 13 additions & 1 deletion crates/subspace-farmer/src/single_disk_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ pub struct SingleDiskFarmOptions<NC, P> {
pub faster_read_sector_record_chunks_mode_barrier: Arc<Barrier>,
/// Limit concurrency of internal benchmarking between different farms
pub faster_read_sector_record_chunks_mode_concurrency: Arc<Semaphore>,
/// Whether to create a farm if it doesn't yet exist
pub create: bool,
}

/// Errors happening when trying to create/open single disk farm
Expand Down Expand Up @@ -1063,6 +1065,7 @@ impl SingleDiskFarm {
max_pieces_in_sector,
cache_percentage,
disable_farm_locking,
create,
..
} = options;

Expand All @@ -1071,7 +1074,16 @@ impl SingleDiskFarm {

fs::create_dir_all(directory)?;

let identity = Identity::open_or_create(directory)?;
let identity = if *create {
Identity::open_or_create(directory)?
} else {
Identity::open(directory)?.ok_or_else(|| {
IdentityError::Io(io::Error::new(
io::ErrorKind::NotFound,
"Farm does not exist and creation was explicitly disabled",
))
})?
};
let public_key = identity.public_key().to_bytes().into();

let single_disk_farm_info = match SingleDiskFarmInfo::load_from(directory)? {
Expand Down

0 comments on commit d0aaf10

Please sign in to comment.