marlowe_lang 0.2.1

experimental parser lib for Cardano Marlowe DSL
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
use crate::serialization;
#[cfg(test)]
use crate::{
    parsing,
    parsing::Rule,
    types::marlowe::*,
    deserialization::marlowe::deserialize,
    serialization::marlowe::serialize
};

#[cfg(test)]
use pest::iterators::Pair;

#[test]
fn deserialize_unwrapped_simple_observation_should_succeed() {
    let simple_contract = "When [ (Case (Notify TrueObs) Close) ] (TimeParam \"test\") Close";
    let deserialized = deserialize(simple_contract);
    match deserialized {
        Ok(_d) => {},
        Err(e) => panic!("{e:?}"),
    }
}

#[test]
fn deserialize_wrapped_simple_observation_should_fail() {
    let simple_contract = "When [ (Case (Notify (TrueObs)) Close) ] (TimeParam \"test\") Close";
    let deserialized = deserialize(simple_contract);
    match deserialized {
        Ok(v) => panic!("{}",v.contract),
        Err(_e) => {},
    }
}

#[test]
fn serialize_and_print() {
    let my_contract = Contract::When {
        when: vec![
            Some(Case { 
                case: Some(Action::Notify { 
                    notify_if: Some(Observation::True) 
                }), 
                then: Some(PossiblyMerkleizedContract::Raw(Contract::Close.boxed())) }
            )],
        timeout: Some(Timeout::TimeParam("test".into())),
        timeout_continuation: Some(Contract::Close.boxed()),
    };

    let serialized = serialize(my_contract);
    match deserialize(&serialized) {
        Ok(_) => {
            // println!("{}",d.contract)
        },
        Err(e) => panic!("{e:?}"),
    };
    
}

#[test]
fn can_generate_contract() {
    Contract::When {
        when: vec![
            Some(Case {
                case: Some(Action::Notify { 
                    notify_if: Some(Observation::True)
                }),
                then: Some(PossiblyMerkleizedContract::Raw(Contract::Pay { 
                    from_account: Some(Party::Role { role_token: "test".to_string() }), 
                    to: Some(Payee::Account(Some(Party::Address (
                        Address::from_bech32("addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x")
                            .expect("valid bech32 address")
                    )))), 
                    token: Some(Token::ada()), 
                    pay: Some(Value::ConstantValue(42)), 
                    then: Some(Contract::Close.boxed())
                }.boxed()))
            }),
            Some(Case { 
                case: Some(Action::Notify { 
                    notify_if: Some(Observation::True) 
                }), 
                then: Some(PossiblyMerkleizedContract::Raw(Contract::Close.boxed()))})
        ],
        timeout: Some(Timeout::TimeParam("test".to_owned())),
        timeout_continuation: Some( Contract::Close.boxed())
    };
}

// This test will go through each contract in the playground directory
// and deserialize them, then re-serialize them, and then compare them
// such that they are identical (not counting newlines,whitespaces etc.)
#[test]
fn can_parse_playground_samples() {
    _ = std::fs::remove_file("IN.TEMP");
    _ = std::fs::remove_file("OUT.TEMP");
    _ = std::fs::remove_file("OUT.UNCOMPRESSED.TEMP");
    let paths = std::fs::read_dir("test_data").unwrap();
    let mut count = 0;
    for path in paths {

        count += 1;
        let canonical_path = path.unwrap().path().canonicalize().unwrap();
        let path_string = canonical_path.display().to_string();
        if !path_string.to_uppercase().ends_with(".MARLOWE") || path_string.contains("test_simple_addr") {
            continue
        }
        let serialized_contract = read_from_file(&path_string);

        let deserialization_result = deserialize(&serialized_contract);
        match deserialization_result {
            Ok(x) => {
                 // we dont care about whitespace etc,
                // and we also want a common format that is 
                // easy to compare in diff viewer should this test fail.
                let strep = |x:&str| {
                    x.replace('\n', "")
                        .replace('(',"(\n")
                        .replace(['\r', ' ', '\t'], "")
                        
                };

                //println!("Successfully deserialized contract: {path_string}");

                let compressed_serialized_input = strep(&serialized_contract);
                let compressed_serialized_output = strep(&serialize(x.contract));
                
                if compressed_serialized_output != compressed_serialized_input {
                    _ = std::fs::write("OUT.TEMP", compressed_serialized_output);
                    _ = std::fs::write("IN.TEMP", compressed_serialized_input);
                    _ = std::fs::write("OUT.UNCOMPRESSED.TEMP",serialized_contract);
                    panic!("the re-serialized contract {path_string} is different from the original! see in.temp and out.temp")
                } else {
                    //println!("Successfully validated {path_string}");
                }
            },
            Err(e) => panic!("{path_string} ::: {e:?}"),
            
        }
       
    }
    if count == 0 {
        panic!("The test_data directory is empty!!!")
    }
    
}

#[test]
fn can_parse_sample() {
    let serialized_contract = read_from_file("sample.marlowe");
    match deserialize(&serialized_contract) {
        Ok(_) => {},
        Err(e) => panic!("{e:?}"),
    }
}

#[test]
fn example_of_how_to_modify_a_contract() {
    let serialized_contract = read_from_file("sample.marlowe");
    let result = deserialize(&serialized_contract).unwrap();
    modify(result.contract);
}

#[test]
fn should_not_parse_unwrapped() {
    use pest::Parser;
    let test = "Party Role \"Lender\"";
    let result = crate::parsing::MarloweParser::parse(crate::parsing::Rule::PayeeParty,test);
    match result {
        Ok(v) => panic!("Should not have been possible to parse.. {v:?}"),
        Err(_e) => {},
    }
}

#[test]
fn modify_example_works(){
    let serialized_contract = read_from_file("sample.marlowe");
    let result = deserialize(&serialized_contract).unwrap();
    modify(result.contract); 
}

#[cfg(test)]
pub(crate) fn read_from_file(path:&str) -> String {
    let path_exists = std::path::Path::new(&path).exists();
    if path_exists {
        std::fs::read_to_string(path).expect("failed to read from file.")
    } else {
        panic!("no such file exists.");
    }
}

#[cfg(test)]
fn modify(contract:Contract) -> Contract {


    match contract {
        Contract::Close => contract,
        Contract::When { when: cases, timeout, timeout_continuation } => 
            Contract::When {
                when: cases,
                timeout,
                timeout_continuation
            },
        Contract::If { x_if: observation, then: then_contract, x_else: else_contract } => 
            Contract::If { 
                x_if: observation, 
                then: Some(modify(*then_contract.unwrap()).boxed()), 
                x_else: Some(modify(*else_contract.unwrap()).boxed())
            },
        Contract::Assert { assert, then } => 
            Contract::Assert { 
                assert, 
                then: Some(modify(*then.unwrap()).boxed()) 
            },
        Contract::Let { x_let: let_x, be: be_value, then:continue_as } => 
            Contract::Let { 
                x_let: let_x, 
                be: be_value, 
                then: Some(Box::new(modify(*continue_as.unwrap())))  
            },
        Contract::Pay { from_account: party, to: payee, token: currency, pay: amount, then: continue_as } => 
            Contract::Pay {
                from_account: party,
                to: payee,
                token: currency,
                pay: amount,
                then: Some(match continue_as {
                    Some(c) => 
                        Box::new(modify(*c)),
                    _ => panic!("expected a contract to exist at this point.."),
                }) 
            },
    }
}


#[test]
fn new_parser() {
    let simple_contract = "When [ (Case (Notify TrueObs) Close) ] (TimeParam \"test\") Close";
    let deserialized = deserialize(simple_contract);
    match deserialized {
        Ok(d) => {
            let _ = serialize(d.contract);
            //println!("{serialized}");
        },
        Err(e) => panic!("{e:?}"),
    }
}

#[test]
fn json_core_should_return_error_for_uninitialized_timeout_params() {
    let contract_path = "test_data/test_uninitialized_timeout.marlowe";
    let serialized_contract = read_from_file(contract_path);
    let tokens = <parsing::MarloweParser as pest::Parser<crate::parsing::Rule>>::parse(
        crate::parsing::Rule::MainContract,
        &serialized_contract,                     
    ).unwrap();
    
    let flat = tokens.flatten();

    // Verify that we have uninitialized time params
    let bad : Vec<Pair<Rule>> = flat.filter( |x| x.as_rule() == crate::parsing::Rule::TimeParam ).collect();
    
    if bad.is_empty() {
        panic!("This test is broken. There should be uninitialized time parameters in the contract, but none were found: {:?}",contract_path);   
    }

    let result = deserialize(&serialized_contract).unwrap();

    match serialization::json::serialize(result.contract) {
        Ok(_v) => {
            panic!("Should not be possible to serialize prior to initializing all constant params")
        },
        Err(_e) => {
            //println!("Successfully validated that contracts with uninitialized timeouts can not be serialized: {:?}",e)
        },
    }
    
}

#[test]
fn json_core_should_return_error_for_uninitialized_constant_params() {
    let contract_path = "test_data/test_uninitialized_constants.marlowe";
    let serialized_contract = read_from_file(contract_path);
    let tokens = 
        match <parsing::MarloweParser as pest::Parser<crate::parsing::Rule>>::parse(
            crate::parsing::Rule::MainContract,
            &serialized_contract,                     
        ) {
            Ok(v) => v,
            Err(e) => panic!("{:#}",e),
        };
    
    let flat = tokens.flatten();
    let bad : Vec<Pair<Rule>> = flat.filter( |x| x.as_rule() == crate::parsing::Rule::ConstantParam ).collect();
    
    if bad.is_empty() {
        panic!("This test is broken. There should be uninitialized constant parameters in the contract, but none were found: {:?}",contract_path);   
    }

    let result = deserialize(&serialized_contract).unwrap();

    match serialization::json::serialize(result.contract) {
        Ok(_v) => {
            panic!("Should not be possible to serialize prior to initializing all constant params")
        },
        Err(_e) => {
            //println!("Successfully validated that contracts with uninitialized constant parameters can not be serialized: {:?}",e)
    },
    }
    
}



#[test]
fn json_core_should_return_output_identical_to_playground() {
    let contract_path = "test_data/test_timeouts.marlowe";
    let serialized_contract = read_from_file(contract_path);
    let tokens = 
        match <parsing::MarloweParser as pest::Parser<crate::parsing::Rule>>::parse(
            crate::parsing::Rule::MainContract,
            &serialized_contract,                     
        ) {
            Ok(v) => v,
            Err(e) => panic!("{:#}",e),
        };
    
    let flat = tokens.flatten();
    let bad : Vec<Pair<Rule>> = flat.filter( |x| x.as_rule() == crate::parsing::Rule::ConstantParam ).collect();
    
    if bad.is_empty() {
        panic!("This test is broken. There should be uninitialized constant parameters in the contract, but none were found: {:?}",contract_path);   
    }

    let mut input = std::collections::HashMap::new();
    input.insert("TEST_PARAMETER_ONE".to_string(),666);
    input.insert("TEST_PARAMETER_TWO".to_string(),4242);
    input.insert("TEST_PARAMETER_THREE".to_string(),1658504132546);
    let deserialized = crate::deserialization::marlowe::deserialize_with_input(&serialized_contract,input).unwrap();

    match serialization::json::serialize(deserialized.contract) {
        Ok(json_core) => {

            // when writing this test, playground had not yet been updated to use ADDRESS but 
            // still used PK, so the sample data is manually updated to use address with bech32
            // which should be pretty to safe to assume is how playground will encode it.
            // will re-visit this and run the serialization thru playground when its updated
            // to be on the safe side.
            let json_play = read_from_file("test_data/test_timeouts_as_serialized_by_playground_probably_when_it_supports_addresses.json")
                .replace([' ', '\t', '\n', '\r'], "");

            let json_core = json_core
                .replace([' ', '\t', '\n', '\r'], "");

            if json_play == json_core {
                //println!("Successfully validated re-enc test_timeouts_as_serialized_by_playground_v2 are identical!")
            } else {
                _ = std::fs::write("c:/temp/jsontest_core.json", json_core);
                _ = std::fs::write("c:/temp/jsontest_play.json", json_play);
                
                panic!("json serialization by marlowe_lang differs from that of the playground.")
            }
        },
        Err(e) => {
            panic!("Input was given but serialization failed anyway...: {:?}",e)
        },
    }
    
}





// Extended marlowe supports time-parameters and constant-parameters.
// We need to be able to get a list of such items in a contract
// so that we can tell how a contract must be initialized properly.
#[test]
fn can_find_uninitialized_inputs() -> Result<(),String> {
    let contract_path = "test_data/test_deeply_nested_contract.marlowe";
    let contract_dsl = std::fs::read_to_string(contract_path).expect("failed to read from file.");
    
    let result = 
        crate::deserialization::marlowe::deserialize(&contract_dsl)
            .unwrap();

    if result.uninitialized_const_params.is_empty() {
        return Err(String::from("Did not find uninitialized const params!"))
    }

    if result.uninitialized_time_params.is_empty() {
        return Err(String::from("Did not find uninitialized time params!"))
    }


    let found_inputs = result.contract.list_input_params();

    if found_inputs.is_empty() {
        return Err(String::from("Did not find any inputs in the contract."))
    }

    if found_inputs.len() != (result.uninitialized_const_params.len() + result.uninitialized_time_params.len()) {
        // println!("parser found these consts: {:?}",result.uninitialized_const_params);
        // println!("parser found these times: {:?}",result.uninitialized_time_params);
        // println!("contract impl found these: {:?}",found_inputs);
        return Err("PARSE AND STRUCT IMPL DIFF!".to_string())
    }
    
    for x in found_inputs {
        match x {
            RequiredContractInputField::TimeParam(name) => {
                if !result.uninitialized_time_params.contains(&name) {
                    return Err(format!("During parsing we found an uninitialized time-param ('{name}') but Contract::list_input_params did not find such a field."))
                }
                //println!("Successfully validated uninitialized time param: {name}")
            },
            RequiredContractInputField::ConstantParam(name) => {
                if !result.uninitialized_const_params.contains(&name) {
                    return Err(format!("During parsing we found an uninitialized const-param ('{name}') but Contract::list_input_params did not find such a field."))
                }
                //println!("Successfully validated uninitialized const param: {name}")
            },
        }
    }

    Ok(())
    
}






#[test]
pub fn marlowe_strict_conversion() -> Result<(),String> {
    use crate::types::marlowe_strict::*;
    let simple = Contract::When { 
        when: vec![
            Case { 
                case: Action::Notify { 
                    notify_if: Observation::True 
                }, 
                then: Contract::When { 
                    when: vec![
                        Case { 
                            case: Action::Notify { 
                                notify_if: Observation::True 
                            }, 
                            then: Contract::When { 
                                when: vec![
                                    Case { 
                                        case: Action::Notify { 
                                            notify_if: Observation::True 
                                        }, 
                                        then: Contract::Close 
                                    }
                                ], 
                                timeout: 42, 
                                timeout_continuation: Contract::When { 
                                    when: vec![
                                        Case { 
                                            case: Action::Notify { 
                                                notify_if: Observation::True 
                                            }, 
                                            then: Contract::Close 
                                        }
                                    ], 
                                    timeout: 42, 
                                    timeout_continuation: Contract::Close.into()
                                }.into()
                            }
                        }
                    ], 
                    timeout: 42, 
                    timeout_continuation: Contract::When { 
                        when: vec![
                            Case { 
                                case: Action::Notify { 
                                    notify_if: Observation::True 
                                }, 
                                then: Contract::Close 
                            }
                        ], 
                        timeout: 42, 
                        timeout_continuation: Contract::Close.into()
                    }.into()
                } 
            }
        ], 
        timeout: 42, 
        timeout_continuation: Contract::When { 
            when: vec![
                Case { 
                    case: Action::Notify { 
                        notify_if: Observation::True 
                    }, 
                    then: Contract::Close 
                }
            ], 
            timeout: 42, 
            timeout_continuation: Contract::Close.into()
        }.into()
    };

    let _ : crate::types::marlowe::Contract = simple.try_into()?;
    Ok(())

}

#[test]
pub fn basic_marlowe_strict_example_code() -> Result<(),String> {

    use crate::types::marlowe_strict::*;
    // use crate::serialization::*;

    let p1 = Party::role("P1");
    let p2 = Party::role("P2");
    let tok = Token::ada();
    let quantity = Value::ConstantValue(42000000);

    let contract = Contract::When { 
        when: vec![
            Case { 
                case: Action::Deposit { 
                    into_account: p2.clone(), 
                    party: p1.clone(), 
                    of_token: tok.clone(), 
                    deposits: quantity
                }, 
                then: 
                    Contract::Pay { 
                        from_account: p1, 
                        to: Payee::Party(p2), 
                        token: tok, 
                        pay: Value::ConstantValue(42), 
                        then: Contract::Close.into()
                    } 
            }
        ], 
        timeout: 999999999, 
        timeout_continuation: Contract::Close.into()
    };

    let _serializable_contract : crate::types::marlowe::Contract = contract.try_into()?;

    // println!("{}",marlowe::serialize(serializable_contract.clone()));
    // println!("{}",json::serialize(serializable_contract.clone())?);
    // println!("{}",cborhex::serialize(serializable_contract.clone())?);


    Ok(())
}


#[test]
fn marlowe_strict_with_iter_example() {
        
    use chrono::Days;
    use crate::{types::{
        marlowe_strict::*,
        marlowe::{Token,Bound}
    },serialization::*};

    let choice_owner = Party::role("bank");
    
    let winner_choice = ChoiceId { 
        choice_name: "winner".into(), 
        choice_owner: choice_owner.clone() 
    };

    let quantity = Value::ConstantValue(42000000);

    let pay_this_winner = |pt| -> Contract {
        Contract::Pay { 
            from_account: choice_owner.clone(), 
            to: Payee::Party(pt), 
            token: Token::ada(), 
            pay: quantity.clone(), 
            then: Box::new(Contract::Close)
        }
    };

    let contract = Contract::When { 
        when: vec![
            Case { 
                case: Action::Deposit { 
                    into_account: choice_owner.clone(), 
                    party: choice_owner.clone(), 
                    of_token: Token::ada(), 
                    deposits: quantity.clone()
                }, 
                then: Contract::When { 
                        when: (1..11).map(|n| {
                            Case { 
                                case: Action::Choice { 
                                    for_choice: winner_choice.clone(), 
                                    choose_between: vec![Bound(n,n)]
                                }, 
                                then: pay_this_winner(Party::role(&format!("P{n}")))
                            }
                        }).collect(), 
                        timeout: chrono::Utc::now().checked_add_days(Days::new(2)).unwrap().timestamp_millis(), 
                        timeout_continuation: Contract::Close.into()
                    }
            }
        ], 
        timeout: chrono::Utc::now().checked_add_days(Days::new(1)).unwrap().timestamp_millis(), 
        timeout_continuation: Contract::Close.into()
    };
    let _serialized = marlowe::serialize_strict(contract).unwrap();
    // println!("{}",parsing::fmt::fmt(&serialized))
}




#[test]
fn walk_for_roles() {
    let serialized_contract = crate::tests::core::read_from_file("test_data/test_deeply_nested_contract.marlowe");
    match crate::deserialization::marlowe::deserialize(&serialized_contract) {
        Ok(c) => {
            // make sure we capture all roles both when deserializing
            // and when using ast_node walking. 
            let mut actual_parties = c.parties;
            let mut found_parties = c.contract.parties();
            actual_parties.dedup_by_key(|x|format!("{x:?}"));
            found_parties.dedup_by_key(|x|format!("{x:?}"));
            actual_parties.sort_by(|a,b| {
                let aa = format!("{a:?}");
                let bb = format!("{b:?}");
                aa.cmp(&bb)
            });
            found_parties.sort_by(|a,b| {
                let aa = format!("{a:?}");
                let bb = format!("{b:?}");
                aa.cmp(&bb)
            });
            // println!("FOUND PARTIES : {found_parties:?}");
            // println!("ACTUAL PARTIES: {actual_parties:?}");
            assert!(actual_parties.eq(&found_parties));

            
        },
        _ => panic!()
    }
}