hopr_transport_protocol/ticket_aggregation/
processor.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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
use futures::stream::{Stream, StreamExt};
use futures::{
    channel::{
        mpsc::{channel, Receiver, Sender},
        oneshot,
    },
    future::{poll_fn, Either},
    pin_mut,
};
use libp2p::request_response::{OutboundRequestId, ResponseChannel};
use rust_stream_ext_concurrent::then_concurrent::StreamThenConcurrentExt;
use std::{pin::Pin, task::Poll};
use tracing::{error, warn};

use hopr_async_runtime::prelude::{sleep, spawn};
use hopr_crypto_types::prelude::*;
use hopr_db_api::{
    errors::DbError,
    tickets::{AggregationPrerequisites, HoprDbTicketOperations},
};
use hopr_internal_types::prelude::*;
use hopr_transport_identity::PeerId;

use crate::errors::{
    ProtocolError::{Retry, TransportError},
    Result,
};

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

#[cfg(all(feature = "prometheus", not(test)))]
lazy_static::lazy_static! {
    static ref METRIC_AGGREGATED_TICKETS: SimpleCounter = SimpleCounter::new(
        "hopr_aggregated_tickets_count",
        "Number of aggregated tickets"
    )
    .unwrap();
    static ref METRIC_AGGREGATION_COUNT: SimpleCounter = SimpleCounter::new(
        "hopr_aggregations_count",
        "Number of performed ticket aggregations"
    )
    .unwrap();
}

// Default sizes of the acknowledgement queues
pub const TICKET_AGGREGATION_TX_QUEUE_SIZE: usize = 2048;
pub const TICKET_AGGREGATION_RX_QUEUE_SIZE: usize = 2048;

/// The input to the processor background pipeline
#[allow(clippy::type_complexity)] // TODO: The type needs to be significantly refactored to easily move around
#[allow(clippy::large_enum_variant)] // TODO: refactor the large types used in the enum
#[derive(Debug)]
pub enum TicketAggregationToProcess<T, U> {
    ToReceive(PeerId, std::result::Result<Ticket, String>, U),
    ToProcess(PeerId, Vec<TransferableWinningTicket>, T),
    ToSend(Hash, AggregationPrerequisites, TicketAggregationFinalizer),
}

/// Emitted by the processor background pipeline once processed
#[allow(clippy::large_enum_variant)] // TODO: refactor the large types used in the enum
#[derive(Debug)]
pub enum TicketAggregationProcessed<T, U> {
    Receive(PeerId, AcknowledgedTicket, U),
    Reply(PeerId, std::result::Result<Ticket, String>, T),
    Send(
        PeerId,
        Vec<hopr_internal_types::legacy::AcknowledgedTicket>,
        TicketAggregationFinalizer,
    ),
}

#[async_trait::async_trait]
pub trait TicketAggregatorTrait {
    /// Pushes a new collection of tickets into the processing.
    async fn aggregate_tickets(&self, channel: &Hash, prerequisites: AggregationPrerequisites) -> Result<()>;
}

#[derive(Debug)]
pub struct AwaitingAggregator<T, U, Db>
where
    Db: HoprDbTicketOperations + Send + Sync + Clone + std::fmt::Debug,
    T: Send,
    U: Send,
{
    db: Db,
    writer: TicketAggregationActions<T, U>,
    agg_timeout: std::time::Duration,
}

impl<T, U, Db> Clone for AwaitingAggregator<T, U, Db>
where
    Db: HoprDbTicketOperations + Send + Sync + Clone + std::fmt::Debug,
    T: Send,
    U: Send,
{
    fn clone(&self) -> Self {
        Self {
            db: self.db.clone(),
            writer: self.writer.clone(),
            agg_timeout: self.agg_timeout,
        }
    }
}

impl<T, U, Db> AwaitingAggregator<T, U, Db>
where
    Db: HoprDbTicketOperations + Send + Sync + Clone + std::fmt::Debug,
    T: Send,
    U: Send,
{
    pub fn new(db: Db, writer: TicketAggregationActions<T, U>, agg_timeout: std::time::Duration) -> Self {
        Self {
            db,
            writer,
            agg_timeout,
        }
    }
}

#[async_trait::async_trait]
impl<T, U, Db> TicketAggregatorTrait for AwaitingAggregator<T, U, Db>
where
    Db: HoprDbTicketOperations + Send + Sync + Clone + std::fmt::Debug,
    T: Send,
    U: Send,
{
    #[tracing::instrument(level = "debug", skip(self))]
    async fn aggregate_tickets(&self, channel: &Hash, prerequisites: AggregationPrerequisites) -> Result<()> {
        let awaiter = self.writer.clone().aggregate_tickets(channel, prerequisites)?;

        if let Err(e) = awaiter.consume_and_wait(self.agg_timeout).await {
            warn!(%channel, error = %e, "Error during ticket aggregation, performing a rollback");
            self.db.rollback_aggregation_in_channel(*channel).await?;
        }

        Ok(())
    }
}

#[derive(Debug)]
pub struct TicketAggregationAwaiter {
    rx: oneshot::Receiver<()>,
}

impl From<oneshot::Receiver<()>> for TicketAggregationAwaiter {
    fn from(value: oneshot::Receiver<()>) -> Self {
        Self { rx: value }
    }
}

impl TicketAggregationAwaiter {
    pub async fn consume_and_wait(self, until_timeout: std::time::Duration) -> Result<()> {
        let timeout = sleep(until_timeout);
        let resolve = self.rx;

        pin_mut!(resolve, timeout);
        match futures::future::select(resolve, timeout).await {
            Either::Left((result, _)) => result.map_err(|_| TransportError("Canceled".to_owned())),
            Either::Right(_) => Err(TransportError("Timed out on sending a packet".to_owned())),
        }
    }
}

#[derive(Debug)]
pub struct TicketAggregationFinalizer {
    tx: Option<oneshot::Sender<()>>,
}

impl TicketAggregationFinalizer {
    pub fn new(tx: oneshot::Sender<()>) -> Self {
        Self { tx: Some(tx) }
    }

    pub fn finalize(mut self) {
        if let Some(sender) = self.tx.take() {
            if sender.send(()).is_err() {
                error!("Failed to notify the awaiter about the successful ticket aggregation")
            }
        } else {
            error!("Sender for packet send signalization is already spent")
        }
    }
}

/// External API for feeding Ticket Aggregation actions into the Ticket Aggregation
/// processor processing the elements independently in the background.
#[derive(Debug)]
pub struct TicketAggregationActions<T, U> {
    pub queue: Sender<TicketAggregationToProcess<T, U>>,
}

pub type BasicTicketAggregationActions<T> = TicketAggregationActions<ResponseChannel<T>, OutboundRequestId>;

impl<T, U> Clone for TicketAggregationActions<T, U> {
    /// Generic type requires handwritten clone function
    fn clone(&self) -> Self {
        Self {
            queue: self.queue.clone(),
        }
    }
}

impl<T, U> TicketAggregationActions<T, U> {
    /// Pushes the aggregated ticket received from the transport layer into processing.
    pub fn receive_ticket(
        &mut self,
        source: PeerId,
        ticket: std::result::Result<Ticket, String>,
        request: U,
    ) -> Result<()> {
        self.process(TicketAggregationToProcess::ToReceive(source, ticket, request))
    }

    /// Process the received aggregation request
    pub fn receive_aggregation_request(
        &mut self,
        source: PeerId,
        tickets: Vec<TransferableWinningTicket>,
        request: T,
    ) -> Result<()> {
        self.process(TicketAggregationToProcess::ToProcess(source, tickets, request))
    }

    /// Pushes a new collection of tickets into the processing.
    pub fn aggregate_tickets(
        &mut self,
        channel: &Hash,
        prerequisites: AggregationPrerequisites,
    ) -> Result<TicketAggregationAwaiter> {
        let (tx, rx) = oneshot::channel::<()>();

        self.process(TicketAggregationToProcess::ToSend(
            *channel,
            prerequisites,
            TicketAggregationFinalizer::new(tx),
        ))?;

        Ok(rx.into())
    }

    fn process(&mut self, event: TicketAggregationToProcess<T, U>) -> Result<()> {
        self.queue.try_send(event).map_err(|e| {
            if e.is_full() {
                Retry
            } else if e.is_disconnected() {
                TransportError("queue is closed".to_string())
            } else {
                TransportError(format!("Unknown error: {}", e))
            }
        })
    }
}

type AckEventQueue<T, U> = (
    Sender<TicketAggregationToProcess<T, U>>,
    Receiver<TicketAggregationProcessed<T, U>>,
);

/// Sets up processing of ticket aggregation interactions and returns relevant read and write mechanism.
pub struct TicketAggregationInteraction<T, U>
where
    T: Send,
    U: Send,
{
    ack_event_queue: AckEventQueue<T, U>,
}

impl<T: 'static, U: 'static> TicketAggregationInteraction<T, U>
where
    T: Send,
    U: Send,
{
    /// Creates a new instance given the DB to process the ticket aggregation requests.
    pub fn new<Db>(db: Db, chain_key: &ChainKeypair) -> Self
    where
        Db: HoprDbTicketOperations + Send + Sync + Clone + std::fmt::Debug + 'static,
    {
        let (processing_in_tx, processing_in_rx) = channel::<TicketAggregationToProcess<T, U>>(
            TICKET_AGGREGATION_RX_QUEUE_SIZE + TICKET_AGGREGATION_TX_QUEUE_SIZE,
        );
        let (processing_out_tx, processing_out_rx) = channel::<TicketAggregationProcessed<T, U>>(
            TICKET_AGGREGATION_RX_QUEUE_SIZE + TICKET_AGGREGATION_TX_QUEUE_SIZE,
        );

        let chain_key = chain_key.clone();

        let mut processing_stream = processing_in_rx.then_concurrent(move |event| {
            let chain_key = chain_key.clone();
            let db = db.clone();
            let mut processed_tx = processing_out_tx.clone();

            async move {
                let processed = match event {
                    TicketAggregationToProcess::ToProcess(destination, acked_tickets, response) => {
                        let opk: std::result::Result<OffchainPublicKey, hopr_primitive_types::errors::GeneralError> =
                            destination.try_into();
                        match opk {
                            Ok(opk) => match db.aggregate_tickets(opk, acked_tickets, &chain_key).await {
                                Ok(ticket) => Some(TicketAggregationProcessed::Reply(
                                    destination,
                                    Ok(ticket.leak()),
                                    response,
                                )),
                                Err(DbError::TicketAggregationError(e)) => {
                                    // forward error to counterparty
                                    Some(TicketAggregationProcessed::Reply(destination, Err(e), response))
                                }
                                Err(e) => {
                                    error!(error = %e, "Dropping tickets aggregation request due to an error");
                                    None
                                }
                            },
                            Err(e) => {
                                error!(
                                    ?destination, error = %e,
                                    "Failed to deserialize the destination to an offchain public key"
                                );
                                None
                            }
                        }
                    }
                    TicketAggregationToProcess::ToReceive(destination, aggregated_ticket, request) => {
                        match aggregated_ticket {
                            Ok(ticket) => match db.process_received_aggregated_ticket(ticket.clone(), &chain_key).await
                            {
                                Ok(acked_ticket) => {
                                    Some(TicketAggregationProcessed::Receive(destination, acked_ticket, request))
                                }
                                Err(e) => {
                                    error!(error = %e, "Error while handling aggregated ticket");
                                    None
                                }
                            },
                            Err(e) => {
                                warn!(error = %e, "Counterparty refused to aggregate tickets");
                                None
                            }
                        }
                    }
                    TicketAggregationToProcess::ToSend(channel, prerequsites, finalizer) => {
                        match db.prepare_aggregation_in_channel(&channel, prerequsites).await {
                            Ok(Some((source, tickets, dst))) if !tickets.is_empty() => {
                                #[cfg(all(feature = "prometheus", not(test)))]
                                {
                                    METRIC_AGGREGATED_TICKETS.increment_by(tickets.len() as u64);
                                    METRIC_AGGREGATION_COUNT.increment();
                                }

                                // TODO: remove this transformation in 3.0 once proper aggregation protocol format is introduced
                                let addr = chain_key.public().to_address();
                                let tickets = tickets
                                    .into_iter()
                                    .map(|t| hopr_internal_types::legacy::AcknowledgedTicket::new(t, &addr, &dst))
                                    .collect::<Vec<_>>();
                                Some(TicketAggregationProcessed::Send(source.into(), tickets, finalizer))
                            }
                            Err(e) => {
                                error!(error = %e, "An error occured when preparing the channel aggregation");
                                None
                            }
                            _ => {
                                finalizer.finalize();
                                None
                            }
                        }
                    }
                };

                if let Some(event) = processed {
                    match poll_fn(|cx| Pin::new(&mut processed_tx).poll_ready(cx)).await {
                        Ok(_) => match processed_tx.start_send(event) {
                            Ok(_) => {}
                            Err(e) => error!(error = %e, "Failed to pass a processed ack message"),
                        },
                        Err(e) => {
                            warn!(error = %e, "The receiver for processed ack no longer exists");
                        }
                    };
                }
            }
        });

        // NOTE: This spawned task does not need to be explicitly canceled, since it will
        // be automatically dropped when the event sender object is dropped.
        spawn(async move {
            // poll the stream until it's done
            while processing_stream.next().await.is_some() {}
        });

        Self {
            ack_event_queue: (processing_in_tx, processing_out_rx),
        }
    }

    pub fn writer(&self) -> TicketAggregationActions<T, U> {
        TicketAggregationActions {
            queue: self.ack_event_queue.0.clone(),
        }
    }
}

impl<T, U> Stream for TicketAggregationInteraction<T, U>
where
    T: Send,
    U: Send,
{
    type Item = TicketAggregationProcessed<T, U>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Option<Self::Item>> {
        Pin::new(self).ack_event_queue.1.poll_next_unpin(cx)
    }
}

#[cfg(test)]
mod tests {
    use super::TicketAggregationProcessed;
    use async_std::prelude::FutureExt;
    use futures::pin_mut;
    use futures::stream::StreamExt;
    use hex_literal::hex;
    use hopr_crypto_types::{
        keypairs::{ChainKeypair, Keypair, OffchainKeypair},
        types::{Hash, Response},
    };
    use hopr_db_sql::accounts::HoprDbAccountOperations;
    use hopr_db_sql::api::tickets::HoprDbTicketOperations;
    use hopr_db_sql::channels::HoprDbChannelOperations;
    use hopr_db_sql::info::HoprDbInfoOperations;
    use hopr_db_sql::HoprDbGeneralModelOperations;
    use hopr_db_sql::{api::info::DomainSeparator, db::HoprDb};
    use hopr_internal_types::prelude::*;
    use hopr_primitive_types::prelude::*;
    use lazy_static::lazy_static;
    use std::ops::{Add, Mul};
    use std::time::Duration;

    lazy_static! {
        static ref PEERS: Vec<OffchainKeypair> = [
            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> = [
            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,
    ) -> anyhow::Result<AcknowledgedTicket> {
        let price_per_packet: U256 = 10000000000000000u128.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(1)
            .win_prob(ticket_win_prob)
            .channel_epoch(1)
            .challenge(response.to_challenge().into())
            .build_signed(signer, &domain_separator)?
            .into_acknowledged(response))
    }

    async fn init_db(db: HoprDb) -> anyhow::Result<()> {
        let db_clone = db.clone();

        let peers = PEERS.clone();
        let peers_chain = PEERS_CHAIN.clone();

        db.begin_transaction()
            .await?
            .perform(move |tx| {
                Box::pin(async move {
                    db_clone
                        .set_domain_separator(Some(tx), DomainSeparator::Channel, Hash::default())
                        .await?;
                    for (offchain, chain) in peers.iter().zip(peers_chain.iter()) {
                        db_clone
                            .insert_account(
                                Some(tx),
                                AccountEntry::new(
                                    *offchain.public(),
                                    chain.public().to_address(),
                                    AccountType::NotAnnounced,
                                ),
                            )
                            .await?
                    }

                    Ok::<(), hopr_db_sql::errors::DbSqlError>(())
                })
            })
            .await?;

        Ok(())
    }

    #[async_std::test]
    async fn test_ticket_aggregation() -> anyhow::Result<()> {
        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?;

        const NUM_TICKETS: u64 = 30;

        let mut tickets = vec![];
        let mut agg_balance = Balance::zero(BalanceType::HOPR);
        // Generate acknowledged tickets
        for i in 1..=NUM_TICKETS {
            let mut ack_ticket = mock_acknowledged_ticket(&PEERS_CHAIN[0], &PEERS_CHAIN[1], i)?;

            // Mark the first ticket as redeemed, so it does not enter the aggregation
            if i == 1 {
                ack_ticket.status = AcknowledgedTicketStatus::BeingRedeemed;
            } else {
                agg_balance = agg_balance.add(&ack_ticket.verified_ticket().amount);
            }

            tickets.push(ack_ticket)
        }

        let alice_addr: Address = (&PEERS_CHAIN[0]).into();
        let bob_addr: Address = (&PEERS_CHAIN[1]).into();

        let alice_packet_key = PEERS[0].public().into();
        let bob_packet_key = PEERS[1].public().into();

        let channel_alice_bob = ChannelEntry::new(
            alice_addr,
            bob_addr,
            agg_balance.mul(10),
            1_u32.into(),
            ChannelStatus::Open,
            1u32.into(),
        );

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

        for ticket in tickets.into_iter() {
            db_bob.upsert_ticket(None, ticket).await?;
        }

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

        let mut alice = super::TicketAggregationInteraction::<(), ()>::new(db_alice.clone(), &PEERS_CHAIN[0]);
        let mut bob = super::TicketAggregationInteraction::<(), ()>::new(db_bob.clone(), &PEERS_CHAIN[1]);

        let awaiter = bob
            .writer()
            .aggregate_tickets(&channel_alice_bob.get_id(), Default::default())?;

        let mut finalizer = None;
        match bob.next().timeout(Duration::from_secs(5)).await {
            Ok(Some(TicketAggregationProcessed::Send(_, acked_tickets, request_finalizer))) => {
                let _ = finalizer.insert(request_finalizer);
                assert_eq!(
                    NUM_TICKETS - 1,
                    acked_tickets.len() as u64,
                    "invalid number of tickets to aggregate"
                );
                alice.writer().receive_aggregation_request(
                    bob_packet_key,
                    acked_tickets.into_iter().map(TransferableWinningTicket::from).collect(),
                    (),
                )?;
            }
            _ => panic!("unexpected action happened while sending agg request by Bob"),
        };

        match alice.next().timeout(Duration::from_secs(5)).await {
            Ok(Some(TicketAggregationProcessed::Reply(_, aggregated_ticket, ()))) => {
                bob.writer().receive_ticket(alice_packet_key, aggregated_ticket, ())?
            }
            _ => panic!("unexpected action happened while awaiting agg request at Alice"),
        };

        match bob.next().timeout(Duration::from_secs(5)).await {
            Ok(Some(TicketAggregationProcessed::Receive(_destination, _acked_tkt, ()))) => {
                finalizer.take().expect("finalizer should be present").finalize()
            }
            _ => panic!("unexpected action happened while awaiting agg response at Bob"),
        }

        pin_mut!(bob_notify_rx);
        bob_notify_rx
            .next()
            .await
            .expect("bob should have received the ticket notification");

        let stored_acked_tickets = db_bob.get_tickets((&channel_alice_bob).into()).await?;

        assert_eq!(
            stored_acked_tickets.len(),
            2,
            "there should be 1 aggregated ticket and 1 ticket being redeemed"
        );

        assert_eq!(
            AcknowledgedTicketStatus::BeingRedeemed,
            stored_acked_tickets[0].status,
            "first ticket must be being redeemed"
        );
        assert!(
            stored_acked_tickets[1].verified_ticket().is_aggregated(),
            "last ticket must be the aggregated one"
        );
        assert_eq!(
            AcknowledgedTicketStatus::Untouched,
            stored_acked_tickets[1].status,
            "second ticket must be untouched"
        );
        assert_eq!(
            agg_balance,
            stored_acked_tickets[1].verified_ticket().amount,
            "aggregated balance invalid"
        );

        Ok(awaiter.consume_and_wait(Duration::from_millis(2000)).await?)
    }

    #[async_std::test]
    async fn test_ticket_aggregation_skip_lower_indices() -> anyhow::Result<()> {
        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())?;

        const NUM_TICKETS: u64 = 30;
        const CHANNEL_TICKET_IDX: u64 = 20;

        let mut tickets = vec![];
        let mut agg_balance = Balance::zero(BalanceType::HOPR);
        // Generate acknowledged tickets
        for i in 1..=NUM_TICKETS {
            let ack_ticket = mock_acknowledged_ticket(&PEERS_CHAIN[0], &PEERS_CHAIN[1], i)?;
            if i >= CHANNEL_TICKET_IDX {
                agg_balance = agg_balance.add(&ack_ticket.verified_ticket().amount);
            }
            tickets.push(ack_ticket)
        }

        let alice_addr: Address = (&PEERS_CHAIN[0]).into();
        let bob_addr: Address = (&PEERS_CHAIN[1]).into();

        let alice_packet_key = PEERS[0].public().into();
        let bob_packet_key = PEERS[1].public().into();

        let channel_alice_bob = ChannelEntry::new(
            alice_addr,
            bob_addr,
            agg_balance.mul(10),
            CHANNEL_TICKET_IDX.into(),
            ChannelStatus::Open,
            1u32.into(),
        );

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

        for ticket in tickets.into_iter() {
            db_bob.upsert_ticket(None, ticket).await?;
        }

        let mut alice = super::TicketAggregationInteraction::<(), ()>::new(db_alice.clone(), &PEERS_CHAIN[0]);
        let mut bob = super::TicketAggregationInteraction::<(), ()>::new(db_bob.clone(), &PEERS_CHAIN[1]);

        let awaiter = bob
            .writer()
            .aggregate_tickets(&channel_alice_bob.get_id(), Default::default())?;

        let mut finalizer = None;
        match bob.next().timeout(Duration::from_secs(5)).await {
            Ok(Some(TicketAggregationProcessed::Send(_, acked_tickets, request_finalizer))) => {
                let _ = finalizer.insert(request_finalizer);
                assert_eq!(
                    NUM_TICKETS - CHANNEL_TICKET_IDX + 1,
                    acked_tickets.len() as u64,
                    "invalid number of tickets to aggregate"
                );
                alice.writer().receive_aggregation_request(
                    bob_packet_key,
                    acked_tickets.into_iter().map(TransferableWinningTicket::from).collect(),
                    (),
                )?;
            }
            _ => panic!("unexpected action happened while sending agg request by Bob"),
        };

        match alice.next().timeout(Duration::from_secs(5)).await {
            Ok(Some(TicketAggregationProcessed::Reply(_, aggregated_ticket, ()))) => {
                bob.writer().receive_ticket(alice_packet_key, aggregated_ticket, ())?
            }
            _ => panic!("unexpected action happened while awaiting agg request at Alice"),
        };

        match bob.next().timeout(Duration::from_secs(5)).await {
            Ok(Some(TicketAggregationProcessed::Receive(_destination, _acked_tkt, ()))) => {
                finalizer.take().expect("finalizer should be present").finalize()
            }
            _ => panic!("unexpected action happened while awaiting agg response at Bob"),
        }

        pin_mut!(bob_notify_rx);
        bob_notify_rx
            .next()
            .await
            .expect("bob should have received the ticket notification");

        let stored_acked_tickets = db_bob.get_tickets((&channel_alice_bob).into()).await?;

        assert_eq!(
            stored_acked_tickets.len(),
            20,
            "there should be 1 aggregated ticket and 19 old tickets"
        );

        assert!(
            stored_acked_tickets[19].verified_ticket().is_aggregated(),
            "last ticket must be the aggregated one"
        );
        for i in 0..19 {
            assert_eq!(
                AcknowledgedTicketStatus::Untouched,
                stored_acked_tickets[i].status,
                "ticket #{i} must be untouched"
            );
        }
        assert_eq!(
            agg_balance,
            stored_acked_tickets[19].verified_ticket().amount,
            "aggregated balance invalid"
        );

        Ok(awaiter.consume_and_wait(Duration::from_millis(2000)).await?)
    }
}