Skip to content

Commit c9c4b9b

Browse files
author
Adrian Nagy
committed
refactor: minor stuff
1 parent 43c06e3 commit c9c4b9b

File tree

5 files changed

+66
-27
lines changed

5 files changed

+66
-27
lines changed

cli/src/commands/node/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,26 @@ pub struct Node {
138138
#[arg(short = 'c', long, env)]
139139
pub config: Option<PathBuf>,
140140

141-
// /// Enable archive mode (seding blocks to the archive process).
142-
// #[arg(long, env)]
143-
// pub archive_address: Option<Url>,
144141
/// Enable local precomputed storage.
142+
///
143+
/// This option requires the following environment variables to be set:
144+
/// - OPENMINA_ARCHIVE_LOCAL_STORAGE_PATH (otherwise the path to the working directory will be used)
145145
#[arg(long, env)]
146146
pub archive_local_storage: bool,
147147

148-
// TODO(adonagy): Sort out this... Do we want to support the ocaml options 1:1?
149-
// we could move the addrss to an env var, just like gcp and aws
150148
/// Enable archiver process.
149+
///
150+
/// This requires the following environment variables to be set:
151+
/// - OPENMINA_ARCHIVE_ADDRESS
151152
#[arg(long, env)]
152153
pub archive_archiver_process: bool,
153154

154155
/// Enable GCP precomputed storage.
156+
///
157+
/// This requires the following environment variables to be set:
158+
/// - GCP_CREDENTIALS_JSON
159+
/// - GCP_BUCKET_NAME
160+
///
155161
#[arg(long, env)]
156162
pub archive_gcp_storage: bool,
157163

@@ -333,7 +339,6 @@ impl Node {
333339
.validate_env_vars()
334340
.map_err(|e| anyhow::anyhow!(e))?;
335341

336-
// TODO(adonagy): add options
337342
node_builder.archive(archive_storage_options, work_dir.clone());
338343
}
339344

mina-p2p-messages/src/v2/manual.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,9 +836,10 @@ pub struct PrecomputedBlock {
836836
pub staged_ledger_diff: StagedLedgerDiffDiffStableV2,
837837
// FIXME: for some reason in OCaml the base58check conversion for the JSON value
838838
// uses version byte = 0x05 (ledger hash) instead of 0x10 (StateHash) and 0x11 (StateBodyHash)
839+
// Note: keeping the proper types here, we should raise an issue in the ocaml repo
839840
pub delta_transition_chain_proof: (
840-
LedgerHash, // StateHash
841-
List<LedgerHash>, // StateBodyHash
841+
StateHash, // LedgerHash, // StateHash
842+
List<StateBodyHash>, // List<LedgerHash>, // StateBodyHash
842843
),
843844
pub protocol_version: ProtocolVersionStableV2,
844845
#[serde(default)]

node/common/src/service/archive/gcp.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,31 @@ pub(crate) struct ArchiveGCPClient {
1212

1313
impl ArchiveGCPClient {
1414
pub async fn new() -> Result<Self, Error> {
15-
let cred_file = env::var("GCP_CREDENTIALS_JSON")
16-
.map_err(|_| Error::EnvironmentVariableNotSet("GCP_CREDENTIALS_JSON".to_string()))?;
17-
let bucket_name = env::var("GCP_BUCKET_NAME")
18-
.map_err(|_| Error::EnvironmentVariableNotSet("GCP_BUCKET_NAME".to_string()))?;
19-
let credentials = GcpCredentialsFile::new_from_file(cred_file).await.unwrap();
15+
let get_env_var = |var: &str| -> Result<String, Error> {
16+
env::var(var).map_err(|_| Error::EnvironmentVariableNotSet(var.to_string()))
17+
};
18+
19+
let cred_file = get_env_var("GCP_CREDENTIALS_JSON")?;
20+
let bucket_name = get_env_var("GCP_BUCKET_NAME")?;
21+
22+
let credentials = GcpCredentialsFile::new_from_file(cred_file)
23+
.await
24+
.map_err(|e| Error::UploadError(format!("GCP credentials error: {}", e)))?;
25+
2026
let config = gcs::client::ClientConfig::default()
2127
.with_credentials(credentials)
2228
.await
23-
.unwrap();
24-
let client = gcs::client::Client::new(config);
29+
.map_err(|e| Error::UploadError(format!("GCP config error: {}", e)))?;
30+
2531
Ok(ArchiveGCPClient {
26-
client,
32+
client: gcs::client::Client::new(config),
2733
bucket_name,
2834
})
2935
}
3036

3137
pub async fn upload_block(&self, key: &str, data: &[u8]) -> Result<(), Error> {
3238
let upload_type = gcs_upload::UploadType::Simple(gcs_upload::Media::new(key.to_string()));
39+
3340
self.client
3441
.upload_object(
3542
&gcs_upload::UploadObjectRequest {
@@ -40,7 +47,8 @@ impl ArchiveGCPClient {
4047
&upload_type,
4148
)
4249
.await
43-
.map_err(|e| Error::UploadError(e.to_string()))?;
50+
.map_err(|e| Error::UploadError(format!("GCP upload failed: {}", e)))?;
51+
4452
Ok(())
4553
}
4654
}

node/common/src/service/archive/mod.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ impl ArchiveServiceClients {
114114
let precomputed_block: PrecomputedBlock = match breadcrumb.try_into() {
115115
Ok(block) => block,
116116
Err(_) => {
117-
node::core::warn!(summary = "Failed to convert breadcrumb to precomputed block");
117+
node::core::warn!(
118+
summary = "Failed to convert breadcrumb to precomputed block"
119+
);
118120
return;
119121
}
120122
};
@@ -131,11 +133,19 @@ impl ArchiveServiceClients {
131133
};
132134

133135
if options.uses_local_precomputed_storage() {
134-
// TODO(adonagy): Cleanup the unwraps (fn that returns a Result + log the error)
135136
if let Some(path) = &self.local_path {
136-
let file_path = Path::new(path).join(key.clone());
137-
let mut file = File::create(file_path).unwrap();
138-
file.write_all(&data).unwrap();
137+
let key_clone = key.clone();
138+
match write_to_local_storage(path, &key, &data) {
139+
Ok(_) => node::core::info!(
140+
summary = "Successfully wrote precomputed block to local storage",
141+
key = key_clone
142+
),
143+
Err(e) => node::core::warn!(
144+
summary = "Failed to write precomputed block to local storage",
145+
key = key_clone,
146+
error = e.to_string()
147+
),
148+
}
139149
} else {
140150
node::core::warn!(summary = "Local precomputed storage path not set");
141151
}
@@ -273,3 +283,22 @@ impl node::transition_frontier::archive::archive_service::ArchiveService for Nod
273283
// Note: Placeholder for the wasm implementation, if we decide to include an archive mode in the future
274284
#[cfg(target_arch = "wasm32")]
275285
mod rpc {}
286+
287+
fn write_to_local_storage(base_path: &str, key: &str, data: &[u8]) -> Result<(), Error> {
288+
use std::fs::{create_dir_all, File};
289+
use std::path::Path;
290+
291+
let path = Path::new(base_path).join(key);
292+
if let Some(parent) = path.parent() {
293+
create_dir_all(parent)
294+
.map_err(|e| Error::UploadError(format!("Directory creation failed: {}", e)))?;
295+
}
296+
297+
let mut file = File::create(&path)
298+
.map_err(|e| Error::UploadError(format!("File creation failed: {}", e)))?;
299+
300+
file.write_all(data)
301+
.map_err(|e| Error::UploadError(format!("File write failed: {}", e)))?;
302+
303+
Ok(())
304+
}

node/src/ledger/write/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ impl TryFrom<&BlockApplyResult> for v2::PrecomputedBlock {
194194
.clone()
195195
.into(),
196196
staged_ledger_diff: value.block.body().staged_ledger_diff.clone(),
197-
// TODO(adonagy): add the actual delta transition chain proof
198-
delta_transition_chain_proof: (
199-
mina_p2p_messages::v2::LedgerHash::zero(),
200-
mina_p2p_messages::list::List::new(),
201-
),
197+
delta_transition_chain_proof: value.block.header().delta_block_chain_proof.clone(),
202198
protocol_version: value.block.header().current_protocol_version.clone(),
203199
proposed_protocol_version: None,
204200
accounts_accessed: archive_transition_frontier_diff.accounts_accessed(),

0 commit comments

Comments
 (0)