Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkgs/id/src/currency_poc/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,19 @@ impl<B: LedgerBackend, S: IntentStore> ConversionExecutor<B, S> {
.put(record)
.context("intent state persist (Submitted) failed")?;
let results = self.backend.create_transfers(&record.legs);
// #4669: the trait contract is one result per leg. zip()
// stops at the shorter side, so a short vector would leave
// legs unexamined and still advance to Posted — refuse here,
// before any per-leg verdict, and stay Submitted for the
// recovery scanner.
if results.len() != record.legs.len() {
bail!(
"conversion {}: backend returned {} results for {} legs \u{2014} refusing to mark Posted on a short result vector",
record.conversion_id,
results.len(),
record.legs.len()
);
}
for (leg, result) in record.legs.iter().zip(results.iter()) {
match result {
Ok(()) | Err(TransferError::Exists) => {}
Expand Down
5 changes: 4 additions & 1 deletion pkgs/id/src/currency_poc/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ pub enum TransferError {
/// The ledger operations the conversion executor needs.
pub trait LedgerBackend {
fn create_accounts(&mut self, accounts: &[Account]) -> Vec<Result<(), AccountError>>;
/// Submit a batch. Contiguous runs ending at a transfer with
/// Submit a batch. The returned vector carries exactly one
/// result per submitted transfer, in submission order — callers
/// treat any other length as a backend failure, never as
/// success (#4669). Contiguous runs ending at a transfer with
/// `linked == false` form linked chains and post atomically.
///
/// A batch whose final chain is unterminated — its last transfer
Expand Down
12 changes: 8 additions & 4 deletions pkgs/id/src/currency_poc/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ pub fn reconcile<B: LedgerBackend, S: IntentStore>(
match policy {
RecoveryPolicy::Resubmit => {
let results = backend.create_transfers(&record.legs);
let failed = results
.iter()
.find(|result| !matches!(result, Ok(()) | Err(TransferError::Exists)));
if failed.is_some() {
// #4669: a short result vector is a failed
// resubmission, not a vacuous success — require
// one result per leg before scanning for failures.
let failed = results.len() != record.legs.len()
|| results
.iter()
.any(|result| !matches!(result, Ok(()) | Err(TransferError::Exists)));
if failed {
// One rejected resubmission must not abort
// recovery of every later record: leave this
// record unresolved, report it, and continue.
Expand Down
107 changes: 105 additions & 2 deletions pkgs/id/src/currency_poc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use super::executor::{ConversionExecutor, ConversionRequest, RateQuote};
use super::intent::{FileIntentStore, InMemoryIntentStore, IntentRecord, IntentState, IntentStore};
use super::ledger::{
Account, AccountError, AccountFlags, AccountId, InMemoryLedger, LedgerBackend, Transfer,
TransferError,
Account, AccountBalance, AccountError, AccountFlags, AccountId, InMemoryLedger, LedgerBackend,
Transfer, TransferError, TransferId,
};
use super::recovery::{ReconcileOutcome, RecoveryPolicy, reconcile};

Expand Down Expand Up @@ -1852,3 +1852,106 @@ fn execute_retry_on_posted_record_fails_explicitly() {
"the persisted record stays Posted after the retry"
);
}

/// A backend violating the `create_transfers` result contract
/// (#4669): it reports `keep` `Ok` results and posts nothing, no
/// matter how many transfers were submitted. Everything else
/// delegates to a funded in-memory ledger, so lookups honestly
/// report that no legs are present.
struct ShortResultBackend {
inner: InMemoryLedger,
keep: usize,
}

impl LedgerBackend for ShortResultBackend {
fn create_accounts(&mut self, accounts: &[Account]) -> Vec<Result<(), AccountError>> {
self.inner.create_accounts(accounts)
}

fn create_transfers(&mut self, _batch: &[Transfer]) -> Vec<Result<(), TransferError>> {
(0..self.keep).map(|_| Ok(())).collect()
}

fn lookup_transfer(&self, id: TransferId) -> Option<Transfer> {
self.inner.lookup_transfer(id)
}

fn lookup_account(&self, id: AccountId) -> Option<Account> {
self.inner.lookup_account(id)
}

fn account_balance(&self, id: AccountId) -> Option<AccountBalance> {
self.inner.account_balance(id)
}

fn transfers_with_user_data_128(&self, value: u128) -> Vec<Transfer> {
self.inner.transfers_with_user_data_128(value)
}
}

fn short_result_executor(
keep: usize,
) -> ConversionExecutor<ShortResultBackend, InMemoryIntentStore> {
ConversionExecutor::new(
ShortResultBackend {
inner: funded_ledger(),
keep,
},
InMemoryIntentStore::new(),
)
}

#[test]
fn a_short_create_transfers_result_never_marks_posted() {
// #4669: zip() stops at the shorter side, so a backend returning
// fewer results than legs used to sail through to Posted with
// nothing (or only part) in the ledger. Any length but one result
// per leg is a backend failure: submit errors and the record
// stays Submitted for the recovery scanner.
for keep in [0, 1, 2] {
let mut executor = short_result_executor(keep);
let error = executor
.execute(&request())
.expect_err("a short result vector must fail the submission");
assert!(
error.to_string().contains("results for 3 legs"),
"the error should name the short result vector: {error:#}"
);
assert_eq!(
executor.store.get(777).unwrap().unwrap().state,
IntentState::Submitted,
"keep={keep}: the record must not advance to Posted"
);
}
}

#[test]
fn recovery_resubmit_rejects_a_short_create_transfers_result() {
// #4669: the Resubmit path scanned the result vector for a
// failure entry, so an empty (or short) vector read as vacuous
// success and the record was persisted Posted. A short vector is
// a rejected resubmission: the record stays unresolved for a
// later scan.
for keep in [0, 2] {
let mut executor = short_result_executor(keep);
let record = executor.prepare(&request()).expect("intent prepared");
assert_eq!(record.state, IntentState::Recorded);

let outcomes = reconcile(
&mut executor.backend,
&mut executor.store,
RecoveryPolicy::Resubmit,
)
.expect("reconcile");
assert_eq!(
outcomes,
vec![(777, ReconcileOutcome::ResubmissionRejected)],
"keep={keep}"
);
assert_eq!(
executor.store.get(777).unwrap().unwrap().state,
IntentState::Recorded,
"keep={keep}: the record must stay unresolved, not Posted"
);
}
}
Loading