miden-node-store 0.17.0-rc.4

Miden node's state store component
Documentation
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
//!
//! Tests for delta update functionality.

use std::collections::{BTreeMap, BTreeSet};

use assert_matches::assert_matches;
use miden_node_utils::fee::{test_fee_params, test_protocol_config};
use miden_protocol::account::auth::{AuthScheme, PublicKeyCommitment};
use miden_protocol::account::component::AccountComponentMetadata;
use miden_protocol::account::{
    Account,
    AccountBuilder,
    AccountCodePatch,
    AccountComponent,
    AccountHeader,
    AccountId,
    AccountIdVersion,
    AccountPatch,
    AccountStorageHeader,
    AccountStoragePatch,
    AccountType,
    AccountUpdateDetails,
    AccountVaultPatch,
    AssetCallbackFlag,
    StorageMap,
    StorageMapKey,
    StorageMapPatch,
    StorageSlot,
    StorageSlotName,
    StorageSlotPatch,
    StorageValuePatch,
};
use miden_protocol::asset::{Asset, FungibleAsset};
use miden_protocol::block::{
    BlockAccountUpdate,
    BlockHeader,
    BlockNumber,
    BlockSignatures,
    ValidatorConfig,
};
use miden_protocol::crypto::dsa::ecdsa_k256_keccak::SigningKey;
use miden_protocol::testing::account_id::{
    ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET,
    ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_1,
};
use miden_protocol::{EMPTY_WORD, Felt, Word};
use miden_standards::account::auth::{Approver, AuthSingleSig};
use miden_standards::code_builder::CodeBuilder;

use crate::db::queries::{
    self,
    PrecomputedPublicAccountState,
    PrecomputedPublicAccountStates,
    VALID_FOREVER,
};
use crate::db::{Result, TestDb};
use crate::errors::DatabaseError;

fn block_account_update(
    account_id: AccountId,
    final_state_commitment: Word,
    details: AccountUpdateDetails,
) -> BlockAccountUpdate {
    BlockAccountUpdate::new(account_id, final_state_commitment, details)
        .expect("test account update should be valid")
}

// QUERY DRIVERS
// ================================================================================================
//
// Each driver runs one query function on the test database, so a test body reads the same as the
// production call site with the transaction handle replaced by the test handle.

fn upsert_accounts(
    db: &TestDb,
    accounts: &[BlockAccountUpdate],
    block_num: BlockNumber,
    precomputed_public_states: &PrecomputedPublicAccountStates,
    new_account_ids: &BTreeSet<AccountId>,
) -> Result<usize> {
    let accounts = accounts.to_vec();
    let precomputed_public_states = precomputed_public_states.clone();
    let new_account_ids = new_account_ids.clone();
    db.write(move |tx| {
        queries::upsert_accounts(
            tx,
            &accounts,
            block_num,
            &precomputed_public_states,
            &new_account_ids,
        )
    })
}

fn select_full_account(db: &TestDb, account_id: AccountId) -> Result<Account> {
    db.read(move |tx| queries::select_full_account(tx, account_id))
}

fn select_vault_at_block(
    db: &TestDb,
    account_id: AccountId,
    block_num: BlockNumber,
) -> Result<Vec<Asset>> {
    db.read(move |tx| queries::select_vault_at_block(tx, account_id, block_num))
}

fn select_account_header_with_storage_header_at_block(
    db: &TestDb,
    account_id: AccountId,
    block_num: BlockNumber,
) -> Result<Option<(AccountHeader, AccountStorageHeader)>> {
    db.read(move |tx| {
        queries::select_account_header_with_storage_header_at_block(tx, account_id, block_num)
    })
}

fn insert_block_header(db: &TestDb, block_num: BlockNumber) {
    let secret_key = SigningKey::new();
    let block_header = BlockHeader::new(
        Word::default(),
        block_num,
        Word::default(),
        Word::default(),
        Word::default(),
        Word::default(),
        Word::default(),
        ValidatorConfig::new(vec![secret_key.public_key()], 1).unwrap(),
        test_fee_params(),
        test_protocol_config().to_commitment(),
        None,
        0,
    );
    let signatures =
        BlockSignatures::new(vec![secret_key.sign(block_header.commitment())]).unwrap();

    db.write::<_, DatabaseError, _>(move |tx| {
        queries::insert_block_header(tx, &block_header, &signatures)
    })
    .expect("Failed to insert block header");
}

/// Returns the current `accounts` row's commitment, nonce, and code commitment.
fn latest_account_row(db: &TestDb, account_id: AccountId) -> (Word, Option<Felt>, Option<Word>) {
    const SQL: &str = "SELECT account_commitment, nonce, code_commitment \
                       FROM accounts WHERE account_id = ?1 AND valid_until = ?2";

    db.read::<_, DatabaseError, _>(move |tx| {
        Ok(tx
            .query(SQL, &[&account_id, &VALID_FOREVER], |row| {
                Ok((row.get::<Word>(0)?, row.get::<Option<Felt>>(1)?, row.get::<Option<Word>>(2)?))
            })?
            .into_iter()
            .next())
    })
    .expect("query should succeed")
    .expect("Account should exist in DB")
}

fn precomputed_state_from_account(account: &Account) -> PrecomputedPublicAccountState {
    PrecomputedPublicAccountState {
        vault_root: account.vault().root(),
        storage_map_roots: account
            .storage()
            .slots()
            .iter()
            .filter_map(|slot| match slot.content() {
                miden_protocol::account::StorageSlotContent::Map(map) => {
                    Some((slot.name().clone(), map.root()))
                },
                miden_protocol::account::StorageSlotContent::Value(_) => None,
            })
            .collect(),
    }
}

fn precomputed_states_from_account(account: &Account) -> PrecomputedPublicAccountStates {
    [(account.id(), precomputed_state_from_account(account))]
        .into_iter()
        .collect::<PrecomputedPublicAccountStates>()
}

fn callback_enabled_faucet_id() -> AccountId {
    AccountId::dummy(
        [41u8; 15],
        AccountIdVersion::Version1,
        AccountType::Public,
        AssetCallbackFlag::Enabled,
    )
}

fn callback_delta_test_account(seed: [u8; 32], slot_index: usize) -> Account {
    let component_storage =
        vec![StorageSlot::with_value(StorageSlotName::mock(slot_index), EMPTY_WORD)];
    let account_component_code = CodeBuilder::default()
        .compile_component_code("test::interface", "@account_procedure pub proc vault push.1 end")
        .unwrap();
    let component = AccountComponent::new(
        account_component_code,
        component_storage,
        AccountComponentMetadata::new("test"),
    )
    .unwrap();

    AccountBuilder::new(seed)
        .account_type(AccountType::Public)
        .with_component(component)
        .with_component(AuthSingleSig::new(Approver::new(
            PublicKeyCommitment::from(EMPTY_WORD),
            AuthScheme::Falcon512Poseidon2,
        )))
        .build_existing()
        .unwrap()
}

fn insert_public_account(db: &TestDb, block_num: BlockNumber, account: &Account) {
    let patch_initial = AccountPatch::try_from(account.clone()).unwrap();
    upsert_accounts(
        db,
        &[block_account_update(
            account.id(),
            account.to_commitment(),
            AccountUpdateDetails::Public(patch_initial),
        )],
        block_num,
        &precomputed_states_from_account(account),
        &BTreeSet::from([account.id()]),
    )
    .expect("initial upsert failed");
}

fn apply_callback_delta(
    db: &TestDb,
    account_id: AccountId,
    faucet_id: AccountId,
    block: BlockNumber,
    amount: u64,
    nonce_delta: u64,
) -> Account {
    let prev = select_full_account(db, account_id).expect("load account");
    let callback_template = FungibleAsset::new(faucet_id, amount).unwrap();
    let prev_amount = prev
        .vault()
        .get(callback_template.id())
        .and_then(|asset| asset.as_fungible())
        .map_or(0, |asset| asset.amount().as_u64());

    let absolute_asset = Asset::from(FungibleAsset::new(faucet_id, prev_amount + amount).unwrap());
    let mut vault_patch = AccountVaultPatch::default();
    vault_patch.insert_asset(absolute_asset);
    let final_nonce = Felt::new_unchecked(prev.nonce().as_canonical_u64() + nonce_delta);
    let patch = AccountPatch::new(
        account_id,
        AccountStoragePatch::new(),
        vault_patch,
        AccountCodePatch::default(),
        Some(final_nonce),
    )
    .unwrap();

    let mut expected = prev.clone();
    expected.apply_patch(&patch).unwrap();
    let precomputed_public_states = precomputed_states_from_account(&expected);

    upsert_accounts(
        db,
        &[block_account_update(
            account_id,
            expected.to_commitment(),
            AccountUpdateDetails::Public(patch),
        )],
        block,
        &precomputed_public_states,
        &BTreeSet::new(),
    )
    .expect("partial delta upsert failed");

    let after = select_full_account(db, account_id).expect("load account after");
    assert_eq!(after.vault().root(), expected.vault().root(), "vault root mismatch");
    assert_eq!(after.to_commitment(), expected.to_commitment(), "commitment mismatch");
    after
}

/// Tests that the optimized delta update path produces the same results as the old
/// method that loads the full account.
///
/// Covers partial deltas that update:
/// - Nonce (via `nonce_delta`)
/// - Value storage slots
/// - Vault assets (fungible) starting from empty vault
///
/// The test ensures the optimized code path in `upsert_accounts` produces correct results
/// by comparing the final account state against a manually constructed expected state.
#[test]
#[expect(
    clippy::too_many_lines,
    reason = "test exercises multiple storage and vault paths"
)]
fn optimized_delta_matches_full_account_method() {
    // Use deterministic account seed to keep account IDs stable.
    const ACCOUNT_SEED: [u8; 32] = [10u8; 32];
    // Use fixed block numbers to ensure deterministic ordering.
    const BLOCK_NUM_1: u32 = 1;
    const BLOCK_NUM_2: u32 = 2;
    // Use explicit slot indices to avoid magic numbers.
    const SLOT_INDEX_PRIMARY: usize = 0;
    const SLOT_INDEX_SECONDARY: usize = 1;
    // Use fixed values to verify storage delta updates.
    const INITIAL_SLOT_VALUES: [u64; 4] = [100, 200, 300, 400];
    const UPDATED_SLOT_VALUES: [u64; 4] = [111, 222, 333, 444];
    // Use fixed delta values to validate nonce and vault changes.
    const NONCE_DELTA: u64 = 5;
    const VAULT_AMOUNT: u64 = 500;

    let db = TestDb::new();

    // Create an account with value slots only (no map slots to avoid SmtForest complexity)
    let slot_value_initial = Word::from([
        Felt::new_unchecked(INITIAL_SLOT_VALUES[0]),
        Felt::new_unchecked(INITIAL_SLOT_VALUES[1]),
        Felt::new_unchecked(INITIAL_SLOT_VALUES[2]),
        Felt::new_unchecked(INITIAL_SLOT_VALUES[3]),
    ]);

    let component_storage = vec![
        StorageSlot::with_value(StorageSlotName::mock(SLOT_INDEX_PRIMARY), slot_value_initial),
        StorageSlot::with_value(StorageSlotName::mock(SLOT_INDEX_SECONDARY), EMPTY_WORD),
    ];

    let account_component_code = CodeBuilder::default()
        .compile_component_code("test::interface", "@account_procedure pub proc foo push.1 end")
        .unwrap();

    let component = AccountComponent::new(
        account_component_code,
        component_storage,
        AccountComponentMetadata::new("test"),
    )
    .unwrap();

    let account = AccountBuilder::new(ACCOUNT_SEED)
        .account_type(AccountType::Public)
        .with_component(component)
        .with_component(AuthSingleSig::new(Approver::new(
            PublicKeyCommitment::from(EMPTY_WORD),
            AuthScheme::Falcon512Poseidon2,
        )))
        .build_existing()
        .unwrap();

    let block_1 = BlockNumber::from(BLOCK_NUM_1);
    let block_2 = BlockNumber::from(BLOCK_NUM_2);
    insert_block_header(&db, block_1);
    insert_block_header(&db, block_2);

    // Insert the initial account at block 1 (full state) - no vault assets
    let patch_initial = AccountPatch::try_from(account.clone()).unwrap();
    let account_update_initial = block_account_update(
        account.id(),
        account.to_commitment(),
        AccountUpdateDetails::Public(patch_initial),
    );
    upsert_accounts(
        &db,
        &[account_update_initial],
        block_1,
        &precomputed_states_from_account(&account),
        &BTreeSet::from([account.id()]),
    )
    .expect("Initial upsert failed");

    // Verify initial state
    let full_account_before =
        select_full_account(&db, account.id()).expect("Failed to load full account");
    assert_eq!(full_account_before.nonce(), account.nonce());
    assert!(
        full_account_before.vault().assets().next().is_none(),
        "Vault should be empty initially"
    );

    // Create a partial delta to apply:
    // - Increment nonce by 5
    // - Update the first value slot
    // - Add 500 tokens to the vault (starting from empty)

    let new_slot_value = Word::from([
        Felt::new_unchecked(UPDATED_SLOT_VALUES[0]),
        Felt::new_unchecked(UPDATED_SLOT_VALUES[1]),
        Felt::new_unchecked(UPDATED_SLOT_VALUES[2]),
        Felt::new_unchecked(UPDATED_SLOT_VALUES[3]),
    ]);
    let faucet_id = AccountId::try_from(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET).unwrap();

    // Find the slot name from the account's storage
    let value_slot_name =
        full_account_before.storage().slots().iter().next().unwrap().name().clone();

    // Build the storage delta (value slot update only)
    let storage_patch = {
        let deltas = [(
            value_slot_name.clone(),
            StorageSlotPatch::Value(StorageValuePatch::Update { value: new_slot_value }),
        )]
        .into_iter()
        .collect::<BTreeMap<_, _>>();
        AccountStoragePatch::from_raw(deltas).unwrap()
    };

    // Build the vault patch (the absolute end-state is 500 tokens, starting from an empty vault)
    let vault_patch = {
        let asset = Asset::from(FungibleAsset::new(faucet_id, VAULT_AMOUNT).unwrap());
        AccountVaultPatch::with_assets([asset])
    };

    // Create a partial patch
    let nonce_delta = Felt::new_unchecked(NONCE_DELTA);
    let expected_nonce = Felt::new_unchecked(
        full_account_before.nonce().as_canonical_u64() + nonce_delta.as_canonical_u64(),
    );
    let partial_patch = AccountPatch::new(
        full_account_before.id(),
        storage_patch,
        vault_patch,
        AccountCodePatch::default(),
        Some(expected_nonce),
    )
    .unwrap();

    // Construct the expected final account by applying the patch
    let expected_code_commitment = full_account_before.code().commitment();

    let mut expected_account = full_account_before.clone();
    expected_account.apply_patch(&partial_patch).unwrap();
    let final_account_for_commitment = expected_account;

    let final_commitment = final_account_for_commitment.to_commitment();
    let expected_storage_commitment = final_account_for_commitment.storage().to_commitment();
    let expected_vault_root = final_account_for_commitment.vault().root();
    let precomputed_public_states = precomputed_states_from_account(&final_account_for_commitment);

    // ----- Apply the partial patch via upsert_accounts (optimized path) -----
    let account_update = block_account_update(
        account.id(),
        final_commitment,
        AccountUpdateDetails::Public(partial_patch),
    );
    upsert_accounts(&db, &[account_update], block_2, &precomputed_public_states, &BTreeSet::new())
        .expect("Partial delta upsert failed");

    // ----- VERIFY: Query the DB and check that optimized path produced correct results -----

    let (header_after, storage_header_after) =
        select_account_header_with_storage_header_at_block(&db, account.id(), block_2)
            .expect("Query should succeed")
            .expect("Account should exist");

    // Verify nonce
    assert_eq!(
        header_after.nonce(),
        expected_nonce,
        "Nonce mismatch: optimized={:?}, expected={:?}",
        header_after.nonce(),
        expected_nonce
    );

    // Verify code commitment (should be unchanged)
    assert_eq!(
        header_after.code_commitment(),
        expected_code_commitment,
        "Code commitment mismatch"
    );

    // Verify storage header commitment
    assert_eq!(
        storage_header_after.to_commitment(),
        expected_storage_commitment,
        "Storage header commitment mismatch"
    );

    // Verify vault assets
    let vault_assets_after =
        select_vault_at_block(&db, account.id(), block_2).expect("Query vault should succeed");

    assert_eq!(vault_assets_after.len(), 1, "Should have 1 vault asset");
    let fungible_asset = vault_assets_after[0].unwrap_fungible();
    assert_eq!(fungible_asset.faucet_id(), faucet_id, "Faucet ID should match");
    assert_eq!(fungible_asset.amount().as_u64(), VAULT_AMOUNT, "Amount should be 500");

    // Verify the account commitment matches
    assert_eq!(
        header_after.to_commitment(),
        final_commitment,
        "Account commitment should match the expected final state"
    );

    // Also verify we can load the full account and it has correct state
    let full_account_after =
        select_full_account(&db, account.id()).expect("Failed to load full account after update");

    assert_eq!(full_account_after.nonce(), expected_nonce, "Full account nonce mismatch");
    assert_eq!(
        full_account_after.storage().to_commitment(),
        expected_storage_commitment,
        "Full account storage commitment mismatch"
    );
    assert_eq!(
        full_account_after.vault().root(),
        expected_vault_root,
        "Full account vault root mismatch"
    );
}

#[test]
#[expect(
    clippy::too_many_lines,
    reason = "test exercises vault deltas across multiple blocks"
)]
fn optimized_delta_updates_non_empty_vault() {
    const ACCOUNT_SEED: [u8; 32] = [40u8; 32];
    const BLOCK_NUM_1: u32 = 1;
    const BLOCK_NUM_2: u32 = 2;
    const BLOCK_NUM_3: u32 = 3;
    const NONCE_DELTA: u64 = 1;
    const INITIAL_AMOUNT: u64 = 700;
    const ADDED_AMOUNT_BLOCK_2: u64 = 250;
    const ADDED_AMOUNT_BLOCK_3: u64 = 150;
    const SLOT_INDEX: usize = 0;

    let db = TestDb::new();

    let faucet_id = AccountId::try_from(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET).unwrap();
    let faucet_id_1 = AccountId::try_from(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_1).unwrap();
    let initial_asset = Asset::from(FungibleAsset::new(faucet_id, INITIAL_AMOUNT).unwrap());

    let component_storage =
        vec![StorageSlot::with_value(StorageSlotName::mock(SLOT_INDEX), EMPTY_WORD)];

    let account_component_code = CodeBuilder::default()
        .compile_component_code("test::interface", "@account_procedure pub proc vault push.1 end")
        .unwrap();

    let component = AccountComponent::new(
        account_component_code,
        component_storage,
        AccountComponentMetadata::new("test"),
    )
    .unwrap();

    let account = AccountBuilder::new(ACCOUNT_SEED)
        .account_type(AccountType::Public)
        .with_component(component)
        .with_component(AuthSingleSig::new(Approver::new(
            PublicKeyCommitment::from(EMPTY_WORD),
            AuthScheme::Falcon512Poseidon2,
        )))
        .with_assets([initial_asset])
        .build_existing()
        .unwrap();

    let block_1 = BlockNumber::from(BLOCK_NUM_1);
    let block_2 = BlockNumber::from(BLOCK_NUM_2);
    let block_3 = BlockNumber::from(BLOCK_NUM_3);
    insert_block_header(&db, block_1);
    insert_block_header(&db, block_2);
    insert_block_header(&db, block_3);

    // Block 1: insert full-state patch (initial account with 700 tokens of faucet_id)
    let patch_initial = AccountPatch::try_from(account.clone()).unwrap();
    let account_update_initial = block_account_update(
        account.id(),
        account.to_commitment(),
        AccountUpdateDetails::Public(patch_initial),
    );
    upsert_accounts(
        &db,
        &[account_update_initial],
        block_1,
        &precomputed_states_from_account(&account),
        &BTreeSet::from([account.id()]),
    )
    .expect("Initial upsert failed");

    let full_account_before =
        select_full_account(&db, account.id()).expect("Failed to load full account");

    // Block 2: partial patch — remove faucet_id (700), add faucet_id_1 (250)
    let removed_asset = Asset::from(FungibleAsset::new(faucet_id, INITIAL_AMOUNT).unwrap());
    let added_asset = Asset::from(FungibleAsset::new(faucet_id_1, ADDED_AMOUNT_BLOCK_2).unwrap());
    let mut vault_patch = AccountVaultPatch::default();
    vault_patch.insert_asset(added_asset);
    vault_patch.remove_asset(removed_asset.id());

    let final_nonce =
        Felt::new_unchecked(full_account_before.nonce().as_canonical_u64() + NONCE_DELTA);
    let partial_patch = AccountPatch::new(
        account.id(),
        AccountStoragePatch::new(),
        vault_patch,
        AccountCodePatch::default(),
        Some(final_nonce),
    )
    .unwrap();

    let mut expected_account = full_account_before.clone();
    expected_account.apply_patch(&partial_patch).unwrap();
    let expected_commitment = expected_account.to_commitment();
    let expected_vault_root = expected_account.vault().root();
    let precomputed_public_states = precomputed_states_from_account(&expected_account);

    let account_update = block_account_update(
        account.id(),
        expected_commitment,
        AccountUpdateDetails::Public(partial_patch),
    );
    upsert_accounts(&db, &[account_update], block_2, &precomputed_public_states, &BTreeSet::new())
        .expect("Partial delta upsert failed");

    let vault_assets_after =
        select_vault_at_block(&db, account.id(), block_2).expect("Query vault should succeed");

    assert_eq!(vault_assets_after.len(), 1, "Should have 1 vault asset");
    let fungible_asset = vault_assets_after[0].unwrap_fungible();
    assert_eq!(fungible_asset.faucet_id(), faucet_id_1, "Faucet ID should match");
    assert_eq!(fungible_asset.amount().as_u64(), ADDED_AMOUNT_BLOCK_2, "Amount should match");

    let full_account_after =
        select_full_account(&db, account.id()).expect("Failed to load full account after update");

    assert_eq!(full_account_after.vault().root(), expected_vault_root);
    assert_eq!(full_account_after.to_commitment(), expected_commitment);

    // Block 3: partial patch — add more of faucet_id_1 (150 more, total = 400)
    let mut vault_patch_3 = AccountVaultPatch::default();
    vault_patch_3.insert_asset(Asset::from(
        FungibleAsset::new(faucet_id_1, ADDED_AMOUNT_BLOCK_2 + ADDED_AMOUNT_BLOCK_3).unwrap(),
    ));

    let final_nonce_3 =
        Felt::new_unchecked(full_account_after.nonce().as_canonical_u64() + NONCE_DELTA);
    let partial_patch_3 = AccountPatch::new(
        account.id(),
        AccountStoragePatch::new(),
        vault_patch_3,
        AccountCodePatch::default(),
        Some(final_nonce_3),
    )
    .unwrap();

    let mut expected_after_3 = full_account_after.clone();
    expected_after_3.apply_patch(&partial_patch_3).unwrap();
    let commitment_3 = expected_after_3.to_commitment();
    let expected_vault_root_3 = expected_after_3.vault().root();
    let precomputed_public_states_3 = precomputed_states_from_account(&expected_after_3);

    let account_update_3 = block_account_update(
        account.id(),
        commitment_3,
        AccountUpdateDetails::Public(partial_patch_3),
    );
    upsert_accounts(
        &db,
        &[account_update_3],
        block_3,
        &precomputed_public_states_3,
        &BTreeSet::new(),
    )
    .expect("Block 3 upsert failed");

    let full_account_final =
        select_full_account(&db, account.id()).expect("Failed to load after block 3");

    let final_assets: Vec<Asset> = full_account_final.vault().assets().collect();
    assert_eq!(final_assets.len(), 1, "Should have exactly 1 vault asset");
    let fungible_asset = final_assets[0].unwrap_fungible();
    assert_eq!(fungible_asset.faucet_id(), faucet_id_1);
    assert_eq!(
        fungible_asset.amount().as_u64(),
        ADDED_AMOUNT_BLOCK_2 + ADDED_AMOUNT_BLOCK_3,
        "Expected total of 400"
    );

    assert_eq!(full_account_final.vault().root(), expected_vault_root_3);
    assert_eq!(full_account_final.to_commitment(), commitment_3);
}

/// The partial delta path must preserve a fungible asset's callback flag (part of its vault key)
/// across blocks, so the recomputed vault root and account commitment match the kernel's. Applies a
/// callback-enabled asset over two partial-delta blocks and checks the second one still matches.
#[test]
fn optimized_delta_updates_preserve_callback_flag() {
    const ACCOUNT_SEED: [u8; 32] = [41u8; 32];
    const NONCE_DELTA: u64 = 1;
    const ADDED_AMOUNT_BLOCK_2: u64 = 250;
    const ADDED_AMOUNT_BLOCK_3: u64 = 150;
    const SLOT_INDEX: usize = 0;

    let db = TestDb::new();

    let block_1 = BlockNumber::from(1u32);
    let block_2 = BlockNumber::from(2u32);
    let block_3 = BlockNumber::from(3u32);
    insert_block_header(&db, block_1);
    insert_block_header(&db, block_2);
    insert_block_header(&db, block_3);

    let faucet_id = callback_enabled_faucet_id();
    let account = callback_delta_test_account(ACCOUNT_SEED, SLOT_INDEX);
    insert_public_account(&db, block_1, &account);

    apply_callback_delta(&db, account.id(), faucet_id, block_2, ADDED_AMOUNT_BLOCK_2, NONCE_DELTA);
    let final_account = apply_callback_delta(
        &db,
        account.id(),
        faucet_id,
        block_3,
        ADDED_AMOUNT_BLOCK_3,
        NONCE_DELTA,
    );

    let final_assets: Vec<Asset> = final_account.vault().assets().collect();
    assert_eq!(final_assets.len(), 1, "Should have exactly 1 vault asset");
    let fungible_asset = final_assets[0].unwrap_fungible();
    assert_eq!(fungible_asset.faucet_id(), faucet_id);
    assert_eq!(
        fungible_asset.callbacks(),
        AssetCallbackFlag::Enabled,
        "callback flag must be preserved through delta application"
    );
    assert_eq!(fungible_asset.amount().as_u64(), ADDED_AMOUNT_BLOCK_2 + ADDED_AMOUNT_BLOCK_3);
}

#[test]
#[expect(clippy::too_many_lines)]
fn optimized_delta_updates_storage_map_header() {
    // Use deterministic account seed to keep account IDs stable.
    const ACCOUNT_SEED: [u8; 32] = [30u8; 32];
    // Use fixed block numbers to ensure deterministic ordering.
    const BLOCK_NUM_1: u32 = 1;
    const BLOCK_NUM_2: u32 = 2;
    // Use explicit slot index to avoid magic numbers.
    const SLOT_INDEX_MAP: usize = 3;
    // Use fixed map values to validate root updates.
    const MAP_KEY_VALUES: [u64; 4] = [7, 0, 0, 0];
    const MAP_VALUE_INITIAL: [u64; 4] = [10, 20, 30, 40];
    const MAP_VALUE_UPDATED: [u64; 4] = [50, 60, 70, 80];
    // Use nonzero nonce delta (required when storage/vault changes).
    const NONCE_DELTA: u64 = 1;

    let db = TestDb::new();

    let map_key = StorageMapKey::new(Word::from([
        Felt::new_unchecked(MAP_KEY_VALUES[0]),
        Felt::new_unchecked(MAP_KEY_VALUES[1]),
        Felt::new_unchecked(MAP_KEY_VALUES[2]),
        Felt::new_unchecked(MAP_KEY_VALUES[3]),
    ]));
    let map_value_initial = Word::from([
        Felt::new_unchecked(MAP_VALUE_INITIAL[0]),
        Felt::new_unchecked(MAP_VALUE_INITIAL[1]),
        Felt::new_unchecked(MAP_VALUE_INITIAL[2]),
        Felt::new_unchecked(MAP_VALUE_INITIAL[3]),
    ]);
    let map_value_updated = Word::from([
        Felt::new_unchecked(MAP_VALUE_UPDATED[0]),
        Felt::new_unchecked(MAP_VALUE_UPDATED[1]),
        Felt::new_unchecked(MAP_VALUE_UPDATED[2]),
        Felt::new_unchecked(MAP_VALUE_UPDATED[3]),
    ]);

    let storage_map = StorageMap::with_entries(vec![(map_key, map_value_initial)]).unwrap();
    let component_storage =
        vec![StorageSlot::with_map(StorageSlotName::mock(SLOT_INDEX_MAP), storage_map)];

    let account_component_code = CodeBuilder::default()
        .compile_component_code("test::interface", "@account_procedure pub proc map push.1 end")
        .unwrap();

    let component = AccountComponent::new(
        account_component_code,
        component_storage,
        AccountComponentMetadata::new("test"),
    )
    .unwrap();

    let account = AccountBuilder::new(ACCOUNT_SEED)
        .account_type(AccountType::Public)
        .with_component(component)
        .with_component(AuthSingleSig::new(Approver::new(
            PublicKeyCommitment::from(EMPTY_WORD),
            AuthScheme::Falcon512Poseidon2,
        )))
        .build_existing()
        .unwrap();

    let block_1 = BlockNumber::from(BLOCK_NUM_1);
    let block_2 = BlockNumber::from(BLOCK_NUM_2);
    insert_block_header(&db, block_1);
    insert_block_header(&db, block_2);

    let patch_initial = AccountPatch::try_from(account.clone()).unwrap();
    let account_update_initial = block_account_update(
        account.id(),
        account.to_commitment(),
        AccountUpdateDetails::Public(patch_initial),
    );
    upsert_accounts(
        &db,
        &[account_update_initial],
        block_1,
        &precomputed_states_from_account(&account),
        &BTreeSet::from([account.id()]),
    )
    .expect("Initial upsert failed");

    let full_account_before =
        select_full_account(&db, account.id()).expect("Failed to load full account");

    let map_patch = StorageMapPatch::from_iters([], [(map_key, map_value_updated)]);
    let storage_patch = AccountStoragePatch::from_raw(
        [(StorageSlotName::mock(SLOT_INDEX_MAP), StorageSlotPatch::Map(map_patch))]
            .into_iter()
            .collect::<BTreeMap<_, _>>(),
    )
    .unwrap();

    let final_nonce =
        Felt::new_unchecked(full_account_before.nonce().as_canonical_u64() + NONCE_DELTA);
    let partial_patch = AccountPatch::new(
        account.id(),
        storage_patch,
        AccountVaultPatch::default(),
        AccountCodePatch::default(),
        Some(final_nonce),
    )
    .unwrap();

    let mut expected_account = full_account_before.clone();
    expected_account.apply_patch(&partial_patch).unwrap();
    let expected_commitment = expected_account.to_commitment();
    let expected_storage_commitment = expected_account.storage().to_commitment();
    let precomputed_public_states = precomputed_states_from_account(&expected_account);

    let account_update = block_account_update(
        account.id(),
        expected_commitment,
        AccountUpdateDetails::Public(partial_patch),
    );
    upsert_accounts(&db, &[account_update], block_2, &precomputed_public_states, &BTreeSet::new())
        .expect("Partial delta upsert failed");

    let (header_after, storage_header_after) =
        select_account_header_with_storage_header_at_block(&db, account.id(), block_2)
            .expect("Query should succeed")
            .expect("Account should exist");

    assert_eq!(
        storage_header_after.to_commitment(),
        expected_storage_commitment,
        "Storage commitment should match after map delta"
    );
    assert_eq!(
        header_after.to_commitment(),
        expected_commitment,
        "Account commitment should match after map delta"
    );
}

#[test]
fn apply_storage_patch_with_roots_uses_precomputed_map_root() {
    use miden_protocol::account::{AccountStorageHeader, StorageSlotHeader, StorageSlotType};

    use super::apply_storage_patch_with_roots;

    let slot_name = StorageSlotName::mock(70);
    let key = StorageMapKey::new(Word::from([Felt::new_unchecked(71); 4]));
    let old_value = Word::from([Felt::new_unchecked(72); 4]);
    let new_value = Word::from([Felt::new_unchecked(73); 4]);
    let old_root = StorageMap::with_entries([(key, old_value)]).unwrap().root();
    let new_root = StorageMap::with_entries([(key, new_value)]).unwrap().root();
    let header = AccountStorageHeader::new(vec![StorageSlotHeader::new(
        slot_name.clone(),
        StorageSlotType::Map,
        old_root,
    )])
    .unwrap();
    let patch = AccountStoragePatch::from_raw(
        [(
            slot_name.clone(),
            StorageSlotPatch::Map(StorageMapPatch::from_iters([], [(key, new_value)])),
        )]
        .into_iter()
        .collect::<BTreeMap<_, _>>(),
    )
    .unwrap();
    let precomputed_roots = [(slot_name.clone(), new_root)].into_iter().collect::<BTreeMap<_, _>>();

    let new_header = apply_storage_patch_with_roots(&header, &patch, &precomputed_roots).unwrap();

    let updated_slot = new_header.find_slot_header_by_name(&slot_name).unwrap();
    assert_eq!(updated_slot.value(), new_root);
    assert_eq!(updated_slot.slot_type(), StorageSlotType::Map);
}

#[test]
fn partial_public_upsert_requires_precomputed_state() {
    const ACCOUNT_SEED: [u8; 32] = [80u8; 32];
    const SLOT_INDEX: usize = 0;

    let db = TestDb::new();
    let block_1 = BlockNumber::from(1u32);
    let block_2 = BlockNumber::from(2u32);
    insert_block_header(&db, block_1);
    insert_block_header(&db, block_2);

    let component_storage =
        vec![StorageSlot::with_value(StorageSlotName::mock(SLOT_INDEX), EMPTY_WORD)];
    let account_component_code = CodeBuilder::default()
        .compile_component_code(
            "test::interface",
            "@account_procedure pub proc required push.1 end",
        )
        .unwrap();
    let component = AccountComponent::new(
        account_component_code,
        component_storage,
        AccountComponentMetadata::new("test"),
    )
    .unwrap();
    let account = AccountBuilder::new(ACCOUNT_SEED)
        .account_type(AccountType::Public)
        .with_component(component)
        .with_component(AuthSingleSig::new(Approver::new(
            PublicKeyCommitment::from(EMPTY_WORD),
            AuthScheme::Falcon512Poseidon2,
        )))
        .build_existing()
        .unwrap();

    let patch_initial = AccountPatch::try_from(account.clone()).unwrap();
    upsert_accounts(
        &db,
        &[block_account_update(
            account.id(),
            account.to_commitment(),
            AccountUpdateDetails::Public(patch_initial),
        )],
        block_1,
        &precomputed_states_from_account(&account),
        &BTreeSet::from([account.id()]),
    )
    .expect("initial full-state upsert failed");

    let mut current_account = select_full_account(&db, account.id()).unwrap();
    let patch = AccountPatch::new(
        account.id(),
        AccountStoragePatch::new(),
        AccountVaultPatch::default(),
        AccountCodePatch::default(),
        Some(Felt::new_unchecked(current_account.nonce().as_canonical_u64() + 1)),
    )
    .unwrap();
    current_account.apply_patch(&patch).unwrap();

    let err = upsert_accounts(
        &db,
        &[block_account_update(
            account.id(),
            current_account.to_commitment(),
            AccountUpdateDetails::Public(patch),
        )],
        block_2,
        &PrecomputedPublicAccountStates::new(),
        &BTreeSet::new(),
    )
    .expect_err("partial public upsert should require precomputed roots");

    assert_matches!(err, DatabaseError::DataCorrupted(message) if message.contains("missing precomputed public account state"));
}

#[test]
fn partial_public_upsert_rejects_bad_precomputed_root() {
    const ACCOUNT_SEED: [u8; 32] = [81u8; 32];
    const SLOT_INDEX: usize = 0;

    let db = TestDb::new();
    let block_1 = BlockNumber::from(1u32);
    let block_2 = BlockNumber::from(2u32);
    insert_block_header(&db, block_1);
    insert_block_header(&db, block_2);

    let component_storage =
        vec![StorageSlot::with_value(StorageSlotName::mock(SLOT_INDEX), EMPTY_WORD)];
    let account_component_code = CodeBuilder::default()
        .compile_component_code("test::interface", "@account_procedure pub proc badroot push.1 end")
        .unwrap();
    let component = AccountComponent::new(
        account_component_code,
        component_storage,
        AccountComponentMetadata::new("test"),
    )
    .unwrap();
    let account = AccountBuilder::new(ACCOUNT_SEED)
        .account_type(AccountType::Public)
        .with_component(component)
        .with_component(AuthSingleSig::new(Approver::new(
            PublicKeyCommitment::from(EMPTY_WORD),
            AuthScheme::Falcon512Poseidon2,
        )))
        .build_existing()
        .unwrap();

    let patch_initial = AccountPatch::try_from(account.clone()).unwrap();
    upsert_accounts(
        &db,
        &[block_account_update(
            account.id(),
            account.to_commitment(),
            AccountUpdateDetails::Public(patch_initial),
        )],
        block_1,
        &precomputed_states_from_account(&account),
        &BTreeSet::from([account.id()]),
    )
    .expect("initial full-state upsert failed");

    let mut expected_account = select_full_account(&db, account.id()).unwrap();
    let patch = AccountPatch::new(
        account.id(),
        AccountStoragePatch::new(),
        AccountVaultPatch::default(),
        AccountCodePatch::default(),
        Some(Felt::new_unchecked(expected_account.nonce().as_canonical_u64() + 1)),
    )
    .unwrap();
    expected_account.apply_patch(&patch).unwrap();

    let mut precomputed = precomputed_states_from_account(&expected_account);
    precomputed.get_mut(&account.id()).unwrap().vault_root =
        Word::from([Felt::new_unchecked(999); 4]);

    let err = upsert_accounts(
        &db,
        &[block_account_update(
            account.id(),
            expected_account.to_commitment(),
            AccountUpdateDetails::Public(patch),
        )],
        block_2,
        &precomputed,
        &BTreeSet::new(),
    )
    .expect_err("bad precomputed roots should be validated against the final commitment");

    assert_matches!(err, DatabaseError::AccountCommitmentsMismatch { .. });
}

/// Tests that a private account update (no public state) is handled correctly.
///
/// Private accounts store only the account commitment, not the full state.
#[test]
fn upsert_private_account() {
    use miden_protocol::account::{AccountIdVersion, AccountType};

    // Use deterministic account seed to keep account IDs stable.
    const ACCOUNT_ID_SEED: [u8; 15] = [20u8; 15];
    // Use fixed block number to keep test ordering deterministic.
    const BLOCK_NUM: u32 = 1;
    // Use fixed commitment values to validate storage behavior.
    const COMMITMENT_WORDS: [u64; 4] = [1, 2, 3, 4];

    let db = TestDb::new();

    let block_num = BlockNumber::from(BLOCK_NUM);
    insert_block_header(&db, block_num);

    // Create a private account ID
    let account_id = AccountId::dummy(
        ACCOUNT_ID_SEED,
        AccountIdVersion::Version1,
        AccountType::Private,
        AssetCallbackFlag::Disabled,
    );

    let account_commitment = Word::from([
        Felt::new_unchecked(COMMITMENT_WORDS[0]),
        Felt::new_unchecked(COMMITMENT_WORDS[1]),
        Felt::new_unchecked(COMMITMENT_WORDS[2]),
        Felt::new_unchecked(COMMITMENT_WORDS[3]),
    ]);

    // Insert as private account
    let account_update =
        block_account_update(account_id, account_commitment, AccountUpdateDetails::Private);

    upsert_accounts(
        &db,
        &[account_update],
        block_num,
        &PrecomputedPublicAccountStates::new(),
        &BTreeSet::from([account_id]),
    )
    .expect("Private account upsert failed");

    // Verify the account exists and commitment matches

    let (stored_commitment, stored_nonce, stored_code) = latest_account_row(&db, account_id);

    assert_eq!(stored_commitment, account_commitment, "Stored commitment should match");

    // Private accounts have NULL for nonce, code_commitment, storage_header, vault_root
    assert!(stored_nonce.is_none(), "Private account should have NULL nonce");
    assert!(stored_code.is_none(), "Private account should have NULL code_commitment");
}

/// Tests that a full-state delta (new account creation) is handled correctly.
///
/// Full-state deltas contain the complete account state including code.
#[test]
fn upsert_full_state_delta() {
    // Use deterministic account seed to keep account IDs stable.
    const ACCOUNT_SEED: [u8; 32] = [20u8; 32];
    // Use fixed block number to keep test ordering deterministic.
    const BLOCK_NUM: u32 = 1;
    // Use fixed slot values to validate storage behavior.
    const SLOT_VALUES: [u64; 4] = [10, 20, 30, 40];
    // Use explicit slot index to avoid magic numbers.
    const SLOT_INDEX: usize = 0;

    let db = TestDb::new();

    let block_num = BlockNumber::from(BLOCK_NUM);
    insert_block_header(&db, block_num);

    // Create an account with storage
    let slot_value = Word::from([
        Felt::new_unchecked(SLOT_VALUES[0]),
        Felt::new_unchecked(SLOT_VALUES[1]),
        Felt::new_unchecked(SLOT_VALUES[2]),
        Felt::new_unchecked(SLOT_VALUES[3]),
    ]);
    let component_storage =
        vec![StorageSlot::with_value(StorageSlotName::mock(SLOT_INDEX), slot_value)];

    let account_component_code = CodeBuilder::default()
        .compile_component_code("test::interface", "@account_procedure pub proc bar push.2 end")
        .unwrap();

    let component = AccountComponent::new(
        account_component_code,
        component_storage,
        AccountComponentMetadata::new("test"),
    )
    .unwrap();

    let account = AccountBuilder::new(ACCOUNT_SEED)
        .account_type(AccountType::Public)
        .with_component(component)
        .with_component(AuthSingleSig::new(Approver::new(
            PublicKeyCommitment::from(EMPTY_WORD),
            AuthScheme::Falcon512Poseidon2,
        )))
        .build_existing()
        .unwrap();

    // Create a full-state patch from the account
    let patch = AccountPatch::try_from(account.clone()).unwrap();

    let account_update = block_account_update(
        account.id(),
        account.to_commitment(),
        AccountUpdateDetails::Public(patch),
    );

    upsert_accounts(
        &db,
        &[account_update],
        block_num,
        &precomputed_states_from_account(&account),
        &BTreeSet::from([account.id()]),
    )
    .expect("Full-state delta upsert failed");

    // Verify the account state was stored correctly
    let (header, storage_header) =
        select_account_header_with_storage_header_at_block(&db, account.id(), block_num)
            .expect("Query should succeed")
            .expect("Account should exist");

    assert_eq!(header.nonce(), account.nonce(), "Nonce should match");
    assert_eq!(
        header.code_commitment(),
        account.code().commitment(),
        "Code commitment should match"
    );
    assert_eq!(
        storage_header.to_commitment(),
        account.storage().to_commitment(),
        "Storage commitment should match"
    );

    // Verify we can load the full account back
    let loaded_account = select_full_account(&db, account.id()).expect("Should load full account");

    assert_eq!(loaded_account.nonce(), account.nonce());
    assert_eq!(loaded_account.code().commitment(), account.code().commitment());
    assert_eq!(loaded_account.storage().to_commitment(), account.storage().to_commitment());
}

/// Tests that `apply_storage_patch` mirrors the protocol's patch semantics for slot creation,
/// update, and removal, for both value and map slots.
#[test]
fn apply_storage_patch_handles_create_update_and_remove() {
    use std::collections::HashMap;

    use miden_protocol::account::{
        AccountStorageHeader,
        StorageMapPatchEntries,
        StorageSlotHeader,
        StorageSlotType,
    };

    use super::apply_storage_patch;

    let removed_value_name = StorageSlotName::mock(10);
    let removed_map_name = StorageSlotName::mock(11);
    let updated_value_name = StorageSlotName::mock(12);
    let created_value_name = StorageSlotName::mock(13);
    let created_map_name = StorageSlotName::mock(14);

    let old_value = Word::from([Felt::new_unchecked(1); 4]);
    let updated_value = Word::from([Felt::new_unchecked(2); 4]);
    let created_value = Word::from([Felt::new_unchecked(3); 4]);
    let old_map_root = StorageMap::with_entries(std::iter::empty()).unwrap().root();

    let mut slots = vec![
        StorageSlotHeader::new(removed_value_name.clone(), StorageSlotType::Value, old_value),
        StorageSlotHeader::new(removed_map_name.clone(), StorageSlotType::Map, old_map_root),
        StorageSlotHeader::new(updated_value_name.clone(), StorageSlotType::Value, old_value),
    ];
    slots.sort_by_key(StorageSlotHeader::id);
    let header = AccountStorageHeader::new(slots).unwrap();

    let patch = AccountStoragePatch::from_raw(
        [
            (removed_value_name.clone(), StorageSlotPatch::Value(StorageValuePatch::Remove)),
            (removed_map_name.clone(), StorageSlotPatch::Map(StorageMapPatch::Remove)),
            (
                updated_value_name.clone(),
                StorageSlotPatch::Value(StorageValuePatch::Update { value: updated_value }),
            ),
            (
                created_value_name.clone(),
                StorageSlotPatch::Value(StorageValuePatch::Create { value: created_value }),
            ),
            (
                created_map_name.clone(),
                StorageSlotPatch::Map(StorageMapPatch::Create {
                    entries: StorageMapPatchEntries::new(),
                }),
            ),
        ]
        .into_iter()
        .collect::<BTreeMap<_, _>>(),
    )
    .unwrap();

    let new_header =
        apply_storage_patch(&header, &patch, &HashMap::new()).expect("patch should apply");

    assert_eq!(new_header.num_slots(), 3, "two slots removed, two created");
    assert!(
        new_header.find_slot_header_by_name(&removed_value_name).is_none(),
        "removed value slot should be dropped from the header"
    );
    assert!(
        new_header.find_slot_header_by_name(&removed_map_name).is_none(),
        "removed map slot should be dropped from the header"
    );

    let updated_slot = new_header.find_slot_header_by_name(&updated_value_name).unwrap();
    assert_eq!(updated_slot.value(), updated_value);
    assert_eq!(updated_slot.slot_type(), StorageSlotType::Value);

    let created_slot = new_header.find_slot_header_by_name(&created_value_name).unwrap();
    assert_eq!(created_slot.value(), created_value);
    assert_eq!(created_slot.slot_type(), StorageSlotType::Value);

    let created_map_slot = new_header.find_slot_header_by_name(&created_map_name).unwrap();
    assert_eq!(created_map_slot.value(), old_map_root, "empty map root expected");
    assert_eq!(created_map_slot.slot_type(), StorageSlotType::Map);
}