Skip to content
Open
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
28 changes: 27 additions & 1 deletion crates/common/src/interop/ssv/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ pub async fn request_ssv_pubkeys_from_ssv_node(
http_timeout: Duration,
) -> eyre::Result<SSVNodeResponse> {
let client = reqwest::ClientBuilder::new().timeout(http_timeout).build()?;
// The SSV node API expects operator IDs as numeric (uint64) values. Serializing the
// U256 directly emits a JSON string, which the node rejects with a 400, so narrow it
// to a u64 first.
let operator_id = u64::try_from(node_operator_id)
.map_err(|e| eyre::eyre!("SSV node operator ID does not fit in u64: {e}"))?;
let body = json!({
"operators": [node_operator_id]
"operators": [operator_id]
});
let response = client.get(url).json(&body).send().await.map_err(|e| {
if e.is_timeout() {
Expand Down Expand Up @@ -51,3 +56,24 @@ pub async fn request_ssv_pubkeys_from_public_api(
serde_json::from_slice::<SSVPublicResponse>(&body_bytes)
.wrap_err("failed to parse SSV response")
}

#[cfg(test)]
mod tests {
use alloy::primitives::U256;
use serde_json::json;

#[test]
fn ssv_node_request_serializes_operator_as_number() {
let node_operator_id = U256::from(100u64);

// Regression guard: serializing the U256 directly emits a (hex) JSON string, which
// the SSV node rejects ("cannot unmarshal string into ... uint64").
let stringy = serde_json::to_string(&json!({ "operators": [node_operator_id] })).unwrap();
assert_eq!(stringy, r#"{"operators":["0x64"]}"#);

// The fix narrows to u64 so the operator ID is emitted as a numeric value.
let operator_id = u64::try_from(node_operator_id).unwrap();
let numeric = serde_json::to_string(&json!({ "operators": [operator_id] })).unwrap();
assert_eq!(numeric, r#"{"operators":[100]}"#);
}
}
Loading