Skip to content

Commit

Permalink
fixup! fix: build key type with RawContentKey
Browse files Browse the repository at this point in the history
  • Loading branch information
carver committed Oct 12, 2024
1 parent c8fa070 commit 8791397
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion glados-audit/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing::warn;
/// Checks that content bytes correspond to a correctly formatted
/// content value.
pub fn content_is_valid(content: &content::Model, content_bytes: &[u8]) -> bool {
let raw_key = RawContentKey::from(content.content_key.clone());
let raw_key = RawContentKey::from_iter(&content.content_key);
match content.protocol_id {
content::SubProtocol::History => {
let content_key = match HistoryContentKey::try_from(raw_key) {
Expand Down
4 changes: 2 additions & 2 deletions glados-core/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl PortalApi {
self,
content: &content::Model,
) -> Result<Option<Content>, JsonRpcError> {
let raw_key = RawContentKey::from(content.content_key.clone());
let raw_key = RawContentKey::from_iter(&content.content_key);
match content.protocol_id {
content::SubProtocol::History => match HistoryNetworkApiClient::recursive_find_content(
&self.client,
Expand Down Expand Up @@ -226,7 +226,7 @@ impl PortalApi {
self,
content: &content::Model,
) -> Result<(Option<Content>, String), JsonRpcError> {
let raw_key = RawContentKey::from(content.content_key.clone());
let raw_key = RawContentKey::from_iter(&content.content_key);
match content.protocol_id {
content::SubProtocol::History => {
match HistoryNetworkApiClient::trace_recursive_find_content(
Expand Down
57 changes: 29 additions & 28 deletions glados-web/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use ethportal_api::types::{
};
use ethportal_api::utils::bytes::{hex_decode, hex_encode};
use ethportal_api::{jsonrpsee::core::__reexports::serde_json, BeaconContentKey, StateContentKey};
use ethportal_api::{HistoryContentKey, OverlayContentKey, RawContentKey};
use ethportal_api::{HistoryContentKey, OverlayContentKey};
use glados_core::stats::{
filter_audits, get_audit_stats, AuditFilters, ContentTypeFilter, Period, StrategyFilter,
SuccessFilter,
Expand Down Expand Up @@ -460,12 +460,35 @@ pub async fn contentkey_detail(
Path(content_key_hex): Path<String>,
Extension(state): Extension<Arc<State>>,
) -> Result<HtmlTemplate<ContentKeyDetailTemplate>, StatusCode> {
let content_key_raw = hex_decode(&content_key_hex).map_err(|e| {
error!(content.key=content_key_hex, err=?e, "Could not decode up key bytes");
StatusCode::INTERNAL_SERVER_ERROR
})?;
let (content_id, content_kind, key_bytes) =
if let Ok(content_key) = HistoryContentKey::from_hex(&content_key_hex) {
let content_id = hex_encode(content_key.content_id());
let content_kind = content_key.to_string();
let key_bytes = content_key.to_bytes().to_vec();
(content_id, content_kind, key_bytes)
} else if let Ok(content_key) = StateContentKey::from_hex(&content_key_hex) {
let content_id = hex_encode(content_key.content_id());
let content_kind = content_key.to_string();
let key_bytes = content_key.to_bytes().to_vec();
(content_id, content_kind, key_bytes)
} else if let Ok(content_key) = BeaconContentKey::from_hex(&content_key_hex) {
let content_id = hex_encode(content_key.content_id());
let content_kind = content_key.to_string();
let key_bytes = content_key.to_bytes().to_vec();
(content_id, content_kind, key_bytes)
} else if let Err(e) = hex_decode(&content_key_hex) {
error!(content.key=content_key_hex, err=?e, "Content key must be hex-encoded bytes");
return Err(StatusCode::INTERNAL_SERVER_ERROR);
} else {
error!(
content.key = content_key_hex,
"Could not create key from bytes"
);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
};

let content_key_model = content::Entity::find()
.filter(content::Column::ContentKey.eq(content_key_raw.clone()))
.filter(content::Column::ContentKey.eq(key_bytes))
.one(&state.database_connection)
.await
.map_err(|e| {
Expand All @@ -485,28 +508,6 @@ pub async fn contentkey_detail(
error!(content.key=content_key_hex, err=?e, "Could not look up audits for key");
StatusCode::NOT_FOUND
})?;

let content_key_raw: RawContentKey = content_key_raw.into();
let (content_id, content_kind) =
if let Ok(content_key) = HistoryContentKey::try_from(content_key_raw.clone()) {
let content_id = hex_encode(content_key.content_id());
let content_kind = content_key.to_string();
(content_id, content_kind)
} else if let Ok(content_key) = StateContentKey::try_from(content_key_raw.clone()) {
let content_id = hex_encode(content_key.content_id());
let content_kind = content_key.to_string();
(content_id, content_kind)
} else if let Ok(content_key) = BeaconContentKey::try_from(content_key_raw) {
let content_id = hex_encode(content_key.content_id());
let content_kind = content_key.to_string();
(content_id, content_kind)
} else {
error!(
content.key = content_key_hex,
"Could not create key from bytes"
);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
};
let metadata_model = execution_metadata::Entity::find()
.filter(execution_metadata::Column::Content.eq(content_key_model.id))
.one(&state.database_connection)
Expand Down

0 comments on commit 8791397

Please sign in to comment.