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
1291
1292
1293
1294
1295
1296
1297
1298
//! # Executive Module
//!
//! The executive is the main orchestrator for the entire runtime.
//! It has functions that implement the Core, BlockBuilder, and TxPool runtime APIs.
//!
//! It does all the reusable verification of UTXO transactions such as checking that there
//! are no duplicate inputs, and that the verifiers are satisfied.

use crate::{
    constraint_checker::ConstraintChecker,
    dynamic_typing::DynamicallyTypedData,
    ensure,
    inherents::PARENT_INHERENT_IDENTIFIER,
    types::{
        Block, BlockNumber, DispatchResult, Header, OutputRef, RedemptionStrategy, Transaction,
        UtxoError,
    },
    utxo_set::TransparentUtxoSet,
    verifier::Verifier,
    EXTRINSIC_KEY, HEADER_KEY, HEIGHT_KEY, LOG_TARGET,
};
use log::debug;
use parity_scale_codec::{Decode, Encode};
use sp_core::H256;
use sp_inherents::{CheckInherentsResult, InherentData};
use sp_runtime::{
    traits::{BlakeTwo256, Block as BlockT, Extrinsic, Hash as HashT, Header as HeaderT},
    transaction_validity::{
        InvalidTransaction, TransactionLongevity, TransactionSource, TransactionValidity,
        TransactionValidityError, ValidTransaction,
    },
    ApplyExtrinsicResult, ExtrinsicInclusionMode, StateVersion,
};
use sp_std::marker::PhantomData;
use sp_std::{collections::btree_set::BTreeSet, vec::Vec};

/// The executive. Each runtime is encouraged to make a type alias called `Executive` that fills
/// in the proper generic types.
pub struct Executive<V, C>(PhantomData<(V, C)>);

impl<V, C> Executive<V, C>
where
    V: Verifier,
    C: ConstraintChecker,
    Block<V, C>: BlockT<Extrinsic = Transaction<V, C>, Hash = sp_core::H256>,
    Transaction<V, C>: Extrinsic,
{
    /// Does pool-style validation of a tuxedo transaction.
    /// Does not commit anything to storage.
    /// This returns Ok even if some inputs are still missing because the tagged transaction pool can handle that.
    /// We later check that there are no missing inputs in `apply_tuxedo_transaction`
    pub fn validate_tuxedo_transaction(
        transaction: &Transaction<V, C>,
    ) -> Result<ValidTransaction, UtxoError<C::Error>> {
        debug!(
            target: LOG_TARGET,
            "validating tuxedo transaction",
        );

        // Make sure there are no duplicate inputs
        // Duplicate peeks are allowed, although they are inefficient and wallets should not create such transactions
        {
            let input_set: BTreeSet<_> = transaction.inputs.iter().map(|o| o.encode()).collect();
            ensure!(
                input_set.len() == transaction.inputs.len(),
                UtxoError::DuplicateInput
            );
        }

        // Build the stripped transaction (with the redeemers stripped) and encode it
        // This will be passed to the verifiers
        let mut stripped = transaction.clone();
        for input in stripped.inputs.iter_mut() {
            input.redeemer = Default::default();
        }
        let stripped_encoded = stripped.encode();

        // Check that the verifiers of all inputs are satisfied
        // Keep a Vec of the input data for passing to the constraint checker
        // Keep track of any missing inputs for use in the tagged transaction pool
        let mut input_data = Vec::new();
        let mut evicted_input_data = Vec::new();
        let mut missing_inputs = Vec::new();
        for input in transaction.inputs.iter() {
            if let Some(input_utxo) = TransparentUtxoSet::<V>::peek_utxo(&input.output_ref) {
                match input.redeemer {
                    RedemptionStrategy::Redemption(ref redeemer) => {
                        let redeemer = V::Redeemer::decode(&mut &redeemer[..])
                            .map_err(|_| UtxoError::VerifierError)?;
                        ensure!(
                            input_utxo.verifier.verify(
                                &stripped_encoded,
                                Self::block_height(),
                                &redeemer
                            ),
                            UtxoError::VerifierError
                        );
                        input_data.push(input_utxo.payload);
                    }
                    RedemptionStrategy::Eviction => evicted_input_data.push(input_utxo.payload),
                }
            } else {
                missing_inputs.push(input.output_ref.clone().encode());
            }
        }

        // Make a Vec of the peek data for passing to the constraint checker
        // Keep track of any missing peeks for use in the tagged transaction pool
        // Use the same vec as previously to keep track of missing peeks
        let mut peek_data = Vec::new();
        for output_ref in transaction.peeks.iter() {
            if let Some(peek_utxo) = TransparentUtxoSet::<V>::peek_utxo(output_ref) {
                peek_data.push(peek_utxo.payload);
            } else {
                missing_inputs.push(output_ref.encode());
            }
        }

        // Make sure no outputs already exist in storage
        let tx_hash = BlakeTwo256::hash_of(&transaction.encode());
        for index in 0..transaction.outputs.len() {
            let output_ref = OutputRef {
                tx_hash,
                index: index as u32,
            };

            debug!(
                target: LOG_TARGET,
                "Checking for pre-existing output {:?}", output_ref
            );

            ensure!(
                TransparentUtxoSet::<V>::peek_utxo(&output_ref).is_none(),
                UtxoError::PreExistingOutput
            );
        }

        // Calculate the tx-pool tags provided by this transaction, which
        // are just the encoded OutputRefs
        let provides = (0..transaction.outputs.len())
            .map(|i| {
                let output_ref = OutputRef {
                    tx_hash,
                    index: i as u32,
                };
                output_ref.encode()
            })
            .collect::<Vec<_>>();

        // If any of the inputs are missing, we cannot make any more progress
        // If they are all present, we may proceed to call the constraint checker
        if !missing_inputs.is_empty() {
            debug!(
                target: LOG_TARGET,
                "Transaction is valid but still has missing inputs. Returning early.",
            );
            return Ok(ValidTransaction {
                requires: missing_inputs,
                provides,
                priority: 0,
                longevity: TransactionLongevity::MAX,
                propagate: true,
            });
        }

        // Extract the payload data from each output
        let output_data: Vec<DynamicallyTypedData> = transaction
            .outputs
            .iter()
            .map(|o| o.payload.clone())
            .collect();

        // Call the constraint checker
        transaction
            .checker
            .check(&input_data, &evicted_input_data, &peek_data, &output_data)
            .map_err(UtxoError::ConstraintCheckerError)?;

        // Return the valid transaction
        Ok(ValidTransaction {
            requires: Vec::new(),
            provides,
            priority: 0,
            longevity: TransactionLongevity::MAX,
            propagate: true,
        })
    }

    /// Does full verification and application of tuxedo transactions.
    /// Most of the validation happens in the call to `validate_tuxedo_transaction`.
    /// Once those checks are done we make sure there are no missing inputs and then update storage.
    pub fn apply_tuxedo_transaction(transaction: Transaction<V, C>) -> DispatchResult<C::Error> {
        debug!(
            target: LOG_TARGET,
            "applying tuxedo transaction {:?}", transaction
        );

        // Re-do the pre-checks. These should have been done in the pool, but we can't
        // guarantee that foreign nodes to these checks faithfully, so we need to check on-chain.
        let valid_transaction = Self::validate_tuxedo_transaction(&transaction)?;

        // If there are still missing inputs, we cannot execute this,
        // although it would be valid in the pool
        ensure!(
            valid_transaction.requires.is_empty(),
            UtxoError::MissingInput
        );

        // At this point, all validation is complete, so we can commit the storage changes.
        Self::update_storage(transaction);

        Ok(())
    }

    /// Helper function to update the utxo set according to the given transaction.
    /// This function does absolutely no validation. It assumes that the transaction
    /// has already passed validation. Changes proposed by the transaction are written
    /// blindly to storage.
    fn update_storage(transaction: Transaction<V, C>) {
        // Remove verified UTXOs
        for input in &transaction.inputs {
            TransparentUtxoSet::<V>::consume_utxo(&input.output_ref);
        }

        debug!(
            target: LOG_TARGET,
            "Transaction before updating storage {:?}", transaction
        );
        // Write the newly created utxos
        for (index, output) in transaction.outputs.iter().enumerate() {
            let output_ref = OutputRef {
                tx_hash: BlakeTwo256::hash_of(&transaction.encode()),
                index: index as u32,
            };
            TransparentUtxoSet::<V>::store_utxo(output_ref, output);
        }
    }

    /// A helper function that allows tuxedo runtimes to read the current block height
    pub fn block_height() -> BlockNumber {
        sp_io::storage::get(HEIGHT_KEY)
            .and_then(|d| BlockNumber::decode(&mut &*d).ok())
            .expect("A height is stored at the beginning of block one and never cleared.")
    }

    // These next three methods are for the block authoring workflow.
    // Open the block, apply zero or more extrinsics, close the block

    pub fn open_block(header: &Header) -> ExtrinsicInclusionMode {
        debug!(
            target: LOG_TARGET,
            "Entering initialize_block. header: {:?}", header
        );

        // Store the transient partial header for updating at the end of the block.
        // This will be removed from storage before the end of the block.
        sp_io::storage::set(HEADER_KEY, &header.encode());

        // Also store the height persistently so it is available when
        // performing pool validations and other off-chain runtime calls.
        sp_io::storage::set(HEIGHT_KEY, &header.number().encode());

        // Tuxedo blocks always allow user transactions.
        ExtrinsicInclusionMode::AllExtrinsics
    }

    pub fn apply_extrinsic(extrinsic: Transaction<V, C>) -> ApplyExtrinsicResult {
        debug!(
            target: LOG_TARGET,
            "Entering apply_extrinsic: {:?}", extrinsic
        );

        // Append the current extrinsic to the transient list of extrinsics.
        // This will be used when we calculate the extrinsics root at the end of the block.
        let mut extrinsics = sp_io::storage::get(EXTRINSIC_KEY)
            .and_then(|d| <Vec<Vec<u8>>>::decode(&mut &*d).ok())
            .unwrap_or_default();
        extrinsics.push(extrinsic.encode());
        sp_io::storage::set(EXTRINSIC_KEY, &extrinsics.encode());

        // Now actually apply the extrinsic
        Self::apply_tuxedo_transaction(extrinsic).map_err(|e| {
            log::warn!(
                target: LOG_TARGET,
                "Tuxedo Transaction did not apply successfully: {:?}",
                e,
            );
            TransactionValidityError::Invalid(e.into())
        })?;

        Ok(Ok(()))
    }

    pub fn close_block() -> Header {
        let mut header = sp_io::storage::get(HEADER_KEY)
            .and_then(|d| Header::decode(&mut &*d).ok())
            .expect("We initialized with header, it never got mutated, qed");

        // the header itself contains the state root, so it cannot be inside the state (circular
        // dependency..). Make sure in execute block path we have the same rule.
        sp_io::storage::clear(HEADER_KEY);

        let extrinsics = sp_io::storage::get(EXTRINSIC_KEY)
            .and_then(|d| <Vec<Vec<u8>>>::decode(&mut &*d).ok())
            .unwrap_or_default();
        let extrinsics_root =
            <Header as HeaderT>::Hashing::ordered_trie_root(extrinsics, StateVersion::V0);
        sp_io::storage::clear(EXTRINSIC_KEY);
        header.set_extrinsics_root(extrinsics_root);

        let raw_state_root = &sp_io::storage::root(StateVersion::V1)[..];
        let state_root = <Header as HeaderT>::Hash::decode(&mut &raw_state_root[..]).unwrap();
        header.set_state_root(state_root);

        debug!(target: LOG_TARGET, "finalizing block {:?}", header);
        header
    }

    // This one is for the Core api. It is used to import blocks authored by foreign nodes.

    pub fn execute_block(block: Block<V, C>) {
        debug!(
            target: LOG_TARGET,
            "Entering execute_block. block: {:?}", block
        );

        // Store the header. Although we don't need to mutate it, we do need to make
        // info, such as the block height, available to individual pieces. This will
        // be cleared before the end of the block
        sp_io::storage::set(HEADER_KEY, &block.header().encode());

        // Also store the height persistently so it is available when
        // performing pool validations and other off-chain runtime calls.
        sp_io::storage::set(HEIGHT_KEY, &block.header().number().encode());

        // Tuxedo requires that inherents are at the beginning (and soon end) of the
        // block and not scattered throughout. We use this flag to enforce that.
        let mut finished_with_opening_inherents = false;

        // Apply each extrinsic
        for extrinsic in block.extrinsics() {
            // Enforce that inherents are in the right place
            let current_tx_is_inherent = extrinsic.checker.is_inherent();
            if current_tx_is_inherent && finished_with_opening_inherents {
                panic!("Tried to execute opening inherent after switching to non-inherents.");
            }
            if !current_tx_is_inherent && !finished_with_opening_inherents {
                // This is the first non-inherent, so we update our flag and continue.
                finished_with_opening_inherents = true;
            }

            match Self::apply_tuxedo_transaction(extrinsic.clone()) {
                Ok(()) => debug!(
                    target: LOG_TARGET,
                    "Successfully executed extrinsic: {:?}", extrinsic
                ),
                Err(e) => panic!("{:?}", e),
            }
        }

        // Clear the transient header out of storage
        sp_io::storage::clear(HEADER_KEY);

        // Check state root
        let raw_state_root = &sp_io::storage::root(StateVersion::V1)[..];
        let state_root = <Header as HeaderT>::Hash::decode(&mut &raw_state_root[..]).unwrap();
        assert_eq!(
            *block.header().state_root(),
            state_root,
            "state root mismatch"
        );

        // Check extrinsics root.
        let extrinsics = block
            .extrinsics()
            .iter()
            .map(|x| x.encode())
            .collect::<Vec<_>>();
        let extrinsics_root =
            <Header as HeaderT>::Hashing::ordered_trie_root(extrinsics, StateVersion::V0);
        assert_eq!(
            *block.header().extrinsics_root(),
            extrinsics_root,
            "extrinsics root mismatch"
        );
    }

    // This one is the pool api. It is used to make preliminary checks in the transaction pool

    pub fn validate_transaction(
        source: TransactionSource,
        tx: Transaction<V, C>,
        block_hash: <Block<V, C> as BlockT>::Hash,
    ) -> TransactionValidity {
        debug!(
            target: LOG_TARGET,
            "Entering validate_transaction. source: {:?}, tx: {:?}, block hash: {:?}",
            source,
            tx,
            block_hash
        );

        // Inherents are not permitted in the pool. They only come from the block author.
        // We perform this check here rather than in the `validate_tuxedo_transaction` helper,
        // because that helper is called again during on-chain execution. Inherents are valid
        // during execution, so we do not want this check repeated.
        let r = if tx.checker.is_inherent() {
            Err(TransactionValidityError::Invalid(InvalidTransaction::Call))
        } else {
            Self::validate_tuxedo_transaction(&tx).map_err(|e| {
                log::warn!(
                    target: LOG_TARGET,
                    "Tuxedo Transaction did not validate (in the pool): {:?}",
                    e,
                );
                TransactionValidityError::Invalid(e.into())
            })
        };

        debug!(target: LOG_TARGET, "Validation result: {:?}", r);

        r
    }

    // The next two are for the standard beginning-of-block inherent extrinsics.
    pub fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<Transaction<V, C>> {
        debug!(
            target: LOG_TARGET,
            "Entering `inherent_extrinsics`."
        );

        // Extract the complete parent block from the inherent data
        let parent: Block<V, C> = data
            .get_data(&PARENT_INHERENT_IDENTIFIER)
            .expect("Parent block inherent data should be able to decode.")
            .expect("Parent block should be present among authoring inherent data.");

        // Extract the inherents from the previous block, which can be found at the beginning of the extrinsics list.
        // The parent is already imported, so we know it is valid and we know its inherents came first.
        // We also annotate each transaction with its original hash for purposes of constructing output refs later.
        // This is necessary because the transaction hash changes as we unwrap layers of aggregation,
        // and we need an original universal transaction id.
        let previous_blocks_inherents: Vec<(Transaction<V, C>, H256)> = parent
            .extrinsics()
            .iter()
            .cloned()
            .take_while(|tx| tx.checker.is_inherent())
            .map(|tx| {
                let id = BlakeTwo256::hash_of(&tx.encode());
                (tx, id)
            })
            .collect();

        debug!(
            target: LOG_TARGET,
            "The previous block had {} extrinsics ({} inherents).", parent.extrinsics().len(), previous_blocks_inherents.len()
        );

        // Call into constraint checker's own inherent hooks to create the actual transactions
        C::create_inherents(&data, previous_blocks_inherents)
    }

    pub fn check_inherents(
        block: Block<V, C>,
        data: InherentData,
    ) -> sp_inherents::CheckInherentsResult {
        debug!(
            target: LOG_TARGET,
            "Entering `check_inherents`"
        );

        let mut result = CheckInherentsResult::new();

        // Tuxedo requires that all inherents come at the beginning of the block.
        // (Soon we will also allow them at the end, but never throughout the body.)
        // (TODO revise this logic once that is implemented.)
        // At this off-chain pre-check stage, we assume that requirement is upheld.
        // It will be verified later once we are executing on-chain.
        let inherents: Vec<Transaction<V, C>> = block
            .extrinsics()
            .iter()
            .cloned()
            .take_while(|tx| tx.checker.is_inherent())
            .collect();

        C::check_inherents::<V>(&data, inherents, &mut result);

        result
    }
}

#[cfg(test)]
mod tests {
    use sp_core::H256;
    use sp_io::TestExternalities;
    use sp_runtime::{generic::Header, transaction_validity::ValidTransactionBuilder};

    use crate::{
        constraint_checker::testing::TestConstraintChecker,
        dynamic_typing::{testing::Bogus, UtxoData},
        types::{Input, Output},
        verifier::TestVerifier,
    };

    use super::*;

    type TestTransaction = Transaction<TestVerifier, TestConstraintChecker>;
    pub type TestHeader = sp_runtime::generic::Header<u32, BlakeTwo256>;
    pub type TestBlock = sp_runtime::generic::Block<TestHeader, TestTransaction>;
    pub type TestExecutive = Executive<TestVerifier, TestConstraintChecker>;

    /// Construct a mock OutputRef from a transaction number and index in that transaction.
    ///
    /// When setting up tests, it is often useful to have some Utxos in the storage
    /// before the test begins. There are no real transactions before the test, so there
    /// are also no real OutputRefs. This function constructs an OutputRef that can be
    /// used in the test from a "transaction number" (a simple u32) and an output index in
    /// that transaction (also a u32).
    fn mock_output_ref(tx_num: u32, index: u32) -> OutputRef {
        OutputRef {
            tx_hash: H256::from_low_u64_le(tx_num as u64),
            index,
        }
    }

    /// Builder pattern for test transactions.
    #[derive(Default)]
    struct TestTransactionBuilder {
        inputs: Vec<Input>,
        peeks: Vec<OutputRef>,
        outputs: Vec<Output<TestVerifier>>,
    }

    impl TestTransactionBuilder {
        fn with_input(mut self, input: Input) -> Self {
            self.inputs.push(input);
            self
        }

        fn with_peek(mut self, peek: OutputRef) -> Self {
            self.peeks.push(peek);
            self
        }

        fn with_output(mut self, output: Output<TestVerifier>) -> Self {
            self.outputs.push(output);
            self
        }

        fn build(self, checks: bool, inherent: bool) -> TestTransaction {
            TestTransaction {
                inputs: self.inputs,
                peeks: self.peeks,
                outputs: self.outputs,
                checker: TestConstraintChecker { checks, inherent },
            }
        }
    }

    /// Builds test externalities using a minimal builder pattern.
    #[derive(Default)]
    struct ExternalityBuilder {
        utxos: Vec<(OutputRef, Output<TestVerifier>)>,
        pre_header: Option<TestHeader>,
        noted_extrinsics: Vec<Vec<u8>>,
    }

    impl ExternalityBuilder {
        /// Add the given Utxo to the storage.
        ///
        /// There are no real transactions to calculate OutputRefs so instead we
        /// provide an output ref as a parameter. See the function `mock_output_ref`
        /// for a convenient way to construct testing output refs.
        ///
        /// For the Outputs themselves, this function accepts payloads of any type that
        /// can be represented as DynamicallyTypedData, and a boolean about whether the
        /// verifier should succeed or not.
        fn with_utxo<T: UtxoData>(
            mut self,
            output_ref: OutputRef,
            payload: T,
            verifies: bool,
        ) -> Self {
            let output = Output {
                payload: payload.into(),
                verifier: TestVerifier { verifies },
            };
            self.utxos.push((output_ref, output));
            self
        }

        /// Add a preheader to the storage.
        ///
        /// In normal execution `open_block` stores a header in storage
        /// before any extrinsics are applied. This function allows setting up
        /// a test case with a stored pre-header.
        ///
        /// Rather than passing in a header, we pass in parts of it. This ensures
        /// that a realistic pre-header (without extrinsics root or state root)
        /// is stored.
        ///
        /// Although a partial digest would be part of the pre-header, we have no
        /// use case for setting one, so it is also omitted here.
        fn with_pre_header(mut self, parent_hash: H256, number: u32) -> Self {
            let h = TestHeader {
                parent_hash,
                number,
                state_root: H256::zero(),
                extrinsics_root: H256::zero(),
                digest: Default::default(),
            };

            self.pre_header = Some(h);
            self
        }

        /// Add a noted extrinsic to the state.
        ///
        /// In normal block authoring, extrinsics are noted in state as they are
        /// applied so that an extrinsics root can be calculated at the end of the
        /// block. This function allows setting up a test case with som extrinsics
        /// already noted.
        ///
        /// The extrinsic is already encoded so that it doesn't have to be a proper
        /// extrinsic, but can just be some example bytes.
        fn with_noted_extrinsic(mut self, ext: Vec<u8>) -> Self {
            self.noted_extrinsics.push(ext);
            self
        }

        /// Build the test externalities with all the utxos already stored
        fn build(self) -> TestExternalities {
            let mut ext = TestExternalities::default();

            // Write all the utxos
            for (output_ref, output) in self.utxos {
                ext.insert(output_ref.encode(), output.encode());
            }

            // Write a pre-header. If none was supplied, create a use a default one.
            let pre_header = self.pre_header.unwrap_or(Header {
                parent_hash: Default::default(),
                number: 0,
                state_root: H256::zero(),
                extrinsics_root: H256::zero(),
                digest: Default::default(),
            });
            ext.insert(HEADER_KEY.to_vec(), pre_header.encode());

            // Write a block height.
            ext.insert(HEIGHT_KEY.to_vec(), pre_header.number.encode());

            // Write the noted extrinsics
            ext.insert(EXTRINSIC_KEY.to_vec(), self.noted_extrinsics.encode());

            ext
        }
    }

    #[test]
    fn validate_empty_works() {
        let tx = TestTransactionBuilder::default().build(true, false);

        let vt = TestExecutive::validate_tuxedo_transaction(&tx).unwrap();

        let expected_result = ValidTransactionBuilder::default().into();

        assert_eq!(vt, expected_result);
    }

    #[test]
    fn validate_with_input_works() {
        let output_ref = mock_output_ref(0, 0);

        ExternalityBuilder::default()
            .with_utxo(output_ref.clone(), Bogus, true)
            .build()
            .execute_with(|| {
                let input = Input {
                    output_ref,
                    redeemer: RedemptionStrategy::Redemption(Vec::new()),
                };

                let tx = TestTransactionBuilder::default()
                    .with_input(input)
                    .build(true, false);

                let vt = TestExecutive::validate_tuxedo_transaction(&tx).unwrap();

                let expected_result = ValidTransactionBuilder::default().into();

                assert_eq!(vt, expected_result);
            });
    }

    #[test]
    fn validate_with_peek_works() {
        let output_ref = mock_output_ref(0, 0);

        ExternalityBuilder::default()
            .with_utxo(output_ref.clone(), Bogus, true)
            .build()
            .execute_with(|| {
                let tx = TestTransactionBuilder::default()
                    .with_peek(output_ref)
                    .build(true, false);

                let vt = TestExecutive::validate_tuxedo_transaction(&tx).unwrap();

                let expected_result = ValidTransactionBuilder::default().into();

                assert_eq!(vt, expected_result);
            });
    }

    #[test]
    fn validate_with_output_works() {
        ExternalityBuilder::default().build().execute_with(|| {
            let output = Output {
                payload: Bogus.into(),
                verifier: TestVerifier { verifies: false },
            };
            let tx = TestTransactionBuilder::default()
                .with_output(output)
                .build(true, false);

            // This is a real transaction, so we need to calculate a real OutputRef
            let tx_hash = BlakeTwo256::hash_of(&tx.encode());
            let output_ref = OutputRef { tx_hash, index: 0 };

            let vt = TestExecutive::validate_tuxedo_transaction(&tx).unwrap();

            let expected_result = ValidTransactionBuilder::default()
                .and_provides(output_ref)
                .into();

            assert_eq!(vt, expected_result);
        });
    }

    #[test]
    fn validate_with_missing_input_works() {
        ExternalityBuilder::default().build().execute_with(|| {
            let output_ref = mock_output_ref(0, 0);
            let input = Input {
                output_ref: output_ref.clone(),
                redeemer: Default::default(),
            };

            let tx = TestTransactionBuilder::default()
                .with_input(input)
                .build(true, false);

            let vt = TestExecutive::validate_tuxedo_transaction(&tx).unwrap();

            let expected_result = ValidTransactionBuilder::default()
                .and_requires(output_ref)
                .into();

            assert_eq!(vt, expected_result);
        });
    }

    #[test]
    fn validate_with_missing_peek_works() {
        ExternalityBuilder::default().build().execute_with(|| {
            let output_ref = mock_output_ref(0, 0);

            let tx = TestTransactionBuilder::default()
                .with_peek(output_ref.clone())
                .build(true, false);

            let vt = TestExecutive::validate_tuxedo_transaction(&tx).unwrap();

            let expected_result = ValidTransactionBuilder::default()
                .and_requires(output_ref)
                .into();

            assert_eq!(vt, expected_result);
        });
    }

    #[test]
    fn validate_with_duplicate_input_fails() {
        let output_ref = mock_output_ref(0, 0);

        ExternalityBuilder::default()
            .with_utxo(output_ref.clone(), Bogus, false)
            .build()
            .execute_with(|| {
                let input = Input {
                    output_ref,
                    redeemer: Default::default(),
                };

                let tx = TestTransactionBuilder::default()
                    .with_input(input.clone())
                    .with_input(input)
                    .build(true, false);

                let result = TestExecutive::validate_tuxedo_transaction(&tx);

                assert_eq!(result, Err(UtxoError::DuplicateInput));
            });
    }

    #[test]
    fn validate_with_duplicate_peek_works() {
        // Peeking at the same input twice is considered valid. However, wallets should do their best
        // not to construct such transactions whenever possible because it makes the transactions space inefficient.

        let output_ref = mock_output_ref(0, 0);

        ExternalityBuilder::default()
            .with_utxo(output_ref.clone(), Bogus, false)
            .build()
            .execute_with(|| {
                let tx = TestTransactionBuilder::default()
                    .with_peek(output_ref.clone())
                    .with_peek(output_ref)
                    .build(true, false);

                let vt = TestExecutive::validate_tuxedo_transaction(&tx).unwrap();

                let expected_result = ValidTransactionBuilder::default().into();

                assert_eq!(vt, expected_result);
            });
    }

    #[test]
    fn validate_with_unsatisfied_verifier_fails() {
        let output_ref = mock_output_ref(0, 0);

        ExternalityBuilder::default()
            .with_utxo(output_ref.clone(), Bogus, false)
            .build()
            .execute_with(|| {
                let input = Input {
                    output_ref,
                    redeemer: Default::default(),
                };

                let tx = TestTransactionBuilder::default()
                    .with_input(input)
                    .build(true, false);

                let result = TestExecutive::validate_tuxedo_transaction(&tx);

                assert_eq!(result, Err(UtxoError::VerifierError));
            });
    }

    #[test]
    fn validate_with_pre_existing_output_fails() {
        // This test requires a transaction to create an output at a location where
        // an output already exists. This could happen in the wild when two transactions
        // don't have inputs and have the same outputs. I initially couldn't think of how
        // this could happen.

        // First we create the transaction that will be submitted in the test.
        let output = Output {
            payload: Bogus.into(),
            verifier: TestVerifier { verifies: false },
        };
        let tx = TestTransactionBuilder::default()
            .with_output(output)
            .build(true, false);

        // Now calculate the output ref that the transaction creates so we can pre-populate the state.
        let tx_hash = BlakeTwo256::hash_of(&tx.encode());
        let output_ref = OutputRef { tx_hash, index: 0 };

        ExternalityBuilder::default()
            .with_utxo(output_ref, Bogus, false)
            .build()
            .execute_with(|| {
                let result = TestExecutive::validate_tuxedo_transaction(&tx);
                assert_eq!(result, Err(UtxoError::PreExistingOutput));
            });
    }

    #[test]
    fn validate_with_constraint_error_fails() {
        ExternalityBuilder::default().build().execute_with(|| {
            let tx = TestTransactionBuilder::default().build(false, false);

            let vt = TestExecutive::validate_tuxedo_transaction(&tx);

            assert_eq!(vt, Err(UtxoError::ConstraintCheckerError(())));
        });
    }

    #[test]
    fn apply_empty_works() {
        ExternalityBuilder::default().build().execute_with(|| {
            let tx = TestTransactionBuilder::default().build(true, false);

            let vt = TestExecutive::apply_tuxedo_transaction(tx);

            assert_eq!(vt, Ok(()));
        });
    }

    #[test]
    fn apply_with_missing_input_fails() {
        ExternalityBuilder::default().build().execute_with(|| {
            let output_ref = mock_output_ref(0, 0);
            let input = Input {
                output_ref: output_ref.clone(),
                redeemer: Default::default(),
            };

            let tx = TestTransactionBuilder::default()
                .with_input(input)
                .build(true, false);

            let vt = TestExecutive::apply_tuxedo_transaction(tx);

            assert_eq!(vt, Err(UtxoError::MissingInput));
        });
    }

    #[test]
    fn apply_with_missing_peek_fails() {
        ExternalityBuilder::default().build().execute_with(|| {
            let output_ref = mock_output_ref(0, 0);

            let tx = TestTransactionBuilder::default()
                .with_peek(output_ref)
                .build(true, false);

            let vt = TestExecutive::apply_tuxedo_transaction(tx);

            assert_eq!(vt, Err(UtxoError::MissingInput));
        });
    }

    #[test]
    fn update_storage_consumes_input() {
        let output_ref = mock_output_ref(0, 0);

        ExternalityBuilder::default()
            .with_utxo(output_ref.clone(), Bogus, true)
            .build()
            .execute_with(|| {
                let input = Input {
                    output_ref: output_ref.clone(),
                    redeemer: Default::default(),
                };

                let tx = TestTransactionBuilder::default()
                    .with_input(input)
                    .build(true, false);

                // Commit the tx to storage
                TestExecutive::update_storage(tx);

                // Check whether the Input is still in storage
                assert!(!sp_io::storage::exists(&output_ref.encode()));
            });
    }

    #[test]
    fn update_storage_adds_output() {
        ExternalityBuilder::default().build().execute_with(|| {
            let output = Output {
                payload: Bogus.into(),
                verifier: TestVerifier { verifies: false },
            };

            let tx = TestTransactionBuilder::default()
                .with_output(output.clone())
                .build(true, false);

            let tx_hash = BlakeTwo256::hash_of(&tx.encode());
            let output_ref = OutputRef { tx_hash, index: 0 };

            // Commit the tx to storage
            TestExecutive::update_storage(tx);

            // Check whether the Output has been written to storage and the proper value is stored
            let stored_bytes = sp_io::storage::get(&output_ref.encode()).unwrap();
            let stored_value = Output::decode(&mut &stored_bytes[..]).unwrap();
            assert_eq!(stored_value, output);
        });
    }

    #[test]
    fn open_block_works() {
        let header = TestHeader {
            parent_hash: H256::repeat_byte(5),
            number: 5,
            state_root: H256::repeat_byte(6),
            extrinsics_root: H256::repeat_byte(7),
            digest: Default::default(),
        };

        ExternalityBuilder::default().build().execute_with(|| {
            // Call open block which just writes the header to storage
            TestExecutive::open_block(&header);

            // Fetch the header back out of storage
            let retrieved_header = sp_io::storage::get(HEADER_KEY)
                .and_then(|d| TestHeader::decode(&mut &*d).ok())
                .expect("Open block should have written a header to storage");

            // Make sure the header that came out is the same one that went in.
            assert_eq!(retrieved_header, header);
        });
    }

    #[test]
    fn apply_valid_extrinsic_work() {
        ExternalityBuilder::default().build().execute_with(|| {
            let tx = TestTransactionBuilder::default().build(true, false);

            let apply_result = TestExecutive::apply_extrinsic(tx.clone());

            // Make sure the returned result is Ok
            assert_eq!(apply_result, Ok(Ok(())));

            // Make sure the transaction is noted in storage
            let noted_extrinsics = sp_io::storage::get(EXTRINSIC_KEY)
                .and_then(|d| <Vec<Vec<u8>>>::decode(&mut &*d).ok())
                .unwrap_or_default();

            assert_eq!(noted_extrinsics, vec![tx.encode()]);
        });
    }

    #[test]
    fn apply_invalid_extrinsic_rejects() {
        ExternalityBuilder::default().build().execute_with(|| {
            let tx = TestTransactionBuilder::default().build(false, false);

            let apply_result = TestExecutive::apply_extrinsic(tx.clone());

            // Make sure the returned result is an error
            assert!(apply_result.is_err());

            // TODO Do we actually want to note transactions that ultimately reject?
            // Make sure the transaction is noted in storage
            let noted_extrinsics = sp_io::storage::get(EXTRINSIC_KEY)
                .and_then(|d| <Vec<Vec<u8>>>::decode(&mut &*d).ok())
                .unwrap_or_default();

            assert_eq!(noted_extrinsics, vec![tx.encode()]);
        });
    }

    #[test]
    fn close_block_works() {
        let parent_hash = H256::repeat_byte(5);
        let block_number = 6;
        let extrinsic = vec![1, 2, 3];
        ExternalityBuilder::default()
            .with_pre_header(parent_hash, block_number)
            .with_noted_extrinsic(extrinsic.clone())
            .build()
            .execute_with(|| {
                let returned_header = TestExecutive::close_block();

                // Make sure the header is as we expected
                let raw_state_root = &sp_io::storage::root(StateVersion::V1)[..];
                let state_root = H256::decode(&mut &raw_state_root[..]).unwrap();
                let expected_header = TestHeader {
                    parent_hash,
                    number: block_number,
                    state_root,
                    extrinsics_root: BlakeTwo256::ordered_trie_root(
                        vec![extrinsic],
                        StateVersion::V0,
                    ),
                    digest: Default::default(),
                };

                assert_eq!(returned_header, expected_header);

                // Make sure the transient storage has been removed
                assert!(!sp_io::storage::exists(HEADER_KEY));
                assert!(!sp_io::storage::exists(EXTRINSIC_KEY));
            });
    }

    #[test]
    fn execute_empty_block_works() {
        ExternalityBuilder::default().build().execute_with(|| {
            let b = TestBlock {
                header: TestHeader {
                    parent_hash: H256::zero(),
                    number: 6,
                    state_root: array_bytes::hex_n_into_unchecked(
                        "cc2d78f5977b6e9e16f4417f60cbd7edaad0c39a6a7cd21281e847da7dd210b9",
                    ),
                    extrinsics_root: array_bytes::hex_n_into_unchecked(
                        "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
                    ),
                    digest: Default::default(),
                },
                extrinsics: Vec::new(),
            };

            TestExecutive::execute_block(b);
        });
    }

    #[test]
    fn execute_block_with_transaction_works() {
        ExternalityBuilder::default().build().execute_with(|| {
            let b = TestBlock {
                header: TestHeader {
                    parent_hash: H256::zero(),
                    number: 6,
                    state_root: array_bytes::hex_n_into_unchecked(
                        "cc2d78f5977b6e9e16f4417f60cbd7edaad0c39a6a7cd21281e847da7dd210b9",
                    ),
                    extrinsics_root: array_bytes::hex_n_into_unchecked(
                        "d609af1c51521f5891054014cf667619067a93f4bca518b398f5a39aeb270cca",
                    ),
                    digest: Default::default(),
                },
                extrinsics: vec![TestTransactionBuilder::default().build(true, false)],
            };

            TestExecutive::execute_block(b);
        });
    }

    #[test]
    #[should_panic(expected = "ConstraintCheckerError(())")]
    fn execute_block_invalid_transaction() {
        ExternalityBuilder::default().build().execute_with(|| {
            let b = TestBlock {
                header: TestHeader {
                    parent_hash: H256::zero(),
                    number: 6,
                    state_root: array_bytes::hex_n_into_unchecked(
                        "cc2d78f5977b6e9e16f4417f60cbd7edaad0c39a6a7cd21281e847da7dd210b9",
                    ),
                    extrinsics_root: array_bytes::hex_n_into_unchecked(
                        "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
                    ),
                    digest: Default::default(),
                },
                extrinsics: vec![TestTransactionBuilder::default().build(false, false)],
            };

            TestExecutive::execute_block(b);
        });
    }

    #[test]
    #[should_panic(expected = "state root mismatch")]
    fn execute_block_state_root_mismatch() {
        ExternalityBuilder::default().build().execute_with(|| {
            let b = TestBlock {
                header: TestHeader {
                    parent_hash: H256::zero(),
                    number: 6,
                    state_root: H256::zero(),
                    extrinsics_root: array_bytes::hex_n_into_unchecked(
                        "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
                    ),
                    digest: Default::default(),
                },
                extrinsics: Vec::new(),
            };

            TestExecutive::execute_block(b);
        });
    }

    #[test]
    #[should_panic(expected = "extrinsics root mismatch")]
    fn execute_block_extrinsic_root_mismatch() {
        ExternalityBuilder::default().build().execute_with(|| {
            let b = TestBlock {
                header: TestHeader {
                    parent_hash: H256::zero(),
                    number: 6,
                    state_root: array_bytes::hex_n_into_unchecked(
                        "cc2d78f5977b6e9e16f4417f60cbd7edaad0c39a6a7cd21281e847da7dd210b9",
                    ),
                    extrinsics_root: H256::zero(),
                    digest: Default::default(),
                },
                extrinsics: Vec::new(),
            };

            TestExecutive::execute_block(b);
        });
    }

    #[test]
    fn execute_block_inherent_only_works() {
        ExternalityBuilder::default().build().execute_with(|| {
            let b = TestBlock {
                header: TestHeader {
                    parent_hash: H256::zero(),
                    number: 6,
                    state_root: array_bytes::hex_n_into_unchecked(
                        "cc2d78f5977b6e9e16f4417f60cbd7edaad0c39a6a7cd21281e847da7dd210b9",
                    ),
                    extrinsics_root: array_bytes::hex_n_into_unchecked(
                        "799fc6d36f68fc83ae3408de607006e02836181e91701aa3a8021960b1f3507c",
                    ),
                    digest: Default::default(),
                },
                extrinsics: vec![TestTransactionBuilder::default().build(true, true)],
            };

            TestExecutive::execute_block(b);
        });
    }

    #[test]
    fn execute_block_inherent_first_works() {
        ExternalityBuilder::default().build().execute_with(|| {
            let b = TestBlock {
                header: TestHeader {
                    parent_hash: H256::zero(),
                    number: 6,
                    state_root: array_bytes::hex_n_into_unchecked(
                        "cc2d78f5977b6e9e16f4417f60cbd7edaad0c39a6a7cd21281e847da7dd210b9",
                    ),
                    extrinsics_root: array_bytes::hex_n_into_unchecked(
                        "bf3e98799022bee8f0a55659af5f498717736ae012d2aff6274cdb7c2b0d78e9",
                    ),
                    digest: Default::default(),
                },
                extrinsics: vec![
                    TestTransactionBuilder::default().build(true, true),
                    TestTransactionBuilder::default().build(true, false),
                ],
            };

            TestExecutive::execute_block(b);
        });
    }

    #[test]
    #[should_panic(
        expected = "Tried to execute opening inherent after switching to non-inherents."
    )]
    fn execute_block_inherents_must_be_first() {
        ExternalityBuilder::default().build().execute_with(|| {
            let b = TestBlock {
                header: TestHeader {
                    parent_hash: H256::zero(),
                    number: 6,
                    state_root: array_bytes::hex_n_into_unchecked(
                        "cc2d78f5977b6e9e16f4417f60cbd7edaad0c39a6a7cd21281e847da7dd210b9",
                    ),
                    extrinsics_root: array_bytes::hex_n_into_unchecked(
                        "df64890515cd8ef5a8e736248394f7c72a1df197bd400a4e31affcaf6e051984",
                    ),
                    digest: Default::default(),
                },
                extrinsics: vec![
                    TestTransactionBuilder::default().build(true, false),
                    TestTransactionBuilder::default().build(true, true),
                ],
            };

            TestExecutive::execute_block(b);
        });
    }

    #[test]
    #[should_panic(
        expected = "Tried to execute opening inherent after switching to non-inherents."
    )]
    fn execute_block_inherents_must_all_be_first() {
        ExternalityBuilder::default().build().execute_with(|| {
            let b = TestBlock {
                header: TestHeader {
                    parent_hash: H256::zero(),
                    number: 6,
                    state_root: array_bytes::hex_n_into_unchecked(
                        "cc2d78f5977b6e9e16f4417f60cbd7edaad0c39a6a7cd21281e847da7dd210b9",
                    ),
                    extrinsics_root: array_bytes::hex_n_into_unchecked(
                        "0x36601deae36de127b974e8498e118e348a50aa4aa94bc5713e29c56e0d37e44f",
                    ),
                    digest: Default::default(),
                },
                extrinsics: vec![
                    TestTransactionBuilder::default().build(true, true),
                    TestTransactionBuilder::default().build(true, false),
                    TestTransactionBuilder::default().build(true, true),
                ],
            };

            TestExecutive::execute_block(b);
        });
    }
}