forked from StellarSend/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconciliation.rs
More file actions
408 lines (365 loc) · 15.1 KB
/
Copy pathreconciliation.rs
File metadata and controls
408 lines (365 loc) · 15.1 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use crate::{
error::AppResult,
models::transaction::TransactionStatus,
services::stellar::{HorizonTransaction, StellarService},
};
use chrono::{DateTime, Duration, Utc};
use sqlx::PgPool;
/// How many distinct stuck hashes to resolve per sweep — mirrors
/// `SubscriptionService::run_due_executions`' `KEEPER_BATCH_LIMIT` pattern
/// so a single pass can't run unbounded.
const RECONCILIATION_BATCH_LIMIT: i64 = 50;
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ReconciliationSummary {
pub considered: usize,
pub resolved_completed: usize,
pub resolved_failed: usize,
pub still_unconfirmed: usize,
}
/// Recovers `transactions.status` from the two failure modes
/// `BatchPaymentService::execute_batch` can't fully protect against on its
/// own (#30):
///
/// - a crash between Horizon accepting a submission and our own
/// post-submission `UPDATE` committing, which would otherwise leave the
/// row `pending` forever even though the payment went through; and
/// - a client-side timeout/connection failure on submission that doesn't
/// tell us whether Horizon actually received and processed it, which
/// `execute_batch` marks `submitted_unconfirmed` rather than a
/// false-negative `failed`.
///
/// Both converge the same way: look the transaction's precomputed hash up
/// on Horizon directly and trust *that* over our own uncertain local
/// state, rather than ever guessing.
pub struct ReconciliationService {
pool: PgPool,
}
impl ReconciliationService {
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
/// Finds transaction rows stuck in a non-terminal state with a known
/// hash, older than `stale_after`, and converges each distinct hash's
/// rows to their true on-chain outcome.
pub async fn reconcile_stuck_transactions(
&self,
stellar: &StellarService,
stale_after: Duration,
) -> AppResult<ReconciliationSummary> {
let mut summary = ReconciliationSummary::default();
let cutoff: DateTime<Utc> = Utc::now() - stale_after;
// FOR UPDATE SKIP LOCKED (mirroring
// SubscriptionService::run_due_executions) means an overlapping
// sweep, or a second server instance running the same loop, simply
// skips rows another pass already grabbed instead of racing it.
let mut tx = self.pool.begin().await?;
let stuck_hashes: Vec<String> = sqlx::query_scalar(
r#"
SELECT DISTINCT stellar_tx_hash
FROM transactions
WHERE status IN ('pending', 'submitted_unconfirmed')
AND stellar_tx_hash IS NOT NULL
AND updated_at < $1
ORDER BY stellar_tx_hash
LIMIT $2
FOR UPDATE SKIP LOCKED
"#,
)
.bind(cutoff)
.bind(RECONCILIATION_BATCH_LIMIT)
.fetch_all(&mut *tx)
.await?;
tx.commit().await?;
summary.considered = stuck_hashes.len();
for hash in stuck_hashes {
let horizon_result = stellar.fetch_transaction(&hash).await;
match resolve_from_horizon_result(&horizon_result) {
Some(status) => {
sqlx::query(
r#"
UPDATE transactions
SET status = $2, updated_at = NOW()
WHERE stellar_tx_hash = $1
AND status IN ('pending', 'submitted_unconfirmed')
"#,
)
.bind(&hash)
.bind(&status)
.execute(&self.pool)
.await?;
match status {
TransactionStatus::Completed => summary.resolved_completed += 1,
_ => summary.resolved_failed += 1,
}
}
None => summary.still_unconfirmed += 1,
}
}
Ok(summary)
}
}
/// Pure decision: given Horizon's answer for a stuck hash, decide the
/// terminal status to converge to, or `None` to leave it for the next
/// sweep because the outcome is still genuinely ambiguous.
///
/// `NotFound` means Horizon has never seen this hash — it could still be
/// en route, or the submission never actually reached Horizon at all — so
/// it's deliberately *not* treated as a rejection. Any other error is a
/// transient problem with *this* reconciliation attempt (Horizon outage,
/// network blip), not new information about the transaction, so it's
/// handled identically: try again next sweep.
fn resolve_from_horizon_result(
result: &AppResult<HorizonTransaction>,
) -> Option<TransactionStatus> {
match result {
Ok(tx) if tx.successful => Some(TransactionStatus::Completed),
Ok(_) => Some(TransactionStatus::Failed),
Err(_) => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::AppError;
fn horizon_tx(successful: bool) -> HorizonTransaction {
HorizonTransaction {
id: "abc".into(),
hash: "abc".into(),
ledger: 12345,
created_at: "2024-01-01T00:00:00Z".into(),
source_account: "GSOURCE".into(),
fee_charged: "100".into(),
successful,
}
}
#[test]
fn a_successful_horizon_transaction_resolves_to_completed() {
let result = Ok(horizon_tx(true));
assert_eq!(
resolve_from_horizon_result(&result),
Some(TransactionStatus::Completed)
);
}
#[test]
fn a_confirmed_but_unsuccessful_horizon_transaction_resolves_to_failed() {
let result = Ok(horizon_tx(false));
assert_eq!(
resolve_from_horizon_result(&result),
Some(TransactionStatus::Failed)
);
}
#[test]
fn horizon_never_having_seen_the_hash_stays_unresolved() {
let result: AppResult<HorizonTransaction> =
Err(AppError::NotFound("Stellar transaction 'abc'".into()));
assert_eq!(resolve_from_horizon_result(&result), None);
}
#[test]
fn a_transient_horizon_error_stays_unresolved_rather_than_guessing() {
let result: AppResult<HorizonTransaction> =
Err(AppError::HorizonError("503 Service Unavailable".into()));
assert_eq!(resolve_from_horizon_result(&result), None);
}
}
/// End-to-end coverage requiring a real Postgres (`DATABASE_URL`) — this
/// crate is bin-only (no `src/lib.rs`), so these live here as `#[ignore]`d
/// `#[tokio::test]`s rather than in `tests/`, which would have no access to
/// internal types. Not runnable in this sandbox (no local Postgres); run
/// with `cargo test -- --ignored` against a real database. Covers exactly
/// the two scenarios from #30's "Testing strategy":
///
/// 1. a batch crashes between Horizon accepting the submission and our
/// `UPDATE` committing — simulated by inserting a row already in that
/// exact stuck state rather than literally killing the process — and a
/// reconciliation pass resolves it to `completed`.
/// 2. a submission that never got a definitive answer from Horizon is
/// marked `submitted_unconfirmed`, not `failed`, and a retry with the
/// identical `signed_xdr` is rejected rather than risking a double
/// payment.
#[cfg(test)]
mod db_tests {
use super::*;
use crate::{
error::AppError,
models::batch_payment::{BatchPaymentLeg, SendBatchPaymentRequest},
services::{batch::BatchPaymentService, tx_hash::compute_transaction_hash},
};
use sqlx::postgres::PgPoolOptions;
use uuid::Uuid;
use wiremock::{
matchers::{method, path},
Mock, MockServer, ResponseTemplate,
};
const NETWORK: &str = "Test SDF Network ; September 2015";
async fn test_pool() -> PgPool {
let database_url = std::env::var("DATABASE_URL")
.expect("DATABASE_URL must be set to run this integration test");
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&database_url)
.await
.expect("failed to connect to DATABASE_URL");
sqlx::migrate!("./migrations")
.run(&pool)
.await
.expect("failed to run migrations");
pool
}
async fn cleanup(pool: &PgPool, batch_id: Uuid, hash: &str) {
let _ = sqlx::query("DELETE FROM transactions WHERE batch_id = $1")
.bind(batch_id)
.execute(pool)
.await;
let _ = sqlx::query("DELETE FROM batch_submissions WHERE stellar_tx_hash = $1")
.bind(hash)
.execute(pool)
.await;
}
/// A syntactically valid, minimal signed_xdr — real enough to parse and
/// hash, not real enough (or meant) to ever be submitted to a live
/// network. Mirrors `tx_hash::tests::sample_transaction`.
fn sample_signed_xdr() -> String {
use stellar_xdr::curr::{
Limits, Memo, MuxedAccount, Operation, OperationBody, PaymentOp, Preconditions,
SequenceNumber, Transaction, TransactionEnvelope, TransactionExt,
TransactionV1Envelope, Uint256, VecM, WriteXdr,
};
const SOURCE: &str = "GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
let source_id = stellar_strkey::ed25519::PublicKey::from_string(SOURCE)
.expect("valid test source account");
let tx = Transaction {
source_account: MuxedAccount::Ed25519(Uint256(source_id.0)),
fee: 100,
seq_num: SequenceNumber(1),
cond: Preconditions::None,
memo: Memo::None,
operations: vec![Operation {
source_account: None,
body: OperationBody::Payment(PaymentOp {
destination: MuxedAccount::Ed25519(Uint256(source_id.0)),
asset: stellar_xdr::curr::Asset::Native,
amount: 100_000_000,
}),
}]
.try_into()
.expect("single operation fits VecM"),
ext: TransactionExt::V0,
};
let envelope = TransactionEnvelope::Tx(TransactionV1Envelope {
tx,
signatures: VecM::default(),
});
envelope
.to_xdr_base64(Limits::none())
.expect("encoding should succeed")
}
#[tokio::test]
#[ignore]
async fn reconciliation_resolves_a_crashed_pending_row_to_completed() {
let pool = test_pool().await;
let user_id = Uuid::new_v4();
let batch_id = Uuid::new_v4();
let hash = format!("{:064x}", Uuid::new_v4().as_u128());
// Simulates the crash the issue describes: a row already sitting in
// 'pending' with its hash recorded, updated_at old enough to count
// as stuck, exactly as `execute_batch` would leave it if the
// process died right after the leg-insert transaction committed
// but before the post-submission UPDATE ever ran.
sqlx::query(
r#"
INSERT INTO transactions (
id, user_id, from_asset, to_asset, send_amount, receive_amount,
source_account, destination_account, status, stellar_tx_hash,
batch_id, batch_index, updated_at
)
VALUES ($1, $2, 'XLM', 'XLM', '10', NULL, 'GSOURCE', 'GDEST', 'pending', $3, $4, 0, NOW() - INTERVAL '5 minutes')
"#,
)
.bind(Uuid::new_v4())
.bind(user_id)
.bind(&hash)
.bind(batch_id)
.execute(&pool)
.await
.expect("seed insert should succeed");
let mock_horizon = MockServer::start().await;
Mock::given(method("GET"))
.and(path(format!("/transactions/{hash}")))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"id": hash,
"hash": hash,
"ledger": 100,
"created_at": "2024-01-01T00:00:00Z",
"source_account": "GSOURCE",
"fee_charged": "100",
"successful": true,
})))
.mount(&mock_horizon)
.await;
let stellar = StellarService::new(mock_horizon.uri());
let reconciliation = ReconciliationService::new(pool.clone());
let summary = reconciliation
.reconcile_stuck_transactions(&stellar, Duration::seconds(60))
.await
.expect("reconciliation pass should succeed");
assert_eq!(summary.resolved_completed, 1);
let status: TransactionStatus =
sqlx::query_scalar("SELECT status FROM transactions WHERE batch_id = $1")
.bind(batch_id)
.fetch_one(&pool)
.await
.expect("row should still exist");
assert_eq!(status, TransactionStatus::Completed);
cleanup(&pool, batch_id, &hash).await;
}
#[tokio::test]
#[ignore]
async fn ambiguous_submission_is_unconfirmed_not_failed_and_blocks_a_retry() {
let pool = test_pool().await;
let user_id = Uuid::new_v4();
// An address nothing listens on: submit_transaction fails at the
// connection level (AppError::HttpClient), the same class of error
// a client-side timeout produces — Horizon never gives us a
// definitive answer either way.
let stellar = StellarService::new("http://127.0.0.1:1");
let batch_svc = BatchPaymentService::new(pool.clone());
let req = SendBatchPaymentRequest {
source_account: "GSOURCE".into(),
signed_xdr: sample_signed_xdr(),
legs: vec![BatchPaymentLeg {
destination: "GDEST".into(),
amount: "10".into(),
asset_code: "XLM".into(),
asset_issuer: None,
}],
};
let hash = compute_transaction_hash(&req.signed_xdr, NETWORK)
.expect("fixture xdr should at least parse for this test's purposes");
let first = batch_svc
.execute_batch(user_id, &stellar, NETWORK, &req)
.await;
assert!(matches!(first, Err(AppError::HttpClient(_))));
let status: TransactionStatus =
sqlx::query_scalar("SELECT status FROM transactions WHERE stellar_tx_hash = $1")
.bind(&hash)
.fetch_one(&pool)
.await
.expect("leg row should have been inserted before submission");
assert_eq!(status, TransactionStatus::SubmittedUnconfirmed);
// Retrying the identical signed_xdr must not be allowed to execute
// a second time while the first attempt's outcome is still
// unknown — this is the "never allow a plain retry of a batch
// whose xdr may have already landed" requirement.
let retry = batch_svc
.execute_batch(user_id, &stellar, NETWORK, &req)
.await;
assert!(matches!(retry, Err(AppError::Conflict(_))));
let batch_id: Uuid =
sqlx::query_scalar("SELECT batch_id FROM transactions WHERE stellar_tx_hash = $1")
.bind(&hash)
.fetch_one(&pool)
.await
.unwrap();
cleanup(&pool, batch_id, &hash).await;
}
}