diff --git a/blockzilla/get-block/scripts/test-worker.mjs b/blockzilla/get-block/scripts/test-worker.mjs index 101b8d24..24fc8469 100644 --- a/blockzilla/get-block/scripts/test-worker.mjs +++ b/blockzilla/get-block/scripts/test-worker.mjs @@ -344,6 +344,25 @@ async function main() { assert.equal(missing.body.result, null); assert.notEqual(missing.response.headers.get('X-Blockzilla-Cache'), 'HIT'); } + for (let attempt = 0; attempt < 2; attempt += 1) { + const unarchived = await getJson('/block/999999999.json'); + assert.equal(unarchived.response.status, 404, JSON.stringify(unarchived.body)); + assertEnvelope(unarchived.body); + assert.equal(unarchived.body.error.code, -32004); + assert.equal(unarchived.body.error.message, 'Block not available for slot 999999999'); + assert.notEqual(unarchived.response.headers.get('X-Blockzilla-Cache'), 'HIT'); + for (const forbidden of [ARCHIVE_PREFIX, 'BZ_ARCHIVE_BUCKET', 'R2 GET', 'S3 GET', '.index']) { + assert.ok(!JSON.stringify(unarchived.body).includes(forbidden)); + } + const unarchivedBin = await request('/block/999999999.bin'); + assert.equal(unarchivedBin.status, 404); + assert.notEqual(unarchivedBin.headers.get('X-Blockzilla-Cache'), 'HIT'); + const unarchivedBinBody = await unarchivedBin.json(); + assert.equal(unarchivedBinBody.error.code, 'block_unavailable'); + for (const forbidden of [ARCHIVE_PREFIX, 'BZ_ARCHIVE_BUCKET', 'R2 GET', 'S3 GET', '.index']) { + assert.ok(!JSON.stringify(unarchivedBinBody).includes(forbidden)); + } + } for (const invalidPath of ['/block/not-a-slot.json', '/block/432000.json?rewards=invalid', ...['json', 'base58', 'jsonParsed', 'invalid'].map(encoding => `/block/432000.json?encoding=${encoding}`)]) { const invalid = await getJson(invalidPath); @@ -418,6 +437,10 @@ async function main() { assert.equal(invalid.response.status, 502); assertEnvelope(invalid.body); assert.ok(invalid.body.error); + assert.equal(invalid.body.error.message, `Failed to fetch block for slot ${FIRST_SLOT}`); + for (const forbidden of [ARCHIVE_PREFIX, CURRENT_MARKER, 'BZ_OTHER_BUCKET', 'BZ_ARCHIVE_BUCKET', 'R2', 'S3']) { + assert.ok(!JSON.stringify(invalid.body).includes(forbidden)); + } assert.notEqual(invalid.response.headers.get('X-Blockzilla-Cache'), 'HIT'); } await putObject(CURRENT_MARKER, currentMarker); @@ -426,6 +449,10 @@ async function main() { result = await getJson(path); assert.equal(result.response.status, 502); assertEnvelope(result.body); + assert.equal(result.body.error.message, `Failed to fetch block for slot ${FIRST_SLOT}`); + for (const forbidden of [ARCHIVE_PREFIX, CURRENT_MARKER, MAY24_MARKER, 'BZ_OTHER_BUCKET', 'BZ_ARCHIVE_BUCKET', 'R2', 'S3']) { + assert.ok(!JSON.stringify(result.body).includes(forbidden)); + } await deleteObject(CURRENT_MARKER); await putObject(LEGACY_METADATA_MARKER, LEGACY_METADATA_MARKER_BYTES); await update({ generation: 'local-test-historical-markers' }); diff --git a/blockzilla/get-block/src/worker.rs b/blockzilla/get-block/src/worker.rs index b6f54271..2e4bcee4 100644 --- a/blockzilla/get-block/src/worker.rs +++ b/blockzilla/get-block/src/worker.rs @@ -1331,8 +1331,14 @@ async fn handle_authenticated_direct_block( source } Err(err) => { + console_error!("worker configuration error: {err}"); return finish_metered_response( - json_error_profiled(500, "config_error", &err, profile.as_ref())?, + json_error_profiled( + 500, + "config_error", + "Service configuration error", + profile.as_ref(), + )?, &api_key, &metrics, MeteredRequest::new(method, "config_error", cache_status, transport, 0, 0), @@ -1452,10 +1458,15 @@ async fn handle_authenticated_direct_block( Err(error) => error, _ => "Foundation finalized lookup returned an invalid result type".into(), }; + console_error!( + "foundation finalized lookup failed for slot {}: {}", + request.slot, + error + ); let mut response = json_error_profiled( 502, "foundation_lookup_error", - &error, + "Upstream block lookup failed", profile.as_ref(), )?; response @@ -1575,10 +1586,15 @@ async fn handle_authenticated_direct_block( ); } Err(error) => { + console_error!( + "foundation archive lookup failed for slot {}: {}", + request.slot, + error + ); let mut response = json_error_profiled( 502, "foundation_lookup_error", - &error, + "Upstream block lookup failed", profile.as_ref(), )?; response @@ -1685,6 +1701,32 @@ async fn handle_authenticated_direct_block( "not_found", ) } + Err(err) if is_archive_epoch_not_found(&err) => { + console_error!( + "archive epoch not available for slot {}: {}", + request.slot, + err + ); + source.record_elapsed( + "route_total", + route_started, + 0, + "json_unavailable", + None, + ); + ( + json_rpc_response_profiled( + error_response( + Value::from(1), + -32004, + format!("Block not available for slot {}", request.slot), + ), + profile.as_ref(), + )? + .with_status(404), + "not_found", + ) + } Err(err) => { console_error!("block route failed for slot {}: {}", request.slot, err); source.record_elapsed("route_total", route_started, 0, "json_error", None); @@ -1693,7 +1735,7 @@ async fn handle_authenticated_direct_block( error_response( Value::from(1), -32000, - format!("failed to fetch slot {}: {err}", request.slot), + format!("Failed to fetch block for slot {}", request.slot), ), profile.as_ref(), )? @@ -1717,15 +1759,22 @@ async fn handle_authenticated_direct_block( .await { Ok(response) => (response, "ok"), - Err(error) => ( - json_error_profiled( - 502, - "foundation_block_error", - &error, - profile.as_ref(), - )?, - "backend_error", - ), + Err(error) => { + console_error!( + "foundation binary route failed for slot {}: {}", + request.slot, + error + ); + ( + json_error_profiled( + 502, + "foundation_block_error", + "Upstream block fetch failed", + profile.as_ref(), + )?, + "backend_error", + ) + } } } else if bundle_object::covered(&source, request.slot) { if client_binary || access_mode != BlockBinAccessMode::Include { @@ -1749,10 +1798,22 @@ async fn handle_authenticated_direct_block( .await { Ok(response) => (response, "ok"), - Err(err) => ( - json_error_profiled(502, "bundle_error", &err, profile.as_ref())?, - "backend_error", - ), + Err(err) => { + console_error!( + "bundle binary route failed for slot {}: {}", + request.slot, + err + ); + ( + json_error_profiled( + 502, + "bundle_error", + "Failed to fetch stored block bundle", + profile.as_ref(), + )?, + "backend_error", + ) + } } } } else if client_binary && !resolution::covered(&source, request.slot) { @@ -1782,10 +1843,22 @@ async fn handle_authenticated_direct_block( .await { Ok(response) => (response, "ok"), - Err(err) => ( - json_error_profiled(502, "resolution_error", &err, profile.as_ref())?, - "backend_error", - ), + Err(err) => { + console_error!( + "resolution binary route failed for slot {}: {}", + request.slot, + err + ); + ( + json_error_profiled( + 502, + "resolution_error", + "Upstream block resolution failed", + profile.as_ref(), + )?, + "backend_error", + ) + } } } else { match fetch_block_bundle_bytes(&source, request.slot, access_mode).await { @@ -1820,6 +1893,29 @@ async fn handle_authenticated_direct_block( "not_found", ) } + Err(err) if is_archive_epoch_not_found(&err) => { + console_error!( + "archive epoch not available for wincode slot {}: {}", + request.slot, + err + ); + source.record_elapsed( + "route_total", + route_started, + 0, + "wincode_unavailable", + None, + ); + ( + json_error_profiled( + 404, + "block_unavailable", + "Block is not available for the requested slot", + profile.as_ref(), + )?, + "not_found", + ) + } Err(err) => { console_error!("wincode route failed for slot {}: {}", request.slot, err); source.record_elapsed( @@ -1833,7 +1929,7 @@ async fn handle_authenticated_direct_block( json_error_profiled( 502, "blockzilla_decode_error", - &format!("failed to fetch slot {}: {err}", request.slot), + &format!("Failed to fetch block for slot {}", request.slot), profile.as_ref(), )?, "backend_error", @@ -4073,7 +4169,14 @@ async fn read_get_block_index_row_with_access( let started = now_ms(); let bytes = source .get_cached_range(&index_path, offset, ARCHIVE_V2_GET_BLOCK_INDEX_ROW_LEN) - .await?; + .await + .map_err(|err| { + if is_missing_primary_object_error(&err, &index_path) { + archive_epoch_not_found_error(epoch, &err) + } else { + err + } + })?; let row = ArchiveV2GetBlockIndexRow { block_offset: u64::from_le_bytes(bytes[0..8].try_into().unwrap()), block_len: u32::from_le_bytes(bytes[8..12].try_into().unwrap()), @@ -7824,6 +7927,24 @@ fn block_bundle_response_profiled( Ok(Response::from_bytes(bytes)?.with_headers(headers)) } +const ARCHIVE_EPOCH_NOT_FOUND_PREFIX: &str = "archive_epoch_not_found:"; + +fn archive_epoch_not_found_error(epoch: u64, detail: &str) -> String { + format!("{ARCHIVE_EPOCH_NOT_FOUND_PREFIX}{epoch}: {detail}") +} + +fn is_archive_epoch_not_found(err: &str) -> bool { + err.starts_with(ARCHIVE_EPOCH_NOT_FOUND_PREFIX) +} + +fn is_missing_primary_object_error(err: &str, path: &str) -> bool { + let trimmed = path.trim_start_matches('/'); + let matches_path = err.contains(path) || (!trimmed.is_empty() && err.contains(trimmed)); + matches_path + && ((err.starts_with("R2 GET ") && err.ends_with(" returned missing object")) + || (err.starts_with("S3 GET ") && err.contains(" returned HTTP 404 for "))) +} + fn json_error(status: u16, code: &str, message: &str) -> Result { json_error_profiled(status, code, message, None) } @@ -8376,4 +8497,29 @@ mod tests { assert!(parse_prefix_content_range("bytes 1-51/52", 52).is_err()); assert!(parse_prefix_content_range("bytes 0-52/53", 52).is_err()); } + + #[test] + fn classifies_missing_primary_index_objects_without_matching_other_failures() { + let path = "compact-v2/blockzilla-20261004/epoch-1101/archive-v2-blocks.index"; + let s3_404 = format!( + "S3 GET {path} returned HTTP 404 for bytes=0-35; an exact partial response is required" + ); + let s3_503 = format!( + "S3 GET {path} returned HTTP 503 for bytes=0-35; an exact partial response is required" + ); + let r2_missing = format!("R2 GET {path} via BZ_ARCHIVE_BUCKET returned missing object"); + let r2_no_body = + format!("R2 GET {path} via BZ_ARCHIVE_BUCKET returned metadata without a body"); + let other_path = "S3 GET compact-v2/blockzilla-20261004/epoch-1101/archive-v2-blocks.zstd returned HTTP 404 for bytes=0-35; an exact partial response is required"; + + assert!(is_missing_primary_object_error(&s3_404, path)); + assert!(is_missing_primary_object_error(&r2_missing, path)); + assert!(!is_missing_primary_object_error(&s3_503, path)); + assert!(!is_missing_primary_object_error(&r2_no_body, path)); + assert!(!is_missing_primary_object_error(other_path, path)); + + let tagged = archive_epoch_not_found_error(1101, &s3_404); + assert!(is_archive_epoch_not_found(&tagged)); + assert!(!is_archive_epoch_not_found(&s3_503)); + } } diff --git a/blockzilla/get-block/src/worker/resolution.rs b/blockzilla/get-block/src/worker/resolution.rs index 335846d3..a971b5b7 100644 --- a/blockzilla/get-block/src/worker/resolution.rs +++ b/blockzilla/get-block/src/worker/resolution.rs @@ -1681,8 +1681,9 @@ async fn handle_authenticated_index( let mut source = match WorkerSource::from_env(&env, profile.clone()) { Ok(source) => source, Err(err) => { + console_error!("resolution worker configuration error: {err}"); return finish_metered_response( - json_error(500, "config_error", &err)?, + json_error(500, "config_error", "Service configuration error")?, &key, &metrics, MeteredRequest::new( @@ -1717,7 +1718,13 @@ async fn handle_authenticated_index( } else { match index_response(&source, slot).await { Ok(response) => (response, "ok"), - Err(err) => (json_error(502, "resolution_error", &err)?, "backend_error"), + Err(err) => { + console_error!("resolution index route failed for slot {slot}: {err}"); + ( + json_error(502, "resolution_error", "Upstream block resolution failed")?, + "backend_error", + ) + } } }; let cached = if outcome == "ok" { diff --git a/blockzilla/get-block/src/worker/stored.rs b/blockzilla/get-block/src/worker/stored.rs index 90888b5f..061e09fd 100644 --- a/blockzilla/get-block/src/worker/stored.rs +++ b/blockzilla/get-block/src/worker/stored.rs @@ -525,9 +525,19 @@ async fn find_index_row( let path = epoch_file(&source.archive_prefix, epoch, ARCHIVE_V2_BLOCK_INDEX_FILE); let header_bytes = source .get_cached_range(&path, 0, ARCHIVE_V2_HOT_INDEX_HEADER_LEN) - .await?; + .await + .map_err(|err| { + if is_missing_primary_object_error(&err, &path) { + archive_epoch_not_found_error(epoch, &err) + } else { + err + } + })?; let Some(size) = source.object_size(&path).await? else { - return Err(format!("archive epoch {epoch} is unavailable")); + return Err(archive_epoch_not_found_error( + epoch, + &format!("archive epoch {epoch} is unavailable"), + )); }; let header = parse_index_header(&header_bytes, size)?; let candidates = crate::slot_lookup::ordinal_window( @@ -582,7 +592,14 @@ async fn find_dense_row( let path = epoch_file(&source.archive_prefix, epoch, slot_index::SLOT_INDEX_FILE); let bytes = source .get_cached_range(&path, 0, slot_index::SLOT_INDEX_HEADER_LEN) - .await?; + .await + .map_err(|err| { + if is_missing_primary_object_error(&err, &path) { + archive_epoch_not_found_error(epoch, &err) + } else { + err + } + })?; let header = slot_index::parse_header(&bytes)?; if source.object_size(&path).await? != Some(header.file_len()?) { return Err("slot-index object length differs from its header geometry".to_string());