Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(torii): handle failed metadata fetch and empty metadata in db #2755

Merged
merged 1 commit into from
Dec 3, 2024
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
13 changes: 11 additions & 2 deletions crates/torii/core/src/executor/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,18 @@
"Failed to fetch metadata for token_id: {}",
register_erc721_token.actual_token_id
)
})?;
});

Check warning on line 245 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L245

Added line #L245 was not covered by tests

serde_json::to_string(&metadata).context("Failed to serialize metadata")?
if let Ok(metadata) = metadata {
serde_json::to_string(&metadata).context("Failed to serialize metadata")?

Check warning on line 248 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L247-L248

Added lines #L247 - L248 were not covered by tests
} else {
warn!(
contract_address = format!("{:#x}", register_erc721_token.contract_address),
token_id = %register_erc721_token.actual_token_id,
"Error fetching metadata, empty metadata will be used instead.",

Check warning on line 253 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L250-L253

Added lines #L250 - L253 were not covered by tests
);
"".to_string()

Check warning on line 255 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L255

Added line #L255 was not covered by tests
}
};

Ok(RegisterErc721TokenMetadata { query: register_erc721_token, metadata, name, symbol })
Expand Down
2 changes: 0 additions & 2 deletions crates/torii/core/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,6 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {

self.register_tasks.spawn(async move {
let permit = semaphore.acquire().await.unwrap();
let span = tracing::span!(tracing::Level::INFO, "contract_address_span", contract_address = %register_erc721_token.contract_address);
let _enter = span.enter();

let result = Self::process_register_erc721_token_query(
register_erc721_token,
Expand Down
16 changes: 7 additions & 9 deletions crates/torii/graphql/src/object/erc/token_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@
JOIN tokens t ON b.token_id = t.id
JOIN contracts c ON t.contract_address = c.contract_address"
);
let mut conditions = vec!["b.account_address = ?".to_string()];
let mut conditions = vec![
"(b.account_address = ?)".to_string(),
"(t.metadata IS NULL OR length(t.metadata) > 0)".to_string(),
];

Check warning on line 109 in crates/torii/graphql/src/object/erc/token_balance.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_balance.rs#L106-L109

Added lines #L106 - L109 were not covered by tests

let mut cursor_param = &connection.after;
if let Some(after_cursor) = &connection.after {
Expand Down Expand Up @@ -234,14 +237,9 @@
let token_id = row.token_id.split(':').collect::<Vec<&str>>();
assert!(token_id.len() == 2);

// skip the token if metadata is null
if row.metadata.is_none() {
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
let metadata_str = row.metadata.as_ref().unwrap();

let metadata_str = row.metadata;

Check warning on line 240 in crates/torii/graphql/src/object/erc/token_balance.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_balance.rs#L240

Added line #L240 was not covered by tests
let metadata: serde_json::Value =
serde_json::from_str(metadata_str).expect("metadata is always json");
serde_json::from_str(&metadata_str).expect("metadata is always json");

Check warning on line 242 in crates/torii/graphql/src/object/erc/token_balance.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_balance.rs#L242

Added line #L242 was not covered by tests
Comment on lines +240 to +242
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Consider using proper error handling for JSON parsing.

Using .expect() could cause runtime panics if the metadata is not valid JSON. Even if metadata is present, it might not be well-formed JSON.

Consider handling the JSON parsing error gracefully:

-                let metadata: serde_json::Value =
-                    serde_json::from_str(&metadata_str).expect("metadata is always json");
+                let metadata: serde_json::Value = match serde_json::from_str(&metadata_str) {
+                    Ok(value) => value,
+                    Err(e) => {
+                        warn!("Failed to parse metadata JSON: {}", e);
+                        continue;
+                    }
+                };

Committable suggestion skipped: line range outside the PR's diff.

let metadata_name =
metadata.get("name").map(|v| v.to_string().trim_matches('"').to_string());
let metadata_description = metadata
Expand Down Expand Up @@ -301,5 +299,5 @@
pub token_id: String,
pub balance: String,
pub contract_type: String,
pub metadata: Option<String>,
pub metadata: String,
}
16 changes: 7 additions & 9 deletions crates/torii/graphql/src/object/erc/token_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@
"#,
);

let mut conditions = vec!["et.from_address = ? OR et.to_address = ?".to_string()];
let mut conditions = vec![
"(et.from_address = ? OR et.to_address = ?)".to_string(),
"(t.metadata IS NULL OR length(t.metadata) > 0)".to_string(),
];

Check warning on line 125 in crates/torii/graphql/src/object/erc/token_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_transfer.rs#L122-L125

Added lines #L122 - L125 were not covered by tests

let mut cursor_param = &connection.after;
if let Some(after_cursor) = &connection.after {
Expand Down Expand Up @@ -263,14 +266,9 @@
let token_id = row.token_id.split(':').collect::<Vec<&str>>();
assert!(token_id.len() == 2);

// skip the token if metadata is null
if row.metadata.is_none() {
continue;
}

let metadata_str = row.metadata.as_ref().unwrap();
let metadata_str = row.metadata;

Check warning on line 269 in crates/torii/graphql/src/object/erc/token_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_transfer.rs#L269

Added line #L269 was not covered by tests
let metadata: serde_json::Value =
serde_json::from_str(metadata_str).expect("metadata is always json");
serde_json::from_str(&metadata_str).expect("metadata is always json");

Check warning on line 271 in crates/torii/graphql/src/object/erc/token_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_transfer.rs#L271

Added line #L271 was not covered by tests
let metadata_name =
metadata.get("name").map(|v| v.to_string().trim_matches('"').to_string());
let metadata_description = metadata
Expand Down Expand Up @@ -339,7 +337,7 @@
pub symbol: String,
pub decimals: u8,
pub contract_type: String,
pub metadata: Option<String>,
pub metadata: String,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add tests for metadata handling.

The change to non-optional metadata field requires comprehensive testing to ensure robustness.

Consider adding tests for:

  1. Valid metadata handling
  2. Invalid JSON metadata handling
  3. Edge cases (empty strings, very large metadata)

Would you like me to help create a test suite for these scenarios?

}

#[derive(Debug, Clone)]
Expand Down
Loading