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
//! Transactions and their storage

use crate::core::ClientId;
use crate::utils::build_custom_error;
use rust_decimal::prelude::*;
use serde::{de, Deserialize};
use std::{any::Any, collections::HashMap, error::Error, fmt};
pub(crate) use transaction::*;
use typestate::typestate;

type TransactionId = u32;
type BoxedTransaction = Box<dyn Any + 'static>;

/// Storage for transactions. Holds mapping between transaction ID and the transaction itself
#[derive(Default, Debug)]
pub(crate) struct Ledger(HashMap<TransactionId, BoxedTransaction>);

impl Ledger {
    pub(crate) fn get_transaction_by_id(
        &mut self,
        tx_id: TransactionId,
    ) -> Option<&mut BoxedTransaction> {
        self.0.get_mut(&tx_id)
    }
    pub(crate) fn insert_transaction<S: 'static + TxState>(&mut self, tx: Tx<S>) {
        self.0.insert(tx.id, Box::new(tx));
    }
}

/// Transaction types
#[derive(Deserialize, Debug, Clone, PartialEq, Copy)]
#[serde(rename_all = "lowercase")]
pub(crate) enum TxType {
    Chargeback,
    Resolve,
    Dispute,
    Withdrawal,
    Deposit,
}

#[typestate]
pub(crate) mod transaction {
    use super::{de, fmt, ClientId, Decimal, Deserialize, TransactionId, TxType};

    /// A single transaction.
    /// This struct is used both for ser/deserializing by `sered`, and
    /// by `typestate` crate to generate `<TxState>` for this struct.
    #[derive(Deserialize, Debug, Clone, PartialEq, Copy)]
    #[serde(default, deny_unknown_fields)]
    #[automaton]
    pub struct Tx {
        #[serde(alias = "type")]
        pub(crate) tx_type: TxType,
        #[serde(alias = "client")]
        pub(crate) client_id: ClientId,
        #[serde(alias = "tx")]
        pub(crate) id: TransactionId,
        #[serde(deserialize_with = "deserialize_decimal")]
        pub(crate) amount: Option<Decimal>,
    }

    /// Default transaction state assigned to transactions which
    /// have been parsed. If trasaction is of type Withdrawal or Dispute,
    /// and has never been *disputed*, it will remain in Defualt state
    #[state]
    #[derive(Deserialize, Debug, Clone, PartialEq, Copy)]
    pub(crate) struct Default;
    /// Transaction state assigned to transactions which has been *disputed*
    #[state]
    #[derive(Deserialize, Debug, Clone, PartialEq, Copy)]
    pub(crate) struct Disputed;
    /// Transaction state assigned to transactions after disputed transaction
    /// has been *chargebacked* (returned)
    #[state]
    #[derive(Deserialize, Debug, Clone, PartialEq, Copy)]
    pub(crate) struct Chargebacked;

    pub(crate) trait Default {
        fn create() -> Default;
        fn dispute(self) -> Disputed;
    }
    pub(crate) trait Disputed {
        fn resolve(self) -> Default;
        fn chargeback(self) -> Chargebacked;
    }
    pub(crate) trait Chargebacked {
        fn archive(self);
    }
    fn deserialize_decimal<'de, D>(deserializer: D) -> Result<Option<Decimal>, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        let s: &str = de::Deserialize::deserialize(deserializer)?;
        match Decimal::from_str_exact(s) {
            Ok(v) => Ok(Some(v)),
            Err(_) => Ok(None),
        }
    }

    impl<State: std::fmt::Debug + TxState> fmt::Display for Tx<State> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(
                f,
                "type:{:?},client:{},tx:{},amount:{:?},state:{:?}",
                self.tx_type, self.client_id, self.id, self.amount, self.state
            )
        }
    }
}

impl std::default::Default for Tx<Default> {
    fn default() -> Tx<transaction::Default> {
        Tx::<transaction::Default> {
            amount: Some(Decimal::ONE),
            state: transaction::Default,
            client_id: 0,
            id: 0,
            tx_type: TxType::Chargeback,
        }
    }
}

impl DefaultState for Tx<transaction::Default> {
    fn create() -> Tx<transaction::Default> {
        Tx::<transaction::Default> {
            amount: Some(Decimal::ONE),
            state: transaction::Default,
            client_id: 8,
            id: 9,
            tx_type: TxType::Chargeback,
        }
    }
    fn dispute(self) -> Tx<Disputed> {
        Tx::<Disputed> {
            // repetition easliy avoidable with `..self`
            // but will have to wait once this is stable
            // https://github.com/rust-lang/rust/issues/86555
            state: Disputed,
            amount: self.amount,
            client_id: self.client_id,
            id: self.id,
            tx_type: self.tx_type,
        }
    }
}

impl DisputedState for Tx<Disputed> {
    fn resolve(self) -> Tx<transaction::Default> {
        Tx::<Default> {
            state: Default,
            amount: self.amount,
            client_id: self.client_id,
            id: self.id,
            tx_type: self.tx_type,
        }
    }
    fn chargeback(self) -> Tx<Chargebacked> {
        Tx::<Chargebacked> {
            state: Chargebacked,
            amount: self.amount,
            client_id: self.client_id,
            id: self.id,
            tx_type: self.tx_type,
        }
    }
}

build_custom_error!(
    IrreversableTransaction,
    "ERROR: Attempted to chargeback a transaction which is currently not disputed."
);
build_custom_error!(
    UnresolvableTransaction,
    "ERROR: Attempted to resolve a transaction which is currently not disputed."
);
build_custom_error!(
    UndisputableTransaction,
    "ERROR: Withdrawal transaction cannot be disputed."
);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn successful_tx_ledger_crud() {
        let mut l = Ledger::default();
        let t = Tx::<Default>::create();
        let t = t.dispute();
        l.insert_transaction(t);
        assert!(l.0.contains_key(&t.id));
        let crud_tx = l.get_transaction_by_id(t.id).unwrap();
        if let Some(crud_tx) = crud_tx.downcast_ref::<Tx<Disputed>>() {
            assert!(crud_tx.to_owned() == t);
        } else {
            assert!(false);
        }
    }

    #[test]
    fn insert_to_ledger_transactions_with_different_types() {
        let mut l = Ledger::default();
        let mut t = Tx::<Default>::create();
        t.id = 11; // avoid key collision in HashMap
        let t = t.dispute();
        l.insert_transaction(t);
        let t = Tx::<Default>::create();
        let t = t.dispute();
        let t = t.chargeback();
        l.insert_transaction(t);
        assert_eq!(l.0.values().len(), 2);
    }

    #[test]
    fn successful_resolve_after_dispute() -> Result<(), Box<dyn Error>> {
        let tx = Tx::<Default>::create();
        assert!(tx.state == Default);
        let tx = tx.dispute();
        assert!(tx.state == Disputed);
        let tx = tx.resolve();
        assert!(tx.state == Default);
        Ok(())
    }

    #[test]
    fn successful_chargeback_after_dispute() {
        let tx = Tx::<Default>::create();
        assert!(tx.state == Default);
        let tx = tx.dispute();
        assert!(tx.state == Disputed);
        let tx = tx.chargeback();
        assert!(tx.state == Chargebacked);
    }

    #[should_panic]
    #[test]
    fn failed_tx_ledger_crud() {
        let mut l = Ledger::default();
        l.get_transaction_by_id(888).unwrap();
    }
}