ddk-trie 1.0.11

Data structures for storage and retrival of numerical Discreet 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
//! Utility functions to decompose numeric outcome values

use ddk_dlc::{Payout, RangePayout};

/// Decompose a numeric value into digits in the specified base. If the decomposed
/// value contains less than `nb_digits`, zeroes will be prepended to reach `nb_digits`
/// size.
pub fn decompose_value(mut value: usize, base: usize, nb_digits: usize) -> Vec<usize> {
    let mut res = Vec::new();

    while value > 0 {
        res.push(value % base);
        value = ((value as f64) / (base as f64)).floor() as usize;
    }

    while res.len() < nb_digits {
        res.push(0);
    }

    assert_eq!(nb_digits, res.len());

    res.into_iter().rev().collect()
}

/// Takes a decomposed representation of a numerical value in a given base and returns
/// the represented value as a `usize`
pub fn compose_value(values: &[usize], base: usize) -> usize {
    let mut composed = 0;
    for i in 0..values.len() {
        let pow = values.len() - i - 1;
        composed += values[i] * base.pow(pow as u32);
    }

    composed
}

/// Takes a vector or `RangePayout` and (if necessary) updates the first element
/// to cover the range [0, first_end] where first_end is the end value of the
/// first element, and updates the last element to cover the range
/// [last_start, max_val] where last_start is the start value of the last
/// `RangePayout` in `original_outcomes` and max_val is equal to base^nb_digits - 1.
pub fn pad_range_payouts(
    original_outcomes: Vec<RangePayout>,
    base: usize,
    nb_digits: usize,
) -> Vec<RangePayout> {
    let mut outcomes: Vec<RangePayout> = original_outcomes;
    if outcomes[0].start != 0 {
        outcomes[0] = RangePayout {
            start: 0,
            count: outcomes[0].count + outcomes[0].start,
            payout: Payout {
                offer: outcomes[0].payout.offer,
                accept: outcomes[0].payout.accept,
            },
        };
    }
    let last_index = outcomes.len() - 1;
    let last_outcome = &outcomes[last_index];
    let max_value = base.pow(nb_digits as u32);
    if last_outcome.start + last_outcome.count != max_value {
        outcomes[last_index] = RangePayout {
            start: last_outcome.start,
            count: max_value - last_outcome.start,
            payout: Payout {
                offer: last_outcome.payout.offer,
                accept: last_outcome.payout.accept,
            },
        }
    }

    outcomes
}

/// Takes away the common prefix of start and end and returns it.
#[inline]
fn take_prefix(start: &mut Vec<usize>, end: &mut Vec<usize>) -> Vec<usize> {
    if start == end {
        end.clear();
        return core::mem::take(start);
    }
    let mut i = 0;
    while start[i] == end[i] {
        i += 1;
    }

    start.drain(0..i);
    end.drain(0..i).collect()
}

/// Remove the trailing digits of `v` if equal to `to_remove`.
#[inline]
fn remove_tail(v: &mut Vec<usize>, to_remove: usize) {
    while v.len() > 1 && v[v.len() - 1] == to_remove {
        v.pop();
    }
}

/// Returns the set of decomposed prefixes that cover the range [start, end].
///
/// # Panics
///
/// Panics if `start` is greater than `end`.
pub fn group_by_ignoring_digits(
    start: usize,
    end: usize,
    base: usize,
    nb_digits: usize,
) -> Vec<Vec<usize>> {
    assert!(start <= end);

    let mut ds = decompose_value(start, base, nb_digits);
    let mut de = decompose_value(end, base, nb_digits);

    // We take the common prefix of start and end and save it, so we are guaranteed that ds[0] != de[0].
    let prefix = take_prefix(&mut ds, &mut de);

    // If start is all 0 and end is all base - 1, the prefix is enough to cover the interval.
    if (ds.is_empty() && de.is_empty())
        || (ds.iter().all(|x| *x == 0) && de.iter().all(|x| *x == base - 1))
    {
        return vec![prefix];
    }

    // We can remove the trailing 0s from the start and trailing base - 1 from the end
    // as they will be covered the interval represented by the digits in front of them.
    remove_tail(&mut ds, 0);
    remove_tail(&mut de, base - 1);

    // We initialize the stack with the start digits.
    let mut stack = ds.clone();
    let mut list = Vec::new();

    // This will generate all the prefixes for the interval [start, start[0]..base - 1]. E.g.
    // if start is 1234 in base 10, this will generate for [1234, 1999].
    while stack.len() != 1 {
        let i = stack.len() - 1;
        // Once the last digit of the stack is base - 1, we can save the prefix and pop a digit.
        // E.g. if we have our stack as [1, 2, 3, 9], next is [1, 2, 4, 0], but we don't need the last 0
        // as we can cover with [1, 2, 4].
        if stack[i] == base - 1 {
            list.push(stack.clone());
            stack.pop();
            // We can remove any base - 1 digits at this point. E.g. if we had [1, 2, 9, 9] above,
            // now we have [1, 2, 9], next is [1, 3, 0], but similarly as above we can get rid of
            // the trailing zero.
            remove_tail(&mut stack, base - 1);
            // We increment the last digit (e.g. move from [1, 2] to [1, 3] in the example above).
            let j = stack.len() - 1;
            stack[j] += 1;
        } else if stack[i] == 0 {
            // We can always get rid of trailing zeros (up to the first digit). E.g. if we have
            // out stack as [1, 3, 0, 0], [1, 3] is enough to cover the interval
            remove_tail(&mut stack, 0);
        } else {
            // We save the stack an increment the last digit. E.g. if we had [1, 2, 3, 4], we save it
            // and move to [1, 2, 3, 5].
            list.push(stack.clone());
            stack[i] += 1;
        }
        assert!(stack.iter().all(|x| x < &base));
    }

    // All the single digits in ]start[0]; end[0][ are sufficient to cover their respective intervals.
    // E.g. with start = 1234 and end = 4567, 2___ and 3___ are enough to cover between 2000 and 3999.
    while stack[0] != de[0] {
        list.push(stack.clone());
        stack[0] += 1;
    }

    // We take care of the interval [end[0]..0; end]. E.g. if end is 4567 that's [4000; 4567].
    while stack != de {
        let i = stack.len() - 1;
        // If stack has common prefix with end, we need to push a zero. E.g. if stack is [4], we
        // want then have stack as [4, 0], so we will cover [4000; 4499] with (40, 41, 42, 43).
        if stack[i] == de[i] {
            stack.push(0);
        } else {
            // We save the stack and increment the last digit. E.g. if we have [4, 0], we move to [4, 1].
            list.push(stack.clone());
            stack[i] += 1;
        }
    }

    // We need to include end (previous condition exit when stack is equal to end).
    list.push(de);

    // We add the common prefix of start and end if there was one and return our list of prefixes.
    if !prefix.is_empty() {
        list.into_iter()
            .map(|mut x| {
                let mut p = prefix.clone();
                p.append(&mut x);
                p
            })
            .collect()
    } else {
        list
    }
}

#[cfg(test)]
mod tests {
    use bitcoin::Amount;
    use ddk_dlc::{Payout, RangePayout};
    struct DecompositionTestCase {
        composed: usize,
        decomposed: Vec<usize>,
        base: usize,
        nb_digits: usize,
    }

    struct GroupingTestCase {
        start_index: usize,
        end_index: usize,
        base: usize,
        nb_digits: usize,
        expected: Vec<Vec<usize>>,
    }
    struct SetMaxRangeTestCase {
        original: Vec<RangePayout>,
        expected: Vec<RangePayout>,
        base: usize,
        nb_digits: usize,
    }

    fn decomposition_test_cases() -> Vec<DecompositionTestCase> {
        vec![
            DecompositionTestCase {
                composed: 123456789,
                decomposed: vec![1, 2, 3, 4, 5, 6, 7, 8, 9],
                base: 10,
                nb_digits: 9,
            },
            DecompositionTestCase {
                composed: 4321,
                decomposed: vec![1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1],
                base: 2,
                nb_digits: 13,
            },
            DecompositionTestCase {
                composed: 0,
                decomposed: vec![0, 0, 0, 0],
                base: 8,
                nb_digits: 4,
            },
            DecompositionTestCase {
                composed: 2,
                decomposed: vec![0, 2],
                base: 10,
                nb_digits: 2,
            },
            DecompositionTestCase {
                composed: 1,
                decomposed: vec![1],
                base: 2,
                nb_digits: 1,
            },
        ]
    }

    fn grouping_test_cases() -> Vec<GroupingTestCase> {
        vec![
            GroupingTestCase {
                start_index: 123,
                end_index: 123,
                base: 10,
                nb_digits: 3,
                expected: vec![vec![1, 2, 3]],
            },
            GroupingTestCase {
                start_index: 171,
                end_index: 210,
                base: 16,
                nb_digits: 2,
                expected: vec![
                    vec![10, 11],
                    vec![10, 12],
                    vec![10, 13],
                    vec![10, 14],
                    vec![10, 15],
                    vec![11],
                    vec![12],
                    vec![13, 0],
                    vec![13, 1],
                    vec![13, 2],
                ],
            },
            GroupingTestCase {
                start_index: 73899,
                end_index: 73938,
                base: 16,
                nb_digits: 6,
                expected: vec![
                    vec![0, 1, 2, 0, 10, 11],
                    vec![0, 1, 2, 0, 10, 12],
                    vec![0, 1, 2, 0, 10, 13],
                    vec![0, 1, 2, 0, 10, 14],
                    vec![0, 1, 2, 0, 10, 15],
                    vec![0, 1, 2, 0, 11],
                    vec![0, 1, 2, 0, 12],
                    vec![0, 1, 2, 0, 13, 0],
                    vec![0, 1, 2, 0, 13, 1],
                    vec![0, 1, 2, 0, 13, 2],
                ],
            },
            GroupingTestCase {
                start_index: 1234,
                end_index: 4321,
                base: 10,
                nb_digits: 4,
                expected: vec![
                    vec![1, 2, 3, 4],
                    vec![1, 2, 3, 5],
                    vec![1, 2, 3, 6],
                    vec![1, 2, 3, 7],
                    vec![1, 2, 3, 8],
                    vec![1, 2, 3, 9],
                    vec![1, 2, 4],
                    vec![1, 2, 5],
                    vec![1, 2, 6],
                    vec![1, 2, 7],
                    vec![1, 2, 8],
                    vec![1, 2, 9],
                    vec![1, 3],
                    vec![1, 4],
                    vec![1, 5],
                    vec![1, 6],
                    vec![1, 7],
                    vec![1, 8],
                    vec![1, 9],
                    vec![2],
                    vec![3],
                    vec![4, 0],
                    vec![4, 1],
                    vec![4, 2],
                    vec![4, 3, 0],
                    vec![4, 3, 1],
                    vec![4, 3, 2, 0],
                    vec![4, 3, 2, 1],
                ],
            },
            GroupingTestCase {
                start_index: 1201234,
                end_index: 1204321,
                base: 10,
                nb_digits: 8,
                expected: vec![
                    vec![0, 1, 2, 0, 1, 2, 3, 4],
                    vec![0, 1, 2, 0, 1, 2, 3, 5],
                    vec![0, 1, 2, 0, 1, 2, 3, 6],
                    vec![0, 1, 2, 0, 1, 2, 3, 7],
                    vec![0, 1, 2, 0, 1, 2, 3, 8],
                    vec![0, 1, 2, 0, 1, 2, 3, 9],
                    vec![0, 1, 2, 0, 1, 2, 4],
                    vec![0, 1, 2, 0, 1, 2, 5],
                    vec![0, 1, 2, 0, 1, 2, 6],
                    vec![0, 1, 2, 0, 1, 2, 7],
                    vec![0, 1, 2, 0, 1, 2, 8],
                    vec![0, 1, 2, 0, 1, 2, 9],
                    vec![0, 1, 2, 0, 1, 3],
                    vec![0, 1, 2, 0, 1, 4],
                    vec![0, 1, 2, 0, 1, 5],
                    vec![0, 1, 2, 0, 1, 6],
                    vec![0, 1, 2, 0, 1, 7],
                    vec![0, 1, 2, 0, 1, 8],
                    vec![0, 1, 2, 0, 1, 9],
                    vec![0, 1, 2, 0, 2],
                    vec![0, 1, 2, 0, 3],
                    vec![0, 1, 2, 0, 4, 0],
                    vec![0, 1, 2, 0, 4, 1],
                    vec![0, 1, 2, 0, 4, 2],
                    vec![0, 1, 2, 0, 4, 3, 0],
                    vec![0, 1, 2, 0, 4, 3, 1],
                    vec![0, 1, 2, 0, 4, 3, 2, 0],
                    vec![0, 1, 2, 0, 4, 3, 2, 1],
                ],
            },
            GroupingTestCase {
                start_index: 2200,
                end_index: 4999,
                base: 10,
                nb_digits: 4,
                expected: vec![
                    vec![2, 2],
                    vec![2, 3],
                    vec![2, 4],
                    vec![2, 5],
                    vec![2, 6],
                    vec![2, 7],
                    vec![2, 8],
                    vec![2, 9],
                    vec![3],
                    vec![4],
                ],
            },
            GroupingTestCase {
                start_index: 100,
                end_index: 199,
                base: 10,
                nb_digits: 3,
                expected: vec![vec![1]],
            },
            GroupingTestCase {
                start_index: 100,
                end_index: 200,
                base: 10,
                nb_digits: 3,
                expected: vec![vec![1], vec![2, 0, 0]],
            },
            GroupingTestCase {
                start_index: 11,
                end_index: 18,
                base: 10,
                nb_digits: 2,
                expected: vec![
                    vec![1, 1],
                    vec![1, 2],
                    vec![1, 3],
                    vec![1, 4],
                    vec![1, 5],
                    vec![1, 6],
                    vec![1, 7],
                    vec![1, 8],
                ],
            },
            GroupingTestCase {
                start_index: 11,
                end_index: 23,
                base: 2,
                nb_digits: 5,
                expected: vec![vec![0, 1, 0, 1, 1], vec![0, 1, 1], vec![1, 0]],
            },
            GroupingTestCase {
                start_index: 5677,
                end_index: 8621,
                base: 2,
                nb_digits: 14,
                expected: vec![
                    vec![0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1],
                    vec![0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1],
                    vec![0, 1, 0, 1, 1, 0, 0, 0, 1, 1],
                    vec![0, 1, 0, 1, 1, 0, 0, 1],
                    vec![0, 1, 0, 1, 1, 0, 1],
                    vec![0, 1, 0, 1, 1, 1],
                    vec![0, 1, 1],
                    vec![1, 0, 0, 0, 0, 0],
                    vec![1, 0, 0, 0, 0, 1, 0],
                    vec![1, 0, 0, 0, 0, 1, 1, 0, 0],
                    vec![1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0],
                    vec![1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0],
                    vec![1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0],
                ],
            },
        ]
    }

    fn get_max_range_test_cases() -> Vec<SetMaxRangeTestCase> {
        vec![
            SetMaxRangeTestCase {
                original: vec![
                    RangePayout {
                        start: 10,
                        count: 10,
                        payout: Payout {
                            offer: Amount::ZERO,
                            accept: Amount::from_sat(10),
                        },
                    },
                    RangePayout {
                        start: 20,
                        count: 10,
                        payout: Payout {
                            offer: Amount::from_sat(10),
                            accept: Amount::ZERO,
                        },
                    },
                ],
                expected: vec![
                    RangePayout {
                        start: 0,
                        count: 20,
                        payout: Payout {
                            offer: Amount::ZERO,
                            accept: Amount::from_sat(10),
                        },
                    },
                    RangePayout {
                        start: 20,
                        count: 80,
                        payout: Payout {
                            offer: Amount::from_sat(10),
                            accept: Amount::ZERO,
                        },
                    },
                ],
                base: 10,
                nb_digits: 2,
            },
            SetMaxRangeTestCase {
                original: vec![RangePayout {
                    start: 10,
                    count: 50,
                    payout: Payout {
                        offer: Amount::ZERO,
                        accept: Amount::from_sat(10),
                    },
                }],
                expected: vec![RangePayout {
                    start: 0,
                    count: 100,
                    payout: Payout {
                        offer: Amount::ZERO,
                        accept: Amount::from_sat(10),
                    },
                }],
                base: 10,
                nb_digits: 2,
            },
            SetMaxRangeTestCase {
                original: vec![
                    RangePayout {
                        start: 10,
                        count: 10,
                        payout: Payout {
                            offer: Amount::ZERO,
                            accept: Amount::from_sat(10),
                        },
                    },
                    RangePayout {
                        start: 20,
                        count: 10,
                        payout: Payout {
                            offer: Amount::from_sat(10),
                            accept: Amount::ZERO,
                        },
                    },
                ],
                expected: vec![
                    RangePayout {
                        start: 0,
                        count: 20,
                        payout: Payout {
                            offer: Amount::ZERO,
                            accept: Amount::from_sat(10),
                        },
                    },
                    RangePayout {
                        start: 20,
                        count: 12,
                        payout: Payout {
                            offer: Amount::from_sat(10),
                            accept: Amount::ZERO,
                        },
                    },
                ],
                base: 2,
                nb_digits: 5,
            },
            SetMaxRangeTestCase {
                original: vec![
                    RangePayout {
                        start: 0,
                        count: 20,
                        payout: Payout {
                            offer: Amount::ZERO,
                            accept: Amount::from_sat(10),
                        },
                    },
                    RangePayout {
                        start: 20,
                        count: 10,
                        payout: Payout {
                            offer: Amount::from_sat(10),
                            accept: Amount::ZERO,
                        },
                    },
                ],
                expected: vec![
                    RangePayout {
                        start: 0,
                        count: 20,
                        payout: Payout {
                            offer: Amount::ZERO,
                            accept: Amount::from_sat(10),
                        },
                    },
                    RangePayout {
                        start: 20,
                        count: 12,
                        payout: Payout {
                            offer: Amount::from_sat(10),
                            accept: Amount::ZERO,
                        },
                    },
                ],
                base: 2,
                nb_digits: 5,
            },
        ]
    }

    #[test]
    fn compose_value_test() {
        for test_case in decomposition_test_cases() {
            assert_eq!(
                test_case.composed,
                super::compose_value(&test_case.decomposed, test_case.base)
            );
        }
    }

    #[test]
    fn decompose_value_test() {
        for test_case in decomposition_test_cases() {
            assert_eq!(
                test_case.decomposed,
                super::decompose_value(test_case.composed, test_case.base, test_case.nb_digits)
            );
        }
    }

    #[test]
    fn group_by_ignoring_digits_test() {
        for test_case in grouping_test_cases() {
            assert_eq!(
                test_case.expected,
                super::group_by_ignoring_digits(
                    test_case.start_index,
                    test_case.end_index,
                    test_case.base,
                    test_case.nb_digits
                )
            );
        }
    }

    #[test]
    #[should_panic]
    fn group_by_ignoring_digits_start_greater_than_end_panics() {
        super::group_by_ignoring_digits(11, 10, 2, 4);
    }

    #[test]
    fn get_max_ranges_test() {
        for test_case in get_max_range_test_cases() {
            assert_eq!(
                test_case.expected,
                super::pad_range_payouts(test_case.original, test_case.base, test_case.nb_digits)
            );
        }
    }
}