|
| 1 | +use crate::state::system_state::SystemState; |
| 2 | +use alloy::primitives::{Address, U256}; |
| 3 | +use anyhow::Result; |
| 4 | +use log::{debug, error, info}; |
| 5 | +use reqwest::header::HeaderValue; |
| 6 | +use reqwest::Client; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use shared::models::node::Node; |
| 9 | +use shared::security::request_signer::sign_request; |
| 10 | +use shared::web3::contracts::core::builder::Contracts; |
| 11 | +use shared::web3::wallet::Wallet; |
| 12 | +use std::str::FromStr; |
| 13 | +use std::sync::Arc; |
| 14 | + |
| 15 | +#[derive(Deserialize, Serialize, Debug)] |
| 16 | +pub struct RequestUploadRequest { |
| 17 | + pub file_name: String, |
| 18 | + pub file_size: u64, |
| 19 | + pub file_type: String, |
| 20 | +} |
| 21 | + |
| 22 | +/// Handles a file upload request |
| 23 | +pub async fn handle_file_upload( |
| 24 | + storage_path: &str, |
| 25 | + task_id: &str, |
| 26 | + file_name: &str, |
| 27 | + wallet: &Arc<Wallet>, |
| 28 | + state: &Arc<SystemState>, |
| 29 | +) -> Result<()> { |
| 30 | + println!("Starting file upload handler..."); |
| 31 | + info!("📄 Received file upload request: {}", file_name); |
| 32 | + |
| 33 | + // Get orchestrator endpoint |
| 34 | + println!("Getting orchestrator endpoint..."); |
| 35 | + let endpoint = state |
| 36 | + .get_heartbeat_endpoint() |
| 37 | + .await |
| 38 | + .ok_or_else(|| { |
| 39 | + error!("Orchestrator endpoint is not set - cannot upload file."); |
| 40 | + anyhow::anyhow!("Orchestrator endpoint not set") |
| 41 | + })? |
| 42 | + .replace("/heartbeat", ""); |
| 43 | + println!("Got endpoint: {}", endpoint); |
| 44 | + |
| 45 | + // Construct file path |
| 46 | + let file = format!("{}/prime-task-{}/{}", storage_path, task_id, file_name); |
| 47 | + println!("Constructed file path: {}", file); |
| 48 | + debug!("File: {:?}", file); |
| 49 | + |
| 50 | + // Get file size |
| 51 | + println!("Getting file size..."); |
| 52 | + let file_size = std::fs::metadata(&file).map(|m| m.len()).unwrap_or(0); |
| 53 | + |
| 54 | + // Calculate SHA |
| 55 | + println!("Calculating file SHA..."); |
| 56 | + let file_sha = tokio::fs::read(&file) |
| 57 | + .await |
| 58 | + .map(|contents| { |
| 59 | + use sha2::{Digest, Sha256}; |
| 60 | + let mut hasher = Sha256::new(); |
| 61 | + hasher.update(&contents); |
| 62 | + format!("{:x}", hasher.finalize()) |
| 63 | + }) |
| 64 | + .unwrap_or_else(|e| { |
| 65 | + error!("Failed to calculate file SHA: {}", e); |
| 66 | + String::new() |
| 67 | + }); |
| 68 | + |
| 69 | + debug!("File size: {:?}", file_size); |
| 70 | + debug!("File SHA: {}", file_sha); |
| 71 | + println!("File size: {}, SHA: {}", file_size, file_sha); |
| 72 | + |
| 73 | + // Create upload request |
| 74 | + println!("Creating upload request..."); |
| 75 | + let client = Client::new(); |
| 76 | + let request = RequestUploadRequest { |
| 77 | + file_name: file_sha.to_string(), |
| 78 | + file_size, |
| 79 | + file_type: "application/json".to_string(), // Assume JSON |
| 80 | + }; |
| 81 | + |
| 82 | + // Sign request |
| 83 | + println!("Signing request..."); |
| 84 | + let request_value = serde_json::to_value(&request)?; |
| 85 | + let signature = sign_request("/storage/request-upload", wallet, Some(&request_value)) |
| 86 | + .await |
| 87 | + .map_err(|e| anyhow::anyhow!(e.to_string()))?; |
| 88 | + println!("Request signed with signature: {}", signature); |
| 89 | + |
| 90 | + // Prepare headers |
| 91 | + println!("Preparing request headers..."); |
| 92 | + let mut headers = reqwest::header::HeaderMap::new(); |
| 93 | + headers.insert( |
| 94 | + "x-address", |
| 95 | + HeaderValue::from_str(&wallet.address().to_string())?, |
| 96 | + ); |
| 97 | + headers.insert("x-signature", HeaderValue::from_str(&signature)?); |
| 98 | + |
| 99 | + // Create upload URL |
| 100 | + let upload_url = format!("{}/storage/request-upload", endpoint); |
| 101 | + println!("Upload URL: {}", upload_url); |
| 102 | + |
| 103 | + // Send request |
| 104 | + println!("Sending request to get signed URL..."); |
| 105 | + let response = client |
| 106 | + .post(&upload_url) |
| 107 | + .json(&request) |
| 108 | + .headers(headers) |
| 109 | + .send() |
| 110 | + .await?; |
| 111 | + |
| 112 | + println!("Response: {:?}", response); |
| 113 | + // Process response |
| 114 | + let json = response.json::<serde_json::Value>().await?; |
| 115 | + println!("Response JSON: {:?}", json); |
| 116 | + |
| 117 | + if let Some(signed_url) = json["signed_url"].as_str() { |
| 118 | + info!("Got signed URL for upload: {}", signed_url); |
| 119 | + println!("Got signed URL: {}", signed_url); |
| 120 | + |
| 121 | + // Read file contents |
| 122 | + println!("Reading file contents..."); |
| 123 | + let file_contents = tokio::fs::read(&file).await?; |
| 124 | + println!("File contents size: {} bytes", file_contents.len()); |
| 125 | + |
| 126 | + // Upload file to S3 using signed URL |
| 127 | + println!("Uploading file to S3..."); |
| 128 | + let upload_response = client |
| 129 | + .put(signed_url) |
| 130 | + .body(file_contents) |
| 131 | + .header("Content-Type", "application/json") |
| 132 | + .send() |
| 133 | + .await?; |
| 134 | + |
| 135 | + println!("S3 upload response: {:?}", upload_response); |
| 136 | + info!("Successfully uploaded file to S3"); |
| 137 | + } else { |
| 138 | + println!("Error: Missing signed_url in response"); |
| 139 | + return Err(anyhow::anyhow!("Missing signed_url in response")); |
| 140 | + } |
| 141 | + |
| 142 | + println!("File upload completed successfully"); |
| 143 | + Ok(()) |
| 144 | +} |
| 145 | + |
| 146 | +/// Handles a file validation request |
| 147 | +pub async fn handle_file_validation( |
| 148 | + file_sha: &str, |
| 149 | + contracts: &Arc<Contracts>, |
| 150 | + node: &Node, |
| 151 | +) -> Result<()> { |
| 152 | + info!("📄 Received file SHA for validation: {}", file_sha); |
| 153 | + |
| 154 | + let pool_id = node.compute_pool_id; |
| 155 | + let node_address = &node.id; |
| 156 | + |
| 157 | + let decoded_sha = hex::decode(file_sha)?; |
| 158 | + debug!( |
| 159 | + "Decoded file sha: {:?} ({} bytes)", |
| 160 | + decoded_sha, |
| 161 | + decoded_sha.len() |
| 162 | + ); |
| 163 | + |
| 164 | + let result = contracts |
| 165 | + .compute_pool |
| 166 | + .submit_work( |
| 167 | + U256::from(pool_id), |
| 168 | + Address::from_str(node_address)?, |
| 169 | + decoded_sha.to_vec(), |
| 170 | + ) |
| 171 | + .await; |
| 172 | + |
| 173 | + debug!("Submit work result: {:?}", result); |
| 174 | + |
| 175 | + Ok(()) |
| 176 | +} |
0 commit comments