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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::unit_arg)]

use anyhow::Result;
#[cfg(any(test, feature = "fuzzing"))]
use proptest::prelude::*;
#[cfg(any(test, feature = "fuzzing"))]
use proptest_derive::Arbitrary;
use std::{convert::TryFrom, fmt};

/// A `MempoolStatus` is represented as a required status code that is semantic coupled with an optional sub status and message.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
#[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))]
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
pub struct MempoolStatus {
    /// insertion status code
    pub code: MempoolStatusCode,
    /// optional message
    pub message: String,
}

impl MempoolStatus {
    pub fn new(code: MempoolStatusCode) -> Self {
        Self {
            code,
            message: "".to_string(),
        }
    }

    /// Adds a message to the Mempool status.
    pub fn with_message(mut self, message: String) -> Self {
        self.message = message;
        self
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))]
#[repr(u64)]
pub enum MempoolStatusCode {
    // Transaction was accepted by Mempool
    Accepted = 0,
    // Sequence number is old, etc.
    InvalidSeqNumber = 1,
    // Mempool is full (reached max global capacity)
    MempoolIsFull = 2,
    // Account reached max capacity per account
    TooManyTransactions = 3,
    // Invalid update. Only gas price increase is allowed
    InvalidUpdate = 4,
    // transaction didn't pass vm_validation
    VmError = 5,
    UnknownStatus = 6,
}

impl TryFrom<u64> for MempoolStatusCode {
    type Error = &'static str;

    fn try_from(value: u64) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(MempoolStatusCode::Accepted),
            1 => Ok(MempoolStatusCode::InvalidSeqNumber),
            2 => Ok(MempoolStatusCode::MempoolIsFull),
            3 => Ok(MempoolStatusCode::TooManyTransactions),
            4 => Ok(MempoolStatusCode::InvalidUpdate),
            5 => Ok(MempoolStatusCode::VmError),
            6 => Ok(MempoolStatusCode::UnknownStatus),
            _ => Err("invalid StatusCode"),
        }
    }
}

impl From<MempoolStatusCode> for u64 {
    fn from(status: MempoolStatusCode) -> u64 {
        status as u64
    }
}

impl fmt::Display for MempoolStatusCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}