-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathoperator.rs
More file actions
210 lines (195 loc) · 7.05 KB
/
operator.rs
File metadata and controls
210 lines (195 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use crate::config::OperatorConfig;
use crate::error::OperatorError;
use crate::operator::{
feepayer_monitor, fetcher, processor, reconciliation, sender, DbTransactionWriter, RetryConfig,
RpcClientWithRetry,
};
use crate::shutdown_utils::shutdown_operator;
use crate::storage::Storage;
use crate::ContraIndexerConfig;
use solana_sdk::commitment_config::CommitmentConfig;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use tracing::{info, warn};
pub async fn run(
storage: Arc<Storage>,
common_config: ContraIndexerConfig,
config: OperatorConfig,
) -> Result<(), OperatorError> {
info!("Starting Contra Operator");
info!("Program: {:?}", common_config.program_type);
info!("Poll interval: {:?}", config.db_poll_interval);
info!("Retry max attempts: {}", config.retry_max_attempts);
let cancellation_token = CancellationToken::new();
// Initialize global RPC client with retry
let rpc_client = Arc::new(RpcClientWithRetry::with_retry_config(
common_config.rpc_url.clone(),
RetryConfig::default(),
CommitmentConfig {
commitment: config.rpc_commitment,
},
));
// Initialize source RPC client if configured
let source_rpc_client = common_config.source_rpc_url.as_ref().map(|url| {
Arc::new(RpcClientWithRetry::with_retry_config(
url.clone(),
RetryConfig::default(),
CommitmentConfig {
commitment: config.rpc_commitment,
},
))
});
let (processor_tx, processor_rx) = mpsc::channel(config.channel_buffer_size);
let (sender_tx, sender_rx) = mpsc::channel(config.channel_buffer_size);
let (storage_tx, storage_rx) = mpsc::channel::<sender::TransactionStatusUpdate>(100);
// Start fetcher task
let fetcher_storage = storage.clone();
let fetcher_config = config.clone();
let fetcher_token = cancellation_token.clone();
let fetcher_handle = tokio::spawn(async move {
if let Err(e) = fetcher::run_fetcher(
fetcher_storage,
processor_tx,
fetcher_config,
common_config.program_type,
fetcher_token,
)
.await
{
tracing::error!("Fetcher error: {}", e);
}
});
// Start processor task
let program_type = common_config.program_type;
let instance_pda = common_config.escrow_instance_id;
let processor_storage = storage.clone();
let processor_rpc = rpc_client.clone();
let processor_source_rpc = source_rpc_client.clone();
let processor_handle = tokio::spawn(async move {
processor::run_processor(
processor_rx,
sender_tx,
program_type,
instance_pda,
processor_storage,
processor_rpc,
processor_source_rpc,
)
.await;
});
// Start storage writer task (receives updates from sender)
let writer_storage = storage.clone();
let storage_writer = DbTransactionWriter::new(
writer_storage,
storage_rx,
config.alert_webhook_url.clone(),
common_config.program_type,
);
let storage_writer_handle = tokio::spawn(async move {
if let Err(e) = storage_writer.start().await {
tracing::error!("Storage writer error: {}", e);
}
});
// Start sender task
let sender_token = cancellation_token.clone();
let sender_storage = storage.clone();
let sender_commitment = config.rpc_commitment;
let sender_source_rpc = source_rpc_client.clone();
let sender_common_config = common_config.clone();
let sender_handle = tokio::spawn(async move {
if let Err(e) = sender::run_sender(
&sender_common_config,
sender_commitment,
sender_rx,
storage_tx,
sender_token,
sender_storage,
config.retry_max_attempts,
sender_source_rpc,
)
.await
{
tracing::error!("Sender error: {}", e);
}
});
// Start reconciliation task for escrow operators only.
// Withdraw operators don't maintain escrow ATA balances, so reconciliation is skipped.
let reconciliation_handle = if common_config.program_type == crate::config::ProgramType::Escrow
{
if let Some(reconciliation_escrow) = common_config.escrow_instance_id {
let reconciliation_storage = storage.clone();
let reconciliation_config = config.clone();
let reconciliation_rpc = source_rpc_client
.clone()
.unwrap_or_else(|| rpc_client.clone());
let reconciliation_token = cancellation_token.clone();
tokio::spawn(async move {
if let Err(e) = reconciliation::run_reconciliation(
reconciliation_storage,
reconciliation_config,
reconciliation_rpc,
reconciliation_escrow,
reconciliation_token,
)
.await
{
tracing::error!("Reconciliation error: {}", e);
}
})
} else {
warn!("Skipping reconciliation: escrow_instance_id is not configured");
tokio::spawn(async {})
}
} else {
tokio::spawn(async {})
};
// Start feepayer balance monitor for escrow operators only.
// Monitors SOL balance of the feepayer wallet used for ReleaseFunds transactions.
let feepayer_monitor_handle =
if common_config.program_type == crate::config::ProgramType::Escrow {
let feepayer_config = config.clone();
let feepayer_rpc = source_rpc_client
.clone()
.unwrap_or_else(|| rpc_client.clone());
let feepayer_program_type = common_config.program_type;
let feepayer_token = cancellation_token.clone();
tokio::spawn(async move {
if let Err(e) = feepayer_monitor::run_feepayer_monitor(
feepayer_config,
feepayer_rpc,
feepayer_program_type,
feepayer_token,
)
.await
{
tracing::error!("Feepayer monitor error: {}", e);
}
})
} else {
tokio::spawn(async {})
};
info!("Operator started, waiting for shutdown signal...");
// Wait for shutdown signal
tokio::signal::ctrl_c()
.await
.map_err(|_| OperatorError::ShutdownChannelSend)?;
info!("Shutdown signal received, initiating graceful shutdown...");
// Graceful shutdown
shutdown_operator(
cancellation_token,
storage,
fetcher_handle,
processor_handle,
sender_handle,
storage_writer_handle,
reconciliation_handle,
feepayer_monitor_handle,
config.batch_size,
config.db_poll_interval,
)
.await
.map_err(|_| OperatorError::ShutdownChannelSend)?;
info!("Operator shutdown complete");
Ok(())
}