iop_morpheus_node/
txns.rs

1use super::*;
2
3pub(super) struct DidTransactionItem<'a> {
4    pub did: String,
5    pub txid: &'a str,
6    pub height: BlockHeight,
7}
8
9#[derive(Debug, Default, Clone)]
10pub(super) struct DidTransactionsState {
11    map: HashMap<String, Vec<TransactionIdWithHeight>>,
12}
13
14impl DidTransactionsState {
15    pub fn get_between(
16        &self, did: &str, from_height_inc: BlockHeight, until_height_inc: Option<BlockHeight>,
17    ) -> Option<impl Iterator<Item = &'_ TransactionIdWithHeight> + '_> {
18        self.map.get(did).map(move |txns| {
19            txns.iter().filter(move |item| {
20                is_height_in_range_inc_until(item.height, Some(from_height_inc), until_height_inc)
21            })
22        })
23    }
24
25    pub fn apply(&mut self, item: DidTransactionItem) {
26        let (did, txid, height) = (item.did, item.txid, item.height);
27        let txns = self.map.entry(did).or_default();
28        if txns.iter().all(|i| i.transaction_id != txid) {
29            txns.insert(0, TransactionIdWithHeight { transaction_id: txid.to_owned(), height });
30        }
31    }
32
33    pub fn revert(&mut self, item: DidTransactionItem) {
34        // NOTE A transaction might include multiple operations related to a single Did.
35        //      Reverting the first operation attempt already removed the transactionId
36        //      from the array, we are likely processing the next related operation here
37        if let Some(txns) = self.map.get_mut(&item.did) {
38            txns.retain(|i| i.transaction_id != item.txid);
39        }
40    }
41}