Skip to content

Commit 73714ca

Browse files
committed
address review comments
1 parent dcd2cc3 commit 73714ca

File tree

9 files changed

+101
-76
lines changed

9 files changed

+101
-76
lines changed

src/generators/correct/v1.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::v1::interval::{Extended, LowerBound, PlutusInterval, UpperBound};
1414
use crate::v1::redeemer::{Redeemer, RedeemerHash};
1515
use crate::v1::script::{MintingPolicyHash, ScriptHash, ValidatorHash};
1616
use crate::v1::transaction::{
17-
DelegationCertification, POSIXTime, ScriptContext, ScriptPurpose, TransactionHash,
18-
TransactionInfo, TransactionInput, TransactionOutput, TxInInfo,
17+
DCert, POSIXTime, ScriptContext, ScriptPurpose, TransactionHash, TransactionInfo,
18+
TransactionInput, TransactionOutput, TxInInfo,
1919
};
2020
use crate::v1::tuple::Tuple;
2121
use crate::v2::value::{AssetClass, CurrencySymbol, TokenName, Value};
@@ -246,7 +246,7 @@ pub fn arb_transaction_index() -> impl Strategy<Value = TransactionIndex> {
246246
arb_integer().prop_map(TransactionIndex)
247247
}
248248

249-
/// Strategy to generate a certificate indVexV
249+
/// Strategy to generate a certificate index.
250250
pub fn arb_certificate_index() -> impl Strategy<Value = CertificateIndex> {
251251
arb_integer().prop_map(CertificateIndex)
252252
}
@@ -297,55 +297,61 @@ pub fn arb_tx_in_info() -> impl Strategy<Value = TxInInfo> {
297297
.prop_map(|(reference, output)| TxInInfo { reference, output })
298298
}
299299

300+
/// Strategy to generate an AssocMap, given the strategies to generate keys and values
300301
pub fn arb_assoc_map<K: std::fmt::Debug, V: std::fmt::Debug>(
301302
arb_k: impl Strategy<Value = K>,
302303
arb_v: impl Strategy<Value = V>,
303304
) -> impl Strategy<Value = AssocMap<K, V>> {
304305
vec((arb_k, arb_v), 10).prop_map(AssocMap)
305306
}
306307

308+
/// Strategy to generate a Tuple, given the strategies to generate its two elements
307309
pub fn arb_tuple<T: std::fmt::Debug, U: std::fmt::Debug>(
308310
arb_k: impl Strategy<Value = T>,
309311
arb_v: impl Strategy<Value = U>,
310312
) -> impl Strategy<Value = Tuple<T, U>> {
311313
(arb_k, arb_v).prop_map(|(l, r)| Tuple(l, r))
312314
}
313315

316+
/// Strategy to generate a PaymentPubKeyHash
314317
pub fn arb_payment_pub_key_hash() -> impl Strategy<Value = PaymentPubKeyHash> {
315318
arb_ed25519_pub_key_hash().prop_map(PaymentPubKeyHash)
316319
}
317320

318-
pub fn arb_delegation_certification() -> impl Strategy<Value = DelegationCertification> {
321+
/// Strategy to generate a DCert
322+
pub fn arb_d_cert() -> impl Strategy<Value = DCert> {
319323
prop_oneof![
320-
arb_staking_credential().prop_map(DelegationCertification::DelegKey),
321-
arb_staking_credential().prop_map(DelegationCertification::DelegDeregKey),
324+
arb_staking_credential().prop_map(DCert::DelegKey),
325+
arb_staking_credential().prop_map(DCert::DelegDeregKey),
322326
(arb_staking_credential(), arb_payment_pub_key_hash())
323-
.prop_map(|(sc, pkh)| DelegationCertification::DelegDelegate(sc, pkh)),
327+
.prop_map(|(sc, pkh)| DCert::DelegDelegate(sc, pkh)),
324328
(arb_payment_pub_key_hash(), arb_payment_pub_key_hash())
325-
.prop_map(|(p1, p2)| DelegationCertification::PoolRegister(p1, p2)),
326-
(arb_payment_pub_key_hash(), arb_integer())
327-
.prop_map(|(pkh, i)| DelegationCertification::PoolRetire(pkh, i)),
328-
Just(DelegationCertification::Genesis),
329-
Just(DelegationCertification::Mir)
329+
.prop_map(|(p1, p2)| DCert::PoolRegister(p1, p2)),
330+
(arb_payment_pub_key_hash(), arb_integer()).prop_map(|(pkh, i)| DCert::PoolRetire(pkh, i)),
331+
Just(DCert::Genesis),
332+
Just(DCert::Mir)
330333
]
331334
}
332335

336+
/// Strategy to generate a ScriptPurpose
333337
pub fn arb_script_purpose() -> impl Strategy<Value = ScriptPurpose> {
334338
prop_oneof![
335339
arb_currency_symbol().prop_map(ScriptPurpose::Minting),
336340
arb_transaction_input().prop_map(ScriptPurpose::Spending),
337341
arb_staking_credential().prop_map(ScriptPurpose::Rewarding),
338-
arb_delegation_certification().prop_map(ScriptPurpose::Certifying)
342+
arb_d_cert().prop_map(ScriptPurpose::Certifying)
339343
]
340344
}
341345

346+
/// Strategy to generate a TransactionInfo. Note that its inputs, outputs, d_cert,
347+
/// signatories and datums field will each have a length of 0 to 5
342348
pub fn arb_transaction_info() -> impl Strategy<Value = TransactionInfo> {
343349
(
344350
vec(arb_tx_in_info(), 5),
345351
vec(arb_transaction_output(), 5),
346352
arb_value(),
347353
arb_value(),
348-
vec(arb_delegation_certification(), 5),
354+
vec(arb_d_cert(), 5),
349355
vec(arb_tuple(arb_staking_credential(), arb_integer()), 5),
350356
arb_plutus_interval_posix_time(),
351357
vec(arb_payment_pub_key_hash(), 5),
@@ -370,6 +376,7 @@ pub fn arb_transaction_info() -> impl Strategy<Value = TransactionInfo> {
370376
)
371377
}
372378

379+
/// Strategy to generate a ScriptContext
373380
pub fn arb_script_context() -> impl Strategy<Value = ScriptContext> {
374381
(arb_script_purpose(), arb_transaction_info())
375382
.prop_map(|(purpose, tx_info)| ScriptContext { purpose, tx_info })

src/generators/correct/v2.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use proptest::strategy::Strategy;
1313

1414
use super::primitive::arb_integer;
1515
use super::v1::{
16-
arb_assoc_map, arb_delegation_certification, arb_payment_pub_key_hash,
17-
arb_plutus_interval_posix_time, arb_redeemer, arb_script_purpose, arb_staking_credential,
18-
arb_transaction_hash,
16+
arb_assoc_map, arb_d_cert, arb_payment_pub_key_hash, arb_plutus_interval_posix_time,
17+
arb_redeemer, arb_script_purpose, arb_staking_credential, arb_transaction_hash,
1918
};
2019

2120
/// Strategy to generate transaction output
@@ -58,7 +57,7 @@ pub fn arb_transaction_info() -> impl Strategy<Value = TransactionInfo> {
5857
vec(arb_transaction_output(), 5),
5958
arb_value(),
6059
arb_value(),
61-
vec(arb_delegation_certification(), 5),
60+
vec(arb_d_cert(), 5),
6261
arb_assoc_map(arb_staking_credential(), arb_integer()),
6362
arb_plutus_interval_posix_time(),
6463
vec(arb_payment_pub_key_hash(), 5),

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pub mod v1;
77
pub mod v2;
88
#[cfg(feature = "lbf")]
99
pub use lbr_prelude::json;
10+
pub(crate) mod utils;

src/plutus_data.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use lbr_prelude::json::{
99
};
1010
use num_bigint::BigInt;
1111
use std::collections::{BTreeMap, BTreeSet};
12-
use std::iter::{empty, once};
1312

1413
#[cfg(feature = "serde")]
1514
use serde::{Deserialize, Serialize};
@@ -570,6 +569,8 @@ pub fn verify_constr_fields(
570569
}
571570
}
572571

572+
/// Given a vector of PlutusData, parse it as an array whose length is known at
573+
/// compile time.
573574
pub fn parse_fixed_len_constr_fields<'a, const LEN: usize>(
574575
v: &'a [PlutusData],
575576
) -> Result<&'a [PlutusData; LEN], PlutusDataError> {
@@ -580,6 +581,8 @@ pub fn parse_fixed_len_constr_fields<'a, const LEN: usize>(
580581
})
581582
}
582583

584+
/// Given a PlutusData, parse it as PlutusData::Constr and its tag as u32. Return
585+
/// the u32 tag and fields.
583586
pub fn parse_constr<'a>(
584587
data: &'a PlutusData,
585588
) -> Result<(u32, &'a Vec<PlutusData>), PlutusDataError> {
@@ -597,6 +600,7 @@ pub fn parse_constr<'a>(
597600
}
598601
}
599602

603+
/// Given a PlutusData, parse it as PlutusData::Constr and verify its tag.
600604
pub fn parse_constr_with_tag<'a>(
601605
data: &'a PlutusData,
602606
expected_tag: u32,
@@ -612,17 +616,3 @@ pub fn parse_constr_with_tag<'a>(
612616
Ok(fields)
613617
}
614618
}
615-
616-
pub fn singleton<T, C>(value: T) -> C
617-
where
618-
C: FromIterator<T>,
619-
{
620-
once(value).into_iter().collect()
621-
}
622-
623-
pub fn none<T, C>() -> C
624-
where
625-
C: FromIterator<T>,
626-
{
627-
empty::<T>().into_iter().collect()
628-
}

src/utils.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::iter::{empty, once};
2+
3+
/// Create a container C from one element.
4+
pub fn singleton<T, C>(value: T) -> C
5+
where
6+
C: FromIterator<T>,
7+
{
8+
once(value).collect()
9+
}
10+
11+
/// Create an empty container.
12+
pub fn none<T, C>() -> C
13+
where
14+
C: FromIterator<T>,
15+
{
16+
empty::<T>().collect()
17+
}

src/v1/transaction.rs

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
//! Types related to Cardano transactions.
2+
use super::{
3+
address::{Address, StakingCredential},
4+
crypto::{LedgerBytes, PaymentPubKeyHash},
5+
datum::{Datum, DatumHash},
6+
interval::PlutusInterval,
7+
tuple::Tuple,
8+
value::{CurrencySymbol, Value},
9+
};
210
use crate::plutus_data::{
3-
none, parse_constr, parse_constr_with_tag, parse_fixed_len_constr_fields, singleton,
4-
verify_constr_fields, IsPlutusData, PlutusData, PlutusDataError, PlutusType,
11+
parse_constr, parse_constr_with_tag, parse_fixed_len_constr_fields, verify_constr_fields,
12+
IsPlutusData, PlutusData, PlutusDataError, PlutusType,
513
};
6-
use crate::v1::address::Address;
7-
use crate::v1::crypto::LedgerBytes;
8-
use crate::v1::datum::DatumHash;
9-
use crate::v1::interval::PlutusInterval;
10-
use crate::v1::value::Value;
14+
use crate::utils::{none, singleton};
1115
#[cfg(feature = "lbf")]
1216
use lbr_prelude::json::Json;
1317
use num_bigint::BigInt;
14-
1518
#[cfg(feature = "serde")]
1619
use serde::{Deserialize, Serialize};
1720

18-
use super::address::StakingCredential;
19-
use super::crypto::PaymentPubKeyHash;
20-
use super::datum::Datum;
21-
use super::tuple::Tuple;
22-
use super::value::CurrencySymbol;
23-
2421
/// An input of a transaction
2522
///
2623
/// Also know as `TxOutRef` from Plutus, this identifies a UTxO by its transacton hash and index
@@ -215,35 +212,47 @@ impl IsPlutusData for TxInInfo {
215212
}
216213
}
217214

215+
/// Partial representation of digests of certificates on the ledger.
218216
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
219217
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
220218
#[cfg_attr(feature = "lbf", derive(Json))]
221-
pub enum DelegationCertification {
219+
pub enum DCert {
222220
DelegKey(StakingCredential),
223221
DelegDeregKey(StakingCredential),
224-
DelegDelegate(StakingCredential, PaymentPubKeyHash),
225-
PoolRegister(PaymentPubKeyHash, PaymentPubKeyHash),
226-
PoolRetire(PaymentPubKeyHash, BigInt),
222+
DelegDelegate(
223+
/// Delegator
224+
StakingCredential,
225+
/// Delegatee
226+
PaymentPubKeyHash,
227+
),
228+
/// A digest of the PoolParam
229+
PoolRegister(
230+
/// Pool id
231+
PaymentPubKeyHash,
232+
/// Pool VFR
233+
PaymentPubKeyHash,
234+
),
235+
PoolRetire(
236+
PaymentPubKeyHash,
237+
/// Epoch
238+
BigInt,
239+
),
227240
Genesis,
228241
Mir,
229242
}
230243

231-
impl IsPlutusData for DelegationCertification {
244+
impl IsPlutusData for DCert {
232245
fn to_plutus_data(&self) -> PlutusData {
233246
let (tag, fields) = match self {
234-
DelegationCertification::DelegKey(c) => (0u32, singleton(c.to_plutus_data())),
235-
DelegationCertification::DelegDeregKey(c) => (1, singleton(c.to_plutus_data())),
236-
DelegationCertification::DelegDelegate(c, pkh) => {
237-
(2, vec![c.to_plutus_data(), pkh.to_plutus_data()])
238-
}
239-
DelegationCertification::PoolRegister(pkh, pkh1) => {
247+
DCert::DelegKey(c) => (0u32, singleton(c.to_plutus_data())),
248+
DCert::DelegDeregKey(c) => (1, singleton(c.to_plutus_data())),
249+
DCert::DelegDelegate(c, pkh) => (2, vec![c.to_plutus_data(), pkh.to_plutus_data()]),
250+
DCert::PoolRegister(pkh, pkh1) => {
240251
(3, vec![pkh.to_plutus_data(), pkh1.to_plutus_data()])
241252
}
242-
DelegationCertification::PoolRetire(pkh, i) => {
243-
(4, vec![pkh.to_plutus_data(), i.to_plutus_data()])
244-
}
245-
DelegationCertification::Genesis => (5, none()),
246-
DelegationCertification::Mir => (6, none()),
253+
DCert::PoolRetire(pkh, i) => (4, vec![pkh.to_plutus_data(), i.to_plutus_data()]),
254+
DCert::Genesis => (5, none()),
255+
DCert::Mir => (6, none()),
247256
};
248257

249258
PlutusData::Constr(BigInt::from(tag), fields)
@@ -298,14 +307,15 @@ impl IsPlutusData for DelegationCertification {
298307
}
299308
}
300309

310+
/// The purpose of the script that's currently running.
301311
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
302312
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
303313
#[cfg_attr(feature = "lbf", derive(Json))]
304314
pub enum ScriptPurpose {
305315
Minting(CurrencySymbol),
306316
Spending(TransactionInput),
307317
Rewarding(StakingCredential),
308-
Certifying(DelegationCertification),
318+
Certifying(DCert),
309319
}
310320

311321
impl IsPlutusData for ScriptPurpose {
@@ -337,6 +347,7 @@ impl IsPlutusData for ScriptPurpose {
337347
}
338348
}
339349

350+
/// A pending transaction as seen by validator scripts, also known as TxInfo in Plutus
340351
#[derive(Debug, PartialEq, Eq, Clone)]
341352
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
342353
#[cfg_attr(feature = "lbf", derive(Json))]
@@ -345,7 +356,7 @@ pub struct TransactionInfo {
345356
pub outputs: Vec<TransactionOutput>,
346357
pub fee: Value,
347358
pub mint: Value,
348-
pub d_cert: Vec<DelegationCertification>,
359+
pub d_cert: Vec<DCert>,
349360
pub wdrl: Vec<Tuple<StakingCredential, BigInt>>,
350361
pub valid_range: POSIXTimeRange,
351362
pub signatories: Vec<PaymentPubKeyHash>,
@@ -392,6 +403,7 @@ impl IsPlutusData for TransactionInfo {
392403
}
393404
}
394405

406+
/// The context that is presented to the currently-executing script.
395407
#[derive(Debug, PartialEq, Eq, Clone)]
396408
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
397409
#[cfg_attr(feature = "lbf", derive(Json))]

src/v2/transaction.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ use crate::plutus_data::{
44
verify_constr_fields, IsPlutusData, PlutusData, PlutusDataError, PlutusType,
55
};
66
pub use crate::v1::transaction::{
7-
DelegationCertification, POSIXTime, POSIXTimeRange, ScriptPurpose, TransactionHash,
8-
TransactionInput,
7+
DCert, POSIXTime, POSIXTimeRange, ScriptPurpose, TransactionHash, TransactionInput,
98
};
10-
use crate::v2::address::Address;
11-
use crate::v2::datum::OutputDatum;
12-
use crate::v2::script::ScriptHash;
13-
use crate::v2::value::Value;
149
#[cfg(feature = "lbf")]
1510
use lbr_prelude::json::Json;
1611
use num_bigint::BigInt;
@@ -19,11 +14,13 @@ use num_bigint::BigInt;
1914
use serde::{Deserialize, Serialize};
2015

2116
use super::{
22-
address::StakingCredential,
17+
address::{Address, StakingCredential},
2318
assoc_map::AssocMap,
2419
crypto::PaymentPubKeyHash,
25-
datum::{Datum, DatumHash},
20+
datum::{Datum, DatumHash, OutputDatum},
2621
redeemer::Redeemer,
22+
script::ScriptHash,
23+
value::Value,
2724
};
2825

2926
/// An output of a transaction
@@ -123,6 +120,7 @@ impl IsPlutusData for TxInInfo {
123120
}
124121
}
125122

123+
/// A pending transaction as seen by validator scripts, also known as TxInfo in Plutus
126124
#[derive(Debug, PartialEq, Eq, Clone)]
127125
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
128126
#[cfg_attr(feature = "lbf", derive(Json))]
@@ -132,7 +130,7 @@ pub struct TransactionInfo {
132130
pub outputs: Vec<TransactionOutput>,
133131
pub fee: Value,
134132
pub mint: Value,
135-
pub d_cert: Vec<DelegationCertification>,
133+
pub d_cert: Vec<DCert>,
136134
pub wdrl: AssocMap<StakingCredential, BigInt>,
137135
pub valid_range: POSIXTimeRange,
138136
pub signatories: Vec<PaymentPubKeyHash>,
@@ -184,6 +182,7 @@ impl IsPlutusData for TransactionInfo {
184182
}
185183
}
186184

185+
/// The context that is presented to the currently-executing script.
187186
#[derive(Debug, PartialEq, Eq, Clone)]
188187
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
189188
#[cfg_attr(feature = "lbf", derive(Json))]

0 commit comments

Comments
 (0)