evm-fork-cache 0.4.0-alpha.3

Forked EVM state cache, snapshots, overlays, and simulation utilities for EVM search
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
#![cfg(feature = "raw-flashblocks-json")]

use alloy_network::Ethereum;
#[cfg(feature = "reactive-ws")]
use alloy_primitives::U256;
use alloy_primitives::{Address, B256};
use alloy_provider::ProviderBuilder;
#[cfg(feature = "reactive-ws")]
use alloy_rpc_types_eth::Filter;
use alloy_transport::mock::Asserter;
use evm_fork_cache::reactive::{
    AlloySubscriber, FlashblockInvalidationReason, FlashblockUpdate, FlashblockUpdateChannelError,
    PreconfirmationMode, ProviderRef, RawJsonFlashblocksAdapter, RawJsonFlashblocksLimits,
    SubscriberConfig, SubscriberMode,
};
#[cfg(feature = "reactive-ws")]
use evm_fork_cache::reactive::{
    ChainStatus, EventSubscriber, LogInterest, ReactiveInput, ReactiveInterest,
};
use proptest::prelude::*;

const TX_ONE: B256 =
    alloy_primitives::b256!("5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2");
const TX_TWO: B256 =
    alloy_primitives::b256!("f2ee15ea639b73fa3db9b34a245bdfa015c260c598b211bf05a1ecc4b3e3b4f2");

fn index_zero() -> Vec<u8> {
    br#"{
        "payload_id":"0x1111111111111111",
        "index":0,
        "base":{
            "parent_hash":"0x6464646464646464646464646464646464646464646464646464646464646464",
            "block_number":"0x65",
            "timestamp":"0x6553f165",
            "gas_limit":"0x1c9c380",
            "base_fee_per_gas":"0x7",
            "fee_recipient":"0xcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcb",
            "prev_randao":"0x7777777777777777777777777777777777777777777777777777777777777777"
        },
        "diff":{
            "state_root":"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
            "block_hash":"0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
            "transactions":["0x01"]
        },
        "metadata":{
            "block_number":101,
            "receipts":{
                "0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2":{
                    "type":"0x2",
                    "status":"0x1",
                    "cumulativeGasUsed":"0x5208",
                    "logs":[{
                        "address":"0x4242424242424242424242424242424242424242",
                        "topics":["0x4343434343434343434343434343434343434343434343434343434343434343"],
                        "data":"0x0102"
                    }]
                }
            }
        }
    }"#
    .to_vec()
}

fn index_one(receipt_transaction: B256) -> Vec<u8> {
    format!(
        r#"{{
            "payload_id":"0x1111111111111111",
            "index":1,
            "diff":{{
                "state_root":"0xabababababababababababababababababababababababababababababababab",
                "block_hash":"0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
                "transactions":["0x02"]
            }},
            "metadata":{{
                "block_number":"0x65",
                "receipts":{{
                    "{receipt_transaction:#x}":{{
                        "logs":[
                            {{
                                "address":"0x4444444444444444444444444444444444444444",
                                "topics":[],
                                "data":"0x03"
                            }},
                            {{
                                "address":"0x4545454545454545454545454545454545454545",
                                "topics":[],
                                "data":"0x04"
                            }}
                        ]
                    }}
                }}
            }}
        }}"#
    )
    .into_bytes()
}

fn index_two(receipt_transaction: B256) -> Vec<u8> {
    format!(
        r#"{{
            "payload_id":"0x1111111111111111",
            "index":2,
            "diff":{{
                "state_root":"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                "block_hash":"0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
                "transactions":["0x03"]
            }},
            "metadata":{{
                "block_number":"0x65",
                "receipts":{{
                    "{receipt_transaction:#x}":{{
                        "logs":[]
                    }}
                }}
            }}
        }}"#
    )
    .into_bytes()
}

fn conflicting_index_one(receipt_transaction: B256) -> Vec<u8> {
    format!(
        r#"{{
            "payload_id":"0x1111111111111111",
            "index":1,
            "diff":{{
                "state_root":"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                "block_hash":"0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
                "transactions":["0x03"]
            }},
            "metadata":{{
                "block_number":"0x65",
                "receipts":{{
                    "{receipt_transaction:#x}":{{
                        "logs":[]
                    }}
                }}
            }}
        }}"#
    )
    .into_bytes()
}

fn alternate_index_zero() -> Vec<u8> {
    let third_transaction = alloy_primitives::keccak256([3_u8]);
    String::from_utf8(index_zero())
        .expect("index-zero fixture is UTF-8")
        .replace("\"transactions\":[\"0x01\"]", "\"transactions\":[\"0x03\"]")
        .replace(&format!("{TX_ONE:#x}"), &format!("{third_transaction:#x}"))
        .into_bytes()
}

fn alternate_index_one(receipt_transaction: B256) -> Vec<u8> {
    format!(
        r#"{{
            "payload_id":"0x1111111111111111",
            "index":1,
            "diff":{{
                "state_root":"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                "block_hash":"0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
                "transactions":["0x04"]
            }},
            "metadata":{{
                "block_number":"0x65",
                "receipts":{{
                    "{receipt_transaction:#x}":{{
                        "logs":[]
                    }}
                }}
            }}
        }}"#
    )
    .into_bytes()
}

fn index_zero_for_block(block_number: u64) -> Vec<u8> {
    String::from_utf8(index_zero())
        .expect("index-zero fixture is UTF-8")
        .replace(
            "\"block_number\":\"0x65\"",
            &format!("\"block_number\":\"0x{block_number:x}\""),
        )
        .replace(
            "\"block_number\":101",
            &format!("\"block_number\":{block_number}"),
        )
        .into_bytes()
}

fn index_one_for_block(block_number: u64) -> Vec<u8> {
    String::from_utf8(index_one(TX_TWO))
        .expect("index-one fixture is UTF-8")
        .replace(
            "\"block_number\":\"0x65\"",
            &format!("\"block_number\":\"0x{block_number:x}\""),
        )
        .into_bytes()
}

fn adapter(endpoint: &str, generation: u64) -> RawJsonFlashblocksAdapter {
    RawJsonFlashblocksAdapter::new(ProviderRef::new(endpoint, generation))
}

#[tokio::test]
async fn bounded_update_sender_remains_external_to_subscriber_ownership() {
    let source = ProviderRef::new("raw-json", 7);
    let provider = ProviderBuilder::new().connect_mocked_client(Asserter::new());
    let mut subscriber = AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::Auto,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    let sender = subscriber
        .open_external_flashblock_update_channel(1)
        .expect("bounded update channel");
    let mut adapter = RawJsonFlashblocksAdapter::new(source.clone());
    let update = adapter
        .ingest_json(&index_zero())
        .expect("valid raw frame")
        .expect("standardized update");

    let _receipt = sender.try_send(update.clone()).expect("first queue slot");
    assert!(matches!(
        sender.try_send(update.clone()),
        Err(FlashblockUpdateChannelError::Full)
    ));

    let mut other = RawJsonFlashblocksAdapter::new(ProviderRef::new("other-endpoint", 1));
    let other_update = other
        .ingest_json(&index_zero())
        .expect("other raw frame")
        .expect("other standardized update");
    assert!(matches!(
        sender.try_send(other_update),
        Err(FlashblockUpdateChannelError::UnexpectedEndpoint)
    ));

    drop(subscriber);
    assert_eq!(
        sender.send(update).await,
        Err(FlashblockUpdateChannelError::Closed)
    );
}

#[test]
fn update_channel_configuration_is_single_and_nonzero() {
    let source = ProviderRef::new("raw-json", 7);
    let provider = ProviderBuilder::new().connect_mocked_client(Asserter::new());
    let mut subscriber = AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::Auto,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source)
        .expect("configure external source");

    assert!(
        subscriber
            .open_external_flashblock_update_channel(0)
            .expect_err("zero capacity")
            .to_string()
            .contains("greater than zero")
    );
    subscriber
        .open_external_flashblock_update_channel(1)
        .expect("first channel");
    assert!(
        subscriber
            .open_external_flashblock_update_channel(1)
            .expect_err("second channel")
            .to_string()
            .contains("already opened")
    );
}

#[test]
fn direct_standardized_ingest_rejects_an_index_gap() {
    let source = ProviderRef::new("raw-json", 7);
    let provider = ProviderBuilder::new().connect_mocked_client(Asserter::new());
    let mut subscriber = AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::Auto,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    let mut adapter = RawJsonFlashblocksAdapter::new(source);

    let first = adapter
        .ingest_json(&index_zero())
        .expect("valid index zero")
        .expect("index zero snapshot");
    let _skipped = adapter
        .ingest_json(&index_one(TX_TWO))
        .expect("valid index one")
        .expect("index one snapshot");
    let third_transaction = alloy_primitives::keccak256([3_u8]);
    let gap = adapter
        .ingest_json(&index_two(third_transaction))
        .expect("valid index two")
        .expect("index two snapshot");

    subscriber
        .ingest_flashblock_update(first)
        .expect("index zero is accepted");
    assert!(
        subscriber
            .ingest_flashblock_update(gap)
            .expect_err("subscriber must reject a skipped index")
            .to_string()
            .contains("index")
    );
}

#[test]
fn direct_standardized_ingest_requires_index_zero_for_a_new_payload() {
    let source = ProviderRef::new("raw-json", 7);
    let provider = ProviderBuilder::new().connect_mocked_client(Asserter::new());
    let mut subscriber = AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::Auto,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    let mut adapter = RawJsonFlashblocksAdapter::new(source);
    let _ = adapter
        .ingest_json(&index_zero())
        .expect("valid index zero")
        .expect("index zero snapshot");
    let starts_late = adapter
        .ingest_json(&index_one(TX_TWO))
        .expect("valid index one")
        .expect("index one snapshot");

    assert!(
        subscriber
            .ingest_flashblock_update(starts_late)
            .expect_err("a new payload cannot start after index zero")
            .to_string()
            .contains("begin at index zero")
    );
}

#[test]
fn direct_standardized_ingest_rejects_an_index_regression() {
    let source = ProviderRef::new("raw-json", 7);
    let provider = ProviderBuilder::new().connect_mocked_client(Asserter::new());
    let mut subscriber = AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::Auto,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    let mut adapter = RawJsonFlashblocksAdapter::new(source);
    let first = adapter
        .ingest_json(&index_zero())
        .expect("valid index zero")
        .expect("index zero snapshot");
    let repeated_first = first.clone();
    let next = adapter
        .ingest_json(&index_one(TX_TWO))
        .expect("valid index one")
        .expect("index one snapshot");
    subscriber
        .ingest_flashblock_update(first)
        .expect("index zero is accepted");
    subscriber
        .ingest_flashblock_update(next)
        .expect("index one is accepted");

    assert!(
        subscriber
            .ingest_flashblock_update(repeated_first)
            .expect_err("an older index cannot replace a newer snapshot")
            .to_string()
            .contains("regressed")
    );
}

#[test]
fn direct_standardized_ingest_accepts_an_exact_duplicate_as_a_no_op() {
    let source = ProviderRef::new("raw-json", 7);
    let provider = ProviderBuilder::new().connect_mocked_client(Asserter::new());
    let mut subscriber = AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::Auto,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    let mut adapter = RawJsonFlashblocksAdapter::new(source);
    let first = adapter
        .ingest_json(&index_zero())
        .expect("valid index zero")
        .expect("index zero snapshot");
    subscriber
        .ingest_flashblock_update(first.clone())
        .expect("index zero is accepted");
    subscriber
        .ingest_flashblock_update(first)
        .expect("an exact duplicate is idempotent");
}

#[test]
fn direct_standardized_ingest_rejects_changed_content_at_the_same_index() {
    let source = ProviderRef::new("raw-json", 7);
    let provider = ProviderBuilder::new().connect_mocked_client(Asserter::new());
    let mut subscriber = AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::Auto,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    let mut first_source = RawJsonFlashblocksAdapter::new(source.clone());
    let first = first_source
        .ingest_json(&index_zero())
        .expect("valid index zero")
        .expect("index zero snapshot");
    let index_one = first_source
        .ingest_json(&index_one(TX_TWO))
        .expect("valid index one")
        .expect("index one snapshot");
    subscriber
        .ingest_flashblock_update(first)
        .expect("index zero is accepted");
    subscriber
        .ingest_flashblock_update(index_one)
        .expect("index one is accepted");

    let mut conflicting_source = RawJsonFlashblocksAdapter::new(source);
    let _ = conflicting_source
        .ingest_json(&index_zero())
        .expect("valid alternate index zero")
        .expect("alternate index zero snapshot");
    let third_transaction = alloy_primitives::keccak256([3_u8]);
    let conflict = conflicting_source
        .ingest_json(&conflicting_index_one(third_transaction))
        .expect("independently valid conflicting index one")
        .expect("conflicting index one snapshot");

    assert!(
        subscriber
            .ingest_flashblock_update(conflict)
            .expect_err("same-index content changes must fail closed")
            .to_string()
            .contains("same index")
    );
}

#[test]
fn direct_standardized_ingest_rejects_non_prefix_cumulative_membership() {
    let source = ProviderRef::new("raw-json", 7);
    let provider = ProviderBuilder::new().connect_mocked_client(Asserter::new());
    let mut subscriber = AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::Auto,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    let mut accepted_source = RawJsonFlashblocksAdapter::new(source.clone());
    let first = accepted_source
        .ingest_json(&index_zero())
        .expect("valid index zero")
        .expect("index zero snapshot");
    subscriber
        .ingest_flashblock_update(first)
        .expect("index zero is accepted");

    let mut divergent_source = RawJsonFlashblocksAdapter::new(source);
    let _ = divergent_source
        .ingest_json(&alternate_index_zero())
        .expect("valid divergent index zero")
        .expect("divergent index zero snapshot");
    let fourth_transaction = alloy_primitives::keccak256([4_u8]);
    let non_prefix = divergent_source
        .ingest_json(&alternate_index_one(fourth_transaction))
        .expect("independently valid divergent index one")
        .expect("divergent index one snapshot");

    assert!(
        subscriber
            .ingest_flashblock_update(non_prefix)
            .expect_err("cumulative membership must retain the prior prefix")
            .to_string()
            .contains("prefix")
    );
}

#[test]
fn direct_standardized_ingest_rejects_base_identity_changes_within_a_payload() {
    let source = ProviderRef::new("raw-json", 7);
    let provider = ProviderBuilder::new().connect_mocked_client(Asserter::new());
    let mut subscriber = AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::Auto,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    let mut accepted_source = RawJsonFlashblocksAdapter::new(source.clone());
    let first = accepted_source
        .ingest_json(&index_zero())
        .expect("valid index zero")
        .expect("index zero snapshot");
    subscriber
        .ingest_flashblock_update(first)
        .expect("index zero is accepted");

    let mut altered_source = RawJsonFlashblocksAdapter::new(source);
    let _ = altered_source
        .ingest_json(&index_zero_for_block(102))
        .expect("valid alternate base")
        .expect("alternate index zero snapshot");
    let altered_base = altered_source
        .ingest_json(&index_one_for_block(102))
        .expect("valid alternate index one")
        .expect("alternate index one snapshot");

    assert!(
        subscriber
            .ingest_flashblock_update(altered_base)
            .expect_err("base identity must stay stable within a payload")
            .to_string()
            .contains("base identity")
    );
}

#[test]
fn direct_standardized_ingest_rejects_delta_logs_from_prior_transactions() {
    let source = ProviderRef::new("raw-json", 7);
    let provider = ProviderBuilder::new().connect_mocked_client(Asserter::new());
    let mut subscriber = AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::Auto,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    let mut adapter = RawJsonFlashblocksAdapter::new(source);
    let first = adapter
        .ingest_json(&index_zero())
        .expect("valid index zero")
        .expect("index zero snapshot");
    subscriber
        .ingest_flashblock_update(first)
        .expect("index zero is accepted");

    let mut next = adapter
        .ingest_json(&index_one(TX_TWO))
        .expect("valid index one")
        .expect("index one snapshot");
    let FlashblockUpdate::Snapshot(snapshot) = &mut next else {
        panic!("expected snapshot")
    };
    snapshot.logs[0].transaction_hash = Some(TX_ONE);
    snapshot.logs[0].transaction_index = Some(0);
    snapshot.logs[0].log_index = Some(99);

    assert!(
        subscriber
            .ingest_flashblock_update(next)
            .expect_err("delta logs must come from newly appended transactions")
            .to_string()
            .contains("newly appended")
    );
}

#[test]
fn indexed_delta_becomes_a_standard_flashblock_update() {
    let provider = ProviderRef::new("raw-json", 7);
    let mut adapter = RawJsonFlashblocksAdapter::new(provider.clone());

    let update = adapter
        .ingest_json(&index_zero())
        .expect("valid raw Flashblock")
        .expect("first index publishes a batch");
    let FlashblockUpdate::Snapshot(batch) = update else {
        panic!("expected standardized snapshot")
    };

    assert_eq!(batch.flashblock.provider, provider);
    assert_eq!(batch.flashblock.payload_id, Some([0x11; 8].into()));
    assert_eq!(batch.flashblock.index, Some(0));
    assert_eq!(batch.flashblock.block_number, 101);
    assert_eq!(batch.flashblock.transaction_hashes, vec![TX_ONE]);
    assert_ne!(batch.flashblock.content_hash, B256::ZERO);
    assert_eq!(batch.logs.len(), 1);
    assert_eq!(batch.logs[0].address(), Address::repeat_byte(0x42));
    assert_eq!(batch.logs[0].transaction_hash, Some(TX_ONE));
    assert_eq!(batch.logs[0].transaction_index, Some(0));
    assert_eq!(batch.logs[0].log_index, Some(0));
    assert_eq!(
        batch.logs[0].block_hash,
        Some(batch.flashblock.content_hash)
    );
}

#[test]
fn converter_is_chain_neutral_and_carries_only_caller_provenance() {
    let mut first = adapter("chain-a-provider", 2);
    let mut second = adapter("chain-b-provider", 9);

    let FlashblockUpdate::Snapshot(first) = first
        .ingest_json(&index_zero())
        .expect("first provider frame")
        .expect("first provider snapshot")
    else {
        panic!("expected snapshot")
    };
    let FlashblockUpdate::Snapshot(second) = second
        .ingest_json(&index_zero())
        .expect("second provider frame")
        .expect("second provider snapshot")
    else {
        panic!("expected snapshot")
    };

    assert_eq!(
        first.flashblock.provider,
        ProviderRef::new("chain-a-provider", 2)
    );
    assert_eq!(
        second.flashblock.provider,
        ProviderRef::new("chain-b-provider", 9)
    );
    assert_ne!(
        first.flashblock.content_hash,
        second.flashblock.content_hash
    );
}

#[test]
fn rejected_delta_does_not_poison_a_corrected_retry() {
    let mut adapter = adapter("raw-json", 7);
    adapter
        .ingest_json(&index_zero())
        .expect("valid index zero");

    let error = adapter
        .ingest_json(&index_one(TX_ONE))
        .expect_err("receipt membership mismatch");
    assert!(error.to_string().contains("receipt-map membership"));

    let FlashblockUpdate::Snapshot(batch) = adapter
        .ingest_json(&index_one(TX_TWO))
        .expect("corrected retry remains admissible")
        .expect("corrected retry publishes")
    else {
        panic!("expected snapshot")
    };
    assert_eq!(batch.flashblock.transaction_hashes, vec![TX_ONE, TX_TWO]);
    assert_eq!(batch.logs.len(), 2);
    assert_eq!(batch.logs[0].transaction_index, Some(1));
    assert_eq!(batch.logs[0].log_index, Some(1));
    assert_eq!(batch.logs[1].log_index, Some(2));
}

#[test]
fn semantic_duplicate_is_idempotent_across_json_serializations() {
    let mut adapter = adapter("raw-json", 7);
    let frame = index_zero();
    adapter.ingest_json(&frame).expect("first delivery");
    let compact = serde_json::to_vec(
        &serde_json::from_slice::<serde_json::Value>(&frame).expect("fixture JSON"),
    )
    .expect("compact fixture");

    assert!(adapter.ingest_json(&compact).expect("duplicate").is_none());
}

#[test]
fn duplicate_receipt_map_keys_are_rejected_before_normalization() {
    let receipt_key = format!("{TX_ONE:#x}");
    let original_key = format!("\"{receipt_key}\":{{");
    let uppercase_key = format!("0x{}", receipt_key[2..].to_ascii_uppercase());

    for duplicate_key in [&receipt_key, &uppercase_key] {
        let repeated_key = format!("\"{receipt_key}\":{{\"logs\":[]}},\"{duplicate_key}\":{{");
        let frame = String::from_utf8(index_zero())
            .expect("index-zero fixture is UTF-8")
            .replace(&original_key, &repeated_key);
        let mut adapter = adapter("raw-json", 7);

        let error = adapter
            .ingest_json(frame.as_bytes())
            .expect_err("duplicate receipt key must be rejected");
        assert!(error.to_string().contains("duplicate receipt key"));
    }
}

#[test]
fn sequence_failures_emit_standard_invalidations() {
    let mut adapter = adapter("raw-json", 7);
    adapter.ingest_json(&index_zero()).expect("first delivery");
    let gap = String::from_utf8(index_one(TX_TWO))
        .expect("fixture UTF-8")
        .replace("\"index\":1", "\"index\":2")
        .into_bytes();

    let FlashblockUpdate::Invalidated(invalidation) = adapter
        .ingest_json(&gap)
        .expect("gap is an invalidation")
        .expect("gap is observable")
    else {
        panic!("expected invalidation")
    };
    assert_eq!(invalidation.provider, ProviderRef::new("raw-json", 7));
    assert_eq!(invalidation.reason, FlashblockInvalidationReason::IndexGap);
    assert!(
        adapter
            .ingest_json(&index_one(TX_TWO))
            .expect("ignored remainder")
            .is_none()
    );

    let replacement = String::from_utf8(index_zero())
        .expect("fixture UTF-8")
        .replace("0x1111111111111111", "0x2222222222222222")
        .into_bytes();
    assert!(matches!(
        adapter.ingest_json(&replacement).expect("next payload"),
        Some(FlashblockUpdate::Snapshot(_))
    ));
}

#[test]
fn joining_after_index_zero_invalidates_instead_of_inventing_a_base() {
    let mut adapter = adapter("raw-json", 7);
    let FlashblockUpdate::Invalidated(invalidation) = adapter
        .ingest_json(&index_one(TX_TWO))
        .expect("late join becomes an invalidation")
        .expect("late join is observable")
    else {
        panic!("expected invalidation")
    };
    assert_eq!(
        invalidation.reason,
        FlashblockInvalidationReason::MissingInitialIndex
    );
}

#[test]
fn every_delta_must_retain_the_index_zero_block_number() {
    let mut adapter = adapter("raw-json", 7);
    adapter.ingest_json(&index_zero()).expect("index zero");
    let wrong_block = String::from_utf8(index_one(TX_TWO))
        .expect("fixture UTF-8")
        .replace("\"block_number\":\"0x65\"", "\"block_number\":\"0x66\"")
        .into_bytes();
    assert!(
        adapter
            .ingest_json(&wrong_block)
            .expect_err("block drift")
            .to_string()
            .contains("block numbers disagree")
    );
    assert!(matches!(
        adapter
            .ingest_json(&index_one(TX_TWO))
            .expect("correct retry"),
        Some(FlashblockUpdate::Snapshot(_))
    ));
}

#[test]
fn caller_reset_revokes_the_active_generation_without_reconnecting() {
    let mut adapter = adapter("raw-json", 7);
    adapter.ingest_json(&index_zero()).expect("first delivery");

    let FlashblockUpdate::Invalidated(invalidation) = adapter
        .reset(ProviderRef::new("raw-json", 8))
        .expect("valid source transition")
        .expect("active payload is revoked")
    else {
        panic!("expected invalidation")
    };
    assert_eq!(invalidation.provider, ProviderRef::new("raw-json", 7));
    assert_eq!(
        invalidation.reason,
        FlashblockInvalidationReason::SourceReset
    );

    let FlashblockUpdate::Snapshot(replacement) = adapter
        .ingest_json(&index_zero())
        .expect("new generation frame")
        .expect("new generation snapshot")
    else {
        panic!("expected replacement snapshot")
    };
    assert_eq!(
        replacement.flashblock.provider,
        ProviderRef::new("raw-json", 8)
    );
}

#[test]
fn reset_rejects_endpoint_changes_and_non_increasing_generations() {
    let mut adapter = adapter("raw-json", 7);
    adapter.ingest_json(&index_zero()).expect("first delivery");

    for replacement in [
        ProviderRef::new("raw-json", 7),
        ProviderRef::new("raw-json", 6),
        ProviderRef::new("other-source", 8),
    ] {
        assert!(
            adapter
                .reset(replacement)
                .expect_err("ambiguous transition")
                .to_string()
                .contains("source transition")
        );
    }

    let FlashblockUpdate::Snapshot(next) = adapter
        .ingest_json(&index_one(TX_TWO))
        .expect("rejected reset preserves active state")
        .expect("next delta remains admissible")
    else {
        panic!("expected snapshot")
    };
    assert_eq!(next.flashblock.provider, ProviderRef::new("raw-json", 7));
    assert_eq!(next.flashblock.index, Some(1));
}

#[test]
fn zero_resource_bounds_are_rejected_at_construction() {
    let mut limits = RawJsonFlashblocksLimits::default();
    limits.max_frame_bytes = 0;
    let error = RawJsonFlashblocksAdapter::with_limits(ProviderRef::new("raw-json", 1), limits)
        .expect_err("zero frame limit must be rejected");
    assert!(error.to_string().contains("max_frame_bytes"));
}

#[test]
fn conflicting_duplicate_revokes_the_active_payload() {
    let mut adapter = adapter("raw-json", 7);
    adapter.ingest_json(&index_zero()).expect("first delivery");
    let conflicting = String::from_utf8(index_zero())
        .expect("fixture UTF-8")
        .replace("\"data\":\"0x0102\"", "\"data\":\"0x99\"")
        .into_bytes();

    let FlashblockUpdate::Invalidated(invalidation) = adapter
        .ingest_json(&conflicting)
        .expect("conflict becomes an invalidation")
        .expect("conflict is observable")
    else {
        panic!("expected invalidation")
    };
    assert_eq!(
        invalidation.reason,
        FlashblockInvalidationReason::ConflictingDuplicate
    );
    assert_eq!(invalidation.payload_id, [0x11_u8; 8]);
}

#[test]
fn a_new_payload_missing_index_zero_revokes_the_previous_payload() {
    let mut adapter = adapter("raw-json", 7);
    adapter.ingest_json(&index_zero()).expect("first delivery");
    let next_payload_gap = String::from_utf8(index_one(TX_TWO))
        .expect("fixture UTF-8")
        .replace("0x1111111111111111", "0x2222222222222222")
        .into_bytes();

    let FlashblockUpdate::Invalidated(invalidation) = adapter
        .ingest_json(&next_payload_gap)
        .expect("missing base becomes an invalidation")
        .expect("missing base is observable")
    else {
        panic!("expected invalidation")
    };
    assert_eq!(
        invalidation.reason,
        FlashblockInvalidationReason::MissingInitialIndex
    );
    assert_eq!(
        invalidation.payload_id, [0x11_u8; 8],
        "the subscriber can revoke the exact payload that was previously published"
    );
}

#[test]
fn rejected_replacement_frame_preserves_the_active_payload_for_explicit_reset() {
    let mut adapter = adapter("raw-json", 7);
    adapter.ingest_json(&index_zero()).expect("first delivery");
    let malformed_replacement = String::from_utf8(index_zero())
        .expect("fixture UTF-8")
        .replace("0x1111111111111111", "0x2222222222222222")
        .replace("\"block_number\":101", "\"block_number\":102")
        .into_bytes();

    assert!(adapter.ingest_json(&malformed_replacement).is_err());
    let FlashblockUpdate::Invalidated(invalidation) = adapter
        .reset(ProviderRef::new("raw-json", 8))
        .expect("valid source transition")
        .expect("the last published payload remains revocable")
    else {
        panic!("expected invalidation")
    };
    assert_eq!(invalidation.payload_id, [0x11_u8; 8]);
}

#[test]
fn duplicate_transaction_across_deltas_is_rejected_without_advancing_sequence() {
    let mut adapter = adapter("raw-json", 7);
    adapter.ingest_json(&index_zero()).expect("first delivery");
    let duplicate = String::from_utf8(index_one(TX_ONE))
        .expect("fixture UTF-8")
        .replace("\"transactions\":[\"0x02\"]", "\"transactions\":[\"0x01\"]")
        .into_bytes();
    assert!(
        adapter
            .ingest_json(&duplicate)
            .expect_err("duplicate cumulative member")
            .to_string()
            .contains("more than one indexed delta")
    );

    let FlashblockUpdate::Snapshot(corrected) = adapter
        .ingest_json(&index_one(TX_TWO))
        .expect("corrected index remains admissible")
        .expect("corrected index publishes")
    else {
        panic!("expected snapshot")
    };
    assert_eq!(corrected.flashblock.index, Some(1));
}

#[test]
fn duplicate_transaction_inside_one_delta_is_rejected_without_advancing_sequence() {
    let mut adapter = adapter("raw-json", 7);
    let duplicate = String::from_utf8(index_zero())
        .expect("fixture UTF-8")
        .replace(
            "\"transactions\":[\"0x01\"]",
            "\"transactions\":[\"0x01\",\"0x01\"]",
        )
        .into_bytes();

    assert!(
        adapter
            .ingest_json(&duplicate)
            .expect_err("duplicate transaction inside a delta")
            .to_string()
            .contains("duplicate hash")
    );
    assert!(matches!(
        adapter.ingest_json(&index_zero()).expect("corrected index"),
        Some(FlashblockUpdate::Snapshot(_))
    ));
}

#[test]
fn configured_frame_transaction_and_log_bounds_fail_closed() {
    let source = ProviderRef::new("raw-json", 1);
    let mut frame_limits = RawJsonFlashblocksLimits::default();
    frame_limits.max_frame_bytes = index_zero().len() - 1;
    let mut frame_limited = RawJsonFlashblocksAdapter::with_limits(source.clone(), frame_limits)
        .expect("valid frame limit");
    assert!(
        frame_limited
            .ingest_json(&index_zero())
            .expect_err("oversized frame")
            .to_string()
            .contains("byte limit")
    );

    let mut transaction_limits = RawJsonFlashblocksLimits::default();
    transaction_limits.max_transactions_per_payload = 1;
    let mut transaction_limited =
        RawJsonFlashblocksAdapter::with_limits(source.clone(), transaction_limits)
            .expect("valid transaction limit");
    transaction_limited
        .ingest_json(&index_zero())
        .expect("first transaction");
    assert!(
        transaction_limited
            .ingest_json(&index_one(TX_TWO))
            .expect_err("second cumulative transaction")
            .to_string()
            .contains("transaction count")
    );

    let mut log_limits = RawJsonFlashblocksLimits::default();
    log_limits.max_logs_per_payload = 1;
    let mut log_limited =
        RawJsonFlashblocksAdapter::with_limits(source, log_limits).expect("valid log limit");
    log_limited.ingest_json(&index_zero()).expect("first log");
    assert!(
        log_limited
            .ingest_json(&index_one(TX_TWO))
            .expect_err("second cumulative log")
            .to_string()
            .contains("log count")
    );
}

#[test]
fn receipt_logs_with_more_than_four_topics_are_rejected() {
    let mut adapter = adapter("raw-json", 7);
    let topics = format!(
        "\"topics\":[\"{0:#x}\",\"{1:#x}\",\"{2:#x}\",\"{3:#x}\",\"{4:#x}\"]",
        B256::repeat_byte(1),
        B256::repeat_byte(2),
        B256::repeat_byte(3),
        B256::repeat_byte(4),
        B256::repeat_byte(5),
    );
    let frame = String::from_utf8(index_zero())
        .expect("fixture UTF-8")
        .replace(
            "\"topics\":[\"0x4343434343434343434343434343434343434343434343434343434343434343\"]",
            &topics,
        )
        .into_bytes();
    assert!(
        adapter
            .ingest_json(&frame)
            .expect_err("too many topics")
            .to_string()
            .contains("more than four topics")
    );
}

proptest! {
    #[test]
    fn arbitrary_application_frames_never_panic_or_prevent_explicit_recovery(
        frame in proptest::collection::vec(any::<u8>(), 0..4096),
    ) {
        let mut adapter = adapter("raw-json", 1);
        let _ = adapter.ingest_json(&frame);
        let _ = adapter
            .reset(ProviderRef::new("raw-json", 2))
            .expect("valid source transition");
        prop_assert!(matches!(
            adapter.ingest_json(&index_zero()),
            Ok(Some(FlashblockUpdate::Snapshot(_)))
        ));
    }
}

#[tokio::test]
#[cfg(feature = "reactive-ws")]
async fn standardized_updates_enter_the_existing_preconfirmation_pipeline() {
    let asserter = Asserter::new();
    asserter.push_success(&U256::from(10));
    let provider = ProviderBuilder::new().connect_mocked_client(asserter.clone());
    let source = ProviderRef::new("raw-json", 7);
    let mut subscriber = evm_fork_cache::reactive::AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::PubSub,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Preferred,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    subscriber
        .register_interests(&[ReactiveInterest::Logs(LogInterest {
            provider_filter: Filter::new()
                .address(Address::repeat_byte(0x42))
                .event_signature(B256::repeat_byte(0x43)),
            local_matcher: None,
            route_key: None,
        })])
        .await
        .expect("register raw update interest");

    let mut adapter = RawJsonFlashblocksAdapter::new(source);
    let update = adapter
        .ingest_json(&index_zero())
        .expect("decode raw frame")
        .expect("publish raw frame");
    subscriber
        .ingest_flashblock_update(update)
        .expect("ingest standardized update");

    let batch = subscriber
        .next_scoped_batch()
        .await
        .expect("subscriber remains healthy")
        .expect("preconfirmed batch");
    assert_eq!(batch.records().len(), 1);
    assert!(batch.records()[0].scope().is_preconfirmed());
    assert!(matches!(batch.records()[0].input, ReactiveInput::Log(_)));
    assert!(matches!(
        batch.records()[0].context.chain_status,
        ChainStatus::Preconfirmed { ref flashblock }
            if flashblock.provider == ProviderRef::new("raw-json", 7)
    ));
    assert_eq!(subscriber.flashblocks_rpc_metrics().total_requests(), 0);

    let reset = adapter
        .reset(ProviderRef::new("raw-json", 8))
        .expect("valid source transition")
        .expect("active source reset invalidates");
    subscriber
        .ingest_flashblock_update(reset)
        .expect("ingest source reset");
    let invalidation = subscriber
        .next_scoped_batch()
        .await
        .expect("subscriber remains healthy")
        .expect("invalidation batch");
    assert!(invalidation.preconfirmation_invalidated());
    assert!(invalidation.records().is_empty());

    let next = adapter
        .ingest_json(&index_zero())
        .expect("next provider generation frame")
        .expect("next provider generation publishes");
    subscriber
        .ingest_flashblock_update(next)
        .expect("ingest next provider generation");
    let next_batch = subscriber
        .next_scoped_batch()
        .await
        .expect("subscriber remains healthy")
        .expect("replacement batch");
    assert!(matches!(
        next_batch.records()[0].context.chain_status,
        ChainStatus::Preconfirmed { ref flashblock }
            if flashblock.provider == ProviderRef::new("raw-json", 8)
    ));

    let mut stale = RawJsonFlashblocksAdapter::new(ProviderRef::new("raw-json", 7));
    subscriber
        .ingest_flashblock_update(
            stale
                .ingest_json(&index_zero())
                .expect("stale frame decodes")
                .expect("stale snapshot"),
        )
        .expect("stale snapshot is ignored");
    let stale_invalidation = stale
        .reset(ProviderRef::new("raw-json", 9))
        .expect("valid stale-source transition")
        .expect("stale source invalidation");
    subscriber
        .ingest_flashblock_update(stale_invalidation)
        .expect("stale invalidation is ignored");

    let current_invalidation = adapter
        .reset(ProviderRef::new("raw-json", 9))
        .expect("valid current-source transition")
        .expect("current generation invalidation");
    subscriber
        .ingest_flashblock_update(current_invalidation)
        .expect("current invalidation is accepted");
    let current_invalidation = subscriber
        .next_scoped_batch()
        .await
        .expect("subscriber remains healthy")
        .expect("current invalidation batch");
    assert!(current_invalidation.preconfirmation_invalidated());
    assert!(current_invalidation.records().is_empty());
}

#[tokio::test]
#[cfg(feature = "reactive-ws")]
async fn subscriber_rejects_tampered_or_wrong_source_snapshots_without_queue_mutation() {
    let asserter = Asserter::new();
    asserter.push_success(&U256::from(10));
    let provider = ProviderBuilder::new().connect_mocked_client(asserter);
    let source = ProviderRef::new("raw-json", 7);
    let mut subscriber = evm_fork_cache::reactive::AlloySubscriber::<_, Ethereum>::new(
        provider,
        SubscriberMode::PubSub,
        SubscriberConfig {
            preconfirmations: PreconfirmationMode::Required,
            ..SubscriberConfig::default()
        },
    );
    subscriber
        .configure_external_flashblock_updates(source.clone())
        .expect("configure external source");
    subscriber
        .register_interests(&[ReactiveInterest::Logs(LogInterest {
            provider_filter: Filter::new()
                .address(Address::repeat_byte(0x42))
                .event_signature(B256::repeat_byte(0x43)),
            local_matcher: None,
            route_key: None,
        })])
        .await
        .expect("register raw update interest");

    let mut adapter = RawJsonFlashblocksAdapter::new(source);
    let valid = adapter
        .ingest_json(&index_zero())
        .expect("decode raw frame")
        .expect("publish raw frame");
    let mut tampered = valid.clone();
    let FlashblockUpdate::Snapshot(snapshot) = &mut tampered else {
        panic!("expected snapshot")
    };
    snapshot.flashblock.content_hash = B256::repeat_byte(0xff);
    assert!(
        subscriber
            .ingest_flashblock_update(tampered)
            .expect_err("tampered commitment")
            .to_string()
            .contains("content commitment")
    );

    let wrong_source = RawJsonFlashblocksAdapter::new(ProviderRef::new("other-source", 7));
    let mut wrong_source = wrong_source;
    assert!(
        subscriber
            .ingest_flashblock_update(
                wrong_source
                    .ingest_json(&index_zero())
                    .expect("decode other source")
                    .expect("other source snapshot")
            )
            .expect_err("wrong source endpoint")
            .to_string()
            .contains("unexpected provider endpoint")
    );

    subscriber
        .ingest_flashblock_update(valid)
        .expect("valid snapshot remains admissible");
    let batch = subscriber
        .next_scoped_batch()
        .await
        .expect("subscriber remains healthy")
        .expect("only valid snapshot was queued");
    assert_eq!(batch.records().len(), 1);
    assert!(batch.records()[0].scope().is_preconfirmed());
}