Skip to content
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
27 changes: 27 additions & 0 deletions blockzilla/get-block/scripts/test-worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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' });
Expand Down
192 changes: 169 additions & 23 deletions blockzilla/get-block/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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(),
)?
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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",
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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<Response> {
json_error_profiled(status, code, message, None)
}
Expand Down Expand Up @@ -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));
}
}
11 changes: 9 additions & 2 deletions blockzilla/get-block/src/worker/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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" {
Expand Down
23 changes: 20 additions & 3 deletions blockzilla/get-block/src/worker/stored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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());
Expand Down