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
use bee_common::packable::Packable;
use serde::{Deserialize, Serialize};
use std::io::{Read, Write};
#[derive(Debug, thiserror::Error)]
pub enum ConflictError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Invalid conflict byte")]
InvalidConflict(u8),
}
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum ConflictReason {
None = 0,
InputUtxoAlreadySpent = 1,
InputUtxoAlreadySpentInThisMilestone = 2,
InputUtxoNotFound = 3,
InputOutputSumMismatch = 4,
InvalidSignature = 5,
InvalidDustAllowance = 6,
SemanticValidationFailed = 255,
}
impl Default for ConflictReason {
fn default() -> Self {
Self::None
}
}
impl TryFrom<u8> for ConflictReason {
type Error = ConflictError;
fn try_from(c: u8) -> Result<Self, Self::Error> {
Ok(match c {
0 => Self::None,
1 => Self::InputUtxoAlreadySpent,
2 => Self::InputUtxoAlreadySpentInThisMilestone,
3 => Self::InputUtxoNotFound,
4 => Self::InputOutputSumMismatch,
5 => Self::InvalidSignature,
6 => Self::InvalidDustAllowance,
255 => Self::SemanticValidationFailed,
x => return Err(Self::Error::InvalidConflict(x)),
})
}
}
impl Packable for ConflictReason {
type Error = ConflictError;
fn packed_len(&self) -> usize {
(*self as u8).packed_len()
}
fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Ok((*self as u8).pack(writer)?)
}
fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error>
where
Self: Sized,
{
u8::unpack_inner::<R, CHECK>(reader)?.try_into()
}
}