hopr_strategy/
aggregating.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
//! ## Aggregating Strategy
//! This strategy automates ticket aggregation on different channel/ticket events.
//! Note that the aggregating strategy can be combined with the Auto Redeem Strategy above.
//!
//! Ticket aggregation is an interactive process and requires cooperation of the ticket issuer, the aggregation
//! will fail if the aggregation takes more than `aggregation_timeout` (in seconds). This does not affect runtime of the
//! strategy, since the ticket aggregation and awaiting it is performed on a separate thread.
//!
//! This strategy listens for two distinct channel events and triggers the interactive aggregation based on different criteria:
//!
//! ### 1) New winning acknowledged ticket event
//!
//! This strategy listens to newly added acknowledged winning tickets and once the amount of tickets in a certain channel reaches
//! an `aggregation_threshold`, the strategy will initiate ticket aggregation in that channel.
//! The strategy can independently also check if the unrealized balance (current balance _minus_ total unredeemed unaggregated tickets value) in a certain channel
//! has not gone over `unrelalized_balance_ratio` percent of the current balance in that channel. If that happens, the strategy will also initiate
//! ticket aggregation.
//!
//! ### 2) Channel transition from `Open` to `PendingToClose` event
//!
//! If the `aggregate_on_channel_close` flag is set, the aggregation will be triggered once a channel transitions from `Open` to `PendingToClose` state.
//! This behavior does not have any additional criteria, unlike in the previous event, but there must be at least 2 tickets in the channel.
//!
//!
//! For details on default parameters see [AggregatingStrategyConfig].
use async_trait::async_trait;
use hopr_async_runtime::prelude::{spawn, JoinHandle};
use hopr_crypto_types::prelude::Hash;
use hopr_db_sql::api::tickets::{AggregationPrerequisites, HoprDbTicketOperations};
use hopr_db_sql::channels::HoprDbChannelOperations;
use hopr_internal_types::prelude::*;
use hopr_transport_protocol::ticket_aggregation::processor::TicketAggregatorTrait;

use async_lock::RwLock;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use std::collections::HashMap;
use std::fmt::Debug;
use std::{
    fmt::{Display, Formatter},
    sync::Arc,
};
use tracing::{debug, error, info, warn};
use validator::Validate;

use crate::{strategy::SingularStrategy, Strategy};

#[cfg(all(feature = "prometheus", not(test)))]
use hopr_metrics::metrics::SimpleCounter;

#[cfg(all(feature = "prometheus", not(test)))]
lazy_static::lazy_static! {
    static ref METRIC_COUNT_AGGREGATIONS: SimpleCounter =
        SimpleCounter::new("hopr_strategy_aggregating_aggregation_count", "Count of initiated automatic aggregations").unwrap();
}

use hopr_platform::time::native::current_time;

/// Configuration object for the `AggregatingStrategy`
#[serde_as]
#[derive(Debug, Clone, Copy, PartialEq, smart_default::SmartDefault, Validate, Serialize, Deserialize)]
pub struct AggregatingStrategyConfig {
    /// Number of acknowledged winning tickets in a channel that triggers the ticket aggregation
    /// in that channel when exceeded.
    ///
    /// This condition is independent of `unrealized_balance_ratio`.
    ///
    /// Default is 100.
    #[validate(range(min = 2))]
    #[default(Some(100))]
    pub aggregation_threshold: Option<u32>,

    /// Percentage of unrealized balance in unaggregated tickets in a channel
    /// that triggers the ticket aggregation when exceeded.
    ///
    /// The unrealized balance in this case is the proportion of the channel balance allocated in unredeemed unaggregated tickets.
    /// This condition is independent of `aggregation_threshold`.
    ///
    /// Default is 0.9
    #[validate(range(min = 0_f32, max = 1.0_f32))]
    #[default(Some(0.9))]
    pub unrealized_balance_ratio: Option<f32>,

    /// If set, the strategy will automatically aggregate tickets in channel that has transitioned
    /// to the `PendingToClose` state.
    ///
    /// This happens regardless if `aggregation_threshold` or `unrealized_balance_ratio` thresholds are met on that channel.
    /// If the aggregation on-close fails, the tickets are automatically sent for redeeming instead.
    ///
    /// Default is true.
    #[default = true]
    pub aggregate_on_channel_close: bool,
}

impl From<AggregatingStrategyConfig> for AggregationPrerequisites {
    fn from(value: AggregatingStrategyConfig) -> Self {
        AggregationPrerequisites {
            min_ticket_count: value.aggregation_threshold.map(|x| x as usize),
            min_unaggregated_ratio: value.unrealized_balance_ratio.map(|x| x as f64),
        }
    }
}

/// Represents a strategy that starts aggregating tickets in a certain
/// channel, once the number of acknowledged tickets in that channel goes
/// above the given threshold.
/// Optionally, the strategy can also redeem the aggregated ticket, if the aggregation
/// was successful.
pub struct AggregatingStrategy<Db>
where
    Db: HoprDbTicketOperations + Send + Sync + Clone + std::fmt::Debug,
{
    db: Db,
    ticket_aggregator: Arc<dyn TicketAggregatorTrait + Send + Sync + 'static>,
    cfg: AggregatingStrategyConfig,
    #[allow(clippy::type_complexity)]
    agg_tasks: Arc<RwLock<HashMap<Hash, (bool, JoinHandle<()>)>>>,
}

impl<Db> Debug for AggregatingStrategy<Db>
where
    Db: HoprDbTicketOperations + Send + Sync + Clone + std::fmt::Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", Strategy::Aggregating(self.cfg))
    }
}

impl<Db> Display for AggregatingStrategy<Db>
where
    Db: HoprDbTicketOperations + Send + Sync + Clone + std::fmt::Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", Strategy::Aggregating(self.cfg))
    }
}

impl<Db> AggregatingStrategy<Db>
where
    Db: HoprDbTicketOperations + Send + Sync + Clone + std::fmt::Debug,
{
    pub fn new(
        cfg: AggregatingStrategyConfig,
        db: Db,
        ticket_aggregator: Arc<dyn TicketAggregatorTrait + Send + Sync + 'static>,
    ) -> Self {
        Self {
            db,
            cfg,
            ticket_aggregator,
            agg_tasks: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

impl<Db> AggregatingStrategy<Db>
where
    Db: HoprDbChannelOperations + HoprDbTicketOperations + Send + Sync + Clone + std::fmt::Debug + 'static,
{
    async fn try_start_aggregation(
        &self,
        channel_id: Hash,
        criteria: AggregationPrerequisites,
    ) -> crate::errors::Result<()> {
        if !self.is_strategy_aggregating_in_channel(channel_id).await {
            debug!("checking aggregation in {channel_id} with criteria {criteria:?}...");

            let agg_tasks_clone = self.agg_tasks.clone();
            let aggregator_clone = self.ticket_aggregator.clone();
            let (can_remove_tx, can_remove_rx) = futures::channel::oneshot::channel();
            let task = spawn(async move {
                match aggregator_clone.aggregate_tickets(&channel_id, criteria).await {
                    Ok(_) => {
                        debug!("tried ticket aggregation in channel {channel_id} without any issues");

                        #[cfg(all(feature = "prometheus", not(test)))]
                        METRIC_COUNT_AGGREGATIONS.increment();
                    }
                    Err(e) => {
                        error!("cannot complete aggregation in channel {channel_id}: {e}");
                    }
                }

                // Wait until we're added to the aggregation tasks table
                let _ = can_remove_rx.await;
                if let Some((done, _)) = agg_tasks_clone.write().await.get_mut(&channel_id) {
                    *done = true;
                }
            });

            self.agg_tasks.write().await.insert(channel_id, (false, task));
            let _ = can_remove_tx.send(()); // Allow the task to mark itself as done
        } else {
            warn!(channel = %channel_id, "this strategy already aggregates in channel");
        }

        Ok(())
    }

    async fn is_strategy_aggregating_in_channel(&self, channel_id: Hash) -> bool {
        let existing = self.agg_tasks.read().await.get(&channel_id).map(|(done, _)| *done);
        if let Some(done) = existing {
            // Task exists, check if it has been completed
            if done {
                if let Some((_, task)) = self.agg_tasks.write().await.remove(&channel_id) {
                    // Task has been completed, remove it and await its join handle
                    let _ = task.await;
                    false
                } else {
                    // Should not happen, but means there's no more aggregation task for the channel
                    false
                }
            } else {
                // There's still a running aggregation task for this channel
                true
            }
        } else {
            // No aggregation task found for this channel
            false
        }
    }
}

#[async_trait]
impl<Db> SingularStrategy for AggregatingStrategy<Db>
where
    Db: HoprDbChannelOperations + HoprDbTicketOperations + Clone + Send + Sync + std::fmt::Debug + 'static,
{
    async fn on_tick(&self) -> crate::errors::Result<()> {
        let incoming = self
            .db
            .get_incoming_channels(None)
            .await
            .map_err(hopr_db_sql::api::errors::DbError::from)?
            .into_iter()
            .filter(|c| !c.closure_time_passed(current_time()))
            .map(|c| c.get_id());

        for channel_id in incoming {
            if let Err(e) = self.try_start_aggregation(channel_id, self.cfg.into()).await {
                debug!("skipped aggregation in channel {channel_id}: {e}");
            }
        }

        Ok(())
    }

    async fn on_own_channel_changed(
        &self,
        channel: &ChannelEntry,
        direction: ChannelDirection,
        change: ChannelChange,
    ) -> crate::errors::Result<()> {
        if !self.cfg.aggregate_on_channel_close || direction != ChannelDirection::Incoming {
            return Ok(());
        }

        if let ChannelChange::Status { left: old, right: new } = change {
            if old != ChannelStatus::Open || !matches!(new, ChannelStatus::PendingToClose(_)) {
                debug!("ignoring channel {channel} state change that's not in PendingToClose");
                return Ok(());
            }

            info!(%channel, "going to aggregate tickets in channel because it transitioned to PendingToClose");

            // On closing there must be at least 2 tickets to justify aggregation
            let on_close_agg_prerequisites = AggregationPrerequisites {
                min_ticket_count: Some(2),
                min_unaggregated_ratio: None,
            };

            Ok(self
                .try_start_aggregation(channel.get_id(), on_close_agg_prerequisites)
                .await?)
        } else {
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::strategy::SingularStrategy;
    use anyhow::Context;
    use async_std::prelude::FutureExt as AsyncStdFutureExt;
    use futures::{pin_mut, FutureExt, StreamExt};
    use hex_literal::hex;
    use hopr_crypto_types::prelude::*;
    use hopr_db_sql::accounts::HoprDbAccountOperations;
    use hopr_db_sql::api::{info::DomainSeparator, tickets::HoprDbTicketOperations};
    use hopr_db_sql::channels::HoprDbChannelOperations;
    use hopr_db_sql::db::HoprDb;
    use hopr_db_sql::errors::DbSqlError;
    use hopr_db_sql::info::HoprDbInfoOperations;
    use hopr_db_sql::{HoprDbGeneralModelOperations, TargetDb};
    use hopr_internal_types::prelude::*;
    use hopr_primitive_types::prelude::*;
    use hopr_transport_protocol::ticket_aggregation::processor::{
        AwaitingAggregator, TicketAggregationInteraction, TicketAggregationProcessed,
    };
    use lazy_static::lazy_static;
    use std::ops::Add;
    use std::pin::pin;
    use std::sync::Arc;
    use std::time::Duration;
    use tracing::{debug, error};

    lazy_static! {
        static ref PEERS: Vec<OffchainKeypair> = vec![
            hex!("b91a28ff9840e9c93e5fafd581131f0b9f33f3e61b02bf5dd83458aa0221f572"),
            hex!("82283757872f99541ce33a47b90c2ce9f64875abf08b5119a8a434b2fa83ea98")
        ]
        .iter()
        .map(|private| OffchainKeypair::from_secret(private).expect("lazy static keypair should be valid"))
        .collect();
        static ref PEERS_CHAIN: Vec<ChainKeypair> = vec![
            hex!("51d3003d908045a4d76d0bfc0d84f6ff946b5934b7ea6a2958faf02fead4567a"),
            hex!("e1f89073a01831d0eed9fe2c67e7d65c144b9d9945320f6d325b1cccc2d124e9"),
        ]
        .iter()
        .map(|private| ChainKeypair::from_secret(private).expect("lazy static keypair should be valid"))
        .collect();
    }

    fn mock_acknowledged_ticket(
        signer: &ChainKeypair,
        destination: &ChainKeypair,
        index: u64,
        index_offset: u32,
    ) -> anyhow::Result<AcknowledgedTicket> {
        let price_per_packet: U256 = 20_u32.into();
        let ticket_win_prob = 1.0f64;

        let channel_id = generate_channel_id(&signer.into(), &destination.into());

        let channel_epoch = 1u64;
        let domain_separator = Hash::default();

        let response = Response::try_from(
            Hash::create(&[channel_id.as_ref(), &channel_epoch.to_be_bytes(), &index.to_be_bytes()]).as_ref(),
        )?;

        Ok(TicketBuilder::default()
            .addresses(signer, destination)
            .amount(price_per_packet.div_f64(ticket_win_prob)?)
            .index(index)
            .index_offset(index_offset)
            .win_prob(ticket_win_prob)
            .channel_epoch(1)
            .challenge(response.to_challenge().into())
            .build_signed(signer, &domain_separator)?
            .into_acknowledged(response))
    }

    async fn populate_db_with_ack_tickets(
        db: HoprDb,
        amount: usize,
    ) -> anyhow::Result<(Vec<AcknowledgedTicket>, ChannelEntry)> {
        let db_clone = db.clone();
        let (acked_tickets, total_value) = db
            .begin_transaction_in_db(TargetDb::Tickets)
            .await?
            .perform(|tx| {
                Box::pin(async move {
                    let mut acked_tickets = Vec::new();
                    let mut total_value = Balance::zero(BalanceType::HOPR);

                    for i in 0..amount {
                        let acked_ticket = mock_acknowledged_ticket(&PEERS_CHAIN[0], &PEERS_CHAIN[1], i as u64, 1)
                            .expect("should be able to create an ack ticket");
                        debug!("inserting {acked_ticket}");

                        db_clone.upsert_ticket(Some(tx), acked_ticket.clone()).await?;

                        total_value = total_value.add(&acked_ticket.verified_ticket().amount);
                        acked_tickets.push(acked_ticket);
                    }

                    Ok::<_, DbSqlError>((acked_tickets, total_value))
                })
            })
            .await?;

        let channel = ChannelEntry::new(
            (&PEERS_CHAIN[0]).into(),
            (&PEERS_CHAIN[1]).into(),
            total_value,
            0_u32.into(),
            ChannelStatus::Open,
            1u32.into(),
        );

        Ok((acked_tickets, channel))
    }

    async fn init_db(db: HoprDb) -> anyhow::Result<()> {
        let db_clone = db.clone();
        db.begin_transaction()
            .await?
            .perform(|tx| {
                Box::pin(async move {
                    db_clone
                        .set_domain_separator(Some(tx), DomainSeparator::Channel, Hash::default())
                        .await?;
                    for i in 0..PEERS_CHAIN.len() {
                        debug!(
                            "linking {} with {}",
                            PEERS[i].public(),
                            PEERS_CHAIN[i].public().to_address()
                        );
                        db_clone
                            .insert_account(
                                Some(tx),
                                AccountEntry::new(
                                    *PEERS[i].public(),
                                    PEERS_CHAIN[i].public().to_address(),
                                    AccountType::NotAnnounced,
                                ),
                            )
                            .await?;
                    }
                    Ok::<_, DbSqlError>(())
                })
            })
            .await?;

        Ok(())
    }

    fn spawn_aggregation_interaction(
        db_alice: HoprDb,
        db_bob: HoprDb,
        key_alice: &ChainKeypair,
        key_bob: &ChainKeypair,
    ) -> anyhow::Result<(
        AwaitingAggregator<(), (), HoprDb>,
        futures::channel::oneshot::Receiver<()>,
    )> {
        let mut alice = TicketAggregationInteraction::<(), ()>::new(db_alice, key_alice);
        let mut bob = TicketAggregationInteraction::<(), ()>::new(db_bob.clone(), key_bob);

        let (tx, awaiter) = futures::channel::oneshot::channel::<()>();
        let bob_aggregator = bob.writer();

        async_std::task::spawn(async move {
            let mut finalizer = None;

            match bob.next().await {
                Some(TicketAggregationProcessed::Send(_, acked_tickets, request_finalizer)) => {
                    let _ = finalizer.insert(request_finalizer);
                    match alice.writer().receive_aggregation_request(
                        PEERS[1].public().into(),
                        acked_tickets.into_iter().map(TransferableWinningTicket::from).collect(),
                        (),
                    ) {
                        Ok(_) => {}
                        Err(e) => error!(error = %e, "Failed to received aggregation ticket"),
                    }
                }
                //  alice.ack_event_queue.0.start_send(super::TicketAggregationToProcess::ToProcess(destination, acked_tickets)),
                _ => panic!("unexpected action happened"),
            };

            match alice.next().await {
                Some(TicketAggregationProcessed::Reply(_, aggregated_ticket, ())) => {
                    match bob
                        .writer()
                        .receive_ticket(PEERS[0].public().into(), aggregated_ticket, ())
                    {
                        Ok(_) => {}
                        Err(e) => error!(error = %e, "Failed to receive a ticket"),
                    }
                }

                _ => panic!("unexpected action happened"),
            };

            match bob.next().await {
                Some(TicketAggregationProcessed::Receive(_destination, _ticket, ())) => (),
                _ => panic!("unexpected action happened"),
            };

            finalizer.expect("should have a value present").finalize();
            let _ = tx.send(());
        });

        Ok((
            AwaitingAggregator::new(db_bob, bob_aggregator, Duration::from_secs(5)),
            awaiter,
        ))
    }

    #[async_std::test]
    async fn test_strategy_aggregation_on_tick() -> anyhow::Result<()> {
        // db_0: Alice (channel source)
        // db_1: Bob (channel destination)
        let db_alice = HoprDb::new_in_memory(PEERS_CHAIN[0].clone()).await?;
        let db_bob = HoprDb::new_in_memory(PEERS_CHAIN[1].clone()).await?;

        init_db(db_alice.clone()).await?;
        init_db(db_bob.clone()).await?;

        let (bob_notify_tx, bob_notify_rx) = futures::channel::mpsc::unbounded();
        db_bob.start_ticket_processing(bob_notify_tx.into())?;

        let (_, channel) = populate_db_with_ack_tickets(db_bob.clone(), 5).await?;

        db_alice.upsert_channel(None, channel).await?;
        db_bob.upsert_channel(None, channel).await?;

        let (bob_aggregator, awaiter) =
            spawn_aggregation_interaction(db_alice.clone(), db_bob.clone(), &PEERS_CHAIN[0], &PEERS_CHAIN[1])?;

        let cfg = super::AggregatingStrategyConfig {
            aggregation_threshold: Some(5),
            unrealized_balance_ratio: None,
            aggregate_on_channel_close: false,
        };

        let aggregation_strategy = super::AggregatingStrategy::new(cfg, db_bob.clone(), Arc::new(bob_aggregator));

        //let threshold_ticket = acked_tickets.last().unwrap();
        aggregation_strategy.on_tick().await?;

        // Wait until aggregation has finished
        let f1 = pin!(awaiter);
        let f2 = pin!(async_std::task::sleep(Duration::from_secs(5)).fuse());
        let _ = futures::future::select(f1, f2).await;

        pin_mut!(bob_notify_rx);
        let notified_ticket = bob_notify_rx.next().await.expect("should have a ticket");

        let tickets = db_bob.get_tickets((&channel).into()).await?;
        assert_eq!(tickets.len(), 1, "there should be a single aggregated ticket");
        assert_eq!(notified_ticket, tickets[0]);

        Ok(())
    }

    #[async_std::test]
    async fn test_strategy_aggregation_on_tick_when_unrealized_balance_exceeded() -> anyhow::Result<()> {
        // db_0: Alice (channel source)
        // db_1: Bob (channel destination)
        let db_alice = HoprDb::new_in_memory(PEERS_CHAIN[0].clone()).await?;
        let db_bob = HoprDb::new_in_memory(PEERS_CHAIN[1].clone()).await?;

        init_db(db_alice.clone()).await?;
        init_db(db_bob.clone()).await?;

        let (bob_notify_tx, bob_notify_rx) = futures::channel::mpsc::unbounded();
        db_bob.start_ticket_processing(bob_notify_tx.into())?;

        let (_, channel) = populate_db_with_ack_tickets(db_bob.clone(), 4).await?;

        db_alice.upsert_channel(None, channel).await?;
        db_bob.upsert_channel(None, channel).await?;

        let (bob_aggregator, awaiter) =
            spawn_aggregation_interaction(db_alice.clone(), db_bob.clone(), &PEERS_CHAIN[0], &PEERS_CHAIN[1])?;

        let cfg = super::AggregatingStrategyConfig {
            aggregation_threshold: None,
            unrealized_balance_ratio: Some(0.75),
            aggregate_on_channel_close: false,
        };

        let aggregation_strategy = super::AggregatingStrategy::new(cfg, db_bob.clone(), Arc::new(bob_aggregator));

        //let threshold_ticket = acked_tickets.last().unwrap();
        aggregation_strategy.on_tick().await?;

        // Wait until aggregation has finished
        let f1 = pin!(awaiter);
        let f2 = pin!(async_std::task::sleep(Duration::from_secs(5)));
        let _ = futures::future::select(f1, f2).await;

        pin_mut!(bob_notify_rx);
        let notified_ticket = bob_notify_rx.next().await.expect("should have a ticket");

        let tickets = db_bob.get_tickets((&channel).into()).await?;
        assert_eq!(tickets.len(), 1, "there should be a single aggregated ticket");
        assert_eq!(notified_ticket, tickets[0]);

        Ok(())
    }

    #[async_std::test]
    async fn test_strategy_aggregation_on_tick_should_not_agg_when_unrealized_balance_exceeded_via_aggregated_tickets(
    ) -> anyhow::Result<()> {
        // db_0: Alice (channel source)
        // db_1: Bob (channel destination)
        let db_alice = HoprDb::new_in_memory(PEERS_CHAIN[0].clone()).await?;
        let db_bob = HoprDb::new_in_memory(PEERS_CHAIN[1].clone()).await?;

        init_db(db_alice.clone()).await?;
        init_db(db_bob.clone()).await?;

        db_bob.start_ticket_processing(None)?;

        const NUM_TICKETS: usize = 4;
        let (mut acked_tickets, mut channel) = populate_db_with_ack_tickets(db_bob.clone(), NUM_TICKETS).await?;

        let (bob_aggregator, awaiter) =
            spawn_aggregation_interaction(db_alice.clone(), db_bob.clone(), &PEERS_CHAIN[0], &PEERS_CHAIN[1])?;

        // Make this ticket aggregated
        acked_tickets[0] = mock_acknowledged_ticket(&PEERS_CHAIN[0], &PEERS_CHAIN[1], 0, 2)?;

        debug!("upserting {}", acked_tickets[0]);
        db_bob.upsert_ticket(None, acked_tickets[0].clone()).await?;

        let tickets = db_bob.get_tickets((&channel).into()).await?;
        assert_eq!(tickets.len(), NUM_TICKETS, "nothing should be aggregated");

        channel.balance = Balance::new(100_u32, BalanceType::HOPR);

        db_alice.upsert_channel(None, channel).await?;
        db_bob.upsert_channel(None, channel).await?;

        let cfg = super::AggregatingStrategyConfig {
            aggregation_threshold: None,
            unrealized_balance_ratio: Some(0.75),
            aggregate_on_channel_close: false,
        };

        let aggregation_strategy = super::AggregatingStrategy::new(cfg, db_bob.clone(), Arc::new(bob_aggregator));

        //let threshold_ticket = acked_tickets.last().unwrap();
        aggregation_strategy.on_tick().await?;

        let tickets = db_bob.get_tickets((&channel).into()).await?;
        assert_eq!(tickets.len(), NUM_TICKETS, "nothing should be aggregated");
        std::mem::drop(awaiter);

        Ok(())
    }

    #[async_std::test]
    async fn test_strategy_aggregation_on_channel_close() -> anyhow::Result<()> {
        // db_0: Alice (channel source)
        // db_1: Bob (channel destination)
        let db_alice = HoprDb::new_in_memory(PEERS_CHAIN[0].clone()).await?;
        let db_bob = HoprDb::new_in_memory(PEERS_CHAIN[1].clone()).await?;

        init_db(db_alice.clone()).await?;
        init_db(db_bob.clone()).await?;

        let (bob_notify_tx, bob_notify_rx) = futures::channel::mpsc::unbounded();
        db_bob.start_ticket_processing(bob_notify_tx.into())?;

        let (_, mut channel) = populate_db_with_ack_tickets(db_bob.clone(), 5).await?;

        let cfg = super::AggregatingStrategyConfig {
            aggregation_threshold: Some(100),
            unrealized_balance_ratio: None,
            aggregate_on_channel_close: true,
        };

        channel.status = ChannelStatus::PendingToClose(std::time::SystemTime::now());

        db_alice.upsert_channel(None, channel).await?;
        db_bob.upsert_channel(None, channel).await?;

        let (bob_aggregator, awaiter) =
            spawn_aggregation_interaction(db_alice.clone(), db_bob.clone(), &PEERS_CHAIN[0], &PEERS_CHAIN[1])?;

        let aggregation_strategy = super::AggregatingStrategy::new(cfg, db_alice.clone(), Arc::new(bob_aggregator));

        aggregation_strategy
            .on_own_channel_changed(
                &channel,
                ChannelDirection::Incoming,
                ChannelChange::Status {
                    left: ChannelStatus::Open,
                    right: ChannelStatus::PendingToClose(std::time::SystemTime::now()),
                },
            )
            .await?;

        // Wait until aggregation has finished
        awaiter.timeout(Duration::from_secs(5)).await.context("Timeout")??;

        pin_mut!(bob_notify_rx);
        let notified_ticket = bob_notify_rx.next().await.expect("should have a ticket");

        let tickets = db_bob.get_tickets((&channel).into()).await?;
        assert_eq!(tickets.len(), 1, "there should be a single aggregated ticket");
        assert_eq!(notified_ticket, tickets[0]);

        Ok(())
    }

    #[async_std::test]
    async fn test_strategy_aggregation_on_tick_should_not_agg_on_channel_close_if_only_single_ticket(
    ) -> anyhow::Result<()> {
        // db_0: Alice (channel source)
        // db_1: Bob (channel destination)
        let db_alice = HoprDb::new_in_memory(PEERS_CHAIN[0].clone()).await?;
        let db_bob = HoprDb::new_in_memory(PEERS_CHAIN[1].clone()).await?;

        init_db(db_alice.clone()).await?;
        init_db(db_bob.clone()).await?;

        db_bob.start_ticket_processing(None)?;

        const NUM_TICKETS: usize = 1;
        let (_, channel) = populate_db_with_ack_tickets(db_bob.clone(), NUM_TICKETS).await?;

        let (bob_aggregator, awaiter) =
            spawn_aggregation_interaction(db_alice.clone(), db_bob.clone(), &PEERS_CHAIN[0], &PEERS_CHAIN[1])?;

        let tickets = db_bob.get_tickets((&channel).into()).await?;
        assert_eq!(tickets.len(), NUM_TICKETS, "should have a single ticket");

        db_alice.upsert_channel(None, channel).await?;
        db_bob.upsert_channel(None, channel).await?;

        let cfg = super::AggregatingStrategyConfig {
            aggregation_threshold: Some(100),
            unrealized_balance_ratio: Some(0.75),
            aggregate_on_channel_close: true,
        };

        let aggregation_strategy = super::AggregatingStrategy::new(cfg, db_bob.clone(), Arc::new(bob_aggregator));

        aggregation_strategy
            .on_own_channel_changed(
                &channel,
                ChannelDirection::Incoming,
                ChannelChange::Status {
                    left: ChannelStatus::Open,
                    right: ChannelStatus::PendingToClose(std::time::SystemTime::now()),
                },
            )
            .await?;

        awaiter
            .timeout(Duration::from_millis(500))
            .await
            .expect_err("should timeout");

        let tickets = db_bob.get_tickets((&channel).into()).await?;
        assert_eq!(tickets.len(), NUM_TICKETS, "nothing should be aggregated");
        Ok(())
    }
}