plutus_ledger_api/generators/correct/
v2.rs

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
//! Proptest strategies for Plutus V2 types
//!
//! These strategies always return valid values.
use super::primitive::arb_natural;
use super::v1::{
    arb_assoc_map, arb_d_cert, arb_payment_pub_key_hash, arb_plutus_interval_posix_time,
    arb_redeemer, arb_script_purpose, arb_staking_credential, arb_transaction_hash,
};
use crate::generators::correct::v1::{
    arb_address, arb_datum, arb_datum_hash, arb_script_hash, arb_transaction_input, arb_value,
};
use crate::v2::datum::OutputDatum;
use crate::v2::transaction::{ScriptContext, TransactionInfo, TransactionOutput, TxInInfo};
use proptest::collection::vec;
use proptest::option;
use proptest::prelude::{prop_oneof, Just};
use proptest::strategy::Strategy;

/// Strategy to generate transaction output
pub fn arb_transaction_output() -> impl Strategy<Value = TransactionOutput> {
    (
        arb_address(),
        arb_value(),
        arb_output_datum(),
        option::of(arb_script_hash()),
    )
        .prop_map(
            |(address, value, datum, reference_script)| TransactionOutput {
                address,
                value,
                datum,
                reference_script,
            },
        )
}

/// Strategy to generate an output datum
pub fn arb_output_datum() -> impl Strategy<Value = OutputDatum> {
    prop_oneof![
        Just(OutputDatum::None),
        arb_datum_hash().prop_map(OutputDatum::DatumHash),
        arb_datum().prop_map(OutputDatum::InlineDatum)
    ]
}

/// Strategy to generate a TxInInfo
pub fn arb_tx_in_info() -> impl Strategy<Value = TxInInfo> {
    (arb_transaction_input(), arb_transaction_output())
        .prop_map(|(reference, output)| TxInInfo { reference, output })
}

pub fn arb_transaction_info() -> impl Strategy<Value = TransactionInfo> {
    (
        vec(arb_tx_in_info(), 5),
        vec(arb_tx_in_info(), 5),
        vec(arb_transaction_output(), 5),
        arb_value(),
        arb_value(),
        vec(arb_d_cert(), 5),
        arb_assoc_map(arb_staking_credential(), arb_natural(1)),
        arb_plutus_interval_posix_time(),
        vec(arb_payment_pub_key_hash(), 5),
        arb_assoc_map(arb_script_purpose(), arb_redeemer()),
        arb_assoc_map(arb_datum_hash(), arb_datum()),
        arb_transaction_hash(),
    )
        .prop_map(
            |(
                inputs,
                reference_inputs,
                outputs,
                fee,
                mint,
                d_cert,
                wdrl,
                valid_range,
                signatories,
                redeemers,
                datums,
                id,
            )| {
                TransactionInfo {
                    inputs,
                    reference_inputs,
                    outputs,
                    fee,
                    mint,
                    d_cert,
                    wdrl,
                    valid_range,
                    signatories,
                    redeemers,
                    datums,
                    id,
                }
            },
        )
}

pub fn arb_script_context() -> impl Strategy<Value = ScriptContext> {
    (arb_script_purpose(), arb_transaction_info())
        .prop_map(|(purpose, tx_info)| ScriptContext { purpose, tx_info })
}