ethexe-common 2.0.0-pre.1

Shared types and primitives for the ethexe execution layer
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
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

use crate::{
    Address, BlockData, BlockHeader, CodeBlobInfo, Digest, HashOf, ProgramStates,
    ProtocolTimelines, Rfm, Schedule, ScheduledTask, Sd, SimpleBlockData, StateHashWithQueueSize,
    Sum, ValidatorsVec,
    consensus::BatchCommitmentValidationRequest,
    db::*,
    events::BlockEvent,
    gear::{
        BatchCommitment, ChainCommitment, CodeCommitment, Message, MessageType, StateTransition,
    },
    injected::{InjectedTransaction, Promise},
    malachite::Operations,
};
use alloc::{collections::BTreeMap, vec};
use gear_core::{
    code::{CodeMetadata, InstrumentedCode},
    limited::LimitedVec,
    message::{ReplyCode, SuccessReplyReason},
    rpc::ReplyInfo,
    tasks::ScheduledTask as CoreScheduledTask,
};
use gprimitives::{ActorId, CodeId, H256, MessageId, ReservationId};
use itertools::Itertools;
use proptest::{
    arbitrary::Arbitrary,
    collection,
    prelude::{BoxedStrategy, Strategy, any},
    prop_oneof,
    strategy::{Just, ValueTree},
    test_runner::TestRunner,
};
use std::collections::VecDeque;
pub use tap::Tap;

fn arbitrary_value<T>(args: T::Parameters) -> T
where
    T: Arbitrary + 'static,
{
    T::arbitrary_with(args)
        .new_tree(&mut TestRunner::default())
        .expect("mock strategy must produce a value")
        .current()
}

pub trait Mock<Args = ()> {
    fn mock(args: Args) -> Self;
}

impl<T, Args> Mock<Args> for T
where
    T: Arbitrary + 'static,
    Args: Into<T::Parameters>,
{
    fn mock(args: Args) -> Self {
        arbitrary_value::<T>(args.into())
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct BlockHeaderParams {
    parent_hash: Option<H256>,
}

impl From<()> for BlockHeaderParams {
    fn from((): ()) -> Self {
        Self::default()
    }
}

impl From<H256> for BlockHeaderParams {
    fn from(parent_hash: H256) -> Self {
        Self {
            parent_hash: Some(parent_hash),
        }
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct ChainCommitmentParams {
    head: Option<H256>,
}

impl From<()> for ChainCommitmentParams {
    fn from((): ()) -> Self {
        Self::default()
    }
}

impl From<H256> for ChainCommitmentParams {
    fn from(head: H256) -> Self {
        Self { head: Some(head) }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BlockChainParams {
    len: u32,
    validators: ValidatorsVec,
}

impl From<u32> for BlockChainParams {
    fn from(len: u32) -> Self {
        Self {
            len,
            validators: Default::default(),
        }
    }
}

impl From<(u32, ValidatorsVec)> for BlockChainParams {
    fn from((len, validators): (u32, ValidatorsVec)) -> Self {
        Self { len, validators }
    }
}

fn h256_strategy() -> BoxedStrategy<H256> {
    any::<[u8; 32]>().prop_map(Into::into).boxed()
}

fn digest_strategy() -> BoxedStrategy<Digest> {
    any::<[u8; 32]>().prop_map(Digest).boxed()
}

fn address_strategy() -> BoxedStrategy<Address> {
    any::<[u8; 20]>().prop_map(Address).boxed()
}

fn actor_id_strategy() -> BoxedStrategy<ActorId> {
    h256_strategy().prop_map(Into::into).boxed()
}

fn code_id_strategy() -> BoxedStrategy<CodeId> {
    h256_strategy().prop_map(Into::into).boxed()
}

fn message_id_strategy() -> BoxedStrategy<MessageId> {
    h256_strategy().prop_map(Into::into).boxed()
}

fn reservation_id_strategy() -> BoxedStrategy<ReservationId> {
    any::<[u8; 32]>().prop_map(Into::into).boxed()
}

fn limited_bytes_strategy<const N: usize>(
    range: impl Into<collection::SizeRange>,
) -> BoxedStrategy<LimitedVec<u8, N>> {
    collection::vec(any::<u8>(), range)
        .prop_map(|bytes| {
            LimitedVec::try_from(bytes).expect("strategy range must fit within LimitedVec bound")
        })
        .boxed()
}

pub fn scheduled_task_strategy() -> BoxedStrategy<ScheduledTask> {
    prop_oneof![
        (
            actor_id_strategy(),
            actor_id_strategy(),
            message_id_strategy()
        )
            .prop_map(|(program_id, user_id, message_id)| {
                CoreScheduledTask::<Rfm, Sd, Sum>::RemoveFromMailbox(
                    (program_id, user_id),
                    message_id,
                )
            }),
        (actor_id_strategy(), message_id_strategy()).prop_map(|(program_id, message_id)| {
            CoreScheduledTask::<Rfm, Sd, Sum>::RemoveFromWaitlist(program_id, message_id)
        }),
        (actor_id_strategy(), message_id_strategy()).prop_map(|(program_id, message_id)| {
            CoreScheduledTask::<Rfm, Sd, Sum>::WakeMessage(program_id, message_id)
        }),
        (actor_id_strategy(), message_id_strategy()).prop_map(|(program_id, message_id)| {
            CoreScheduledTask::<Rfm, Sd, Sum>::SendDispatch((program_id, message_id))
        }),
        (message_id_strategy(), actor_id_strategy()).prop_map(|(message_id, to_mailbox)| {
            CoreScheduledTask::<Rfm, Sd, Sum>::SendUserMessage {
                message_id,
                to_mailbox,
            }
        }),
        (actor_id_strategy(), reservation_id_strategy()).prop_map(
            |(program_id, reservation_id)| {
                CoreScheduledTask::<Rfm, Sd, Sum>::RemoveGasReservation(program_id, reservation_id)
            }
        ),
    ]
    .boxed()
}

pub fn schedule_strategy() -> BoxedStrategy<Schedule> {
    collection::btree_map(
        any::<u32>(),
        collection::btree_set(scheduled_task_strategy(), 0..=4),
        0..=4,
    )
    .boxed()
}

impl Arbitrary for SimpleBlockData {
    type Parameters = BlockHeaderParams;
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
        (h256_strategy(), BlockHeader::arbitrary_with(args))
            .prop_map(|(hash, header)| Self { hash, header })
            .boxed()
    }
}

impl Arbitrary for BlockHeader {
    type Parameters = BlockHeaderParams;
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
        let parent_hash = match args.parent_hash {
            Some(parent_hash) => Just(parent_hash).boxed(),
            None => h256_strategy(),
        };

        parent_hash
            .prop_map(|parent_hash| Self {
                height: 43,
                timestamp: 120,
                parent_hash,
            })
            .boxed()
    }
}

impl Arbitrary for ProtocolTimelines {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        Just(Self {
            genesis_ts: 0,
            era: 1000.try_into().unwrap(),
            election: 200,
            slot: 10.try_into().unwrap(),
        })
        .boxed()
    }
}

impl Arbitrary for MessageType {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        prop_oneof![Just(Self::Canonical), Just(Self::Injected)].boxed()
    }
}

impl Arbitrary for StateHashWithQueueSize {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        (h256_strategy(), any::<u8>(), any::<u8>())
            .prop_map(|(hash, canonical_queue_size, injected_queue_size)| Self {
                hash,
                canonical_queue_size,
                injected_queue_size,
            })
            .boxed()
    }
}

impl Arbitrary for CodeCommitment {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        code_id_strategy()
            .prop_map(|id| Self { id, valid: true })
            .boxed()
    }
}

impl Arbitrary for ChainCommitment {
    type Parameters = ChainCommitmentParams;
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
        let head = match args.head {
            Some(head) => Just(head).boxed(),
            None => h256_strategy(),
        };

        (
            StateTransition::arbitrary_with(()),
            StateTransition::arbitrary_with(()),
            head,
        )
            .prop_map(|(first, second, head)| Self {
                transitions: vec![first, second],
                head,
                last_advanced_eth_block: H256::zero(),
            })
            .boxed()
    }
}

impl Arbitrary for BatchCommitment {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        (
            h256_strategy(),
            digest_strategy(),
            ChainCommitment::arbitrary_with(().into()),
            CodeCommitment::arbitrary_with(()),
            CodeCommitment::arbitrary_with(()),
        )
            .prop_map(
                |(
                    block_hash,
                    previous_batch,
                    chain_commitment,
                    code_commitment_1,
                    code_commitment_2,
                )| Self {
                    block_hash,
                    timestamp: 42,
                    previous_batch,
                    expiry: 10,
                    chain_commitment: Some(chain_commitment),
                    code_commitments: vec![code_commitment_1, code_commitment_2],
                    validators_commitment: None,
                    rewards_commitment: None,
                },
            )
            .boxed()
    }
}

impl Arbitrary for BatchCommitmentValidationRequest {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        (
            digest_strategy(),
            h256_strategy(),
            code_id_strategy(),
            code_id_strategy(),
        )
            .prop_map(|(digest, head, code_1, code_2)| Self {
                digest,
                head: Some(head),
                codes: vec![code_1, code_2],
                validators: false,
                rewards: false,
            })
            .boxed()
    }
}

impl Arbitrary for StateTransition {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        (
            actor_id_strategy(),
            h256_strategy(),
            actor_id_strategy(),
            message_id_strategy(),
            actor_id_strategy(),
        )
            .prop_map(
                |(actor_id, new_state_hash, inheritor, message_id, destination)| Self {
                    actor_id,
                    new_state_hash,
                    exited: false,
                    inheritor,
                    value_to_receive: 123,
                    value_to_receive_negative_sign: false,
                    value_claims: vec![],
                    messages: vec![Message {
                        id: message_id,
                        destination,
                        payload: b"Hello, World!".to_vec(),
                        value: 0,
                        reply_details: None,
                        call: false,
                    }],
                },
            )
            .boxed()
    }
}

impl Arbitrary for InjectedTransaction {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        limited_bytes_strategy::<32>(32..=32)
            .prop_map(|salt| Self {
                destination: Default::default(),
                payload: LimitedVec::new(),
                value: 0,
                reference_block: Default::default(),
                salt,
            })
            .boxed()
    }
}

impl Mock<()> for Promise {
    fn mock(_args: ()) -> Self {
        Promise::mock(HashOf::random())
    }
}

impl Mock<HashOf<InjectedTransaction>> for Promise {
    fn mock(tx_hash: HashOf<InjectedTransaction>) -> Self {
        Promise {
            tx_hash,
            reply: ReplyInfo {
                payload: H256::random().0.to_vec(),
                value: 42,
                code: ReplyCode::Success(SuccessReplyReason::Manual),
            },
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyncedBlockData {
    pub header: BlockHeader,
    pub events: Vec<BlockEvent>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PreparedBlockData {
    pub codes_queue: VecDeque<CodeId>,
    pub last_committed_batch: Digest,
    pub last_committed_mb: H256,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockFullData {
    pub hash: H256,
    pub synced: Option<SyncedBlockData>,
    pub prepared: Option<PreparedBlockData>,
}

impl BlockFullData {
    #[track_caller]
    pub fn as_synced(&self) -> &SyncedBlockData {
        self.synced.as_ref().expect("block not synced")
    }

    #[track_caller]
    pub fn as_synced_mut(&mut self) -> &mut SyncedBlockData {
        self.synced.as_mut().expect("block not synced")
    }

    #[track_caller]
    pub fn as_prepared(&self) -> &PreparedBlockData {
        self.prepared.as_ref().expect("block is not prepared")
    }

    #[track_caller]
    pub fn as_prepared_mut(&mut self) -> &mut PreparedBlockData {
        self.prepared.as_mut().expect("block is not prepared")
    }

    #[track_caller]
    pub fn to_simple(&self) -> SimpleBlockData {
        SimpleBlockData {
            hash: self.hash,
            header: self.as_synced().header,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InstrumentedCodeData {
    pub instrumented: InstrumentedCode,
    pub meta: CodeMetadata,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodeData {
    pub original_bytes: Vec<u8>,
    pub blob_info: CodeBlobInfo,
    pub instrumented: Option<InstrumentedCodeData>,
}

impl CodeData {
    pub fn as_instrumented(&self) -> &InstrumentedCodeData {
        self.instrumented.as_ref().expect("code not instrumented")
    }

    pub fn as_instrumented_mut(&mut self) -> &mut InstrumentedCodeData {
        self.instrumented.as_mut().expect("code not instrumented")
    }
}

/// Computed-side payload for an [`MbFullData`]. `None` on
/// [`MbFullData::computed`] means `mb_meta.computed = false` and
/// `mb_program_states` is left unwritten.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct MockComputedMbData {
    pub program_states: ProgramStates,
}

/// One MB entry in the [`BlockChain`] mock. Paralleled with
/// [`BlockChain::blocks`]: `mbs[i]` corresponds to `blocks[i]`. The
/// first entry (`mbs[0]`) is a sentinel with `hash = H256::zero()`
/// (mirrors the `blocks[0]` genesis-parent placeholder) and is not
/// written to the DB.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MbFullData {
    pub hash: H256,
    /// Parent MB hash. `H256::zero()` for the very first real MB.
    pub parent: H256,
    /// MB height. Set to the block index `i` so it monotonically
    /// matches [`BlockChain::blocks`].
    pub height: u64,
    /// Computed-side data. `Some(default)` by default; setting to
    /// `None` skips writing `mb_program_states` and `mb_meta.computed`.
    pub computed: Option<MockComputedMbData>,
    /// SCALE-encoded operations blob to write under this MB.
    /// Defaults to an empty list. Tests that need specific operations in
    /// the dedup-window walk (e.g. tx_validity::Duplicate) can set this.
    pub operations: Operations,
}

impl MbFullData {
    #[track_caller]
    pub fn as_computed(&self) -> &MockComputedMbData {
        self.computed
            .as_ref()
            .expect("MB not marked computed in this mock chain")
    }

    #[track_caller]
    pub fn as_computed_mut(&mut self) -> &mut MockComputedMbData {
        self.computed
            .as_mut()
            .expect("MB not marked computed in this mock chain")
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockChain {
    pub blocks: VecDeque<BlockFullData>,
    /// One MB per `blocks[i]`. `mbs[0]` is a sentinel — see [`MbFullData`].
    pub mbs: VecDeque<MbFullData>,
    pub codes: BTreeMap<CodeId, CodeData>,
    pub validators: ValidatorsVec,
    pub config: DBConfig,
    pub globals: DBGlobals,
}

impl BlockChain {
    /// `mbs[idx]` accessor. Panics on out-of-range — mirrors the
    /// existing direct field access for `blocks[idx]`.
    #[track_caller]
    pub fn mb_at(&self, idx: usize) -> &MbFullData {
        &self.mbs[idx]
    }

    #[track_caller]
    pub fn mb_at_mut(&mut self, idx: usize) -> &mut MbFullData {
        &mut self.mbs[idx]
    }

    /// Convenience for the common `mbs[idx].hash` pattern.
    #[track_caller]
    pub fn mb_hash_at(&self, idx: usize) -> H256 {
        self.mbs[idx].hash
    }
}

/// Seed the zero MB as a computed genesis ancestor, mirroring
/// `initialize_empty_db`. The malachite genesis block (height 1) points at
/// the zero hash as its parent, and the compute / tx-validity pipelines read
/// that parent as a normal computed record — empty `program_states` and
/// `schedule`, `computed = true`, `last_advanced_eb = zero`. Every test DB
/// that exercises those pipelines must carry it.
pub fn seed_genesis_zero_mb<DB>(db: &DB)
where
    DB: MbStorageRW,
{
    let operations_hash = db.set_operations(Operations::new(vec![]));
    db.set_mb_compact_block(
        H256::zero(),
        CompactMb {
            parent: H256::zero(),
            height: 0,
            operations_hash,
        },
    );
    db.set_mb_program_states(H256::zero(), Default::default());
    db.set_mb_schedule(H256::zero(), Default::default());
    db.set_mb_outcome(H256::zero(), Vec::new());
    db.mutate_mb_meta(H256::zero(), |m| {
        m.computed = true;
        m.last_advanced_eb = H256::zero();
    });
}

impl BlockChain {
    #[track_caller]
    pub fn setup<DB>(self, db: &DB) -> Self
    where
        DB: BlockMetaStorageRW
            + OnChainStorageRW
            + CodesStorageRW
            + MbStorageRW
            + SetConfig
            + SetGlobals,
    {
        let BlockChain {
            blocks,
            mbs,
            codes,
            validators,
            config,
            globals,
        } = self.clone();

        db.set_config(config.clone());
        db.set_globals(globals);

        // The zero MB is the genesis block's parent; seed it as a computed
        // ancestor exactly as `initialize_empty_db` does in production.
        seed_genesis_zero_mb(db);

        // Write MB rows in chronological order. Skip the index-0
        // sentinel (zero hash). Empty-operations MBs share one CAS
        // entry naturally — `set_operations` is content-addressed.
        for mb in &mbs {
            if mb.hash == H256::zero() {
                continue;
            }
            let operations_hash = db.set_operations(mb.operations.clone());
            db.set_mb_compact_block(
                mb.hash,
                CompactMb {
                    parent: mb.parent,
                    height: mb.height,
                    operations_hash,
                },
            );
            if let Some(computed) = &mb.computed {
                db.set_mb_program_states(mb.hash, computed.program_states.clone());
                db.mutate_mb_meta(mb.hash, |meta| {
                    meta.computed = true;
                });
            }
        }

        for BlockFullData {
            hash,
            synced,
            prepared,
        } in blocks
        {
            if let Some(SyncedBlockData { header, events }) = synced {
                db.set_block_header(hash, header);
                db.set_block_events(hash, &events);
                db.set_block_synced(hash);

                let block_era = config.timelines.era_from_ts(header.timestamp).unwrap();
                db.set_validators(block_era, validators.clone());
                db.mutate_block_meta(hash, |meta| {
                    meta.latest_era_validators_committed = Some(block_era)
                });
            }

            if let Some(PreparedBlockData {
                codes_queue,
                last_committed_batch,
                last_committed_mb,
            }) = prepared
            {
                db.mutate_block_meta(hash, |meta| {
                    *meta = BlockMeta {
                        prepared: true,
                        codes_queue: Some(codes_queue),
                        last_committed_batch: Some(last_committed_batch),
                        last_committed_mb: Some(last_committed_mb),
                        ..*meta
                    };
                });
            }
        }

        for (
            code_id,
            CodeData {
                original_bytes,
                blob_info,
                instrumented,
            },
        ) in codes
        {
            db.set_original_code(&original_bytes);

            if let Some(InstrumentedCodeData { instrumented, meta }) = instrumented {
                db.set_instrumented_code(1, code_id, instrumented);
                db.set_code_metadata(code_id, meta);
                db.set_code_blob_info(code_id, blob_info);
                db.set_code_valid(code_id, true);
            }
        }

        self
    }

    fn with_params(params: BlockChainParams, router_address: Address) -> Self {
        let BlockChainParams { len, validators } = params;
        let slot = 10;
        let genesis_height = 1_000_000;
        let genesis_ts = 1_000_000;

        // i = 0, h = None - genesis parent
        // i = 1, h = 0 - genesis
        // i = 2, h = 1 - first block
        // ...
        // i = len + 1, h = len - last block
        let blocks: VecDeque<_> = (0..len + 2)
            .map(|i| {
                if let Some(h) = i.checked_sub(1) {
                    // Human readable blocks, to avoid zero values append some readable numbers
                    let hash = H256::from_low_u64_be(h as u64).tap_mut(|hash| hash.0[0] = 0x10);
                    let height = genesis_height + h;
                    let timestamp = genesis_ts + h * slot as u32;
                    (hash, height, timestamp)
                } else {
                    (H256([u8::MAX; 32]), 0, 0)
                }
            })
            .tuple_windows()
            .map(
                |((parent_hash, _, _), (block_hash, block_height, block_timestamp))| {
                    BlockFullData {
                        hash: block_hash,
                        synced: Some(SyncedBlockData {
                            header: BlockHeader {
                                height: block_height,
                                timestamp: block_timestamp as u64,
                                parent_hash,
                            },
                            events: Default::default(),
                        }),
                        prepared: Some(PreparedBlockData {
                            codes_queue: Default::default(),
                            last_committed_batch: Digest::zero(),
                            last_committed_mb: H256::zero(),
                        }),
                    }
                },
            )
            .collect();

        let config = DBConfig {
            version: 0,
            chain_id: 0,
            router_address,
            timelines: ProtocolTimelines {
                genesis_ts: genesis_ts as u64,
                era: (slot * 100).try_into().unwrap(),
                election: slot * 20,
                slot: slot.try_into().unwrap(),
            },
            genesis_block_hash: blocks[0].hash,
            max_validators: 10,
        };

        // Build a parallel MB chain. `mbs[0]` is a sentinel matching
        // the `blocks[0]` genesis-parent placeholder; subsequent MBs
        // link parent-to-parent in chronological order.
        let mut mbs: VecDeque<MbFullData> = VecDeque::with_capacity(blocks.len());
        let mut prev_mb_hash = H256::zero();
        for i in 0..blocks.len() {
            if i == 0 {
                mbs.push_back(MbFullData {
                    hash: H256::zero(),
                    parent: H256::zero(),
                    height: 0,
                    computed: None,
                    operations: Operations::new(vec![]),
                });
                continue;
            }
            // Synthetic but stable, non-zero hash distinct from block hashes.
            let mut hb = [0u8; 32];
            hb[0] = 0xCD;
            hb[1..9].copy_from_slice(&(i as u64).to_be_bytes());
            let hash = H256::from(hb);
            mbs.push_back(MbFullData {
                hash,
                parent: prev_mb_hash,
                height: i as u64,
                computed: Some(MockComputedMbData::default()),
                operations: Operations::new(vec![]),
            });
            prev_mb_hash = hash;
        }

        // NOTE: `latest_{finalized,computed}_mb_hash` default to zero
        // so existing tests that only set up blocks (and not an MB
        // chain) keep their old "no MBs committed yet" semantics.
        // Tests that exercise the MB pipeline must explicitly
        // `tap_mut(|c| c.globals.latest_computed_mb_hash = c.mb_hash_at(N))`.
        let globals = DBGlobals {
            start_block_hash: blocks[0].hash,
            latest_synced_eb: blocks.back().unwrap().to_simple(),
            latest_prepared_eb_hash: blocks.back().unwrap().hash,
            latest_finalized_mb_hash: H256::zero(),
            latest_computed_mb_hash: H256::zero(),
        };

        Self {
            blocks,
            mbs,
            codes: Default::default(),
            validators,
            config,
            globals,
        }
    }
}

impl Arbitrary for BlockChain {
    type Parameters = BlockChainParams;
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
        address_strategy()
            .prop_map(move |router_address| Self::with_params(args.clone(), router_address))
            .boxed()
    }
}

impl SimpleBlockData {
    pub fn setup<DB>(self, db: &DB) -> Self
    where
        DB: OnChainStorageRW,
    {
        db.set_block_header(self.hash, self.header);
        db.set_block_events(self.hash, &[]);
        db.set_block_synced(self.hash);
        self
    }

    pub fn next_block(self) -> Self {
        Self {
            hash: H256::from_low_u64_be(self.hash.to_low_u64_be() + 1),
            header: BlockHeader {
                height: self.header.height + 1,
                parent_hash: self.hash,
                timestamp: self.header.timestamp + 10,
            },
        }
    }
}

impl BlockData {
    pub fn setup<DB>(self, db: &DB) -> Self
    where
        DB: OnChainStorageRW,
    {
        db.set_block_header(self.hash, self.header);
        db.set_block_events(self.hash, &self.events);
        db.set_block_synced(self.hash);
        self
    }
}

impl Arbitrary for DBConfig {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        (ProtocolTimelines::arbitrary_with(()), h256_strategy())
            .prop_map(|(timelines, genesis_block_hash)| Self {
                version: 0,
                chain_id: 0,
                router_address: Address::default(),
                timelines,
                genesis_block_hash,
                max_validators: 0,
            })
            .boxed()
    }
}

impl Arbitrary for DBGlobals {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        (
            h256_strategy(),
            SimpleBlockData::arbitrary_with(().into()),
            h256_strategy(),
            h256_strategy(),
            h256_strategy(),
        )
            .prop_map(
                |(
                    start_block_hash,
                    latest_synced_eb,
                    latest_prepared_eb_hash,
                    latest_finalized_mb_hash,
                    latest_computed_mb_hash,
                )| Self {
                    start_block_hash,
                    latest_synced_eb,
                    latest_prepared_eb_hash,
                    latest_finalized_mb_hash,
                    latest_computed_mb_hash,
                },
            )
            .boxed()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn injected_transaction_mock_produces_distinct_hashes() {
        let tx_hashes: std::collections::BTreeSet<_> = (0..8)
            .map(|_| InjectedTransaction::mock(()).to_hash())
            .collect();

        assert_eq!(tx_hashes.len(), 8);
    }
}