Generates a random ID for JSON-RPC requests. Uses a secure random number generator to create a hex-encoded ID. Falls back to a timestamp-based ID if random generation fails.
()
| 42 | /// Uses a secure random number generator to create a hex-encoded ID. Falls back |
| 43 | /// to a timestamp-based ID if random generation fails. |
| 44 | fn generate_random_id() -> String { |
| 45 | let mut bytes = [0u8; 10]; |
| 46 | match SysRng.try_fill_bytes(&mut bytes) { |
| 47 | Ok(_) => hex::encode(bytes), |
| 48 | Err(_) => { |
| 49 | // Fallback to a timestamp-based ID if random generation fails |
| 50 | let timestamp = std::time::SystemTime::now() |
| 51 | .duration_since(std::time::UNIX_EPOCH) |
| 52 | .unwrap_or_default() |
| 53 | .as_nanos(); |
| 54 | format!("fallback-{}", timestamp) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// # RequestObject |
| 60 | /// |