Pārlūkot izejas kodu

Make required_state own the load-set invariant instead of documenting it

Validation's correctness rests on being handed complete state: a missing
balance key silently defaults to zero and would flip an overdraft
decision. required_state names what the loader must fetch, but that set
and the set validate_and_plan actually reads were two independent walks
of the envelope, kept in agreement by hand ("change one, change the
other") and backstopped only by golden vectors and a release-stripped
debug_assert.

Factor the account-owner and balance-key derivation into one shared
posting_keys walk. required_state layers the snapshot-pinned accounts on
top of it (the only keys the loader needs beyond posting owners), and
validate_and_plan's account check now consumes the same walk. What
validation reads is now structurally what the loader fetched, so the two
cannot drift.

Snapshot-only accounts stay out of the shared walk: they are validated
separately (step 5b) and must not enter the frozen/closed check, or a
transfer that pins a frozen account it never spends from would start
being rejected.
Cesar Rodas 3 mēneši atpakaļ
vecāks
revīzija
defd5f2b25
1 mainītis faili ar 48 papildinājumiem un 28 dzēšanām
  1. 48 28
      crates/kuatia-core/src/validate.rs

+ 48 - 28
crates/kuatia-core/src/validate.rs

@@ -71,37 +71,59 @@ pub struct RequiredState {
     pub balances: Vec<(AccountId, AssetId)>,
 }
 
+/// Accounts and balance keys implied purely by the postings an envelope touches:
+/// owners of its created and consumed postings, plus the `(owner, asset)` balance
+/// key for each. This is the one walk [`required_state`] (what the loader fetches)
+/// and [`validate_and_plan`] (what it reads) both derive from, so the load set and
+/// the read set cannot drift.
+struct PostingKeys {
+    owners: Vec<AccountId>,
+    balances: Vec<(AccountId, AssetId)>,
+}
+
+fn posting_keys(envelope: &Envelope, consumed_postings: &[Posting]) -> PostingKeys {
+    let mut owners: Vec<AccountId> = Vec::new();
+    let mut balances: Vec<(AccountId, AssetId)> = Vec::new();
+    for np in envelope.creates() {
+        owners.push(np.owner);
+        balances.push((np.owner, np.asset));
+    }
+    for p in consumed_postings {
+        owners.push(p.owner);
+        balances.push((p.owner, p.asset));
+    }
+    owners.sort();
+    owners.dedup();
+    balances.sort();
+    balances.dedup();
+    PostingKeys { owners, balances }
+}
+
 /// Derive the state key-set [`validate_and_plan`] will read.
 ///
 /// The consumed postings must be passed in because their owner and asset (which
 /// drive the account and balance key-sets) live in the store, not the envelope.
 /// The loader therefore fetches `envelope.consumes()` first, then calls this to
-/// own every remaining key. Kept in lockstep with what `validate_and_plan`
-/// actually reads: change one, change the other.
+/// own every remaining key. Both this and [`validate_and_plan`] derive their
+/// account and balance keys from one shared `posting_keys` walk, so they cannot
+/// drift; the only
+/// extra keys named here are the snapshot-pinned accounts (validated in step 5b),
+/// which the loader must fetch even when they own no posting.
 pub fn required_state(envelope: &Envelope, consumed_postings: &[Posting]) -> RequiredState {
-    let mut accounts: Vec<AccountId> = envelope.creates().iter().map(|np| np.owner).collect();
-    for p in consumed_postings {
-        accounts.push(p.owner);
-    }
-    for snap in envelope.account_snapshots() {
-        accounts.push(snap.account);
-    }
-    accounts.sort();
-    accounts.dedup();
+    let PostingKeys {
+        mut owners,
+        balances,
+    } = posting_keys(envelope, consumed_postings);
 
-    let mut balances: Vec<(AccountId, AssetId)> = Vec::new();
-    for p in consumed_postings {
-        balances.push((p.owner, p.asset));
-    }
-    for np in envelope.creates() {
-        balances.push((np.owner, np.asset));
+    for snap in envelope.account_snapshots() {
+        owners.push(snap.account);
     }
-    balances.sort();
-    balances.dedup();
+    owners.sort();
+    owners.dedup();
 
     RequiredState {
         consumed_postings: envelope.consumes().to_vec(),
-        accounts,
+        accounts: owners,
         balances,
     }
 }
@@ -312,14 +334,12 @@ pub fn validate_and_plan(input: PlanInput<'_>) -> Result<Plan, ValidationError>
             .ok_or(ValidationError::PostingNotFound(*pid))?;
     }
 
-    // 5. Every referenced account exists, not FROZEN, not CLOSED
-    let mut all_account_ids: Vec<AccountId> = envelope.creates().iter().map(|p| p.owner).collect();
-    for pid in envelope.consumes() {
-        let posting = consumed_by_id[pid];
-        all_account_ids.push(posting.owner);
-    }
-    all_account_ids.sort();
-    all_account_ids.dedup();
+    // 5. Every referenced account exists, not FROZEN, not CLOSED. The account
+    // and balance key-sets come from the same `posting_keys` walk `required_state`
+    // hands the loader, so what validation reads here is structurally what was
+    // fetched. Snapshot-pinned accounts are handled separately in 5b, so this set
+    // deliberately excludes snapshot-only accounts.
+    let all_account_ids = posting_keys(envelope, input.consumed_postings).owners;
 
     for aid in &all_account_ids {
         let account = input