ddk-manager 1.1.0

Creation and handling of Discrete Log Contracts (DLC).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
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
#![allow(dead_code)]

use bitcoin::{
    consensus::{Decodable, Encodable},
    Amount, Network, Transaction,
};
use bitcoincore_rpc::{Auth, Client, RpcApi};
use bitcoincore_rpc_json::AddressType;
use ddk::{chain::EsploraClient, logger::Logger, wallet::DlcDevKitWallet};
use ddk::{oracle::memory::MemoryOracle, storage::memory::MemoryStorage};
use ddk_dlc::{EnumerationPayout, Payout};
use ddk_manager::payout_curve::{
    PayoutFunction, PayoutFunctionPiece, PayoutPoint, PolynomialPayoutCurvePiece, RoundingInterval,
    RoundingIntervals,
};
use ddk_manager::Oracle;
use ddk_manager::Time;
use ddk_manager::{
    contract::{
        contract_input::{ContractInput, ContractInputInfo, OracleInput},
        enum_descriptor::EnumDescriptor,
        numerical_descriptor::{DifferenceParams, NumericalDescriptor},
        ContractDescriptor,
    },
    payout_curve::HyperbolaPayoutCurvePiece,
};
use ddk_trie::OracleNumericInfo;
use secp256k1_zkp::rand::{seq::SliceRandom, thread_rng, Fill, RngCore};
use std::fmt::Write;
use std::{cell::RefCell, sync::Arc};

pub const NB_DIGITS: u32 = 10;
pub const MIN_SUPPORT_EXP: usize = 1;
pub const MAX_ERROR_EXP: usize = 2;
pub const BASE: u32 = 2;
pub const EVENT_MATURITY: u32 = 1623133104;
pub const EVENT_ID: &str = "Test";
pub const OFFER_COLLATERAL: u64 = 90000000;
pub const ACCEPT_COLLATERAL: u64 = 11000000;
pub const TOTAL_COLLATERAL: Amount = Amount::from_sat(OFFER_COLLATERAL + ACCEPT_COLLATERAL);
pub const MID_POINT: u64 = 5;
pub const ROUNDING_MOD: u64 = 1;

#[macro_export]
macro_rules! receive_loop {
    ($receive:expr, $manager:expr, $send:expr, $expect_err:expr, $sync_send:expr, $rcv_callback: expr, $msg_callback: expr) => {
        tokio::spawn(async move {
            tokio::task::yield_now().await;
            loop {
                match $receive.recv().await {
                    Some(Some(msg)) => match $manager
                        .lock()
                        .await
                        .on_dlc_message(
                            &msg,
                            "0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166"
                                .parse()
                                .unwrap(),
                        )
                        .await
                    {
                        Ok(opt) => {
                            if $expect_err.load(Ordering::Relaxed) != false {
                                panic!("Expected error not raised");
                            }
                            match opt {
                                Some(msg) => {
                                    let msg_opt = $rcv_callback(msg);
                                    if let Some(msg) = msg_opt {
                                        #[allow(clippy::redundant_closure_call)]
                                        $msg_callback(&msg);
                                        (&$send).send(Some(msg)).await.expect("Error sending");
                                    }
                                }
                                None => {}
                            }
                        }
                        Err(e) => {
                            if $expect_err.load(Ordering::Relaxed) != true {
                                panic!("Unexpected error {}", e);
                            }
                        }
                    },
                    None | Some(None) => return,
                };
                $sync_send.send(()).await.expect("Error syncing");
            }
        })
    };
}

#[macro_export]
macro_rules! periodic_check {
    ($d:expr, $id:expr, $p:ident) => {
        $d.lock()
            .await
            .periodic_check(true)
            .await
            .expect("Periodic check error");
        assert_contract_state!($d, $id, $p);
    };
}

#[macro_export]
macro_rules! write_contract {
    ($contract: ident, $state: ident) => {
        match $contract {
            Contract::$state(s) => {
                let mut buf = Vec::new();
                s.write(&mut buf)
                    .expect("to be able to serialize the contract.");
                std::fs::write(format!("{}", stringify!($state)), buf)
                    .expect("to be able to save the contract to file.");
            }
            _ => {}
        }
    };
}

#[macro_export]
macro_rules! assert_contract_state {
    ($d:expr, $id:expr, $p:ident) => {
        let res = $d
            .lock()
            .await
            .get_store()
            .get_contract(&$id)
            .await
            .expect("Could not retrieve contract");
        if let Some(c) = res {
            if let Contract::$p(_) = c {
            } else {
                panic!("Unexpected contract state {:?}", c);
            }
            if std::env::var("GENERATE_SERIALIZED_CONTRACT").is_ok() {
                write_contract!(c, $p);
            }
        } else {
            panic!("Contract {:02x?} does not exist in store", $id);
        }
    };
}

#[macro_export]
macro_rules! write_channel {
    ($channel: ident, $state: ident) => {
        let suffix = if let Channel::Signed(s) = &$channel {
            format!("{}", s.state)
        } else {
            "".to_string()
        };
        match $channel {
            Channel::$state(s) => {
                let mut buf = Vec::new();
                s.write(&mut buf)
                    .expect("to be able to serialize the channel.");
                std::fs::write(format!("{}Channel{}", stringify!($state), suffix), buf)
                    .expect("to be able to save the channel to file.");
            }
            _ => {}
        }
    };
}

#[macro_export]
macro_rules! assert_channel_state {
    ($d:expr, $id:expr, $p:ident $(, $s: ident)?) => {{
        assert_channel_state_unlocked!($d.lock().await, $id, $p $(, $s)?)
    }};
}

#[allow(unused_macros)]
macro_rules! assert_channel_state_unlocked {
    ($d:expr, $id:expr, $p:ident $(, $s: ident)?) => {{
        let res = $d
            .get_store()
            .get_channel(&$id)
            .expect("Could not retrieve channel");
        if let Some(Channel::$p(c)) = res {
            $(if let ddk_manager::channel::signed_channel::SignedChannelState::$s { .. } = c.state {
            } else {
                panic!("Unexpected signed channel state {:?}", c.state);
            })?
            if std::env::var("GENERATE_SERIALIZED_CHANNEL").is_ok() {
                let channel = Channel::$p(c);
                write_channel!(channel, $p);
            }
        } else if let Some(c) = res {
            panic!("Unexpected channel state {:?}", c);
        } else {
            panic!("Could not find requested channel");
        }
    }};
}

pub fn enum_outcomes() -> Vec<String> {
    vec![
        "a".to_owned(),
        "b".to_owned(),
        "c".to_owned(),
        "d".to_owned(),
    ]
}

pub fn max_value() -> u32 {
    BASE.pow(NB_DIGITS) - 1
}

pub fn max_value_from_digits(nb_digits: usize) -> u32 {
    BASE.pow(nb_digits as u32) - 1
}

pub fn select_active_oracles(nb_oracles: usize, threshold: usize) -> Vec<usize> {
    let nb_active_oracles = if threshold == nb_oracles {
        threshold
    } else {
        (thread_rng().next_u32() % ((nb_oracles - threshold) as u32) + (threshold as u32)) as usize
    };
    let mut oracle_indexes: Vec<usize> = (0..nb_oracles).collect();
    oracle_indexes.shuffle(&mut thread_rng());
    oracle_indexes = oracle_indexes.into_iter().take(nb_active_oracles).collect();
    oracle_indexes.sort_unstable();
    oracle_indexes
}

#[derive(Debug)]
pub struct TestParams {
    pub oracles: Vec<MemoryOracle>,
    pub contract_input: ContractInput,
}

pub fn get_difference_params() -> DifferenceParams {
    DifferenceParams {
        max_error_exp: MAX_ERROR_EXP,
        min_support_exp: MIN_SUPPORT_EXP,
        maximize_coverage: false,
    }
}

pub fn get_enum_contract_descriptor() -> ContractDescriptor {
    let outcome_payouts: Vec<_> = enum_outcomes()
        .iter()
        .enumerate()
        .map(|(i, x)| {
            let payout = if i % 2 == 0 {
                Payout {
                    offer: TOTAL_COLLATERAL,
                    accept: Amount::ZERO,
                }
            } else {
                Payout {
                    offer: Amount::ZERO,
                    accept: TOTAL_COLLATERAL,
                }
            };
            EnumerationPayout {
                outcome: x.to_owned(),
                payout,
            }
        })
        .collect();
    ContractDescriptor::Enum(EnumDescriptor { outcome_payouts })
}

pub async fn generate_blocks(nb_blocks: u32, electrs: Arc<EsploraClient>, sink: Arc<Client>) {
    let prev_blockchain_height = electrs.async_client.get_height().await.unwrap();
    let sink_address = sink
        .get_new_address(None, None)
        .expect("RPC Error")
        .assume_checked();
    sink.generate_to_address(nb_blocks as u64, &sink_address)
        .expect("RPC Error");

    // Use a more stack-friendly polling approach
    let target_height = prev_blockchain_height + nb_blocks;
    loop {
        let current_height = electrs.async_client.get_height().await.unwrap();
        if current_height >= target_height {
            break;
        }

        // Yield control to prevent stack buildup
        tokio::task::yield_now().await;
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;
    }
}

pub async fn get_enum_oracle() -> MemoryOracle {
    let oracle = MemoryOracle::default();

    oracle
        .oracle
        .create_enum_event(EVENT_ID.to_string(), enum_outcomes(), EVENT_MATURITY)
        .await
        .unwrap();

    oracle
}

pub async fn get_enum_oracles(nb_oracles: usize, threshold: usize) -> Vec<MemoryOracle> {
    let mut oracles: Vec<_> = vec![];
    for _ in 0..nb_oracles {
        let oracle = get_enum_oracle().await;
        oracles.push(oracle);
    }

    let active_oracles = select_active_oracles(nb_oracles, threshold);
    let outcomes = enum_outcomes();
    let outcome = outcomes[(thread_rng().next_u32() as usize) % outcomes.len()].clone();
    for index in active_oracles {
        oracles
            .get_mut(index)
            .unwrap()
            .oracle
            .sign_enum_event(EVENT_ID.to_string(), outcome.clone())
            .await
            .unwrap();
    }

    oracles
}

pub async fn get_enum_test_params(
    nb_oracles: usize,
    threshold: usize,
    oracles: Option<Vec<MemoryOracle>>,
) -> TestParams {
    let oracles = match oracles {
        Some(o) => o,
        None => get_enum_oracles(nb_oracles, threshold).await,
    };

    let contract_descriptor = get_enum_contract_descriptor();
    let contract_info = ContractInputInfo {
        contract_descriptor,
        oracles: OracleInput {
            public_keys: oracles.iter().map(|x| x.get_public_key()).collect(),
            event_id: EVENT_ID.to_owned(),
            threshold: threshold as u16,
        },
    };

    let contract_input = ContractInput {
        offer_collateral: Amount::from_sat(OFFER_COLLATERAL),
        accept_collateral: Amount::from_sat(ACCEPT_COLLATERAL),
        fee_rate: 2,
        contract_flags: 0,
        contract_infos: vec![contract_info],
    };

    TestParams {
        oracles,
        contract_input,
    }
}

pub fn get_splice_in_enum_contract_descriptor(collateral: Amount) -> ContractDescriptor {
    let outcome_payouts: Vec<_> = enum_outcomes()
        .iter()
        .enumerate()
        .map(|(i, x)| {
            let payout = if i % 2 == 0 {
                Payout {
                    offer: collateral,
                    accept: Amount::ZERO,
                }
            } else {
                Payout {
                    offer: Amount::ZERO,
                    accept: collateral,
                }
            };
            EnumerationPayout {
                outcome: x.to_owned(),
                payout,
            }
        })
        .collect();
    ContractDescriptor::Enum(EnumDescriptor { outcome_payouts })
}

pub fn get_splice_out_enum_contract_descriptor(collateral: Amount) -> ContractDescriptor {
    let outcome_payouts: Vec<_> = enum_outcomes()
        .iter()
        .enumerate()
        .map(|(i, x)| {
            let payout = if i % 2 == 0 {
                Payout {
                    offer: collateral,
                    accept: Amount::ZERO,
                }
            } else {
                Payout {
                    offer: Amount::ZERO,
                    accept: collateral,
                }
            };
            EnumerationPayout {
                outcome: x.to_owned(),
                payout,
            }
        })
        .collect();
    ContractDescriptor::Enum(EnumDescriptor { outcome_payouts })
}

pub async fn get_splice_in_test_params_with_maturity(
    oracles: Vec<MemoryOracle>,
    maturity: u32,
) -> TestParams {
    let splice_event_id = format!("{}-splice-in", EVENT_ID);

    // Create enum announcements for the splice event with future maturity
    for oracle in &oracles {
        oracle
            .oracle
            .create_enum_event(splice_event_id.clone(), enum_outcomes(), maturity)
            .await
            .unwrap();
    }

    // Sign the splice enum events (similar to get_enum_oracles)
    let active_oracles = select_active_oracles(oracles.len(), 1); // threshold = 1 for splice
    let outcomes = enum_outcomes();
    let outcome = outcomes[(thread_rng().next_u32() as usize) % outcomes.len()].clone();
    for index in active_oracles {
        oracles
            .get(index)
            .unwrap()
            .oracle
            .sign_enum_event(splice_event_id.clone(), outcome.clone())
            .await
            .unwrap();
    }

    let offer_collateral = TOTAL_COLLATERAL + Amount::from_sat(50_000_000);
    let contract_descriptor = get_splice_in_enum_contract_descriptor(offer_collateral);
    let contract_info = ContractInputInfo {
        contract_descriptor,
        oracles: OracleInput {
            public_keys: oracles.iter().map(|x| x.get_public_key()).collect(),
            event_id: splice_event_id,
            threshold: 1,
        },
    };

    let contract_input = ContractInput {
        offer_collateral,
        accept_collateral: Amount::ZERO,
        fee_rate: 2,
        contract_flags: 0,
        contract_infos: vec![contract_info],
    };

    TestParams {
        oracles,
        contract_input,
    }
}

pub async fn get_splice_out_test_params_with_maturity(
    oracles: Vec<MemoryOracle>,
    maturity: u32,
) -> TestParams {
    let splice_event_id = format!("{}-splice-out", EVENT_ID);

    // Create enum announcements for the splice event with future maturity
    for oracle in &oracles {
        oracle
            .oracle
            .create_enum_event(splice_event_id.clone(), enum_outcomes(), maturity)
            .await
            .unwrap();
    }

    // Sign the splice enum events (similar to get_enum_oracles)
    let active_oracles = select_active_oracles(oracles.len(), 1); // threshold = 1 for splice
    let outcomes = enum_outcomes();
    let outcome = outcomes[(thread_rng().next_u32() as usize) % outcomes.len()].clone();
    for index in active_oracles {
        oracles
            .get(index)
            .unwrap()
            .oracle
            .sign_enum_event(splice_event_id.clone(), outcome.clone())
            .await
            .unwrap();
    }

    let offer_collateral = Amount::from_sat(500_000);

    let contract_descriptor = get_splice_out_enum_contract_descriptor(offer_collateral);
    let contract_info = ContractInputInfo {
        contract_descriptor,
        oracles: OracleInput {
            public_keys: oracles.iter().map(|x| x.get_public_key()).collect(),
            event_id: splice_event_id,
            threshold: 1,
        },
    };

    let contract_input = ContractInput {
        offer_collateral,
        accept_collateral: Amount::ZERO,
        fee_rate: 2,
        contract_flags: 0,
        contract_infos: vec![contract_info],
    };

    TestParams {
        oracles,
        contract_input,
    }
}

pub async fn get_single_funded_test_params(nb_oracles: usize, threshold: usize) -> TestParams {
    let oracles = get_enum_oracles(nb_oracles, threshold).await;
    let contract_descriptor = get_enum_contract_descriptor();
    let contract_info = ContractInputInfo {
        contract_descriptor,
        oracles: OracleInput {
            public_keys: oracles.iter().map(|x| x.get_public_key()).collect(),
            event_id: EVENT_ID.to_owned(),
            threshold: 1,
        },
    };

    let contract_input = ContractInput {
        offer_collateral: TOTAL_COLLATERAL,
        accept_collateral: Amount::ZERO,
        fee_rate: 5,
        contract_flags: 0,
        contract_infos: vec![contract_info],
    };

    TestParams {
        oracles,
        contract_input,
    }
}

pub fn get_polynomial_payout_curve_pieces(min_nb_digits: usize) -> Vec<PayoutFunctionPiece> {
    vec![
        PayoutFunctionPiece::PolynomialPayoutCurvePiece(
            PolynomialPayoutCurvePiece::new(vec![
                PayoutPoint {
                    event_outcome: 0,
                    outcome_payout: Amount::ZERO,
                    extra_precision: 0,
                },
                PayoutPoint {
                    event_outcome: 3,
                    outcome_payout: Amount::from_sat(OFFER_COLLATERAL),
                    extra_precision: 0,
                },
                PayoutPoint {
                    event_outcome: MID_POINT,
                    outcome_payout: TOTAL_COLLATERAL,
                    extra_precision: 0,
                },
            ])
            .unwrap(),
        ),
        PayoutFunctionPiece::PolynomialPayoutCurvePiece(
            PolynomialPayoutCurvePiece::new(vec![
                PayoutPoint {
                    event_outcome: MID_POINT,
                    outcome_payout: TOTAL_COLLATERAL,
                    extra_precision: 0,
                },
                PayoutPoint {
                    event_outcome: max_value_from_digits(min_nb_digits) as u64,
                    outcome_payout: TOTAL_COLLATERAL,
                    extra_precision: 0,
                },
            ])
            .unwrap(),
        ),
    ]
}

pub fn get_hyperbola_payout_curve_pieces(min_nb_digits: usize) -> Vec<PayoutFunctionPiece> {
    vec![PayoutFunctionPiece::HyperbolaPayoutCurvePiece(
        HyperbolaPayoutCurvePiece::new(
            PayoutPoint {
                event_outcome: 0,
                outcome_payout: Amount::ZERO,
                extra_precision: 0,
            },
            PayoutPoint {
                event_outcome: max_value_from_digits(min_nb_digits) as u64,
                outcome_payout: Amount::ZERO,
                extra_precision: 0,
            },
            true,
            50.0,
            50.0,
            5.0,
            -1.0,
            0.0,
            1.0,
        )
        .unwrap(),
    )]
}

pub fn get_numerical_contract_descriptor(
    oracle_numeric_infos: OracleNumericInfo,
    function_pieces: Vec<PayoutFunctionPiece>,
    difference_params: Option<DifferenceParams>,
) -> ContractDescriptor {
    ContractDescriptor::Numerical(NumericalDescriptor {
        payout_function: PayoutFunction::new(function_pieces).unwrap(),
        rounding_intervals: RoundingIntervals {
            intervals: vec![RoundingInterval {
                begin_interval: 0,
                rounding_mod: ROUNDING_MOD,
            }],
        },
        oracle_numeric_infos,
        difference_params,
    })
}

pub async fn get_digit_decomposition_oracle(nb_digits: u16) -> MemoryOracle {
    let oracle = MemoryOracle::default();

    oracle
        .oracle
        .create_numeric_event(
            EVENT_ID.to_string(),
            nb_digits,
            false,
            0,
            "sats/sec".to_owned(),
            EVENT_MATURITY,
        )
        .await
        .unwrap();
    oracle
}

pub async fn get_digit_decomposition_oracles(
    oracle_numeric_infos: &OracleNumericInfo,
    threshold: usize,
    with_diff: bool,
    use_max_value: bool,
) -> Vec<MemoryOracle> {
    let mut oracles = vec![];
    for digit in &oracle_numeric_infos.nb_digits {
        oracles.push(get_digit_decomposition_oracle(*digit as u16).await);
    }

    let outcome_value = if use_max_value {
        max_value_from_digits(oracle_numeric_infos.get_min_nb_digits()) as usize
    } else {
        (thread_rng().next_u32() % max_value()) as usize
    };
    let oracle_indexes = select_active_oracles(oracle_numeric_infos.nb_digits.len(), threshold);

    for (i, index) in oracle_indexes.iter().enumerate() {
        let cur_outcome: usize = if !use_max_value && (i == 0 || !with_diff) {
            outcome_value
        } else if !use_max_value {
            let mut delta = (thread_rng().next_u32() % BASE.pow(MIN_SUPPORT_EXP as u32)) as i32;
            delta = if thread_rng().next_u32() % 2 == 1 {
                -delta
            } else {
                delta
            };

            let tmp_outcome = (outcome_value as i32) + delta;
            if tmp_outcome < 0 {
                0
            } else if tmp_outcome
                > (max_value_from_digits(oracle_numeric_infos.nb_digits[*index]) as i32)
            {
                max_value() as usize
            } else {
                tmp_outcome as usize
            }
        } else {
            let max_value = max_value_from_digits(oracle_numeric_infos.nb_digits[*index]) as usize;
            if max_value == outcome_value {
                outcome_value
            } else {
                outcome_value + 1 + (thread_rng().next_u32() as usize % (max_value - outcome_value))
            }
        };

        for oracle in &oracles {
            let _sign_even_if_it_fails_spent_an_hour_tracking_ci_bug = oracle
                .oracle
                .sign_numeric_event(EVENT_ID.to_string(), cur_outcome as i64)
                .await;
        }
    }

    oracles
}

pub async fn get_numerical_test_params(
    oracle_numeric_infos: &OracleNumericInfo,
    threshold: usize,
    with_diff: bool,
    contract_descriptor: ContractDescriptor,
    use_max_value: bool,
) -> TestParams {
    let oracles =
        get_digit_decomposition_oracles(oracle_numeric_infos, threshold, with_diff, use_max_value)
            .await;
    let contract_info = ContractInputInfo {
        oracles: OracleInput {
            public_keys: oracles.iter().map(|x| x.get_public_key()).collect(),
            event_id: EVENT_ID.to_owned(),
            threshold: threshold as u16,
        },
        contract_descriptor,
    };

    let contract_input = ContractInput {
        offer_collateral: Amount::from_sat(OFFER_COLLATERAL),
        accept_collateral: Amount::from_sat(ACCEPT_COLLATERAL),
        fee_rate: 2,
        contract_flags: 0,
        contract_infos: vec![contract_info],
    };

    TestParams {
        oracles,
        contract_input,
    }
}

pub async fn get_enum_and_numerical_test_params(
    nb_oracles: usize,
    threshold: usize,
    with_diff: bool,
    difference_params: Option<DifferenceParams>,
) -> TestParams {
    let oracle_numeric_infos = get_same_num_digits_oracle_numeric_infos(nb_oracles);
    let enum_oracles = get_enum_oracles(nb_oracles, threshold).await;
    let enum_contract_descriptor = get_enum_contract_descriptor();
    let enum_contract_info = ContractInputInfo {
        oracles: OracleInput {
            public_keys: enum_oracles.iter().map(|x| x.get_public_key()).collect(),
            event_id: EVENT_ID.to_owned(),
            threshold: threshold as u16,
        },
        contract_descriptor: enum_contract_descriptor,
    };
    let numerical_oracles =
        get_digit_decomposition_oracles(&oracle_numeric_infos, threshold, with_diff, false).await;
    let numerical_contract_descriptor = get_numerical_contract_descriptor(
        get_same_num_digits_oracle_numeric_infos(nb_oracles),
        get_polynomial_payout_curve_pieces(oracle_numeric_infos.get_min_nb_digits()),
        difference_params,
    );
    let numerical_contract_info = ContractInputInfo {
        oracles: OracleInput {
            public_keys: numerical_oracles
                .iter()
                .map(|x| x.get_public_key())
                .collect(),
            event_id: EVENT_ID.to_owned(),
            threshold: threshold as u16,
        },
        contract_descriptor: numerical_contract_descriptor,
    };

    let contract_infos = if thread_rng().next_u32() % 2 == 0 {
        vec![enum_contract_info, numerical_contract_info]
    } else {
        vec![numerical_contract_info, enum_contract_info]
    };

    let contract_input = ContractInput {
        offer_collateral: Amount::from_sat(OFFER_COLLATERAL),
        accept_collateral: Amount::from_sat(ACCEPT_COLLATERAL),
        fee_rate: 2,
        contract_flags: 0,
        contract_infos,
    };

    TestParams {
        oracles: enum_oracles.into_iter().chain(numerical_oracles).collect(),
        contract_input,
    }
}

pub fn get_same_num_digits_oracle_numeric_infos(nb_oracles: usize) -> OracleNumericInfo {
    OracleNumericInfo {
        nb_digits: std::iter::repeat(NB_DIGITS as usize)
            .take(nb_oracles)
            .collect(),
        base: BASE as usize,
    }
}

pub fn get_variable_oracle_numeric_infos(nb_digits: &[usize]) -> OracleNumericInfo {
    OracleNumericInfo {
        base: BASE as usize,
        nb_digits: nb_digits.to_vec(),
    }
}

pub async fn refresh_wallet(wallet: &DlcDevKitWallet, expected_funds: u64) {
    let mut retry = 0;
    let balance = wallet.get_balance().await.unwrap().confirmed.to_sat();
    while balance < expected_funds {
        if retry > 30 {
            panic!("Wallet refresh taking too long.")
        }
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;
        wallet.sync().await.unwrap();
        retry += 1;
    }
}

pub fn rpc_client() -> Client {
    let user = std::env::var("BITCOIND_USER").expect("BITCOIND_USER must be set");
    let pass = std::env::var("BITCOIND_PASS").expect("BITCOIND_PASS must be set");
    let host =
        std::env::var("BITCOIND_HOST").unwrap_or_else(|_| "http://localhost:18443".to_owned());
    Client::new(&host, Auth::UserPass(user, pass)).unwrap()
}

pub async fn init_clients(
    logger: Arc<Logger>,
    esplora: Arc<EsploraClient>,
    offer_amount: Amount,
    accept_amount: Amount,
) -> (
    DlcDevKitWallet,
    Arc<MemoryStorage>,
    DlcDevKitWallet,
    Arc<MemoryStorage>,
    Client,
) {
    let sink_rpc = rpc_client();

    tokio::time::sleep(std::time::Duration::from_millis(100)).await;
    let offer_rpc = create_and_fund_wallet(logger.clone(), esplora.clone(), offer_amount).await;
    let accept_rpc = create_and_fund_wallet(logger.clone(), esplora.clone(), accept_amount).await;

    let sink_address = sink_rpc
        .get_new_address(None, Some(AddressType::Bech32))
        .unwrap()
        .assume_checked();

    sink_rpc.generate_to_address(101, &sink_address).unwrap(); //should be 101

    (
        offer_rpc.0,
        offer_rpc.1,
        accept_rpc.0,
        accept_rpc.1,
        sink_rpc,
    )
}

pub async fn create_and_fund_wallet(
    logger: Arc<Logger>,
    esplora: Arc<EsploraClient>,
    amount: Amount,
) -> (DlcDevKitWallet, Arc<MemoryStorage>) {
    let sink_rpc = rpc_client();
    let sink_address = sink_rpc
        .get_new_address(None, None)
        .unwrap()
        .assume_checked();
    let mut seed = [0u8; 64];
    seed.try_fill(&mut bitcoin::key::rand::thread_rng())
        .unwrap();
    let memory_storage = Arc::new(MemoryStorage::new());
    let wallet = DlcDevKitWallet::new(
        &seed,
        esplora,
        Network::Regtest,
        memory_storage.clone(),
        None,
        logger.clone(),
    )
    .await
    .unwrap();

    let address = wallet.new_external_address().await.unwrap().address;
    if amount > Amount::ZERO {
        sink_rpc
            .send_to_address(&address, amount, None, None, None, None, None, None)
            .unwrap();
    }

    sink_rpc.generate_to_address(5, &sink_address).unwrap();
    let mut done = false;
    while !done {
        wallet.sync().await.unwrap();
        let balance = wallet.get_balance().await.unwrap();
        if balance.confirmed > Amount::ZERO || amount == Amount::ZERO {
            done = true;
        }
    }
    (wallet, memory_storage)
}

/// Utility function used to parse hex into a target u8 buffer. Returns
/// the number of bytes converted or an error if it encounters an invalid
/// character or unexpected end of string.
#[allow(clippy::result_unit_err)] // This is just a test util
pub fn from_hex(hex: &str, target: &mut [u8]) -> Result<usize, ()> {
    if hex.len() % 2 == 1 || hex.len() > target.len() * 2 {
        return Err(());
    }

    let mut b = 0;
    let mut idx = 0;
    for c in hex.bytes() {
        b <<= 4;
        match c {
            b'A'..=b'F' => b |= c - b'A' + 10,
            b'a'..=b'f' => b |= c - b'a' + 10,
            b'0'..=b'9' => b |= c - b'0',
            _ => return Err(()),
        }
        if (idx & 1) == 1 {
            target[idx / 2] = b;
            b = 0;
        }
        idx += 1;
    }
    Ok(idx / 2)
}

/// Transforms an hex string to a Vec<u8>.
/// Panics if the string is not valid hex.
pub fn str_to_hex(hex_str: &str) -> Vec<u8> {
    let mut hex = vec![0; hex_str.len() / 2];
    from_hex(hex_str, &mut hex).unwrap();
    hex
}

/// Serialize a transaction to an lower hex string.
pub fn tx_to_string(tx: &Transaction) -> String {
    let mut writer = Vec::new();
    tx.consensus_encode(&mut writer).unwrap();
    let mut serialized = String::new();
    for x in writer {
        write!(&mut serialized, "{:02x}", x).unwrap();
    }
    serialized
}

/// Deserialize an hex string to a bitcoin transaction.
/// Panics if given invalid hex or data.
pub fn tx_from_string(tx_str: &str) -> Transaction {
    let tx_hex = str_to_hex(tx_str);
    Transaction::consensus_decode(&mut tx_hex.as_slice()).unwrap()
}

thread_local! {
  static MOCK_TIME: RefCell<u64> = RefCell::new(0);
}

pub struct MockTime {}

impl Time for MockTime {
    fn unix_time_now(&self) -> u64 {
        MOCK_TIME.with(|f| *f.borrow())
    }
}

pub fn set_time(time: u64) {
    MOCK_TIME.with(|f| {
        *f.borrow_mut() = time;
    });
}