transaction_pool/
error.rs

1// Copyright 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use std::{error, fmt, result};
10
11/// Transaction Pool Error
12#[derive(Debug)]
13pub enum Error<Hash: fmt::Debug + fmt::LowerHex> {
14	/// Transaction is already imported
15	AlreadyImported(Hash),
16	/// Transaction is too cheap to enter the queue
17	TooCheapToEnter(Hash, String),
18	/// Transaction is too cheap to replace existing transaction that occupies the same slot.
19	TooCheapToReplace(Hash, Hash),
20}
21
22/// Transaction Pool Result
23pub type Result<T, H> = result::Result<T, Error<H>>;
24
25impl<H: fmt::Debug + fmt::LowerHex> fmt::Display for Error<H> {
26	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27		match self {
28			Error::AlreadyImported(h) => write!(f, "[{:?}] already imported", h),
29			Error::TooCheapToEnter(hash, min_score) => {
30				write!(f, "[{:x}] too cheap to enter the pool. Min score: {}", hash, min_score)
31			}
32			Error::TooCheapToReplace(old_hash, hash) => write!(f, "[{:x}] too cheap to replace: {:x}", hash, old_hash),
33		}
34	}
35}
36
37impl<H: fmt::Debug + fmt::LowerHex> error::Error for Error<H> {}
38
39#[cfg(test)]
40impl<H: fmt::Debug + fmt::LowerHex> PartialEq for Error<H>
41where
42	H: PartialEq,
43{
44	fn eq(&self, other: &Self) -> bool {
45		use self::Error::*;
46
47		match (self, other) {
48			(&AlreadyImported(ref h1), &AlreadyImported(ref h2)) => h1 == h2,
49			(&TooCheapToEnter(ref h1, ref s1), &TooCheapToEnter(ref h2, ref s2)) => h1 == h2 && s1 == s2,
50			(&TooCheapToReplace(ref old1, ref new1), &TooCheapToReplace(ref old2, ref new2)) => {
51				old1 == old2 && new1 == new2
52			}
53			_ => false,
54		}
55	}
56}