|
| 1 | +// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Tokio-based replacement for SenderAllocation actor |
| 5 | +//! |
| 6 | +//! This is a simplified, tokio-based replacement for the ractor SenderAllocation |
| 7 | +//! that maintains API compatibility while using tasks and channels. |
| 8 | +
|
| 9 | +use std::{marker::PhantomData, time::Instant}; |
| 10 | + |
| 11 | +use tokio::sync::{mpsc, oneshot}; |
| 12 | + |
| 13 | +use super::{ |
| 14 | + sender_account::{RavInformation, ReceiptFees, SenderAccountMessage}, |
| 15 | + sender_accounts_manager::{AllocationId, NewReceiptNotification}, |
| 16 | + unaggregated_receipts::UnaggregatedReceipts, |
| 17 | +}; |
| 18 | +use crate::{ |
| 19 | + actor_migrate::{LifecycleManager, RestartPolicy, TaskHandle}, |
| 20 | + tap::context::NetworkVersion, |
| 21 | +}; |
| 22 | + |
| 23 | +/// Message types for SenderAllocationTask - matches original SenderAllocationMessage |
| 24 | +#[derive(Debug)] |
| 25 | +pub enum SenderAllocationMessage { |
| 26 | + /// New receipt message |
| 27 | + NewReceipt(NewReceiptNotification), |
| 28 | + /// Triggers a Rav Request for the current allocation |
| 29 | + TriggerRavRequest, |
| 30 | + #[cfg(any(test, feature = "test"))] |
| 31 | + /// Return the internal state (used for tests) |
| 32 | + GetUnaggregatedReceipts(oneshot::Sender<UnaggregatedReceipts>), |
| 33 | +} |
| 34 | + |
| 35 | +/// Tokio task-based replacement for SenderAllocation actor |
| 36 | +pub struct SenderAllocationTask<T: NetworkVersion> { |
| 37 | + _phantom: PhantomData<T>, |
| 38 | +} |
| 39 | + |
| 40 | +/// Simple state structure for the task |
| 41 | +struct TaskState { |
| 42 | + /// Sum of all receipt fees for the current allocation |
| 43 | + unaggregated_fees: UnaggregatedReceipts, |
| 44 | + /// Handle to communicate with parent SenderAccount |
| 45 | + sender_account_handle: TaskHandle<SenderAccountMessage>, |
| 46 | + /// Current allocation ID |
| 47 | + allocation_id: AllocationId, |
| 48 | +} |
| 49 | + |
| 50 | +impl<T: NetworkVersion> SenderAllocationTask<T> { |
| 51 | + /// Spawn a new SenderAllocationTask with minimal arguments |
| 52 | + pub async fn spawn_simple( |
| 53 | + lifecycle: &LifecycleManager, |
| 54 | + name: Option<String>, |
| 55 | + allocation_id: AllocationId, |
| 56 | + sender_account_handle: TaskHandle<SenderAccountMessage>, |
| 57 | + ) -> anyhow::Result<TaskHandle<SenderAllocationMessage>> { |
| 58 | + let state = TaskState { |
| 59 | + unaggregated_fees: UnaggregatedReceipts::default(), |
| 60 | + sender_account_handle, |
| 61 | + allocation_id, |
| 62 | + }; |
| 63 | + |
| 64 | + lifecycle |
| 65 | + .spawn_task( |
| 66 | + name, |
| 67 | + RestartPolicy::Never, |
| 68 | + 100, // Buffer size for message channel |
| 69 | + |rx, _ctx| Self::run_task(state, rx), |
| 70 | + ) |
| 71 | + .await |
| 72 | + } |
| 73 | + |
| 74 | + /// Main task loop |
| 75 | + async fn run_task( |
| 76 | + mut state: TaskState, |
| 77 | + mut rx: mpsc::Receiver<SenderAllocationMessage>, |
| 78 | + ) -> anyhow::Result<()> { |
| 79 | + // Send initial state to parent |
| 80 | + state |
| 81 | + .sender_account_handle |
| 82 | + .cast(SenderAccountMessage::UpdateReceiptFees( |
| 83 | + state.allocation_id, |
| 84 | + ReceiptFees::UpdateValue(state.unaggregated_fees.clone()), |
| 85 | + )) |
| 86 | + .await?; |
| 87 | + |
| 88 | + while let Some(message) = rx.recv().await { |
| 89 | + match message { |
| 90 | + SenderAllocationMessage::NewReceipt(notification) => { |
| 91 | + if let Err(e) = Self::handle_new_receipt(&mut state, notification).await { |
| 92 | + tracing::error!( |
| 93 | + allocation_id = ?state.allocation_id, |
| 94 | + error = %e, |
| 95 | + "Error handling new receipt" |
| 96 | + ); |
| 97 | + } |
| 98 | + } |
| 99 | + SenderAllocationMessage::TriggerRavRequest => { |
| 100 | + if let Err(e) = Self::handle_rav_request(&mut state).await { |
| 101 | + tracing::error!( |
| 102 | + allocation_id = ?state.allocation_id, |
| 103 | + error = %e, |
| 104 | + "Error handling RAV request" |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + #[cfg(any(test, feature = "test"))] |
| 109 | + SenderAllocationMessage::GetUnaggregatedReceipts(reply) => { |
| 110 | + let _ = reply.send(state.unaggregated_fees.clone()); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | + |
| 118 | + /// Handle new receipt - simplified version |
| 119 | + async fn handle_new_receipt( |
| 120 | + state: &mut TaskState, |
| 121 | + notification: NewReceiptNotification, |
| 122 | + ) -> anyhow::Result<()> { |
| 123 | + // For now, just accept all receipts as valid |
| 124 | + // In the full implementation, this would include validation |
| 125 | + |
| 126 | + let value = match notification { |
| 127 | + NewReceiptNotification::V1(ref n) => n.value, |
| 128 | + NewReceiptNotification::V2(ref n) => n.value, |
| 129 | + }; |
| 130 | + |
| 131 | + let timestamp_ns = match notification { |
| 132 | + NewReceiptNotification::V1(ref n) => n.timestamp_ns, |
| 133 | + NewReceiptNotification::V2(ref n) => n.timestamp_ns, |
| 134 | + }; |
| 135 | + |
| 136 | + // Update local state |
| 137 | + state.unaggregated_fees.value += value; |
| 138 | + state.unaggregated_fees.counter += 1; |
| 139 | + |
| 140 | + // Notify parent |
| 141 | + state |
| 142 | + .sender_account_handle |
| 143 | + .cast(SenderAccountMessage::UpdateReceiptFees( |
| 144 | + state.allocation_id, |
| 145 | + ReceiptFees::NewReceipt(value, timestamp_ns), |
| 146 | + )) |
| 147 | + .await?; |
| 148 | + |
| 149 | + Ok(()) |
| 150 | + } |
| 151 | + |
| 152 | + /// Handle RAV request - simplified version |
| 153 | + async fn handle_rav_request(state: &mut TaskState) -> anyhow::Result<()> { |
| 154 | + let _start_time = Instant::now(); |
| 155 | + |
| 156 | + // For now, simulate a successful RAV request |
| 157 | + // In the full implementation, this would make actual gRPC calls |
| 158 | + |
| 159 | + // Create a dummy RAV info |
| 160 | + let rav_info = RavInformation { |
| 161 | + allocation_id: match state.allocation_id { |
| 162 | + AllocationId::Legacy(id) => id.into_inner(), |
| 163 | + AllocationId::Horizon(id) => { |
| 164 | + // Convert CollectionId to Address - this is a simplification |
| 165 | + thegraph_core::AllocationId::from(id).into_inner() |
| 166 | + } |
| 167 | + }, |
| 168 | + value_aggregate: state.unaggregated_fees.value, |
| 169 | + }; |
| 170 | + |
| 171 | + // Reset local fees since they're now covered by RAV |
| 172 | + state.unaggregated_fees = UnaggregatedReceipts::default(); |
| 173 | + |
| 174 | + // Notify parent of successful RAV |
| 175 | + state |
| 176 | + .sender_account_handle |
| 177 | + .cast(SenderAccountMessage::UpdateReceiptFees( |
| 178 | + state.allocation_id, |
| 179 | + ReceiptFees::RavRequestResponse( |
| 180 | + state.unaggregated_fees.clone(), |
| 181 | + Ok(Some(rav_info)), |
| 182 | + ), |
| 183 | + )) |
| 184 | + .await?; |
| 185 | + |
| 186 | + Ok(()) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +#[cfg(test)] |
| 191 | +mod tests { |
| 192 | + use super::*; |
| 193 | + use crate::tap::context::Legacy; |
| 194 | + |
| 195 | + #[tokio::test] |
| 196 | + async fn test_sender_allocation_task_creation() { |
| 197 | + // Test basic task creation and message handling |
| 198 | + let lifecycle = LifecycleManager::new(); |
| 199 | + |
| 200 | + // Create a dummy parent handle for testing |
| 201 | + let (parent_tx, mut parent_rx) = mpsc::channel(10); |
| 202 | + let parent_handle = TaskHandle::new_for_test( |
| 203 | + parent_tx, |
| 204 | + Some("test_parent".to_string()), |
| 205 | + std::sync::Arc::new(lifecycle.clone()), |
| 206 | + ); |
| 207 | + |
| 208 | + let allocation_id = |
| 209 | + AllocationId::Legacy(thegraph_core::AllocationId::new([1u8; 20].into())); |
| 210 | + |
| 211 | + let task_handle = SenderAllocationTask::<Legacy>::spawn_simple( |
| 212 | + &lifecycle, |
| 213 | + Some("test_allocation".to_string()), |
| 214 | + allocation_id, |
| 215 | + parent_handle, |
| 216 | + ) |
| 217 | + .await |
| 218 | + .unwrap(); |
| 219 | + |
| 220 | + // Test sending a message |
| 221 | + let notification = NewReceiptNotification::V1( |
| 222 | + super::super::sender_accounts_manager::NewReceiptNotificationV1 { |
| 223 | + id: 1, |
| 224 | + allocation_id: thegraph_core::AllocationId::new([1u8; 20].into()).into_inner(), |
| 225 | + signer_address: thegraph_core::alloy::primitives::Address::ZERO, |
| 226 | + timestamp_ns: 1000, |
| 227 | + value: 100, |
| 228 | + }, |
| 229 | + ); |
| 230 | + |
| 231 | + task_handle |
| 232 | + .cast(SenderAllocationMessage::NewReceipt(notification)) |
| 233 | + .await |
| 234 | + .unwrap(); |
| 235 | + |
| 236 | + // Verify parent received the message |
| 237 | + let parent_message = |
| 238 | + tokio::time::timeout(std::time::Duration::from_millis(100), parent_rx.recv()) |
| 239 | + .await |
| 240 | + .unwrap() |
| 241 | + .unwrap(); |
| 242 | + |
| 243 | + // Check it's the right type of message |
| 244 | + matches!(parent_message, SenderAccountMessage::UpdateReceiptFees(..)); |
| 245 | + } |
| 246 | +} |
0 commit comments