trident_fuzz/
error.rs

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
#![allow(dead_code)]

use solana_sdk::{pubkey::Pubkey, transaction::TransactionError};
use std::fmt::{Debug, Display};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum FuzzClientError {
    #[error("Custom fuzz client error: {0}")]
    Custom(u32),
    #[error("Transaction failed: {0}")]
    TransactionFailed(#[from] TransactionError),
}

#[derive(Debug, Error)]
pub enum FuzzingError {
    #[error("Custom fuzzing error: {0}\n")]
    Custom(u32),
    #[error("Fuzzing error with Custom Message: {0}\n")]
    CustomMessage(String),
}

impl FuzzClientError {
    pub(crate) fn with_origin(self, origin: Origin) -> FuzzClientErrorWithOrigin {
        let mut error_with_origin = FuzzClientErrorWithOrigin::from(self);
        error_with_origin.origin = Some(origin);
        error_with_origin
    }
    pub(crate) fn with_context(self, context: Context) -> FuzzClientErrorWithOrigin {
        let mut error_with_origin = FuzzClientErrorWithOrigin::from(self);
        error_with_origin.context = Some(context);
        error_with_origin
    }
}

impl FuzzingError {
    pub fn with_message(message: &str) -> Self {
        Self::CustomMessage(message.to_string())
    }
    pub(crate) fn with_origin(self, origin: Origin) -> FuzzingErrorWithOrigin {
        let mut error_with_origin = FuzzingErrorWithOrigin::from(self);
        error_with_origin.origin = Some(origin);
        error_with_origin
    }
    pub(crate) fn with_context(self, context: Context) -> FuzzingErrorWithOrigin {
        let mut error_with_origin = FuzzingErrorWithOrigin::from(self);
        error_with_origin.context = Some(context);
        error_with_origin
    }
}

#[derive(Debug)]
pub enum Origin {
    Instruction(String),
    Account(Pubkey),
}

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

#[derive(Debug)]
pub enum Context {
    Pre,
    Post,
}

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

#[derive(Debug)]
pub struct FuzzClientErrorWithOrigin {
    pub client_error: FuzzClientError,
    pub origin: Option<Origin>,
    pub context: Option<Context>,
}

#[derive(Debug)]
pub struct FuzzingErrorWithOrigin {
    pub fuzzing_error: FuzzingError,
    pub origin: Option<Origin>,
    pub context: Option<Context>,
}

impl From<FuzzClientError> for FuzzClientErrorWithOrigin {
    fn from(client_error: FuzzClientError) -> Self {
        Self {
            client_error,
            origin: None,
            context: None,
        }
    }
}

impl From<FuzzingError> for FuzzingErrorWithOrigin {
    fn from(fuzzing_error: FuzzingError) -> Self {
        Self {
            fuzzing_error,
            origin: None,
            context: None,
        }
    }
}
impl Display for FuzzClientErrorWithOrigin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.client_error, f)?;
        if let Some(o) = &self.origin {
            Display::fmt(o, f)?;
        }
        if let Some(c) = &self.context {
            Display::fmt(c, f)?;
        }
        Ok(())
    }
}
impl Display for FuzzingErrorWithOrigin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.fuzzing_error, f)?;
        if let Some(o) = &self.origin {
            Display::fmt(o, f)?;
        }
        if let Some(c) = &self.context {
            Display::fmt(c, f)?;
        }
        Ok(())
    }
}

impl FuzzClientErrorWithOrigin {
    pub fn with_origin(mut self, origin: Origin) -> Self {
        self.origin = Some(origin);
        self
    }
    pub fn with_context(mut self, context: Context) -> Self {
        self.context = Some(context);
        self
    }
}
impl FuzzingErrorWithOrigin {
    pub fn with_origin(mut self, origin: Origin) -> Self {
        self.origin = Some(origin);
        self
    }
    pub fn with_context(mut self, context: Context) -> Self {
        self.context = Some(context);
        self
    }
}